Class: RailsAiBridge::Registry::ProviderRequestScope

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_ai_bridge/registry/provider_request_scope.rb

Overview

Per-invocation memo for context-provider tool results.

The scope lives for one MCP tool invocation (one rails_get_provider_context call). It is not a process-wide cache, Rails controller request cache, or cross-request HTTP cache. The tool that creates the scope is responsible for discarding it after the call completes.

Instance Method Summary collapse

Constructor Details

#initializeProviderRequestScope

Returns a new instance of ProviderRequestScope.



12
13
14
# File 'lib/rails_ai_bridge/registry/provider_request_scope.rb', line 12

def initialize
  @cache = {}
end

Instance Method Details

#clearHash

Removes all cached entries.

Returns:

  • (Hash)

    the cleared cache



34
# File 'lib/rails_ai_bridge/registry/provider_request_scope.rb', line 34

delegate :clear, to: :@cache

#fetch_or_store(provider_name, tool_name, args_key: nil) ⇒ Object

Returns the cached result for the given provider and tool, or yields the block and stores the result for subsequent lookups. The optional args_key distinguishes same-name tool calls with different arguments.

Parameters:

  • provider_name (String)

    provider identity

  • tool_name (String)

    remote tool name

  • args_key (Object, nil) (defaults to: nil)

    stable representation of effective arguments

Returns:

  • (Object)

    the cached or freshly fetched result



24
25
26
27
28
29
# File 'lib/rails_ai_bridge/registry/provider_request_scope.rb', line 24

def fetch_or_store(provider_name, tool_name, args_key: nil)
  key = [provider_name, tool_name, args_key]
  return @cache[key] if @cache.key?(key)

  @cache[key] = yield
end