fix: normalize message roles for local model backends (llama.cpp, Ollama)

Local backends using strict chat templates (e.g. Mistral 3) rejected
Flynn's Anthropic-style tool_use/tool_result content blocks, causing
'roles must alternate' errors. Added getMessageTextWithTools() and
normalizeMessagesForLocal() to serialize structured blocks to plain
text, drop empty messages, and merge consecutive same-role messages.
Also fixed compaction to ensure kept messages start with user role.
This commit is contained in:
William Valentin
2026-02-10 22:04:17 -08:00
parent 2f6d045e2a
commit 6761dca1c2
6 changed files with 318 additions and 43 deletions
+6
View File
@@ -53,6 +53,12 @@ export async function compactHistory(opts: {
const toCompact = messages.slice(0, -keepCount);
const toKeep = messages.slice(-keepCount);
// Ensure toKeep starts with a user message to avoid assistant→assistant
// after the compaction summary (which has role 'assistant').
while (toKeep.length > 0 && toKeep[0].role === 'assistant') {
toCompact.push(toKeep.shift()!);
}
const formattedConversation = toCompact.map((msg) => `${msg.role}: ${getMessageText(msg)}`).join('\n\n');
const tier = orchestrator.getDelegationTier('compaction');