Commit b6d5ce4bc for llama.cpp
commit b6d5ce4bc9a3691d26b3207cc6c9c63b732d67e8
Author: Aleksander Grygier <aleksander.grygier@gmail.com>
Date: Mon Nov 23 16:18:59 2026 +0100
ui: drop tool calls cut off by Stop from the next request (#32738)
* ui: drop tool calls cut off by Stop from the next request
A tool call stopped mid-stream could stay on its message with cut-off
arguments. The server parses every call's arguments as JSON, so the next
message in that chat ("Continue") failed with "Failed to parse tool call
arguments as JSON". Requests now leave such calls out, with the tool results
that answer them, and send a call with no arguments as {}.
Assisted-by: Claude (Opus 5.5 Medium)
* ui: add a demo trace that ends in a stopped tool call
A conversation whose last tool call was stopped while its arguments were
still streaming, for recording the fix in the previous commit.
Assisted-by: Claude (Opus 5.5 Medium)
diff --git a/ui/apps/chat/demo/fixtures/traces.ts b/ui/apps/chat/demo/fixtures/traces.ts
index dc766458e..ce673a85e 100644
--- a/ui/apps/chat/demo/fixtures/traces.ts
+++ b/ui/apps/chat/demo/fixtures/traces.ts
@@ -47,6 +47,17 @@ class Trace {
this.push({ content: '', role: 'system', type: 'root' });
}
+ /** An assistant turn whose tool call was stopped mid-stream: cut-off arguments and no result. */
+ stoppedTool(name: string, cutArguments: string, content = ''): this {
+ const id = uuid(this.name, `call-${this.calls++}`).replaceAll('-', '');
+
+ return this.push({
+ content,
+ role: 'assistant',
+ toolCalls: [{ function: { arguments: cutArguments, name }, id, type: 'function' }]
+ });
+ }
+
toJsonl(): string {
const last = this.messages.at(-1)!;
const session = {
@@ -330,11 +341,38 @@ function diagrams(): Trace {
return trace;
}
+/** A chat whose last tool call was stopped while its arguments were still streaming. */
+function stoppedToolCall(): Trace {
+ const trace = new Trace('Smoke test for the slides page');
+
+ trace.user('Write a quick Playwright smoke test for slides.html and run it.');
+ trace.stoppedTool(
+ 'exec_shell_command',
+ JSON.stringify({
+ command: [
+ "cat > /tmp/slides-smoke.mjs <<'EOF'",
+ "import { chromium } from 'playwright';",
+ '',
+ 'const browser = await chromium.launch();',
+ 'const page = await browser.newPage();',
+ '',
+ "await page.goto('file:///tmp/slides.html');",
+ 'EOF',
+ 'node /tmp/slides-smoke.mjs'
+ ].join('\n')
+ }).slice(0, 160),
+ 'I will write the script to a file and run it.'
+ );
+
+ return trace;
+}
+
/** Built-in traces, by name. */
export const DEMO_TRACES: Record<string, () => Trace> = {
'agentic-debugging': agenticDebugging,
diagrams,
- 'long-chat': longChat
+ 'long-chat': longChat,
+ 'stopped-tool-call': stoppedToolCall
};
export function generateTrace(name: string): null | string {
diff --git a/ui/apps/chat/src/lib/services/chat.service.ts b/ui/apps/chat/src/lib/services/chat.service.ts
index 38c7eba14..9d0a146cd 100644
--- a/ui/apps/chat/src/lib/services/chat.service.ts
+++ b/ui/apps/chat/src/lib/services/chat.service.ts
@@ -8,6 +8,7 @@
import { getAudioInputFormat } from '../utils/audio-format';
import { capImageDataURLSize } from '../utils/cap-img-size';
+import { dropIncompleteToolCalls } from '../utils/incomplete-tool-calls';
import {
API_CHAT,
API_SLOTS,
@@ -851,8 +852,7 @@ export class ChatService {
model?: string | null
): Promise<ApiChatMessageData[]> {
const options = ChatService.conversionOptionsFor(model);
-
- return (
+ const converted = (
await Promise.all(
messages.map((msg) => {
if ('id' in msg && 'convId' in msg && 'timestamp' in msg) {
@@ -875,6 +875,10 @@ export class ChatService {
return true;
});
+
+ // a tool call cut off by Stop can still sit on the message: the server
+ // would reject the whole request on its arguments
+ return dropIncompleteToolCalls(converted);
}
/**
diff --git a/ui/apps/chat/src/lib/utils/incomplete-tool-calls.ts b/ui/apps/chat/src/lib/utils/incomplete-tool-calls.ts
new file mode 100644
index 000000000..62ba58061
--- /dev/null
+++ b/ui/apps/chat/src/lib/utils/incomplete-tool-calls.ts
@@ -0,0 +1,76 @@
+import { MessageRole } from '#lib/enums/index.ts';
+import type { ApiChatCompletionToolCall, ApiChatMessageData } from '#lib/types/api.d.ts';
+
+const EMPTY_ARGUMENTS = '{}';
+
+/**
+ * Drop the tool calls a stopped stream left with cut-off arguments, and the
+ * tool results that answer them. The server parses every call's arguments as
+ * JSON and rejects the whole request when one doesn't parse, so a call cut
+ * off mid-stream would otherwise break every request after it. A call with
+ * no arguments at all is kept and sent with `{}`.
+ */
+export function dropIncompleteToolCalls(messages: ApiChatMessageData[]): ApiChatMessageData[] {
+ const droppedIds = new Set<string>();
+ const result: ApiChatMessageData[] = [];
+
+ for (const message of messages) {
+ if (
+ message.role === MessageRole.TOOL &&
+ message.tool_call_id &&
+ droppedIds.has(message.tool_call_id)
+ ) {
+ continue;
+ }
+
+ if (!message.tool_calls?.length) {
+ result.push(message);
+
+ continue;
+ }
+
+ const calls = message.tool_calls;
+ const kept: ApiChatCompletionToolCall[] = [];
+
+ for (const call of calls) {
+ const sendable = sendableToolCall(call);
+
+ if (sendable) {
+ kept.push(sendable);
+ } else if (call.id) {
+ droppedIds.add(call.id);
+ }
+ }
+
+ if (kept.length === calls.length && kept.every((call, i) => call === calls[i])) {
+ result.push(message);
+
+ continue;
+ }
+
+ const { tool_calls: _dropped, ...rest } = message;
+
+ result.push(kept.length > 0 ? { ...rest, tool_calls: kept } : rest);
+ }
+
+ return result;
+}
+
+/** The call as it can be sent, or null when its name or arguments were cut off. */
+function sendableToolCall(call: ApiChatCompletionToolCall): ApiChatCompletionToolCall | null {
+ if (!call.function?.name) return null;
+
+ const args = call.function.arguments;
+
+ if (!args?.trim()) {
+ return { ...call, function: { ...call.function, arguments: EMPTY_ARGUMENTS } };
+ }
+
+ try {
+ JSON.parse(args);
+
+ return call;
+ } catch {
+ return null;
+ }
+}
diff --git a/ui/apps/chat/tests/unit/incomplete-tool-calls.test.ts b/ui/apps/chat/tests/unit/incomplete-tool-calls.test.ts
new file mode 100644
index 000000000..f287b1cbf
--- /dev/null
+++ b/ui/apps/chat/tests/unit/incomplete-tool-calls.test.ts
@@ -0,0 +1,69 @@
+import { MessageRole } from '#lib/enums/index.ts';
+import type { ApiChatCompletionToolCall, ApiChatMessageData } from '#lib/types/api.d.ts';
+import { dropIncompleteToolCalls } from '#lib/utils/incomplete-tool-calls.ts';
+import { describe, expect, it } from 'vitest';
+
+function call(id: string, args: string, name = 'exec_shell_command'): ApiChatCompletionToolCall {
+ return { function: { arguments: args, name }, id, type: 'function' };
+}
+
+const CUT = '{"command": "cd /repo && cat > /tmp/x.mjs <<\'EOF\'\\nimport';
+
+describe('dropIncompleteToolCalls', () => {
+ it('leaves complete tool calls and their results alone', () => {
+ const messages: ApiChatMessageData[] = [
+ { content: 'run it', role: MessageRole.USER },
+ { content: '', role: MessageRole.ASSISTANT, tool_calls: [call('a', '{"command":"ls"}')] },
+ { content: 'file.txt', role: MessageRole.TOOL, tool_call_id: 'a' }
+ ];
+ const result = dropIncompleteToolCalls(messages);
+
+ expect(result).toEqual(messages);
+ expect(result[1]).toBe(messages[1]);
+ });
+
+ it('drops a call whose arguments were cut off, keeping the text of the message', () => {
+ const result = dropIncompleteToolCalls([
+ {
+ content: 'Let me write the script.',
+ role: MessageRole.ASSISTANT,
+ tool_calls: [call('a', CUT)]
+ },
+ { content: 'Continue', role: MessageRole.USER }
+ ]);
+
+ expect(result).toEqual([
+ { content: 'Let me write the script.', role: MessageRole.ASSISTANT },
+ { content: 'Continue', role: MessageRole.USER }
+ ]);
+ });
+
+ it('keeps the complete calls of a message and drops the results of the cut one', () => {
+ const result = dropIncompleteToolCalls([
+ {
+ content: '',
+ role: MessageRole.ASSISTANT,
+ tool_calls: [call('a', '{"command":"ls"}'), call('b', CUT)]
+ },
+ { content: 'file.txt', role: MessageRole.TOOL, tool_call_id: 'a' },
+ { content: 'error', role: MessageRole.TOOL, tool_call_id: 'b' }
+ ]);
+
+ expect(result).toEqual([
+ { content: '', role: MessageRole.ASSISTANT, tool_calls: [call('a', '{"command":"ls"}')] },
+ { content: 'file.txt', role: MessageRole.TOOL, tool_call_id: 'a' }
+ ]);
+ });
+
+ it('drops a call with no name and sends a call with no arguments as {}', () => {
+ const result = dropIncompleteToolCalls([
+ {
+ content: '',
+ role: MessageRole.ASSISTANT,
+ tool_calls: [call('a', '{}', ''), call('b', '', 'list_tasks')]
+ }
+ ]);
+
+ expect(result[0].tool_calls).toEqual([call('b', '{}', 'list_tasks')]);
+ });
+});