Inspired by Claude Code, this implementation adds advanced agentic capabilities including persistent shell sessions, background process management, task planning, and parallel tool execution.
Persistent shell sessions that maintain working directory state across commands.
- Session Persistence: Working directory (
cd) persists across commands - Background Execution: Run long-running commands asynchronously
- Streaming Output: Real-time command output display
- Timeout Control: Configurable command timeouts
- Command History: Track all executed commands with results
run_command- Execute shell commands with enhanced capabilitiesget_shell_cwd- Get current working directorylist_background_processes- List all background processesget_background_output- Get output from a background processkill_background_process- Terminate a background process
/shell <cmd> # Execute shell command
/sh <cmd> # Alias for /shell
/cwd # Show current working directory
/pwd # Alias for /cwd
/bg # List background processes
/background # Alias for /bg// Basic command execution
await shellManager.execute('ls -la');
// Change directory (persists across calls)
await shellManager.execute('cd /home/user/projects');
await shellManager.execute('pwd'); // Shows /home/user/projects
// Background execution
await shellManager.execute('npm run build', { background: true });
// Streaming output
await shellManager.executeStreaming('npm test', (data) => {
console.log(data.data);
}, { timeout: 300000 });Intelligent task planning, tracking, and execution system.
- Task Planning: Break down complex operations into steps
- Status Tracking: Monitor tasks (pending, in_progress, completed, failed, blocked)
- Dependency Management: Define task dependencies
- Progress Visualization: Display progress with tables and status symbols
- Task Duration Tracking: Monitor execution time
- ๐ต pending - Task not yet started
- ๐ท in_progress - Currently executing
- ๐ข completed - Successfully finished
- ๐ด failed - Execution failed
- ๐ก blocked - Waiting for dependencies
/tasks # View current task list with progressconst { TaskManager } = require('./lib/agent');
const taskManager = new TaskManager();
// Add tasks
taskManager.addTask('Install dependencies');
taskManager.addTask('Run tests');
taskManager.addTask('Build project');
// Execute with status updates
taskManager.startTask('task_1');
// ... do work ...
taskManager.completeTask('task_1', { result: 'Success' });
// Display progress
taskManager.display();Execute multiple tools concurrently for improved performance.
- Concurrent Execution: Run up to 5 tools simultaneously
- Dependency Resolution: Automatically handle task dependencies
- Error Handling: Graceful failure handling for parallel operations
- Execution Modes: Parallel, sequential, or dependency-based
const { ParallelToolExecutor } = require('./lib/tools');
const parallelExecutor = new ParallelToolExecutor(toolExecutor);
// Execute multiple tools in parallel
const results = await parallelExecutor.executeParallel([
{ name: 'read_file', args: { path: 'package.json' } },
{ name: 'read_file', args: { path: 'README.md' } },
{ name: 'list_directory', args: { path: '.' } }
]);
// Execute with dependencies
const results = await parallelExecutor.executeWithDependencies([
{ name: 'create_directory', args: { path: './build' }, dependencies: [] },
{ name: 'write_file', args: { path: './build/output.txt', content: 'Hello' }, dependencies: [0] }
]);Build complex multi-step operations with conditional execution.
- Step-by-Step Execution: Chain multiple tools together
- Conditional Logic: Skip steps based on conditions
- Result Callbacks: Process intermediate results
- Error Propagation: Stop chain on first error
const { ToolChain } = require('./lib/tools');
const chain = new ToolChain(toolExecutor);
chain
.add('create_directory', { path: './dist' })
.add('write_file', {
path: './dist/index.js',
content: 'console.log("Hello");'
})
.add('run_command', {
command: 'node ./dist/index.js'
}, {
onResult: (result) => {
console.log('Output:', result.stdout);
}
});
await chain.execute();-
ShellManager (
lib/shell.js)- Manages persistent shell sessions
- Handles background processes
- Maintains working directory state
- Provides streaming output
-
TaskManager (
lib/agent.js)- Plans and tracks tasks
- Manages task dependencies
- Visualizes progress
- Exports/imports task state
-
ParallelToolExecutor (
lib/tools.js)- Executes tools concurrently
- Resolves dependencies
- Handles errors gracefully
-
ToolChain (
lib/tools.js)- Builds complex workflows
- Supports conditional execution
- Provides result callbacks
The agentic features are integrated into the Grok CLI through:
- Enhanced Tool Definitions: New tools for shell and process management
- Slash Commands: Direct access to shell and task features
- Function Calling: Tools available to the LLM for autonomous execution
> Help me set up a new React project and run tests
Grok will:
1. Create project directory
2. Initialize npm
3. Install dependencies
4. Create project structure
5. Run tests in background
6. Monitor test output> Clone the repo, install deps, and start the dev server
Grok can:
1. Execute git clone
2. Change to project directory (cd persists)
3. Run npm install
4. Start dev server in background
5. Monitor server logs> Check disk usage, clean logs older than 30 days, and restart services
Grok can:
1. Execute df -h in parallel with du commands
2. Find and remove old logs
3. Restart services with systemctl
4. Monitor restart statusconst shellManager = new ShellManager();
// Execute with custom options
await shellManager.execute('command', {
timeout: 60000, // 1 minute timeout
background: true, // Run in background
cwd: '/custom/path' // Override working directory
});const taskManager = new TaskManager();
// Add task with dependencies
taskManager.addTask('Build project', {
dependencies: ['task_1', 'task_2'],
metadata: { priority: 'high' }
});const parallelExecutor = new ParallelToolExecutor(toolExecutor);
parallelExecutor.maxConcurrent = 3; // Limit to 3 concurrent executions# In Grok interactive mode:
/shell ls -la
/shell cd /tmp
/cwd # Should show /tmp
/shell pwd # Should show /tmp/shell sleep 30 &
/bg # Should show the background process/tasks # View current tasks- Command Approval: All commands require approval (except in auto modes)
- Sandboxing: Commands run in the user's context (no privilege escalation)
- Timeout Protection: All commands have configurable timeouts
- Error Handling: Graceful failure handling prevents cascading errors
class ShellManager {
execute(command, options) // Execute command
executeStreaming(command, onData, options) // Stream output
getCwd(sessionId) // Get working directory
listSessions() // List all sessions
}class TaskManager {
addTask(description, options) // Add new task
startTask(id) // Start task
completeTask(id, result) // Complete task
display() // Display task list
toJSON() // Export tasks
}class ParallelToolExecutor {
executeParallel(toolCalls) // Execute in parallel
executeWithDependencies(toolCalls) // Resolve dependencies
executeSequential(toolCalls) // Sequential execution
}- Efficiency: Parallel execution reduces overall execution time
- Visibility: Task tracking provides clear progress indication
- Reliability: Persistent sessions and error handling improve robustness
- Flexibility: Multiple execution modes support various use cases
- Autonomy: LLM can plan and execute complex multi-step operations
- MCP (Model Context Protocol) integration
- Advanced task scheduling and prioritization
- Distributed execution across multiple sessions
- Enhanced security with sandboxing
- Web-based task monitoring dashboard
- Integration with CI/CD pipelines
- Task templates and presets
- Rollback capabilities for failed operations
Inspired by Claude Code - These features bring autonomous agentic capabilities to Grok CLI, enabling it to handle complex, multi-step operations with intelligence and efficiency.