Class: SwarmSDK::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/swarm_sdk/configuration.rb,
lib/swarm_sdk/configuration/parser.rb,
lib/swarm_sdk/configuration/translator.rb

Overview

Configuration facade that delegates to Parser and Translator

This class maintains the public API while internally delegating to:

  • Configuration::Parser - YAML parsing, validation, and normalization
  • Configuration::Translator - Translation to Swarm/Workflow DSL builders

Public API (unchanged)

  • Configuration.load_file(path) - Load from file
  • Configuration.new(yaml_content, base_dir:) - Load from string
  • config.load_and_validate - Parse and validate
  • config.to_swarm(allow_filesystem_tools:) - Convert to Swarm/Workflow
  • config.agent_names - Get list of agent names
  • config.connections_for(agent_name) - Get delegation targets

Architecture

The facade pattern keeps backward compatibility while separating concerns:

  • Parser handles all YAML parsing and validation logic
  • Translator handles all DSL builder translation logic
  • Configuration delegates to both, exposing parsed data via attr_readers

Defined Under Namespace

Classes: Parser, Translator

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(yaml_content, base_dir: Dir.pwd, env_interpolation: nil) ⇒ Configuration

Initialize configuration from YAML string

Parameters:

  • yaml_content (String)

    YAML configuration content

  • base_dir (String, Pathname) (defaults to: Dir.pwd)

    Base directory for resolving agent file paths (default: Dir.pwd)

  • env_interpolation (Boolean, nil) (defaults to: nil)

    Whether to interpolate environment variables. When nil, uses the global SwarmSDK.config.env_interpolation setting. When true, interpolates $VAR and $VAR:=default patterns. When false, skips interpolation entirely.

Raises:

  • (ArgumentError)


71
72
73
74
75
76
77
78
79
80
# File 'lib/swarm_sdk/configuration.rb', line 71

def initialize(yaml_content, base_dir: Dir.pwd, env_interpolation: nil)
  raise ArgumentError, "yaml_content cannot be nil" if yaml_content.nil?
  raise ArgumentError, "base_dir cannot be nil" if base_dir.nil?

  @yaml_content = yaml_content
  @base_dir = Pathname.new(base_dir).expand_path
  @env_interpolation = env_interpolation
  @parser = nil
  @translator = nil
end

Instance Attribute Details

#agentsObject (readonly)

Returns the value of attribute agents.



24
25
26
# File 'lib/swarm_sdk/configuration.rb', line 24

def agents
  @agents
end

#all_agents_configObject (readonly)

Returns the value of attribute all_agents_config.



24
25
26
# File 'lib/swarm_sdk/configuration.rb', line 24

def all_agents_config
  @all_agents_config
end

#all_agents_hooksObject (readonly)

Returns the value of attribute all_agents_hooks.



24
25
26
# File 'lib/swarm_sdk/configuration.rb', line 24

def all_agents_hooks
  @all_agents_hooks
end

#config_typeObject (readonly)

Returns the value of attribute config_type.



24
25
26
# File 'lib/swarm_sdk/configuration.rb', line 24

def config_type
  @config_type
end

#external_swarmsObject (readonly)

Returns the value of attribute external_swarms.



24
25
26
# File 'lib/swarm_sdk/configuration.rb', line 24

def external_swarms
  @external_swarms
end

#lead_agentObject (readonly)

Returns the value of attribute lead_agent.



24
25
26
# File 'lib/swarm_sdk/configuration.rb', line 24

def lead_agent
  @lead_agent
end

#nodesObject (readonly)

Returns the value of attribute nodes.



24
25
26
# File 'lib/swarm_sdk/configuration.rb', line 24

def nodes
  @nodes
end

#scratchpad_enabledObject (readonly)

Returns the value of attribute scratchpad_enabled.



24
25
26
# File 'lib/swarm_sdk/configuration.rb', line 24

def scratchpad_enabled
  @scratchpad_enabled
end

#start_nodeObject (readonly)

Returns the value of attribute start_node.



24
25
26
# File 'lib/swarm_sdk/configuration.rb', line 24

def start_node
  @start_node
end

#swarm_hooksObject (readonly)

Returns the value of attribute swarm_hooks.



24
25
26
# File 'lib/swarm_sdk/configuration.rb', line 24

def swarm_hooks
  @swarm_hooks
end

#swarm_idObject (readonly)

Returns the value of attribute swarm_id.



24
25
26
# File 'lib/swarm_sdk/configuration.rb', line 24

def swarm_id
  @swarm_id
end

#swarm_nameObject (readonly)

Returns the value of attribute swarm_name.



24
25
26
# File 'lib/swarm_sdk/configuration.rb', line 24

def swarm_name
  @swarm_name
end

Class Method Details

.load_file(path, env_interpolation: nil) ⇒ Configuration

Load configuration from YAML file

Parameters:

  • path (String, Pathname)

    Path to YAML configuration file

  • env_interpolation (Boolean, nil) (defaults to: nil)

    Whether to interpolate environment variables. When nil, uses the global SwarmSDK.config.env_interpolation setting. When true, interpolates $VAR and $VAR:=default patterns. When false, skips interpolation entirely.

Returns:

Raises:



47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/swarm_sdk/configuration.rb', line 47

def load_file(path, env_interpolation: nil)
  path = Pathname.new(path).expand_path

  unless path.exist?
    raise ConfigurationError, "Configuration file not found: #{path}"
  end

  yaml_content = File.read(path)
  base_dir = path.dirname

  new(yaml_content, base_dir: base_dir, env_interpolation: env_interpolation).tap(&:load_and_validate)
rescue Errno::ENOENT
  raise ConfigurationError, "Configuration file not found: #{path}"
end

Instance Method Details

#agent_namesObject



98
99
100
# File 'lib/swarm_sdk/configuration.rb', line 98

def agent_names
  @agents.keys
end

#connections_for(agent_name) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/swarm_sdk/configuration.rb', line 102

def connections_for(agent_name)
  agent_config = @agents[agent_name]
  return [] unless agent_config

  delegates = agent_config[:delegates_to] || []

  # Handle both array and hash formats for delegates_to
  case delegates
  when Array
    # Array of symbols: [:frontend, :backend]
    # OR array of hashes: [{agent: :frontend, tool_name: "Custom"}]
    delegates.map do |item|
      case item
      when Symbol, String
        item.to_sym
      when Hash
        # Extract agent name from hash format
        agent_name = item[:agent] || item["agent"]
        agent_name&.to_sym
      end
    end.compact # Remove nils from malformed hashes
  when Hash
    # Hash format: {frontend: "Custom", backend: nil}
    delegates.keys.map(&:to_sym)
  else
    []
  end
end

#load_and_validateself

Parse and validate YAML configuration

Delegates to Parser for all parsing logic, then syncs parsed data to instance variables for backward compatibility.

Returns:

  • (self)


88
89
90
91
92
93
94
95
96
# File 'lib/swarm_sdk/configuration.rb', line 88

def load_and_validate
  @parser = Parser.new(@yaml_content, base_dir: @base_dir, env_interpolation: @env_interpolation)
  @parser.parse

  # Sync parsed data to instance variables for backward compatibility
  sync_from_parser

  self
end

#to_swarm(allow_filesystem_tools: nil) ⇒ Swarm, Workflow

Convert configuration to Swarm or Workflow using appropriate builder

Delegates to Translator for all DSL translation logic.

Parameters:

  • allow_filesystem_tools (Boolean, nil) (defaults to: nil)

    Whether to allow filesystem tools (nil uses global setting)

Returns:

Raises:



137
138
139
140
141
142
# File 'lib/swarm_sdk/configuration.rb', line 137

def to_swarm(allow_filesystem_tools: nil)
  raise ConfigurationError, "Configuration not loaded. Call load_and_validate first." unless @parser

  @translator = Translator.new(@parser)
  @translator.to_swarm(allow_filesystem_tools: allow_filesystem_tools)
end