@@ -99,10 +99,6 @@ export class OpenAiHandler extends BaseProvider implements SingleCompletionHandl
9999 const deepseekReasoner = modelId . includes ( "deepseek-reasoner" ) || enabledR1Format
100100 const ark = modelUrl . includes ( ".volces.com" )
101101
102- // Accumulators for final response logging
103- const accumulatedText : string [ ] = [ ]
104- const accumulatedReasoning : string [ ] = [ ]
105- const toolCalls : Array < { id ?: string ; name ?: string } > = [ ]
106102 let lastUsage : any
107103
108104 // Handle O3 family models separately with their own logging
@@ -213,31 +209,18 @@ export class OpenAiHandler extends BaseProvider implements SingleCompletionHandl
213209
214210 if ( delta . content ) {
215211 for ( const matchedChunk of matcher . update ( delta . content ) ) {
216- if ( matchedChunk . type === "text" ) {
217- accumulatedText . push ( matchedChunk . text )
218- } else if ( matchedChunk . type === "reasoning" ) {
219- accumulatedReasoning . push ( matchedChunk . text )
220- }
221212 yield matchedChunk
222213 }
223214 }
224215
225216 if ( "reasoning_content" in delta && delta . reasoning_content ) {
226- accumulatedReasoning . push ( ( delta . reasoning_content as string | undefined ) || "" )
227217 yield {
228218 type : "reasoning" ,
229219 text : ( delta . reasoning_content as string | undefined ) || "" ,
230220 }
231221 }
232222
233- // Track tool calls for logging and use processToolCalls for proper tool_call_end events
234- if ( delta . tool_calls ) {
235- for ( const toolCall of delta . tool_calls ) {
236- if ( toolCall . id || toolCall . function ?. name ) {
237- toolCalls . push ( { id : toolCall . id , name : toolCall . function ?. name } )
238- }
239- }
240- }
223+ // Use processToolCalls for proper tool_call_end events
241224 yield * this . processToolCalls ( delta , finishReason , activeToolCallIds )
242225
243226 if ( chunk . usage ) {
@@ -246,11 +229,6 @@ export class OpenAiHandler extends BaseProvider implements SingleCompletionHandl
246229 }
247230
248231 for ( const matchedChunk of matcher . final ( ) ) {
249- if ( matchedChunk . type === "text" ) {
250- accumulatedText . push ( matchedChunk . text )
251- } else if ( matchedChunk . type === "reasoning" ) {
252- accumulatedReasoning . push ( matchedChunk . text )
253- }
254232 yield matchedChunk
255233 }
256234
@@ -291,7 +269,6 @@ export class OpenAiHandler extends BaseProvider implements SingleCompletionHandl
291269 if ( message ?. tool_calls ) {
292270 for ( const toolCall of message . tool_calls ) {
293271 if ( toolCall . type === "function" ) {
294- toolCalls . push ( { id : toolCall . id , name : toolCall . function . name } )
295272 yield {
296273 type : "tool_call" ,
297274 id : toolCall . id ,
@@ -302,7 +279,6 @@ export class OpenAiHandler extends BaseProvider implements SingleCompletionHandl
302279 }
303280 }
304281
305- accumulatedText . push ( message ?. content || "" )
306282 yield {
307283 type : "text" ,
308284 text : message ?. content || "" ,
@@ -373,9 +349,6 @@ export class OpenAiHandler extends BaseProvider implements SingleCompletionHandl
373349 const modelInfo = this . getModel ( ) . info
374350 const methodIsAzureAiInference = this . _isAzureAiInference ( this . options . openAiBaseUrl )
375351
376- // Accumulators for response logging
377- const accumulatedText : string [ ] = [ ]
378- const toolCalls : Array < { id ?: string ; name ?: string } > = [ ]
379352 let lastUsage : any
380353
381354 if ( this . options . openAiStreamingEnabled ?? true ) {
@@ -424,21 +397,12 @@ export class OpenAiHandler extends BaseProvider implements SingleCompletionHandl
424397
425398 if ( delta ) {
426399 if ( delta . content ) {
427- accumulatedText . push ( delta . content )
428400 yield {
429401 type : "text" ,
430402 text : delta . content ,
431403 }
432404 }
433405
434- // Track tool calls for logging and use processToolCalls for proper tool_call_end events
435- if ( delta . tool_calls ) {
436- for ( const toolCall of delta . tool_calls ) {
437- if ( toolCall . id || toolCall . function ?. name ) {
438- toolCalls . push ( { id : toolCall . id , name : toolCall . function ?. name } )
439- }
440- }
441- }
442406 yield * this . processToolCalls ( delta , finishReason , activeToolCallIds )
443407 }
444408
@@ -489,7 +453,6 @@ export class OpenAiHandler extends BaseProvider implements SingleCompletionHandl
489453 if ( message ?. tool_calls ) {
490454 for ( const toolCall of message . tool_calls ) {
491455 if ( toolCall . type === "function" ) {
492- toolCalls . push ( { id : toolCall . id , name : toolCall . function . name } )
493456 yield {
494457 type : "tool_call" ,
495458 id : toolCall . id ,
@@ -500,7 +463,6 @@ export class OpenAiHandler extends BaseProvider implements SingleCompletionHandl
500463 }
501464 }
502465
503- accumulatedText . push ( message ?. content || "" )
504466 yield {
505467 type : "text" ,
506468 text : message ?. content || "" ,
@@ -511,34 +473,6 @@ export class OpenAiHandler extends BaseProvider implements SingleCompletionHandl
511473 }
512474 }
513475
514- private async * handleStreamResponse ( stream : AsyncIterable < OpenAI . Chat . Completions . ChatCompletionChunk > ) : ApiStream {
515- const activeToolCallIds = new Set < string > ( )
516-
517- for await ( const chunk of stream ) {
518- const delta = chunk . choices ?. [ 0 ] ?. delta
519- const finishReason = chunk . choices ?. [ 0 ] ?. finish_reason
520-
521- if ( delta ) {
522- if ( delta . content ) {
523- yield {
524- type : "text" ,
525- text : delta . content ,
526- }
527- }
528-
529- yield * this . processToolCalls ( delta , finishReason , activeToolCallIds )
530- }
531-
532- if ( chunk . usage ) {
533- yield {
534- type : "usage" ,
535- inputTokens : chunk . usage . prompt_tokens || 0 ,
536- outputTokens : chunk . usage . completion_tokens || 0 ,
537- }
538- }
539- }
540- }
541-
542476 /**
543477 * Helper generator to process tool calls from a stream chunk.
544478 * Tracks active tool call IDs and yields tool_call_partial and tool_call_end events.
0 commit comments