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

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

  # 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 += "#{assoc_count} associations, #{val_count} validations" 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