AI-powered code editing tools for agents to read, write, and modify code files with precision using SEARCH/REPLACE diffs
PraisonAI Code provides AI agents with powerful tools to read, write, and modify code files with surgical precision. Inspired by Kilo Code’s architecture, it uses a SEARCH/REPLACE diff strategy with fuzzy matching for reliable code modifications.
The CODE_TOOLS list contains all tools ready for use with PraisonAI agents:
Copy
from praisonaiagents import Agentfrom praisonai.code import CODE_TOOLS, set_workspace# Set workspace before creating agentset_workspace("/path/to/project")# Create an agent with code editing capabilitiesagent = Agent( name="Code Editor", instructions="""You are a code editing assistant. Use the code tools to read, modify, and write files. Always read a file before modifying it.""", tools=CODE_TOOLS)# The agent can now edit codeagent.start("Add error handling to the main.py file")
List files and directories with optional filtering.
Copy
from praisonai.code import code_list_files, set_workspaceset_workspace("/my/project")# List current directoryfiles = code_list_files(".")# List recursively with extension filterfiles = code_list_files("src", recursive=True, extensions="py,js")
Apply precise, surgical modifications using SEARCH/REPLACE diffs.
Copy
from praisonai.code import code_apply_diff, set_workspaceset_workspace("/my/project")diff = """<<<<<<< SEARCH:start_line:5-------def calculate_total(items): total = 0 for item in items: total += item return total=======def calculate_total(items): \"\"\"Calculate total with 10% markup.\"\"\" return sum(item * 1.1 for item in items)>>>>>>> REPLACE"""result = code_apply_diff("src/utils.py", diff)
Execute shell commands safely within the workspace.
Copy
from praisonai.code import code_execute_command, set_workspaceset_workspace("/my/project")# Run testsresult = code_execute_command("python -m pytest tests/")print(result)# Run linterresult = code_execute_command("ruff check src/")# Run in subdirectoryresult = code_execute_command("npm test", cwd="frontend")
The diff strategy includes fuzzy matching using Levenshtein distance, which helps when:
Code has been slightly modified since you read it
There are minor whitespace differences
Line numbers have shifted due to other changes
Copy
from praisonai.code import apply_diff# The tool uses a 95% similarity threshold by default# This allows for minor variations while preventing incorrect matches
All file operations are restricted to the configured workspace:
Copy
set_workspace("/my/project")# This works - within workspacecode_read_file("src/main.py")# This fails - path traversal blockedcode_read_file("../../../etc/passwd")# Error: Path '../../../etc/passwd' is outside the workspace
Here’s a complete workflow showing an agent that adds error handling to a function:
Copy
from praisonaiagents import Agentfrom praisonai.code import CODE_TOOLS, set_workspace# Configure workspaceset_workspace("/path/to/my/project")# Create coding agentagent = Agent( name="Error Handler", instructions="""You are a Python expert. Your task is to: 1. Read the specified file 2. Identify functions without error handling 3. Add appropriate try/except blocks 4. Verify the changes compile correctly Always use code_read_file before making changes. Use code_apply_diff for precise modifications. Run tests after making changes.""", tools=CODE_TOOLS)# Run the agentresult = agent.start( "Add error handling to all database functions in src/database.py")print(result)