Skip to content

[FEATURE] Implement Edit Tool #54

@edenreich

Description

@edenreich

Summary

We need to implement a new built-in tool for the Inference Gateway CLI called Edit. This tool enables exact string replacements in files with strict safety rules to prevent unintended modifications.

Tool Spec

{
  "name": "Edit",
  "description": "Performs exact string replacements in files.\n\nUsage:\n- You must use your `Read` tool at least once in the conversation before editing. This tool will error if you attempt an edit without reading the file.\n- When editing text from Read tool output, ensure you preserve the exact indentation (tabs/spaces) as it appears AFTER the line number prefix. The line number prefix format is: spaces + line number + tab. Everything after that tab is the actual file content to match. Never include any part of the line number prefix in the old_string or new_string.\n- ALWAYS prefer editing existing files in the codebase. NEVER write new files unless explicitly required.\n- Only use emojis if the user explicitly requests it. Avoid adding emojis to files unless asked.\n- The edit will FAIL if `old_string` is not unique in the file. Either provide a larger string with more surrounding context to make it unique or use `replace_all` to change every instance of `old_string`.\n- Use `replace_all` for replacing and renaming strings across the file. This parameter is useful if you want to rename a variable for instance.",
  "input_schema": {
    "$schema": "http://json-schema.org/draft-07/schema#",
    "type": "object",
    "additionalProperties": false,
    "properties": {
      "file_path": {
        "type": "string",
        "description": "The absolute path to the file to modify"
      },
      "old_string": {
        "type": "string",
        "description": "The text to replace"
      },
      "new_string": {
        "type": "string",
        "description": "The text to replace it with (must be different from old_string)"
      },
      "replace_all": {
        "type": "boolean",
        "default": false,
        "description": "Replace all occurrences of old_string (default false)"
      }
    },
    "required": ["file_path", "old_string", "new_string"]
  }
}

Acceptance Criteria

  • Add Edit as a built-in tool in the CLI.
  • Ensure it errors if invoked before a Read tool has been used.
  • Enforce correct handling of indentation and line number prefixes.
  • Ensure old_string must be unique unless replace_all is true.
  • Fail safely with clear error messages if edits cannot be applied.
  • Include unit tests for single replacement, multiple replacements, non-unique matches, and safety checks.

Notes

  • The Edit tool must only work with existing files.
  • No creation of new files unless explicitly requested by the user.
  • Keep behavior deterministic and safe for LLM usage.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions