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
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



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
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
# File 'lib/rails_ai_context/tools/analyze_feature.rb', line 28

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