fix: restaurar tool_calls en historial al cargar conversación del agente
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 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 4.6
parent
ee9a2f09d6
commit
622a89e966
@@ -1031,10 +1031,26 @@ Comandos: /reset /instancias /ayuda`, nil
|
|||||||
{Role: "system", Content: agentSystemPrompt()},
|
{Role: "system", Content: agentSystemPrompt()},
|
||||||
}
|
}
|
||||||
for _, h := range history {
|
for _, h := range history {
|
||||||
msg := agentMessage{Role: h.Role, Content: h.Content}
|
msg := agentMessage{Role: h.Role}
|
||||||
if h.Role == "tool" {
|
switch h.Role {
|
||||||
|
case "tool":
|
||||||
|
// Restaurar tool result: ToolCallID viene de ToolName (donde guardamos tc.ID)
|
||||||
msg.ToolCallID = h.ToolName
|
msg.ToolCallID = h.ToolName
|
||||||
msg.Content = h.ToolResult
|
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)
|
messages = append(messages, msg)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user