Fix Kiro
This commit is contained in:
@@ -49,43 +49,57 @@ function convertMessages(messages, tools, model) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (role === "user") {
|
if (role === "user") {
|
||||||
// Skip user messages with only tool_result blocks (Kiro API doesn't support tool results)
|
let finalContent = content;
|
||||||
|
|
||||||
|
// Check if this user message contains tool_result blocks
|
||||||
if (Array.isArray(msg.content)) {
|
if (Array.isArray(msg.content)) {
|
||||||
const hasOnlyToolResults = msg.content.every(c => c.type === "tool_result");
|
const toolResultBlocks = msg.content.filter(c => c.type === "tool_result");
|
||||||
if (hasOnlyToolResults) {
|
if (toolResultBlocks.length > 0) {
|
||||||
continue;
|
const toolResults = toolResultBlocks.map(block => ({
|
||||||
|
content: [{
|
||||||
|
text: Array.isArray(block.content)
|
||||||
|
? block.content.map(c => c.text || "").join("\n")
|
||||||
|
: (typeof block.content === "string" ? block.content : "")
|
||||||
|
}],
|
||||||
|
status: "success",
|
||||||
|
toolUseId: block.tool_use_id
|
||||||
|
}));
|
||||||
|
|
||||||
|
// JSON stringify tool results and add to content
|
||||||
|
finalContent = JSON.stringify(toolResults);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const userMsg = {
|
const userMsg = {
|
||||||
userInputMessage: {
|
userInputMessage: {
|
||||||
content: content,
|
content: finalContent,
|
||||||
modelId: "",
|
modelId: "",
|
||||||
origin: "AI_EDITOR"
|
origin: "AI_EDITOR"
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
if (tools && tools.length > 0 && history.length === 0) {
|
if (tools && tools.length > 0 && history.length === 0) {
|
||||||
userMsg.userInputMessage.userInputMessageContext = {
|
if (!userMsg.userInputMessage.userInputMessageContext) {
|
||||||
tools: tools.map(t => {
|
userMsg.userInputMessage.userInputMessageContext = {};
|
||||||
const name = t.function?.name || t.name;
|
}
|
||||||
let description = t.function?.description || t.description || "";
|
userMsg.userInputMessage.userInputMessageContext.tools = tools.map(t => {
|
||||||
|
const name = t.function?.name || t.name;
|
||||||
|
let description = t.function?.description || t.description || "";
|
||||||
|
|
||||||
if (!description.trim()) {
|
if (!description.trim()) {
|
||||||
description = `Tool: ${name}`;
|
description = `Tool: ${name}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
toolSpecification: {
|
toolSpecification: {
|
||||||
name,
|
name,
|
||||||
description,
|
description,
|
||||||
inputSchema: {
|
inputSchema: {
|
||||||
json: t.function?.parameters || t.parameters || t.input_schema || {}
|
json: t.function?.parameters || t.parameters || t.input_schema || {}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
})
|
};
|
||||||
};
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
currentMessage = userMsg;
|
currentMessage = userMsg;
|
||||||
@@ -120,58 +134,13 @@ function convertMessages(messages, tools, model) {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
const toolResults = [];
|
|
||||||
for (const tc of toolCallsOrUses) {
|
|
||||||
const toolId = tc.id;
|
|
||||||
const toolResult = toolResultsMap.get(toolId);
|
|
||||||
if (toolResult !== undefined) {
|
|
||||||
toolResults.push({
|
|
||||||
content: [{ text: toolResult }],
|
|
||||||
status: "success",
|
|
||||||
toolUseId: toolId
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (toolResults.length > 0) {
|
|
||||||
assistantMsg.toolResults = toolResults;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
history.push(assistantMsg);
|
history.push(assistantMsg);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (let i = 0; i < history.length; i++) {
|
// If last message in history is userInputMessage, use it as currentMessage
|
||||||
if (history[i].assistantResponseMessage?._toolResults) {
|
|
||||||
const toolResults = history[i].assistantResponseMessage._toolResults;
|
|
||||||
delete history[i].assistantResponseMessage._toolResults;
|
|
||||||
|
|
||||||
for (let j = i + 1; j < history.length; j++) {
|
|
||||||
if (history[j].userInputMessage) {
|
|
||||||
if (!history[j].userInputMessage.userInputMessageContext) {
|
|
||||||
history[j].userInputMessage.userInputMessageContext = {};
|
|
||||||
}
|
|
||||||
history[j].userInputMessage.userInputMessageContext.toolResults = toolResults;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (history.length > 0 && history[history.length - 1].assistantResponseMessage?._toolResults) {
|
|
||||||
const toolResults = history[history.length - 1].assistantResponseMessage._toolResults;
|
|
||||||
delete history[history.length - 1].assistantResponseMessage._toolResults;
|
|
||||||
|
|
||||||
if (currentMessage?.userInputMessage) {
|
|
||||||
if (!currentMessage.userInputMessage.userInputMessageContext) {
|
|
||||||
currentMessage.userInputMessage.userInputMessageContext = {};
|
|
||||||
}
|
|
||||||
currentMessage.userInputMessage.userInputMessageContext.toolResults = toolResults;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (history.length > 0 && history[history.length - 1].userInputMessage) {
|
if (history.length > 0 && history[history.length - 1].userInputMessage) {
|
||||||
currentMessage = history.pop();
|
currentMessage = history.pop();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user