Skip to content

Commit fea92a0

Browse files
committed
stash
1 parent abdce8a commit fea92a0

File tree

16 files changed

+256
-872
lines changed

16 files changed

+256
-872
lines changed

.vscode/settings.json

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,26 @@
1-
{}
1+
{
2+
"editor.indentSize": 4,
3+
"editor.tabSize": 4,
4+
"editor.insertSpaces": true,
5+
"editor.detectIndentation": false,
6+
"[typescript]": {
7+
"editor.tabSize": 4,
8+
"editor.insertSpaces": true
9+
},
10+
"[javascript]": {
11+
"editor.tabSize": 4,
12+
"editor.insertSpaces": true
13+
},
14+
"[json]": {
15+
"editor.tabSize": 4,
16+
"editor.insertSpaces": true
17+
},
18+
"[markdown]": {
19+
"editor.tabSize": 4,
20+
"editor.insertSpaces": true
21+
},
22+
"[yaml]": {
23+
"editor.tabSize": 4,
24+
"editor.insertSpaces": true
25+
}
26+
}

examples/retrieve.ts

Lines changed: 82 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,106 +1,113 @@
11
#!/usr/bin/env -S npm run tsn -T
22

3-
import { BrowserUse } from 'browser-use-sdk';
3+
import { BrowserUseClient } from "browser-use-sdk";
44

5-
import { env, spinner } from './utils';
6-
import z from 'zod';
5+
import { env, spinner } from "./utils";
6+
import z from "zod";
77

88
env();
99

1010
// gets API Key from environment variable BROWSER_USE_API_KEY
11-
const browseruse = new BrowserUse();
11+
const browseruse = new BrowserUseClient({
12+
apiKey: process.env.BROWSER_USE_API_KEY!,
13+
environment: "production",
14+
});
15+
16+
// Basic ---------------------------------------------------------------------
1217

1318
async function basic() {
14-
let log = 'starting';
15-
const stop = spinner(() => log);
16-
17-
// Create Task
18-
const rsp = await browseruse.tasks.create({
19-
task: "What's the weather line in SF and what's the temperature?",
20-
agentSettings: { llm: 'gemini-2.5-flash' },
21-
});
22-
23-
poll: do {
24-
// Wait for Task to Finish
25-
const status = await browseruse.tasks.retrieve(rsp.id);
26-
27-
switch (status.status) {
28-
case 'started':
29-
case 'paused':
30-
case 'stopped':
31-
log = `agent ${status.status} - live: ${status.session.liveUrl}`;
32-
33-
await new Promise((resolve) => setTimeout(resolve, 2000));
34-
break;
35-
36-
case 'finished':
37-
stop();
38-
39-
console.log(status.doneOutput);
40-
break poll;
41-
}
42-
} while (true);
19+
let log = "starting";
20+
const stop = spinner(() => log);
21+
22+
// Create Task
23+
const rsp = await browseruse.tasks.createTask({
24+
task: "What's the weather line in SF and what's the temperature?",
25+
agent: { llm: "gemini-2.5-flash" },
26+
});
27+
28+
poll: do {
29+
// Wait for Task to Finish
30+
const status = await browseruse.tasks.getTask(rsp.id);
31+
32+
switch (status.status) {
33+
case "started":
34+
case "paused":
35+
case "stopped":
36+
log = `agent ${status.status} - live: ${status.session.liveUrl}`;
37+
38+
await new Promise((resolve) => setTimeout(resolve, 2000));
39+
break;
40+
41+
case "finished":
42+
stop();
43+
44+
console.log(status.output);
45+
break poll;
46+
}
47+
} while (true);
4348
}
4449

50+
// Structured ----------------------------------------------------------------
51+
4552
// Define Structured Output Schema
4653
const HackerNewsResponse = z.object({
47-
title: z.string(),
48-
url: z.string(),
49-
score: z.number(),
54+
title: z.string(),
55+
url: z.string(),
56+
score: z.number(),
5057
});
5158

5259
const TaskOutput = z.object({
53-
posts: z.array(HackerNewsResponse),
60+
posts: z.array(HackerNewsResponse),
5461
});
5562

5663
async function structured() {
57-
let log = 'starting';
58-
const stop = spinner(() => log);
59-
60-
// Create Task
61-
const rsp = await browseruse.tasks.create({
62-
task: 'Extract top 10 Hacker News posts and return the title, url, and score',
63-
schema: TaskOutput,
64-
agentSettings: { llm: 'gpt-4.1' },
65-
});
66-
67-
poll: do {
68-
// Wait for Task to Finish
69-
const status = await browseruse.tasks.retrieve({
70-
taskId: rsp.id,
71-
schema: TaskOutput,
64+
let log = "starting";
65+
const stop = spinner(() => log);
66+
67+
// Create Task
68+
const rsp = await browseruse.tasks.createTask({
69+
task: "Extract top 10 Hacker News posts and return the title, url, and score",
70+
schema: TaskOutput,
71+
agent: { llm: "gpt-4.1" },
7272
});
7373

74-
switch (status.status) {
75-
case 'started':
76-
case 'paused':
77-
case 'stopped': {
78-
log = `agent ${status.status} ${status.session.liveUrl} | ${status.steps.length} steps`;
74+
poll: do {
75+
// Wait for Task to Finish
76+
const status = await browseruse.tasks.getTask({
77+
taskId: rsp.id,
78+
schema: TaskOutput,
79+
});
7980

80-
await new Promise((resolve) => setTimeout(resolve, 2000));
81+
switch (status.status) {
82+
case "started":
83+
case "paused":
84+
case "stopped": {
85+
log = `agent ${status.status} ${status.session.liveUrl} | ${status.steps.length} steps`;
8186

82-
break;
83-
}
87+
await new Promise((resolve) => setTimeout(resolve, 2000));
8488

85-
case 'finished':
86-
if (status.parsedOutput == null) {
87-
throw new Error('No output');
88-
}
89+
break;
90+
}
8991

90-
stop();
92+
case "finished":
93+
if (status.parsed == null) {
94+
throw new Error("No output");
95+
}
9196

92-
// Print Structured Output
93-
console.log('Top Hacker News Posts:');
97+
stop();
9498

95-
for (const post of status.parsedOutput.posts) {
96-
console.log(` - ${post.title} (${post.score}) ${post.url}`);
97-
}
99+
// Print Structured Output
100+
console.log("Top Hacker News Posts:");
101+
102+
for (const post of status.parsed.posts) {
103+
console.log(` - ${post.title} (${post.score}) ${post.url}`);
104+
}
98105

99-
break poll;
100-
}
101-
} while (true);
106+
break poll;
107+
}
108+
} while (true);
102109
}
103110

104111
basic()
105-
.then(() => structured())
106-
.catch(console.error);
112+
.then(() => structured())
113+
.catch(console.error);

examples/run.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ const browseruse = new BrowserUseClient({
1313
environment: "production",
1414
});
1515

16+
// Basic ---------------------------------------------------------------------
17+
1618
async function basic() {
1719
console.log(`Basic: Running Task...`);
1820

@@ -28,6 +30,8 @@ async function basic() {
2830
console.log(`Basic: DONE`);
2931
}
3032

33+
// Structured ----------------------------------------------------------------
34+
3135
const HackerNewsResponse = z.object({
3236
title: z.string(),
3337
url: z.string(),
@@ -44,7 +48,7 @@ async function structured() {
4448
const rsp = await browseruse.tasks.createTask({
4549
task: "Search for the top 10 Hacker News posts and return the title and url!",
4650
schema: TaskOutput,
47-
agentSettings: { llm: "gpt-4.1" },
51+
agent: { llm: "gpt-4.1" },
4852
});
4953

5054
const result = await rsp.complete();

0 commit comments

Comments
 (0)