7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
# File 'lib/generators/llm_meta_client/scaffold/templates/app/controllers/chat_streams_controller.rb', line 7
def show
response.["Content-Type"] = "text/event-stream"
response.["Cache-Control"] = "no-cache"
response.["X-Accel-Buffering"] = "no"
chat = find_chat
prompt_execution = PromptNavigator::PromptExecution.find_by!(execution_id: params[:execution_id])
unless chat.messages.exists?(prompt_navigator_prompt_execution_id: prompt_execution.id)
raise ActiveRecord::RecordNotFound
end
jwt_token = current_user.id_token if user_signed_in?
generation_settings = parse_generation_settings(params[:generation_settings_json])
tool_ids = Array(params[:tool_ids]).reject(&:blank?)
assembled = chat.stream_assistant_response(prompt_execution, jwt_token, tool_ids: tool_ids, generation_settings: generation_settings) do |event|
if event[:event] == "tool_calls"
tool_calls = event[:data]["tool_calls"] || []
forward(event: "tool_calls", data: {
tool_calls: tool_calls,
html: view_context.render(partial: "chats/tool_call_message", locals: { tool_calls: tool_calls })
})
else
forward(event)
end
end
if assembled.present?
assistant_message = chat.finalize_streamed_response(prompt_execution, assembled, jwt_token)
if assistant_message
forward(event: "saved", data: {
message_id: assistant_message.id,
execution_id: prompt_execution.execution_id,
html: view_context.render(partial: "chats/message", locals: { message: assistant_message })
})
end
title_before = chat.title
chat.generate_title(prompt_execution.prompt, jwt_token)
if chat.reload.title.present? && chat.title != title_before
forward(event: "title", data: {
title: chat.title,
chat_uuid: chat.uuid,
turbo_stream: (chat)
})
end
end
forward(event: "done", data: {})
rescue ActionController::Live::ClientDisconnected
Rails.logger.info "[ChatStream] client disconnected"
rescue ActiveRecord::RecordNotFound
forward(event: "error", data: { code: "not_found", message: "Chat or prompt execution not found" }) rescue nil
rescue StandardError => e
Rails.logger.error "[ChatStream] #{e.class}: #{e.message}"
forward(event: "error", data: { code: e.class.name, message: e.message }) rescue nil
ensure
response.stream.close
end
|