1.5: Fix tool results — use role:tool with tool_call_id

Previously tool results were injected as role:user messages which
confuses some models. Now the live agent loop uses proper OpenAI
function-calling format:
- Assistant messages include tool_calls array with IDs
- Tool results use role:tool with matching tool_call_id

History replay in router.py is unchanged (no tool_call_ids in DB).

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
cora-start
PeninsulaInd 2026-02-17 10:00:21 -06:00
parent 202a5e99e4
commit 4a646373b6
1 changed files with 20 additions and 7 deletions

View File

@ -185,12 +185,23 @@ class Agent:
# Execute tools # Execute tools
if self._tools: if self._tools:
messages.append( # Build OpenAI-format assistant message with tool_calls
openai_tool_calls = [
{ {
"role": "assistant", "id": tc.get("id", f"call_{tc['name']}_{i}"),
"content": full_response or "I'll use some tools to help with that.", "type": "function",
"function": {
"name": tc["name"],
"arguments": json.dumps(tc.get("input", {})),
},
} }
) for i, tc in enumerate(unique_tool_calls)
]
messages.append({
"role": "assistant",
"content": full_response or None,
"tool_calls": openai_tool_calls,
})
for tc in unique_tool_calls: for tc in unique_tool_calls:
yield f"\n\n**Using tool: {tc['name']}**\n" yield f"\n\n**Using tool: {tc['name']}**\n"
@ -201,9 +212,11 @@ class Agent:
yield f"```\n{result[:2000]}\n```\n\n" yield f"```\n{result[:2000]}\n```\n\n"
self.db.add_message(conv_id, "tool", result, tool_result=tc["name"]) self.db.add_message(conv_id, "tool", result, tool_result=tc["name"])
messages.append( messages.append({
{"role": "user", "content": f'[Tool "{tc["name"]}" result]\n{result}'} "role": "tool",
) "tool_call_id": tc.get("id", f"call_{tc['name']}"),
"content": result,
})
else: else:
# No tool system configured - just mention tool was requested # No tool system configured - just mention tool was requested
if full_response: if full_response: