The fastest browser for AI agents to run browser automation, built for sharing your logged-in browser state with your AI agents, like Codex or Claude Code, without disturbing you. Zero cost, zero config.
JavaScript agent-skillsai-agentautomationbrowserbrowser-automationclaude-codecodexhermes-agentskillsskills-sh
import { mkdir, writeFile } from "node:fs/promises";
import { dirname, isAbsolute } from "node:path";
import {
browserCdp,
browserEgo,
drainPageEvents,
ensureNetworkTracking,
ensureSession,
ensureFrameSessions,
responseTimeoutMs,
invalidateSession,
isCdpRequestTimeoutError,
isPageUnresponsiveError,
PageUnresponsiveError,
isBrowserRuntime,
isPageDialogOpenedError,
isSessionLostError,
networkActivity,
pageNetworkSessions,
pendingDialog,
prepareFileChooser,
setPreferredTarget,
subscribeBrowserEvents,
subscribePageEvents,
type FileChooserInterception,
type FileChooserOpenedEvent,
} from "./browser-runtime.js";
import { assertNoSessionIdInParams, runtimeValue } from "./cdp-eval.js";
import { withTemporaryClipboardText } from "./clipboard.js";
import {
captureScreenshotForSession,
snapshotRaw,
type CaptureScreenshotOptions,
type SnapshotOptions,
} from "./driver/observe.js";
import {
clickPointInPage,
clickInPage,
dragAndDropInPage,
fillInPage,
focusInPage,
hoverInPage,
mouseButtonInPage,
mouseButtonMask,
moveMouseInPage,
selectOptionInPage,
wheelInPage,
type MouseButton,
type PageClickOptions,
type PageDragAndDropOptions,
type PageFillOptions,
type PageHoverOptions,
type PageMouseButtonOptions,
type PageMouseClickOptions,
type PageMouseMoveOptions,
type PageMouseWheelOptions,
type PageSelectOption,
} from "./driver/page-actions.js";
import {
normalizeFilePaths,
setFilesOnBackendNode,
setInputFilesInPage,
} from "./driver/page-input.js";
import {
preparePageDownload,
type DownloadArtifact,
type DownloadInterception,
} from "./driver/downloads.js";
import {
PageKeyboardController,
type PageClipboardContent,
type PageKeyboardPressOptions,
type PageKeyboardTypeOptions,
} from "./driver/page-keyboard.js";
import {
isTransientElementError,
navigateInPage,
reloadInPage,
waitForLoadStateInPage,
waitForSelectorInPage,
waitForURLInPage,
type PageGotoWaitUntil,
type PageURLMatcher,
type PageWaitForLoadStateOptions,
type PageWaitForSelectorOptions,
type PageWaitForURLOptions,
} from "./driver/page-waits.js";
import { ElementResolutionError } from "./element-resolver.js";
import { invokeEgo, probeAgentControl } from "./ego-errors.js";
import {
withPage as defaultWithPage,
withSpace as defaultWithSpace,
type PageExecutionContext,
type WithPageOptions,
} from "./native-gate.js";
import {
PageLedgerStore,
runtimeInstanceId,
type ManagedPage,
type PageLedger,
type PageOrigin,
} from "./page-ledger.js";
import {
PageRefRegistry,
pageRefDocumentId,
type PageRefState,
} from "./page-ref-registry.js";
import {
preparePageSnapshotResult,
rewriteSnapshotRefIds,
} from "./snapshot-result.js";
import {
clearSpacePageNotices,
forgetPageNotice,
markPageObserved,
peekUnhandledPageNotices,
refreshUnhandledPageNotice,
recordUnhandledPage,
subscribeUnhandledPageNotices,
type UnhandledPageNotice,
} from "./page-discovery.js";
import { bufferOutput } from "./output-sink.js";
import {
publicApiEntry,
validatePublicApiOptions,
} from "./public-api-schema.js";
import { parseRef, type RefMap } from "./ref-map.js";
import { state } from "./state.js";
type TaskSpaceDescriptor = {
taskId?: string | number;
id: number;
name: string;
createdBy?: string;
ownership?: string;
recentTabTitles?: string[];
};
/**
* An action failed on element state after resolution (hidden, moving, covered)
* and may simply be attempted again. Transport and session errors are not
* included: input may already have reached the page.
*/
function isRetryableElementStateError(
error: unknown,
): error is ElementResolutionError {
return (
error instanceof ElementResolutionError &&
error.kind === "transient" &&
!error.message.startsWith("Unknown ref:")
);
}
/**
* Resolution or frame discovery failed before any input was dispatched. On top
* of the element-state cases above, a frame that vanished or an iframe session
* that was lost is recovered by rediscovering frames; losing the Page's own
* session is terminal.
*/
function isRetryableResolutionError(
error: unknown,
pageSessionId: string,
): error is Error {
if (isTransientElementError(error)) {
return !error.message.startsWith("Unknown ref:");
}
return (
isSessionLostError(error) &&
typeof error.sessionId === "string" &&
error.sessionId !== pageSessionId
);
}
/** A resolution failure that found nothing to act on, as opposed to a blocked target. */
function isNoMatchResolutionError(error: unknown): error is Error {
return (
error instanceof ElementResolutionError &&
error.kind === "transient" &&
/matched 0 elements/.test(error.message)
);
}
type PopupReceiptEntry = { label: string; targetId: string };
type PopupWatch = {
collect(sessionId: string): Promise<PopupReceiptEntry[]>;
dispose(): void;
};
type DomActivityProbe = {
readyState: string;
visible: boolean;
idleMs: number;
framesLoading: boolean;
};
// Installs one MutationObserver per document on first use and reports the
// time since the last observed mutation (zero right after installation). The
// retry loop separately requires a full idle window since the action started.
const DOM_ACTIVITY_PROBE = `(() => {
const key = "__egoBrowserDomActivity";
let tracker = window[key];
if (!tracker) {
tracker = { last: performance.now(), observer: null };
try {
const observer = new MutationObserver(() => {
tracker.last = performance.now();
});
observer.observe(document.documentElement || document, {
subtree: true,
childList: true,
attributes: true,
characterData: true,
});
tracker.observer = observer;
} catch (error) {
tracker.observer = null;
}
Object.defineProperty(window, key, {
value: tracker,
configurable: true,
enumerable: false,
writable: true,
});
}
let framesLoading = false;
for (const frame of document.querySelectorAll("iframe, frame")) {
try {
const doc = frame.contentDocument;
if (doc && doc.readyState !== "complete") framesLoading = true;
} catch (error) {
// Cross-origin frames are covered by network tracking.
}
}
return {
readyState: document.readyState,
// Hidden documents run throttled timers, so their quiet is not idleness.
visible: document.visibilityState === "visible",
idleMs: tracker.observer ? performance.now() - tracker.last : -1,
framesLoading,
};
})()`;
// Records, for one guarded action, whether a click reached a file input in
// the main document. A capture listener sees trusted clicks, label clicks and
// scripted clicks on attached inputs (retargeted through shadow roots); a
// detached input created on the fly only receives its own click, so the
// prototype methods are wrapped for the duration of the action. Frames have
// their own documents, so a page with frames reports "unknown".
const FILE_INPUT_PROBE_INSTALL = `(() => {
const key = "__egoBrowserFileInputProbe";
const previous = window[key];
if (previous) previous.restore();
const proto = HTMLInputElement.prototype;
const originals = { click: proto.click, showPicker: proto.showPicker };
const probe = { seen: false, handler: null, restore: null };
const markIfFile = (input) => {
if (input instanceof HTMLInputElement && input.type === "file") probe.seen = true;
};
probe.handler = (event) => {
const path = typeof event.composedPath === "function" ? event.composedPath() : [];
markIfFile(path.length > 0 ? path[0] : event.target);
};
document.addEventListener("click", probe.handler, true);
for (const name of Object.keys(originals)) {
if (typeof originals[name] !== "function") continue;
proto[name] = function (...args) {
markIfFile(this);
return originals[name].apply(this, args);
};
}
probe.restore = () => {
document.removeEventListener("click", probe.handler, true);
for (const name of Object.keys(originals)) {
if (typeof originals[name] === "function") proto[name] = originals[name];
}
delete window[key];
};
Object.defineProperty(window, key, {
value: probe,
configurable: true,
enumerable: false,
writable: true,
});
return true;
})()`;
const FILE_INPUT_PROBE_READ = `(() => {
const key = "__egoBrowserFileInputProbe";
const probe = window[key];
if (!probe) return null;
probe.restore();
if (probe.seen === true) return true;
return document.querySelector("iframe, frame") ? null : false;
})()`;
/**
* Build the Target.setDiscoverTargets enabler. Browser-level commands are
* routed through the selected task space, so discovery is enabled once per
* space. A failed or unsupported attempt resolves false and is retried on the
* next call.
*/
function createTargetDiscoveryEnabler(
supported: () => boolean,
cdp: (method: string, params: Record<string, unknown>) => Promise<unknown>,
): (spaceId: number) => Promise<boolean> {
const enabling = new Map<number, Promise<boolean>>();
return (spaceId) => {
if (!supported()) return Promise.resolve(false);
let pending = enabling.get(spaceId);
if (!pending) {
pending = cdp("Target.setDiscoverTargets", { discover: true }).then(
() => true,
() => {
enabling.delete(spaceId);
return false;
},
);
enabling.set(spaceId, pending);
}
return pending;
};
}
type AdoptPageOptions = {
as?: string;
};
type PageGotoOptions = {
referer?: string;
timeout?: number;
waitUntil?: PageGotoWaitUntil;
};
type PageReloadOptions = Pick<PageGotoOptions, "timeout" | "waitUntil">;
type PageSnapshotOptions = Omit<SnapshotOptions, "root"> & {
root?: string;
};
type PageScreenshotOptions = Omit<CaptureScreenshotOptions, "full"> & {
path?: string;
fullPage?: boolean;
};
type CdpOptions = {
timeout?: number;
};
type PagePressOptions = PageKeyboardPressOptions & {
timeout?: number;
};
type WaitForControlOptions = {
interval?: number;
timeout?: number;
};
type FinishOptions = {
keep: "all" | string[];
};
export type TaskFinishReceipt = {
spaceId: number;
closedSpace: boolean;
keptManagedLabels: string[];
closedManagedLabels: string[];
preservedUnmanagedCount: number;
};
type PageWaitForFileChooserOptions = {
timeout?: number;
};
type PageWaitForEventOptions = {
timeout?: number;
};
type PageWaitForFunctionOptions = {
timeout?: number;
polling?: number;
};
export type PageFetchOptions = {
timeout?: number;
saveAs?: string;
method?: string;
headers?: Record<string, string>;
body?: string;
cache?:
| "default"
| "no-store"
| "reload"
| "no-cache"
| "force-cache"
| "only-if-cached";
credentials?: "omit" | "same-origin" | "include";
integrity?: string;
keepalive?: boolean;
mode?: "cors" | "no-cors" | "same-origin";
redirect?: "follow" | "error" | "manual";
referrer?: string;
referrerPolicy?: string;
};
export type PageFetchResponse = {
ok: boolean;
status: number;
statusText: string;
url: string;
headers: Record<string, string>;
body?: string;
savedPath?: string;
};
type PageFetchPayload = {
url: string;
options: Record<string, unknown>;
timeoutMs: number;
responseType: "text" | "base64";
};
type PageFetchResult =
| (PageFetchResponse & { bodyBase64?: string })
| { fetchError: string };
type PageTarget = {
spaceId: number;
targetId: string;
};
type SpaceScope = {
spaceId: number;
};
type OperationGate = {
withSpace<T>(
spaceId: number,
operation: (scope: SpaceScope) => T | Promise<T>,
): Promise<T>;
withPage<T>(
page: PageTarget,
operation: (context: PageExecutionContext) => T | Promise<T>,
options?: WithPageOptions,
): Promise<T>;
};
type LedgerPort = {
read(spaceId: number): Promise<PageLedger>;
discard(spaceId: number): Promise<void>;
retainForUser(spaceId: number, targetIds: string[]): Promise<void>;
initializeCreatedSpace(
spaceId: number,
targetId: string,
): Promise<ManagedPage>;
addPage(
spaceId: number,
targetId: string,
options?: { as?: string; openedBy?: PageOrigin },
): Promise<ManagedPage>;
getPage(spaceId: number, label: string): Promise<ManagedPage>;
setPageRefs(
spaceId: number,
label: string,
refs: PageRefState,
): Promise<void>;
closePage(spaceId: number, label: string): Promise<ManagedPage>;
releasePage(spaceId: number, label: string): Promise<ManagedPage>;
keepUnmanaged(
spaceId: number,
targetId: string,
openedBy?: PageOrigin,
): Promise<void>;
beginUserControl(spaceId: number): Promise<void>;
cancelUserControl(spaceId: number): Promise<void>;
reconcile(
spaceId: number,
liveTargetIds: Iterable<string>,
options?: { autoAdoptNew?: boolean; afterUserControl?: boolean },
): Promise<PageLedger>;
};
type RuntimeTab = {
targetId: string;
active?: boolean;
title?: string;
url?: string;
openerId?: string;
};
type PageModelServices = {
ledger: LedgerPort;
pageRefs: PageRefRegistry;
gate: OperationGate;
createTab(url: string): Promise<string>;
listTabs(): Promise<RuntimeTab[]>;
probeAgentControl(): Promise<boolean>;
handOffTaskSpace(): Promise<void>;
completeTaskSpace(): Promise<void>;
closeTaskSpace(): Promise<void>;
cdp(
method: string,
params?: Record<string, unknown>,
sessionId?: string,
timeoutMs?: number,
): Promise<any>;
showAgentMousePosition(x: number, y: number): Promise<void>;
showAgentTaskState(state: string): Promise<void>;
withTemporaryClipboardText<T>(
content: PageClipboardContent,
action: () => Promise<T>,
): Promise<T>;
snapshot(options?: SnapshotOptions): Promise<any>;
screenshot(
path: string | undefined,
options: CaptureScreenshotOptions,
sessionId: string,
): Promise<string>;
pendingDialog(sessionId: string): Record<string, unknown> | null;
prepareFileChooser(
sessionId: string,
options: { timeoutMs: number; cancel: boolean },
): FileChooserInterception;
drainEvents(sessionId: string): any[];
ensureNetworkTracking(
sessionIds: string[],
timeoutMs?: number,
): Promise<void>;
pageNetworkSessions(sessionId: string, timeoutMs?: number): Promise<string[]>;
networkActivity(sessionIds: string[]): {
tracking: boolean;
inflight: number;
lastActivityAt: number;
pendingRequests?: string[];
};
ensureSession(targetId: string, timeoutMs?: number): Promise<string>;
ensureFrameSessions(
targetId: string,
timeoutMs?: number,
): Promise<Map<string, string>>;
invalidateSession(targetId: string): void;
setPreferredTarget(targetId: string): void;
supportsBackgroundPageDiscovery(): boolean;
/**
* Enable target discovery once per task space, under that space's gate
* selection. Resolves false when the runtime cannot deliver Target.*
* events, so callers fall back to polling the tab list.
*/
ensureTargetDiscovery(spaceId: number): Promise<boolean>;
subscribeBrowserEvents(listener: (event: any) => void): () => void;
subscribePageEvents(
targetId: string,
listener: (event: any) => void,
): () => void;
prepareDownload(
targetId: string,
options: { timeoutMs: number },
): DownloadInterception;
now(): number;
/** Absolute `now()` time at which the running element action gives up. */
actionDeadline?: number;
sleep(ms: number): Promise<void>;
platform: string;
pageBudget: number;
};
export type TabInventoryItem = {
targetId: string;
label?: string;
page: Page | UnmanagedPage;
title: string;
url: string;
active: boolean;
openedBy: PageOrigin;
};
export type PageActionReceipt = {
popups?: Array<{ label: string; targetId: string }>;
dialog?: Record<string, unknown>;
};
export class PageEvaluationTimeoutError extends Error {
readonly code = "EGO_PAGE_EVALUATION_TIMED_OUT";
readonly timeoutMs: number;
readonly executionStopped: boolean;
readonly mayHaveLateEffects: boolean;
readonly pageResponsive: boolean;
/**
* A main-frame navigation that had not committed kept the Page from
* answering; it was stopped once the wait ran out and the Page answers
* again, showing `currentUrl`.
*/
stopped = false;
currentUrl?: string;
constructor(
message: string,
{
timeoutMs,
executionStopped,
mayHaveLateEffects,
pageResponsive,
}: {
timeoutMs: number;
executionStopped: boolean;
mayHaveLateEffects: boolean;
pageResponsive: boolean;
},
) {
super(message);
this.name = "PageEvaluationTimeoutError";
this.timeoutMs = timeoutMs;
this.executionStopped = executionStopped;
this.mayHaveLateEffects = mayHaveLateEffects;
this.pageResponsive = pageResponsive;
}
}
const PAGE_CLOSE_CONFIRM_TIMEOUT_MS = 2_000;
const PAGE_CLOSE_CONFIRM_INTERVAL_MS = 50;
const CREATED_SPACE_TAB_TIMEOUT_MS = 2_000;
const CREATED_SPACE_TAB_POLL_INTERVAL_MS = 50;
const DEFAULT_PAGE_ACTION_TIMEOUT_MS = 3_000;
// A blocked target (hidden, disabled, covered) is re-examined at this cadence
// until the action timeout; a short interval lets an element that becomes
// actionable a few frames later be clicked without waiting most of a second.
const PAGE_ACTION_RESOLUTION_RETRY_MS = 50;
// A missing action target stops being retried once the page has shown no DOM
// mutation and no network activity for this long; a page that is still
// loading or rendering keeps the full action timeout.
const MISSING_TARGET_IDLE_MS = 500;
const MISSING_TARGET_PROBE_TIMEOUT_MS = 1_000;
// One page round trip after an action orders a popup's Target.targetCreated
// event ahead of the response, so no settle delay is needed.
const POPUP_SYNC_TIMEOUT_MS = 1_000;
const PAGE_UNRESPONSIVE_URL_TIMEOUT_MS = 1_000;
// Page.stopLoading is answered by the browser process; the frame-tree read
// afterwards is what shows whether the renderer answers again.
const PAGE_UNRESPONSIVE_STOP_TIMEOUT_MS = 1_000;
// The native tab list can lag the CDP target event by a few milliseconds.
const POPUP_TAB_LIST_RETRIES = 10;
const POPUP_TAB_LIST_RETRY_MS = 20;
// Legacy settle for runtimes that cannot deliver Target.* events.
const POPUP_POLL_SETTLE_MS = 50;
// After a navigation commits, cross-process iframes are still being created
// and auto-attached. Enumerating frame sessions in that window can attach a
// second session to a child and drop its in-flight requests from network
// tracking (see ensureFrameSessions), so navigation keeps the settle that the
// previous tab-list diff provided. Input actions do not need it.
const NAVIGATION_FRAME_SETTLE_MS = 60;
// A snapshot whose capture straddles a document commit is retried against the
// new document. Two retries absorb the common "action, then snapshot" race
// without turning a page that keeps navigating into an unbounded loop.
const SNAPSHOT_DOCUMENT_CHANGE_RETRIES = 2;
// A file chooser request can trail the input acknowledgement. When the page
// reports that the action clicked a file input, wait for the chooser event
// (it resolves the race early); when the page could not be asked, fall back
// to the short settle the tab-list diff used to provide.
const FILE_CHOOSER_OPEN_WAIT_MS = 1_000;
const FILE_CHOOSER_SETTLE_MS = 50;
const CONTROL_POLL_INTERVAL_MS = 20_000;
// page.evaluate waits one runtime response timeout for the transport and
// asks Chromium to stop the script one second earlier, so a long synchronous
// callback is reported as stopped rather than as an unanswered command.
const PAGE_EVALUATE_EXECUTION_MARGIN_MS = 1_000;
function pageEvaluateTransportTimeoutMs(): number {
return responseTimeoutMs();
}
function pageEvaluateExecutionTimeoutMs(): number {
return Math.max(
PAGE_EVALUATE_EXECUTION_MARGIN_MS,
pageEvaluateTransportTimeoutMs() - PAGE_EVALUATE_EXECUTION_MARGIN_MS,
);
}
const PAGE_EVALUATE_HEALTH_TIMEOUT_MS = 250;
const PAGE_EVALUATE_HEALTH_EXECUTION_TIMEOUT_MS = 200;
const PAGE_EVALUATE_TERMINATE_TIMEOUT_MS = 1_000;
const WAIT_FOR_FUNCTION_TRANSPORT_GRACE_MS = 250;
const CONTROL_WAIT_TIMEOUT_MS = 600_000;
type RawActionRunner = (
operation: (services: PageModelServices, sessionId: string) => Promise<void>,
) => Promise<void>;
type ObservedActionRunner = (
operation: (services: PageModelServices, sessionId: string) => Promise<void>,
) => Promise<PageActionReceipt>;
/** Page-scoped mouse state and CDP Input primitives. */
class PageMouse {
readonly #run: RawActionRunner;
readonly #runObserved: ObservedActionRunner;
readonly #modifierMask: () => number;
#x = 0;
#y = 0;
#buttons = 0;
#lastButton: MouseButton | "none" = "none";
constructor(
run: RawActionRunner,
runObserved: ObservedActionRunner,
modifierMask: () => number,
) {
this.#run = run;
this.#runObserved = runObserved;
this.#modifierMask = modifierMask;
}
async click(
x: number,
y: number,
options: PageMouseClickOptions = {},
): Promise<PageActionReceipt> {
validatePublicApiOptions("Page.mouse.click", options);
const receipt = await this.#runObserved((services, sessionId) =>
clickPointInPage(
services,
sessionId,
x,
y,
options,
this.#modifierMask(),
this.#buttons,
),
);
this.#x = x;
this.#y = y;
this.#lastButton = "none";
return receipt;
}
async move(
x: number,
y: number,
options: PageMouseMoveOptions = {},
): Promise<void> {
validatePublicApiOptions("Page.mouse.move", options);
await this.#run((services, sessionId) =>
moveMouseInPage(services, sessionId, this.#x, this.#y, x, y, {
...options,
button: this.#lastButton,
buttons: this.#buttons,
modifiers: this.#modifierMask(),
}),
);
this.#x = x;
this.#y = y;
}
async down(options: PageMouseButtonOptions = {}): Promise<void> {
validatePublicApiOptions("Page.mouse.down", options);
const button = options.button ?? "left";
const nextButtons = this.#buttons | mouseButtonMask(button);
await this.#run((services, sessionId) =>
mouseButtonInPage(
services,
sessionId,
"mousePressed",
this.#x,
this.#y,
nextButtons,
options,
this.#modifierMask(),
).then(() => undefined),
);
this.#buttons = nextButtons;
this.#lastButton = button;
}
async up(options: PageMouseButtonOptions = {}): Promise<void> {
validatePublicApiOptions("Page.mouse.up", options);
const button = options.button ?? "left";
const nextButtons = this.#buttons & ~mouseButtonMask(button);
await this.#run((services, sessionId) =>
mouseButtonInPage(
services,
sessionId,
"mouseReleased",
this.#x,
this.#y,
nextButtons,
options,
this.#modifierMask(),
).then(() => undefined),
);
this.#buttons = nextButtons;
this.#lastButton = "none";
}
async wheel(
deltaX: number,
deltaY: number,
options: PageMouseWheelOptions = {},
): Promise<void> {
validatePublicApiOptions("Page.mouse.wheel", options);
return this.#run((services, sessionId) =>
wheelInPage(
services,
sessionId,
this.#x,
this.#y,
deltaX,
deltaY,
this.#modifierMask(),
options,
),
);
}
}
/** Page-scoped keyboard input with Playwright-style key state. */
class PageKeyboard {
readonly #controller: PageKeyboardController;
constructor(
services: PageModelServices,
run: RawActionRunner,
runObserved: ObservedActionRunner,
) {
this.#controller = new PageKeyboardController(
services,
(operation) => run((_services, sessionId) => operation(sessionId)),
(operation) =>
runObserved((_services, sessionId) => operation(sessionId)),
);
}
modifierMask(): number {
return this.#controller.modifierMask();
}
async down(key: string): Promise<void> {
await this.#controller.down(key);
}
async up(key: string): Promise<void> {
await this.#controller.up(key);
}
async press(
chord: string,
options: PageKeyboardPressOptions = {},
): Promise<PageActionReceipt> {
validatePublicApiOptions("Page.keyboard.press", options);
return (await this.#controller.press(chord, options)) as PageActionReceipt;
}
async pressInSession(
sessionId: string,
chord: string,
options: PageKeyboardPressOptions = {},
): Promise<void> {
validatePublicApiOptions("Page.keyboard.press", options);
await this.#controller.pressInSession(sessionId, chord, options);
}
async paste(content: PageClipboardContent): Promise<PageActionReceipt> {
return (await this.#controller.paste(content)) as PageActionReceipt;
}
async insertText(text: string): Promise<void> {
await this.#controller.insertText(text);
}
async type(
text: string,
options: PageKeyboardTypeOptions = {},
): Promise<void> {
validatePublicApiOptions("Page.keyboard.type", options);
await this.#controller.type(text, options);
}
}
class PageBudgetError extends Error {
readonly code = "EGO_PAGE_BUDGET_REACHED";
readonly spaceId: number;
readonly limit: number;
constructor(spaceId: number, limit: number, message: string) {
super(message);
this.name = "PageBudgetError";
this.spaceId = spaceId;
this.limit = limit;
}
}
let defaultLedger: PageLedgerStore | undefined;
const defaultPageRefs = new PageRefRegistry();
const unmanagedPageConstructorToken = Symbol("UnmanagedPage");
const captureUserBoundaryToken = Symbol("captureUserBoundary");
const initializeTaskSpaceToken = Symbol("initializeTaskSpace");
const initializeCreatedSpaceToken = Symbol("initializeCreatedSpace");
const rollbackCreatedTaskSpaceToken = Symbol("rollbackCreatedTaskSpace");
const describeMissingPageToken = Symbol("describeMissingPage");
const defaultGate: OperationGate = {
withSpace: defaultWithSpace,
withPage: defaultWithPage,
};
const baseDefaultServices: Omit<PageModelServices, "ledger" | "pageBudget"> = {
gate: defaultGate,
pageRefs: defaultPageRefs,
async createTab(url) {
const result = await invokeEgo("task.newPage", () =>
browserEgo().createTab(url),
);
const targetId = result?.targetId || result?.result?.targetId;
if (typeof targetId !== "string" || targetId.length === 0) {
throw new Error("task.newPage returned no targetId");
}
return targetId;
},
async listTabs() {
const result = await invokeEgo("task.listTabs", () =>
browserEgo().listTabs(),
);
return result?.tabs || result?.targetInfos || [];
},
async probeAgentControl() {
// Do not route this through invokeEgo: observing user control is the
// expected waiting state, not a hard-stop signal.
return probeAgentControl(() =>
browserEgo().snapshot({ maxResultLength: 1 }),
);
},
async handOffTaskSpace() {
const ego = browserEgo();
if (typeof ego.handOffTaskSpace !== "function") {
throw new Error("task.handOff requires ego.handOffTaskSpace");
}
await invokeEgo("task.handOff", () => ego.handOffTaskSpace());
},
async completeTaskSpace() {
const ego = browserEgo();
if (typeof ego.completeTaskSpace !== "function") {
throw new Error("task.finish requires ego.completeTaskSpace");
}
await invokeEgo("task.finish", () => ego.completeTaskSpace());
},
async closeTaskSpace() {
const ego = browserEgo();
if (typeof ego.closeTaskSpace !== "function") {
throw new Error("task.close requires ego.closeTaskSpace");
}
await invokeEgo("task.close", () => ego.closeTaskSpace());
},
async cdp(method, params = {}, sessionId, timeoutMs) {
const response = await browserCdp(method, params, sessionId, timeoutMs);
return response?.result || {};
},
async showAgentMousePosition(x, y) {
const ego = browserEgo();
if (typeof ego.animationHighlightMouseToPosition !== "function") return;
await invokeEgo("page.mouse.move", () =>
ego.animationHighlightMouseToPosition(x, y),
);
},
async showAgentTaskState(state) {
const ego = browserEgo();
if (typeof ego.setAgentTaskState !== "function") return;
await invokeEgo("page.mouse.label", () => ego.setAgentTaskState(state));
},
withTemporaryClipboardText,
snapshot: snapshotRaw,
screenshot: captureScreenshotForSession,
pendingDialog,
prepareFileChooser,
drainEvents: drainPageEvents,
ensureNetworkTracking,
pageNetworkSessions,
networkActivity,
ensureSession,
ensureFrameSessions,
invalidateSession,
setPreferredTarget,
supportsBackgroundPageDiscovery: isBrowserRuntime,
ensureTargetDiscovery: createTargetDiscoveryEnabler(
() => isBrowserRuntime(),
(method, params) => browserCdp(method, params),
),
subscribeBrowserEvents,
subscribePageEvents,
prepareDownload(targetId, options) {
return preparePageDownload(
{
cdp: baseDefaultServices.cdp,
subscribePageEvents,
},
targetId,
options,
);
},
now: () => state.now(),
async sleep(ms) {
await state.sleep(ms);
},
get platform() {
return state.platform;
},
};
/**
* Create a TaskSpace object around a resolved native task-space descriptor.
* The helper layer owns name/id resolution; this layer owns page identity and
* routes every browser operation through the native operation gate.
*/
export function createTaskSpaceHandle(
descriptor: TaskSpaceDescriptor,
overrides: Partial<PageModelServices> = {},
): TaskSpace {
if (!descriptor || !Number.isInteger(descriptor.id)) {
throw new TypeError("TaskSpace requires a numeric id");
}
// Ego Lite imports the SDK before it evaluates the submitted script. Resolve
// environment-backed settings lazily so SDK callers can configure a round
// before their first taskSpace() call.
defaultLedger ||= new PageLedgerStore({
browserInstanceId: runtimeInstanceId,
});
const services = {
...baseDefaultServices,
ledger: defaultLedger,
pageBudget: configuredPageBudget(),
...overrides,
};
if (!Number.isInteger(services.pageBudget) || services.pageBudget < 1) {
throw new TypeError("pageBudget must be a positive integer");
}
return new TaskSpace(descriptor, services);
}
/**
* Capture the active tab at a claim/takeover boundary before Agent actions can
* change it. This is called by the helper layer and is not injected directly.
*/
export async function captureTaskSpaceUserBoundary(
task: TaskSpace,
): Promise<void> {
await task.captureUserBoundary(captureUserBoundaryToken);
}
/** Initialize Page state and round-local discovery before exposing a TaskSpace. */
export async function initializeTaskSpaceHandle(
task: TaskSpace,
options: { created?: boolean } = {},
): Promise<void> {
if (options.created) {
await task.initializeCreatedSpace(initializeCreatedSpaceToken);
}
await task.initializeBackgroundPageDiscovery(initializeTaskSpaceToken);
}
/** Close a newly created space whose Page-model initialization did not commit. */
export async function rollbackCreatedTaskSpace(task: TaskSpace): Promise<void> {
await task[rollbackCreatedTaskSpaceToken]();
}
class TaskSpace {
readonly taskId?: string | number;
readonly id: number;
readonly name: string;
readonly createdBy?: string;
readonly ownership?: string;
readonly recentTabTitles?: string[];
readonly #services: PageModelServices;
#userPage?: Page | UnmanagedPage;
#stopBrowserEvents?: () => void;
#backgroundDiscoveryInitialized = false;
#finished = false;
constructor(descriptor: TaskSpaceDescriptor, services: PageModelServices) {
this.taskId = descriptor.taskId;
this.id = descriptor.id;
this.name = descriptor.name;
this.createdBy = descriptor.createdBy;
this.ownership = descriptor.ownership;
this.recentTabTitles = descriptor.recentTabTitles;
this.#services = services;
}
/** Stable task-space identifier. `id` remains as a compatibility alias. */
get spaceId(): number {
return this.id;
}
page(label: string): Page {
return new Page(this, label, this.#services);
}
/** The tab active at the most recent claim/takeover boundary, if any. */
userPage(): Page | UnmanagedPage | undefined {
return this.#userPage;
}
async initializeCreatedSpace(token: symbol): Promise<void> {
if (token !== initializeCreatedSpaceToken) {
throw new TypeError("created-space initialization is internal");
}
if (this.ownership !== "agent") {
throw new Error("only a newly created Agent TaskSpace can initialize p1");
}
await this.#services.gate.withSpace(this.id, async () => {
const targetId = await this.#waitForCreatedSpaceTab();
const page = await this.#services.ledger.initializeCreatedSpace(
this.id,
targetId,
);
this.#services.setPreferredTarget(page.targetId);
});
}
async #waitForCreatedSpaceTab(): Promise<string> {
const deadline = this.#services.now() + CREATED_SPACE_TAB_TIMEOUT_MS;
while (true) {
const tabs = await this.#services.listTabs();
if (tabs.length > 1) {
throw new Error(
`new task space expected one default tab, found ${tabs.length}`,
);
}
if (tabs.length === 1) {
const targetId = tabs[0]?.targetId;
if (typeof targetId !== "string" || targetId.length === 0) {
throw new Error("new task space default tab returned no targetId");
}
return targetId;
}
const remainingMs = deadline - this.#services.now();
if (remainingMs <= 0) {
throw new Error(
`new task space did not expose its default tab within ${CREATED_SPACE_TAB_TIMEOUT_MS}ms`,
);
}
await this.#services.sleep(
Math.min(CREATED_SPACE_TAB_POLL_INTERVAL_MS, remainingMs),
);
}
}
async initializeBackgroundPageDiscovery(token: symbol): Promise<void> {
if (token !== initializeTaskSpaceToken) {
throw new TypeError(
"background page discovery is initialized internally",
);
}
if (
this.#backgroundDiscoveryInitialized ||
this.ownership !== "agent" ||
!this.#services.supportsBackgroundPageDiscovery()
) {
return;
}
this.#backgroundDiscoveryInitialized = true;
this.#stopBrowserEvents = this.#services.subscribeBrowserEvents((event) => {
this.#handleBrowserEvent(event);
});
// Subscribe before enabling discovery so a target created during setup
// cannot fall into the gap between the initial inventory and the callback.
try {
await this.#services.gate.withSpace(this.id, async () => {
await this.#services.ensureTargetDiscovery(this.id);
await this.#recoverExistingChildPages();
});
} catch {
// Background discovery is an observation aid. Existing action receipts
// and task.tabs() reconciliation remain the correctness fallback.
}
}
async captureUserBoundary(token: symbol): Promise<void> {
if (token !== captureUserBoundaryToken) {
throw new TypeError(
"the user-page boundary is captured automatically by claim/takeover",
);
}
await this.#services.gate.withSpace(this.id, async () => {
const tabs = await this.#services.listTabs();
if (tabs.length === 0) {
this.#userPage = undefined;
return;
}
const ledger = await this.#services.ledger.reconcile(
this.id,
tabs.map((tab) => tab.targetId),
{ autoAdoptNew: false, afterUserControl: true },
);
const active = tabs.find((tab) => tab.active);
this.#userPage = active
? tabInventory(this, this.#services, ledger, tabs).find(
(item) => item.targetId === active.targetId,
)?.page
: undefined;
});
}
/** Return managed Page handles after reconciling the browser tab list. */
async pages(): Promise<Page[]> {
const tabs = await this.tabs();
return tabs
.filter((item) => item.page instanceof Page)
.map((item) => item.page as Page);
}
/** Return managed and unmanaged tabs after reconciling browser state. */
async tabs(): Promise<TabInventoryItem[]> {
return this.#services.gate.withSpace(this.id, async () => {
const { ledger, tabs } = await this.#reconcilePages();
return tabInventory(this, this.#services, ledger, tabs);
});
}
/** Wait until this space is controllable without taking control from the user. */
async waitForControl(options: WaitForControlOptions = {}): Promise<void> {
validatePublicApiOptions("TaskSpace.waitForControl", options);
const intervalMs = options.interval ?? CONTROL_POLL_INTERVAL_MS;
const timeoutMs = options.timeout ?? CONTROL_WAIT_TIMEOUT_MS;
const deadline = this.#services.now() + timeoutMs;
while (true) {
const available = await this.#services.gate.withSpace(this.id, () =>
this.#services.probeAgentControl(),
);
if (available) return;
const remainingMs = deadline - this.#services.now();
if (remainingMs <= 0) {
throw new Error(`task.waitForControl timed out after ${timeoutMs}ms`);
}
await this.#services.sleep(Math.min(intervalMs, remainingMs));
}
}
/** Give control of this task space to the user while keeping Page state. */
async handOff(): Promise<void> {
await this.#services.gate.withSpace(this.spaceId, async () => {
await this.#reconcilePages();
await this.#services.ledger.beginUserControl(this.spaceId);
try {
await this.#services.handOffTaskSpace();
this.#stopBackgroundPageDiscovery();
} catch (error) {
await this.#services.ledger.cancelUserControl(this.spaceId);
throw error;
}
});
}
/** Finish the task with an explicit managed-Page retention policy. */
async finish(options: FinishOptions): Promise<TaskFinishReceipt> {
validatePublicApiOptions("TaskSpace.finish", options);
if (!Object.hasOwn(options, "keep") || options.keep === undefined) {
throw new TypeError(
"task.finish requires the keep option. Expected: await task.finish({ keep })",
);
}
return this.#services.gate.withSpace(this.spaceId, async () => {
const { ledger, tabs } = await this.#reconcilePages();
const keepLabels =
options.keep === "all"
? new Set(Object.keys(ledger.pages))
: new Set(options.keep);
for (const label of keepLabels) {
if (!ledger.pages[label]) {
// Reuse the ledger's permanent-label diagnostics and validate every
// requested label before closing any Page.
await this.#services.ledger.getPage(this.spaceId, label);
}
}
const managedByTarget = new Map(
Object.values(ledger.pages).map((page) => [page.targetId, page]),
);
const labelsToClose = Object.entries(ledger.pages)
.filter(
([label, page]) =>
page.openedBy === "agent" && !keepLabels.has(label),
)
.map(([label]) => label);
const closedManagedLabelSet = new Set(labelsToClose);
const keptManagedLabels = Object.keys(ledger.pages).filter(
(label) => !closedManagedLabelSet.has(label),
);
// Unmanaged tabs the SDK opened itself (the blank anchor page.close()
// leaves behind) belong to the agent: they are closed, not protected.
const agentUnmanagedTargets = tabs
.filter(
(tab) =>
!managedByTarget.has(tab.targetId) &&
ledger.unmanagedTargets[tab.targetId] === "agent",
)
.map((tab) => tab.targetId);
const agentUnmanagedSet = new Set(agentUnmanagedTargets);
const preservedUnmanagedCount = tabs.filter(
(tab) =>
!managedByTarget.has(tab.targetId) &&
!agentUnmanagedSet.has(tab.targetId),
).length;
const protectedTabCount = tabs.filter((tab) => {
const managed = managedByTarget.get(tab.targetId);
if (managed) return managed.openedBy === "unknown";
return !agentUnmanagedSet.has(tab.targetId);
}).length;
const keepsNothing = options.keep !== "all" && options.keep.length === 0;
if (keepsNothing && protectedTabCount === 0) {
await this.#services.closeTaskSpace();
await this.#discardTerminalState();
return {
spaceId: this.spaceId,
closedSpace: true,
keptManagedLabels: [],
closedManagedLabels: labelsToClose,
preservedUnmanagedCount,
};
}
for (const label of labelsToClose) {
await this.page(label).close();
}
// Tabs an earlier finish() left for the user go only when this finish
// keeps nothing; a keep list can only name labels, so it cannot cover them.
const retainedEarlier = new Set(
keepsNothing ? [] : ledger.retainedTargets,
);
const anchorsLeftOpen = await this.#closeAgentUnmanagedTabs(
agentUnmanagedTargets.filter((id) => !retainedEarlier.has(id)),
);
await this.#services.completeTaskSpace();
await this.#discardTerminalState([
...keptManagedLabels
.map((label) => ledger.pages[label])
.filter((page) => page.openedBy === "agent")
.map((page) => page.targetId),
...agentUnmanagedTargets.filter((id) => retainedEarlier.has(id)),
...anchorsLeftOpen,
]);
if (keepsNothing) {
bufferOutput(
`[ego-browser] finish({ keep: [] }) did not close task space ${this.spaceId}: ` +
`${protectedTabCount} tab(s) of user or unknown origin stayed open, ` +
"and the space now belongs to the user.\n",
);
}
return {
spaceId: this.spaceId,
closedSpace: false,
keptManagedLabels,
closedManagedLabels: labelsToClose,
preservedUnmanagedCount,
};
});
}
/**
* Close SDK-opened unmanaged tabs (blank anchors) that the space no longer
* needs. A tab that is the last one in the space stays: closing it would
* destroy the space the caller chose to keep. Returns the tabs left open.
*/
async #closeAgentUnmanagedTabs(targetIds: string[]): Promise<string[]> {
if (targetIds.length === 0) return [];
const leftOpen: string[] = [];
let live = new Set(
(await this.#services.listTabs()).map((tab) => tab.targetId),
);
for (const targetId of targetIds) {
if (!live.has(targetId)) continue;
if (live.size <= 1) {
leftOpen.push(targetId);
continue;
}
const result = await this.#services.cdp("Target.closeTarget", {
targetId,
});
if (result?.success !== true) {
throw new Error(`failed to close anchor tab ${targetId}`);
}
const disappeared = await waitForTargetToDisappear(
this.#services,
targetId,
PAGE_CLOSE_CONFIRM_TIMEOUT_MS,
);
if (!disappeared) {
throw new Error(
`anchor tab ${targetId} did not close within ${PAGE_CLOSE_CONFIRM_TIMEOUT_MS}ms`,
);
}
this.#services.invalidateSession(targetId);
this.#services.pageRefs.clear(targetId);
live = new Set(
(await this.#services.listTabs()).map((tab) => tab.targetId),
);
}
return leftOpen;
}
async [rollbackCreatedTaskSpaceToken](): Promise<void> {
await this.#services.gate.withSpace(this.spaceId, async () => {
await this.#services.closeTaskSpace();
await this.#discardTerminalState();
});
}
async #discardTerminalState(retainedTargets: string[] = []): Promise<void> {
this.#finished = true;
if (retainedTargets.length > 0) {
await this.#services.ledger.retainForUser(this.spaceId, retainedTargets);
} else {
await this.#services.ledger.discard(this.spaceId);
}
this.#stopBackgroundPageDiscovery();
clearSpacePageNotices(this.spaceId);
}
/**
* Explain an unknown Page label in task-lifecycle terms: the same ledger miss
* means "never created" during a task but "already ended" after finish().
*/
async [describeMissingPageToken](
label: string,
error: unknown,
): Promise<unknown> {
if (
!(error instanceof Error) ||
error.message !== `page label not found: ${label}`
) {
return error;
}
if (this.#finished) {
return new Error(
`${error.message}. Task space ${this.id} already finished, which ended its Page labels; do not use Pages after task.finish()`,
{ cause: error },
);
}
if (this.ownership !== "agent") {
return new Error(
`${error.message}. Task space ${this.id} is owned by the user, so labels from a finished Agent task no longer exist; stop operating it unless the user asks you to continue`,
{ cause: error },
);
}
const labels = Object.keys(
(await this.#services.ledger.read(this.id)).pages,
);
return new Error(
`${error.message}. Managed Pages in task space ${this.id}: ${labels.join(", ") || "none"}. task.page(label) only refers to an existing Page; create one with await task.newPage()`,
{ cause: error },
);
}
/** Send a Target or Browser domain command within this selected space. */
async cdp(
method: string,
params: Record<string, unknown> = {},
options: CdpOptions = {},
): Promise<any> {
assertCdpCall("TaskSpace.cdp", method, params, options);
if (!method.startsWith("Target.") && !method.startsWith("Browser.")) {
throw new TypeError(
"task.cdp only supports Target. and Browser. commands",
);
}
return this.#services.gate.withSpace(this.id, () =>
this.#services.cdp(method, params, undefined, options.timeout),
);
}
/**
* Bring an untracked browser tab under the durable Page lifecycle.
* Untracked handles intentionally cannot operate on the tab before adoption.
*/
async adopt(
page: UnmanagedPage,
options: AdoptPageOptions = {},
): Promise<Page> {
validatePublicApiOptions("TaskSpace.adopt", options);
assertUnmanagedPage(page);
if (page.spaceId !== this.id) {
throw new Error(
`untracked page ${page.targetId} belongs to space ${page.spaceId}, not space ${this.id}`,
);
}
return this.#services.gate.withSpace(this.id, async () => {
const { ledger, tabs } = await this.#reconcilePages();
const live = tabs.some((tab) => tab.targetId === page.targetId);
if (!live) {
throw new Error(`untracked page ${page.targetId} is no longer open`);
}
const existing = Object.entries(ledger.pages).find(
([, entry]) => entry.targetId === page.targetId,
);
if (existing) {
throw new Error(
`target ${page.targetId} is already page ${existing[0]}`,
);
}
if (Object.keys(ledger.pages).length >= this.#services.pageBudget) {
throw pageBudgetError(this, this.#services.pageBudget, ledger, tabs);
}
const entry = await this.#services.ledger.addPage(
this.id,
page.targetId,
{
as: options.as,
openedBy: page.openedBy,
},
);
return new Page(this, entry.label, this.#services, entry);
});
}
/**
* Stop managing an unknown-origin page without closing its browser tab.
* Agent-created pages must be closed so they cannot become untracked orphans.
*/
async release(label: string): Promise<UnmanagedPage> {
return this.#services.gate.withSpace(this.id, async () => {
await this.#reconcilePages();
const entry = await this.#services.ledger.getPage(this.id, label);
if (entry.openedBy === "agent") {
throw new Error(
`page ${label} was created by the agent; close it instead of releasing it`,
);
}
const released = await this.#services.ledger.releasePage(this.id, label);
return new UnmanagedPage(
this,
released.targetId,
released.openedBy,
unmanagedPageConstructorToken,
);
});
}
async newPage(...args: unknown[]): Promise<Page> {
if (args.length > 0) {
throw new TypeError("task.newPage does not accept arguments");
}
return this.#services.gate.withSpace(this.id, async () => {
const { ledger, tabs } = await this.#reconcilePages();
const managedCount = Object.keys(ledger.pages).length;
if (managedCount >= this.#services.pageBudget) {
throw pageBudgetError(this, this.#services.pageBudget, ledger, tabs);
}
const targetId = await this.#services.createTab("about:blank");
this.#services.setPreferredTarget(targetId);
const existingManaged = Object.entries(ledger.pages).find(
([, page]) => page.targetId === targetId,
);
if (existingManaged) {
throw new Error(
`task.newPage did not create a distinct tab; target ${targetId} is already page ${existingManaged[0]}`,
);
}
const existedBeforeCreate = tabs.some((tab) => tab.targetId === targetId);
let entry: ManagedPage;
try {
entry = await this.#services.ledger.addPage(this.id, targetId, {
openedBy: "agent",
});
} catch (error) {
// A tab without a committed label cannot be returned safely. Close it
// only when createTab produced a new target. Ego Lite may reuse a blank
// anchor tab; closing a pre-existing target on a ledger error could
// destroy a page the runtime does not own.
if (!existedBeforeCreate) {
await this.#services
.cdp("Target.closeTarget", { targetId })
.catch(() => {});
this.#services.invalidateSession(targetId);
}
throw error;
}
await this.#services.ensureSession(targetId);
return new Page(this, entry.label, this.#services, entry);
});
}
async #reconcilePages(): Promise<{
ledger: PageLedger;
tabs: RuntimeTab[];
}> {
// Some embedders provide a minimal ledger port. Adoption still works
// without the optional before-image; only the round notice is skipped.
const before =
typeof this.#services.ledger.read === "function"
? await this.#services.ledger.read(this.id)
: undefined;
const tabs = await this.#services.listTabs();
const ledger = await this.#services.ledger.reconcile(
this.id,
tabs.map((tab) => tab.targetId),
{ autoAdoptNew: this.ownership === "agent" },
);
if (before) {
const knownTargets = new Set(
Object.values(before.pages).map((page) => page.targetId),
);
for (const [label, page] of Object.entries(ledger.pages)) {
if (knownTargets.has(page.targetId)) continue;
const tab = tabs.find(
(candidate) => candidate.targetId === page.targetId,
);
recordDiscoveredPage(
this.id,
{ label, ...page },
labelForTarget(ledger, tab?.openerId),
tab?.url,
);
}
}
return { ledger, tabs };
}
async #adoptDiscoveredChildPage(
ledger: PageLedger,
tab: RuntimeTab,
): Promise<ManagedPage | undefined> {
const openerLabel = labelForTarget(ledger, tab.openerId);
if (
!openerLabel ||
labelForTarget(ledger, tab.targetId) ||
Object.hasOwn(ledger.unmanagedTargets, tab.targetId)
) {
return undefined;
}
try {
const page = await this.#services.ledger.addPage(this.id, tab.targetId, {
openedBy: "agent",
});
// Keep the current inventory usable while adopting a chain of child
// pages from one listTabs() result.
ledger.pages[page.label] = {
targetId: page.targetId,
openedBy: page.openedBy,
};
recordDiscoveredPage(this.id, page, openerLabel, tab.url);
return page;
} catch {
// Another discovery path may have adopted the same target first.
return undefined;
}
}
#handleBrowserEvent(event: any): void {
if (event?.method === "Target.targetDestroyed") {
const targetId = event.params?.targetId;
if (typeof targetId === "string") {
forgetPageNotice(this.id, targetId);
}
return;
}
if (event?.method === "Target.targetInfoChanged") {
const info = event.params?.targetInfo;
if (
info?.type === "page" &&
typeof info.targetId === "string" &&
typeof info.url === "string"
) {
refreshUnhandledPageNotice(this.id, info.targetId, info.url);
}
return;
}
const info = event?.params?.targetInfo;
if (
event?.method !== "Target.targetCreated" ||
info?.type !== "page" ||
typeof info.targetId !== "string" ||
typeof info.openerId !== "string"
) {
return;
}
void this.#services.gate
.withSpace(this.id, async () => {
const before = await this.#services.ledger.read(this.id);
const tabs = await this.#services.listTabs();
const tab = tabs.find(
(candidate) => candidate.targetId === info.targetId,
);
if (!tab) return;
await this.#adoptDiscoveredChildPage(before, {
...tab,
openerId: info.openerId,
url: tab.url || info.url,
});
})
.catch(() => {
// The next task.tabs() reconciliation remains the fallback.
});
}
async #recoverExistingChildPages(): Promise<void> {
const ledger = await this.#services.ledger.read(this.id);
const tabs = await this.#services.listTabs();
for (const tab of tabs) {
await this.#adoptDiscoveredChildPage(ledger, tab);
}
}
#stopBackgroundPageDiscovery(): void {
this.#stopBrowserEvents?.();
this.#stopBrowserEvents = undefined;
}
}
function labelForTarget(
ledger: PageLedger,
targetId: unknown,
): string | undefined {
if (typeof targetId !== "string") return undefined;
return Object.entries(ledger.pages).find(
([, page]) => page.targetId === targetId,
)?.[0];
}
function recordDiscoveredPage(
spaceId: number,
page: ManagedPage,
openerLabel?: string,
url?: string,
): void {
recordUnhandledPage({
spaceId,
targetId: page.targetId,
label: page.label,
openerLabel,
url,
});
}
/**
* A read-only identity for a live tab that is not managed by the Page model.
* Obtain one from TaskSpace.tabs(), then call TaskSpace.adopt() before
* navigating, observing, or closing the tab.
*/
class UnmanagedPage {
readonly spaceId: number;
readonly targetId: string;
readonly openedBy: PageOrigin;
constructor(
task: TaskSpace,
targetId: string,
openedBy: PageOrigin,
token: symbol,
) {
if (token !== unmanagedPageConstructorToken) {
throw new TypeError(
"UnmanagedPage handles can only be obtained from task.tabs()",
);
}
this.spaceId = task.id;
this.targetId = targetId;
this.openedBy = openedBy;
Object.freeze(this);
}
}
type ArmedFileChooser = {
page: PageTarget;
interception: FileChooserInterception;
};
type PendingFileChooser = {
arm: Promise<ArmedFileChooser>;
};
type ArmedDownload = {
page: PageTarget;
interception: DownloadInterception;
};
type PendingDownload = {
arm: Promise<ArmedDownload>;
};
/** A file chooser intercepted before Chromium can open a native dialog. */
class FileChooser {
readonly #services: PageModelServices;
readonly #page: PageTarget;
readonly #event: FileChooserOpenedEvent;
readonly #interception: FileChooserInterception;
#handled = false;
constructor(
services: PageModelServices,
armed: ArmedFileChooser,
event: FileChooserOpenedEvent,
) {
this.#services = services;
this.#page = armed.page;
this.#interception = armed.interception;
this.#event = event;
}
isMultiple(): boolean {
return this.#event.mode === "selectMultiple";
}
async setFiles(path: string | string[]): Promise<PageActionReceipt> {
if (this.#handled) throw new Error("this file chooser was already handled");
const files = normalizeFilePaths(path, "fileChooser.setFiles");
this.#handled = true;
try {
await this.#services.gate.withPage(this.#page, async ({ sessionId }) => {
await setFilesOnBackendNode(
this.#services,
sessionId,
files,
this.#event.backendNodeId,
);
});
return {};
} catch (error) {
if (isPageDialogOpenedError(error)) {
return { dialog: error.dialog };
}
throw error;
} finally {
await this.#interception.dispose();
}
}
}
/** A Page download backed by a round-local artifact. */
class Download {
readonly #pageHandle: Page;
readonly #artifact: DownloadArtifact;
constructor(page: Page, artifact: DownloadArtifact) {
this.#pageHandle = page;
this.#artifact = artifact;
}
page(): Page {
return this.#pageHandle;
}
url(): string {
return this.#artifact.url;
}
suggestedFilename(): string {
return this.#artifact.suggestedFilename;
}
saveAs(path: string): Promise<void> {
return this.#artifact.saveAs(path);
}
path(): Promise<string> {
return this.#artifact.path();
}
failure(): Promise<string | null> {
return this.#artifact.failure();
}
cancel(): Promise<void> {
return this.#artifact.cancel();
}
delete(): Promise<void> {
return this.#artifact.delete();
}
}
class Page {
readonly label: string;
readonly spaceId: number;
readonly mouse: PageMouse;
readonly keyboard: PageKeyboard;
readonly #task: TaskSpace;
readonly #services: PageModelServices;
readonly #spaceName: string;
#targetId?: string;
#openedBy?: PageOrigin;
#pendingFileChooser?: PendingFileChooser;
#pendingDownload?: PendingDownload;
#activeDownload?: Promise<void>;
constructor(
task: TaskSpace,
label: string,
services: PageModelServices,
entry?: ManagedPage,
) {
this.label = label;
this.spaceId = task.id;
this.#task = task;
this.#spaceName = task.name;
this.#services = services;
this.#targetId = entry?.targetId;
this.#openedBy = entry?.openedBy;
this.keyboard = new PageKeyboard(
this.#services,
(operation) =>
this.#runRawAction((sessionId) => operation(this.#services, sessionId)),
(operation) =>
this.#runObservedAction((sessionId) =>
operation(this.#services, sessionId),
),
);
this.mouse = new PageMouse(
(operation) =>
this.#runRawAction((sessionId) => operation(this.#services, sessionId)),
(operation) =>
this.#runObservedAction((sessionId) =>
operation(this.#services, sessionId),
),
() => this.keyboard.modifierMask(),
);
}
get targetId(): string | undefined {
return this.#targetId;
}
get openedBy(): PageOrigin | undefined {
return this.#openedBy;
}
async goto(
url: string,
options: PageGotoOptions = {},
): Promise<PageActionReceipt> {
assertUrl(url);
validatePublicApiOptions("Page.goto", options);
const timeoutMs = options.timeout ?? 15_000;
const waitUntil = options.waitUntil ?? "load";
const page = await this.#resolve();
const deadline = this.#services.now() + timeoutMs;
const { receipt } = await this.#runActionBoundary(
page,
async (sessionId) => {
await navigateInPage(this.#services, sessionId, url, {
referer: options.referer,
timeoutMs,
waitUntil,
deadline,
});
},
{ inputAction: false, sessionTimeoutMs: timeoutMs },
);
return receipt;
}
async reload(options: PageReloadOptions = {}): Promise<PageActionReceipt> {
validatePublicApiOptions("Page.reload", options);
const timeoutMs = options.timeout ?? 15_000;
const waitUntil = options.waitUntil ?? "load";
const page = await this.#resolve();
const deadline = this.#services.now() + timeoutMs;
const { receipt } = await this.#runActionBoundary(
page,
async (sessionId) => {
await reloadInPage(this.#services, sessionId, {
timeoutMs,
waitUntil,
deadline,
});
},
{ inputAction: false, sessionTimeoutMs: timeoutMs },
);
return receipt;
}
async snapshot(options: PageSnapshotOptions = {}): Promise<string> {
validatePublicApiOptions("Page.snapshot", options);
const rootRefId =
options.scope === "subtree" && options.root !== undefined
? parseRef(options.root)
: undefined;
if (options.scope === "subtree" && options.root === undefined) {
throw pageSnapshotOptionsError(
"page.snapshot subtree scope requires root to be a snapshot ref such as @21",
);
}
if (options.scope !== "subtree" && options.root !== undefined) {
throw pageSnapshotOptionsError(
"page.snapshot root is only supported when scope is subtree",
);
}
if (options.root !== undefined && !rootRefId) {
throw pageSnapshotOptionsError(
"page.snapshot root must be a snapshot ref such as @21",
);
}
const page = await this.#resolve();
return this.#withPage(page, async ({ sessionId }) => {
await this.#activate(page.targetId);
let root: number | undefined;
let rootContext:
| {
frameId?: string;
frameProvenance?: "page" | "frame" | "unknown";
}
| undefined;
if (rootRefId) {
const refs = await this.#refMapForAction(
page,
sessionId,
options.root!,
);
const entry = refs.get(rootRefId);
if (!entry) {
throw new ElementResolutionError(
`Unknown ref: ${options.root}`,
"permanent",
);
}
root = entry.backendNodeId;
rootContext = {
...(entry.frameId ? { frameId: entry.frameId } : {}),
...(entry.frameProvenance
? { frameProvenance: entry.frameProvenance }
: {}),
};
}
const { root: _root, ...snapshotOptions } = options;
const snapshotScope = options.scope ?? "only_within_viewport";
const capture = async () => {
const result = await this.#services.snapshot({
...snapshotOptions,
...(root === undefined ? {} : { root }),
scope: snapshotScope,
includeActionMarks: options.includeActionMarks ?? true,
includeStableLocator: options.includeStableLocator ?? true,
});
const iframeSessions =
Array.isArray(result?.refs) && result.refs.length > 0
? await this.#frameSessions(page)
: new Map<string, string>();
await preparePageSnapshotResult(
this.#services,
sessionId,
iframeSessions,
result,
rootContext,
);
return result;
};
// An input action that triggers a navigation returns before the new
// document commits, so the commit often lands inside the very next
// snapshot. A capture that straddles a commit mixes two documents and is
// discarded; the documents read after it are the baseline for the retry,
// which captures the stable new document in the common case.
let documents = await this.#pageDocuments(page, sessionId);
let result: Awaited<ReturnType<typeof capture>>;
for (let attempt = 0; ; attempt += 1) {
const beforeDocuments = documents;
result = await capture();
documents = await this.#pageDocuments(page, sessionId);
if (rootRefId) {
// The subtree root is a node of the document the refs were taken
// from. Once that document is gone, a retry would capture whatever
// node now carries its backend id, so the root is stale instead.
const registry = this.#services.pageRefs;
registry.invalidateChangedDocuments(page.targetId, documents);
if (registry.isInvalidated(page.targetId, rootRefId)) {
throw new ElementResolutionError(
`Stale ref: @${rootRefId}; take a new snapshot`,
"permanent",
);
}
}
const changed = (result?.refs || []).some((ref) => {
const documentId = pageRefDocumentId(ref, documents);
return (
!documentId ||
documentId !== pageRefDocumentId(ref, beforeDocuments)
);
});
if (!changed) break;
if (attempt >= SNAPSHOT_DOCUMENT_CHANGE_RETRIES) {
throw new ElementResolutionError(
"Page changed during snapshot; take a new snapshot",
"transient",
);
}
}
for (const ref of result?.refs || []) {
ref.documentId = pageRefDocumentId(ref, documents);
}
await this.#loadRefs(page);
this.#services.pageRefs.invalidateChangedDocuments(
page.targetId,
documents,
);
await this.#registerSnapshotRefs(
page,
result,
snapshotScope === "full_page",
);
const content = result?.content || "";
const header = await this.#snapshotHeader(page);
const overflow =
snapshotScope === "only_within_viewport"
? await this.#viewportOverflowHint(sessionId)
: "";
if (!overflow) return `${header}\n${content}`;
// The native content ends with a newline; keep the hint directly below
// the last node instead of after a blank line.
return `${header}\n${content.replace(/\n+$/, "")}\n${overflow}`;
});
}
/**
* One trailing line for a viewport snapshot when the document scrolls past
* the viewport. The native viewport snapshot silently omits offscreen nodes,
* so without this line an agent cannot tell "no result yet" from "the result
* is below the fold". Any failure to read the metrics (pending dialog, lost
* frame, slow page) degrades to no hint rather than a failed snapshot.
*/
async #viewportOverflowHint(sessionId: string): Promise<string> {
if (this.#services.pendingDialog(sessionId)) return "";
let metrics: ViewportMetrics | undefined;
try {
metrics = await evaluateInSession<ViewportMetrics>(
this.#services,
sessionId,
VIEWPORT_METRICS_EXPRESSION,
false,
undefined,
VIEWPORT_METRICS_TIMEOUT_MS,
VIEWPORT_METRICS_TIMEOUT_MS,
);
} catch {
return "";
}
return viewportOverflowHint(metrics);
}
async url(): Promise<string> {
return this.#evaluate("location.href", false);
}
async waitForURL(
expected: PageURLMatcher,
options: PageWaitForURLOptions = {},
): Promise<void> {
validatePublicApiOptions("Page.waitForURL", options);
const page = await this.#resolve();
return this.#services.gate.withPage(page, ({ sessionId }) =>
waitForURLInPage(this.#services, sessionId, expected, options, {
interrupt: (lastUrl, matches) =>
matchingPopupWaitError(this.spaceId, this.label, lastUrl, matches),
}),
);
}
/** Wait for the next popup or download started by this Page. */
waitForEvent(
event: "popup" | "download",
options: PageWaitForEventOptions = {},
): Promise<Page | Download> {
if (event !== "popup" && event !== "download") {
throw new TypeError(
"page.waitForEvent only supports the popup and download events",
);
}
validatePublicApiOptions("Page.waitForEvent", options);
const timeoutMs = options.timeout ?? 10_000;
if (event === "download") return this.#waitForDownload(timeoutMs);
// Subscribe synchronously so the common `const pending = waitForEvent();
// await click()` pattern cannot miss a popup created by the click.
return new Promise<Page>((resolve, reject) => {
let settled = false;
let timer: ReturnType<typeof setTimeout> | undefined;
const finish = (operation: () => void) => {
if (settled) return;
settled = true;
unsubscribe();
if (timer) clearTimeout(timer);
operation();
};
const onNotice = (notice: UnhandledPageNotice) => {
if (
notice.spaceId !== this.spaceId ||
notice.openerLabel !== this.label
) {
return;
}
finish(() => {
markPageObserved(notice.spaceId, notice.targetId);
resolve(this.#task.page(notice.label));
});
};
const unsubscribe = subscribeUnhandledPageNotices(onNotice);
timer = setTimeout(() => {
finish(() =>
reject(
new Error(
`page.waitForEvent("popup") timed out after ${timeoutMs}ms`,
),
),
);
}, timeoutMs);
// Playwright-style event waits observe only future events. Replaying a
// pending notice here can return a popup from an earlier action.
// Resolve the source after arming the listener. A stale Page should fail
// the waiter, but no popup may be lost while that validation is pending.
if (!settled) {
void this.#resolve().catch((error) => finish(() => reject(error)));
}
});
}
#waitForDownload(timeoutMs: number): Promise<Download> {
if (this.#pendingDownload || this.#activeDownload) {
throw new Error("this Page already has an active download wait");
}
const pending: PendingDownload = {
arm: (async () => {
const page = await this.#resolve();
return this.#services.gate.withPage(page, async ({ sessionId }) => {
const interception = this.#services.prepareDownload(page.targetId, {
timeoutMs,
});
try {
await interception.ready(sessionId);
return { page, interception };
} catch (error) {
await interception.dispose(asError(error));
throw error;
}
});
})(),
};
this.#pendingDownload = pending;
return (async () => {
let armed: ArmedDownload | undefined;
try {
armed = await pending.arm;
const artifact = await armed.interception.event;
const active = artifact.finished.finally(() => {
if (this.#activeDownload === active) {
this.#activeDownload = undefined;
}
});
this.#activeDownload = active;
void active.catch(() => {});
return new Download(this, artifact);
} catch (error) {
if (armed) await armed.interception.dispose(asError(error));
throw error;
} finally {
if (this.#pendingDownload === pending) {
this.#pendingDownload = undefined;
}
}
})();
}
/** Wait without activating this Page or occupying the native operation gate. */
async waitForTimeout(timeout: number): Promise<void> {
if (
typeof timeout !== "number" ||
!Number.isFinite(timeout) ||
timeout < 0
) {
throw new TypeError(
"page.waitForTimeout requires a non-negative number of milliseconds",
);
}
await this.#resolve();
await this.#services.sleep(timeout);
}
async title(): Promise<string> {
return this.#evaluate("document.title", false);
}
async info(): Promise<Record<string, unknown>> {
const page = await this.#resolve();
return this.#withPage(page, async ({ sessionId }) => {
const dialog = this.#services.pendingDialog(sessionId);
if (dialog) return { dialog };
return evaluateInSession(
this.#services,
sessionId,
"({url:location.href,title:document.title,w:innerWidth,h:innerHeight,sx:scrollX,sy:scrollY,pw:document.documentElement.scrollWidth,ph:document.documentElement.scrollHeight})",
false,
);
});
}
/** Accept the JavaScript dialog currently blocking this Page, if any. */
async acceptDialog(promptText?: string): Promise<boolean> {
if (promptText !== undefined && typeof promptText !== "string") {
throw new TypeError("page.acceptDialog promptText must be a string");
}
return this.#handleJavaScriptDialog(true, promptText);
}
/** Dismiss the JavaScript dialog currently blocking this Page, if any. */
async dismissDialog(): Promise<boolean> {
return this.#handleJavaScriptDialog(false);
}
async evaluate<T = unknown>(
expression: string | ((argument: any) => T | Promise<T>),
argument?: unknown,
): Promise<T> {
const hasArgument = arguments.length >= 2;
return this.#evaluate(expression, hasArgument, argument, true);
}
/**
* Wait until a Page expression returns a truthy value. Resolves to `true`,
* not that value, and throws on timeout; read Page state afterwards with
* `page.evaluate()`.
*/
async waitForFunction(
expression: string | ((argument: any) => unknown | Promise<unknown>),
argument?: unknown,
options: PageWaitForFunctionOptions = {},
): Promise<true> {
if (
arguments.length === 2 &&
typeof expression === "function" &&
expression.length === 0 &&
looksLikeWaitForFunctionOptions(argument)
) {
const signature = publicApiEntry("Page.waitForFunction")?.signature;
throw new TypeError(
`page.waitForFunction options are the third argument; pass undefined when omitting the callback argument. Expected: ${signature}`,
);
}
validatePublicApiOptions("Page.waitForFunction", options);
// Passing `undefined` is how callers omit the optional argument while
// supplying the third options parameter, matching Playwright's shape.
const hasArgument = arguments.length >= 2 && argument !== undefined;
const serializedArgument = validateEvaluateInput(
"page.waitForFunction",
expression,
hasArgument,
argument,
);
const timeoutMs = options.timeout ?? 10_000;
const pollingMs = options.polling ?? 100;
const source = waitForFunctionExpression(
expression,
hasArgument,
serializedArgument,
);
const page = await this.#resolve();
return this.#services.gate.withPage(page, async ({ sessionId }) => {
await this.#activate(page.targetId);
const deadline = this.#services.now() + timeoutMs;
let lastUrl = "";
let lastTitle = "";
try {
while (this.#services.now() <= deadline) {
const remaining = Math.max(1, deadline - this.#services.now());
const executionTimeoutMs = remaining;
const transportTimeoutMs =
remaining + WAIT_FOR_FUNCTION_TRANSPORT_GRACE_MS;
const evaluationStartedAt = this.#services.now();
try {
let response;
try {
response = await this.#services.cdp(
"Runtime.evaluate",
{
expression: source,
returnByValue: true,
awaitPromise: true,
timeout: executionTimeoutMs,
},
sessionId,
transportTimeoutMs,
);
} catch (error) {
throw normalizeProtocolExecutionTimeout(
error,
this.#services.now() - evaluationStartedAt,
executionTimeoutMs,
);
}
const state = runtimeValue(response, source);
if (isWaitForFunctionState(state)) {
lastUrl = state.url;
lastTitle = state.title;
if (state.matched) return true as const;
}
} catch (error) {
if (isEvaluationExecutionDeadlineError(error)) {
break;
}
if (isRuntimeEvaluateTransportTimeout(error)) {
throw await recoverPageEvaluationTimeout(
this.#services,
sessionId,
"page.waitForFunction",
timeoutMs,
this.label,
);
}
if (!isRetryablePageEvaluationError(error)) {
throw enrichPageCallbackReferenceError(
error,
"page.waitForFunction",
);
}
if (this.#services.now() >= deadline) break;
}
const waitMs = deadline - this.#services.now();
if (waitMs <= 0) break;
await this.#services.sleep(Math.min(pollingMs, waitMs));
}
} finally {
// The predicate may mutate the DOM, so snapshot refs are no longer
// guaranteed to identify the same elements.
await this.#invalidateRefs(page);
}
throw waitForFunctionTimeoutError(
this.spaceId,
this.label,
timeoutMs,
lastUrl,
lastTitle,
);
});
}
/**
* Run window.fetch inside this Page and return a CDP-serializable response.
* Relative URLs, cookies, and service workers use the addressed document's
* browser context. Browser CORS still applies.
*/
async fetch(
url: string,
options: PageFetchOptions = {},
): Promise<PageFetchResponse> {
assertUrl(url);
const { payload, saveAs } = pageFetchPayload(url, options);
const page = await this.#resolve();
return this.#withPage(page, async ({ sessionId }) => {
await this.#activate(page.targetId);
const response = await evaluateInSession<PageFetchResult>(
this.#services,
sessionId,
fetchInPage,
true,
payload,
payload.timeoutMs + 1_000,
);
if ("fetchError" in response) {
throw new Error(response.fetchError);
}
if (!saveAs) return response;
if (typeof response.bodyBase64 !== "string") {
throw new Error("page.fetch received no binary response body");
}
await mkdir(dirname(saveAs), { recursive: true });
await writeFile(saveAs, Buffer.from(response.bodyBase64, "base64"));
const { bodyBase64: _bodyBase64, ...metadata } = response;
return { ...metadata, savedPath: saveAs };
});
}
/** Send one CDP command through this Page's target session. */
async cdp(
method: string,
params: Record<string, unknown> = {},
options: CdpOptions = {},
): Promise<any> {
assertCdpCall("Page.cdp", method, params, options);
const page = await this.#resolve();
return this.#services.gate.withPage(page, async ({ sessionId }) => {
// A modal dialog already belongs to the active Page. Re-activating it
// can hand browser control to the user before CDP gets a chance to close
// the dialog, so send this one command directly to its existing session.
if (method !== "Page.handleJavaScriptDialog") {
await this.#activate(page.targetId);
}
try {
return await this.#services.cdp(
method,
params,
sessionId,
options.timeout,
);
} finally {
// Raw CDP can navigate or mutate the document, so existing refs are no
// longer safe even when the command looked observational.
await this.#invalidateRefs(page);
}
});
}
async waitForSelector(
selector: string,
options: PageWaitForSelectorOptions = {},
): Promise<true> {
validatePublicApiOptions("Page.waitForSelector", options);
const page = await this.#resolve();
return this.#services.gate.withPage(page, async ({ sessionId }) => {
await this.#activate(page.targetId);
const refMap = await this.#refMapForAction(page, sessionId, selector);
return waitForSelectorInPage(
this.#services,
sessionId,
refMap,
selector,
options,
(timeoutMs) => this.#frameSessions(page, timeoutMs),
);
});
}
async waitForLoadState(
state: "domcontentloaded" | "load" | "networkidle" = "load",
options: PageWaitForLoadStateOptions = {},
): Promise<void> {
validatePublicApiOptions("Page.waitForLoadState", options);
const page = await this.#resolve();
return this.#services.gate.withPage(page, async ({ sessionId }) => {
await this.#activate(page.targetId);
try {
await waitForLoadStateInPage(this.#services, sessionId, state, options);
} finally {
await this.#invalidateRefs(page);
}
});
}
/** Drain only CDP events routed to this Page session. */
async events(): Promise<any[]> {
const page = await this.#resolve();
return this.#services.gate.withPage(page, ({ sessionId }) =>
this.#services.drainEvents(sessionId),
);
}
async screenshot(options: PageScreenshotOptions = {}): Promise<string> {
validatePublicApiOptions("Page.screenshot", options);
const { path, fullPage, ...captureOptions } = options;
if (path !== undefined) {
assertAbsoluteFilePath(path, "page.screenshot path");
}
const page = await this.#resolve();
return this.#withPage(page, async ({ sessionId }) => {
await this.#activate(page.targetId);
return this.#services.screenshot(
path,
fullPage === undefined
? captureOptions
: { ...captureOptions, full: fullPage },
sessionId,
);
});
}
async click(
selector: string,
options: PageClickOptions = {},
): Promise<PageActionReceipt> {
validatePublicApiOptions("Page.click", options);
return this.#runAction(
selector,
(sessionId, refMap, iframeSessions, services) =>
clickInPage(
services,
sessionId,
refMap,
selector,
options,
this.keyboard.modifierMask(),
iframeSessions,
),
{
actionName: "page.click",
guardFileChooser: true,
timeout: options.timeout,
},
);
}
async dblclick(
selector: string,
options: Omit<PageClickOptions, "clickCount"> = {},
): Promise<PageActionReceipt> {
validatePublicApiOptions("Page.dblclick", options);
return this.#runAction(
selector,
(sessionId, refMap, iframeSessions, services) =>
clickInPage(
services,
sessionId,
refMap,
selector,
{ ...options, clickCount: 2 },
this.keyboard.modifierMask(),
iframeSessions,
),
{
actionName: "page.dblclick",
guardFileChooser: true,
timeout: options.timeout,
},
);
}
async hover(
selector: string,
options: PageHoverOptions = {},
): Promise<PageActionReceipt> {
validatePublicApiOptions("Page.hover", options);
return this.#runAction(
selector,
(sessionId, refMap, iframeSessions, services) =>
hoverInPage(
services,
sessionId,
refMap,
selector,
options,
this.keyboard.modifierMask(),
iframeSessions,
),
{ actionName: "page.hover", timeout: options.timeout },
);
}
async dragAndDrop(
sourceSelector: string,
targetSelector: string,
options: PageDragAndDropOptions = {},
): Promise<PageActionReceipt> {
validatePublicApiOptions("Page.dragAndDrop", options);
return this.#runAction(
[sourceSelector, targetSelector],
(sessionId, refMap, iframeSessions, services) =>
dragAndDropInPage(
services,
sessionId,
refMap,
sourceSelector,
targetSelector,
options,
this.keyboard.modifierMask(),
iframeSessions,
),
{ actionName: "page.dragAndDrop", timeout: options.timeout },
);
}
async fill(
selector: string,
value: string,
options: PageFillOptions = {},
): Promise<PageActionReceipt> {
validatePublicApiOptions("Page.fill", options);
return this.#runAction(
selector,
(sessionId, refMap, iframeSessions, services) =>
fillInPage(
services,
sessionId,
refMap,
selector,
value,
options,
iframeSessions,
),
{ actionName: "page.fill", timeout: options.timeout },
);
}
async selectOption(
selector: string,
valueOrValues: PageSelectOption | PageSelectOption[] | null,
options: { timeout?: number } = {},
): Promise<string[]> {
validatePublicApiOptions("Page.selectOption", options);
const choices =
valueOrValues === null
? []
: Array.isArray(valueOrValues)
? valueOrValues
: [valueOrValues];
choices.forEach(validateSelectOptionChoice);
const timeoutMs = options.timeout ?? DEFAULT_PAGE_ACTION_TIMEOUT_MS;
const page = await this.#resolve();
return this.#runInputBoundary(page, (sessionId) =>
this.#retryElementAction(
"page.selectOption",
timeoutMs,
sessionId,
(remainingMs) =>
this.#resolveActionTargets(page, sessionId, remainingMs, selector),
({ refMap, iframeSessions }, services) =>
selectOptionInPage(
services,
sessionId,
refMap,
selector,
choices,
iframeSessions,
),
),
);
}
async focus(
selector: string,
options: { timeout?: number } = {},
): Promise<PageActionReceipt> {
validatePublicApiOptions("Page.focus", options);
return this.#runAction(
selector,
(sessionId, refMap, iframeSessions, services) =>
focusInPage(services, sessionId, refMap, selector, iframeSessions),
{ actionName: "page.focus", timeout: options.timeout },
);
}
async press(
selector: string,
chord: string,
options: PagePressOptions = {},
): Promise<PageActionReceipt> {
validatePublicApiOptions("Page.press", options);
const { timeout, ...pressOptions } = options;
return this.#runAction(
selector,
async (sessionId, refMap, iframeSessions, services) => {
await focusInPage(
services,
sessionId,
refMap,
selector,
iframeSessions,
);
await this.keyboard.pressInSession(sessionId, chord, pressOptions);
},
{
actionName: "page.press",
guardFileChooser: true,
timeout,
},
);
}
async setInputFiles(
selector: string,
path: string | string[],
): Promise<PageActionReceipt> {
return this.#runAction(
selector,
(sessionId, refMap, iframeSessions, services) =>
setInputFilesInPage(
services,
sessionId,
refMap,
selector,
path,
iframeSessions,
),
{ actionName: "page.setInputFiles" },
);
}
waitForFileChooser(
options: PageWaitForFileChooserOptions = {},
): Promise<FileChooser> {
validatePublicApiOptions("Page.waitForFileChooser", options);
const timeoutMs = options.timeout ?? 10_000;
if (this.#pendingFileChooser) {
throw new Error("this Page is already waiting for a file chooser");
}
const pending: PendingFileChooser = {
arm: (async () => {
const page = await this.#resolve();
return this.#services.gate.withPage(page, async ({ sessionId }) => {
await this.#activate(page.targetId);
const interception = this.#services.prepareFileChooser(sessionId, {
timeoutMs,
cancel: false,
});
await interception.ready;
return { page, interception };
});
})(),
};
this.#pendingFileChooser = pending;
return (async () => {
let armed: ArmedFileChooser | undefined;
try {
armed = await pending.arm;
const event = await armed.interception.event;
return new FileChooser(this.#services, armed, event);
} catch (error) {
if (armed) await armed.interception.dispose(asError(error));
throw error;
} finally {
if (this.#pendingFileChooser === pending) {
this.#pendingFileChooser = undefined;
}
}
})();
}
async close(): Promise<void> {
const page = await this.#resolve();
await this.#services.gate.withSpace(this.spaceId, async () => {
const tabs = await this.#services.listTabs();
const live = tabs.some((tab) => tab.targetId === page.targetId);
if (!live) {
await this.#services.ledger.closePage(this.spaceId, this.label);
this.#services.pageRefs.clear(page.targetId);
throw new Error(`page ${this.label} was closed`);
}
if (tabs.length <= 1) {
// Ego Lite destroys a task space when its last tab closes, so open a
// blank anchor first. It is SDK-owned: recording it as an agent-origin
// unmanaged tab keeps reconcile from adopting it as a managed Page and
// lets finish() close it instead of protecting it as a user tab.
const anchorTargetId = await this.#services.createTab("about:blank");
try {
await this.#services.ledger.keepUnmanaged(
this.spaceId,
anchorTargetId,
"agent",
);
} catch (error) {
// The original page is still live, so a failed anchor bookkeeping
// write can safely roll the new anchor back before aborting close.
await this.#services
.cdp("Target.closeTarget", { targetId: anchorTargetId })
.catch(() => {});
throw error;
}
}
const result = await this.#services.cdp("Target.closeTarget", {
targetId: page.targetId,
});
if (result?.success !== true) {
throw new Error(`failed to close page ${this.label}`);
}
const disappeared = await waitForTargetToDisappear(
this.#services,
page.targetId,
PAGE_CLOSE_CONFIRM_TIMEOUT_MS,
);
if (!disappeared) {
// Keep the durable label while the native tab still exists. The caller
// can retry close safely instead of leaving an unmanaged orphan.
throw new Error(
`page ${this.label} did not close within ${PAGE_CLOSE_CONFIRM_TIMEOUT_MS}ms`,
);
}
this.#services.invalidateSession(page.targetId);
this.#services.pageRefs.clear(page.targetId);
await this.#services.ledger.closePage(this.spaceId, this.label);
});
}
async #resolve(): Promise<PageTarget> {
const entry = await this.#services.ledger
.getPage(this.spaceId, this.label)
.catch(async (error) => {
throw await this.#task[describeMissingPageToken](this.label, error);
});
this.#targetId = entry.targetId;
this.#openedBy = entry.openedBy;
markPageObserved(this.spaceId, entry.targetId);
return { spaceId: this.spaceId, targetId: entry.targetId };
}
async #evaluate<T>(
expression: string | ((argument: any) => T | Promise<T>),
hasArgument: boolean,
argument?: unknown,
activate = false,
): Promise<T> {
const serializedArgument = validateEvaluateInput(
"page.evaluate",
expression,
hasArgument,
argument,
);
const page = await this.#resolve();
return this.#services.gate.withPage(page, async ({ sessionId }) => {
if (activate) await this.#activate(page.targetId);
try {
return await evaluateInSession<T>(
this.#services,
sessionId,
expression,
hasArgument,
serializedArgument,
pageEvaluateTransportTimeoutMs(),
pageEvaluateExecutionTimeoutMs(),
);
} catch (error) {
if (isEvaluationExecutionDeadlineError(error)) {
throw evaluationExecutionDeadlineError(
"page.evaluate",
pageEvaluateExecutionTimeoutMs(),
);
}
if (isRuntimeEvaluateTransportTimeout(error)) {
throw await recoverPageEvaluationTimeout(
this.#services,
sessionId,
"page.evaluate",
pageEvaluateTransportTimeoutMs(),
this.label,
);
}
if (typeof expression === "function") {
throw enrichPageCallbackReferenceError(error, "page.evaluate");
}
throw error;
}
});
}
async #runAction(
selector: string | string[],
operation: (
sessionId: string,
refMap: RefMap,
iframeSessions: Map<string, string>,
services: PageModelServices,
) => Promise<void>,
options: {
actionName?: string;
guardFileChooser?: boolean;
timeout?: number;
} = {},
): Promise<PageActionReceipt> {
const page = await this.#resolve();
const selectors = Array.isArray(selector) ? selector : [selector];
const timeoutMs = options.timeout ?? DEFAULT_PAGE_ACTION_TIMEOUT_MS;
const actionName = options.actionName ?? "page action";
const { receipt } = await this.#runActionBoundary(
page,
(sessionId) =>
this.#retryElementAction(
actionName,
timeoutMs,
sessionId,
(remainingMs) =>
this.#resolveActionTargets(
page,
sessionId,
remainingMs,
...selectors,
),
({ refMap, iframeSessions }, services) =>
operation(sessionId, refMap, iframeSessions, services),
),
{ guardFileChooser: options.guardFileChooser },
);
return receipt;
}
async #resolveActionTargets(
page: PageTarget,
sessionId: string,
remainingMs: number,
...selectors: string[]
): Promise<{ refMap: RefMap; iframeSessions: Map<string, string> }> {
const refMap = await this.#refMapForAction(page, sessionId, ...selectors);
const iframeSessions = await this.#frameSessions(
page,
Math.max(1, remainingMs),
);
return { refMap, iframeSessions };
}
/**
* Report how long the page has been idle when an action target is missing.
* Returns null while the page is still loading, mutating, fetching, or
* hidden (throttled timers), or when the runtime cannot observe network
* activity, so the action keeps waiting for its full timeout in every
* uncertain case.
*/
async #idleWithoutTarget(
sessionId: string,
): Promise<{ idleMs: number } | null> {
let sessions: string[];
try {
sessions = await this.#services.pageNetworkSessions(
sessionId,
MISSING_TARGET_PROBE_TIMEOUT_MS,
);
} catch {
return null;
}
const network = this.#services.networkActivity(sessions);
if (!network.tracking || network.inflight > 0) return null;
if (
this.#services.now() - network.lastActivityAt <
MISSING_TARGET_IDLE_MS
) {
return null;
}
let probe: DomActivityProbe | undefined;
try {
const response = await this.#services.cdp(
"Runtime.evaluate",
{ expression: DOM_ACTIVITY_PROBE, returnByValue: true },
sessionId,
MISSING_TARGET_PROBE_TIMEOUT_MS,
);
probe = runtimeValue(response, DOM_ACTIVITY_PROBE) as DomActivityProbe;
} catch {
return null;
}
if (
!probe ||
probe.readyState !== "complete" ||
!probe.visible ||
probe.framesLoading ||
typeof probe.idleMs !== "number" ||
probe.idleMs < MISSING_TARGET_IDLE_MS
) {
return null;
}
return { idleMs: Math.round(probe.idleMs) };
}
/**
* Services whose CDP transport reports the first `Input.*` command and
* which carry the action deadline into the driver's scroll loops.
*/
#inputTrackingServices(
deadline: number,
onInput: () => void,
): PageModelServices {
const services = this.#services;
return {
...services,
actionDeadline: deadline,
cdp(method, params, sessionId, timeoutMs) {
if (method.startsWith("Input.")) onInput();
return services.cdp(method, params, sessionId, timeoutMs);
},
};
}
/**
* Retry an element action until its deadline. Until the first `Input.*`
* command reaches the page, failures may include a vanished frame or a lost
* iframe session and are retried after rediscovering frames. Once input was
* dispatched only element-state transients are retried, so a gesture that
* already reached the page is never dispatched twice.
*/
async #retryElementAction<Prepared, Result>(
actionName: string,
timeoutMs: number,
pageSessionId: string,
prepare: (remainingMs: number) => Promise<Prepared>,
perform: (
prepared: Prepared,
services: PageModelServices,
) => Promise<Result>,
): Promise<Result> {
const startedAt = this.#services.now();
const deadline = startedAt + timeoutMs;
let lastBlocker: Error | undefined;
while (true) {
let inputDispatched = false;
const services = this.#inputTrackingServices(deadline, () => {
inputDispatched = true;
});
try {
const prepared = await prepare(deadline - this.#services.now());
return await perform(prepared, services);
} catch (error) {
const remainingMs = deadline - this.#services.now();
// Frame discovery runs on the remaining budget; a transport timeout at
// the deadline is this action's timeout, reported with the last real
// blocker rather than as a CDP failure.
const exhausted =
!inputDispatched &&
isCdpRequestTimeoutError(error) &&
remainingMs <= 0;
const retryable = inputDispatched
? isRetryableElementStateError(error)
: isRetryableResolutionError(error, pageSessionId);
if (!retryable && !exhausted) throw error;
// Idle time counts from this action's start, not from the page's
// last mutation: work started by the previous action may not have
// touched the DOM yet, so the first attempt never gives up early.
if (
!inputDispatched &&
isNoMatchResolutionError(error) &&
this.#services.now() - startedAt >= MISSING_TARGET_IDLE_MS
) {
const idle = await this.#idleWithoutTarget(pageSessionId);
if (idle) {
throw new ElementResolutionError(
`${actionName} failed: ${error.message}. The page has been idle ` +
`for ${idle.idleMs}ms with no DOM changes and no network ` +
`requests in flight, so the target is not about to appear. ` +
`Check the selector against a fresh snapshot, or use ` +
`page.waitForSelector() when the element is expected later.`,
"permanent",
);
}
}
if (remainingMs <= 0) {
const blocker = exhausted && lastBlocker ? lastBlocker : error;
// Report the time the action really took: a slow page can overrun
// the configured timeout, and the configured value is the caller's.
const elapsedMs = this.#services.now() - startedAt;
throw new ElementResolutionError(
`${actionName} timed out after ${elapsedMs}ms: ${blocker.message}`,
"transient",
);
}
lastBlocker = error;
await this.#services.sleep(
Math.min(PAGE_ACTION_RESOLUTION_RETRY_MS, remainingMs),
);
}
}
}
async #runRawAction(
operation: (sessionId: string) => Promise<void>,
): Promise<void> {
const page = await this.#resolve();
try {
await this.#runInputBoundary(page, operation);
} catch (error) {
// A modal dialog is now the page's observable result. The interrupted
// driver stack has already unwound, so no later click/key steps resume
// unexpectedly after the caller handles the dialog.
if (isPageDialogOpenedError(error)) return;
throw error;
}
}
async #runObservedAction(
operation: (sessionId: string) => Promise<void>,
): Promise<PageActionReceipt> {
const page = await this.#resolve();
const { receipt } = await this.#runActionBoundary(page, operation, {
guardFileChooser: true,
});
return receipt;
}
async #handleJavaScriptDialog(
accept: boolean,
promptText?: string,
): Promise<boolean> {
const page = await this.#resolve();
return this.#services.gate.withPage(page, async ({ sessionId }) => {
try {
await this.#services.cdp(
"Page.handleJavaScriptDialog",
{
accept,
...(promptText === undefined ? {} : { promptText }),
},
sessionId,
);
// A handled dialog resumes its callback and may mutate the DOM. A
// no-dialog response leaves the page untouched, so its refs stay valid.
await this.#invalidateRefs(page);
return true;
} catch (error) {
if (isNoJavaScriptDialogError(error)) return false;
throw error;
}
});
}
async #runInputBoundary<T>(
page: PageTarget,
operation: (sessionId: string) => Promise<T>,
): Promise<T> {
return this.#withPage(page, async ({ sessionId }) => {
await this.#activate(page.targetId);
return operation(sessionId);
});
}
async #runActionBoundary<T>(
page: PageTarget,
operation: (sessionId: string) => Promise<T>,
{
guardFileChooser = false,
// Navigation already waits on page lifecycle; only input actions need
// the ordering round trip to see a popup created by the input itself.
inputAction = true,
sessionTimeoutMs,
}: {
guardFileChooser?: boolean;
inputAction?: boolean;
sessionTimeoutMs?: number;
} = {},
): Promise<{ value: T; receipt: PageActionReceipt }> {
const explicitDownload = this.#pendingDownload;
const armedDownload = explicitDownload
? await explicitDownload.arm
: undefined;
if (armedDownload && armedDownload.page.targetId !== page.targetId) {
throw new Error("download waiter belongs to a different Page");
}
const explicitFileChooser = guardFileChooser
? this.#pendingFileChooser
: undefined;
if (explicitFileChooser) {
const armed = await explicitFileChooser.arm;
if (armed.page.targetId !== page.targetId) {
throw new Error("file chooser waiter belongs to a different Page");
}
}
return this.#withPage(
page,
async ({ sessionId }) => {
await this.#activate(page.targetId);
await armedDownload?.interception.ready(sessionId);
const fileChooserGuard =
guardFileChooser && !explicitFileChooser
? this.#services.prepareFileChooser(sessionId, {
timeoutMs: 1_000,
cancel: true,
})
: undefined;
const popupWatch = await this.#watchPopups(inputAction);
try {
await fileChooserGuard?.ready;
const fileInputProbe = fileChooserGuard
? await this.#installFileInputProbe(sessionId)
: false;
let actionError: unknown;
let value: T | undefined;
try {
value = await operation(sessionId);
} catch (error) {
actionError = error;
}
if (isPageDialogOpenedError(actionError)) {
const dialog =
this.#services.pendingDialog(sessionId) || actionError.dialog;
return {
value: undefined as T,
receipt: { dialog },
};
}
if (!inputAction) {
await this.#services.sleep(NAVIGATION_FRAME_SETTLE_MS);
}
let popupError: unknown;
let popups: PopupReceiptEntry[] = [];
try {
popups = await popupWatch.collect(sessionId);
} catch (error) {
popupError = error;
}
if (fileChooserGuard && !fileChooserGuard.peek()) {
const clickedFileInput = fileInputProbe
? await this.#readFileInputProbe(sessionId)
: null;
if (clickedFileInput !== false) {
await Promise.race([
fileChooserGuard.event.catch(() => {}),
this.#services.sleep(
clickedFileInput
? FILE_CHOOSER_OPEN_WAIT_MS
: FILE_CHOOSER_SETTLE_MS,
),
]);
}
}
if (fileChooserGuard?.peek()) {
throw unhandledFileChooserError();
}
if (actionError) throw actionError;
if (popupError) throw popupError;
return {
value: value as T,
receipt: popups.length > 0 ? { popups } : {},
};
} finally {
popupWatch.dispose();
await fileChooserGuard?.dispose();
}
},
{ sessionTimeoutMs },
);
}
/**
* Start observing popups for one action. With target discovery the watch
* records Target.targetCreated events and costs nothing when no popup
* opens; without it the watch falls back to diffing the tab list.
*/
async #watchPopups(inputAction: boolean): Promise<PopupWatch> {
const discovering = await this.#services.ensureTargetDiscovery(
this.spaceId,
);
if (!discovering) return this.#pollingPopupWatch();
const created = new Map<string, string>();
const stop = this.#services.subscribeBrowserEvents((event) => {
const info = event?.params?.targetInfo;
if (
event?.method !== "Target.targetCreated" ||
info?.type !== "page" ||
typeof info.targetId !== "string"
) {
return;
}
created.set(info.targetId, typeof info.url === "string" ? info.url : "");
});
return {
dispose: stop,
collect: async (sessionId) => {
if (created.size === 0 && inputAction) {
// A popup opened by the input is created by the browser before this
// page answers any later command, so one round trip delivers its
// Target.targetCreated event ahead of the response.
await this.#syncWithPage(sessionId);
}
if (created.size === 0) return [];
return this.#adoptCreatedPopups(created);
},
};
}
async #pollingPopupWatch(): Promise<PopupWatch> {
const before = new Set(
(await this.#services.listTabs()).map((tab) => tab.targetId),
);
return {
dispose: () => {},
collect: async () => {
// A popup is normally created synchronously by the input event. A short
// settle covers native tab-list propagation without turning this into a
// navigation wait or silently changing the page's active state.
await this.#services.sleep(POPUP_POLL_SETTLE_MS);
const created = new Map<string, string>();
for (const tab of await this.#services.listTabs()) {
if (!before.has(tab.targetId)) created.set(tab.targetId, tab.url);
}
if (created.size === 0) return [];
return this.#adoptCreatedPopups(created);
},
};
}
/** Returns true when the main document accepted the file-input click probe. */
async #installFileInputProbe(sessionId: string): Promise<boolean> {
try {
const response = await this.#services.cdp(
"Runtime.evaluate",
{ expression: FILE_INPUT_PROBE_INSTALL, returnByValue: true },
sessionId,
POPUP_SYNC_TIMEOUT_MS,
);
return runtimeValue(response, FILE_INPUT_PROBE_INSTALL) === true;
} catch {
return false;
}
}
/** Returns whether a click reached a file input, or null when unknown. */
async #readFileInputProbe(sessionId: string): Promise<boolean | null> {
try {
const response = await this.#services.cdp(
"Runtime.evaluate",
{ expression: FILE_INPUT_PROBE_READ, returnByValue: true },
sessionId,
POPUP_SYNC_TIMEOUT_MS,
);
const value = runtimeValue(response, FILE_INPUT_PROBE_READ);
return typeof value === "boolean" ? value : null;
} catch {
return null;
}
}
async #syncWithPage(sessionId: string): Promise<void> {
try {
await this.#services.cdp(
"Runtime.evaluate",
{ expression: "0", returnByValue: true },
sessionId,
POPUP_SYNC_TIMEOUT_MS,
);
} catch {
// A navigating or dialog-blocked page cannot answer; the popup, if any,
// is adopted by background discovery or the next task.tabs().
}
}
async #adoptCreatedPopups(
created: Map<string, string>,
): Promise<PopupReceiptEntry[]> {
// Target discovery is browser-wide; the tab list scopes it to this space.
let tabs = await this.#services.listTabs();
for (let attempt = 0; attempt < POPUP_TAB_LIST_RETRIES; attempt += 1) {
const known = new Set(tabs.map((tab) => tab.targetId));
if ([...created.keys()].every((targetId) => known.has(targetId))) break;
await this.#services.sleep(POPUP_TAB_LIST_RETRY_MS);
tabs = await this.#services.listTabs();
}
const popups: PopupReceiptEntry[] = [];
for (const [targetId, eventUrl] of created) {
const tab = tabs.find((candidate) => candidate.targetId === targetId);
if (!tab) continue;
const url = tab.url || eventUrl;
try {
const managed = await this.#services.ledger.addPage(
this.spaceId,
targetId,
{ openedBy: "agent" },
);
recordDiscoveredPage(this.spaceId, managed, this.label, url);
popups.push({ label: managed.label, targetId: managed.targetId });
} catch (error) {
// Background discovery may have adopted the same target first.
const label =
typeof this.#services.ledger.read === "function"
? labelForTarget(
await this.#services.ledger.read(this.spaceId),
targetId,
)
: undefined;
if (!label) throw error;
popups.push({ label, targetId });
}
}
return popups;
}
/**
* Frame discovery for this Page. Every call site goes through here so an
* unresponsive Page is always reported with its label and recovery steps,
* whichever step of an operation first ran into it.
*/
/**
* Run one public call on this Page. A renderer-bound command that the Page
* never answered is the same condition frame discovery reports as an
* unresponsive Page, so it is described the same way: the pending
* navigation is stopped and the error names the Page and what it shows.
*/
async #withPage<T>(
page: PageTarget,
operation: (context: PageExecutionContext) => T | Promise<T>,
options: WithPageOptions = {},
): Promise<T> {
let activeSessionId: string | undefined;
try {
return await this.#services.gate.withPage(
page,
(context) => {
activeSessionId = context.sessionId;
return operation(context);
},
options,
);
} catch (error) {
if (
!isCdpRequestTimeoutError(error) ||
isPageUnresponsiveError(error) ||
isBrowserLevelMethod(error.method)
) {
throw error;
}
throw await this.#describeUnresponsivePage(
new PageUnresponsiveError(
error.method,
error.timeoutMs,
page.targetId,
error.sessionId ?? activeSessionId,
),
page,
);
}
}
async #frameSessions(
page: PageTarget,
timeoutMs?: number,
): Promise<Map<string, string>> {
try {
return await this.#services.ensureFrameSessions(page.targetId, timeoutMs);
} catch (error) {
if (!isPageUnresponsiveError(error)) throw error;
throw await this.#describeUnresponsivePage(error, page);
}
}
/**
* Replace the runtime's generic "did not answer" error with one that names
* this Page and what happened next. The wait that just ran out was this
* call's whole budget, so a main-frame navigation that did not commit in it
* is stopped here, as goto does at its own timeout: the Page then answers
* again instead of costing every later call another full wait. Thrown from
* here so the stack shows the public call instead of a transport timer.
*/
async #describeUnresponsivePage(
error: PageUnresponsiveError,
page: PageTarget,
): Promise<PageUnresponsiveError> {
let url: string | undefined;
try {
// The browser process answers Target.* on its own, so this cannot stall.
// Its URL may already show the pending navigation, so do not call it
// the committed one.
const response = await this.#services.cdp(
"Target.getTargetInfo",
{ targetId: page.targetId },
undefined,
PAGE_UNRESPONSIVE_URL_TIMEOUT_MS,
);
const info = response?.result?.targetInfo || response?.targetInfo;
if (typeof info?.url === "string" && info.url.length > 0) url = info.url;
} catch {
// The URL is a courtesy; the error stands without it.
}
const recovered = error.sessionId
? await this.#stopPendingNavigation(error.sessionId)
: "unknown";
const described = new PageUnresponsiveError(
error.method,
error.waitedMs,
page.targetId,
error.sessionId,
);
const head = `Page ${this.label}${url ? ` (${url})` : ""} did not answer ${error.method} within ${error.waitedMs}ms`;
if (recovered === "hung") {
described.message = `${head}, and still did not answer after Page.stopLoading, so its renderer is probably hung. Continue on a new Page from task.newPage(), or close this Page and open it again.`;
} else if (recovered === "unknown") {
described.message =
`${head}. ` +
"Chromium suspends every renderer command while a main-frame navigation has started but not committed, and a hung renderer behaves the same way. " +
'Retry once the pending navigation commits, call page.goto(url, { timeout }) with a longer timeout, stop the pending navigation with page.cdp("Page.stopLoading"), or continue on a new Page from task.newPage().';
} else {
described.stopped = true;
described.currentUrl = recovered.url;
described.message =
`${head}: a main-frame navigation had started but not committed. ` +
`It was stopped once that wait ran out, so the Page answers again${recovered.url ? ` and shows ${recovered.url}` : ""}. ` +
"Retry the call to continue on that document; to load the pending one, call page.goto(url, { timeout }) with a longer timeout, or continue on a new Page from task.newPage().";
}
return described;
}
async #stopPendingNavigation(
sessionId: string,
): Promise<{ url?: string } | "hung"> {
return stopPendingNavigationInSession(this.#services, sessionId);
}
async #refMapForAction(
page: PageTarget,
sessionId: string,
...selectors: string[]
): Promise<RefMap> {
if (!selectors.some((selector) => parseRef(selector))) {
return this.#services.pageRefs.forTarget(page.targetId);
}
await this.#loadRefs(page);
const registry = this.#services.pageRefs;
registry.invalidateChangedDocuments(
page.targetId,
await this.#pageDocuments(page, sessionId),
);
await this.#saveRefs(page);
const refs = registry.forTarget(page.targetId);
for (const selector of selectors) {
const refId = parseRef(selector);
if (!refId) continue;
if (registry.isInvalidated(page.targetId, refId)) {
throw new ElementResolutionError(
`Stale ref: @${refId}; take a new snapshot`,
"permanent",
);
}
if (!refs.get(refId)) {
throw new ElementResolutionError(
`Unknown ref: ${refId}; take a new snapshot`,
"permanent",
);
}
}
return refs;
}
async #pageDocuments(
page: PageTarget,
sessionId: string,
): Promise<Map<string, string>> {
const iframeSessions = await this.#frameSessions(page);
const readTree = async (session: string) => {
const response = await this.#services.cdp(
"Page.getFrameTree",
{},
session,
);
return (response.result || response).frameTree;
};
const tree = await readTree(sessionId);
const documents = new Map<string, string>();
const visit = (node) => {
if (
typeof node?.frame?.id !== "string" ||
typeof node.frame.loaderId !== "string"
)
return;
documents.set(
node.frame.id,
JSON.stringify([
tree.frame.id,
tree.frame.loaderId,
node.frame.id,
node.frame.loaderId,
]),
);
for (const child of node.childFrames || []) visit(child);
};
visit(tree);
const root = documents.get(tree?.frame?.id);
if (!root)
throw new ElementResolutionError(
"Cannot verify Page document; take a new snapshot",
"transient",
);
// The Page tree excludes out-of-process frames. A failed session read must
// reach the retry boundary, not persistently expire refs in a live frame.
const frames = await Promise.all(
[...new Set(iframeSessions.values())]
.filter((session) => session !== sessionId)
.map(readTree),
);
for (const frame of frames) visit(frame);
documents.set("", root);
return documents;
}
async #loadRefs(page: PageTarget): Promise<void> {
const stored = await this.#services.ledger.getPage(
page.spaceId,
this.label,
);
this.#services.pageRefs.restore(page.targetId, stored.refs);
}
async #saveRefs(page: PageTarget): Promise<void> {
const refs = this.#services.pageRefs.exportState(page.targetId);
if (refs)
await this.#services.ledger.setPageRefs(page.spaceId, this.label, refs);
}
async #invalidateRefs(page: PageTarget): Promise<void> {
await this.#loadRefs(page);
this.#services.pageRefs.invalidate(page.targetId);
await this.#saveRefs(page);
}
async #registerSnapshotRefs(
page: PageTarget,
result: any,
replace: boolean,
): Promise<RefMap> {
const nativeRefs = result?.refs || [];
const originalIds = nativeRefs.map((ref) =>
String(ref.refId ?? ref.backendNodeId),
);
const registry = this.#services.pageRefs;
const refs = replace
? registry.replace(page.targetId, nativeRefs)
: registry.merge(page.targetId, nativeRefs);
if (typeof result?.content === "string") {
result.content = rewriteSnapshotRefIds(
result.content,
new Map(
nativeRefs.map((ref, index) => [
originalIds[index],
String(ref.refId),
]),
),
);
}
await this.#saveRefs(page);
return refs;
}
async #snapshotHeader(page: PageTarget): Promise<string> {
try {
const [tabs, ledger] = await Promise.all([
this.#services.listTabs(),
this.#services.ledger.read(this.spaceId),
]);
return snapshotSourceHeader({
currentLabel: this.label,
currentTargetId: page.targetId,
ledger,
pageBudget: this.#services.pageBudget,
spaceId: this.spaceId,
spaceName: this.#spaceName,
tabs,
});
} catch {
return `[${this.label} | space ${JSON.stringify(this.#spaceName)}(${this.spaceId})]`;
}
}
async #activate(targetId: string): Promise<void> {
await this.#services.cdp("Target.activateTarget", { targetId });
this.#services.setPreferredTarget(targetId);
}
}
type ViewportMetrics = {
viewportHeight: number;
scrollY: number;
pageHeight: number;
};
const VIEWPORT_METRICS_TIMEOUT_MS = 2_000;
// Keep the field names distinct from page.info() so test doubles and log
// readers can tell the two reads apart.
const VIEWPORT_METRICS_EXPRESSION =
"({viewportHeight:innerHeight,scrollY:scrollY,pageHeight:document.documentElement.scrollHeight})";
/**
* Describe scrollable content outside the viewport, or return "" when the
* viewport already shows the whole document. Sub-pixel differences from
* fractional layout are ignored so a page that fits never gets a hint.
*/
export function viewportOverflowHint(metrics: unknown): string {
if (!metrics || typeof metrics !== "object") return "";
const { viewportHeight, scrollY, pageHeight } = metrics as Record<
string,
unknown
>;
if (
!Number.isFinite(viewportHeight) ||
!Number.isFinite(scrollY) ||
!Number.isFinite(pageHeight)
) {
return "";
}
const top = Math.round(scrollY as number);
const viewport = Math.round(viewportHeight as number);
const page = Math.round(pageHeight as number);
const above = top > 1;
const below = page - (top + viewport) > 1;
if (!above && !below) return "";
const where = above && below ? "above and below" : above ? "above" : "below";
return `[more content ${where} the viewport: scrollY=${top}, viewport ${viewport} / page ${page} px; scroll or use snapshot({ scope: "full_page" })]`;
}
function pageSnapshotOptionsError(message: string): TypeError {
const signature = publicApiEntry("Page.snapshot")?.signature;
return new TypeError(`${message}. Expected: ${signature}`);
}
function snapshotSourceHeader(input: {
currentLabel: string;
currentTargetId: string;
ledger: PageLedger;
pageBudget: number;
spaceId: number;
spaceName: string;
tabs: RuntimeTab[];
}): string {
const tabsByTarget = new Map(input.tabs.map((tab) => [tab.targetId, tab]));
const managedTargets = new Set(
Object.values(input.ledger.pages).map((page) => page.targetId),
);
const current = tabsByTarget.get(input.currentTargetId);
const currentTitle = compactPageTitle(
current?.title || current?.url || "untitled",
);
const pages = Object.entries(input.ledger.pages).map(([label, page]) => {
const tab = tabsByTarget.get(page.targetId);
const title = compactPageTitle(tab?.title || tab?.url || "untitled");
return `${label}${page.targetId === input.currentTargetId ? "*" : ""} ${JSON.stringify(title)}`;
});
const untracked = input.tabs.filter(
(tab) => !managedTargets.has(tab.targetId),
).length;
const managed = pages.length;
const budget =
managed >= input.pageBudget - 1
? ` | budget ${managed}/${input.pageBudget}`
: "";
const inventory = pages.length > 0 ? ` — ${pages.join(", ")}` : "";
return `[${input.currentLabel} ${JSON.stringify(currentTitle)} | space ${JSON.stringify(input.spaceName)}(${input.spaceId}): ${managed} managed, ${untracked} untracked${inventory}${budget}]`;
}
async function evaluateInSession<T>(
services: PageModelServices,
sessionId: string,
expression: string | ((argument: any) => T | Promise<T>),
hasArgument: boolean,
serializedArgument?: unknown,
timeoutMs?: number,
executionTimeoutMs?: number,
): Promise<T> {
const startedAt = services.now();
if (typeof expression === "string") {
let response;
try {
response = await services.cdp(
"Runtime.evaluate",
{
expression,
returnByValue: true,
awaitPromise: true,
...(executionTimeoutMs === undefined
? {}
: { timeout: executionTimeoutMs }),
},
sessionId,
timeoutMs,
);
} catch (error) {
throw normalizeProtocolExecutionTimeout(
error,
services.now() - startedAt,
executionTimeoutMs,
);
}
return runtimeValue(response, expression) as T;
}
const source = expression.toString();
let response;
try {
response = await services.cdp(
"Runtime.evaluate",
{
expression: `(async function __egoPageEvaluate() {
return await ${pageFunctionCallExpression(
source,
hasArgument,
serializedArgument,
)};
})()`,
returnByValue: true,
awaitPromise: true,
...(executionTimeoutMs === undefined
? {}
: { timeout: executionTimeoutMs }),
},
sessionId,
timeoutMs,
);
} catch (error) {
throw normalizeProtocolExecutionTimeout(
error,
services.now() - startedAt,
executionTimeoutMs,
);
}
return runtimeValue(response, source) as T;
}
function validateEvaluateInput(
apiName: string,
expression: unknown,
hasArgument: boolean,
argument: unknown,
): unknown {
if (typeof expression !== "string" && typeof expression !== "function") {
throw new TypeError(`${apiName} expects a function or string expression`);
}
if (typeof expression === "string") {
if (expression.length === 0) {
throw new TypeError(`${apiName} expression must not be empty`);
}
if (hasArgument) {
throw new TypeError(
`${apiName} string expression does not accept an argument`,
);
}
return undefined;
}
return hasArgument ? serializeEvaluateArgument(apiName, argument) : undefined;
}
function serializeEvaluateArgument(
apiName: string,
argument: unknown,
): unknown {
return serializeJsonValue(
argument,
`${apiName} argument must be JSON-serializable`,
);
}
function waitForFunctionExpression(
expression: string | ((argument: any) => unknown | Promise<unknown>),
hasArgument: boolean,
serializedArgument: unknown,
): string {
if (typeof expression === "string") {
return `(async function __egoWaitForFunction() { return { matched: Boolean(await (${expression})), url: location.href, title: document.title }; })()`;
}
const source = expression.toString();
return `(async function __egoWaitForFunction() { return { matched: Boolean(await ${pageFunctionCallExpression(
source,
hasArgument,
serializedArgument,
)}), url: location.href, title: document.title }; })()`;
}
function pageFunctionCallExpression(
source: string,
hasArgument: boolean,
serializedArgument: unknown,
): string {
if (!hasArgument) return `(${source})()`;
const json = JSON.stringify(serializedArgument);
return `(${source})(JSON.parse(${JSON.stringify(json)}))`;
}
function isWaitForFunctionState(
value: unknown,
): value is { matched: boolean; url: string; title: string } {
if (!value || typeof value !== "object") return false;
const state = value as Record<string, unknown>;
return (
typeof state.matched === "boolean" &&
typeof state.url === "string" &&
typeof state.title === "string"
);
}
function waitForFunctionTimeoutError(
spaceId: number,
pageLabel: string,
timeoutMs: number,
lastUrl: string,
lastTitle: string,
): Error {
const title = lastTitle
? `; last title was ${JSON.stringify(lastTitle)}`
: "";
const popup = peekUnhandledPageNotices().find(
(notice) => notice.spaceId === spaceId && notice.openerLabel === pageLabel,
);
const popupHint = popup
? ` Popup ${popup.label} opened from ${pageLabel} at ${JSON.stringify(popup.url)}; inspect task.page(${JSON.stringify(popup.label)}) before retrying the preceding action.`
: "";
return new Error(
`page.waitForFunction timed out after ${timeoutMs}ms on page ${pageLabel}; last URL was ${JSON.stringify(lastUrl)}${title}.${popupHint}`,
);
}
function isRetryablePageEvaluationError(error: unknown): boolean {
if (!(error instanceof Error)) return false;
return (
error.message.includes("Execution context was destroyed") ||
error.message.includes("Cannot find context with specified id") ||
error.message.includes("Inspected target navigated")
);
}
function isRuntimeEvaluateTransportTimeout(error: unknown): boolean {
return (
(isCdpRequestTimeoutError(error) && error.method === "Runtime.evaluate") ||
(error instanceof Error &&
error.message.includes("CDP request timed out: Runtime.evaluate"))
);
}
function isEvaluationExecutionDeadlineError(error: unknown): boolean {
return (
error instanceof Error &&
(error as Error & { code?: string }).code === "EGO_PAGE_EXECUTION_DEADLINE"
);
}
function isProtocolExecutionTimeout(
error: unknown,
elapsedMs?: number,
timeoutMs?: number,
): boolean {
return (
error instanceof Error &&
(/Execution was terminated|Script execution timed out/i.test(
error.message,
) ||
(error.message === "Internal error" &&
timeoutMs !== undefined &&
elapsedMs !== undefined &&
elapsedMs >= timeoutMs - 100))
);
}
function normalizeProtocolExecutionTimeout(
error: unknown,
elapsedMs: number,
timeoutMs?: number,
): unknown {
if (!isProtocolExecutionTimeout(error, elapsedMs, timeoutMs)) return error;
if (error instanceof Error) {
(error as Error & { code?: string }).code = "EGO_PAGE_EXECUTION_DEADLINE";
}
return error;
}
function evaluationExecutionDeadlineError(
apiName: string,
timeoutMs: number,
): PageEvaluationTimeoutError {
return new PageEvaluationTimeoutError(
`${apiName} exceeded its ${timeoutMs}ms page-execution safety limit. The current JavaScript execution was stopped and the Page is ready for another command, but work scheduled earlier may still produce late side effects.`,
{
timeoutMs,
executionStopped: true,
mayHaveLateEffects: true,
pageResponsive: true,
},
);
}
async function recoverPageEvaluationTimeout(
services: PageModelServices,
sessionId: string,
apiName: string,
timeoutMs: number,
label?: string,
): Promise<PageEvaluationTimeoutError> {
if (await pageEvaluationHealthProbe(services, sessionId)) {
return new PageEvaluationTimeoutError(
`${apiName} timed out after ${timeoutMs}ms while waiting for page JavaScript. The Page is responsive, but the evaluation is still pending and may produce late side effects. Reload or close the Page when that would be unsafe.`,
{
timeoutMs,
executionStopped: false,
mayHaveLateEffects: true,
pageResponsive: true,
},
);
}
let terminationSent = false;
try {
await services.cdp(
"Runtime.terminateExecution",
{},
sessionId,
PAGE_EVALUATE_TERMINATE_TIMEOUT_MS,
);
terminationSent = true;
} catch (error) {
if (!isCdpRequestTimeoutError(error)) throw error;
}
if (
terminationSent &&
(await pageEvaluationHealthProbe(services, sessionId))
) {
return new PageEvaluationTimeoutError(
`${apiName} timed out after ${timeoutMs}ms and the renderer stopped responding. The current execution was stopped and the Page recovered, but work scheduled earlier may still produce late side effects.`,
{
timeoutMs,
executionStopped: true,
mayHaveLateEffects: true,
pageResponsive: true,
},
);
}
// Nothing from the renderer: the usual cause is a main-frame navigation
// that has not committed. The wait that ran out was this call's budget, so
// stop that navigation as the other public calls do; the evaluation itself
// was never confirmed stopped and may still run on the document in place.
const recovered = await stopPendingNavigationInSession(services, sessionId);
if (recovered !== "hung") {
const error = new PageEvaluationTimeoutError(
`${apiName}${label ? ` on Page ${label}` : ""} timed out after ${timeoutMs}ms: a main-frame navigation had started but not committed, so the Page answered nothing. It was stopped once that wait ran out, so the Page answers again${recovered.url ? ` and shows ${recovered.url}` : ""}; the evaluation may still run late on that document. Retry the call to continue on that document; to load the pending one, call page.goto(url, { timeout }) with a longer timeout, or continue on a new Page from task.newPage().`,
{
timeoutMs,
executionStopped: false,
mayHaveLateEffects: true,
pageResponsive: true,
},
);
error.stopped = true;
error.currentUrl = recovered.url;
return error;
}
return new PageEvaluationTimeoutError(
`${apiName}${label ? ` on Page ${label}` : ""} timed out after ${timeoutMs}ms and the Page is still unresponsive, even after Page.stopLoading, so its renderer is probably hung. Execution could not be confirmed stopped; continue on a new Page from task.newPage(), or close this Page and open it again.`,
{
timeoutMs,
executionStopped: false,
mayHaveLateEffects: true,
pageResponsive: false,
},
);
}
/**
* Stop whatever main-frame navigation keeps a Page from answering and read
* its main frame once it does. "hung" means the Page still answers nothing: a
* hung renderer, which Page.stopLoading cannot help.
*/
async function stopPendingNavigationInSession(
services: PageModelServices,
sessionId: string,
): Promise<{ url?: string } | "hung"> {
try {
await services.cdp(
"Page.stopLoading",
{},
sessionId,
PAGE_UNRESPONSIVE_STOP_TIMEOUT_MS,
);
const response = await services.cdp(
"Page.getFrameTree",
{},
sessionId,
PAGE_UNRESPONSIVE_STOP_TIMEOUT_MS,
);
const frame =
response?.result?.frameTree?.frame || response?.frameTree?.frame;
return {
...(typeof frame?.url === "string" && frame.url.length > 0
? { url: frame.url }
: {}),
};
} catch {
return "hung";
}
}
function isBrowserLevelMethod(method: string): boolean {
return method.startsWith("Target.") || method.startsWith("Browser.");
}
async function pageEvaluationHealthProbe(
services: PageModelServices,
sessionId: string,
): Promise<boolean> {
for (let attempt = 0; attempt < 2; attempt += 1) {
const startedAt = services.now();
try {
const response = await services.cdp(
"Runtime.evaluate",
{
expression: "1",
returnByValue: true,
awaitPromise: false,
timeout: PAGE_EVALUATE_HEALTH_EXECUTION_TIMEOUT_MS,
},
sessionId,
PAGE_EVALUATE_HEALTH_TIMEOUT_MS,
);
return response?.result?.value === 1;
} catch (error) {
if (isRuntimeEvaluateTransportTimeout(error)) return false;
if (
attempt === 0 &&
isProtocolExecutionTimeout(
error,
services.now() - startedAt,
PAGE_EVALUATE_HEALTH_EXECUTION_TIMEOUT_MS,
)
) {
// Runtime.terminateExecution may consume the next evaluation rather
// than the original one. Probe once more before declaring the Page bad.
continue;
}
throw error;
}
}
return false;
}
function enrichPageCallbackReferenceError(
error: unknown,
apiName: string,
): unknown {
if (
!(error instanceof Error) ||
!/\bReferenceError: .* is not defined/.test(error.message) ||
error.message.includes("cannot access variables from the Node.js script")
) {
return error;
}
error.message +=
`\n${apiName}() callbacks run inside the Page and cannot access variables ` +
"from the Node.js script. Define the value inside the callback or pass " +
"JSON data as the second argument.";
return error;
}
function serializeJsonValue(value: unknown, message: string): unknown {
try {
const json = JSON.stringify(value, (_key, item) => {
if (
typeof item === "bigint" ||
(typeof item === "number" && !Number.isFinite(item))
) {
throw new TypeError("unsupported value");
}
return item;
});
if (json === undefined) throw new TypeError("unsupported value");
return JSON.parse(json);
} catch (error) {
throw new TypeError(message, { cause: error });
}
}
// The script does not run in the caller's shell working directory: the host
// starts the Node context from the application's own directory, which is not
// writable on Windows. Reject relative paths up front instead of surfacing a
// confusing EPERM from that directory.
function assertAbsoluteFilePath(path: unknown, label: string): void {
if (typeof path !== "string" || path.length === 0 || !isAbsolute(path)) {
throw new TypeError(`${label} must be an absolute file path`);
}
}
function pageFetchPayload(
url: string,
options: PageFetchOptions,
): { payload: PageFetchPayload; saveAs?: string } {
validatePublicApiOptions("Page.fetch", options);
const { timeout = 20_000, saveAs, ...requestOptions } = options;
if (saveAs !== undefined) {
assertAbsoluteFilePath(saveAs, "page.fetch saveAs");
}
return {
payload: {
url,
options: serializeJsonValue(
requestOptions,
"page.fetch options must be JSON-serializable",
) as Record<string, unknown>,
timeoutMs: timeout,
responseType: saveAs ? "base64" : "text",
},
...(saveAs ? { saveAs } : {}),
};
}
async function fetchInPage({
url,
options,
timeoutMs,
responseType,
}: PageFetchPayload): Promise<PageFetchResult> {
const controller = new AbortController();
const timer = window.setTimeout(() => controller.abort(), timeoutMs);
try {
const response = await window.fetch(url, {
...options,
signal: controller.signal,
});
const headers: Record<string, string> = {};
response.headers.forEach((value, key) => {
headers[key] = value;
});
const metadata = {
ok: response.ok,
status: response.status,
statusText: response.statusText,
url: response.url,
headers,
};
if (responseType === "text") {
return { ...metadata, body: await response.text() };
}
const bytes = new Uint8Array(await response.arrayBuffer());
let binary = "";
for (let offset = 0; offset < bytes.length; offset += 0x8000) {
binary += String.fromCharCode(...bytes.subarray(offset, offset + 0x8000));
}
return { ...metadata, bodyBase64: btoa(binary) } as PageFetchResult;
} catch (error) {
if (controller.signal.aborted) {
return { fetchError: `page.fetch timed out after ${timeoutMs}ms` };
}
let requestUrl = url;
try {
requestUrl = new URL(url, window.location.href).href;
} catch {
// Keep the caller's URL when it cannot be resolved in the Page.
}
const detail =
error instanceof Error
? `${error.name}: ${error.message}`
: String(error);
return {
fetchError:
`page.fetch uses window.fetch and obeys browser CORS. ` +
`Request ${JSON.stringify(requestUrl)} from ${JSON.stringify(window.location.origin)} failed: ${detail}`,
};
} finally {
window.clearTimeout(timer);
}
}
function looksLikeWaitForFunctionOptions(value: unknown): boolean {
if (!value || typeof value !== "object" || Array.isArray(value)) return false;
const keys = Object.keys(value);
return (
keys.length > 0 &&
keys.every((key) => key === "timeout" || key === "polling")
);
}
function isNoJavaScriptDialogError(error: unknown): boolean {
if (!(error instanceof Error)) return false;
return /no (?:javascript )?dialog (?:is showing|is open|to handle)/i.test(
error.message,
);
}
function validateSelectOptionChoice(
choice: PageSelectOption,
index: number,
): void {
const path = `page.selectOption valueOrValues[${index}]`;
if (typeof choice === "string") return;
if (!choice || typeof choice !== "object" || Array.isArray(choice)) {
throw new TypeError(
`${path} must be a string or an object with value, label, or index`,
);
}
const keys = Object.keys(choice);
const unknown = keys.find(
(key) => key !== "value" && key !== "label" && key !== "index",
);
if (unknown) {
throw new TypeError(`${path} has unknown field ${JSON.stringify(unknown)}`);
}
if (
choice.value === undefined &&
choice.label === undefined &&
choice.index === undefined
) {
throw new TypeError(`${path} must specify value, label, or index`);
}
if (choice.value !== undefined && typeof choice.value !== "string") {
throw new TypeError(`${path}.value must be a string`);
}
if (choice.label !== undefined && typeof choice.label !== "string") {
throw new TypeError(`${path}.label must be a string`);
}
if (
choice.index !== undefined &&
(!Number.isInteger(choice.index) || choice.index < 0)
) {
throw new TypeError(`${path}.index must be a non-negative integer`);
}
}
function assertUrl(url: string): void {
if (typeof url !== "string" || url.length === 0) {
throw new TypeError("page URL must be a non-empty string");
}
}
function asError(value: unknown): Error {
return value instanceof Error ? value : new Error(String(value));
}
function unhandledFileChooserError(): Error & { code: string } {
const error = new Error(
"This action opened a file chooser, which was cancelled before the system dialog appeared. " +
"Use page.setInputFiles() for an existing file input, or call " +
"page.waitForFileChooser() before the action when the input is created dynamically.",
) as Error & { code: string };
error.code = "EGO_FILE_CHOOSER_OPENED";
return error;
}
function matchingPopupWaitError(
spaceId: number,
openerLabel: string,
lastUrl: string,
matches: (url: string) => boolean,
): (Error & { code: string }) | undefined {
const popup = peekUnhandledPageNotices().find(
(notice) =>
notice.spaceId === spaceId &&
notice.openerLabel === openerLabel &&
typeof notice.url === "string" &&
matches(notice.url),
);
if (!popup) return undefined;
const error = new Error(
`page ${openerLabel} did not navigate from ${JSON.stringify(lastUrl)}, ` +
`but popup ${popup.label} opened from it at ${JSON.stringify(popup.url)}. ` +
`The triggering action already succeeded; do not repeat it. Continue with ` +
`task.page(${JSON.stringify(popup.label)}).`,
) as Error & { code: string };
error.code = "EGO_URL_OPENED_IN_POPUP";
return error;
}
function assertCdpCall(
apiName: "Page.cdp" | "TaskSpace.cdp",
method: string,
params: Record<string, unknown>,
options: CdpOptions,
): void {
if (typeof method !== "string" || method.length === 0) {
throw new TypeError("cdp method must be a non-empty string");
}
if (!params || typeof params !== "object" || Array.isArray(params)) {
throw new TypeError("cdp params must be an object");
}
assertNoSessionIdInParams(
apiName,
method,
params,
`${apiName} selects its own session and cannot route to another one.`,
);
validatePublicApiOptions(apiName, options);
}
async function waitForTargetToDisappear(
services: PageModelServices,
targetId: string,
timeoutMs: number,
): Promise<boolean> {
const deadline = services.now() + timeoutMs;
while (true) {
const tabs = await services.listTabs();
if (!tabs.some((tab) => tab.targetId === targetId)) return true;
const remaining = deadline - services.now();
if (remaining <= 0) return false;
await services.sleep(Math.min(PAGE_CLOSE_CONFIRM_INTERVAL_MS, remaining));
}
}
function tabInventory(
task: TaskSpace,
services: PageModelServices,
ledger: PageLedger,
tabs: RuntimeTab[],
): TabInventoryItem[] {
const managedByTarget = new Map(
Object.entries(ledger.pages).map(([label, entry]) => [
entry.targetId,
{ label, entry },
]),
);
return tabs.map((tab) => {
const managed = managedByTarget.get(tab.targetId);
if (!managed) {
const openedBy = ledger.unmanagedTargets[tab.targetId] || "unknown";
return {
targetId: tab.targetId,
page: new UnmanagedPage(
task,
tab.targetId,
openedBy,
unmanagedPageConstructorToken,
),
title: tab.title || "",
url: tab.url || "",
active: Boolean(tab.active),
openedBy,
};
}
const entry = { label: managed.label, ...managed.entry };
return {
targetId: tab.targetId,
label: managed.label,
page: new Page(task, managed.label, services, entry),
title: tab.title || "",
url: tab.url || "",
active: Boolean(tab.active),
openedBy: entry.openedBy,
};
});
}
function assertUnmanagedPage(page: unknown): asserts page is UnmanagedPage {
if (!(page instanceof UnmanagedPage)) {
throw new TypeError(
"task.adopt requires an untracked page returned by task.tabs()",
);
}
}
function pageBudgetError(
task: TaskSpace,
limit: number,
ledger: PageLedger,
tabs: RuntimeTab[],
): PageBudgetError {
const tabsByTarget = new Map(tabs.map((tab) => [tab.targetId, tab]));
const entries = Object.entries(ledger.pages);
const lines = entries.map(([label, page]) => {
const tab = tabsByTarget.get(page.targetId);
const title = compactPageTitle(tab?.title || tab?.url || "untitled");
return ` ${label.padEnd(6)} ${JSON.stringify(title)}${tab?.active ? " active" : ""}`;
});
const suggestion = entries[0]?.[0] || "p1";
return new PageBudgetError(
task.id,
limit,
[
`Page budget reached (${entries.length}/${limit}) in space ${JSON.stringify(task.name)}.`,
"",
...lines,
"",
`Close: await task.page('${suggestion}').close()`,
`Reuse: await task.page('${suggestion}').goto(url)`,
].join("\n"),
);
}
function compactPageTitle(value: string): string {
const compact = value.replace(/\s+/g, " ").trim();
return compact.length > 80 ? `${compact.slice(0, 77)}...` : compact;
}
function configuredPageBudget(): number {
const configured = Number(process.env.EGO_BROWSER_PAGE_BUDGET || 8);
return Number.isInteger(configured) && configured > 0 ? configured : 8;
}