Skip to content

Commit ecbd215

Browse files
committed
comm
1 parent d1c5317 commit ecbd215

File tree

24 files changed

+1219
-0
lines changed

24 files changed

+1219
-0
lines changed

.env

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
BROWSER_USE_API_KEY="bu_NnbmRimOkIVSCKaayzv4w_AAm-fsI1xinuLZr_wh71g"

.fernignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
# Specify files that shouldn't be modified by Fern
2+
src/CustomClient.ts

.vscode/settings.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}

examples/.env

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
BROWSER_USE_API_KEY="bu_NnbmRimOkIVSCKaayzv4w_AAm-fsI1xinuLZr_wh71g"
2+
SECRET_KEY="secret"

examples/.env.example

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# ----------------------------------------------------------------------------
2+
#
3+
# :GUIDE:
4+
#
5+
# Copy `.env.example` to `.env` to run all examples without any
6+
# additional setup. You can also manually export variables
7+
# and run each example separately.
8+
#
9+
# ----------------------------------------------------------------------------
10+
11+
# API ------------------------------------------------------------------------
12+
13+
# Browser Use API Key
14+
BROWSER_USE_API_KEY=""
15+
16+
# Webhooks -------------------------------------------------------------------
17+
18+
# NOTE: Use something simple in development. In production, Browser Use Cloud
19+
# will give you the production secret.
20+
SECRET_KEY="secret"
21+
22+
# ----------------------------------------------------------------------------

examples/.keep

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
File generated from our OpenAPI spec by Stainless.
2+
3+
This directory can be used to store example files demonstrating usage of this SDK.
4+
It is ignored by Stainless code generation and its content (other than this keep file) won't be touched.

examples/retrieve.ts

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
#!/usr/bin/env -S npm run tsn -T
2+
3+
import { BrowserUse } from 'browser-use-sdk';
4+
5+
import { env, spinner } from './utils';
6+
import z from 'zod';
7+
8+
env();
9+
10+
// gets API Key from environment variable BROWSER_USE_API_KEY
11+
const browseruse = new BrowserUse();
12+
13+
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);
43+
}
44+
45+
// Define Structured Output Schema
46+
const HackerNewsResponse = z.object({
47+
title: z.string(),
48+
url: z.string(),
49+
score: z.number(),
50+
});
51+
52+
const TaskOutput = z.object({
53+
posts: z.array(HackerNewsResponse),
54+
});
55+
56+
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,
72+
});
73+
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`;
79+
80+
await new Promise((resolve) => setTimeout(resolve, 2000));
81+
82+
break;
83+
}
84+
85+
case 'finished':
86+
if (status.parsedOutput == null) {
87+
throw new Error('No output');
88+
}
89+
90+
stop();
91+
92+
// Print Structured Output
93+
console.log('Top Hacker News Posts:');
94+
95+
for (const post of status.parsedOutput.posts) {
96+
console.log(` - ${post.title} (${post.score}) ${post.url}`);
97+
}
98+
99+
break poll;
100+
}
101+
} while (true);
102+
}
103+
104+
basic()
105+
.then(() => structured())
106+
.catch(console.error);

examples/run.ts

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#!/usr/bin/env -S npm run tsn -T
2+
3+
import { BrowserUse } from 'browser-use-sdk';
4+
import { z } from 'zod';
5+
6+
import { env } from './utils';
7+
8+
env();
9+
10+
// gets API Key from environment variable BROWSER_USE_API_KEY
11+
const browseruse = new BrowserUse();
12+
13+
async function basic() {
14+
console.log(`Basic: Running Task...`);
15+
16+
// Create Task
17+
const rsp = await browseruse.tasks.run({
18+
task: "What's the weather line in SF and what's the temperature?",
19+
agentSettings: { llm: 'gemini-2.5-flash' },
20+
});
21+
22+
console.log(`Basic: ${rsp.doneOutput}`);
23+
24+
console.log(`Basic: DONE`);
25+
}
26+
27+
const HackerNewsResponse = z.object({
28+
title: z.string(),
29+
url: z.string(),
30+
});
31+
32+
const TaskOutput = z.object({
33+
posts: z.array(HackerNewsResponse),
34+
});
35+
36+
async function structured() {
37+
console.log(`Structured: Running Task...`);
38+
39+
// Create Task
40+
const rsp = await browseruse.tasks.run({
41+
task: 'Search for the top 10 Hacker News posts and return the title and url!',
42+
schema: TaskOutput,
43+
agentSettings: { llm: 'gpt-4.1' },
44+
});
45+
46+
const posts = rsp.parsedOutput?.posts;
47+
48+
if (posts == null) {
49+
throw new Error('Structured: No posts found');
50+
}
51+
52+
console.log(`Structured: Top Hacker News posts:`);
53+
54+
for (const post of posts) {
55+
console.log(` - ${post.title} - ${post.url}`);
56+
}
57+
58+
console.log(`\nStructured: DONE`);
59+
}
60+
61+
basic()
62+
.then(() => structured())
63+
.catch(console.error);

examples/stream.ts

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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);

examples/utils.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import dotenv from '@dotenvx/dotenvx';
2+
3+
const SPINNER_FRAMES = ['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏'];
4+
5+
/**
6+
* Start a spinner that updates the text every 100ms.
7+
*
8+
* @param renderText - A function that returns the text to display.
9+
* @returns A function to stop the spinner.
10+
*/
11+
export function spinner(renderText: () => string): () => void {
12+
let frameIndex = 0;
13+
const interval = setInterval(() => {
14+
const frame = SPINNER_FRAMES[frameIndex++ % SPINNER_FRAMES.length];
15+
const text = `${frame} ${renderText()}`;
16+
if (typeof process.stdout.clearLine === 'function') {
17+
process.stdout.clearLine(0);
18+
process.stdout.cursorTo(0);
19+
}
20+
process.stdout.write(text);
21+
}, 100);
22+
23+
return () => {
24+
clearInterval(interval);
25+
if (typeof process.stdout.clearLine === 'function') {
26+
process.stdout.clearLine(0);
27+
process.stdout.cursorTo(0);
28+
}
29+
};
30+
}
31+
32+
export function env() {
33+
dotenv.config({ path: [__dirname + '/.env', '.env'] });
34+
}

0 commit comments

Comments
 (0)