Class: RailsAiContext::Introspector

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_ai_context/introspector.rb

Overview

Orchestrates all sub-introspectors to build a complete picture of the Rails application for AI consumption.

Constant Summary collapse

INTROSPECTOR_MAP =

Single source of truth: symbol → introspector class. Used by both the dispatcher below AND the Configuration presets validation, so adding/renaming introspectors only requires one edit.

{
  schema: Introspectors::SchemaIntrospector,
  models: Introspectors::ModelIntrospector,
  routes: Introspectors::RouteIntrospector,
  jobs: Introspectors::JobIntrospector,
  gems: Introspectors::GemIntrospector,
  conventions: Introspectors::ConventionIntrospector,
  stimulus: Introspectors::StimulusIntrospector,
  database_stats: Introspectors::DatabaseStatsIntrospector,
  controllers: Introspectors::ControllerIntrospector,
  views: Introspectors::ViewIntrospector,
  view_templates: Introspectors::ViewTemplateIntrospector,
  turbo: Introspectors::TurboIntrospector,
  i18n: Introspectors::I18nIntrospector,
  config: Introspectors::ConfigIntrospector,
  active_storage: Introspectors::ActiveStorageIntrospector,
  action_text: Introspectors::ActionTextIntrospector,
  auth: Introspectors::AuthIntrospector,
  api: Introspectors::ApiIntrospector,
  tests: Introspectors::TestIntrospector,
  rake_tasks: Introspectors::RakeTaskIntrospector,
  assets: Introspectors::AssetPipelineIntrospector,
  devops: Introspectors::DevOpsIntrospector,
  action_mailbox: Introspectors::ActionMailboxIntrospector,
  migrations: Introspectors::MigrationIntrospector,
  seeds: Introspectors::SeedsIntrospector,
  middleware: Introspectors::MiddlewareIntrospector,
  engines: Introspectors::EngineIntrospector,
  multi_database: Introspectors::MultiDatabaseIntrospector,
  components: Introspectors::ComponentIntrospector,
  performance: Introspectors::PerformanceIntrospector,
  frontend_frameworks: Introspectors::FrontendFrameworkIntrospector,
  initializers: Introspectors::InitializerIntrospector,
  autoload: Introspectors::AutoloadIntrospector,
  connection_pool: Introspectors::ConnectionPoolIntrospector,
  active_support: Introspectors::ActiveSupportIntrospector,
  credentials: Introspectors::CredentialsIntrospector,
  security: Introspectors::SecurityIntrospector,
  observability: Introspectors::ObservabilityIntrospector,
  env: Introspectors::EnvIntrospector
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Introspector

Returns a new instance of Introspector.



11
12
13
14
# File 'lib/rails_ai_context/introspector.rb', line 11

def initialize(app)
  @app    = app
  @config = RailsAiContext.configuration
end

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



9
10
11
# File 'lib/rails_ai_context/introspector.rb', line 9

def app
  @app
end

#configObject (readonly)

Returns the value of attribute config.



9
10
11
# File 'lib/rails_ai_context/introspector.rb', line 9

def config
  @config
end

Instance Method Details

#callHash

Run all configured introspectors and return unified context hash

Returns:

  • (Hash)

    complete application context



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/rails_ai_context/introspector.rb', line 19

def call
  context = {
    app_name: app_name,
    ruby_version: RUBY_VERSION,
    rails_version: rails_version,
    environment: environment_name,
    generated_at: Time.now.utc.iso8601,
    generator: "rails-ai-context v#{RailsAiContext::VERSION}"
  }

  config.introspectors.each do |name|
    introspector = resolve_introspector(name)
    context[name] = run_introspector(introspector)
  rescue StandardError, ScriptError => e
    # ScriptError included: a syntax-broken app file surfacing through
    # eager loading must cost one section, not the whole process (the
    # MCP server would otherwise die mid-session).
    context[name] = { error: e.message }
    RailsAiContext.log_warn "[rails-ai-context] #{name} introspection failed: #{e.message}"
  end

  # Collect warnings for introspectors that failed, so serializers can
  # render them and AI clients know which sections are missing.
  warnings = []
  config.introspectors.each do |name|
    data = context[name]
    if data.is_a?(Hash) && data[:error]
      warnings << { introspector: name.to_s, error: data[:error] }
    end
  end
  context[:_warnings] = warnings if warnings.any?

  context
end