@@ -5,7 +5,6 @@ import { extractReasoningMiddleware } from 'ai'
5
5
import { z } from 'zod'
6
6
7
7
import { nonNullable } from '../array'
8
- import { debounce } from '../debounce'
9
8
import { ParseFunctionCallError } from '../error'
10
9
import { generateRandomId } from '../id'
11
10
import Logger from '../logger'
@@ -190,37 +189,50 @@ export const normalizeToolCallsMiddleware: LanguageModelV1Middleware = {
190
189
export const rawLoggingMiddleware : LanguageModelV1Middleware = {
191
190
wrapStream : async ( { doStream, params } ) => {
192
191
const log = logger . child ( 'rawLoggingMiddleware' )
192
+ const text : string [ ] = [ ]
193
+ const reasoning : string [ ] = [ ]
193
194
194
195
const { stream, ...rest } = await doStream ( )
195
196
196
- log . debug ( 'Stream started' , { params } )
197
- let text = ''
198
- let reasoning = ''
199
- const printLog = debounce ( ( ) => {
200
- log . debug ( 'Stream progress' , { text, reasoning } )
201
- } , 2000 )
202
-
203
197
const transformStream = new TransformStream <
204
198
LanguageModelV1StreamPart ,
205
199
LanguageModelV1StreamPart
206
200
> ( {
207
201
transform ( chunk , controller ) {
208
202
if ( chunk . type === 'text-delta' ) {
209
- text += chunk . textDelta
203
+ text . push ( chunk . textDelta )
210
204
}
211
205
else if ( chunk . type === 'reasoning' ) {
212
- reasoning += chunk . textDelta
206
+ reasoning . push ( chunk . textDelta )
213
207
}
214
- printLog ( )
215
208
controller . enqueue ( chunk )
216
209
} ,
210
+ flush ( ) {
211
+ log . info ( 'LLM Stream Result' , {
212
+ params,
213
+ text : text . join ( '' ) ,
214
+ reasoning : reasoning . join ( '' ) ,
215
+ } )
216
+ } ,
217
217
} )
218
218
219
219
return {
220
220
stream : stream . pipeThrough ( transformStream ) ,
221
221
...rest ,
222
222
}
223
223
} ,
224
+ wrapGenerate : async ( { doGenerate, params } ) => {
225
+ const log = logger . child ( 'rawLoggingMiddleware' )
226
+
227
+ const result = await doGenerate ( )
228
+
229
+ log . info ( 'LLM Generate Result' , {
230
+ params,
231
+ result,
232
+ } )
233
+
234
+ return result
235
+ } ,
224
236
}
225
237
226
238
const errorResponse = / < \| c h a n n e l \| > (? ! \s * c o m m e n t a r y \s + t o = [ a - z _ . ] + \s * > ) [ ^ < ] + > ( < \/ a s s i s t a n t ) ? / gs
@@ -437,9 +449,9 @@ export const lmStudioHarmonyEncodingMiddleware: LanguageModelV1Middleware = {
437
449
}
438
450
439
451
export const middlewares = [
440
- // rawLoggingMiddleware,
441
452
normalizeToolCallsMiddleware ,
442
453
extractPromptBasedToolCallsMiddleware ,
443
454
lmStudioHarmonyEncodingMiddleware ,
444
455
reasoningMiddleware ,
456
+ rawLoggingMiddleware ,
445
457
]
0 commit comments