3
3
import z from "zod" ;
4
4
5
5
import { BrowserUseClient } from ".." ;
6
+
6
7
import { env } from "./utils" ;
7
8
8
9
env ( ) ;
9
10
11
+ const browseruse = new BrowserUseClient ( {
12
+ apiKey : process . env . BROWSER_USE_API_KEY ! ,
13
+ environment : "https://api.browser-use.com/api/v2" ,
14
+ } ) ;
15
+
10
16
// Basic ---------------------------------------------------------------------
11
17
12
18
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
19
console . log ( "Basic: Creating task and starting stream..." ) ;
20
20
21
21
// Create a task and get the stream
22
22
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?" ,
24
24
agent : { llm : "gemini-2.5-flash" } ,
25
25
} ) ;
26
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
- }
27
+ for await ( const msg of task . stream ( ) ) {
28
+ console . log ( msg ) ;
37
29
}
38
30
31
+ const result = await task . complete ( ) ;
32
+ console . log ( result ) ;
33
+
39
34
console . log ( "\nBasic: Stream completed" ) ;
40
35
}
41
36
@@ -53,12 +48,6 @@ const TaskOutput = z.object({
53
48
} ) ;
54
49
55
50
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
51
console . log ( "Structured: Creating task and starting stream...\n" ) ;
63
52
64
53
// Create a task and get the stream
@@ -68,32 +57,13 @@ async function structured() {
68
57
agent : { llm : "gpt-4.1" } ,
69
58
} ) ;
70
59
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 ) ;
95
62
}
96
63
64
+ const result = await task . complete ( ) ;
65
+ console . log ( result ) ;
66
+
97
67
console . log ( "\nStructured: Stream completed" ) ;
98
68
}
99
69
0 commit comments