|
| 1 | +import { Stagehand } from '@browserbasehq/stagehand'; |
| 2 | +import 'dotenv/config'; |
| 3 | + |
| 4 | +type JsonObject = Record<string, unknown>; |
| 5 | + |
| 6 | +interface SatoshiEnvelope { |
| 7 | + data?: JsonObject; |
| 8 | + meta?: JsonObject; |
| 9 | + error?: unknown; |
| 10 | +} |
| 11 | + |
| 12 | +function getString(value: unknown): string | undefined { |
| 13 | + return typeof value === 'string' && value.trim().length > 0 |
| 14 | + ? value |
| 15 | + : undefined; |
| 16 | +} |
| 17 | + |
| 18 | +function getNumber(value: unknown): number | undefined { |
| 19 | + return typeof value === 'number' && Number.isFinite(value) |
| 20 | + ? value |
| 21 | + : undefined; |
| 22 | +} |
| 23 | + |
| 24 | +function getObject(value: unknown): Record<string, unknown> | undefined { |
| 25 | + return value && typeof value === 'object' && !Array.isArray(value) |
| 26 | + ? (value as Record<string, unknown>) |
| 27 | + : undefined; |
| 28 | +} |
| 29 | + |
| 30 | +function getEstimate( |
| 31 | + data: Record<string, unknown>, |
| 32 | + keys: string[] |
| 33 | +): number | undefined { |
| 34 | + const estimates = getObject(data.estimates); |
| 35 | + for (const key of keys) { |
| 36 | + const estimate = estimates ? getNumber(estimates[key]) : undefined; |
| 37 | + if (estimate !== undefined) { |
| 38 | + return estimate; |
| 39 | + } |
| 40 | + |
| 41 | + const topLevel = getNumber(data[key]); |
| 42 | + if (topLevel !== undefined) { |
| 43 | + return topLevel; |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + return undefined; |
| 48 | +} |
| 49 | + |
| 50 | +function summarizeFeeDecision(data: JsonObject): string { |
| 51 | + const action = |
| 52 | + getString(data.action) ?? |
| 53 | + getString(data.recommendation) ?? |
| 54 | + getString(data.decision) ?? |
| 55 | + 'inspect_fee_context'; |
| 56 | + |
| 57 | + const summary = |
| 58 | + getString(data.summary) ?? |
| 59 | + getString(data.message) ?? |
| 60 | + getString(data.reason) ?? |
| 61 | + 'Satoshi API returned live Bitcoin fee context for the browser agent.'; |
| 62 | + |
| 63 | + const nextBlockFee = getEstimate(data, [ |
| 64 | + '1', |
| 65 | + 'high', |
| 66 | + 'fastestFee', |
| 67 | + 'fastest_fee', |
| 68 | + 'nextBlockFee', |
| 69 | + 'next_block_fee_sat_vb', |
| 70 | + ]); |
| 71 | + |
| 72 | + const feeLine = |
| 73 | + nextBlockFee === undefined |
| 74 | + ? '' |
| 75 | + : ` Next-block fee baseline: ${nextBlockFee} sat/vB.`; |
| 76 | + |
| 77 | + return `${action}: ${summary}${feeLine}`; |
| 78 | +} |
| 79 | + |
| 80 | +async function fetchSatoshiFees(): Promise<SatoshiEnvelope> { |
| 81 | + const baseUrl = process.env.SATOSHI_API_URL ?? 'https://bitcoinsapi.com'; |
| 82 | + const headers: Record<string, string> = { Accept: 'application/json' }; |
| 83 | + |
| 84 | + if (process.env.SATOSHI_API_KEY) { |
| 85 | + headers['X-API-Key'] = process.env.SATOSHI_API_KEY; |
| 86 | + } |
| 87 | + |
| 88 | + const response = await fetch( |
| 89 | + `${baseUrl.replace(/\/$/, '')}/api/v1/fees/recommended`, |
| 90 | + { headers } |
| 91 | + ); |
| 92 | + |
| 93 | + if (!response.ok) { |
| 94 | + throw new Error( |
| 95 | + `Satoshi API returned HTTP ${response.status}: ${await response.text()}` |
| 96 | + ); |
| 97 | + } |
| 98 | + |
| 99 | + const payload = (await response.json()) as SatoshiEnvelope; |
| 100 | + if (!payload.data || typeof payload.data !== 'object') { |
| 101 | + throw new Error('Satoshi API response did not include a data object.'); |
| 102 | + } |
| 103 | + |
| 104 | + return payload; |
| 105 | +} |
| 106 | + |
| 107 | +async function main() { |
| 108 | + const feePayload = await fetchSatoshiFees(); |
| 109 | + const feeDecision = summarizeFeeDecision(feePayload.data ?? {}); |
| 110 | + |
| 111 | + const stagehand = new Stagehand({ |
| 112 | + env: 'BROWSERBASE', |
| 113 | + model: process.env.BROWSERBASE_MODEL ?? 'google/gemini-3-flash-preview', |
| 114 | + }); |
| 115 | + |
| 116 | + await stagehand.init(); |
| 117 | + |
| 118 | + try { |
| 119 | + const page = stagehand.context.pages()[0]; |
| 120 | + if (!page) { |
| 121 | + throw new Error('Browserbase session did not create an initial page.'); |
| 122 | + } |
| 123 | + |
| 124 | + await page.goto('https://mempool.space/', { |
| 125 | + waitUntil: 'domcontentloaded', |
| 126 | + }); |
| 127 | + |
| 128 | + console.log('Satoshi API fee decision'); |
| 129 | + console.log(`- ${feeDecision}`); |
| 130 | + console.log(''); |
| 131 | + console.log('Browserbase session'); |
| 132 | + console.log(`- session_id: ${stagehand.browserbaseSessionID ?? 'unknown'}`); |
| 133 | + console.log(`- page_title: ${await page.title()}`); |
| 134 | + } finally { |
| 135 | + await stagehand.close(); |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +main().catch(error => { |
| 140 | + console.error(error); |
| 141 | + process.exitCode = 1; |
| 142 | +}); |
0 commit comments