diff --git a/cheddahbot/agent.py b/cheddahbot/agent.py index e4a96ae..96643c6 100644 --- a/cheddahbot/agent.py +++ b/cheddahbot/agent.py @@ -185,12 +185,23 @@ class Agent: # Execute tools if self._tools: - messages.append( + # Build OpenAI-format assistant message with tool_calls + openai_tool_calls = [ { - "role": "assistant", - "content": full_response or "I'll use some tools to help with that.", + "id": tc.get("id", f"call_{tc['name']}_{i}"), + "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: yield f"\n\n**Using tool: {tc['name']}**\n" @@ -201,9 +212,11 @@ class Agent: yield f"```\n{result[:2000]}\n```\n\n" self.db.add_message(conv_id, "tool", result, tool_result=tc["name"]) - messages.append( - {"role": "user", "content": f'[Tool "{tc["name"]}" result]\n{result}'} - ) + messages.append({ + "role": "tool", + "tool_call_id": tc.get("id", f"call_{tc['name']}"), + "content": result, + }) else: # No tool system configured - just mention tool was requested if full_response: