First iteration on /load

Signed-off-by: Víctor Mayoral Vilches <v.mayoralv@gmail.com>
This commit is contained in:
Víctor Mayoral Vilches 2025-05-09 12:32:45 +02:00
parent 36a85a3bdf
commit 55bdd76895
2 changed files with 88 additions and 11 deletions

View File

@ -215,7 +215,7 @@ def run_cai_cli(starting_agent, context_variables=None, stream=False, max_turns=
agent.model.disable_rich_streaming = False # Now True as the model handles streaming
if hasattr(agent.model, 'suppress_final_output'):
agent.model.suppress_final_output = True
# Set the agent name in the model for proper display in streaming panel
if hasattr(agent.model, 'set_agent_name'):
agent.model.set_agent_name(get_agent_short_name(agent))
@ -224,9 +224,9 @@ def run_cai_cli(starting_agent, context_variables=None, stream=False, max_turns=
try:
# Start measuring user idle time
start_idle_timer()
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'):
@ -234,7 +234,7 @@ def run_cai_cli(starting_agent, context_variables=None, stream=False, max_turns=
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:
@ -242,18 +242,18 @@ def run_cai_cli(starting_agent, context_variables=None, stream=False, max_turns=
# 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 = False # Now False to let model handle streaming
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
# Set agent name in the model for streaming display
if hasattr(agent.model, 'set_agent_name'):
agent.model.set_agent_name(get_agent_short_name(agent))
@ -269,11 +269,11 @@ def run_cai_cli(starting_agent, context_variables=None, stream=False, max_turns=
current_text
)
idle_time += time.time() - idle_start_time
# Stop measuring user idle time and start measuring active time
stop_idle_timer()
start_active_timer()
except KeyboardInterrupt:
def format_time(seconds):
mins, secs = divmod(int(seconds), 60)
@ -468,7 +468,7 @@ def run_cai_cli(starting_agent, context_variables=None, stream=False, max_turns=
}
add_to_message_history(tool_msg)
turn_count += 1
# Stop measuring active time and start measuring idle time again
stop_active_timer()
start_idle_timer()
@ -500,7 +500,7 @@ def main():
# Get the agent instance by name
agent = get_agent_by_name(agent_type)
# Configure model flags to work well with CLI
if hasattr(agent, 'model'):
# Disable rich streaming in the model to avoid conflicts

View File

@ -0,0 +1,77 @@
"""
Load command for CAI REPL.
This module provides commands for loading a jsonl into
the context of the current session.
"""
import os
import signal
from typing import (
List,
Optional
)
from rich.console import Console # pylint: disable=import-error
from cai.repl.commands.base import Command, register_command
from cai.sdk.agents.models.openai_chatcompletions import message_history
console = Console()
class LoadCommand(Command):
"""Command for loading a jsonl into the context of the current session."""
def __init__(self):
"""Initialize the load command."""
super().__init__(
name="/load",
description="Load a jsonl into the context of the current session",
aliases=["/l"]
)
def handle(self, args: Optional[List[str]] = None) -> bool:
"""Handle the load command.
Args:
args: Optional list of command arguments
Returns:
True if the command was handled successfully, False otherwise
"""
return self.handle_load_command(args)
def handle_load_command(self, args: List[str]) -> bool:
"""Load a jsonl into the context of the current session.
Args:
args: List containing the PID to kill
Returns:
bool: True if the jsonl was loaded successfully
"""
if not args:
console.print("[red]Error: No jsonl file specified[/red]")
return False
try:
jsonl_file = args[0]
# Try to load the jsonl file
try:
with open(jsonl_file, 'r') as f:
for line in f:
print(line)
console.print(f"[green]Jsonl file {jsonl_file} loaded[/green]")
except BaseException: # pylint: disable=broad-exception-caught
# If killing the process group fails, try killing just the
# process
console.print(f"[red]Error: Failed to load jsonl file {jsonl_file}[/red]")
# fetch longest message from jsonl file and send to message_history
# TODO @luijait
except Exception as e: # pylint: disable=broad-exception-caught
console.print(f"[red]Error loading jsonl file: {str(e)}[/red]")
return False
# Register the command
register_command(LoadCommand())