@@ -44,6 +44,11 @@ type EditResponseMetadata struct {
45const EditToolName = "edit"
45const EditToolName = "edit"
48+ oldStringNotFoundErr = fantasy.NewTextErrorResponse("old_string not found in file. Make sure it matches exactly, including whitespace and line breaks.")
49+ oldStringMultipleMatchesErr = fantasy.NewTextErrorResponse("old_string appears multiple times in the file. Please provide more context to ensure a unique match, or set replace_all to true")
47//go:embed edit.md
52//go:embed edit.md
48var editDescription []byte
53var editDescription []byte
@@ -217,12 +222,12 @@ func deleteContent(edit editContext, filePath, oldString string, replaceAll bool
217 newContent = strings.ReplaceAll(oldContent, oldString, "")
222 newContent = strings.ReplaceAll(oldContent, oldString, "")
218 deletionCount = strings.Count(oldContent, oldString)
223 deletionCount = strings.Count(oldContent, oldString)
219 if deletionCount == 0 {
224 if deletionCount == 0 {
220− return fantasy.NewTextErrorResponse("old_string not found in file. Make sure it matches exactly, including whitespace and line breaks"), nil
225+ return oldStringNotFoundErr, nil
223 index := strings.Index(oldContent, oldString)
228 index := strings.Index(oldContent, oldString)
224 if index == -1 {
229 if index == -1 {
225− return fantasy.NewTextErrorResponse("old_string not found in file. Make sure it matches exactly, including whitespace and line breaks"), nil
230+ return oldStringNotFoundErr, nil
228 lastIndex := strings.LastIndex(oldContent, oldString)
233 lastIndex := strings.LastIndex(oldContent, oldString)
@@ -287,7 +292,7 @@ func deleteContent(edit editContext, filePath, oldString string, replaceAll bool
289 if file.Content != oldContent {
294 if file.Content != oldContent {
290− // User Manually changed the content store an intermediate version
295+ // User manually changed the content; store an intermediate version
291 _, err = edit.files.CreateVersion(edit.ctx, sessionID, filePath, oldContent)
296 _, err = edit.files.CreateVersion(edit.ctx, sessionID, filePath, oldContent)
292 if err != nil {
297 if err != nil {
293 slog.Error("Error creating file history version", "error", err)
298 slog.Error("Error creating file history version", "error", err)
@@ -353,17 +358,17 @@ func replaceContent(edit editContext, filePath, oldString, newString string, rep
353 newContent = strings.ReplaceAll(oldContent, oldString, newString)
358 newContent = strings.ReplaceAll(oldContent, oldString, newString)
354 replacementCount = strings.Count(oldContent, oldString)
359 replacementCount = strings.Count(oldContent, oldString)
355 if replacementCount == 0 {
360 if replacementCount == 0 {
356− return fantasy.NewTextErrorResponse("old_string not found in file. Make sure it matches exactly, including whitespace and line breaks"), nil
361+ return oldStringNotFoundErr, nil
359 index := strings.Index(oldContent, oldString)
364 index := strings.Index(oldContent, oldString)
360 if index == -1 {
365 if index == -1 {
361− return fantasy.NewTextErrorResponse("old_string not found in file. Make sure it matches exactly, including whitespace and line breaks"), nil
366+ return oldStringNotFoundErr, nil
364 lastIndex := strings.LastIndex(oldContent, oldString)
369 lastIndex := strings.LastIndex(oldContent, oldString)
365 if index != lastIndex {
370 if index != lastIndex {
366− return fantasy.NewTextErrorResponse("old_string appears multiple times in the file. Please provide more context to ensure a unique match, or set replace_all to true"), nil
371+ return oldStringMultipleMatchesErr, nil
369 newContent = oldContent[:index] + newString + oldContent[index+len(oldString):]
374 newContent = oldContent[:index] + newString + oldContent[index+len(oldString):]
@@ -425,7 +430,7 @@ func replaceContent(edit editContext, filePath, oldString, newString string, rep
427 if file.Content != oldContent {
432 if file.Content != oldContent {
428− // User Manually changed the content store an intermediate version
433+ // User manually changed the content; store an intermediate version
429 _, err = edit.files.CreateVersion(edit.ctx, sessionID, filePath, oldContent)
434 _, err = edit.files.CreateVersion(edit.ctx, sessionID, filePath, oldContent)
430 if err != nil {
435 if err != nil {
431 slog.Debug("Error creating file history version", "error", err)
436 slog.Debug("Error creating file history version", "error", err)