Skip to content

The all-in-one local AI studio for your desktop: chat, image and video generation and a coding agent in one free, open source app. Windows and Linux. No Docker, no terminal, no cloud required.

fix(codex): parse tool calls from markdown code fence

PurpleDoubleDcb226357af07
diff --git a/src/hooks/useCodex.ts b/src/hooks/useCodex.ts
index 0222cc44..333ffe0b 100644
--- a/src/hooks/useCodex.ts
+++ b/src/hooks/useCodex.ts
@@ -23,7 +23,7 @@ import { explainError as explainToolError } from '../api/agents/error-hints'
import { budgetFromSettings } from '../api/agents/budget'
import { finalStripThinkingTags } from '../lib/thinking-stripper'
import { ollamaUrl, localFetchStream } from '../api/backend'
-import { repairToolCallArgs } from '../lib/tool-call-repair'
+import { repairToolCallArgs, extractToolCallsFromContent } from '../lib/tool-call-repair'
const CODEX_SYSTEM_PROMPT = `You are Codex, an autonomous coding agent inside Locally Uncensored. You execute coding tasks end-to-end by reading files, writing code, and running shell commands. You MUST use tools — never guess file contents.
@@ -393,6 +393,25 @@ export function useCodex() {
thinkingContent += (thinkingContent ? '\n\n' : '') + turn.thinking
useChatStore.getState().updateMessageThinking(convId!, assistantMsg.id, thinkingContent)
}
+
+ // v2.5.0 fix (post-merge bug hunt): some Ollama models
+ // (qwen2.5-coder:3b confirmed) emit tool calls as a fenced
+ // ```json { "name":..., "arguments":... } ``` block inside
+ // message.content INSTEAD of the native message.tool_calls
+ // array. When the native list is empty but content looks like
+ // a tool call, extract it and strip the fence so the user
+ // doesn't see raw JSON.
+ if (toolCalls.length === 0 && turnContent) {
+ const extracted = extractToolCallsFromContent(turnContent)
+ if (extracted.length > 0) {
+ toolCalls = extracted.map(tc => ({ function: { name: tc.name, arguments: tc.arguments } }))
+ // Strip ```json ... ``` fences that wrap the extracted call
+ // so the chat bubble isn't just a big code block.
+ turnContent = turnContent
+ .replace(/```(?:json)?\s*\{[\s\S]*?\}\s*```/g, '')
+ .trim()
+ }
+ }
} else {
// ── Non-streaming fallback for OpenAI/Anthropic providers ──
let turn: { content: string; toolCalls: ToolCall[] }
diff --git a/src/lib/__tests__/tool-call-repair.test.ts b/src/lib/__tests__/tool-call-repair.test.ts
index 64c1a589..073f9ade 100644
--- a/src/lib/__tests__/tool-call-repair.test.ts
+++ b/src/lib/__tests__/tool-call-repair.test.ts
@@ -215,6 +215,32 @@ describe('tool-call-repair', () => {
expect(extractToolCallsFromContent('Hello, how are you?')).toEqual([])
})
+ // v2.5.0 regression test: qwen2.5-coder:3b emits tool calls wrapped
+ // in a markdown code-fence instead of Ollama's native tool_calls
+ // array. The extractor MUST find the call so useCodex can fall back.
+ it('extracts tool call from markdown code-fence (qwen2.5-coder pattern)', () => {
+ const content = '```json\n{\n "name": "file_read",\n "arguments": {\n "path": "demo.js"\n }\n}\n```'
+ const calls = extractToolCallsFromContent(content)
+ expect(calls).toHaveLength(1)
+ expect(calls[0].name).toBe('file_read')
+ expect(calls[0].arguments).toEqual({ path: 'demo.js' })
+ })
+
+ it('extracts multiline tool call with whitespace between keys', () => {
+ const content = `Some text before.
+\`\`\`json
+{
+ "name": "shell_execute",
+ "arguments": { "command": "ls -la" }
+}
+\`\`\`
+And after.`
+ const calls = extractToolCallsFromContent(content)
+ expect(calls).toHaveLength(1)
+ expect(calls[0].name).toBe('shell_execute')
+ expect(calls[0].arguments.command).toBe('ls -la')
+ })
+
it('returns empty array for empty string', () => {
expect(extractToolCallsFromContent('')).toEqual([])
})