summaryrefslogtreecommitdiffhomepage
path: root/lib/dispatch/tool/files/edit_file.rb
blob: b1bd159d92da732f8742871e3cf38448ebde65ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# frozen_string_literal: true

module Dispatch
  module Tool
    module Files
      EDIT_FILE = Dispatch::Tools::Definition.new(
        name: "edit_file",
        description: "Apply text edits to an existing file. Each edit replaces old_text with new_text sequentially.",
        parameters: {
          type: "object",
          properties: {
            path: {
              type: "string",
              description: "Path to the file to edit, relative to the worktree root."
            },
            edits: {
              type: "array",
              description: "Array of edit operations to apply sequentially.",
              items: {
                type: "object",
                properties: {
                  old_text: {
                    type: "string",
                    description: "The exact text to find in the file."
                  },
                  new_text: {
                    type: "string",
                    description: "The text to replace old_text with."
                  }
                },
                required: %w[old_text new_text],
                additionalProperties: false
              }
            }
          },
          required: %w[path edits],
          additionalProperties: false
        }
      ) do |params, context|
        worktree_path = context[:worktree_path]
        path = params[:path]
        edits = params[:edits]

        resolved = begin
          Sandbox.resolve_path(path, worktree_path:)
        rescue SandboxError => e
          next Dispatch::Tools::Result.failure(error: e.message)
        end

        unless File.exist?(resolved)
          next Dispatch::Tools::Result.failure(error: "File not found: #{path}")
        end

        content = File.read(resolved)
        edits_applied = 0
        error_result = nil

        edits.each do |edit|
          old_text = edit[:old_text] || edit["old_text"]
          new_text = edit[:new_text] || edit["new_text"]

          escaped = Regexp.new(Regexp.escape(old_text))
          occurrences = content.scan(escaped).length

          if occurrences.zero?
            error_result = Dispatch::Tools::Result.failure(
              error: "Edit #{edits_applied + 1}: old_text not found in #{path}"
            )
            break
          end

          if occurrences > 1
            error_result = Dispatch::Tools::Result.failure(
              error: "Edit #{edits_applied + 1}: ambiguous match — old_text found #{occurrences} times in #{path}. " \
                     "Provide more context to make the match unique."
            )
            break
          end

          content = content.sub(old_text, new_text)
          edits_applied += 1
        end

        next error_result if error_result

        File.write(resolved, content)
        Dispatch::Tools::Result.success(output: "Applied #{edits_applied} edit(s) to #{path}")
      end
    end
  end
end