Class: RailsAiContext::Tools::GetTurboMap

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

Constant Summary collapse

BROADCAST_METHODS =
%w[
  broadcast_replace_to
  broadcast_append_to
  broadcast_prepend_to
  broadcast_remove_to
  broadcast_update_to
  broadcast_action_to
].freeze
MODEL_BROADCAST_MACROS =
%w[
  broadcasts
  broadcasts_to
  broadcasts_refreshes
  broadcasts_refreshes_to
].freeze

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



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

def self.call(detail: "standard", stream: nil, controller: nil, server_context: nil)
  root = Rails.root.to_s

  # Collect all Turbo data
  model_broadcasts = scan_model_broadcasts(root)
  rb_broadcasts = scan_rb_broadcasts(root)
  view_subscriptions = scan_view_subscriptions(root)
  view_frames = scan_view_frames(root)

  # Apply filters
  if stream
    stream_lower = stream.downcase
    model_broadcasts = model_broadcasts.select { |b|
      b[:stream]&.downcase&.include?(stream_lower) ||
      b[:snippet]&.downcase&.include?(stream_lower)
    }
    rb_broadcasts = rb_broadcasts.select { |b|
      b[:stream]&.downcase&.include?(stream_lower) ||
      b[:snippet]&.downcase&.include?(stream_lower)
    }
    view_subscriptions = view_subscriptions.select { |s|
      s[:stream]&.downcase&.include?(stream_lower) ||
      s[:snippet]&.downcase&.include?(stream_lower)
    }
  end

  if controller
    ctrl_lower = controller.downcase
    # Filter subscriptions and frames by controller path
    view_subscriptions = view_subscriptions.select { |s| s[:file]&.downcase&.include?(ctrl_lower) }
    view_frames = view_frames.select { |f| f[:file]&.downcase&.include?(ctrl_lower) }

    # For broadcasts: include those in the controller path OR those whose
    # stream matches any subscription that survived the filter (e.g. jobs
    # broadcasting to streams that the controller's views subscribe to)
    matched_streams = view_subscriptions.map { |s| s[:stream] }.compact
    rb_broadcasts = rb_broadcasts.select { |b|
      b[:file]&.downcase&.include?(ctrl_lower) ||
        (b[:stream] && matched_streams.any? { |ss| streams_match?(b[:stream], ss) })
    }
  end

  # Detect mismatches
  warnings = detect_mismatches(model_broadcasts, rb_broadcasts, view_subscriptions)

  filter_label = stream ? "stream:\"#{stream}\"" : controller ? "controller:\"#{controller}\"" : nil

  case detail
  when "summary"
    format_summary(model_broadcasts, rb_broadcasts, view_subscriptions, view_frames, warnings, filter_label: filter_label)
  when "standard"
    format_standard(model_broadcasts, rb_broadcasts, view_subscriptions, view_frames, warnings, filter_label: filter_label)
  when "full"
    format_full(model_broadcasts, rb_broadcasts, view_subscriptions, view_frames, warnings, filter_label: filter_label)
  else
    text_response("Unknown detail level: #{detail}. Use summary, standard, or full.")
  end
end