|
| 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 summarizeFeeDecision(data: JsonObject): string { |
| 25 | + const action = |
| 26 | + getString(data.action) ?? |
| 27 | + getString(data.recommendation) ?? |
| 28 | + getString(data.decision) ?? |
| 29 | + 'inspect_fee_context'; |
| 30 | + |
| 31 | + const summary = |
| 32 | + getString(data.summary) ?? |
| 33 | + getString(data.message) ?? |
| 34 | + getString(data.reason) ?? |
| 35 | + 'Satoshi API returned live Bitcoin fee context for the browser agent.'; |
| 36 | + |
| 37 | + const nextBlockFee = |
| 38 | + getNumber(data.next_block_fee_sat_vb) ?? |
| 39 | + getNumber(data.fastestFee) ?? |
| 40 | + getNumber(data.fastest_fee) ?? |
| 41 | + getNumber(data.nextBlockFee); |
| 42 | + |
| 43 | + const feeLine = |
| 44 | + nextBlockFee === undefined |
| 45 | + ? '' |
| 46 | + : ` Next-block fee baseline: ${nextBlockFee} sat/vB.`; |
| 47 | + |
| 48 | + return `${action}: ${summary}${feeLine}`; |
| 49 | +} |
| 50 | + |
| 51 | +async function fetchSatoshiFees(): Promise<SatoshiEnvelope> { |
| 52 | + const baseUrl = process.env.SATOSHI_API_URL ?? 'https://bitcoinsapi.com'; |
| 53 | + const headers: Record<string, string> = { Accept: 'application/json' }; |
| 54 | + |
| 55 | + if (process.env.SATOSHI_API_KEY) { |
| 56 | + headers['X-API-Key'] = process.env.SATOSHI_API_KEY; |
| 57 | + } |
| 58 | + |
| 59 | + const response = await fetch( |
| 60 | + `${baseUrl.replace(/\/$/, '')}/api/v1/fees/recommended`, |
| 61 | + { headers } |
| 62 | + ); |
| 63 | + |
| 64 | + if (!response.ok) { |
| 65 | + throw new Error( |
| 66 | + `Satoshi API returned HTTP ${response.status}: ${await response.text()}` |
| 67 | + ); |
| 68 | + } |
| 69 | + |
| 70 | + const payload = (await response.json()) as SatoshiEnvelope; |
| 71 | + if (!payload.data || typeof payload.data !== 'object') { |
| 72 | + throw new Error('Satoshi API response did not include a data object.'); |
| 73 | + } |
| 74 | + |
| 75 | + return payload; |
| 76 | +} |
| 77 | + |
| 78 | +async function main() { |
| 79 | + const feePayload = await fetchSatoshiFees(); |
| 80 | + const feeDecision = summarizeFeeDecision(feePayload.data ?? {}); |
| 81 | + |
| 82 | + const stagehand = new Stagehand({ |
| 83 | + env: 'BROWSERBASE', |
| 84 | + model: process.env.BROWSERBASE_MODEL ?? 'google/gemini-3-flash-preview', |
| 85 | + }); |
| 86 | + |
| 87 | + await stagehand.init(); |
| 88 | + |
| 89 | + try { |
| 90 | + const page = stagehand.context.pages()[0]; |
| 91 | + if (!page) { |
| 92 | + throw new Error('Browserbase session did not create an initial page.'); |
| 93 | + } |
| 94 | + |
| 95 | + await page.goto('https://mempool.space/', { |
| 96 | + waitUntil: 'domcontentloaded', |
| 97 | + }); |
| 98 | + |
| 99 | + console.log('Satoshi API fee decision'); |
| 100 | + console.log(`- ${feeDecision}`); |
| 101 | + console.log(''); |
| 102 | + console.log('Browserbase session'); |
| 103 | + console.log(`- session_id: ${stagehand.browserbaseSessionID ?? 'unknown'}`); |
| 104 | + console.log(`- page_title: ${await page.title()}`); |
| 105 | + } finally { |
| 106 | + await stagehand.close(); |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +main().catch(error => { |
| 111 | + console.error(error); |
| 112 | + process.exitCode = 1; |
| 113 | +}); |
0 commit comments