edit.go

  1package tools
  2
  3import (
  4	"context"
  5	"encoding/json"
  6	"fmt"
  7	"os"
  8	"path/filepath"
  9	"strings"
 10	"time"
 11
 12	"github.com/kujtimiihoxha/termai/internal/config"
 13	"github.com/kujtimiihoxha/termai/internal/lsp"
 14	"github.com/kujtimiihoxha/termai/internal/permission"
 15	"github.com/sergi/go-diff/diffmatchpatch"
 16)
 17
 18type EditParams struct {
 19	FilePath  string `json:"file_path"`
 20	OldString string `json:"old_string"`
 21	NewString string `json:"new_string"`
 22}
 23
 24type EditPermissionsParams struct {
 25	FilePath  string `json:"file_path"`
 26	OldString string `json:"old_string"`
 27	NewString string `json:"new_string"`
 28	Diff      string `json:"diff"`
 29}
 30
 31type editTool struct {
 32	lspClients  map[string]*lsp.Client
 33	permissions permission.Service
 34}
 35
 36const (
 37	EditToolName    = "edit"
 38	editDescription = `Edits files by replacing text, creating new files, or deleting content. For moving or renaming files, use the Bash tool with the 'mv' command instead. For larger file edits, use the FileWrite tool to overwrite files.
 39
 40Before using this tool:
 41
 421. Use the FileRead tool to understand the file's contents and context
 43
 442. Verify the directory path is correct (only applicable when creating new files):
 45   - Use the LS tool to verify the parent directory exists and is the correct location
 46
 47To make a file edit, provide the following:
 481. file_path: The absolute path to the file to modify (must be absolute, not relative)
 492. old_string: The text to replace (must be unique within the file, and must match the file contents exactly, including all whitespace and indentation)
 503. new_string: The edited text to replace the old_string
 51
 52Special cases:
 53- To create a new file: provide file_path and new_string, leave old_string empty
 54- To delete content: provide file_path and old_string, leave new_string empty
 55
 56The tool will replace ONE occurrence of old_string with new_string in the specified file.
 57
 58CRITICAL REQUIREMENTS FOR USING THIS TOOL:
 59
 601. UNIQUENESS: The old_string MUST uniquely identify the specific instance you want to change. This means:
 61   - Include AT LEAST 3-5 lines of context BEFORE the change point
 62   - Include AT LEAST 3-5 lines of context AFTER the change point
 63   - Include all whitespace, indentation, and surrounding code exactly as it appears in the file
 64
 652. SINGLE INSTANCE: This tool can only change ONE instance at a time. If you need to change multiple instances:
 66   - Make separate calls to this tool for each instance
 67   - Each call must uniquely identify its specific instance using extensive context
 68
 693. VERIFICATION: Before using this tool:
 70   - Check how many instances of the target text exist in the file
 71   - If multiple instances exist, gather enough context to uniquely identify each one
 72   - Plan separate tool calls for each instance
 73
 74WARNING: If you do not follow these requirements:
 75   - The tool will fail if old_string matches multiple locations
 76   - The tool will fail if old_string doesn't match exactly (including whitespace)
 77   - You may change the wrong instance if you don't include enough context
 78
 79When making edits:
 80   - Ensure the edit results in idiomatic, correct code
 81   - Do not leave the code in a broken state
 82   - Always use absolute file paths (starting with /)
 83
 84Remember: when making multiple file edits in a row to the same file, you should prefer to send all edits in a single message with multiple calls to this tool, rather than multiple messages with a single call each.`
 85)
 86
 87func NewEditTool(lspClients map[string]*lsp.Client, permissions permission.Service) BaseTool {
 88	return &editTool{
 89		lspClients:  lspClients,
 90		permissions: permissions,
 91	}
 92}
 93
 94func (e *editTool) Info() ToolInfo {
 95	return ToolInfo{
 96		Name:        EditToolName,
 97		Description: editDescription,
 98		Parameters: map[string]any{
 99			"file_path": map[string]any{
100				"type":        "string",
101				"description": "The absolute path to the file to modify",
102			},
103			"old_string": map[string]any{
104				"type":        "string",
105				"description": "The text to replace",
106			},
107			"new_string": map[string]any{
108				"type":        "string",
109				"description": "The text to replace it with",
110			},
111		},
112		Required: []string{"file_path", "old_string", "new_string"},
113	}
114}
115
116func (e *editTool) Run(ctx context.Context, call ToolCall) (ToolResponse, error) {
117	var params EditParams
118	if err := json.Unmarshal([]byte(call.Input), &params); err != nil {
119		return NewTextErrorResponse("invalid parameters"), nil
120	}
121
122	if params.FilePath == "" {
123		return NewTextErrorResponse("file_path is required"), nil
124	}
125
126	if !filepath.IsAbs(params.FilePath) {
127		wd := config.WorkingDirectory()
128		params.FilePath = filepath.Join(wd, params.FilePath)
129	}
130
131	if params.OldString == "" {
132		result, err := e.createNewFile(params.FilePath, params.NewString)
133		if err != nil {
134			return NewTextErrorResponse(fmt.Sprintf("error creating file: %s", err)), nil
135		}
136		return NewTextResponse(result), nil
137	}
138
139	if params.NewString == "" {
140		result, err := e.deleteContent(params.FilePath, params.OldString)
141		if err != nil {
142			return NewTextErrorResponse(fmt.Sprintf("error deleting content: %s", err)), nil
143		}
144		return NewTextResponse(result), nil
145	}
146
147	result, err := e.replaceContent(params.FilePath, params.OldString, params.NewString)
148	if err != nil {
149		return NewTextErrorResponse(fmt.Sprintf("error replacing content: %s", err)), nil
150	}
151
152	waitForLspDiagnostics(ctx, params.FilePath, e.lspClients)
153	result = fmt.Sprintf("<result>\n%s\n</result>\n", result)
154	result += appendDiagnostics(params.FilePath, e.lspClients)
155	return NewTextResponse(result), nil
156}
157
158func (e *editTool) createNewFile(filePath, content string) (string, error) {
159	fileInfo, err := os.Stat(filePath)
160	if err == nil {
161		if fileInfo.IsDir() {
162			return "", fmt.Errorf("path is a directory, not a file: %s", filePath)
163		}
164		return "", fmt.Errorf("file already exists: %s. Use the Replace tool to overwrite an existing file", filePath)
165	} else if !os.IsNotExist(err) {
166		return "", fmt.Errorf("failed to access file: %w", err)
167	}
168
169	dir := filepath.Dir(filePath)
170	if err = os.MkdirAll(dir, 0o755); err != nil {
171		return "", fmt.Errorf("failed to create parent directories: %w", err)
172	}
173
174	p := e.permissions.Request(
175		permission.CreatePermissionRequest{
176			Path:        filepath.Dir(filePath),
177			ToolName:    EditToolName,
178			Action:      "create",
179			Description: fmt.Sprintf("Create file %s", filePath),
180			Params: EditPermissionsParams{
181				FilePath:  filePath,
182				OldString: "",
183				NewString: content,
184				Diff:      GenerateDiff("", content),
185			},
186		},
187	)
188	if !p {
189		return "", fmt.Errorf("permission denied")
190	}
191
192	err = os.WriteFile(filePath, []byte(content), 0o644)
193	if err != nil {
194		return "", fmt.Errorf("failed to write file: %w", err)
195	}
196
197	recordFileWrite(filePath)
198	recordFileRead(filePath)
199
200	return "File created: " + filePath, nil
201}
202
203func (e *editTool) deleteContent(filePath, oldString string) (string, error) {
204	fileInfo, err := os.Stat(filePath)
205	if err != nil {
206		if os.IsNotExist(err) {
207			return "", fmt.Errorf("file not found: %s", filePath)
208		}
209		return "", fmt.Errorf("failed to access file: %w", err)
210	}
211
212	if fileInfo.IsDir() {
213		return "", fmt.Errorf("path is a directory, not a file: %s", filePath)
214	}
215
216	if getLastReadTime(filePath).IsZero() {
217		return "", fmt.Errorf("you must read the file before editing it. Use the View tool first")
218	}
219
220	modTime := fileInfo.ModTime()
221	lastRead := getLastReadTime(filePath)
222	if modTime.After(lastRead) {
223		return "", fmt.Errorf("file %s has been modified since it was last read (mod time: %s, last read: %s)",
224			filePath, modTime.Format(time.RFC3339), lastRead.Format(time.RFC3339))
225	}
226
227	content, err := os.ReadFile(filePath)
228	if err != nil {
229		return "", fmt.Errorf("failed to read file: %w", err)
230	}
231
232	oldContent := string(content)
233
234	index := strings.Index(oldContent, oldString)
235	if index == -1 {
236		return "", fmt.Errorf("old_string not found in file. Make sure it matches exactly, including whitespace and line breaks")
237	}
238
239	lastIndex := strings.LastIndex(oldContent, oldString)
240	if index != lastIndex {
241		return "", fmt.Errorf("old_string appears multiple times in the file. Please provide more context to ensure a unique match")
242	}
243
244	newContent := oldContent[:index] + oldContent[index+len(oldString):]
245
246	p := e.permissions.Request(
247		permission.CreatePermissionRequest{
248			Path:        filepath.Dir(filePath),
249			ToolName:    EditToolName,
250			Action:      "delete",
251			Description: fmt.Sprintf("Delete content from file %s", filePath),
252			Params: EditPermissionsParams{
253				FilePath:  filePath,
254				OldString: oldString,
255				NewString: "",
256				Diff:      GenerateDiff(oldContent, newContent),
257			},
258		},
259	)
260	if !p {
261		return "", fmt.Errorf("permission denied")
262	}
263
264	err = os.WriteFile(filePath, []byte(newContent), 0o644)
265	if err != nil {
266		return "", fmt.Errorf("failed to write file: %w", err)
267	}
268
269	recordFileWrite(filePath)
270	recordFileRead(filePath)
271
272	return "Content deleted from file: " + filePath, nil
273}
274
275func (e *editTool) replaceContent(filePath, oldString, newString string) (string, error) {
276	fileInfo, err := os.Stat(filePath)
277	if err != nil {
278		if os.IsNotExist(err) {
279			return "", fmt.Errorf("file not found: %s", filePath)
280		}
281		return "", fmt.Errorf("failed to access file: %w", err)
282	}
283
284	if fileInfo.IsDir() {
285		return "", fmt.Errorf("path is a directory, not a file: %s", filePath)
286	}
287
288	if getLastReadTime(filePath).IsZero() {
289		return "", fmt.Errorf("you must read the file before editing it. Use the View tool first")
290	}
291
292	modTime := fileInfo.ModTime()
293	lastRead := getLastReadTime(filePath)
294	if modTime.After(lastRead) {
295		return "", fmt.Errorf("file %s has been modified since it was last read (mod time: %s, last read: %s)",
296			filePath, modTime.Format(time.RFC3339), lastRead.Format(time.RFC3339))
297	}
298
299	content, err := os.ReadFile(filePath)
300	if err != nil {
301		return "", fmt.Errorf("failed to read file: %w", err)
302	}
303
304	oldContent := string(content)
305
306	index := strings.Index(oldContent, oldString)
307	if index == -1 {
308		return "", fmt.Errorf("old_string not found in file. Make sure it matches exactly, including whitespace and line breaks")
309	}
310
311	lastIndex := strings.LastIndex(oldContent, oldString)
312	if index != lastIndex {
313		return "", fmt.Errorf("old_string appears multiple times in the file. Please provide more context to ensure a unique match")
314	}
315
316	newContent := oldContent[:index] + newString + oldContent[index+len(oldString):]
317
318	startIndex := max(0, index-3)
319	oldEndIndex := min(len(oldContent), index+len(oldString)+3)
320	newEndIndex := min(len(newContent), index+len(newString)+3)
321
322	diff := GenerateDiff(oldContent[startIndex:oldEndIndex], newContent[startIndex:newEndIndex])
323
324	p := e.permissions.Request(
325		permission.CreatePermissionRequest{
326			Path:        filepath.Dir(filePath),
327			ToolName:    EditToolName,
328			Action:      "replace",
329			Description: fmt.Sprintf("Replace content in file %s", filePath),
330			Params: EditPermissionsParams{
331				FilePath:  filePath,
332				OldString: oldString,
333				NewString: newString,
334				Diff:      diff,
335			},
336		},
337	)
338	if !p {
339		return "", fmt.Errorf("permission denied")
340	}
341
342	err = os.WriteFile(filePath, []byte(newContent), 0o644)
343	if err != nil {
344		return "", fmt.Errorf("failed to write file: %w", err)
345	}
346
347	recordFileWrite(filePath)
348	recordFileRead(filePath)
349
350	return "Content replaced in file: " + filePath, nil
351}
352
353func GenerateDiff(oldContent, newContent string) string {
354	dmp := diffmatchpatch.New()
355	fileAdmp, fileBdmp, dmpStrings := dmp.DiffLinesToChars(oldContent, newContent)
356	diffs := dmp.DiffMain(fileAdmp, fileBdmp, false)
357	diffs = dmp.DiffCharsToLines(diffs, dmpStrings)
358	diffs = dmp.DiffCleanupSemantic(diffs)
359	buff := strings.Builder{}
360
361	buff.WriteString("Changes:\n")
362
363	for _, diff := range diffs {
364		text := diff.Text
365
366		switch diff.Type {
367		case diffmatchpatch.DiffInsert:
368			for line := range strings.SplitSeq(text, "\n") {
369				if line == "" {
370					continue
371				}
372				_, _ = buff.WriteString("+ " + line + "\n")
373			}
374		case diffmatchpatch.DiffDelete:
375			for line := range strings.SplitSeq(text, "\n") {
376				if line == "" {
377					continue
378				}
379				_, _ = buff.WriteString("- " + line + "\n")
380			}
381		case diffmatchpatch.DiffEqual:
382			lines := strings.Split(text, "\n")
383			if len(lines) > 3 {
384				if lines[0] != "" {
385					_, _ = buff.WriteString("  " + lines[0] + "\n")
386				}
387				_, _ = buff.WriteString("  ...\n")
388				if lines[len(lines)-1] != "" {
389					_, _ = buff.WriteString("  " + lines[len(lines)-1] + "\n")
390				}
391			} else {
392				for _, line := range lines {
393					if line == "" {
394						continue
395					}
396					_, _ = buff.WriteString("  " + line + "\n")
397				}
398			}
399		}
400	}
401	return buff.String()
402}