fix: fix message order is not user -> assistant -> user

This commit is contained in:
fatelei 2026-02-16 11:13:56 +08:00
parent 9a3f3611c3
commit 1d4fa2727c
No known key found for this signature in database
GPG key ID: 2F91DA05646F4EED

View file

@ -12,6 +12,7 @@ import (
"github.com/sipeed/picoclaw/pkg/providers"
"github.com/sipeed/picoclaw/pkg/skills"
"github.com/sipeed/picoclaw/pkg/tools"
"github.com/sipeed/picoclaw/pkg/utils"
)
type ContextBuilder struct {
@ -189,17 +190,25 @@ func (cb *ContextBuilder) BuildMessages(history []providers.Message, summary str
systemPrompt += "\n\n## Summary of Previous Conversation\n\n" + summary
}
//This fix prevents the session memory from LLM failure due to elimination of toolu_IDs required from LLM
// This fix prevents the session memory from LLM failure due to elimination of toolu_IDs required from LLM
// --- INICIO DEL FIX ---
//Diegox-17
// Diegox-17
for len(history) > 0 && (history[0].Role == "tool") {
logger.DebugCF("agent", "Removing orphaned tool message from history to prevent LLM error",
map[string]interface{}{"role": history[0].Role})
history = history[1:]
}
//Diegox-17
// Diegox-17
// --- FIN DEL FIX ---
if len(history) > 0 && history[len(history)-1].Role == "user" {
logger.WarnCF("agent", "Removing trailing user message from history to prevent consecutive user messages",
map[string]interface{}{
"content_preview": utils.Truncate(history[len(history)-1].Content, 50),
})
history = history[:len(history)-1]
}
messages = append(messages, providers.Message{
Role: "system",
Content: systemPrompt,