diff --git a/examples/run-zod.ts b/examples/run-zod.ts new file mode 100755 index 0000000..a987d09 --- /dev/null +++ b/examples/run-zod.ts @@ -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); diff --git a/examples/run.ts b/examples/run.ts new file mode 100755 index 0000000..0f4fcb4 --- /dev/null +++ b/examples/run.ts @@ -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); diff --git a/examples/stream-zod.ts b/examples/stream-zod.ts index fa283ca..8989bd7 100755 --- a/examples/stream-zod.ts +++ b/examples/stream-zod.ts @@ -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({ diff --git a/examples/zod.ts b/examples/zod.ts index eb5dbf7..dec48c3 100755 --- a/examples/zod.ts +++ b/examples/zod.ts @@ -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 { diff --git a/src/lib/parse.ts b/src/lib/parse.ts index 8c651b8..f235edf 100644 --- a/src/lib/parse.ts +++ b/src/lib/parse.ts @@ -4,7 +4,7 @@ import type { TaskCreateParams, TaskView } from '../resources/tasks'; // RUN export type TaskCreateParamsWithSchema = Omit & { - structuredOutputJson: T; + schema: T; }; export function stringifyStructuredOutput(schema: T): string { diff --git a/src/resources/tasks.ts b/src/resources/tasks.ts index a32eb9e..398e28c 100644 --- a/src/resources/tasks.ts +++ b/src/resources/tasks.ts @@ -70,8 +70,8 @@ export class Tasks extends APIResource { body: TaskCreateParams | TaskCreateParamsWithSchema, options?: RequestOptions, ): APIPromise { - 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, @@ -190,6 +190,47 @@ export class Tasks extends APIResource { } } + /** + * Create and run an agent task. + * + * @returns The output of the task. + */ + run( + body: TaskCreateParamsWithSchema, + options?: RequestOptions, + ): APIPromise>; + run(body: TaskCreateParams, options?: RequestOptions): APIPromise; + run( + body: TaskCreateParams | TaskCreateParamsWithSchema, + options?: RequestOptions, + ): APIPromise { + 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({ 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. *