From 0c9ad12055151e20bbe5fe991d2de55b03f989ff Mon Sep 17 00:00:00 2001 From: decolua Date: Mon, 23 Mar 2026 12:05:22 +0700 Subject: [PATCH] Fix : Fix error 400 --- open-sse/translator/request/openai-to-kiro.js | 35 ++++++++++--------- src/mitm/dev | 2 +- 2 files changed, 20 insertions(+), 17 deletions(-) diff --git a/open-sse/translator/request/openai-to-kiro.js b/open-sse/translator/request/openai-to-kiro.js index c388002..d4471bd 100644 --- a/open-sse/translator/request/openai-to-kiro.js +++ b/open-sse/translator/request/openai-to-kiro.js @@ -219,37 +219,40 @@ function convertMessages(messages, tools, model) { flushPending(); } - // If last message in history is userInputMessage, use it as currentMessage - if (history.length > 0 && history[history.length - 1].userInputMessage) { - currentMessage = history.pop(); + // Pop last userInputMessage as currentMessage (search from end, skip trailing assistant messages) + for (let i = history.length - 1; i >= 0; i--) { + if (history[i].userInputMessage) { + currentMessage = history.splice(i, 1)[0]; + break; + } } - const firstHistoryItem = history[0]; - if (firstHistoryItem?.userInputMessage?.userInputMessageContext?.tools && - !currentMessage?.userInputMessage?.userInputMessageContext?.tools) { - if (!currentMessage.userInputMessage.userInputMessageContext) { - currentMessage.userInputMessage.userInputMessageContext = {}; - } - currentMessage.userInputMessage.userInputMessageContext.tools = - firstHistoryItem.userInputMessage.userInputMessageContext.tools; - } - + // Grab tools from first history item BEFORE cleanup removes them + const firstHistoryTools = history[0]?.userInputMessage?.userInputMessageContext?.tools; + // Clean up history for Kiro API compatibility history.forEach(item => { if (item.userInputMessage?.userInputMessageContext?.tools) { delete item.userInputMessage.userInputMessageContext.tools; } - - if (item.userInputMessage?.userInputMessageContext && + if (item.userInputMessage?.userInputMessageContext && Object.keys(item.userInputMessage.userInputMessageContext).length === 0) { delete item.userInputMessage.userInputMessageContext; } - if (item.userInputMessage && !item.userInputMessage.modelId) { item.userInputMessage.modelId = model; } }); + // Inject tools into currentMessage AFTER cleanup + if (firstHistoryTools && currentMessage?.userInputMessage && + !currentMessage.userInputMessage.userInputMessageContext?.tools) { + if (!currentMessage.userInputMessage.userInputMessageContext) { + currentMessage.userInputMessage.userInputMessageContext = {}; + } + currentMessage.userInputMessage.userInputMessageContext.tools = firstHistoryTools; + } + return { history, currentMessage }; } diff --git a/src/mitm/dev b/src/mitm/dev index ebfb215..b3b7473 160000 --- a/src/mitm/dev +++ b/src/mitm/dev @@ -1 +1 @@ -Subproject commit ebfb215130fd06f9623ef75a5b41fc41a8ea2f2f +Subproject commit b3b7473d6df221586f9056190bd98389d3b5c0d7