Skip to content

Commit da34385

Browse files
committed
Enhance chat UI generator with improved UX and models management
- Simplify form inputs: remove labels, add placeholders, autofocus - Add models table view with pricing information ($/1M tokens) - Create model partial for cleaner index view - Add refresh models functionality (synchronous, no background job) - Improve post-install message to be concise (DHH-style) - Remove README template in favor of inline generator message - Fix broadcasting setup for messages - Make forms submit on Enter key - Follow Rails scaffold conventions for views
1 parent 7a77dcc commit da34385

File tree

8 files changed

+77
-55
lines changed

8 files changed

+77
-55
lines changed

lib/generators/ruby_llm/chat_ui/chat_ui_generator.rb

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ def create_views
5959
# Model views
6060
template 'views/models/index.html.erb', "app/views/#{model_model_name.tableize}/index.html.erb"
6161
template 'views/models/show.html.erb', "app/views/#{model_model_name.tableize}/show.html.erb"
62+
template 'views/models/_model.html.erb',
63+
"app/views/#{model_model_name.tableize}/_#{model_model_name.underscore}.html.erb"
6264
end
6365

6466
def create_controllers
@@ -72,7 +74,14 @@ def create_jobs
7274
end
7375

7476
def add_routes
75-
route "resources :#{model_model_name.tableize}, only: [:index, :show]"
77+
model_routes = <<~ROUTES.strip
78+
resources :#{model_model_name.tableize}, only: [:index, :show] do
79+
collection do
80+
post :refresh
81+
end
82+
end
83+
ROUTES
84+
route model_routes
7685
chat_routes = <<~ROUTES.strip
7786
resources :#{chat_model_name.tableize} do
7887
resources :#{message_model_name.tableize}, only: [:create]
@@ -95,7 +104,11 @@ def add_broadcasting_to_message_model
95104
end
96105

97106
def display_post_install_message
98-
readme 'README' if behavior == :invoke
107+
return unless behavior == :invoke
108+
109+
say "\n ✅ Chat UI installed!", :green
110+
say "\n Start your server and visit http://localhost:3000/#{chat_model_name.tableize}", :cyan
111+
say "\n"
99112
end
100113
end
101114
end

lib/generators/ruby_llm/chat_ui/templates/README

Lines changed: 0 additions & 31 deletions
This file was deleted.

lib/generators/ruby_llm/chat_ui/templates/controllers/models_controller.rb.tt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,9 @@ class <%= model_model_name.pluralize %>Controller < ApplicationController
66
def show
77
@<%= model_model_name.underscore %> = <%= model_model_name %>.find(params[:id])
88
end
9+
10+
def refresh
11+
<%= model_model_name %>.refresh!
12+
redirect_to <%= model_model_name.tableize %>_path, notice: "<%= model_model_name.pluralize %> refreshed successfully"
13+
end
914
end
Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
<div id="<%%= dom_id <%= chat_model_name.underscore %> %>">
2-
<p><strong><%= chat_model_name %> #<%%= <%= chat_model_name.underscore %>.id %></strong></p>
3-
<p><%%= <%= chat_model_name.underscore %>.model_association.name %></p>
4-
<p><%%= <%= chat_model_name.underscore %>.created_at.strftime("%B %d, %Y at %I:%M %p") %></p>
5-
<p><%%= pluralize(<%= chat_model_name.underscore %>.messages_association.count, 'message') %></p>
2+
<div>
3+
<strong>Model:</strong>
4+
<%%= <%= chat_model_name.underscore %>.model_association.name %>
5+
</div>
6+
7+
<div>
8+
<strong>Messages:</strong>
9+
<%%= <%= chat_model_name.underscore %>.messages_association.count %>
10+
</div>
11+
12+
<div>
13+
<strong>Created:</strong>
14+
<%%= <%= chat_model_name.underscore %>.created_at.strftime("%B %d, %Y at %I:%M %p") %>
15+
</div>
616
</div>

lib/generators/ruby_llm/chat_ui/templates/views/chats/_form.html.erb.tt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,10 @@
2020
</div>
2121

2222
<div style="margin-top: 15px;">
23-
<%%= form.label :prompt, "Enter your prompt:", style: "display: block" %>
24-
<%%= form.text_area :prompt, rows: 4, style: "width: 100%; max-width: 600px;" %>
23+
<%%= form.text_field :prompt, style: "width: 100%; max-width: 600px;", placeholder: "What would you like to discuss?", autofocus: true %>
2524
</div>
2625

27-
<div style="margin-top: 15px;">
26+
<div>
2827
<%%= form.submit "Start new <%= chat_model_name.underscore.humanize.downcase %>" %>
2928
</div>
3029
<%% end %>

lib/generators/ruby_llm/chat_ui/templates/views/messages/_form.html.erb.tt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@
1212
<%% end %>
1313

1414
<div>
15-
<%%= form.label :content, "Your message:", style: "display: block" %>
16-
<%%= form.text_area :content, rows: 3, style: "width: 100%; max-width: 600px;" %>
15+
<%%= form.text_field :content, style: "width: 100%; max-width: 600px;", placeholder: "Message...", autofocus: true %>
1716
</div>
1817

1918
<div>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<tr id="<%%= dom_id <%= model_model_name.underscore %> %>">
2+
<td><%%= <%= model_model_name.underscore %>.provider %></td>
3+
<td><%%= <%= model_model_name.underscore %>.name %></td>
4+
<td><%%= number_with_delimiter(<%= model_model_name.underscore %>.context_window) if <%= model_model_name.underscore %>.context_window %></td>
5+
<td>
6+
<%% if <%= model_model_name.underscore %>.pricing && <%= model_model_name.underscore %>.pricing['text_tokens'] && <%= model_model_name.underscore %>.pricing['text_tokens']['standard'] %>
7+
<%% input = <%= model_model_name.underscore %>.pricing['text_tokens']['standard']['input_per_million'] %>
8+
<%% output = <%= model_model_name.underscore %>.pricing['text_tokens']['standard']['output_per_million'] %>
9+
<%% if input && output %>
10+
$<%%= "%.2f" % input %> / $<%%= "%.2f" % output %>
11+
<%% end %>
12+
<%% end %>
13+
</td>
14+
<td><%%= link_to "Show", <%= model_model_name.underscore %> %></td>
15+
<td><%%= link_to "Start <%= chat_model_name.underscore.humanize.downcase %>", new_<%= chat_model_name.underscore %>_path(model: <%= model_model_name.underscore %>.model_id) %></td>
16+
</tr>
Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,30 @@
1+
<p style="color: green"><%%= notice %></p>
2+
13
<%% content_for :title, "<%= model_model_name.pluralize %>" %>
24

35
<h1><%= model_model_name.pluralize %></h1>
46

5-
<%% @<%= model_model_name.tableize %>.each do |provider, <%= model_model_name.tableize %>| %>
6-
<h2><%%= provider.upcase %></h2>
7+
<p>
8+
<%%= button_to "Refresh <%= model_model_name.pluralize %>", refresh_<%= model_model_name.tableize %>_path, method: :post %>
9+
</p>
710

8-
<ul>
9-
<%% <%= model_model_name.tableize %>.each do |<%= model_model_name.underscore %>| %>
10-
<li>
11-
<strong><%%= <%= model_model_name.underscore %>.name %></strong>
12-
(<%%= <%= model_model_name.underscore %>.model_id %>)
13-
- <%%= link_to "Start <%= chat_model_name.underscore.humanize.downcase %>", new_<%= chat_model_name.underscore %>_path(model: <%= model_model_name.underscore %>.model_id) %>
14-
</li>
15-
<%% end %>
16-
</ul>
17-
<%% end %>
11+
<div id="<%= model_model_name.tableize %>">
12+
<table>
13+
<thead>
14+
<tr>
15+
<th>Provider</th>
16+
<th>Model</th>
17+
<th>Context Window</th>
18+
<th>$/1M tokens (In/Out)</th>
19+
<th colspan="2"></th>
20+
</tr>
21+
</thead>
22+
<tbody>
23+
<%% @<%= model_model_name.tableize %>.values.flatten.each do |<%= model_model_name.underscore %>| %>
24+
<%%= render <%= model_model_name.underscore %> %>
25+
<%% end %>
26+
</tbody>
27+
</table>
28+
</div>
1829

19-
<p><%%= link_to "Back to <%= chat_model_name.tableize.humanize.downcase %>", <%= chat_model_name.tableize %>_path %></p>
30+
<%%= link_to "Back to <%= chat_model_name.tableize.humanize.downcase %>", <%= chat_model_name.tableize %>_path %>

0 commit comments

Comments
 (0)