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