Files
loki/widget/surface-modes.ts
CatoandClaude Opus 5.5 6be6045295 feat(widget): one conversation with Loki; Watch in the header (#1092)
* feat(widget): one conversation with Loki; Watch is in the header for anyone who owns the site

The panel was three tools sharing a box: Request a change (a form), Ask Loki
(an advisor) and Chat, behind tabs and scope chips, each with a history that
vanished on close - and Watch could only be switched on by arriving through
one link on the project page in Loki, so an owner simply visiting their site
never saw it.

Now:
- One thread (widget/conversation.ts). Type, speak, paste a screenshot or
  point at an element; Loki answers about this page (one tap: whole site).
  Every suggested change, and your own message as written, has Send to
  builder / Build this, which opens a confirmation card in the thread; the
  receipt (building / track link) lands in the thread.
- The thread is kept per tab (widget/thread.ts, validated on restore), so it
  survives the site's own page loads.
- Watch in the header: Review for the owner; for anyone else an explainer
  with "This is my site - sign in with Loki", via GET /api/widget/owner,
  which checks ownership of the widget's project and returns to the same
  page with a pass. The return URL must be on the project's own site
  (ownerReturnUrl), so the route cannot hand a pass to someone else's page;
  a non-owner lands back with a plain explanation.
- data-fc-modes now only picks who answers: advisor (default), the studio
  concierge (chat), or none (report). The owner always gets the advisor.
- widget/loki-api.ts holds the two calls; advise.ts and chat.ts are gone.

Tests: widget-thread (new), owner-note-builds (return-URL guard),
widget-embed-browser 41/41, widget-watch-browser 40/40,
widget-host-avoid-browser 24/24.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* test(widget): style guard follows the panel's real controls; comment no longer reads as an import

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-08 08:02:54 -07:00

37 lines
1.9 KiB
TypeScript

/**
* `data-fc-modes` — what an embed lets the widget's one conversation do.
*
* There are no tabs any more (widget/conversation.ts): one thread, in which
* you talk to Loki and anything worth building is sent to the builder. The
* attribute now only decides WHO answers there (widget/thread.ts assistantFor):
* report — no AI: messages go straight to the builder, after a confirmation.
* ask — the site advisor answers about this page or site (the default).
* chat — a studio front desk: the Cat and Loki answer from the fleet map.
* watch — accepted and ignored: Watch is the OWNER's, switched on from the
* panel header (widget/watch.ts, src/app/api/widget/owner), never
* by an embed, and visitors are never watched.
* The owner always gets the advisor, whatever the embed lists.
*
* Keep this file free of DOM so Node tests can import it.
*/
export const WIDGET_SURFACE_MODES = ["report", "ask", "chat", "watch"] as const;
export type WidgetSurfaceMode = (typeof WIDGET_SURFACE_MODES)[number];
/** What an embed with no `data-fc-modes` gets: request a change, or ask first. */
export const DEFAULT_WIDGET_SURFACE_MODES: WidgetSurfaceMode[] = ["report", "ask"];
/**
* The embed's `data-fc-modes`, in the order written. Unknown names and repeats
* are dropped; an absent or wholly invalid attribute gets the default pair.
* `data-fc-modes="report"` is how a site opts out of AI answers.
*/
export function parseWidgetSurfaceModes(raw: string | null | undefined): WidgetSurfaceMode[] {
if (!raw || !raw.trim()) return [...DEFAULT_WIDGET_SURFACE_MODES];
const out: WidgetSurfaceMode[] = [];
for (const name of raw.split(",").map((s) => s.trim().toLowerCase())) {
const mode = WIDGET_SURFACE_MODES.find((m) => m === name);
if (mode && !out.includes(mode)) out.push(mode);
}
return out.length ? out : [...DEFAULT_WIDGET_SURFACE_MODES];
}