Class: SwarmMemory::Tools::MemoryRead

Inherits:
RubyLLM::Tool
  • Object
show all
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

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



51
52
53
54
55
# File 'lib/swarm_memory/tools/memory_read.rb', line 51

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)

    JSON with content and metadata



66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/swarm_memory/tools/memory_read.rb', line 66

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)

  # Always return JSON format (metadata always exists - at minimum title)
  format_as_json(entry)
rescue ArgumentError => e
  validation_error(e.message)
end

#nameObject

Override name to return simple "MemoryRead"



58
59
60
# File 'lib/swarm_memory/tools/memory_read.rb', line 58

def name
  "MemoryRead"
end