From 1d4fa2727ce466fb6a3711c9d1436f8adc69659d Mon Sep 17 00:00:00 2001 From: fatelei Date: Mon, 16 Feb 2026 11:13:56 +0800 Subject: [PATCH] fix: fix message order is not user -> assistant -> user --- pkg/agent/context.go | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/pkg/agent/context.go b/pkg/agent/context.go index cf5ce2913..f61caaff8 100644 --- a/pkg/agent/context.go +++ b/pkg/agent/context.go @@ -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,