mirror of
https://github.com/bitbaum/loki.git
synced 2026-12-04 09:47:22 +01:00
On a client's site the owner said a description field had no microphone and asked, twice, whether the site used the fleet's packages. Loki gave the same advice about one textarea twice, in German to an English question, and its Review called the minutes spent talking to it "the page never responded for 194 s" with an invented endpoint. - The advisor now sees how the site is built: the packages its code uses, the kickoff's plan, and the owner's build conventions (src/config/build-conventions.ts, also carried by every dispatch). - Naming a package is no longer treated as "explaining how to edit"; a point about every form is answered as a rule across the site; no repeated answers; replies follow the language of the latest message. - What the owner says to Loki is a `talk` step in Watch's trail, so a Review reads that time as conversation; Review may not name an endpoint the evidence does not show. - Loki's unasked page read skips test data and replaces the previous read instead of stacking one per page. - First build convention: fields where people describe something get a microphone from @bitbaum/chatkit; short fields never do. Claude-Session: https://claude.ai/code/session_018LMe5Uv6mfWJibAiCaNbbk Co-authored-by: Claude <noreply@anthropic.com>
217 lines
6.9 KiB
TypeScript
217 lines
6.9 KiB
TypeScript
// The widget's one conversation, as data (widget/thread.ts): who answers on
|
|
// which embed, a stored thread restored only as far as it validates (anything
|
|
// on the host page can write sessionStorage), and the thread said back to the
|
|
// model as history — receipts included, so "and the footer too" has context.
|
|
// Run: npx tsx scripts/test/widget-thread.ts
|
|
import assert from "node:assert/strict";
|
|
import {
|
|
assistantFor,
|
|
historyFor,
|
|
isPageReadKey,
|
|
pushItem,
|
|
pushPageRead,
|
|
restoreThread,
|
|
THREAD_MAX,
|
|
THREAD_MAX_AGE_MS,
|
|
type ThreadItem,
|
|
} from "../../widget/thread";
|
|
import { CHAT_MAX_HISTORY, CHAT_MAX_MESSAGE } from "../../widget/loki-api";
|
|
import { parseWidgetSurfaceModes } from "../../widget/surface-modes";
|
|
import { readFileSync } from "node:fs";
|
|
|
|
// Read, not imported: the route module opens the database on import.
|
|
const chatRoute = readFileSync("src/app/api/widget/chat/route.ts", "utf8");
|
|
const routeCap = (name: string) =>
|
|
Number(new RegExp(`export const ${name} = (\\d+)`).exec(chatRoute)?.[1]);
|
|
const WIDGET_CHAT_MAX_MESSAGE = routeCap("WIDGET_CHAT_MAX_MESSAGE");
|
|
const WIDGET_CHAT_MAX_HISTORY = routeCap("WIDGET_CHAT_MAX_HISTORY");
|
|
|
|
// ---- who answers ----
|
|
assert.equal(
|
|
assistantFor(parseWidgetSurfaceModes(null), false),
|
|
"advisor",
|
|
"default: the site advisor",
|
|
);
|
|
assert.equal(assistantFor(parseWidgetSurfaceModes("chat,report"), false), "concierge");
|
|
assert.equal(
|
|
assistantFor(parseWidgetSurfaceModes("report"), false),
|
|
"none",
|
|
"report-only opts out of AI",
|
|
);
|
|
assert.equal(
|
|
assistantFor(parseWidgetSurfaceModes("report"), true),
|
|
"advisor",
|
|
"the owner always gets advice",
|
|
);
|
|
assert.equal(
|
|
assistantFor(parseWidgetSurfaceModes("chat"), true),
|
|
"advisor",
|
|
"even on a front desk",
|
|
);
|
|
assert.deepEqual(parseWidgetSurfaceModes("chat, report, chat, nonsense"), ["chat", "report"]);
|
|
assert.deepEqual(parseWidgetSurfaceModes("nonsense"), ["report", "ask"]);
|
|
|
|
// ---- restoring a stored thread ----
|
|
{
|
|
const now = 50_000_000;
|
|
const restored = restoreThread(
|
|
[
|
|
{ kind: "you", at: now - 1000, text: "Is the menu ok?" },
|
|
{
|
|
kind: "loki",
|
|
at: now - 900,
|
|
text: "Mostly.",
|
|
speaker: "cat",
|
|
changes: ["Shorten it", 7, "x".repeat(900)],
|
|
links: [
|
|
{ label: "OK", url: "https://example.org/a" },
|
|
{ label: "evil", url: "javascript:alert(1)" },
|
|
],
|
|
},
|
|
{
|
|
kind: "sent",
|
|
at: now - 800,
|
|
text: "Shorten it",
|
|
owner: true,
|
|
building: true,
|
|
claimUrl: "data:x",
|
|
},
|
|
{
|
|
kind: "sent",
|
|
at: now - 790,
|
|
text: "The panel covers the keyboard",
|
|
owner: false,
|
|
toLoki: true,
|
|
},
|
|
{ kind: "you", at: now - THREAD_MAX_AGE_MS - 1, text: "from an earlier visit" },
|
|
{ kind: "you", at: now + 60_000, text: "from the future" },
|
|
{ kind: "you", at: now - 10, text: " " },
|
|
{ kind: "admin", at: now - 10, text: "forged" },
|
|
null,
|
|
"nonsense",
|
|
],
|
|
now,
|
|
);
|
|
assert.equal(restored.length, 4, `only valid, fresh items survive (${JSON.stringify(restored)})`);
|
|
const loki = restored[1] as Extract<ThreadItem, { kind: "loki" }>;
|
|
assert.equal(loki.speaker, "cat");
|
|
assert.equal(loki.changes?.length, 2, "non-string changes dropped");
|
|
assert.equal(loki.changes?.[1].length, 300, "a long change is capped");
|
|
assert.deepEqual(
|
|
loki.links,
|
|
[{ label: "OK", url: "https://example.org/a" }],
|
|
"only http(s) links",
|
|
);
|
|
const sent = restored[2] as Extract<ThreadItem, { kind: "sent" }>;
|
|
assert.equal(sent.claimUrl, undefined, "a non-http claim link is dropped");
|
|
assert.equal(sent.building, true);
|
|
const toLoki = restored[3] as Extract<ThreadItem, { kind: "sent" }>;
|
|
assert.equal(toLoki.toLoki, true, "a note about Loki itself is remembered as such");
|
|
assert.deepEqual(restoreThread("not an array", now), []);
|
|
}
|
|
|
|
// ---- Loki's remarks survive a reload, and need their fix to be a remark ----
|
|
{
|
|
const now = 50_000_000;
|
|
const r = restoreThread(
|
|
[
|
|
{
|
|
kind: "noticed",
|
|
at: now - 5,
|
|
text: "It found nothing there (404).",
|
|
fix: "Fix /hours",
|
|
filed: true,
|
|
},
|
|
{ kind: "noticed", at: now - 4, text: "No fix to send" },
|
|
{
|
|
kind: "noticed",
|
|
at: now - 3,
|
|
text: "The page has no main heading.",
|
|
fix: "Add one",
|
|
key: "/p|checks|The page has no main heading (h#)",
|
|
again: true,
|
|
},
|
|
],
|
|
now,
|
|
);
|
|
assert.deepEqual(r, [
|
|
{
|
|
kind: "noticed",
|
|
at: now - 5,
|
|
text: "It found nothing there (404).",
|
|
fix: "Fix /hours",
|
|
filed: true,
|
|
},
|
|
{
|
|
kind: "noticed",
|
|
at: now - 3,
|
|
text: "The page has no main heading.",
|
|
fix: "Add one",
|
|
key: "/p|checks|The page has no main heading (h#)",
|
|
again: true,
|
|
},
|
|
]);
|
|
assert.match(historyFor(r, 4, 200)[0].content, /^\(Noticed while watching: /);
|
|
}
|
|
|
|
// ---- bounded ----
|
|
{
|
|
let t: ThreadItem[] = [];
|
|
for (let i = 0; i < THREAD_MAX + 7; i++) t = pushItem(t, { kind: "you", at: i, text: `m${i}` });
|
|
assert.equal(t.length, THREAD_MAX);
|
|
assert.equal((t[0] as { text: string }).text, "m7", "the oldest go first");
|
|
}
|
|
|
|
// ---- Loki's unasked page reads: only the newest stays ----
|
|
{
|
|
// 2026-10-15: one read per page browsed, four stacked above the owner's own
|
|
// words. A new read replaces the last; every other item is untouched.
|
|
const read = (at: number, key: string): ThreadItem => ({
|
|
kind: "noticed",
|
|
at,
|
|
text: `read ${at}`,
|
|
fix: "f",
|
|
key,
|
|
});
|
|
let t: ThreadItem[] = [];
|
|
t = pushPageRead(t, read(1, "/a|read"));
|
|
t = pushItem(t, { kind: "noticed", at: 2, text: "slow", fix: "f", key: "/a|notice|slow" });
|
|
t = pushItem(t, { kind: "you", at: 3, text: "hello" });
|
|
t = pushPageRead(t, read(4, "/b|read"));
|
|
assert.deepEqual(
|
|
t.map((i) => i.at),
|
|
[2, 3, 4],
|
|
"the earlier read goes; a measured notice and your words stay",
|
|
);
|
|
assert.ok(
|
|
isPageReadKey("/b|read") && !isPageReadKey("/a|notice|slow") && !isPageReadKey(undefined),
|
|
);
|
|
}
|
|
|
|
// ---- the thread as model history ----
|
|
{
|
|
const t: ThreadItem[] = [
|
|
{ kind: "you", at: 1, text: "Change the title" },
|
|
{ kind: "loki", at: 2, text: "Try “Your pharmacy”." },
|
|
{ kind: "sent", at: 3, text: "Use “Your pharmacy” as the title", owner: true },
|
|
{ kind: "you", at: 4, text: "and the footer too" },
|
|
];
|
|
const h = historyFor(t, 3, 20);
|
|
assert.deepEqual(
|
|
h.map((x) => x.role),
|
|
["assistant", "assistant", "user"],
|
|
"the last N turns",
|
|
);
|
|
assert.ok(h[1].content.startsWith("(Sent to the bu"), "a receipt is said back as Loki's");
|
|
assert.ok(
|
|
h.every((x) => x.content.length <= 20),
|
|
"each turn is capped",
|
|
);
|
|
}
|
|
|
|
// ---- the widget clamps to what the chat route accepts ----
|
|
assert.equal(CHAT_MAX_MESSAGE, WIDGET_CHAT_MAX_MESSAGE, "chat message cap mirrors the route");
|
|
assert.equal(CHAT_MAX_HISTORY, WIDGET_CHAT_MAX_HISTORY, "chat history cap mirrors the route");
|
|
|
|
console.log("widget-thread: ok");
|