mirror of
https://github.com/router-for-me/CLIProxyAPIPlus.git
synced 2026-05-13 10:57:32 +08:00
Merge pull request #162 from taetaetae/fix/kiro-compaction-empty-content
fix(kiro): handle empty content in messages to prevent Bad Request errors
This commit is contained in:
@@ -576,9 +576,23 @@ func processOpenAIMessages(messages gjson.Result, modelID, origin string) ([]Kir
|
||||
}
|
||||
}
|
||||
|
||||
// Truncate history if too long to prevent Kiro API errors
|
||||
history = truncateHistoryIfNeeded(history)
|
||||
|
||||
return history, currentUserMsg, currentToolResults
|
||||
}
|
||||
|
||||
const kiroMaxHistoryMessages = 50
|
||||
|
||||
func truncateHistoryIfNeeded(history []KiroHistoryMessage) []KiroHistoryMessage {
|
||||
if len(history) <= kiroMaxHistoryMessages {
|
||||
return history
|
||||
}
|
||||
|
||||
log.Debugf("kiro-openai: truncating history from %d to %d messages", len(history), kiroMaxHistoryMessages)
|
||||
return history[len(history)-kiroMaxHistoryMessages:]
|
||||
}
|
||||
|
||||
// buildUserMessageFromOpenAI builds a user message from OpenAI format and extracts tool results
|
||||
func buildUserMessageFromOpenAI(msg gjson.Result, modelID, origin string) (KiroUserInputMessage, []KiroToolResult) {
|
||||
content := msg.Get("content")
|
||||
@@ -677,8 +691,23 @@ func buildAssistantMessageFromOpenAI(msg gjson.Result) KiroAssistantResponseMess
|
||||
}
|
||||
}
|
||||
|
||||
// CRITICAL FIX: Kiro API requires non-empty content for assistant messages
|
||||
// This can happen with compaction requests or error recovery scenarios
|
||||
finalContent := contentBuilder.String()
|
||||
if strings.TrimSpace(finalContent) == "" {
|
||||
const defaultAssistantContentWithTools = "I'll help you with that."
|
||||
const defaultAssistantContent = "I understand."
|
||||
|
||||
if len(toolUses) > 0 {
|
||||
finalContent = defaultAssistantContentWithTools
|
||||
} else {
|
||||
finalContent = defaultAssistantContent
|
||||
}
|
||||
log.Debugf("kiro-openai: assistant content was empty, using default: %s", finalContent)
|
||||
}
|
||||
|
||||
return KiroAssistantResponseMessage{
|
||||
Content: contentBuilder.String(),
|
||||
Content: finalContent,
|
||||
ToolUses: toolUses,
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user