@@ -10,9 +10,8 @@ import { path } from '../internal/utils/path';
1010import {
1111 parseStructuredTaskOutput ,
1212 stringifyStructuredOutput ,
13- type TaskViewWithStructuredOutput ,
14- type GetTaskStatusParamsWithStructuredOutput ,
15- type RunTaskCreateParamsWithStructuredOutput ,
13+ type TaskViewWithSchema ,
14+ type TaskCreateParamsWithSchema ,
1615} from '../lib/parse' ;
1716
1817export class Tasks extends APIResource {
@@ -62,17 +61,33 @@ export class Tasks extends APIResource {
6261 * - 404: If referenced agent/browser profiles don't exist
6362 * - 400: If session is stopped or already has a running task
6463 */
65- create ( body : TaskCreateParams , options ?: RequestOptions ) : APIPromise < TaskView > {
66- return this . _client . post ( '/tasks' , { body, ...options } ) ;
67- }
68-
69- createWithStructuredOutput < T extends ZodType > (
70- body : RunTaskCreateParamsWithStructuredOutput < T > ,
64+ create < T extends ZodType > (
65+ body : TaskCreateParamsWithSchema < T > ,
7166 options ?: RequestOptions ,
72- ) : APIPromise < TaskViewWithStructuredOutput < T > > {
73- return this . create ( stringifyStructuredOutput ( body ) , options ) . _thenUnwrap ( ( rsp ) =>
74- parseStructuredTaskOutput ( rsp as TaskView , body ) ,
75- ) ;
67+ ) : APIPromise < TaskViewWithSchema < T > > ;
68+ create ( body : TaskCreateParams , options ?: RequestOptions ) : APIPromise < TaskView > ;
69+ create (
70+ body : TaskCreateParams | TaskCreateParamsWithSchema < ZodType > ,
71+ options ?: RequestOptions ,
72+ ) : APIPromise < unknown > {
73+ if ( body . structuredOutputJson == null || typeof body . structuredOutputJson === 'string' ) {
74+ return this . _client . post ( '/tasks' , { body, ...options } ) ;
75+ }
76+
77+ if ( typeof body . structuredOutputJson === 'object' ) {
78+ const schema = body . structuredOutputJson ;
79+
80+ const _body : TaskCreateParams = {
81+ ...body ,
82+ structuredOutputJson : stringifyStructuredOutput ( schema ) ,
83+ } ;
84+
85+ return this . _client
86+ . post ( '/tasks' , { body : _body , ...options } )
87+ . _thenUnwrap ( ( rsp ) => parseStructuredTaskOutput ( rsp as TaskView , schema ) ) ;
88+ }
89+
90+ return this . _client . post ( '/tasks' , { body, ...options } ) ;
7691 }
7792
7893 /**
@@ -101,22 +116,21 @@ export class Tasks extends APIResource {
101116 *
102117 * - 404: If the user agent task doesn't exist
103118 */
104- retrieve ( taskID : string , options ?: RequestOptions ) : APIPromise < TaskView > {
105- return this . _client . get ( path `/tasks/${ taskID } ` , options ) ;
106- }
107-
108- retrieveWithStructuredOutput < T extends ZodType > (
109- taskID : string ,
110- query : GetTaskStatusParamsWithStructuredOutput < T > ,
119+ retrieve < T extends ZodType > (
120+ req : { taskId : string ; schema : T } ,
111121 options ?: RequestOptions ,
112- ) : APIPromise < TaskViewWithStructuredOutput < T > > {
113- // NOTE: We manually remove structuredOutputJson from the query object because
114- // it's not a valid Browser Use Cloud parameter.
115- const { structuredOutputJson, ...rest } = query ;
116-
117- return this . retrieve ( taskID , rest , options ) . _thenUnwrap ( ( rsp ) =>
118- parseStructuredTaskOutput ( rsp as TaskView , query ) ,
119- ) ;
122+ ) : APIPromise < TaskViewWithSchema < T > > ;
123+ retrieve ( taskID : string , options ?: RequestOptions ) : APIPromise < TaskView > ;
124+ retrieve ( req : string | { taskId : string ; schema : ZodType } , options ?: RequestOptions ) : APIPromise < unknown > {
125+ if ( typeof req === 'string' ) {
126+ return this . _client . get ( path `/tasks/${ req } ` , options ) ;
127+ }
128+
129+ const { taskId, schema } = req ;
130+
131+ return this . _client
132+ . get ( path `/tasks/${ taskId } ` , options )
133+ . _thenUnwrap ( ( rsp ) => parseStructuredTaskOutput ( rsp as TaskView , schema ) ) ;
120134 }
121135
122136 /**
0 commit comments