import { ToolCallDelta, ToolCallState } from "core"; import { incrementalParseJson } from "core/util/incrementalParseJson"; // Merge streamed tool calls // See example of data coming in here: // https://platform.openai.com/docs/guides/function-calling?api-mode=chat#streaming export function addToolCallDeltaToState( toolCallDelta: ToolCallDelta, currentState: ToolCallState | undefined, ): ToolCallState { const currentCall = currentState?.toolCall; // If we have a current state and the delta has a different ID, ignore the delta if ( currentState && toolCallDelta.id && currentCall?.id !== toolCallDelta.id ) { return currentState; } // These will/should not be partially streamed const callType = toolCallDelta.type ?? "function"; const callId = currentCall?.id || toolCallDelta.id || ""; // These may be streamed in chunks const currentName = currentCall?.function.name ?? ""; const currentArgs = currentCall?.function.arguments ?? ""; const nameDelta = toolCallDelta.function?.name ?? ""; const argsDelta = toolCallDelta.function?.arguments ?? ""; let mergedName = currentName; if (nameDelta.startsWith(currentName)) { // Case where model progresssively streams name but full name each time e.g. "readFi" -> "readFil" -> "readFile" mergedName = nameDelta; } else if (!currentName.startsWith(nameDelta)) { mergedName = currentName + nameDelta; } // Similar logic for args, with an extra JSON check let mergedArgs = currentArgs; try { // If args is JSON parseable, it is complete, don't add to it JSON.parse(currentArgs); } catch (e) { if (argsDelta.startsWith(currentArgs)) { // Case where model progresssively streams args but full args each time e.g. "{"file": "file1"}" -> "{"file": "file1", "line": 1}" mergedArgs = argsDelta; } else if (!currentArgs.startsWith(argsDelta)) { // Case where model streams in args in parts e.g. "{"file": "file1"" -> ", "line": 1}" mergedArgs = currentArgs + argsDelta; } } const [_, parsedArgs] = incrementalParseJson(mergedArgs || "{}"); return { status: "generating", toolCall: { id: callId, type: callType, function: { name: mergedName, arguments: mergedArgs, }, }, toolCallId: callId, parsedArgs, }; }