From 5342214c5a6f4fe554011effa20764ef9eb7096a Mon Sep 17 00:00:00 2001 From: Zhafron Kautsar Date: Fri, 9 Jan 2026 05:13:15 -0500 Subject: [PATCH] fix(kiro): preserve tool definitions when history contains tool calls When conversation history includes tool usage but current request doesn't provide tool definitions, reconstruct minimal tool specifications from history to prevent API errors. This ensures continuity in multi-turn conversations with tool calling. - Extract tool names from historical tool uses - Generate placeholder tool specs with empty schemas - Only apply when history has tool calls but toolsContext is empty - Remove obsolete Chinese comment --- src/claude/claude-kiro.js | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/src/claude/claude-kiro.js b/src/claude/claude-kiro.js index 2c4e9b2..b015eec 100644 --- a/src/claude/claude-kiro.js +++ b/src/claude/claude-kiro.js @@ -912,11 +912,39 @@ export class KiroApiService { } userInputMessageContext.toolResults = uniqueToolResults; } + const historyHasToolCalling = history.some(h => + h.assistantResponseMessage?.toolUses || + h.userInputMessage?.userInputMessageContext?.toolResults + ); + if (Object.keys(toolsContext).length > 0 && toolsContext.tools) { userInputMessageContext.tools = toolsContext.tools; + } else if (historyHasToolCalling && !toolsContext.tools) { + const toolNamesInHistory = new Set(); + history.forEach(h => { + if (h.assistantResponseMessage?.toolUses) { + h.assistantResponseMessage.toolUses.forEach(tu => { + toolNamesInHistory.add(tu.name); + }); + } + }); + + if (toolNamesInHistory.size > 0) { + userInputMessageContext.tools = Array.from(toolNamesInHistory).map(name => ({ + toolSpecification: { + name: name, + description: "Tool", + inputSchema: { + json: { + type: "object", + properties: {} + } + } + } + })); + } } - // 只有当 userInputMessageContext 有内容时才添加 if (Object.keys(userInputMessageContext).length > 0) { userInputMessage.userInputMessageContext = userInputMessageContext; }