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)

Parameters:

  • swarm (Swarm)

    The parent swarm instance



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



47
48
49
# File 'lib/swarm_sdk/swarm/agent_initializer.rb', line 47

def agent_contexts
  @agent_contexts
end

Instance Method Details

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

Create a tool that delegates work to another agent

This method is public for testing delegation from Swarm.

Parameters:

  • name (String)

    Delegate agent name

  • description (String)

    Delegate agent description

  • delegate_chat (Agent::Chat)

    The delegate's chat instance

  • agent_name (Symbol)

    Name of the delegating agent

  • delegating_chat (Agent::Chat, nil) (defaults to: nil)

    The chat instance of the agent doing the delegating

Returns:



92
93
94
95
96
97
98
99
100
101
# File 'lib/swarm_sdk/swarm/agent_initializer.rb', line 92

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

#initialize_allHash

Initialize all agents with their chat instances and tools

This implements a 5-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)

Returns:

  • (Hash)

    agents hash { agent_name => Agent::Chat }



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

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

  @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")

Parameters:

  • agent_name (Symbol)

    Name of agent to initialize

Returns:

Raises:



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

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