diff --git a/src/lib/stores/app.ts b/src/lib/stores/app.ts index 8af544f..07f9637 100644 --- a/src/lib/stores/app.ts +++ b/src/lib/stores/app.ts @@ -246,8 +246,8 @@ export function updateAgentStatus(id: string, status: Agent['status']) { ); } -export function addToolCall(agentId: string, tool: string, args: Record): string { - const id = crypto.randomUUID(); +export function addToolCall(agentId: string, tool: string, args: Record, fixedId?: string): string { + const id = fixedId || crypto.randomUUID(); const call: ToolCall = { id, agentId, @@ -270,18 +270,30 @@ export function addToolCall(agentId: string, tool: string, args: Record - calls.map((c) => - c.id === id - ? { - ...c, - status: failed ? 'failed' : 'completed', - completedAt: new Date(), - result - } - : c - ) - ); + toolCalls.update((calls) => { + // Exakte ID-Suche + let found = calls.some((c) => c.id === id); + + if (found) { + return calls.map((c) => + c.id === id + ? { ...c, status: (failed ? 'failed' : 'completed') as ToolCall['status'], completedAt: new Date(), result } + : c + ); + } + + // Fallback: Letzten laufenden Tool-Call abschließen (für Events ohne passende ID) + const lastRunning = [...calls].reverse().find((c) => c.status === 'running'); + if (lastRunning) { + return calls.map((c) => + c.id === lastRunning.id + ? { ...c, status: (failed ? 'failed' : 'completed') as ToolCall['status'], completedAt: new Date(), result } + : c + ); + } + + return calls; + }); } export function clearAll() { diff --git a/src/lib/stores/events.ts b/src/lib/stores/events.ts index 12b955f..225af5c 100644 --- a/src/lib/stores/events.ts +++ b/src/lib/stores/events.ts @@ -226,8 +226,8 @@ export async function initEventListeners(): Promise { // Tool Start listeners.push( await listen('tool-start', async (event) => { - const { tool, input } = event.payload; - console.log('🔧 Tool Start:', tool); + const { id, tool, input } = event.payload; + console.log('🔧 Tool Start:', tool, id); // Inline-Aktivitätsanzeige aktualisieren currentTool.set({ tool: tool || 'unknown', input: input || {} }); @@ -236,7 +236,8 @@ export async function initEventListeners(): Promise { agents.update((ags) => { const activeAgent = ags.find((a) => a.status === 'active'); if (activeAgent) { - addToolCall(activeAgent.id, tool || 'unknown', input || {}); + // Backend-ID durchreichen damit tool-end den Call matchen kann + addToolCall(activeAgent.id, tool || 'unknown', input || {}, id); } return ags; });