Class: ChatsController

Inherits:
ApplicationController
  • Object
show all
Includes:
ChatManager::ChatManageable, ChatManager::CsvDownloadable, PromptNavigator::HistoryManageable
Defined in:
lib/generators/llm_meta_client/scaffold/templates/app/controllers/chats_controller.rb

Defined Under Namespace

Classes: InvalidGenerationSettingsError

Instance Method Summary collapse

Instance Method Details

#createObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/generators/llm_meta_client/scaffold/templates/app/controllers/chats_controller.rb', line 55

def create
  jwt_token = current_user.id_token if user_signed_in?

  # Initialize chat sidebar
  initialize_chat current_user&.chats

  # Find or create chat
  @chat = Chat.find_or_switch_for_session(
    session,
    current_user
  )
  add_chat @chat
  @messages = @chat&.ordered_messages || []

  # initialize history for the chat
  initialize_history @chat&.ordered_by_descending_prompt_executions

  if params[:message].present?
    # Validate generation settings before proceeding (raises if invalid).
    # The streaming controller re-parses them from the URL.
    begin
      generation_settings_param
    rescue InvalidGenerationSettingsError => e
      @error_message = e.message
      respond_to do |format|
        format.turbo_stream
        format.html { redirect_to new_chat_path, alert: e.message }
      end
      return
    end

    # Add user message (will be rendered via turbo stream)
    @prompt_execution, @user_message = @chat.add_user_message(params[:message],
                                                              params[:api_key_uuid],
                                                              params[:model],
                                                              params[:branch_from_uuid])
    # Push to history for rendering
    push_to_history @prompt_execution
    # Set active message UUID for highlighting in UI
    set_active_message_uuid(@prompt_execution&.execution_id || params.dig(:chat, :branch_from_uuid))

    # The assistant response is streamed by ChatStreamsController (SSE).
    # The streaming bubble is rendered by create.turbo_stream.erb and opens
    # the EventSource on connect; persistence + title gen happen at stream close.
    @generation_settings_json = params[:generation_settings_json]
    @tool_ids = Array(params[:tool_ids]).reject(&:blank?)
  end

  # Return turbo stream to render both messages
  respond_to do |format|
    format.turbo_stream
    format.html { redirect_to new_chat_path }
  end
end

#editObject



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/generators/llm_meta_client/scaffold/templates/app/controllers/chats_controller.rb', line 133

def edit
  # Initialize chat context
  initialize_chat current_user&.chats

  # Get LLM options available for users
  jwt_token = current_user.id_token if user_signed_in?
  @llm_families = LlmMetaClient::ServerResource.available_llm_families(jwt_token)

  @chat = Chat.find_or_switch_for_session(
    session,
    current_user
  )
  @messages = @chat&.ordered_messages || []
  # initialize history for the chat
  initialize_history @chat&.ordered_by_descending_prompt_executions
  # Set active message UUID for highlighting in UI
  set_active_message_uuid(params.dig(:chat, :branch_from_uuid))
rescue StandardError => e
  Rails.logger.error "Error in ChatsController#edit: #{e.class} - #{e.message}\n#{e.backtrace&.join("\n")}"
  @llm_families = []
  flash.now[:alert] = "Chat service is currently unavailable. Please try again later."
end

#newObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/generators/llm_meta_client/scaffold/templates/app/controllers/chats_controller.rb', line 34

def new
  initialize_chat current_user&.chats
  # Find current conversation or create it on create method if not found
  @chat = Chat.find_or_switch_for_session(
    session,
    current_user
  )
  add_chat @chat
  @messages = @chat&.ordered_messages || []
  # initialize history for the chat
  initialize_history @chat&.ordered_by_descending_prompt_executions

  # Get LLM options available for users
  jwt_token = current_user.id_token if user_signed_in?
  @llm_families = LlmMetaClient::ServerResource.available_llm_families(jwt_token)
rescue StandardError => e
  Rails.logger.error "Error in ChatsController#new: #{e.class} - #{e.message}\n#{e.backtrace&.join("\n")}"
  @llm_families = []
  flash.now[:alert] = "Chat service is currently unavailable. Please try again later."
end

#showObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/generators/llm_meta_client/scaffold/templates/app/controllers/chats_controller.rb', line 9

def show
  # Initialize chat context
  initialize_chat current_user&.chats

  @chat = current_user&.chats.includes(:messages).find_by!(uuid: params[:id])
  session[:chat_id] = @chat.id
  @messages = @chat.ordered_messages

  # Initialize history
  initialize_history @chat.ordered_by_descending_prompt_executions

  # Get LLM options available for users
  jwt_token = current_user.id_token if user_signed_in?
  @llm_families = LlmMetaClient::ServerResource.available_llm_families(jwt_token)

  # Set active UUID for history sidebar highlighting
  @prompt_execution = @chat.ordered_by_descending_prompt_executions.first
  set_active_message_uuid(@prompt_execution&.execution_id)

  render "chats/edit"
rescue StandardError => e
  Rails.logger.error "Error in PromptsController#show: #{e.class} - #{e.message}\n#{e.backtrace&.join("\n")}"
  redirect_to root_path, alert: "Message not found."
end

#start_newObject



128
129
130
131
# File 'lib/generators/llm_meta_client/scaffold/templates/app/controllers/chats_controller.rb', line 128

def start_new
  session.delete(:chat_id)
  redirect_to root_path
end

#updateObject



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/generators/llm_meta_client/scaffold/templates/app/controllers/chats_controller.rb', line 156

def update
  jwt_token = current_user.id_token if user_signed_in?

  # Use the chat identified by the URL, not the session
  @chat = current_user.chats.find(params[:id])
  session[:chat_id] = @chat.id
  @messages = @chat&.ordered_messages || []
  # initialize history for the chat
  initialize_history @chat&.ordered_by_descending_prompt_executions

  if params[:message].present?
    # Validate generation settings before proceeding (raises if invalid).
    # The streaming controller re-parses them from the URL.
    begin
      generation_settings_param
    rescue InvalidGenerationSettingsError => e
      @error_message = e.message
      respond_to do |format|
        format.turbo_stream
        format.html { redirect_to chat_path(@chat), alert: e.message }
      end
      return
    end

    # Add user message (will be rendered via turbo stream)
    @prompt_execution, @user_message = @chat.add_user_message(params[:message],
                                                             params[:api_key_uuid],
                                                             params[:model],
                                                             params[:branch_from_uuid])
    # Push to history for rendering
    push_to_history @prompt_execution
    # Set active message UUID for highlighting in UI
    set_active_message_uuid(@prompt_execution&.execution_id || params.dig(:chat, :branch_from_uuid))

    # The assistant response is streamed by ChatStreamsController (SSE).
    # See create action for details.
    @generation_settings_json = params[:generation_settings_json]
    @tool_ids = Array(params[:tool_ids]).reject(&:blank?)
  end

  # Return turbo stream to render both messages
  respond_to do |format|
    format.turbo_stream
    format.html { redirect_to new_chat_path }
  end
end

#update_titleObject



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/generators/llm_meta_client/scaffold/templates/app/controllers/chats_controller.rb', line 110

def update_title
  chat = current_user.chats.find_by!(uuid: params[:id])
  title = params[:title].to_s.strip

  if title.blank?
    render json: { error: "Title cannot be blank" }, status: :unprocessable_entity
    return
  end

  title = title.truncate(255)
  chat.update!(title: title)

  render json: {
    title: title,
    truncated_title: title.truncate(30)
  }
end