diff --git a/src/cai/cli.py b/src/cai/cli.py index 9bd915eb..46d753bf 100644 --- a/src/cai/cli.py +++ b/src/cai/cli.py @@ -1216,40 +1216,41 @@ def run_cai_cli( # text content and ignore tool call entries to prevent schema # mismatches when converting to OpenAI chat format. history_context = [] - # Get agent name for history lookup - agent_history_name = getattr(agent, 'name', last_agent_type) - for msg in get_agent_message_history(agent_history_name): - role = msg.get("role") - content = msg.get("content") - tool_calls = msg.get("tool_calls") + # Use the agent's model's message history directly instead of AGENT_MANAGER + # This ensures compaction actually clears the history + if hasattr(agent, 'model') and hasattr(agent.model, 'message_history'): + for msg in agent.model.message_history: + role = msg.get("role") + content = msg.get("content") + tool_calls = msg.get("tool_calls") - if role == "user": - history_context.append({"role": "user", "content": content or ""}) - elif role == "system": - history_context.append({"role": "system", "content": content or ""}) - elif role == "assistant": - if tool_calls: + if role == "user": + history_context.append({"role": "user", "content": content or ""}) + elif role == "system": + history_context.append({"role": "system", "content": content or ""}) + elif role == "assistant": + if tool_calls: + history_context.append( + { + "role": "assistant", + "content": content, # Can be None + "tool_calls": tool_calls, + } + ) + elif content is not None: + history_context.append({"role": "assistant", "content": content}) + elif ( + content is None and not tool_calls + ): # Explicitly handle empty assistant message + history_context.append({"role": "assistant", "content": None}) + elif role == "tool": history_context.append( { - "role": "assistant", - "content": content, # Can be None - "tool_calls": tool_calls, + "role": "tool", + "tool_call_id": msg.get("tool_call_id"), + "content": msg.get("content"), # Tool output } ) - elif content is not None: - history_context.append({"role": "assistant", "content": content}) - elif ( - content is None and not tool_calls - ): # Explicitly handle empty assistant message - history_context.append({"role": "assistant", "content": None}) - elif role == "tool": - history_context.append( - { - "role": "tool", - "tool_call_id": msg.get("tool_call_id"), - "content": msg.get("content"), # Tool output - } - ) # Fix message list structure BEFORE sending to the model to prevent errors try: diff --git a/src/cai/repl/commands/compact.py b/src/cai/repl/commands/compact.py index 3e1f3055..2db5d219 100644 --- a/src/cai/repl/commands/compact.py +++ b/src/cai/repl/commands/compact.py @@ -632,9 +632,8 @@ class CompactCommand(Command): if agent_name in PERSISTENT_MESSAGE_HISTORIES: PERSISTENT_MESSAGE_HISTORIES[agent_name].clear() - # Clear in AGENT_MANAGER as well - if hasattr(AGENT_MANAGER, '_message_history') and agent_name in AGENT_MANAGER._message_history: - AGENT_MANAGER._message_history[agent_name].clear() + # Clear in AGENT_MANAGER as well using the proper method + AGENT_MANAGER.clear_history(agent_name) else: console.print(f"[red]Failed to compact conversation[/red]")