Class: RailsAiBridge::ContextProvider

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

Overview

Builds and caches introspection snapshots for MCP tools and resources. This keeps all runtime reads aligned behind one explicit boundary.

Class Method Summary collapse

Class Method Details

.fetch(app = Rails.application) ⇒ Hash

Returns the latest introspection snapshot for the given app, reusing a cached value while the TTL is valid and the fingerprint is unchanged.

Parameters:

  • app (Rails::Application) (defaults to: Rails.application)

    application to introspect

Returns:

  • (Hash)

    introspection payload



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/rails_ai_bridge/context_provider.rb', line 16

def fetch(app = Rails.application)
  mutex.synchronize do
    cached = cache[cache_key(app)]
    return rebuild(app) unless cached[:full]

    current_fingerprint = Fingerprinter.snapshot(app)
    return cached[:full][:context] if ttl_valid?(cached[:full]) && current_fingerprint == cached[:full][:fingerprint]

    rebuild(app, fingerprint: current_fingerprint)
  end
end

.fetch_section(section, app = Rails.application) ⇒ Object?

Returns a single introspection section using a dedicated cache entry for that section. If a valid full snapshot is already cached, reuse it.

Parameters:

  • section (Symbol)

    introspector key to retrieve

  • app (Rails::Application) (defaults to: Rails.application)

    application to introspect

Returns:

  • (Object, nil)

    requested section payload



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/rails_ai_bridge/context_provider.rb', line 34

def fetch_section(section, app = Rails.application)
  mutex.synchronize do
    key = cache_key(app)
    cached = cache[key]
    current_fingerprint = Fingerprinter.snapshot(app)

    full = cached[:full]
    return full[:context][section] if full && ttl_valid?(full) && current_fingerprint == full[:fingerprint]

    section_entry = cached[:sections][section]
    return section_entry[:context] if section_entry && ttl_valid?(section_entry) && current_fingerprint == section_entry[:fingerprint]

    rebuild_section(section, app, fingerprint: current_fingerprint)
  end
end

.reset!void

This method returns an undefined value.

Clears all cached snapshots.



53
54
55
56
# File 'lib/rails_ai_bridge/context_provider.rb', line 53

def reset!
  @cache = build_cache_store
  @mutex = Mutex.new
end