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 granted, err := CheckHookPermission(edit.ctx, edit.permissions, 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 !granted {
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 }), nil
223}
224
225func processMultiEditExistingFile(edit editContext, params MultiEditParams, call fantasy.ToolCall) (fantasy.ToolResponse, error) {
226 // Validate file exists and is readable
227 fileInfo, err := os.Stat(params.FilePath)
228 if err != nil {
229 if os.IsNotExist(err) {
230 return fantasy.NewTextErrorResponse(fmt.Sprintf("file not found: %s", params.FilePath)), nil
231 }
232 return fantasy.ToolResponse{}, fmt.Errorf("failed to access file: %w", err)
233 }
234
235 if fileInfo.IsDir() {
236 return fantasy.NewTextErrorResponse(fmt.Sprintf("path is a directory, not a file: %s", params.FilePath)), nil
237 }
238
239 // Check if file was read before editing
240 if getLastReadTime(params.FilePath).IsZero() {
241 return fantasy.NewTextErrorResponse("you must read the file before editing it. Use the View tool first"), nil
242 }
243
244 // Check if file was modified since last read
245 modTime := fileInfo.ModTime()
246 lastRead := getLastReadTime(params.FilePath)
247 if modTime.After(lastRead) {
248 return fantasy.NewTextErrorResponse(
249 fmt.Sprintf("file %s has been modified since it was last read (mod time: %s, last read: %s)",
250 params.FilePath, modTime.Format(time.RFC3339), lastRead.Format(time.RFC3339),
251 )), nil
252 }
253
254 // Read current file content
255 content, err := os.ReadFile(params.FilePath)
256 if err != nil {
257 return fantasy.ToolResponse{}, fmt.Errorf("failed to read file: %w", err)
258 }
259
260 oldContent, isCrlf := fsext.ToUnixLineEndings(string(content))
261 currentContent := oldContent
262
263 // Apply all edits sequentially, tracking failures
264 var failedEdits []FailedEdit
265 for i, edit := range params.Edits {
266 newContent, err := applyEditToContent(currentContent, edit)
267 if err != nil {
268 failedEdits = append(failedEdits, FailedEdit{
269 Index: i + 1,
270 Error: err.Error(),
271 Edit: edit,
272 })
273 continue
274 }
275 currentContent = newContent
276 }
277
278 // Check if content actually changed
279 if oldContent == currentContent {
280 // If we have failed edits, report them
281 if len(failedEdits) > 0 {
282 return fantasy.WithResponseMetadata(
283 fantasy.NewTextErrorResponse(fmt.Sprintf("no changes made - all %d edit(s) failed", len(failedEdits))),
284 MultiEditResponseMetadata{
285 EditsApplied: 0,
286 EditsFailed: failedEdits,
287 },
288 ), nil
289 }
290 return fantasy.NewTextErrorResponse("no changes made - all edits resulted in identical content"), nil
291 }
292
293 // Get session and message IDs
294 sessionID := GetSessionFromContext(edit.ctx)
295 if sessionID == "" {
296 return fantasy.ToolResponse{}, fmt.Errorf("session ID is required for editing file")
297 }
298
299 // Generate diff and check permissions
300 _, additions, removals := diff.GenerateDiff(oldContent, currentContent, strings.TrimPrefix(params.FilePath, edit.workingDir))
301 granted, err := CheckHookPermission(edit.ctx, edit.permissions, permission.CreatePermissionRequest{
302 SessionID: sessionID,
303 Path: fsext.PathOrPrefix(params.FilePath, edit.workingDir),
304 ToolCallID: call.ID,
305 ToolName: MultiEditToolName,
306 Action: "write",
307 Description: fmt.Sprintf("Apply %d edits to file %s", len(params.Edits), params.FilePath),
308 Params: MultiEditPermissionsParams{
309 FilePath: params.FilePath,
310 OldContent: oldContent,
311 NewContent: currentContent,
312 },
313 })
314 if !granted {
315 return fantasy.ToolResponse{}, permission.ErrorPermissionDenied
316 }
317
318 if isCrlf {
319 currentContent, _ = fsext.ToWindowsLineEndings(currentContent)
320 }
321
322 // Write the updated content
323 err = os.WriteFile(params.FilePath, []byte(currentContent), 0o644)
324 if err != nil {
325 return fantasy.ToolResponse{}, fmt.Errorf("failed to write file: %w", err)
326 }
327
328 // Update file history
329 file, err := edit.files.GetByPathAndSession(edit.ctx, params.FilePath, sessionID)
330 if err != nil {
331 _, err = edit.files.Create(edit.ctx, sessionID, params.FilePath, oldContent)
332 if err != nil {
333 return fantasy.ToolResponse{}, fmt.Errorf("error creating file history: %w", err)
334 }
335 }
336 if file.Content != oldContent {
337 // User manually changed the content, store an intermediate version
338 _, err = edit.files.CreateVersion(edit.ctx, sessionID, params.FilePath, oldContent)
339 if err != nil {
340 slog.Debug("Error creating file history version", "error", err)
341 }
342 }
343
344 // Store the new version
345 _, err = edit.files.CreateVersion(edit.ctx, sessionID, params.FilePath, currentContent)
346 if err != nil {
347 slog.Debug("Error creating file history version", "error", err)
348 }
349
350 recordFileWrite(params.FilePath)
351 recordFileRead(params.FilePath)
352
353 editsApplied := len(params.Edits) - len(failedEdits)
354 var message string
355 if len(failedEdits) > 0 {
356 message = fmt.Sprintf("Applied %d of %d edits to file: %s (%d edit(s) failed)", editsApplied, len(params.Edits), params.FilePath, len(failedEdits))
357 } else {
358 message = fmt.Sprintf("Applied %d edits to file: %s", len(params.Edits), params.FilePath)
359 }
360
361 return fantasy.WithResponseMetadata(
362 fantasy.NewTextResponse(message),
363 MultiEditResponseMetadata{
364 OldContent: oldContent,
365 NewContent: currentContent,
366 Additions: additions,
367 Removals: removals,
368 EditsApplied: editsApplied,
369 EditsFailed: failedEdits,
370 }), nil
371}
372
373func applyEditToContent(content string, edit MultiEditOperation) (string, error) {
374 if edit.OldString == "" && edit.NewString == "" {
375 return content, nil
376 }
377
378 if edit.OldString == "" {
379 return "", fmt.Errorf("old_string cannot be empty for content replacement")
380 }
381
382 var newContent string
383 var replacementCount int
384
385 if edit.ReplaceAll {
386 newContent = strings.ReplaceAll(content, edit.OldString, edit.NewString)
387 replacementCount = strings.Count(content, edit.OldString)
388 if replacementCount == 0 {
389 return "", fmt.Errorf("old_string not found in content. Make sure it matches exactly, including whitespace and line breaks")
390 }
391 } else {
392 index := strings.Index(content, edit.OldString)
393 if index == -1 {
394 return "", fmt.Errorf("old_string not found in content. Make sure it matches exactly, including whitespace and line breaks")
395 }
396
397 lastIndex := strings.LastIndex(content, edit.OldString)
398 if index != lastIndex {
399 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")
400 }
401
402 newContent = content[:index] + edit.NewString + content[index+len(edit.OldString):]
403 replacementCount = 1
404 }
405
406 return newContent, nil
407}