Command line operations
The command line module provides safe access to system commands, allowing you to execute whitelisted operations.
Security model
The command line tools follow strict security principles:
-
Whitelisted Commands
- Only pre-approved commands are allowed
- Commands are categorized by risk level
- Workspace-modifying operations are restricted
-
Workspace Containment
- File operations are restricted to the workspace
- Absolute paths are resolved relative to workspace
- Parent directory traversal is prevented
-
Permission Model
- Read operations are generally unrestricted
- Write operations are workspace-scoped
- System operations are limited
Available tools
Command line-run command
Executes whitelisted command line operations safely.
Parameters
-
command
(string, required)- The command to execute
- Must start with a whitelisted command
- Arguments are validated
- Workspace paths are enforced
-
working_dir
(string, optional)- Working directory for the command
- Default: "." (current directory)
- Must be within workspace
- Relative paths preferred
Whitelisted commands
Navigation & read operations (unrestricted):
-
ls
: List directory contentsls # List current directory
ls -la # List with details
ls path/ # List specific directory -
pwd
: Print working directorypwd # Show current path
-
cd
: Change directorycd path/ # Change to directory
cd .. # Go up one level
cd # Go to home directory -
cat
: Display file contentscat file.txt # Show file
cat file1.txt file2.txt # Show multiple files -
find
: Search for files by namefind . -name "*.py" # Find Python files
find . -type d # Find directories -
grep
: Search for patterns in filesgrep "pattern" file.txt # Search in file
grep -r "pattern" . # Recursive search -
tree
: Display directory structuretree # Show directory tree
tree -L 2 # Limit to 2 levels -
wc
: Count lines/words/characterswc file.txt # Count all
wc -l file.txt # Count lines -
head/tail
: Show start/end of fileshead file.txt # Show first 10 lines
tail -n 20 file.txt # Show last 20 lines -
diff
: Compare filesdiff file1.txt file2.txt # Show differences
File operations (workspace-restricted):
-
mkdir
: Create directorymkdir new_dir # Create directory
mkdir -p a/b/c # Create nested dirs -
touch
: Create empty filetouch file.txt # Create/update file
-
rm
: Remove file/empty dirrm file.txt # Remove file
rm -r dir/ # Remove directory -
cp
: Copy filecp source.txt dest.txt # Copy file
cp -r src/ dest/ # Copy directory -
mv
: Move/rename filemv old.txt new.txt # Rename file
mv file.txt dir/ # Move file -
echo
: Write to fileecho "text" > file.txt # Write to file
echo "text" >> file.txt # Append to file -
sed
: Edit file contentsed 's/old/new/' file.txt # Replace text
sed -i '' 's/old/new/' file.txt # In-place edit
Example usage
from tyler.models import Agent, Thread, Message
# Create an agent with command line tools
agent = Agent(
model_name="gpt-4o",
purpose="To help with file operations",
tools=["command_line"]
)
# Create a thread with a command request
thread = Thread()
message = Message(
role="user",
content="Can you list all Python files in the current directory?"
)
thread.add_message(message)
# Process the thread - agent will use command_line-run_command tool
processed_thread, new_messages = await agent.go(thread)
Best practices
-
Path Handling
- Use relative paths when possible
- Validate paths before operations
- Handle path resolution carefully
-
File Operations
- Check file existence before operations
- Handle file permissions appropriately
- Use safe file operation patterns
-
Command Construction
- Validate command syntax
- Escape special characters
- Use appropriate flags
-
Error Handling
- Check command exit codes
- Handle common error cases
- Provide meaningful error messages
Common use cases
-
File Management
- Organize project files
- Clean up directories
- Batch file operations
-
Content Search
- Find specific files
- Search file contents
- Pattern matching
-
Directory Navigation
- Browse project structure
- Locate resources
- Manage paths
-
File Analysis
- Compare file versions
- Analyze file contents
- Count lines/words
Security considerations
-
Command Injection
- Validate all inputs
- Escape special characters
- Use safe command construction
-
File Access
- Respect workspace boundaries
- Check file permissions
- Validate file operations
-
Resource Usage
- Monitor command execution
- Handle timeouts
- Prevent resource exhaustion