33import z from "zod" ;
44
55import { BrowserUseClient } from ".." ;
6+
67import { env } from "./utils" ;
78
89env ( ) ;
910
11+ const browseruse = new BrowserUseClient ( {
12+ apiKey : process . env . BROWSER_USE_API_KEY ! ,
13+ environment : "https://api.browser-use.com/api/v2" ,
14+ } ) ;
15+
1016// Basic ---------------------------------------------------------------------
1117
1218async 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-
1919 console . log ( "Basic: Creating task and starting stream..." ) ;
2020
2121 // Create a task and get the stream
2222 const task = await browseruse . tasks . createTask ( {
23- task : "What's the weather line in SF and what's the temperature?" ,
23+ task : "What's the weather in SF and what's the temperature?" ,
2424 agent : { llm : "gemini-2.5-flash" } ,
2525 } ) ;
2626
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- }
27+ for await ( const msg of task . stream ( ) ) {
28+ console . log ( msg ) ;
3729 }
3830
31+ const result = await task . complete ( ) ;
32+ console . log ( result ) ;
33+
3934 console . log ( "\nBasic: Stream completed" ) ;
4035}
4136
@@ -53,12 +48,6 @@ const TaskOutput = z.object({
5348} ) ;
5449
5550async 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-
6251 console . log ( "Structured: Creating task and starting stream...\n" ) ;
6352
6453 // Create a task and get the stream
@@ -68,32 +57,13 @@ async function structured() {
6857 agent : { llm : "gpt-4.1" } ,
6958 } ) ;
7059
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- }
60+ for await ( const msg of task . stream ( ) ) {
61+ console . log ( msg ) ;
9562 }
9663
64+ const result = await task . complete ( ) ;
65+ console . log ( result ) ;
66+
9767 console . log ( "\nStructured: Stream completed" ) ;
9868}
9969
0 commit comments