multiedit.go

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