@@ -83,9 +83,11 @@ export async function generateHelper(
83
83
registry : Registry ,
84
84
input : z . infer < typeof GenerateUtilParamSchema > ,
85
85
middleware ?: ModelMiddleware [ ] ,
86
- currentTurns ?: number
86
+ currentTurns ?: number ,
87
+ messageIndex ?: number
87
88
) : Promise < GenerateResponseData > {
88
89
currentTurns = currentTurns ?? 0 ;
90
+ messageIndex = messageIndex ?? 0 ;
89
91
// do tracing
90
92
return await runInNewSpan (
91
93
registry ,
@@ -100,7 +102,13 @@ export async function generateHelper(
100
102
async ( metadata ) => {
101
103
metadata . name = 'generate' ;
102
104
metadata . input = input ;
103
- const output = await generate ( registry , input , middleware , currentTurns ! ) ;
105
+ const output = await generate (
106
+ registry ,
107
+ input ,
108
+ middleware ,
109
+ currentTurns ! ,
110
+ messageIndex !
111
+ ) ;
104
112
metadata . output = JSON . stringify ( output ) ;
105
113
return output ;
106
114
}
@@ -111,7 +119,8 @@ async function generate(
111
119
registry : Registry ,
112
120
rawRequest : z . infer < typeof GenerateUtilParamSchema > ,
113
121
middleware : ModelMiddleware [ ] | undefined ,
114
- currentTurn : number
122
+ currentTurn : number ,
123
+ messageIndex : number
115
124
) : Promise < GenerateResponseData > {
116
125
const { modelAction : model } = await resolveModel ( registry , rawRequest . model ) ;
117
126
if ( model . __action . metadata ?. model . stage === 'deprecated' ) {
@@ -152,15 +161,17 @@ async function generate(
152
161
streamingCallback
153
162
? ( chunk : GenerateResponseChunkData ) => {
154
163
// Store accumulated chunk data
155
- streamingCallback (
156
- new GenerateResponseChunk ( chunk , {
157
- index : 0 ,
158
- role : 'model' ,
159
- previousChunks : accumulatedChunks ,
160
- parser : resolvedFormat ?. handler ( request . output ?. schema )
161
- . parseChunk ,
162
- } )
163
- ) ;
164
+ if ( streamingCallback ) {
165
+ streamingCallback ! (
166
+ new GenerateResponseChunk ( chunk , {
167
+ index : messageIndex ,
168
+ role : 'model' ,
169
+ previousChunks : accumulatedChunks ,
170
+ parser : resolvedFormat ?. handler ( request . output ?. schema )
171
+ . parseChunk ,
172
+ } )
173
+ ) ;
174
+ }
164
175
accumulatedChunks . push ( chunk ) ;
165
176
}
166
177
: undefined ,
@@ -246,6 +257,7 @@ async function generate(
246
257
} ) ;
247
258
}
248
259
}
260
+ messageIndex ++ ;
249
261
const nextRequest = {
250
262
...rawRequest ,
251
263
messages : [
@@ -257,11 +269,26 @@ async function generate(
257
269
] as MessageData [ ] ,
258
270
tools : newTools ,
259
271
} ;
272
+ // stream out the tool responses
273
+ streamingCallback ?.(
274
+ new GenerateResponseChunk (
275
+ {
276
+ content : toolResponses ,
277
+ } ,
278
+ {
279
+ index : messageIndex ,
280
+ role : 'model' ,
281
+ previousChunks : accumulatedChunks ,
282
+ parser : resolvedFormat ?. handler ( request . output ?. schema ) . parseChunk ,
283
+ }
284
+ )
285
+ ) ;
260
286
return await generateHelper (
261
287
registry ,
262
288
nextRequest ,
263
289
middleware ,
264
- currentTurn + 1
290
+ currentTurn + 1 ,
291
+ messageIndex + 1
265
292
) ;
266
293
}
267
294
0 commit comments