fix(ui): 修复工具调用重复显示问题

在 useChat hook 中添加去重逻辑:
- tool_start: 检查是否已存在相同 ID 的工具调用,存在则跳过
- subagent:tool_start: 同样添加去重检查

问题原因:服务端可能因 AI SDK 触发两次 tool-call chunk
导致发送重复的 tool_start 事件,前端之前没有去重逻辑
This commit is contained in:
2025-12-16 23:02:34 +08:00
parent eb80b2c9e6
commit 791c4a4616
+17
View File
@@ -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,