@@ -70,8 +70,8 @@ export class Tasks extends APIResource {
7070 body : TaskCreateParams | TaskCreateParamsWithSchema < ZodType > ,
7171 options ?: RequestOptions ,
7272 ) : 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 ;
7575
7676 const _body : TaskCreateParams = {
7777 ...body ,
@@ -190,6 +190,47 @@ export class Tasks extends APIResource {
190190 }
191191 }
192192
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+
193234 /**
194235 * Control the execution of an AI agent task.
195236 *
0 commit comments