Class: RubyLLM::Generators::ChatUIGenerator
- Inherits:
-
Rails::Generators::Base
- Object
- Rails::Generators::Base
- RubyLLM::Generators::ChatUIGenerator
- 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
- #add_available_chat_models_to_application_controller ⇒ Object
- #add_broadcasting_to_message_model ⇒ Object
- #add_routes ⇒ Object
- #check_model_exists ⇒ Object
- #create_controllers ⇒ Object
- #create_helpers ⇒ Object
- #create_jobs ⇒ Object
- #create_views ⇒ Object
- #display_post_install_message ⇒ Object
Methods included from GeneratorHelpers
#acts_as_chat_declaration, #acts_as_message_declaration, #acts_as_model_declaration, #acts_as_tool_call_declaration, #create_migration_class_name, #create_namespace_modules, #migration_version, #mysql?, #parse_model_mappings, #postgresql?, #table_exists?
Instance Method Details
#add_available_chat_models_to_application_controller ⇒ Object
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/generators/ruby_llm/chat_ui/chat_ui_generator.rb', line 109 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
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 |
# File 'lib/generators/ruby_llm/chat_ui/chat_ui_generator.rb', line 165 def msg_var = variable_name_for() chat_var = variable_name_for(chat_model_name) msg_path = .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 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 "#{} model not found. Add broadcasting code to your model.", :yellow say broadcasting_callbacks, :yellow end |
#add_routes ⇒ Object
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 160 161 162 163 |
# File 'lib/generators/ruby_llm/chat_ui/chat_ui_generator.rb', line 126 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 = .demodulize.underscore.pluralize model_resource = model_model_name.demodulize.underscore.pluralize routes_content = <<~ROUTES.strip namespace :#{namespace} do resources :#{model_resource}, only: [ :index, :show ] do collection do post :refresh end end resources :#{chat_resource} do resources :#{}, only: [ :create ] end end ROUTES route routes_content else model_routes = <<~ROUTES.strip resources :#{model_table_name}, only: [ :index, :show ] do collection do post :refresh end end ROUTES route model_routes chat_routes = <<~ROUTES.strip resources :#{chat_table_name} do resources :#{}, 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 40 41 42 43 44 |
# File 'lib/generators/ruby_llm/chat_ui/chat_ui_generator.rb', line 23 def check_model_exists model_path = "app/models/#{.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:#{}" if != '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, <<~ERROR Model file not found: #{model_path} Please run the install generator first: bin/rails generate ruby_llm:install#{arg_string} Or if upgrading from <= 1.6.x, run the upgrade generator: bin/rails generate ruby_llm:upgrade_to_v1_7#{arg_string} ERROR end |
#create_controllers ⇒ Object
90 91 92 93 94 95 96 97 98 99 |
# File 'lib/generators/ruby_llm/chat_ui/chat_ui_generator.rb', line 90 def create_controllers # For namespaced models, use the proper Rails convention path chat_controller_path = chat_model_name.underscore.pluralize = .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/#{}_controller.rb" template 'controllers/models_controller.rb', "app/controllers/#{model_controller_path}_controller.rb" end |
#create_helpers ⇒ Object
105 106 107 |
# File 'lib/generators/ruby_llm/chat_ui/chat_ui_generator.rb', line 105 def create_helpers template 'helpers/messages_helper.rb', "app/helpers/#{.underscore.pluralize}_helper.rb" end |
#create_jobs ⇒ Object
101 102 103 |
# File 'lib/generators/ruby_llm/chat_ui/chat_ui_generator.rb', line 101 def create_jobs template 'jobs/chat_response_job.rb', "app/jobs/#{variable_name_for(chat_model_name)}_response_job.rb" end |
#create_views ⇒ Object
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 84 85 86 87 88 |
# File 'lib/generators/ruby_llm/chat_ui/chat_ui_generator.rb', line 46 def create_views # Design contract: # - `scaffold` should stay close to Rails scaffold ERB output. # - `tailwind` should stay close to tailwindcss-rails scaffold output. # - Only small chat-specific affordances should be layered on top. # For namespaced models, use the proper Rails convention path chat_view_path = chat_model_name.underscore.pluralize = .underscore.pluralize model_view_path = model_model_name.underscore.pluralize # Chat views 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" # Message views template ui_template('views/messages/_assistant.html.erb'), "app/views/#{}/_assistant.html.erb" template ui_template('views/messages/_user.html.erb'), "app/views/#{}/_user.html.erb" template ui_template('views/messages/_system.html.erb'), "app/views/#{}/_system.html.erb" template ui_template('views/messages/_tool.html.erb'), "app/views/#{}/_tool.html.erb" template ui_template('views/messages/_error.html.erb'), "app/views/#{}/_error.html.erb" template ui_template('views/messages/_tool_calls.html.erb'), "app/views/#{}/_tool_calls.html.erb" empty_directory "app/views/#{}/tool_calls" template ui_template('views/messages/tool_calls/_default.html.erb'), "app/views/#{}/tool_calls/_default.html.erb" empty_directory "app/views/#{}/tool_results" template ui_template('views/messages/tool_results/_default.html.erb'), "app/views/#{}/tool_results/_default.html.erb" template ui_template('views/messages/create.turbo_stream.erb'), "app/views/#{}/create.turbo_stream.erb" template ui_template('views/messages/_content.html.erb'), "app/views/#{}/_content.html.erb" template ui_template('views/messages/_form.html.erb'), "app/views/#{}/_form.html.erb" # Model views 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
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/generators/ruby_llm/chat_ui/chat_ui_generator.rb', line 193 def 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 " UI template: #{ui_variant}", :cyan say "\n Start your server and visit http://localhost:3000/#{url_path}", :cyan say "\n" end |