ask-rails 0.3.0 → 0.11.1
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 +139 -0
- data/app/controllers/ask/rails/chat_controller.rb +145 -22
- data/app/models/ask/rails/session.rb +23 -0
- data/app/views/layouts/ask/rails/application.html.erb +364 -63
- data/config/routes.rb +3 -1
- data/lib/ask/rails/audit_log.rb +181 -0
- data/lib/ask/rails/configuration.rb +53 -1
- data/lib/ask/rails/environment_permissions.rb +39 -0
- data/lib/ask/rails/persistence.rb +21 -4
- 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 +105 -3
- 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 +7 -1
|
@@ -5,75 +5,173 @@
|
|
|
5
5
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
6
6
|
<style>
|
|
7
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; }
|
|
8
|
+
body { font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif; background: #0d0d0f; color: #e1e1e6; height: 100vh; display: flex; overflow: hidden; }
|
|
9
9
|
a { color: #8b8bf0; text-decoration: none; }
|
|
10
10
|
a:hover { color: #a0a0ff; }
|
|
11
11
|
|
|
12
|
-
|
|
13
|
-
.sidebar
|
|
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; }
|
|
14
15
|
.sidebar-header h1 { font-size: 16px; font-weight: 600; color: #c0c0d0; }
|
|
15
|
-
.sidebar-header
|
|
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; }
|
|
16
21
|
.sidebar-actions { padding: 12px 16px; }
|
|
17
|
-
.sidebar-actions
|
|
18
|
-
.sidebar-actions
|
|
19
|
-
.
|
|
20
|
-
|
|
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; }
|
|
21
29
|
.session-item:hover { background: #1a1a24; color: #c0c0d0; }
|
|
22
30
|
.session-item.active { background: #1e1e30; color: #d0d0e0; }
|
|
23
|
-
.session-item .
|
|
24
|
-
.
|
|
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; }
|
|
25
48
|
|
|
49
|
+
/* --- Main chat area --- */
|
|
26
50
|
.main { flex: 1; display: flex; flex-direction: column; min-width: 0; }
|
|
27
|
-
.chat-header { padding: 14px 20px; border-bottom: 1px solid #1e1e28; font-size: 13px; color: #808090; }
|
|
28
|
-
.
|
|
29
|
-
|
|
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; }
|
|
30
57
|
.message.user { background: #1e1e30; align-self: flex-end; border-bottom-right-radius: 4px; }
|
|
31
58
|
.message.assistant { background: #181825; align-self: flex-start; border-bottom-left-radius: 4px; }
|
|
32
59
|
.message.thinking { background: #151520; align-self: flex-start; font-style: italic; color: #707080; font-size: 13px; border-left: 2px solid #404068; }
|
|
33
60
|
.message.system { background: #101018; align-self: center; color: #505060; font-size: 12px; text-align: center; }
|
|
34
|
-
.message .label { font-size: 11px; color: #606070; margin-bottom: 4px; }
|
|
61
|
+
.message .msg-label { font-size: 11px; color: #606070; margin-bottom: 4px; font-weight: 500; }
|
|
35
62
|
.empty-state { flex: 1; display: flex; align-items: center; justify-content: center; color: #404050; font-size: 14px; text-align: center; padding: 40px; }
|
|
36
|
-
.empty-state p { max-width: 300px; }
|
|
63
|
+
.empty-state p { max-width: 300px; line-height: 1.6; }
|
|
37
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 */
|
|
38
83
|
.input-area { padding: 16px 20px; border-top: 1px solid #1e1e28; }
|
|
39
84
|
.input-row { display: flex; gap: 8px; }
|
|
40
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; }
|
|
41
86
|
.input-row input:focus { border-color: #404068; }
|
|
42
|
-
.input-row button { padding: 10px 20px; background: #404068; border: none; border-radius: 8px; color: #e1e1e6; font-size: 14px; cursor: pointer; }
|
|
87
|
+
.input-row button { padding: 10px 20px; background: #404068; border: none; border-radius: 8px; color: #e1e1e6; font-size: 14px; cursor: pointer; font-weight: 500; }
|
|
43
88
|
.input-row button:hover { background: #505080; }
|
|
44
|
-
.input-row button:disabled { opacity: 0.
|
|
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; }
|
|
45
103
|
</style>
|
|
46
104
|
</head>
|
|
47
105
|
<body>
|
|
106
|
+
<!-- Sidebar -->
|
|
48
107
|
<div class="sidebar">
|
|
49
108
|
<div class="sidebar-header">
|
|
50
109
|
<h1>Ask</h1>
|
|
51
|
-
<
|
|
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>
|
|
52
115
|
</div>
|
|
53
116
|
<div class="sidebar-actions">
|
|
54
|
-
<%= button_to "
|
|
55
|
-
<a href="#" onclick="document.getElementById('new-session-form').click(); return false;">+ New Session</a>
|
|
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>
|
|
56
138
|
</div>
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
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.
|
|
62
154
|
</div>
|
|
63
155
|
<% end %>
|
|
64
156
|
</div>
|
|
157
|
+
|
|
65
158
|
<div class="sidebar-footer">
|
|
66
|
-
|
|
159
|
+
<span>v<%= Ask::Rails::VERSION %></span>
|
|
160
|
+
<span><%= ::Rails.env %></span>
|
|
67
161
|
</div>
|
|
68
162
|
</div>
|
|
69
163
|
|
|
164
|
+
<!-- Main chat area -->
|
|
70
165
|
<div class="main">
|
|
71
166
|
<div class="chat-header" id="chat-header">
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
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>
|
|
77
175
|
</div>
|
|
78
176
|
<div class="messages" id="messages">
|
|
79
177
|
<div class="empty-state" id="empty-state">
|
|
@@ -85,24 +183,40 @@
|
|
|
85
183
|
<input type="text" id="message-input" placeholder="Ask about your app..." autofocus>
|
|
86
184
|
<button id="send-button" onclick="sendMessage()">Send</button>
|
|
87
185
|
</div>
|
|
186
|
+
<div class="input-hint">Enter to send · <span id="stream-indicator"></span></div>
|
|
88
187
|
</div>
|
|
89
188
|
</div>
|
|
90
189
|
|
|
91
190
|
<script>
|
|
92
191
|
let currentSessionId = '<%= @active_session&.dig(:id) || "" %>';
|
|
93
192
|
let isStreaming = false;
|
|
193
|
+
let currentToolId = null;
|
|
94
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 ---
|
|
95
213
|
function addMessage(role, content) {
|
|
96
214
|
const el = document.getElementById('empty-state');
|
|
97
215
|
if (el) el.remove();
|
|
98
216
|
|
|
99
217
|
const msg = document.createElement('div');
|
|
100
218
|
msg.className = 'message ' + role;
|
|
101
|
-
|
|
102
|
-
msg.innerHTML = '<div class="label">Thinking...</div>' + escapeHtml(content);
|
|
103
|
-
} else {
|
|
104
|
-
msg.innerHTML = (role === 'user' ? '' : '<div class="label">Assistant</div>') + escapeHtml(content);
|
|
105
|
-
}
|
|
219
|
+
msg.innerHTML = (role === 'user' ? '' : '<div class="msg-label">' + (role === 'thinking' ? 'Thinking...' : 'Assistant') + '</div>') + escapeHtml(content);
|
|
106
220
|
document.getElementById('messages').appendChild(msg);
|
|
107
221
|
msg.scrollIntoView({ behavior: 'smooth' });
|
|
108
222
|
return msg;
|
|
@@ -112,18 +226,86 @@
|
|
|
112
226
|
const msgs = document.getElementById('messages').querySelectorAll('.message.' + role);
|
|
113
227
|
if (msgs.length === 0) return addMessage(role, content);
|
|
114
228
|
const last = msgs[msgs.length - 1];
|
|
115
|
-
const textEl = last.querySelector('div:last-child');
|
|
116
|
-
|
|
117
|
-
else last.innerHTML += escapeHtml(content);
|
|
229
|
+
const textEl = last.querySelector('div:last-child') || last;
|
|
230
|
+
textEl.textContent += content;
|
|
118
231
|
last.scrollIntoView({ behavior: 'smooth' });
|
|
119
232
|
}
|
|
120
233
|
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
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');
|
|
125
289
|
}
|
|
126
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 ---
|
|
127
309
|
function sendMessage() {
|
|
128
310
|
const input = document.getElementById('message-input');
|
|
129
311
|
const prompt = input.value.trim();
|
|
@@ -132,17 +314,14 @@
|
|
|
132
314
|
input.value = '';
|
|
133
315
|
isStreaming = true;
|
|
134
316
|
document.getElementById('send-button').disabled = true;
|
|
317
|
+
document.getElementById('stream-indicator').textContent = 'Streaming...';
|
|
135
318
|
|
|
136
319
|
addMessage('user', prompt);
|
|
137
320
|
|
|
138
321
|
if (!currentSessionId) {
|
|
139
|
-
// Create session first
|
|
140
322
|
fetch('<%= ask_rails.sessions_path %>', { method: 'POST' })
|
|
141
323
|
.then(r => r.text())
|
|
142
|
-
.then(() =>
|
|
143
|
-
// Reload to get the new session
|
|
144
|
-
window.location.reload();
|
|
145
|
-
});
|
|
324
|
+
.then(() => window.location.reload());
|
|
146
325
|
return;
|
|
147
326
|
}
|
|
148
327
|
|
|
@@ -162,6 +341,7 @@
|
|
|
162
341
|
if (done) {
|
|
163
342
|
isStreaming = false;
|
|
164
343
|
document.getElementById('send-button').disabled = false;
|
|
344
|
+
document.getElementById('stream-indicator').textContent = '';
|
|
165
345
|
return;
|
|
166
346
|
}
|
|
167
347
|
buffer += decoder.decode(value, { stream: true });
|
|
@@ -172,17 +352,41 @@
|
|
|
172
352
|
if (line.startsWith('data: ')) {
|
|
173
353
|
try {
|
|
174
354
|
const data = JSON.parse(line.slice(6));
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
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;
|
|
184
386
|
}
|
|
185
|
-
} catch(e) {
|
|
387
|
+
} catch(e) {
|
|
388
|
+
// Skip malformed JSON
|
|
389
|
+
}
|
|
186
390
|
}
|
|
187
391
|
});
|
|
188
392
|
processChunk();
|
|
@@ -192,19 +396,18 @@
|
|
|
192
396
|
}).catch(() => {
|
|
193
397
|
isStreaming = false;
|
|
194
398
|
document.getElementById('send-button').disabled = false;
|
|
399
|
+
document.getElementById('stream-indicator').textContent = '';
|
|
195
400
|
});
|
|
196
401
|
}
|
|
197
402
|
|
|
198
|
-
|
|
199
|
-
if (e.key === 'Enter') sendMessage();
|
|
200
|
-
});
|
|
201
|
-
|
|
403
|
+
// --- Session management ---
|
|
202
404
|
function switchSession(id) {
|
|
203
405
|
currentSessionId = id;
|
|
204
406
|
document.getElementById('empty-state')?.remove();
|
|
205
407
|
document.getElementById('messages').innerHTML = '';
|
|
206
|
-
document.getElementById('
|
|
408
|
+
document.getElementById('header-title').textContent = 'Session ' + id.substring(0, 8);
|
|
207
409
|
|
|
410
|
+
// Show session messages
|
|
208
411
|
fetch('<%= ask_rails.session_history_path("SESSION_ID") %>'.replace('SESSION_ID', id))
|
|
209
412
|
.then(r => r.json())
|
|
210
413
|
.then(messages => {
|
|
@@ -214,7 +417,105 @@
|
|
|
214
417
|
}
|
|
215
418
|
});
|
|
216
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
|
+
});
|
|
217
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 %>
|
|
218
519
|
</script>
|
|
219
520
|
</body>
|
|
220
521
|
</html>
|
data/config/routes.rb
CHANGED
|
@@ -5,11 +5,13 @@ Ask::Rails::Engine.routes.draw do
|
|
|
5
5
|
|
|
6
6
|
resources :sessions, only: [:index, :create, :show], controller: "chat" do
|
|
7
7
|
collection do
|
|
8
|
-
delete :destroy
|
|
8
|
+
delete :destroy, action: :destroy_all
|
|
9
9
|
end
|
|
10
10
|
end
|
|
11
11
|
|
|
12
|
+
delete "sessions/:id" => "chat#destroy_session", as: :session
|
|
12
13
|
post "sessions/:session_id/messages" => "chat#message", as: :session_message
|
|
13
14
|
get "sessions/:session_id/stream" => "chat#stream", as: :session_stream
|
|
14
15
|
get "sessions/:session_id/messages" => "chat#history", as: :session_history
|
|
16
|
+
get "sessions/:session_id/audit" => "chat#audit", as: :session_audit
|
|
15
17
|
end
|