Skip to content

Commit 17bb9b4

Browse files
committed
Add Custom Utilities and Examples
1 parent 2ddd3ff commit 17bb9b4

File tree

17 files changed

+930
-13
lines changed

17 files changed

+930
-13
lines changed

.fernignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
11
# Specify files that shouldn't be modified by Fern
2+
src/wrapper/*
3+
src/examples/*
4+
examples/*
5+
.vscode/*

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
node_modules
22
.DS_Store
3-
/dist
3+
/dist
4+
.cursor
5+
6+
.env

.vscode/settings.json

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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/.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/retrieve.ts

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
#!/usr/bin/env -S npm run tsn -T
2+
3+
import z from "zod";
4+
5+
import { BrowserUseClient } from "..";
6+
7+
import { env, spinner } from "./utils";
8+
9+
env();
10+
11+
// gets API Key from environment variable BROWSER_USE_API_KEY
12+
const browseruse = new BrowserUseClient({
13+
apiKey: process.env.BROWSER_USE_API_KEY!,
14+
environment: "production",
15+
});
16+
17+
// Basic ---------------------------------------------------------------------
18+
19+
async function basic() {
20+
let log = "starting";
21+
const stop = spinner(() => log);
22+
23+
// Create Task
24+
const rsp = await browseruse.tasks.createTask({
25+
task: "What's the weather line in SF and what's the temperature?",
26+
agent: { llm: "gemini-2.5-flash" },
27+
});
28+
29+
poll: do {
30+
// Wait for Task to Finish
31+
const status = await browseruse.tasks.getTask(rsp.id);
32+
33+
switch (status.status) {
34+
case "started":
35+
case "paused":
36+
case "stopped":
37+
log = `agent ${status.status}`;
38+
39+
await new Promise((resolve) => setTimeout(resolve, 2000));
40+
break;
41+
42+
case "finished":
43+
stop();
44+
45+
console.log(status.output);
46+
break poll;
47+
}
48+
} while (true);
49+
}
50+
51+
// Structured ----------------------------------------------------------------
52+
53+
// Define Structured Output Schema
54+
const HackerNewsResponse = z.object({
55+
title: z.string(),
56+
url: z.string(),
57+
score: z.number(),
58+
});
59+
60+
const TaskOutput = z.object({
61+
posts: z.array(HackerNewsResponse),
62+
});
63+
64+
async function structured() {
65+
let log = "starting";
66+
const stop = spinner(() => log);
67+
68+
// Create Task
69+
const rsp = await browseruse.tasks.createTask({
70+
task: "Extract top 10 Hacker News posts and return the title, url, and score",
71+
schema: TaskOutput,
72+
agent: { llm: "gpt-4.1" },
73+
});
74+
75+
poll: do {
76+
// Wait for Task to Finish
77+
const status = await browseruse.tasks.getTask({
78+
taskId: rsp.id,
79+
schema: TaskOutput,
80+
});
81+
82+
switch (status.status) {
83+
case "started":
84+
case "paused":
85+
case "stopped": {
86+
log = `agent ${status.status} ${status.sessionId} | ${status.steps.length} steps`;
87+
88+
await new Promise((resolve) => setTimeout(resolve, 2000));
89+
90+
break;
91+
}
92+
93+
case "finished":
94+
if (status.parsed == null) {
95+
throw new Error("No output");
96+
}
97+
98+
stop();
99+
100+
// Print Structured Output
101+
console.log("Top Hacker News Posts:");
102+
103+
for (const post of status.parsed.posts) {
104+
console.log(` - ${post.title} (${post.score}) ${post.url}`);
105+
}
106+
107+
break poll;
108+
}
109+
} while (true);
110+
}
111+
112+
basic()
113+
.then(() => structured())
114+
.catch(console.error);

examples/run.ts

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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+
// gets API Key from environment variable BROWSER_USE_API_KEY
11+
const browseruse = new BrowserUseClient({
12+
apiKey: process.env.BROWSER_USE_API_KEY!,
13+
environment: "production",
14+
});
15+
16+
// Basic ---------------------------------------------------------------------
17+
18+
async function basic() {
19+
console.log(`Basic: Running Task...`);
20+
21+
// Create Task
22+
const rsp = await browseruse.tasks.createTask({
23+
task: "What's the weather line in SF and what's the temperature?",
24+
});
25+
26+
const result = await rsp.complete();
27+
28+
console.log(`Basic: ${result.output}`);
29+
30+
console.log(`Basic: DONE`);
31+
}
32+
33+
// Structured ----------------------------------------------------------------
34+
35+
const HackerNewsResponse = z.object({
36+
title: z.string(),
37+
url: z.string(),
38+
});
39+
40+
const TaskOutput = z.object({
41+
posts: z.array(HackerNewsResponse),
42+
});
43+
44+
async function structured() {
45+
console.log(`Structured: Running Task...`);
46+
47+
// Create Task
48+
const rsp = await browseruse.tasks.createTask({
49+
task: "Search for the top 10 Hacker News posts and return the title and url!",
50+
schema: TaskOutput,
51+
agent: { llm: "gpt-4.1" },
52+
});
53+
54+
const result = await rsp.complete();
55+
56+
const posts = result.parsed?.posts;
57+
58+
if (posts == null) {
59+
throw new Error("Structured: No posts found");
60+
}
61+
62+
console.log(`Structured: Top Hacker News posts:`);
63+
64+
for (const post of posts) {
65+
console.log(` - ${post.title} - ${post.url}`);
66+
}
67+
68+
console.log(`\nStructured: DONE`);
69+
}
70+
71+
basic()
72+
.then(() => structured())
73+
.catch(console.error);

examples/stream.ts

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

examples/tsconfig.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"extends": "../tsconfig.json",
3+
"compilerOptions": {
4+
"outDir": null,
5+
"rootDir": ".",
6+
"baseUrl": "."
7+
},
8+
"include": ["."],
9+
"exclude": []
10+
}

0 commit comments

Comments
 (0)