From 791c4a46160991d2311ee45af37cc2b4ff88bbc6 Mon Sep 17 00:00:00 2001 From: kurihada Date: Tue, 16 Dec 2025 23:02:34 +0800 Subject: [PATCH] =?UTF-8?q?fix(ui):=20=E4=BF=AE=E5=A4=8D=E5=B7=A5=E5=85=B7?= =?UTF-8?q?=E8=B0=83=E7=94=A8=E9=87=8D=E5=A4=8D=E6=98=BE=E7=A4=BA=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 在 useChat hook 中添加去重逻辑: - tool_start: 检查是否已存在相同 ID 的工具调用,存在则跳过 - subagent:tool_start: 同样添加去重检查 问题原因:服务端可能因 AI SDK 触发两次 tool-call chunk 导致发送重复的 tool_start 事件,前端之前没有去重逻辑 --- packages/ui/src/hooks/useChat.ts | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/packages/ui/src/hooks/useChat.ts b/packages/ui/src/hooks/useChat.ts index 3565789..4fce528 100644 --- a/packages/ui/src/hooks/useChat.ts +++ b/packages/ui/src/hooks/useChat.ts @@ -197,6 +197,15 @@ export function useChat({ sessionId, onError, onSessionNotFound, onSessionUpdate metadata: { agentName: prev.currentAgent }, }; + // 检查是否已存在相同 ID 的工具调用(去重) + const existingTool = streaming.parts.find( + (part) => part.type === 'tool' && part.id === payload.id + ); + if (existingTool) { + // 已存在相同 ID 的工具调用,跳过 + return prev; + } + // 添加工具调用 part const toolPart: ToolMessagePart = { type: 'tool', @@ -370,6 +379,14 @@ export function useChat({ sessionId, onError, onSessionNotFound, onSessionUpdate if (!prev.currentSubagent || prev.currentSubagent.id !== payload.agentId) { return prev; } + // 检查是否已存在相同 ID 的工具调用(去重) + const existingTool = prev.currentSubagent.tools.find( + (tool) => tool.id === payload.toolCallId + ); + if (existingTool) { + // 已存在相同 ID 的工具调用,跳过 + return prev; + } const newTool: SubagentToolInfo = { id: payload.toolCallId, toolName: payload.toolName,