Class: RailsAiContext::Tools::GetStimulus

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

Constant Summary

Constants inherited from BaseTool

BaseTool::DEFAULT_SESSION, BaseTool::MAX_SESSIONS, BaseTool::MAX_SESSION_ID_LENGTH, BaseTool::SESSION_CONTEXT, BaseTool::SHARED_CACHE

Class Method Summary collapse

Methods inherited from BaseTool

abstract!, abstract?, api_only_app?, api_only_note, cache_key, cached_context, config, current_session, #dedupe_put_patch_routes, detail_param?, error_response, evict_oldest_sessions, extract_method_source_from_file, extract_method_source_from_string, find_closest_match, fuzzy_find_key, guide_row, inherited, introspection_warnings_note, invalid_detail_note, normalize_detail, not_found_response, paginate, rails_app, rails_env_name, registered_tools, reset_all_caches!, reset_cache!, session_from, session_params, session_queries, session_record, session_reset!, static_tier_banner, static_tier_refusal, text_response, touch_session, unavailable_note, with_session, with_session_for

Methods included from SectionFetch

#fetch_section, usable?

Class Method Details

.call(controller: nil, detail: "standard", limit: nil, offset: 0, server_context: nil) ⇒ Object



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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/rails_ai_context/tools/get_stimulus.rb', line 42

def self.call(controller: nil, detail: "standard", limit: nil, offset: 0, server_context: nil)
  fetch_section(:stimulus, subject: "Stimulus introspection") do |data|
    all_controllers = data[:controllers] || []
    if all_controllers.empty?
      note = api_only_note("app/javascript/controllers")
      return text_response(note) if note

      return text_response("No Stimulus controllers found.")
    end

    # Specific controller - accepts both dash and underscore naming
    # (HTML uses data-controller="weekly-chart", file is weekly_chart_controller.js)
    if controller
      normalized = controller.downcase.tr("-", "_").delete_suffix("_controller")
      # Also handle PascalCase: PostStatus → post_status
      underscored = controller.underscore.downcase.tr("-", "_").delete_suffix("_controller")
      ctrl = all_controllers.find { |c|
        name_norm = c[:name]&.downcase&.tr("-", "_")
        name_norm == normalized || name_norm == underscored
      }
      unless ctrl
        names = all_controllers.map { |c| c[:name] }.sort
        return not_found_response("Stimulus controller", controller, names,
          recovery_tool: "Call rails_get_stimulus(detail:\"summary\") to see all controllers. Note: use dashes in HTML, underscores for lookup.")
      end
      return text_response(format_controller_full(ctrl))
    end

    # Pagination
    total = all_controllers.size
    offset_val = [ offset.to_i, 0 ].max
    limit_val = limit.nil? ? 50 : [ limit.to_i, 1 ].max
    sorted_all = all_controllers.sort_by { |c| c[:name]&.to_s || "" }
    controllers = sorted_all.drop(offset_val).first(limit_val)

    if controllers.empty? && total > 0
      return text_response("No controllers at offset #{offset_val}. Total: #{total}. Use `offset:0` to start over.")
    end

    pagination_hint = offset_val + limit_val < total ? "\n_Showing #{controllers.size} of #{total}. Use `offset:#{offset_val + limit_val}` for more. cache_key: #{cache_key}_" : ""

    case detail
    when "summary"
      active = controllers.select { |c| (c[:targets] || []).any? || (c[:actions] || []).any? || (c[:values].is_a?(Hash) ? c[:values] : {}).any? }
      empty = controllers.reject { |c| (c[:targets] || []).any? || (c[:actions] || []).any? || (c[:values].is_a?(Hash) ? c[:values] : {}).any? }

      lines = [ "# Stimulus Controllers (#{total})", "" ]
      active.each do |ctrl|
        targets = (ctrl[:targets] || []).size
        values = (ctrl[:values].is_a?(Hash) ? ctrl[:values] : {}).size
        actions = (ctrl[:actions] || []).size
        parts = []
        parts << count_phrase(targets, "target") if targets > 0
        parts << count_phrase(values, "value") if values > 0
        parts << count_phrase(actions, "action") if actions > 0
        lines << "- **#{ctrl[:name]}** - #{parts.join(', ')}"
      end
      if empty.any?
        names = empty.map { |c| c[:name] }.join(", ")
        lines << "- _#{names}_ (lifecycle only)"
      end
      lines << "" << "_Use `controller:\"name\"` for full detail._#{pagination_hint}"
      text_response(lines.join("\n"))

    when "standard"
      active = controllers.select { |c| (c[:targets] || []).any? || (c[:actions] || []).any? || (c[:values].is_a?(Hash) ? c[:values] : {}).any? }
      empty = controllers.reject { |c| (c[:targets] || []).any? || (c[:actions] || []).any? || (c[:values].is_a?(Hash) ? c[:values] : {}).any? }

      lines = [ "# Stimulus Controllers (#{total})", "" ]
      active.each do |ctrl|
        lines << "## #{ctrl[:name]}"
        lines << "- Targets: #{(ctrl[:targets] || []).join(', ')}" if ctrl[:targets]&.any?
        lines << "- Values: #{(ctrl[:values].is_a?(Hash) ? ctrl[:values] : {}).map { |k, v| "#{k} (#{v})" }.join(', ')}" if (ctrl[:values].is_a?(Hash) ? ctrl[:values] : {}).any?
        lines << "- Actions: #{(ctrl[:actions] || []).join(', ')}" if ctrl[:actions]&.any?
        if ctrl[:complexity].is_a?(Hash)
          parts = []
          parts << "#{ctrl[:complexity][:loc]} LOC" if ctrl[:complexity][:loc]
          parts << count_phrase(ctrl[:complexity][:method_count], "method") if ctrl[:complexity][:method_count]
          lines << "- Complexity: #{parts.join(', ')}" if parts.any?
        end
        lines << "- Imports: #{ctrl[:import_graph].join(', ')}" if ctrl[:import_graph]&.any?
        lines << "- Turbo events: #{ctrl[:turbo_event_listeners].join(', ')}" if ctrl[:turbo_event_listeners]&.any?
        lines << ""
      end
      if empty.any?
        names = empty.map { |c| c[:name] }.join(", ")
        lines << "_Lifecycle only (no targets/values/actions): #{names}_"
      end

      # Cross-controller composition
      if data[:cross_controller_composition]&.any?
        lines << "" << "## Cross-Controller Composition"
        data[:cross_controller_composition].first(10).each do |comp|
          lines << "- #{comp}"
        end
      end

      lines << pagination_hint unless pagination_hint.empty?
      text_response(lines.join("\n"))

    when "full"
      lines = [ "# Stimulus Controllers (#{total})", "" ]
      lines << "_HTML naming: `data-controller=\"my-name\"` (dashes in HTML, underscores in filenames)_" << ""
      controllers.each do |ctrl|
        lines << format_controller_full(ctrl) << ""
      end

      # Cross-controller composition
      if data[:cross_controller_composition]&.any?
        lines << "## Cross-Controller Composition"
        data[:cross_controller_composition].first(10).each do |comp|
          lines << "- #{comp}"
        end
        lines << ""
      end

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

    end
  end
end