fix /agent and /model

This commit is contained in:
Mery-Sanz 2025-05-05 16:22:57 +02:00
parent 3c037bfb7b
commit 7db55f6a93
2 changed files with 37 additions and 5 deletions

View File

@ -175,7 +175,8 @@ def run_cai_cli(starting_agent, context_variables=None, stream=False, max_turns=
ACTIVE_TIME = 0
idle_time = 0
console = Console()
last_model = os.getenv('CAI_MODEL', 'qwen2.5:14b')
last_agent_type = os.getenv('CAI_AGENT_TYPE', 'one_tool_agent')
# Initialize command completer and key bindings
command_completer = FuzzyCommandCompleter()
current_text = ['']
@ -208,6 +209,36 @@ def run_cai_cli(starting_agent, context_variables=None, stream=False, max_turns=
while turn_count < max_turns:
try:
idle_start_time = time.time()
# Check if model has changed and update if needed
current_model = os.getenv('CAI_MODEL', 'qwen2.5:14b')
if current_model != last_model and hasattr(agent, 'model'):
# Update the model in the agent
if hasattr(agent.model, 'model'):
agent.model.model = current_model
last_model = current_model
# Check if agent type has changed and recreate agent if needed
current_agent_type = os.getenv('CAI_AGENT_TYPE', 'one_tool_agent')
if current_agent_type != last_agent_type:
try:
# Import is already at the top level
agent = get_agent_by_name(current_agent_type)
last_agent_type = current_agent_type
# Configure the new agent's model flags
if hasattr(agent, 'model'):
if hasattr(agent.model, 'disable_rich_streaming'):
agent.model.disable_rich_streaming = True
if hasattr(agent.model, 'suppress_final_output'):
agent.model.suppress_final_output = True
# Apply current model to the new agent
if hasattr(agent.model, 'model'):
agent.model.model = current_model
except Exception as e:
console.print(f"[red]Error switching agent: {str(e)}[/red]")
# Get user input with command completion and history
user_input = get_user_input(
command_completer,

View File

@ -191,9 +191,6 @@ class AgentCommand(Command):
console.print(table)
return True
console.print(table)
return True
def handle_select(self, args: Optional[List[str]] = None) -> bool: # pylint: disable=too-many-branches,line-too-long # noqa: E501
"""Handle /agent select command.
@ -226,16 +223,20 @@ class AgentCommand(Command):
return False
else:
# Treat as agent key
selected_agent_key = None
for key, agent_obj in agents_to_display.items():
if key == agent_id:
agent = agent_obj
selected_agent_key = key
agent_name = getattr(agent_obj, "name", key)
break
else:
console.print(f"[red]Error: Unknown agent key: {agent_id}[/red]")
return False
os.environ["CAI_MODEL"] = agent_name
# Set the agent key in environment variable (not the agent name)
os.environ["CAI_AGENT_TYPE"] = selected_agent_key
console.print(
f"[green]Switched to agent: {agent_name}[/green]")
visualize_agent_graph(agent)