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 diff := GenerateDiff(oldString, newContent)
264
265 p := permission.Default.Request(
266 permission.CreatePermissionRequest{
267 Path: filepath.Dir(filePath),
268 ToolName: EditToolName,
269 Action: "replace",
270 Description: fmt.Sprintf("Replace content in file %s", filePath),
271 Params: EditPermissionsParams{
272 FilePath: filePath,
273 OldString: oldString,
274 NewString: newString,
275 Diff: diff,
276 },
277 },
278 )
279 if !p {
280 return "", fmt.Errorf("permission denied")
281 }
282
283 err = os.WriteFile(filePath, []byte(newContent), 0o644)
284 if err != nil {
285 return "", fmt.Errorf("failed to write file: %w", err)
286 }
287
288 recordFileWrite(filePath)
289 recordFileRead(filePath)
290
291 return "Content replaced in file: " + filePath, nil
292}
293
294func GenerateDiff(oldContent, newContent string) string {
295 dmp := diffmatchpatch.New()
296 fileAdmp, fileBdmp, dmpStrings := dmp.DiffLinesToChars(oldContent, newContent)
297 diffs := dmp.DiffMain(fileAdmp, fileBdmp, false)
298 diffs = dmp.DiffCharsToLines(diffs, dmpStrings)
299 diffs = dmp.DiffCleanupSemantic(diffs)
300 buff := strings.Builder{}
301 for _, diff := range diffs {
302 text := diff.Text
303
304 switch diff.Type {
305 case diffmatchpatch.DiffInsert:
306 for line := range strings.SplitSeq(text, "\n") {
307 _, _ = buff.WriteString("+ " + line + "\n")
308 }
309 case diffmatchpatch.DiffDelete:
310 for line := range strings.SplitSeq(text, "\n") {
311 _, _ = buff.WriteString("- " + line + "\n")
312 }
313 case diffmatchpatch.DiffEqual:
314 if len(text) > 40 {
315 _, _ = buff.WriteString(" " + text[:20] + "..." + text[len(text)-20:] + "\n")
316 } else {
317 for line := range strings.SplitSeq(text, "\n") {
318 _, _ = buff.WriteString(" " + line + "\n")
319 }
320 }
321 }
322 }
323 return buff.String()
324}
325
326func editDescription() string {
327 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.
328
329Before using this tool:
330
3311. Use the FileRead tool to understand the file's contents and context
332
3332. Verify the directory path is correct (only applicable when creating new files):
334 - Use the LS tool to verify the parent directory exists and is the correct location
335
336To make a file edit, provide the following:
3371. file_path: The absolute path to the file to modify (must be absolute, not relative)
3382. old_string: The text to replace (must be unique within the file, and must match the file contents exactly, including all whitespace and indentation)
3393. new_string: The edited text to replace the old_string
340
341Special cases:
342- To create a new file: provide file_path and new_string, leave old_string empty
343- To delete content: provide file_path and old_string, leave new_string empty
344
345The tool will replace ONE occurrence of old_string with new_string in the specified file.
346
347CRITICAL REQUIREMENTS FOR USING THIS TOOL:
348
3491. UNIQUENESS: The old_string MUST uniquely identify the specific instance you want to change. This means:
350 - Include AT LEAST 3-5 lines of context BEFORE the change point
351 - Include AT LEAST 3-5 lines of context AFTER the change point
352 - Include all whitespace, indentation, and surrounding code exactly as it appears in the file
353
3542. SINGLE INSTANCE: This tool can only change ONE instance at a time. If you need to change multiple instances:
355 - Make separate calls to this tool for each instance
356 - Each call must uniquely identify its specific instance using extensive context
357
3583. VERIFICATION: Before using this tool:
359 - Check how many instances of the target text exist in the file
360 - If multiple instances exist, gather enough context to uniquely identify each one
361 - Plan separate tool calls for each instance
362
363WARNING: If you do not follow these requirements:
364 - The tool will fail if old_string matches multiple locations
365 - The tool will fail if old_string doesn't match exactly (including whitespace)
366 - You may change the wrong instance if you don't include enough context
367
368When making edits:
369 - Ensure the edit results in idiomatic, correct code
370 - Do not leave the code in a broken state
371 - Always use absolute file paths (starting with /)
372
373Remember: 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.`
374}
375
376func NewEditTool(lspClients map[string]*lsp.Client) BaseTool {
377 return &editTool{
378 lspClients,
379 }
380}