Class: RailsAiContext::Tools::AnalyzeFeature

Inherits:
BaseTool
  • Object
show all
Defined in:
lib/rails_ai_context/tools/analyze_feature.rb

Constant Summary collapse

AUTH_KEYWORDS =

Map well-known feature keywords to gem-based patterns

%w[auth authentication login signup signin session devise omniauth].freeze
MAX_SCAN_FILES =

Cap per-directory file scans to avoid unbounded work on large monorepos. Note: Dir.glob materialises the full path array before .first() truncates —the directory walk itself is unbounded; only per-file reading and processing is capped at MAX_SCAN_FILES. Applied to every discover_* method that walks the filesystem. Tool output notes when the cap is hit so the AI agent knows to narrow its feature keyword. v5.8.1 hardening; all glob sites in r2.

500
AUTH_GEM_NAMES =
%w[devise omniauth rodauth sorcery clearance authlogic warden jwt].freeze

Constants inherited from BaseTool

BaseTool::SESSION_CONTEXT, BaseTool::SHARED_CACHE

Class Method Summary collapse

Methods inherited from BaseTool

abstract!, abstract?, cache_key, cached_context, config, extract_method_source_from_file, extract_method_source_from_string, find_closest_match, fuzzy_find_key, inherited, not_found_response, paginate, rails_app, registered_tools, reset_all_caches!, reset_cache!, session_queries, session_record, session_reset!, set_call_params, text_response

Class Method Details

.call(feature:, server_context: nil) ⇒ Object

rubocop:disable Metrics



36
37
38
39
40
41
42
43
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
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/rails_ai_context/tools/analyze_feature.rb', line 36

def self.call(feature:, server_context: nil) # rubocop:disable Metrics
  feature = feature.to_s.strip
  return text_response("Please provide a feature keyword (e.g. 'cook', 'payment', 'authentication').") if feature.empty?
  set_call_params(feature: feature)

  ctx = cached_context
  pattern = feature.downcase
  root = rails_app.root.to_s
  lines = [ "# Feature Analysis: #{feature}", "" ]

  matched_models = discover_models(ctx, pattern, lines)
  discover_controllers(ctx, pattern, lines)
  discover_routes(ctx, pattern, lines)
  discover_services(root, pattern, lines)
  discover_jobs(root, pattern, lines)
  discover_views(ctx, root, pattern, lines)
  discover_stimulus(ctx, pattern, lines)
  test_files = discover_tests(root, pattern, lines)
  discover_related_models(ctx, matched_models, lines)
  discover_concerns(ctx, matched_models, lines)
  discover_callbacks(ctx, matched_models, lines)
  discover_channels(root, pattern, lines)
  discover_mailers(root, pattern, lines)
  discover_env_dependencies(root, pattern, matched_models, lines)
  discover_test_gaps(root, pattern, matched_models, ctx, test_files || [], lines)
  discover_components(ctx, pattern, lines)

  # For auth-related keywords, also discover auth gems
  if AUTH_KEYWORDS.include?(pattern)
    gems = ctx[:gems]
    if gems.is_a?(Hash) && !gems[:error]
      notable = gems[:notable_gems] || []
      auth_gems = notable.select { |g| AUTH_GEM_NAMES.include?(g[:name]) }
      if auth_gems.any?
        lines << "" << "## Auth Gems" << ""
        auth_gems.each { |g| lines << "- **#{g[:name]}** #{g[:version]}#{g[:config] ? " (config: #{g[:config]})" : ""}" }
      end
    end
  end

  # If nothing was discovered, return a clean "no match" with real suggestions
  has_content = lines.any? { |l| l.start_with?("## ") || l.start_with?("### ") }
  unless has_content
    model_names = (ctx[:models] || {}).keys.map(&:to_s).sort.first(10)
    suggestions = model_names.any? ? model_names.join(", ") : "user, payment, order"
    return text_response("No matches found for '#{feature}'. No models, controllers, routes, services, or views match this keyword.\n\nTry one of your model names: #{suggestions}")
  end

  text_response(lines.join("\n"))
end