@@ -134,65 +134,42 @@ export class Tasks extends APIResource {
134
134
stream < T extends ZodType > (
135
135
body : TaskCreateParamsWithSchema < T > ,
136
136
options ?: RequestOptions ,
137
- ) : ReadableStream < Uint8Array < ArrayBufferLike > > ;
138
- stream ( body : TaskCreateParams , options ?: RequestOptions ) : ReadableStream < Uint8Array < ArrayBufferLike > > ;
137
+ ) : AsyncGenerator < { event : 'status' ; data : TaskViewWithSchema < T > } > ;
139
138
stream (
139
+ body : TaskCreateParams ,
140
+ options ?: RequestOptions ,
141
+ ) : AsyncGenerator < { event : 'status' ; data : TaskView } > ;
142
+ async * stream (
140
143
body : TaskCreateParams | TaskCreateParamsWithSchema < ZodType > ,
141
144
options ?: RequestOptions ,
142
- ) : ReadableStream < Uint8Array < ArrayBufferLike > > {
143
- const self = this ;
144
-
145
- const enc = new TextEncoder ( ) ;
146
-
147
- const stream = new ReadableStream < Uint8Array > ( {
148
- async start ( controller ) {
149
- // open the SSE stream quickly
150
- controller . enqueue ( enc . encode ( ': connected\n\n' ) ) ;
151
-
152
- try {
153
- let req : TaskCreateParams ;
154
-
155
- if (
156
- 'structuredOutputJson' in body &&
157
- body . structuredOutputJson != null &&
158
- typeof body . structuredOutputJson === 'object'
159
- ) {
160
- req = {
161
- ...body ,
162
- structuredOutputJson : stringifyStructuredOutput ( body . structuredOutputJson ) ,
163
- } ;
164
- } else {
165
- req = body as TaskCreateParams ;
166
- }
167
-
168
- for await ( const msg of self . watch ( req , { interval : 500 } , options ) ) {
169
- if ( options ?. signal ?. aborted ) {
170
- break ;
171
- }
172
-
173
- let data : string ;
174
-
175
- if ( body . structuredOutputJson != null && typeof body . structuredOutputJson === 'object' ) {
176
- const parsed = parseStructuredTaskOutput < ZodType > ( msg . data , body . structuredOutputJson ) ;
177
- data = JSON . stringify ( parsed ) ;
178
- } else {
179
- data = JSON . stringify ( msg . data ) ;
180
- }
181
-
182
- const payload = `event: ${ msg . event } \ndata: ${ data } \n\n` ;
183
- controller . enqueue ( enc . encode ( payload ) ) ;
184
- }
185
-
186
- controller . enqueue ( enc . encode ( 'event: end\ndata: {}\n\n' ) ) ;
187
- } catch ( e ) {
188
- controller . enqueue ( enc . encode ( `event: error\ndata: ${ JSON . stringify ( { message : String ( e ) } ) } \n\n` ) ) ;
189
- } finally {
190
- controller . close ( ) ;
191
- }
192
- } ,
193
- } ) ;
145
+ ) : AsyncGenerator < unknown > {
146
+ let req : TaskCreateParams ;
147
+
148
+ if (
149
+ 'structuredOutputJson' in body &&
150
+ body . structuredOutputJson != null &&
151
+ typeof body . structuredOutputJson === 'object'
152
+ ) {
153
+ req = {
154
+ ...body ,
155
+ structuredOutputJson : stringifyStructuredOutput ( body . structuredOutputJson ) ,
156
+ } ;
157
+ } else {
158
+ req = body as TaskCreateParams ;
159
+ }
194
160
195
- return stream ;
161
+ for await ( const msg of this . watch ( req , { interval : 500 } , options ) ) {
162
+ if ( options ?. signal ?. aborted ) {
163
+ break ;
164
+ }
165
+
166
+ if ( body . structuredOutputJson != null && typeof body . structuredOutputJson === 'object' ) {
167
+ const parsed = parseStructuredTaskOutput < ZodType > ( msg . data , body . structuredOutputJson ) ;
168
+ yield { event : 'status' , data : parsed } ;
169
+ } else {
170
+ yield { event : 'status' , data : msg . data } ;
171
+ }
172
+ }
196
173
}
197
174
198
175
/**
0 commit comments