@parcha/pi-browser 0.1.0-beta.27 → 0.1.0-beta.29
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 +19 -0
- package/README.md +29 -8
- package/dist/coding-agent-local.d.ts +3 -7
- package/dist/coding-agent-local.js +12 -80
- package/dist/coding-agent.d.ts +10 -1
- package/dist/coding-agent.js +45 -10
- package/dist/core/contract.d.ts +2 -0
- package/dist/core/contract.js +11 -4
- package/dist/core/decisions.d.ts +3 -1
- package/dist/core/effects.d.ts +8 -0
- package/dist/core/effects.js +54 -5
- package/dist/core/entities.d.ts +3 -0
- package/dist/core/entities.js +68 -0
- package/dist/core/guard.d.ts +3 -0
- package/dist/core/guard.js +5 -0
- package/dist/core/host.d.ts +24 -6
- package/dist/core/page-classifier.d.ts +77 -0
- package/dist/core/page-classifier.js +92 -0
- package/dist/core/page-markdown.d.ts +5 -0
- package/dist/core/page-markdown.js +151 -0
- package/dist/core/page-scan.d.ts +95 -0
- package/dist/core/page-scan.js +542 -0
- package/dist/core/public-fetch.d.ts +32 -0
- package/dist/core/public-fetch.js +134 -0
- package/dist/core/read-page-worker.d.ts +8 -0
- package/dist/core/read-page-worker.js +32 -0
- package/dist/core/read-page.d.ts +46 -0
- package/dist/core/read-page.js +96 -0
- package/dist/core/tools.d.ts +9 -2
- package/dist/core/tools.js +56 -31
- package/dist/core/vault.d.ts +68 -0
- package/dist/core/vault.js +79 -0
- package/dist/core/web.d.ts +33 -0
- package/dist/core/web.js +155 -16
- package/dist/driver/stagehand.js +2 -1
- package/dist/durable/web.d.ts +7 -1
- package/dist/durable/web.js +4 -1
- package/dist/durable.d.ts +2 -1
- package/dist/durable.js +58 -10
- package/dist/index.d.ts +4 -0
- package/dist/index.js +4 -0
- package/dist/providers/browserbase.d.ts +1 -1
- package/dist/providers/browserbase.js +11 -1
- package/dist/providers/cdp-session.d.ts +28 -0
- package/dist/providers/cdp-session.js +153 -0
- package/dist/providers/cdp.d.ts +6 -0
- package/dist/providers/cdp.js +52 -2
- package/dist/providers/chrome-args.d.ts +3 -1
- package/dist/providers/chrome-args.js +3 -2
- package/dist/providers/kernel.js +6 -0
- package/dist/providers/profiles.d.ts +4 -0
- package/dist/providers/profiles.js +10 -0
- package/dist/providers/worker-extension.d.ts +14 -0
- package/dist/providers/worker-extension.js +63 -0
- package/dist/vault-session.d.ts +58 -0
- package/dist/vault-session.js +219 -0
- package/package.json +5 -1
|
@@ -11,7 +11,11 @@ export declare function isWriter(tag: string): boolean;
|
|
|
11
11
|
* `resolve` returns the name's context, calling `make` only when there is none, and no two callers may both make one. */
|
|
12
12
|
export type ProfileStore = {
|
|
13
13
|
resolve(key: string, make: (signal: AbortSignal) => Promise<string>): Promise<string>;
|
|
14
|
+
/** True when the store writes the ids it holds to a clear file (the default one does). A session vault refuses a provider whose store does. */
|
|
15
|
+
readonly clear?: boolean;
|
|
14
16
|
};
|
|
17
|
+
/** Context ids kept in this process only: for a host that moves them into its own vault, so none lands on disk. */
|
|
18
|
+
export declare function memoryProfileStore(): ProfileStore;
|
|
15
19
|
/** A JSON file, read, extended and written under one lock, so two processes' first uses of a name make one context; a file that is
|
|
16
20
|
* not this format, or cannot be read, is never overwritten. */
|
|
17
21
|
export declare function fileProfileStore(file?: string, options?: {
|
|
@@ -74,6 +74,15 @@ export function isWriter(tag) {
|
|
|
74
74
|
return true;
|
|
75
75
|
return false;
|
|
76
76
|
}
|
|
77
|
+
/** Context ids kept in this process only: for a host that moves them into its own vault, so none lands on disk. */
|
|
78
|
+
export function memoryProfileStore() {
|
|
79
|
+
const ids = new Map();
|
|
80
|
+
return { clear: false, resolve: (key, make) => { let known = ids.get(key); if (!known) {
|
|
81
|
+
known = make(AbortSignal.timeout(20_000));
|
|
82
|
+
ids.set(key, known);
|
|
83
|
+
known.catch(() => ids.delete(key));
|
|
84
|
+
} return known; } };
|
|
85
|
+
}
|
|
77
86
|
const SCHEMA = "pi-browser.profiles.v1";
|
|
78
87
|
const defaultProfilesFile = () => path.join(process.env.XDG_CONFIG_HOME || path.join(os.homedir(), ".config"), "pi-browser", "profiles.json");
|
|
79
88
|
const notOurs = (file) => new Error(`the profiles file ${path.basename(file)} is not a pi-browser profiles file, so it was left as it is`);
|
|
@@ -103,6 +112,7 @@ export function fileProfileStore(file = defaultProfilesFile(), options = {}) {
|
|
|
103
112
|
// to run past the lock that protects it.
|
|
104
113
|
const makeLimitMs = options.makeLimitMs ?? 20_000;
|
|
105
114
|
return {
|
|
115
|
+
clear: true,
|
|
106
116
|
async resolve(key, make) {
|
|
107
117
|
await mkdir(path.dirname(file), { recursive: true });
|
|
108
118
|
return withLock(`${file}.lock`, async () => {
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export declare const COPY = "stagehand-extension";
|
|
2
|
+
/** `<into>/stagehand-extension`, a copy of Stagehand's extension directory. */
|
|
3
|
+
export declare function copyExtension(into: string, source?: string): Promise<string>;
|
|
4
|
+
/** The policy for the extension's pages and worker: its own files, and the browser's DevTools socket by its whole URL path. A
|
|
5
|
+
* manifest that already sets one is left alone only when it is exactly this one (an earlier load of this copy wrote it and failed
|
|
6
|
+
* before the browser took it, so a retry is safe); any other is not guessed at. */
|
|
7
|
+
export declare function connectPolicy(manifest: Record<string, unknown>, devtoolsUrl: string): Record<string, unknown>;
|
|
8
|
+
/** The id Chrome gives the unpacked extension at `copy`: the Origin the browser's socket was opened to, without its scheme. */
|
|
9
|
+
export declare const copyId: (copy: string) => string;
|
|
10
|
+
/** Writes the policy into the copy and loads it into the running browser; answers the extension's id. */
|
|
11
|
+
export declare function loadConfined(copy: string, devtoolsUrl: string): Promise<string>;
|
|
12
|
+
/** Whether the browser already holds, enabled, the extension with this id: the one a copy's path implies, never another that
|
|
13
|
+
* merely carries Stagehand's name (an earlier, unconfined copy would be taken for the lease's own). */
|
|
14
|
+
export declare function isLoaded(devtoolsUrl: string, id: string): Promise<boolean>;
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
// The Stagehand extension a local launched Chrome is given: a copy in the lease's private TMPDIR whose pages and worker may
|
|
2
|
+
// connect to nothing but the extension's own files and the browser's DevTools socket. Run code executes in that worker with
|
|
3
|
+
// the extension's host permissions, so the browser itself refuses what the observer can only watch. A copy, because the
|
|
4
|
+
// manifest is Stagehand's bytes and a directory shared by every lease cannot name this lease's socket; created before the
|
|
5
|
+
// browser starts, because its path decides the extension's id and so the one Origin the browser's socket is opened to.
|
|
6
|
+
import { cp, readFile, writeFile } from "node:fs/promises";
|
|
7
|
+
import path from "node:path";
|
|
8
|
+
import { cdpCall } from "./cdp-call.js";
|
|
9
|
+
import { extensionDirectory, extensionOrigin } from "./chrome-args.js";
|
|
10
|
+
export const COPY = "stagehand-extension";
|
|
11
|
+
const LOAD_MS = 20_000;
|
|
12
|
+
/** `<into>/stagehand-extension`, a copy of Stagehand's extension directory. */
|
|
13
|
+
export async function copyExtension(into, source = extensionDirectory()) {
|
|
14
|
+
const copy = path.join(into, COPY);
|
|
15
|
+
await cp(source, copy, { recursive: true, dereference: true });
|
|
16
|
+
return copy;
|
|
17
|
+
}
|
|
18
|
+
/** The policy for the extension's pages and worker: its own files, and the browser's DevTools socket by its whole URL path. A
|
|
19
|
+
* manifest that already sets one is left alone only when it is exactly this one (an earlier load of this copy wrote it and failed
|
|
20
|
+
* before the browser took it, so a retry is safe); any other is not guessed at. */
|
|
21
|
+
export function connectPolicy(manifest, devtoolsUrl) {
|
|
22
|
+
const socket = new URL(devtoolsUrl);
|
|
23
|
+
if (socket.protocol !== "ws:" && socket.protocol !== "wss:")
|
|
24
|
+
throw new Error("the browser's DevTools URL is not a WebSocket URL");
|
|
25
|
+
const policy = { extension_pages: `script-src 'self'; object-src 'self'; connect-src 'self' ${socket.protocol}//${socket.host}${socket.pathname}` };
|
|
26
|
+
if (manifest.content_security_policy === undefined)
|
|
27
|
+
return { ...manifest, content_security_policy: policy };
|
|
28
|
+
if (JSON.stringify(manifest.content_security_policy) === JSON.stringify(policy))
|
|
29
|
+
return manifest;
|
|
30
|
+
throw new Error("Stagehand's extension manifest already sets a content security policy, so its connections cannot be limited");
|
|
31
|
+
}
|
|
32
|
+
/** The id Chrome gives the unpacked extension at `copy`: the Origin the browser's socket was opened to, without its scheme. */
|
|
33
|
+
export const copyId = (copy) => extensionOrigin(copy).slice("chrome-extension://".length);
|
|
34
|
+
/** Writes the policy into the copy and loads it into the running browser; answers the extension's id. */
|
|
35
|
+
export async function loadConfined(copy, devtoolsUrl) {
|
|
36
|
+
const file = path.join(copy, "manifest.json");
|
|
37
|
+
const manifest = JSON.parse(await readFile(file, "utf8"));
|
|
38
|
+
await writeFile(file, JSON.stringify(connectPolicy(manifest, devtoolsUrl), null, 2));
|
|
39
|
+
const session = await cdpCall(devtoolsUrl, "Extensions.loadUnpacked", { path: copy }, LOAD_MS);
|
|
40
|
+
try {
|
|
41
|
+
const id = session.result?.id;
|
|
42
|
+
if (typeof id !== "string" || !id)
|
|
43
|
+
throw new Error("the browser did not return the extension's id");
|
|
44
|
+
// The socket is open to the Origin the copy's path implies: an extension named otherwise could not use it.
|
|
45
|
+
if (id !== copyId(copy))
|
|
46
|
+
throw new Error("the browser named the loaded extension differently than its directory implies");
|
|
47
|
+
return id;
|
|
48
|
+
}
|
|
49
|
+
finally {
|
|
50
|
+
session.close();
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
/** Whether the browser already holds, enabled, the extension with this id: the one a copy's path implies, never another that
|
|
54
|
+
* merely carries Stagehand's name (an earlier, unconfined copy would be taken for the lease's own). */
|
|
55
|
+
export async function isLoaded(devtoolsUrl, id) {
|
|
56
|
+
const session = await cdpCall(devtoolsUrl, "Extensions.getExtensions", {}, LOAD_MS);
|
|
57
|
+
try {
|
|
58
|
+
return (session.result?.extensions ?? []).some((e) => e.id === id && e.enabled);
|
|
59
|
+
}
|
|
60
|
+
finally {
|
|
61
|
+
session.close();
|
|
62
|
+
}
|
|
63
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import type { AttachTarget, BrowserProvider, SessionSpec } from "./core/host.js";
|
|
2
|
+
import type { LeaseRef } from "./core/lease.js";
|
|
3
|
+
import { type DrivingGate } from "./core/driving.js";
|
|
4
|
+
import { type SessionVaultPort, type VaultTarget } from "./core/vault.js";
|
|
5
|
+
export type VaultOptions = {
|
|
6
|
+
/** Keeps a signed-in browser session for the host (encrypted, per-item keys). Absent: nothing is captured or restored. */
|
|
7
|
+
sessionVault?: SessionVaultPort;
|
|
8
|
+
/** The oldest a session cookie may be at restore. Absent, 24 h; above 24 h it is clamped to 24 h; zero or negative is refused when the extension is made. */
|
|
9
|
+
sessionVaultMaxSessionCookieAgeMs?: number;
|
|
10
|
+
};
|
|
11
|
+
export type VaultRuntime = {
|
|
12
|
+
port: SessionVaultPort;
|
|
13
|
+
maxAgeMs: number;
|
|
14
|
+
track(secret: string): void;
|
|
15
|
+
warn(message: string): void;
|
|
16
|
+
now(): Date; /** The newest capture attempt per vault key: an older one that is still reading when a newer one starts writes nothing. */
|
|
17
|
+
latest: Map<string, number>;
|
|
18
|
+
attempts: {
|
|
19
|
+
next: number;
|
|
20
|
+
}; /** How long a restore, and a capture, may take (20 s each); tests shorten them. */
|
|
21
|
+
restoreMs: number;
|
|
22
|
+
captureMs: number;
|
|
23
|
+
};
|
|
24
|
+
/** `null` when the host gave no vault. A bad age cap throws here, so a host learns at setup. */
|
|
25
|
+
export declare function vaultRuntime(options: VaultOptions, track: (secret: string) => void, warn: (message: string) => void, now?: () => Date, limits?: {
|
|
26
|
+
restoreMs?: number;
|
|
27
|
+
captureMs?: number;
|
|
28
|
+
}): VaultRuntime | null;
|
|
29
|
+
/** Waits, up to `ms`, for a person to stop driving a session: a capture reads only once they have handed back. True when the gate is free. */
|
|
30
|
+
export declare function untilQuiet(gate: DrivingGate | null, ms?: number): Promise<boolean>;
|
|
31
|
+
/** `vaulted`: this session was created under the host's vault plan (a context kind asked the provider for a saved context or profile), so it may be captured; a session that
|
|
32
|
+
* started with no target cannot be given one later, because its provider holds no context for it. `fresh`: nothing was kept, so a new context was made and its id is
|
|
33
|
+
* given to the vault right after the create (a restart then still finds it). */
|
|
34
|
+
export type RestorePlan = {
|
|
35
|
+
spec: SessionSpec;
|
|
36
|
+
afterAttach: ((target: AttachTarget) => Promise<void>) | null;
|
|
37
|
+
vaulted: boolean;
|
|
38
|
+
fresh: boolean;
|
|
39
|
+
/** Set when the vault may hold a saved sign-in this session did not take (the read failed, or what it holds could not be used here): this session is NOT captured, so it
|
|
40
|
+
* can never replace what is saved. The text is the warning given once at the first capture. */
|
|
41
|
+
holdBack?: string;
|
|
42
|
+
};
|
|
43
|
+
/** What the create does with the vault: the spec the provider is asked for (a kept context or profile for Browserbase and Kernel) and a step that runs after the
|
|
44
|
+
* browser is attached and before the driver is (a local Chrome's cookies and local storage). The record the adapter stores is made from the ORIGINAL spec, never this one. */
|
|
45
|
+
export declare function planRestore(rt: VaultRuntime | null, target: VaultTarget | undefined, provider: BrowserProvider, spec: SessionSpec): Promise<RestorePlan>;
|
|
46
|
+
export type CaptureOptions = {
|
|
47
|
+
/** Whether the session was created under the vault plan (RestorePlan.vaulted). A context kind session that was not cannot be captured. */
|
|
48
|
+
vaulted: boolean;
|
|
49
|
+
/** The session's driving gate, read when the capture runs: a person who begins driving while the browser is being read voids that read. */
|
|
50
|
+
gate?: () => DrivingGate | null;
|
|
51
|
+
/** RestorePlan.holdBack: a saved sign-in may exist that this session did not take, so it is never captured; the text is warned once. */
|
|
52
|
+
holdBack?: string;
|
|
53
|
+
};
|
|
54
|
+
/** A capture of one session, to run while its browser still exists. Never throws: a failure is a warning. Bounded in time from its first step: the target, the
|
|
55
|
+
* browser's DevTools socket and the host's port all sit inside the deadline, and when it passes the socket is closed and nothing more is read or written.
|
|
56
|
+
* `target` is read AT THE CAPTURE, so a host that learns the site and its origins mid-run (the person is asked to sign in first) is captured under what it set
|
|
57
|
+
* last; a target that reads as absent means the host no longer wants this session kept, and nothing is captured. */
|
|
58
|
+
export declare function captureFor(rt: VaultRuntime, target: VaultTarget | undefined | (() => Promise<VaultTarget | undefined> | VaultTarget | undefined), provider: BrowserProvider, ref: LeaseRef, options?: CaptureOptions): () => Promise<void>;
|
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
// How an adapter uses a host's session vault (core/vault.ts): restore a kept sign-in when a session is created, before the driver attaches, and capture
|
|
2
|
+
// it when the session is released or a person hands it back. The state stays in memory and crosses only the host's port: nothing here logs a value, and
|
|
3
|
+
// a provider's context id is registered with the run's redactor the moment it is seen. A restore or capture that fails never fails the browser: the session
|
|
4
|
+
// starts signed out, or ends without a fresh capture, and a process warning says which, with no value in it.
|
|
5
|
+
import { randomBytes } from "node:crypto";
|
|
6
|
+
import { BrowserFailureError } from "./core/custody.js";
|
|
7
|
+
import { browserFailure } from "./core/failures.js";
|
|
8
|
+
import { MIN_REDACTED_VALUE_LENGTH } from "./core/redact.js";
|
|
9
|
+
import { keptFor } from "./core/driving.js";
|
|
10
|
+
import { clampSessionCookieAgeMs, coversEveryOrigin, vaultTarget } from "./core/vault.js";
|
|
11
|
+
import { captureCdpState, connectCdp, restoreCdpState } from "./providers/cdp-session.js";
|
|
12
|
+
/** `null` when the host gave no vault. A bad age cap throws here, so a host learns at setup. */
|
|
13
|
+
export function vaultRuntime(options, track, warn, now = () => new Date(), limits = {}) {
|
|
14
|
+
if (!options.sessionVault)
|
|
15
|
+
return null;
|
|
16
|
+
return { port: options.sessionVault, maxAgeMs: clampSessionCookieAgeMs(options.sessionVaultMaxSessionCookieAgeMs), track, warn, now, latest: new Map(), attempts: { next: 0 }, restoreMs: limits.restoreMs ?? RESTORE_MS, captureMs: limits.captureMs ?? CAPTURE_MS };
|
|
17
|
+
}
|
|
18
|
+
/** Waits, up to `ms`, for a person to stop driving a session: a capture reads only once they have handed back. True when the gate is free. */
|
|
19
|
+
export async function untilQuiet(gate, ms = 5000) {
|
|
20
|
+
const until = Date.now() + ms;
|
|
21
|
+
while (gate && keptFor(gate) === "person_driving" && Date.now() < until)
|
|
22
|
+
await new Promise((resolve) => setTimeout(resolve, 200));
|
|
23
|
+
return !gate || keptFor(gate) !== "person_driving";
|
|
24
|
+
}
|
|
25
|
+
const kindOf = (error) => (error instanceof Error ? error.name : "Error");
|
|
26
|
+
const CAPTURE_MS = 20_000;
|
|
27
|
+
const RESTORE_MS = 20_000;
|
|
28
|
+
/** `work`'s value, or a TimeoutError once `ms` pass (`onTimeout` runs then, to stop what `work` is still doing). The work is never left holding a caller. */
|
|
29
|
+
async function within(work, ms, onTimeout = () => { }) {
|
|
30
|
+
let timer;
|
|
31
|
+
try {
|
|
32
|
+
return await Promise.race([work, new Promise((_, reject) => { timer = setTimeout(() => { onTimeout(); reject(Object.assign(new Error("timed out"), { name: "TimeoutError" })); }, ms); })]);
|
|
33
|
+
}
|
|
34
|
+
finally {
|
|
35
|
+
clearTimeout(timer);
|
|
36
|
+
work.catch(() => undefined);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
const refused = (message) => new BrowserFailureError(browserFailure("refused", message));
|
|
40
|
+
/** A secret too short for the redactor to scrub is not used as one: the redactor ignores values under its minimum, so it would leak. */
|
|
41
|
+
function trackSecret(rt, secret, what) {
|
|
42
|
+
if (secret.length < MIN_REDACTED_VALUE_LENGTH) {
|
|
43
|
+
rt.warn(`the session vault's ${what} is too short for the redactor to scrub, so it is not used`);
|
|
44
|
+
return false;
|
|
45
|
+
}
|
|
46
|
+
rt.track(secret);
|
|
47
|
+
return true;
|
|
48
|
+
}
|
|
49
|
+
const HELD_FAILED = "the vault could not be read at the start, so a saved sign-in may exist under this key and is left untouched: this session's sign-in is not captured";
|
|
50
|
+
const HELD_UNUSED = "the vault holds a saved sign-in this session could not use, and it is left untouched: this session's sign-in is not captured";
|
|
51
|
+
/** What the create does with the vault: the spec the provider is asked for (a kept context or profile for Browserbase and Kernel) and a step that runs after the
|
|
52
|
+
* browser is attached and before the driver is (a local Chrome's cookies and local storage). The record the adapter stores is made from the ORIGINAL spec, never this one. */
|
|
53
|
+
export async function planRestore(rt, target, provider, spec) {
|
|
54
|
+
const plain = { spec, afterAttach: null, vaulted: false, fresh: false };
|
|
55
|
+
if (!rt || !target)
|
|
56
|
+
return plain;
|
|
57
|
+
let checked;
|
|
58
|
+
try {
|
|
59
|
+
checked = vaultTarget(target);
|
|
60
|
+
}
|
|
61
|
+
catch (error) {
|
|
62
|
+
throw refused(`browser vault: ${error instanceof Error ? error.message : "a bad target"}. No session was started.`);
|
|
63
|
+
}
|
|
64
|
+
const vault = provider.vault;
|
|
65
|
+
if (!vault) {
|
|
66
|
+
rt.warn("this browser provider cannot keep a session in a vault (a Chrome attached by endpoint is offered only when the host sets ownBrowser: true), so nothing was restored or will be captured");
|
|
67
|
+
return plain;
|
|
68
|
+
}
|
|
69
|
+
if (vault.kind === "context" && vault.contextIdsInClear)
|
|
70
|
+
throw refused("browser vault: this provider writes its context ids to a clear file, so a vault cannot use it. Give it a profile store that keeps ids in memory (memoryProfileStore). No session was started.");
|
|
71
|
+
// A vault that stops answering never holds the session back: it starts signed out.
|
|
72
|
+
let kept = null;
|
|
73
|
+
let holdBack;
|
|
74
|
+
try {
|
|
75
|
+
kept = await within(Promise.resolve().then(() => rt.port.restore(checked)), rt.restoreMs);
|
|
76
|
+
}
|
|
77
|
+
catch (error) {
|
|
78
|
+
holdBack = HELD_FAILED;
|
|
79
|
+
rt.warn(`the session vault could not restore (${kindOf(error)}); the session starts signed out`);
|
|
80
|
+
}
|
|
81
|
+
if (vault.kind === "context") {
|
|
82
|
+
if (kept && (kept.kind !== "provider-context" || kept.provider !== vault.provider)) {
|
|
83
|
+
rt.warn(`the vault holds a ${kept.kind} state this ${vault.provider} provider cannot use; it was ignored`);
|
|
84
|
+
kept = null;
|
|
85
|
+
holdBack = HELD_UNUSED;
|
|
86
|
+
}
|
|
87
|
+
// A context is a whole browser profile: it comes back only for a request that covers every origin it was saved for, never for some of them.
|
|
88
|
+
if (kept?.kind === "provider-context" && !coversEveryOrigin(checked.origins, kept.origins)) {
|
|
89
|
+
rt.warn("the vault holds a context whose saved origins this request does not cover every origin of (or records none); it was not restored");
|
|
90
|
+
kept = null;
|
|
91
|
+
holdBack = HELD_UNUSED;
|
|
92
|
+
}
|
|
93
|
+
if (kept?.kind === "provider-context" && !trackSecret(rt, kept.contextId, "context id")) {
|
|
94
|
+
kept = null;
|
|
95
|
+
holdBack = HELD_UNUSED;
|
|
96
|
+
}
|
|
97
|
+
if (kept?.kind === "provider-context")
|
|
98
|
+
return { spec: vault.provider === "browserbase" ? { ...spec, contextId: kept.contextId, profile: undefined } : { ...spec, profile: { name: kept.contextId, save: true } }, afterAttach: null, vaulted: true, fresh: false };
|
|
99
|
+
// A saved sign-in may exist that this session did not take: nothing persists for it, and it is never captured.
|
|
100
|
+
if (holdBack)
|
|
101
|
+
return { spec, afterAttach: null, vaulted: true, fresh: false, holdBack };
|
|
102
|
+
// Nothing kept: a context that persists is made for this session, its id held in memory and given to the vault right after the create.
|
|
103
|
+
return { spec: { ...spec, contextId: undefined, profile: { name: `vault-${randomBytes(9).toString("hex")}`, save: true } }, afterAttach: null, vaulted: true, fresh: true };
|
|
104
|
+
}
|
|
105
|
+
if (!kept)
|
|
106
|
+
return { ...plain, vaulted: true, ...(holdBack ? { holdBack } : {}) };
|
|
107
|
+
if (kept.kind !== "cdp") {
|
|
108
|
+
rt.warn(`the vault holds a ${kept.kind} state a local Chrome cannot use; it was ignored`);
|
|
109
|
+
return { ...plain, vaulted: true, holdBack: HELD_UNUSED };
|
|
110
|
+
}
|
|
111
|
+
return {
|
|
112
|
+
spec,
|
|
113
|
+
vaulted: true,
|
|
114
|
+
fresh: false,
|
|
115
|
+
afterAttach: async (attached) => {
|
|
116
|
+
let link;
|
|
117
|
+
try {
|
|
118
|
+
await within((async () => {
|
|
119
|
+
link = await connectCdp(attached.sdkCdpUrl);
|
|
120
|
+
const counts = await restoreCdpState(link, kept, checked.origins, rt.now(), rt.maxAgeMs);
|
|
121
|
+
rt.warn(`the session vault restored ${counts.cookies} cookies and ${counts.origins} origins of local storage (dropped: ${counts.droppedExpired} expired, ${counts.droppedAged} aged, ${counts.droppedOutside} outside the origins)`);
|
|
122
|
+
})(), rt.restoreMs, () => link?.close());
|
|
123
|
+
}
|
|
124
|
+
catch (error) {
|
|
125
|
+
// Cookies are written before local storage, so a failure part-way leaves what was written: the session may be signed in only in part.
|
|
126
|
+
rt.warn(`the session vault restore failed (${kindOf(error)}); the session may start signed out, or signed in only in part`);
|
|
127
|
+
}
|
|
128
|
+
finally {
|
|
129
|
+
link?.close();
|
|
130
|
+
}
|
|
131
|
+
},
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
/** A capture of one session, to run while its browser still exists. Never throws: a failure is a warning. Bounded in time from its first step: the target, the
|
|
135
|
+
* browser's DevTools socket and the host's port all sit inside the deadline, and when it passes the socket is closed and nothing more is read or written.
|
|
136
|
+
* `target` is read AT THE CAPTURE, so a host that learns the site and its origins mid-run (the person is asked to sign in first) is captured under what it set
|
|
137
|
+
* last; a target that reads as absent means the host no longer wants this session kept, and nothing is captured. */
|
|
138
|
+
export function captureFor(rt, target, provider, ref, options = { vaulted: true }) {
|
|
139
|
+
let warnedLate = false;
|
|
140
|
+
return async () => {
|
|
141
|
+
const vault = provider.vault;
|
|
142
|
+
if (!vault)
|
|
143
|
+
return;
|
|
144
|
+
if (options.holdBack) {
|
|
145
|
+
if (!warnedLate) {
|
|
146
|
+
warnedLate = true;
|
|
147
|
+
rt.warn(options.holdBack);
|
|
148
|
+
}
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
151
|
+
if (vault.kind === "context" && !options.vaulted) {
|
|
152
|
+
if (!warnedLate) {
|
|
153
|
+
warnedLate = true;
|
|
154
|
+
rt.warn("this session started with no vault target, so its provider holds no saved context for it: its sign-in cannot be kept. Set the target before the session starts");
|
|
155
|
+
}
|
|
156
|
+
return;
|
|
157
|
+
}
|
|
158
|
+
const stop = new AbortController();
|
|
159
|
+
let link;
|
|
160
|
+
// Numbered when it starts: an older capture that is still reading when a newer one of the same key begins writes nothing.
|
|
161
|
+
const attempt = ++rt.attempts.next;
|
|
162
|
+
const work = (async () => {
|
|
163
|
+
const wanted = typeof target === "function" ? await target() : target;
|
|
164
|
+
if (!wanted)
|
|
165
|
+
return;
|
|
166
|
+
const checked = vaultTarget(wanted);
|
|
167
|
+
const key = checked.sessionKey;
|
|
168
|
+
if ((rt.latest.get(key) ?? 0) < attempt)
|
|
169
|
+
rt.latest.set(key, attempt);
|
|
170
|
+
const capturedAt = rt.now();
|
|
171
|
+
let state;
|
|
172
|
+
if (vault.kind === "context") {
|
|
173
|
+
const id = vault.contextOf(ref);
|
|
174
|
+
if (!id) {
|
|
175
|
+
rt.warn("this provider no longer knows the context this session runs on (the run restarted), so its sign-in was not captured; the context the vault holds stands");
|
|
176
|
+
return;
|
|
177
|
+
}
|
|
178
|
+
if (!trackSecret(rt, id, "context id"))
|
|
179
|
+
return;
|
|
180
|
+
state = { kind: "provider-context", capturedAt: capturedAt.toISOString(), provider: vault.provider, contextId: id, origins: [...checked.origins] };
|
|
181
|
+
}
|
|
182
|
+
else {
|
|
183
|
+
const gate = options.gate?.() ?? null;
|
|
184
|
+
let interrupted = false;
|
|
185
|
+
const off = gate?.onChange((now) => { if (now === "person")
|
|
186
|
+
interrupted = true; });
|
|
187
|
+
try {
|
|
188
|
+
const attached = await provider.attach(ref, stop.signal);
|
|
189
|
+
link = await connectCdp(attached.sdkCdpUrl);
|
|
190
|
+
if (stop.signal.aborted) {
|
|
191
|
+
link.close();
|
|
192
|
+
return;
|
|
193
|
+
}
|
|
194
|
+
state = await captureCdpState(link, checked.origins, capturedAt);
|
|
195
|
+
}
|
|
196
|
+
finally {
|
|
197
|
+
off?.();
|
|
198
|
+
}
|
|
199
|
+
if (interrupted || (gate && keptFor(gate) === "person_driving")) {
|
|
200
|
+
rt.warn("a person began driving while the session was being read, so that capture was discarded; the last capture stands");
|
|
201
|
+
return;
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
// Nothing is written once the deadline has passed or a newer capture of this key has begun: an older read never replaces a newer one.
|
|
205
|
+
if (stop.signal.aborted || rt.latest.get(key) !== attempt)
|
|
206
|
+
return;
|
|
207
|
+
await rt.port.capture({ ...checked, state });
|
|
208
|
+
})();
|
|
209
|
+
try {
|
|
210
|
+
await within(work, rt.captureMs, () => { stop.abort(); link?.close(); });
|
|
211
|
+
}
|
|
212
|
+
catch (error) {
|
|
213
|
+
rt.warn(`the session vault could not capture this session (${kindOf(error)}); the last capture stands`);
|
|
214
|
+
}
|
|
215
|
+
finally {
|
|
216
|
+
link?.close();
|
|
217
|
+
}
|
|
218
|
+
};
|
|
219
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@parcha/pi-browser",
|
|
3
|
-
"version": "0.1.0-beta.
|
|
3
|
+
"version": "0.1.0-beta.29",
|
|
4
4
|
"description": "Your agent gets its own browser, and you can take over and hand it back.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"pi-package",
|
|
@@ -40,6 +40,10 @@
|
|
|
40
40
|
"types": "./dist/providers/cdp.d.ts",
|
|
41
41
|
"import": "./dist/providers/cdp.js"
|
|
42
42
|
},
|
|
43
|
+
"./providers/cdp-session": {
|
|
44
|
+
"types": "./dist/providers/cdp-session.d.ts",
|
|
45
|
+
"import": "./dist/providers/cdp-session.js"
|
|
46
|
+
},
|
|
43
47
|
"./providers/chrome-args": {
|
|
44
48
|
"types": "./dist/providers/chrome-args.d.ts",
|
|
45
49
|
"import": "./dist/providers/chrome-args.js"
|