Class: Mana::Config

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

Overview

Central configuration for Mana. Set via Mana.configure { |c| ... }.

Key options:

model            - LLM model name (default: claude-sonnet-4-6)
api_key          - API key, falls back to ANTHROPIC_API_KEY or OPENAI_API_KEY env vars
base_url         - Custom API endpoint, falls back to ANTHROPIC_API_URL or OPENAI_API_URL
backend          - :anthropic, :openai, or nil (auto-detect from model name)
timeout          - HTTP timeout in seconds (default: 120)

Constant Summary collapse

DEFAULT_ANTHROPIC_URL =
"https://api.anthropic.com"
DEFAULT_OPENAI_URL =
"https://api.openai.com"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfig

All config options can be set via environment variables:

MANA_MODEL, MANA_VERBOSE, MANA_TIMEOUT, MANA_BACKEND
ANTHROPIC_API_KEY / OPENAI_API_KEY
ANTHROPIC_API_URL / OPENAI_API_URL


31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/mana/config.rb', line 31

def initialize
  @model = ENV["MANA_MODEL"] || "claude-sonnet-4-6"
  @temperature = 0
  @api_key = ENV["ANTHROPIC_API_KEY"] || ENV["OPENAI_API_KEY"]
  @max_iterations = 50
  @base_url = ENV["ANTHROPIC_API_URL"] || ENV["OPENAI_API_URL"]
  self.timeout = (ENV["MANA_TIMEOUT"] || 120).to_i
  @verbose = %w[1 true yes].include?(ENV["MANA_VERBOSE"]&.downcase)
  @backend = ENV["MANA_BACKEND"]&.to_sym
  @namespace = nil
  @memory_store = nil
  @memory_path = nil
  @context_window = 128_000
  @context_class = nil         # nil = use Mana::Context; set to custom class
  @knowledge_provider = nil    # nil = use Mana::Knowledge; set to custom module with .query(topic)
end

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.



13
14
15
# File 'lib/mana/config.rb', line 13

def api_key
  @api_key
end

#backendObject

Returns the value of attribute backend.



13
14
15
# File 'lib/mana/config.rb', line 13

def backend
  @backend
end

#base_urlObject

Returns the value of attribute base_url.



13
14
15
# File 'lib/mana/config.rb', line 13

def base_url
  @base_url
end

#context_classObject Also known as: memory_class

Returns the value of attribute context_class.



13
14
15
# File 'lib/mana/config.rb', line 13

def context_class
  @context_class
end

#context_windowObject

Returns the value of attribute context_window.



13
14
15
# File 'lib/mana/config.rb', line 13

def context_window
  @context_window
end

#knowledge_providerObject

Returns the value of attribute knowledge_provider.



13
14
15
# File 'lib/mana/config.rb', line 13

def knowledge_provider
  @knowledge_provider
end

#max_iterationsObject

Returns the value of attribute max_iterations.



13
14
15
# File 'lib/mana/config.rb', line 13

def max_iterations
  @max_iterations
end

#memory_pathObject

Returns the value of attribute memory_path.



13
14
15
# File 'lib/mana/config.rb', line 13

def memory_path
  @memory_path
end

#memory_storeObject

Returns the value of attribute memory_store.



13
14
15
# File 'lib/mana/config.rb', line 13

def memory_store
  @memory_store
end

#modelObject

Returns the value of attribute model.



13
14
15
# File 'lib/mana/config.rb', line 13

def model
  @model
end

#namespaceObject

Returns the value of attribute namespace.



13
14
15
# File 'lib/mana/config.rb', line 13

def namespace
  @namespace
end

#temperatureObject

Returns the value of attribute temperature.



13
14
15
# File 'lib/mana/config.rb', line 13

def temperature
  @temperature
end

#timeoutObject

Returns the value of attribute timeout.



18
19
20
# File 'lib/mana/config.rb', line 18

def timeout
  @timeout
end

#verboseObject

Returns the value of attribute verbose.



13
14
15
# File 'lib/mana/config.rb', line 13

def verbose
  @verbose
end

Instance Method Details

#effective_base_urlObject

Resolve the effective base URL based on the configured or auto-detected backend. Falls back to the appropriate default when no explicit URL is set.



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/mana/config.rb', line 59

def effective_base_url
  # Return user-configured URL if explicitly set
  return @base_url if @base_url

  # Otherwise pick the default URL based on backend type
  if anthropic_backend?
    DEFAULT_ANTHROPIC_URL
  else
    DEFAULT_OPENAI_URL
  end
end

#validate!Object

Validate configuration and raise early if something is wrong. Called automatically by Mana.configure, or manually via Mana.config.validate!



73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/mana/config.rb', line 73

def validate!
  if @api_key.nil? || @api_key.to_s.strip.empty?
    raise ConfigError,
      "API key is not configured. Set it via environment variable or Mana.configure:\n\n" \
      "  export ANTHROPIC_API_KEY=your_key_here\n" \
      "  # or\n" \
      "  export OPENAI_API_KEY=your_key_here\n" \
      "  # or\n" \
      "  Mana.configure { |c| c.api_key = \"your_key_here\" }\n"
  end
  true
end