Class: Collavre::AiAgent::ResponseStreamer

Inherits:
Object
  • Object
show all
Defined in:
app/services/collavre/ai_agent/response_streamer.rb

Overview

Handles streaming response content from AI to comments. Manages throttling, broadcasting, and placeholder cleanup.

Constant Summary collapse

STREAM_THROTTLE_INTERVAL =

Minimum interval (in seconds) between streaming updates to avoid excessive DB writes

0.1

Instance Method Summary collapse

Constructor Details

#initialize(reply_comment:, creative:) ⇒ ResponseStreamer

Returns a new instance of ResponseStreamer.



11
12
13
14
15
16
# File 'app/services/collavre/ai_agent/response_streamer.rb', line 11

def initialize(reply_comment:, creative:)
  @reply_comment = reply_comment
  @creative = creative
  @content = ""
  @last_broadcast_at = Process.clock_gettime(Process::CLOCK_MONOTONIC)
end

Instance Method Details

#append(delta) ⇒ Object

Append delta content and broadcast if throttle interval passed



19
20
21
22
23
24
25
26
27
# File 'app/services/collavre/ai_agent/response_streamer.rb', line 19

def append(delta)
  @content += delta

  now = Process.clock_gettime(Process::CLOCK_MONOTONIC)
  if @reply_comment && (now - @last_broadcast_at) >= STREAM_THROTTLE_INTERVAL
    update_and_broadcast(streaming: true)
    @last_broadcast_at = now
  end
end

#contentObject

Get accumulated content



30
31
32
# File 'app/services/collavre/ai_agent/response_streamer.rb', line 30

def content
  @content
end

#content_present?Boolean

Check if there’s content accumulated

Returns:

  • (Boolean)


40
41
42
# File 'app/services/collavre/ai_agent/response_streamer.rb', line 40

def content_present?
  @content.present?
end

#finalizeObject

Finalize streaming by broadcasting final update



35
36
37
# File 'app/services/collavre/ai_agent/response_streamer.rb', line 35

def finalize
  update_and_broadcast(streaming: false) if @reply_comment && @content.present?
end