Code Editing
PraisonAI provides a powerful code editing module for AI-powered code manipulation, inspired by Kilo Code’s architecture.
Installation
The code editing module is included with PraisonAI:
Quick Start
The code editing module uses a SEARCH/REPLACE diff format for precise code modifications:
Line Number Hints
For faster matching in large files, use line number hints:
code_read_file
Read file contents with optional line range:
code_write_file
Create or overwrite a file:
code_apply_diff
Apply SEARCH/REPLACE diffs:
code_search_replace
Apply multiple search/replace operations:
code_list_files
List files in a directory:
code_execute_command
Execute shell commands:
Using with Agents
The code tools can be used directly with PraisonAI agents:
Features
Fuzzy Matching
The diff application uses fuzzy matching with Levenshtein distance:
Indentation Preservation
The module automatically preserves indentation when applying diffs:
Workspace Security
Every file-modifying tool (code_apply_diff, code_search_replace, code_write_file, code_read_file, code_list_files, and the low-level append_to_file) is confined to the workspace root. Paths that resolve outside the workspace are rejected with "Path '<path>' is outside the workspace".
- Default workspace is the current working directory. If you never call
set_workspace(...), the tools confine writes and reads to cwd. Absolute paths outside cwd are rejected.
- Explicit
set_workspace(...) is still recommended — it makes the confinement boundary explicit and portable across working directories.
- Protected-path check runs first. System paths (e.g.
/etc/hosts, ~/.ssh/) are rejected even if they sit inside the workspace. See Protected Paths.
- Gitignore Support: Respects
.gitignore patterns for listing.
- Access Control: Configurable file access rules.
Behaviour change (PraisonAI #3087, commit d1d0272). Before this fix, workspace confinement was silently skipped in apply_diff, search_replace, and append_to_file whenever no workspace was set. Agents could escape cwd via absolute paths like apply_diff("/etc/hosts", ...). After the fix, cwd is used as the default workspace and confinement is always enforced. If you relied on writing to absolute paths outside cwd without calling set_workspace(), call set_workspace(...) at the intended root instead.
append_to_file
append_to_file is available as a low-level import: from praisonai.code.tools.write_file import append_to_file. An agent-tool wrapper (code_append_to_file) is not yet exported from praisonai.code.
Append content to a file (creates it if missing):
Returns {"success": bool, "path": str, "created": bool, "bytes_appended": int} on success, or {"success": False, "error": str} on failure.
Configuration
Set Workspace
Environment Variables
Module Structure