Class: SwarmMemory::Tools::MemoryEdit

Inherits:
SwarmSDK::Tools::Base
  • Object
show all
Defined in:
lib/swarm_memory/tools/memory_edit.rb

Overview

Tool for editing memory entries with exact string replacement

Performs exact string replacements in memory content. Each agent has its own isolated memory storage.

Instance Method Summary collapse

Constructor Details

#initialize(storage:, agent_name:) ⇒ MemoryEdit

Initialize with storage instance and agent name

Parameters:

  • storage (Core::Storage)

    Storage instance

  • agent_name (String, Symbol)

    Agent identifier



96
97
98
99
100
# File 'lib/swarm_memory/tools/memory_edit.rb', line 96

def initialize(storage:, agent_name:)
  super()
  @storage = storage
  @agent_name = agent_name.to_sym
end

Instance Method Details

#execute(file_path:, old_string:, new_string:, replace_all: false) ⇒ String

Execute the tool

Parameters:

  • file_path (String)

    Path to memory entry

  • old_string (String)

    Text to replace

  • new_string (String)

    Replacement text

  • replace_all (Boolean) (defaults to: false)

    Replace all occurrences

Returns:

  • (String)

    Success message or error



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/swarm_memory/tools/memory_edit.rb', line 114

def execute(file_path:, old_string:, new_string:, replace_all: false)
  # Validate inputs
  return validation_error("file_path is required") if file_path.nil? || file_path.to_s.strip.empty?
  return validation_error("old_string is required") if old_string.nil? || old_string.empty?
  return validation_error("new_string is required") if new_string.nil?

  # old_string and new_string must be different
  if old_string == new_string
    return validation_error("old_string and new_string must be different. They are currently identical.")
  end

  # Read current content (this will raise ArgumentError if entry doesn't exist)
  content = @storage.read(file_path: file_path)

  # Enforce read-before-edit with content verification
  unless Core::StorageReadTracker.entry_read?(@agent_name, file_path, @storage)
    return validation_error(
      "Cannot edit memory entry without reading it first. " \
        "You must use MemoryRead on 'memory://#{file_path}' before editing it. " \
        "This ensures you have the current content to match against.",
    )
  end

  # Check if old_string exists in content
  unless content.include?(old_string)
    return validation_error(<<~ERROR.chomp)
      old_string not found in memory entry. Make sure it matches exactly, including all whitespace and indentation.
      Do not include line number prefixes from MemoryRead tool output.
    ERROR
  end

  # Count occurrences
  occurrences = content.scan(old_string).count

  # If not replace_all and multiple occurrences, error
  if !replace_all && occurrences > 1
    return validation_error(<<~ERROR.chomp)
      Found #{occurrences} occurrences of old_string.
      Either provide more surrounding context to make the match unique, or use replace_all: true to replace all occurrences.
    ERROR
  end

  # Perform replacement
  new_content = if replace_all
    content.gsub(old_string, new_string)
  else
    content.sub(old_string, new_string)
  end

  # Get existing entry metadata
  entry = @storage.read_entry(file_path: file_path)

  # Write updated content back (preserving the title and metadata)
  @storage.write(
    file_path: file_path,
    content: new_content,
    title: entry.title,
    metadata: entry.,
  )

  # Build success message
  replaced_count = replace_all ? occurrences : 1
  "Successfully replaced #{replaced_count} occurrence(s) in memory://#{file_path}"
rescue ArgumentError => e
  validation_error(e.message)
end

#nameObject

Override name to return simple "MemoryEdit"



103
104
105
# File 'lib/swarm_memory/tools/memory_edit.rb', line 103

def name
  "MemoryEdit"
end