|
| 1 | +#!/usr/bin/env -S npm run tsn -T |
| 2 | + |
| 3 | +import { BrowserUse } from 'browser-use-sdk'; |
| 4 | + |
| 5 | +import { env } from './utils'; |
| 6 | +import z from 'zod'; |
| 7 | + |
| 8 | +env(); |
| 9 | + |
| 10 | +async function basic() { |
| 11 | + // gets API Key from environment variable BROWSER_USE_API_KEY |
| 12 | + const browseruse = new BrowserUse(); |
| 13 | + |
| 14 | + console.log('Basic: Creating task and starting stream...'); |
| 15 | + |
| 16 | + // Create a task and get the stream |
| 17 | + const task = await browseruse.tasks.create({ |
| 18 | + task: 'What is the weather in San Francisco?', |
| 19 | + agentSettings: { llm: 'gemini-2.5-flash' }, |
| 20 | + }); |
| 21 | + |
| 22 | + const gen = browseruse.tasks.stream(task.id); |
| 23 | + |
| 24 | + for await (const msg of gen) { |
| 25 | + console.log( |
| 26 | + `Basic: ${msg.data.status} ${msg.data.session.liveUrl} ${msg.data.steps[msg.data.steps.length - 1]?.nextGoal}`, |
| 27 | + ); |
| 28 | + |
| 29 | + if (msg.data.status === 'finished') { |
| 30 | + console.log(`Basic: ${msg.data.doneOutput}`); |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + console.log('\nBasic: Stream completed'); |
| 35 | +} |
| 36 | + |
| 37 | +const HackerNewsResponse = z.object({ |
| 38 | + title: z.string(), |
| 39 | + url: z.string(), |
| 40 | + score: z.number(), |
| 41 | +}); |
| 42 | + |
| 43 | +const TaskOutput = z.object({ |
| 44 | + posts: z.array(HackerNewsResponse), |
| 45 | +}); |
| 46 | + |
| 47 | +async function structured() { |
| 48 | + // gets API Key from environment variable BROWSER_USE_API_KEY |
| 49 | + const browseruse = new BrowserUse(); |
| 50 | + |
| 51 | + console.log('Structured: Creating task and starting stream...\n'); |
| 52 | + |
| 53 | + // Create a task and get the stream |
| 54 | + const task = await browseruse.tasks.create({ |
| 55 | + task: 'Extract top 10 Hacker News posts and return the title, url, and score', |
| 56 | + schema: TaskOutput, |
| 57 | + agentSettings: { llm: 'gpt-4.1' }, |
| 58 | + }); |
| 59 | + |
| 60 | + const stream = browseruse.tasks.stream({ |
| 61 | + taskId: task.id, |
| 62 | + schema: TaskOutput, |
| 63 | + }); |
| 64 | + |
| 65 | + for await (const msg of stream) { |
| 66 | + // Regular |
| 67 | + process.stdout.write(`Structured: ${msg.data.status}`); |
| 68 | + if (msg.data.session.liveUrl) { |
| 69 | + process.stdout.write(` | Live URL: ${msg.data.session.liveUrl}`); |
| 70 | + } |
| 71 | + |
| 72 | + if (msg.data.steps.length > 0) { |
| 73 | + const latestStep = msg.data.steps[msg.data.steps.length - 1]; |
| 74 | + process.stdout.write(` | ${latestStep!.nextGoal}`); |
| 75 | + } |
| 76 | + |
| 77 | + process.stdout.write('\n'); |
| 78 | + |
| 79 | + // Output |
| 80 | + if (msg.data.status === 'finished') { |
| 81 | + process.stdout.write(`\n\nOUTPUT:`); |
| 82 | + |
| 83 | + for (const post of msg.data.parsedOutput!.posts) { |
| 84 | + process.stdout.write(`\n - ${post.title} (${post.score}) ${post.url}`); |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + console.log('\nStructured: Stream completed'); |
| 90 | +} |
| 91 | + |
| 92 | +basic() |
| 93 | + .then(() => structured()) |
| 94 | + .catch(console.error); |
0 commit comments