mirror of
https://github.com/bitbaum/loki.git
synced 2026-12-04 00:37:00 +01:00
* fix(prompts): one card per saved prompt, schedules in words, dialogs that say why
What the owner found on /prompts from an Android phone, item by item:
1. Saved prompts: saving the same name + words again no longer inserts a
row — POST /api/prompts answers with the prompt already saved
(existing: true); the form opens it, the default's save button says
"Already in your prompts". A card that stands for identical copies says
how many and offers "Remove N extra copies" — one explicit tap, the
caller's own rows only, the same soft delete as Delete
(DELETE /api/prompts/<id>/copies). Same name with different words says
"2 versions with this name, worded differently".
2. Filters: the scope filter is a <Segmented> (All / Any project / One
project) sitting with the category row on the list they both filter,
under "Loki's prompts". Category counts follow the scope. Filter chip
rows on a phone get a wider fade, snap to a chip's start and leave room
after the last chip, so the edge reads as "more" rather than a cut word.
3. New prompt: Create stays disabled until there is a name and a prompt,
and says what is missing beside it. Opening or closing the form never
saved anything; the copy glyph on every default card did, in one tap
with no label on a phone — it is now a labelled "save a copy" button.
4. Schedule: pick Every day / Weekdays / Every week / Every month, a day
and a time; Loki writes the cron (lib/cron-words), with "Write it as a
cron line instead" for experts. The schedule reads back in words with
the time zone it runs in ("Every Monday at 10:00, Zurich time
(Europe/Zurich)") and a link to change it; the zone is sent with the
job. The API refuses a cron line the scheduler cannot read, in words.
Create is the primary button and says why when it cannot be pressed.
5. Run: a prompt that names {{project_name}} asks for a project whatever
its scope (a saved copy is global), defaulting to the project in use;
the prompt is shown only once complete. Each action says in one line
who does the work; Send to terminal works for any of your projects
(tab-inject starts or queues the session) and says why when it cannot.
Project pickers list projectTitle(), not folder keys.
6. Category chips are neutral (ui-tag-neutral); the ui-cat-* rainbow and
the featured star's amber are gone. "control" was a second id for the
fleet prompts — one category now, "Fleet control".
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NZqZujmXpupVDC1Hi5Jbsx
* fix(voice): spoken sentences start with a capital and name a repeated project once
The briefing read "one run is waiting for a builder: loki ... three runs
failed: loki, loki and loki". Sentences now open with a capital, and the
runs' projects are said once each: "Three runs failed on loki.", "Four runs
failed: loki three times and heidi once." The same for runs waiting for a
builder, agents working, and the announcements between turns.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NZqZujmXpupVDC1Hi5Jbsx
* fix(voice): say why the studio voice is off, with the link that fixes it
POST /api/voice/speak answered 502 with Groq's reason only in the server
console, and the phone fell back to its own voice in silence, so every voice
in the picker sounded the same. Groq's refusal is now classified
(lib/voice/tts-failure: terms gate, bad key, limit, model gone, vendor down,
no key) into one plain sentence and, where one exists, the link that fixes
it — for the Orpheus terms gate, Groq's voice page and "press Accept". The
route returns it in the 502/503 body and records it in debug_logs (once per
reason per half hour) so System shows it. The speaker reports the fallback
to the page; useStudioVoiceStatus keeps it for the tab's session and
StudioVoiceNotice shows it. The phone-voice fallback stays.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NZqZujmXpupVDC1Hi5Jbsx
* feat(voice): change Loki's voice by saying so, and answer questions about voice mode truthfully
"I don't like your voice, can I change your voice" was answered like a
generic chatbot ("in most voice-enabled AI chat apps... look in settings").
Now "use Troy", "change your voice to Diana", "other voice" and
"switch voice" are a spoken command (config/no-screen.ts, lib/voice/commands)
that the phone answers itself; with no name it lists the voices. A question
that does reach Loki carries voiceModeContext (lib/voice/self.ts), built from
the SSOT: the voices by name, how to change one, the three switches with the
same one-line notes the screen shows (NO_SCREEN_OPTIONS), every command, the
current settings and why studio voices are off, if they are.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NZqZujmXpupVDC1Hi5Jbsx
* fix(voice): a music bed a phone can actually play, and keep its sound awake
The bed sat at about -38 dBFS RMS alone and -50 under the voice, every note
98-370 Hz behind a 900 Hz low-pass: below what a phone speaker or many
earbuds reproduce, so "music bed on" was silence. It is now an octave
higher (196-740 Hz), about 12 dB louder, ducks by 10 dB instead of 11 more on
top, and earcons sit above it. The AudioContext is resumed inside the Start
tap, on every tap on the circle or headphone button, on return to the page,
and when the phone suspends it mid-session.
useSoundscape also returned a new object every render, so effects keyed on
it re-ran ~20 times a second while listening: the gain was re-ramped every
frame and the 20-second fleet poll was cleared before it could fire. It is
memoized now.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NZqZujmXpupVDC1Hi5Jbsx
* fix(voice): one h1 per screen, End clear of the feedback launcher, voices as an even grid
- Speaking screen: an h1 below md, where the app top bar steps aside; the
start screen's big "Voice" is no longer a second h1 beside the top bar's.
- The speaking screen sets data-fc-place="tab", so the feedback launcher
and its Watching pill become the slim edge handle instead of sitting on
End; End is larger, with room below it, and the stage scrolls rather than
pushing End off a short screen.
- The voice picker is a <Segmented> grid of four columns (seven voices as
4 + 3, the last two cells wide) instead of a chip row cut off at the edge;
the setting groups have real headings.
- The unclear note under the switches is one short line under each switch,
shared with what Loki is told about itself.
- The page wires the studio-voice notice (under the picker and under the
circle, said aloud once), the spoken voice change, the page's state sent
with each question, and the sound resume on every tap.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NZqZujmXpupVDC1Hi5Jbsx
* fix(widget): one card per finding, settled page checks, honest fix status, a count that means what needs you
Watching on Loki's own /voice from an Android phone showed six problems in
one panel. Each is fixed where it starts:
- Notices no longer stack. A page's checks own ONE card (checksSlot), which
each new reading replaces in place and a clean reading takes back
(thread.ts pushNoticed / dropNoticed); a remark whose words are already on
screen is not said again; a finding's signature ignores its count and its
placeholder example (findingKind), and the card names its page.
- The heading miscount: the check counted headings inside aria-hidden and
inert subtrees, and measured the page once, mid-swap — /voice's setup
screen (top bar h1 + page h1) leaving for its speaking screen (immersive
mode hides the top bar: no h1). Checks now count only rendered elements and
run once the page has held still, twice, keeping only what both readings
agree on (settledPageChecks / stableChecks). The Voice page itself now has
one h1 on each screen.
- A fix Watch started that failed no longer promises it "goes live by
itself": it says the fix did not finish, and "Open it in Loki to see why"
is a real link to that run's Watch page (owner-view ownerTap; watch-fix
now resolves a failed run's Watch page even without a live page).
- Self-watch: "I couldn't check on your changes just now" replaces the old
sentence; a request that lived through the page being put away (a phone
locking) is never a failure; background reads count only after three
failures in a row; the changes route always answers with CORS headers and
logs what failed.
- The header's one number is what waits on the owner's decision (failed
attempts, saved notes, notes not started, changes said but not saved),
counted by the server over ALL of the owner's rows, not inside the newest
twelve; the open list shows every one of them first. Nothing waiting:
just "Watching".
- The save receipt never quotes a cut sentence: several changes are listed
one per line under it, clamped to whole lines with "Show all".
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NZqZujmXpupVDC1Hi5Jbsx
* docs(changelog): voice, Watching and Prompts fixes
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NZqZujmXpupVDC1Hi5Jbsx
---------
Co-authored-by: Claude <noreply@anthropic.com>
272 lines
9.9 KiB
TypeScript
272 lines
9.9 KiB
TypeScript
/**
|
|
* The conversation with Loki, as data — kept pure so it is tested without a
|
|
* browser (scripts/test/widget-thread.ts).
|
|
*
|
|
* The panel used to be three tools in one box: a report form, an "Ask Loki"
|
|
* advisor and a studio chat, each with its own history that vanished on close.
|
|
* Now there is one thread. You talk to Loki; Loki answers; anything worth
|
|
* building is sent to the builder FROM the thread, and the "sent" receipt
|
|
* lands in it too. It is kept per tab (sessionStorage), so the conversation
|
|
* survives the site's own page loads — on a multi-page site, every link click
|
|
* used to wipe it.
|
|
*
|
|
* Restored items are untrusted input (anything can write sessionStorage on
|
|
* the host page), so restoring validates every field and caps every string.
|
|
*/
|
|
|
|
export type Speaker = "loki" | "cat";
|
|
export type Link = { label: string; url: string };
|
|
|
|
export type ThreadItem =
|
|
| { kind: "you"; at: number; text: string }
|
|
| {
|
|
kind: "loki";
|
|
at: number;
|
|
text: string;
|
|
speaker?: Speaker;
|
|
/** Changes Loki recommends — each one tap from the builder. */
|
|
changes?: string[];
|
|
links?: Link[];
|
|
/** No model could answer; the question itself can still be sent. */
|
|
degraded?: boolean;
|
|
}
|
|
| {
|
|
kind: "sent";
|
|
at: number;
|
|
text: string;
|
|
/** The owner's note starts a build; a visitor's waits for the builder. */
|
|
owner: boolean;
|
|
/** A project member's own note (their pass): `note` is what happened. */
|
|
member?: boolean;
|
|
building?: boolean;
|
|
savedOnly?: boolean;
|
|
note?: string;
|
|
claimUrl?: string;
|
|
/** Sent about the widget itself, to Loki's own team (self-report.ts). */
|
|
toLoki?: boolean;
|
|
/** Several changes filed at once (a spoken note's list): `text` holds
|
|
* them one per line. */
|
|
changes?: number;
|
|
}
|
|
| {
|
|
/** Watch spoke up on its own: something it saw while the owner used
|
|
* the site (watch-trail.ts explainNotice). */
|
|
kind: "noticed";
|
|
at: number;
|
|
text: string;
|
|
/** The change request "Fix this" sends. */
|
|
fix: string;
|
|
/** It broke outright and a fix was already started — nothing to tap. */
|
|
filed?: boolean;
|
|
/** The remark's signature (watch-trail.ts) — what "Fix this" files, and
|
|
* what the card's status is looked up by (known-fixes.ts). */
|
|
key?: string;
|
|
/** Loki fixed this before and it is back. */
|
|
again?: boolean;
|
|
};
|
|
|
|
/** Enough to read back a visit; small enough for sessionStorage and a prompt. */
|
|
export const THREAD_MAX = 40;
|
|
/** A thread older than this belongs to an earlier visit. */
|
|
export const THREAD_MAX_AGE_MS = 2 * 60 * 60_000;
|
|
const TEXT_MAX = 6000;
|
|
const CHANGE_MAX = 300;
|
|
|
|
export function pushItem(thread: ThreadItem[], item: ThreadItem): ThreadItem[] {
|
|
return [...thread, item].slice(-THREAD_MAX);
|
|
}
|
|
|
|
/** A remark's key that marks Loki's own unasked READ of a page (watch.ts). */
|
|
export const isPageReadKey = (key: string | undefined): boolean => !!key?.endsWith("|read");
|
|
|
|
/**
|
|
* Add Loki's unasked read of a page, replacing any earlier one. Reads are
|
|
* about the page the owner is on; four of them stacked above the owner's own
|
|
* words (one per page browsed) buried the conversation they came to have
|
|
* (operator's phone, 2026-10-15). An earlier read loses nothing: a fix it
|
|
* started is tracked under Your changes, and the page is read again on the
|
|
* next visit.
|
|
*/
|
|
export function pushPageRead(thread: ThreadItem[], item: ThreadItem): ThreadItem[] {
|
|
const kept = thread.filter((i) => !(i.kind === "noticed" && isPageReadKey(i.key)));
|
|
return pushItem(kept, item);
|
|
}
|
|
|
|
/**
|
|
* The card a remark belongs to. A page's checks own ONE card (its newest
|
|
* reading, watch-trail.ts checksSlot); everything else is its own remark.
|
|
*/
|
|
export function noticeSlot(key: string | undefined): string | null {
|
|
if (!key) return null;
|
|
const at = key.indexOf("|checks|");
|
|
return at >= 0 ? key.slice(0, at + "|checks".length) : key;
|
|
}
|
|
|
|
type Noticed = Extract<ThreadItem, { kind: "noticed" }>;
|
|
|
|
/**
|
|
* Add something Watch noticed — or update the card that already says it.
|
|
* Remarks used to be appended, so one page's checks taken three times were
|
|
* three cards, and two readings of a form ("1 field", "2 fields") two
|
|
* (operator's phone, 2026-10-20). Now a remark whose slot already has a card
|
|
* replaces that card where it stands, and one whose words are already on
|
|
* screen is not said again. Reads keep their own rule (pushPageRead).
|
|
*/
|
|
export function pushNoticed(thread: ThreadItem[], item: Noticed): ThreadItem[] {
|
|
if (isPageReadKey(item.key)) return pushPageRead(thread, item);
|
|
const slot = noticeSlot(item.key);
|
|
const same = (i: ThreadItem): i is Noticed =>
|
|
i.kind === "noticed" && ((!!slot && noticeSlot(i.key) === slot) || i.text === item.text);
|
|
const at = thread.findIndex(same);
|
|
if (at < 0) return pushItem(thread, item);
|
|
const prev = thread[at] as Noticed;
|
|
if (prev.text === item.text && prev.key === item.key && prev.filed === item.filed) return thread;
|
|
return thread.flatMap((i, n): ThreadItem[] => (n === at ? [item] : same(i) ? [] : [i]));
|
|
}
|
|
|
|
/** Take back the card a slot owns: the newest reading of that page found
|
|
* nothing, so the older one is no longer true. */
|
|
export function dropNoticed(thread: ThreadItem[], slot: string): ThreadItem[] {
|
|
const next = thread.filter((i) => !(i.kind === "noticed" && noticeSlot(i.key) === slot));
|
|
return next.length === thread.length ? thread : next;
|
|
}
|
|
|
|
const str = (v: unknown, max: number): string | null =>
|
|
typeof v === "string" && v.trim() ? v.slice(0, max) : null;
|
|
|
|
/** Only http(s) links ever become anchors on someone else's site. */
|
|
export function safeHttpUrl(raw: unknown): string | null {
|
|
if (typeof raw !== "string") return null;
|
|
try {
|
|
const u = new URL(raw);
|
|
return u.protocol === "https:" || u.protocol === "http:" ? u.href : null;
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/** Validate one stored item; null drops it. */
|
|
function restoreItem(raw: unknown, now: number): ThreadItem | null {
|
|
if (!raw || typeof raw !== "object") return null;
|
|
const r = raw as Record<string, unknown>;
|
|
const at =
|
|
typeof r.at === "number" && r.at <= now && now - r.at <= THREAD_MAX_AGE_MS ? r.at : null;
|
|
const text = str(r.text, TEXT_MAX);
|
|
if (at === null || text === null) return null;
|
|
if (r.kind === "you") return { kind: "you", at, text };
|
|
if (r.kind === "loki") {
|
|
const changes = Array.isArray(r.changes)
|
|
? r.changes
|
|
.map((c) => str(c, CHANGE_MAX))
|
|
.filter((c): c is string => !!c)
|
|
.slice(0, 5)
|
|
: [];
|
|
const links = Array.isArray(r.links)
|
|
? r.links
|
|
.map((l) => {
|
|
const o = (l ?? {}) as Record<string, unknown>;
|
|
const url = safeHttpUrl(o.url);
|
|
const label = str(o.label, 80);
|
|
return url && label ? { label, url } : null;
|
|
})
|
|
.filter((l): l is Link => !!l)
|
|
.slice(0, 5)
|
|
: [];
|
|
return {
|
|
kind: "loki",
|
|
at,
|
|
text,
|
|
speaker: r.speaker === "cat" ? "cat" : "loki",
|
|
...(changes.length ? { changes } : {}),
|
|
...(links.length ? { links } : {}),
|
|
...(r.degraded === true ? { degraded: true } : {}),
|
|
};
|
|
}
|
|
if (r.kind === "sent") {
|
|
const claimUrl = safeHttpUrl(r.claimUrl);
|
|
const note = str(r.note, 500);
|
|
return {
|
|
kind: "sent",
|
|
at,
|
|
text,
|
|
owner: r.owner === true,
|
|
...(r.member === true ? { member: true } : {}),
|
|
...(r.building === true ? { building: true } : {}),
|
|
...(r.savedOnly === true ? { savedOnly: true } : {}),
|
|
...(r.toLoki === true ? { toLoki: true } : {}),
|
|
...(typeof r.changes === "number" &&
|
|
Number.isInteger(r.changes) &&
|
|
r.changes > 1 &&
|
|
r.changes <= 50
|
|
? { changes: r.changes }
|
|
: {}),
|
|
...(note ? { note } : {}),
|
|
...(claimUrl ? { claimUrl } : {}),
|
|
};
|
|
}
|
|
if (r.kind === "noticed") {
|
|
const fix = str(r.fix, 1000);
|
|
if (!fix) return null;
|
|
const key = str(r.key, 300);
|
|
return {
|
|
kind: "noticed",
|
|
at,
|
|
text,
|
|
fix,
|
|
...(r.filed === true ? { filed: true } : {}),
|
|
...(key ? { key } : {}),
|
|
...(r.again === true ? { again: true } : {}),
|
|
};
|
|
}
|
|
return null;
|
|
}
|
|
|
|
export function restoreThread(raw: unknown, now: number): ThreadItem[] {
|
|
if (!Array.isArray(raw)) return [];
|
|
return raw
|
|
.map((r) => restoreItem(r, now))
|
|
.filter((i): i is ThreadItem => !!i)
|
|
.slice(-THREAD_MAX);
|
|
}
|
|
|
|
/**
|
|
* The thread as model history: what was said, in order, as user/assistant
|
|
* turns. "Sent" receipts are said back as Loki's words, so a follow-up like
|
|
* "and the footer too" knows what has already gone to the builder.
|
|
*/
|
|
export function historyFor(
|
|
thread: ThreadItem[],
|
|
maxTurns: number,
|
|
maxChars: number,
|
|
): { role: "user" | "assistant"; content: string }[] {
|
|
return thread
|
|
.map((i) =>
|
|
i.kind === "you"
|
|
? { role: "user" as const, content: i.text }
|
|
: i.kind === "loki"
|
|
? { role: "assistant" as const, content: i.text }
|
|
: i.kind === "noticed"
|
|
? { role: "assistant" as const, content: `(Noticed while watching: ${i.text})` }
|
|
: { role: "assistant" as const, content: `(Sent to the builder: ${i.text})` },
|
|
)
|
|
.slice(-maxTurns)
|
|
.map((t) => ({ ...t, content: t.content.slice(0, maxChars) }));
|
|
}
|
|
|
|
/** Which assistant answers in the thread. */
|
|
export type Assistant = "advisor" | "concierge" | "none";
|
|
|
|
/**
|
|
* The owner always gets the site advisor — it is their site, and Review
|
|
* answers there. Otherwise the embed decides: a studio front desk
|
|
* (`data-fc-modes` lists chat) gets the concierge; the default gets the
|
|
* advisor; `data-fc-modes="report"` opts out of AI and messages go straight
|
|
* to the builder.
|
|
*/
|
|
export function assistantFor(modes: readonly string[], owner: boolean): Assistant {
|
|
if (owner) return "advisor";
|
|
if (modes.includes("chat")) return "concierge";
|
|
if (modes.includes("ask")) return "advisor";
|
|
return "none";
|
|
}
|