ITADN
obra/lace/Issues

Unify file-edit and file-insert tools into single file-edit tool

#24Openobra 创建于 2025-06-27
O
obracommented
## Overview Replace the current `file-edit.ts` and `file-insert.ts` tools with a single unified `file-edit` tool that handles all file modification operations. No backwards compatibility - clean slate design. ## Goals - Reduce cognitive load (one tool vs two) - Add `replace_all` option for bulk replacements - Improve error messages with actionable guidance - Emphasize raw strings (no escaping) to prevent model confusion ## Tool Specification ### New Schema ```typescript { name: 'file_edit', description: `Edit files with exact text matching. Always use file_read first. REPLACE MODE: old_text contains content to replace INSERT MODE: old_text is "", insert_line specified APPEND MODE: old_text is "", no insert_line CRITICAL: Use raw strings - do NOT escape quotes, newlines, or special characters`, input_schema: { properties: { path: { type: 'string', description: 'File path to edit' }, old_text: { type: 'string', description: 'Exact text to replace. Empty "" for insert/append.' }, new_text: { type: 'string', description: 'Replacement or new content' }, replace_all: { type: 'boolean', default: false, description: 'Replace all matches vs require exactly one' }, insert_line: { type: 'number', description: 'Line number to insert after (INSERT mode only)' } }, required: ['path', 'old_text', 'new_text'] } } ``` ### Operation Modes - **REPLACE**: `old_text` has content → replace exact matches - **INSERT**: `old_text=""` + `insert_line=N` → insert after line N - **APPEND**: `old_text=""` + no `insert_line` → append to end ### Improved Error Messages ``` No matches found in ${path}. SOLUTION: Use file_read to copy exact text including all whitespace. Found ${count} matches. Use replace_all=true or add more context to old_text. For INSERT mode: old_text="", insert_line=5, new_text="content" ``` ## Implementation Tasks - [ ] **Test First**: Write comprehensive tests covering all modes and edge cases - [ ] **Implement**: Single unified FileEditTool with mode detection logic - [ ] **Clean Up**: Delete file-insert tool and tests completely - [ ] **Update**: Remove file-insert registration from ToolExecutor - [ ] **Validate**: Ensure all tests pass and build succeeds ## Test Coverage Required - Replace mode: single match, multiple matches (error/success), no matches - Insert mode: after specified line, line out of range errors - Append mode: end of file, empty files, newline handling - Multi-line content with exact whitespace preservation - Parameter validation and helpful error messages ## Files to Change **Remove:** - `src/tools/implementations/file-insert.ts` - `src/tools/__tests__/file-insert.test.ts` **Modify:** - `src/tools/implementations/file-edit.ts` (complete rewrite) - `src/tools/__tests__/file-edit.test.ts` (expand tests) - `src/tools/executor.ts` (remove file-insert registration) See detailed implementation plan: `docs/plans/file-edit-tool.md`
0 条评论