From 622a89e96623ddd0689ec1ed7c9598e2064e4346 Mon Sep 17 00:00:00 2001 From: Lizandro GD Date: Mon, 13 Jul 2026 19:36:48 +0000 Subject: [PATCH] =?UTF-8?q?fix:=20restaurar=20tool=5Fcalls=20en=20historia?= =?UTF-8?q?l=20al=20cargar=20conversaci=C3=B3n=20del=20agente?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Cuando el agente hacía múltiples rondas de herramientas, los mensajes del asistente con tool_calls se guardaban como JSON en Content. Al recargar el historial, ToolCalls quedaba nil y Anthropic recibía tool_result sin el tool_use correspondiente → error 400. Ahora se detecta si Content es un array JSON y se deserializa de vuelta a []agentToolCall antes de enviarlo a la API. Co-Authored-By: Claude Opus 4.6 --- pkg/services/telegram_agent_service.go | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/pkg/services/telegram_agent_service.go b/pkg/services/telegram_agent_service.go index c82d793..18501a5 100644 --- a/pkg/services/telegram_agent_service.go +++ b/pkg/services/telegram_agent_service.go @@ -1031,10 +1031,26 @@ Comandos: /reset /instancias /ayuda`, nil {Role: "system", Content: agentSystemPrompt()}, } for _, h := range history { - msg := agentMessage{Role: h.Role, Content: h.Content} - if h.Role == "tool" { + msg := agentMessage{Role: h.Role} + switch h.Role { + case "tool": + // Restaurar tool result: ToolCallID viene de ToolName (donde guardamos tc.ID) msg.ToolCallID = h.ToolName msg.Content = h.ToolResult + case "assistant": + // Si el Content es un array JSON, son tool_calls serializados — restaurarlos + content := strings.TrimSpace(h.Content) + if len(content) > 0 && content[0] == '[' { + var tcs []agentToolCall + if json.Unmarshal([]byte(content), &tcs) == nil && len(tcs) > 0 { + msg.ToolCalls = tcs + // Content debe quedar vacío cuando hay tool_calls (Anthropic lo requiere) + break + } + } + msg.Content = h.Content + default: + msg.Content = h.Content } messages = append(messages, msg) }