Skip to content

Commit fdf71b1

Browse files
committed
improved text_editor tool schema
1 parent 4db8f5e commit fdf71b1

3 files changed

Lines changed: 322 additions & 18 deletions

File tree

Lines changed: 78 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,82 @@
1-
Perform text editing operations on files.
1+
# Text Editor Tool: File Content Manipulation
2+
3+
Provides commands to perform text editing operations on files, such as viewing, creating, overwriting, and modifying content, along with an undo capability for recent changes.
4+
5+
This tool is designed to allow an LLM to programmatically interact with file content in a controlled manner.
6+
7+
## When to Use This Tool
8+
9+
This tool is ideal for tasks such as:
10+
11+
* **Viewing File Content**: Inspecting the contents of configuration files, source code, logs, or any text-based file.
12+
* **Creating New Files**: Drafting new text files, scripts, or documents from scratch.
13+
* **Overwriting Existing Files**: Completely replacing the content of an existing file with new text (e.g., updating a configuration).
14+
* **Making Specific Modifications**: Performing targeted string replacements within a file (e.g., correcting a typo, updating a value).
15+
* **Correcting Recent Edits**: Reverting the last file modification if an error was made.
16+
17+
## Commands Overview
218

319
The `command` parameter specifies the operation to perform. Allowed options are:
4-
- `view`: View the content of a file.
5-
- `write`: Create or overwrite a file with the given content
6-
- `str_replace`: Replace a string in a file with a new string.
7-
- `undo_edit`: Undo the last edit made to a file.
820

9-
To use the write command, you must specify `file_text` which will become the new content of the file. Be careful with
10-
existing files! This is a full overwrite, so you must include everything - not just sections you are modifying.
21+
* `view`: View the content of a file.
22+
* `write`: Create or overwrite a file with the given content.
23+
* `str_replace`: Replace a specific string in a file with a new string.
24+
* `undo_edit`: Undo the last edit made by `write` or `str_replace` to a file.
25+
26+
## General Parameters
27+
28+
These parameters are used by most commands:
29+
30+
* `command` (string, **required**): One of `view`, `write`, `str_replace`, `undo_edit`.
31+
* `path` (string, **required**): Absolute path to the file to operate on (e.g., `/project/config.txt`).
32+
33+
## Detailed Command Descriptions and Parameters
34+
35+
### 1. `view`
36+
* **Purpose**: Reads and returns the content of the specified file.
37+
* **Output**: The file's content, formatted in a Markdown code block with language detection.
38+
* **Limitations**:
39+
* Files are limited to 400KB in size.
40+
* File content is limited to 400,000 characters.
41+
42+
### 2. `write`
43+
* **Purpose**: Creates a new file or fully overwrites an existing file with the provided text.
44+
* **Parameters**:
45+
* `file_text` (string, **required**): The entire new content for the file.
46+
* **Important Notes**:
47+
* **Full Overwrite**: This command completely replaces the file's content. If you only mean to modify a part of the file, consider using `str_replace` or a view-modify-write sequence.
48+
* Parent directories will be created if they do not exist.
49+
* The input `file_text` is limited to 400,000 characters.
50+
* **Output**: A success message and the newly written content, formatted in a Markdown code block.
51+
52+
### 3. `str_replace`
53+
* **Purpose**: Replaces an existing string within a file with a new string.
54+
* **Parameters**:
55+
* `old_str` (string, **required**): The exact string to be replaced. This string must appear exactly once in the file.
56+
* `new_str` (string, **required**): The string that will replace `old_str`.
57+
* **Important Notes**:
58+
* **Exact and Unique Match**: The `old_str` must be an *exact and unique* segment of the file content, including any whitespace. If `old_str` is not found, or if it appears multiple times, the operation will fail.
59+
* **Output**: A success message and a snippet showing the context of the change.
60+
61+
### 4. `undo_edit`
62+
* **Purpose**: Reverts the last change made to a file by a `write` or `str_replace` operation performed by this tool.
63+
* **Important Notes**:
64+
* The system maintains a history of recent states (e.g., up to 10, configurable by the server admin) for each edited file.
65+
* If a file was newly created by a `write` command, undoing that write will effectively revert the file to an empty state (or how it was before its first save by this tool).
66+
* **Output**: A success message indicating the undo operation was performed.
67+
68+
## Best Practices for LLMs
69+
70+
* **Verify Before Modifying**: Especially for `str_replace` or `write` on existing files, consider using `view` first to understand the current file structure and content. This helps in formulating accurate `old_str` values or ensuring `file_text` for `write` is complete.
71+
* **Handle `write` with Care**: Remember that `write` is a destructive operation (full overwrite). Ensure `file_text` contains all the content the file should have, not just the parts you are changing.
72+
* **Exactness in `str_replace`**: When using `str_replace`, provide enough of the surrounding text in `old_str` to ensure it's the unique segment you intend to change. The tool will verify uniqueness, but providing an accurate `old_str` is key.
73+
* **Use `undo_edit` Promptly**: If you suspect an edit was incorrect, use `undo_edit` as soon as possible.
74+
* **Check File Paths**: Ensure the `path` provided is an absolute path to a *file*, not a directory (unless the intention is for `write` to create a file within a path where parent directories might need creation).
75+
76+
## Important Limitations
1177

12-
To use the str_replace command, you must specify both `old_str` and `new_str` - the `old_str` needs to exactly match one
13-
unique section of the original file, including any whitespace. Make sure to include enough context that the match is not
14-
ambiguous. The entire original string will be replaced with `new_str`.
78+
* **File Size/Character Limits**:
79+
* `view`: Max file size 400KB, max characters 400,000.
80+
* `write` (input `file_text`): Max characters 400,000.
81+
* **Undo History**: Undo history is limited per file (e.g., to the last 10 states, server configurable).
82+
* **No Directory Operations**: This tool operates on individual files. It does not list directories, delete directories, or perform recursive operations on directories.

src/developer/mod.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,15 @@ impl Developer {
8686
.expect("Failed to create empty gitignore")
8787
}));
8888

89+
// Configure text editor history limit from environment or use default
90+
let text_editor_max_history = std::env::var("TEXT_EDITOR_MAX_HISTORY")
91+
.ok()
92+
.and_then(|s| s.parse::<usize>().ok())
93+
.unwrap_or(10);
94+
8995
Self {
90-
text_editor: TextEditor::new().with_ignore_patterns(ignore_patterns.clone()),
96+
text_editor: TextEditor::new_with_history_limit(text_editor_max_history)
97+
.with_ignore_patterns(ignore_patterns.clone()),
9198
shell: Shell::new().with_ignore_patterns(ignore_patterns),
9299
screen_capture: ScreenCapture::new(),
93100
image_processor: ImageProcessor::new(),
@@ -151,7 +158,7 @@ impl Developer {
151158
command: String,
152159
#[tool(param)]
153160
#[schemars(
154-
description = "Absolute path to file or directory, e.g. `/repo/file.py` or `/repo`."
161+
description = "Absolute path to the file to operate on, e.g. `/repo/file.py`. For the `write` command, parent directories will be created if they do not exist."
155162
)]
156163
path: String,
157164
#[tool(param)]

0 commit comments

Comments
 (0)