@@ -132,7 +132,10 @@ export function responsesSseToChatCompletionsSse(
132132 // tool call_id -> streaming index (OpenAI requires stable indices per tool call)
133133 const toolIndexByCallId = new Map < string , number > ( ) ;
134134 const toolIndexByItemId = new Map < string , number > ( ) ;
135+ const toolCallIdByIndex = new Map < number , string > ( ) ;
135136 const toolNameByIndex = new Map < number , string > ( ) ;
137+ const toolArgumentsByIndex = new Map < number , string > ( ) ;
138+ const emittedToolIndexes = new Set < number > ( ) ;
136139 let nextToolIndex = 0 ;
137140 let sseIterator : AsyncGenerator < { event ?: string ; data : string } > | undefined ;
138141 const upstreamAbort = new AbortController ( ) ;
@@ -166,8 +169,47 @@ export function responsesSseToChatCompletionsSse(
166169 frame . choices = [ { index : 0 , delta : { reasoning_content : text } , finish_reason : null } ] ;
167170 emit ( frame ) ;
168171 } ;
172+ const resolveFinalArguments = ( candidate : unknown , streamed : string ) => {
173+ if ( typeof candidate !== "string" ) return streamed ;
174+ // Some compatible providers send an empty final snapshot. Do not let that erase
175+ // non-empty streamed arguments; genuinely empty calls have no buffered content.
176+ return candidate . length > 0 || streamed . length === 0 ? candidate : streamed ;
177+ } ;
178+ const emitToolCall = ( toolIndex : number , callId : string , name : string , args : string ) => {
179+ if ( ! callId || emittedToolIndexes . has ( toolIndex ) ) return ;
180+ emittedToolIndexes . add ( toolIndex ) ;
181+ ensureRole ( ) ;
182+ const frame = chunkBase ( id , model , created ) ;
183+ frame . choices = [ {
184+ index : 0 ,
185+ delta : {
186+ tool_calls : [ {
187+ index : toolIndex ,
188+ id : callId ,
189+ type : "function" ,
190+ function : { name, arguments : args } ,
191+ } ] ,
192+ } ,
193+ finish_reason : null ,
194+ } ] ;
195+ emit ( frame ) ;
196+ } ;
197+ const flushPendingToolCalls = ( ) => {
198+ const pending = [ ...toolCallIdByIndex . entries ( ) ] . sort ( ( [ a ] , [ b ] ) => a - b ) ;
199+ for ( const [ toolIndex , callId ] of pending ) {
200+ emitToolCall (
201+ toolIndex ,
202+ callId ,
203+ toolNameByIndex . get ( toolIndex ) ?? "" ,
204+ toolArgumentsByIndex . get ( toolIndex ) ?? "" ,
205+ ) ;
206+ }
207+ } ;
169208 const finish = ( finishReason : string , usage : unknown ) => {
170209 if ( terminated ) return ;
210+ // A valid completed/incomplete terminal frame may arrive without output_item.done.
211+ // Preserve any known tool call before emitting its finish reason.
212+ flushPendingToolCalls ( ) ;
171213 terminated = true ;
172214 ensureRole ( ) ;
173215 const frame = chunkBase ( id , model , created ) ;
@@ -232,24 +274,15 @@ export function responsesSseToChatCompletionsSse(
232274 toolIndex = nextToolIndex ++ ;
233275 toolIndexByCallId . set ( callId , toolIndex ) ;
234276 }
277+ toolCallIdByIndex . set ( toolIndex , callId ) ;
235278 if ( typeof item . id === "string" ) toolIndexByItemId . set ( item . id , toolIndex ) ;
236279 if ( name ) toolNameByIndex . set ( toolIndex , name ) ;
237- const frame = chunkBase ( id , model , created ) ;
238- frame . choices = [ {
239- index : 0 ,
240- delta : {
241- tool_calls : [ {
242- index : toolIndex ,
243- id : callId ,
244- type : "function" ,
245- // Always include name so clients that replace (not merge) tool_calls
246- // deltas never end up with function.name-less history.
247- function : { name, arguments : "" } ,
248- } ] ,
249- } ,
250- finish_reason : null ,
251- } ] ;
252- emit ( frame ) ;
280+ if ( ! toolArgumentsByIndex . has ( toolIndex ) ) {
281+ toolArgumentsByIndex . set (
282+ toolIndex ,
283+ typeof item . arguments === "string" ? item . arguments : "" ,
284+ ) ;
285+ }
253286 break ;
254287 }
255288 case "response.function_call_arguments.delta" : {
@@ -258,23 +291,24 @@ export function responsesSseToChatCompletionsSse(
258291 const toolIndex = ( itemId ? toolIndexByItemId . get ( itemId ) : undefined )
259292 ?? ( nextToolIndex > 0 ? nextToolIndex - 1 : 0 ) ;
260293 ensureRole ( ) ;
261- const knownName = toolNameByIndex . get ( toolIndex ) ?? "" ;
262- const fn : Rec = { arguments : data . delta } ;
263- // Re-emit name on every args delta: GitHub Copilot App / some ChatGPT clients
264- // replace the whole function object instead of merging, which otherwise drops name.
265- if ( knownName ) fn . name = knownName ;
266- const frame = chunkBase ( id , model , created ) ;
267- frame . choices = [ {
268- index : 0 ,
269- delta : {
270- tool_calls : [ {
271- index : toolIndex ,
272- function : fn ,
273- } ] ,
274- } ,
275- finish_reason : null ,
276- } ] ;
277- emit ( frame ) ;
294+ toolArgumentsByIndex . set (
295+ toolIndex ,
296+ ( toolArgumentsByIndex . get ( toolIndex ) ?? "" ) + data . delta ,
297+ ) ;
298+ break ;
299+ }
300+ case "response.function_call_arguments.done" : {
301+ const itemId = typeof data . item_id === "string" ? data . item_id : undefined ;
302+ const toolIndex = itemId ? toolIndexByItemId . get ( itemId ) : undefined ;
303+ if ( toolIndex === undefined ) break ;
304+ sawToolUse = true ;
305+ const name = typeof data . name === "string" ? data . name : "" ;
306+ if ( name ) toolNameByIndex . set ( toolIndex , name ) ;
307+ const streamedArgs = toolArgumentsByIndex . get ( toolIndex ) ?? "" ;
308+ toolArgumentsByIndex . set (
309+ toolIndex ,
310+ resolveFinalArguments ( data . arguments , streamedArgs ) ,
311+ ) ;
278312 break ;
279313 }
280314 case "response.output_item.done" : {
@@ -284,34 +318,25 @@ export function responsesSseToChatCompletionsSse(
284318 sawToolUse = true ;
285319 const callId = typeof item . call_id === "string" ? item . call_id : "" ;
286320 const name = typeof item . name === "string" ? item . name : "" ;
287- const args = typeof item . arguments === "string" ? item . arguments : "" ;
288321 if ( ! callId ) break ;
289- const existingIndex = toolIndexByCallId . get ( callId ) ;
290- const isNew = existingIndex === undefined ;
322+ const itemId = typeof item . id === "string" ? item . id : undefined ;
323+ const existingIndex = toolIndexByCallId . get ( callId )
324+ ?? ( itemId ? toolIndexByItemId . get ( itemId ) : undefined ) ;
291325 const toolIndex = existingIndex ?? nextToolIndex ++ ;
292- if ( isNew ) toolIndexByCallId . set ( callId , toolIndex ) ;
293- if ( typeof item . id === "string" ) toolIndexByItemId . set ( item . id , toolIndex ) ;
326+ toolIndexByCallId . set ( callId , toolIndex ) ;
327+ toolCallIdByIndex . set ( toolIndex , callId ) ;
328+ if ( itemId ) toolIndexByItemId . set ( itemId , toolIndex ) ;
294329 if ( name ) toolNameByIndex . set ( toolIndex , name ) ;
295330 const finalName = name || toolNameByIndex . get ( toolIndex ) || "" ;
296- // Last-write-wins: the done-frame snapshot is authoritative final arguments.
297- // Always re-emit full identity (id/type/name) so replace-style clients keep function.name.
298- if ( args . length > 0 || isNew || finalName ) {
299- ensureRole ( ) ;
300- const frame = chunkBase ( id , model , created ) ;
301- frame . choices = [ {
302- index : 0 ,
303- delta : {
304- tool_calls : [ {
305- index : toolIndex ,
306- id : callId ,
307- type : "function" ,
308- function : { name : finalName , arguments : args } ,
309- } ] ,
310- } ,
311- finish_reason : null ,
312- } ] ;
313- emit ( frame ) ;
314- }
331+ const streamedArgs = toolArgumentsByIndex . get ( toolIndex ) ?? "" ;
332+ // A Responses stream can carry incremental/finalized argument events plus an
333+ // authoritative output_item.done snapshot. Chat Completions tool-call fields are
334+ // append-only deltas, so forwarding multiple representations corrupts clients that
335+ // accumulate them. Emit one complete call here; finish() flushes any item whose
336+ // done event was omitted, and replace-style clients still get a complete object.
337+ const args = resolveFinalArguments ( item . arguments , streamedArgs ) ;
338+ toolArgumentsByIndex . set ( toolIndex , args ) ;
339+ emitToolCall ( toolIndex , callId , finalName , args ) ;
315340 }
316341 break ;
317342 }
0 commit comments