@@ -70,8 +70,8 @@ export class Tasks extends APIResource {
70
70
body : TaskCreateParams | TaskCreateParamsWithSchema < ZodType > ,
71
71
options ?: RequestOptions ,
72
72
) : APIPromise < TaskCreateResponse > {
73
- if ( body . structuredOutputJson != null && typeof body . structuredOutputJson === 'object' ) {
74
- const schema = body . structuredOutputJson ;
73
+ if ( 'schema' in body && body . schema != null && typeof body . schema === 'object' ) {
74
+ const schema = body . schema ;
75
75
76
76
const _body : TaskCreateParams = {
77
77
...body ,
@@ -190,6 +190,47 @@ export class Tasks extends APIResource {
190
190
}
191
191
}
192
192
193
+ /**
194
+ * Create and run an agent task.
195
+ *
196
+ * @returns The output of the task.
197
+ */
198
+ run < T extends ZodType > (
199
+ body : TaskCreateParamsWithSchema < T > ,
200
+ options ?: RequestOptions ,
201
+ ) : APIPromise < TaskViewWithSchema < T > > ;
202
+ run ( body : TaskCreateParams , options ?: RequestOptions ) : APIPromise < TaskView > ;
203
+ run (
204
+ body : TaskCreateParams | TaskCreateParamsWithSchema < ZodType > ,
205
+ options ?: RequestOptions ,
206
+ ) : APIPromise < unknown > {
207
+ if ( 'schema' in body && body . schema != null && typeof body . schema === 'object' ) {
208
+ return this . create ( body , options ) . _thenUnwrap ( async ( data ) => {
209
+ const taskId = data . id ;
210
+
211
+ for await ( const msg of this . stream < ZodType > ( { taskId, schema : body . schema } , options ) ) {
212
+ if ( msg . data . status === 'finished' ) {
213
+ return msg . data ;
214
+ }
215
+ }
216
+
217
+ throw new Error ( 'Task did not finish' ) ;
218
+ } ) ;
219
+ }
220
+
221
+ return this . create ( body , options ) . _thenUnwrap ( async ( data ) => {
222
+ const taskId = data . id ;
223
+
224
+ for await ( const msg of this . stream ( taskId , options ) ) {
225
+ if ( msg . data . status === 'finished' ) {
226
+ return msg . data ;
227
+ }
228
+ }
229
+
230
+ throw new Error ( 'Task did not finish' ) ;
231
+ } ) ;
232
+ }
233
+
193
234
/**
194
235
* Control the execution of an AI agent task.
195
236
*
0 commit comments