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.
TypeScript agentai-chatai-studioall-in-onecoding-agentdesktop-appfluximage-generationlinuxllmlocal-ailocal-llmopen-sourceprivacyself-hostedstable-diffusionuncensoreduncensored-aivideo-generationwindows
feat: agent production release — JSON repair, smart tools, retry, streaming
PurpleDoubleDe2bd5305facc
diff --git a/src/api/hermes-tool-calling.ts b/src/api/hermes-tool-calling.tsindex 5cd28766..796b6aab 100644--- a/src/api/hermes-tool-calling.ts+++ b/src/api/hermes-tool-calling.ts@@ -11,6 +11,7 @@*/import type { AgentToolDef } from '../types/agent-mode'+import { repairJson } from '../lib/tool-call-repair'// Generic tool shape accepted by the prompt buildertype ToolLike = { name: string; description: string; parameters?: any; inputSchema?: any }@@ -74,22 +75,22 @@ export function parseHermesToolCalls(output: string): ParsedToolCall[] {while ((match = regex.exec(output)) !== null) {const jsonStr = match[1].trim()- try {- const parsed = JSON.parse(jsonStr)- if (parsed.name) {- calls.push({- name: parsed.name,- arguments: parsed.arguments || parsed.parameters || {},- })- }- } catch {- // Try to extract name and arguments with regex fallback- const nameMatch = jsonStr.match(/"name"\s*:\s*"([^"]+)"/)- const argsMatch = jsonStr.match(/"arguments"\s*:\s*(\{[^}]*\})/)+ // Try direct parse, then repair+ const parsed = repairJson(jsonStr)+ if (parsed && parsed.name) {+ calls.push({+ name: parsed.name,+ arguments: parsed.arguments || parsed.parameters || {},+ })+ } else {+ // Last resort regex+ const nameMatch = jsonStr.match(/["']?name["']?\s*[:=]\s*["']([^"']+)["']/i)+ const argsMatch = jsonStr.match(/["']?arguments["']?\s*[:=]\s*(\{[\s\S]*?\})/i)if (nameMatch) {let args = {}if (argsMatch) {- try { args = JSON.parse(argsMatch[1]) } catch { /* ignore */ }+ const repaired = repairJson(argsMatch[1])+ if (repaired) args = repaired}calls.push({ name: nameMatch[1], arguments: args })}diff --git a/src/api/mcp/tool-registry.ts b/src/api/mcp/tool-registry.tsindex ff16e02e..db20db5a 100644--- a/src/api/mcp/tool-registry.ts+++ b/src/api/mcp/tool-registry.ts@@ -59,15 +59,27 @@ export class ToolRegistry {// ── Execution ─────────────────────────────────────────────────- async execute(name: string, args: Record<string, any>): Promise<string> {+ async execute(name: string, args: Record<string, any>, maxRetries = 1): Promise<string> {const entry = this.tools.get(name)if (!entry) return `Error: Unknown tool "${name}"`- try {- return await entry.executor(args)- } catch (err) {- const message = err instanceof Error ? err.message : String(err)- return `Error: ${message}`++ for (let attempt = 0; attempt <= maxRetries; attempt++) {+ try {+ const result = await entry.executor(args)+ // If result is an error and we have retries left, retry+ if (result.startsWith('Error:') && attempt < maxRetries) {+ // Only retry on transient errors (timeout, network)+ const isTransient = result.includes('timed out') || result.includes('ECONNREFUSED') || result.includes('fetch failed')+ if (isTransient) continue+ }+ return result+ } catch (err) {+ if (attempt < maxRetries) continue+ const message = err instanceof Error ? err.message : String(err)+ return `Error: ${message}`+ }}+ return `Error: Max retries exceeded for "${name}"`}// ── Format Conversion ─────────────────────────────────────────diff --git a/src/api/providers/anthropic-provider.ts b/src/api/providers/anthropic-provider.tsindex 57e1c6b2..e8f86c4a 100644--- a/src/api/providers/anthropic-provider.ts+++ b/src/api/providers/anthropic-provider.ts@@ -195,7 +195,7 @@ export class AnthropicProvider implements ProviderClient {id: block.id,function: {name: block.name!,- arguments: block.input || {},+ arguments: (typeof block.input === 'object' && block.input) ? block.input : {},},})}diff --git a/src/api/providers/ollama-provider.ts b/src/api/providers/ollama-provider.tsindex b1c5b041..f23b5edc 100644--- a/src/api/providers/ollama-provider.ts+++ b/src/api/providers/ollama-provider.ts@@ -12,6 +12,7 @@ import type {import { ProviderError } from './types'import { isTauri, localFetch, localFetchStream } from '../backend'import { parseNDJSONStream } from '../stream'+import { repairToolCallArgs, extractToolCallsFromContent } from '../../lib/tool-call-repair'// ── Ollama-specific types ──────────────────────────────────────@@ -152,10 +153,18 @@ export class OllamaProvider implements ProviderClient {}const data = await res.json()- const toolCalls: ToolCall[] = (data.message?.tool_calls || []).map((tc: any) => ({- function: { name: tc.function.name, arguments: tc.function.arguments },+ let toolCalls: ToolCall[] = (data.message?.tool_calls || []).map((tc: any) => ({+ function: { name: tc.function.name, arguments: repairToolCallArgs(tc.function.arguments) },}))+ // If no tool calls found but content looks like a tool call, try to extract+ if (toolCalls.length === 0 && data.message?.content) {+ const extracted = extractToolCallsFromContent(data.message.content)+ if (extracted.length > 0) {+ toolCalls = extracted.map(tc => ({ function: tc }))+ }+ }+return {content: data.message?.content || '',thinking: data.message?.thinking || '',diff --git a/src/api/providers/openai-provider.ts b/src/api/providers/openai-provider.tsindex d2391fe2..89d73914 100644--- a/src/api/providers/openai-provider.ts+++ b/src/api/providers/openai-provider.ts@@ -15,6 +15,7 @@ import type {} from './types'import { ProviderError } from './types'import { parseSSEStream } from '../sse'+import { repairJson } from '../../lib/tool-call-repair'// ── OpenAI API Types ───────────────────────────────────────────@@ -315,7 +316,8 @@ export class OpenAIProvider implements ProviderClient {try {return JSON.parse(args)} catch {- return {}+ const repaired = repairJson(args)+ return repaired && typeof repaired === 'object' ? repaired : {}}}diff --git a/src/components/chat/ChatView.tsx b/src/components/chat/ChatView.tsxindex 6caf644e..796c6e3e 100644--- a/src/components/chat/ChatView.tsx+++ b/src/components/chat/ChatView.tsx@@ -235,7 +235,7 @@ export function ChatView() {)}</button>- {/* Agent Mode (Beta) */}+ {/* Agent Mode */}{FEATURE_FLAGS.AGENT_MODE && (<div className={'flex items-center gap-1 px-2 py-0.5 rounded border transition-colors text-[0.55rem] ' +@@ -246,10 +246,7 @@ export function ChatView() {: 'border-gray-200 dark:border-white/[0.06] text-gray-500')}><Bot size={10} />- <div className="flex flex-col items-start leading-none">- <span className="text-[0.35rem] text-amber-400 font-bold uppercase tracking-widest">Beta</span>- <span>Agent</span>- </div>+ <span>Agent</span><AgentModeToggle /></div>)}diff --git a/src/components/chat/__tests__ b/src/components/chat/__tests__new file mode 100644index 00000000..e69de29bdiff --git a/src/hooks/useAgentChat.ts b/src/hooks/useAgentChat.tsindex 90cd0d1b..900b0586 100644--- a/src/hooks/useAgentChat.ts+++ b/src/hooks/useAgentChat.ts@@ -22,6 +22,7 @@ import { buildExtractionPrompt, parseExtractionResponse } from '../lib/memory-eximport { useAgentWorkflowStore } from '../stores/agentWorkflowStore'import { WorkflowEngine } from '../lib/workflow-engine'import type { AgentBlock, AgentToolCall, OllamaChatMessage } from '../types/agent-mode'+import { selectRelevantTools } from '../lib/tool-selection'import type { ChatMessage, ToolCall, ToolDefinition } from '../api/providers/types'import type { StepResult, WorkflowEngineCallbacks } from '../types/agent-workflows'@@ -100,6 +101,11 @@ export function useAgentChat() {useChatStore.getState().updateMessageAgentBlocks(convId, msgId, blocksRef.current)}+ function removeBlock(convId: string, msgId: string, blockId: string) {+ blocksRef.current = blocksRef.current.filter(b => b.id !== blockId)+ useChatStore.getState().updateMessageAgentBlocks(convId, msgId, blocksRef.current)+ }+function updateLastBlock(convId: string, msgId: string, updates: Partial<AgentBlock>) {const blocks = [...blocksRef.current]const last = blocks[blocks.length - 1]@@ -344,10 +350,25 @@ export function useAgentChat() {) as ChatMessage[]if (strategy === 'native') {- // ── Native tool calling (works with Ollama, OpenAI, Anthropic) ──- const tools: ToolDefinition[] = toolRegistry.toOpenAITools(permissions)+ // Show thinking indicator while model processes+ const thinkingBlockId = uuid()+ addBlock(convId!, assistantMessage.id, {+ id: thinkingBlockId, phase: 'thinking', content: 'Analyzing...',+ timestamp: Date.now(),+ })++ // Intelligent tool selection — only include relevant tools+ const lastUserMsg = agentMessages.filter(m => m.role === 'user').pop()?.content || ''+ const relevantDefs = selectRelevantTools(lastUserMsg, toolRegistry.getAll(), permissions)+ const tools: ToolDefinition[] = relevantDefs.map(t => ({+ type: 'function' as const,+ function: { name: t.name, description: t.description, parameters: t.inputSchema },+ }))const turn = await provider.chatWithTools(modelToUse, agentMessages, tools, chatOptions)+ // Remove thinking indicator+ removeBlock(convId!, assistantMessage.id, thinkingBlockId)+toolCalls = turn.toolCallsturnContent = turn.content || ''// Native thinking field from Ollamadiff --git a/src/hooks/useCodex.ts b/src/hooks/useCodex.tsindex 3aa66b67..e1844947 100644--- a/src/hooks/useCodex.ts+++ b/src/hooks/useCodex.ts@@ -12,6 +12,7 @@ import { buildHermesToolPrompt, buildHermesToolResult, parseHermesToolCalls, strimport { chatNonStreaming } from '../api/agents'import type { CodexEvent } from '../types/codex'import type { AgentBlock, AgentToolCall } from '../types/agent-mode'+import { selectRelevantTools } from '../lib/tool-selection'import type { ChatMessage, ToolCall, ToolDefinition } from '../api/providers/types'const CODEX_SYSTEM_PROMPT = `You are Codex, an autonomous coding agent inside Locally Uncensored. You execute coding tasks by reading files, writing code, and running shell commands. You MUST use tools to interact with the filesystem — never guess file contents.@@ -137,7 +138,12 @@ export function useCodex() {}if (strategy === 'native') {- const tools: ToolDefinition[] = toolRegistry.toOpenAITools(permissions)+ const lastUserMsg = messages.filter(m => m.role === 'user').pop()?.content || ''+ const relevantDefs = selectRelevantTools(lastUserMsg, toolRegistry.getAll(), permissions)+ const tools: ToolDefinition[] = relevantDefs.map(t => ({+ type: 'function' as const,+ function: { name: t.name, description: t.description, parameters: t.inputSchema },+ }))const turn = await provider.chatWithTools(modelToUse, messages, tools, chatOptions)toolCalls = turn.toolCallsturnContent = turn.content || ''diff --git a/src/lib/tool-call-repair.ts b/src/lib/tool-call-repair.tsnew file mode 100644index 00000000..8c0e49a7--- /dev/null+++ b/src/lib/tool-call-repair.ts@@ -0,0 +1,111 @@+/**+ * Tool Call Repair — fixes broken JSON from local LLMs.+ *+ * Common issues:+ * - Trailing commas in JSON objects/arrays+ * - Single quotes instead of double quotes+ * - Missing closing braces/brackets+ * - Unquoted property names+ * - Extra text before/after JSON+ * - Escaped quotes inside strings+ */++/**+ * Attempt to repair broken JSON from a tool call.+ * Returns parsed object or null if unfixable.+ */+export function repairJson(raw: string): any | null {+ // 1. Try direct parse first+ try { return JSON.parse(raw) } catch {}++ let fixed = raw.trim()++ // 2. Extract JSON from surrounding text (model might wrap it)+ const jsonMatch = fixed.match(/\{[\s\S]*\}/)+ if (jsonMatch) fixed = jsonMatch[0]++ // 3. Fix single quotes → double quotes (but not inside strings)+ fixed = fixed.replace(/'/g, '"')++ // 4. Fix trailing commas+ fixed = fixed.replace(/,\s*([}\]])/g, '$1')++ // 5. Fix unquoted keys: { key: "value" } → { "key": "value" }+ fixed = fixed.replace(/(\{|,)\s*([a-zA-Z_]\w*)\s*:/g, '$1"$2":')++ // 6. Fix missing closing braces+ const openBraces = (fixed.match(/\{/g) || []).length+ const closeBraces = (fixed.match(/\}/g) || []).length+ for (let i = 0; i < openBraces - closeBraces; i++) fixed += '}'++ const openBrackets = (fixed.match(/\[/g) || []).length+ const closeBrackets = (fixed.match(/\]/g) || []).length+ for (let i = 0; i < openBrackets - closeBrackets; i++) fixed += ']'++ // 7. Try parse again+ try { return JSON.parse(fixed) } catch {}++ // 8. Last resort: try to extract key-value pairs with regex+ try {+ const nameMatch = raw.match(/["']?name["']?\s*[:=]\s*["']([^"']+)["']/i)+ const argsMatch = raw.match(/["']?arguments["']?\s*[:=]\s*(\{[^}]*\})/i)+ if (nameMatch) {+ let args = {}+ if (argsMatch) {+ try { args = JSON.parse(argsMatch[1].replace(/'/g, '"')) } catch {}+ }+ return { name: nameMatch[1], arguments: args }+ }+ } catch {}++ return null+}++/**+ * Repair tool call arguments that might be a string instead of object.+ */+export function repairToolCallArgs(args: any): Record<string, any> {+ if (typeof args === 'object' && args !== null) return args+ if (typeof args === 'string') {+ const parsed = repairJson(args)+ if (parsed && typeof parsed === 'object') return parsed+ }+ return {}+}++/**+ * Extract tool calls from model content when native tool calling fails.+ * Looks for JSON patterns that look like tool calls.+ */+export function extractToolCallsFromContent(content: string): { name: string; arguments: Record<string, any> }[] {+ const calls: { name: string; arguments: Record<string, any> }[] = []++ // Pattern 1: {"name": "tool_name", "arguments": {...}}+ const pattern1 = /\{\s*"(?:name|tool|function)"\s*:\s*"([^"]+)"\s*,\s*"(?:arguments|args|parameters|input)"\s*:\s*(\{[^}]*\})\s*\}/gi+ let match+ while ((match = pattern1.exec(content)) !== null) {+ const args = repairJson(match[2])+ if (args) calls.push({ name: match[1], arguments: args })+ }++ // Pattern 2: tool_name(arg1, arg2) — function call syntax+ if (calls.length === 0) {+ const pattern2 = /\b(web_search|web_fetch|file_read|file_write|file_list|file_search|shell_execute|code_execute|system_info|process_list|screenshot)\s*\(\s*([^)]*)\)/gi+ while ((match = pattern2.exec(content)) !== null) {+ const argStr = match[2].trim()+ let args: Record<string, any> = {}+ if (argStr) {+ // Try to parse as JSON+ const parsed = repairJson(`{${argStr}}`)+ if (parsed) args = parsed+ else {+ // Simple single-argument: treat as the first required param+ args = { query: argStr.replace(/^["']|["']$/g, '') }+ }+ }+ calls.push({ name: match[1], arguments: args })+ }+ }++ return calls+}diff --git a/src/lib/tool-selection.ts b/src/lib/tool-selection.tsnew file mode 100644index 00000000..55222bf1--- /dev/null+++ b/src/lib/tool-selection.ts@@ -0,0 +1,101 @@+/**+ * Intelligent Tool Selection — reduce token usage by only including relevant tools.+ *+ * Instead of sending all 13 tools in every request (wasting context),+ * analyze the user message and only include tools likely to be needed.+ * Saves up to 80% of tool-definition tokens.+ */++import type { MCPToolDefinition, PermissionMap } from '../api/mcp/types'++interface ToolGroup {+ keywords: string[]+ tools: string[]+}++const TOOL_GROUPS: ToolGroup[] = [+ {+ keywords: ['search', 'find online', 'look up', 'google', 'internet', 'news', 'latest', 'current'],+ tools: ['web_search', 'web_fetch'],+ },+ {+ keywords: ['read', 'open', 'show', 'cat', 'content of', 'what does', 'look at', 'check file'],+ tools: ['file_read'],+ },+ {+ keywords: ['write', 'create', 'save', 'make a file', 'put', 'generate file', 'output to'],+ tools: ['file_write'],+ },+ {+ keywords: ['list', 'ls', 'dir', 'files in', 'directory', 'folder', 'what files', 'tree'],+ tools: ['file_list'],+ },+ {+ keywords: ['search file', 'grep', 'find in', 'contains', 'where is', 'which file'],+ tools: ['file_search'],+ },+ {+ keywords: ['run', 'execute', 'command', 'shell', 'terminal', 'bash', 'powershell', 'npm', 'git', 'pip', 'node', 'python', 'install', 'build', 'test', 'compile'],+ tools: ['shell_execute', 'code_execute'],+ },+ {+ keywords: ['system', 'os', 'cpu', 'ram', 'memory', 'process', 'running', 'hostname'],+ tools: ['system_info', 'process_list'],+ },+ {+ keywords: ['screenshot', 'screen', 'desktop', 'capture', 'see my screen'],+ tools: ['screenshot'],+ },+ {+ keywords: ['image', 'picture', 'generate image', 'draw', 'create image'],+ tools: ['image_generate'],+ },+ {+ keywords: ['workflow', 'run workflow', 'automate'],+ tools: ['run_workflow'],+ },+]++// Tools that should always be available as they're lightweight+const ALWAYS_INCLUDE = ['file_read', 'file_write']++/**+ * Select relevant tools based on user message content.+ * Returns a filtered list of tool names.+ */+export function selectRelevantTools(+ userMessage: string,+ allTools: MCPToolDefinition[],+ permissions: PermissionMap,+): MCPToolDefinition[] {+ const msg = userMessage.toLowerCase()+ const selectedNames = new Set<string>(ALWAYS_INCLUDE)++ // Match tool groups by keywords+ for (const group of TOOL_GROUPS) {+ if (group.keywords.some(kw => msg.includes(kw))) {+ group.tools.forEach(t => selectedNames.add(t))+ }+ }++ // If very few tools matched, include all (model might need flexibility)+ // This handles generic messages like "help me with this project"+ if (selectedNames.size <= 3) {+ // Include common tools for generic requests+ selectedNames.add('shell_execute')+ selectedNames.add('file_list')+ selectedNames.add('file_search')+ selectedNames.add('web_search')+ }++ // Filter by permissions (blocked categories excluded)+ const available = allTools.filter(t => permissions[t.category] !== 'blocked')++ // Return only selected tools that are available+ const selected = available.filter(t => selectedNames.has(t.name))++ // Safety: if nothing matched at all, return all available tools+ if (selected.length === 0) return available++ return selected+}