Class: SwarmSDK::Config

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

Overview

Centralized configuration for SwarmSDK

Config provides a single entry point for all SwarmSDK configuration, including API keys (proxied to RubyLLM), defaults override, and WebFetch settings.

Priority Order

Configuration values are resolved in this order:

  1. Explicit value (set via SwarmSDK.configure)
  2. Environment variable
  3. Module default (from SwarmSDK::Defaults)

API Key Proxying

API keys are automatically proxied to RubyLLM.config when set, ensuring RubyLLM always has the correct credentials.

Examples:

Basic configuration

SwarmSDK.configure do |config|
  config.openai_api_key = "sk-..."
  config.default_model = "claude-sonnet-4"
  config.agent_request_timeout = 600
end

Testing setup

def setup
  SwarmSDK.reset_config!
end

Constant Summary collapse

API_KEY_MAPPINGS =

API keys that proxy to RubyLLM.config Maps SwarmSDK config key => [RubyLLM config key, ENV variable]

{
  openai_api_key: [:openai_api_key, "OPENAI_API_KEY"],
  openai_api_base: [:openai_api_base, "OPENAI_API_BASE"],
  openai_organization_id: [:openai_organization_id, "OPENAI_ORG_ID"],
  openai_project_id: [:openai_project_id, "OPENAI_PROJECT_ID"],
  anthropic_api_key: [:anthropic_api_key, "ANTHROPIC_API_KEY"],
  gemini_api_key: [:gemini_api_key, "GEMINI_API_KEY"],
  gemini_api_base: [:gemini_api_base, "GEMINI_API_BASE"],
  vertexai_project_id: [:vertexai_project_id, "GOOGLE_CLOUD_PROJECT"],
  vertexai_location: [:vertexai_location, "GOOGLE_CLOUD_LOCATION"],
  deepseek_api_key: [:deepseek_api_key, "DEEPSEEK_API_KEY"],
  mistral_api_key: [:mistral_api_key, "MISTRAL_API_KEY"],
  perplexity_api_key: [:perplexity_api_key, "PERPLEXITY_API_KEY"],
  openrouter_api_key: [:openrouter_api_key, "OPENROUTER_API_KEY"],
  bedrock_api_key: [:bedrock_api_key, "AWS_ACCESS_KEY_ID"],
  bedrock_secret_key: [:bedrock_secret_key, "AWS_SECRET_ACCESS_KEY"],
  bedrock_region: [:bedrock_region, "AWS_REGION"],
  bedrock_session_token: [:bedrock_session_token, "AWS_SESSION_TOKEN"],
  ollama_api_base: [:ollama_api_base, "OLLAMA_API_BASE"],
  gpustack_api_base: [:gpustack_api_base, "GPUSTACK_API_BASE"],
  gpustack_api_key: [:gpustack_api_key, "GPUSTACK_API_KEY"],
}.freeze
DEFAULTS_MAPPINGS =

SwarmSDK defaults that can be overridden Maps config key => [ENV variable, default proc]

{
  default_model: ["SWARM_SDK_DEFAULT_MODEL", -> { Defaults::Agent::MODEL }],
  default_provider: ["SWARM_SDK_DEFAULT_PROVIDER", -> { Defaults::Agent::PROVIDER }],
  agent_request_timeout: ["SWARM_SDK_AGENT_REQUEST_TIMEOUT", -> { Defaults::Timeouts::AGENT_REQUEST_SECONDS }],
  bash_command_timeout: ["SWARM_SDK_BASH_COMMAND_TIMEOUT", -> { Defaults::Timeouts::BASH_COMMAND_MS }],
  bash_command_max_timeout: ["SWARM_SDK_BASH_COMMAND_MAX_TIMEOUT", -> { Defaults::Timeouts::BASH_COMMAND_MAX_MS }],
  web_fetch_timeout: ["SWARM_SDK_WEB_FETCH_TIMEOUT", -> { Defaults::Timeouts::WEB_FETCH_SECONDS }],
  hook_shell_timeout: ["SWARM_SDK_HOOK_SHELL_TIMEOUT", -> { Defaults::Timeouts::HOOK_SHELL_SECONDS }],
  transformer_command_timeout: ["SWARM_SDK_TRANSFORMER_COMMAND_TIMEOUT", -> { Defaults::Timeouts::TRANSFORMER_COMMAND_SECONDS }],
  global_concurrency_limit: ["SWARM_SDK_GLOBAL_CONCURRENCY_LIMIT", -> { Defaults::Concurrency::GLOBAL_LIMIT }],
  local_concurrency_limit: ["SWARM_SDK_LOCAL_CONCURRENCY_LIMIT", -> { Defaults::Concurrency::LOCAL_LIMIT }],
  output_character_limit: ["SWARM_SDK_OUTPUT_CHARACTER_LIMIT", -> { Defaults::Limits::OUTPUT_CHARACTERS }],
  read_line_limit: ["SWARM_SDK_READ_LINE_LIMIT", -> { Defaults::Limits::READ_LINES }],
  line_character_limit: ["SWARM_SDK_LINE_CHARACTER_LIMIT", -> { Defaults::Limits::LINE_CHARACTERS }],
  web_fetch_character_limit: ["SWARM_SDK_WEB_FETCH_CHARACTER_LIMIT", -> { Defaults::Limits::WEB_FETCH_CHARACTERS }],
  glob_result_limit: ["SWARM_SDK_GLOB_RESULT_LIMIT", -> { Defaults::Limits::GLOB_RESULTS }],
  scratchpad_entry_size_limit: ["SWARM_SDK_SCRATCHPAD_ENTRY_SIZE_LIMIT", -> { Defaults::Storage::ENTRY_SIZE_BYTES }],
  scratchpad_total_size_limit: ["SWARM_SDK_SCRATCHPAD_TOTAL_SIZE_LIMIT", -> { Defaults::Storage::TOTAL_SIZE_BYTES }],
  context_compression_threshold: ["SWARM_SDK_CONTEXT_COMPRESSION_THRESHOLD", -> { Defaults::Context::COMPRESSION_THRESHOLD_PERCENT }],
  todowrite_reminder_interval: ["SWARM_SDK_TODOWRITE_REMINDER_INTERVAL", -> { Defaults::Context::TODOWRITE_REMINDER_INTERVAL }],
  chars_per_token_prose: ["SWARM_SDK_CHARS_PER_TOKEN_PROSE", -> { Defaults::TokenEstimation::CHARS_PER_TOKEN_PROSE }],
  chars_per_token_code: ["SWARM_SDK_CHARS_PER_TOKEN_CODE", -> { Defaults::TokenEstimation::CHARS_PER_TOKEN_CODE }],
  mcp_log_level: ["SWARM_SDK_MCP_LOG_LEVEL", -> { Defaults::Logging::MCP_LOG_LEVEL }],
}.freeze
SETTINGS_MAPPINGS =

WebFetch and control settings Maps config key => [ENV variable, default value]

{
  webfetch_provider: ["SWARM_SDK_WEBFETCH_PROVIDER", nil],
  webfetch_model: ["SWARM_SDK_WEBFETCH_MODEL", nil],
  webfetch_base_url: ["SWARM_SDK_WEBFETCH_BASE_URL", nil],
  webfetch_max_tokens: ["SWARM_SDK_WEBFETCH_MAX_TOKENS", 4096],
  allow_filesystem_tools: ["SWARM_SDK_ALLOW_FILESYSTEM_TOOLS", true],
  env_interpolation: ["SWARM_SDK_ENV_INTERPOLATION", true],
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfig

Note:

Use Config.instance instead of new for the singleton pattern

Initialize a new Config instance



119
120
121
122
123
124
# File 'lib/swarm_sdk/config.rb', line 119

def initialize
  @explicit_values = {}
  @env_values = {}
  @env_loaded = false
  @env_mutex = Mutex.new
end

Class Method Details

.instanceConfig

Get the singleton Config instance

Returns:

  • (Config)

    The singleton instance



101
102
103
# File 'lib/swarm_sdk/config.rb', line 101

def instance
  @instance ||= new
end

.reset!void

This method returns an undefined value.

Reset the Config instance

Clears all configuration including explicit values and cached ENV values. Use in tests to ensure clean state.



111
112
113
# File 'lib/swarm_sdk/config.rb', line 111

def reset!
  @instance = nil
end

Instance Method Details

#allow_filesystem_toolsBoolean

Get whether filesystem tools are allowed

Returns:

  • (Boolean)

    true if allowed



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/swarm_sdk/config.rb', line 187

SETTINGS_MAPPINGS.each_key do |config_key|
  _env_key, default_value = SETTINGS_MAPPINGS[config_key]

  # Getter with default fallback
  define_method(config_key) do
    ensure_env_loaded!
    if @explicit_values.key?(config_key)
      @explicit_values[config_key]
    elsif @env_values.key?(config_key)
      @env_values[config_key]
    else
      default_value
    end
  end

  # Setter
  define_method("#{config_key}=") do |value|
    @explicit_values[config_key] = value
  end
end

#default_modelString

Get the default model

Returns:

  • (String)

    The default model (falls back to Defaults::Agent::MODEL)



162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/swarm_sdk/config.rb', line 162

DEFAULTS_MAPPINGS.each_key do |config_key|
  _env_key, default_proc = DEFAULTS_MAPPINGS[config_key]

  # Getter with default fallback
  define_method(config_key) do
    ensure_env_loaded!
    @explicit_values[config_key] || @env_values[config_key] || default_proc.call
  end

  # Setter
  define_method("#{config_key}=") do |value|
    @explicit_values[config_key] = value
  end
end

#default_model=(value) ⇒ Object

Set the default model

Parameters:

  • value (String)

    The default model



162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/swarm_sdk/config.rb', line 162

DEFAULTS_MAPPINGS.each_key do |config_key|
  _env_key, default_proc = DEFAULTS_MAPPINGS[config_key]

  # Getter with default fallback
  define_method(config_key) do
    ensure_env_loaded!
    @explicit_values[config_key] || @env_values[config_key] || default_proc.call
  end

  # Setter
  define_method("#{config_key}=") do |value|
    @explicit_values[config_key] = value
  end
end

#openai_api_keyString?

Get the OpenAI API key

Returns:

  • (String, nil)

    The API key



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/swarm_sdk/config.rb', line 136

API_KEY_MAPPINGS.each_key do |config_key|
  ruby_llm_key, _ = API_KEY_MAPPINGS[config_key]

  # Getter
  define_method(config_key) do
    ensure_env_loaded!
    @explicit_values[config_key] || @env_values[config_key]
  end

  # Setter with RubyLLM proxying
  define_method("#{config_key}=") do |value|
    @explicit_values[config_key] = value
    RubyLLM.config.public_send("#{ruby_llm_key}=", value) if value
  end
end

#openai_api_key=(value) ⇒ Object

Set the OpenAI API key (proxied to RubyLLM)

Parameters:

  • value (String)

    The API key



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/swarm_sdk/config.rb', line 136

API_KEY_MAPPINGS.each_key do |config_key|
  ruby_llm_key, _ = API_KEY_MAPPINGS[config_key]

  # Getter
  define_method(config_key) do
    ensure_env_loaded!
    @explicit_values[config_key] || @env_values[config_key]
  end

  # Setter with RubyLLM proxying
  define_method("#{config_key}=") do |value|
    @explicit_values[config_key] = value
    RubyLLM.config.public_send("#{ruby_llm_key}=", value) if value
  end
end

#webfetch_llm_enabled?Boolean

Check if WebFetch LLM processing is enabled

WebFetch uses LLM processing when both provider and model are configured.

Returns:

  • (Boolean)

    true if WebFetch LLM is configured



215
216
217
# File 'lib/swarm_sdk/config.rb', line 215

def webfetch_llm_enabled?
  !webfetch_provider.nil? && !webfetch_model.nil?
end

#webfetch_providerString?

Get the WebFetch LLM provider

Returns:

  • (String, nil)

    The provider



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/swarm_sdk/config.rb', line 187

SETTINGS_MAPPINGS.each_key do |config_key|
  _env_key, default_value = SETTINGS_MAPPINGS[config_key]

  # Getter with default fallback
  define_method(config_key) do
    ensure_env_loaded!
    if @explicit_values.key?(config_key)
      @explicit_values[config_key]
    elsif @env_values.key?(config_key)
      @env_values[config_key]
    else
      default_value
    end
  end

  # Setter
  define_method("#{config_key}=") do |value|
    @explicit_values[config_key] = value
  end
end