Class: RailsAi::AiStreamChannel

Inherits:
ApplicationCable::Channel
  • Object
show all
Defined in:
app/channels/ai_stream_channel.rb

Instance Method Summary collapse

Instance Method Details

#stream_ai(data) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'app/channels/ai_stream_channel.rb', line 13

def stream_ai(data)
  stream_id = data["stream_id"]
  prompt = data["prompt"]
  model = data["model"] || RailsAi.config.default_model

  RailsAi.provider.stream_chat!(
    messages: [{role: "user", content: prompt}],
    model: model
  ) do |token|
    ActionCable.server.broadcast("ai_stream_#{stream_id}", {
      type: "token",
      content: token
    })
  end

  ActionCable.server.broadcast("ai_stream_#{stream_id}", {
    type: "complete"
  })
end

#subscribedObject



5
6
7
# File 'app/channels/ai_stream_channel.rb', line 5

def subscribed
  stream_from "ai_stream_#{params[:stream_id]}"
end

#unsubscribedObject



9
10
11
# File 'app/channels/ai_stream_channel.rb', line 9

def unsubscribed
  # Any cleanup needed when channel is unsubscribed
end