Class: RubyLLM::Generators::ChatUIGenerator

Inherits:
Rails::Generators::Base
  • Object
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

Instance Method Summary collapse

Methods included from GeneratorHelpers

#acts_as_chat_declaration, #acts_as_message_declaration, #acts_as_model_declaration, #acts_as_tool_call_declaration, #create_namespace_modules, #migration_version, #mysql?, #parse_model_mappings, #postgresql?, #table_exists?

Instance Method Details

#add_broadcasting_to_message_modelObject



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
160
161
162
163
164
165
166
167
168
169
# File 'lib/generators/ruby_llm/chat_ui/chat_ui_generator.rb', line 128

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

  # For namespaced models, we need the association name which might be different
  # e.g., for LLM::Message, the chat association might be :llm_chat
  chat_association = chat_table_name.singularize

  # Use Rails convention paths for partials
  partial_path = message_model_name.underscore.pluralize

  # For broadcasts, we need to explicitly set the partial path
  # Turbo will pass the record with the demodulized name (e.g. 'message' for Llm::Message)
  broadcasting_code = if message_model_name.include?('::')
                        partial_name = "#{partial_path}/#{message_model_name.demodulize.underscore}"
                        "                          broadcasts_to ->(\#{msg_var}) { \"\#{chat_var}_\\\#{\#{msg_var}.\#{chat_association}_id}\" },\n                            partial: \"\#{partial_name}\"\n                        RUBY\n                      else\n                        \"broadcasts_to ->(\#{msg_var}) { \\\"\#{chat_var}_\\\#{\#{msg_var}.\#{chat_association}_id}\\\" }\"\n                      end\n\n  broadcast_append_chunk_method = <<-RUBY\n\n  def broadcast_append_chunk(content)\n    broadcast_append_to \"\#{chat_var}_\\\#{\#{chat_association}_id}\",\ntarget: \"\#{msg_var}_\\\#{id}_content\",\npartial: \"\#{partial_path}/content\",\nlocals: { content: content }\n  end\n  RUBY\n\n  inject_into_file \"app/models/\#{msg_path}.rb\", before: \"end\\n\" do\n    \"  \#{broadcasting_code}\\n\#{broadcast_append_chunk_method}\"\n  end\nrescue Errno::ENOENT\n  say \"\#{message_model_name} model not found. Add broadcasting code to your model.\", :yellow\n  say \"  \#{broadcasting_code}\", :yellow\n  say broadcast_append_chunk_method, :yellow\nend\n".strip

#add_routesObject



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/generators/ruby_llm/chat_ui/chat_ui_generator.rb', line 89

def add_routes
  # For namespaced models, use Rails convention with namespace blocks
  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 = "      namespace :\#{namespace} do\n        resources :\#{model_resource}, only: [:index, :show] do\n          collection do\n            post :refresh\n          end\n        end\n        resources :\#{chat_resource} do\n          resources :\#{message_resource}, only: [:create]\n        end\n      end\n    ROUTES\n    route routes_content\n  else\n    model_routes = <<~ROUTES.strip\n      resources :\#{model_table_name}, only: [:index, :show] do\n        collection do\n          post :refresh\n        end\n      end\n    ROUTES\n    route model_routes\n    chat_routes = <<~ROUTES.strip\n      resources :\#{chat_table_name} do\n        resources :\#{message_table_name}, only: [:create]\n      end\n    ROUTES\n    route chat_routes\n  end\nend\n".strip

#check_model_existsObject

Raises:

  • (Thor::Error)


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/generators/ruby_llm/chat_ui/chat_ui_generator.rb', line 21

def check_model_exists
  model_path = "app/models/#{message_model_name.underscore}.rb"
  return if File.exist?(model_path)

  # Build the argument string for the install/upgrade commands
  args = []
  args << "chat:#{chat_model_name}" if chat_model_name != 'Chat'
  args << "message:#{message_model_name}" if message_model_name != 'Message'
  args << "model:#{model_model_name}" if model_model_name != 'Model'
  args << "tool_call:#{tool_call_model_name}" if tool_call_model_name != 'ToolCall'
  arg_string = args.any? ? " #{args.join(' ')}" : ''

  raise Thor::Error, "    Model file not found: \#{model_path}\n\n    Please run the install generator first:\n      rails generate ruby_llm:install\#{arg_string}\n\n    Or if upgrading from <= 1.6.x, run the upgrade generator:\n      rails generate ruby_llm:upgrade_to_v1_7\#{arg_string}\n  ERROR\nend\n"

#create_controllersObject



74
75
76
77
78
79
80
81
82
83
# File 'lib/generators/ruby_llm/chat_ui/chat_ui_generator.rb', line 74

def create_controllers
  # For namespaced models, use the proper Rails convention path
  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_jobsObject



85
86
87
# File 'lib/generators/ruby_llm/chat_ui/chat_ui_generator.rb', line 85

def create_jobs
  template 'jobs/chat_response_job.rb', "app/jobs/#{variable_name_for(chat_model_name)}_response_job.rb"
end

#create_viewsObject



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
# File 'lib/generators/ruby_llm/chat_ui/chat_ui_generator.rb', line 44

def create_views
  # For namespaced models, use the proper Rails convention path
  chat_view_path = chat_model_name.underscore.pluralize
  message_view_path = message_model_name.underscore.pluralize
  model_view_path = model_model_name.underscore.pluralize

  # Chat views
  template 'views/chats/index.html.erb', "app/views/#{chat_view_path}/index.html.erb"
  template 'views/chats/new.html.erb', "app/views/#{chat_view_path}/new.html.erb"
  template 'views/chats/show.html.erb', "app/views/#{chat_view_path}/show.html.erb"
  template 'views/chats/_chat.html.erb',
           "app/views/#{chat_view_path}/_#{chat_model_name.demodulize.underscore}.html.erb"
  template 'views/chats/_form.html.erb', "app/views/#{chat_view_path}/_form.html.erb"

  # Message views
  template 'views/messages/_message.html.erb',
           "app/views/#{message_view_path}/_#{message_model_name.demodulize.underscore}.html.erb"
  template 'views/messages/_tool_calls.html.erb',
           "app/views/#{message_view_path}/_tool_calls.html.erb"
  template 'views/messages/_content.html.erb', "app/views/#{message_view_path}/_content.html.erb"
  template 'views/messages/_form.html.erb', "app/views/#{message_view_path}/_form.html.erb"
  template 'views/messages/create.turbo_stream.erb', "app/views/#{message_view_path}/create.turbo_stream.erb"

  # Model views
  template 'views/models/index.html.erb', "app/views/#{model_view_path}/index.html.erb"
  template 'views/models/show.html.erb', "app/views/#{model_view_path}/show.html.erb"
  template 'views/models/_model.html.erb',
           "app/views/#{model_view_path}/_#{model_model_name.demodulize.underscore}.html.erb"
end

#display_post_install_messageObject



171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/generators/ruby_llm/chat_ui/chat_ui_generator.rb', line 171

def display_post_install_message
  return unless behavior == :invoke

  # Show the correct URL based on whether models are namespaced
  url_path = if chat_model_name.include?('::')
               chat_model_name.underscore.pluralize
             else
               chat_table_name
             end

  say "\n  ✅ Chat UI installed!", :green
  say "\n  Start your server and visit http://localhost:3000/#{url_path}", :cyan
  say "\n"
end