shadok-ai 0.20.34 → 0.20.36

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 CHANGED
@@ -355,7 +355,9 @@ Enter to skip; you can add it later from the web UI).
355
355
  browser's maker. To keep it on your server, an admin picks version menu →
356
356
  *Web dictation* → *On this server (whisper)*: the clip is transcribed by the
357
357
  local model, after a wait. Other browsers and Telegram/WhatsApp voice notes
358
- always use the local model.
358
+ always use the local model, and so does a browser whose recogniser fails
359
+ (a network that cannot reach its speech service, say): the same click
360
+ switches to recording for the server, and says so.
359
361
  - **Dictate a prompt on the web** — a 🎙 button beside *Send* records your
360
362
  microphone and transcribes it with the **same local whisper.cpp** the voice
361
363
  notes use (nothing leaves the machine). The text drops into the composer
@@ -428,6 +430,12 @@ Enter to skip; you can add it later from the web UI).
428
430
  never a message. Older `#agent=<session>&msg=<id>` links still open. A message
429
431
  older than the history shown, or a conversation that was closed, says so
430
432
  instead of opening somewhere else.
433
+ - **Who wrote each message** — every bubble opens with a round chip instead of
434
+ a written name: the person's initials on their own colour (stable across
435
+ devices, the palette the agent tabs use), the Shadok for the agent and for the
436
+ cockpit's own messages. Hover it for the full name ("Ada · telegram"). Your
437
+ own chip uses your account's name, or a plain silhouette in a cockpit with no
438
+ accounts.
431
439
  - **Run the command an agent asks you to** — when an agent writes `! cmd` for
432
440
  you to run (typically because it cannot), a ▶ run button sits right on that
433
441
  command's code block, beside its copy button — so it runs where it was
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "shadok-ai",
3
- "version": "0.20.34",
3
+ "version": "0.20.36",
4
4
  "main": "dist/session.js",
5
5
  "scripts": {
6
6
  "test": "node --import tsx --test --test-timeout=120000 test/*.ts context/*/test/*.test.mjs",
@@ -0,0 +1,73 @@
1
+ // Who a chat bubble is from, drawn as a round chip instead of a written name:
2
+ // initials for a person, the Shadok for the agent (and for the server's own
3
+ // messages). A pure decision, hence testable without a browser — loaded as is
4
+ // by the page (ESM) and imported by the tests, like echo-author.js.
5
+ //
6
+ // The full name is NOT lost: the page puts it in the chip's tooltip and
7
+ // aria-label. A photo can later replace the initials without changing callers:
8
+ // `avatarFor` is the one place that decides what a speaker looks like.
9
+
10
+ /** Labels that name a machine, not a person: drawn as the Shadok. */
11
+ const MACHINE = new Set(["claude", "shadok-ai", "auto-retry", "cron", "agent"]);
12
+
13
+ /**
14
+ * The person part of a label: `echoAuthor` writes "Name · telegram" — the
15
+ * platform after the dot is not part of who spoke.
16
+ * @param {string} who
17
+ */
18
+ export function speakerName(who) {
19
+ return String(who ?? "").split(" · ")[0].trim();
20
+ }
21
+
22
+ /**
23
+ * Up to two initials: first letter of the first and of the last word
24
+ * ("Alexandre Cognard" → "AC", "ada" → "A"). Letters only, so an email-like
25
+ * name ("ana@example.org") still reads "A", and digits/emoji never fill a chip.
26
+ * @param {string} name
27
+ */
28
+ export function initials(name) {
29
+ const words = String(name ?? "")
30
+ .split(/[\s@._-]+/)
31
+ .map((w) => w.match(/\p{L}/u)?.[0] ?? "")
32
+ .filter(Boolean);
33
+ if (!words.length) return "?";
34
+ const pick = name && /@/.test(name) ? [words[0]] : words.length > 1 ? [words[0], words[words.length - 1]] : [words[0]];
35
+ return pick.join("").toUpperCase();
36
+ }
37
+
38
+ /** The palette the agent tabs already use (index.html `AVATAR_COLORS`), so a
39
+ * person's chip and an agent's tab share one visual language. */
40
+ export const AVATAR_COLORS = ["#5b5fc7", "#0078d4", "#038387", "#498205", "#c4314b", "#ca5010", "#8764b8", "#986f0b", "#00727c", "#b146c2"];
41
+
42
+ /**
43
+ * A stable colour per name, so the same person keeps it on every device and
44
+ * every reload — never a random draw, never the list position. Same hash as
45
+ * the tabs' `paintTabAvatar`.
46
+ * @param {string} name
47
+ */
48
+ export function colorFor(name) {
49
+ let h = 0;
50
+ for (const ch of String(name ?? "")) h = (h * 31 + ch.codePointAt(0)) >>> 0;
51
+ return AVATAR_COLORS[h % AVATAR_COLORS.length];
52
+ }
53
+
54
+ /**
55
+ * @param {string} who the label the bubble would have carried ("claude",
56
+ * "you", "Ada · telegram", "you · shell"…)
57
+ * @param {{ role?: string, me?: string | null }} [ctx] `role` = the turn's role
58
+ * ("claude" for the agent's side); `me` = the signed-in account's name, which
59
+ * is what "you" stands for.
60
+ * @returns {{ kind: "shadok" } | { kind: "initials", text: string, color: string }
61
+ * | { kind: "you" }}
62
+ */
63
+ export function avatarFor(who, ctx = {}) {
64
+ const name = speakerName(who);
65
+ if (ctx.role === "claude" || MACHINE.has(name.toLowerCase())) return { kind: "shadok" };
66
+ if (name === "you" || name === "") {
67
+ const me = String(ctx.me ?? "").trim();
68
+ // No account (a cockpit with no password) or `/me` still in flight: a
69
+ // neutral silhouette rather than a made-up "Y".
70
+ return me ? { kind: "initials", text: initials(me), color: colorFor(me) } : { kind: "you" };
71
+ }
72
+ return { kind: "initials", text: initials(name), color: colorFor(name) };
73
+ }
package/public/index.html CHANGED
@@ -151,6 +151,22 @@
151
151
  margin: 0 5px;
152
152
  opacity: 0.7;
153
153
  }
154
+ /* Who spoke, as a round chip in place of the written name (public/avatar.js):
155
+ initials on the person's colour, the Shadok for the agent. The name lives in
156
+ the chip's tooltip. The time follows the chip with no "·" — there is no
157
+ name for it to separate from. */
158
+ .speaker {
159
+ display: inline-grid; place-items: center; vertical-align: middle;
160
+ width: 20px; height: 20px; border-radius: 50%; flex: none;
161
+ font-size: 9px; font-weight: 700; letter-spacing: .02em; line-height: 1;
162
+ color: #fff; background: var(--ac, var(--text-dim)); cursor: default;
163
+ }
164
+ .speaker.shadok { background: color-mix(in srgb, var(--amber) 18%, transparent); color: var(--amber); }
165
+ .speaker svg { width: 16px; height: 16px; }
166
+ .speaker.you svg { width: 12px; height: 12px; }
167
+ .label .speaker + .time::before { content: none; }
168
+ .label .speaker + .time { margin-left: 6px; }
169
+ .label .speaker + .what { margin-left: 6px; }
154
170
 
155
171
  /* ── Instrument bar ──────────────────────────────────── */
156
172
  header {
@@ -4896,6 +4912,7 @@
4896
4912
  import { notifyState, BLINK_MS } from "/notify.js?v=__ASSET_V__";
4897
4913
  import { shrinkToFit, nameReads, chanOverflowIds, chanOverflowItems } from "/bar-overflow.js?v=__ASSET_V__";
4898
4914
  import { echoAuthor, isOwnPrompt } from "/echo-author.js?v=__ASSET_V__";
4915
+ import { avatarFor } from "/avatar.js?v=__ASSET_V__";
4899
4916
  import { dialPos, dialColor, dialTitle } from "/gauge-dial.js?v=__ASSET_V__";
4900
4917
  import { TOUR_DEMO, TOUR_STEPS, bubblePlacement, tourNeedsDemo, tourStanding, unionRect, visibleSteps } from "/tour-steps.js?v=__ASSET_V__";
4901
4918
  import { pickChannelSource, dirKey, serverSwapped } from "/channel-store.js?v=__ASSET_V__";
@@ -5025,6 +5042,7 @@
5025
5042
  window.hasReadonlyPreset = hasReadonlyPreset;
5026
5043
  window.applyReadonlyPreset = applyReadonlyPreset;
5027
5044
  window.echoAuthor = echoAuthor;
5045
+ window.avatarFor = avatarFor;
5028
5046
  window.isOwnPrompt = isOwnPrompt;
5029
5047
  window.notifyState = notifyState;
5030
5048
  window.BLINK_MS = BLINK_MS;
@@ -5208,6 +5226,9 @@
5208
5226
  extractLiveText: () => "",
5209
5227
  livePreviewDecision: () => null,
5210
5228
  echoAuthor: () => "",
5229
+ // Mirrors the agent case so its chip is right from the first paint; every
5230
+ // chip is repainted from the real module on `shadok-bridge-ready`.
5231
+ avatarFor: (who, ctx) => ({ kind: (ctx && ctx.role === "claude") || who === "claude" ? "shadok" : "you" }),
5211
5232
  isOwnPrompt: () => true,
5212
5233
  notifyState: () => ({ color: null, badge: "", blink: false }),
5213
5234
  dialPos: () => 0,
@@ -9399,6 +9420,57 @@
9399
9420
  * the left — still a tinted human bubble, only the agent is white. The author
9400
9421
  * is kept on the turn so the side can be re-decided once `/me` answers
9401
9422
  * (`resideAllPrompts`). */
9423
+ // ── Speaker chips (who wrote a bubble) ──
9424
+ // Static markup only (no user data), so innerHTML is safe here. The Shadok is
9425
+ // the header's brand mark without its animation hooks.
9426
+ const SPEAKER_SHADOK_SVG = '<svg viewBox="0 0 32 32" aria-hidden="true"><g fill="currentColor" stroke="currentColor" stroke-width="1.3" stroke-linecap="round"><line x1="13" y1="23" x2="12.6" y2="30.6"/><line x1="11" y1="30.7" x2="14.1" y2="30.7"/><line x1="18.6" y1="23" x2="19" y2="30.6"/><line x1="17.6" y1="30.7" x2="20.7" y2="30.7"/><line x1="21.6" y1="17.2" x2="26" y2="20.4"/><g stroke-width="0.9"><line x1="13.8" y1="5.4" x2="11.8" y2="2.8"/><circle cx="11.8" cy="2.8" r="1" stroke="none"/><line x1="18.2" y1="5.4" x2="20.2" y2="2.8"/><circle cx="20.2" cy="2.8" r="1" stroke="none"/></g><circle cx="16" cy="16.2" r="8.4" stroke="none"/><g stroke="none"><circle cx="13.1" cy="7.8" r="2.6" fill="#fff"/><circle cx="18.9" cy="7.8" r="2.6" fill="#fff"/><circle cx="13.4" cy="8.1" r="1.3"/><circle cx="19.2" cy="8.1" r="1.3"/></g></g></svg>';
9427
+ const SPEAKER_YOU_SVG = '<svg viewBox="0 0 24 24" aria-hidden="true" fill="none" stroke="currentColor" stroke-width="2.2" stroke-linecap="round"><circle cx="12" cy="8" r="4"/><path d="M4 21c1.5-4 4.5-6 8-6s6.5 2 8 6"/></svg>';
9428
+ /** The signed-in name, read defensively: `me` is declared further down the
9429
+ * script, and a chip may be painted before that line has run. */
9430
+ function myName() {
9431
+ try { return me?.name ?? null; } catch { return null; }
9432
+ }
9433
+ /** (Re)paint one chip from its `data-who` / `data-role`. */
9434
+ function paintSpeaker(chip) {
9435
+ const a = window.avatarFor(chip.dataset.who, { role: chip.dataset.role, me: myName() });
9436
+ chip.className = "speaker" + (a.kind === "initials" ? "" : " " + a.kind);
9437
+ if (a.kind === "initials") {
9438
+ chip.textContent = a.text;
9439
+ chip.style.setProperty("--ac", a.color);
9440
+ } else {
9441
+ chip.style.removeProperty("--ac");
9442
+ chip.innerHTML = a.kind === "shadok" ? SPEAKER_SHADOK_SVG : SPEAKER_YOU_SVG;
9443
+ }
9444
+ }
9445
+ /**
9446
+ * Fill a bubble's label with WHO spoke: a chip in place of the written name,
9447
+ * the full name in its tooltip, plus an optional `what` ("choice requested",
9448
+ * a draft's title) for the labels that also say what the bubble is. The name
9449
+ * stays on `label.dataset.who` — what grouping by speaker compares.
9450
+ */
9451
+ function fillSpeakerLabel(label, who, role, what) {
9452
+ label.dataset.who = who;
9453
+ const chip = document.createElement("span");
9454
+ chip.dataset.who = who;
9455
+ chip.dataset.role = role === "claude" ? "claude" : "";
9456
+ chip.title = who;
9457
+ chip.setAttribute("role", "img");
9458
+ chip.setAttribute("aria-label", who);
9459
+ paintSpeaker(chip);
9460
+ label.appendChild(chip);
9461
+ if (what) {
9462
+ const w = document.createElement("span");
9463
+ w.className = "what";
9464
+ w.textContent = what;
9465
+ label.appendChild(w);
9466
+ }
9467
+ }
9468
+ /** Repaint every chip: the real module landed, or `/me` told us who "you" is. */
9469
+ function repaintSpeakers() {
9470
+ for (const chip of document.querySelectorAll(".label > .speaker[data-who]")) paintSpeaker(chip);
9471
+ }
9472
+ window.addEventListener("shadok-bridge-ready", repaintSpeakers);
9473
+
9402
9474
  function markPromptSide(bubble, msg) {
9403
9475
  const turn = bubble.parentElement;
9404
9476
  turn.dataset.from = msg?.from ?? "";
@@ -9406,8 +9478,8 @@
9406
9478
  if (msg?.auto) turn.dataset.auto = "1";
9407
9479
  // A run of prompts shows one label — but only while it is ONE speaker.
9408
9480
  const prev = turn.previousElementSibling;
9409
- const who = turn.querySelector(":scope > .label")?.firstChild?.textContent ?? "";
9410
- const prevWho = prev?.classList.contains("user") ? prev.querySelector(":scope > .label")?.firstChild?.textContent ?? "" : who;
9481
+ const who = turn.querySelector(":scope > .label")?.dataset.who ?? "";
9482
+ const prevWho = prev?.classList.contains("user") ? prev.querySelector(":scope > .label")?.dataset.who ?? "" : who;
9411
9483
  turn.classList.toggle("new-speaker", who !== prevWho);
9412
9484
  applyPromptSide(turn);
9413
9485
  }
@@ -9434,7 +9506,7 @@
9434
9506
  if (role !== "system") {
9435
9507
  const label = document.createElement("span");
9436
9508
  label.className = "label";
9437
- label.textContent = who;
9509
+ fillSpeakerLabel(label, who, role);
9438
9510
  // The time lives INSIDE the label: a run of bubbles from the same speaker
9439
9511
  // has only one label (CSS rule `.turn.claude + .turn.claude > .label`), so
9440
9512
  // the time groups the same way instead of repeating on every block.
@@ -9729,7 +9801,7 @@
9729
9801
  turn.className = "turn user";
9730
9802
  const label = document.createElement("span");
9731
9803
  label.className = "label";
9732
- label.textContent = "you · shell";
9804
+ fillSpeakerLabel(label, "you", "user", "shell");
9733
9805
  const time = fmtTime(at);
9734
9806
  if (time) label.appendChild(time);
9735
9807
  const bubble = document.createElement("div");
@@ -9765,7 +9837,7 @@
9765
9837
  turn.className = "turn claude";
9766
9838
  const label = document.createElement("span");
9767
9839
  label.className = "label";
9768
- label.textContent = "claude";
9840
+ fillSpeakerLabel(label, "claude", "claude");
9769
9841
  const time = fmtTime(at);
9770
9842
  if (time) label.appendChild(time);
9771
9843
  turn.appendChild(label);
@@ -9845,7 +9917,7 @@
9845
9917
  turn.textContent = "";
9846
9918
  const label = document.createElement("span");
9847
9919
  label.className = "label";
9848
- label.textContent = "claude — " + m.title;
9920
+ fillSpeakerLabel(label, "claude", "claude", m.title);
9849
9921
  const time = fmtTime(Number.isFinite(at) ? at : draft.createdAt);
9850
9922
  if (time) label.appendChild(time);
9851
9923
  const bubble = document.createElement("div");
@@ -10295,9 +10367,9 @@
10295
10367
  turn.className = "turn claude dialog";
10296
10368
  const label = document.createElement("span");
10297
10369
  label.className = "label";
10298
- label.textContent = msg.multi
10299
- ? "claude — multi-select (check then submit)"
10300
- : "claude — choice requested";
10370
+ fillSpeakerLabel(label, "claude", "claude", msg.multi
10371
+ ? "multi-select (check then submit)"
10372
+ : "choice requested");
10301
10373
  // A question asked while you were away: HOW LONG it has been waiting is
10302
10374
  // exactly what you want to see on coming back.
10303
10375
  const time = fmtTime(Date.now());
@@ -12354,7 +12426,7 @@
12354
12426
  turn.className = "turn claude dialog";
12355
12427
  const label = document.createElement("span");
12356
12428
  label.className = "label";
12357
- label.textContent = "shadok-ai — pace exceeded";
12429
+ fillSpeakerLabel(label, "shadok-ai", "claude", "pace exceeded");
12358
12430
  const bubble = document.createElement("div");
12359
12431
  bubble.className = "bubble";
12360
12432
  const q = document.createElement("div");
@@ -13010,7 +13082,7 @@
13010
13082
  const cur = promptInput.value;
13011
13083
  const at = promptInput.selectionStart ?? cur.length;
13012
13084
  const before = cur.slice(0, at), after = cur.slice(promptInput.selectionEnd ?? at);
13013
- let heard = false;
13085
+ let heard = false, lastError = null;
13014
13086
  rec.onresult = (e) => {
13015
13087
  if (liveRec !== rec) return;
13016
13088
  const t = ld.liveTranscript(Array.from(e.results, (r) => ({ final: r.isFinal, text: r[0] ? r[0].transcript : "" })));
@@ -13023,18 +13095,31 @@
13023
13095
  autosizePrompt();
13024
13096
  updateSendDisabled();
13025
13097
  };
13098
+ // The verdict waits for `end`, which always follows an `error`: that is
13099
+ // where a recogniser that failed WITHOUT naming an error shows up too.
13026
13100
  rec.onerror = (e) => {
13027
13101
  if (liveRec !== rec) return;
13028
- const f = ld.recognitionFailure(e.error);
13029
- // A recogniser that refuses here will refuse again: the next click records
13030
- // for the server instead.
13031
- if (f.fallback) liveBroken = true;
13032
- if (f.message && !(e.error === "no-speech" && heard)) micMsg(f.message);
13102
+ lastError = e.error || "unknown";
13103
+ // Kept in the console: the one trace there is when a browser refuses.
13104
+ console.warn("live dictation: recogniser error", lastError);
13033
13105
  };
13034
13106
  rec.onend = () => {
13035
13107
  if (liveRec !== rec) return;
13108
+ const f = ld.recognitionEnd({ requested: rec.shadokStopRequested === true, heard, error: lastError });
13036
13109
  liveRec = null;
13037
13110
  endRecordingUI();
13111
+ if (f.fallback) {
13112
+ // A recogniser that refuses here will refuse again: record for the
13113
+ // server now, in the same click, rather than leave the person to
13114
+ // guess that a second one would work.
13115
+ liveBroken = true;
13116
+ if (!lastError) console.warn("live dictation: recogniser ended with no result and no error");
13117
+ // Said only once the recording runs: a refused microphone keeps
13118
+ // startRecording's own message.
13119
+ startRecording().then(() => { if (micRec) micMsg(f.message); });
13120
+ return;
13121
+ }
13122
+ if (f.message && !(lastError === "no-speech" && heard)) micMsg(f.message);
13038
13123
  promptInput.focus();
13039
13124
  };
13040
13125
  try { rec.start(); } catch { liveBroken = true; return startRecording(); }
@@ -13060,6 +13145,9 @@
13060
13145
  endRecordingUI();
13061
13146
  try { rec.abort(); } catch { /* already over */ }
13062
13147
  } else {
13148
+ // Asked to finish: an end with nothing heard is then the person's own
13149
+ // stop, not a recogniser that gave up (`recognitionEnd`).
13150
+ rec.shadokStopRequested = true;
13063
13151
  try { rec.stop(); } catch { /* already over: onend clears the button */ }
13064
13152
  }
13065
13153
  }
@@ -17026,6 +17114,7 @@
17026
17114
  }
17027
17115
  async function loadMe() {
17028
17116
  try { me = await (await fetch("/me")).json(); } catch { me = null; }
17117
+ repaintSpeakers();
17029
17118
  const b = $("usersBtn");
17030
17119
  // Every signed-in PERSON opens it — a member manages their own passkeys
17031
17120
  // there; only an admin sees the accounts below.
@@ -62,8 +62,9 @@ export function spliceDictation(before, spoken, after) {
62
62
 
63
63
  /**
64
64
  * What a recognition error means for the person. `message` is shown (null: say
65
- * nothing); `fallback` means the browser cannot do this at all here — the next
66
- * click should record for the server instead of failing the same way again.
65
+ * nothing); `fallback` means the browser cannot do this at all here — the page
66
+ * records for the server at once, and every later click does too, instead of
67
+ * failing the same way again.
67
68
  */
68
69
  export function recognitionFailure(error) {
69
70
  switch (error) {
@@ -74,6 +75,23 @@ export function recognitionFailure(error) {
74
75
  default:
75
76
  // network, service-not-allowed, language-not-supported, or a name this
76
77
  // list does not know: the recogniser is not usable in this browser.
77
- return { message: "Browser dictation is unavailable here — click again to use the server's", fallback: true };
78
+ return { message: FALLBACK_MESSAGE, fallback: true };
78
79
  }
79
80
  }
81
+
82
+ const FALLBACK_MESSAGE = "Browser dictation is unavailable here — recording for the server instead, speak again";
83
+
84
+ /**
85
+ * What a recogniser that ENDED means, once its error (if any) is known. Most
86
+ * failures name themselves through `error`; this adds the one that does not.
87
+ * A recogniser that stops by itself having produced nothing and reported no
88
+ * error is just as unusable as one saying `network` — nothing obliges a browser
89
+ * to name its failure — and, read as "nothing to report", it left the button
90
+ * blinking back to idle with not a word said: the shape a person reports as
91
+ * "it does not work".
92
+ */
93
+ export function recognitionEnd({ requested, heard, error }) {
94
+ if (error) return recognitionFailure(error);
95
+ if (requested || heard) return { message: null, fallback: false };
96
+ return { message: FALLBACK_MESSAGE, fallback: true };
97
+ }