@@ -358,14 +358,14 @@ export const make: <Config>(
358
358
if ( Option . isNone ( spanTransformer ) ) {
359
359
return stream
360
360
}
361
- let lastResponse : AiResponse . AiResponse | undefined
361
+ let finalResponse = AiResponse . empty
362
362
return stream . pipe (
363
363
Stream . map ( ( response ) => {
364
- lastResponse = response
364
+ finalResponse = AiResponse . merge ( finalResponse , response )
365
365
return response
366
366
} ) ,
367
367
Stream . ensuring ( Effect . sync ( ( ) => {
368
- spanTransformer . value ( { ...options , response : lastResponse ! } )
368
+ spanTransformer . value ( { ...options , response : finalResponse } )
369
369
} ) )
370
370
)
371
371
} ) ) ,
@@ -470,49 +470,49 @@ const getDescription = (ast: AST.AST): string => {
470
470
return AST . DescriptionAnnotationId in annotations ? annotations [ AST . DescriptionAnnotationId ] as string : ""
471
471
}
472
472
473
- const resolveParts = < Tools extends AiTool . Any > ( options : {
473
+ const resolveParts = Effect . fnUntraced ( function * < Tools extends AiTool . Any > ( options : {
474
474
readonly response : AiResponse . AiResponse
475
475
readonly toolkit : AiToolkit . ToHandler < Tools >
476
476
readonly concurrency : Concurrency | undefined
477
477
readonly method : string
478
- } ) =>
479
- Effect . gen ( function * ( ) {
480
- const toolNames : Array < string > = [ ]
481
- const toolParts = options . response . parts . filter (
482
- ( part ) : part is AiResponse . ToolCallPart => {
483
- if ( part . _tag === "ToolCallPart" ) {
484
- toolNames . push ( part . name )
485
- return true
486
- }
487
- return false
478
+ } ) {
479
+ const toolNames : Array < string > = [ ]
480
+ const toolParts = options . response . parts . filter (
481
+ ( part ) : part is AiResponse . ToolCallPart => {
482
+ if ( part . _tag === "ToolCallPart" ) {
483
+ toolNames . push ( part . name )
484
+ return true
488
485
}
489
- )
490
- if ( toolParts . length === 0 ) {
491
- return new AiResponse . WithToolCallResults ( {
492
- parts : options . response . parts ,
493
- results : constEmptyMap ,
494
- encodedResults : constEmptyMap
495
- } , constDisableValidation )
486
+ return false
496
487
}
497
- yield * Effect . annotateCurrentSpan ( "toolCalls" , toolNames )
498
- const results = new Map < AiResponse . ToolCallId , AiTool . Success < Tools > > ( )
499
- const encodedResults = new Map < AiResponse . ToolCallId , unknown > ( )
500
- yield * Effect . forEach ( toolParts , ( part ) => {
501
- const id = part . id as AiResponse . ToolCallId
502
- const name = part . name as AiTool . Name < Tools >
503
- const params = part . params as AiTool . Parameters < Tools >
504
- const toolCall = options . toolkit . handle ( name , params )
505
- return Effect . map ( toolCall , ( { encodedResult, result } ) => {
506
- results . set ( id , result )
507
- encodedResults . set ( id , encodedResult )
508
- } )
509
- } , { concurrency : options . concurrency , discard : true } )
488
+ )
489
+ if ( toolParts . length === 0 ) {
510
490
return new AiResponse . WithToolCallResults ( {
511
491
parts : options . response . parts ,
512
- results,
513
- encodedResults
492
+ results : constEmptyMap ,
493
+ encodedResults : constEmptyMap
514
494
} , constDisableValidation )
515
- } )
495
+ }
496
+ yield * Effect . annotateCurrentSpan ( "toolCalls" , toolNames )
497
+ const results = new Map < AiResponse . ToolCallId , AiTool . Success < Tools > > ( )
498
+ const encodedResults = new Map < AiResponse . ToolCallId , unknown > ( )
499
+ const resolve = Effect . forEach ( toolParts , ( part ) => {
500
+ const id = part . id as AiResponse . ToolCallId
501
+ const name = part . name as AiTool . Name < Tools >
502
+ const params = part . params as AiTool . Parameters < Tools >
503
+ const toolCall = options . toolkit . handle ( name , params )
504
+ return Effect . map ( toolCall , ( { encodedResult, result } ) => {
505
+ results . set ( id , result )
506
+ encodedResults . set ( id , encodedResult )
507
+ } )
508
+ } , { concurrency : options . concurrency , discard : true } )
509
+ yield * resolve
510
+ return new AiResponse . WithToolCallResults ( {
511
+ parts : options . response . parts ,
512
+ results,
513
+ encodedResults
514
+ } , constDisableValidation )
515
+ } )
516
516
517
517
/**
518
518
* Generate text using a large language model for the specified `prompt`.
0 commit comments