Class: RailsAiContext::Tools::GetModelDetails

Inherits:
BaseTool
  • Object
show all
Defined in:
lib/rails_ai_context/tools/get_model_details.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(model: 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
# File 'lib/rails_ai_context/tools/get_model_details.rb', line 42

def self.call(model: nil, detail: "standard", limit: nil, offset: 0, server_context: nil)
  fetch_section(:models, subject: "Model introspection") do |models|
    # Specific model - always full detail (strip whitespace for fuzzy input)
    if model
      model = model.strip
      key = fuzzy_find_key(models.keys, model) || model
      data = models[key]
      unless data
        return not_found_response("Model", model, models.keys.sort,
          recovery_tool: "Call rails_get_model_details(detail:\"summary\") to see all models")
      end
      return text_response("Error inspecting #{key}: #{data[:error]}") if data[:error]
      return text_response(format_model(key, data))
    end

    # Pagination - sort by association count (most connected first)
    all_names = models.keys.sort_by { |m| -(models[m][:associations]&.size || 0) }
    page = paginate(all_names, offset: offset, limit: limit, default_limit: 50)
    paginated = page[:items]

    if paginated.empty? && page[:total] > 0
      return text_response(page[:hint])
    end

    pagination_hint = page[:hint].empty? ? "" : "\n#{page[:hint]}"

    # Listing mode
    case detail
    when "summary"
      model_list = paginated.map { |m| "- #{m}" }.join("\n")
      text_response("# Available models (#{page[:total]})\n\n#{model_list}\n\n_Use `model:\"Name\"` for full detail._#{pagination_hint}")

    when "standard"
      lines = [ "# Models (#{page[:total]})", "" ]
      paginated.each do |name|
        data = models[name]
        next if data[:error]
        assoc_count = (data[:associations] || []).size
        val_count = (data[:validations] || []).size
        line = "- **#{name}**"
        line += " - #{count_phrase(assoc_count, "association")}, #{count_phrase(val_count, "validation")}" if assoc_count > 0 || val_count > 0
        lines << line
      end
      lines << "" << "_Use `model:\"Name\"` for full detail, or `detail:\"full\"` for association lists._#{pagination_hint}"
      text_response(lines.join("\n"))

    when "full"
      lines = [ "# Models (#{page[:total]})", "" ]
      paginated.each do |name|
        data = models[name]
        next if data[:error]
        assocs = (data[:associations] || []).map { |a| "#{a[:type]} :#{a[:name]}" }.join(", ")
        line = "- **#{name}**"
        line += " (table: #{data[:table_name]})" if data[:table_name]
        line += " - #{assocs}" unless assocs.empty?
        lines << line
      end
      lines << "" << "_Use `model:\"Name\"` for validations, scopes, callbacks, and more._#{pagination_hint}"
      text_response(lines.join("\n"))

    else
      model_list = paginated.map { |m| "- #{m}" }.join("\n")
      text_response("# Available models (#{page[:total]})\n\n#{model_list}#{pagination_hint}")
    end
  end
end