on main help sign in
chore(tools/edit): various fixes (#1921)
9e018b9Christian Rocha
internal/agent/tools/edit.go+3−12
@@ -216,12 +216,10 @@ func deleteContent(edit editContext, filePath, oldString string, replaceAll bool
216 oldContent, isCrlf := fsext.ToUnixLineEndings(string(content))
216 oldContent, isCrlf := fsext.ToUnixLineEndings(string(content))
217
217
218 var newContent string
218 var newContent string
219− var deletionCount int
220
219
221 if replaceAll {
220 if replaceAll {
222 newContent = strings.ReplaceAll(oldContent, oldString, "")
221 newContent = strings.ReplaceAll(oldContent, oldString, "")
223− deletionCount = strings.Count(oldContent, oldString)
222+ if newContent == oldContent {
224− if deletionCount == 0 {
225 return oldStringNotFoundErr, nil
223 return oldStringNotFoundErr, nil
226 }
224 }
227 } else {
225 } else {
@@ -236,13 +234,12 @@ func deleteContent(edit editContext, filePath, oldString string, replaceAll bool
236 }
234 }
237
235
238 newContent = oldContent[:index] + oldContent[index+len(oldString):]
236 newContent = oldContent[:index] + oldContent[index+len(oldString):]
239− deletionCount = 1
240 }
237 }
241
238
242 sessionID := GetSessionFromContext(edit.ctx)
239 sessionID := GetSessionFromContext(edit.ctx)
243
240
244 if sessionID == "" {
241 if sessionID == "" {
245− return fantasy.ToolResponse{}, fmt.Errorf("session ID is required for creating a new file")
242+ return fantasy.ToolResponse{}, fmt.Errorf("session ID is required for deleting content")
246 }
243 }
247
244
248 _, additions, removals := diff.GenerateDiff(
245 _, additions, removals := diff.GenerateDiff(
@@ -299,7 +296,7 @@ func deleteContent(edit editContext, filePath, oldString string, replaceAll bool
299 }
296 }
300 }
297 }
301 // Store the new version
298 // Store the new version
302− _, err = edit.files.CreateVersion(edit.ctx, sessionID, filePath, "")
299+ _, err = edit.files.CreateVersion(edit.ctx, sessionID, filePath, newContent)
303 if err != nil {
300 if err != nil {
304 slog.Error("Error creating file history version", "error", err)
301 slog.Error("Error creating file history version", "error", err)
305 }
302 }
@@ -352,14 +349,9 @@ func replaceContent(edit editContext, filePath, oldString, newString string, rep
352 oldContent, isCrlf := fsext.ToUnixLineEndings(string(content))
349 oldContent, isCrlf := fsext.ToUnixLineEndings(string(content))
353
350
354 var newContent string
351 var newContent string
355− var replacementCount int
356
352
357 if replaceAll {
353 if replaceAll {
358 newContent = strings.ReplaceAll(oldContent, oldString, newString)
354 newContent = strings.ReplaceAll(oldContent, oldString, newString)
359− replacementCount = strings.Count(oldContent, oldString)
360− if replacementCount == 0 {
361− return oldStringNotFoundErr, nil
362− }
363 } else {
355 } else {
364 index := strings.Index(oldContent, oldString)
356 index := strings.Index(oldContent, oldString)
365 if index == -1 {
357 if index == -1 {
@@ -372,7 +364,6 @@ func replaceContent(edit editContext, filePath, oldString, newString string, rep
372 }
364 }
373
365
374 newContent = oldContent[:index] + newString + oldContent[index+len(oldString):]
366 newContent = oldContent[:index] + newString + oldContent[index+len(oldString):]
375− replacementCount = 1
376 }
367 }
377
368
378 if oldContent == newContent {
369 if oldContent == newContent {
internal/history/file.go+7
@@ -25,10 +25,14 @@ type File struct {
25 UpdatedAt int64
25 UpdatedAt int64
26}
26}
27
27
28+// Service manages file versions and history for sessions.
28type Service interface {
29type Service interface {
29 pubsub.Subscriber[File]
30 pubsub.Subscriber[File]
30 Create(ctx context.Context, sessionID, path, content string) (File, error)
31 Create(ctx context.Context, sessionID, path, content string) (File, error)
32+
33+ // CreateVersion creates a new version of a file.
31 CreateVersion(ctx context.Context, sessionID, path, content string) (File, error)
34 CreateVersion(ctx context.Context, sessionID, path, content string) (File, error)
35+
32 Get(ctx context.Context, id string) (File, error)
36 Get(ctx context.Context, id string) (File, error)
33 GetByPathAndSession(ctx context.Context, path, sessionID string) (File, error)
37 GetByPathAndSession(ctx context.Context, path, sessionID string) (File, error)
34 ListBySession(ctx context.Context, sessionID string) ([]File, error)
38 ListBySession(ctx context.Context, sessionID string) ([]File, error)
@@ -55,6 +59,9 @@ func (s *service) Create(ctx context.Context, sessionID, path, content string) (
55 return s.createWithVersion(ctx, sessionID, path, content, InitialVersion)
59 return s.createWithVersion(ctx, sessionID, path, content, InitialVersion)
56}
60}
57
61
62+// CreateVersion creates a new version of a file with auto-incremented version
63+// number. If no previous versions exist for the path, it creates the initial
64+// version. The provided content is stored as the new version.
58func (s *service) CreateVersion(ctx context.Context, sessionID, path, content string) (File, error) {
65func (s *service) CreateVersion(ctx context.Context, sessionID, path, content string) (File, error) {
59 // Get the latest version for this path
66 // Get the latest version for this path
60 files, err := s.q.ListFilesByPath(ctx, path)
67 files, err := s.q.ListFilesByPath(ctx, path)