Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.

0 downloads

Download zip
Clone
Clone with git
git clone https://gitstable.com/git/askrobots/dbbasic-object-server.git
HTTPS
https://gitstable.com/git/askrobots/dbbasic-object-server.git
SSH
ssh://git@gitstable.com:2222/askrobots/dbbasic-object-server.git

Cloning needs no sign-in. Pushing over HTTPS asks for your username and an access token. SSH needs your key under SSH keys.

danq9 signup: the person chooses the password and name on the confirm page 83c7aa6 2 weeks, 5 days ago History
shell.py · 30.6 KB · 711 lines
1"""The shell: talk to the whole system from one input.
2
3Prefixes are instant record operations against the collection APIs the
4visitor is already authorized for; anything else goes to the AI, which
5answers with the user's own model, key, and MCP tool subset:
6
7 $50 lunch quick note
8 .fix the header quick task
9 ^https://x title save a link
10 ~flywheel global search
11 /help built-ins
12 anything else AI chat with tools
13"""
14
15# Terminal-specific layout only; palette, chrome, and inputs come from /style,
16# so the shell reskins with the active theme like every other page.
17_STYLE = """
18.wrap { display: flex; flex-direction: column; min-height: calc(100vh - 3.5rem); }
19#log { flex: 1; overflow-y: auto; padding-bottom: 1rem;
20 font-family: var(--font-mono); font-size: 0.9rem; }
21.entry { margin-bottom: 0.75rem; }
22.entry .in { color: var(--positive); white-space: pre-wrap; word-break: break-word; }
23.entry .in::before { content: "> "; color: var(--muted); }
24.entry .out { color: var(--text); white-space: pre-wrap; word-break: break-word; }
25.entry .out.err { color: var(--danger); }
26.entry .tools { color: var(--warning); font-size: 0.78rem; }
27.entry .pending { color: var(--muted); }
28form#prompt { display: flex; gap: 0.5rem; border-top: 1px solid var(--line);
29 padding-top: 0.75rem; }
30form#prompt input { flex: 1; font-family: var(--font-mono); }
31#sessbar { border-top: 1px solid var(--line); padding: 0.3rem 0.6rem; }
32#sessbar select { max-width: 100%; font-size: 0.8rem; opacity: 0.85; }
33#attachbar { display: flex; gap: 0.4rem; flex-wrap: wrap; padding: 0.3rem 0.6rem;
34 border-top: 1px solid var(--line); font-size: 0.8rem; }
35#attachbar .chip { border: 1px solid var(--line); border-radius: 10px;
36 padding: 0 0.5rem; display: inline-flex; gap: 0.35rem; align-items: center; }
37#attachbar .chip button { border: 0; background: none; cursor: pointer; color: inherit; }
38body.dragover::after { content: "drop files to attach"; position: fixed; inset: 0;
39 display: grid; place-items: center; font-size: 1.2rem;
40 background: rgba(20, 20, 28, 0.7);
41 border: 2px dashed var(--accent, #b5713a); pointer-events: none; }
42#mic { font-family: var(--font-mono); font-size: 0.78rem; color: var(--muted);
43 background: var(--panel-2); border: 1px solid var(--line); border-radius: var(--radius-sm);
44 padding: 0 0.7rem; cursor: pointer; }
45#mic.listening { color: var(--danger); border-color: var(--danger); }
46/* Rendered-markdown AI output (theme-tokened) */
47.entry .out.md { white-space: normal; }
48.entry .out.md p { margin: 0.35rem 0; }
49.entry .out.md a { color: var(--accent-strong); text-decoration: underline; }
50.entry .out.md strong { color: var(--text); font-weight: 700; }
51.entry .out.md code { background: var(--panel-2); color: var(--accent-strong);
52 padding: 1px 5px; border-radius: 3px; font-size: 0.9em; }
53.entry .out.md pre { background: var(--panel-2); padding: 0.6rem 0.8rem;
54 border-radius: var(--radius-sm); overflow-x: auto; margin: 0.4rem 0; }
55.entry .out.md pre code { background: none; padding: 0; }
56.entry .out.md ul, .entry .out.md ol { padding-left: 1.4rem; margin: 0.3rem 0; }
57.entry .out.md h1, .entry .out.md h2, .entry .out.md h3 { font-size: 1em; margin: 0.4rem 0 0.2rem; }
58/* Materialized /views page, embedded compact under the reply that made it. */
59.viewembed { margin: 0.4rem 0 0.75rem; }
60.viewembed iframe { width: 100%; max-height: 360px; border: 1px solid var(--line);
61 border-radius: var(--radius-md); }
62.viewembed a { display: inline-block; margin-top: 0.25rem; font-size: 0.78rem;
63 color: var(--accent-strong); }
64"""
65
66_HELP = (
67 "$text quick note\\n"
68 ".title quick task\\n"
69 "^url title save a link\\n"
70 "~query global search\\n"
71 "/key anthropic sk-... store your AI key (masked, not logged)\\n"
72 "/keys which services have keys\\n"
73 "/model x set AI model (service:model)\\n"
74 "/tools a,b set AI tool subset\\n"
75 "/voice [on|off] toggle spoken replies + mic input\\n"
76 "/help this text\\n"
77 "anything else goes to the AI with your tools"
78)
79
80_SCRIPT = """
81let SESSION_ID = freshSessionId();
82function freshSessionId() {
83 return crypto.randomUUID ? crypto.randomUUID()
84 : "s-" + Math.random().toString(36).slice(2);
85}
86
87const esc = (s) => String(s ?? "").replace(/[&<>"']/g,
88 (c) => ({"&": "&amp;", "<": "&lt;", ">": "&gt;", '"': "&quot;", "'": "&#39;"}[c]));
89const log = document.getElementById("log");
90let prefs = {id: OWNER_ID, ai_model: "anthropic:claude-sonnet-5",
91 tools: "global_search,list_collections,list_records,get_record,create_record,update_record,read_page",
92 voice_enabled: "false", talk_tts: "auto"};
93let aiHistory = [];
94const TTS_MAX_CHARS = 800;
95const DEFAULT_TALK_TTS = "auto";
96const voiceOn = () => prefs.voice_enabled === "true";
97function talkTtsMode() {
98 const m = String(prefs.talk_tts || DEFAULT_TALK_TTS).trim();
99 return (m === "server" || m === "browser") ? m : DEFAULT_TALK_TTS;
100}
101
102function entry(input) {
103 const div = document.createElement("div");
104 div.className = "entry";
105 div.innerHTML = `<div class="in">${esc(input)}</div><div class="out pending">&hellip;</div>`;
106 log.appendChild(div);
107 log.scrollTop = log.scrollHeight;
108 return div.querySelector(".out");
109}
110
111// Matches a /views/{id} path in already-rendered HTML, but not one that is
112// already the href of a link (dbbasicMarkdown may have linkified it itself
113// from markdown link syntax) -- so linkifyViews() never nests an <a> inside
114// an existing href attribute.
115const VIEWS_PATH_RE = /(?<!href=")\/views\/[A-Za-z0-9_-]+/g;
116
117function linkifyViews(html) {
118 return html.replace(VIEWS_PATH_RE, (path) => `<a href="${path}">${path}</a>`);
119}
120
121function finish(out, text, {err = false, tools = null, markdown = false} = {}) {
122 out.classList.remove("pending");
123 out.classList.toggle("err", err);
124 // Markdown rendering is the shared /markdown utility (window.dbbasicMarkdown),
125 // defined once. If it is unavailable, degrade to escaped plain text — never a
126 // second markdown implementation.
127 if (markdown) {
128 out.classList.add("md");
129 // [[view:<id>]] is the machine channel: extract it for the embed, then
130 // strip it so it is never rendered.
131 const marker = String(text ?? "").match(/\[\[view:([A-Za-z0-9-]+)\]\]/);
132 text = String(text ?? "").replace(/\[\[view:[^\]]*\]\]/g, " ").trim();
133 out.innerHTML = linkifyViews(window.dbbasicMarkdown ? window.dbbasicMarkdown(text) : esc(text));
134 // A materialized page is worth more than a link: embed it right under
135 // the reply, small, with an escape hatch to the full page.
136 const viewMatch = marker ? ["/views/" + marker[1]] : String(text ?? "").match(/\/views\/[A-Za-z0-9_-]+/);
137 if (viewMatch) {
138 const path = viewMatch[0].replace(/[.,;:)]+$/, "");
139 const embed = document.createElement("div");
140 embed.className = "viewembed";
141 embed.innerHTML = `<iframe src="${path}?embed=1"></iframe><a href="${path}" target="_blank" rel="noopener">open ↗</a>`;
142 out.insertAdjacentElement("afterend", embed);
143 }
144 } else { out.textContent = text; }
145 if (tools && tools.length) {
146 const info = document.createElement("div");
147 info.className = "tools";
148 info.textContent = "tools: " + tools.map((t) => `${t.name}(${t.http_status})`).join(" ");
149 out.parentNode.insertBefore(info, out);
150 }
151 log.scrollTop = log.scrollHeight;
152}
153
154// Strip markdown down to sentences worth speaking: fenced/inline code and
155// tool noise never reach TTS, only the assistant's prose does.
156function stripForSpeech(text) {
157 return String(text ?? "")
158 .replace(/```[\\s\\S]*?```/g, " ")
159 .replace(/`[^`]*`/g, " ")
160 .replace(/!\\[[^\\]]*\\]\\([^)]*\\)/g, " ")
161 .replace(/\\[([^\\]]*)\\]\\([^)]*\\)/g, "$1")
162 .replace(/https?:\\/\\/\\S+/g, " ")
163 .replace(/\\/views\\/\\S+/g, " ")
164 .replace(/\\[\\[view:[^\\]]*\\]\\]/g, " ")
165 .replace(/[*_#>~]/g, " ")
166 .replace(/\\s+/g, " ")
167 .trim()
168 .slice(0, TTS_MAX_CHARS);
169}
170
171let currentAudio = null;
172
173// Voice picking for the browser speechSynthesis engine -- kept identical to
174// talk.py's pickVoice()/hasLocalVoice(): prefer a natural-sounding named
175// voice, then the first on-device (localService) English voice, else leave
176// utter.voice unset and let the browser use its own default.
177const PREFERRED_VOICE_RE = /Samantha|Ava|Karen|Daniel/;
178function getVoices() {
179 return (window.speechSynthesis && window.speechSynthesis.getVoices()) || [];
180}
181function pickVoice() {
182 const voices = getVoices();
183 if (!voices.length) return null;
184 const byName = voices.find((v) => PREFERRED_VOICE_RE.test(v.name));
185 if (byName) return byName;
186 const localEn = voices.find((v) => v.localService && /^en/i.test(v.lang));
187 return localEn || null;
188}
189// The "auto" engine gate: is there at least one on-device voice at all
190// (any language) -- if so, speaking never has to leave the device.
191function hasLocalVoice() {
192 return getVoices().some((v) => v.localService);
193}
194
195// speechSynthesis-only speaking path, used both when talk_tts is "browser"
196// and as the fallback when "server"/"auto" server TTS fails.
197function speakBrowser(text) {
198 if (!window.speechSynthesis) return;
199 window.speechSynthesis.cancel();
200 const utter = new SpeechSynthesisUtterance(text);
201 const voice = pickVoice();
202 if (voice) utter.voice = voice;
203 window.speechSynthesis.speak(utter);
204}
205
206// Speak one assistant reply, routed by the talk_tts preference:
207// "server" -- always server TTS (POST /api/tts), speechSynthesis on failure.
208// "browser" -- always speechSynthesis, no server round-trip.
209// "auto" -- speechSynthesis if an on-device voice is available, else
210// server-with-fallback, same as "server".
211// Any failure -- flag off, no engine, network -- falls back to the
212// browser's own speechSynthesis so voice mode never just goes silent.
213async function speak(text) {
214 const spoken = stripForSpeech(text);
215 if (!spoken) return;
216
217 const mode = talkTtsMode();
218 if (mode === "browser") { speakBrowser(spoken); return; }
219 if (mode === "auto" && window.speechSynthesis && hasLocalVoice()) {
220 speakBrowser(spoken);
221 return;
222 }
223
224 try {
225 const res = await fetch("/api/tts", {
226 method: "POST", credentials: "same-origin",
227 headers: {"content-type": "application/json", accept: "audio/wav"},
228 body: JSON.stringify({text: spoken}),
229 });
230 if (!res.ok) throw new Error("tts endpoint failed");
231 const url = URL.createObjectURL(await res.blob());
232 if (currentAudio) currentAudio.pause();
233 currentAudio = new Audio(url);
234 currentAudio.addEventListener("ended", () => URL.revokeObjectURL(url));
235 await currentAudio.play();
236 } catch (e) {
237 speakBrowser(spoken);
238 }
239}
240
241// Push-to-talk mic. Absent the browser API the button stays hidden -- no
242// polyfills, no fallback recorder, voice input just isn't offered.
243const SpeechRecognitionCtor = window.SpeechRecognition || window.webkitSpeechRecognition;
244let recognizer = null;
245let listening = false;
246
247function stopListening() {
248 listening = false;
249 const mic = document.getElementById("mic");
250 if (mic) { mic.classList.remove("listening"); mic.textContent = "mic"; }
251 if (recognizer) { try { recognizer.stop(); } catch (e) { /* already stopped */ } }
252}
253
254function startListening() {
255 const input = document.querySelector('#prompt input[name="line"]');
256 if (!recognizer || listening || !input) return;
257 listening = true;
258 const mic = document.getElementById("mic");
259 if (mic) { mic.classList.add("listening"); mic.textContent = "listening"; }
260 input.value = "";
261 try { recognizer.start(); } catch (e) { stopListening(); }
262}
263
264function initMic() {
265 const mic = document.getElementById("mic");
266 if (!SpeechRecognitionCtor || !mic) return;
267 mic.hidden = false;
268 recognizer = new SpeechRecognitionCtor();
269 recognizer.continuous = false;
270 recognizer.interimResults = true;
271 recognizer.lang = "en-US";
272
273 recognizer.onresult = (event) => {
274 const input = document.querySelector('#prompt input[name="line"]');
275 if (!input) return;
276 let text = "";
277 // Segments are joined with an explicit space. Chrome includes leading
278 // spaces on continuation segments; iOS does NOT, and concatenating
279 // raw produced "comptureshowmemynotes" from an iPad -- one giant
280 // token the whitespace tokenizer cannot split, so the wake gate could
281 // never match and every utterance was invisibly discarded. Trimming
282 // then joining is correct on both: the tokenizer collapses runs of
283 // whitespace anyway, so a doubled space costs nothing.
284 for (let i = 0; i < event.results.length; i++) {
285 const seg = event.results[i][0].transcript.trim();
286 if (seg) text += (text ? " " : "") + seg;
287 }
288 input.value = text;
289 if (event.results[event.results.length - 1].isFinal) {
290 stopListening();
291 // Final transcript goes through the *existing* submit path unchanged.
292 const form = document.getElementById("prompt");
293 if (input.value.trim() && form.requestSubmit) form.requestSubmit();
294 }
295 };
296 recognizer.onerror = () => stopListening();
297 recognizer.onend = () => stopListening();
298
299 mic.addEventListener("click", () => (listening ? stopListening() : startListening()));
300}
301
302async function api(method, path, payload) {
303 const res = await fetch(path, {
304 method, credentials: "same-origin",
305 headers: {"content-type": "application/json", accept: "application/json"},
306 body: payload === undefined ? undefined : JSON.stringify(payload),
307 });
308 return [res.ok, await res.json()];
309}
310
311async function record(input, output, kind) {
312 api("POST", "/collections/shell_commands/records",
313 {id: crypto.randomUUID(), input, output: String(output).slice(0, 4000),
314 kind, owner_id: OWNER_ID});
315}
316
317async function loadPrefs() {
318 const res = await fetch(`/collections/shell_preferences/records/${OWNER_ID}`,
319 {credentials: "same-origin", headers: {accept: "application/json"}});
320 if (res.ok) { const body = await res.json(); prefs = body.record || prefs; }
321}
322
323// === sessions =================================================================
324//
325// A session is a FOLD over the server-recorded turns (grouped by the
326// session_id the server stamps on every /api/ai/chat turn), not a second
327// table -- q9's QuerySession kept messages as one growing JSON blob per
328// row and derived its title in three duplicated places; here the title
329// is simply the first input, computed in exactly one. The q9 behaviours
330// worth keeping ARE kept: resume the latest conversation automatically
331// when it is fresh (their 30-minute window), deep-link with ?session=,
332// mark the current one, and exactly ONE way to start a new session --
333// their own dropdown had two differently-wired "new" affordances, which
334// the port audit flagged as the thing not to copy.
335let sessions = [];
336const RESUME_WINDOW_MS = 30 * 60 * 1000;
337
338function foldSessions(rows) {
339 const by = {};
340 for (const row of rows) {
341 const sid = row.session_id || "";
342 if (!sid) continue; // pre-session history stays queryable, not replayed
343 (by[sid] = by[sid] || []).push(row);
344 }
345 return Object.entries(by).map(([id, rs]) => {
346 rs.sort((a, b) => String(a.created_at || "").localeCompare(String(b.created_at || "")));
347 const first = rs.find((r) => r.input) || rs[0];
348 return {id, rows: rs, count: rs.length,
349 title: String((first && first.input) || "(untitled)").slice(0, 40),
350 last: String(rs[rs.length - 1].created_at || "")};
351 }).sort((a, b) => b.last.localeCompare(a.last)).slice(0, 10);
352}
353
354function agoShort(iso) {
355 const ms = Date.now() - Date.parse(iso || 0);
356 if (!isFinite(ms) || ms < 0) return "";
357 const m = Math.round(ms / 60000);
358 if (m < 1) return "now";
359 if (m < 60) return m + "m";
360 const h = Math.round(m / 60);
361 if (h < 24) return h + "h";
362 return Math.round(h / 24) + "d";
363}
364
365function renderSessionPicker() {
366 const sel = document.getElementById("sessionpick");
367 if (!sel) return;
368 const opts = ['<option value="__new">\u2795 new session</option>'];
369 for (const s of sessions) {
370 const label = s.title + (s.last ? " \u2014 " + agoShort(s.last) : "");
371 opts.push('<option value="' + esc(s.id) + '"'
372 + (s.id === SESSION_ID ? " selected" : "") + ">"
373 + esc(label) + "</option>");
374 }
375 sel.innerHTML = opts.join("");
376 if (!sessions.some((s) => s.id === SESSION_ID)) sel.value = "__new";
377}
378
379function adoptSession(s) {
380 SESSION_ID = s.id;
381 aiHistory = [];
382 const log = document.getElementById("log");
383 if (log) log.innerHTML = "";
384 for (const row of s.rows.slice(-30)) {
385 const out = entry(row.input);
386 finish(out, row.output || "", {markdown: row.kind === "ai"});
387 if (row.kind === "ai" && row.output) {
388 aiHistory.push({role: "user", content: row.input});
389 aiHistory.push({role: "assistant", content: row.output});
390 }
391 }
392 renderSessionPicker();
393}
394
395async function loadHistory() {
396 const res = await fetch("/collections/shell_commands/records?limit=1000",
397 {credentials: "same-origin", headers: {accept: "application/json"}});
398 if (!res.ok) return;
399 const rows = (await res.json()).records || [];
400 sessions = foldSessions(rows);
401
402 const wanted = new URLSearchParams(location.search).get("session");
403 const deep = wanted && sessions.find((s) => s.id === wanted);
404 const latest = sessions[0];
405 const fresh = latest && (Date.now() - Date.parse(latest.last || 0) < RESUME_WINDOW_MS);
406 if (deep) adoptSession(deep);
407 else if (fresh) adoptSession(latest); // q9's lazy resume, same window
408 else renderSessionPicker();
409
410 const sel = document.getElementById("sessionpick");
411 if (sel) sel.addEventListener("change", () => {
412 if (sel.value === "__new") {
413 SESSION_ID = freshSessionId();
414 aiHistory = [];
415 const log = document.getElementById("log");
416 if (log) log.innerHTML = '<div class="entry"><div class="out">new session</div></div>';
417 history.replaceState(null, "", location.pathname);
418 renderSessionPicker();
419 } else {
420 const s = sessions.find((x) => x.id === sel.value);
421 if (s) { adoptSession(s); history.replaceState(null, "", "?session=" + s.id); }
422 }
423 });
424}
425
426async function savePrefs(changes) {
427 Object.assign(prefs, changes);
428 const [ok] = await api("PUT", `/collections/shell_preferences/records/${OWNER_ID}`, changes);
429 if (!ok) await api("POST", "/collections/shell_preferences/records", prefs);
430}
431
432async function run(input) {
433 const display = input.startsWith("/key ")
434 ? input.split(/\\s+/).slice(0, 2).join(" ") + " \\u2022\\u2022\\u2022\\u2022"
435 : input;
436 const out = entry(display);
437 const first = input[0];
438
439 if (first === "$" || first === ".") {
440 const isNote = first === "$";
441 const text = input.slice(1).trim();
442 const path = isNote ? "/collections/notes/records" : "/collections/tasks/records";
443 const payload = isNote
444 ? {id: crypto.randomUUID(), content: text, is_public: "false", owner_id: OWNER_ID}
445 : {id: crypto.randomUUID(), title: text, owner_id: OWNER_ID};
446 const [ok, body] = await api("POST", path, payload);
447 finish(out, ok ? (isNote ? "note saved" : "task created") : body.error, {err: !ok});
448 if (ok) record(input, isNote ? "note saved" : "task created", isNote ? "note" : "task");
449 return;
450 }
451
452 if (first === "^") {
453 const rest = input.slice(1).trim();
454 const [url, ...titleParts] = rest.split(/\\s+/);
455 const [ok, body] = await api("POST", "/collections/links/records",
456 {id: crypto.randomUUID(), url, title: titleParts.join(" ") || url, owner_id: OWNER_ID});
457 finish(out, ok ? "link saved" : body.error, {err: !ok});
458 if (ok) record(input, "link saved", "link");
459 return;
460 }
461
462 if (first === "~") {
463 const query = input.slice(1).trim();
464 const res = await fetch(`/api/search?q=${encodeURIComponent(query)}&limit=5`,
465 {credentials: "same-origin", headers: {accept: "application/json"}});
466 const body = await res.json();
467 const lines = [];
468 for (const [collection, hits] of Object.entries(body.results || {})) {
469 for (const hit of hits) {
470 const summary = Object.entries(hit).filter(([k]) => k !== "id")
471 .map(([, v]) => v).join(" \\u00b7 ").slice(0, 100);
472 lines.push(`${collection}/${hit.id} ${summary}`);
473 }
474 }
475 finish(out, lines.join("\\n") || "no matches");
476 record(input, lines.join("\\n") || "no matches", "search");
477 return;
478 }
479
480 if (first === "/") {
481 const [cmd, ...rest] = input.slice(1).split(/\\s+/);
482 if (cmd === "help") { finish(out, HELP); return; }
483 if (cmd === "key" && rest.length >= 2) {
484 const [service, ...keyParts] = rest;
485 const [ok, body] = await api("PUT", `/identity/users/${OWNER_ID}/service-keys`,
486 {service, key: keyParts.join("")});
487 finish(out, ok ? `${service} key stored (never logged, never readable back)` : body.error,
488 {err: !ok});
489 return;
490 }
491 if (cmd === "keys") {
492 const [ok, body] = await api("GET", `/identity/users/${OWNER_ID}/service-keys`);
493 const lines = ok ? (body.services || []).map((s) => `${s.service} set ${s.updated_at}`) : [];
494 finish(out, ok ? (lines.join("\\n") || "no keys stored; /key anthropic sk-...") : body.error,
495 {err: !ok});
496 return;
497 }
498 if (cmd === "model" && rest.length) {
499 await savePrefs({ai_model: rest[0]});
500 finish(out, `model set to ${rest[0]}`);
501 return;
502 }
503 if (cmd === "tools" && rest.length) {
504 await savePrefs({tools: rest.join("")});
505 finish(out, `tools set to ${prefs.tools}`);
506 return;
507 }
508 if (cmd === "voice") {
509 const arg = (rest[0] || "").toLowerCase();
510 const next = arg === "on" ? "true" : arg === "off" ? "false" : voiceOn() ? "false" : "true";
511 await savePrefs({voice_enabled: next});
512 if (next !== "true" && currentAudio) currentAudio.pause();
513 if (next !== "true" && window.speechSynthesis) window.speechSynthesis.cancel();
514 finish(out, `voice ${next === "true" ? "on" : "off"}`);
515 return;
516 }
517 if (cmd === "time") { finish(out, new Date().toString()); return; }
518 finish(out, "unknown command; /help", {err: true});
519 return;
520 }
521
522 const tools = prefs.tools.split(",").map((t) => t.trim()).filter(Boolean);
523 // Attachments ride THIS turn only. History keeps a text marker instead of
524 // re-sending the bytes -- images are big, and the model's own reply
525 // already carries what it saw in them.
526 const attachIds = pendingAttach.map((f) => f.id);
527 const attachNote = pendingAttach.length
528 ? " [attached: " + pendingAttach.map((f) => f.name).join(", ") + "]" : "";
529 const [ok, body] = await api("POST", "/api/ai/chat",
530 {message: input, model: prefs.ai_model, tools, history: aiHistory.slice(-20),
531 attachments: attachIds.length ? attachIds : undefined,
532 session_id: SESSION_ID, source: "shell",
533 system: "You are the shell of this user's object server. Answer in plain terminal text " +
534 "with no markdown formatting. Be concise. Use your tools when the question is " +
535 "about the user's records. " +
536 "You can also MATERIALIZE PAGES: the views collection turns records into live " +
537 "pages. When the user asks for a page/dashboard/view (or an answer clearly worth " +
538 "keeping as one), create a views record: fields title, layout 'single', " +
539 "owner_id (the user), pinned 'false', is_public 'false', and blocks = a JSON " +
540 "string of a list of block objects. Block kinds: " +
541 "{kind:'count', collection, filters:{field:value}, label, warn_over?} | " +
542 "{kind:'list', collection, filters?, sort?:'newest'|'oldest', title?} | " +
543 "{kind:'form', collection, record_id?} | " +
544 "{kind:'detail', collection, record_id} | " +
545 "{kind:'markdown', text}. " +
546 "After creating it, tell the user the page is at /views/{id} (the record id). " +
547 "Prefer a count block above a list block for status-style pages. " +
548 "You do NOT know from memory which collections exist. Call list_collections " +
549 "and read its result before answering; if the collection the user named " +
550 "appears in that result, USE it -- never say it is missing when it is in " +
551 "the list. The user's tasks live in the collection named tasks. " +
552 "To show one specific record on screen, create a view whose blocks contain a " +
553 "detail block for it. Never claim something is on screen unless you created " +
554 "or updated a views record in this same turn. " +
555 "Whenever the screen should show a view -- newly created OR one that already " +
556 "exists -- end your reply with the marker [[view:<record id>]] alone on the " +
557 "last line. The marker is machine-read; it is never displayed or spoken, so " +
558 "it does not violate the no-ids-aloud rule. " +
559 "Example reply: \\"Here are your open tasks. " +
560 "[[view:26b247ed-3b1a-4206-b060-1d92847194de]]\\"" +
561 " You can also READ WEB PAGES with the read_page tool when the user gives a " +
562 "URL or asks you to read/summarize a page: it returns the page text and its " +
563 "links numbered in order. Offer the links as \\"link 1, link 2, ...\\" so the " +
564 "user can say \\"open link N\\" and you read_page that link's url next." +
565 " Current local date/time: " + new Date().toString() + "."});
566 finish(out, ok ? body.reply : body.error,
567 {err: !ok, tools: ok ? body.tool_calls : null, markdown: ok});
568 if (ok && pendingAttach.length) { pendingAttach = []; renderAttachBar(); }
569 if (ok) {
570 aiHistory.push({role: "user", content: input + attachNote});
571 aiHistory.push({role: "assistant", content: body.reply});
572 // Only the final assistant text is ever spoken -- never tool-call noise.
573 if (voiceOn()) speak(body.reply);
574 }
575 // AI turns are recorded by the server inside /api/ai/chat -- stamped
576 // and session-grouped. Command turns (note/link/search) stay client-
577 // written below: they never touch the chat endpoint.
578}
579
580document.getElementById("prompt").addEventListener("submit", (event) => {
581 event.preventDefault();
582 const box = event.target.elements["line"];
583 const input = box.value.trim();
584 if (!input) return;
585 box.value = "";
586 run(input);
587});
588initMic();
589loadPrefs();
590loadHistory();
591</script>
592
593// === drag a file in, talk about it ==========================================
594//
595// Drop anywhere on the page. Each file is uploaded to the EXISTING
596// /api/files surface (owner-stamped server-side, the same endpoint the
597// record-attachments capability uses -- no parent, so it is simply the
598// user's file), and its id rides the NEXT AI message as `attachments`.
599// The server turns ids into provider content: images become vision input,
600// text files are inlined. Binaries the model cannot see are refused by
601// name at send time rather than arriving as mojibake.
602let pendingAttach = [];
603function renderAttachBar() {
604 const bar = document.getElementById("attachbar");
605 if (!bar) return;
606 bar.hidden = !pendingAttach.length;
607 bar.innerHTML = pendingAttach.map((f, i) =>
608 '<span class="chip">[file] ' + esc(f.name)
609 + '<button type="button" data-detach="' + i + '" aria-label="remove">x</button></span>'
610 ).join("");
611}
612document.addEventListener("click", (e) => {
613 const btn = e.target.closest("[data-detach]");
614 if (!btn) return;
615 pendingAttach.splice(Number(btn.getAttribute("data-detach")), 1);
616 renderAttachBar();
617});
618function attachNoteLine(text) {
619 const log = document.getElementById("log");
620 if (!log) return;
621 const div = document.createElement("div");
622 div.className = "entry";
623 div.innerHTML = '<div class="out">' + esc(text) + "</div>";
624 log.appendChild(div); log.scrollTop = log.scrollHeight;
625}
626async function uploadDropped(file) {
627 const data = new FormData();
628 data.append("file", file);
629 const res = await fetch("/api/files", {method: "POST", credentials: "same-origin", body: data});
630 let resp = null; try { resp = await res.json(); } catch (x) {}
631 if (!res.ok || !resp || !resp.file) {
632 attachNoteLine("could not attach " + file.name + ": " + ((resp && resp.error) || res.status));
633 return;
634 }
635 pendingAttach.push({id: resp.file.id, name: resp.file.filename || file.name});
636 renderAttachBar();
637 attachNoteLine("attached: " + (resp.file.filename || file.name)
638 + " -- it rides your next message");
639}
640let dragDepth = 0;
641document.addEventListener("dragenter", (e) => {
642 const types = (e.dataTransfer && e.dataTransfer.types) || [];
643 if (![].some.call(types, (t) => t === "Files")) return;
644 e.preventDefault(); dragDepth++; document.body.classList.add("dragover");
645});
646document.addEventListener("dragover", (e) => { e.preventDefault(); });
647document.addEventListener("dragleave", () => {
648 if (--dragDepth <= 0) { dragDepth = 0; document.body.classList.remove("dragover"); }
649});
650document.addEventListener("drop", (e) => {
651 e.preventDefault(); dragDepth = 0; document.body.classList.remove("dragover");
652 if (typeof OWNER_ID === "undefined" || !OWNER_ID) {
653 attachNoteLine("sign in to attach files"); return;
654 }
655 const files = (e.dataTransfer && e.dataTransfer.files) || [];
656 for (const file of files) uploadDropped(file);
657});
658
659"""
660
661
662def GET(request):
663 identity = request.get("_identity", {})
664 user_id = identity.get("user_id")
665 _logger.info("site_shell served", user_id=user_id or "anonymous")
666
667 if not user_id:
668 body = '<p class="hint"><a href="/login?next=/shell">Sign in</a> to use the shell.</p>'
669 script = ""
670 else:
671 body = """
672<div id="log"><div class="entry"><div class="out">type /help for commands, or just talk</div></div></div>
673<div id="attachbar" hidden></div>
674<div id="sessbar"><select id="sessionpick" aria-label="conversation">
675<option value="__new">&#10133; new session</option></select></div>
676<form id="prompt" autocomplete="off">
677<input name="line" placeholder="&gt;_" autofocus>
678<button type="submit" class="btn primary" aria-label="send">send</button>
679<button type="button" id="mic" hidden aria-label="voice input">mic</button>
680</form>
681"""
682 script = (
683 f"<script>const OWNER_ID = {user_id!r}; const HELP = \"{_HELP}\";"
684 f"{_SCRIPT}"
685 )
686
687 who = (
688 f"signed in as <strong>{user_id}</strong>"
689 if user_id
690 else '<a href="/login?next=/shell">sign in</a>'
691 )
692 html = f"""<!doctype html>
693<html lang="en">
694<head>
695<meta charset="utf-8">
696<meta name="viewport" content="width=device-width, initial-scale=1">
697<title>Shell</title>
698<link rel="stylesheet" href="/style">
699<style>{_STYLE}</style>
700</head>
701<body>
702<div class="wrap">
703<header class="app"><h1>Shell</h1><div class="who">{who}</div></header>
704{body}
705</div>
706<script src="/markdown"></script>
707{script}
708<script src="/nav"></script>
709</body>
710</html>"""
711 return {"content_type": "text/html; charset=utf-8", "body": html}