The enhanced CLI agent now operates with a structured 4-mode system that provides better error handling, structured JSON output, and a more systematic approach to task execution.
- Purpose: Initialize task execution
- Output: Task description and initial setup
- JSON Structure:
{
"mode": "START",
"timestamp": "2024-01-15T10:30:00.000Z",
"task": "user provided task description",
"iteration": 0
}- Purpose: AI analyzes the task and plans the approach
- Features:
- Multi-step reasoning (3-4 analysis cycles)
- Confidence assessment
- Action planning
- JSON Structure:
{
"mode": "THINK",
"timestamp": "2024-01-15T10:30:01.000Z",
"iteration": 1,
"thinking": {
"analysis": "Detailed analysis of the situation",
"considerations": ["factor 1", "factor 2", "factor 3"],
"plan": "Step-by-step plan for execution",
"confidence": "high|medium|low"
},
"conclusion": "CONTINUE|COMPLETE",
"nextAction": {
"tool": "toolName",
"parameters": { "param1": "value1" }
},
"output": "Final output if conclusion is COMPLETE"
}- Purpose: Execute tools and commands
- Features:
- Tool execution with timing
- Error handling
- Result capture
- JSON Structure:
{
"mode": "ACTION",
"timestamp": "2024-01-15T10:30:02.000Z",
"iteration": 1,
"action": {
"tool": "listDirectory",
"parameters": {"dirPath": "/current/directory"},
"duration": "150ms"
},
"result": {
"success": true,
"items": [...],
"count": 5
},
"success": true
}- Purpose: Analyze action results and determine next steps
- Features:
- Result evaluation
- Continuation logic
- Final output extraction
- JSON Structure:
{
"mode": "OBSERVE",
"timestamp": "2024-01-15T10:30:03.000Z",
"iteration": 1,
"observation": {
"actionSuccess": true,
"actionTool": "listDirectory",
"result": {...},
"timestamp": "2024-01-15T10:30:02.000Z"
},
"status": "CONTINUE|COMPLETE",
"finalOutput": "result if status is COMPLETE"
}- Purpose: Provide final results and summary
- JSON Structure:
{
"mode": "OUTPUT",
"timestamp": "2024-01-15T10:30:04.000Z",
"completed": true,
"iterations": 2,
"finalOutput": "Task execution results",
"summary": {
"totalIterations": 2,
"actionsPerformed": 1,
"successfulActions": 1,
"finalStatus": "completed"
}
}- executeCommand(command) - Execute shell commands with timeout
- createFile(filePath, content) - Create new files with validation
- writeFile(filePath, content) - Write/overwrite files
- readFile(filePath) - Read file contents with metadata
- deleteFile(filePath) - Delete files safely
- listDirectory(dirPath) - List directory contents with details
- searchFiles(pattern, directory) - Search for files matching patterns
- getSystemInfo() - Get comprehensive system information
# Start interactive mode (default behavior)
node src/cli.js
# Then type your requests:
# - "list the files in the current directory"
# - "show me system information"
# - "find all .js files in the src directory"
# - Press Ctrl+C to exit# Run one task and exit
node src/cli.js run "list the files in the current directory"
# Get system information
node src/cli.js run "show me system information"
# Search for specific files
node src/cli.js run "find all .js files in the src directory"# Enable structured JSON output
node src/cli.js run "list files" --json
# JSON output with verbose error details
node src/cli.js run "complex task" --json --verboseimport { Agent } from "./src/agent.js";
import { config } from "./src/config.js";
const agent = new Agent(config);
// Human-readable output
const result1 = await agent.execute("list files", { json: false });
// Structured JSON output
const result2 = await agent.execute("list files", { json: true });
console.log(JSON.stringify(result2, null, 2));The agent can handle and recover from:
- File not found (ENOENT)
- Permission denied (EACCES)
- Command timeouts (ETIMEDOUT)
- Command execution failures
{
"mode": "ERROR",
"timestamp": "2024-01-15T10:30:05.000Z",
"iteration": 2,
"error": {
"message": "Error description",
"recoverable": true,
"context": "Agent loop execution"
}
}OPENROUTER_API_KEY- Your OpenRouter API key for AI functionalityCLIAGENT_MAX_ITERATIONS- Maximum iterations (default: 10)CLIAGENT_TIMEOUT- Command timeout in ms (default: 30000)
# Configure API key
node src/cli.js config
# Or set environment variable
export OPENROUTER_API_KEY="your-api-key-here"# Run comprehensive tests
node test-agent.js
# Test specific functionality
node src/cli.js run "test task" --json --verbose- Directory listing with human output
- System information gathering
- JSON output formatting
- Error handling scenarios
- Multi-iteration workflows
- Maximum Iterations: Prevents infinite loops (default: 10)
- Smart Completion: Automatic task completion detection
- Context Preservation: Maintains state across iterations
- Human-Readable: Colored, formatted output for terminal use
- JSON Mode: Structured data for programmatic consumption
- Verbose Mode: Detailed error information and stack traces
- Input Validation: All tools validate inputs before execution
- Detailed Results: Rich metadata in all tool responses
- Error Recovery: Graceful handling of tool failures
// Parse agent output for automation
const output = JSON.parse(agentOutput);
switch (output.mode) {
case "OUTPUT":
console.log("Task completed:", output.finalOutput);
break;
case "ERROR":
console.error("Task failed:", output.error.message);
break;
}#!/bin/bash
# Automated workflow using JSON output
RESULT=$(node src/cli.js run "backup important files" --json)
STATUS=$(echo $RESULT | jq -r '.mode')
if [ "$STATUS" = "OUTPUT" ]; then
echo "Backup completed successfully"
else
echo "Backup failed"
exit 1
fi- API Key Not Set: Configure OpenRouter API key
- Permission Errors: Ensure proper file/directory permissions
- Timeout Issues: Increase timeout for long-running commands
- JSON Parse Errors: Check for malformed AI responses
# Enable verbose output for debugging
node src/cli.js run "problematic task" --verbose --json- Iteration Limits: Prevents runaway processes
- Command Timeouts: Prevents hanging on long operations
- Memory Management: Efficient handling of large outputs
- Error Recovery: Graceful degradation on failures
- Plugin system for custom tools
- Parallel action execution
- Advanced reasoning patterns
- Integration with more AI providers
- Web interface for agent management
The agent now supports persistent sessions that maintain a rolling context window on disk and inject the most relevant snippets back into the prompt for better reasoning.
# Create a new session
cliagent new-session
# Run a task inside that session
cliagent run "How do I add RAG?" --session <id>
# Show what the model remembers so far
cliagent show-context <id>
# Clear the context window
cliagent clear-context <id>| Key | Description | Default |
|---|---|---|
| CONTEXT_WINDOW_SIZE | Max JSONL lines per session | 4000 |
| RAG_TOP_K | Number of snippets injected | 3 |
| RAG_STRATEGY | Retrieval strategy (keyword, embedding) |
keyword |
| SESSIONS_DIR | Directory for sessions | ~/.cliagent/sessions |
Update these values via cliagent config or by editing your .env file.
- Each mode output is appended to
context_window.jsonl. - Before THINK, top-K snippets are fetched via
fast-fuzzykeyword search.