Class: SwarmSDK::AgentRegistry
- Inherits:
-
Object
- Object
- SwarmSDK::AgentRegistry
- Defined in:
- lib/swarm_sdk/agent_registry.rb
Overview
This registry is not thread-safe. In multi-threaded environments, register all agents before spawning threads, or synchronize access externally. For typical fiber-based async usage (the default in SwarmSDK), this is not a concern.
Global registry for reusable agent definitions
AgentRegistry allows declaring agents in separate files that can be referenced by name in swarm definitions. This promotes code reuse and separation of concerns - agent definitions can live in dedicated files while swarm configurations compose them together.
Usage
Register agents globally (typically in separate files):
# agents/backend.rb
SwarmSDK.agent :backend do
model "claude-sonnet-4"
description "Backend API developer"
system_prompt "You build REST APIs"
tools :Read, :Edit, :Bash
end
Reference registered agents in swarm definitions:
# swarm.rb
SwarmSDK.build do
name "Dev Team"
lead :backend
agent :backend # Pulls from registry
end
Override Support
Registered agents can be extended with additional configuration:
SwarmSDK.build do
name "Dev Team"
lead :backend
agent :backend do
# Registry config is applied first, then this block
tools :CustomTool # Adds to tools from registry
delegates_to :database
end
end
Class Method Summary collapse
-
.clear ⇒ void
Clear all registrations.
-
.get(name) ⇒ Proc?
Retrieve a registered agent block.
-
.names ⇒ Array<Symbol>
List all registered agent names.
-
.register(name) { ... } ⇒ void
Register an agent definition block.
-
.registered?(name) ⇒ Boolean
Check if an agent is registered.
Class Method Details
.clear ⇒ void
This method returns an undefined value.
Clear all registrations
Primarily useful for testing to ensure clean state between tests.
141 142 143 |
# File 'lib/swarm_sdk/agent_registry.rb', line 141 def clear @agents.clear end |
.get(name) ⇒ Proc?
Retrieve a registered agent block
103 104 105 |
# File 'lib/swarm_sdk/agent_registry.rb', line 103 def get(name) @agents[name.to_sym] end |
.names ⇒ Array<Symbol>
List all registered agent names
127 128 129 |
# File 'lib/swarm_sdk/agent_registry.rb', line 127 def names @agents.keys end |
.register(name) { ... } ⇒ void
This method returns an undefined value.
Register an agent definition block
Stores a configuration block that will be executed when the agent is referenced in a swarm definition. The block receives an Agent::Builder context and can use all builder DSL methods.
82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/swarm_sdk/agent_registry.rb', line 82 def register(name, &block) raise ArgumentError, "Block required for agent registration" unless block_given? sym_name = name.to_sym if @agents.key?(sym_name) raise ArgumentError, "Agent '#{sym_name}' is already registered. " \ "Use SwarmSDK.clear_agent_registry! to reset, or choose a different name." end @agents[sym_name] = block end |
.registered?(name) ⇒ Boolean
Check if an agent is registered
116 117 118 |
# File 'lib/swarm_sdk/agent_registry.rb', line 116 def registered?(name) @agents.key?(name.to_sym) end |