Class: SwarmSDK::Swarm::AgentInitializer

Inherits:
Object
  • Object
show all
Defined in:
lib/swarm_sdk/swarm/agent_initializer.rb

Overview

Handles the complex 5-pass agent initialization process

Responsibilities:

  • Create all agent chat instances (pass 1)
  • Register delegation tools (pass 2)
  • Setup agent contexts (pass 3)
  • Configure hook system (pass 4)
  • Apply YAML hooks if present (pass 5)

This encapsulates the complex initialization logic that was previously embedded in Swarm#initialize_agents.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(swarm) ⇒ AgentInitializer

Initialize with swarm reference (all data accessible via swarm)



20
21
22
23
24
# File 'lib/swarm_sdk/swarm/agent_initializer.rb', line 20

def initialize(swarm)
  @swarm = swarm
  @agents = {}
  @agent_contexts = {}
end

Instance Attribute Details

#agent_contextsObject (readonly)

Provide access to agent contexts for Swarm



49
50
51
# File 'lib/swarm_sdk/swarm/agent_initializer.rb', line 49

def agent_contexts
  @agent_contexts
end

Instance Method Details

#create_delegation_tool(name:, description:, delegate_chat:, agent_name:, delegating_chat: nil, custom_tool_name: nil, preserve_context: true) ⇒ Tools::Delegate

Create a tool that delegates work to another agent

This method is public for testing delegation from Swarm.



96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/swarm_sdk/swarm/agent_initializer.rb', line 96

def create_delegation_tool(name:, description:, delegate_chat:, agent_name:, delegating_chat: nil, custom_tool_name: nil, preserve_context: true)
  Tools::Delegate.new(
    delegate_name: name,
    delegate_description: description,
    delegate_chat: delegate_chat,
    agent_name: agent_name,
    swarm: @swarm,
    delegating_chat: delegating_chat,
    custom_tool_name: custom_tool_name,
    preserve_context: preserve_context,
  )
end

#initialize_allHash

Initialize all agents with their chat instances and tools

This implements a 6-pass algorithm:

  1. Create all Agent::Chat instances
  2. Register delegation tools (agents can call each other)
  3. Setup agent contexts for tracking
  4. Configure hook system
  5. Apply YAML hooks (if loaded from YAML)
  6. Activate tools (Plan 025: populate @llm_chat.tools from registry after plugins)


37
38
39
40
41
42
43
44
45
46
# File 'lib/swarm_sdk/swarm/agent_initializer.rb', line 37

def initialize_all
  pass_1_create_agents
  pass_2_register_delegation_tools
  pass_3_setup_contexts
  pass_4_configure_hooks
  pass_5_apply_yaml_hooks
  pass_6_activate_tools # Plan 025: Activate tools after all plugins registered

  @agents
end

#initialize_isolated_agent(agent_name) ⇒ Agent::Chat

Initialize a single agent in isolation (for observer agents)

Creates an isolated agent chat without delegation tools, suitable for observer agents that don't need to delegate. Reuses existing create_agent_chat infrastructure.

Examples:

initializer = AgentInitializer.new(swarm)
chat = initializer.initialize_isolated_agent(:profiler)
chat.ask("Analyze this prompt")

Raises:



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/swarm_sdk/swarm/agent_initializer.rb', line 65

def initialize_isolated_agent(agent_name)
  agent_def = @swarm.agent_definitions[agent_name]
  raise ConfigurationError, "Agent '#{agent_name}' not found" unless agent_def

  # Ensure plugin storages are created (needed by ToolConfigurator)
  create_plugin_storages if @swarm.plugin_storages.empty?

  # Reuse existing create_agent_chat infrastructure
  tool_configurator = ToolConfigurator.new(
    @swarm,
    @swarm.scratchpad_storage,
    @swarm.plugin_storages,
  )

  # Create chat using same method as pass_1_create_agents
  # This gives us full tool setup, MCP servers, etc.
  create_agent_chat(agent_name, agent_def, tool_configurator)
end