mirror of
https://github.com/bitbaum/loki.git
synced 2026-12-05 23:19:08 +01:00
* feat(widget): the widget reports on itself, to Loki Watch mode reads the host page and is careful never to blame the site for Loki's own work: the widget's own requests, errors and layout were left out of what it says — which meant dropped. Nobody heard about a panel that did not fit a phone unless the operator was the one holding it. - A fourth "About" chip, "Loki itself": a note about the panel goes to Loki's own project (FEEDBACK_WIDGET_TOKEN's), never to the site's builder, with the same screenshot attach. About bar extracted to widget/about-bar.ts. - Self-watch (owner, while watching): the widget's own errors, its calls to Loki failing or over 20s, and a panel wider than the screen are filed to the same place automatically — once per kind per tab, said in one line in the conversation, and never looping through the report call itself. A 4xx is Loki answering on purpose, not a defect. Watch now hands own-requests over instead of discarding them. - New public route /api/widget/self-report: host fcw_ token + origin allowlist, owner pass required for anything the widget noticed unasked, one row per kind per site via noticeKey. - The owner's top bar steps aside while the site has a modal open: it sat on top of "Choose a model" on the operator's phone. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01E1qoQ9syUYXCRNbyrpqPGa * feat(widget): Loki's own owner's self-reports start the fix on the spot A self-report filed by Loki's OWN owner — using Loki's widget on one of their sites — is the owner's word on their own product, and the owner's word is the decision (api/feedback): a note, a crash or a panel that did not fit starts a build into Loki there and then, and the receipt reads "On it … built into Loki itself" with Follow the build →. A slow or failed call to Loki only files; its duplicate count is the signal. Anyone else's waits in the inbox. The receipt wording moves to widget/receipt.ts (pure, tested) — the thread renderer was over the lint's line cap. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01E1qoQ9syUYXCRNbyrpqPGa * test(widget): the About row has four chips now Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01E1qoQ9syUYXCRNbyrpqPGa --------- Co-authored-by: Claude <noreply@anthropic.com>
203 lines
6.7 KiB
TypeScript
203 lines
6.7 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;
|
|
building?: boolean;
|
|
note?: string;
|
|
claimUrl?: string;
|
|
/** Sent about the widget itself, to Loki's own team (self-report.ts). */
|
|
toLoki?: boolean;
|
|
}
|
|
| {
|
|
/** 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);
|
|
}
|
|
|
|
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.building === true ? { building: true } : {}),
|
|
...(r.toLoki === true ? { toLoki: true } : {}),
|
|
...(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";
|
|
}
|