Module: RobotLab::RailsIntegration::TurboStreamCallbacks

Defined in:
lib/robot_lab/rails_integration/turbo_stream_callbacks.rb

Overview

Stateless utility module that builds callback Procs for Turbo Stream broadcasting.

Safe to require even without turbo-rails installed — checks at call time via ‘defined?(Turbo::StreamsChannel)`.

Class Method Summary collapse

Class Method Details

.available?Boolean

Returns:

  • (Boolean)


11
12
13
# File 'lib/robot_lab/rails_integration/turbo_stream_callbacks.rb', line 11

def self.available?
  defined?(Turbo::StreamsChannel) ? true : false
end

.build_content_callback(stream_name:, target: "robot_response") ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/robot_lab/rails_integration/turbo_stream_callbacks.rb', line 15

def self.build_content_callback(stream_name:, target: "robot_response")
  ->(chunk) {
    content = chunk.respond_to?(:content) ? chunk.content : chunk.to_s
    return unless content && TurboStreamCallbacks.available?

    Turbo::StreamsChannel.broadcast_append_to(
      stream_name,
      target: target,
      html: ERB::Util.html_escape(content)
    )
  }
end

.build_tool_call_callback(stream_name:, target: "robot_tools") ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/robot_lab/rails_integration/turbo_stream_callbacks.rb', line 28

def self.build_tool_call_callback(stream_name:, target: "robot_tools")
  ->(tool_call) {
    return unless TurboStreamCallbacks.available?

    name = tool_call.respond_to?(:name) ? tool_call.name : tool_call.to_s
    Turbo::StreamsChannel.broadcast_append_to(
      stream_name,
      target: target,
      html: "<span class=\"tool-badge\">Using: #{ERB::Util.html_escape(name)}</span>"
    )
  }
end