Class: Octo::Tools::Edit

Inherits:
Base
  • Object
show all
Defined in:
lib/octo/tools/edit.rb

Instance Method Summary collapse

Methods inherited from Base

#category, #description, #name, #parameters, #to_function_definition

Instance Method Details

#execute(path:, old_string:, new_string:, replace_all: false, working_dir: nil) ⇒ Object



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
92
93
94
95
96
97
# File 'lib/octo/tools/edit.rb', line 36

def execute(path:, old_string:, new_string:, replace_all: false, working_dir: nil)
  # Expand ~ to home directory, resolve relative paths against working_dir
  path = expand_path(path, working_dir: working_dir)

  unless File.exist?(path)
    return { error: "File not found: #{path}" }
  end

  unless File.file?(path)
    return { error: "Path is not a file: #{path}" }
  end

  begin
    # Scrub invalid UTF-8 bytes at read time — otherwise editing a file
    # that contains non-UTF-8 bytes would poison history / error messages
    # and cause JSON.generate to fail during replay.
    original_content = safe_utf8(File.read(path))
    content = original_content

    # Find matching string using layered strategy (shared with preview)
    match_result = Utils::StringMatcher.find_match(content, old_string)

    unless match_result
      return build_helpful_error(content, old_string, path)
    end

    actual_old_string = match_result[:matched_string]
    occurrences = match_result[:occurrences]

    # If not replace_all and multiple occurrences, warn about ambiguity
    if !replace_all && occurrences > 1
      return {
        error: "String appears #{occurrences} times in the file. Use replace_all: true to replace all occurrences, " \
               "or provide a more specific string that appears only once."
      }
    end

    # Perform replacement
    content = replace_all ? content.gsub(actual_old_string, new_string) : content.sub(actual_old_string, new_string)

    File.write(path, content)

    # Generate a unified diff for UI display. Limit to ~40 lines so
    # large-file edits don't flood the Web UI with noise.
    diff = Diffy::Diff.new(original_content, content, context: 3).to_s(:text)
    diff_lines = diff.lines
    if diff_lines.size > 40
      diff = diff_lines.first(37).join + "...\n(#{(diff_lines.size - 37)} more lines)\n"
    end

    {
      path: File.expand_path(path),
      replacements: replace_all ? occurrences : 1,
      error: nil,
      diff: diff
    }
  rescue Errno::EACCES => e
    { error: "Permission denied: #{e.message}" }
  rescue StandardError => e
    { error: "Failed to edit file: #{e.message}" }
  end
end

#format_call(args) ⇒ Object



134
135
136
137
# File 'lib/octo/tools/edit.rb', line 134

def format_call(args)
  path = args[:file_path] || args["file_path"] || args[:path] || args["path"]
  "Edit(#{Utils::PathHelper.safe_basename(path)})"
end

#format_result(result) ⇒ Object



139
140
141
142
143
144
# File 'lib/octo/tools/edit.rb', line 139

def format_result(result)
  return result[:error] if result[:error]

  replacements = result[:replacements] || result["replacements"] || 1
  "Modified #{replacements} occurrence#{replacements > 1 ? "s" : ""}"
end

#format_result_for_ui(result) ⇒ Object



146
147
148
149
150
151
152
153
154
155
# File 'lib/octo/tools/edit.rb', line 146

def format_result_for_ui(result)
  return nil if result[:error]
  {
    type: "edit",
    path: result[:path],
    operation: "replace",
    occurrences: result[:replacements] || 1,
    diff: result[:diff]
  }
end