Class: SwarmMemory::Tools::MemoryWrite
- Inherits:
-
SwarmSDK::Tools::Base
- Object
- SwarmSDK::Tools::Base
- SwarmMemory::Tools::MemoryWrite
- 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
-
#execute(file_path:, content:, title:, type:, confidence: nil, tags:, related:, domain:, source: nil, tools: nil, permissions: nil) ⇒ String
Execute the tool.
-
#initialize(storage:, agent_name:) ⇒ MemoryWrite
constructor
Initialize with storage instance.
-
#name ⇒ Object
Override name to return simple "MemoryWrite".
Constructor Details
#initialize(storage:, agent_name:) ⇒ MemoryWrite
Initialize with storage instance
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
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() if ["related"] = parse_array_param() if ["domain"] = domain if domain ["source"] = source || "user" # Default to user ["tools"] = parse_array_param(tools) if tools ["permissions"] = parse_object_param() if # 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.) rescue JSON::ParserError => e validation_error("Invalid tool parameter JSON format: #{e.}") end |
#name ⇒ Object
Override name to return simple "MemoryWrite"
113 114 115 |
# File 'lib/swarm_memory/tools/memory_write.rb', line 113 def name "MemoryWrite" end |