@parcha/pi-browser 0.1.0-beta.34 → 0.1.0-beta.36
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 +20 -0
- package/README.md +23 -44
- package/dist/core/contract.d.ts +9 -6
- package/dist/core/contract.js +8 -29
- package/dist/core/driving-guard.d.ts +1 -1
- package/dist/core/driving-guard.js +1 -1
- 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 +1 -75
- package/dist/core/images.d.ts +32 -4
- package/dist/core/images.js +81 -16
- 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.js +4 -1
- package/dist/core/usage.d.ts +0 -2
- package/dist/core/usage.js +0 -2
- package/dist/core/vault.d.ts +0 -10
- package/dist/core/vault.js +0 -13
- package/dist/core/web.d.ts +2 -111
- package/dist/core/web.js +0 -443
- package/dist/driver/cdp.js +3 -3
- package/dist/driver/page-driver.js +7 -3
- package/dist/durable.d.ts +6 -12
- package/dist/durable.js +31 -84
- package/dist/index.d.ts +1 -1
- package/dist/index.js +3 -1
- package/dist/providers/cdp.js +2 -2
- package/dist/providers/profiles.d.ts +1 -15
- package/dist/providers/profiles.js +1 -64
- package/dist/providers/shared.d.ts +1 -47
- package/dist/providers/shared.js +1 -82
- package/dist/providers/worker-extension.js +1 -1
- package/dist/testing/backend.d.ts +6 -24
- package/dist/testing/backend.js +7 -30
- package/dist/vault-session.d.ts +4 -7
- package/dist/vault-session.js +26 -81
- package/dist/vendor/browser-use-pi/page.d.ts +3 -12
- package/dist/vendor/browser-use-pi/page.js +6 -19
- package/package.json +3 -16
- 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/browserbase-net.d.ts +0 -12
- package/dist/providers/browserbase-net.js +0 -95
- package/dist/providers/browserbase.d.ts +0 -169
- package/dist/providers/browserbase.js +0 -319
- package/dist/providers/kernel.d.ts +0 -58
- package/dist/providers/kernel.js +0 -291
- /package/dist/{providers → core}/cdp-call.d.ts +0 -0
- /package/dist/{providers → core}/cdp-call.js +0 -0
package/dist/durable.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { randomUUID } from "node:crypto";
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
2
|
+
import { defineDoc, defineExtension, defineTool } from "@earendil-works/pi-durable";
|
|
3
|
+
import { browserSection, browserToolsFor, listedFor } from "./core/contract.js";
|
|
4
4
|
import { BrowserFailureError, closeDriver, currentRecord, loadedNoPage, custodyTools, FIXED_SESSION_RELAUNCH, fenced, HANDED_BACK, newSessionRecord, notices, NAVIGATION_CAP, NOTHING_TO_HOLD, navigationRefusal, profileRefusal, relaunchRefusal, SESSION_REPLACED, sessionSpec, waitingForHuman, NO_HANDOFF } from "./core/custody.js";
|
|
5
5
|
import { forModel } from "./durable/result.js";
|
|
6
6
|
import { EffectObserver, effectDecider, observerFailure } from "./core/effects.js";
|
|
@@ -10,15 +10,13 @@ import { drivingGuarded } from "./core/driving-guard.js";
|
|
|
10
10
|
import { drivingGates, keptFor, keptReason, personOn, rearmWhenLeft } from "./core/driving.js";
|
|
11
11
|
import { captureFor, planRestore, untilQuiet, vaultRuntime } from "./vault-session.js";
|
|
12
12
|
import { pageTools } from "./core/tools.js";
|
|
13
|
-
import { useWebsite, useWebsiteContract } from "./core/sites.js";
|
|
14
13
|
import { leaseTag } from "./core/lease.js";
|
|
15
14
|
import { createRedactor, hostLiveView, redactContent, redactDeep, scrubPageUrl } from "./core/redact.js";
|
|
16
|
-
import { CREATE_DEADLINE_MS, DEFAULT_BROWSER_POLICY, idleTracker, keyedSerializer,
|
|
15
|
+
import { CREATE_DEADLINE_MS, DEFAULT_BROWSER_POLICY, idleTracker, keyedSerializer, providerKeyValues } from "./core/session-control.js";
|
|
17
16
|
import { custodyStore, now } from "./durable/store.js";
|
|
18
17
|
import { answerOf, clearJournal, cutOf, cutsOf, inChain, journalOf, owed } from "./durable/journal.js";
|
|
19
18
|
import { withinDeadline } from "./core/unanswered.js";
|
|
20
19
|
import { resolvePrices, sessionCharge, withEstimate } from "./core/usage.js";
|
|
21
|
-
export { createWebExtension } from "./durable/web.js";
|
|
22
20
|
export { CONFIRM_POLLS, RELEASE_ATTEMPTS } from "./durable/store.js";
|
|
23
21
|
export { HANDOFF_POLL_MS } from "./core/custody.js";
|
|
24
22
|
const PLANE = "default";
|
|
@@ -49,9 +47,6 @@ async function drainReleases(name, harness, context, signal) {
|
|
|
49
47
|
const open = (record) => record?.state === "live" || record?.state === "creating";
|
|
50
48
|
const releasable = (record, reason) => open(record) || (record?.state === "held" && reason !== "idle");
|
|
51
49
|
const waiting = (record) => new BrowserFailureError(waitingForHuman(record.handoff));
|
|
52
|
-
// A child gets the browser and web tools only: never use_website, so it cannot start children of its own, and never
|
|
53
|
-
// browser_upload or browser_downloads, since its report is all that crosses back (invariant 10) and a site run has no files.
|
|
54
|
-
const CHILD_TOOLS = new Set([...BROWSER_TOOL_NAMES, ...WEB_TOOL_NAMES].filter((name) => name !== "browser_upload" && name !== "browser_downloads"));
|
|
55
50
|
/** How long a release the store could not record may take at the provider. */
|
|
56
51
|
const DIRECT_RELEASE_MS = 30_000;
|
|
57
52
|
/** How long close waits to learn whether a Session that began closing failed. */
|
|
@@ -243,7 +238,6 @@ async function create(c, scope, overrides, presetTag) {
|
|
|
243
238
|
const unsupported = profileRefusal(spec, provider.caps) ?? navigationRefusal(spec, provider);
|
|
244
239
|
if (unsupported)
|
|
245
240
|
throw new BrowserFailureError(unsupported);
|
|
246
|
-
// The vault may name a context or profile for the provider; the record stored below is made from the spec WITHOUT it, so no context id reaches the store.
|
|
247
241
|
const plan = await planRestore(c.vault, config.vault, provider, spec);
|
|
248
242
|
const record = newSessionRecord({ plane: scope.plane, tag, provider: provider.name, spec, at: now(), honours: provider.caps.settings });
|
|
249
243
|
const row = rowOf(scope.cid, record);
|
|
@@ -288,9 +282,6 @@ async function create(c, scope, overrides, presetTag) {
|
|
|
288
282
|
const live = { ...record, state: "live", resourceId: ref.id };
|
|
289
283
|
try {
|
|
290
284
|
const driver = await connect(c, scope, provider, ref, plan.afterAttach, plan.vaulted, plan.holdBack);
|
|
291
|
-
// A context made for this session is given to the vault now, so a restart that loses the provider's memory of it still finds it.
|
|
292
|
-
if (plan.fresh)
|
|
293
|
-
await c.held.capture(ref.tag);
|
|
294
285
|
await emit(c, scope.cid, config.label, live, "launched", provider);
|
|
295
286
|
return { record: live, driver };
|
|
296
287
|
}
|
|
@@ -665,6 +656,10 @@ async function releasePlane(c, conversation, reason, context, still = () => true
|
|
|
665
656
|
const record = still() ? currentRecord(state, PLANE) : null;
|
|
666
657
|
if (!record || !releasable(record, reason))
|
|
667
658
|
return [];
|
|
659
|
+
// A run waiting for its person's answer keeps the session from an idle release; letting the keep go starts the idle
|
|
660
|
+
// window again (keepForPerson).
|
|
661
|
+
if (reason === "idle" && c.waiting.has(record.tag))
|
|
662
|
+
return [];
|
|
668
663
|
// A person driving or watching the session through the live view keeps it from an idle release (#1098); it is
|
|
669
664
|
// released one idle window after they leave.
|
|
670
665
|
kept.gate = reason === "idle" ? personOn(c.options.gates ?? drivingGates, record.tag) : null;
|
|
@@ -771,75 +766,10 @@ function sectionFor(c, names) {
|
|
|
771
766
|
return undefined;
|
|
772
767
|
const config = await input.read.snapshot(c.Config, input.conversationId, context);
|
|
773
768
|
const offered = (tool) => input.agent.tools.some((t) => t.name === tool && names.has(tool));
|
|
774
|
-
return [browserSection({ idleReleaseS: config?.policy.idleReleaseS ?? UNCONFIGURED.policy.idleReleaseS }, { downloads: offered("browser_downloads"), upload: offered("browser_upload") }), c.options.section?.addendum].filter(Boolean).join("\n\n");
|
|
769
|
+
return [browserSection({ idleReleaseS: config?.policy.idleReleaseS ?? UNCONFIGURED.policy.idleReleaseS }, { downloads: offered("browser_downloads"), upload: offered("browser_upload"), signedIn: c.options.signIns ?? !!c.options.sessionVault }), c.options.section?.addendum].filter(Boolean).join("\n\n");
|
|
775
770
|
},
|
|
776
771
|
};
|
|
777
772
|
}
|
|
778
|
-
function childConversation(c, api, context) {
|
|
779
|
-
return async (task) => {
|
|
780
|
-
const config = await api.snapshot(c.Config, api.conversationId, context);
|
|
781
|
-
const childTools = (await api.agent(context)).tools.filter((tool) => CHILD_TOOLS.has(tool.name));
|
|
782
|
-
// One child per request, recorded in the commit that creates it: a replay or a repeated call finds it again, and
|
|
783
|
-
// its submission is deduplicated by request_id.
|
|
784
|
-
const id = await api.commit(async (tx) => {
|
|
785
|
-
const children = await tx.doc(c.Children, api.conversationId);
|
|
786
|
-
const key = `${task.site}:${task.requestId}`;
|
|
787
|
-
const known = children.byRequest[key];
|
|
788
|
-
if (known !== undefined)
|
|
789
|
-
return known;
|
|
790
|
-
const child = await tx.createConversation({ ownership: { kind: "task", taskId: api.taskId } });
|
|
791
|
-
await configure(tx, child.id, { tools: childTools, instructions: null });
|
|
792
|
-
Object.assign(await tx.doc(c.Config, child.id), config ?? UNCONFIGURED);
|
|
793
|
-
children.byRequest[key] = child.id;
|
|
794
|
-
return child.id;
|
|
795
|
-
}, context);
|
|
796
|
-
const child = await api.conversation(id, context);
|
|
797
|
-
if (!child)
|
|
798
|
-
throw new Error("the child conversation is gone");
|
|
799
|
-
// Children on a provider whose sessions share one browser would drive the same pages, so there they take turns. One
|
|
800
|
-
// whose provider cannot be looked up waits its turn too: its own browser calls ask the factory again.
|
|
801
|
-
const shared = await providerOf(c, api, id, PLANE, context).then((provider) => provider.caps.sharedBrowser === true, () => true);
|
|
802
|
-
// The turn covers the report and the release too: on a shared browser the next child attaches only once this one has let go.
|
|
803
|
-
const run = async () => {
|
|
804
|
-
try {
|
|
805
|
-
const settled = await (await child.submit({ type: "input", content: task.prompt, requestId: task.requestId }, context)).wait(context);
|
|
806
|
-
const report = await api.commit(async (tx) => {
|
|
807
|
-
for (const entry of (await tx.scanEntries({ conversationId: id, order: "descending" }, 50)).items) {
|
|
808
|
-
for (const message of [...(entry.model ?? [])].reverse()) {
|
|
809
|
-
if (message.role !== "assistant")
|
|
810
|
-
continue;
|
|
811
|
-
const text = message.content.filter((part) => part.type === "text").map((part) => part.text).join("").trim();
|
|
812
|
-
if (text)
|
|
813
|
-
return text;
|
|
814
|
-
}
|
|
815
|
-
}
|
|
816
|
-
return "";
|
|
817
|
-
}, context);
|
|
818
|
-
if (settled.status !== "done" || !report)
|
|
819
|
-
throw new Error("the child finished without a report");
|
|
820
|
-
return report;
|
|
821
|
-
}
|
|
822
|
-
finally {
|
|
823
|
-
await releasePlane(c, { id, commit: (change, ctx) => api.commit(change, ctx) }, "close", context).catch(() => undefined);
|
|
824
|
-
}
|
|
825
|
-
};
|
|
826
|
-
return await (shared ? c.childTurns(run) : run());
|
|
827
|
-
};
|
|
828
|
-
}
|
|
829
|
-
function siteTool(c, sites) {
|
|
830
|
-
const contract = useWebsiteContract(sites.listing);
|
|
831
|
-
return defineTool({
|
|
832
|
-
name: contract.name,
|
|
833
|
-
description: contract.description,
|
|
834
|
-
parameters: contract.parameters,
|
|
835
|
-
replay: contract.replay,
|
|
836
|
-
executionMode: contract.executionMode,
|
|
837
|
-
execute: async (args, api, context) => {
|
|
838
|
-
const out = await useWebsite(args, sites.port, sites.listing, { signal: context.abortSignal ?? new AbortController().signal, runChild: childConversation(c, api, context) });
|
|
839
|
-
return { ...forModel(redactContent(out.content, c.redact)), ...(out.isError ? { isError: true } : {}) };
|
|
840
|
-
},
|
|
841
|
-
});
|
|
842
|
-
}
|
|
843
773
|
/** A Session that a storage error failed ends `{ kind: "failed" }` (`closed`, pi-durable's SessionEnd), and close begins
|
|
844
774
|
* at once (`subscribeClose`). */
|
|
845
775
|
function endingOf(harness) {
|
|
@@ -860,7 +790,6 @@ export function createBrowserExtension(options) {
|
|
|
860
790
|
const redact = createRedactor(() => [...providerKeyValues(process.env, options.redact?.values), ...tracked]);
|
|
861
791
|
const vault = vaultRuntime(options, (secret) => { tracked.add(secret); }, (message) => process.emitWarning(`pi-browser: ${redact(message)}`, { code: "PI_BROWSER_VAULT" }));
|
|
862
792
|
const Config = defineDoc({ kind: `${name}.config`, version: 1, scope: "conversation", history: "latest", fork: "current", initial: () => structuredClone(UNCONFIGURED), checkpointWhen: () => false });
|
|
863
|
-
const Children = defineDoc({ kind: `${name}.children`, version: 1, scope: "conversation", history: "latest", fork: "current", initial: () => ({ byRequest: {} }), checkpointWhen: () => false });
|
|
864
793
|
// A fork starts with no sessions: it never shares a live browser.
|
|
865
794
|
const Sessions = defineDoc({ kind: `${name}.sessions`, version: 1, scope: "conversation", history: "latest", fork: "initial", initial: emptySessions, checkpointWhen: () => false });
|
|
866
795
|
const readers = { options, redact, Config };
|
|
@@ -905,7 +834,7 @@ export function createBrowserExtension(options) {
|
|
|
905
834
|
stopped: (tag) => { c.stops.get(tag)?.stop(); c.stops.delete(tag); },
|
|
906
835
|
});
|
|
907
836
|
const c = {
|
|
908
|
-
...readers, name, Sessions,
|
|
837
|
+
...readers, name, Sessions, store, bill, held: new Attachments(), vault, fences: new Map(), leaving, waiting: new Map(), serial: keyedSerializer(), guards: new Map(), owned, failed: false, stops: new Map(), ending: null, opened: null, incarnation: randomUUID(),
|
|
909
838
|
idle: idleTracker({ idleMs: DEFAULT_BROWSER_POLICY.idleReleaseS * 1000, onIdle: (cid, fired, idleMs) => void releaseIdle(c, cid, fired, idleMs).catch(() => undefined) }),
|
|
910
839
|
};
|
|
911
840
|
// Every browser tool is a model call on its session's driving gate: refused while a person drives the live view, and the viewer's input is held while it runs. The host's own
|
|
@@ -914,11 +843,10 @@ export function createBrowserExtension(options) {
|
|
|
914
843
|
// pi checks a call's arguments against the tool's schema before execute, so each implementation receives its own shape.
|
|
915
844
|
const tools = toolsFor(c, own);
|
|
916
845
|
const names = new Set(tools.map((tool) => tool.name));
|
|
917
|
-
const siteTools = options.sites ? [siteTool(c, options.sites)] : [];
|
|
918
846
|
return {
|
|
919
|
-
extension: defineExtension({ name, tools
|
|
920
|
-
docs: { Config, Sessions, Inventory: store.Inventory
|
|
921
|
-
tools
|
|
847
|
+
extension: defineExtension({ name, tools, sections: [sectionFor(c, names)], tasks: [store.ReleaseTask, store.HandoffTask] }),
|
|
848
|
+
docs: { Config, Sessions, Inventory: store.Inventory },
|
|
849
|
+
tools,
|
|
922
850
|
release: (conversation, reason, context) => releasePlane(c, conversation, reason, context),
|
|
923
851
|
async reconcile(harness, isActive, context) {
|
|
924
852
|
c.opened = { harness, context };
|
|
@@ -956,5 +884,24 @@ export function createBrowserExtension(options) {
|
|
|
956
884
|
c.owned.clear();
|
|
957
885
|
},
|
|
958
886
|
retained: () => new Set([...c.guards.keys(), ...c.idle.keys(), ...c.serial.keys().map((key) => Number(key.slice(0, key.indexOf(":"))))]).size,
|
|
887
|
+
keepForPerson(tag) {
|
|
888
|
+
c.waiting.set(tag, (c.waiting.get(tag) ?? 0) + 1);
|
|
889
|
+
let kept = true;
|
|
890
|
+
return () => {
|
|
891
|
+
if (!kept)
|
|
892
|
+
return;
|
|
893
|
+
kept = false;
|
|
894
|
+
const left = (c.waiting.get(tag) ?? 1) - 1;
|
|
895
|
+
if (left > 0) {
|
|
896
|
+
c.waiting.set(tag, left);
|
|
897
|
+
return;
|
|
898
|
+
}
|
|
899
|
+
c.waiting.delete(tag);
|
|
900
|
+
// The idle window starts again from now, for a session still owned here.
|
|
901
|
+
const cid = c.owned.get(tag)?.cid;
|
|
902
|
+
if (cid !== undefined && c.idle.keys().includes(cid))
|
|
903
|
+
c.idle.activity(cid, 0);
|
|
904
|
+
};
|
|
905
|
+
},
|
|
959
906
|
};
|
|
960
907
|
}
|
package/dist/index.d.ts
CHANGED
|
@@ -17,9 +17,9 @@ export { isPrivateAddress, pinnedLookup, PUBLIC_MAX_BYTES, publicFetch, type Pub
|
|
|
17
17
|
export { READ_DEADLINE_MS, READ_MAX_CHARS, readPage, type PageRead, type ReadPageOptions } from "./core/read-page.js";
|
|
18
18
|
export * from "./core/receipts.js";
|
|
19
19
|
export * from "./core/redact.js";
|
|
20
|
-
export * from "./core/sites.js";
|
|
21
20
|
export { DEFAULT_BROWSER_POLICY } from "./core/session-control.js";
|
|
22
21
|
export * from "./core/tools.js";
|
|
23
22
|
export * from "./core/usage.js";
|
|
24
23
|
export * from "./core/web.js";
|
|
25
24
|
export * from "./core/vault.js";
|
|
25
|
+
export { cdpCall, type CdpSession } from "./core/cdp-call.js";
|
package/dist/index.js
CHANGED
|
@@ -18,9 +18,11 @@ export { isPrivateAddress, pinnedLookup, PUBLIC_MAX_BYTES, publicFetch } from ".
|
|
|
18
18
|
export { READ_DEADLINE_MS, READ_MAX_CHARS, readPage } from "./core/read-page.js";
|
|
19
19
|
export * from "./core/receipts.js";
|
|
20
20
|
export * from "./core/redact.js";
|
|
21
|
-
export * from "./core/sites.js";
|
|
22
21
|
export { DEFAULT_BROWSER_POLICY } from "./core/session-control.js";
|
|
23
22
|
export * from "./core/tools.js";
|
|
24
23
|
export * from "./core/usage.js";
|
|
25
24
|
export * from "./core/web.js";
|
|
26
25
|
export * from "./core/vault.js";
|
|
26
|
+
// One CDP call over its own socket, kept open for a setting that lasts as long as it (Browser.setDownloadBehavior): what a
|
|
27
|
+
// host holding a vendor's browser for the tool needs beside the driver.
|
|
28
|
+
export { cdpCall } from "./core/cdp-call.js";
|
package/dist/providers/cdp.js
CHANGED
|
@@ -7,12 +7,12 @@ import path from "node:path";
|
|
|
7
7
|
import { BrowserFailureError, PROFILE_NAME, refusedError } from "../core/custody.js";
|
|
8
8
|
import { DownloadNotSettled } from "../core/downloads.js";
|
|
9
9
|
import { browserFailure } from "../core/failures.js";
|
|
10
|
-
import { cdpCall } from "
|
|
10
|
+
import { cdpCall } from "../core/cdp-call.js";
|
|
11
11
|
import { chromeTail, openChromeStderr, watchChromeStderr } from "./chrome-stderr.js";
|
|
12
12
|
import { claimWriter, freeWriter, noteWriter } from "./profiles.js";
|
|
13
13
|
import { chromeArgs } from "./chrome-args.js";
|
|
14
14
|
import { COPY, copyExtension, copyId, isLoaded, loadConfined, soleStagehand } from "./worker-extension.js";
|
|
15
|
-
const CAPS = {
|
|
15
|
+
const CAPS = { profiles: false, settings: { proxies: false, verified: false, geolocation: false, region: false, context: false } };
|
|
16
16
|
const downloadsDir = (profileRoot, tag) => path.join(profileDir(profileRoot, tag) + ".downloads");
|
|
17
17
|
const READY_MS = 20_000;
|
|
18
18
|
/** The browser is gone and directories it used are not: a profile left behind holds the session's cookies, so a release that cannot remove
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
/**
|
|
1
|
+
/** A profile keeps the last writer's state and nothing locks it, so within this process one session at a time may save to a
|
|
2
2
|
* profile. A holder that is no longer running is replaced; the same owner's successor (a relaunch asks for its replacement
|
|
3
3
|
* while the old session is still being released) waits for the release; anyone else is refused. */
|
|
4
4
|
export declare function claimWriter(key: string, name: string, tag: string, owner: string, signal?: AbortSignal): Promise<void>;
|
|
@@ -7,17 +7,3 @@ export declare function claimWriter(key: string, name: string, tag: string, owne
|
|
|
7
7
|
export declare function noteWriter(tag: string, running: (signal: AbortSignal) => Promise<boolean>, settle?: () => Promise<void>): void;
|
|
8
8
|
export declare function freeWriter(tag: string): void;
|
|
9
9
|
export declare function isWriter(tag: string): boolean;
|
|
10
|
-
/** Context ids are not secrets, and Browserbase has no way to look a context up by name, so the name to id map is the host's.
|
|
11
|
-
* `resolve` returns the name's context, calling `make` only when there is none, and no two callers may both make one. */
|
|
12
|
-
export type ProfileStore = {
|
|
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;
|
|
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;
|
|
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
|
|
20
|
-
* not this format, or cannot be read, is never overwritten. */
|
|
21
|
-
export declare function fileProfileStore(file?: string, options?: {
|
|
22
|
-
makeLimitMs?: number;
|
|
23
|
-
}): ProfileStore;
|
|
@@ -1,9 +1,4 @@
|
|
|
1
|
-
import { randomBytes } from "node:crypto";
|
|
2
|
-
import { mkdir, readFile, rename, writeFile } from "node:fs/promises";
|
|
3
|
-
import os from "node:os";
|
|
4
|
-
import path from "node:path";
|
|
5
1
|
import { refusedError } from "../core/custody.js";
|
|
6
|
-
import { withLock } from "../core/downloads.js";
|
|
7
2
|
import { profileHoldings } from "./shared.js";
|
|
8
3
|
const writers = profileHoldings.writers;
|
|
9
4
|
const WAIT_MS = 20_000;
|
|
@@ -31,7 +26,7 @@ async function waitFor(work, signal, ms) {
|
|
|
31
26
|
signal?.removeEventListener("abort", abort);
|
|
32
27
|
}
|
|
33
28
|
}
|
|
34
|
-
/**
|
|
29
|
+
/** A profile keeps the last writer's state and nothing locks it, so within this process one session at a time may save to a
|
|
35
30
|
* profile. A holder that is no longer running is replaced; the same owner's successor (a relaunch asks for its replacement
|
|
36
31
|
* while the old session is still being released) waits for the release; anyone else is refused. */
|
|
37
32
|
export async function claimWriter(key, name, tag, owner, signal) {
|
|
@@ -74,61 +69,3 @@ export function isWriter(tag) {
|
|
|
74
69
|
return true;
|
|
75
70
|
return false;
|
|
76
71
|
}
|
|
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
|
-
}
|
|
86
|
-
const SCHEMA = "pi-browser.profiles.v1";
|
|
87
|
-
const defaultProfilesFile = () => path.join(process.env.XDG_CONFIG_HOME || path.join(os.homedir(), ".config"), "pi-browser", "profiles.json");
|
|
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`);
|
|
89
|
-
async function readProfiles(file) {
|
|
90
|
-
let text;
|
|
91
|
-
try {
|
|
92
|
-
text = await readFile(file, "utf8");
|
|
93
|
-
}
|
|
94
|
-
catch (error) {
|
|
95
|
-
if (error.code === "ENOENT")
|
|
96
|
-
return { schema: SCHEMA, contexts: {} };
|
|
97
|
-
throw error;
|
|
98
|
-
}
|
|
99
|
-
try {
|
|
100
|
-
const parsed = JSON.parse(text);
|
|
101
|
-
const contexts = parsed?.contexts;
|
|
102
|
-
if (parsed?.schema === SCHEMA && contexts && typeof contexts === "object" && !Array.isArray(contexts) && Object.values(contexts).every((id) => typeof id === "string" && id !== ""))
|
|
103
|
-
return parsed;
|
|
104
|
-
}
|
|
105
|
-
catch { /* not JSON */ }
|
|
106
|
-
throw notOurs(file);
|
|
107
|
-
}
|
|
108
|
-
/** 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
|
|
109
|
-
* not this format, or cannot be read, is never overwritten. */
|
|
110
|
-
export function fileProfileStore(file = defaultProfilesFile(), options = {}) {
|
|
111
|
-
// The lock is taken over once it is 30 s old (core/downloads.ts's rule), so `make` must end well before: a slower one is cut here, never left
|
|
112
|
-
// to run past the lock that protects it.
|
|
113
|
-
const makeLimitMs = options.makeLimitMs ?? 20_000;
|
|
114
|
-
return {
|
|
115
|
-
clear: true,
|
|
116
|
-
async resolve(key, make) {
|
|
117
|
-
await mkdir(path.dirname(file), { recursive: true });
|
|
118
|
-
return withLock(`${file}.lock`, async () => {
|
|
119
|
-
const current = await readProfiles(file);
|
|
120
|
-
const known = current.contexts[key];
|
|
121
|
-
if (known)
|
|
122
|
-
return known;
|
|
123
|
-
const limit = AbortSignal.timeout(makeLimitMs);
|
|
124
|
-
// Listening before `make` does, so the cut's own words are what the caller hears.
|
|
125
|
-
const cut = new Promise((_, reject) => limit.addEventListener("abort", () => reject(new Error(`making the profile's context took longer than ${makeLimitMs} ms, so it was cut before the profiles file's lock could be taken over`)), { once: true }));
|
|
126
|
-
const id = await Promise.race([make(limit), cut]);
|
|
127
|
-
const next = `${file}.${randomBytes(4).toString("hex")}.tmp`;
|
|
128
|
-
await writeFile(next, `${JSON.stringify({ schema: SCHEMA, contexts: { ...current.contexts, [key]: id } }, null, 2)}\n`);
|
|
129
|
-
await rename(next, file);
|
|
130
|
-
return id;
|
|
131
|
-
}, "the profiles file");
|
|
132
|
-
},
|
|
133
|
-
};
|
|
134
|
-
}
|
|
@@ -1,50 +1,5 @@
|
|
|
1
|
-
import type { ResourceStatus } from "../core/lease.js";
|
|
2
|
-
import type { CdpSession } from "./cdp-call.js";
|
|
3
1
|
export declare const nonEmpty: (v: unknown) => string;
|
|
4
|
-
/**
|
|
5
|
-
export declare const tagValue: (v: string) => string;
|
|
6
|
-
export declare function assertTag(tag: string): void;
|
|
7
|
-
export declare function extensionArchive(): string;
|
|
8
|
-
export declare const accountKey: (origin: string, apiKey: string) => string;
|
|
9
|
-
export declare const defaultSleep: (ms: number) => Promise<void>;
|
|
10
|
-
export declare const DEFAULT_CONFIRM: {
|
|
11
|
-
polls: number;
|
|
12
|
-
intervalMs: number;
|
|
13
|
-
};
|
|
14
|
-
/** The work itself goes on when `signal` fires: another caller may be waiting for it. */
|
|
15
|
-
export declare function untilAborted<T>(work: Promise<T>, signal?: AbortSignal): Promise<T>;
|
|
16
|
-
/** One upload per process and account; a failed upload is forgotten so the next create tries again. */
|
|
17
|
-
export declare function uploadOnce<T>(uploads: Map<string, Promise<T>>, account: string, upload: () => Promise<T>): Promise<T>;
|
|
18
|
-
/** The request is asked twice at most; released means the provider says the session is no longer running, not that the
|
|
19
|
-
* request was sent. `request` answers "gone" when the provider no longer knows the session. */
|
|
20
|
-
export declare function releaseAndConfirm(o: {
|
|
21
|
-
request: () => Promise<"gone" | void>;
|
|
22
|
-
status: () => Promise<ResourceStatus>;
|
|
23
|
-
forget: () => void | Promise<void>;
|
|
24
|
-
confirm: {
|
|
25
|
-
polls: number;
|
|
26
|
-
intervalMs: number;
|
|
27
|
-
};
|
|
28
|
-
sleep: (ms: number) => Promise<void>;
|
|
29
|
-
stillRunning: string;
|
|
30
|
-
}): Promise<void>;
|
|
31
|
-
export declare const browserbaseHoldings: {
|
|
32
|
-
connectUrls: Map<string, string>;
|
|
33
|
-
settings: Map<string, Pick<CdpSession, "close" | "open">>;
|
|
34
|
-
opening: Map<string, Promise<void>>;
|
|
35
|
-
uploads: Map<string, Promise<string>>;
|
|
36
|
-
boundDials: Map<string, string>;
|
|
37
|
-
};
|
|
38
|
-
export declare const kernelHoldings: {
|
|
39
|
-
cdpUrls: Map<string, string>;
|
|
40
|
-
liveUrls: Map<string, string>;
|
|
41
|
-
uploads: Map<string, Promise<{
|
|
42
|
-
id: string;
|
|
43
|
-
} | {
|
|
44
|
-
name: string;
|
|
45
|
-
}>>;
|
|
46
|
-
};
|
|
47
|
-
/** Which session may save to a named profile now, by profile: claimed by both cloud providers (providers/profiles.ts). */
|
|
2
|
+
/** Which session may save to a named profile now, by profile: claimed by the local Chrome provider (providers/profiles.ts). */
|
|
48
3
|
export declare const profileHoldings: {
|
|
49
4
|
writers: Map<string, {
|
|
50
5
|
tag: string;
|
|
@@ -53,4 +8,3 @@ export declare const profileHoldings: {
|
|
|
53
8
|
settle?: () => Promise<void>;
|
|
54
9
|
}>;
|
|
55
10
|
};
|
|
56
|
-
export declare function resetProviderHoldings(): void;
|
package/dist/providers/shared.js
CHANGED
|
@@ -1,86 +1,5 @@
|
|
|
1
|
-
import { createHash } from "node:crypto";
|
|
2
|
-
import path from "node:path";
|
|
3
|
-
import { fileURLToPath } from "node:url";
|
|
4
1
|
export const nonEmpty = (v) => String(v ?? "").trim();
|
|
5
|
-
/**
|
|
6
|
-
export const tagValue = (v) => String(v || "").replace(/[^A-Za-z0-9._:-]+/g, "_").slice(0, 60) || "unknown";
|
|
7
|
-
export function assertTag(tag) {
|
|
8
|
-
if (!/^[A-Za-z0-9_.-]+$/.test(tag))
|
|
9
|
-
throw new Error("a session tag is [A-Za-z0-9_.-]");
|
|
10
|
-
}
|
|
11
|
-
export function extensionArchive() {
|
|
12
|
-
return nonEmpty(process.env.STAGEHAND_EXTENSION_ARCHIVE_PATH) || path.join(path.dirname(fileURLToPath(import.meta.resolve("@browserbasehq/stagehand"))), "assets", "stagehand-extension.zip");
|
|
13
|
-
}
|
|
14
|
-
export const accountKey = (origin, apiKey) => `${origin}|${createHash("sha256").update(apiKey).digest("hex").slice(0, 16)}`;
|
|
15
|
-
export const defaultSleep = (ms) => new Promise((r) => setTimeout(r, ms));
|
|
16
|
-
export const DEFAULT_CONFIRM = { polls: 6, intervalMs: 500 };
|
|
17
|
-
/** The work itself goes on when `signal` fires: another caller may be waiting for it. */
|
|
18
|
-
export function untilAborted(work, signal) {
|
|
19
|
-
if (!signal)
|
|
20
|
-
return work;
|
|
21
|
-
return new Promise((resolve, reject) => {
|
|
22
|
-
const abort = () => reject(signal.reason ?? new DOMException("The operation was aborted.", "AbortError"));
|
|
23
|
-
// The outcome is taken first, so a failure after the caller left is handled, never unhandled.
|
|
24
|
-
work.then(resolve, reject).finally(() => signal.removeEventListener("abort", abort));
|
|
25
|
-
if (signal.aborted)
|
|
26
|
-
return abort();
|
|
27
|
-
signal.addEventListener("abort", abort, { once: true });
|
|
28
|
-
});
|
|
29
|
-
}
|
|
30
|
-
/** One upload per process and account; a failed upload is forgotten so the next create tries again. */
|
|
31
|
-
export function uploadOnce(uploads, account, upload) {
|
|
32
|
-
let up = uploads.get(account);
|
|
33
|
-
if (!up) {
|
|
34
|
-
up = upload();
|
|
35
|
-
uploads.set(account, up);
|
|
36
|
-
up.catch(() => uploads.delete(account));
|
|
37
|
-
}
|
|
38
|
-
return up;
|
|
39
|
-
}
|
|
40
|
-
/** The request is asked twice at most; released means the provider says the session is no longer running, not that the
|
|
41
|
-
* request was sent. `request` answers "gone" when the provider no longer knows the session. */
|
|
42
|
-
export async function releaseAndConfirm(o) {
|
|
43
|
-
let failure;
|
|
44
|
-
for (let attempt = 0; attempt < 2; attempt += 1) {
|
|
45
|
-
try {
|
|
46
|
-
if ((await o.request()) === "gone")
|
|
47
|
-
return o.forget();
|
|
48
|
-
failure = undefined;
|
|
49
|
-
break;
|
|
50
|
-
}
|
|
51
|
-
catch (error) {
|
|
52
|
-
failure = error;
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
for (let poll = 0;; poll += 1) {
|
|
56
|
-
const now = await o.status();
|
|
57
|
-
if (now !== "running" && now !== "pending")
|
|
58
|
-
return o.forget();
|
|
59
|
-
if (poll >= o.confirm.polls)
|
|
60
|
-
throw failure ?? new Error(o.stillRunning);
|
|
61
|
-
await o.sleep(o.confirm.intervalMs);
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
// Process-wide on purpose: a provider built later (custody builds one for the release) must still close the attach socket,
|
|
65
|
-
// drop the dial binding and reuse the account's upload. The URLs carry tokens and are never written anywhere.
|
|
66
|
-
export const browserbaseHoldings = {
|
|
67
|
-
connectUrls: new Map(),
|
|
68
|
-
settings: new Map(),
|
|
69
|
-
opening: new Map(),
|
|
70
|
-
uploads: new Map(),
|
|
71
|
-
boundDials: new Map(),
|
|
72
|
-
};
|
|
73
|
-
export const kernelHoldings = {
|
|
74
|
-
cdpUrls: new Map(),
|
|
75
|
-
liveUrls: new Map(),
|
|
76
|
-
uploads: new Map(),
|
|
77
|
-
};
|
|
78
|
-
/** Which session may save to a named profile now, by profile: claimed by both cloud providers (providers/profiles.ts). */
|
|
2
|
+
/** Which session may save to a named profile now, by profile: claimed by the local Chrome provider (providers/profiles.ts). */
|
|
79
3
|
export const profileHoldings = {
|
|
80
4
|
writers: new Map(),
|
|
81
5
|
};
|
|
82
|
-
export function resetProviderHoldings() {
|
|
83
|
-
for (const holdings of [browserbaseHoldings, kernelHoldings, profileHoldings])
|
|
84
|
-
for (const map of Object.values(holdings))
|
|
85
|
-
map.clear();
|
|
86
|
-
}
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
// browser starts, because its path decides the extension's id and so the one Origin the browser's socket is opened to.
|
|
6
6
|
import { cp, readFile, writeFile } from "node:fs/promises";
|
|
7
7
|
import path from "node:path";
|
|
8
|
-
import { cdpCall } from "
|
|
8
|
+
import { cdpCall } from "../core/cdp-call.js";
|
|
9
9
|
import { extensionDirectory, extensionOrigin } from "./chrome-args.js";
|
|
10
10
|
export const COPY = "stagehand-extension";
|
|
11
11
|
/** The name Stagehand's extension carries, which is how Stagehand finds it in a browser. */
|
|
@@ -1,20 +1,6 @@
|
|
|
1
1
|
import { EventEmitter } from "node:events";
|
|
2
|
-
import type { AttachTarget, BrowserProvider, ProviderCaps, RemoteFile
|
|
2
|
+
import type { AttachTarget, BrowserProvider, ProviderCaps, RemoteFile } from "../core/host.js";
|
|
3
3
|
import { type Sentinels } from "./sentinels.js";
|
|
4
|
-
export type FakeFetchByFormat = {
|
|
5
|
-
markdown?: {
|
|
6
|
-
contentType: string;
|
|
7
|
-
content: string;
|
|
8
|
-
} | {
|
|
9
|
-
status: number;
|
|
10
|
-
};
|
|
11
|
-
raw?: {
|
|
12
|
-
contentType: string;
|
|
13
|
-
content: string;
|
|
14
|
-
} | {
|
|
15
|
-
status: number;
|
|
16
|
-
};
|
|
17
|
-
};
|
|
18
4
|
/** A vendor SDK's failure: the HTTP status is on the error, never inside the text. */
|
|
19
5
|
export declare class FakeProviderError extends Error {
|
|
20
6
|
readonly status: number;
|
|
@@ -63,9 +49,6 @@ export type FakeBackendOptions = {
|
|
|
63
49
|
caps?: Partial<ProviderCaps>;
|
|
64
50
|
start?: string;
|
|
65
51
|
pages?: Record<string, FixturePage>;
|
|
66
|
-
/** A URL not listed is a 404; an object answers markdown and raw apart. */
|
|
67
|
-
fetches?: Record<string, string | FakeFetchByFormat>;
|
|
68
|
-
search?: SearchResult["results"];
|
|
69
52
|
/** Every ledger row is also appended here as JSONL, so a killed process leaves its calls behind. */
|
|
70
53
|
logFile?: string;
|
|
71
54
|
connections?: BrokerConnection[];
|
|
@@ -77,7 +60,7 @@ export type FakeBackendOptions = {
|
|
|
77
60
|
fullPageHeight?: number;
|
|
78
61
|
/** What `locate` finds, by the snapshot ID or the CSS selector a caller gives: one box or several (a selector that matches more than one). Anything else matches nothing. */
|
|
79
62
|
elements?: Record<string, FakeBox | FakeBox[]>;
|
|
80
|
-
/** Device pixels per CSS pixel; a screenshot is this many times larger
|
|
63
|
+
/** Device pixels per CSS pixel; a screenshot is this many times larger, times the `scale` it asks for. */
|
|
81
64
|
deviceScale?: number;
|
|
82
65
|
};
|
|
83
66
|
export type FakeBox = {
|
|
@@ -90,7 +73,7 @@ export type FakeScreenshotOptions = {
|
|
|
90
73
|
fullPage?: boolean;
|
|
91
74
|
type?: "png" | "jpeg";
|
|
92
75
|
quality?: number;
|
|
93
|
-
scale?:
|
|
76
|
+
scale?: number;
|
|
94
77
|
clip?: FakeBox;
|
|
95
78
|
};
|
|
96
79
|
/** A PNG or JPEG whose header declares `width` x `height`: enough for a reader that decodes the header, not a picture. */
|
|
@@ -128,7 +111,7 @@ export declare class FakeBackend {
|
|
|
128
111
|
private delays;
|
|
129
112
|
private peak;
|
|
130
113
|
private echoOn;
|
|
131
|
-
readonly opts: Required<Pick<FakeBackendOptions, "start" | "
|
|
114
|
+
readonly opts: Required<Pick<FakeBackendOptions, "start" | "connections" | "downloads">> & FakeBackendOptions;
|
|
132
115
|
constructor(options?: FakeBackendOptions);
|
|
133
116
|
/** The next call of `op` is applied and recorded, then never answered. */
|
|
134
117
|
hold(op: string): void;
|
|
@@ -155,8 +138,6 @@ export declare class FakeBackend {
|
|
|
155
138
|
attaches: number;
|
|
156
139
|
releases: number;
|
|
157
140
|
releaseNoops: number;
|
|
158
|
-
fetches: number;
|
|
159
|
-
searches: number;
|
|
160
141
|
dispatches: number;
|
|
161
142
|
reads: number;
|
|
162
143
|
peakLive: number;
|
|
@@ -227,7 +208,8 @@ export declare class FakeDriverFace {
|
|
|
227
208
|
}): Promise<unknown>;
|
|
228
209
|
/** No `dispatched` row: a read of the URL is not a read of the page. */
|
|
229
210
|
url(id: string): Promise<string>;
|
|
230
|
-
/** Header-only image of the viewport (or full page) times the device scale
|
|
211
|
+
/** Header-only image of the viewport (or full page) times the device scale and the asked scale, rounded as Chrome
|
|
212
|
+
* rounds; each capture is a dispatched row. */
|
|
231
213
|
screenshot(id: string, options?: FakeScreenshotOptions): Promise<{
|
|
232
214
|
data: string;
|
|
233
215
|
mimeType: string;
|