Merge pull request #6540 from continuedev/dallin/nvidia-templating

fix: Nvidia Agent and handle different arg streaming styles
This commit is contained in:
Nate Sesti 2025-07-09 19:19:33 -07:00 • committed by GitHub
commit 84daec27bd
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 116 additions and 6 deletions

View file

@ -46,6 +46,7 @@ import { PROVIDER_TOOL_SUPPORT } from "./toolSupport.js";
const PROVIDER_HANDLES_TEMPLATING: string[] = [
"lmstudio",
"openai",
"nvidia",
"ollama",
"together",
"novita",

View file

@ -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 });
});
});

View file

@ -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 || "{}");