Files
loki/widget/send-report.ts
CatoandClaude Opus 5.5 edb1249552 feat(watch): Review: Loki judges what you just did on your site (#1081)
* feat(watch): Review — Loki judges what you just did on your site and suggests what to improve

Watch mode only acted when something broke. It now also notices what a
demanding reviewer would (4xx, slow requests, console errors, slow loads,
main-thread freezes, layout shift, pauses between steps) plus measured page
checks (unnamed controls, unlabeled fields, tiny tap targets, missing alt,
broken images, sideways scroll, h1/lang). The pill counts them; Review sends
the session to Ask, which judges it through errors, design, engineering,
process and product - every point cited from the session - and each change
is one tap from a request that carries the watched steps. The trail now
survives page loads within a tab, so a multi-page site reads as one visit.

No model runs until Review is pressed. The report POST is now one helper
(widget/send-report.ts) shared by Send and the automatic fix.

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

* fix(watch): Review never blames the site for Loki's own work; plain-text answers

Found by looking at the 390px render, not by the tests:
- opening Loki's own Review panel scored a 0.27 layout shift against the
  site (Chrome reports shadow-DOM sources with a null node) - only shifts
  pinned on a site node count now;
- the widget's own boot registered as 'the page froze 369ms' - a freeze now
  counts only when it overlaps the second after a tap on the site;
- the review asked for **bold** lenses and the widget shows text, so clients
  saw literal asterisks - plain-text rubric + plainAnswer() on every reply;
- '404 in 0.0s' - duration only when slow.

The browser test now proves the detectors fire on real jank (a Load more
that freezes and pushes content down) AND stay quiet for Loki's own panel.
The 'typed values never leave the page' checks were vacuous (fill() never
taps the field) - they tap the filled field now and fail under mutation.
Docs: the architecture table described Watch as 'still a seam'.

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

---------

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

52 lines
1.8 KiB
TypeScript

/**
* The one POST to Loki's feedback ingest. The panel's Send and Watch's
* automatic fix both file through it, so the clamps and the error reading
* exist once.
*
* Every field is clamped to the server's own cap (api/feedback/route.ts
* FeedbackBody — scripts/test/widget-server-caps-mirror.ts compares them). The
* ingest rejects the WHOLE submission with a bare "Invalid submission" naming
* no field, so one character over a cap silently loses someone's report.
*/
import type { SelectedEl } from "./picker";
export type ReportFields = {
token: string;
suggestion: string;
contact?: string;
scope: "element" | "page" | "site";
screenshots?: string[];
selectedElements?: SelectedEl[];
ownerPass?: string;
};
export type ReportAnswer = {
claimUrl?: string;
owner?: boolean;
building?: boolean;
buildNote?: string;
};
/** File a report about the page the person is on. Throws the server's own
* words when it refuses, so the panel can show them. */
export async function sendReport(apiBase: string, fields: ReportFields): Promise<ReportAnswer> {
const res = await fetch(`${apiBase}/api/feedback`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
...fields,
contact: fields.contact?.trim().slice(0, 200) || undefined,
page: location.pathname.slice(0, 300),
url: location.href.slice(0, 1000),
pageTitle: document.title.slice(0, 300) || undefined,
screenshots: fields.screenshots?.length ? fields.screenshots : undefined,
selectedElements: fields.selectedElements?.length ? fields.selectedElements : undefined,
}),
});
if (!res.ok) {
const body = (await res.json().catch(() => null)) as { error?: string } | null;
throw new Error(body?.error ?? `Request failed (${res.status})`);
}
return (await res.json()) as ReportAnswer;
}