Skip to content

Commit 3f3ee17

Browse files
authored
Merge pull request #19 from browser-use/feat/run
feat: Add `run` Method, Change `create` Params
2 parents ce135a3 + fef5379 commit 3f3ee17

File tree

6 files changed

+114
-5
lines changed

6 files changed

+114
-5
lines changed

examples/run-zod.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#!/usr/bin/env -S npm run tsn -T
2+
3+
import { BrowserUse } from 'browser-use-sdk';
4+
5+
import z from 'zod';
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+
const HackerNewsResponse = z.object({
14+
title: z.string(),
15+
url: z.string(),
16+
score: z.number(),
17+
});
18+
19+
const TaskOutput = z.object({
20+
posts: z.array(HackerNewsResponse),
21+
});
22+
23+
async function main() {
24+
console.log(`Creating Task...`);
25+
26+
// Create Task
27+
const rsp = await browseruse.tasks.run({
28+
task: 'Search for the top 10 Hacker News posts and return the title, url, and score',
29+
schema: TaskOutput,
30+
});
31+
32+
const posts = rsp.doneOutput?.posts;
33+
34+
if (posts == null) {
35+
throw new Error('No posts found');
36+
}
37+
38+
for (const post of posts) {
39+
console.log(`${post.title} (${post.score}) - ${post.url}`);
40+
}
41+
42+
console.log(`\nFound ${posts.length} posts`);
43+
}
44+
45+
main().catch(console.error);

examples/run.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/usr/bin/env -S npm run tsn -T
2+
3+
import { BrowserUse } from 'browser-use-sdk';
4+
5+
import { env } from './utils';
6+
7+
env();
8+
9+
// gets API Key from environment variable BROWSER_USE_API_KEY
10+
const browseruse = new BrowserUse();
11+
12+
async function main() {
13+
console.log(`Creating Task...`);
14+
15+
// Create Task
16+
const rsp = await browseruse.tasks.run({
17+
task: "What's the weather line in SF and what's the temperature?",
18+
});
19+
20+
console.log(rsp.doneOutput);
21+
}
22+
23+
main().catch(console.error);

examples/stream-zod.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ async function main() {
2626
// Create a task and get the stream
2727
const task = await browseruse.tasks.create({
2828
task: 'Extract top 10 Hacker News posts and return the title, url, and score',
29-
structuredOutputJson: TaskOutput,
29+
schema: TaskOutput,
3030
});
3131

3232
const stream = browseruse.tasks.stream({

examples/zod.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ async function main() {
2828
// Create Task
2929
const rsp = await browseruse.tasks.create({
3030
task: 'Extract top 10 Hacker News posts and return the title, url, and score',
31-
structuredOutputJson: TaskOutput,
31+
schema: TaskOutput,
3232
});
3333

3434
poll: do {

src/lib/parse.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import type { TaskCreateParams, TaskView } from '../resources/tasks';
44
// RUN
55

66
export type TaskCreateParamsWithSchema<T extends ZodType> = Omit<TaskCreateParams, 'structuredOutputJson'> & {
7-
structuredOutputJson: T;
7+
schema: T;
88
};
99

1010
export function stringifyStructuredOutput<T extends ZodType>(schema: T): string {

src/resources/tasks.ts

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ export class Tasks extends APIResource {
7070
body: TaskCreateParams | TaskCreateParamsWithSchema<ZodType>,
7171
options?: RequestOptions,
7272
): APIPromise<TaskCreateResponse> {
73-
if (body.structuredOutputJson != null && typeof body.structuredOutputJson === 'object') {
74-
const schema = body.structuredOutputJson;
73+
if ('schema' in body && body.schema != null && typeof body.schema === 'object') {
74+
const schema = body.schema;
7575

7676
const _body: TaskCreateParams = {
7777
...body,
@@ -190,6 +190,47 @@ export class Tasks extends APIResource {
190190
}
191191
}
192192

193+
/**
194+
* Create and run an agent task.
195+
*
196+
* @returns The output of the task.
197+
*/
198+
run<T extends ZodType>(
199+
body: TaskCreateParamsWithSchema<T>,
200+
options?: RequestOptions,
201+
): APIPromise<TaskViewWithSchema<T>>;
202+
run(body: TaskCreateParams, options?: RequestOptions): APIPromise<TaskView>;
203+
run(
204+
body: TaskCreateParams | TaskCreateParamsWithSchema<ZodType>,
205+
options?: RequestOptions,
206+
): APIPromise<unknown> {
207+
if ('schema' in body && body.schema != null && typeof body.schema === 'object') {
208+
return this.create(body, options)._thenUnwrap(async (data) => {
209+
const taskId = data.id;
210+
211+
for await (const msg of this.stream<ZodType>({ taskId, schema: body.schema }, options)) {
212+
if (msg.data.status === 'finished') {
213+
return msg.data;
214+
}
215+
}
216+
217+
throw new Error('Task did not finish');
218+
});
219+
}
220+
221+
return this.create(body, options)._thenUnwrap(async (data) => {
222+
const taskId = data.id;
223+
224+
for await (const msg of this.stream(taskId, options)) {
225+
if (msg.data.status === 'finished') {
226+
return msg.data;
227+
}
228+
}
229+
230+
throw new Error('Task did not finish');
231+
});
232+
}
233+
193234
/**
194235
* Control the execution of an AI agent task.
195236
*

0 commit comments

Comments
 (0)