@@ -10,9 +10,8 @@ import { path } from '../internal/utils/path';
10
10
import {
11
11
parseStructuredTaskOutput ,
12
12
stringifyStructuredOutput ,
13
- type TaskViewWithStructuredOutput ,
14
- type GetTaskStatusParamsWithStructuredOutput ,
15
- type RunTaskCreateParamsWithStructuredOutput ,
13
+ type TaskViewWithSchema ,
14
+ type TaskCreateParamsWithSchema ,
16
15
} from '../lib/parse' ;
17
16
18
17
export class Tasks extends APIResource {
@@ -62,17 +61,33 @@ export class Tasks extends APIResource {
62
61
* - 404: If referenced agent/browser profiles don't exist
63
62
* - 400: If session is stopped or already has a running task
64
63
*/
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 > ,
71
66
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 } ) ;
76
91
}
77
92
78
93
/**
@@ -101,22 +116,21 @@ export class Tasks extends APIResource {
101
116
*
102
117
* - 404: If the user agent task doesn't exist
103
118
*/
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 } ,
111
121
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 ) ) ;
120
134
}
121
135
122
136
/**
0 commit comments