From a7d2dba1eb7722ceadb5100a174c6c7d42cf551f Mon Sep 17 00:00:00 2001 From: Diego Rodrigues de Sa e Souza Date: Sun, 9 Aug 2026 09:52:54 -0300 Subject: [PATCH] fix(types): narrow DeepSeek tool calls (#9860) Co-authored-by: backryun --- open-sse/executors/deepseek-web.ts | 6 +++--- tests/unit/deepseek-web-tool-result-prompt-4712.test.ts | 8 ++++++++ 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/open-sse/executors/deepseek-web.ts b/open-sse/executors/deepseek-web.ts index 99fda068af..4f3314ca8d 100644 --- a/open-sse/executors/deepseek-web.ts +++ b/open-sse/executors/deepseek-web.ts @@ -515,7 +515,6 @@ export function messagesToPrompt( historyWindow = 0 ): string { if (messages.length === 0) return ""; - const systemParts: string[] = []; const conversation: Array<{ role: string; text: string }> = []; const callNameById = new Map(); @@ -527,8 +526,9 @@ export function messagesToPrompt( } else if (m.role === "user" || m.role === "assistant") { if (text) conversation.push({ role: m.role, text }); if (m.role === "user") lastUserContent = text; - const calls = Array.isArray((m as { tool_calls?: unknown }).tool_calls) - ? (m as { tool_calls: Array<{ id?: string; function?: { name?: string } }> }).tool_calls + const toolCalls = (m as { tool_calls?: unknown }).tool_calls; + const calls = Array.isArray(toolCalls) + ? (toolCalls as Array<{ id?: string; function?: { name?: string } }>) : []; for (const c of calls) { if (c?.id && typeof c.function?.name === "string") callNameById.set(c.id, c.function.name); diff --git a/tests/unit/deepseek-web-tool-result-prompt-4712.test.ts b/tests/unit/deepseek-web-tool-result-prompt-4712.test.ts index 38136f270d..eed0311b8c 100644 --- a/tests/unit/deepseek-web-tool-result-prompt-4712.test.ts +++ b/tests/unit/deepseek-web-tool-result-prompt-4712.test.ts @@ -64,3 +64,11 @@ test("messagesToPrompt still drops empty tool results without crashing (#4712)", assert.match(prompt, /hello/); assert.match(prompt, /world/); }); + +test("messagesToPrompt ignores malformed assistant tool calls", () => { + const prompt = messagesToPrompt([ + { role: "assistant", content: "thinking", tool_calls: { id: "not-an-array" } }, + { role: "user", content: "continue" }, + ]); + assert.match(prompt, /continue/); +});