Class: RailsAiBridge::Registry::ContextAggregator
- Inherits:
-
Object
- Object
- RailsAiBridge::Registry::ContextAggregator
- Defined in:
- lib/rails_ai_bridge/registry/context_aggregator.rb
Overview
Orchestrates fetching context from declared providers through ContextProviderClient instances. Iterates providers and their tools sequentially in deterministic order, maps results to declared fields, enforces budgets and count caps, and memoizes within a per-invocation ProviderRequestScope.
Optional provider failures are recorded as warnings and skipped; required provider failures are visible in the aggregate result. The aggregator never uses a process-wide cache.
Defined Under Namespace
Classes: AggregateResult
Instance Method Summary collapse
-
#fetch_all ⇒ AggregateResult
Fetches all declared providers and returns an aggregate result.
-
#fetch_one(name) ⇒ AggregateResult
Fetches a single provider by name and returns its mapped tool results.
- #initialize(manifest:, config:, client_factory:, scope:) ⇒ ContextAggregator constructor
Constructor Details
#initialize(manifest:, config:, client_factory:, scope:) ⇒ ContextAggregator
42 43 44 45 46 47 |
# File 'lib/rails_ai_bridge/registry/context_aggregator.rb', line 42 def initialize(manifest:, config:, client_factory:, scope:) @manifest = manifest @config = config @client_factory = client_factory @scope = scope end |
Instance Method Details
#fetch_all ⇒ AggregateResult
Fetches all declared providers and returns an aggregate result.
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 |
# File 'lib/rails_ai_bridge/registry/context_aggregator.rb', line 52 def fetch_all providers = @manifest.context_providers results = {} failures = [] failed_optional = false failed_required = false start = monotonic_now deadline = start + @config.aggregation_budget_seconds providers.each_with_index do |(name, provider), index| break if index >= @config.max_providers break if monotonic_now >= deadline outcome = fetch_provider(name, provider, deadline:) error = outcome[:error] if error failures << error failed_optional = true if provider.optional? failed_required = true unless provider.optional? next end results[name] = outcome[:data] end AggregateResult.new( results: results, failures: failures, status: aggregate_status(failed_required, failed_optional), elapsed_ms: elapsed_ms(start) ) end |
#fetch_one(name) ⇒ AggregateResult
Fetches a single provider by name and returns its mapped tool results.
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/rails_ai_bridge/registry/context_aggregator.rb', line 89 def fetch_one(name) provider = @manifest.context_providers[name] raise ArgumentError, "unknown provider: #{name}" unless provider start = monotonic_now outcome = fetch_provider(name, provider, deadline: nil) elapsed = elapsed_ms(start) error = outcome[:error] return error_result(error, elapsed) if error AggregateResult.new( results: outcome[:data] || {}, failures: [], status: :success, elapsed_ms: elapsed ) end |