@@ -90,10 +90,23 @@ export class RequestForwarder {
9090 return null
9191 }
9292
93- /**
94- * Check if messages contain MCP-style tool definitions
95- * MCP tools are defined in system message using <tools><tool> XML format
96- */
93+ private applyToolCallsToResponse (
94+ result : any ,
95+ model : string ,
96+ tools : any [ ] | undefined
97+ ) : void {
98+ if ( tools && tools . length > 0 && ! isNativeFunctionCallingModel ( model ) ) {
99+ const content = result ?. choices ?. [ 0 ] ?. message ?. content || ''
100+ const toolCalls = this . parseToolCallsFromContent ( content )
101+
102+ if ( toolCalls && toolCalls . length > 0 ) {
103+ result . choices [ 0 ] . message . tool_calls = toolCalls
104+ result . choices [ 0 ] . message . content = null
105+ result . choices [ 0 ] . finish_reason = 'tool_calls'
106+ }
107+ }
108+ }
109+
97110 private hasMCPToolDefinitions ( messages : any [ ] ) : boolean {
98111 for ( const msg of messages ) {
99112 if ( msg . role === 'system' && typeof msg . content === 'string' ) {
@@ -636,20 +649,8 @@ CRITICAL RULES:
636649 // Non-streaming requests need to collect stream data and convert
637650 const result = await handler . handleNonStream ( response . data )
638651
639- // Parse tool calls from response content if using prompt-based tool calling
640- if ( request . tools && request . tools . length > 0 && ! isNativeFunctionCallingModel ( request . model ) ) {
641- const content = result ?. choices ?. [ 0 ] ?. message ?. content || ''
642- const toolCalls = this . parseToolCallsFromContent ( content )
643-
644- if ( toolCalls && toolCalls . length > 0 ) {
645- // Found tool calls in response, add them to the response
646- result . choices [ 0 ] . message . tool_calls = toolCalls
647- result . choices [ 0 ] . message . content = null
648- result . choices [ 0 ] . finish_reason = 'tool_calls'
649- }
650- }
652+ this . applyToolCallsToResponse ( result , request . model , request . tools )
651653
652- // Delete session after non-streaming request ends
653654 if ( deleteSessionCallback ) {
654655 await deleteSessionCallback ( )
655656 }
@@ -757,19 +758,8 @@ CRITICAL RULES:
757758
758759 const result = await handler . handleNonStream ( response . data )
759760
760- // Parse tool calls from response content if using prompt-based tool calling
761- if ( request . tools && request . tools . length > 0 && ! isNativeFunctionCallingModel ( request . model ) ) {
762- const content = result ?. choices ?. [ 0 ] ?. message ?. content || ''
763- const toolCalls = this . parseToolCallsFromContent ( content )
764-
765- if ( toolCalls && toolCalls . length > 0 ) {
766- result . choices [ 0 ] . message . tool_calls = toolCalls
767- result . choices [ 0 ] . message . content = null
768- result . choices [ 0 ] . finish_reason = 'tool_calls'
769- }
770- }
761+ this . applyToolCallsToResponse ( result , request . model , request . tools )
771762
772- // Delete session after non-stream response
773763 if ( shouldDeleteSession ( ) ) {
774764 const convId = handler . getConversationId ( )
775765 if ( convId ) {
@@ -860,19 +850,8 @@ CRITICAL RULES:
860850
861851 const result = await handler . handleNonStream ( response . data )
862852
863- // Parse tool calls from response content if using prompt-based tool calling
864- if ( request . tools && request . tools . length > 0 && ! isNativeFunctionCallingModel ( request . model ) ) {
865- const content = result ?. choices ?. [ 0 ] ?. message ?. content || ''
866- const toolCalls = this . parseToolCallsFromContent ( content )
867-
868- if ( toolCalls && toolCalls . length > 0 ) {
869- result . choices [ 0 ] . message . tool_calls = toolCalls
870- result . choices [ 0 ] . message . content = null
871- result . choices [ 0 ] . finish_reason = 'tool_calls'
872- }
873- }
853+ this . applyToolCallsToResponse ( result , request . model , request . tools )
874854
875- // Delete conversation if needed
876855 if ( shouldDeleteSession ( ) ) {
877856 const realChatId = handler . getConversationId ( )
878857 if ( realChatId && realChatId . startsWith ( 'kimi-' ) === false ) {
@@ -967,17 +946,7 @@ CRITICAL RULES:
967946
968947 const result = await handler . handleNonStream ( response . data , response )
969948
970- // Parse tool calls from response content if using prompt-based tool calling
971- if ( request . tools && request . tools . length > 0 && ! isNativeFunctionCallingModel ( request . model ) ) {
972- const content = result ?. choices ?. [ 0 ] ?. message ?. content || ''
973- const toolCalls = this . parseToolCallsFromContent ( content )
974-
975- if ( toolCalls && toolCalls . length > 0 ) {
976- result . choices [ 0 ] . message . tool_calls = toolCalls
977- result . choices [ 0 ] . message . content = null
978- result . choices [ 0 ] . finish_reason = 'tool_calls'
979- }
980- }
949+ this . applyToolCallsToResponse ( result , request . model , request . tools )
981950
982951 const sid = handler . getSessionId ( )
983952 if ( deleteSessionCallback && sid ) {
@@ -1066,17 +1035,7 @@ CRITICAL RULES:
10661035
10671036 const result = await handler . handleNonStream ( response . data )
10681037
1069- // Parse tool calls from response content if using prompt-based tool calling
1070- if ( request . tools && request . tools . length > 0 && ! isNativeFunctionCallingModel ( request . model ) ) {
1071- const content = result ?. choices ?. [ 0 ] ?. message ?. content || ''
1072- const toolCalls = this . parseToolCallsFromContent ( content )
1073-
1074- if ( toolCalls && toolCalls . length > 0 ) {
1075- result . choices [ 0 ] . message . tool_calls = toolCalls
1076- result . choices [ 0 ] . message . content = null
1077- result . choices [ 0 ] . finish_reason = 'tool_calls'
1078- }
1079- }
1038+ this . applyToolCallsToResponse ( result , request . model , request . tools )
10801039
10811040 if ( deleteChatCallback ) {
10821041 await deleteChatCallback ( chatId )
@@ -1167,17 +1126,7 @@ CRITICAL RULES:
11671126
11681127 const result = await handler . handleNonStream ( response . data )
11691128
1170- // Parse tool calls from response content if using prompt-based tool calling
1171- if ( request . tools && request . tools . length > 0 && ! isNativeFunctionCallingModel ( request . model ) ) {
1172- const content = result ?. choices ?. [ 0 ] ?. message ?. content || ''
1173- const toolCalls = this . parseToolCallsFromContent ( content )
1174-
1175- if ( toolCalls && toolCalls . length > 0 ) {
1176- result . choices [ 0 ] . message . tool_calls = toolCalls
1177- result . choices [ 0 ] . message . content = null
1178- result . choices [ 0 ] . finish_reason = 'tool_calls'
1179- }
1180- }
1129+ this . applyToolCallsToResponse ( result , request . model , request . tools )
11811130
11821131 if ( deleteChatCallback ) {
11831132 await deleteChatCallback ( chatId )
@@ -1273,19 +1222,8 @@ CRITICAL RULES:
12731222 }
12741223
12751224 if ( response ) {
1276- // Parse tool calls from response content if using prompt-based tool calling
1277- if ( request . tools && request . tools . length > 0 && ! isNativeFunctionCallingModel ( request . model ) ) {
1278- const content = response . data ?. choices ?. [ 0 ] ?. message ?. content || ''
1279- const toolCalls = this . parseToolCallsFromContent ( content )
1280-
1281- if ( toolCalls && toolCalls . length > 0 ) {
1282- response . data . choices [ 0 ] . message . tool_calls = toolCalls
1283- response . data . choices [ 0 ] . message . content = null
1284- response . data . choices [ 0 ] . finish_reason = 'tool_calls'
1285- }
1286- }
1225+ this . applyToolCallsToResponse ( response . data , request . model , request . tools )
12871226
1288- // Response is already formatted as OpenAI-compatible format
12891227 if ( deleteChatCallback ) {
12901228 await deleteChatCallback ( chatId )
12911229 }
@@ -1454,19 +1392,8 @@ CRITICAL RULES:
14541392 const handler = new PerplexityStreamHandler ( actualModel , sessionId , undefined , adapter )
14551393 const result = await handler . handleNonStream ( stream )
14561394
1457- // Parse tool calls from response content if tools were provided
1458- if ( request . tools && request . tools . length > 0 && ! isNativeFunctionCallingModel ( request . model ) ) {
1459- const content = result ?. choices ?. [ 0 ] ?. message ?. content || ''
1460- const toolCalls = this . parseToolCallsFromContent ( content )
1461-
1462- if ( toolCalls && toolCalls . length > 0 ) {
1463- result . choices [ 0 ] . message . tool_calls = toolCalls
1464- result . choices [ 0 ] . message . content = null
1465- result . choices [ 0 ] . finish_reason = 'tool_calls'
1466- }
1467- }
1395+ this . applyToolCallsToResponse ( result , request . model , request . tools )
14681396
1469- // Delete session after non-stream response
14701397 if ( shouldDeleteSession ( ) ) {
14711398 await adapter . deleteSession ( sessionId )
14721399 }
0 commit comments