/** * Using the page in the Studio's preview: the assistant's `interact_page`. * * The assistant changes a site and then has to find out whether the change * works, and a page that only renders right is not yet a page that works: a * form that does not submit, a menu that does not open, a layout that breaks * at phone width all look fine from `read_page`. So the page can be clicked, * typed into and keyed at -- by the model, through the Studio -- and every * answer is the page AFTER the action took effect: it waits for the page to * settle (see {@link waitForPageSettle}) and then reads it again. * * Plain DOM and in `shared` for the reason `pageInspection.ts` is: the canvas * bridge (`@valbuild/next`, `@valbuild/tanstack`) runs it inside the page, * the only place the page's elements can be reached, and both bindings run * this one copy. Only what {@link interactWithPage} returns crosses the frame * (`valCanvasProtocol.ts`). * * ## What it asks first, and what it will not do * * The preview is the real site, talking to its real backend, so: * * - **Every click, and every Enter or Space, asks the person first.** Before * anything is dispatched to the page, the answer is `needs-approval`, * naming the element (its label or text, its role, the page); the Studio * asks in its own words, and Allow sends the same action back with the * element's FINGERPRINT (`approved`). If the element is no longer the one * that was shown -- a re-render, a navigation, a click the person made * meanwhile -- the page asks again rather than act. An approved action * runs as the person's own: nothing it sends to this site is held. * - **While the actions that do not ask run** -- typing, other keys, the * settle after opening a page or changing the width -- requests that send * data are held until the page settles (`pageRequestGate.ts`) and shown to * the person, and Allow sends exactly those (the `release` action). * - **Links and forms that go to another site are refused.** A link to * another origin or into another window is refused before anything is * asked; sends of data to another site (POST, PUT, PATCH, DELETE, forms, * beacons), and a form that would leave it, are refused when the page * tries. (A script navigation or a redirect can still leave the site; * the Studio then says no page arrived.) * - **It never types into a password field.** Whatever the model was told. * * Not covered: what a page sends while it loads after `open` or any * navigation (nothing of Val's runs in a document before it does), what it * sends after it settled (an autosave on a timer), requests through a * `fetch` or XHR captured before the action and XHRs opened before it, * GET, HEAD and OPTIONS requests -- to other sites too -- and image or * script loads, WebSockets, workers, embedded frames, and script * navigations and redirects, which can still leave the site. * * ## The events are synthetic * * They are dispatched, not typed by a person, so `isTrusted` is false and the * browser does none of what it does for a real key: no text is inserted by a * key event, Tab moves no focus, Enter submits nothing. What a real key would * have done is done here instead, after the page had its chance to cancel the * `keydown` -- see {@link pressKey}. A page that ignores untrusted events * (rare) does not react, and the answer says what was done, not what the * page did with it. */ import type { PageLog, PageLogEntry } from "./pageLog.js"; import { type PageRequest } from "./pageRequestGate.js"; import { type PageSnapshot } from "./pageInspection.js"; /** * The keys `press` knows. Each one either reaches the page's own listeners * (Escape, the arrows) or is also done the way the browser would (Enter, * Space, Tab): see {@link pressKey}. Enter and Space ask the person first, * as a click does: {@link ASKING_KEYS}. */ export declare const PAGE_KEYS: readonly ["Enter", "Space", "Escape", "Tab", "Shift+Tab", "ArrowUp", "ArrowDown", "ArrowLeft", "ArrowRight"]; export type PageKey = (typeof PAGE_KEYS)[number]; /** The keys that activate what has the focus, as a click does: asked about. */ export declare const ASKING_KEYS: ReadonlySet; /** * What the page itself can be asked to do. Each `kind` is a capability of * its own (`interact:` in `valCanvasProtocol.ts`), so a new one is one * more member here and one more name a bridge lists. * * Opening another page and changing the width are not here: the Studio does * those to the frame (its `src`, its width) and then asks the page to * `settle`. */ export type PageAction = /** * Nothing: only wait for the page to settle and read it. What follows a * navigation or a change of width. `width`: also wait until the page is * this wide -- the frame it was resized to. */ { kind: "settle"; width?: number; } /** A click on the element, as a pointer would: down, up, click. */ | { kind: "click"; selector: string; } /** * Text into an input, a textarea or something editable: the focused * element, or `selector`'s. Replaces what is there unless `append`. */ | { kind: "type"; text: string; selector?: string; append?: boolean; } /** A key, on the focused element or `selector`'s. */ | { kind: "press"; key: PageKey; selector?: string; } /** * What the person decided about the requests an action held * (`needs-approval`): send exactly those (`allow`), or refuse them. Then * the page settles and is read, as after any action -- and anything new * it tries to send is held again. */ | { kind: "release"; hold: string; allow: boolean; }; export type PageActionKind = PageAction["kind"]; /** Every kind of {@link PageAction}: what a bridge of this release answers. */ export declare const PAGE_ACTION_KINDS: readonly PageActionKind[]; /** The longest text `type` takes. */ export declare const PAGE_TYPE_MAX_LENGTH = 5000; /** Whether `value` is a {@link PageAction} this release knows. */ export declare function isPageAction(value: unknown): value is PageAction; /** * The one bound on an interaction: how long, after the action, the page is * given to settle -- and, when the action took it to a new document, for * that document to arrive and settle. The Studio hands what is left of it to * the page, so the two halves cannot wait twice. */ export declare const INTERACT_SETTLE_TIMEOUT_MS = 15000; /** * How long the page must be quiet -- no change to the DOM, no request in * flight, no resource finishing -- to count as settled. Long enough for a * transition and the render after a response; short enough not to be felt. */ export declare const INTERACT_QUIET_MS = 500; /** Where the page is: what an interaction says changed. */ export type PageState = { /** Its path, query and hash, without Val's own parameters. */ url: string; /** Absent when it is not known: before a page the Studio opened. */ title?: string; }; export type { PageRequest } from "./pageRequestGate.js"; export { BODY_EXCERPT_LENGTH, MAX_HELD_REQUESTS } from "./pageRequestGate.js"; /** * A click, or Enter or Space, the page will do once the person allows it -- * nothing has been dispatched yet. Every string in it is the page's: the * Studio checks it before it shows any of it. */ export type ActionApproval = { kind: "action"; /** * The element as it was shown: what Allow sends back (`approved`), and * what the page checks it against before it acts. */ fingerprint: string; action: "click" | "press"; /** For `press`: the key. */ key?: PageKey; /** The page it is on. */ page: string; /** What the element says: its accessible label or its text, shortened. */ label: string; /** What it is: its role (`button`, `link`) or its tag. */ role: string; tag: string; /** * Enter here submits a form: what the form's submit button says, or "" * when it has none with a label. Absent when Enter submits nothing. */ submits?: string; before: PageState; }; /** * Requests an action that did not ask made the page try, held unsent until * the person says (`pageRequestGate.ts`). Every string in it is the page's. */ export type RequestsApproval = { kind: "requests"; /** What to send back with the person's answer (`release`). */ hold: string; /** The page they were tried on. */ page: string; /** What was acted on, shortened: the page's words. */ label: string; /** What was done. */ did: string; before: PageState; /** What the page tried to send, as the browser resolved it. */ requests: PageRequest[]; }; /** What the person is asked about: an action, or what an action sent. */ export type PageApproval = ActionApproval | RequestsApproval; /** Why the page did not settle in time: what was still going on. */ export type PageBusy = "loading" | "requests" | "changing" | "width"; /** An interaction, done: the page as it is now. */ export type PageInteraction = { /** What was done, in words: `clicked