Class: SwarmMemory::Tools::MemoryWrite

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

Overview

Tool for writing content to memory storage

Stores content and metadata in persistent, per-agent memory storage. Each agent has its own isolated memory storage that persists across sessions.

Instance Method Summary collapse

Constructor Details

#initialize(storage:, agent_name:) ⇒ MemoryWrite

Initialize with storage instance

Parameters:

  • storage (Core::Storage)

    Storage instance

  • agent_name (String, Symbol)

    Agent identifier



106
107
108
109
110
# File 'lib/swarm_memory/tools/memory_write.rb', line 106

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

Instance Method Details

#execute(file_path:, content:, title:, type:, confidence: nil, tags:, related:, domain:, source: nil, tools: nil, permissions: nil) ⇒ String

Execute the tool

Parameters:

  • file_path (String)

    Path to store content (.md file)

  • content (String)

    Content to store (pure markdown)

  • title (String)

    Brief title

  • type (String, nil)

    Entry type

  • confidence (String, nil) (defaults to: nil)

    Confidence level

  • tags (Array, nil)

    Tags

  • related (Array, nil)

    Related paths

  • domain (String, nil)

    Domain

  • source (String, nil) (defaults to: nil)

    Source

  • tools (Array, nil) (defaults to: nil)

    Tools required (for skills)

  • permissions (Hash, nil) (defaults to: nil)

    Tool permissions (for skills)

Returns:

  • (String)

    Success message



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
180
181
# File 'lib/swarm_memory/tools/memory_write.rb', line 131

def execute(
  file_path:,
  content:,
  title:,
  type:,
  confidence: nil,
  tags:,
  related:,
  domain:,
  source: nil,
  tools: nil,
  permissions: nil
)
  # Validate content length (250 word limit)
  word_count = content.split(/\s+/).size
  if word_count > 250
    return validation_error(
      "Content exceeds 250-word limit (#{word_count} words). " \
        "Please extract the key entities and concepts from this content, then split it into multiple smaller, " \
        "focused memories (each under 250 words) that still capture ALL the important details. " \
        "Link related memories together using the 'related' metadata field with memory:// URIs. " \
        "Each memory should cover one specific aspect or concept while preserving completeness.",
    )
  end

  # Build metadata hash from params
  # Handle both JSON strings (from LLMs) and Ruby arrays (from tests/code)
   = {}
  ["type"] = type if type
  ["confidence"] = confidence || "medium" # Default to medium
  ["tags"] = parse_array_param(tags) if tags
  ["related"] = parse_array_param(related) if related
  ["domain"] = domain if domain
  ["source"] = source || "user" # Default to user
  ["tools"] = parse_array_param(tools) if tools
  ["permissions"] = parse_object_param(permissions) if permissions

  # Write to storage (metadata passed separately, not in content)
  entry = @storage.write(
    file_path: file_path,
    content: content,
    title: title,
    metadata: ,
  )

  "Stored at memory://#{file_path} (#{format_bytes(entry.size)})"
rescue ArgumentError => e
  validation_error(e.message)
rescue JSON::ParserError => e
  validation_error("Invalid tool parameter JSON format: #{e.message}")
end

#nameObject

Override name to return simple "MemoryWrite"



113
114
115
# File 'lib/swarm_memory/tools/memory_write.rb', line 113

def name
  "MemoryWrite"
end