@@ -227,6 +227,12 @@ export function render<
227
227
)
228
228
: undefined ;
229
229
230
+ if ( functions && tools ) {
231
+ throw new Error (
232
+ "You can't have both functions and tools defined. Please choose one or the other." ,
233
+ ) ;
234
+ }
235
+
230
236
let finished : ReturnType < typeof createResolvablePromise > | undefined ;
231
237
232
238
async function handleRender (
@@ -254,20 +260,23 @@ export function render<
254
260
typeof value === 'object' &&
255
261
Symbol . asyncIterator in value
256
262
) {
257
- for await ( const node of value as AsyncGenerator <
263
+ const it = value as AsyncGenerator <
258
264
React . ReactNode ,
259
265
React . ReactNode ,
260
266
void
261
- > ) {
262
- res . update ( node ) ;
267
+ > ;
268
+ while ( true ) {
269
+ const { done, value } = await it . next ( ) ;
270
+ res . update ( value ) ;
271
+ if ( done ) break ;
263
272
}
264
273
finished ?. resolve ( void 0 ) ;
265
274
} else if ( value && typeof value === 'object' && Symbol . iterator in value ) {
266
275
const it = value as Generator < React . ReactNode , React . ReactNode , void > ;
267
276
while ( true ) {
268
277
const { done, value } = it . next ( ) ;
269
- if ( done ) break ;
270
278
res . update ( value ) ;
279
+ if ( done ) break ;
271
280
}
272
281
finished ?. resolve ( void 0 ) ;
273
282
} else {
@@ -299,14 +308,19 @@ export function render<
299
308
: { } ) ,
300
309
} ) ) as any ,
301
310
{
302
- async experimental_onFunctionCall ( functionCallPayload ) {
303
- hasFunction = true ;
304
- handleRender (
305
- functionCallPayload . arguments ,
306
- options . functions ?. [ functionCallPayload . name as any ] ?. render ,
307
- ui ,
308
- ) ;
309
- } ,
311
+ ...( functions
312
+ ? {
313
+ async experimental_onFunctionCall ( functionCallPayload ) {
314
+ hasFunction = true ;
315
+ handleRender (
316
+ functionCallPayload . arguments ,
317
+ options . functions ?. [ functionCallPayload . name as any ]
318
+ ?. render ,
319
+ ui ,
320
+ ) ;
321
+ } ,
322
+ }
323
+ : { } ) ,
310
324
...( tools
311
325
? {
312
326
async experimental_onToolCall ( toolCallPayload : any ) {
@@ -323,15 +337,18 @@ export function render<
323
337
} ,
324
338
}
325
339
: { } ) ,
326
- onToken ( token ) {
327
- text += token ;
328
- if ( hasFunction ) return ;
340
+ onText ( chunk ) {
341
+ text += chunk ;
329
342
handleRender ( { content : text , done : false } , options . text , ui ) ;
330
343
} ,
331
344
async onFinal ( ) {
332
- if ( hasFunction ) return ;
333
- handleRender ( { content : text , done : true } , options . text , ui ) ;
345
+ if ( hasFunction ) {
346
+ await finished ?. promise ;
347
+ ui . done ( ) ;
348
+ return ;
349
+ }
334
350
351
+ handleRender ( { content : text , done : true } , options . text , ui ) ;
335
352
await finished ?. promise ;
336
353
ui . done ( ) ;
337
354
} ,
0 commit comments