Class: RailsAiContext::Tools::DependencyGraph

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

Constant Summary collapse

MAX_NODES =
50

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, depth: 2, format: "mermaid", show_cycles: false, show_sti: false, 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
# File 'lib/rails_ai_context/tools/dependency_graph.rb', line 42

def self.call(model: nil, depth: 2, format: "mermaid", show_cycles: false, show_sti: false, server_context: nil)
  models_data = cached_context[:models]

  unless models_data.is_a?(Hash) && !models_data[:error]
    return text_response("No model data available. Ensure :models introspector is enabled.")
  end

  model = model.to_s.strip if model
  depth = [ [ depth.to_i, 1 ].max, 3 ].min

  set_call_params(model: model, depth: depth, format: format, show_cycles: show_cycles, show_sti: show_sti)

  # Build adjacency list from model associations
  graph = build_graph(models_data)

  if model
    model_key = find_model_key(model, graph.keys)
    unless model_key
      return not_found_response("Model", model, graph.keys.sort,
        recovery_tool: "Call rails_dependency_graph() without model to see all models")
    end
    subgraph = extract_subgraph(graph, model_key, depth)
  else
    subgraph = graph
  end

  # Limit nodes
  if subgraph.size > MAX_NODES
    subgraph = subgraph.first(MAX_NODES).to_h
  end

  # Optional analyses
  cycles = show_cycles ? detect_cycles(graph) : []
  sti_groups = show_sti ? extract_sti_groups(models_data) : []

  case format
  when "mermaid"
    text_response(render_mermaid(subgraph, model, cycles: cycles, sti_groups: sti_groups))
  else
    text_response(render_text(subgraph, model, cycles: cycles, sti_groups: sti_groups))
  end
end