From 1527345c712c5e3939eecd9da7a0cf37dc041857 Mon Sep 17 00:00:00 2001 From: Dallin Romney Date: Wed, 9 Jul 2025 15:38:33 -0700 Subject: [PATCH 1/2] handle different arg streaming styles --- core/llm/autodetect.ts | 3 +- gui/src/util/toolCallState.test.ts | 100 +++++++++++++++++++++++++++++ gui/src/util/toolCallState.ts | 21 ++++-- 3 files changed, 117 insertions(+), 7 deletions(-) diff --git a/core/llm/autodetect.ts b/core/llm/autodetect.ts index 9a3d37af9..91d14bfdc 100644 --- a/core/llm/autodetect.ts +++ b/core/llm/autodetect.ts @@ -46,7 +46,8 @@ import { PROVIDER_TOOL_SUPPORT } from "./toolSupport.js"; const PROVIDER_HANDLES_TEMPLATING: string[] = [ "lmstudio", "openai", - "ollama", + "nvidia", + "ollama", "together", "novita", "msty", diff --git a/gui/src/util/toolCallState.test.ts b/gui/src/util/toolCallState.test.ts index 3207b606a..486f26c66 100644 --- a/gui/src/util/toolCallState.test.ts +++ b/gui/src/util/toolCallState.test.ts @@ -479,4 +479,104 @@ describe("addToolCallDeltaToState", () => { ); expect(currentState.parsedArgs).toEqual({ location: "Paris, France" }); }); + + it("should handle arguments streamed in full progressive chunks", () => { + // Test case where model streams args progressively but includes full prefix each time + // e.g. '{"query":"te' -> '{"query":"tes' -> '{"query":"test"}' + const currentState: ToolCallState = { + status: "generating", + toolCall: { + id: "call123", + type: "function", + function: { + name: "searchFiles", + arguments: '{"query":"te', + }, + }, + toolCallId: "call123", + parsedArgs: { query: "te" }, + }; + + const delta: ToolCallDelta = { + function: { + arguments: '{"query":"tes', + }, + }; + + const result = addToolCallDeltaToState(delta, currentState); + expect(result.toolCall.function.arguments).toBe('{"query":"tes'); + expect(result.parsedArgs).toEqual({ query: "tes" }); + + // Continue the streaming + const nextDelta: ToolCallDelta = { + function: { + arguments: '{"query":"test"}', + }, + }; + + const finalResult = addToolCallDeltaToState(nextDelta, result); + expect(finalResult.toolCall.function.arguments).toBe('{"query":"test"}'); + expect(finalResult.parsedArgs).toEqual({ query: "test" }); + }); + + it("should handle when args delta is a superset of current args", () => { + // Test case where model sends a subset of the current arguments + // This can happen in certain LLM implementations + const currentState: ToolCallState = { + status: "generating", + toolCall: { + id: "call123", + type: "function", + function: { + name: "searchFiles", + arguments: '{"query":"test"', + }, + }, + toolCallId: "call123", + parsedArgs: { query: "test" }, + }; + + const delta: ToolCallDelta = { + function: { + arguments: '{"query":"test","limit":10}', + }, + }; + + // The subset should be ignored, keeping the current state + const result = addToolCallDeltaToState(delta, currentState); + expect(result.toolCall.function.arguments).toBe( + '{"query":"test","limit":10}', + ); + expect(result.parsedArgs).toEqual({ query: "test", limit: 10 }); + }); + + it("should handle when args are complete JSON and new deltas arrive", () => { + // When args are already a valid JSON object, new deltas should not modify it + const currentState: ToolCallState = { + status: "generating", + toolCall: { + id: "call123", + type: "function", + function: { + name: "searchFiles", + arguments: '{"query":"test","limit":10}', + }, + }, + toolCallId: "call123", + parsedArgs: { query: "test", limit: 10 }, + }; + + const delta: ToolCallDelta = { + function: { + arguments: ',"sort":"asc"}', + }, + }; + + // The complete JSON should not be modified + const result = addToolCallDeltaToState(delta, currentState); + expect(result.toolCall.function.arguments).toBe( + '{"query":"test","limit":10}', + ); + expect(result.parsedArgs).toEqual({ query: "test", limit: 10 }); + }); }); diff --git a/gui/src/util/toolCallState.ts b/gui/src/util/toolCallState.ts index e42ac6430..dab9b87b4 100644 --- a/gui/src/util/toolCallState.ts +++ b/gui/src/util/toolCallState.ts @@ -34,15 +34,24 @@ export function addToolCallDeltaToState( 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)) { - // Case where model streams in full name each time e.g. readFile -> readFile -> readFile - // do nothing - } else { - // Case where model streams in name in parts e.g. "read" -> "File" + } else if (!currentName.startsWith(nameDelta)) { mergedName = currentName + nameDelta; } - const mergedArgs = currentArgs + argsDelta; + // 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 || "{}"); From eea1b13e403835030067c7ed25abe8f73ba3c26f Mon Sep 17 00:00:00 2001 From: Dallin Romney Date: Wed, 9 Jul 2025 15:45:54 -0700 Subject: [PATCH 2/2] fix autodetect prettier --- core/llm/autodetect.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/llm/autodetect.ts b/core/llm/autodetect.ts index 91d14bfdc..a220b5dbd 100644 --- a/core/llm/autodetect.ts +++ b/core/llm/autodetect.ts @@ -47,7 +47,7 @@ const PROVIDER_HANDLES_TEMPLATING: string[] = [ "lmstudio", "openai", "nvidia", - "ollama", + "ollama", "together", "novita", "msty",