Files
loki/scripts/test/widget-continue.ts
T
CatoandClaude Fable 5.1 5bc05bbef4 feat: the first day is the whole product — three ways in, /build, your website in onboarding, Loki on it, help when a build stalls, and the owner's phone fixes (#1110)
* feat(widget): the owner's way into Loki — Continue in Loki, an advisor that knows whose site it is, a header that fits a phone

On 2026-10-09 the owner of heidi.orangecat.ch opened the widget on their
phone and asked for one new page. The widget treated them as a stranger:
it told them which heading to insert and to "keep the structure in a data
file (JSON/YAML)", wrote five invented sentences in the site's own language,
and answered "switch to Loki so I can develop it there with either chat or
terminal" with the same advice a third time. The owner's sign-in lived only
behind "Watch". Once signed in, Review + Stop watching pushed ✕ off the
right edge of the screen.

- widget/continue.ts + src/lib/widget/continue.ts: the thread as one
  message (newest kept, ≤1800 chars) and where it lands in Loki.
- GET /api/widget/continue: signs the person in if needed, checks
  getProjectAccess on the widget's project, resolves the project key and
  redirects into /loki?project=&q= or /terminal?project=&view=terminal;
  a stranger goes back to the site with #loki-owner-denied.
- widget/conversation.ts: "Continue in Loki · Chat → · Terminal →" under
  the composer for the owner; one sign-in link for anyone else after their
  first message; "This is my site — sign in with Loki →" under Loki's
  greeting, on the first screen. The owner pass travels with each question.
- /api/widget/advise + advisor.ts: the pass is verified as ingest verifies
  it; with it the advisor knows the person OWNS the site and Build this
  builds. For everyone, four hands-off rules: never explain how to edit the
  site, never write its content, a build request becomes the one change to
  tap, and "take this into Loki" is answered with where that control is.
- widget/theme.ts: the header's text column shrinks before the actions do;
  Stop watching stays on one line; the continue row wraps on a phone.
- widget/main.ts: the owner is not offered to hide their own tool.
- scripts/test/widget-continue.ts (new), widget-advise.ts: pinned.
- docs/architecture/feedback-widget.md: the row and the route.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01N1Yw4cCwXWRP2S5Hj62u2Z

* feat(widget): the loop closes on the site — Your changes, a live announcement, an honest Follow link

"On it — Loki tells you when" was the end of the widget's story, and on the
site Loki never told anyone: the telling went to push and Telegram, the page
the owner came back to said nothing, and "Follow the build" toured the
unchanged page with "let me show you what changed".

- widget/changes.ts: "Your changes" above the thread for the owner — the
  project's recent changes with one word each (Building · On its way · Live ·
  Needs you …), See it → once live, Open in Loki → when something needs them,
  asked for on arrival and every 30 s while something moves. A change this
  browser saw building that is now live is announced once: in the thread when
  the panel is open, beside the launcher (bubble + unread) when it is not.
- POST /api/widget/owner/changes: the one read with the owner pass. Same
  attachFeedbackWork the inbox uses, then src/lib/feedback/owner-view.ts
  writes the owner's words; the work view never leaves the server. No valid
  pass → no rows.
- /feedback/<id>/watch-fix lands on the inbox row while the change is still
  being built; the walkthrough only once it is live.
- The owner's repeated note keeps its follow link.
- widget/continue-row.ts, widget/greeting.ts: split out of createConversation
  (the lint rule caps it at 510 lines; CI caught it at 543).
- scripts/test/widget-changes.ts (new); widget-continue.ts updated.
- docs/architecture/feedback-widget.md.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01N1Yw4cCwXWRP2S5Hj62u2Z

* fix(widget): the owner's door is not an .act — the embed test counts those

widget-embed-browser asserts exactly one "as written" link (.act) under the
visitor's last message; the sign-in door under the greeting shared the class
and made it two. Its own class, same look.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01N1Yw4cCwXWRP2S5Hj62u2Z

* feat(public): three ways in — build it yourself, with a partner, or let the studio — and every stranger's door ends at /build

The ways to get something built were scattered: the landing page offered
only "Start a project", hiring the studio was a word in the nav, the partner
directory was reachable from OrangeCat's careers page and nowhere here, and
a stranger who watched a fix land on someone else's site was sent to a bare
homepage. "Start a project" at the bottom of six marketing pages went to
sign-IN, and so did a stranger arriving with a brief.

- src/config/build-paths.ts: the one list — yourself (sign-up), with a
  partner (bitbaum.orangecat.ch/partners/), the studio (the commission
  config's hire address) — plus buildPageHref/fromHost.
- /build (public, allowlisted): the invitation, naming the site the person
  came from (validated host only) and the three doors (BuildPaths cards).
- Landing: the "Three ways in" section after "Who it is for"; the closing
  line and FinalCta name the other two ways; FinalCta → sign-up.
- /change and /take send a signed-out person to sign-up with the brief kept.
- Widget: "Made with Loki — build something of your own →" as the last line
  for anyone who is not the owner (/build?from=<host>); the tour's "Get this
  for your site" lands on /build naming the site; the concierge gets the
  same door (appended last — the test addresses the others by index).
- scripts/test/build-paths.ts pins all of it.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01N1Yw4cCwXWRP2S5Hj62u2Z

* fix(widget): /api/widget/continue is public — it signs the person in itself

Live, the merged route answered a bare 401: the session middleware caught it
before the route could send the person to sign-in. Same list entry as
/api/widget/owner, pinned by widget-continue.ts.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01N1Yw4cCwXWRP2S5Hj62u2Z

* test(audit): /build is measured by the responsive audit

A new page has to be a decision — measured, or excluded with a reason.
Measured: it is a public page a stranger lands on from a phone.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01N1Yw4cCwXWRP2S5Hj62u2Z

* fix(widget): yours means yours, a review that picks three, the composer above the keyboard

From the owner's phone on 2026-10-09:
- "Your changes" listed a stranger's "so nice picture!" as the owner's own
  request. The strip now shows what the owner asked for and what Loki filed
  itself; visitors' notes are counted ("3 notes from visitors, in Loki →").
- Review answered with five nits (alt text, a renamed button, a table) and
  five identical buttons. The rubric now asks for at most three changes a
  visitor would notice or that fix something broken, and says none is fine.
- With the keyboard open the box being typed into sat half behind it; it
  scrolls into view once the keyboard has settled.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01N1Yw4cCwXWRP2S5Hj62u2Z

* feat: the first day is the whole product — your website in onboarding, Loki on it, help doors when a build stalls, the terminal hand-off with its text, a launcher that stays in the corner

From the owner's phone on 2026-10-09, and the three follow-ups named then:

- Onboarding asks for the website (optional). A project created with one
  (`liveUrl` on POST /api/user-projects) gets its widget token at once, so
  the token's origin is the site's; the new step "Put Loki on your site"
  shows the one line to paste, lets an agent add it when there is a repo,
  and opens the site with the owner pass (`openSiteUrl` on the token
  route). Finishing lands on the project, not Control. A refused project
  no longer advances the step in silence.
- A stalled build and a failed kickoff offer the other two ways to get it
  built — find a partner, hire the studio — from the same list as the
  landing page (GetHelpLine), the studio with the brief.
- "Continue in Loki → Terminal" carries the thread: /terminal?q= prefills
  the rail's composer as an unsent draft, and opens the rail sheet on a
  phone (the same path ?explain=1 takes).
- The launcher stays in the corner: a near slot over plain text beats a
  free slot mid-page (heidi.orangecat.ch: the site's own bar took one
  corner and the launcher floated beside a heading). A near sheet still
  sends it climbing.
- Page checks: a small-caps label by design is not a defect, 11.84px is
  not under 12, and one kind of finding is one card (the signature drops
  counts and examples; "30 pieces" then "6 pieces" were two cards).

Tests: onboarding-site-step.ts (new), widget-placement.ts, widget-watch.ts,
widget-continue.ts, build-paths.ts; all widget browser suites pass locally.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01N1Yw4cCwXWRP2S5Hj62u2Z

* refactor(widget): keepInViewOnFocus lives in dom.ts — createConversation back under the 510-line cap after the merge

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01N1Yw4cCwXWRP2S5Hj62u2Z

* fix(widget): import keepInViewOnFocus — the panel threw at mount and boot swallowed it

An unimported identifier compiles fine with esbuild and fails at run time;
boot's catch hid it, so the launcher rendered and the panel never opened.
Caught by widget-embed-browser.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01N1Yw4cCwXWRP2S5Hj62u2Z

---------

Co-authored-by: Claude <noreply@anthropic.com>
2026-10-09 05:10:30 -07:00

209 lines
8.2 KiB
TypeScript

// "Continue in Loki": the widget's conversation can be carried into Loki's
// chat on the project (the thread in the composer) or the project's terminal.
// The owner asked for exactly this on 2026-10-09 — "switch to Loki so I can
// develop it there with either chat or terminal interface" — and got the same
// advice a third time. Pinned here: the hand-off keeps what they said last
// when it has to cut, never exceeds the address-bar budget, lands on the
// right Loki surface, and the widget never sends the pass or the project
// name anywhere but through Loki's own signed-in route.
// Run: npx tsx scripts/test/widget-continue.ts
import assert from "node:assert/strict";
import { readFileSync } from "node:fs";
import { continueUrl, HANDOFF_MAX, handoffText } from "../../widget/continue";
import type { ThreadItem } from "../../widget/thread";
import { CONTINUE_TEXT_MAX, CONTINUE_VIEWS, continueHref } from "@/lib/widget/continue";
const page = {
title: "Im Treppehuus — Wo Sie s bruuched · Heidi",
url: "https://heidi.orangecat.ch/gsw/situations/neighbours",
};
// ---- the thread as one message ----
{
const thread: ThreadItem[] = [
{ kind: "you", at: 1, text: "I want you to add a situation for the word Bünzli." },
{ kind: "loki", at: 2, text: "Here is what I'd build.", changes: ["Add a Bünzli scene"] },
{ kind: "sent", at: 3, text: "Add a Bünzli scene", owner: true, building: true },
{ kind: "noticed", at: 4, text: "This page took 3.6s to load.", fix: "Make it faster" },
];
const text = handoffText(thread, page);
assert.ok(text.startsWith(`From Loki on “${page.title}” (${page.url}):`), "the page leads");
assert.ok(
text.includes("Me: I want you to add a situation for the word Bünzli."),
"their words, in full",
);
assert.ok(text.includes("Loki: Here is what I'd build."));
assert.ok(
text.includes("Sent to the builder: Add a Bünzli scene"),
"receipts travel as one line",
);
assert.ok(text.includes("Loki noticed: This page took 3.6s to load."));
assert.ok(text.length <= HANDOFF_MAX);
}
{
// Over budget: the oldest turns go first, the newest stay whole.
const thread: ThreadItem[] = Array.from({ length: 30 }, (_, i) => ({
kind: "you" as const,
at: i,
text: `turn ${i} ${"x".repeat(120)}`,
}));
const text = handoffText(thread, page);
assert.ok(text.length <= HANDOFF_MAX, "never over the address-bar budget");
assert.ok(text.includes("turn 29 "), "what they said last is kept");
assert.ok(!text.includes("Me: turn 0 "), "the oldest is dropped first");
}
{
// Loki's answers are clipped; the person's own words are not.
const long = "a".repeat(1000);
const text = handoffText([{ kind: "loki", at: 1, text: long }], page);
assert.ok(text.length < 450, "an answer is clipped to a line");
assert.ok(text.endsWith("…"));
const mine = handoffText([{ kind: "you", at: 1, text: long }], page);
assert.ok(mine.includes(long), "the owner's own words are never cut");
}
{
const empty = handoffText([], { title: " ", url: "https://x.test/p" });
assert.equal(
empty,
"From Loki on “https://x.test/p” (https://x.test/p):",
"no title: the url stands in",
);
}
// ---- the link out ----
{
const u = new URL(
continueUrl({
apiBase: "https://loki.orangecat.ch",
token: "fcw_abc",
view: "chat",
text: "Me: hello",
here: "https://heidi.orangecat.ch/gsw/situations/neighbours#loki",
}),
);
assert.equal(u.origin + u.pathname, "https://loki.orangecat.ch/api/widget/continue");
assert.equal(u.searchParams.get("token"), "fcw_abc");
assert.equal(u.searchParams.get("view"), "chat");
assert.equal(u.searchParams.get("q"), "Me: hello");
assert.equal(
u.searchParams.get("return"),
"https://heidi.orangecat.ch/gsw/situations/neighbours",
"the fragment never travels",
);
const term = new URL(
continueUrl({
apiBase: "https://loki.orangecat.ch",
token: "fcw_abc",
view: "terminal",
text: "Me: hi",
here: "https://h.test/",
}),
);
assert.equal(term.searchParams.get("view"), "terminal");
assert.equal(
term.searchParams.get("q"),
"Me: hi",
"the terminal's rail composer takes the thread too",
);
}
// ---- where it lands, inside Loki ----
assert.deepEqual([...CONTINUE_VIEWS], ["chat", "terminal"]);
assert.ok(CONTINUE_TEXT_MAX >= HANDOFF_MAX, "the route accepts what the widget sends");
assert.equal(continueHref("Heidi", "chat", "Me: hello"), "/loki?project=Heidi&q=Me%3A%20hello");
assert.equal(continueHref("Heidi", "chat", " "), "/loki?project=Heidi", "nothing to prefill");
assert.equal(
continueHref("Heidi", "terminal", "Me: hello"),
"/terminal?project=Heidi&view=terminal&q=Me%3A%20hello",
);
const termPage = readFileSync("src/components/terminal/TerminalPageClient.tsx", "utf8");
assert.match(termPage, /searchParams\.get\("q"\)/, "/terminal reads ?q= into the rail's composer");
const rail = readFileSync("src/components/terminal/TerminalLokiRail.tsx", "utf8");
assert.match(
rail,
/initialDraft \? \{ text: initialDraft, nonce: 1 \} : null/,
"as a draft, unsent",
);
assert.equal(continueHref("my project", "chat", null), "/loki?project=my%20project");
// ---- the route is reachable: it signs the person in itself ----
// Observed live on 2026-10-09: the merged route answered a bare 401 because
// the session middleware caught it first, so "Continue in Loki" was a wall.
const proxy = readFileSync("src/proxy.ts", "utf8");
assert.match(
proxy,
/\|api\/widget\/continue\|/,
"the route is on the public list and guards itself",
);
assert.match(
proxy,
/\|api\/widget\/owner\|/,
"so is /api/widget/owner, which covers owner/changes",
);
// ---- the route is the only place the project is resolved ----
const route = readFileSync("src/app/api/widget/continue/route.ts", "utf8");
assert.match(route, /getApiUserId\(\)/, "signed in with Loki, or sent to sign in");
assert.match(route, /callbackUrl/, "and brought back afterwards");
assert.match(
route,
/getProjectAccess\(userId, token\.projectId\)/,
"only someone who may work on the project",
);
assert.match(route, /ownerReturnUrl\(/, "a stranger goes back to the site they came from");
assert.match(route, /OWNER_DENIED_HASH/, "— with a word about why");
assert.match(route, /getWidgetProjectKey\(/, "the token becomes a project here, not in the widget");
assert.match(route, /continueHref\(key, view, text\)/);
// ---- the widget side ----
const row = readFileSync("widget/continue-row.ts", "utf8");
assert.match(row, /"Continue in Loki"/, "the row exists");
assert.match(row, /"Chat →"/);
assert.match(row, /"Terminal →"/);
assert.match(
row,
/This is my site — continue in Loki →/,
"a visitor has one door, which signs them in",
);
const greeting = readFileSync("widget/greeting.ts", "utf8");
assert.match(
greeting,
/This is my site — sign in with Loki →/,
"the owner's door is under the greeting",
);
const convo = readFileSync("widget/conversation.ts", "utf8");
assert.match(convo, /createContinueRow\(/, "and the conversation renders the row");
assert.match(convo, /ownerDoor\(/, "and the door");
assert.match(
convo,
/ownerPass: opts\.ownerPass\(\) \?\? undefined/,
"the owner is the owner to the advisor too",
);
const api = readFileSync("widget/loki-api.ts", "utf8");
assert.match(api, /ownerPass: opts\.ownerPass/, "the pass travels with the question");
const advise = readFileSync("src/app/api/widget/advise/route.ts", "utf8");
assert.match(advise, /ownerPass: z\.string\(\)/, "and the route accepts it");
assert.match(advise, /verifyOwnerPass\(data\.ownerPass\)/, "verified, never trusted");
assert.match(
advise,
/pass\.projectId === token\.projectId && pass\.userId === token\.userId/,
"for this project and this owner",
);
// ---- the phone ----
const theme = readFileSync("widget/theme.ts", "utf8");
assert.match(
theme,
/\.hdr > div:first-child \{ min-width: 0; flex: 1 1 auto; \}/,
"the header's text column shrinks before ✕ leaves the screen",
);
assert.match(theme, /\.watchbtn \{[^\n]*white-space: nowrap/, "Stop watching stays one line");
const main = readFileSync("widget/main.ts", "utf8");
assert.match(
main,
/hideLink\.style\.display = ownerPass \? "none" : ""/,
"the owner is not offered to hide their own tool",
);
console.log("widget-continue: ok");