Module: RubyLLM::Agents::Budget::ConfigResolver Private
- Defined in:
- lib/ruby_llm/agents/infrastructure/budget/config_resolver.rb
Overview
This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.
Resolves budget configuration for tenants and global settings
Handles the resolution priority chain:
-
Runtime config passed to run()
-
tenant_config_resolver lambda
-
TenantBudget database record
-
Global configuration
Class Method Summary collapse
-
.global_budget_config(config) ⇒ Hash
private
Builds global budget config from configuration.
-
.lookup_tenant_budget(tenant_id) ⇒ TenantBudget?
private
Safely looks up tenant budget, handling missing table.
-
.normalize_budget_config(raw_config, global_config) ⇒ Hash
private
Normalizes runtime/resolver config to standard budget config format.
-
.reset_tenant_budget_table_check! ⇒ void
private
Resets the memoized tenant budget table existence check (useful for testing).
-
.resolve_budget_config(tenant_id, runtime_config: nil) ⇒ Hash
private
Resolves budget configuration for a tenant.
-
.resolve_tenant_id(explicit_tenant_id) ⇒ String?
private
Resolves the current tenant ID.
-
.tenant_budget_table_exists? ⇒ Boolean
private
Checks if the tenants table exists (supports old and new table names).
Class Method Details
.global_budget_config(config) ⇒ Hash
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Builds global budget config from configuration
80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/ruby_llm/agents/infrastructure/budget/config_resolver.rb', line 80 def global_budget_config(config) { enabled: config.budgets_enabled?, enforcement: config.budget_enforcement, global_daily: config.budgets&.dig(:global_daily), global_monthly: config.budgets&.dig(:global_monthly), per_agent_daily: config.budgets&.dig(:per_agent_daily), per_agent_monthly: config.budgets&.dig(:per_agent_monthly), global_daily_tokens: config.budgets&.dig(:global_daily_tokens), global_monthly_tokens: config.budgets&.dig(:global_monthly_tokens) } end |
.lookup_tenant_budget(tenant_id) ⇒ TenantBudget?
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Safely looks up tenant budget, handling missing table
119 120 121 122 123 124 125 126 |
# File 'lib/ruby_llm/agents/infrastructure/budget/config_resolver.rb', line 119 def lookup_tenant_budget(tenant_id) return nil unless tenant_budget_table_exists? TenantBudget.for_tenant(tenant_id) rescue => e Rails.logger.warn("[RubyLLM::Agents] Failed to lookup tenant budget: #{e.}") nil end |
.normalize_budget_config(raw_config, global_config) ⇒ Hash
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Normalizes runtime/resolver config to standard budget config format
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/ruby_llm/agents/infrastructure/budget/config_resolver.rb', line 98 def normalize_budget_config(raw_config, global_config) enforcement = raw_config[:enforcement]&.to_sym || global_config.budget_enforcement { enabled: enforcement != :none, enforcement: enforcement, # Cost/budget limits (USD) global_daily: raw_config[:daily_budget_limit], global_monthly: raw_config[:monthly_budget_limit], per_agent_daily: raw_config[:per_agent_daily] || {}, per_agent_monthly: raw_config[:per_agent_monthly] || {}, # Token limits global_daily_tokens: raw_config[:daily_token_limit], global_monthly_tokens: raw_config[:monthly_token_limit] } end |
.reset_tenant_budget_table_check! ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
Resets the memoized tenant budget table existence check (useful for testing)
144 145 146 |
# File 'lib/ruby_llm/agents/infrastructure/budget/config_resolver.rb', line 144 def reset_tenant_budget_table_check! remove_instance_variable(:@tenant_budget_table_exists) if defined?(@tenant_budget_table_exists) end |
.resolve_budget_config(tenant_id, runtime_config: nil) ⇒ Hash
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Resolves budget configuration for a tenant
Priority order:
-
runtime_config (passed to run())
-
tenant_config_resolver (configured lambda)
-
TenantBudget database record
-
Global configuration
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/ruby_llm/agents/infrastructure/budget/config_resolver.rb', line 44 def resolve_budget_config(tenant_id, runtime_config: nil) config = RubyLLM::Agents.configuration # Priority 1: Runtime config passed directly to run() if runtime_config.present? return normalize_budget_config(runtime_config, config) end # If multi-tenancy is disabled or no tenant, use global config if tenant_id.nil? || !config.multi_tenancy_enabled? return global_budget_config(config) end # Priority 2: tenant_config_resolver lambda if config.tenant_config_resolver.present? resolved_config = config.tenant_config_resolver.call(tenant_id) if resolved_config.present? return normalize_budget_config(resolved_config, config) end end # Priority 3: Look up tenant-specific budget from database tenant_budget = lookup_tenant_budget(tenant_id) if tenant_budget tenant_budget.to_budget_config else # Priority 4: Fall back to global config for unknown tenants global_budget_config(config) end end |
.resolve_tenant_id(explicit_tenant_id) ⇒ String?
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Resolves the current tenant ID
21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/ruby_llm/agents/infrastructure/budget/config_resolver.rb', line 21 def resolve_tenant_id(explicit_tenant_id) config = RubyLLM::Agents.configuration # Ignore tenant_id entirely when multi-tenancy is disabled return nil unless config.multi_tenancy_enabled? # Use explicit tenant_id if provided, otherwise use resolver return explicit_tenant_id if explicit_tenant_id.present? config.tenant_resolver&.call end |
.tenant_budget_table_exists? ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Checks if the tenants table exists (supports old and new table names)
131 132 133 134 135 136 137 138 139 |
# File 'lib/ruby_llm/agents/infrastructure/budget/config_resolver.rb', line 131 def tenant_budget_table_exists? return @tenant_budget_table_exists if defined?(@tenant_budget_table_exists) # Check for new table name (tenants) or old table name (tenant_budgets) for backward compatibility @tenant_budget_table_exists = ::ActiveRecord::Base.connection.table_exists?(:ruby_llm_agents_tenants) || ::ActiveRecord::Base.connection.table_exists?(:ruby_llm_agents_tenant_budgets) rescue @tenant_budget_table_exists = false end |