Class: SwarmMemory::Tools::LoadSkill

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

Overview

Tool for loading skills from memory and dynamically swapping agent tools

LoadSkill reads a skill from memory, validates it, and swaps the agent's mutable tools to match the skill's requirements. Immutable tools (Memory*, Think, LoadSkill) are always preserved.

Skills must:

  • Be stored in the skill/ hierarchy
  • Have type: 'skill' in metadata
  • Include tools array in metadata (optional)
  • Include permissions hash in metadata (optional)

Instance Method Summary collapse

Constructor Details

#initialize(storage:, agent_name:, chat:, tool_configurator: nil, agent_definition: nil) ⇒ LoadSkill

Initialize with storage and chat context

Parameters:

  • storage (Core::Storage)

    Memory storage

  • agent_name (Symbol)

    Agent identifier

  • chat (SwarmSDK::Agent::Chat)

    The agent's chat instance

  • tool_configurator (SwarmSDK::ToolConfigurator) (defaults to: nil)

    For creating tools (unused in Plan 025)

  • agent_definition (SwarmSDK::Agent::Definition) (defaults to: nil)

    For permissions (unused in Plan 025)



138
139
140
141
142
143
144
145
# File 'lib/swarm_memory/tools/load_skill.rb', line 138

def initialize(storage:, agent_name:, chat:, tool_configurator: nil, agent_definition: nil)
  super()
  @storage = storage
  @agent_name = agent_name
  @chat = chat
  # NOTE: tool_configurator and agent_definition kept for API compatibility
  # but unused in Plan 025 (tools come from registry, not recreation)
end

Instance Method Details

#execute(file_path:) ⇒ String

Execute the tool (Plan 025: Simplified - no tool swapping)

Parameters:

  • file_path (String)

    Path to skill in memory

Returns:

  • (String)

    Skill content with line numbers, or error message



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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/swarm_memory/tools/load_skill.rb', line 156

def execute(file_path:)
  # 1. Validate path starts with skill/
  unless file_path.start_with?("skill/")
    return validation_error("Skills must be stored in skill/ hierarchy. Got: #{file_path}")
  end

  # 2. Read entry with metadata
  begin
    entry = @storage.read_entry(file_path: file_path)
  rescue ArgumentError => e
    return validation_error(e.message)
  end

  # 3. Validate it's a skill
  unless entry. && entry.["type"] == "skill"
    type = entry.&.dig("type") || "none"
    return validation_error("memory://#{file_path} is not a skill (type: #{type})")
  end

  # 4. Extract tool requirements and permissions
  required_tools = entry.["tools"]
  permissions = entry.["permissions"] || {}

  # 5. Validate tools exist in registry (only if tools are specified and non-empty)
  if required_tools && !required_tools.empty?
    begin
      validate_skill_tools(required_tools)
    rescue ArgumentError => e
      return validation_error(e.message)
    end
  end

  # 6. Create and set skill state
  # Note: tools: nil or tools: [] both mean "no restriction" (keep all tools)
  skill_state = SwarmMemory::SkillState.new(
    file_path: file_path,
    tools: required_tools, # May be nil or [] (no restriction)
    permissions: permissions,
  )
  @chat.load_skill_state(skill_state)

  # 7. Activate tools if skill restricts them
  # If no restriction (nil or []), tools remain unchanged
  if skill_state.restricts_tools?
    @chat.activate_tools_for_prompt
  end
  # Otherwise, tools stay as-is (no swap)

  # 8. Return content with confirmation message
  title = entry.title || "Untitled Skill"
  result = "Loaded skill: #{title}\n\n"
  result += format_with_line_numbers(entry.content)

  # 9. Add system reminder if tools were restricted
  if skill_state.restricts_tools?
    result += "\n\n"
    result += build_toolset_update_reminder(required_tools)
  end

  result
end

#nameObject

Override name to return simple "LoadSkill"



148
149
150
# File 'lib/swarm_memory/tools/load_skill.rb', line 148

def name
  "LoadSkill"
end