from praisonai.code import ( set_workspace, code_read_file, code_write_file, code_apply_diff, code_execute_command, CODE_TOOLS)# Set the workspace directoryset_workspace("/path/to/project")# Read a file with line numberscontent = code_read_file("src/main.py")print(content)# Write to a filecode_write_file("src/new_file.py", "print('Hello, World!')")# Execute a commandoutput = code_execute_command("python --version")
Execute Python source in a subprocess (not a sandbox — the child inherits the current interpreter and environment). Source is passed as an argv list, so newlines, backslashes and quotes reach the child interpreter exactly as written, identically on POSIX and Windows.
from praisonai.code import code_run_pythonresult = code_run_python( code="print('hello')\nfor i in range(3): print(i)", cwd=".", timeout=30,)# {# 'success': True, 'exit_code': 0,# 'stdout': 'hello\n0\n1\n2\n',# 'stderr': '',# 'command': '/usr/bin/python -c <code>',# 'cwd': '.',# }
Field
Type
Notes
success
bool
True iff exit_code == 0
exit_code
int
-1 on timeout / launch error
stdout
str
Child stdout
stderr
str
Child stderr
command
str
Always "<python> -c <code>" — source is not echoed back
cwd
str
Working directory used
error
str
Present only on timeout / launch error
Not a sandbox. Do not pass untrusted code from end users without isolating the host process (subprocess with setrlimit, container, or VM). See python_tools for the safer, already-timeout-boxed alternative for user-supplied code.
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 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):
from praisonai.code.tools.write_file import append_to_fileresult = append_to_file( "logs/run.log", "new line\n", workspace="/path/to/project", # optional — defaults to cwd create_if_missing=True, # default True encoding="utf-8", # default utf-8)
Returns {"success": bool, "path": str, "created": bool, "bytes_appended": int} on success, or {"success": False, "error": str} on failure.
from praisonai.code import set_workspace, get_workspace# Set workspaceset_workspace("/path/to/project")# Get current workspaceworkspace = get_workspace()print(workspace) # "/path/to/project"