Class: RailsAiContext::Tools::GetRoutes
- Defined in:
- lib/rails_ai_context/tools/get_routes.rb
Constant Summary
Constants inherited from BaseTool
BaseTool::SESSION_CONTEXT, BaseTool::SHARED_CACHE
Class Method Summary collapse
- .call(controller: nil, detail: "standard", limit: nil, offset: 0, app_only: true, server_context: nil) ⇒ Object
- .route_prefixes ⇒ Object
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, app_only: true, server_context: nil) ⇒ Object
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 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/rails_ai_context/tools/get_routes.rb', line 43 def self.call(controller: nil, detail: "standard", limit: nil, offset: 0, app_only: true, server_context: nil) routes = cached_context[:routes] return text_response("Route introspection not available. Add :routes to introspectors.") unless routes return text_response("Route introspection failed: #{routes[:error]}") if routes[:error] by_controller = routes[:by_controller] || {} offset = [ offset.to_i, 0 ].max # Filter out internal Rails routes by default if app_only by_controller = by_controller.reject { |k, _| route_prefixes.any? { |p| k.downcase.start_with?(p) } } end # Filter by controller — accepts "cooks", "CooksController", "cooks_controller", "Api::V1::Posts" if controller normalized = controller.underscore.delete_suffix("_controller") normalized_alt = controller.downcase.delete_suffix("_controller").delete_suffix("controller") filtered = by_controller.select { |k, _| k.downcase.include?(normalized) || k.downcase.include?(normalized_alt) } return text_response("No routes for '#{controller}'. Controllers: #{by_controller.keys.sort.join(', ')}") if filtered.empty? by_controller = filtered end # Combine PUT/PATCH duplicates (Rails generates both for update routes) by_controller = by_controller.transform_values { |actions| dedupe_put_patch(actions) } filtered_total = by_controller.values.sum(&:size) case detail when "summary" # Separate app routes from framework routes for cleaner output app_routes = controller ? by_controller : by_controller.reject { |k, _| route_prefixes.any? { |p| k.downcase.start_with?(p) } } framework_routes = controller ? {} : by_controller.select { |k, _| route_prefixes.any? { |p| k.downcase.start_with?(p) } } lines = [ "# Routes Summary (#{filtered_total} routes)", "" ] # Group sibling routes with identical verb patterns (e.g., bonus/*) grouped = app_routes.keys.sort.group_by do |ctrl| actions = app_routes[ctrl] namespace = ctrl.include?("/") ? ctrl.split("/").first : nil verbs_sig = actions.map { |r| r[:verb] }.sort.join(",") count_sig = actions.size namespace && app_routes.count { |k, v| k.start_with?("#{namespace}/") && v.size == count_sig && v.map { |r| r[:verb] }.sort.join(",") == verbs_sig } > 2 ? "#{namespace}/*|#{count_sig}|#{verbs_sig}" : ctrl end grouped.each do |_key, ctrls| if ctrls.size > 2 namespace = ctrls.first.split("/").first actions = app_routes[ctrls.first] verbs = actions.map { |r| r[:verb] }.tally.map { |v, c| "#{c} #{v}" }.join(", ") short_names = ctrls.map { |c| c.split("/").last } lines << "- **#{namespace}/*** (#{short_names.join(', ')}) — #{actions.size} routes each (#{verbs})" else ctrls.each do |ctrl| actions = app_routes[ctrl] verbs = actions.map { |r| r[:verb] }.tally.map { |v, c| "#{c} #{v}" }.join(", ") lines << "- **#{ctrl}** — #{actions.size} routes (#{verbs})" end end end # Show framework routes as a compact summary if framework_routes.any? total_fw = framework_routes.values.sum(&:size) fw_names = framework_routes.keys.map { |k| k.split("/").first }.uniq.join(", ") lines << "- _#{fw_names} framework routes: #{total_fw} total_" end if routes[:api_namespaces]&.any? lines << "" << "API namespaces: #{routes[:api_namespaces].join(', ')}" end lines << "" << "_Use `controller:\"name\"` to see routes for a specific controller._" text_response(lines.join("\n")) when "standard" # Flatten all routes into a tagged array, then paginate app_routes = controller ? by_controller : by_controller.reject { |k, _| route_prefixes.any? { |p| k.downcase.start_with?(p) } } framework_routes = controller ? {} : by_controller.select { |k, _| route_prefixes.any? { |p| k.downcase.start_with?(p) } } flat_routes = app_routes.sort.flat_map { |ctrl, actions| actions.map { |r| r.merge(_ctrl: ctrl) } } page = paginate(flat_routes, offset: offset, limit: limit, default_limit: 150) lines = [ "# Routes (#{filtered_total} routes)", "" ] current_ctrl = nil page[:items].each do |r| ctrl = r[:_ctrl] if ctrl != current_ctrl current_ctrl = ctrl ctrl_class = "#{ctrl.camelize}Controller" ctrl_data = cached_context.dig(:controllers, :controllers, ctrl_class) ctrl_summary = "" if ctrl_data filters = (ctrl_data[:filters] || []).map { |f| f[:name] }.first(3) formats = ctrl_data[:respond_to_formats] parts = [] parts << "filters: #{filters.join(', ')}" if filters.any? parts << "formats: #{formats.join(', ')}" if formats&.any? ctrl_summary = " (#{parts.join(' | ')})" if parts.any? end lines << "## #{ctrl}#{ctrl_summary}" end params = r[:path].scan(/:(\w+)/).flatten params_part = params.any? ? " [#{params.join(', ')}]" : "" helper_part = if r[:name] args = params.any? ? "(#{params.map { |p| p == 'id' ? '@record' : ":#{p}" }.join(', ')})" : "" " `#{r[:name]}_path#{args}`" else "" end lines << "- `#{r[:verb]}` `#{r[:path]}` → #{r[:action]}#{helper_part}#{params_part}" end if framework_routes.any? total_fw = framework_routes.values.sum(&:size) fw_names = framework_routes.keys.map { |k| k.split("/").first }.uniq.join(", ") lines << "" << "_#{fw_names} framework routes: #{total_fw} total (use `controller:\"devise/sessions\"` to see details)_" end lines << "" << page[:hint] unless page[:hint].empty? text_response(lines.join("\n")) when "full" flat_routes = by_controller.sort.flat_map { |ctrl, actions| actions.map { |r| r.merge(_ctrl: ctrl) } } page = paginate(flat_routes, offset: offset, limit: limit, default_limit: 200) lines = [ "# Routes Full Detail (#{filtered_total} routes)", "" ] lines << "| Verb | Path | Controller#Action | Name |" lines << "|------|------|-------------------|------|" page[:items].each do |r| lines << "| #{r[:verb]} | `#{r[:path]}` | #{r[:_ctrl]}##{r[:action]} | #{r[:name] || '-'} |" end if routes[:api_namespaces]&.any? lines << "" << "## API namespaces: #{routes[:api_namespaces].join(', ')}" end lines << "" << page[:hint] unless page[:hint].empty? text_response(lines.join("\n")) else text_response("Unknown detail level: #{detail}. Use summary, standard, or full.") end end |
.route_prefixes ⇒ Object
37 38 39 |
# File 'lib/rails_ai_context/tools/get_routes.rb', line 37 def self.route_prefixes RailsAiContext.configuration.excluded_route_prefixes end |