Class: ChatsController
- Inherits:
-
ApplicationController
- Object
- ApplicationController
- ChatsController
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
#add_prompt ⇒ Object
Add a prompt to a specific chat identified by URL uuid. This is the tab-safe entry point: chat identity comes from the URL, not session, so navigation in another tab can never re-target the prompt.
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
202
203
204
205
|
# File 'lib/generators/llm_meta_client/scaffold/templates/app/controllers/chats_controller.rb', line 160
def add_prompt
jwt_token = current_user.jwt_token if user_signed_in?
scope = user_signed_in? ? current_user.chats : Chat.where(user_id: nil)
@chat = scope.find_by!(uuid: params[:id])
initialize_chat current_user&.chats
add_chat @chat
set_active_chat_uuid(@chat&.uuid)
@messages = @chat&.ordered_messages || []
initialize_history @chat&.ordered_by_descending_prompt_executions
if params[:message].present?
begin
generation_settings_param
rescue InvalidGenerationSettingsError => e
@error_message = e.message
respond_to do |format|
format.turbo_stream { render :create }
format.html { redirect_to chat_path(@chat.uuid), alert: e.message }
end
return
end
@prompt_execution, @user_message = @chat.add_user_message(params[:message],
params[:api_key_uuid],
params[:model],
params[:branch_from_uuid],
llm_platform: params[:family],
image: uploaded_image_payload)
push_to_history @prompt_execution
set_active_message_uuid(@prompt_execution&.execution_id || params.dig(:chat, :branch_from_uuid))
@generation_settings_json = params[:generation_settings_json]
@tool_ids = Array(params[:tool_ids]).reject(&:blank?)
end
@llm_families = LlmMetaClient::ServerResource.available_llm_families(jwt_token) rescue []
respond_to do |format|
format.turbo_stream { render :create }
format.html { redirect_to chat_path(@chat.uuid) }
end
rescue ActiveRecord::RecordNotFound
redirect_to root_path, alert: "Chat not found."
end
|
#batch_destroy ⇒ Object
128
129
130
131
132
133
|
# File 'lib/generators/llm_meta_client/scaffold/templates/app/controllers/chats_controller.rb', line 128
def batch_destroy
scope = user_signed_in? ? current_user.chats : Chat.where(user_id: nil)
uuids = Array(params[:uuids]).reject(&:blank?)
scope.where(uuid: uuids).destroy_all
redirect_to root_path
end
|
#create ⇒ Object
51
52
53
54
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
|
# File 'lib/generators/llm_meta_client/scaffold/templates/app/controllers/chats_controller.rb', line 51
def create
jwt_token = current_user.jwt_token if user_signed_in?
initialize_chat current_user&.chats
@chat = Chat.create!(user: current_user)
add_chat @chat
set_active_chat_uuid(@chat&.uuid)
@messages = @chat&.ordered_messages || []
initialize_history @chat&.ordered_by_descending_prompt_executions
if params[:message].present?
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
@prompt_execution, @user_message = @chat.add_user_message(params[:message],
params[:api_key_uuid],
params[:model],
params[:branch_from_uuid],
llm_platform: params[:family],
image: uploaded_image_payload)
push_to_history @prompt_execution
set_active_message_uuid(@prompt_execution&.execution_id || params.dig(:chat, :branch_from_uuid))
@generation_settings_json = params[:generation_settings_json]
@tool_ids = Array(params[:tool_ids]).reject(&:blank?)
end
respond_to do |format|
format.turbo_stream
format.html { redirect_to new_chat_path }
end
end
|
#destroy ⇒ Object
108
109
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 108
def destroy
scope = user_signed_in? ? current_user.chats : Chat.where(user_id: nil)
chat = scope.find_by(uuid: params[:id])
was_viewed = chat && request.referer.to_s.include?("/chats/#{chat.uuid}")
chat&.destroy
initialize_chat(user_signed_in? ? current_user.chats : nil)
if was_viewed
redirect_to root_path
else
respond_to do |format|
format.turbo_stream
format.html { redirect_to root_path }
end
end
end
|
#new ⇒ Object
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
# File 'lib/generators/llm_meta_client/scaffold/templates/app/controllers/chats_controller.rb', line 34
def new
initialize_chat current_user&.chats
@chat = nil
@messages = []
initialize_history []
jwt_token = current_user.jwt_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
|
#show ⇒ Object
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 current_user&.chats
@chat = current_user&.chats.includes(:messages).find_by!(uuid: params[:id])
set_active_chat_uuid(@chat&.uuid)
@messages = @chat.ordered_messages
initialize_history @chat.ordered_by_descending_prompt_executions
jwt_token = current_user.jwt_token if user_signed_in?
@llm_families = LlmMetaClient::ServerResource.available_llm_families(jwt_token)
@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_new ⇒ Object
153
154
155
|
# File 'lib/generators/llm_meta_client/scaffold/templates/app/controllers/chats_controller.rb', line 153
def start_new
redirect_to root_path
end
|
#update_title ⇒ Object
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
|
# File 'lib/generators/llm_meta_client/scaffold/templates/app/controllers/chats_controller.rb', line 135
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
|