@@ -16,6 +16,12 @@ import {
1616 extendUsageWithCacheMetrics ,
1717 type ClaudeUsage ,
1818} from "../utils/cacheControlUtils.js" ;
19+ import {
20+ extractToolCallMetadata ,
21+ mergeToolCallMetadata ,
22+ applyToolCallMetadataToResult ,
23+ normalizeToolCallName ,
24+ } from "../utils/toolCallMetadata.js" ;
1925
2026import * as os from "os" ;
2127import * as fs from "fs" ;
@@ -94,13 +100,7 @@ function isGitRepository(dirPath: string): string {
94100type OpenAIModelConfig = Omit <
95101 ChatCompletionCreateParamsNonStreaming ,
96102 "messages"
97- > & {
98- vertexai ?: {
99- thinking_config : {
100- thinking_level : string ;
101- } ;
102- } ;
103- } ;
103+ > ;
104104
105105/**
106106 * Get specific configuration parameters based on model name
@@ -124,14 +124,6 @@ function getModelConfig(
124124 config . temperature = undefined ;
125125 }
126126
127- if ( modelName . startsWith ( "gemini-3" ) ) {
128- config . vertexai = {
129- thinking_config : {
130- thinking_level : "minimal" ,
131- } ,
132- } ;
133- }
134-
135127 return config ;
136128}
137129
@@ -337,9 +329,9 @@ Today's date: ${new Date().toISOString().split("T")[0]}
337329 const resolvedMaxTokens = options . maxTokens ?? modelConfig . maxTokens ;
338330
339331 processedTools = tools ;
340- console . log ( "当前采用的模型为" , currentModel ) ;
332+ // console.log("当前采用的模型为", currentModel);
341333 if ( isClaudeModel ( currentModel ) ) {
342- console . log ( "走到了claude的处理" , currentModel ) ;
334+ // console.log("走到了claude的处理", currentModel);
343335 openaiMessages = transformMessagesForClaudeCache (
344336 openaiMessages ,
345337 currentModel ,
@@ -423,7 +415,7 @@ Today's date: ${new Date().toISOString().split("T")[0]}
423415
424416 if ( ! response ?. choices ?. [ 0 ] ?. message ) {
425417 console . error ( "callAgent 返回为空-原始 responese" , response ) ;
426- console . error ( "callAgent 返回为空-请求 messages" , openaiMessages ) ;
418+ // console.error("callAgent 返回为空-请求 messages", openaiMessages);
427419 const errorMessage =
428420 ( response as unknown as { error ?: { message ?: string } } ) ?. error
429421 ?. message || "No response from AI" ;
@@ -659,6 +651,7 @@ async function processStreamingResponse(
659651 arguments : string ;
660652 } ;
661653 } [ ] = [ ] ;
654+ const toolCallMetadataById = new Map < string , Record < string , unknown > > ( ) ;
662655 const additionalDeltaFields : Record < string , unknown > = { } ;
663656 let usage : CallAgentResult [ "usage" ] = undefined ;
664657 let finishReason : CallAgentResult [ "finish_reason" ] = null ;
@@ -740,11 +733,19 @@ async function processStreamingResponse(
740733 const toolCallDelta =
741734 rawToolCall as ChatCompletionChunk . Choice . Delta . ToolCall ;
742735
743- if ( ! toolCallDelta . function ) {
736+ const deltaMetadata = extractToolCallMetadata (
737+ rawToolCall as unknown as Record < string , unknown > ,
738+ ) ;
739+
740+ const rawRecord = rawToolCall as unknown as Record < string , unknown > ;
741+ const topLevelName =
742+ typeof rawRecord . name === "string" ? rawRecord . name : undefined ;
743+
744+ if ( ! toolCallDelta . function && ! topLevelName ) {
744745 continue ;
745746 }
746747
747- const functionDelta = toolCallDelta . function ;
748+ const functionDelta = toolCallDelta . function ?? { } ;
748749
749750 let existingCall ;
750751 let isNew = false ;
@@ -756,7 +757,9 @@ async function processStreamingResponse(
756757 id : toolCallDelta . id ,
757758 type : "function" as const ,
758759 function : {
759- name : functionDelta . name || "" ,
760+ name : normalizeToolCallName (
761+ functionDelta . name || topLevelName ,
762+ ) ,
760763 arguments : "" ,
761764 } ,
762765 } ;
@@ -771,32 +774,56 @@ async function processStreamingResponse(
771774 continue ;
772775 }
773776
774- if ( functionDelta . name ) {
775- existingCall . function . name = functionDelta . name ;
777+ if ( deltaMetadata ) {
778+ const callId = existingCall . id || toolCallDelta . id ;
779+ if ( callId ) {
780+ toolCallMetadataById . set (
781+ callId ,
782+ mergeToolCallMetadata (
783+ toolCallMetadataById . get ( callId ) ,
784+ deltaMetadata ,
785+ ) ! ,
786+ ) ;
787+ }
776788 }
777789
778- // Emit start stage when a new tool call is created and we have the tool name
779- if ( onToolUpdate && isNew && existingCall . function . name ) {
790+ const previousName = existingCall . function . name ;
791+ const resolvedName = normalizeToolCallName (
792+ functionDelta . name || topLevelName || previousName ,
793+ ) ;
794+ if ( resolvedName ) {
795+ existingCall . function . name = resolvedName ;
796+ }
797+
798+ const nameJustResolved =
799+ ! normalizeToolCallName ( previousName ) &&
800+ Boolean ( existingCall . function . name ) ;
801+
802+ // Emit start when a tool call first gets a resolvable name
803+ if (
804+ onToolUpdate &&
805+ existingCall . function . name &&
806+ ( isNew || nameJustResolved )
807+ ) {
780808 onToolUpdate ( {
781809 id : existingCall . id ,
782810 name : existingCall . function . name ,
783- parameters : "" , // Empty parameters for start stage
784- parametersChunk : "" , // Empty chunk for start stage
785- stage : "start" , // New tool call triggers start stage
811+ parameters : "" ,
812+ parametersChunk : "" ,
813+ stage : "start" ,
786814 } ) ;
787- isNew = false ; // Prevent duplicate start emissions
815+ isNew = false ;
788816 }
789817
790818 if ( functionDelta . arguments ) {
791819 existingCall . function . arguments += functionDelta . arguments ;
792820 }
793821
794- // Emit streaming updates for all chunks with actual content (including first chunk)
795822 if (
796823 onToolUpdate &&
797824 existingCall . function . name &&
798825 functionDelta . arguments &&
799- functionDelta . arguments . length > 0 // Only emit streaming for chunks with actual content
826+ functionDelta . arguments . length > 0
800827 ) {
801828 onToolUpdate ( {
802829 id : existingCall . id ,
@@ -828,7 +855,14 @@ async function processStreamingResponse(
828855 }
829856
830857 if ( toolCalls . length > 0 ) {
831- result . tool_calls = toolCalls ;
858+ result . tool_calls = toolCalls
859+ . filter ( ( toolCall ) => normalizeToolCallName ( toolCall . function . name ) )
860+ . map ( ( toolCall ) =>
861+ applyToolCallMetadataToResult (
862+ toolCall ,
863+ toolCallMetadataById . get ( toolCall . id ) ,
864+ ) ,
865+ ) ;
832866 }
833867
834868 if ( usage ) {
0 commit comments