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