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
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.
-
#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
-
#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_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.
- #validate! ⇒ Object
Constructor Details
#initialize ⇒ Configuration
Returns a new instance of Configuration.
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/rails_console_ai/configuration.rb', line 36 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 = {} end |
Instance Attribute Details
#admin_password ⇒ Object
Returns the value of attribute admin_password.
21 22 23 |
# File 'lib/rails_console_ai/configuration.rb', line 21 def admin_password @admin_password end |
#admin_username ⇒ Object
Returns the value of attribute admin_username.
21 22 23 |
# File 'lib/rails_console_ai/configuration.rb', line 21 def admin_username @admin_username end |
#api_key ⇒ Object
Returns the value of attribute api_key.
21 22 23 |
# File 'lib/rails_console_ai/configuration.rb', line 21 def api_key @api_key end |
#authenticate ⇒ Object
Returns the value of attribute authenticate.
21 22 23 |
# File 'lib/rails_console_ai/configuration.rb', line 21 def authenticate @authenticate end |
#auto_execute ⇒ Object
Returns the value of attribute auto_execute.
21 22 23 |
# File 'lib/rails_console_ai/configuration.rb', line 21 def auto_execute @auto_execute end |
#bedrock_region ⇒ Object
Returns the value of attribute bedrock_region.
21 22 23 |
# File 'lib/rails_console_ai/configuration.rb', line 21 def bedrock_region @bedrock_region end |
#bypass_guards_for_methods ⇒ Object
Returns the value of attribute bypass_guards_for_methods.
21 22 23 |
# File 'lib/rails_console_ai/configuration.rb', line 21 def bypass_guards_for_methods @bypass_guards_for_methods end |
#channels ⇒ Object
Returns the value of attribute channels.
21 22 23 |
# File 'lib/rails_console_ai/configuration.rb', line 21 def channels @channels end |
#code_search_paths ⇒ Object
Returns the value of attribute code_search_paths.
21 22 23 |
# File 'lib/rails_console_ai/configuration.rb', line 21 def code_search_paths @code_search_paths end |
#connection_class ⇒ Object
Returns the value of attribute connection_class.
21 22 23 |
# File 'lib/rails_console_ai/configuration.rb', line 21 def connection_class @connection_class end |
#debug ⇒ Object
Returns the value of attribute debug.
21 22 23 |
# File 'lib/rails_console_ai/configuration.rb', line 21 def debug @debug end |
#local_api_key ⇒ Object
Returns the value of attribute local_api_key.
21 22 23 |
# File 'lib/rails_console_ai/configuration.rb', line 21 def local_api_key @local_api_key end |
#local_model ⇒ Object
Returns the value of attribute local_model.
21 22 23 |
# File 'lib/rails_console_ai/configuration.rb', line 21 def local_model @local_model end |
#local_url ⇒ Object
Returns the value of attribute local_url.
21 22 23 |
# File 'lib/rails_console_ai/configuration.rb', line 21 def local_url @local_url end |
#max_tokens ⇒ Object
Returns the value of attribute max_tokens.
21 22 23 |
# File 'lib/rails_console_ai/configuration.rb', line 21 def max_tokens @max_tokens end |
#max_tool_rounds ⇒ Object
Returns the value of attribute max_tool_rounds.
21 22 23 |
# File 'lib/rails_console_ai/configuration.rb', line 21 def max_tool_rounds @max_tool_rounds end |
#memories_enabled ⇒ Object
Returns the value of attribute memories_enabled.
21 22 23 |
# File 'lib/rails_console_ai/configuration.rb', line 21 def memories_enabled @memories_enabled end |
#model ⇒ Object
Returns the value of attribute model.
21 22 23 |
# File 'lib/rails_console_ai/configuration.rb', line 21 def model @model end |
#provider ⇒ Object
Returns the value of attribute provider.
21 22 23 |
# File 'lib/rails_console_ai/configuration.rb', line 21 def provider @provider end |
#session_logging ⇒ Object
Returns the value of attribute session_logging.
21 22 23 |
# File 'lib/rails_console_ai/configuration.rb', line 21 def session_logging @session_logging end |
#slack_allowed_usernames ⇒ Object
Returns the value of attribute slack_allowed_usernames.
21 22 23 |
# File 'lib/rails_console_ai/configuration.rb', line 21 def slack_allowed_usernames @slack_allowed_usernames end |
#slack_app_token ⇒ Object
Returns the value of attribute slack_app_token.
21 22 23 |
# File 'lib/rails_console_ai/configuration.rb', line 21 def slack_app_token @slack_app_token end |
#slack_bot_token ⇒ Object
Returns the value of attribute slack_bot_token.
21 22 23 |
# File 'lib/rails_console_ai/configuration.rb', line 21 def slack_bot_token @slack_bot_token end |
#slack_channel_ids ⇒ Object
Returns the value of attribute slack_channel_ids.
21 22 23 |
# File 'lib/rails_console_ai/configuration.rb', line 21 def slack_channel_ids @slack_channel_ids end |
#storage_adapter ⇒ Object
Returns the value of attribute storage_adapter.
21 22 23 |
# File 'lib/rails_console_ai/configuration.rb', line 21 def storage_adapter @storage_adapter end |
#temperature ⇒ Object
Returns the value of attribute temperature.
21 22 23 |
# File 'lib/rails_console_ai/configuration.rb', line 21 def temperature @temperature end |
#thinking_model ⇒ Object
Returns the value of attribute thinking_model.
21 22 23 |
# File 'lib/rails_console_ai/configuration.rb', line 21 def thinking_model @thinking_model end |
#timeout ⇒ Object
Returns the value of attribute timeout.
21 22 23 |
# File 'lib/rails_console_ai/configuration.rb', line 21 def timeout @timeout end |
#user_extra_info ⇒ Object
Returns the value of attribute user_extra_info.
21 22 23 |
# File 'lib/rails_console_ai/configuration.rb', line 21 def user_extra_info @user_extra_info end |
Instance Method Details
#resolve_user_extra_info(username) ⇒ Object
69 70 71 72 |
# File 'lib/rails_console_ai/configuration.rb', line 69 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
119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/rails_console_ai/configuration.rb', line 119 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
149 150 151 152 153 |
# File 'lib/rails_console_ai/configuration.rb', line 149 def resolved_max_tokens return @max_tokens if @max_tokens DEFAULT_MAX_TOKENS.fetch(resolved_model, 4096) end |
#resolved_model ⇒ Object
134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/rails_console_ai/configuration.rb', line 134 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_thinking_model ⇒ Object
155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/rails_console_ai/configuration.rb', line 155 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
170 171 172 |
# File 'lib/rails_console_ai/configuration.rb', line 170 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
89 90 91 |
# File 'lib/rails_console_ai/configuration.rb', line 89 def safety_guard(name, &block) safety_guards.add(name, &block) end |
#safety_guards ⇒ Object
74 75 76 77 78 79 |
# File 'lib/rails_console_ai/configuration.rb', line 74 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")
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/rails_console_ai/configuration.rb', line 100 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(guard_name, key) } end end |
#validate! ⇒ Object
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/rails_console_ai/configuration.rb', line 174 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 |