ask-rails 0.2.5 → 0.11.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +157 -0
- data/README.md +94 -10
- data/app/controllers/ask/rails/chat_controller.rb +308 -0
- data/app/models/ask/rails/session.rb +23 -0
- data/app/views/ask/rails/chat/index.html.erb +6 -0
- data/app/views/layouts/ask/rails/application.html.erb +521 -0
- data/config/routes.rb +17 -0
- data/lib/ask/rails/audit_log.rb +181 -0
- data/lib/ask/rails/auth.rb +22 -0
- data/lib/ask/rails/configuration.rb +53 -1
- data/lib/ask/rails/engine.rb +1 -1
- data/lib/ask/rails/environment_permissions.rb +39 -0
- data/lib/ask/rails/persistence.rb +18 -5
- data/lib/ask/rails/railtie.rb +5 -1
- data/lib/ask/rails/tool.rb +29 -0
- data/lib/ask/rails/tools/read_file.rb +1 -4
- data/lib/ask/rails/tools/read_routes.rb +1 -4
- data/lib/ask/rails/tools/route_inspector.rb +69 -0
- data/lib/ask/rails/tools/run_command.rb +39 -1
- data/lib/ask/rails/tools/schema_graph.rb +174 -0
- data/lib/ask/rails/tools/search_codebase.rb +6 -7
- data/lib/ask/rails/version.rb +1 -1
- data/lib/ask/rails.rb +111 -4
- data/lib/generators/ask/rails/install/install_generator.rb +5 -0
- data/lib/generators/ask/rails/install/templates/audit_log_migration.rb +22 -0
- data/lib/generators/ask/rails/install/templates/initializer.rb +1 -1
- metadata +12 -1
|
@@ -0,0 +1,521 @@
|
|
|
1
|
+
<!DOCTYPE html>
|
|
2
|
+
<html>
|
|
3
|
+
<head>
|
|
4
|
+
<title>Ask — Rails Agent</title>
|
|
5
|
+
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
6
|
+
<style>
|
|
7
|
+
* { margin: 0; padding: 0; box-sizing: border-box; }
|
|
8
|
+
body { font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif; background: #0d0d0f; color: #e1e1e6; height: 100vh; display: flex; overflow: hidden; }
|
|
9
|
+
a { color: #8b8bf0; text-decoration: none; }
|
|
10
|
+
a:hover { color: #a0a0ff; }
|
|
11
|
+
|
|
12
|
+
/* --- Sidebar --- */
|
|
13
|
+
.sidebar { width: 300px; background: #121218; border-right: 1px solid #1e1e28; display: flex; flex-direction: column; flex-shrink: 0; }
|
|
14
|
+
.sidebar-header { padding: 16px; border-bottom: 1px solid #1e1e28; display: flex; justify-content: space-between; align-items: center; }
|
|
15
|
+
.sidebar-header h1 { font-size: 16px; font-weight: 600; color: #c0c0d0; }
|
|
16
|
+
.sidebar-header .model-badge { font-size: 10px; background: #1a1a28; color: #7070a0; padding: 2px 8px; border-radius: 4px; }
|
|
17
|
+
.sidebar-tabs { display: flex; border-bottom: 1px solid #1e1e28; }
|
|
18
|
+
.sidebar-tab { flex: 1; padding: 10px; text-align: center; font-size: 12px; color: #606070; cursor: pointer; border-bottom: 2px solid transparent; }
|
|
19
|
+
.sidebar-tab:hover { color: #9090a0; }
|
|
20
|
+
.sidebar-tab.active { color: #c0c0d0; border-bottom-color: #5050a0; }
|
|
21
|
+
.sidebar-actions { padding: 12px 16px; }
|
|
22
|
+
.sidebar-actions .new-btn { display: block; padding: 8px 12px; background: #1a1a24; border-radius: 6px; font-size: 13px; text-align: center; color: #c0c0d0; cursor: pointer; }
|
|
23
|
+
.sidebar-actions .new-btn:hover { background: #222230; }
|
|
24
|
+
.sidebar-content { flex: 1; overflow-y: auto; }
|
|
25
|
+
|
|
26
|
+
/* Session list */
|
|
27
|
+
.session-list { padding: 8px; }
|
|
28
|
+
.session-item { padding: 10px 12px; border-radius: 6px; font-size: 13px; cursor: pointer; color: #9090a0; }
|
|
29
|
+
.session-item:hover { background: #1a1a24; color: #c0c0d0; }
|
|
30
|
+
.session-item.active { background: #1e1e30; color: #d0d0e0; }
|
|
31
|
+
.session-item .sess-preview { font-size: 12px; color: #606070; margin-top: 2px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
|
|
32
|
+
.session-item .sess-meta { font-size: 11px; color: #404050; margin-top: 2px; display: flex; gap: 8px; }
|
|
33
|
+
.session-item .sess-delete { float: right; color: #404050; cursor: pointer; padding: 2px 4px; border-radius: 3px; }
|
|
34
|
+
.session-item .sess-delete:hover { color: #ff5050; background: #2a1a1a; }
|
|
35
|
+
|
|
36
|
+
/* Audit log panel */
|
|
37
|
+
.audit-panel { display: none; padding: 8px; }
|
|
38
|
+
.audit-panel.active { display: block; }
|
|
39
|
+
.audit-entry { padding: 8px 10px; border-radius: 4px; font-size: 12px; margin-bottom: 4px; background: #0d0d12; }
|
|
40
|
+
.audit-entry .audit-tool { font-weight: 600; color: #9090c0; }
|
|
41
|
+
.audit-entry .audit-status { font-size: 11px; padding: 1px 6px; border-radius: 3px; margin-left: 4px; }
|
|
42
|
+
.audit-entry .audit-status.success { background: #0a1a0a; color: #40a040; }
|
|
43
|
+
.audit-entry .audit-status.rejected { background: #1a0a0a; color: #c04040; }
|
|
44
|
+
.audit-entry .audit-status.error { background: #1a0a0a; color: #c04040; }
|
|
45
|
+
.audit-entry .audit-time { font-size: 10px; color: #404050; }
|
|
46
|
+
.audit-entry .audit-duration { font-size: 10px; color: #505060; margin-left: 4px; }
|
|
47
|
+
.sidebar-footer { padding: 8px 16px; border-top: 1px solid #1e1e28; font-size: 10px; color: #303040; display: flex; justify-content: space-between; }
|
|
48
|
+
|
|
49
|
+
/* --- Main chat area --- */
|
|
50
|
+
.main { flex: 1; display: flex; flex-direction: column; min-width: 0; }
|
|
51
|
+
.chat-header { padding: 14px 20px; border-bottom: 1px solid #1e1e28; font-size: 13px; color: #808090; display: flex; justify-content: space-between; align-items: center; }
|
|
52
|
+
.chat-header .env-badge { font-size: 10px; padding: 2px 8px; border-radius: 4px; background: #1a1a28; color: #606090; }
|
|
53
|
+
|
|
54
|
+
/* Messages */
|
|
55
|
+
.messages { flex: 1; overflow-y: auto; padding: 20px; display: flex; flex-direction: column; gap: 12px; }
|
|
56
|
+
.message { max-width: 85%; padding: 12px 16px; border-radius: 10px; font-size: 14px; line-height: 1.5; white-space: pre-wrap; }
|
|
57
|
+
.message.user { background: #1e1e30; align-self: flex-end; border-bottom-right-radius: 4px; }
|
|
58
|
+
.message.assistant { background: #181825; align-self: flex-start; border-bottom-left-radius: 4px; }
|
|
59
|
+
.message.thinking { background: #151520; align-self: flex-start; font-style: italic; color: #707080; font-size: 13px; border-left: 2px solid #404068; }
|
|
60
|
+
.message.system { background: #101018; align-self: center; color: #505060; font-size: 12px; text-align: center; }
|
|
61
|
+
.message .msg-label { font-size: 11px; color: #606070; margin-bottom: 4px; font-weight: 500; }
|
|
62
|
+
.empty-state { flex: 1; display: flex; align-items: center; justify-content: center; color: #404050; font-size: 14px; text-align: center; padding: 40px; }
|
|
63
|
+
.empty-state p { max-width: 300px; line-height: 1.6; }
|
|
64
|
+
|
|
65
|
+
/* Tool execution cards */
|
|
66
|
+
.tool-card { align-self: flex-start; width: 100%; max-width: 85%; margin: 4px 0; }
|
|
67
|
+
.tool-card-header { display: flex; align-items: center; gap: 8px; padding: 8px 12px; background: #151520; border: 1px solid #1e1e30; border-radius: 8px; cursor: pointer; font-size: 13px; color: #9090b0; }
|
|
68
|
+
.tool-card-header:hover { background: #1a1a28; }
|
|
69
|
+
.tool-card-header .tool-icon { width: 20px; height: 20px; border-radius: 4px; display: flex; align-items: center; justify-content: center; font-size: 11px; flex-shrink: 0; }
|
|
70
|
+
.tool-card-header .tool-icon.start { background: #1a2a1a; color: #40c040; }
|
|
71
|
+
.tool-card-header .tool-icon.error { background: #2a1a1a; color: #c04040; }
|
|
72
|
+
.tool-card-header .tool-name { font-weight: 600; }
|
|
73
|
+
.tool-card-header .tool-status { font-size: 11px; color: #606080; }
|
|
74
|
+
.tool-card-header .tool-duration { font-size: 11px; color: #505060; margin-left: auto; }
|
|
75
|
+
.tool-card-body { display: none; padding: 10px 12px; background: #101018; border: 1px solid #1e1e30; border-top: none; border-radius: 0 0 8px 8px; font-size: 12px; color: #808090; }
|
|
76
|
+
.tool-card-body.open { display: block; }
|
|
77
|
+
.tool-card-body pre { background: #0a0a10; padding: 8px; border-radius: 4px; overflow-x: auto; font-size: 11px; color: #b0b0c0; margin-top: 6px; }
|
|
78
|
+
.tool-card-body .param-row { display: flex; gap: 8px; padding: 2px 0; }
|
|
79
|
+
.tool-card-body .param-key { color: #606090; flex-shrink: 0; }
|
|
80
|
+
.tool-card-body .param-value { color: #b0b0c0; overflow: hidden; text-overflow: ellipsis; }
|
|
81
|
+
|
|
82
|
+
/* Input */
|
|
83
|
+
.input-area { padding: 16px 20px; border-top: 1px solid #1e1e28; }
|
|
84
|
+
.input-row { display: flex; gap: 8px; }
|
|
85
|
+
.input-row input { flex: 1; padding: 10px 14px; background: #181825; border: 1px solid #252535; border-radius: 8px; color: #e1e1e6; font-size: 14px; outline: none; }
|
|
86
|
+
.input-row input:focus { border-color: #404068; }
|
|
87
|
+
.input-row button { padding: 10px 20px; background: #404068; border: none; border-radius: 8px; color: #e1e1e6; font-size: 14px; cursor: pointer; font-weight: 500; }
|
|
88
|
+
.input-row button:hover { background: #505080; }
|
|
89
|
+
.input-row button:disabled { opacity: 0.4; cursor: default; }
|
|
90
|
+
.input-hint { font-size: 11px; color: #303040; margin-top: 6px; text-align: center; }
|
|
91
|
+
|
|
92
|
+
/* Scrollbar */
|
|
93
|
+
::-webkit-scrollbar { width: 6px; }
|
|
94
|
+
::-webkit-scrollbar-track { background: transparent; }
|
|
95
|
+
::-webkit-scrollbar-thumb { background: #252535; border-radius: 3px; }
|
|
96
|
+
::-webkit-scrollbar-thumb:hover { background: #353545; }
|
|
97
|
+
|
|
98
|
+
/* Search */
|
|
99
|
+
.search-box { padding: 8px 12px; }
|
|
100
|
+
.search-box input { width: 100%; padding: 6px 10px; background: #0d0d12; border: 1px solid #1e1e28; border-radius: 4px; color: #c0c0d0; font-size: 12px; outline: none; }
|
|
101
|
+
.search-box input:focus { border-color: #353550; }
|
|
102
|
+
.search-box input::placeholder { color: #404050; }
|
|
103
|
+
</style>
|
|
104
|
+
</head>
|
|
105
|
+
<body>
|
|
106
|
+
<!-- Sidebar -->
|
|
107
|
+
<div class="sidebar">
|
|
108
|
+
<div class="sidebar-header">
|
|
109
|
+
<h1>Ask</h1>
|
|
110
|
+
<span class="model-badge"><%= Ask::Rails.configuration.default_model %></span>
|
|
111
|
+
</div>
|
|
112
|
+
<div class="sidebar-tabs">
|
|
113
|
+
<div class="sidebar-tab active" onclick="switchSidebarTab('sessions')" id="tab-sessions">Sessions</div>
|
|
114
|
+
<div class="sidebar-tab" onclick="switchSidebarTab('audit')" id="tab-audit">Activity</div>
|
|
115
|
+
</div>
|
|
116
|
+
<div class="sidebar-actions">
|
|
117
|
+
<%= button_to "", ask_rails.sessions_path, method: :post, style: "display: none", id: "new-session-form" %>
|
|
118
|
+
<a class="new-btn" href="#" onclick="document.getElementById('new-session-form').click(); return false;">+ New Session</a>
|
|
119
|
+
</div>
|
|
120
|
+
|
|
121
|
+
<!-- Session list tab -->
|
|
122
|
+
<div class="sidebar-content session-list" id="panel-sessions">
|
|
123
|
+
<div class="search-box">
|
|
124
|
+
<input type="text" id="session-search" placeholder="Search sessions..." oninput="filterSessions()">
|
|
125
|
+
</div>
|
|
126
|
+
<div id="session-items">
|
|
127
|
+
<% @sessions.each do |s| %>
|
|
128
|
+
<div class="session-item<%= s[:id] == (@active_session&.dig(:id) || params[:session_id]) ? ' active' : '' %>" data-session-id="<%= s[:id] %>" onclick="switchSession('<%= s[:id] %>')">
|
|
129
|
+
<span class="sess-delete" onclick="event.stopPropagation(); deleteSession('<%= s[:id] %>')">✕</span>
|
|
130
|
+
<%= s[:preview].present? ? s[:preview] : "Session #{s[:id].to_s[0..7]}" %>
|
|
131
|
+
<div class="sess-meta">
|
|
132
|
+
<span><%= s[:message_count] %> messages</span>
|
|
133
|
+
<% if s[:created_at] %><span><%= time_ago(s[:created_at]) %></span><% end %>
|
|
134
|
+
</div>
|
|
135
|
+
</div>
|
|
136
|
+
<% end %>
|
|
137
|
+
</div>
|
|
138
|
+
</div>
|
|
139
|
+
|
|
140
|
+
<!-- Audit log tab -->
|
|
141
|
+
<div class="sidebar-content audit-panel" id="panel-audit">
|
|
142
|
+
<% if @audit_logs.any? %>
|
|
143
|
+
<% @audit_logs.each do |entry| %>
|
|
144
|
+
<div class="audit-entry">
|
|
145
|
+
<span class="audit-tool"><%= entry[:tool_name] %></span>
|
|
146
|
+
<span class="audit-status <%= entry[:status] %>"><%= entry[:status] %></span>
|
|
147
|
+
<span class="audit-duration"><%= entry[:duration_ms] %>ms</span>
|
|
148
|
+
<div class="audit-time"><%= entry[:recorded_at] %></div>
|
|
149
|
+
</div>
|
|
150
|
+
<% end %>
|
|
151
|
+
<% else %>
|
|
152
|
+
<div style="padding: 20px; text-align: center; color: #404050; font-size: 12px;">
|
|
153
|
+
No tool activity yet. Start a session to see audit logs.
|
|
154
|
+
</div>
|
|
155
|
+
<% end %>
|
|
156
|
+
</div>
|
|
157
|
+
|
|
158
|
+
<div class="sidebar-footer">
|
|
159
|
+
<span>v<%= Ask::Rails::VERSION %></span>
|
|
160
|
+
<span><%= ::Rails.env %></span>
|
|
161
|
+
</div>
|
|
162
|
+
</div>
|
|
163
|
+
|
|
164
|
+
<!-- Main chat area -->
|
|
165
|
+
<div class="main">
|
|
166
|
+
<div class="chat-header" id="chat-header">
|
|
167
|
+
<span id="header-title">
|
|
168
|
+
<% if @active_session %>
|
|
169
|
+
<%= @active_session[:preview].present? ? @active_session[:preview].truncate(50) : "Session #{@active_session[:id].to_s[0..7]}" %>
|
|
170
|
+
<% else %>
|
|
171
|
+
Start a new session
|
|
172
|
+
<% end %>
|
|
173
|
+
</span>
|
|
174
|
+
<span class="env-badge"><%= ::Rails.env %></span>
|
|
175
|
+
</div>
|
|
176
|
+
<div class="messages" id="messages">
|
|
177
|
+
<div class="empty-state" id="empty-state">
|
|
178
|
+
<p>Send a message to start talking to the Rails agent.</p>
|
|
179
|
+
</div>
|
|
180
|
+
</div>
|
|
181
|
+
<div class="input-area">
|
|
182
|
+
<div class="input-row">
|
|
183
|
+
<input type="text" id="message-input" placeholder="Ask about your app..." autofocus>
|
|
184
|
+
<button id="send-button" onclick="sendMessage()">Send</button>
|
|
185
|
+
</div>
|
|
186
|
+
<div class="input-hint">Enter to send · <span id="stream-indicator"></span></div>
|
|
187
|
+
</div>
|
|
188
|
+
</div>
|
|
189
|
+
|
|
190
|
+
<script>
|
|
191
|
+
let currentSessionId = '<%= @active_session&.dig(:id) || "" %>';
|
|
192
|
+
let isStreaming = false;
|
|
193
|
+
let currentToolId = null;
|
|
194
|
+
|
|
195
|
+
// --- Sidebar tabs ---
|
|
196
|
+
function switchSidebarTab(tab) {
|
|
197
|
+
document.querySelectorAll('.sidebar-tab').forEach(t => t.classList.remove('active'));
|
|
198
|
+
document.getElementById('tab-' + tab).classList.add('active');
|
|
199
|
+
document.querySelectorAll('.sidebar-content').forEach(p => p.classList.remove('active'));
|
|
200
|
+
document.getElementById('panel-' + tab).classList.add('active');
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
// --- Session search ---
|
|
204
|
+
function filterSessions() {
|
|
205
|
+
const q = document.getElementById('session-search').value.toLowerCase();
|
|
206
|
+
document.querySelectorAll('.session-item').forEach(item => {
|
|
207
|
+
const match = item.textContent.toLowerCase().includes(q);
|
|
208
|
+
item.style.display = match ? '' : 'none';
|
|
209
|
+
});
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
// --- Chat messages ---
|
|
213
|
+
function addMessage(role, content) {
|
|
214
|
+
const el = document.getElementById('empty-state');
|
|
215
|
+
if (el) el.remove();
|
|
216
|
+
|
|
217
|
+
const msg = document.createElement('div');
|
|
218
|
+
msg.className = 'message ' + role;
|
|
219
|
+
msg.innerHTML = (role === 'user' ? '' : '<div class="msg-label">' + (role === 'thinking' ? 'Thinking...' : 'Assistant') + '</div>') + escapeHtml(content);
|
|
220
|
+
document.getElementById('messages').appendChild(msg);
|
|
221
|
+
msg.scrollIntoView({ behavior: 'smooth' });
|
|
222
|
+
return msg;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
function updateLastMessage(role, content) {
|
|
226
|
+
const msgs = document.getElementById('messages').querySelectorAll('.message.' + role);
|
|
227
|
+
if (msgs.length === 0) return addMessage(role, content);
|
|
228
|
+
const last = msgs[msgs.length - 1];
|
|
229
|
+
const textEl = last.querySelector('div:last-child') || last;
|
|
230
|
+
textEl.textContent += content;
|
|
231
|
+
last.scrollIntoView({ behavior: 'smooth' });
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
// --- Tool execution cards ---
|
|
235
|
+
function addToolCard(data) {
|
|
236
|
+
const container = document.getElementById('messages');
|
|
237
|
+
const el = document.getElementById('empty-state');
|
|
238
|
+
if (el) el.remove();
|
|
239
|
+
|
|
240
|
+
const card = document.createElement('div');
|
|
241
|
+
card.className = 'tool-card';
|
|
242
|
+
card.id = 'tool-' + data.id;
|
|
243
|
+
|
|
244
|
+
card.innerHTML = `
|
|
245
|
+
<div class="tool-card-header" onclick="toggleToolDetails('${data.id}')">
|
|
246
|
+
<div class="tool-icon start">▶</div>
|
|
247
|
+
<span class="tool-name">${escapeHtml(data.name)}</span>
|
|
248
|
+
<span class="tool-status">running...</span>
|
|
249
|
+
<span class="tool-duration" id="tool-time-${data.id}"></span>
|
|
250
|
+
</div>
|
|
251
|
+
<div class="tool-card-body" id="tool-body-${data.id}">
|
|
252
|
+
<div class="param-row"><span class="param-key">args:</span> <span class="param-value">${escapeHtml(JSON.stringify(data.args || {}))}</span></div>
|
|
253
|
+
</div>
|
|
254
|
+
`;
|
|
255
|
+
|
|
256
|
+
container.appendChild(card);
|
|
257
|
+
card.scrollIntoView({ behavior: 'smooth' });
|
|
258
|
+
currentToolId = data.id;
|
|
259
|
+
startToolTimer(data.id);
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
function updateToolCard(data) {
|
|
263
|
+
const header = document.querySelector('#tool-' + data.id + ' .tool-card-header');
|
|
264
|
+
if (!header) return;
|
|
265
|
+
const icon = header.querySelector('.tool-icon');
|
|
266
|
+
const status = header.querySelector('.tool-status');
|
|
267
|
+
|
|
268
|
+
if (data.is_error) {
|
|
269
|
+
icon.className = 'tool-icon error';
|
|
270
|
+
icon.innerHTML = '✗';
|
|
271
|
+
status.textContent = 'failed';
|
|
272
|
+
} else {
|
|
273
|
+
icon.className = 'tool-icon start';
|
|
274
|
+
icon.innerHTML = '✓';
|
|
275
|
+
status.textContent = 'done';
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
const durationEl = document.getElementById('tool-time-' + data.id);
|
|
279
|
+
if (data.duration_ms) {
|
|
280
|
+
durationEl.textContent = data.duration_ms + 'ms';
|
|
281
|
+
}
|
|
282
|
+
stopToolTimer(data.id);
|
|
283
|
+
currentToolId = null;
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
function toggleToolDetails(id) {
|
|
287
|
+
const body = document.getElementById('tool-body-' + id);
|
|
288
|
+
body.classList.toggle('open');
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
let toolTimers = {};
|
|
292
|
+
|
|
293
|
+
function startToolTimer(id) {
|
|
294
|
+
const start = Date.now();
|
|
295
|
+
toolTimers[id] = setInterval(() => {
|
|
296
|
+
const el = document.getElementById('tool-time-' + id);
|
|
297
|
+
if (el) el.textContent = (Date.now() - start) + 'ms';
|
|
298
|
+
}, 100);
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
function stopToolTimer(id) {
|
|
302
|
+
if (toolTimers[id]) {
|
|
303
|
+
clearInterval(toolTimers[id]);
|
|
304
|
+
delete toolTimers[id];
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
// --- SSE streaming ---
|
|
309
|
+
function sendMessage() {
|
|
310
|
+
const input = document.getElementById('message-input');
|
|
311
|
+
const prompt = input.value.trim();
|
|
312
|
+
if (!prompt || isStreaming) return;
|
|
313
|
+
|
|
314
|
+
input.value = '';
|
|
315
|
+
isStreaming = true;
|
|
316
|
+
document.getElementById('send-button').disabled = true;
|
|
317
|
+
document.getElementById('stream-indicator').textContent = 'Streaming...';
|
|
318
|
+
|
|
319
|
+
addMessage('user', prompt);
|
|
320
|
+
|
|
321
|
+
if (!currentSessionId) {
|
|
322
|
+
fetch('<%= ask_rails.sessions_path %>', { method: 'POST' })
|
|
323
|
+
.then(r => r.text())
|
|
324
|
+
.then(() => window.location.reload());
|
|
325
|
+
return;
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
const url = '<%= ask_rails.session_message_path("SESSION_ID") %>'.replace('SESSION_ID', currentSessionId);
|
|
329
|
+
|
|
330
|
+
fetch(url, {
|
|
331
|
+
method: 'POST',
|
|
332
|
+
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
|
|
333
|
+
body: new URLSearchParams({ message: prompt })
|
|
334
|
+
}).then(response => {
|
|
335
|
+
const reader = response.body.getReader();
|
|
336
|
+
const decoder = new TextDecoder();
|
|
337
|
+
let buffer = '';
|
|
338
|
+
|
|
339
|
+
function processChunk() {
|
|
340
|
+
reader.read().then(({ done, value }) => {
|
|
341
|
+
if (done) {
|
|
342
|
+
isStreaming = false;
|
|
343
|
+
document.getElementById('send-button').disabled = false;
|
|
344
|
+
document.getElementById('stream-indicator').textContent = '';
|
|
345
|
+
return;
|
|
346
|
+
}
|
|
347
|
+
buffer += decoder.decode(value, { stream: true });
|
|
348
|
+
const lines = buffer.split('\n');
|
|
349
|
+
buffer = lines.pop() || '';
|
|
350
|
+
|
|
351
|
+
lines.forEach(line => {
|
|
352
|
+
if (line.startsWith('data: ')) {
|
|
353
|
+
try {
|
|
354
|
+
const data = JSON.parse(line.slice(6));
|
|
355
|
+
switch (data.type) {
|
|
356
|
+
case 'delta':
|
|
357
|
+
updateLastMessage('assistant', data.content);
|
|
358
|
+
break;
|
|
359
|
+
case 'thinking':
|
|
360
|
+
addMessage('thinking', data.content);
|
|
361
|
+
break;
|
|
362
|
+
case 'tool_start':
|
|
363
|
+
addToolCard(data);
|
|
364
|
+
break;
|
|
365
|
+
case 'tool_end':
|
|
366
|
+
updateToolCard(data);
|
|
367
|
+
break;
|
|
368
|
+
case 'tool_update':
|
|
369
|
+
// Could show progress in the card
|
|
370
|
+
break;
|
|
371
|
+
case 'error':
|
|
372
|
+
addMessage('system', '⚠️ ' + (data.message || 'An error occurred'));
|
|
373
|
+
isStreaming = false;
|
|
374
|
+
document.getElementById('send-button').disabled = false;
|
|
375
|
+
document.getElementById('stream-indicator').textContent = '';
|
|
376
|
+
break;
|
|
377
|
+
case 'done':
|
|
378
|
+
isStreaming = false;
|
|
379
|
+
document.getElementById('send-button').disabled = false;
|
|
380
|
+
document.getElementById('stream-indicator').textContent = '';
|
|
381
|
+
refreshSidebar();
|
|
382
|
+
break;
|
|
383
|
+
case 'start':
|
|
384
|
+
currentSessionId = data.session_id;
|
|
385
|
+
break;
|
|
386
|
+
}
|
|
387
|
+
} catch(e) {
|
|
388
|
+
// Skip malformed JSON
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
});
|
|
392
|
+
processChunk();
|
|
393
|
+
});
|
|
394
|
+
}
|
|
395
|
+
processChunk();
|
|
396
|
+
}).catch(() => {
|
|
397
|
+
isStreaming = false;
|
|
398
|
+
document.getElementById('send-button').disabled = false;
|
|
399
|
+
document.getElementById('stream-indicator').textContent = '';
|
|
400
|
+
});
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
// --- Session management ---
|
|
404
|
+
function switchSession(id) {
|
|
405
|
+
currentSessionId = id;
|
|
406
|
+
document.getElementById('empty-state')?.remove();
|
|
407
|
+
document.getElementById('messages').innerHTML = '';
|
|
408
|
+
document.getElementById('header-title').textContent = 'Session ' + id.substring(0, 8);
|
|
409
|
+
|
|
410
|
+
// Show session messages
|
|
411
|
+
fetch('<%= ask_rails.session_history_path("SESSION_ID") %>'.replace('SESSION_ID', id))
|
|
412
|
+
.then(r => r.json())
|
|
413
|
+
.then(messages => {
|
|
414
|
+
messages.forEach(m => {
|
|
415
|
+
if (m.role === 'user' || m.role === 'assistant') {
|
|
416
|
+
addMessage(m.role, m.content);
|
|
417
|
+
}
|
|
418
|
+
});
|
|
419
|
+
});
|
|
420
|
+
|
|
421
|
+
// Update active state in sidebar
|
|
422
|
+
document.querySelectorAll('.session-item').forEach(el => {
|
|
423
|
+
el.classList.toggle('active', el.dataset.sessionId === id);
|
|
424
|
+
});
|
|
425
|
+
|
|
426
|
+
// Refresh audit log for this session
|
|
427
|
+
refreshAuditLog(id);
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
function deleteSession(id) {
|
|
431
|
+
if (!confirm('Delete this session?')) return;
|
|
432
|
+
fetch('<%= ask_rails.session_path("ID") %>'.replace('ID', id), { method: 'DELETE' })
|
|
433
|
+
.then(() => window.location.reload());
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
function refreshSidebar() {
|
|
437
|
+
fetch('<%= ask_rails.sessions_path %>')
|
|
438
|
+
.then(r => r.json())
|
|
439
|
+
.then(sessions => {
|
|
440
|
+
const container = document.getElementById('session-items');
|
|
441
|
+
container.innerHTML = sessions.map(s => `
|
|
442
|
+
<div class="session-item${s.id === currentSessionId ? ' active' : ''}" data-session-id="${s.id}" onclick="switchSession('${s.id}')">
|
|
443
|
+
<span class="sess-delete" onclick="event.stopPropagation(); deleteSession('${s.id}')">✕</span>
|
|
444
|
+
${s.preview ? escapeHtml(s.preview) : 'Session ' + s.id.substring(0, 8)}
|
|
445
|
+
<div class="sess-meta">
|
|
446
|
+
<span>${s.message_count} messages</span>
|
|
447
|
+
${s.created_at ? '<span>' + timeAgo(s.created_at) + '</span>' : ''}
|
|
448
|
+
</div>
|
|
449
|
+
</div>
|
|
450
|
+
`).join('');
|
|
451
|
+
});
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
function refreshAuditLog(sessionId) {
|
|
455
|
+
const url = sessionId
|
|
456
|
+
? '<%= ask_rails.session_audit_path("SESSION_ID") %>'.replace('SESSION_ID', sessionId)
|
|
457
|
+
: '<%= ask_rails.sessions_path %>/audit';
|
|
458
|
+
|
|
459
|
+
if (!sessionId) return;
|
|
460
|
+
|
|
461
|
+
fetch(url)
|
|
462
|
+
.then(r => r.json())
|
|
463
|
+
.then(logs => {
|
|
464
|
+
const panel = document.getElementById('panel-audit');
|
|
465
|
+
if (!panel) return;
|
|
466
|
+
panel.innerHTML = logs.length
|
|
467
|
+
? logs.map(e => `
|
|
468
|
+
<div class="audit-entry">
|
|
469
|
+
<span class="audit-tool">${escapeHtml(e.tool_name)}</span>
|
|
470
|
+
<span class="audit-status ${e.status}">${e.status}</span>
|
|
471
|
+
<span class="audit-duration">${e.duration_ms}ms</span>
|
|
472
|
+
<div class="audit-time">${e.recorded_at || ''}</div>
|
|
473
|
+
</div>
|
|
474
|
+
`).join('')
|
|
475
|
+
: '<div style="padding: 20px; text-align: center; color: #404050; font-size: 12px;">No tool activity for this session.</div>';
|
|
476
|
+
});
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
// --- Keyboard ---
|
|
480
|
+
document.getElementById('message-input').addEventListener('keydown', function(e) {
|
|
481
|
+
if (e.key === 'Enter' && !e.shiftKey) {
|
|
482
|
+
e.preventDefault();
|
|
483
|
+
sendMessage();
|
|
484
|
+
}
|
|
485
|
+
});
|
|
486
|
+
|
|
487
|
+
// Ctrl+K to focus input
|
|
488
|
+
document.addEventListener('keydown', function(e) {
|
|
489
|
+
if ((e.metaKey || e.ctrlKey) && e.key === 'k') {
|
|
490
|
+
e.preventDefault();
|
|
491
|
+
document.getElementById('message-input').focus();
|
|
492
|
+
}
|
|
493
|
+
});
|
|
494
|
+
|
|
495
|
+
// --- Utilities ---
|
|
496
|
+
function escapeHtml(text) {
|
|
497
|
+
const d = document.createElement('div');
|
|
498
|
+
d.textContent = text;
|
|
499
|
+
return d.innerHTML;
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
function timeAgo(dateStr) {
|
|
503
|
+
if (!dateStr) return '';
|
|
504
|
+
const diff = Date.now() - new Date(dateStr).getTime();
|
|
505
|
+
const mins = Math.floor(diff / 60000);
|
|
506
|
+
if (mins < 1) return 'just now';
|
|
507
|
+
if (mins < 60) return mins + 'm ago';
|
|
508
|
+
const hrs = Math.floor(mins / 60);
|
|
509
|
+
if (hrs < 24) return hrs + 'h ago';
|
|
510
|
+
return Math.floor(hrs / 24) + 'd ago';
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
// Load initial session if one is active
|
|
514
|
+
<% if @active_session %>
|
|
515
|
+
document.addEventListener('DOMContentLoaded', function() {
|
|
516
|
+
refreshAuditLog(currentSessionId);
|
|
517
|
+
});
|
|
518
|
+
<% end %>
|
|
519
|
+
</script>
|
|
520
|
+
</body>
|
|
521
|
+
</html>
|
data/config/routes.rb
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
Ask::Rails::Engine.routes.draw do
|
|
4
|
+
root to: "chat#index"
|
|
5
|
+
|
|
6
|
+
resources :sessions, only: [:index, :create, :show], controller: "chat" do
|
|
7
|
+
collection do
|
|
8
|
+
delete :destroy, action: :destroy_all
|
|
9
|
+
end
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
delete "sessions/:id" => "chat#destroy_session", as: :session
|
|
13
|
+
post "sessions/:session_id/messages" => "chat#message", as: :session_message
|
|
14
|
+
get "sessions/:session_id/stream" => "chat#stream", as: :session_stream
|
|
15
|
+
get "sessions/:session_id/messages" => "chat#history", as: :session_history
|
|
16
|
+
get "sessions/:session_id/audit" => "chat#audit", as: :session_audit
|
|
17
|
+
end
|