1package tools
2
3import (
4 "context"
5 _ "embed"
6 "fmt"
7 "log/slog"
8 "os"
9 "path/filepath"
10 "strings"
11 "time"
12
13 "charm.land/fantasy"
14 "github.com/charmbracelet/crush/internal/csync"
15 "github.com/charmbracelet/crush/internal/diff"
16 "github.com/charmbracelet/crush/internal/filepathext"
17 "github.com/charmbracelet/crush/internal/fsext"
18 "github.com/charmbracelet/crush/internal/history"
19 "github.com/charmbracelet/crush/internal/lsp"
20 "github.com/charmbracelet/crush/internal/permission"
21)
22
23type MultiEditOperation struct {
24 OldString string `json:"old_string" description:"The text to replace"`
25 NewString string `json:"new_string" description:"The text to replace it with"`
26 ReplaceAll bool `json:"replace_all,omitempty" description:"Replace all occurrences of old_string (default false)."`
27}
28
29type MultiEditParams struct {
30 FilePath string `json:"file_path" description:"The absolute path to the file to modify"`
31 Edits []MultiEditOperation `json:"edits" description:"Array of edit operations to perform sequentially on the file"`
32}
33
34type MultiEditPermissionsParams struct {
35 FilePath string `json:"file_path"`
36 OldContent string `json:"old_content,omitempty"`
37 NewContent string `json:"new_content,omitempty"`
38}
39
40type FailedEdit struct {
41 Index int `json:"index"`
42 Error string `json:"error"`
43 Edit MultiEditOperation `json:"edit"`
44}
45
46type MultiEditResponseMetadata struct {
47 Additions int `json:"additions"`
48 Removals int `json:"removals"`
49 OldContent string `json:"old_content,omitempty"`
50 NewContent string `json:"new_content,omitempty"`
51 EditsApplied int `json:"edits_applied"`
52 EditsFailed []FailedEdit `json:"edits_failed,omitempty"`
53}
54
55const MultiEditToolName = "multiedit"
56
57//go:embed multiedit.md
58var multieditDescription []byte
59
60func NewMultiEditTool(lspClients *csync.Map[string, *lsp.Client], permissions permission.Service, files history.Service, workingDir string) fantasy.AgentTool {
61 return fantasy.NewAgentTool(
62 MultiEditToolName,
63 string(multieditDescription),
64 func(ctx context.Context, params MultiEditParams, call fantasy.ToolCall) (fantasy.ToolResponse, error) {
65 if params.FilePath == "" {
66 return fantasy.NewTextErrorResponse("file_path is required"), nil
67 }
68
69 if len(params.Edits) == 0 {
70 return fantasy.NewTextErrorResponse("at least one edit operation is required"), nil
71 }
72
73 params.FilePath = filepathext.SmartJoin(workingDir, params.FilePath)
74
75 // Validate all edits before applying any
76 if err := validateEdits(params.Edits); err != nil {
77 return fantasy.NewTextErrorResponse(err.Error()), nil
78 }
79
80 var response fantasy.ToolResponse
81 var err error
82
83 editCtx := editContext{ctx, permissions, files, workingDir}
84 // Handle file creation case (first edit has empty old_string)
85 if len(params.Edits) > 0 && params.Edits[0].OldString == "" {
86 response, err = processMultiEditWithCreation(editCtx, params, call)
87 } else {
88 response, err = processMultiEditExistingFile(editCtx, params, call)
89 }
90
91 if err != nil {
92 return response, err
93 }
94
95 if response.IsError {
96 return response, nil
97 }
98
99 // Notify LSP clients about the change
100 notifyLSPs(ctx, lspClients, params.FilePath)
101
102 // Wait for LSP diagnostics and add them to the response
103 text := fmt.Sprintf("<result>\n%s\n</result>\n", response.Content)
104 text += getDiagnostics(params.FilePath, lspClients)
105 response.Content = text
106 return response, nil
107 })
108}
109
110func validateEdits(edits []MultiEditOperation) error {
111 for i, edit := range edits {
112 // Only the first edit can have empty old_string (for file creation)
113 if i > 0 && edit.OldString == "" {
114 return fmt.Errorf("edit %d: only the first edit can have empty old_string (for file creation)", i+1)
115 }
116 }
117 return nil
118}
119
120func processMultiEditWithCreation(edit editContext, params MultiEditParams, call fantasy.ToolCall) (fantasy.ToolResponse, error) {
121 // First edit creates the file
122 firstEdit := params.Edits[0]
123 if firstEdit.OldString != "" {
124 return fantasy.NewTextErrorResponse("first edit must have empty old_string for file creation"), nil
125 }
126
127 // Check if file already exists
128 if _, err := os.Stat(params.FilePath); err == nil {
129 return fantasy.NewTextErrorResponse(fmt.Sprintf("file already exists: %s", params.FilePath)), nil
130 } else if !os.IsNotExist(err) {
131 return fantasy.ToolResponse{}, fmt.Errorf("failed to access file: %w", err)
132 }
133
134 // Create parent directories
135 dir := filepath.Dir(params.FilePath)
136 if err := os.MkdirAll(dir, 0o755); err != nil {
137 return fantasy.ToolResponse{}, fmt.Errorf("failed to create parent directories: %w", err)
138 }
139
140 // Start with the content from the first edit
141 currentContent := firstEdit.NewString
142
143 // Apply remaining edits to the content, tracking failures
144 var failedEdits []FailedEdit
145 for i := 1; i < len(params.Edits); i++ {
146 edit := params.Edits[i]
147 newContent, err := applyEditToContent(currentContent, edit)
148 if err != nil {
149 failedEdits = append(failedEdits, FailedEdit{
150 Index: i + 1,
151 Error: err.Error(),
152 Edit: edit,
153 })
154 continue
155 }
156 currentContent = newContent
157 }
158
159 // Get session and message IDs
160 sessionID := GetSessionFromContext(edit.ctx)
161 if sessionID == "" {
162 return fantasy.ToolResponse{}, fmt.Errorf("session ID is required for creating a new file")
163 }
164
165 // Check permissions
166 _, additions, removals := diff.GenerateDiff("", currentContent, strings.TrimPrefix(params.FilePath, edit.workingDir))
167
168 p := edit.permissions.Request(permission.CreatePermissionRequest{
169 SessionID: sessionID,
170 Path: fsext.PathOrPrefix(params.FilePath, edit.workingDir),
171 ToolCallID: call.ID,
172 ToolName: MultiEditToolName,
173 Action: "write",
174 Description: fmt.Sprintf("Create file %s with %d edits", params.FilePath, len(params.Edits)),
175 Params: MultiEditPermissionsParams{
176 FilePath: params.FilePath,
177 OldContent: "",
178 NewContent: currentContent,
179 },
180 })
181 if !p {
182 return fantasy.ToolResponse{}, permission.ErrorPermissionDenied
183 }
184
185 // Write the file
186 err := os.WriteFile(params.FilePath, []byte(currentContent), 0o644)
187 if err != nil {
188 return fantasy.ToolResponse{}, fmt.Errorf("failed to write file: %w", err)
189 }
190
191 // Update file history
192 _, err = edit.files.Create(edit.ctx, sessionID, params.FilePath, "")
193 if err != nil {
194 return fantasy.ToolResponse{}, fmt.Errorf("error creating file history: %w", err)
195 }
196
197 _, err = edit.files.CreateVersion(edit.ctx, sessionID, params.FilePath, currentContent)
198 if err != nil {
199 slog.Debug("Error creating file history version", "error", err)
200 }
201
202 recordFileWrite(params.FilePath)
203 recordFileRead(params.FilePath)
204
205 editsApplied := len(params.Edits) - len(failedEdits)
206 var message string
207 if len(failedEdits) > 0 {
208 message = fmt.Sprintf("File created with %d of %d edits: %s (%d edit(s) failed)", editsApplied, len(params.Edits), params.FilePath, len(failedEdits))
209 } else {
210 message = fmt.Sprintf("File created with %d edits: %s", len(params.Edits), params.FilePath)
211 }
212
213 return fantasy.WithResponseMetadata(
214 fantasy.NewTextResponse(message),
215 MultiEditResponseMetadata{
216 OldContent: "",
217 NewContent: currentContent,
218 Additions: additions,
219 Removals: removals,
220 EditsApplied: editsApplied,
221 EditsFailed: failedEdits,
222 },
223 ), nil
224}
225
226func processMultiEditExistingFile(edit editContext, params MultiEditParams, call fantasy.ToolCall) (fantasy.ToolResponse, error) {
227 // Validate file exists and is readable
228 fileInfo, err := os.Stat(params.FilePath)
229 if err != nil {
230 if os.IsNotExist(err) {
231 return fantasy.NewTextErrorResponse(fmt.Sprintf("file not found: %s", params.FilePath)), nil
232 }
233 return fantasy.ToolResponse{}, fmt.Errorf("failed to access file: %w", err)
234 }
235
236 if fileInfo.IsDir() {
237 return fantasy.NewTextErrorResponse(fmt.Sprintf("path is a directory, not a file: %s", params.FilePath)), nil
238 }
239
240 // Check if file was read before editing
241 if getLastReadTime(params.FilePath).IsZero() {
242 return fantasy.NewTextErrorResponse("you must read the file before editing it. Use the View tool first"), nil
243 }
244
245 // Check if file was modified since last read
246 modTime := fileInfo.ModTime()
247 lastRead := getLastReadTime(params.FilePath)
248 if modTime.After(lastRead) {
249 return fantasy.NewTextErrorResponse(
250 fmt.Sprintf("file %s has been modified since it was last read (mod time: %s, last read: %s)",
251 params.FilePath, modTime.Format(time.RFC3339), lastRead.Format(time.RFC3339),
252 )), nil
253 }
254
255 // Read current file content
256 content, err := os.ReadFile(params.FilePath)
257 if err != nil {
258 return fantasy.ToolResponse{}, fmt.Errorf("failed to read file: %w", err)
259 }
260
261 oldContent, isCrlf := fsext.ToUnixLineEndings(string(content))
262 currentContent := oldContent
263
264 // Apply all edits sequentially, tracking failures
265 var failedEdits []FailedEdit
266 for i, edit := range params.Edits {
267 newContent, err := applyEditToContent(currentContent, edit)
268 if err != nil {
269 failedEdits = append(failedEdits, FailedEdit{
270 Index: i + 1,
271 Error: err.Error(),
272 Edit: edit,
273 })
274 continue
275 }
276 currentContent = newContent
277 }
278
279 // Check if content actually changed
280 if oldContent == currentContent {
281 // If we have failed edits, report them
282 if len(failedEdits) > 0 {
283 return fantasy.WithResponseMetadata(
284 fantasy.NewTextErrorResponse(fmt.Sprintf("no changes made - all %d edit(s) failed", len(failedEdits))),
285 MultiEditResponseMetadata{
286 EditsApplied: 0,
287 EditsFailed: failedEdits,
288 },
289 ), nil
290 }
291 return fantasy.NewTextErrorResponse("no changes made - all edits resulted in identical content"), nil
292 }
293
294 // Get session and message IDs
295 sessionID := GetSessionFromContext(edit.ctx)
296 if sessionID == "" {
297 return fantasy.ToolResponse{}, fmt.Errorf("session ID is required for editing file")
298 }
299
300 // Generate diff and check permissions
301 _, additions, removals := diff.GenerateDiff(oldContent, currentContent, strings.TrimPrefix(params.FilePath, edit.workingDir))
302 p := edit.permissions.Request(permission.CreatePermissionRequest{
303 SessionID: sessionID,
304 Path: fsext.PathOrPrefix(params.FilePath, edit.workingDir),
305 ToolCallID: call.ID,
306 ToolName: MultiEditToolName,
307 Action: "write",
308 Description: fmt.Sprintf("Apply %d edits to file %s", len(params.Edits), params.FilePath),
309 Params: MultiEditPermissionsParams{
310 FilePath: params.FilePath,
311 OldContent: oldContent,
312 NewContent: currentContent,
313 },
314 })
315 if !p {
316 return fantasy.ToolResponse{}, permission.ErrorPermissionDenied
317 }
318
319 if isCrlf {
320 currentContent, _ = fsext.ToWindowsLineEndings(currentContent)
321 }
322
323 // Write the updated content
324 err = os.WriteFile(params.FilePath, []byte(currentContent), 0o644)
325 if err != nil {
326 return fantasy.ToolResponse{}, fmt.Errorf("failed to write file: %w", err)
327 }
328
329 // Update file history
330 file, err := edit.files.GetByPathAndSession(edit.ctx, params.FilePath, sessionID)
331 if err != nil {
332 _, err = edit.files.Create(edit.ctx, sessionID, params.FilePath, oldContent)
333 if err != nil {
334 return fantasy.ToolResponse{}, fmt.Errorf("error creating file history: %w", err)
335 }
336 }
337 if file.Content != oldContent {
338 // User manually changed the content, store an intermediate version
339 _, err = edit.files.CreateVersion(edit.ctx, sessionID, params.FilePath, oldContent)
340 if err != nil {
341 slog.Debug("Error creating file history version", "error", err)
342 }
343 }
344
345 // Store the new version
346 _, err = edit.files.CreateVersion(edit.ctx, sessionID, params.FilePath, currentContent)
347 if err != nil {
348 slog.Debug("Error creating file history version", "error", err)
349 }
350
351 recordFileWrite(params.FilePath)
352 recordFileRead(params.FilePath)
353
354 editsApplied := len(params.Edits) - len(failedEdits)
355 var message string
356 if len(failedEdits) > 0 {
357 message = fmt.Sprintf("Applied %d of %d edits to file: %s (%d edit(s) failed)", editsApplied, len(params.Edits), params.FilePath, len(failedEdits))
358 } else {
359 message = fmt.Sprintf("Applied %d edits to file: %s", len(params.Edits), params.FilePath)
360 }
361
362 return fantasy.WithResponseMetadata(
363 fantasy.NewTextResponse(message),
364 MultiEditResponseMetadata{
365 OldContent: oldContent,
366 NewContent: currentContent,
367 Additions: additions,
368 Removals: removals,
369 EditsApplied: editsApplied,
370 EditsFailed: failedEdits,
371 },
372 ), nil
373}
374
375func applyEditToContent(content string, edit MultiEditOperation) (string, error) {
376 if edit.OldString == "" && edit.NewString == "" {
377 return content, nil
378 }
379
380 if edit.OldString == "" {
381 return "", fmt.Errorf("old_string cannot be empty for content replacement")
382 }
383
384 var newContent string
385 var replacementCount int
386
387 if edit.ReplaceAll {
388 newContent = strings.ReplaceAll(content, edit.OldString, edit.NewString)
389 replacementCount = strings.Count(content, edit.OldString)
390 if replacementCount == 0 {
391 return "", fmt.Errorf("old_string not found in content. Make sure it matches exactly, including whitespace and line breaks")
392 }
393 } else {
394 index := strings.Index(content, edit.OldString)
395 if index == -1 {
396 return "", fmt.Errorf("old_string not found in content. Make sure it matches exactly, including whitespace and line breaks")
397 }
398
399 lastIndex := strings.LastIndex(content, edit.OldString)
400 if index != lastIndex {
401 return "", fmt.Errorf("old_string appears multiple times in the content. Please provide more context to ensure a unique match, or set replace_all to true")
402 }
403
404 newContent = content[:index] + edit.NewString + content[index+len(edit.OldString):]
405 replacementCount = 1
406 }
407
408 return newContent, nil
409}