Skip to content

Commit e02bd07

Browse files
committed
improvements
1 parent 19685ea commit e02bd07

File tree

3 files changed

+17
-15
lines changed

3 files changed

+17
-15
lines changed

examples/structured-output.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,17 @@ const TaskOutput = z.object({
1818
});
1919

2020
async function main() {
21+
let log = 'starting';
22+
const stop = spinner(() => log);
23+
24+
// Create Task
2125
const rsp = await browseruse.tasks.createWithStructuredOutput({
2226
task: 'Extract top 10 Hacker News posts and return the title, url, and score',
2327
structuredOutputJson: TaskOutput,
2428
});
2529

26-
let latestStatusText = 'starting';
27-
28-
const stop = spinner(() => latestStatusText);
29-
3030
poll: do {
31+
// Wait for Task to Finish
3132
const status = await browseruse.tasks.retrieveWithStructuredOutput(rsp.id, {
3233
structuredOutputJson: TaskOutput,
3334
});
@@ -42,14 +43,15 @@ async function main() {
4243
const lastGoal = lastGoalDescription ? `, last: ${lastGoalDescription}` : '';
4344
const liveUrl = status.sessionLiveUrl ? `, live: ${status.sessionLiveUrl}` : '';
4445

45-
latestStatusText = `agent ${status.status} (${steps}${lastGoal}${liveUrl}) `;
46+
log = `agent ${status.status} (${steps}${lastGoal}${liveUrl}) `;
4647

4748
await new Promise((resolve) => setTimeout(resolve, 2000));
4849
}
4950

5051
case 'finished':
5152
stop();
5253

54+
// Print Structured Output
5355
console.log('TOP POSTS:');
5456

5557
for (const post of status.doneOutput!.posts) {

src/lib/parse.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
import z from 'zod';
1+
import z, { type ZodType } from 'zod';
22
import type { TaskCreateParams, TaskRetrieveParams, TaskView } from '../resources/tasks';
33

44
// RUN
55

6-
export type RunTaskCreateParamsWithStructuredOutput<T extends z.ZodTypeAny> = Omit<
6+
export type RunTaskCreateParamsWithStructuredOutput<T extends ZodType> = Omit<
77
TaskCreateParams,
88
'structuredOutputJson'
99
> & {
1010
structuredOutputJson: T;
1111
};
1212

13-
export function stringifyStructuredOutput<T extends z.ZodTypeAny>(
13+
export function stringifyStructuredOutput<T extends ZodType>(
1414
req: RunTaskCreateParamsWithStructuredOutput<T>,
1515
): TaskCreateParams {
1616
return {
@@ -21,19 +21,19 @@ export function stringifyStructuredOutput<T extends z.ZodTypeAny>(
2121

2222
// RETRIEVE
2323

24-
export type GetTaskStatusParamsWithStructuredOutput<T extends z.ZodTypeAny> = Omit<
24+
export type GetTaskStatusParamsWithStructuredOutput<T extends ZodType> = Omit<
2525
TaskRetrieveParams,
2626
'statusOnly'
2727
> & {
2828
statusOnly?: false;
2929
structuredOutputJson: T;
3030
};
3131

32-
export type TaskViewWithStructuredOutput<T extends z.ZodTypeAny> = Omit<TaskView, 'doneOutput'> & {
32+
export type TaskViewWithStructuredOutput<T extends ZodType> = Omit<TaskView, 'doneOutput'> & {
3333
doneOutput: z.output<T> | null;
3434
};
3535

36-
export function parseStructuredTaskOutput<T extends z.ZodTypeAny>(
36+
export function parseStructuredTaskOutput<T extends ZodType>(
3737
res: TaskView,
3838
body: GetTaskStatusParamsWithStructuredOutput<T>,
3939
): TaskViewWithStructuredOutput<T> {

src/resources/tasks.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
22

3-
import z from 'zod';
3+
import type { ZodType } from 'zod';
44

55
import { APIResource } from '../core/resource';
66
import * as TasksAPI from './tasks';
@@ -10,7 +10,7 @@ import { path } from '../internal/utils/path';
1010
import {
1111
parseStructuredTaskOutput,
1212
stringifyStructuredOutput,
13-
TaskViewWithStructuredOutput,
13+
type TaskViewWithStructuredOutput,
1414
type GetTaskStatusParamsWithStructuredOutput,
1515
type RunTaskCreateParamsWithStructuredOutput,
1616
} from '../lib/parse';
@@ -23,7 +23,7 @@ export class Tasks extends APIResource {
2323
return this._client.post('/tasks', { body, ...options });
2424
}
2525

26-
createWithStructuredOutput<T extends z.ZodTypeAny>(
26+
createWithStructuredOutput<T extends ZodType>(
2727
body: RunTaskCreateParamsWithStructuredOutput<T>,
2828
options?: RequestOptions,
2929
): APIPromise<TaskViewWithStructuredOutput<T>> {
@@ -43,7 +43,7 @@ export class Tasks extends APIResource {
4343
return this._client.get(path`/tasks/${taskID}`, { query, ...options });
4444
}
4545

46-
retrieveWithStructuredOutput<T extends z.ZodTypeAny>(
46+
retrieveWithStructuredOutput<T extends ZodType>(
4747
taskID: string,
4848
query: GetTaskStatusParamsWithStructuredOutput<T>,
4949
options?: RequestOptions,

0 commit comments

Comments
 (0)