labs-claude 0.2.6 → 0.3.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.
- package/README.md +141 -122
- package/bin/labs.js +245 -178
- package/package.json +34 -34
- package/src/auto-fallback.js +95 -0
- package/src/autostart.js +43 -43
- package/src/bedrock.js +162 -162
- package/src/browser-setup.js +38 -38
- package/src/chat/index.html +534 -534
- package/src/chat-routes.js +149 -149
- package/src/config.js +71 -63
- package/src/gateway-client.js +124 -0
- package/src/local-auth.js +33 -33
- package/src/proxy-manager.js +85 -82
- package/src/proxy.js +801 -801
- package/src/read-shunt.js +26 -26
- package/src/setup.js +1 -1
- package/src/subscription-failure.js +36 -0
- package/src/usage.js +44 -44
package/src/chat/index.html
CHANGED
|
@@ -1,534 +1,534 @@
|
|
|
1
|
-
<!doctype html>
|
|
2
|
-
<html lang="en">
|
|
3
|
-
<head>
|
|
4
|
-
<meta charset="utf-8">
|
|
5
|
-
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
6
|
-
<title>Labs Chat</title>
|
|
7
|
-
<style>
|
|
8
|
-
:root {
|
|
9
|
-
--bg: #f7f7f5; --surface: #ffffff; --line: #e6e4df; --text: #1d1b18; --muted: #77726b;
|
|
10
|
-
--accent: #0f766e; --accent-soft: #e6f2f0; --danger: #b42318; --code: #f3f2ef;
|
|
11
|
-
--radius: 12px; --mono: ui-monospace, "Cascadia Code", Consolas, monospace;
|
|
12
|
-
}
|
|
13
|
-
* { box-sizing: border-box; }
|
|
14
|
-
html, body { height: 100%; margin: 0; }
|
|
15
|
-
body { background: var(--bg); color: var(--text); font: 15px/1.55 system-ui, -apple-system, "Segoe UI", sans-serif; }
|
|
16
|
-
button, input, textarea, select { font: inherit; color: inherit; }
|
|
17
|
-
button { cursor: pointer; border: 1px solid var(--line); background: var(--surface); border-radius: 8px; padding: 6px 12px; }
|
|
18
|
-
button:hover { border-color: #cfccc5; }
|
|
19
|
-
button.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
|
|
20
|
-
button.ghost { border-color: transparent; background: transparent; }
|
|
21
|
-
.hidden { display: none !important; }
|
|
22
|
-
|
|
23
|
-
/* login */
|
|
24
|
-
#login { height: 100%; display: grid; place-items: center; padding: 16px; }
|
|
25
|
-
#login form { width: min(380px, 100%); background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 28px; }
|
|
26
|
-
#login h1 { margin: 0 0 4px; font-size: 20px; }
|
|
27
|
-
#login p { margin: 0 0 18px; color: var(--muted); font-size: 14px; }
|
|
28
|
-
#login input { width: 100%; padding: 10px 12px; border: 1px solid var(--line); border-radius: 8px; margin-bottom: 12px; }
|
|
29
|
-
#login .error { color: var(--danger); font-size: 14px; min-height: 20px; }
|
|
30
|
-
|
|
31
|
-
/* layout */
|
|
32
|
-
#app { height: 100%; display: grid; grid-template-columns: 260px 1fr; }
|
|
33
|
-
aside { border-right: 1px solid var(--line); background: var(--surface); display: flex; flex-direction: column; min-height: 0; }
|
|
34
|
-
aside .top { padding: 14px; display: flex; gap: 8px; }
|
|
35
|
-
aside .top button { flex: 1; }
|
|
36
|
-
#list { overflow-y: auto; padding: 0 8px 12px; flex: 1; }
|
|
37
|
-
#list .item { display: flex; align-items: center; gap: 6px; padding: 8px 10px; border-radius: 8px; cursor: pointer; font-size: 14px; }
|
|
38
|
-
#list .item:hover { background: var(--bg); }
|
|
39
|
-
#list .item.active { background: var(--accent-soft); }
|
|
40
|
-
#list .item span { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
|
|
41
|
-
#list .item .del { opacity: 0; padding: 0 6px; border: 0; background: transparent; color: var(--muted); }
|
|
42
|
-
#list .item:hover .del { opacity: 1; }
|
|
43
|
-
#list .tag { font-size: 11px; color: var(--muted); }
|
|
44
|
-
main { display: flex; flex-direction: column; min-width: 0; min-height: 0; }
|
|
45
|
-
header { display: flex; align-items: center; gap: 10px; padding: 10px 18px; border-bottom: 1px solid var(--line); background: var(--surface); flex-wrap: wrap; }
|
|
46
|
-
header .title { font-weight: 600; flex: 1; min-width: 120px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
|
|
47
|
-
.tabs { display: inline-flex; border: 1px solid var(--line); border-radius: 8px; overflow: hidden; }
|
|
48
|
-
.tabs button { border: 0; border-radius: 0; padding: 5px 14px; }
|
|
49
|
-
.tabs button.on { background: var(--accent); color: #fff; }
|
|
50
|
-
select { border: 1px solid var(--line); border-radius: 8px; padding: 5px 8px; background: var(--surface); }
|
|
51
|
-
#menu { display: none; }
|
|
52
|
-
|
|
53
|
-
/* agent bar */
|
|
54
|
-
#agentbar { display: flex; gap: 8px; padding: 10px 18px; border-bottom: 1px solid var(--line); background: var(--surface); flex-wrap: wrap; }
|
|
55
|
-
#agentbar input { flex: 1; min-width: 200px; padding: 6px 10px; border: 1px solid var(--line); border-radius: 8px; font-family: var(--mono); font-size: 13px; }
|
|
56
|
-
|
|
57
|
-
/* messages */
|
|
58
|
-
#scroll { flex: 1; overflow-y: auto; }
|
|
59
|
-
#messages { max-width: 820px; margin: 0 auto; padding: 24px 18px 40px; }
|
|
60
|
-
.empty { color: var(--muted); text-align: center; margin-top: 18vh; }
|
|
61
|
-
.empty h2 { color: var(--text); font-weight: 600; margin-bottom: 6px; }
|
|
62
|
-
.msg { margin: 0 0 22px; }
|
|
63
|
-
.msg.user .bubble { background: var(--accent-soft); border-radius: var(--radius); padding: 10px 14px; margin-left: auto; width: fit-content; max-width: 85%; white-space: pre-wrap; }
|
|
64
|
-
.msg .files { display: flex; gap: 6px; flex-wrap: wrap; justify-content: flex-end; margin-bottom: 6px; }
|
|
65
|
-
.chip { font-size: 12px; border: 1px solid var(--line); border-radius: 999px; padding: 2px 10px; background: var(--surface); color: var(--muted); }
|
|
66
|
-
.msg.assistant .body > :first-child { margin-top: 0; }
|
|
67
|
-
.msg .meta { font-size: 12px; color: var(--muted); margin-top: 6px; }
|
|
68
|
-
.msg.error .body { color: var(--danger); }
|
|
69
|
-
pre { background: var(--code); border: 1px solid var(--line); border-radius: 10px; padding: 12px 14px; overflow-x: auto; position: relative; font: 13px/1.5 var(--mono); }
|
|
70
|
-
pre .copy { position: absolute; top: 6px; right: 6px; font-size: 12px; padding: 2px 8px; }
|
|
71
|
-
code { font-family: var(--mono); font-size: 0.9em; background: var(--code); padding: 1px 5px; border-radius: 5px; }
|
|
72
|
-
pre code { background: none; padding: 0; }
|
|
73
|
-
.step { font: 13px/1.5 var(--mono); border-left: 3px solid var(--line); padding: 4px 10px; margin: 6px 0; color: var(--muted); white-space: pre-wrap; word-break: break-word; }
|
|
74
|
-
.step.tool { border-color: var(--accent); color: var(--text); }
|
|
75
|
-
.step details summary { cursor: pointer; }
|
|
76
|
-
|
|
77
|
-
/* composer */
|
|
78
|
-
#composer { border-top: 1px solid var(--line); background: var(--surface); padding: 12px 18px 16px; }
|
|
79
|
-
#composer .box { max-width: 820px; margin: 0 auto; border: 1px solid var(--line); border-radius: var(--radius); padding: 8px; background: var(--surface); }
|
|
80
|
-
#composer.drag .box { border-color: var(--accent); background: var(--accent-soft); }
|
|
81
|
-
#input { width: 100%; border: 0; outline: 0; resize: none; max-height: 240px; min-height: 44px; padding: 4px 6px; background: transparent; }
|
|
82
|
-
#composer .row { display: flex; align-items: center; gap: 8px; }
|
|
83
|
-
#composer .row .grow { flex: 1; display: flex; gap: 6px; flex-wrap: wrap; }
|
|
84
|
-
#composer .hint { max-width: 820px; margin: 6px auto 0; font-size: 12px; color: var(--muted); }
|
|
85
|
-
|
|
86
|
-
@media (max-width: 760px) {
|
|
87
|
-
#app { grid-template-columns: 1fr; }
|
|
88
|
-
aside { position: fixed; inset: 0 30% 0 0; z-index: 5; transform: translateX(-100%); transition: transform .2s; }
|
|
89
|
-
aside.open { transform: none; box-shadow: 0 0 0 100vw rgba(0,0,0,.2); }
|
|
90
|
-
#menu { display: inline-block; }
|
|
91
|
-
}
|
|
92
|
-
</style>
|
|
93
|
-
</head>
|
|
94
|
-
<body>
|
|
95
|
-
|
|
96
|
-
<div id="login" class="hidden">
|
|
97
|
-
<form id="loginForm">
|
|
98
|
-
<h1>Labs Chat</h1>
|
|
99
|
-
<p>Private to this laptop. Type <code>labs chat</code> in a terminal: it opens this page already logged in. Or paste your labs key here.</p>
|
|
100
|
-
<input id="keyInput" type="password" placeholder="labs key" autocomplete="off">
|
|
101
|
-
<div class="error" id="loginError"></div>
|
|
102
|
-
<button class="primary" type="submit">Open</button>
|
|
103
|
-
</form>
|
|
104
|
-
</div>
|
|
105
|
-
|
|
106
|
-
<div id="app" class="hidden">
|
|
107
|
-
<aside id="side">
|
|
108
|
-
<div class="top"><button class="primary" id="newChat">+ New chat</button></div>
|
|
109
|
-
<div id="list"></div>
|
|
110
|
-
</aside>
|
|
111
|
-
<main>
|
|
112
|
-
<header>
|
|
113
|
-
<button class="ghost" id="menu" aria-label="Chats">☰</button>
|
|
114
|
-
<div class="title" id="title">New chat</div>
|
|
115
|
-
<div class="tabs" role="tablist">
|
|
116
|
-
<button id="tabChat" class="on">Chat</button>
|
|
117
|
-
<button id="tabAgent">Agent</button>
|
|
118
|
-
</div>
|
|
119
|
-
<select id="model" title="Model"></select>
|
|
120
|
-
</header>
|
|
121
|
-
<div id="agentbar" class="hidden">
|
|
122
|
-
<input id="folder" placeholder="Folder for the agent, e.g. D:\Humopedia" spellcheck="false">
|
|
123
|
-
<select id="access">
|
|
124
|
-
<option value="edits">Can read and edit files</option>
|
|
125
|
-
<option value="full">Full access (commands too)</option>
|
|
126
|
-
</select>
|
|
127
|
-
</div>
|
|
128
|
-
<div id="scroll"><div id="messages"></div></div>
|
|
129
|
-
<div id="composer">
|
|
130
|
-
<div class="box">
|
|
131
|
-
<textarea id="input" rows="1" placeholder="Message GLM 5.3"></textarea>
|
|
132
|
-
<div class="row">
|
|
133
|
-
<div class="grow">
|
|
134
|
-
<button class="ghost" id="attach" title="Attach files">📎</button>
|
|
135
|
-
<span id="pending"></span>
|
|
136
|
-
</div>
|
|
137
|
-
<button class="primary" id="send">Send</button>
|
|
138
|
-
<button id="stop" class="hidden">Stop</button>
|
|
139
|
-
</div>
|
|
140
|
-
</div>
|
|
141
|
-
<div class="hint" id="hint">Enter to send · Shift+Enter for a new line · drop files here</div>
|
|
142
|
-
<input type="file" id="fileInput" multiple class="hidden">
|
|
143
|
-
</div>
|
|
144
|
-
</main>
|
|
145
|
-
</div>
|
|
146
|
-
|
|
147
|
-
<script>
|
|
148
|
-
// GLM 5.3 on Bedrock, US endpoint (third-party listing, Oct 2026); used only for the cost line.
|
|
149
|
-
const PRICE_PER_MILLION = { input: 1.85, output: 5.81 };
|
|
150
|
-
const SYSTEM = 'You are a sharp, friendly expert assistant for Boss (Murodjon), founder of Humopedia. Answer clearly and concisely. Use Markdown. When reviewing security, stay defensive: his own systems only.';
|
|
151
|
-
const $ = id => document.getElementById(id);
|
|
152
|
-
const state = { convo: null, list: [], pending: [], abort: null, busy: false };
|
|
153
|
-
|
|
154
|
-
function newConvo(mode = 'chat') {
|
|
155
|
-
return { id: `c${Date.now().toString(36)}${Math.random().toString(36).slice(2, 6)}`, title: 'New chat', mode, messages: [], events: [], usage: { input: 0, output: 0 }, folder: localStorage.getItem('labs.folder') || '', sessionId: null };
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
// ---------- api ----------
|
|
159
|
-
async function api(path, options = {}) {
|
|
160
|
-
const response = await fetch(path, { ...options, headers: { 'content-type': 'application/json', 'x-labs-chat': '1', ...(options.headers || {}) } });
|
|
161
|
-
if (response.status === 401) { showLogin(); throw new Error('Please log in'); }
|
|
162
|
-
if (!response.ok) throw new Error((await response.json().catch(() => ({}))).error?.message || `HTTP ${response.status}`);
|
|
163
|
-
return response;
|
|
164
|
-
}
|
|
165
|
-
const save = () => api(`/chat/api/conversations/${state.convo.id}`, { method: 'PUT', body: JSON.stringify(state.convo) }).then(loadList);
|
|
166
|
-
|
|
167
|
-
async function loadList() {
|
|
168
|
-
const { conversations } = await (await api('/chat/api/conversations')).json();
|
|
169
|
-
state.list = conversations;
|
|
170
|
-
$('list').innerHTML = '';
|
|
171
|
-
for (const item of conversations) {
|
|
172
|
-
const row = document.createElement('div');
|
|
173
|
-
row.className = `item${state.convo?.id === item.id ? ' active' : ''}`;
|
|
174
|
-
row.innerHTML = `<span></span><small class="tag">${item.mode === 'agent' ? 'agent' : ''}</small><button class="del" title="Delete">×</button>`;
|
|
175
|
-
row.querySelector('span').textContent = item.title || 'Untitled';
|
|
176
|
-
row.onclick = () => openConvo(item.id);
|
|
177
|
-
row.querySelector('.del').onclick = async event => {
|
|
178
|
-
event.stopPropagation();
|
|
179
|
-
await api(`/chat/api/conversations/${item.id}`, { method: 'DELETE' });
|
|
180
|
-
if (state.convo?.id === item.id) startNew();
|
|
181
|
-
loadList();
|
|
182
|
-
};
|
|
183
|
-
$('list').appendChild(row);
|
|
184
|
-
}
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
async function openConvo(id) {
|
|
188
|
-
state.convo = await (await api(`/chat/api/conversations/${id}`)).json();
|
|
189
|
-
state.convo.events ||= []; state.convo.messages ||= []; state.convo.usage ||= { input: 0, output: 0 };
|
|
190
|
-
$('side').classList.remove('open');
|
|
191
|
-
render(); loadList();
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
function startNew(mode = state.convo?.mode || 'chat') {
|
|
195
|
-
state.convo = newConvo(mode);
|
|
196
|
-
render(); loadList();
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
// ---------- rendering ----------
|
|
200
|
-
function escapeHtml(text) {
|
|
201
|
-
return text.replace(/[&<>"']/g, c => ({ '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' }[c]));
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
function markdown(source) {
|
|
205
|
-
const blocks = [];
|
|
206
|
-
let text = escapeHtml(source).replace(/```([\w+-]*)\n([\s\S]*?)(```|$)/g, (m, lang, code) => {
|
|
207
|
-
blocks.push(`<pre><button class="copy">Copy</button><code>${code.replace(/\n$/, '')}</code></pre>`);
|
|
208
|
-
return `\u0000${blocks.length - 1}\u0000`;
|
|
209
|
-
});
|
|
210
|
-
text = text
|
|
211
|
-
.replace(/`([^`\n]+)`/g, '<code>$1</code>')
|
|
212
|
-
.replace(/\*\*([^*\n]+)\*\*/g, '<strong>$1</strong>')
|
|
213
|
-
.replace(/^#{1,6} (.+)$/gm, '<strong>$1</strong>')
|
|
214
|
-
.replace(/\[([^\]]+)\]\((https?:[^)\s]+)\)/g, '<a href="$2" target="_blank" rel="noopener">$1</a>');
|
|
215
|
-
const html = text.split(/\n{2,}/).map(part => {
|
|
216
|
-
if (/^\u0000\d+\u0000$/.test(part.trim())) return part.trim();
|
|
217
|
-
if (/^(\s*([-*]|\d+\.) .+\n?)+$/.test(part)) {
|
|
218
|
-
const ordered = /^\s*\d+\./.test(part);
|
|
219
|
-
const items = part.trim().split('\n').map(line => `<li>${line.replace(/^\s*([-*]|\d+\.) /, '')}</li>`).join('');
|
|
220
|
-
return ordered ? `<ol>${items}</ol>` : `<ul>${items}</ul>`;
|
|
221
|
-
}
|
|
222
|
-
return `<p>${part.replace(/\n/g, '<br>')}</p>`;
|
|
223
|
-
}).join('');
|
|
224
|
-
return html.replace(/\u0000(\d+)\u0000/g, (m, index) => blocks[index]);
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
function messageText(content) {
|
|
228
|
-
return typeof content === 'string' ? content : content.filter(b => b.type === 'text').map(b => b.text).join('\n');
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
function addMessage(role, html, meta = '') {
|
|
232
|
-
const element = document.createElement('div');
|
|
233
|
-
element.className = `msg ${role}`;
|
|
234
|
-
element.innerHTML = `<div class="${role === 'user' ? 'bubble' : 'body'}"></div><div class="meta"></div>`;
|
|
235
|
-
if (role === 'user') element.firstChild.textContent = html; else element.firstChild.innerHTML = html;
|
|
236
|
-
element.lastChild.textContent = meta;
|
|
237
|
-
$('messages').appendChild(element);
|
|
238
|
-
return element;
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
// Only GLM 5.3 is billed in dollars (AWS); every other model spends ElevenLabs credits.
|
|
242
|
-
function costText(usage, model = $('model').value) {
|
|
243
|
-
const tokens = `in ${usage.input.toLocaleString()} · out ${usage.output.toLocaleString()} tokens`;
|
|
244
|
-
if (model !== 'glm') return `${tokens} · ElevenLabs credits`;
|
|
245
|
-
const dollars = usage.input / 1e6 * PRICE_PER_MILLION.input + usage.output / 1e6 * PRICE_PER_MILLION.output;
|
|
246
|
-
return `${tokens} · ≈ ${dollars.toFixed(4)} AWS`;
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
function renderStep(event) {
|
|
250
|
-
// prompts typed in agent mode are stored as events so a reload shows them
|
|
251
|
-
if (event.type === 'prompt') return addMessage('user', event.text);
|
|
252
|
-
const element = document.createElement('div');
|
|
253
|
-
if (event.type === 'assistant') {
|
|
254
|
-
for (const block of event.message?.content || []) {
|
|
255
|
-
if (block.type === 'text' && block.text.trim()) addMessage('assistant', markdown(block.text));
|
|
256
|
-
if (block.type === 'tool_use') {
|
|
257
|
-
const step = document.createElement('div');
|
|
258
|
-
step.className = 'step tool';
|
|
259
|
-
const input = block.input?.command || block.input?.file_path || block.input?.pattern || JSON.stringify(block.input || {});
|
|
260
|
-
step.textContent = `🔧 ${block.name} ${String(input).slice(0, 300)}`;
|
|
261
|
-
$('messages').appendChild(step);
|
|
262
|
-
}
|
|
263
|
-
}
|
|
264
|
-
return;
|
|
265
|
-
}
|
|
266
|
-
if (event.type === 'user') {
|
|
267
|
-
for (const block of event.message?.content || []) {
|
|
268
|
-
if (block.type !== 'tool_result') continue;
|
|
269
|
-
const text = typeof block.content === 'string' ? block.content : (block.content || []).map(b => b.text || '').join('\n');
|
|
270
|
-
element.className = 'step';
|
|
271
|
-
element.innerHTML = '<details><summary></summary><div></div></details>';
|
|
272
|
-
element.querySelector('summary').textContent = `${block.is_error ? '⚠️' : '↳'} ${text.split('\n')[0].slice(0, 120) || '(no output)'}`;
|
|
273
|
-
element.querySelector('details div').textContent = text.slice(0, 6000);
|
|
274
|
-
$('messages').appendChild(element);
|
|
275
|
-
}
|
|
276
|
-
return;
|
|
277
|
-
}
|
|
278
|
-
if (event.type === 'result') {
|
|
279
|
-
const usage = event.usage || {};
|
|
280
|
-
const total = { input: (usage.input_tokens || 0) + (usage.cache_read_input_tokens || 0) + (usage.cache_creation_input_tokens || 0), output: usage.output_tokens || 0 };
|
|
281
|
-
element.className = 'msg assistant';
|
|
282
|
-
element.innerHTML = '<div class="meta"></div>';
|
|
283
|
-
element.firstChild.textContent = `${event.is_error ? '❌ ' : '✅ '}done in ${Math.round((event.duration_ms || 0) / 1000)} s · ${event.num_turns || 0} turns · ${costText(total)}`;
|
|
284
|
-
$('messages').appendChild(element);
|
|
285
|
-
return;
|
|
286
|
-
}
|
|
287
|
-
if (event.type === 'log' && /error|❌|🚨/i.test(event.text)) {
|
|
288
|
-
element.className = 'step';
|
|
289
|
-
element.textContent = event.text.trim();
|
|
290
|
-
$('messages').appendChild(element);
|
|
291
|
-
}
|
|
292
|
-
}
|
|
293
|
-
|
|
294
|
-
function render() {
|
|
295
|
-
const convo = state.convo;
|
|
296
|
-
$('title').textContent = convo.title;
|
|
297
|
-
$('tabChat').classList.toggle('on', convo.mode === 'chat');
|
|
298
|
-
$('tabAgent').classList.toggle('on', convo.mode === 'agent');
|
|
299
|
-
$('agentbar').classList.toggle('hidden', convo.mode !== 'agent');
|
|
300
|
-
$('folder').value = convo.folder || '';
|
|
301
|
-
$('input').placeholder = convo.mode === 'agent' ? 'Tell the agent what to do in this folder' : `Message ${$('model').selectedOptions[0]?.textContent || 'GLM 5.3'}`;
|
|
302
|
-
$('messages').innerHTML = '';
|
|
303
|
-
const empty = convo.mode === 'agent' ? convo.events.length === 0 : convo.messages.length === 0;
|
|
304
|
-
if (empty) {
|
|
305
|
-
$('messages').innerHTML = convo.mode === 'agent'
|
|
306
|
-
? '<div class="empty"><h2>Agent</h2>Claude Code on GLM 5.3 works inside the folder above: it reads, edits and runs things there.</div>'
|
|
307
|
-
: '<div class="empty"><h2>What are we working on?</h2>GLM 5.3 on Amazon Bedrock · paid from your AWS credits</div>';
|
|
308
|
-
}
|
|
309
|
-
if (convo.mode === 'agent') convo.events.forEach(renderStep);
|
|
310
|
-
else for (const message of convo.messages) {
|
|
311
|
-
if (message.role === 'user') {
|
|
312
|
-
const element = addMessage('user', message.display ?? messageText(message.content));
|
|
313
|
-
if (message.files?.length) {
|
|
314
|
-
const files = document.createElement('div');
|
|
315
|
-
files.className = 'files';
|
|
316
|
-
message.files.forEach(name => { const chip = document.createElement('span'); chip.className = 'chip'; chip.textContent = name; files.appendChild(chip); });
|
|
317
|
-
element.prepend(files);
|
|
318
|
-
}
|
|
319
|
-
} else addMessage('assistant', markdown(messageText(message.content)), message.meta || '');
|
|
320
|
-
}
|
|
321
|
-
scrollDown();
|
|
322
|
-
}
|
|
323
|
-
|
|
324
|
-
const scrollDown = () => { $('scroll').scrollTop = $('scroll').scrollHeight; };
|
|
325
|
-
|
|
326
|
-
// ---------- attachments ----------
|
|
327
|
-
const TEXT_LIMIT = 2_000_000, IMAGE_LIMIT = 5_000_000;
|
|
328
|
-
function readFile(file) {
|
|
329
|
-
return new Promise((resolve, reject) => {
|
|
330
|
-
const reader = new FileReader();
|
|
331
|
-
reader.onerror = () => reject(reader.error);
|
|
332
|
-
if (file.type.startsWith('image/')) {
|
|
333
|
-
if (file.size > IMAGE_LIMIT) return reject(new Error(`${file.name} is over 5 MB`));
|
|
334
|
-
reader.onload = () => resolve({ name: file.name, image: { type: 'image', source: { type: 'base64', media_type: file.type, data: reader.result.split(',')[1] } } });
|
|
335
|
-
reader.readAsDataURL(file);
|
|
336
|
-
} else {
|
|
337
|
-
if (file.size > TEXT_LIMIT) return reject(new Error(`${file.name} is over 2 MB`));
|
|
338
|
-
reader.onload = () => resolve({ name: file.name, text: reader.result });
|
|
339
|
-
reader.readAsText(file);
|
|
340
|
-
}
|
|
341
|
-
});
|
|
342
|
-
}
|
|
343
|
-
|
|
344
|
-
async function addFiles(files) {
|
|
345
|
-
for (const file of files) {
|
|
346
|
-
try { state.pending.push(await readFile(file)); } catch (error) { alertHint(error.message); }
|
|
347
|
-
}
|
|
348
|
-
showPending();
|
|
349
|
-
}
|
|
350
|
-
|
|
351
|
-
function showPending() {
|
|
352
|
-
$('pending').innerHTML = '';
|
|
353
|
-
state.pending.forEach((file, index) => {
|
|
354
|
-
const chip = document.createElement('span');
|
|
355
|
-
chip.className = 'chip';
|
|
356
|
-
chip.textContent = `${file.name} ×`;
|
|
357
|
-
chip.style.cursor = 'pointer';
|
|
358
|
-
chip.onclick = () => { state.pending.splice(index, 1); showPending(); };
|
|
359
|
-
$('pending').appendChild(chip);
|
|
360
|
-
});
|
|
361
|
-
}
|
|
362
|
-
|
|
363
|
-
function alertHint(text) {
|
|
364
|
-
$('hint').textContent = text;
|
|
365
|
-
setTimeout(() => { $('hint').textContent = 'Enter to send · Shift+Enter for a new line · drop files here'; }, 5000);
|
|
366
|
-
}
|
|
367
|
-
|
|
368
|
-
// ---------- streaming ----------
|
|
369
|
-
async function readSse(response, onData) {
|
|
370
|
-
const reader = response.body.getReader();
|
|
371
|
-
const decoder = new TextDecoder();
|
|
372
|
-
let buffer = '';
|
|
373
|
-
for (;;) {
|
|
374
|
-
const { value, done } = await reader.read();
|
|
375
|
-
if (done) break;
|
|
376
|
-
buffer += decoder.decode(value, { stream: true });
|
|
377
|
-
const parts = buffer.split('\n\n');
|
|
378
|
-
buffer = parts.pop();
|
|
379
|
-
for (const part of parts) {
|
|
380
|
-
const line = part.split('\n').find(l => l.startsWith('data: '));
|
|
381
|
-
if (line) onData(JSON.parse(line.slice(6)));
|
|
382
|
-
}
|
|
383
|
-
}
|
|
384
|
-
}
|
|
385
|
-
|
|
386
|
-
function setBusy(busy) {
|
|
387
|
-
state.busy = busy;
|
|
388
|
-
$('send').classList.toggle('hidden', busy);
|
|
389
|
-
$('stop').classList.toggle('hidden', !busy);
|
|
390
|
-
}
|
|
391
|
-
|
|
392
|
-
async function sendChat(text) {
|
|
393
|
-
const convo = state.convo;
|
|
394
|
-
const files = state.pending.splice(0);
|
|
395
|
-
showPending();
|
|
396
|
-
const fileText = files.filter(f => f.text != null).map(f => `\n\n<file name="${f.name}">\n${f.text}\n</file>`).join('');
|
|
397
|
-
const content = [{ type: 'text', text: text + fileText }, ...files.filter(f => f.image).map(f => f.image)];
|
|
398
|
-
convo.messages.push({ role: 'user', content, display: text, files: files.map(f => f.name) });
|
|
399
|
-
if (convo.title === 'New chat') convo.title = text.slice(0, 60) || files[0]?.name || 'Chat';
|
|
400
|
-
render();
|
|
401
|
-
|
|
402
|
-
const element = addMessage('assistant', '<span class="meta">thinking…</span>');
|
|
403
|
-
const body = element.querySelector('.body');
|
|
404
|
-
let reply = '', usage = null, failure = null;
|
|
405
|
-
state.abort = new AbortController();
|
|
406
|
-
setBusy(true);
|
|
407
|
-
try {
|
|
408
|
-
const response = await api('/v1/messages', {
|
|
409
|
-
method: 'POST', signal: state.abort.signal,
|
|
410
|
-
body: JSON.stringify({ model: $('model').value, max_tokens: 16000, stream: true, system: SYSTEM, messages: convo.messages.map(({ role, content }) => ({ role, content })) })
|
|
411
|
-
});
|
|
412
|
-
await readSse(response, data => {
|
|
413
|
-
if (data.type === 'content_block_delta' && data.delta?.text) {
|
|
414
|
-
reply += data.delta.text;
|
|
415
|
-
body.innerHTML = markdown(reply);
|
|
416
|
-
scrollDown();
|
|
417
|
-
} else if (data.type === 'message_delta') usage = data.usage;
|
|
418
|
-
else if (data.type === 'error') failure = data.error?.message;
|
|
419
|
-
});
|
|
420
|
-
} catch (error) {
|
|
421
|
-
if (error.name !== 'AbortError') failure = error.message;
|
|
422
|
-
}
|
|
423
|
-
setBusy(false);
|
|
424
|
-
if (failure) {
|
|
425
|
-
element.classList.add('error');
|
|
426
|
-
body.textContent = `❌ ${failure}`;
|
|
427
|
-
convo.messages.pop();
|
|
428
|
-
$('input').value = text;
|
|
429
|
-
return;
|
|
430
|
-
}
|
|
431
|
-
const turn = { input: usage?.input_tokens || 0, output: usage?.output_tokens || 0 };
|
|
432
|
-
let meta = costText(turn);
|
|
433
|
-
if ($('model').value === 'glm') {
|
|
434
|
-
convo.usage.input += turn.input; convo.usage.output += turn.output;
|
|
435
|
-
meta += ` · chat total ≈ ${(convo.usage.input / 1e6 * PRICE_PER_MILLION.input + convo.usage.output / 1e6 * PRICE_PER_MILLION.output).toFixed(4)}`;
|
|
436
|
-
}
|
|
437
|
-
element.querySelector('.meta').textContent = meta;
|
|
438
|
-
convo.messages.push({ role: 'assistant', content: [{ type: 'text', text: reply || '(stopped)' }], meta });
|
|
439
|
-
save();
|
|
440
|
-
}
|
|
441
|
-
|
|
442
|
-
async function sendAgent(text) {
|
|
443
|
-
const convo = state.convo;
|
|
444
|
-
convo.folder = $('folder').value.trim();
|
|
445
|
-
if (!convo.folder) return alertHint('Pick a folder first');
|
|
446
|
-
localStorage.setItem('labs.folder', convo.folder);
|
|
447
|
-
if (convo.title === 'New chat') convo.title = text.slice(0, 60);
|
|
448
|
-
convo.events.push({ type: 'prompt', text });
|
|
449
|
-
render();
|
|
450
|
-
state.abort = new AbortController();
|
|
451
|
-
setBusy(true);
|
|
452
|
-
try {
|
|
453
|
-
const response = await api('/chat/api/agent', {
|
|
454
|
-
method: 'POST', signal: state.abort.signal,
|
|
455
|
-
body: JSON.stringify({ folder: convo.folder, prompt: text, model: $('model').value, access: $('access').value, sessionId: convo.sessionId })
|
|
456
|
-
});
|
|
457
|
-
await readSse(response, event => {
|
|
458
|
-
if (event.type === 'system' && event.session_id) convo.sessionId = event.session_id;
|
|
459
|
-
if (['assistant', 'user', 'result', 'log'].includes(event.type)) {
|
|
460
|
-
convo.events.push(event);
|
|
461
|
-
renderStep(event);
|
|
462
|
-
scrollDown();
|
|
463
|
-
}
|
|
464
|
-
});
|
|
465
|
-
} catch (error) {
|
|
466
|
-
if (error.name !== 'AbortError') { convo.events.push({ type: 'log', text: `❌ ${error.message}` }); renderStep(convo.events.at(-1)); }
|
|
467
|
-
}
|
|
468
|
-
setBusy(false);
|
|
469
|
-
save();
|
|
470
|
-
}
|
|
471
|
-
|
|
472
|
-
async function send() {
|
|
473
|
-
const text = $('input').value.trim();
|
|
474
|
-
if (state.busy || (!text && !state.pending.length)) return;
|
|
475
|
-
$('input').value = '';
|
|
476
|
-
autosize();
|
|
477
|
-
if (state.convo.mode === 'agent') await sendAgent(text);
|
|
478
|
-
else await sendChat(text);
|
|
479
|
-
}
|
|
480
|
-
|
|
481
|
-
// ---------- wiring ----------
|
|
482
|
-
function autosize() { const input = $('input'); input.style.height = 'auto'; input.style.height = `${Math.min(input.scrollHeight, 240)}px`; }
|
|
483
|
-
$('input').addEventListener('input', autosize);
|
|
484
|
-
$('input').addEventListener('keydown', event => { if (event.key === 'Enter' && !event.shiftKey) { event.preventDefault(); send(); } });
|
|
485
|
-
$('send').onclick = send;
|
|
486
|
-
$('stop').onclick = () => state.abort?.abort();
|
|
487
|
-
$('attach').onclick = () => $('fileInput').click();
|
|
488
|
-
$('fileInput').onchange = event => { addFiles(event.target.files); event.target.value = ''; };
|
|
489
|
-
$('newChat').onclick = () => startNew();
|
|
490
|
-
$('menu').onclick = () => $('side').classList.toggle('open');
|
|
491
|
-
$('tabChat').onclick = () => { if (state.convo.mode !== 'chat') startNew('chat'); };
|
|
492
|
-
$('tabAgent').onclick = () => { if (state.convo.mode !== 'agent') startNew('agent'); };
|
|
493
|
-
$('model').onchange = () => { localStorage.setItem('labs.model', $('model').value); render(); };
|
|
494
|
-
const composer = $('composer');
|
|
495
|
-
['dragenter', 'dragover'].forEach(name => composer.addEventListener(name, event => { event.preventDefault(); composer.classList.add('drag'); }));
|
|
496
|
-
['dragleave', 'drop'].forEach(name => composer.addEventListener(name, event => { event.preventDefault(); composer.classList.remove('drag'); }));
|
|
497
|
-
composer.addEventListener('drop', event => addFiles(event.dataTransfer.files));
|
|
498
|
-
$('messages').addEventListener('click', event => {
|
|
499
|
-
if (!event.target.classList.contains('copy')) return;
|
|
500
|
-
navigator.clipboard.writeText(event.target.parentElement.querySelector('code').textContent);
|
|
501
|
-
event.target.textContent = 'Copied';
|
|
502
|
-
setTimeout(() => { event.target.textContent = 'Copy'; }, 1200);
|
|
503
|
-
});
|
|
504
|
-
|
|
505
|
-
function showLogin() { $('app').classList.add('hidden'); $('login').classList.remove('hidden'); $('keyInput').focus(); }
|
|
506
|
-
$('loginForm').onsubmit = async event => {
|
|
507
|
-
event.preventDefault();
|
|
508
|
-
const response = await fetch('/chat/login', { method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify({ key: $('keyInput').value.trim() }) });
|
|
509
|
-
if (!response.ok) { $('loginError').textContent = 'That key is not right.'; return; }
|
|
510
|
-
$('keyInput').value = '';
|
|
511
|
-
boot();
|
|
512
|
-
};
|
|
513
|
-
|
|
514
|
-
async function boot() {
|
|
515
|
-
try { await api('/chat/api/me'); } catch { return; }
|
|
516
|
-
$('login').classList.add('hidden');
|
|
517
|
-
$('app').classList.remove('hidden');
|
|
518
|
-
const { data } = await (await api('/v1/models')).json();
|
|
519
|
-
const preferred = localStorage.getItem('labs.model') || 'glm';
|
|
520
|
-
$('model').innerHTML = '';
|
|
521
|
-
for (const model of data) {
|
|
522
|
-
const option = document.createElement('option');
|
|
523
|
-
option.value = model.id;
|
|
524
|
-
option.textContent = ({ glm: 'GLM 5.3', glm52: 'GLM 5.2 (ElevenLabs)' })[model.id] || model.id;
|
|
525
|
-
$('model').appendChild(option);
|
|
526
|
-
}
|
|
527
|
-
$('model').value = data.some(m => m.id === preferred) ? preferred : 'glm';
|
|
528
|
-
startNew('chat');
|
|
529
|
-
$('input').focus();
|
|
530
|
-
}
|
|
531
|
-
boot();
|
|
532
|
-
</script>
|
|
533
|
-
</body>
|
|
534
|
-
</html>
|
|
1
|
+
<!doctype html>
|
|
2
|
+
<html lang="en">
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="utf-8">
|
|
5
|
+
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
6
|
+
<title>Labs Chat</title>
|
|
7
|
+
<style>
|
|
8
|
+
:root {
|
|
9
|
+
--bg: #f7f7f5; --surface: #ffffff; --line: #e6e4df; --text: #1d1b18; --muted: #77726b;
|
|
10
|
+
--accent: #0f766e; --accent-soft: #e6f2f0; --danger: #b42318; --code: #f3f2ef;
|
|
11
|
+
--radius: 12px; --mono: ui-monospace, "Cascadia Code", Consolas, monospace;
|
|
12
|
+
}
|
|
13
|
+
* { box-sizing: border-box; }
|
|
14
|
+
html, body { height: 100%; margin: 0; }
|
|
15
|
+
body { background: var(--bg); color: var(--text); font: 15px/1.55 system-ui, -apple-system, "Segoe UI", sans-serif; }
|
|
16
|
+
button, input, textarea, select { font: inherit; color: inherit; }
|
|
17
|
+
button { cursor: pointer; border: 1px solid var(--line); background: var(--surface); border-radius: 8px; padding: 6px 12px; }
|
|
18
|
+
button:hover { border-color: #cfccc5; }
|
|
19
|
+
button.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
|
|
20
|
+
button.ghost { border-color: transparent; background: transparent; }
|
|
21
|
+
.hidden { display: none !important; }
|
|
22
|
+
|
|
23
|
+
/* login */
|
|
24
|
+
#login { height: 100%; display: grid; place-items: center; padding: 16px; }
|
|
25
|
+
#login form { width: min(380px, 100%); background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 28px; }
|
|
26
|
+
#login h1 { margin: 0 0 4px; font-size: 20px; }
|
|
27
|
+
#login p { margin: 0 0 18px; color: var(--muted); font-size: 14px; }
|
|
28
|
+
#login input { width: 100%; padding: 10px 12px; border: 1px solid var(--line); border-radius: 8px; margin-bottom: 12px; }
|
|
29
|
+
#login .error { color: var(--danger); font-size: 14px; min-height: 20px; }
|
|
30
|
+
|
|
31
|
+
/* layout */
|
|
32
|
+
#app { height: 100%; display: grid; grid-template-columns: 260px 1fr; }
|
|
33
|
+
aside { border-right: 1px solid var(--line); background: var(--surface); display: flex; flex-direction: column; min-height: 0; }
|
|
34
|
+
aside .top { padding: 14px; display: flex; gap: 8px; }
|
|
35
|
+
aside .top button { flex: 1; }
|
|
36
|
+
#list { overflow-y: auto; padding: 0 8px 12px; flex: 1; }
|
|
37
|
+
#list .item { display: flex; align-items: center; gap: 6px; padding: 8px 10px; border-radius: 8px; cursor: pointer; font-size: 14px; }
|
|
38
|
+
#list .item:hover { background: var(--bg); }
|
|
39
|
+
#list .item.active { background: var(--accent-soft); }
|
|
40
|
+
#list .item span { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
|
|
41
|
+
#list .item .del { opacity: 0; padding: 0 6px; border: 0; background: transparent; color: var(--muted); }
|
|
42
|
+
#list .item:hover .del { opacity: 1; }
|
|
43
|
+
#list .tag { font-size: 11px; color: var(--muted); }
|
|
44
|
+
main { display: flex; flex-direction: column; min-width: 0; min-height: 0; }
|
|
45
|
+
header { display: flex; align-items: center; gap: 10px; padding: 10px 18px; border-bottom: 1px solid var(--line); background: var(--surface); flex-wrap: wrap; }
|
|
46
|
+
header .title { font-weight: 600; flex: 1; min-width: 120px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
|
|
47
|
+
.tabs { display: inline-flex; border: 1px solid var(--line); border-radius: 8px; overflow: hidden; }
|
|
48
|
+
.tabs button { border: 0; border-radius: 0; padding: 5px 14px; }
|
|
49
|
+
.tabs button.on { background: var(--accent); color: #fff; }
|
|
50
|
+
select { border: 1px solid var(--line); border-radius: 8px; padding: 5px 8px; background: var(--surface); }
|
|
51
|
+
#menu { display: none; }
|
|
52
|
+
|
|
53
|
+
/* agent bar */
|
|
54
|
+
#agentbar { display: flex; gap: 8px; padding: 10px 18px; border-bottom: 1px solid var(--line); background: var(--surface); flex-wrap: wrap; }
|
|
55
|
+
#agentbar input { flex: 1; min-width: 200px; padding: 6px 10px; border: 1px solid var(--line); border-radius: 8px; font-family: var(--mono); font-size: 13px; }
|
|
56
|
+
|
|
57
|
+
/* messages */
|
|
58
|
+
#scroll { flex: 1; overflow-y: auto; }
|
|
59
|
+
#messages { max-width: 820px; margin: 0 auto; padding: 24px 18px 40px; }
|
|
60
|
+
.empty { color: var(--muted); text-align: center; margin-top: 18vh; }
|
|
61
|
+
.empty h2 { color: var(--text); font-weight: 600; margin-bottom: 6px; }
|
|
62
|
+
.msg { margin: 0 0 22px; }
|
|
63
|
+
.msg.user .bubble { background: var(--accent-soft); border-radius: var(--radius); padding: 10px 14px; margin-left: auto; width: fit-content; max-width: 85%; white-space: pre-wrap; }
|
|
64
|
+
.msg .files { display: flex; gap: 6px; flex-wrap: wrap; justify-content: flex-end; margin-bottom: 6px; }
|
|
65
|
+
.chip { font-size: 12px; border: 1px solid var(--line); border-radius: 999px; padding: 2px 10px; background: var(--surface); color: var(--muted); }
|
|
66
|
+
.msg.assistant .body > :first-child { margin-top: 0; }
|
|
67
|
+
.msg .meta { font-size: 12px; color: var(--muted); margin-top: 6px; }
|
|
68
|
+
.msg.error .body { color: var(--danger); }
|
|
69
|
+
pre { background: var(--code); border: 1px solid var(--line); border-radius: 10px; padding: 12px 14px; overflow-x: auto; position: relative; font: 13px/1.5 var(--mono); }
|
|
70
|
+
pre .copy { position: absolute; top: 6px; right: 6px; font-size: 12px; padding: 2px 8px; }
|
|
71
|
+
code { font-family: var(--mono); font-size: 0.9em; background: var(--code); padding: 1px 5px; border-radius: 5px; }
|
|
72
|
+
pre code { background: none; padding: 0; }
|
|
73
|
+
.step { font: 13px/1.5 var(--mono); border-left: 3px solid var(--line); padding: 4px 10px; margin: 6px 0; color: var(--muted); white-space: pre-wrap; word-break: break-word; }
|
|
74
|
+
.step.tool { border-color: var(--accent); color: var(--text); }
|
|
75
|
+
.step details summary { cursor: pointer; }
|
|
76
|
+
|
|
77
|
+
/* composer */
|
|
78
|
+
#composer { border-top: 1px solid var(--line); background: var(--surface); padding: 12px 18px 16px; }
|
|
79
|
+
#composer .box { max-width: 820px; margin: 0 auto; border: 1px solid var(--line); border-radius: var(--radius); padding: 8px; background: var(--surface); }
|
|
80
|
+
#composer.drag .box { border-color: var(--accent); background: var(--accent-soft); }
|
|
81
|
+
#input { width: 100%; border: 0; outline: 0; resize: none; max-height: 240px; min-height: 44px; padding: 4px 6px; background: transparent; }
|
|
82
|
+
#composer .row { display: flex; align-items: center; gap: 8px; }
|
|
83
|
+
#composer .row .grow { flex: 1; display: flex; gap: 6px; flex-wrap: wrap; }
|
|
84
|
+
#composer .hint { max-width: 820px; margin: 6px auto 0; font-size: 12px; color: var(--muted); }
|
|
85
|
+
|
|
86
|
+
@media (max-width: 760px) {
|
|
87
|
+
#app { grid-template-columns: 1fr; }
|
|
88
|
+
aside { position: fixed; inset: 0 30% 0 0; z-index: 5; transform: translateX(-100%); transition: transform .2s; }
|
|
89
|
+
aside.open { transform: none; box-shadow: 0 0 0 100vw rgba(0,0,0,.2); }
|
|
90
|
+
#menu { display: inline-block; }
|
|
91
|
+
}
|
|
92
|
+
</style>
|
|
93
|
+
</head>
|
|
94
|
+
<body>
|
|
95
|
+
|
|
96
|
+
<div id="login" class="hidden">
|
|
97
|
+
<form id="loginForm">
|
|
98
|
+
<h1>Labs Chat</h1>
|
|
99
|
+
<p>Private to this laptop. Type <code>labs chat</code> in a terminal: it opens this page already logged in. Or paste your labs key here.</p>
|
|
100
|
+
<input id="keyInput" type="password" placeholder="labs key" autocomplete="off">
|
|
101
|
+
<div class="error" id="loginError"></div>
|
|
102
|
+
<button class="primary" type="submit">Open</button>
|
|
103
|
+
</form>
|
|
104
|
+
</div>
|
|
105
|
+
|
|
106
|
+
<div id="app" class="hidden">
|
|
107
|
+
<aside id="side">
|
|
108
|
+
<div class="top"><button class="primary" id="newChat">+ New chat</button></div>
|
|
109
|
+
<div id="list"></div>
|
|
110
|
+
</aside>
|
|
111
|
+
<main>
|
|
112
|
+
<header>
|
|
113
|
+
<button class="ghost" id="menu" aria-label="Chats">☰</button>
|
|
114
|
+
<div class="title" id="title">New chat</div>
|
|
115
|
+
<div class="tabs" role="tablist">
|
|
116
|
+
<button id="tabChat" class="on">Chat</button>
|
|
117
|
+
<button id="tabAgent">Agent</button>
|
|
118
|
+
</div>
|
|
119
|
+
<select id="model" title="Model"></select>
|
|
120
|
+
</header>
|
|
121
|
+
<div id="agentbar" class="hidden">
|
|
122
|
+
<input id="folder" placeholder="Folder for the agent, e.g. D:\Humopedia" spellcheck="false">
|
|
123
|
+
<select id="access">
|
|
124
|
+
<option value="edits">Can read and edit files</option>
|
|
125
|
+
<option value="full">Full access (commands too)</option>
|
|
126
|
+
</select>
|
|
127
|
+
</div>
|
|
128
|
+
<div id="scroll"><div id="messages"></div></div>
|
|
129
|
+
<div id="composer">
|
|
130
|
+
<div class="box">
|
|
131
|
+
<textarea id="input" rows="1" placeholder="Message GLM 5.3"></textarea>
|
|
132
|
+
<div class="row">
|
|
133
|
+
<div class="grow">
|
|
134
|
+
<button class="ghost" id="attach" title="Attach files">📎</button>
|
|
135
|
+
<span id="pending"></span>
|
|
136
|
+
</div>
|
|
137
|
+
<button class="primary" id="send">Send</button>
|
|
138
|
+
<button id="stop" class="hidden">Stop</button>
|
|
139
|
+
</div>
|
|
140
|
+
</div>
|
|
141
|
+
<div class="hint" id="hint">Enter to send · Shift+Enter for a new line · drop files here</div>
|
|
142
|
+
<input type="file" id="fileInput" multiple class="hidden">
|
|
143
|
+
</div>
|
|
144
|
+
</main>
|
|
145
|
+
</div>
|
|
146
|
+
|
|
147
|
+
<script>
|
|
148
|
+
// GLM 5.3 on Bedrock, US endpoint (third-party listing, Oct 2026); used only for the cost line.
|
|
149
|
+
const PRICE_PER_MILLION = { input: 1.85, output: 5.81 };
|
|
150
|
+
const SYSTEM = 'You are a sharp, friendly expert assistant for Boss (Murodjon), founder of Humopedia. Answer clearly and concisely. Use Markdown. When reviewing security, stay defensive: his own systems only.';
|
|
151
|
+
const $ = id => document.getElementById(id);
|
|
152
|
+
const state = { convo: null, list: [], pending: [], abort: null, busy: false };
|
|
153
|
+
|
|
154
|
+
function newConvo(mode = 'chat') {
|
|
155
|
+
return { id: `c${Date.now().toString(36)}${Math.random().toString(36).slice(2, 6)}`, title: 'New chat', mode, messages: [], events: [], usage: { input: 0, output: 0 }, folder: localStorage.getItem('labs.folder') || '', sessionId: null };
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
// ---------- api ----------
|
|
159
|
+
async function api(path, options = {}) {
|
|
160
|
+
const response = await fetch(path, { ...options, headers: { 'content-type': 'application/json', 'x-labs-chat': '1', ...(options.headers || {}) } });
|
|
161
|
+
if (response.status === 401) { showLogin(); throw new Error('Please log in'); }
|
|
162
|
+
if (!response.ok) throw new Error((await response.json().catch(() => ({}))).error?.message || `HTTP ${response.status}`);
|
|
163
|
+
return response;
|
|
164
|
+
}
|
|
165
|
+
const save = () => api(`/chat/api/conversations/${state.convo.id}`, { method: 'PUT', body: JSON.stringify(state.convo) }).then(loadList);
|
|
166
|
+
|
|
167
|
+
async function loadList() {
|
|
168
|
+
const { conversations } = await (await api('/chat/api/conversations')).json();
|
|
169
|
+
state.list = conversations;
|
|
170
|
+
$('list').innerHTML = '';
|
|
171
|
+
for (const item of conversations) {
|
|
172
|
+
const row = document.createElement('div');
|
|
173
|
+
row.className = `item${state.convo?.id === item.id ? ' active' : ''}`;
|
|
174
|
+
row.innerHTML = `<span></span><small class="tag">${item.mode === 'agent' ? 'agent' : ''}</small><button class="del" title="Delete">×</button>`;
|
|
175
|
+
row.querySelector('span').textContent = item.title || 'Untitled';
|
|
176
|
+
row.onclick = () => openConvo(item.id);
|
|
177
|
+
row.querySelector('.del').onclick = async event => {
|
|
178
|
+
event.stopPropagation();
|
|
179
|
+
await api(`/chat/api/conversations/${item.id}`, { method: 'DELETE' });
|
|
180
|
+
if (state.convo?.id === item.id) startNew();
|
|
181
|
+
loadList();
|
|
182
|
+
};
|
|
183
|
+
$('list').appendChild(row);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
async function openConvo(id) {
|
|
188
|
+
state.convo = await (await api(`/chat/api/conversations/${id}`)).json();
|
|
189
|
+
state.convo.events ||= []; state.convo.messages ||= []; state.convo.usage ||= { input: 0, output: 0 };
|
|
190
|
+
$('side').classList.remove('open');
|
|
191
|
+
render(); loadList();
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
function startNew(mode = state.convo?.mode || 'chat') {
|
|
195
|
+
state.convo = newConvo(mode);
|
|
196
|
+
render(); loadList();
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
// ---------- rendering ----------
|
|
200
|
+
function escapeHtml(text) {
|
|
201
|
+
return text.replace(/[&<>"']/g, c => ({ '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' }[c]));
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
function markdown(source) {
|
|
205
|
+
const blocks = [];
|
|
206
|
+
let text = escapeHtml(source).replace(/```([\w+-]*)\n([\s\S]*?)(```|$)/g, (m, lang, code) => {
|
|
207
|
+
blocks.push(`<pre><button class="copy">Copy</button><code>${code.replace(/\n$/, '')}</code></pre>`);
|
|
208
|
+
return `\u0000${blocks.length - 1}\u0000`;
|
|
209
|
+
});
|
|
210
|
+
text = text
|
|
211
|
+
.replace(/`([^`\n]+)`/g, '<code>$1</code>')
|
|
212
|
+
.replace(/\*\*([^*\n]+)\*\*/g, '<strong>$1</strong>')
|
|
213
|
+
.replace(/^#{1,6} (.+)$/gm, '<strong>$1</strong>')
|
|
214
|
+
.replace(/\[([^\]]+)\]\((https?:[^)\s]+)\)/g, '<a href="$2" target="_blank" rel="noopener">$1</a>');
|
|
215
|
+
const html = text.split(/\n{2,}/).map(part => {
|
|
216
|
+
if (/^\u0000\d+\u0000$/.test(part.trim())) return part.trim();
|
|
217
|
+
if (/^(\s*([-*]|\d+\.) .+\n?)+$/.test(part)) {
|
|
218
|
+
const ordered = /^\s*\d+\./.test(part);
|
|
219
|
+
const items = part.trim().split('\n').map(line => `<li>${line.replace(/^\s*([-*]|\d+\.) /, '')}</li>`).join('');
|
|
220
|
+
return ordered ? `<ol>${items}</ol>` : `<ul>${items}</ul>`;
|
|
221
|
+
}
|
|
222
|
+
return `<p>${part.replace(/\n/g, '<br>')}</p>`;
|
|
223
|
+
}).join('');
|
|
224
|
+
return html.replace(/\u0000(\d+)\u0000/g, (m, index) => blocks[index]);
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
function messageText(content) {
|
|
228
|
+
return typeof content === 'string' ? content : content.filter(b => b.type === 'text').map(b => b.text).join('\n');
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
function addMessage(role, html, meta = '') {
|
|
232
|
+
const element = document.createElement('div');
|
|
233
|
+
element.className = `msg ${role}`;
|
|
234
|
+
element.innerHTML = `<div class="${role === 'user' ? 'bubble' : 'body'}"></div><div class="meta"></div>`;
|
|
235
|
+
if (role === 'user') element.firstChild.textContent = html; else element.firstChild.innerHTML = html;
|
|
236
|
+
element.lastChild.textContent = meta;
|
|
237
|
+
$('messages').appendChild(element);
|
|
238
|
+
return element;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
// Only GLM 5.3 is billed in dollars (AWS); every other model spends ElevenLabs credits.
|
|
242
|
+
function costText(usage, model = $('model').value) {
|
|
243
|
+
const tokens = `in ${usage.input.toLocaleString()} · out ${usage.output.toLocaleString()} tokens`;
|
|
244
|
+
if (model !== 'glm') return `${tokens} · ElevenLabs credits`;
|
|
245
|
+
const dollars = usage.input / 1e6 * PRICE_PER_MILLION.input + usage.output / 1e6 * PRICE_PER_MILLION.output;
|
|
246
|
+
return `${tokens} · ≈ ${dollars.toFixed(4)} AWS`;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
function renderStep(event) {
|
|
250
|
+
// prompts typed in agent mode are stored as events so a reload shows them
|
|
251
|
+
if (event.type === 'prompt') return addMessage('user', event.text);
|
|
252
|
+
const element = document.createElement('div');
|
|
253
|
+
if (event.type === 'assistant') {
|
|
254
|
+
for (const block of event.message?.content || []) {
|
|
255
|
+
if (block.type === 'text' && block.text.trim()) addMessage('assistant', markdown(block.text));
|
|
256
|
+
if (block.type === 'tool_use') {
|
|
257
|
+
const step = document.createElement('div');
|
|
258
|
+
step.className = 'step tool';
|
|
259
|
+
const input = block.input?.command || block.input?.file_path || block.input?.pattern || JSON.stringify(block.input || {});
|
|
260
|
+
step.textContent = `🔧 ${block.name} ${String(input).slice(0, 300)}`;
|
|
261
|
+
$('messages').appendChild(step);
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
return;
|
|
265
|
+
}
|
|
266
|
+
if (event.type === 'user') {
|
|
267
|
+
for (const block of event.message?.content || []) {
|
|
268
|
+
if (block.type !== 'tool_result') continue;
|
|
269
|
+
const text = typeof block.content === 'string' ? block.content : (block.content || []).map(b => b.text || '').join('\n');
|
|
270
|
+
element.className = 'step';
|
|
271
|
+
element.innerHTML = '<details><summary></summary><div></div></details>';
|
|
272
|
+
element.querySelector('summary').textContent = `${block.is_error ? '⚠️' : '↳'} ${text.split('\n')[0].slice(0, 120) || '(no output)'}`;
|
|
273
|
+
element.querySelector('details div').textContent = text.slice(0, 6000);
|
|
274
|
+
$('messages').appendChild(element);
|
|
275
|
+
}
|
|
276
|
+
return;
|
|
277
|
+
}
|
|
278
|
+
if (event.type === 'result') {
|
|
279
|
+
const usage = event.usage || {};
|
|
280
|
+
const total = { input: (usage.input_tokens || 0) + (usage.cache_read_input_tokens || 0) + (usage.cache_creation_input_tokens || 0), output: usage.output_tokens || 0 };
|
|
281
|
+
element.className = 'msg assistant';
|
|
282
|
+
element.innerHTML = '<div class="meta"></div>';
|
|
283
|
+
element.firstChild.textContent = `${event.is_error ? '❌ ' : '✅ '}done in ${Math.round((event.duration_ms || 0) / 1000)} s · ${event.num_turns || 0} turns · ${costText(total)}`;
|
|
284
|
+
$('messages').appendChild(element);
|
|
285
|
+
return;
|
|
286
|
+
}
|
|
287
|
+
if (event.type === 'log' && /error|❌|🚨/i.test(event.text)) {
|
|
288
|
+
element.className = 'step';
|
|
289
|
+
element.textContent = event.text.trim();
|
|
290
|
+
$('messages').appendChild(element);
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
function render() {
|
|
295
|
+
const convo = state.convo;
|
|
296
|
+
$('title').textContent = convo.title;
|
|
297
|
+
$('tabChat').classList.toggle('on', convo.mode === 'chat');
|
|
298
|
+
$('tabAgent').classList.toggle('on', convo.mode === 'agent');
|
|
299
|
+
$('agentbar').classList.toggle('hidden', convo.mode !== 'agent');
|
|
300
|
+
$('folder').value = convo.folder || '';
|
|
301
|
+
$('input').placeholder = convo.mode === 'agent' ? 'Tell the agent what to do in this folder' : `Message ${$('model').selectedOptions[0]?.textContent || 'GLM 5.3'}`;
|
|
302
|
+
$('messages').innerHTML = '';
|
|
303
|
+
const empty = convo.mode === 'agent' ? convo.events.length === 0 : convo.messages.length === 0;
|
|
304
|
+
if (empty) {
|
|
305
|
+
$('messages').innerHTML = convo.mode === 'agent'
|
|
306
|
+
? '<div class="empty"><h2>Agent</h2>Claude Code on GLM 5.3 works inside the folder above: it reads, edits and runs things there.</div>'
|
|
307
|
+
: '<div class="empty"><h2>What are we working on?</h2>GLM 5.3 on Amazon Bedrock · paid from your AWS credits</div>';
|
|
308
|
+
}
|
|
309
|
+
if (convo.mode === 'agent') convo.events.forEach(renderStep);
|
|
310
|
+
else for (const message of convo.messages) {
|
|
311
|
+
if (message.role === 'user') {
|
|
312
|
+
const element = addMessage('user', message.display ?? messageText(message.content));
|
|
313
|
+
if (message.files?.length) {
|
|
314
|
+
const files = document.createElement('div');
|
|
315
|
+
files.className = 'files';
|
|
316
|
+
message.files.forEach(name => { const chip = document.createElement('span'); chip.className = 'chip'; chip.textContent = name; files.appendChild(chip); });
|
|
317
|
+
element.prepend(files);
|
|
318
|
+
}
|
|
319
|
+
} else addMessage('assistant', markdown(messageText(message.content)), message.meta || '');
|
|
320
|
+
}
|
|
321
|
+
scrollDown();
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
const scrollDown = () => { $('scroll').scrollTop = $('scroll').scrollHeight; };
|
|
325
|
+
|
|
326
|
+
// ---------- attachments ----------
|
|
327
|
+
const TEXT_LIMIT = 2_000_000, IMAGE_LIMIT = 5_000_000;
|
|
328
|
+
function readFile(file) {
|
|
329
|
+
return new Promise((resolve, reject) => {
|
|
330
|
+
const reader = new FileReader();
|
|
331
|
+
reader.onerror = () => reject(reader.error);
|
|
332
|
+
if (file.type.startsWith('image/')) {
|
|
333
|
+
if (file.size > IMAGE_LIMIT) return reject(new Error(`${file.name} is over 5 MB`));
|
|
334
|
+
reader.onload = () => resolve({ name: file.name, image: { type: 'image', source: { type: 'base64', media_type: file.type, data: reader.result.split(',')[1] } } });
|
|
335
|
+
reader.readAsDataURL(file);
|
|
336
|
+
} else {
|
|
337
|
+
if (file.size > TEXT_LIMIT) return reject(new Error(`${file.name} is over 2 MB`));
|
|
338
|
+
reader.onload = () => resolve({ name: file.name, text: reader.result });
|
|
339
|
+
reader.readAsText(file);
|
|
340
|
+
}
|
|
341
|
+
});
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
async function addFiles(files) {
|
|
345
|
+
for (const file of files) {
|
|
346
|
+
try { state.pending.push(await readFile(file)); } catch (error) { alertHint(error.message); }
|
|
347
|
+
}
|
|
348
|
+
showPending();
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
function showPending() {
|
|
352
|
+
$('pending').innerHTML = '';
|
|
353
|
+
state.pending.forEach((file, index) => {
|
|
354
|
+
const chip = document.createElement('span');
|
|
355
|
+
chip.className = 'chip';
|
|
356
|
+
chip.textContent = `${file.name} ×`;
|
|
357
|
+
chip.style.cursor = 'pointer';
|
|
358
|
+
chip.onclick = () => { state.pending.splice(index, 1); showPending(); };
|
|
359
|
+
$('pending').appendChild(chip);
|
|
360
|
+
});
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
function alertHint(text) {
|
|
364
|
+
$('hint').textContent = text;
|
|
365
|
+
setTimeout(() => { $('hint').textContent = 'Enter to send · Shift+Enter for a new line · drop files here'; }, 5000);
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
// ---------- streaming ----------
|
|
369
|
+
async function readSse(response, onData) {
|
|
370
|
+
const reader = response.body.getReader();
|
|
371
|
+
const decoder = new TextDecoder();
|
|
372
|
+
let buffer = '';
|
|
373
|
+
for (;;) {
|
|
374
|
+
const { value, done } = await reader.read();
|
|
375
|
+
if (done) break;
|
|
376
|
+
buffer += decoder.decode(value, { stream: true });
|
|
377
|
+
const parts = buffer.split('\n\n');
|
|
378
|
+
buffer = parts.pop();
|
|
379
|
+
for (const part of parts) {
|
|
380
|
+
const line = part.split('\n').find(l => l.startsWith('data: '));
|
|
381
|
+
if (line) onData(JSON.parse(line.slice(6)));
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
function setBusy(busy) {
|
|
387
|
+
state.busy = busy;
|
|
388
|
+
$('send').classList.toggle('hidden', busy);
|
|
389
|
+
$('stop').classList.toggle('hidden', !busy);
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
async function sendChat(text) {
|
|
393
|
+
const convo = state.convo;
|
|
394
|
+
const files = state.pending.splice(0);
|
|
395
|
+
showPending();
|
|
396
|
+
const fileText = files.filter(f => f.text != null).map(f => `\n\n<file name="${f.name}">\n${f.text}\n</file>`).join('');
|
|
397
|
+
const content = [{ type: 'text', text: text + fileText }, ...files.filter(f => f.image).map(f => f.image)];
|
|
398
|
+
convo.messages.push({ role: 'user', content, display: text, files: files.map(f => f.name) });
|
|
399
|
+
if (convo.title === 'New chat') convo.title = text.slice(0, 60) || files[0]?.name || 'Chat';
|
|
400
|
+
render();
|
|
401
|
+
|
|
402
|
+
const element = addMessage('assistant', '<span class="meta">thinking…</span>');
|
|
403
|
+
const body = element.querySelector('.body');
|
|
404
|
+
let reply = '', usage = null, failure = null;
|
|
405
|
+
state.abort = new AbortController();
|
|
406
|
+
setBusy(true);
|
|
407
|
+
try {
|
|
408
|
+
const response = await api('/v1/messages', {
|
|
409
|
+
method: 'POST', signal: state.abort.signal,
|
|
410
|
+
body: JSON.stringify({ model: $('model').value, max_tokens: 16000, stream: true, system: SYSTEM, messages: convo.messages.map(({ role, content }) => ({ role, content })) })
|
|
411
|
+
});
|
|
412
|
+
await readSse(response, data => {
|
|
413
|
+
if (data.type === 'content_block_delta' && data.delta?.text) {
|
|
414
|
+
reply += data.delta.text;
|
|
415
|
+
body.innerHTML = markdown(reply);
|
|
416
|
+
scrollDown();
|
|
417
|
+
} else if (data.type === 'message_delta') usage = data.usage;
|
|
418
|
+
else if (data.type === 'error') failure = data.error?.message;
|
|
419
|
+
});
|
|
420
|
+
} catch (error) {
|
|
421
|
+
if (error.name !== 'AbortError') failure = error.message;
|
|
422
|
+
}
|
|
423
|
+
setBusy(false);
|
|
424
|
+
if (failure) {
|
|
425
|
+
element.classList.add('error');
|
|
426
|
+
body.textContent = `❌ ${failure}`;
|
|
427
|
+
convo.messages.pop();
|
|
428
|
+
$('input').value = text;
|
|
429
|
+
return;
|
|
430
|
+
}
|
|
431
|
+
const turn = { input: usage?.input_tokens || 0, output: usage?.output_tokens || 0 };
|
|
432
|
+
let meta = costText(turn);
|
|
433
|
+
if ($('model').value === 'glm') {
|
|
434
|
+
convo.usage.input += turn.input; convo.usage.output += turn.output;
|
|
435
|
+
meta += ` · chat total ≈ ${(convo.usage.input / 1e6 * PRICE_PER_MILLION.input + convo.usage.output / 1e6 * PRICE_PER_MILLION.output).toFixed(4)}`;
|
|
436
|
+
}
|
|
437
|
+
element.querySelector('.meta').textContent = meta;
|
|
438
|
+
convo.messages.push({ role: 'assistant', content: [{ type: 'text', text: reply || '(stopped)' }], meta });
|
|
439
|
+
save();
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
async function sendAgent(text) {
|
|
443
|
+
const convo = state.convo;
|
|
444
|
+
convo.folder = $('folder').value.trim();
|
|
445
|
+
if (!convo.folder) return alertHint('Pick a folder first');
|
|
446
|
+
localStorage.setItem('labs.folder', convo.folder);
|
|
447
|
+
if (convo.title === 'New chat') convo.title = text.slice(0, 60);
|
|
448
|
+
convo.events.push({ type: 'prompt', text });
|
|
449
|
+
render();
|
|
450
|
+
state.abort = new AbortController();
|
|
451
|
+
setBusy(true);
|
|
452
|
+
try {
|
|
453
|
+
const response = await api('/chat/api/agent', {
|
|
454
|
+
method: 'POST', signal: state.abort.signal,
|
|
455
|
+
body: JSON.stringify({ folder: convo.folder, prompt: text, model: $('model').value, access: $('access').value, sessionId: convo.sessionId })
|
|
456
|
+
});
|
|
457
|
+
await readSse(response, event => {
|
|
458
|
+
if (event.type === 'system' && event.session_id) convo.sessionId = event.session_id;
|
|
459
|
+
if (['assistant', 'user', 'result', 'log'].includes(event.type)) {
|
|
460
|
+
convo.events.push(event);
|
|
461
|
+
renderStep(event);
|
|
462
|
+
scrollDown();
|
|
463
|
+
}
|
|
464
|
+
});
|
|
465
|
+
} catch (error) {
|
|
466
|
+
if (error.name !== 'AbortError') { convo.events.push({ type: 'log', text: `❌ ${error.message}` }); renderStep(convo.events.at(-1)); }
|
|
467
|
+
}
|
|
468
|
+
setBusy(false);
|
|
469
|
+
save();
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
async function send() {
|
|
473
|
+
const text = $('input').value.trim();
|
|
474
|
+
if (state.busy || (!text && !state.pending.length)) return;
|
|
475
|
+
$('input').value = '';
|
|
476
|
+
autosize();
|
|
477
|
+
if (state.convo.mode === 'agent') await sendAgent(text);
|
|
478
|
+
else await sendChat(text);
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
// ---------- wiring ----------
|
|
482
|
+
function autosize() { const input = $('input'); input.style.height = 'auto'; input.style.height = `${Math.min(input.scrollHeight, 240)}px`; }
|
|
483
|
+
$('input').addEventListener('input', autosize);
|
|
484
|
+
$('input').addEventListener('keydown', event => { if (event.key === 'Enter' && !event.shiftKey) { event.preventDefault(); send(); } });
|
|
485
|
+
$('send').onclick = send;
|
|
486
|
+
$('stop').onclick = () => state.abort?.abort();
|
|
487
|
+
$('attach').onclick = () => $('fileInput').click();
|
|
488
|
+
$('fileInput').onchange = event => { addFiles(event.target.files); event.target.value = ''; };
|
|
489
|
+
$('newChat').onclick = () => startNew();
|
|
490
|
+
$('menu').onclick = () => $('side').classList.toggle('open');
|
|
491
|
+
$('tabChat').onclick = () => { if (state.convo.mode !== 'chat') startNew('chat'); };
|
|
492
|
+
$('tabAgent').onclick = () => { if (state.convo.mode !== 'agent') startNew('agent'); };
|
|
493
|
+
$('model').onchange = () => { localStorage.setItem('labs.model', $('model').value); render(); };
|
|
494
|
+
const composer = $('composer');
|
|
495
|
+
['dragenter', 'dragover'].forEach(name => composer.addEventListener(name, event => { event.preventDefault(); composer.classList.add('drag'); }));
|
|
496
|
+
['dragleave', 'drop'].forEach(name => composer.addEventListener(name, event => { event.preventDefault(); composer.classList.remove('drag'); }));
|
|
497
|
+
composer.addEventListener('drop', event => addFiles(event.dataTransfer.files));
|
|
498
|
+
$('messages').addEventListener('click', event => {
|
|
499
|
+
if (!event.target.classList.contains('copy')) return;
|
|
500
|
+
navigator.clipboard.writeText(event.target.parentElement.querySelector('code').textContent);
|
|
501
|
+
event.target.textContent = 'Copied';
|
|
502
|
+
setTimeout(() => { event.target.textContent = 'Copy'; }, 1200);
|
|
503
|
+
});
|
|
504
|
+
|
|
505
|
+
function showLogin() { $('app').classList.add('hidden'); $('login').classList.remove('hidden'); $('keyInput').focus(); }
|
|
506
|
+
$('loginForm').onsubmit = async event => {
|
|
507
|
+
event.preventDefault();
|
|
508
|
+
const response = await fetch('/chat/login', { method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify({ key: $('keyInput').value.trim() }) });
|
|
509
|
+
if (!response.ok) { $('loginError').textContent = 'That key is not right.'; return; }
|
|
510
|
+
$('keyInput').value = '';
|
|
511
|
+
boot();
|
|
512
|
+
};
|
|
513
|
+
|
|
514
|
+
async function boot() {
|
|
515
|
+
try { await api('/chat/api/me'); } catch { return; }
|
|
516
|
+
$('login').classList.add('hidden');
|
|
517
|
+
$('app').classList.remove('hidden');
|
|
518
|
+
const { data } = await (await api('/v1/models')).json();
|
|
519
|
+
const preferred = localStorage.getItem('labs.model') || 'glm';
|
|
520
|
+
$('model').innerHTML = '';
|
|
521
|
+
for (const model of data) {
|
|
522
|
+
const option = document.createElement('option');
|
|
523
|
+
option.value = model.id;
|
|
524
|
+
option.textContent = ({ glm: 'GLM 5.3', glm52: 'GLM 5.2 (ElevenLabs)' })[model.id] || model.id;
|
|
525
|
+
$('model').appendChild(option);
|
|
526
|
+
}
|
|
527
|
+
$('model').value = data.some(m => m.id === preferred) ? preferred : 'glm';
|
|
528
|
+
startNew('chat');
|
|
529
|
+
$('input').focus();
|
|
530
|
+
}
|
|
531
|
+
boot();
|
|
532
|
+
</script>
|
|
533
|
+
</body>
|
|
534
|
+
</html>
|