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::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(controller: nil, detail: "standard", limit: nil, offset: 0, server_context: nil) ⇒ Object



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

def self.call(controller: nil, detail: "standard", limit: nil, offset: 0, server_context: nil)
  data = cached_context[:stimulus]
  return text_response("Stimulus introspection not available. Add :stimulus to introspectors.") unless data
  return text_response("Stimulus introspection failed: #{data[:error]}") if data[:error]

  all_controllers = data[:controllers] || []
  return text_response("No Stimulus controllers found.") if all_controllers.empty?

  # 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: CookStatus → cook_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 << "#{targets} targets" if targets > 0
      parts << "#{values} values" if values > 0
      parts << "#{actions} actions" 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 << "#{ctrl[:complexity][:method_count]} methods" 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"))

  else
    text_response("Unknown detail level: #{detail}. Use summary, standard, or full.")
  end
end