Skip to content

Voice-to-text dictation app with local (Nvidia Parakeet/Whisper) and cloud models (BYOK). Privacy-first and available cross-platform.

fix: harden agent tool-calling with proper cleanup and error handling

Gabriel Steinedcae987caab
diff --git a/src/components/AgentOverlay.tsx b/src/components/AgentOverlay.tsx
index 0c5af4e02..451037862 100644
--- a/src/components/AgentOverlay.tsx
+++ b/src/components/AgentOverlay.tsx
@@ -47,6 +47,7 @@ export default function AgentOverlay() {
const messagesRef = useRef<Message[]>([]);
const agentStateRef = useRef<AgentState>("idle");
const conversationIdRef = useRef<number | null>(null);
+ const mountedRef = useRef(true);
useEffect(() => {
messagesRef.current = messages;
@@ -56,6 +57,12 @@ export default function AgentOverlay() {
agentStateRef.current = agentState;
}, [agentState]);
+ useEffect(() => {
+ return () => {
+ mountedRef.current = false;
+ };
+ }, []);
+
const addSystemMessage = useCallback((content: string) => {
setMessages((prev) => [
...prev,
@@ -138,6 +145,7 @@ export default function AgentOverlay() {
);
for await (const chunk of stream) {
+ if (!mountedRef.current) break;
if (chunk.type === "content") {
fullContent += chunk.text;
setMessages((prev) =>
@@ -160,17 +168,31 @@ export default function AgentOverlay() {
id: call.id,
name: call.name,
arguments: call.arguments,
- status: "completed" as const,
- result: "Done",
+ status: "executing" as const,
},
],
}
: m
)
);
- setAgentState("streaming");
- setToolStatus("");
}
+ } else if (chunk.type === "tool_result") {
+ setMessages((prev) =>
+ prev.map((m) =>
+ m.id === assistantId && m.toolCalls
+ ? {
+ ...m,
+ toolCalls: m.toolCalls.map((tc) =>
+ tc.id === chunk.callId
+ ? { ...tc, status: "completed" as const, result: chunk.displayText }
+ : tc
+ ),
+ }
+ : m
+ )
+ );
+ setAgentState("streaming");
+ setToolStatus("");
}
}
} else if (isCloudAgent) {
@@ -178,6 +200,7 @@ export default function AgentOverlay() {
systemPrompt,
});
for await (const chunk of streamSource) {
+ if (!mountedRef.current) break;
fullContent += chunk;
setMessages((prev) =>
prev.map((m) => (m.id === assistantId ? { ...m, content: fullContent } : m))
@@ -191,6 +214,7 @@ export default function AgentOverlay() {
{ systemPrompt }
);
for await (const chunk of stream) {
+ if (!mountedRef.current) break;
if (chunk.type === "content") {
fullContent += chunk.text;
setMessages((prev) =>
@@ -256,6 +280,7 @@ export default function AgentOverlay() {
});
audioManagerRef.current = am;
return () => {
+ am.cleanup?.();
window.removeEventListener("api-key-changed", (am as any)._onApiKeyChanged);
};
}, [addSystemMessage, handleTranscriptionComplete]);
diff --git a/src/helpers/ipcHandlers.js b/src/helpers/ipcHandlers.js
index 6f1b20801..0721414cb 100644
--- a/src/helpers/ipcHandlers.js
+++ b/src/helpers/ipcHandlers.js
@@ -2742,7 +2742,8 @@ class IPCHandlers {
};
}
- return await response.json();
+ const data = await response.json();
+ return { success: true, ...data };
} catch (error) {
debugLogger.error("Agent web search error:", error);
return { success: false, error: error.message };
diff --git a/src/services/ReasoningService.ts b/src/services/ReasoningService.ts
index 46e5f59f5..804afa1e6 100644
--- a/src/services/ReasoningService.ts
+++ b/src/services/ReasoningService.ts
@@ -13,6 +13,7 @@ import { getAIModel } from "./ai/providers";
export type AgentStreamChunk =
| { type: "content"; text: string }
| { type: "tool_calls"; calls: Array<{ id: string; name: string; arguments: string }> }
+ | { type: "tool_result"; callId: string; toolName: string; displayText: string }
| { type: "done"; finishReason?: string };
class ReasoningService extends BaseReasoningService {
@@ -1334,6 +1335,15 @@ class ReasoningService extends BaseReasoningService {
},
],
};
+ } else if (chunk.type === "tool-result") {
+ const output = chunk.output;
+ const displayText =
+ typeof output === "string"
+ ? output
+ : output?.error
+ ? String(output.error)
+ : "Done";
+ yield { type: "tool_result", callId: chunk.toolCallId, toolName: chunk.toolName, displayText };
} else if (chunk.type === "finish") {
yield { type: "done", finishReason: chunk.finishReason };
}
diff --git a/src/services/tools/ToolRegistry.ts b/src/services/tools/ToolRegistry.ts
index 7f6c2e612..948fdb78a 100644
--- a/src/services/tools/ToolRegistry.ts
+++ b/src/services/tools/ToolRegistry.ts
@@ -37,8 +37,12 @@ export class ToolRegistry {
description: def.description,
inputSchema: jsonSchema(def.parameters),
execute: async (args: unknown) => {
- const toolResult = await def.execute(args as Record<string, unknown>);
- return toolResult.data;
+ try {
+ const toolResult = await def.execute(args as Record<string, unknown>);
+ return toolResult.success ? toolResult.data : { error: toolResult.displayText };
+ } catch (error) {
+ return { error: (error as Error).message || "Tool execution failed" };
+ }
},
} as Tool;
}