Class: SwarmSDK::Plugin
- Inherits:
-
Object
- Object
- SwarmSDK::Plugin
- Defined in:
- lib/swarm_sdk/plugin.rb
Overview
Base class for SwarmSDK plugins
Plugins provide tools, storage, configuration parsing, and lifecycle hooks. Plugins are self-registering - they call SwarmSDK::PluginRegistry.register when the gem is loaded.
Adding Custom Attributes to Agents
Plugins can add custom attributes to Agent::Definition that are preserved when agents are cloned (e.g., in Workflow). To do this:
- Add attr_reader to Agent::Definition for your attribute
- Parse the attribute in Agent::Definition#initialize
- Implement serialize_config to preserve it during serialization
Now agents can use your custom config:
agent :researcher do
my_custom_config { option: "value" }
end
And it will be preserved when Workflow clones the agent!
Instance Method Summary collapse
-
#create_storage(agent_name:, config:) ⇒ Object?
Create plugin storage for an agent (optional).
-
#create_tool(tool_name, context) ⇒ RubyLLM::Tool
Create a tool instance.
-
#get_tool_result_digest(agent_name:, tool_name:, path:) ⇒ String?
Get digest for a tool result (e.g., file hash, memory entry hash).
-
#immutable_tools ⇒ Array<Symbol>
Tools that should be marked immutable (optional).
-
#memory_configured?(agent_definition) ⇒ Boolean
Check if memory is configured for this agent (optional).
-
#name ⇒ Symbol
Plugin name (must be unique).
-
#on_agent_initialized(agent_name:, agent:, context:) ⇒ Object
Lifecycle: Called when agent is initialized.
-
#on_swarm_started(swarm:) ⇒ Object
Lifecycle: Called when swarm starts.
-
#on_swarm_stopped(swarm:) ⇒ Object
Lifecycle: Called when swarm stops.
-
#on_user_message(agent_name:, prompt:, is_first_message:) ⇒ Array<String>
Lifecycle: Called on every user message.
-
#parse_config(raw_config) ⇒ Object
Parse plugin configuration from agent definition.
-
#restore_agent_state(agent_name, state) ⇒ void
Restore plugin-specific state for an agent.
-
#serialize_config(agent_definition:) ⇒ Hash
Contribute to agent serialization (optional).
-
#snapshot_agent_state(agent_name) ⇒ Hash
Snapshot plugin-specific state for an agent.
-
#system_prompt_contribution(agent_definition:, storage:) ⇒ String?
Contribute to agent system prompt (optional).
-
#tools ⇒ Array<Symbol>
List of tools provided by this plugin.
-
#translate_yaml_config(builder, agent_config) ⇒ void
Translate YAML configuration into DSL calls.
Instance Method Details
#create_storage(agent_name:, config:) ⇒ Object?
Create plugin storage for an agent (optional)
Called during agent initialization. Return nil if plugin doesn't need storage.
112 113 114 |
# File 'lib/swarm_sdk/plugin.rb', line 112 def create_storage(agent_name:, config:) nil end |
#create_tool(tool_name, context) ⇒ RubyLLM::Tool
Create a tool instance
101 102 103 |
# File 'lib/swarm_sdk/plugin.rb', line 101 def create_tool(tool_name, context) raise NotImplementedError, "#{self.class} must implement #create_tool" end |
#get_tool_result_digest(agent_name:, tool_name:, path:) ⇒ String?
Get digest for a tool result (e.g., file hash, memory entry hash)
Called during tool result metadata collection. Returns a digest that can be used to detect if the resource has changed since it was last read. This enables change detection hooks.
277 278 279 |
# File 'lib/swarm_sdk/plugin.rb', line 277 def get_tool_result_digest(agent_name:, tool_name:, path:) nil end |
#immutable_tools ⇒ Array<Symbol>
Tools that should be marked immutable (optional)
Immutable tools cannot be removed by other tools (e.g., LoadSkill).
138 139 140 |
# File 'lib/swarm_sdk/plugin.rb', line 138 def immutable_tools [] end |
#memory_configured?(agent_definition) ⇒ Boolean
Check if memory is configured for this agent (optional)
146 147 148 |
# File 'lib/swarm_sdk/plugin.rb', line 146 def memory_configured?(agent_definition) false end |
#name ⇒ Symbol
Plugin name (must be unique)
80 81 82 |
# File 'lib/swarm_sdk/plugin.rb', line 80 def name raise NotImplementedError, "#{self.class} must implement #name" end |
#on_agent_initialized(agent_name:, agent:, context:) ⇒ Object
Lifecycle: Called when agent is initialized
158 159 160 |
# File 'lib/swarm_sdk/plugin.rb', line 158 def on_agent_initialized(agent_name:, agent:, context:) # Override if needed end |
#on_swarm_started(swarm:) ⇒ Object
Lifecycle: Called when swarm starts
165 166 167 |
# File 'lib/swarm_sdk/plugin.rb', line 165 def on_swarm_started(swarm:) # Override if needed end |
#on_swarm_stopped(swarm:) ⇒ Object
Lifecycle: Called when swarm stops
172 173 174 |
# File 'lib/swarm_sdk/plugin.rb', line 172 def on_swarm_stopped(swarm:) # Override if needed end |
#on_user_message(agent_name:, prompt:, is_first_message:) ⇒ Array<String>
Lifecycle: Called on every user message
Plugins can return system reminders to inject based on the user's prompt. This enables features like semantic skill discovery, context injection, etc.
193 194 195 |
# File 'lib/swarm_sdk/plugin.rb', line 193 def (agent_name:, prompt:, is_first_message:) [] end |
#parse_config(raw_config) ⇒ Object
Parse plugin configuration from agent definition
120 121 122 |
# File 'lib/swarm_sdk/plugin.rb', line 120 def parse_config(raw_config) raw_config end |
#restore_agent_state(agent_name, state) ⇒ void
This method returns an undefined value.
Restore plugin-specific state for an agent
Called during state restoration. Restore any persisted state. This method is idempotent - calling it multiple times with the same state should produce the same result.
256 257 258 |
# File 'lib/swarm_sdk/plugin.rb', line 256 def restore_agent_state(agent_name, state) # Override if needed end |
#serialize_config(agent_definition:) ⇒ Hash
Contribute to agent serialization (optional)
Called when Agent::Definition.to_h is invoked (e.g., for cloning agents in Workflow). Plugins can return config keys that should be included in the serialized hash to preserve their state.
This allows plugins to maintain their configuration when agents are cloned or serialized, without SwarmSDK needing to know about plugin-specific fields.
215 216 217 |
# File 'lib/swarm_sdk/plugin.rb', line 215 def serialize_config(agent_definition:) {} end |
#snapshot_agent_state(agent_name) ⇒ Hash
Snapshot plugin-specific state for an agent
Called during state snapshot creation (e.g., session persistence). Return any state your plugin needs to persist for this agent. The returned hash will be JSON serialized.
235 236 237 |
# File 'lib/swarm_sdk/plugin.rb', line 235 def snapshot_agent_state(agent_name) {} end |
#system_prompt_contribution(agent_definition:, storage:) ⇒ String?
Contribute to agent system prompt (optional)
129 130 131 |
# File 'lib/swarm_sdk/plugin.rb', line 129 def system_prompt_contribution(agent_definition:, storage:) nil end |
#tools ⇒ Array<Symbol>
List of tools provided by this plugin
87 88 89 |
# File 'lib/swarm_sdk/plugin.rb', line 87 def tools [] end |
#translate_yaml_config(builder, agent_config) ⇒ void
This method returns an undefined value.
Translate YAML configuration into DSL calls
Called during YAML-to-DSL translation. Plugins can translate their specific YAML configuration keys into DSL method calls on the builder. This allows SDK to remain plugin-agnostic while plugins can add YAML configuration support.
305 306 307 |
# File 'lib/swarm_sdk/plugin.rb', line 305 def translate_yaml_config(builder, agent_config) # Override if plugin needs YAML configuration support end |