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
This commit is contained in:
parent
148b7b2114
commit
5342214c5a
1 changed files with 29 additions and 1 deletions
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue