From 31da281d9b6784e2618d246caa6ef4d3b2419f4f Mon Sep 17 00:00:00 2001 From: LehaoLin Date: Sat, 21 Mar 2026 00:48:49 +0800 Subject: [PATCH] refactor(ollama): extract parseToolArgs helper for consistent tool argument parsing - Add shared parseToolArgs() function with try/catch - Use helper in both extractToolCalls() and buildAssistantMessage() - Reduces code duplication and ensures consistent error handling Addresses review feedback: https://github.com/openclaw/openclaw/pull/50724#issuecomment-4094662186 --- src/agents/ollama-stream.ts | 41 ++++++++++++++----------------------- 1 file changed, 15 insertions(+), 26 deletions(-) diff --git a/src/agents/ollama-stream.ts b/src/agents/ollama-stream.ts index 5e5cf9326c5..c06d0193279 100644 --- a/src/agents/ollama-stream.ts +++ b/src/agents/ollama-stream.ts @@ -216,6 +216,19 @@ interface OllamaChatResponse { eval_duration?: number; } +// ── Helpers ────────────────────────────────────────────────────────────────── + +function parseToolArgs(raw: unknown): Record { + if (typeof raw === "string") { + try { + return JSON.parse(raw) as Record; + } catch { + return {}; + } + } + return (raw as Record) ?? {}; +} + // ── Message conversion ────────────────────────────────────────────────────── type InputContentPart = @@ -254,19 +267,7 @@ function extractToolCalls(content: unknown): OllamaToolCall[] { const result: OllamaToolCall[] = []; for (const part of parts) { if (part.type === "toolCall") { - // Ensure arguments are always an object, not a string (OpenAI format passes JSON string) - const args = part.arguments; - let parsedArgs: Record; - if (typeof args === "string") { - try { - parsedArgs = JSON.parse(args); - } catch { - parsedArgs = {}; - } - } else { - parsedArgs = args ?? {}; - } - result.push({ function: { name: part.name, arguments: parsedArgs } }); + result.push({ function: { name: part.name, arguments: parseToolArgs(part.arguments) } }); } else if (part.type === "tool_use") { result.push({ function: { name: part.name, arguments: part.input ?? {} } }); } @@ -363,23 +364,11 @@ export function buildAssistantMessage( const toolCalls = response.message.tool_calls; if (toolCalls && toolCalls.length > 0) { for (const tc of toolCalls) { - // Normalize arguments: ensure it's always an object, not a string - const rawArgs = tc.function.arguments; - const normalizedArgs = - typeof rawArgs === "string" - ? (() => { - try { - return JSON.parse(rawArgs) as Record; - } catch { - return {} as Record; - } - })() - : (rawArgs ?? {}); content.push({ type: "toolCall", id: `ollama_call_${randomUUID()}`, name: tc.function.name, - arguments: normalizedArgs, + arguments: parseToolArgs(tc.function.arguments), }); } }