Class: RubyLLM::Generators::ChatUIGenerator
- Inherits:
-
Rails::Generators::Base
- Object
- Rails::Generators::Base
- RubyLLM::Generators::ChatUIGenerator
show all
- Includes:
- GeneratorHelpers
- Defined in:
- lib/generators/ruby_llm/chat_ui/chat_ui_generator.rb
Overview
Generates a simple chat UI scaffold for RubyLLM
Constant Summary
GeneratorHelpers::APPLICATION_MODEL_TYPES, GeneratorHelpers::DEFAULT_MODEL_NAMES
Instance Method Summary
collapse
#acts_as_chat_declaration, #acts_as_message_declaration, #chat_controller_class_name, #chat_job_class_name, #create_migration_class_name, #create_namespace_modules, #gem_in_bundle?, #message_controller_class_name, #migration_version, #model_controller_class_name, #model_model_name, #model_resource_name, #model_variable_name, #mysql?, #parse_model_mappings, #postgresql?, #reference_type, #table_exists?, #tailwind_available?, #tool_call_variable_name, #ui_template, #ui_variant, #unprocessable_content_status, #usage_operations_sql, #usage_statuses_sql, #validate_model_mapping!
Instance Method Details
#add_available_chat_models_to_application_controller ⇒ Object
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
|
# File 'lib/generators/ruby_llm/chat_ui/chat_ui_generator.rb', line 104
def add_available_chat_models_to_application_controller
path = 'app/controllers/application_controller.rb'
return unless File.exist?(path)
application_controller = File.read(path)
return if application_controller.include?('def available_chat_models')
inject_into_file path, <<-RUBY, before: /^end\s*\z/
private
def available_chat_models
RubyLLM.models.chat_models.all
.sort_by { |model| [ model.provider.to_s, model.name.to_s ] }
end
RUBY
end
|
#add_broadcasting_to_message_model ⇒ Object
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
|
# File 'lib/generators/ruby_llm/chat_ui/chat_ui_generator.rb', line 161
def add_broadcasting_to_message_model
msg_var = variable_name_for(message_model_name)
chat_var = variable_name_for(chat_model_name)
msg_path = message_model_name.underscore
chat_association = chat_table_name.singularize
broadcasting_callbacks = <<-RUBY
broadcasts_to ->(#{msg_var}) { "#{chat_var}_\#{#{msg_var}.#{chat_association}_id}" }, inserts_by: :append
def broadcast_append_chunk(content)
broadcast_append_to "#{chat_var}_\#{#{chat_association}_id}",
target: "#{msg_var}_\#{id}_content",
content: ERB::Util.html_escape(content.to_s)
end
RUBY
inject_into_file "app/models/#{msg_path}.rb", before: "end\n" do
broadcasting_callbacks
end
rescue Errno::ENOENT
say "#{message_model_name} model not found. Add broadcasting code to your model.", :yellow
say broadcasting_callbacks, :yellow
end
|
#add_routes ⇒ Object
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
|
# File 'lib/generators/ruby_llm/chat_ui/chat_ui_generator.rb', line 122
def add_routes
if chat_model_name.include?('::')
namespace = chat_model_name.deconstantize.underscore
chat_resource = chat_model_name.demodulize.underscore.pluralize
message_resource = message_model_name.demodulize.underscore.pluralize
model_resource = model_model_name.demodulize.underscore.pluralize
routes_content = <<~ROUTES.strip
namespace :#{namespace} do
resources :#{model_resource}, only: [ :index, :show ], constraints: { id: /[^\\/]+/ } do
collection do
post :refresh
end
end
resources :#{chat_resource} do
resources :#{message_resource}, only: [ :create ]
end
end
ROUTES
route routes_content
else
model_routes = <<~ROUTES.strip
resources :#{model_resource_name}, only: [ :index, :show ], constraints: { id: /[^\\/]+/ } do
collection do
post :refresh
end
end
ROUTES
route model_routes
chat_routes = <<~ROUTES.strip
resources :#{chat_table_name} do
resources :#{message_table_name}, only: [ :create ]
end
ROUTES
route chat_routes
end
end
|
#check_model_exists ⇒ Object
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
# File 'lib/generators/ruby_llm/chat_ui/chat_ui_generator.rb', line 23
def check_model_exists
model_path = "app/models/#{message_model_name.underscore}.rb"
return if File.exist?(model_path)
args = []
args << "chat:#{chat_model_name}" if chat_model_name != 'Chat'
args << "message:#{message_model_name}" if message_model_name != 'Message'
arg_string = args.any? ? " #{args.join(' ')}" : ''
raise Thor::Error, <<~ERROR
Model file not found: #{model_path}
Please run the install generator first:
bin/rails generate ruby_llm:install#{arg_string}
ERROR
end
|
#create_controllers ⇒ Object
85
86
87
88
89
90
91
92
93
94
|
# File 'lib/generators/ruby_llm/chat_ui/chat_ui_generator.rb', line 85
def create_controllers
chat_controller_path = chat_model_name.underscore.pluralize
message_controller_path = message_model_name.underscore.pluralize
model_controller_path = model_model_name.underscore.pluralize
template 'controllers/chats_controller.rb', "app/controllers/#{chat_controller_path}_controller.rb"
template 'controllers/messages_controller.rb', "app/controllers/#{message_controller_path}_controller.rb"
template 'controllers/models_controller.rb', "app/controllers/#{model_controller_path}_controller.rb"
end
|
#create_helpers ⇒ Object
100
101
102
|
# File 'lib/generators/ruby_llm/chat_ui/chat_ui_generator.rb', line 100
def create_helpers
template 'helpers/messages_helper.rb', "app/helpers/#{message_model_name.underscore.pluralize}_helper.rb"
end
|
#create_jobs ⇒ Object
96
97
98
|
# File 'lib/generators/ruby_llm/chat_ui/chat_ui_generator.rb', line 96
def create_jobs
template 'jobs/chat_response_job.rb', "app/jobs/#{variable_name_for(chat_model_name)}_response_job.rb"
end
|
#create_views ⇒ Object
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
# File 'lib/generators/ruby_llm/chat_ui/chat_ui_generator.rb', line 41
def create_views
chat_view_path = chat_model_name.underscore.pluralize
message_view_path = message_model_name.underscore.pluralize
model_view_path = model_model_name.underscore.pluralize
template ui_template('views/chats/index.html.erb'), "app/views/#{chat_view_path}/index.html.erb"
template ui_template('views/chats/new.html.erb'), "app/views/#{chat_view_path}/new.html.erb"
template ui_template('views/chats/show.html.erb'), "app/views/#{chat_view_path}/show.html.erb"
template ui_template('views/chats/_chat.html.erb'),
"app/views/#{chat_view_path}/_#{chat_model_name.demodulize.underscore}.html.erb"
template ui_template('views/chats/_form.html.erb'), "app/views/#{chat_view_path}/_form.html.erb"
template ui_template('views/messages/_assistant.html.erb'), "app/views/#{message_view_path}/_assistant.html.erb"
template ui_template('views/messages/_user.html.erb'), "app/views/#{message_view_path}/_user.html.erb"
template ui_template('views/messages/_system.html.erb'), "app/views/#{message_view_path}/_system.html.erb"
template ui_template('views/messages/_tool.html.erb'), "app/views/#{message_view_path}/_tool.html.erb"
template ui_template('views/messages/_error.html.erb'), "app/views/#{message_view_path}/_error.html.erb"
template ui_template('views/messages/_tool_calls.html.erb'),
"app/views/#{message_view_path}/_tool_calls.html.erb"
empty_directory "app/views/#{message_view_path}/tool_calls"
template ui_template('views/messages/tool_calls/_default.html.erb'),
"app/views/#{message_view_path}/tool_calls/_default.html.erb"
empty_directory "app/views/#{message_view_path}/tool_results"
template ui_template('views/messages/tool_results/_default.html.erb'),
"app/views/#{message_view_path}/tool_results/_default.html.erb"
template ui_template('views/messages/create.turbo_stream.erb'),
"app/views/#{message_view_path}/create.turbo_stream.erb"
template ui_template('views/messages/_content.html.erb'), "app/views/#{message_view_path}/_content.html.erb"
template ui_template('views/messages/_form.html.erb'), "app/views/#{message_view_path}/_form.html.erb"
template ui_template('views/models/index.html.erb'), "app/views/#{model_view_path}/index.html.erb"
template ui_template('views/models/show.html.erb'), "app/views/#{model_view_path}/show.html.erb"
template ui_template('views/models/_model.html.erb'),
"app/views/#{model_view_path}/_#{model_model_name.demodulize.underscore}.html.erb"
end
|
#display_post_install_message ⇒ Object
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
|
# File 'lib/generators/ruby_llm/chat_ui/chat_ui_generator.rb', line 189
def display_post_install_message
return unless behavior == :invoke
url_path = if chat_model_name.include?('::')
chat_model_name.underscore.pluralize
else
chat_table_name
end
say "\n ✅ Chat UI installed!", :green
say " UI template: #{ui_variant}", :cyan
say "\n Start your server and visit http://localhost:3000/#{url_path}", :cyan
say "\n"
end
|