fix(ollama): add try/catch to JSON.parse in buildAssistantMessage

Responds to Greptile review feedback: add try/catch around JSON.parse
in buildAssistantMessage to match extractToolCalls, preventing potential
crash if Ollama returns malformed JSON string.
This commit is contained in:
Yaohua-Leo 2026-03-20 10:00:01 +08:00
parent 1ce0a6f025
commit 55f97ef257

View File

@ -367,7 +367,13 @@ export function buildAssistantMessage(
const rawArgs = tc.function.arguments;
const normalizedArgs =
typeof rawArgs === "string"
? (JSON.parse(rawArgs) as Record<string, unknown>)
? (() => {
try {
return JSON.parse(rawArgs) as Record<string, unknown>;
} catch {
return {} as Record<string, unknown>;
}
})()
: (rawArgs ?? {});
content.push({
type: "toolCall",