Supyagent
ToolsBuilt-in Tools

Edit Tool

Reference for the edit default tool: edit_replace, insert_lines, replace_lines, multi_edit, and regex_replace.

Edit Tool

Script: powers/edit.py Dependencies: pydantic

Surgical file editing using search-and-replace, line insertions, range replacements, batch edits, and regex patterns -- without requiring the agent to rewrite entire files.


edit_replace

Replace exact text in a file. The old_text must match precisely, including whitespace and indentation.

Input

FieldTypeDefaultDescription
filestrrequiredPath to the file to edit
old_textstrrequiredExact text to find (must match precisely, including whitespace)
new_textstrrequiredText to replace old_text with
countint1Maximum number of replacements (0 = all occurrences)

Output

FieldTypeDescription
okbooltrue if at least one replacement was made
replacements_madeintNumber of replacements performed
errorstrError message (e.g., old_text not found)

Examples

// Replace a variable assignment
{"file": "main.py", "old_text": "x = 1", "new_text": "x = 2"}

// Replace a config value
{"file": "config.json", "old_text": "\"debug\": false", "new_text": "\"debug\": true"}

// Replace all occurrences
{"file": "main.py", "old_text": "old_name", "new_text": "new_name", "count": 0}

Notes

  • Always read the file first to find the exact text to replace
  • The match is exact -- whitespace, indentation, and newlines must match precisely
  • Returns an error if old_text is not found in the file

insert_lines

Insert text at a specific line number in a file.

Input

FieldTypeDefaultDescription
filestrrequiredPath to the file to edit
line_numberintrequiredLine number to insert BEFORE (1-based). Use 0 to prepend, -1 to append
textstrrequiredText to insert (can be multiple lines)

Output

FieldTypeDescription
okbooltrue if insertion succeeded
lines_insertedintNumber of lines inserted
errorstrError message on failure

Examples

// Add imports at the top
{"file": "main.py", "line_number": 1, "text": "import os\nimport sys"}

// Append to the end
{"file": "main.py", "line_number": -1, "text": "# End of file"}

// Insert at the very beginning (before line 1)
{"file": "main.py", "line_number": 0, "text": "#!/usr/bin/env python3"}

Notes

  • line_number: 0 prepends at the start of the file
  • line_number: -1 appends at the end of the file
  • A trailing newline is automatically added if missing

replace_lines

Replace a range of lines in a file with new text. The replacement can be more or fewer lines than the original range.

Input

FieldTypeDefaultDescription
filestrrequiredPath to the file to edit
start_lineintrequiredFirst line to replace (1-based, inclusive)
end_lineintrequiredLast line to replace (1-based, inclusive)
new_textstrrequiredReplacement text (can be more or fewer lines)

Output

FieldTypeDescription
okbooltrue if replacement succeeded
lines_removedintNumber of original lines removed
lines_addedintNumber of new lines added
errorstrError message on failure

Examples

// Replace lines 5-10 with a single comment
{"file": "main.py", "start_line": 5, "end_line": 10, "new_text": "# replaced block"}

// Replace a function definition (lines 20-35) with a new implementation
{
  "file": "main.py",
  "start_line": 20,
  "end_line": 35,
  "new_text": "def process(data):\n    return data.strip().lower()"
}

Notes

  • Read the file first to identify the exact line range
  • Line numbers are 1-based and inclusive on both ends
  • The replacement text can span any number of lines

multi_edit

Apply multiple search-and-replace edits to a single file in one operation. Edits are applied sequentially.

Input

FieldTypeDefaultDescription
filestrrequiredPath to the file to edit
editslistrequiredList of {old_text, new_text} edits to apply in order

Each edit in the list:

FieldTypeDescription
old_textstrExact text to find
new_textstrText to replace with

Output

FieldTypeDescription
okbooltrue if at least one edit was applied
appliedintNumber of edits successfully applied
failedlistList of error messages for failed edits
errorstrError message on failure

Examples

// Multiple edits in one call
{
  "file": "main.py",
  "edits": [
    {"old_text": "import os", "new_text": "import os\nimport sys"},
    {"old_text": "x = 1", "new_text": "x = 42"},
    {"old_text": "DEBUG = False", "new_text": "DEBUG = True"}
  ]
}

Notes

  • Edits are applied sequentially -- each edit sees the result of the previous one
  • If an edit fails (old_text not found), it is skipped and reported in failed, but remaining edits still proceed
  • The file is only written if at least one edit was applied

regex_replace

Replace text in a file using a regular expression pattern. Supports capture groups.

Input

FieldTypeDefaultDescription
filestrrequiredPath to the file to edit
patternstrrequiredRegex pattern to match
replacementstrrequiredReplacement string (can use \1, \2 for capture groups)
countint0Maximum replacements (0 = all)

Output

FieldTypeDescription
okbooltrue if pattern was found and replaced
replacements_madeintNumber of replacements performed
errorstrError message on failure (including invalid regex)

Examples

// Add self parameter to all methods
{"file": "main.py", "pattern": "def (\\w+)\\(\\):", "replacement": "def \\1(self):"}

// Update version string
{"file": "config.py", "pattern": "VERSION = \"[^\"]+\"", "replacement": "VERSION = \"2.0.0\""}

// Replace first occurrence only
{"file": "main.py", "pattern": "TODO.*$", "replacement": "DONE", "count": 1}

Notes

  • Uses Python re module syntax
  • Returns an error if the regex pattern is invalid
  • Returns an error if the pattern is not found in the file
  • Capture groups use \1, \2, etc. in the replacement string

Tips

  • Prefer edit_replace over write_file when making small changes -- it's safer because it only changes what you specify
  • Always read the file first to find exact text for replacement
  • Use multi_edit when you need to make several changes to the same file -- it's more efficient than multiple edit_replace calls
  • Use regex_replace for pattern-based changes like renaming variables, updating version strings, or fixing formatting
  • Use replace_lines when you know the exact line range to change (pair with read_file line numbers)