Class: SavvyOpenrouter::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/savvy_openrouter/configuration.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config_path: nil, **options) ⇒ Configuration

Precedence: explicit keyword args (highest) > YAML file > ENV (lowest).



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/savvy_openrouter/configuration.rb', line 27

def initialize(config_path: nil, **options)
  @defaults = {}
  @video_defaults = {}
  @responses_defaults = {}
  @api_call_log = {}
  @chat_retries = {}
  load_from_env!
  yaml_path = config_path || self.class.discover_config_file
  merge_hash!(self.class.load_file(yaml_path)) if yaml_path
  apply_options!(options)
end

Instance Attribute Details

#api_call_logObject (readonly)

Returns the value of attribute api_call_log.



8
9
10
# File 'lib/savvy_openrouter/configuration.rb', line 8

def api_call_log
  @api_call_log
end

#api_keyObject

Returns the value of attribute api_key.



7
8
9
# File 'lib/savvy_openrouter/configuration.rb', line 7

def api_key
  @api_key
end

#app_titleObject

Returns the value of attribute app_title.



7
8
9
# File 'lib/savvy_openrouter/configuration.rb', line 7

def app_title
  @app_title
end

#base_urlObject

Returns the value of attribute base_url.



7
8
9
# File 'lib/savvy_openrouter/configuration.rb', line 7

def base_url
  @base_url
end

#chat_retriesObject (readonly)

Returns the value of attribute chat_retries.



8
9
10
# File 'lib/savvy_openrouter/configuration.rb', line 8

def chat_retries
  @chat_retries
end

#default_modelObject Also known as: llm_model

Returns the value of attribute default_model.



7
8
9
# File 'lib/savvy_openrouter/configuration.rb', line 7

def default_model
  @default_model
end

#defaultsObject (readonly)

Returns the value of attribute defaults.



8
9
10
# File 'lib/savvy_openrouter/configuration.rb', line 8

def defaults
  @defaults
end

#http_refererObject

Returns the value of attribute http_referer.



7
8
9
# File 'lib/savvy_openrouter/configuration.rb', line 7

def http_referer
  @http_referer
end

#responses_defaultsObject (readonly)

Returns the value of attribute responses_defaults.



8
9
10
# File 'lib/savvy_openrouter/configuration.rb', line 8

def responses_defaults
  @responses_defaults
end

#video_defaultsObject (readonly)

Returns the value of attribute video_defaults.



8
9
10
# File 'lib/savvy_openrouter/configuration.rb', line 8

def video_defaults
  @video_defaults
end

Class Method Details

.discover_config_file(cwd = Dir.pwd) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/savvy_openrouter/configuration.rb', line 39

def self.discover_config_file(cwd = Dir.pwd)
  %w[config/savvy_openrouter.yml .savvy_openrouter.yml].each do |rel|
    path = File.join(cwd, rel)
    return path if File.file?(path)
  end
  nil
end

.load_file(path) ⇒ Object



13
14
15
16
17
18
# File 'lib/savvy_openrouter/configuration.rb', line 13

def self.load_file(path)
  return {} unless path && File.file?(path)

  data = YAML.safe_load(File.read(path), permitted_classes: [], permitted_symbols: [], aliases: true)
  data.is_a?(Hash) ? stringify_keys_static(data) : {}
end

.stringify_keys_static(hash) ⇒ Object



20
21
22
23
24
# File 'lib/savvy_openrouter/configuration.rb', line 20

def self.stringify_keys_static(hash)
  hash.each_with_object({}) do |(k, v), acc|
    acc[k.to_s] = v.is_a?(Hash) ? stringify_keys_static(v) : v
  end
end

Instance Method Details

#merge_chat_body(body) ⇒ Object



66
67
68
69
70
71
# File 'lib/savvy_openrouter/configuration.rb', line 66

def merge_chat_body(body)
  body = stringify_keys(body)
  merged = @defaults.merge(body)
  merged["model"] ||= @default_model if @default_model && merged["model"].nil?
  merged
end

#merge_hash!(hash) ⇒ Object



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

def merge_hash!(hash)
  return unless hash.is_a?(Hash)

  hash = self.class.stringify_keys_static(hash)
  @api_key = hash["api_key"] if hash.key?("api_key")
  @base_url = hash["base_url"] if hash.key?("base_url")
  @default_model = hash["default_model"] || hash["llm_model"] if hash.key?("default_model") || hash.key?("llm_model")
  @http_referer = hash["http_referer"] if hash.key?("http_referer")
  @app_title = hash["app_title"] || hash["x_title"] if hash.key?("app_title") || hash.key?("x_title")
  @defaults = @defaults.merge(self.class.stringify_keys_static(hash["defaults"] || {}))
  vd = hash["video_defaults"] || hash["defaults_video"]
  @video_defaults = @video_defaults.merge(self.class.stringify_keys_static(vd || {}))
  rd = hash["responses_defaults"] || hash["defaults_responses"]
  @responses_defaults = @responses_defaults.merge(self.class.stringify_keys_static(rd || {}))
  assign_api_call_log(hash["api_call_log"]) if hash.key?("api_call_log")
  assign_chat_retries(hash["chat_retries"]) if hash.key?("chat_retries")
  assign_chat_retries(hash["completion_retries"]) if hash.key?("completion_retries")
end

#merge_responses_body(body) ⇒ Object

Defaults only for ‘POST /responses` (Responses API beta): `plugins`, `tools`, `max_output_tokens`, `x_search_filter`, etc. Keeps web-search settings off chat/embeddings bodies.



82
83
84
85
86
87
# File 'lib/savvy_openrouter/configuration.rb', line 82

def merge_responses_body(body)
  body = stringify_keys(body)
  merged = @responses_defaults.merge(body)
  merged["model"] ||= @default_model if @default_model && merged["model"].nil?
  merged
end

#merge_video_body(body) ⇒ Object



73
74
75
76
77
78
# File 'lib/savvy_openrouter/configuration.rb', line 73

def merge_video_body(body)
  body = stringify_keys(body)
  merged = @video_defaults.merge(body)
  merged["model"] ||= @default_model if @default_model && merged["model"].nil?
  merged
end