mirror of
https://github.com/bitbaum/loki.git
synced 2026-12-04 20:07:42 +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>
683 lines
24 KiB
TypeScript
683 lines
24 KiB
TypeScript
/**
|
|
* The conversation's state and every call it makes — no DOM.
|
|
*
|
|
* Split out of conversation.ts so the panel can be drawn by more than one view
|
|
* (the vanilla DOM view in conversation.ts today; the shared chat, chatkit,
|
|
* next — docs/architecture/feedback-widget.md) without either one owning the
|
|
* rules. Everything a view needs is in `snapshot()`; everything it can do is a
|
|
* method here; it learns of changes through `subscribe()`.
|
|
*
|
|
* What stays in the view: the scope menu, the screenshots tray, the picker
|
|
* and the inputs — they reach the model only as the readers passed in `opts`.
|
|
*/
|
|
import { CAPTURE_CONTEXT_LIMITS } from "./capture-context";
|
|
import type { FeedbackCapture } from "./feedback-capture";
|
|
import { clampMoments, type ReportMoment } from "./moments-contract";
|
|
import { cleanWalkRead, walkSession, type WalkItem, type WalkRead } from "./walk-read";
|
|
import { understandNote } from "./loki-api";
|
|
import { buildSuggestion, type ReportDiagnostics } from "./report-payload";
|
|
import { sendReport } from "./send-report";
|
|
import { sendSelfReport } from "./self-report";
|
|
import {
|
|
ADVISE_MAX_QUESTION,
|
|
askAdvisor,
|
|
askConcierge,
|
|
REVIEW_QUESTION,
|
|
type Answer,
|
|
} from "./loki-api";
|
|
import {
|
|
dropNoticed,
|
|
historyFor,
|
|
pushItem,
|
|
pushNoticed,
|
|
restoreThread,
|
|
type Assistant,
|
|
type Link,
|
|
type ThreadItem,
|
|
} from "./thread";
|
|
import type { About } from "./about-bar";
|
|
import type { Picker } from "./picker";
|
|
|
|
/** The ingest's cap on `suggestion` (api/feedback FeedbackBody). */
|
|
export const MAX_LEN = 2000;
|
|
/** A spoken question can run two minutes; the server takes what the widget sends. */
|
|
export const ASK_MAX = ADVISE_MAX_QUESTION;
|
|
|
|
const threadKey = (token: string) => `loki-thread:${token}`;
|
|
|
|
function readThread(token: string): ThreadItem[] {
|
|
try {
|
|
return restoreThread(JSON.parse(sessionStorage.getItem(threadKey(token)) ?? "[]"), Date.now());
|
|
} catch {
|
|
return [];
|
|
}
|
|
}
|
|
|
|
function writeThread(token: string, thread: ThreadItem[]): void {
|
|
try {
|
|
sessionStorage.setItem(threadKey(token), JSON.stringify(thread));
|
|
} catch {
|
|
/* storage blocked: the thread lasts for this page view */
|
|
}
|
|
}
|
|
|
|
export const CONCIERGE_GREETING = [
|
|
{
|
|
speaker: "cat" as const,
|
|
text: "Ask about earning, selling or getting paid in Bitcoin.",
|
|
},
|
|
{
|
|
speaker: "loki" as const,
|
|
text: "Ask about building or running something, or improving this site.",
|
|
},
|
|
];
|
|
|
|
export const STARTERS: Record<Assistant, string[]> = {
|
|
advisor: [
|
|
"What would you improve here?",
|
|
"Is this clear to a first-time visitor?",
|
|
"Something here is broken",
|
|
],
|
|
concierge: [
|
|
"What can you build for me?",
|
|
"I want to earn in Bitcoin",
|
|
"How do I run AI agents on my code?",
|
|
],
|
|
none: [],
|
|
};
|
|
|
|
export type ConversationWatch = {
|
|
/** Watching right now (owner, not paused). */
|
|
on: () => boolean;
|
|
/** The session as Review sends it. */
|
|
session: () => string;
|
|
/** The watched steps as report lines — null while paused. */
|
|
diagnostics: () => ReportDiagnostics | null;
|
|
/** Record that the owner talked to Loki, so Review does not read the time
|
|
* as the site doing nothing. Optional: a view without Watch has none. */
|
|
talked?: (text: string) => void;
|
|
};
|
|
|
|
/** A confirmation card in the thread: what will be sent, before it is. */
|
|
export type Draft = {
|
|
text: string;
|
|
diagnostics: ReportDiagnostics | null;
|
|
capture?: FeedbackCapture;
|
|
intent?: "note" | "fix";
|
|
/** What was seen while each thing was said (voice-moments.ts). */
|
|
moments?: ReportMoment[];
|
|
/** A spoken note: the words exactly as transcribed (the box may hold Loki's read of them). */
|
|
verbatim?: string;
|
|
/** How long the person talked. */
|
|
spokenMs?: number;
|
|
/** Loki's read of a spoken note (walk-read.ts), once it is back. */
|
|
read?: WalkRead;
|
|
/** The read's list as the person edits it; absent shows the words in one box. */
|
|
items?: WalkItem[];
|
|
/** The read is on its way — the card works without it in the meantime. */
|
|
reading?: boolean;
|
|
};
|
|
|
|
/**
|
|
* Changes said but not saved yet: a spoken note's change lines while its card
|
|
* is open (or the note itself, before Loki has read it into lines). They wait
|
|
* on the owner's Save, so the header counts them among what needs them.
|
|
*/
|
|
export function unsavedChanges(draft: Draft | null): number {
|
|
if (!draft || draft.intent !== "note") return 0;
|
|
if (draft.items) return draft.items.filter((i) => i.kind === "change" && i.text.trim()).length;
|
|
return draft.text.trim() ? 1 : 0;
|
|
}
|
|
|
|
const draftKey = (token: string) => `loki-voice-draft:${token}`;
|
|
function readRecordedDraft(token: string): Draft | null {
|
|
try {
|
|
const raw = JSON.parse(sessionStorage.getItem(draftKey(token)) ?? "null") as Draft | null;
|
|
if (
|
|
!raw ||
|
|
raw.intent !== "note" ||
|
|
typeof raw.text !== "string" ||
|
|
raw.text.length > 12000 ||
|
|
!raw.capture ||
|
|
typeof raw.capture.url !== "string" ||
|
|
!/^https?:\/\//.test(raw.capture.url) ||
|
|
typeof raw.capture.page !== "string" ||
|
|
!Array.isArray(raw.capture.selected) ||
|
|
raw.capture.selected.length > 10 ||
|
|
!raw.capture.diagnostics ||
|
|
typeof raw.capture.diagnostics !== "object"
|
|
)
|
|
return null;
|
|
const selected = raw.capture.selected
|
|
.filter(
|
|
(item) =>
|
|
typeof item.elementType === "string" &&
|
|
typeof item.elementText === "string" &&
|
|
typeof item.selector === "string",
|
|
)
|
|
.map((item) => ({
|
|
elementType: item.elementType.slice(0, 100),
|
|
elementText: item.elementText.slice(0, 300),
|
|
selector: item.selector.slice(0, 500),
|
|
}));
|
|
const diagnostics = Object.fromEntries(
|
|
Object.entries(raw.capture.diagnostics)
|
|
.slice(0, CAPTURE_CONTEXT_LIMITS.fields)
|
|
.filter(
|
|
([, value]) =>
|
|
typeof value === "string" ||
|
|
typeof value === "boolean" ||
|
|
typeof value === "number" ||
|
|
value === null,
|
|
)
|
|
.map(([key, value]) => [
|
|
key.slice(0, CAPTURE_CONTEXT_LIMITS.keyChars),
|
|
typeof value === "string" ? value.slice(0, CAPTURE_CONTEXT_LIMITS.valueChars) : value,
|
|
]),
|
|
);
|
|
const capture = {
|
|
url: raw.capture.url.slice(0, 1000),
|
|
page: raw.capture.page.slice(0, 300),
|
|
title: String(raw.capture.title ?? "").slice(0, 300),
|
|
selected,
|
|
diagnostics,
|
|
};
|
|
const moments = clampMoments(raw.moments);
|
|
const read = cleanWalkRead(raw.read);
|
|
const items = Array.isArray(raw.items) ? cleanWalkRead({ items: raw.items })?.items : undefined;
|
|
return {
|
|
text: raw.text,
|
|
intent: "note",
|
|
capture,
|
|
diagnostics,
|
|
...(moments.length ? { moments } : {}),
|
|
...(typeof raw.verbatim === "string" ? { verbatim: raw.verbatim.slice(0, 12000) } : {}),
|
|
...(typeof raw.spokenMs === "number" && Number.isFinite(raw.spokenMs)
|
|
? { spokenMs: Math.max(0, Math.min(raw.spokenMs, 3_600_000)) }
|
|
: {}),
|
|
...(read ? { read } : {}),
|
|
...(items?.length ? { items } : {}),
|
|
};
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
function keepRecordedDraft(token: string, draft: Draft | null) {
|
|
try {
|
|
if (draft?.intent === "note") sessionStorage.setItem(draftKey(token), JSON.stringify(draft));
|
|
else sessionStorage.removeItem(draftKey(token));
|
|
} catch {
|
|
/* The current note remains available when storage is blocked. */
|
|
}
|
|
}
|
|
|
|
export type ConversationSnapshot = {
|
|
thread: readonly ThreadItem[];
|
|
/** Waiting on Loki; `review` when it is going through what you did. */
|
|
pending: null | { review: boolean };
|
|
/** The last thing that went wrong, in words — cleared by the next action. */
|
|
error: string;
|
|
draft: Draft | null;
|
|
};
|
|
|
|
export type ConversationModelOpts = {
|
|
apiBase: string;
|
|
token: string;
|
|
assistant: () => Assistant;
|
|
/** The owner pass, while the server still honours it. */
|
|
ownerPass: () => string | null;
|
|
/** A project member's pass (member-pass.ts), while the server honours it. */
|
|
memberPass?: () => string | null;
|
|
/** The server no longer honours the member pass: stop sending it. */
|
|
onMemberRefused?: () => void;
|
|
/** The server refused the pass (expired, revoked): stop presenting it. */
|
|
onPassRefused: () => void;
|
|
picker: Picker;
|
|
watch: () => ConversationWatch | null;
|
|
/** What the next message is about (the view's scope menu). */
|
|
about: () => About;
|
|
/** The ingest's scope for what is about the SITE. */
|
|
scope: () => "element" | "page" | "site";
|
|
/** The screenshots waiting to go with the next note (the view's tray). */
|
|
screenshots: { list: () => string[]; clear: () => void };
|
|
onBusy?: (busy: boolean) => void;
|
|
/** A note was filed — Your changes refreshes to show where it went. */
|
|
onSent?: () => void;
|
|
};
|
|
|
|
/** Which starter questions show, and whether Review leads them. */
|
|
export function startersFor(
|
|
assistant: Assistant,
|
|
thread: readonly ThreadItem[],
|
|
watching: boolean,
|
|
): { list: string[]; review: boolean } {
|
|
// Watching already adds a live status and a tailored review action. Keep
|
|
// the empty conversation focused on that single next step on small screens.
|
|
if (watching) return { list: [REVIEW_QUESTION], review: true };
|
|
const all = [...STARTERS[assistant]];
|
|
// Starters are for an empty conversation; after that they are noise —
|
|
// once the user has begun, they are noise.
|
|
const empty = !thread.some((i) => i.kind === "you" || i.kind === "noticed");
|
|
if (!all.length || !empty) return { list: [], review: false };
|
|
return { list: all, review: false };
|
|
}
|
|
|
|
export function createConversationModel(opts: ConversationModelOpts) {
|
|
let thread = readThread(opts.token);
|
|
let pending: ConversationSnapshot["pending"] = null;
|
|
let error = "";
|
|
let draft: Draft | null = readRecordedDraft(opts.token);
|
|
const listeners = new Set<(follow: boolean) => void>();
|
|
|
|
const owner = () => opts.ownerPass() !== null;
|
|
const snapshot = (): ConversationSnapshot => ({ thread, pending, error, draft });
|
|
/** `follow`: the view should jump to the newest message (your own action). */
|
|
const changed = (follow = false) => listeners.forEach((l) => l(follow));
|
|
|
|
function remember(item: ThreadItem) {
|
|
thread = pushItem(thread, item);
|
|
writeThread(opts.token, thread);
|
|
}
|
|
|
|
function fail(e: unknown, fallback: string) {
|
|
error = e instanceof Error ? e.message : fallback;
|
|
changed();
|
|
}
|
|
|
|
async function ask(question: string, session?: string) {
|
|
const text = question.trim().slice(0, ASK_MAX);
|
|
if (pending || !text) return;
|
|
const a = opts.assistant();
|
|
if (a === "none") return openDraft(text);
|
|
pending = { review: !!session };
|
|
error = "";
|
|
opts.onBusy?.(true);
|
|
remember({ kind: "you", at: Date.now(), text });
|
|
if (!session) opts.watch()?.talked?.(text);
|
|
changed(true);
|
|
const history = historyFor(thread.slice(0, -1), 12, 3000);
|
|
try {
|
|
const answer: Answer =
|
|
a === "concierge"
|
|
? await askConcierge({ apiBase: opts.apiBase, token: opts.token, message: text, history })
|
|
: await askAdvisor({
|
|
apiBase: opts.apiBase,
|
|
token: opts.token,
|
|
question: text,
|
|
scope: session ? "page" : opts.scope(),
|
|
selected: opts.picker.selected(),
|
|
history,
|
|
session,
|
|
ownerPass: opts.ownerPass() ?? undefined,
|
|
});
|
|
answer.messages.forEach((m, i) =>
|
|
remember({
|
|
kind: "loki",
|
|
at: Date.now(),
|
|
speaker: m.speaker,
|
|
text: m.text,
|
|
// Changes and links follow the last speaker's words.
|
|
...(i === answer.messages.length - 1 && answer.changes.length
|
|
? { changes: answer.changes }
|
|
: {}),
|
|
...(i === answer.messages.length - 1 && answer.links.length
|
|
? { links: answer.links }
|
|
: {}),
|
|
...(answer.degraded ? { degraded: true } : {}),
|
|
}),
|
|
);
|
|
} catch (e) {
|
|
// No dead end: the question is still in the thread, one tap from the builder.
|
|
error = e instanceof Error ? e.message : "Could not reach Loki — try again.";
|
|
} finally {
|
|
pending = null;
|
|
opts.onBusy?.(false);
|
|
changed(true);
|
|
}
|
|
}
|
|
|
|
function review() {
|
|
const w = opts.watch();
|
|
if (w) void ask(REVIEW_QUESTION, w.session());
|
|
}
|
|
|
|
/**
|
|
* The owner's word IS the decision: a change starts building straight away,
|
|
* with the watched steps attached, and its receipt says where it went. The
|
|
* confirmation card is a visitor's (it asks who they are), or the owner's
|
|
* when they choose "Edit first".
|
|
*/
|
|
function build(text: string, key?: string) {
|
|
if (draft?.intent === "note") {
|
|
error = "Save or cancel your current note before requesting another change.";
|
|
changed(true);
|
|
return;
|
|
}
|
|
error = "";
|
|
if (opts.about() === "loki" && !key) {
|
|
toLoki(text).catch((e) => fail(e, "Could not send it to Loki — try again."));
|
|
return;
|
|
}
|
|
if (!owner()) return openDraft(text);
|
|
deliver(text, "", opts.watch()?.diagnostics() ?? null, key).catch((e) =>
|
|
fail(e, "Could not start it — try again."),
|
|
);
|
|
}
|
|
|
|
/** A note about the widget itself, to Loki's own project — no confirmation
|
|
* card: there is no builder of this site to introduce yourself to. */
|
|
async function toLoki(body: string) {
|
|
const res = await sendSelfReport(opts.apiBase, {
|
|
token: opts.token,
|
|
kind: "note",
|
|
note: body,
|
|
ownerPass: opts.ownerPass() ?? undefined,
|
|
screenshots: opts.screenshots.list(),
|
|
});
|
|
opts.screenshots.clear();
|
|
remember({
|
|
kind: "sent",
|
|
at: Date.now(),
|
|
text: body.slice(0, MAX_LEN),
|
|
owner: false,
|
|
toLoki: true,
|
|
...(res.building ? { building: true } : {}),
|
|
...(res.note ? { note: res.note } : {}),
|
|
// The build when one started, else the claim link — never no way to follow.
|
|
...(res.followUrl || res.claimUrl ? { claimUrl: res.followUrl ?? res.claimUrl } : {}),
|
|
});
|
|
changed(true);
|
|
}
|
|
|
|
/** File the note; the receipt lands in the thread. Throws the server's words. */
|
|
async function deliver(
|
|
body: string,
|
|
contact: string,
|
|
diag: ReportDiagnostics | null,
|
|
key?: string,
|
|
capture?: FeedbackCapture,
|
|
intent?: "note" | "fix",
|
|
moments?: ReportMoment[],
|
|
) {
|
|
const pass = opts.ownerPass();
|
|
const res = await sendReport(opts.apiBase, {
|
|
token: opts.token,
|
|
intent,
|
|
capture,
|
|
moments: moments?.length ? clampMoments(moments) : undefined,
|
|
suggestion: capture ? body.trim().slice(0, MAX_LEN) : buildSuggestion(body, diag, MAX_LEN),
|
|
contact,
|
|
scope: capture ? (capture.selected.length ? "element" : "page") : opts.scope(),
|
|
screenshots: opts.screenshots.list(),
|
|
selectedElements: opts.picker.selected(),
|
|
ownerPass: pass ?? undefined,
|
|
...memberField(pass),
|
|
// The finding's key rides with its fix, so the next visit's remark
|
|
// finds this row instead of filing another.
|
|
...(pass && key ? { noticeKey: key } : {}),
|
|
});
|
|
if (pass && !res.owner) opts.onPassRefused();
|
|
memberHonoured(pass, res);
|
|
sent(res, body);
|
|
}
|
|
|
|
/** The member pass rides only when the owner's does not: the owner's wins. */
|
|
function memberField(ownerPass: string | null): { memberPass?: string } {
|
|
const member = ownerPass ? null : (opts.memberPass?.() ?? null);
|
|
return member ? { memberPass: member } : {};
|
|
}
|
|
|
|
/** A member pass the server no longer honours is dropped, never resent. */
|
|
function memberHonoured(ownerPass: string | null, res: { member?: boolean }) {
|
|
if (!ownerPass && opts.memberPass?.() && res.member !== true) opts.onMemberRefused?.();
|
|
}
|
|
|
|
/** After filing: the card goes (or keeps what is left), the receipt lands. */
|
|
function sent(
|
|
res: Awaited<ReturnType<typeof sendReport>>,
|
|
body: string,
|
|
left?: Draft,
|
|
count = 1,
|
|
) {
|
|
draft = left ?? null;
|
|
keepRecordedDraft(opts.token, draft);
|
|
opts.screenshots.clear();
|
|
opts.picker.clearSelection();
|
|
remember({
|
|
kind: "sent",
|
|
at: Date.now(),
|
|
text: body.slice(0, MAX_LEN),
|
|
owner: res.owner === true,
|
|
...(res.member ? { member: true } : {}),
|
|
...(res.building ? { building: true } : {}),
|
|
...(res.savedOnly ? { savedOnly: true } : {}),
|
|
...(count > 1 ? { changes: count } : {}),
|
|
...(res.buildNote || res.say ? { note: res.buildNote ?? res.say } : {}),
|
|
// One link either way: a visitor tracks their report, the owner
|
|
// follows the build Loki just started.
|
|
...(res.claimUrl || res.followUrl ? { claimUrl: res.followUrl ?? res.claimUrl } : {}),
|
|
});
|
|
changed(true);
|
|
opts.onSent?.();
|
|
}
|
|
|
|
function openDraft(text: string, diagnostics: ReportDiagnostics | null = null) {
|
|
if (draft?.intent === "note") {
|
|
error = "Save or cancel your current note first.";
|
|
changed(true);
|
|
return;
|
|
}
|
|
const diag = diagnostics ?? (owner() ? (opts.watch()?.diagnostics() ?? null) : null);
|
|
draft = { text: text.slice(0, MAX_LEN), diagnostics: diag };
|
|
changed(true);
|
|
}
|
|
|
|
return {
|
|
snapshot,
|
|
/** Called on every change; `follow` asks the view to show the newest. */
|
|
subscribe(listener: (follow: boolean) => void): () => void {
|
|
listeners.add(listener);
|
|
return () => listeners.delete(listener);
|
|
},
|
|
owner,
|
|
ask: (q: string, session?: string) => void ask(q, session),
|
|
review,
|
|
build,
|
|
openDraft,
|
|
/** Send the open confirmation card, as edited. Throws the server's words
|
|
* for the card to show beside its button. */
|
|
sendDraft: (body: string, contact: string, intent?: "note" | "fix") => {
|
|
if (body.trim().length > MAX_LEN)
|
|
return Promise.reject(
|
|
new Error(
|
|
`Shorten this note to ${MAX_LEN} characters before sending. Your full recording text is kept here.`,
|
|
),
|
|
);
|
|
const diag = draft?.diagnostics ?? null;
|
|
return deliver(
|
|
body,
|
|
contact,
|
|
diag,
|
|
undefined,
|
|
draft?.capture,
|
|
intent ?? draft?.intent,
|
|
draft?.moments,
|
|
);
|
|
},
|
|
recorded(
|
|
text: string,
|
|
capture: FeedbackCapture,
|
|
moments: ReportMoment[] = [],
|
|
spokenMs?: number,
|
|
) {
|
|
const note: Draft = {
|
|
text,
|
|
verbatim: text,
|
|
diagnostics: capture.diagnostics,
|
|
capture,
|
|
intent: "note",
|
|
reading: true,
|
|
...(moments.length ? { moments } : {}),
|
|
...(spokenMs ? { spokenMs } : {}),
|
|
};
|
|
draft = note;
|
|
keepRecordedDraft(opts.token, draft);
|
|
changed(true);
|
|
// Loki reads it the way a person would listen (walk-read.ts). Nothing
|
|
// waits on this: the card works with the words as spoken meanwhile, and
|
|
// a read that fails leaves them exactly as they are.
|
|
void understandNote({ apiBase: opts.apiBase, token: opts.token, note: text, moments }).then(
|
|
(read) => {
|
|
if (draft !== note) return;
|
|
// A new object, so the card is drawn again with the read in it.
|
|
// Only words the person has not touched are replaced.
|
|
const untouched = draft.text === note.verbatim;
|
|
draft = {
|
|
...draft,
|
|
reading: false,
|
|
...(read
|
|
? untouched
|
|
? { read, text: read.ask, items: read.items.map((i) => ({ ...i })) }
|
|
: { read }
|
|
: {}),
|
|
};
|
|
keepRecordedDraft(opts.token, draft);
|
|
changed();
|
|
},
|
|
);
|
|
},
|
|
/** Put the words exactly as spoken back in the box. */
|
|
useExactWords() {
|
|
if (!draft?.verbatim) return;
|
|
draft = { ...draft, text: draft.verbatim, items: undefined };
|
|
keepRecordedDraft(opts.token, draft);
|
|
changed();
|
|
},
|
|
/** Ask Loki about a spoken note, with the whole walk as what it looks at. */
|
|
askAboutNote(text: string) {
|
|
const moments = draft?.moments ?? [];
|
|
draft = null;
|
|
keepRecordedDraft(opts.token, null);
|
|
void ask(text, moments.length > 1 ? walkSession(moments) : undefined);
|
|
},
|
|
/** Change one listed item's words; empty words drop it. */
|
|
editItem(index: number, text: string) {
|
|
if (!draft?.items?.[index]) return;
|
|
draft.items[index] = { ...draft.items[index], text };
|
|
keepRecordedDraft(opts.token, draft);
|
|
},
|
|
dropItem(index: number) {
|
|
if (!draft?.items) return;
|
|
const items = draft.items.filter((_, i) => i !== index);
|
|
draft = items.length
|
|
? { ...draft, items }
|
|
: { ...draft, items: undefined, text: draft.verbatim ?? draft.text };
|
|
keepRecordedDraft(opts.token, draft);
|
|
changed();
|
|
},
|
|
/**
|
|
* File every listed change as its own note — each with the moments it came
|
|
* from, on the page it was said on — so each is built on its own (and the
|
|
* owner's saved notes build page by page). Questions stay on the card for
|
|
* Ask. One receipt for the lot. Throws the server's words.
|
|
*/
|
|
async sendItems(contact: string, intent?: "note" | "fix") {
|
|
const note = draft;
|
|
const items = note?.items?.filter((i) => i.text.trim()) ?? [];
|
|
const changes = items.filter((i) => i.kind === "change");
|
|
if (!note || !changes.length) return;
|
|
const pass = opts.ownerPass();
|
|
let res: Awaited<ReturnType<typeof sendReport>> | null = null;
|
|
for (const item of changes) {
|
|
const moments = (note.moments ?? []).filter((_, i) => item.parts.includes(i));
|
|
const pages = new Set(moments.map((m) => m.page));
|
|
const capture =
|
|
note.capture && pages.size === 1
|
|
? { ...note.capture, page: [...pages][0] }
|
|
: note.capture;
|
|
const body = (item.where ? `${item.where}: ${item.text}` : item.text).slice(0, MAX_LEN);
|
|
res = await sendReport(opts.apiBase, {
|
|
token: opts.token,
|
|
intent: intent ?? "note",
|
|
capture,
|
|
moments: clampMoments(moments.length ? moments : (note.moments ?? [])),
|
|
suggestion: body,
|
|
contact,
|
|
scope: capture?.selected.length ? "element" : "page",
|
|
screenshots: opts.screenshots.list(),
|
|
selectedElements: opts.picker.selected(),
|
|
ownerPass: pass ?? undefined,
|
|
...memberField(pass),
|
|
});
|
|
// Filed: off the card, so a failure on the next keeps only what is left.
|
|
note.items = note.items?.filter((i) => i !== item);
|
|
}
|
|
if (!res) return;
|
|
if (pass && !res.owner) opts.onPassRefused();
|
|
memberHonoured(pass, res);
|
|
const questions = note.items?.filter((i) => i.kind === "question") ?? [];
|
|
// One change per line, each whole: the receipt shows them as a list.
|
|
const said =
|
|
changes.length === 1 ? changes[0].text : changes.map((c) => `• ${c.text}`).join("\n");
|
|
sent(res, said, questions.length ? { ...note, items: questions } : undefined, changes.length);
|
|
},
|
|
editDraft(text: string) {
|
|
if (!draft) return;
|
|
draft.text = text;
|
|
keepRecordedDraft(opts.token, draft);
|
|
},
|
|
closeDraft: () => {
|
|
draft = null;
|
|
keepRecordedDraft(opts.token, null);
|
|
changed();
|
|
},
|
|
clearError: () => {
|
|
if (!error) return;
|
|
error = "";
|
|
changed();
|
|
},
|
|
/** Watch noticed something: Loki says it in the thread, with Fix this. */
|
|
noticed(remark: { say: string; fix: string; filed?: boolean; key?: string; again?: boolean }) {
|
|
const item: Extract<ThreadItem, { kind: "noticed" }> = {
|
|
kind: "noticed",
|
|
at: Date.now(),
|
|
text: remark.say,
|
|
fix: remark.fix,
|
|
...(remark.filed ? { filed: true } : {}),
|
|
...(remark.key ? { key: remark.key } : {}),
|
|
...(remark.again ? { again: true } : {}),
|
|
};
|
|
const next = pushNoticed(thread, item);
|
|
if (next === thread) return;
|
|
thread = next;
|
|
writeThread(opts.token, thread);
|
|
changed();
|
|
},
|
|
/** The newest reading of a page found nothing: its older card goes. */
|
|
unnoticed(slot: string) {
|
|
const next = dropNoticed(thread, slot);
|
|
if (next === thread) return;
|
|
thread = next;
|
|
writeThread(opts.token, thread);
|
|
changed();
|
|
},
|
|
/** Loki on Loki (self-watch.ts) — quiet: never moves your reading place. */
|
|
selfNote(text: string) {
|
|
remember({ kind: "loki", at: Date.now(), speaker: "loki", text });
|
|
changed();
|
|
},
|
|
/** Loki says something unprompted (e.g. "this site is not yours"). */
|
|
say(text: string, links?: Link[]) {
|
|
remember({ kind: "loki", at: Date.now(), speaker: "loki", text, links });
|
|
changed(true);
|
|
},
|
|
/** Closing the panel: the card and the error go; the thread is kept. */
|
|
close() {
|
|
if (draft?.intent !== "note") draft = null;
|
|
error = "";
|
|
},
|
|
};
|
|
}
|
|
|
|
export type ConversationModel = ReturnType<typeof createConversationModel>;
|