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"
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)."`
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"`
34type MultiEditPermissionsParams struct {
35 FilePath string `json:"file_path"`
36 OldContent string `json:"old_content,omitempty"`
37 NewContent string `json:"new_content,omitempty"`
40type FailedEdit struct {
41 Index int `json:"index"`
42 Error string `json:"error"`
43 Edit MultiEditOperation `json:"edit"`
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"`
55const MultiEditToolName = "multiedit"
57//go:embed multiedit.md
58var multieditDescription []byte
60func NewMultiEditTool(lspClients *csync.Map[string, *lsp.Client], permissions permission.Service, files history.Service, workingDir string) fantasy.AgentTool {
61 return fantasy.NewAgentTool(
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
69 if len(params.Edits) == 0 {
70 return fantasy.NewTextErrorResponse("at least one edit operation is required"), nil
73 params.FilePath = filepathext.SmartJoin(workingDir, params.FilePath)
75 // Validate all edits before applying any
76 if err := validateEdits(params.Edits); err != nil {
77 return fantasy.NewTextErrorResponse(err.Error()), nil
80 var response fantasy.ToolResponse
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)
88 response, err = processMultiEditExistingFile(editCtx, params, call)
99 // Notify LSP clients about the change
100 notifyLSPs(ctx, lspClients, params.FilePath)
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
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)
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
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)
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)
140 // Start with the content from the first edit
141 currentContent := firstEdit.NewString
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)
149 failedEdits = append(failedEdits, FailedEdit{
156 currentContent = newContent
159 // Get session and message IDs
160 sessionID := GetSessionFromContext(edit.ctx)
162 return fantasy.ToolResponse{}, fmt.Errorf("session ID is required for creating a new file")
166 _, additions, removals := diff.GenerateDiff("", currentContent, strings.TrimPrefix(params.FilePath, edit.workingDir))
168 editsApplied := len(params.Edits) - len(failedEdits)
169 var description string
170 if len(failedEdits) > 0 {
171 description = fmt.Sprintf("Create file %s with %d of %d edits (%d failed)", params.FilePath, editsApplied, len(params.Edits), len(failedEdits))
173 description = fmt.Sprintf("Create file %s with %d edits", params.FilePath, editsApplied)
175 p := edit.permissions.Request(permission.CreatePermissionRequest{
176 SessionID: sessionID,
177 Path: fsext.PathOrPrefix(params.FilePath, edit.workingDir),
179 ToolName: MultiEditToolName,
181 Description: description,
182 Params: MultiEditPermissionsParams{
183 FilePath: params.FilePath,
185 NewContent: currentContent,
189 return fantasy.ToolResponse{}, permission.ErrorPermissionDenied
193 err := os.WriteFile(params.FilePath, []byte(currentContent), 0o644)
195 return fantasy.ToolResponse{}, fmt.Errorf("failed to write file: %w", err)
198 // Update file history
199 _, err = edit.files.Create(edit.ctx, sessionID, params.FilePath, "")
201 return fantasy.ToolResponse{}, fmt.Errorf("error creating file history: %w", err)
204 _, err = edit.files.CreateVersion(edit.ctx, sessionID, params.FilePath, currentContent)
206 slog.Error("Error creating file history version", "error", err)
209 recordFileWrite(params.FilePath)
210 recordFileRead(params.FilePath)
213 if len(failedEdits) > 0 {
214 message = fmt.Sprintf("File created with %d of %d edits: %s (%d edit(s) failed)", editsApplied, len(params.Edits), params.FilePath, len(failedEdits))
216 message = fmt.Sprintf("File created with %d edits: %s", len(params.Edits), params.FilePath)
219 return fantasy.WithResponseMetadata(
220 fantasy.NewTextResponse(message),
221 MultiEditResponseMetadata{
223 NewContent: currentContent,
224 Additions: additions,
226 EditsApplied: editsApplied,
227 EditsFailed: failedEdits,
232func processMultiEditExistingFile(edit editContext, params MultiEditParams, call fantasy.ToolCall) (fantasy.ToolResponse, error) {
233 // Validate file exists and is readable
234 fileInfo, err := os.Stat(params.FilePath)
236 if os.IsNotExist(err) {
237 return fantasy.NewTextErrorResponse(fmt.Sprintf("file not found: %s", params.FilePath)), nil
239 return fantasy.ToolResponse{}, fmt.Errorf("failed to access file: %w", err)
242 if fileInfo.IsDir() {
243 return fantasy.NewTextErrorResponse(fmt.Sprintf("path is a directory, not a file: %s", params.FilePath)), nil
246 // Check if file was read before editing
247 if getLastReadTime(params.FilePath).IsZero() {
248 return fantasy.NewTextErrorResponse("you must read the file before editing it. Use the View tool first"), nil
251 // Check if file was modified since last read
252 modTime := fileInfo.ModTime()
253 lastRead := getLastReadTime(params.FilePath)
254 if modTime.After(lastRead) {
255 return fantasy.NewTextErrorResponse(
256 fmt.Sprintf("file %s has been modified since it was last read (mod time: %s, last read: %s)",
257 params.FilePath, modTime.Format(time.RFC3339), lastRead.Format(time.RFC3339),
261 // Read current file content
262 content, err := os.ReadFile(params.FilePath)
264 return fantasy.ToolResponse{}, fmt.Errorf("failed to read file: %w", err)
267 oldContent, isCrlf := fsext.ToUnixLineEndings(string(content))
268 currentContent := oldContent
270 // Apply all edits sequentially, tracking failures
271 var failedEdits []FailedEdit
272 for i, edit := range params.Edits {
273 newContent, err := applyEditToContent(currentContent, edit)
275 failedEdits = append(failedEdits, FailedEdit{
282 currentContent = newContent
285 // Check if content actually changed
286 if oldContent == currentContent {
287 // If we have failed edits, report them
288 if len(failedEdits) > 0 {
289 return fantasy.WithResponseMetadata(
290 fantasy.NewTextErrorResponse(fmt.Sprintf("no changes made - all %d edit(s) failed", len(failedEdits))),
291 MultiEditResponseMetadata{
293 EditsFailed: failedEdits,
297 return fantasy.NewTextErrorResponse("no changes made - all edits resulted in identical content"), nil
300 // Get session and message IDs
301 sessionID := GetSessionFromContext(edit.ctx)
303 return fantasy.ToolResponse{}, fmt.Errorf("session ID is required for editing file")
306 // Generate diff and check permissions
307 _, additions, removals := diff.GenerateDiff(oldContent, currentContent, strings.TrimPrefix(params.FilePath, edit.workingDir))
309 editsApplied := len(params.Edits) - len(failedEdits)
310 var description string
311 if len(failedEdits) > 0 {
312 description = fmt.Sprintf("Apply %d of %d edits to file %s (%d failed)", editsApplied, len(params.Edits), params.FilePath, len(failedEdits))
314 description = fmt.Sprintf("Apply %d edits to file %s", editsApplied, params.FilePath)
316 p := edit.permissions.Request(permission.CreatePermissionRequest{
317 SessionID: sessionID,
318 Path: fsext.PathOrPrefix(params.FilePath, edit.workingDir),
320 ToolName: MultiEditToolName,
322 Description: description,
323 Params: MultiEditPermissionsParams{
324 FilePath: params.FilePath,
325 OldContent: oldContent,
326 NewContent: currentContent,
330 return fantasy.ToolResponse{}, permission.ErrorPermissionDenied
334 currentContent, _ = fsext.ToWindowsLineEndings(currentContent)
337 // Write the updated content
338 err = os.WriteFile(params.FilePath, []byte(currentContent), 0o644)
340 return fantasy.ToolResponse{}, fmt.Errorf("failed to write file: %w", err)
343 // Update file history
344 file, err := edit.files.GetByPathAndSession(edit.ctx, params.FilePath, sessionID)
346 _, err = edit.files.Create(edit.ctx, sessionID, params.FilePath, oldContent)
348 return fantasy.ToolResponse{}, fmt.Errorf("error creating file history: %w", err)
351 if file.Content != oldContent {
352 // User manually changed the content, store an intermediate version
353 _, err = edit.files.CreateVersion(edit.ctx, sessionID, params.FilePath, oldContent)
355 slog.Error("Error creating file history version", "error", err)
359 // Store the new version
360 _, err = edit.files.CreateVersion(edit.ctx, sessionID, params.FilePath, currentContent)
362 slog.Error("Error creating file history version", "error", err)
365 recordFileWrite(params.FilePath)
366 recordFileRead(params.FilePath)
369 if len(failedEdits) > 0 {
370 message = fmt.Sprintf("Applied %d of %d edits to file: %s (%d edit(s) failed)", editsApplied, len(params.Edits), params.FilePath, len(failedEdits))
372 message = fmt.Sprintf("Applied %d edits to file: %s", len(params.Edits), params.FilePath)
375 return fantasy.WithResponseMetadata(
376 fantasy.NewTextResponse(message),
377 MultiEditResponseMetadata{
378 OldContent: oldContent,
379 NewContent: currentContent,
380 Additions: additions,
382 EditsApplied: editsApplied,
383 EditsFailed: failedEdits,
388func applyEditToContent(content string, edit MultiEditOperation) (string, error) {
389 if edit.OldString == "" && edit.NewString == "" {
393 if edit.OldString == "" {
394 return "", fmt.Errorf("old_string cannot be empty for content replacement")
397 var newContent string
398 var replacementCount int
401 newContent = strings.ReplaceAll(content, edit.OldString, edit.NewString)
402 replacementCount = strings.Count(content, edit.OldString)
403 if replacementCount == 0 {
404 return "", fmt.Errorf("old_string not found in content. Make sure it matches exactly, including whitespace and line breaks")
407 index := strings.Index(content, edit.OldString)
409 return "", fmt.Errorf("old_string not found in content. Make sure it matches exactly, including whitespace and line breaks")
412 lastIndex := strings.LastIndex(content, edit.OldString)
413 if index != lastIndex {
414 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")
417 newContent = content[:index] + edit.NewString + content[index+len(edit.OldString):]
421 return newContent, nil
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list