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) }