Fix : Fix error 400

This commit is contained in:
decolua 2026-03-23 12:05:22 +07:00
parent e9ccae4ca1
commit 0c9ad12055
2 changed files with 20 additions and 17 deletions

View file

@ -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 };
}

@ -1 +1 @@
Subproject commit ebfb215130fd06f9623ef75a5b41fc41a8ea2f2f
Subproject commit b3b7473d6df221586f9056190bd98389d3b5c0d7