Module: RubyLLM::AiSdk::Controller

Extended by:
ActiveSupport::Concern
Defined in:
lib/ruby_llm/ai_sdk/controller.rb

Overview

Streams a chat turn from a Rails controller action in the format useChat expects. Including it also includes ActionController::Live.

class MessagesController < ApplicationController
include RubyLLM::AiSdk::Controller

def create
  stream_chat SupportAgent.find(params[:chat_id])
end
end

Instance Method Summary collapse

Instance Method Details

#stream_chat(chat, body = request.raw_post) ⇒ Object

Streams one turn of chat driven by body, the JSON useChat posted, and closes the response stream when the turn ends.



26
27
28
29
30
31
# File 'lib/ruby_llm/ai_sdk/controller.rb', line 26

def stream_chat(chat, body = request.raw_post)
  HEADERS.each { |name, value| response.headers[name] = value }
  Stream.new(chat, response.stream).serve(body)
ensure
  response.stream.close
end