Skip to content

Latest commit

 

History

History
320 lines (238 loc) · 10.2 KB

File metadata and controls

320 lines (238 loc) · 10.2 KB

Auto-Execution Feature Implementation

Related to GitHub Issue #20

Overview

This document describes the implementation of the auto-execution feature for the CLI agent (agent.py). This feature brings the CLI agent to parity with the web interface by automatically detecting and executing commands suggested by the AI.

Problem Statement

Previously, the CLI agent only displayed suggested commands but did not execute them. Users had to manually copy the command and re-type it with the run: prefix, creating a poor user experience compared to the web interface which auto-executes commands automatically.

Before (Old Behavior)

💬 > Using the Ford Resume, let's create a new for this job posting: X.md

🤖 Sure, we will create a new resume...

run: python src/tailor.py --resume data/master_resume.json --jd "data/job_listings/X.md" --out out/resume.html --format html --theme modern

💬 > # Command is NOT executed, just displayed

After (New Behavior)

💬 > Using the Ford Resume, let's create a new for this job posting: X.md

🤖 Sure, we will create a new resume...

run: python src/tailor.py --resume data/master_resume.json --jd "data/job_listings/X.md" --out out/resume.html --format html --theme modern

❓ Execute this command? (y/n/edit): y

🔧 Executing command: python src/tailor.py --resume data/master_resume.json --jd "data/job_listings/X.md" --out out/resume.html --format html --theme modern

✅ Command executed successfully:
[command output]

💬 >

Implementation Details

1. New Agent Parameters

Added two new parameters to the Agent.__init__() method:

def __init__(self, memory_file: str = "memory.json", model: str = "gpt-4", 
             auto_execute: bool = True, confirm_execution: bool = True):
  • auto_execute (default: True) - Enable/disable auto-execution of commands from agent responses
  • confirm_execution (default: True) - Enable/disable confirmation prompts before executing

2. Command Extraction Method

Added _extract_command_from_response() method to detect run: pattern in agent responses:

def _extract_command_from_response(self, response: str) -> Optional[str]:
    """Extract command from agent response (looks for 'run:' pattern)."""
    import re
    pattern = r'run:\s*(.+?)(?:\n|$)'
    match = re.search(pattern, response, re.IGNORECASE)
    if match:
        return match.group(1).strip()
    return None

Features:

  • Case-insensitive matching (run: or RUN:)
  • Extracts command until newline or end of string
  • Returns None if no command found

3. Confirmation Flow Method

Added _confirm_execution() method to handle user confirmation:

def _confirm_execution(self, command: str) -> bool:
    """Ask user to confirm command execution."""
    print(f"\n❓ Execute this command? (y/n/edit): ", end="", flush=True)
    response = input().strip().lower()
    
    if response in ['y', 'yes']:
        return True
    elif response == 'edit':
        # Allow user to edit command before executing
        ...
    else:
        print("⏭️  Skipping command execution")
        return False

Options:

  • y or yes - Execute the command as-is
  • n or no - Skip execution
  • edit - Edit the command before executing
  • Handles KeyboardInterrupt and EOFError gracefully

4. Result Formatting Method

Added _format_execution_result() method for consistent output:

def _format_execution_result(self, result: Dict[str, Any]) -> str:
    """Format command execution result."""
    if result["success"]:
        return f"✅ Command executed successfully:\n{result['output']}"
    else:
        return f"❌ Command failed:\n{result['error']}"

5. Integration into Chat Flow

Modified process_message() method to integrate auto-execution:

# After getting response from OpenAI
if self.auto_execute:
    command = self._extract_command_from_response(assistant_message)
    if command:
        # Ask for confirmation if enabled
        should_execute = True
        if self.confirm_execution:
            should_execute = self._confirm_execution(command)
        
        if should_execute:
            print(f"🔧 Executing command: {command}")
            result = self.command_executor.execute(command)
            execution_result = self._format_execution_result(result)
            
            # Add execution result to memory
            self.memory_manager.add_message("user", f"run: {command}")
            self.memory_manager.add_message("assistant", execution_result)
            self.memory_manager.save()
            
            # Append execution result to response
            assistant_message += f"\n\n{execution_result}"

6. Command-Line Arguments

Added comprehensive command-line argument support:

parser.add_argument("--model", default=os.getenv("OPENAI_MODEL", "gpt-4"))
parser.add_argument("--memory", default="memory.json")
parser.add_argument("--auto-execute", action="store_true", default=True)
parser.add_argument("--no-auto-execute", action="store_false", dest="auto_execute")
parser.add_argument("--no-confirm", action="store_false", dest="confirm_execution", default=True)

Usage Examples:

# Default: auto-execute with confirmation
python agent.py

# Disable auto-execution
python agent.py --no-auto-execute

# Auto-execute without confirmation
python agent.py --no-confirm

# Use different model
python agent.py --model gpt-4-turbo

# Combine options
python agent.py --model gpt-3.5-turbo --no-confirm

7. Enhanced User Interface

Updated the startup message to display current settings:

🤖 Local AI Agent Started
==================================================
Commands:
  - Type 'run: <command>' to execute local commands
  - Type 'exit' or 'quit' to stop
  - Type anything else to chat with the AI

Settings:
  - Auto-execute: ✅ Enabled
  - Confirmation: ✅ Required
==================================================

Testing

Unit Tests

Created test_auto_execution.py to verify command extraction logic:

test_cases = [
    {
        "name": "Simple command",
        "response": "I'll update the Ford resume.\n\nrun: python src/update_resume_experience.py ...",
        "expected": "python src/update_resume_experience.py ..."
    },
    # ... more test cases
]

Test Results: ✅ All tests passed

Manual Testing Scenarios

  1. Auto-execution with confirmation - Default behavior
  2. Auto-execution without confirmation - Using --no-confirm
  3. Manual execution only - Using --no-auto-execute
  4. Command editing - Using edit option
  5. Skipping execution - Using n option

Configuration Modes

Mode 1: Auto-Execute with Confirmation (Default)

python agent.py
  • ✅ Safest option
  • ✅ User reviews each command
  • ✅ Can edit commands before execution
  • ✅ Recommended for general use

Mode 2: Auto-Execute without Confirmation

python agent.py --no-confirm
  • ⚠️ Commands execute immediately
  • ⚠️ Matches web interface behavior
  • ⚠️ Use only in trusted environments

Mode 3: Manual Execution Only

python agent.py --no-auto-execute
  • ✅ Most conservative option
  • ✅ User must manually type run: commands
  • ✅ Best for sensitive systems

Security Considerations

  1. Confirmation is enabled by default to prevent accidental execution of dangerous commands
  2. Commands are subject to the same validation as manual run: commands
  3. Users can disable auto-execution with --no-auto-execute flag
  4. Edit option allows command review before execution
  5. All executions are logged to memory.json

Documentation Updates

Updated Files

  1. docs/LOCAL_AI_AGENT.md

    • Added auto-execution feature description
    • Added command-line options table
    • Added new examples with confirmation flow
    • Updated architecture and data flow diagrams
  2. README.md

    • Updated Local AI Agent section
    • Added auto-execution examples
    • Added command-line options

Comparison with Web Interface

Feature Web Interface CLI Agent (Before) CLI Agent (After)
Detects run: pattern
Auto-executes commands
User confirmation N/A ✅ (optional)
Command editing
Shows execution status

Result: CLI agent now has feature parity with web interface, plus additional safety features!

Benefits

  1. Improved User Experience - No more manual copy/paste of commands
  2. Consistency - CLI and web interfaces behave similarly
  3. Safety - Confirmation prompts prevent accidental execution
  4. Flexibility - Multiple configuration modes for different use cases
  5. Transparency - Clear visual indicators for all actions

Future Enhancements

Potential improvements for future iterations:

  • Add "always execute" preference (save to config file)
  • Add dangerous command warnings (e.g., rm -rf, sudo)
  • Support multi-line commands
  • Add command history/logging
  • Add undo/rollback capability
  • Configuration file support (.agentrc)

Related Issues

  • #20 - Add auto-execution of agent-suggested commands in CLI agent (this implementation)
  • #19 - Add CLI resume duplication support (benefits from auto-execution)
  • #17 - CRUD Scripts (easier to use with auto-execution)
  • #12 - Agent Web Integration (web interface already has this feature)
  • #8 - Local AI Agent (original agent implementation)

Acceptance Criteria

  • CLI agent detects run: commands in agent responses
  • Commands are auto-executed with user confirmation (default)
  • User can skip, edit, or execute commands
  • Execution results are displayed clearly
  • Command-line flags work correctly (--auto-execute, --no-confirm)
  • Behavior is consistent with web interface (when confirmation is disabled)
  • All tests pass
  • Documentation updated

Conclusion

The auto-execution feature successfully brings the CLI agent to parity with the web interface while adding additional safety features through confirmation prompts and command editing. The implementation is flexible, well-tested, and thoroughly documented.