π€― LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.
TypeScript agentagent-collaborationagent-harnessloop-engineeringmcppersonal-assistantproactive-agentskillssuper-agenttask-management
π fix: stream tool call arguments incrementally in Response API (#13506)
Arvin Xu126db9612fb7
diff --git a/packages/openapi/src/services/responses.service.ts b/packages/openapi/src/services/responses.service.tsindex 153c612badf..51a3a8c5ea7 100644--- a/packages/openapi/src/services/responses.service.ts+++ b/packages/openapi/src/services/responses.service.ts@@ -467,6 +467,12 @@ export class ResponsesService extends BaseService {let currentOutputIndex = 0;let itemCounter = 0;let textMessageStarted = false;++ // Track active (in-progress) tool calls for proper incremental streaming+ const activeToolCalls = new Map<+ string,+ { fcItemId: string; name: string; outputIndex: number; prevArguments: string }+ >();let currentTextItemId = '';const startTextMessage = function* (seq: { n: number }) {@@ -532,6 +538,35 @@ export class ResponsesService extends BaseService {currentOutputIndex++;};+ const finishActiveToolCalls = function* (seq: { n: number }) {+ for (const [callId, tc] of activeToolCalls) {+ yield {+ arguments: tc.prevArguments || '{}',+ item_id: tc.fcItemId,+ output_index: tc.outputIndex,+ sequence_number: seq.n++,+ type: 'response.function_call_arguments.done' as const,+ };+ yield {+ item: {+ arguments: tc.prevArguments || '{}',+ call_id: callId,+ id: tc.fcItemId,+ name: tc.name,+ status: 'completed' as const,+ type: 'function_call' as const,+ } as OutputItem,+ output_index: tc.outputIndex,+ sequence_number: seq.n++,+ type: 'response.output_item.done' as const,+ };+ }+ if (activeToolCalls.size > 0) {+ currentOutputIndex += activeToolCalls.size;+ activeToolCalls.clear();+ }+ };+// Shared mutable sequence counter for generatorsconst seq = { n: sequenceNumber };@@ -563,69 +598,77 @@ export class ResponsesService extends BaseService {yield* finishTextMessage(seq, accumulatedText);accumulatedText = '';- // Emit function_call output items for each tool call+ // Stream tool call deltas incrementally within stable output itemsfor (const toolCall of chunk.toolsCalling) {- const fcItemId = `fc_${responseId}_${itemCounter++}`;- // Client function tools use original name; hosted tools use identifier/apiName- const isClientTool = toolCall.identifier === 'lobe-client-fn';- const toolDisplayName = isClientTool- ? toolCall.apiName- : `${toolCall.identifier}/${toolCall.apiName}`;- yield {- item: {- arguments: toolCall.arguments ?? '{}',- call_id: toolCall.id,- id: fcItemId,+ const callId = toolCall.id;+ const existing = activeToolCalls.get(callId);++ if (!existing) {+ // First time seeing this tool call β emit output_item.added+ const fcItemId = `fc_${responseId}_${itemCounter++}`;+ const isClientTool = toolCall.identifier === 'lobe-client-fn';+ const toolDisplayName = isClientTool+ ? toolCall.apiName+ : `${toolCall.identifier}/${toolCall.apiName}`;+ const outputIndex = currentOutputIndex + activeToolCalls.size;++ activeToolCalls.set(callId, {+ fcItemId,name: toolDisplayName,- status: 'in_progress' as const,- type: 'function_call' as const,- } as OutputItem,- output_index: currentOutputIndex,- sequence_number: seq.n++,- type: 'response.output_item.added' as const,- };-- // Emit arguments delta- if (toolCall.arguments) {+ outputIndex,+ prevArguments: '',+ });+yield {- delta: toolCall.arguments,- item_id: fcItemId,- output_index: currentOutputIndex,+ item: {+ arguments: '',+ call_id: callId,+ id: fcItemId,+ name: toolDisplayName,+ status: 'in_progress' as const,+ type: 'function_call' as const,+ } as OutputItem,+ output_index: outputIndex,sequence_number: seq.n++,- type: 'response.function_call_arguments.delta' as const,+ type: 'response.output_item.added' as const,};- }- // Emit arguments done- yield {- arguments: toolCall.arguments ?? '{}',- item_id: fcItemId,- output_index: currentOutputIndex,- sequence_number: seq.n++,- type: 'response.function_call_arguments.done' as const,- };-- // Complete the function_call item- yield {- item: {- arguments: toolCall.arguments ?? '{}',- call_id: toolCall.id,- id: fcItemId,- name: toolDisplayName,- status: 'completed' as const,- type: 'function_call' as const,- } as OutputItem,- output_index: currentOutputIndex,- sequence_number: seq.n++,- type: 'response.output_item.done' as const,- };- currentOutputIndex++;+ // Emit initial delta if arguments already present+ if (toolCall.arguments) {+ activeToolCalls.get(callId)!.prevArguments = toolCall.arguments;+ yield {+ delta: toolCall.arguments,+ item_id: fcItemId,+ output_index: outputIndex,+ sequence_number: seq.n++,+ type: 'response.function_call_arguments.delta' as const,+ };+ }+ } else {+ // Subsequent chunk β compute incremental delta+ const currentArgs = toolCall.arguments ?? '';+ const delta = currentArgs.slice(existing.prevArguments.length);++ if (delta) {+ existing.prevArguments = currentArgs;+ yield {+ delta,+ item_id: existing.fcItemId,+ output_index: existing.outputIndex,+ sequence_number: seq.n++,+ type: 'response.function_call_arguments.delta' as const,+ };+ }+ }}} else if (chunk.chunkType === 'reasoning' && chunk.reasoning) {// Emit reasoning as text delta (within a text message)yield* startTextMessage(seq);}} else if (event.type === 'tool_end') {+ // Finalize any remaining active tool calls before emitting tool output+ yield* finishActiveToolCalls(seq);+// Emit function_call_output for completed tool executionconst toolData = event.data as {isSuccess: boolean;@@ -659,10 +702,17 @@ export class ResponsesService extends BaseService {type: 'response.output_item.done' as const,};currentOutputIndex++;+ } else if (event.type === 'stream_retry') {+ // LLM retry β discard stale tool call state from the failed attempt+ // so we don't emit phantom function_calls on final flush+ activeToolCalls.clear();}}}+ // Finalize any in-progress tool calls+ yield* finishActiveToolCalls(seq);+// Close any remaining open text messageyield* finishTextMessage(seq, accumulatedText);sequenceNumber = seq.n;