Class: SwarmMemory::Tools::MemoryRead

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

Overview

Tool for reading content from memory storage

Retrieves content stored by this agent using memory_write. Each agent has its own isolated memory storage.

Instance Method Summary collapse

Methods included from TitleLookup

#format_memory_path_with_title, #lookup_title

Constructor Details

#initialize(storage:, agent_name:) ⇒ MemoryRead

Initialize with storage instance and agent name

Parameters:

  • storage (Core::Storage)

    Storage instance

  • agent_name (String, Symbol)

    Agent identifier



55
56
57
58
59
# File 'lib/swarm_memory/tools/memory_read.rb', line 55

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

Instance Method Details

#execute(file_path:) ⇒ String

Execute the tool

Parameters:

  • file_path (String)

    Path to read from

Returns:

  • (String)

    Content with line numbers and optional related memories reminder



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/swarm_memory/tools/memory_read.rb', line 70

def execute(file_path:)
  # Read full entry with metadata
  entry = @storage.read_entry(file_path: file_path)

  # Register this read in the tracker with content digest
  Core::StorageReadTracker.register_read(@agent_name, file_path, entry.content)

  # Return plain text with line numbers
  result = format_with_line_numbers(entry.content)

  # Append related memories reminder if present
  related_paths = entry.&.dig("related") || []
  result += format_related_memories_reminder(related_paths) if related_paths.any?

  result
rescue ArgumentError => e
  validation_error(e.message)
end

#nameObject

Override name to return simple "MemoryRead"



62
63
64
# File 'lib/swarm_memory/tools/memory_read.rb', line 62

def name
  "MemoryRead"
end