Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions examples/run-zod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/usr/bin/env -S npm run tsn -T

import { BrowserUse } from 'browser-use-sdk';

import z from 'zod';
import { env } from './utils';

env();

// gets API Key from environment variable BROWSER_USE_API_KEY
const browseruse = new BrowserUse();

const HackerNewsResponse = z.object({
title: z.string(),
url: z.string(),
score: z.number(),
});

const TaskOutput = z.object({
posts: z.array(HackerNewsResponse),
});

async function main() {
console.log(`Creating Task...`);

// Create Task
const rsp = await browseruse.tasks.run({
task: 'Search for the top 10 Hacker News posts and return the title, url, and score',
schema: TaskOutput,
});

const posts = rsp.doneOutput?.posts;

if (posts == null) {
throw new Error('No posts found');
}

for (const post of posts) {
console.log(`${post.title} (${post.score}) - ${post.url}`);
}

console.log(`\nFound ${posts.length} posts`);
}

main().catch(console.error);
23 changes: 23 additions & 0 deletions examples/run.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/usr/bin/env -S npm run tsn -T

import { BrowserUse } from 'browser-use-sdk';

import { env } from './utils';

env();

// gets API Key from environment variable BROWSER_USE_API_KEY
const browseruse = new BrowserUse();

async function main() {
console.log(`Creating Task...`);

// Create Task
const rsp = await browseruse.tasks.run({
task: "What's the weather line in SF and what's the temperature?",
});

console.log(rsp.doneOutput);
}

main().catch(console.error);
2 changes: 1 addition & 1 deletion examples/stream-zod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ async function main() {
// Create a task and get the stream
const task = await browseruse.tasks.create({
task: 'Extract top 10 Hacker News posts and return the title, url, and score',
structuredOutputJson: TaskOutput,
schema: TaskOutput,
});

const stream = browseruse.tasks.stream({
Expand Down
2 changes: 1 addition & 1 deletion examples/zod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ async function main() {
// Create Task
const rsp = await browseruse.tasks.create({
task: 'Extract top 10 Hacker News posts and return the title, url, and score',
structuredOutputJson: TaskOutput,
schema: TaskOutput,
});

poll: do {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { TaskCreateParams, TaskView } from '../resources/tasks';
// RUN

export type TaskCreateParamsWithSchema<T extends ZodType> = Omit<TaskCreateParams, 'structuredOutputJson'> & {
structuredOutputJson: T;
schema: T;
};

export function stringifyStructuredOutput<T extends ZodType>(schema: T): string {
Expand Down
45 changes: 43 additions & 2 deletions src/resources/tasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ export class Tasks extends APIResource {
body: TaskCreateParams | TaskCreateParamsWithSchema<ZodType>,
options?: RequestOptions,
): APIPromise<TaskCreateResponse> {
if (body.structuredOutputJson != null && typeof body.structuredOutputJson === 'object') {
const schema = body.structuredOutputJson;
if ('schema' in body && body.schema != null && typeof body.schema === 'object') {
const schema = body.schema;

const _body: TaskCreateParams = {
...body,
Expand Down Expand Up @@ -190,6 +190,47 @@ export class Tasks extends APIResource {
}
}

/**
* Create and run an agent task.
*
* @returns The output of the task.
*/
run<T extends ZodType>(
body: TaskCreateParamsWithSchema<T>,
options?: RequestOptions,
): APIPromise<TaskViewWithSchema<T>>;
run(body: TaskCreateParams, options?: RequestOptions): APIPromise<TaskView>;
run(
body: TaskCreateParams | TaskCreateParamsWithSchema<ZodType>,
options?: RequestOptions,
): APIPromise<unknown> {
if ('schema' in body && body.schema != null && typeof body.schema === 'object') {
return this.create(body, options)._thenUnwrap(async (data) => {
const taskId = data.id;

for await (const msg of this.stream<ZodType>({ taskId, schema: body.schema }, options)) {
if (msg.data.status === 'finished') {
return msg.data;
}
}

throw new Error('Task did not finish');
});
}

return this.create(body, options)._thenUnwrap(async (data) => {
const taskId = data.id;

for await (const msg of this.stream(taskId, options)) {
if (msg.data.status === 'finished') {
return msg.data;
}
}

throw new Error('Task did not finish');
});
}

/**
* Control the execution of an AI agent task.
*
Expand Down