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
#create ⇒ Object
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 current_user&.chats
@chat = Chat.find_or_switch_for_session(
session,
current_user
)
add_chat @chat
@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])
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
|
#edit ⇒ Object
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 current_user&.chats
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 @chat&.ordered_by_descending_prompt_executions
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
|
#new ⇒ Object
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
@chat = Chat.find_or_switch_for_session(
session,
current_user
)
add_chat @chat
@messages = @chat&.ordered_messages || []
initialize_history @chat&.ordered_by_descending_prompt_executions
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
|
#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])
session[:chat_id] = @chat.id
@messages = @chat.ordered_messages
initialize_history @chat.ordered_by_descending_prompt_executions
jwt_token = current_user.id_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
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
|
#update ⇒ Object
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?
@chat = current_user.chats.find(params[:id])
session[:chat_id] = @chat.id
@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 chat_path(@chat), 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])
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
|
#update_title ⇒ Object
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
|