Class: RailsConsoleAi::Configuration
- Inherits:
-
Object
- Object
- RailsConsoleAi::Configuration
- Defined in:
- lib/rails_console_ai/configuration.rb
Constant Summary collapse
- PROVIDERS =
%i[anthropic openai local bedrock].freeze
- PRICING =
cache_read: 0.1x input, cache_write: 1.25x input for Anthropic models
{ 'claude-sonnet-4-6' => { input: 3.0 / 1_000_000, output: 15.0 / 1_000_000, cache_read: 0.30 / 1_000_000, cache_write: 3.75 / 1_000_000 }, 'claude-opus-4-6' => { input: 15.0 / 1_000_000, output: 75.0 / 1_000_000, cache_read: 1.50 / 1_000_000, cache_write: 18.75 / 1_000_000 }, 'claude-haiku-4-5-20251001' => { input: 0.80 / 1_000_000, output: 4.0 / 1_000_000, cache_read: 0.08 / 1_000_000, cache_write: 1.0 / 1_000_000 }, # Bedrock model IDs (same pricing as direct API) 'us.anthropic.claude-sonnet-4-6' => { input: 3.0 / 1_000_000, output: 15.0 / 1_000_000, cache_read: 0.30 / 1_000_000, cache_write: 3.75 / 1_000_000 }, 'us.anthropic.claude-opus-4-6-v1' => { input: 15.0 / 1_000_000, output: 75.0 / 1_000_000, cache_read: 1.50 / 1_000_000, cache_write: 18.75 / 1_000_000 }, }.freeze
- DEFAULT_MAX_TOKENS =
{ 'claude-sonnet-4-6' => 16_000, 'claude-haiku-4-5-20251001' => 16_000, 'claude-opus-4-6' => 4_096, }.freeze
- MODELS_WITHOUT_TEMPERATURE =
Models that reject the ‘temperature` parameter. Configuration#resolved_temperature returns nil for these so providers can omit the field from the request.
Set.new(%w[ claude-opus-4-7 anthropic.claude-opus-4-7 us.anthropic.claude-opus-4-7 eu.anthropic.claude-opus-4-7 jp.anthropic.claude-opus-4-7 global.anthropic.claude-opus-4-7 ]).freeze
Instance Attribute Summary collapse
-
#admin_password ⇒ Object
Returns the value of attribute admin_password.
-
#admin_username ⇒ Object
Returns the value of attribute admin_username.
-
#api_key ⇒ Object
Returns the value of attribute api_key.
-
#authenticate ⇒ Object
Returns the value of attribute authenticate.
-
#auto_execute ⇒ Object
Returns the value of attribute auto_execute.
-
#bedrock_region ⇒ Object
Returns the value of attribute bedrock_region.
-
#bypass_guards_for_methods ⇒ Object
Returns the value of attribute bypass_guards_for_methods.
-
#channels ⇒ Object
Returns the value of attribute channels.
-
#code_search_paths ⇒ Object
Returns the value of attribute code_search_paths.
-
#connection_class ⇒ Object
Returns the value of attribute connection_class.
-
#debug ⇒ Object
Returns the value of attribute debug.
-
#local_api_key ⇒ Object
Returns the value of attribute local_api_key.
-
#local_model ⇒ Object
Returns the value of attribute local_model.
-
#local_url ⇒ Object
Returns the value of attribute local_url.
-
#max_tokens ⇒ Object
Returns the value of attribute max_tokens.
-
#max_tool_rounds ⇒ Object
Returns the value of attribute max_tool_rounds.
-
#memories_enabled ⇒ Object
Returns the value of attribute memories_enabled.
-
#model ⇒ Object
Returns the value of attribute model.
-
#provider ⇒ Object
Returns the value of attribute provider.
-
#session_logging ⇒ Object
Returns the value of attribute session_logging.
-
#slack_allowed_usernames ⇒ Object
Returns the value of attribute slack_allowed_usernames.
-
#slack_app_token ⇒ Object
Returns the value of attribute slack_app_token.
-
#slack_bot_token ⇒ Object
Returns the value of attribute slack_bot_token.
-
#slack_channel_ids ⇒ Object
Returns the value of attribute slack_channel_ids.
-
#storage_adapter ⇒ Object
Returns the value of attribute storage_adapter.
-
#sub_agent_max_rounds ⇒ Object
Returns the value of attribute sub_agent_max_rounds.
-
#sub_agent_model ⇒ Object
Returns the value of attribute sub_agent_model.
-
#temperature ⇒ Object
Returns the value of attribute temperature.
-
#thinking_model ⇒ Object
Returns the value of attribute thinking_model.
-
#timeout ⇒ Object
Returns the value of attribute timeout.
-
#user_extra_info ⇒ Object
Returns the value of attribute user_extra_info.
Instance Method Summary collapse
-
#channel_setting(mode, key) ⇒ Object
Look up a per-channel setting with backward compatibility.
-
#initialize ⇒ Configuration
constructor
A new instance of Configuration.
- #resolve_user_extra_info(username) ⇒ Object
- #resolved_api_key ⇒ Object
- #resolved_max_tokens ⇒ Object
- #resolved_model ⇒ Object
-
#resolved_temperature ⇒ Object
Returns nil for models that reject the ‘temperature` parameter (e.g. opus-4-7).
- #resolved_thinking_model ⇒ Object
- #resolved_timeout ⇒ Object
-
#safety_guard(name, &block) ⇒ Object
Register a custom safety guard by name with an around-block.
- #safety_guards ⇒ Object
-
#use_builtin_safety_guard(name, allow: nil) ⇒ Object
Register a built-in safety guard by name.
-
#username_allowed?(mode, key, username) ⇒ Boolean
Check if a username is permitted by a channel setting.
- #validate! ⇒ Object
Constructor Details
#initialize ⇒ Configuration
Returns a new instance of Configuration.
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/rails_console_ai/configuration.rb', line 51 def initialize @provider = :anthropic @api_key = nil @model = nil @thinking_model = nil @max_tokens = nil @auto_execute = false @temperature = 0.2 @timeout = 30 @debug = false @max_tool_rounds = 200 @storage_adapter = nil @memories_enabled = true @session_logging = true @connection_class = nil @admin_username = nil @admin_password = nil @authenticate = nil @safety_guards = nil @slack_bot_token = nil @slack_app_token = nil @slack_channel_ids = nil @slack_allowed_usernames = nil @local_url = 'http://localhost:11434' @local_model = 'qwen2.5:7b' @local_api_key = nil @bedrock_region = nil @code_search_paths = %w[app] @channels = {} @bypass_guards_for_methods = [] @user_extra_info = {} @sub_agent_max_rounds = 15 @sub_agent_model = nil end |
Instance Attribute Details
#admin_password ⇒ Object
Returns the value of attribute admin_password.
34 35 36 |
# File 'lib/rails_console_ai/configuration.rb', line 34 def admin_password @admin_password end |
#admin_username ⇒ Object
Returns the value of attribute admin_username.
34 35 36 |
# File 'lib/rails_console_ai/configuration.rb', line 34 def admin_username @admin_username end |
#api_key ⇒ Object
Returns the value of attribute api_key.
34 35 36 |
# File 'lib/rails_console_ai/configuration.rb', line 34 def api_key @api_key end |
#authenticate ⇒ Object
Returns the value of attribute authenticate.
34 35 36 |
# File 'lib/rails_console_ai/configuration.rb', line 34 def authenticate @authenticate end |
#auto_execute ⇒ Object
Returns the value of attribute auto_execute.
34 35 36 |
# File 'lib/rails_console_ai/configuration.rb', line 34 def auto_execute @auto_execute end |
#bedrock_region ⇒ Object
Returns the value of attribute bedrock_region.
34 35 36 |
# File 'lib/rails_console_ai/configuration.rb', line 34 def bedrock_region @bedrock_region end |
#bypass_guards_for_methods ⇒ Object
Returns the value of attribute bypass_guards_for_methods.
34 35 36 |
# File 'lib/rails_console_ai/configuration.rb', line 34 def bypass_guards_for_methods @bypass_guards_for_methods end |
#channels ⇒ Object
Returns the value of attribute channels.
34 35 36 |
# File 'lib/rails_console_ai/configuration.rb', line 34 def channels @channels end |
#code_search_paths ⇒ Object
Returns the value of attribute code_search_paths.
34 35 36 |
# File 'lib/rails_console_ai/configuration.rb', line 34 def code_search_paths @code_search_paths end |
#connection_class ⇒ Object
Returns the value of attribute connection_class.
34 35 36 |
# File 'lib/rails_console_ai/configuration.rb', line 34 def connection_class @connection_class end |
#debug ⇒ Object
Returns the value of attribute debug.
34 35 36 |
# File 'lib/rails_console_ai/configuration.rb', line 34 def debug @debug end |
#local_api_key ⇒ Object
Returns the value of attribute local_api_key.
34 35 36 |
# File 'lib/rails_console_ai/configuration.rb', line 34 def local_api_key @local_api_key end |
#local_model ⇒ Object
Returns the value of attribute local_model.
34 35 36 |
# File 'lib/rails_console_ai/configuration.rb', line 34 def local_model @local_model end |
#local_url ⇒ Object
Returns the value of attribute local_url.
34 35 36 |
# File 'lib/rails_console_ai/configuration.rb', line 34 def local_url @local_url end |
#max_tokens ⇒ Object
Returns the value of attribute max_tokens.
34 35 36 |
# File 'lib/rails_console_ai/configuration.rb', line 34 def max_tokens @max_tokens end |
#max_tool_rounds ⇒ Object
Returns the value of attribute max_tool_rounds.
34 35 36 |
# File 'lib/rails_console_ai/configuration.rb', line 34 def max_tool_rounds @max_tool_rounds end |
#memories_enabled ⇒ Object
Returns the value of attribute memories_enabled.
34 35 36 |
# File 'lib/rails_console_ai/configuration.rb', line 34 def memories_enabled @memories_enabled end |
#model ⇒ Object
Returns the value of attribute model.
34 35 36 |
# File 'lib/rails_console_ai/configuration.rb', line 34 def model @model end |
#provider ⇒ Object
Returns the value of attribute provider.
34 35 36 |
# File 'lib/rails_console_ai/configuration.rb', line 34 def provider @provider end |
#session_logging ⇒ Object
Returns the value of attribute session_logging.
34 35 36 |
# File 'lib/rails_console_ai/configuration.rb', line 34 def session_logging @session_logging end |
#slack_allowed_usernames ⇒ Object
Returns the value of attribute slack_allowed_usernames.
34 35 36 |
# File 'lib/rails_console_ai/configuration.rb', line 34 def slack_allowed_usernames @slack_allowed_usernames end |
#slack_app_token ⇒ Object
Returns the value of attribute slack_app_token.
34 35 36 |
# File 'lib/rails_console_ai/configuration.rb', line 34 def slack_app_token @slack_app_token end |
#slack_bot_token ⇒ Object
Returns the value of attribute slack_bot_token.
34 35 36 |
# File 'lib/rails_console_ai/configuration.rb', line 34 def slack_bot_token @slack_bot_token end |
#slack_channel_ids ⇒ Object
Returns the value of attribute slack_channel_ids.
34 35 36 |
# File 'lib/rails_console_ai/configuration.rb', line 34 def slack_channel_ids @slack_channel_ids end |
#storage_adapter ⇒ Object
Returns the value of attribute storage_adapter.
34 35 36 |
# File 'lib/rails_console_ai/configuration.rb', line 34 def storage_adapter @storage_adapter end |
#sub_agent_max_rounds ⇒ Object
Returns the value of attribute sub_agent_max_rounds.
34 35 36 |
# File 'lib/rails_console_ai/configuration.rb', line 34 def sub_agent_max_rounds @sub_agent_max_rounds end |
#sub_agent_model ⇒ Object
Returns the value of attribute sub_agent_model.
34 35 36 |
# File 'lib/rails_console_ai/configuration.rb', line 34 def sub_agent_model @sub_agent_model end |
#temperature ⇒ Object
Returns the value of attribute temperature.
34 35 36 |
# File 'lib/rails_console_ai/configuration.rb', line 34 def temperature @temperature end |
#thinking_model ⇒ Object
Returns the value of attribute thinking_model.
34 35 36 |
# File 'lib/rails_console_ai/configuration.rb', line 34 def thinking_model @thinking_model end |
#timeout ⇒ Object
Returns the value of attribute timeout.
34 35 36 |
# File 'lib/rails_console_ai/configuration.rb', line 34 def timeout @timeout end |
#user_extra_info ⇒ Object
Returns the value of attribute user_extra_info.
34 35 36 |
# File 'lib/rails_console_ai/configuration.rb', line 34 def user_extra_info @user_extra_info end |
Instance Method Details
#channel_setting(mode, key) ⇒ Object
Look up a per-channel setting with backward compatibility. Falls back to top-level slack_* config when channels hash doesn’t have the key.
93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/rails_console_ai/configuration.rb', line 93 def channel_setting(mode, key) channel_cfg = @channels[mode.to_s] || {} value = channel_cfg[key.to_s] # Backward compatibility: slack_allowed_usernames → channels.slack.allowed_usernames if value.nil? && mode.to_s == 'slack' && key.to_s == 'allowed_usernames' value = @slack_allowed_usernames end value end |
#resolve_user_extra_info(username) ⇒ Object
86 87 88 89 |
# File 'lib/rails_console_ai/configuration.rb', line 86 def resolve_user_extra_info(username) return nil if @user_extra_info.nil? || @user_extra_info.empty? || username.nil? @user_extra_info[username.to_s.downcase] end |
#resolved_api_key ⇒ Object
159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/rails_console_ai/configuration.rb', line 159 def resolved_api_key return @api_key if @api_key && !@api_key.empty? case @provider when :anthropic ENV['ANTHROPIC_API_KEY'] when :openai ENV['OPENAI_API_KEY'] when :local @local_api_key || 'no-key' when :bedrock 'aws-sdk' end end |
#resolved_max_tokens ⇒ Object
189 190 191 192 193 |
# File 'lib/rails_console_ai/configuration.rb', line 189 def resolved_max_tokens return @max_tokens if @max_tokens DEFAULT_MAX_TOKENS.fetch(resolved_model, 4096) end |
#resolved_model ⇒ Object
174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/rails_console_ai/configuration.rb', line 174 def resolved_model return @model if @model && !@model.empty? case @provider when :anthropic 'claude-sonnet-4-6' when :openai 'gpt-5.3-codex' when :local @local_model when :bedrock 'us.anthropic.claude-sonnet-4-6' end end |
#resolved_temperature ⇒ Object
Returns nil for models that reject the ‘temperature` parameter (e.g. opus-4-7). Providers should use this in place of @temperature.
197 198 199 200 |
# File 'lib/rails_console_ai/configuration.rb', line 197 def resolved_temperature return nil if MODELS_WITHOUT_TEMPERATURE.include?(resolved_model) @temperature end |
#resolved_thinking_model ⇒ Object
202 203 204 205 206 207 208 209 210 211 212 213 214 215 |
# File 'lib/rails_console_ai/configuration.rb', line 202 def resolved_thinking_model return @thinking_model if @thinking_model && !@thinking_model.empty? case @provider when :anthropic 'claude-opus-4-6' when :openai 'gpt-5.3-codex' when :local @local_model when :bedrock 'us.anthropic.claude-opus-4-6-v1' end end |
#resolved_timeout ⇒ Object
217 218 219 |
# File 'lib/rails_console_ai/configuration.rb', line 217 def resolved_timeout @provider == :local ? [@timeout, 300].max : @timeout end |
#safety_guard(name, &block) ⇒ Object
Register a custom safety guard by name with an around-block.
config.safety_guard :mailers do |&execute|
ActionMailer::Base.perform_deliveries = false
execute.call
ensure
ActionMailer::Base.perform_deliveries = true
end
129 130 131 |
# File 'lib/rails_console_ai/configuration.rb', line 129 def safety_guard(name, &block) safety_guards.add(name, &block) end |
#safety_guards ⇒ Object
114 115 116 117 118 119 |
# File 'lib/rails_console_ai/configuration.rb', line 114 def safety_guards @safety_guards ||= begin require 'rails_console_ai/safety_guards' SafetyGuards.new end end |
#use_builtin_safety_guard(name, allow: nil) ⇒ Object
Register a built-in safety guard by name. Available: :database_writes, :http_mutations, :mailers
Options:
allow: Array of strings or regexps to allowlist for this guard.
- :http_mutations → hosts (e.g. "s3.amazonaws.com", /googleapis\.com/)
- :database_writes → table names (e.g. "rails_console_ai_sessions")
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/rails_console_ai/configuration.rb', line 140 def use_builtin_safety_guard(name, allow: nil) require 'rails_console_ai/safety_guards' guard_name = name.to_sym case guard_name when :database_writes safety_guards.add(:database_writes, &BuiltinGuards.database_writes) when :http_mutations safety_guards.add(:http_mutations, &BuiltinGuards.http_mutations) when :mailers safety_guards.add(:mailers, &BuiltinGuards.mailers) else raise ConfigurationError, "Unknown built-in safety guard: #{name}. Available: database_writes, http_mutations, mailers" end if allow Array(allow).each { |key| safety_guards.allow_global(guard_name, key) } end end |
#username_allowed?(mode, key, username) ⇒ Boolean
Check if a username is permitted by a channel setting. Returns true when the setting is nil (not configured = no restriction).
107 108 109 110 111 112 |
# File 'lib/rails_console_ai/configuration.rb', line 107 def username_allowed?(mode, key, username) list = channel_setting(mode, key) return true if list.nil? normalized = Array(list).map(&:to_s).map(&:downcase) normalized.include?('all') || normalized.include?(username.to_s.downcase) end |
#validate! ⇒ Object
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 |
# File 'lib/rails_console_ai/configuration.rb', line 221 def validate! unless PROVIDERS.include?(@provider) raise ConfigurationError, "Unknown provider: #{@provider}. Valid: #{PROVIDERS.join(', ')}" end if @provider == :local raise ConfigurationError, "No local_url configured for :local provider." unless @local_url && !@local_url.empty? elsif @provider == :bedrock begin require 'aws-sdk-bedrockruntime' rescue LoadError raise ConfigurationError, "aws-sdk-bedrockruntime gem is required for the :bedrock provider. Add it to your Gemfile." end else unless resolved_api_key env_var = @provider == :anthropic ? 'ANTHROPIC_API_KEY' : 'OPENAI_API_KEY' raise ConfigurationError, "No API key. Set config.api_key or #{env_var} env var." end end end |