Related to GitHub Issue #20
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.
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.
💬 > 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💬 > 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]
💬 >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 responsesconfirm_execution(default:True) - Enable/disable confirmation prompts before executing
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 NoneFeatures:
- Case-insensitive matching (
run:orRUN:) - Extracts command until newline or end of string
- Returns
Noneif no command found
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 FalseOptions:
yoryes- Execute the command as-isnorno- Skip executionedit- Edit the command before executing- Handles
KeyboardInterruptandEOFErrorgracefully
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']}"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}"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-confirmUpdated 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
==================================================
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
- Auto-execution with confirmation - Default behavior
- Auto-execution without confirmation - Using
--no-confirm - Manual execution only - Using
--no-auto-execute - Command editing - Using
editoption - Skipping execution - Using
noption
python agent.py- ✅ Safest option
- ✅ User reviews each command
- ✅ Can edit commands before execution
- ✅ Recommended for general use
python agent.py --no-confirm⚠️ Commands execute immediately⚠️ Matches web interface behavior⚠️ Use only in trusted environments
python agent.py --no-auto-execute- ✅ Most conservative option
- ✅ User must manually type
run:commands - ✅ Best for sensitive systems
- Confirmation is enabled by default to prevent accidental execution of dangerous commands
- Commands are subject to the same validation as manual
run:commands - Users can disable auto-execution with
--no-auto-executeflag - Edit option allows command review before execution
- All executions are logged to memory.json
-
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
-
README.md
- Updated Local AI Agent section
- Added auto-execution examples
- Added command-line options
| 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!
- Improved User Experience - No more manual copy/paste of commands
- Consistency - CLI and web interfaces behave similarly
- Safety - Confirmation prompts prevent accidental execution
- Flexibility - Multiple configuration modes for different use cases
- Transparency - Clear visual indicators for all actions
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)
- #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)
- 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
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.