Class: Glancer::ChatsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/glancer/chats_controller.rb

Overview

:nodoc:

Instance Method Summary collapse

Instance Method Details

#destroyObject



44
45
46
47
# File 'app/controllers/glancer/chats_controller.rb', line 44

def destroy
  Glancer::Chat.find(params[:id]).destroy!
  redirect_to glancer.root_path
end

#indexObject



8
9
10
11
# File 'app/controllers/glancer/chats_controller.rb', line 8

def index
  @chats = Glancer::Chat.order(created_at: :desc)
  @chat  = nil
end

#showObject



13
14
15
16
17
18
19
# File 'app/controllers/glancer/chats_controller.rb', line 13

def show
  @chat = Glancer::Chat.find_by(id: params[:id])
  return redirect_to(glancer.root_path, alert: "Chat not found") unless @chat

  @chats    = Glancer::Chat.order(created_at: :desc)
  @messages = @chat.messages.order(:created_at)
end

#startObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'app/controllers/glancer/chats_controller.rb', line 21

def start
  content = params[:content].to_s.strip
  return render(json: { error: "Message required" }, status: :unprocessable_entity) if content.blank?

  @chat    = Glancer::Chat.create!(title: "New Chat")
  @message = @chat.messages.create!(role: :user, content: content)

  @response_message = @chat.messages.create!(
    role: :assistant,
    content: "",
    code_type: "sql",
    user_message: @message,
    status: :processing
  )

  Glancer::AsyncRunner.call(@response_message.id, content)

  render json: { chat_id: @chat.id }
rescue StandardError => e
  Glancer::Utils::Logger.error("ChatsController#start", e.message)
  render json: { error: e.message }, status: :internal_server_error
end