Class: SwarmSDK::Configuration
- Inherits:
-
Object
- Object
- SwarmSDK::Configuration
- 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
-
#agents ⇒ Object
readonly
Returns the value of attribute agents.
-
#all_agents_config ⇒ Object
readonly
Returns the value of attribute all_agents_config.
-
#all_agents_hooks ⇒ Object
readonly
Returns the value of attribute all_agents_hooks.
-
#config_type ⇒ Object
readonly
Returns the value of attribute config_type.
-
#external_swarms ⇒ Object
readonly
Returns the value of attribute external_swarms.
-
#lead_agent ⇒ Object
readonly
Returns the value of attribute lead_agent.
-
#nodes ⇒ Object
readonly
Returns the value of attribute nodes.
-
#scratchpad_enabled ⇒ Object
readonly
Returns the value of attribute scratchpad_enabled.
-
#start_node ⇒ Object
readonly
Returns the value of attribute start_node.
-
#swarm_hooks ⇒ Object
readonly
Returns the value of attribute swarm_hooks.
-
#swarm_id ⇒ Object
readonly
Returns the value of attribute swarm_id.
-
#swarm_name ⇒ Object
readonly
Returns the value of attribute swarm_name.
Class Method Summary collapse
-
.load_file(path, env_interpolation: nil) ⇒ Configuration
Load configuration from YAML file.
Instance Method Summary collapse
- #agent_names ⇒ Object
- #connections_for(agent_name) ⇒ Object
-
#initialize(yaml_content, base_dir: Dir.pwd, env_interpolation: nil) ⇒ Configuration
constructor
Initialize configuration from YAML string.
-
#load_and_validate ⇒ self
Parse and validate YAML configuration.
-
#to_swarm(allow_filesystem_tools: nil) ⇒ Swarm, Workflow
Convert configuration to Swarm or Workflow using appropriate builder.
Constructor Details
#initialize(yaml_content, base_dir: Dir.pwd, env_interpolation: nil) ⇒ Configuration
Initialize configuration from YAML string
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). @env_interpolation = env_interpolation @parser = nil @translator = nil end |
Instance Attribute Details
#agents ⇒ Object (readonly)
Returns the value of attribute agents.
24 25 26 |
# File 'lib/swarm_sdk/configuration.rb', line 24 def agents @agents end |
#all_agents_config ⇒ Object (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_hooks ⇒ Object (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_type ⇒ Object (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_swarms ⇒ Object (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_agent ⇒ Object (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 |
#nodes ⇒ Object (readonly)
Returns the value of attribute nodes.
24 25 26 |
# File 'lib/swarm_sdk/configuration.rb', line 24 def nodes @nodes end |
#scratchpad_enabled ⇒ Object (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_node ⇒ Object (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_hooks ⇒ Object (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_id ⇒ Object (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_name ⇒ Object (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
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). 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_names ⇒ Object
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_validate ⇒ self
Parse and validate YAML configuration
Delegates to Parser for all parsing logic, then syncs parsed data to instance variables for backward compatibility.
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.
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 |