@parcha/pi-browser 0.1.0-beta.33 → 0.1.0-beta.35
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +25 -0
- package/NOTICE +30 -0
- package/README.md +33 -199
- package/dist/core/contract.d.ts +9 -6
- package/dist/core/contract.js +22 -34
- package/dist/core/custody.d.ts +6 -0
- package/dist/core/custody.js +6 -0
- package/dist/core/driving-guard.js +17 -2
- package/dist/core/evidence.d.ts +1 -1
- package/dist/core/files.d.ts +3 -0
- package/dist/core/files.js +4 -1
- package/dist/core/host.d.ts +0 -63
- package/dist/core/redact.d.ts +0 -2
- package/dist/core/redact.js +0 -32
- package/dist/core/session-control.d.ts +0 -2
- package/dist/core/session-control.js +0 -5
- package/dist/core/tools.d.ts +9 -0
- package/dist/core/usage.d.ts +0 -2
- package/dist/core/usage.js +0 -2
- package/dist/core/web.d.ts +2 -111
- package/dist/core/web.js +0 -443
- package/dist/driver/cdp.d.ts +6 -0
- package/dist/driver/cdp.js +245 -0
- package/dist/driver/page-driver.js +29 -3
- package/dist/durable/store.js +13 -4
- package/dist/durable.d.ts +2 -12
- package/dist/durable.js +9 -80
- package/dist/index.d.ts +0 -1
- package/dist/index.js +0 -1
- package/dist/live-view/provider.d.ts +2 -2
- package/dist/live-view/provider.js +2 -2
- package/dist/live-view/server.d.ts +1 -1
- package/dist/providers/browserbase.d.ts +0 -31
- package/dist/providers/browserbase.js +2 -27
- package/dist/providers/chrome-args.d.ts +3 -0
- package/dist/providers/chrome-args.js +13 -2
- package/dist/testing/backend.d.ts +2 -21
- package/dist/testing/backend.js +1 -25
- package/dist/vendor/browser-use-pi/ax.d.ts +106 -0
- package/dist/vendor/browser-use-pi/ax.js +642 -0
- package/dist/vendor/browser-use-pi/cdp.d.ts +19 -0
- package/dist/vendor/browser-use-pi/cdp.js +106 -0
- package/dist/vendor/browser-use-pi/page.d.ts +38 -0
- package/dist/vendor/browser-use-pi/page.js +112 -0
- package/package.json +7 -25
- package/src/vendor/browser-use-pi/UPSTREAM.json +25 -0
- package/src/vendor/stagehand-facade/UPSTREAM.json +0 -4
- package/dist/coding-agent-local.d.ts +0 -12
- package/dist/coding-agent-local.js +0 -72
- package/dist/coding-agent.d.ts +0 -135
- package/dist/coding-agent.js +0 -641
- package/dist/core/sites.d.ts +0 -33
- package/dist/core/sites.js +0 -104
- package/dist/durable/web.d.ts +0 -47
- package/dist/durable/web.js +0 -41
- package/dist/providers/assigned.d.ts +0 -18
- package/dist/providers/assigned.js +0 -75
- package/dist/recorder/credential.d.ts +0 -9
- package/dist/recorder/credential.js +0 -49
- package/dist/recorder/follow.d.ts +0 -36
- package/dist/recorder/follow.js +0 -163
- package/dist/recorder/frames.d.ts +0 -87
- package/dist/recorder/frames.js +0 -377
- package/dist/recorder/index.d.ts +0 -4
- package/dist/recorder/index.js +0 -7
- package/dist/recorder/jpeg.d.ts +0 -8
- package/dist/recorder/jpeg.js +0 -70
- package/dist/recorder/track.d.ts +0 -107
- package/dist/recorder/track.js +0 -280
- package/dist/vendor/stagehand-facade/screenshot-transport.d.ts +0 -15
- package/dist/vendor/stagehand-facade/screenshot-transport.js +0 -51
package/dist/core/sites.d.ts
DELETED
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
import type { ToolContract } from "./contract.js";
|
|
2
|
-
import type { ToolOutput } from "./custody.js";
|
|
3
|
-
import type { ChildRunner, SiteInfo, SiteRunPort } from "./host.js";
|
|
4
|
-
export declare const MAX_SITE_INSTRUCTIONS = 16000;
|
|
5
|
-
/** The site enum and the description render from the listing, which is configuration: every request sends the same
|
|
6
|
-
* text and keeps the prompt cache. */
|
|
7
|
-
export declare function useWebsiteContract(sites: readonly SiteInfo[]): ToolContract;
|
|
8
|
-
/** Only the declared shape crosses back to the model: a port's extra fields, such as capabilities or URLs, are dropped
|
|
9
|
-
* here, and an answer with both shapes or neither is no answer. */
|
|
10
|
-
export declare function siteAnswer(raw: unknown): {
|
|
11
|
-
run: {
|
|
12
|
-
id: string;
|
|
13
|
-
state: string;
|
|
14
|
-
};
|
|
15
|
-
} | {
|
|
16
|
-
report: string;
|
|
17
|
-
} | null;
|
|
18
|
-
export declare function useWebsite(args: {
|
|
19
|
-
site?: unknown;
|
|
20
|
-
request_id?: unknown;
|
|
21
|
-
instructions?: unknown;
|
|
22
|
-
}, port: SiteRunPort, sites: readonly SiteInfo[], ctx: {
|
|
23
|
-
signal: AbortSignal;
|
|
24
|
-
runChild: ChildRunner;
|
|
25
|
-
}): Promise<ToolOutput>;
|
|
26
|
-
export type LocalSite = {
|
|
27
|
-
title: string;
|
|
28
|
-
description: string;
|
|
29
|
-
startUrl: string;
|
|
30
|
-
};
|
|
31
|
-
/** `use_website` for a plain pi user: each task runs a child agent through the adapter's `runChild` and answers its
|
|
32
|
-
* report. A request_id runs once; a failed run may be asked again. */
|
|
33
|
-
export declare function localSites(sites: Record<string, LocalSite>): SiteRunPort;
|
package/dist/core/sites.js
DELETED
|
@@ -1,104 +0,0 @@
|
|
|
1
|
-
import { browserFailure } from "./failures.js";
|
|
2
|
-
export const MAX_SITE_INSTRUCTIONS = 16_000;
|
|
3
|
-
const UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
|
4
|
-
const RUN_ID = /^[A-Za-z0-9_.:-]{1,200}$/;
|
|
5
|
-
const RUN_STATE = /^[a-z_]{1,40}$/;
|
|
6
|
-
/** The site enum and the description render from the listing, which is configuration: every request sends the same
|
|
7
|
-
* text and keeps the prompt cache. */
|
|
8
|
-
export function useWebsiteContract(sites) {
|
|
9
|
-
return {
|
|
10
|
-
name: "use_website",
|
|
11
|
-
label: "Use a website",
|
|
12
|
-
description: [
|
|
13
|
-
"Hand a task to an agent that works on one of these websites in its own browser, and get back what it found or did:",
|
|
14
|
-
...sites.map((site) => `- ${site.site}: ${site.title}. ${site.description}`),
|
|
15
|
-
"Give a new request_id (a UUID) for each new task. Calling again with the request_id of a finished task answers its result without running it again; a task that failed runs again, and may repeat what it already did on the site.",
|
|
16
|
-
"Several calls in one turn run at once, each agent in its own browser.",
|
|
17
|
-
].join("\n"),
|
|
18
|
-
parameters: {
|
|
19
|
-
type: "object", additionalProperties: false, required: ["site", "request_id", "instructions"],
|
|
20
|
-
properties: {
|
|
21
|
-
site: { type: "string", enum: sites.map((site) => site.site), description: "The website to use." },
|
|
22
|
-
request_id: { type: "string", description: "A UUID you choose for this task." },
|
|
23
|
-
instructions: { type: "string", maxLength: MAX_SITE_INSTRUCTIONS, description: "What to do on the website and what to report back." },
|
|
24
|
-
},
|
|
25
|
-
},
|
|
26
|
-
effect: "effect", replay: "safe", executionMode: "parallel", evidence: "none", listedWhen: "sites",
|
|
27
|
-
};
|
|
28
|
-
}
|
|
29
|
-
/** Only the declared shape crosses back to the model: a port's extra fields, such as capabilities or URLs, are dropped
|
|
30
|
-
* here, and an answer with both shapes or neither is no answer. */
|
|
31
|
-
export function siteAnswer(raw) {
|
|
32
|
-
if (!raw || typeof raw !== "object")
|
|
33
|
-
return null;
|
|
34
|
-
const { run, report } = raw;
|
|
35
|
-
if ((run === undefined) === (report === undefined))
|
|
36
|
-
return null;
|
|
37
|
-
if (report !== undefined)
|
|
38
|
-
return typeof report === "string" ? { report } : null;
|
|
39
|
-
const { id, state } = (run ?? {});
|
|
40
|
-
return typeof id === "string" && RUN_ID.test(id) && typeof state === "string" && RUN_STATE.test(state) ? { run: { id, state } } : null;
|
|
41
|
-
}
|
|
42
|
-
const siteOutput = (body, isError = false) => ({ content: [{ type: "text", text: JSON.stringify(body) }], ...(isError ? { isError: true } : {}) });
|
|
43
|
-
const siteRefusal = (message) => siteOutput(browserFailure("refused", message), true);
|
|
44
|
-
export async function useWebsite(args, port, sites, ctx) {
|
|
45
|
-
const site = sites.find((known) => known.site === args.site);
|
|
46
|
-
if (!site)
|
|
47
|
-
return siteRefusal(`site is one of: ${sites.map((known) => known.site).join(", ")}`);
|
|
48
|
-
if (typeof args.request_id !== "string" || !UUID.test(args.request_id))
|
|
49
|
-
return siteRefusal("request_id is a UUID you choose for this task");
|
|
50
|
-
const instructions = typeof args.instructions === "string" ? args.instructions.trim() : "";
|
|
51
|
-
if (!instructions || instructions.length > MAX_SITE_INSTRUCTIONS)
|
|
52
|
-
return siteRefusal(`instructions say what to do on the site, in at most ${MAX_SITE_INSTRUCTIONS} characters`);
|
|
53
|
-
const head = { ok: true, site: site.site, request_id: args.request_id };
|
|
54
|
-
let raw;
|
|
55
|
-
try {
|
|
56
|
-
raw = await port.use({ site: site.site, requestId: args.request_id, instructions }, ctx);
|
|
57
|
-
}
|
|
58
|
-
catch {
|
|
59
|
-
// A port's error text can carry its grant or a URL: none of it is forwarded.
|
|
60
|
-
if (ctx.signal.aborted)
|
|
61
|
-
return siteOutput({ ok: false, code: "aborted", retryable: true, effect: "possibly_effected", message: "The call was cancelled, and the site's task may already have changed the site. Calling again with the same request_id runs an unfinished task again, and it may repeat what it did." }, true);
|
|
62
|
-
return siteOutput({ ok: false, code: "command_failed", retryable: true, effect: "possibly_effected", message: `The ${site.site} task failed, and may already have changed the site. Calling use_website again with the same request_id runs it again, and it may repeat what it did.` }, true);
|
|
63
|
-
}
|
|
64
|
-
const parsed = siteAnswer(raw);
|
|
65
|
-
if (!parsed)
|
|
66
|
-
return siteOutput({ ok: false, code: "command_failed", retryable: false, effect: "possibly_effected", message: `The ${site.site} runner answered neither a started task nor a report.` }, true);
|
|
67
|
-
if ("report" in parsed)
|
|
68
|
-
return siteOutput({ ...head, report: parsed.report });
|
|
69
|
-
return siteOutput({ ...head, run: parsed.run, next: "The site's task was started; its result arrives through your host. Do not call use_website again for this request." });
|
|
70
|
-
}
|
|
71
|
-
const childPrompt = (site, entry, instructions) => [
|
|
72
|
-
`You are doing one task on ${entry.title} (${site}) for another agent, in your own browser.`,
|
|
73
|
-
`Start at ${entry.startUrl}.`,
|
|
74
|
-
`Task: ${instructions}`,
|
|
75
|
-
"Use the browser tools. Do not ask questions. Finish with a short report of what you found or did.",
|
|
76
|
-
].join("\n");
|
|
77
|
-
/** `use_website` for a plain pi user: each task runs a child agent through the adapter's `runChild` and answers its
|
|
78
|
-
* report. A request_id runs once; a failed run may be asked again. */
|
|
79
|
-
export function localSites(sites) {
|
|
80
|
-
for (const [site, entry] of Object.entries(sites)) {
|
|
81
|
-
if (!/^https?:\/\//.test(entry.startUrl))
|
|
82
|
-
throw new Error(`site ${site}: startUrl must be an http or https URL`);
|
|
83
|
-
}
|
|
84
|
-
const runs = new Map();
|
|
85
|
-
return {
|
|
86
|
-
async list() {
|
|
87
|
-
return Object.entries(sites).map(([site, entry]) => ({ site, title: entry.title, description: entry.description }));
|
|
88
|
-
},
|
|
89
|
-
async use({ site, requestId, instructions }, { signal, runChild }) {
|
|
90
|
-
const entry = Object.hasOwn(sites, site) ? sites[site] : undefined;
|
|
91
|
-
if (!entry)
|
|
92
|
-
throw new Error(`no site named ${site}`);
|
|
93
|
-
const key = `${site}\u0000${requestId}`;
|
|
94
|
-
let run = runs.get(key);
|
|
95
|
-
if (!run) {
|
|
96
|
-
run = runChild({ site, requestId, prompt: childPrompt(site, entry, instructions) }, signal);
|
|
97
|
-
runs.set(key, run);
|
|
98
|
-
run.catch(() => { if (runs.get(key) === run)
|
|
99
|
-
runs.delete(key); });
|
|
100
|
-
}
|
|
101
|
-
return { report: await run };
|
|
102
|
-
},
|
|
103
|
-
};
|
|
104
|
-
}
|
package/dist/durable/web.d.ts
DELETED
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
import { defineExtension, type ConversationId, type ToolRegistration } from "@earendil-works/pi-durable";
|
|
2
|
-
import type { Decisions } from "../core/decisions.js";
|
|
3
|
-
import type { EvidenceSink } from "../core/evidence.js";
|
|
4
|
-
import type { FetchRequest, FetchResult, PriceTable, SearchRequest, SearchResult } from "../core/host.js";
|
|
5
|
-
import { type FetchSource, type RenderedPage, type SearchSource, type WebBackup, type WebToolsOptions } from "../core/web.js";
|
|
6
|
-
export type WebExtensionOptions = {
|
|
7
|
-
providers: {
|
|
8
|
-
/** Names the source in `via` (`<name>_fetch`, `<name>_search`); default "provider". */
|
|
9
|
-
name?: string;
|
|
10
|
-
fetch?: (request: FetchRequest, signal?: AbortSignal) => Promise<FetchResult>;
|
|
11
|
-
search?: (request: SearchRequest, signal?: AbortSignal) => Promise<SearchResult>;
|
|
12
|
-
};
|
|
13
|
-
/** Searches tried before `providers.search`, in order (see `SearchSource`). */
|
|
14
|
-
searchSources?: readonly SearchSource[];
|
|
15
|
-
/** Readers tried before `providers.fetch` for a markdown read, in order (see `FetchSource`). */
|
|
16
|
-
fetchSources?: readonly FetchSource[];
|
|
17
|
-
evidence: EvidenceSink;
|
|
18
|
-
/** Per use: evidence label and proxies. Default: label "web", proxies on. */
|
|
19
|
-
scope?: (conversationId: ConversationId) => {
|
|
20
|
-
label: string;
|
|
21
|
-
proxies: boolean;
|
|
22
|
-
} | Promise<{
|
|
23
|
-
label: string;
|
|
24
|
-
proxies: boolean;
|
|
25
|
-
}>;
|
|
26
|
-
prices?: Partial<PriceTable>;
|
|
27
|
-
redact?: {
|
|
28
|
-
values: () => readonly string[];
|
|
29
|
-
};
|
|
30
|
-
/** The host's fallback fetchers, tried in order when the provider cannot fetch a page. */
|
|
31
|
-
backups?: ReadonlyArray<WebBackup>;
|
|
32
|
-
decisions?: Pick<Decisions, "classifyPage">;
|
|
33
|
-
render?: {
|
|
34
|
-
enabled: boolean;
|
|
35
|
-
read(url: string, signal?: AbortSignal): Promise<RenderedPage>;
|
|
36
|
-
};
|
|
37
|
-
pdf?: WebToolsOptions["pdf"];
|
|
38
|
-
/** How long the screening of a search answer's snippets may take in all (see `WebToolsOptions`). */
|
|
39
|
-
snippetScreenMs?: number;
|
|
40
|
-
section?: {
|
|
41
|
-
addendum?: string;
|
|
42
|
-
};
|
|
43
|
-
};
|
|
44
|
-
export declare function createWebExtension(options: WebExtensionOptions): {
|
|
45
|
-
extension: ReturnType<typeof defineExtension>;
|
|
46
|
-
tools: readonly ToolRegistration[];
|
|
47
|
-
};
|
package/dist/durable/web.js
DELETED
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
import { defineExtension, defineTool } from "@earendil-works/pi-durable";
|
|
2
|
-
import { WEB_TOOLS } from "../core/contract.js";
|
|
3
|
-
import { providerKeyValues } from "../core/session-control.js";
|
|
4
|
-
import { toolUsage } from "../core/usage.js";
|
|
5
|
-
import { createWebTools } from "../core/web.js";
|
|
6
|
-
import { forModel } from "./result.js";
|
|
7
|
-
export function createWebExtension(options) {
|
|
8
|
-
const tools = createWebTools({
|
|
9
|
-
name: options.providers.name ?? "provider",
|
|
10
|
-
fetch: options.providers.fetch,
|
|
11
|
-
search: options.providers.search,
|
|
12
|
-
searchSources: options.searchSources,
|
|
13
|
-
fetchSources: options.fetchSources,
|
|
14
|
-
evidence: options.evidence,
|
|
15
|
-
scope: options.scope ?? (() => ({ label: "web", proxies: true })),
|
|
16
|
-
prices: options.prices,
|
|
17
|
-
redact: { values: () => providerKeyValues(process.env, options.redact?.values) },
|
|
18
|
-
backups: options.backups,
|
|
19
|
-
classifyPage: options.decisions?.classifyPage,
|
|
20
|
-
render: options.render,
|
|
21
|
-
pdf: options.pdf,
|
|
22
|
-
snippetScreenMs: options.snippetScreenMs,
|
|
23
|
-
});
|
|
24
|
-
const run = { web_fetch: tools.fetch, web_search: tools.search };
|
|
25
|
-
const result = ({ text, isError, details, usd }) => ({ ...forModel([{ type: "text", text }]), isError, details: details, usage: toolUsage(usd).usage });
|
|
26
|
-
const registrations = WEB_TOOLS
|
|
27
|
-
.filter((tool) => (tool.listedWhen === "fetch" ? options.providers.fetch || options.render?.enabled || options.backups?.length || options.fetchSources?.length : options.providers.search || options.searchSources?.length))
|
|
28
|
-
.map((tool) => defineTool({
|
|
29
|
-
name: tool.name,
|
|
30
|
-
description: tool.description,
|
|
31
|
-
parameters: tool.parameters,
|
|
32
|
-
replay: tool.replay,
|
|
33
|
-
executionMode: tool.executionMode,
|
|
34
|
-
execute: async (args, api, context) => result(await run[tool.name](api.conversationId, args, context.abortSignal)),
|
|
35
|
-
}));
|
|
36
|
-
// Matched by name: another extension's tool registered under the same name would carry the text too.
|
|
37
|
-
const names = new Set(registrations.map((tool) => tool.name));
|
|
38
|
-
const addendum = options.section?.addendum;
|
|
39
|
-
const sections = addendum ? [{ key: "web", render: async (input) => (input.agent.tools.some((tool) => names.has(tool.name)) ? addendum : undefined) }] : [];
|
|
40
|
-
return { extension: defineExtension({ name: "web", tools: registrations, sections }), tools: registrations };
|
|
41
|
-
}
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import { type BrowserFailure } from "../core/failures.js";
|
|
2
|
-
import type { AttachTarget, BrowserProvider } from "../core/host.js";
|
|
3
|
-
import type { ResourceStatus } from "../core/lease.js";
|
|
4
|
-
export type AssignedSession = {
|
|
5
|
-
/** The session's id: the same for every attach. */
|
|
6
|
-
readonly id: string;
|
|
7
|
-
/** A fresh target for the driver; it names no secret in `sdkCdpUrl` (a relay or a signed URL goes in `dial`). A failure
|
|
8
|
-
* the model should read is thrown as an error carrying `failure`. */
|
|
9
|
-
attach(signal?: AbortSignal): Promise<AttachTarget>;
|
|
10
|
-
status(signal?: AbortSignal): Promise<ResourceStatus>;
|
|
11
|
-
/** Why the session can no longer be used (the host fenced it, took it back, it ended), or null while it can. */
|
|
12
|
-
failure?(): BrowserFailure | null;
|
|
13
|
-
};
|
|
14
|
-
export declare function assignedSessionProvider(input: {
|
|
15
|
-
name: string;
|
|
16
|
-
session: AssignedSession;
|
|
17
|
-
maxLifetimeS?: number;
|
|
18
|
-
}): BrowserProvider;
|
|
@@ -1,75 +0,0 @@
|
|
|
1
|
-
// A provider over the one session a host assigned to the run (Parcha's site child: the session it reserved for this task).
|
|
2
|
-
// It never creates or ends a browser. create hands that session to one custody at a time, every attach asks the host for a
|
|
3
|
-
// fresh target (a relay's capability rotates), and release lets go of that custody's hold without asking the host: a
|
|
4
|
-
// custody releases at a failed launch, an orphan sweep or a conversation's end, and the session is the task's, not the
|
|
5
|
-
// conversation's. The host takes it back itself, once, when the task's browser closes; after that the session's own
|
|
6
|
-
// failure() answers every create. A second create while another custody holds it is a refusal: the run has one browser.
|
|
7
|
-
import { BrowserFailureError, refusedError } from "../core/custody.js";
|
|
8
|
-
import { FAILURE_CODES } from "../core/failures.js";
|
|
9
|
-
/** A host session types a failure the model should read as an error carrying `failure`; the tool loop takes it as is. */
|
|
10
|
-
function typed(error) {
|
|
11
|
-
const failure = error?.failure;
|
|
12
|
-
if (error instanceof BrowserFailureError || !failure || failure.ok !== false || !FAILURE_CODES.includes(failure.code))
|
|
13
|
-
return error;
|
|
14
|
-
return new BrowserFailureError(failure);
|
|
15
|
-
}
|
|
16
|
-
let assignedHolds;
|
|
17
|
-
const assignedHoldOf = (session) => {
|
|
18
|
-
assignedHolds ??= new WeakMap();
|
|
19
|
-
let assignedCustody = assignedHolds.get(session);
|
|
20
|
-
if (!assignedCustody) {
|
|
21
|
-
assignedCustody = { heldBy: null, letGo: new Set() };
|
|
22
|
-
assignedHolds.set(session, assignedCustody);
|
|
23
|
-
}
|
|
24
|
-
return assignedCustody;
|
|
25
|
-
};
|
|
26
|
-
export function assignedSessionProvider(input) {
|
|
27
|
-
const { session } = input;
|
|
28
|
-
const caps = { timeoutModel: "wall-clock", maxLifetimeS: input.maxLifetimeS ?? 21_600, survivesDisconnect: true, releaseIsAsync: false, extension: "preinstalled", fixedSession: true,
|
|
29
|
-
// The host made the session and owns its sign-in: none of the run's session settings is applied, and no saved profile
|
|
30
|
-
// can be loaded into it.
|
|
31
|
-
settings: { proxies: false, verified: false, geolocation: false, region: false, context: false }, profiles: false };
|
|
32
|
-
// A hold its custody let go of is over for that custody; the session goes on for the next one.
|
|
33
|
-
const assignedCustody = assignedHoldOf(session);
|
|
34
|
-
const ours = (ref) => ref.id === session.id;
|
|
35
|
-
return {
|
|
36
|
-
name: input.name,
|
|
37
|
-
caps,
|
|
38
|
-
async create(spec) {
|
|
39
|
-
// A session that can no longer be used says why (a sign-in it needs, the host took it back), never that it is open elsewhere.
|
|
40
|
-
const ended = session.failure?.();
|
|
41
|
-
if (ended)
|
|
42
|
-
throw new BrowserFailureError(ended);
|
|
43
|
-
if (assignedCustody.heldBy !== null && assignedCustody.heldBy !== spec.tag)
|
|
44
|
-
throw refusedError("This task has one browser, the website session assigned to it, and it is already open; keep using it.");
|
|
45
|
-
assignedCustody.heldBy = spec.tag;
|
|
46
|
-
assignedCustody.letGo.delete(spec.tag);
|
|
47
|
-
return { id: session.id, tag: spec.tag };
|
|
48
|
-
},
|
|
49
|
-
findByTag: async (tag) => (assignedCustody.heldBy === tag ? [{ id: session.id, tag }] : []),
|
|
50
|
-
async status(ref, signal) {
|
|
51
|
-
if (!ours(ref))
|
|
52
|
-
return "gone";
|
|
53
|
-
if (assignedCustody.letGo.has(ref.tag))
|
|
54
|
-
return "stopped";
|
|
55
|
-
return session.status(signal);
|
|
56
|
-
},
|
|
57
|
-
async attach(ref, signal) {
|
|
58
|
-
if (!ours(ref))
|
|
59
|
-
throw new Error("this session is not the one the host assigned to the run");
|
|
60
|
-
try {
|
|
61
|
-
return await session.attach(signal);
|
|
62
|
-
}
|
|
63
|
-
catch (error) {
|
|
64
|
-
throw typed(error);
|
|
65
|
-
}
|
|
66
|
-
},
|
|
67
|
-
async release(ref) {
|
|
68
|
-
if (!ours(ref))
|
|
69
|
-
return;
|
|
70
|
-
if (assignedCustody.heldBy === ref.tag)
|
|
71
|
-
assignedCustody.heldBy = null;
|
|
72
|
-
assignedCustody.letGo.add(ref.tag);
|
|
73
|
-
},
|
|
74
|
-
};
|
|
75
|
-
}
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
/** Run in the page: whether any field holding a credential or a one-time code has a value. It looks at password fields, fields that say
|
|
2
|
-
* `one-time-code`, and fields whose name, id, label (a `<label>` for it or around it, or `aria-labelledby`), `aria-label` or placeholder names
|
|
3
|
-
* a code (otp, totp, 2fa, passcode, ...), in the document, in open shadow roots and in same-origin frames. The answer is `true` when one holds
|
|
4
|
-
* a value now (`why: "field"`), or held one since the last check (`why: "typed"`: the page keeps a flag, set by the fields' own `input` events,
|
|
5
|
-
* that a check reports once and clears). Anything it cannot read counts as a credential being present: a page that nests deeper than it reads
|
|
6
|
-
* (`why: "too_deep"`) and a visible frame whose content it cannot reach, a cross-origin frame such as a payment or sign-in widget
|
|
7
|
-
* (`why: "unreadable_frame"`). A frame with no box on the page is not a picture of anything and is not counted. The answer is `{ hold, why }`.
|
|
8
|
-
* What it cannot see at all (a closed shadow root, a value set by script with no event) is the host's to pause with `frames.pause`. (Elements are told by tag, not by class: a frame's elements are of its own realm.) */
|
|
9
|
-
export declare const CREDENTIAL_PROBE = "(() => {\n const named = /(^|[^a-z])(otp|totp|2fa|mfa|passcode|one[-_ ]?time|verification[-_ ]?code|security[-_ ]?code|auth(entication)?[-_ ]?code)([^a-z]|$)/i;\n const words = (el) => {\n let text = [el.name, el.id, el.getAttribute(\"aria-label\") || \"\", el.placeholder || \"\"].join(\" \");\n try { for (const label of el.labels || []) text += \" \" + label.textContent; } catch (error) { /* no labels */ }\n const ids = el.getAttribute(\"aria-labelledby\");\n if (ids) { const root = el.getRootNode(); for (const id of ids.split(/s+/)) { const node = root.getElementById ? root.getElementById(id) : null; if (node) text += \" \" + node.textContent; } }\n return text;\n };\n const like = (el) => el.localName === \"input\" && (el.type === \"password\" || el.autocomplete === \"one-time-code\" || named.test(words(el)));\n const state = window.__piFramesSecret || (window.__piFramesSecret = { seen: false });\n if (!state.listening) {\n state.listening = true;\n addEventListener(\"input\", (event) => { const el = event.target; if (el && el.localName === \"input\" && el.value !== \"\" && like(el)) state.seen = true; }, true);\n }\n let blind = null;\n const shown = (el) => { const style = getComputedStyle(el); return el.clientWidth >= 2 && el.clientHeight >= 2 && style.display !== \"none\" && style.visibility !== \"hidden\"; };\n const scan = (root, depth) => {\n for (const el of root.querySelectorAll(\"*\")) {\n if (el.localName === \"input\" && el.value !== \"\" && like(el)) return true;\n const shadow = el.shadowRoot;\n const frame = el.localName === \"iframe\";\n if (!shadow && !frame) continue;\n if (depth >= 8) { blind = blind || \"too_deep\"; continue; }\n if (shadow && scan(shadow, depth + 1)) return true;\n if (frame) {\n let inside = null;\n try { inside = el.contentDocument; } catch (error) { inside = null; }\n if (inside) { if (scan(inside, depth + 1)) return true; }\n else if (shown(el)) blind = blind || \"unreadable_frame\";\n }\n }\n return false;\n };\n const found = scan(document, 0);\n const seen = state.seen;\n state.seen = false;\n if (found) return { hold: true, why: \"field\" };\n if (seen) return { hold: true, why: \"typed\" };\n return blind ? { hold: true, why: blind } : { hold: false, why: null };\n})()";
|
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
/** Run in the page: whether any field holding a credential or a one-time code has a value. It looks at password fields, fields that say
|
|
2
|
-
* `one-time-code`, and fields whose name, id, label (a `<label>` for it or around it, or `aria-labelledby`), `aria-label` or placeholder names
|
|
3
|
-
* a code (otp, totp, 2fa, passcode, ...), in the document, in open shadow roots and in same-origin frames. The answer is `true` when one holds
|
|
4
|
-
* a value now (`why: "field"`), or held one since the last check (`why: "typed"`: the page keeps a flag, set by the fields' own `input` events,
|
|
5
|
-
* that a check reports once and clears). Anything it cannot read counts as a credential being present: a page that nests deeper than it reads
|
|
6
|
-
* (`why: "too_deep"`) and a visible frame whose content it cannot reach, a cross-origin frame such as a payment or sign-in widget
|
|
7
|
-
* (`why: "unreadable_frame"`). A frame with no box on the page is not a picture of anything and is not counted. The answer is `{ hold, why }`.
|
|
8
|
-
* What it cannot see at all (a closed shadow root, a value set by script with no event) is the host's to pause with `frames.pause`. (Elements are told by tag, not by class: a frame's elements are of its own realm.) */
|
|
9
|
-
export const CREDENTIAL_PROBE = `(() => {
|
|
10
|
-
const named = /(^|[^a-z])(otp|totp|2fa|mfa|passcode|one[-_ ]?time|verification[-_ ]?code|security[-_ ]?code|auth(entication)?[-_ ]?code)([^a-z]|$)/i;
|
|
11
|
-
const words = (el) => {
|
|
12
|
-
let text = [el.name, el.id, el.getAttribute("aria-label") || "", el.placeholder || ""].join(" ");
|
|
13
|
-
try { for (const label of el.labels || []) text += " " + label.textContent; } catch (error) { /* no labels */ }
|
|
14
|
-
const ids = el.getAttribute("aria-labelledby");
|
|
15
|
-
if (ids) { const root = el.getRootNode(); for (const id of ids.split(/\s+/)) { const node = root.getElementById ? root.getElementById(id) : null; if (node) text += " " + node.textContent; } }
|
|
16
|
-
return text;
|
|
17
|
-
};
|
|
18
|
-
const like = (el) => el.localName === "input" && (el.type === "password" || el.autocomplete === "one-time-code" || named.test(words(el)));
|
|
19
|
-
const state = window.__piFramesSecret || (window.__piFramesSecret = { seen: false });
|
|
20
|
-
if (!state.listening) {
|
|
21
|
-
state.listening = true;
|
|
22
|
-
addEventListener("input", (event) => { const el = event.target; if (el && el.localName === "input" && el.value !== "" && like(el)) state.seen = true; }, true);
|
|
23
|
-
}
|
|
24
|
-
let blind = null;
|
|
25
|
-
const shown = (el) => { const style = getComputedStyle(el); return el.clientWidth >= 2 && el.clientHeight >= 2 && style.display !== "none" && style.visibility !== "hidden"; };
|
|
26
|
-
const scan = (root, depth) => {
|
|
27
|
-
for (const el of root.querySelectorAll("*")) {
|
|
28
|
-
if (el.localName === "input" && el.value !== "" && like(el)) return true;
|
|
29
|
-
const shadow = el.shadowRoot;
|
|
30
|
-
const frame = el.localName === "iframe";
|
|
31
|
-
if (!shadow && !frame) continue;
|
|
32
|
-
if (depth >= 8) { blind = blind || "too_deep"; continue; }
|
|
33
|
-
if (shadow && scan(shadow, depth + 1)) return true;
|
|
34
|
-
if (frame) {
|
|
35
|
-
let inside = null;
|
|
36
|
-
try { inside = el.contentDocument; } catch (error) { inside = null; }
|
|
37
|
-
if (inside) { if (scan(inside, depth + 1)) return true; }
|
|
38
|
-
else if (shown(el)) blind = blind || "unreadable_frame";
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
return false;
|
|
42
|
-
};
|
|
43
|
-
const found = scan(document, 0);
|
|
44
|
-
const seen = state.seen;
|
|
45
|
-
state.seen = false;
|
|
46
|
-
if (found) return { hold: true, why: "field" };
|
|
47
|
-
if (seen) return { hold: true, why: "typed" };
|
|
48
|
-
return blind ? { hold: true, why: blind } : { hold: false, why: null };
|
|
49
|
-
})()`;
|
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
export type FollowHandlers = {
|
|
2
|
-
/** One screencast frame of the followed page: JPEG bytes and the page's own width and height as the browser reports them. */
|
|
3
|
-
frame(bytes: Buffer, page: {
|
|
4
|
-
deviceWidth: number;
|
|
5
|
-
deviceHeight: number;
|
|
6
|
-
epoch: number;
|
|
7
|
-
}): void;
|
|
8
|
-
/** The followed page's main frame navigated. */
|
|
9
|
-
navigated(): void;
|
|
10
|
-
/** The socket ended and was not asked to. */
|
|
11
|
-
closed(): void;
|
|
12
|
-
};
|
|
13
|
-
export type Follower = {
|
|
14
|
-
/** Runs an expression in the followed page and gives its value; rejects when there is no page, the page errors or nothing answers in `ms`. */
|
|
15
|
-
evaluate(expression: string, ms?: number): Promise<unknown>;
|
|
16
|
-
/** One screenshot of the followed page as it is now, sized like a screencast frame (JPEG, at most the options' width and height), with the page's own CSS size; rejects when there is no page, the browser refuses, nothing answers
|
|
17
|
-
* in `ms`, or the capture moved to another page meanwhile. For a page that does not repaint, so the screencast sends nothing. */
|
|
18
|
-
capture(ms?: number): Promise<{
|
|
19
|
-
bytes: Buffer;
|
|
20
|
-
page: {
|
|
21
|
-
deviceWidth: number;
|
|
22
|
-
deviceHeight: number;
|
|
23
|
-
epoch: number;
|
|
24
|
-
};
|
|
25
|
-
}>;
|
|
26
|
-
/** Which page, at which moment of its life, the capture is on: it changes when the capture moves to another page and at every main-frame
|
|
27
|
-
* navigation, so a frame held across a change can be told from one of the page now shown. */
|
|
28
|
-
epoch(): number;
|
|
29
|
-
close(): void;
|
|
30
|
-
};
|
|
31
|
-
export type FollowOptions = {
|
|
32
|
-
maxWidth: number;
|
|
33
|
-
maxHeight: number;
|
|
34
|
-
quality: number;
|
|
35
|
-
};
|
|
36
|
-
export declare function followPage(url: string, handlers: FollowHandlers, options: FollowOptions): Follower;
|
package/dist/recorder/follow.js
DELETED
|
@@ -1,163 +0,0 @@
|
|
|
1
|
-
// A read-only screencast of the page an agent is using, over a browser's DevTools socket. It attaches beside whatever else is
|
|
2
|
-
// connected (the driver, the effect observer); every frame is acknowledged at once, whatever is done with it.
|
|
3
|
-
export function followPage(url, handlers, options) {
|
|
4
|
-
const socket = new WebSocket(url);
|
|
5
|
-
let seq = 0;
|
|
6
|
-
let closing = false;
|
|
7
|
-
let current = null;
|
|
8
|
-
let epoch = 0;
|
|
9
|
-
/** Every page the browser has: its address, and when it was last opened or moved (an arrival counter, not a clock). */
|
|
10
|
-
const pages = new Map();
|
|
11
|
-
let moves = 0;
|
|
12
|
-
const waiting = new Map();
|
|
13
|
-
const send = (method, params = {}, sessionId) => {
|
|
14
|
-
const id = ++seq;
|
|
15
|
-
if (socket.readyState === 1)
|
|
16
|
-
socket.send(JSON.stringify({ id, method, params, ...(sessionId ? { sessionId } : {}) }));
|
|
17
|
-
return id;
|
|
18
|
-
};
|
|
19
|
-
const follow = (targetId, url) => {
|
|
20
|
-
if (current?.targetId === targetId)
|
|
21
|
-
return;
|
|
22
|
-
epoch += 1;
|
|
23
|
-
if (current?.sessionId)
|
|
24
|
-
send("Target.detachFromTarget", { sessionId: current.sessionId });
|
|
25
|
-
current = { targetId, sessionId: null, url };
|
|
26
|
-
send("Target.attachToTarget", { targetId, flatten: true });
|
|
27
|
-
};
|
|
28
|
-
/** The page in use: the one most recently opened or moved to an address, a blank one only when no other exists. The browser lists its
|
|
29
|
-
* pages in no order that says which the agent is on, so arrival is what is read. */
|
|
30
|
-
const choose = () => {
|
|
31
|
-
let best = null;
|
|
32
|
-
for (const [id, page] of pages) {
|
|
33
|
-
const blank = page.url === "about:blank" || page.url === "";
|
|
34
|
-
const bestBlank = best !== null && (best.url === "about:blank" || best.url === "");
|
|
35
|
-
if (best === null || (bestBlank && !blank) || (bestBlank === blank && page.moved > best.moved))
|
|
36
|
-
best = { id, url: page.url, moved: page.moved };
|
|
37
|
-
}
|
|
38
|
-
if (best)
|
|
39
|
-
follow(best.id, best.url);
|
|
40
|
-
};
|
|
41
|
-
socket.onopen = () => { send("Target.setDiscoverTargets", { discover: true }); };
|
|
42
|
-
socket.onmessage = (event) => {
|
|
43
|
-
let message;
|
|
44
|
-
try {
|
|
45
|
-
message = JSON.parse(String(event.data));
|
|
46
|
-
}
|
|
47
|
-
catch {
|
|
48
|
-
return;
|
|
49
|
-
}
|
|
50
|
-
if (message.id !== undefined && waiting.has(message.id)) {
|
|
51
|
-
const entry = waiting.get(message.id);
|
|
52
|
-
waiting.delete(message.id);
|
|
53
|
-
clearTimeout(entry.timer);
|
|
54
|
-
if (message.error)
|
|
55
|
-
entry.reject(new Error(message.error.message ?? "the browser refused"));
|
|
56
|
-
else if (!entry.raw && message.result?.exceptionDetails)
|
|
57
|
-
entry.reject(new Error("the page threw"));
|
|
58
|
-
else
|
|
59
|
-
entry.resolve(entry.raw ? message.result : message.result?.result?.value);
|
|
60
|
-
return;
|
|
61
|
-
}
|
|
62
|
-
const { method, params, sessionId } = message;
|
|
63
|
-
if (method === "Target.targetCreated" && params.targetInfo.type === "page") {
|
|
64
|
-
pages.set(params.targetInfo.targetId, { url: params.targetInfo.url, moved: (moves += 1) });
|
|
65
|
-
choose();
|
|
66
|
-
}
|
|
67
|
-
else if (method === "Target.targetInfoChanged" && params.targetInfo.type === "page") {
|
|
68
|
-
const known = pages.get(params.targetInfo.targetId);
|
|
69
|
-
// A page that moved to a new address is the one in use; a new title on the same address is not a move.
|
|
70
|
-
if (!known)
|
|
71
|
-
pages.set(params.targetInfo.targetId, { url: params.targetInfo.url, moved: (moves += 1) });
|
|
72
|
-
else if (known.url !== params.targetInfo.url) {
|
|
73
|
-
known.url = params.targetInfo.url;
|
|
74
|
-
known.moved = (moves += 1);
|
|
75
|
-
}
|
|
76
|
-
choose();
|
|
77
|
-
}
|
|
78
|
-
else if (method === "Target.attachedToTarget") {
|
|
79
|
-
const page = current;
|
|
80
|
-
if (!page || params.targetInfo.targetId !== page.targetId) {
|
|
81
|
-
send("Target.detachFromTarget", { sessionId: params.sessionId });
|
|
82
|
-
return;
|
|
83
|
-
}
|
|
84
|
-
page.sessionId = params.sessionId;
|
|
85
|
-
send("Page.enable", {}, params.sessionId);
|
|
86
|
-
send("Page.startScreencast", { format: "jpeg", quality: options.quality, maxWidth: options.maxWidth, maxHeight: options.maxHeight }, params.sessionId);
|
|
87
|
-
}
|
|
88
|
-
else if (method === "Page.screencastFrame") {
|
|
89
|
-
send("Page.screencastFrameAck", { sessionId: params.sessionId }, sessionId);
|
|
90
|
-
if (sessionId !== current?.sessionId)
|
|
91
|
-
return;
|
|
92
|
-
handlers.frame(Buffer.from(String(params.data), "base64"), { deviceWidth: Number(params.metadata?.deviceWidth) || 0, deviceHeight: Number(params.metadata?.deviceHeight) || 0, epoch });
|
|
93
|
-
}
|
|
94
|
-
else if (method === "Page.frameNavigated" && sessionId === current?.sessionId && !params.frame?.parentId) {
|
|
95
|
-
epoch += 1;
|
|
96
|
-
handlers.navigated();
|
|
97
|
-
}
|
|
98
|
-
else if (method === "Target.targetDestroyed") {
|
|
99
|
-
pages.delete(params.targetId);
|
|
100
|
-
if (params.targetId === current?.targetId) {
|
|
101
|
-
current = null;
|
|
102
|
-
epoch += 1;
|
|
103
|
-
}
|
|
104
|
-
choose();
|
|
105
|
-
}
|
|
106
|
-
};
|
|
107
|
-
socket.onerror = () => { };
|
|
108
|
-
socket.onclose = () => {
|
|
109
|
-
for (const entry of waiting.values()) {
|
|
110
|
-
clearTimeout(entry.timer);
|
|
111
|
-
entry.reject(new Error("the browser's socket closed"));
|
|
112
|
-
}
|
|
113
|
-
waiting.clear();
|
|
114
|
-
if (!closing)
|
|
115
|
-
handlers.closed();
|
|
116
|
-
};
|
|
117
|
-
return {
|
|
118
|
-
evaluate(expression, ms = 1_500) {
|
|
119
|
-
const sessionId = current?.sessionId;
|
|
120
|
-
if (!sessionId || socket.readyState !== 1)
|
|
121
|
-
return Promise.reject(new Error("no page to ask"));
|
|
122
|
-
return new Promise((resolve, reject) => {
|
|
123
|
-
const id = send("Runtime.evaluate", { expression, returnByValue: true }, sessionId);
|
|
124
|
-
const timer = setTimeout(() => { waiting.delete(id); reject(new Error("the page did not answer")); }, ms);
|
|
125
|
-
timer.unref?.();
|
|
126
|
-
waiting.set(id, { resolve, reject, timer });
|
|
127
|
-
});
|
|
128
|
-
},
|
|
129
|
-
capture(ms = 4_000) {
|
|
130
|
-
const sessionId = current?.sessionId;
|
|
131
|
-
if (!sessionId || socket.readyState !== 1)
|
|
132
|
-
return Promise.reject(new Error("no page to capture"));
|
|
133
|
-
const at = epoch;
|
|
134
|
-
const ask = (method, params) => new Promise((resolve, reject) => {
|
|
135
|
-
const id = send(method, params, sessionId);
|
|
136
|
-
const timer = setTimeout(() => { waiting.delete(id); reject(new Error("the page did not answer")); }, ms);
|
|
137
|
-
timer.unref?.();
|
|
138
|
-
waiting.set(id, { resolve, reject, timer, raw: true });
|
|
139
|
-
});
|
|
140
|
-
return (async () => {
|
|
141
|
-
const view = (await ask("Page.getLayoutMetrics", {}))?.cssVisualViewport;
|
|
142
|
-
const w = Number(view?.clientWidth) || 0;
|
|
143
|
-
const h = Number(view?.clientHeight) || 0;
|
|
144
|
-
if (!(w > 0 && h > 0))
|
|
145
|
-
throw new Error("the page has no size");
|
|
146
|
-
const shot = await ask("Page.captureScreenshot", { format: "jpeg", quality: options.quality, clip: { x: Number(view.pageX) || 0, y: Number(view.pageY) || 0, width: w, height: h, scale: Math.min(1, options.maxWidth / w, options.maxHeight / h) } });
|
|
147
|
-
if (typeof shot?.data !== "string")
|
|
148
|
-
throw new Error("the browser sent no capture");
|
|
149
|
-
if (epoch !== at || current?.sessionId !== sessionId)
|
|
150
|
-
throw new Error("the capture moved to another page");
|
|
151
|
-
return { bytes: Buffer.from(shot.data, "base64"), page: { deviceWidth: w, deviceHeight: h, epoch: at } };
|
|
152
|
-
})();
|
|
153
|
-
},
|
|
154
|
-
epoch: () => epoch,
|
|
155
|
-
close() { closing = true; for (const entry of waiting.values()) {
|
|
156
|
-
clearTimeout(entry.timer);
|
|
157
|
-
entry.reject(new Error("closed"));
|
|
158
|
-
} waiting.clear(); try {
|
|
159
|
-
socket.close();
|
|
160
|
-
}
|
|
161
|
-
catch { /* already closed */ } },
|
|
162
|
-
};
|
|
163
|
-
}
|