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), ¶ms); 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
307 // Add a header to make the diff more readable
308 buff.WriteString("Changes:\n")
309
310 for _, diff := range diffs {
311 text := diff.Text
312
313 switch diff.Type {
314 case diffmatchpatch.DiffInsert:
315 for _, line := range strings.Split(text, "\n") {
316 if line == "" {
317 continue
318 }
319 _, _ = buff.WriteString("+ " + line + "\n")
320 }
321 case diffmatchpatch.DiffDelete:
322 for _, line := range strings.Split(text, "\n") {
323 if line == "" {
324 continue
325 }
326 _, _ = buff.WriteString("- " + line + "\n")
327 }
328 case diffmatchpatch.DiffEqual:
329 // Only show a small context for unchanged text
330 lines := strings.Split(text, "\n")
331 if len(lines) > 3 {
332 // Show only first and last line of context with a separator
333 if lines[0] != "" {
334 _, _ = buff.WriteString(" " + lines[0] + "\n")
335 }
336 _, _ = buff.WriteString(" ...\n")
337 if lines[len(lines)-1] != "" {
338 _, _ = buff.WriteString(" " + lines[len(lines)-1] + "\n")
339 }
340 } else {
341 // Show all lines for small contexts
342 for _, line := range lines {
343 if line == "" {
344 continue
345 }
346 _, _ = buff.WriteString(" " + line + "\n")
347 }
348 }
349 }
350 }
351 return buff.String()
352}
353
354func editDescription() string {
355 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.
356
357Before using this tool:
358
3591. Use the FileRead tool to understand the file's contents and context
360
3612. Verify the directory path is correct (only applicable when creating new files):
362 - Use the LS tool to verify the parent directory exists and is the correct location
363
364To make a file edit, provide the following:
3651. file_path: The absolute path to the file to modify (must be absolute, not relative)
3662. old_string: The text to replace (must be unique within the file, and must match the file contents exactly, including all whitespace and indentation)
3673. new_string: The edited text to replace the old_string
368
369Special cases:
370- To create a new file: provide file_path and new_string, leave old_string empty
371- To delete content: provide file_path and old_string, leave new_string empty
372
373The tool will replace ONE occurrence of old_string with new_string in the specified file.
374
375CRITICAL REQUIREMENTS FOR USING THIS TOOL:
376
3771. UNIQUENESS: The old_string MUST uniquely identify the specific instance you want to change. This means:
378 - Include AT LEAST 3-5 lines of context BEFORE the change point
379 - Include AT LEAST 3-5 lines of context AFTER the change point
380 - Include all whitespace, indentation, and surrounding code exactly as it appears in the file
381
3822. SINGLE INSTANCE: This tool can only change ONE instance at a time. If you need to change multiple instances:
383 - Make separate calls to this tool for each instance
384 - Each call must uniquely identify its specific instance using extensive context
385
3863. VERIFICATION: Before using this tool:
387 - Check how many instances of the target text exist in the file
388 - If multiple instances exist, gather enough context to uniquely identify each one
389 - Plan separate tool calls for each instance
390
391WARNING: If you do not follow these requirements:
392 - The tool will fail if old_string matches multiple locations
393 - The tool will fail if old_string doesn't match exactly (including whitespace)
394 - You may change the wrong instance if you don't include enough context
395
396When making edits:
397 - Ensure the edit results in idiomatic, correct code
398 - Do not leave the code in a broken state
399 - Always use absolute file paths (starting with /)
400
401Remember: 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.`
402}
403
404func NewEditTool(lspClients map[string]*lsp.Client) BaseTool {
405 return &editTool{
406 lspClients,
407 }
408}