Skip to content

Fix tool call name merging #6029

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
123 changes: 123 additions & 0 deletions gui/src/util/toolCallState.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,129 @@ describe("addToolCallDeltaToState", () => {
expect(result.toolCall.function.name).toBe("searchFiles");
});

it("should handle name streaming in full progressive chunks", () => {
// Test case where model streams the name progressively but includes full prefix each time
// e.g. "readFi" -> "readFil" -> "readFile"
const currentState: ToolCallState = {
status: "generating",
toolCall: {
id: "call123",
type: "function",
function: {
name: "readFi",
arguments: "{}",
},
},
toolCallId: "call123",
parsedArgs: {},
};

const delta: ToolCallDelta = {
function: {
name: "readFil",
},
};

const result = addToolCallDeltaToState(delta, currentState);
expect(result.toolCall.function.name).toBe("readFil");

// Continue the streaming
const nextDelta: ToolCallDelta = {
function: {
name: "readFile",
},
};

const finalResult = addToolCallDeltaToState(nextDelta, result);
expect(finalResult.toolCall.function.name).toBe("readFile");
});

it("should keep original name when receiving duplicate name chunks", () => {
// Test case where model streams the complete name multiple times
// e.g. "readFile" -> "readFile" -> "readFile"
const currentState: ToolCallState = {
status: "generating",
toolCall: {
id: "call123",
type: "function",
function: {
name: "readFile",
arguments: "{}",
},
},
toolCallId: "call123",
parsedArgs: {},
};

const delta: ToolCallDelta = {
function: {
name: "readFile",
},
};

const result = addToolCallDeltaToState(delta, currentState);
expect(result.toolCall.function.name).toBe("readFile");
});

it("should handle partial name streaming", () => {
// Test case where model streams the name in parts
// e.g. "read" -> "File"
const currentState: ToolCallState = {
status: "generating",
toolCall: {
id: "call123",
type: "function",
function: {
name: "read",
arguments: "{}",
},
},
toolCallId: "call123",
parsedArgs: {},
};

const delta: ToolCallDelta = {
function: {
name: "File",
},
};

const result = addToolCallDeltaToState(delta, currentState);
expect(result.toolCall.function.name).toBe("readFile");
});

it("should ignore new tool calls with different IDs", () => {
const currentState: ToolCallState = {
status: "generating",
toolCall: {
id: "call123",
type: "function",
function: {
name: "searchFiles",
arguments: '{"query":"test"}',
},
},
toolCallId: "call123",
parsedArgs: { query: "test" },
};

const delta: ToolCallDelta = {
id: "call456", // Different ID
type: "function",
function: {
name: "readFile",
arguments: '{"path":"file.txt"}',
},
};

const result = addToolCallDeltaToState(delta, currentState);

// Should keep the original state and ignore the new call
expect(result).toBe(currentState);
expect(result.toolCall.id).toBe("call123");
expect(result.toolCall.function.name).toBe("searchFiles");
});

it("should merge function argument deltas correctly", () => {
const currentState: ToolCallState = {
status: "generating",
Expand Down
23 changes: 21 additions & 2 deletions gui/src/util/toolCallState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@ export function addToolCallDeltaToState(
toolCallDelta: ToolCallDelta,
currentState: ToolCallState | undefined,
): ToolCallState {
// This prevents multiple tool calls for now, by ignoring new tool call ids
if (
toolCallDelta.id &&
currentState?.toolCallId &&
toolCallDelta.id !== currentState?.toolCallId
) {
return currentState;
}

const currentCall = currentState?.toolCall;

// These will/should not be partially streamed
Expand All @@ -21,8 +30,18 @@ export function addToolCallDeltaToState(
const nameDelta = toolCallDelta.function?.name ?? "";
const argsDelta = toolCallDelta.function?.arguments ?? "";

const mergedName =
currentName === nameDelta ? currentName : currentName + nameDelta; // Some models may include the name repeatedly. This doesn't account for an edge case where the name is like "dothisdothis" and it happens to stream name in chunks "dothis" and "dothis" but that's a super edge case
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)) {
// 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"
mergedName = currentName + nameDelta;
}

const mergedArgs = currentArgs + argsDelta;

const [_, parsedArgs] = incrementalParseJson(mergedArgs || "{}");
Expand Down
Loading