@parcha/agentrun-computer 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/README.md +251 -46
- package/box/Dockerfile +3 -1
- package/box/agentrun-archil +59 -7
- package/box/build-daytona.py +20 -4
- package/dist/browser.d.ts +73 -23
- package/dist/browser.js +141 -17
- package/dist/browserbase.d.ts +48 -25
- package/dist/browserbase.js +176 -29
- package/dist/chrome.d.ts +3 -4
- package/dist/chrome.js +6 -11
- package/dist/computerd-linux-amd64 +0 -0
- package/dist/computerd-linux-arm64 +0 -0
- package/dist/conformance/browserbase.js +1 -1
- package/dist/conformance/cases/provider.js +78 -3
- package/dist/conformance/daytona.d.ts +4 -0
- package/dist/conformance/daytona.js +7 -1
- package/dist/conformance/e2b.js +2 -1
- package/dist/conformance/fixture.js +24 -2
- package/dist/conformance/kernel.js +1 -1
- package/dist/conformance/kind.js +2 -1
- package/dist/conformance/{local.d.ts → local-chrome.d.ts} +7 -7
- package/dist/conformance/{local.js → local-chrome.js} +11 -11
- package/dist/conformance/orgo.d.ts +11 -5
- package/dist/conformance/orgo.js +37 -8
- package/dist/conformance/phone-harness.js +2 -1
- package/dist/conformance/report.js +3 -2
- package/dist/conformance/runner.js +25 -7
- package/dist/conformance/types.d.ts +21 -8
- package/dist/conformance/types.js +10 -0
- package/dist/conformance/windows-fixture.d.ts +1 -0
- package/dist/conformance/windows-fixture.js +37 -0
- package/dist/daytona.d.ts +16 -35
- package/dist/daytona.js +87 -95
- package/dist/descriptor.d.ts +116 -0
- package/dist/descriptor.js +92 -0
- package/dist/descriptors/browserbase.d.ts +25 -0
- package/dist/descriptors/browserbase.js +20 -0
- package/dist/descriptors/daytona.d.ts +83 -0
- package/dist/descriptors/daytona.js +81 -0
- package/dist/descriptors/docker.d.ts +12 -0
- package/dist/descriptors/docker.js +17 -0
- package/dist/descriptors/e2b.d.ts +22 -0
- package/dist/descriptors/e2b.js +19 -0
- package/dist/descriptors/index.d.ts +256 -0
- package/dist/descriptors/index.js +19 -0
- package/dist/descriptors/kernel.d.ts +21 -0
- package/dist/descriptors/kernel.js +17 -0
- package/dist/descriptors/local-chrome.d.ts +17 -0
- package/dist/descriptors/local-chrome.js +21 -0
- package/dist/descriptors/modal.d.ts +54 -0
- package/dist/descriptors/modal.js +48 -0
- package/dist/descriptors/orgo.d.ts +99 -0
- package/dist/descriptors/orgo.js +49 -0
- package/dist/descriptors/phone-harness.d.ts +33 -0
- package/dist/descriptors/phone-harness.js +23 -0
- package/dist/descriptors/shared.d.ts +7 -0
- package/dist/descriptors/shared.js +6 -0
- package/dist/desktop.d.ts +4 -4
- package/dist/desktop.js +4 -4
- package/dist/docker/egress.d.ts +5 -4
- package/dist/docker/egress.js +7 -6
- package/dist/docker/image.js +17 -15
- package/dist/docker/inspect.d.ts +147 -0
- package/dist/docker/inspect.js +335 -0
- package/dist/docker.d.ts +6 -6
- package/dist/docker.js +53 -47
- package/dist/drift.d.ts +3 -145
- package/dist/drift.js +5 -334
- package/dist/e2b.d.ts +2 -8
- package/dist/e2b.js +17 -23
- package/dist/index.d.ts +9 -12
- package/dist/index.js +14 -12
- package/dist/isolation.js +2 -2
- package/dist/janitor.d.ts +20 -2
- package/dist/janitor.js +67 -19
- package/dist/kernel.d.ts +19 -29
- package/dist/kernel.js +110 -32
- package/dist/kinds.d.ts +59 -34
- package/dist/kinds.js +19 -117
- package/dist/labels.d.ts +20 -0
- package/dist/labels.js +62 -1
- package/dist/{local.d.ts → local-chrome.d.ts} +8 -9
- package/dist/{local.js → local-chrome.js} +26 -19
- package/dist/local-supervisor.d.ts +3 -3
- package/dist/local-supervisor.js +3 -3
- package/dist/modal.d.ts +6 -25
- package/dist/modal.js +86 -88
- package/dist/mount-archil.d.ts +25 -3
- package/dist/mount-archil.js +43 -12
- package/dist/orgo-channel.js +16 -13
- package/dist/orgo-desktop.d.ts +162 -0
- package/dist/orgo-desktop.js +1856 -0
- package/dist/orgo-names.d.ts +178 -0
- package/dist/orgo-names.js +287 -0
- package/dist/orgo.d.ts +34 -20
- package/dist/orgo.js +424 -173
- package/dist/phone-harness.d.ts +7 -12
- package/dist/phone-harness.js +70 -54
- package/dist/protocol.d.ts +5 -0
- package/dist/provider.d.ts +153 -10
- package/dist/provider.js +48 -1
- package/dist/providers.d.ts +23 -24
- package/dist/providers.js +14 -17
- package/dist/result.d.ts +11 -1
- package/dist/result.js +11 -0
- package/dist/test-connection.js +2 -2
- package/package.json +42 -2
package/dist/browserbase.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { type
|
|
1
|
+
import { type BrowserComputerProvider, type BrowserDownloads, type BrowserProfiles, type BrowserStream, type ReleaseConfirm } from "./browser.js";
|
|
2
2
|
import type { KindCapabilities } from "./kinds.js";
|
|
3
3
|
import type { MountKind } from "./mount.js";
|
|
4
|
-
import type
|
|
4
|
+
import { type AttachedComputer, type CallContext, type ComputerCapabilities, type ComputerConnection, type ComputerInfo, type ComputerProvider, type ComputerRef, type ComputerSpec, type CreatedComputer, type NetworkPolicy, type NetworkSpec, type Refusal, type ReleaseHow } from "./provider.js";
|
|
5
5
|
import { type Result } from "./result.js";
|
|
6
6
|
export declare const BROWSERBASE_API_URL = "https://api.browserbase.com";
|
|
7
7
|
/** The domain a Browserbase CDP URL is on (wss://connect.browserbase.com, read live 11-08): cdpUrlProblem holds it there. */
|
|
@@ -18,9 +18,7 @@ export declare const labelKey: (metadataKey: string) => string;
|
|
|
18
18
|
export declare function browserbaseLabelsProblem(labels: unknown): string | undefined;
|
|
19
19
|
/** A browser takes no mount. */
|
|
20
20
|
export declare const MOUNT: readonly MountKind[];
|
|
21
|
-
/** The
|
|
22
|
-
export declare const NETWORK: readonly NetworkForm[];
|
|
23
|
-
/** The one configuration it offers, the kind registry's entry (kinds.ts): a browser with a CDP target and a live view. */
|
|
21
|
+
/** The one configuration it offers, its descriptor's: a browser with a CDP target and a live view. */
|
|
24
22
|
export declare const CAPABILITIES: readonly KindCapabilities[];
|
|
25
23
|
/** A session as Browserbase answers it; only what the provider reads. */
|
|
26
24
|
export interface BrowserbaseSession {
|
|
@@ -42,10 +40,18 @@ export interface BrowserbaseDebug {
|
|
|
42
40
|
debuggerFullscreenUrl?: string;
|
|
43
41
|
}>;
|
|
44
42
|
}
|
|
43
|
+
/** One row of a session's downloads, as GET /v1/downloads answers it; only what the provider reads. */
|
|
44
|
+
export interface BrowserbaseDownload {
|
|
45
|
+
id: string;
|
|
46
|
+
filename: string;
|
|
47
|
+
size: number;
|
|
48
|
+
createdAt?: string;
|
|
49
|
+
}
|
|
45
50
|
/**
|
|
46
51
|
* The calls the provider makes, one Browserbase request each, never retried where a retry could open a second paid
|
|
47
52
|
* browser. A failure throws, with the HTTP status as `status` when one came back; the provider returns it as a result
|
|
48
|
-
* whose hidden cause is the thrown error
|
|
53
|
+
* whose hidden cause is the thrown error. The context calls back the profiles member, the download calls the downloads
|
|
54
|
+
* member.
|
|
49
55
|
*/
|
|
50
56
|
export interface BrowserbaseApi {
|
|
51
57
|
create(body: Record<string, unknown>, signal?: AbortSignal): Promise<BrowserbaseSession>;
|
|
@@ -58,6 +64,17 @@ export interface BrowserbaseApi {
|
|
|
58
64
|
/** "gone" when Browserbase does not know the session (404). */
|
|
59
65
|
release(id: string, signal?: AbortSignal): Promise<"gone" | "asked">;
|
|
60
66
|
debug(id: string, signal?: AbortSignal): Promise<BrowserbaseDebug>;
|
|
67
|
+
/** Makes a context (POST /v1/contexts): its id. */
|
|
68
|
+
createContext(signal?: AbortSignal): Promise<string>;
|
|
69
|
+
/** "gone" when Browserbase does not know the context (404). */
|
|
70
|
+
deleteContext(id: string, signal?: AbortSignal): Promise<"gone" | "deleted">;
|
|
71
|
+
/** One page of a session's downloads, from offset, and how many there are in all. */
|
|
72
|
+
listDownloads(sessionId: string, offset: number, signal?: AbortSignal): Promise<{
|
|
73
|
+
downloads: BrowserbaseDownload[];
|
|
74
|
+
total: number;
|
|
75
|
+
}>;
|
|
76
|
+
/** A download's bytes, streamed; undefined when Browserbase does not know it (404). */
|
|
77
|
+
readDownload(id: string, signal?: AbortSignal): Promise<AsyncIterable<Uint8Array> | undefined>;
|
|
61
78
|
}
|
|
62
79
|
/** Browserbase's v1 REST API over fetch: the default api. */
|
|
63
80
|
export declare function browserbaseRest({ apiKey, apiUrl, projectId, fetch: fetcher }: {
|
|
@@ -66,43 +83,50 @@ export declare function browserbaseRest({ apiKey, apiUrl, projectId, fetch: fetc
|
|
|
66
83
|
projectId?: string;
|
|
67
84
|
fetch?: typeof fetch;
|
|
68
85
|
}): BrowserbaseApi;
|
|
86
|
+
/** How long after a saving session ends Browserbase may still be writing its context back. */
|
|
87
|
+
export declare const BROWSERBASE_PROFILE_SETTLE_MS = 5000;
|
|
88
|
+
/** The directory Browserbase syncs a session's downloads from: a driver sets the download behaviour into it. */
|
|
89
|
+
export declare const BROWSERBASE_DOWNLOADS_PATH = "downloads";
|
|
69
90
|
export interface BrowserbaseProviderOptions {
|
|
70
|
-
/** With apiUrl and fetch, the REST api is built from it
|
|
91
|
+
/** With apiUrl and fetch, the REST api is built from it. */
|
|
71
92
|
apiKey?: string;
|
|
72
|
-
/** Named in every create and release;
|
|
93
|
+
/** Named in every create and release; none behind a gateway (env), whose key names the project. */
|
|
73
94
|
projectId?: string;
|
|
95
|
+
/** Browserbase's API; default BROWSERBASE_BASE_URL in env (a gateway that fronts it), else Browserbase's own. */
|
|
74
96
|
apiUrl?: string;
|
|
97
|
+
/**
|
|
98
|
+
* The host's own variables its descriptor declares (hostEnv): BROWSERBASE_BASE_URL, a gateway that fronts the API, and
|
|
99
|
+
* BROWSERBASE_CONNECT_BASE_URL, one that fronts the sessions' sockets. Behind either, the gateway's key names the
|
|
100
|
+
* project, so no projectId is sent.
|
|
101
|
+
*/
|
|
102
|
+
env?: Readonly<Record<string, string | undefined>>;
|
|
75
103
|
fetch?: typeof fetch;
|
|
76
|
-
/** The calls, a host's own (pi-browser's SDK client); default the REST api. */
|
|
77
|
-
api?: BrowserbaseApi;
|
|
78
|
-
/** Where the signed connect URLs are held; default a Map of this provider's own. */
|
|
79
|
-
urls?: UrlStore;
|
|
80
104
|
confirm?: ReleaseConfirm;
|
|
81
105
|
sleep?: (ms: number) => Promise<void>;
|
|
82
106
|
}
|
|
83
|
-
/** What a host may add to a session's create body: Browserbase's own settings (proxies, browserSettings, region, extensionId, a context). */
|
|
84
|
-
export type BrowserbaseSessionSettings = Record<string, unknown>;
|
|
85
107
|
/** Computers that are Browserbase sessions. */
|
|
86
|
-
export declare class BrowserbaseProvider implements ComputerProvider {
|
|
108
|
+
export declare class BrowserbaseProvider implements ComputerProvider, BrowserComputerProvider {
|
|
87
109
|
#private;
|
|
88
110
|
readonly id = "browserbase";
|
|
89
111
|
readonly version: string;
|
|
90
|
-
|
|
112
|
+
readonly profiles: BrowserProfiles;
|
|
113
|
+
readonly downloads: BrowserDownloads;
|
|
114
|
+
constructor({ apiKey, projectId, apiUrl, env, fetch: fetcher, confirm, sleep }: BrowserbaseProviderOptions);
|
|
91
115
|
/**
|
|
92
116
|
* A browser: Browserbase ends a session at its timeout (lifetime vendor), at most 6 h; it reaches the open web; each
|
|
93
|
-
* session is its own VM.
|
|
94
|
-
*
|
|
117
|
+
* session is its own VM. browser is what its sessions offer beyond the protocol. Its downloads are read through the
|
|
118
|
+
* downloads member (browser.downloads); the group stays undeclared until the kind registry's entry (kinds.ts)
|
|
119
|
+
* declares it too.
|
|
95
120
|
*/
|
|
96
121
|
capabilities(): ComputerCapabilities;
|
|
97
122
|
check(spec: ComputerSpec): Refusal[];
|
|
98
123
|
/**
|
|
99
124
|
* Makes a session at most once: with keepAlive, so a dropped CDP client never ends it, the lifetime as its timeout in
|
|
100
|
-
* whole seconds, and the labels as its userMetadata.
|
|
101
|
-
*
|
|
125
|
+
* whole seconds, and the labels as its userMetadata. spec.browser is the session's own options, each refused by check()
|
|
126
|
+
* unless declared.
|
|
102
127
|
*/
|
|
103
|
-
create(spec: ComputerSpec, { signal
|
|
128
|
+
create(spec: ComputerSpec, { signal }?: {
|
|
104
129
|
signal?: AbortSignal;
|
|
105
|
-
settings?: BrowserbaseSessionSettings;
|
|
106
130
|
}): Promise<Result<CreatedComputer>>;
|
|
107
131
|
/**
|
|
108
132
|
* Settings' test: one read of the project's running sessions (GET /v1/sessions?status=RUNNING), which makes nothing.
|
|
@@ -126,15 +150,14 @@ export declare class BrowserbaseProvider implements ComputerProvider {
|
|
|
126
150
|
*/
|
|
127
151
|
connect(ref: ComputerRef): Promise<Result<ComputerConnection>>;
|
|
128
152
|
/** Asks Browserbase to release the session and waits until it reads as ended; one already ended or unknown is released. */
|
|
129
|
-
release(ref: ComputerRef, how:
|
|
153
|
+
release(ref: ComputerRef, how: ReleaseHow, { signal }?: CallContext): Promise<Result<void>>;
|
|
130
154
|
/** The session's live view, which takes input: a view-only one is refused, never handed over as one. */
|
|
131
155
|
stream(ref: ComputerRef, options?: {
|
|
132
156
|
viewOnly?: boolean;
|
|
133
157
|
}, { signal }?: CallContext): Promise<Result<BrowserStream>>;
|
|
134
158
|
}
|
|
135
159
|
/**
|
|
136
|
-
* The provider, built from its options alone, the key included (options.apiKey,
|
|
137
|
-
* host's own transport); it reads no environment, and throws in words when the key is missing, never echoing one.
|
|
160
|
+
* The provider, built from its options alone, the key included (options.apiKey); it reads no environment, and throws in words when the key is missing, never echoing one.
|
|
138
161
|
*/
|
|
139
162
|
export declare function create(options: BrowserbaseProviderOptions): BrowserbaseProvider;
|
|
140
163
|
/** The conformance CLI's module for Browserbase: its live switch, its image and its notes. */
|
package/dist/browserbase.js
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
// Browserbase as a computer of kind browser: each computer is a Browserbase session, over Browserbase's v1 REST API with
|
|
2
|
-
// fetch (no SDK)
|
|
3
|
-
// userMetadata; attach and list find a session by them; connect hands over the session's signed connectUrl, which a
|
|
2
|
+
// fetch (no SDK). Labels ride the session's own userMetadata; attach and list find a session by them; connect hands over the session's signed connectUrl, which a
|
|
4
3
|
// driver dials; stream is its live view (the debugger's fullscreen URL). Both URLs are secrets: they stay in memory and
|
|
5
4
|
// never reach a ref, a list, an error's words or a report.
|
|
6
5
|
//
|
|
@@ -9,15 +8,25 @@
|
|
|
9
8
|
// Browserbase answer 504 after 75 s), POST /v1/sessions/{id} with status REQUEST_RELEASE (release), and
|
|
10
9
|
// GET /v1/sessions/{id}/debug (the live view). The key is the X-BB-API-Key header.
|
|
11
10
|
//
|
|
11
|
+
// A session's own options (spec.browser): viewport and a profile ride browserSettings (a profile is a context,
|
|
12
|
+
// { id: pointer, persist: save }); stealth is Browserbase's Verified browser with its CAPTCHA solver (verified and
|
|
13
|
+
// solveCaptchas; the solver is off otherwise, as Browserbase's default is on). Profiles are contexts: POST /v1/contexts ({ projectId } when the provider has one) and DELETE /v1/contexts/{id}. Downloads are the session's
|
|
14
|
+
// synced files: GET /v1/downloads?sessionId=&limit=&offset= (paged by total) and GET /v1/downloads/{id}, the bytes.
|
|
15
|
+
//
|
|
12
16
|
// A label's key is not its metadata key: Browserbase refuses a dot in a userMetadata key (POST /v1/sessions answered 400
|
|
13
17
|
// "Key is not a valid metadata key: agentrun.conformance", found live on 11-08), and the protocol's labels are dotted
|
|
14
18
|
// (agentrun.run, agentrun.epoch). metadataKey() writes each "." as "_", and labelKey() reads it back. check() takes a
|
|
15
19
|
// label key of letters, digits and dots alone, so no label key holds an "_" and the mapping is one to one; a value with
|
|
16
20
|
// ' or \ is refused too, since list's query quotes it.
|
|
17
21
|
import { readFileSync } from "node:fs";
|
|
18
|
-
import {
|
|
19
|
-
import {
|
|
22
|
+
import { BROWSERBASE } from "./descriptors/browserbase.js";
|
|
23
|
+
import { BROWSER_IMAGE } from "./descriptors/shared.js";
|
|
24
|
+
import { cdpUrlProblem, browserClient, browserHello, BrowserVendorError, CREATE_LABEL, carries, causeOf, checkBrowser, createLabelValue, createUncertain, DEFAULT_CONFIRM, defaultSleep, httpsBase, idProblem, labelsOf, labelsProblem, limitedBytes, memoryUrls, pointerOk, queryable, recoverCreate, releaseConfirmed, restBytes, restCall, causedFail, vendorFailure, withoutCreateLabel, } from "./browser.js";
|
|
25
|
+
import { runLifetimeMs, } from "./provider.js";
|
|
26
|
+
import { failFor, ok, vendorFail } from "./result.js";
|
|
20
27
|
import { connectionError } from "./test-connection.js";
|
|
28
|
+
/** This module's failures: one with code vendor names Browserbase. */
|
|
29
|
+
const fail = failFor("browserbase");
|
|
21
30
|
const VERSION = JSON.parse(readFileSync(new URL("../package.json", import.meta.url), "utf8")).version;
|
|
22
31
|
const NAME = "Browserbase";
|
|
23
32
|
export const BROWSERBASE_API_URL = "https://api.browserbase.com";
|
|
@@ -56,11 +65,11 @@ export function browserbaseLabelsProblem(labels) {
|
|
|
56
65
|
return undefined;
|
|
57
66
|
}
|
|
58
67
|
/** A browser takes no mount. */
|
|
59
|
-
export const MOUNT =
|
|
60
|
-
/** The
|
|
61
|
-
export const
|
|
62
|
-
/**
|
|
63
|
-
|
|
68
|
+
export const MOUNT = BROWSERBASE.mounts;
|
|
69
|
+
/** The one configuration it offers, its descriptor's: a browser with a CDP target and a live view. */
|
|
70
|
+
export const CAPABILITIES = BROWSERBASE.capabilities;
|
|
71
|
+
/** A plain id as Browserbase answers one (a context's, a download's): a token, never a path. */
|
|
72
|
+
const plainId = (id) => typeof id === "string" && /^[A-Za-z0-9_.:-]{1,128}$/.test(id);
|
|
64
73
|
/** Browserbase's v1 REST API over fetch: the default api. */
|
|
65
74
|
export function browserbaseRest({ apiKey, apiUrl = BROWSERBASE_API_URL, projectId, fetch: fetcher = fetch }) {
|
|
66
75
|
const o = { vendor: NAME, fetch: fetcher, base: httpsBase(apiUrl, "the Browserbase API URL"), headers: { "X-BB-API-Key": apiKey } };
|
|
@@ -106,8 +115,42 @@ export function browserbaseRest({ apiKey, apiUrl = BROWSERBASE_API_URL, projectI
|
|
|
106
115
|
throw new BrowserVendorError(NAME, call, 404);
|
|
107
116
|
return (answer.json ?? {});
|
|
108
117
|
},
|
|
118
|
+
async createContext(signal) {
|
|
119
|
+
const call = "POST /v1/contexts";
|
|
120
|
+
const answer = await restCall(o, "POST", "/v1/contexts", call, { body: projectId ? { projectId } : {}, ...(signal ? { signal } : {}) });
|
|
121
|
+
const id = answer.json?.id;
|
|
122
|
+
if (answer.status === 404 || !plainId(id))
|
|
123
|
+
throw new BrowserVendorError(NAME, call, answer.status, "no_context_id");
|
|
124
|
+
return id;
|
|
125
|
+
},
|
|
126
|
+
async deleteContext(id, signal) {
|
|
127
|
+
const answer = await restCall(o, "DELETE", `/v1/contexts/${encodeURIComponent(id)}`, "DELETE /v1/contexts/{id}", signal ? { signal } : {});
|
|
128
|
+
return answer.status === 404 ? "gone" : "deleted";
|
|
129
|
+
},
|
|
130
|
+
async listDownloads(sessionId, offset, signal) {
|
|
131
|
+
const call = "GET /v1/downloads";
|
|
132
|
+
const query = new URLSearchParams({ sessionId, limit: String(DOWNLOADS_PAGE), offset: String(offset) });
|
|
133
|
+
const answer = await restCall(o, "GET", "/v1/downloads", call, { query, ...(signal ? { signal } : {}) });
|
|
134
|
+
// A list is never "not found": a 404 is a wrong base URL, and an empty answer would hide every file.
|
|
135
|
+
if (answer.status === 404)
|
|
136
|
+
throw new BrowserVendorError(NAME, call, 404);
|
|
137
|
+
const page = answer.json;
|
|
138
|
+
if (!Array.isArray(page?.downloads) || !Number.isInteger(page.total))
|
|
139
|
+
throw new BrowserVendorError(NAME, call, answer.status, "not_a_list");
|
|
140
|
+
return { downloads: page.downloads, total: page.total };
|
|
141
|
+
},
|
|
142
|
+
async readDownload(id, signal) {
|
|
143
|
+
return restBytes(o, `/v1/downloads/${encodeURIComponent(id)}`, "GET /v1/downloads/{id}", signal);
|
|
144
|
+
},
|
|
109
145
|
};
|
|
110
146
|
}
|
|
147
|
+
/** Rows per downloads page, and the most pages a list reads before it fails rather than run on. */
|
|
148
|
+
const DOWNLOADS_PAGE = 100;
|
|
149
|
+
const MAX_PAGES = 100;
|
|
150
|
+
/** How long after a saving session ends Browserbase may still be writing its context back. */
|
|
151
|
+
export const BROWSERBASE_PROFILE_SETTLE_MS = 5_000;
|
|
152
|
+
/** The directory Browserbase syncs a session's downloads from: a driver sets the download behaviour into it. */
|
|
153
|
+
export const BROWSERBASE_DOWNLOADS_PATH = "downloads";
|
|
111
154
|
const refOf = (s) => ({ id: s.id, name: s.id });
|
|
112
155
|
const deadlineOf = (s) => {
|
|
113
156
|
const at = typeof s.expiresAt === "string" ? new Date(s.expiresAt) : undefined;
|
|
@@ -117,36 +160,57 @@ const deadlineOf = (s) => {
|
|
|
117
160
|
export class BrowserbaseProvider {
|
|
118
161
|
id = "browserbase";
|
|
119
162
|
version = VERSION;
|
|
163
|
+
/**
|
|
164
|
+
* A session ends at its timeout, a wall clock; it may be stealth (Verified, with the CAPTCHA solver); it opens a
|
|
165
|
+
* context as a profile (written back about 5 s after a saving session ends) and keeps what is saved into downloads/.
|
|
166
|
+
* capabilities().browser gives it.
|
|
167
|
+
*/
|
|
168
|
+
#browser;
|
|
169
|
+
profiles;
|
|
170
|
+
downloads;
|
|
120
171
|
#api;
|
|
121
172
|
#projectId;
|
|
122
173
|
#urls;
|
|
123
174
|
#confirm;
|
|
124
175
|
#sleep;
|
|
125
|
-
constructor({ apiKey, projectId, apiUrl, fetch: fetcher,
|
|
126
|
-
if (
|
|
127
|
-
throw new Error("set options.apiKey to a Browserbase API key
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
this.#
|
|
176
|
+
constructor({ apiKey, projectId, apiUrl, env = {}, fetch: fetcher, confirm = DEFAULT_CONFIRM, sleep = defaultSleep }) {
|
|
177
|
+
if (typeof apiKey !== "string" || apiKey === "")
|
|
178
|
+
throw new Error("set options.apiKey to a Browserbase API key: the provider reads no environment");
|
|
179
|
+
const gateway = env.BROWSERBASE_BASE_URL?.trim() || undefined;
|
|
180
|
+
const behindGateway = Boolean(gateway || env.BROWSERBASE_CONNECT_BASE_URL?.trim());
|
|
181
|
+
this.#projectId = behindGateway ? undefined : projectId || undefined;
|
|
182
|
+
const base = apiUrl || gateway;
|
|
183
|
+
this.#api = browserbaseRest({ apiKey, ...(base ? { apiUrl: base } : {}), ...(this.#projectId ? { projectId: this.#projectId } : {}), ...(fetcher ? { fetch: fetcher } : {}) });
|
|
184
|
+
this.#urls = memoryUrls();
|
|
131
185
|
this.#confirm = confirm;
|
|
132
186
|
this.#sleep = sleep;
|
|
187
|
+
this.#browser = {
|
|
188
|
+
timeout: "wall-clock",
|
|
189
|
+
stealth: true,
|
|
190
|
+
profiles: { settleMs: BROWSERBASE_PROFILE_SETTLE_MS },
|
|
191
|
+
downloads: { path: BROWSERBASE_DOWNLOADS_PATH },
|
|
192
|
+
};
|
|
193
|
+
this.profiles = { create: (context) => this.#createProfile(context), remove: (pointer, context) => this.#removeProfile(pointer, context) };
|
|
194
|
+
this.downloads = { list: (ref, context) => this.#listDownloads(ref, context), read: (ref, name, maxBytes, context) => this.#readDownload(ref, name, maxBytes, context) };
|
|
133
195
|
}
|
|
134
196
|
/**
|
|
135
197
|
* A browser: Browserbase ends a session at its timeout (lifetime vendor), at most 6 h; it reaches the open web; each
|
|
136
|
-
* session is its own VM.
|
|
137
|
-
*
|
|
198
|
+
* session is its own VM. browser is what its sessions offer beyond the protocol. Its downloads are read through the
|
|
199
|
+
* downloads member (browser.downloads); the group stays undeclared until the kind registry's entry (kinds.ts)
|
|
200
|
+
* declares it too.
|
|
138
201
|
*/
|
|
139
202
|
capabilities() {
|
|
140
203
|
return {
|
|
141
204
|
kind: "browser",
|
|
142
205
|
lifetime: "vendor",
|
|
143
|
-
network: [...
|
|
206
|
+
network: [...BROWSERBASE.network],
|
|
144
207
|
isolation: "vm",
|
|
145
208
|
os: "linux",
|
|
146
209
|
daemon: "none",
|
|
147
210
|
groups: [...CAPABILITIES[0].groups],
|
|
148
211
|
maxLifetimeMs: BROWSERBASE_MAX_LIFETIME_MS,
|
|
149
212
|
release: ["delete"],
|
|
213
|
+
browser: structuredClone(this.#browser),
|
|
150
214
|
};
|
|
151
215
|
}
|
|
152
216
|
check(spec) {
|
|
@@ -161,19 +225,27 @@ export class BrowserbaseProvider {
|
|
|
161
225
|
}
|
|
162
226
|
/**
|
|
163
227
|
* Makes a session at most once: with keepAlive, so a dropped CDP client never ends it, the lifetime as its timeout in
|
|
164
|
-
* whole seconds, and the labels as its userMetadata.
|
|
165
|
-
*
|
|
228
|
+
* whole seconds, and the labels as its userMetadata. spec.browser is the session's own options, each refused by check()
|
|
229
|
+
* unless declared.
|
|
166
230
|
*/
|
|
167
|
-
async create(spec, { signal
|
|
231
|
+
async create(spec, { signal } = {}) {
|
|
168
232
|
const refusals = this.check(spec);
|
|
169
233
|
if (refusals.length > 0)
|
|
170
234
|
return fail("refused", refusals.map(({ field, message }) => `${field}: ${message}`).join("; "));
|
|
171
235
|
if (signal?.aborted)
|
|
172
236
|
return fail("aborted", "create was aborted before Browserbase was called");
|
|
237
|
+
const browser = spec.browser;
|
|
173
238
|
const written = { [CREATE_LABEL]: createLabelValue(), ...spec.labels };
|
|
239
|
+
// Browserbase solves CAPTCHAs unless told not to: a browser's options say so either way, the solver only with stealth.
|
|
240
|
+
const browserSettings = {
|
|
241
|
+
...(browser?.viewport ? { viewport: { width: browser.viewport.width, height: browser.viewport.height } } : {}),
|
|
242
|
+
...(browser !== undefined ? { solveCaptchas: browser.stealth === true } : {}),
|
|
243
|
+
...(browser?.stealth ? { verified: true } : {}),
|
|
244
|
+
...(browser?.profile ? { context: { id: browser.profile.pointer, persist: browser.profile.save } } : {}),
|
|
245
|
+
};
|
|
174
246
|
const body = {
|
|
175
|
-
...
|
|
176
|
-
timeout: Math.ceil(spec
|
|
247
|
+
...(Object.keys(browserSettings).length > 0 ? { browserSettings } : {}),
|
|
248
|
+
timeout: Math.ceil(runLifetimeMs(spec) / 1000),
|
|
177
249
|
keepAlive: true,
|
|
178
250
|
userMetadata: toMetadata(written),
|
|
179
251
|
...(this.#projectId ? { projectId: this.#projectId } : {}),
|
|
@@ -187,20 +259,96 @@ export class BrowserbaseProvider {
|
|
|
187
259
|
if (failed.ok || !createUncertain(error))
|
|
188
260
|
return failed;
|
|
189
261
|
// The session may have been made though its answer never came: found by its create label, never made again.
|
|
190
|
-
return
|
|
262
|
+
return causedFail(failed.error.code, `${failed.error.message}; ${await this.#recover(written)}`, causeOf(failed), "browserbase");
|
|
191
263
|
}
|
|
192
264
|
if (idProblem(made?.id, NAME) !== undefined) {
|
|
193
|
-
return
|
|
265
|
+
return vendorFail("browserbase", `Browserbase POST /v1/sessions answered with no session id, so there is no session to connect to; ${await this.#recover(written)}`);
|
|
194
266
|
}
|
|
195
267
|
const unusable = cdpUrlProblem(made.connectUrl, NAME, CDP_DOMAIN);
|
|
196
268
|
if (unusable !== undefined) {
|
|
197
269
|
// The session is made and billed (keepAlive, up to its timeout): released here, not left to a later sweep.
|
|
198
270
|
const released = await this.release(refOf(made), "delete");
|
|
199
|
-
return
|
|
271
|
+
return vendorFail("browserbase", `Browserbase POST /v1/sessions made a session with no CDP URL to hand over: ${unusable}; ${released.ok ? "the session was released" : `the session was not released (${released.error.code}), and its labels let a sweep find it`}`);
|
|
200
272
|
}
|
|
201
273
|
this.#urls.set(made.id, made.connectUrl);
|
|
202
274
|
return ok(refOf(made));
|
|
203
275
|
}
|
|
276
|
+
/** A new context, a profile's pointer: one POST /v1/contexts, never retried. */
|
|
277
|
+
async #createProfile({ signal } = {}) {
|
|
278
|
+
try {
|
|
279
|
+
return ok(await this.#api.createContext(signal));
|
|
280
|
+
}
|
|
281
|
+
catch (error) {
|
|
282
|
+
return vendorFailure("browserbase", NAME, "POST /v1/contexts", error);
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
/** Deletes the context; one Browserbase no longer knows is removed. The words never carry the pointer. */
|
|
286
|
+
async #removeProfile(pointer, { signal } = {}) {
|
|
287
|
+
if (!pointerOk(pointer))
|
|
288
|
+
return fail("invalid", "a profile's pointer is a plain token, as profiles.create gave it");
|
|
289
|
+
try {
|
|
290
|
+
await this.#api.deleteContext(pointer, signal);
|
|
291
|
+
return ok(undefined);
|
|
292
|
+
}
|
|
293
|
+
catch (error) {
|
|
294
|
+
return vendorFailure("browserbase", NAME, "DELETE /v1/contexts/{id}", error);
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
/** Every row of a session's downloads, page by page until total; a page that cannot be read fails the whole list. */
|
|
298
|
+
async #downloadRows(id, signal) {
|
|
299
|
+
const problem = idProblem(id, NAME);
|
|
300
|
+
if (problem)
|
|
301
|
+
return fail("invalid", problem);
|
|
302
|
+
const rows = [];
|
|
303
|
+
for (let pages = 0; pages < MAX_PAGES; pages++) {
|
|
304
|
+
let got;
|
|
305
|
+
try {
|
|
306
|
+
got = await this.#api.listDownloads(id, rows.length, signal);
|
|
307
|
+
}
|
|
308
|
+
catch (error) {
|
|
309
|
+
return vendorFailure("browserbase", NAME, "GET /v1/downloads", error);
|
|
310
|
+
}
|
|
311
|
+
for (const row of got.downloads) {
|
|
312
|
+
if (!plainId(row?.id) || typeof row.filename !== "string" || row.filename === "" || !Number.isFinite(row.size) || row.size < 0) {
|
|
313
|
+
return vendorFail("browserbase", "Browserbase GET /v1/downloads answered a row that is not a download");
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
rows.push(...got.downloads);
|
|
317
|
+
if (got.downloads.length === 0 || rows.length >= got.total)
|
|
318
|
+
return ok(rows);
|
|
319
|
+
}
|
|
320
|
+
return vendorFail("browserbase", `Browserbase kept answering more pages of downloads past ${MAX_PAGES}`);
|
|
321
|
+
}
|
|
322
|
+
async #listDownloads(ref, { signal } = {}) {
|
|
323
|
+
const rows = await this.#downloadRows(ref?.id, signal);
|
|
324
|
+
if (!rows.ok)
|
|
325
|
+
return rows;
|
|
326
|
+
// Browserbase suffixes each name with a timestamp, so a name is one file in a session.
|
|
327
|
+
return ok(rows.value.map((d) => ({ name: d.filename, sizeBytes: d.size, modifiedAt: typeof d.createdAt === "string" ? d.createdAt : null })));
|
|
328
|
+
}
|
|
329
|
+
/** A download's bytes by its name: refused past maxBytes before it is fetched when its size says so, and cut once the stream passes it. */
|
|
330
|
+
async #readDownload(ref, name, maxBytes, { signal } = {}) {
|
|
331
|
+
if (!Number.isInteger(maxBytes) || maxBytes < 0)
|
|
332
|
+
return fail("invalid", "maxBytes is a whole number of bytes");
|
|
333
|
+
const rows = await this.#downloadRows(ref?.id, signal);
|
|
334
|
+
if (!rows.ok)
|
|
335
|
+
return rows;
|
|
336
|
+
const row = rows.value.find((d) => d.filename === name);
|
|
337
|
+
if (row === undefined)
|
|
338
|
+
return fail("not_found", `the Browserbase session ${ref.id} downloaded no file named ${JSON.stringify(name)}`);
|
|
339
|
+
if (row.size > maxBytes)
|
|
340
|
+
return fail("limit", `the download ${JSON.stringify(name)} is ${row.size} bytes, larger than the ${maxBytes}-byte limit`);
|
|
341
|
+
let body;
|
|
342
|
+
try {
|
|
343
|
+
body = await this.#api.readDownload(row.id, signal);
|
|
344
|
+
}
|
|
345
|
+
catch (error) {
|
|
346
|
+
return vendorFailure("browserbase", NAME, "GET /v1/downloads/{id}", error);
|
|
347
|
+
}
|
|
348
|
+
if (body === undefined)
|
|
349
|
+
return fail("not_found", `Browserbase no longer holds the download ${JSON.stringify(name)}`);
|
|
350
|
+
return ok(limitedBytes(body, maxBytes, `the download ${JSON.stringify(name)}`));
|
|
351
|
+
}
|
|
204
352
|
/**
|
|
205
353
|
* The sessions a lost create made, released: what recoverCreate says of them. They are found as list finds any, by
|
|
206
354
|
* the labels the create wrote (its create label first, so the query names it, then the run's), through #sessions and
|
|
@@ -344,7 +492,7 @@ export class BrowserbaseProvider {
|
|
|
344
492
|
// Whatever held it (create, a read, a host's url store), a URL a driver would dial is wss: on Browserbase's own domain.
|
|
345
493
|
const unusable = cdpUrlProblem(url, NAME, CDP_DOMAIN);
|
|
346
494
|
if (unusable !== undefined)
|
|
347
|
-
return
|
|
495
|
+
return vendorFail("browserbase", `the Browserbase session ${id} is not handed over: ${unusable}`);
|
|
348
496
|
this.#urls.set(id, url);
|
|
349
497
|
const hello = browserHello(VERSION, this.capabilities().groups);
|
|
350
498
|
const client = browserClient(hello, (signal) => this.#alive(id, signal));
|
|
@@ -400,8 +548,7 @@ export class BrowserbaseProvider {
|
|
|
400
548
|
}
|
|
401
549
|
}
|
|
402
550
|
/**
|
|
403
|
-
* The provider, built from its options alone, the key included (options.apiKey,
|
|
404
|
-
* host's own transport); it reads no environment, and throws in words when the key is missing, never echoing one.
|
|
551
|
+
* The provider, built from its options alone, the key included (options.apiKey); it reads no environment, and throws in words when the key is missing, never echoing one.
|
|
405
552
|
*/
|
|
406
553
|
export function create(options) {
|
|
407
554
|
return new BrowserbaseProvider(options ?? {});
|
package/dist/chrome.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { type Result } from "./result.js";
|
|
2
|
-
/** The environment names a Chrome is found by:
|
|
2
|
+
/** The environment names a Chrome is found by: PI_BROWSER_CHROME, then the common CHROME_PATH. */
|
|
3
3
|
export declare const CHROME_ENV: readonly ["PI_BROWSER_CHROME", "CHROME_PATH"];
|
|
4
4
|
/**
|
|
5
5
|
* The Chrome or Chromium on this machine: the one PI_BROWSER_CHROME or CHROME_PATH names in `env`, else the first on
|
|
@@ -22,9 +22,8 @@ export declare const CHROME_FLAGS: readonly string[];
|
|
|
22
22
|
* each refused by the proxy after its lookup; no name or address resolved by Chrome itself but the proxy's, so a request
|
|
23
23
|
* a page sends around the proxy reaches nothing; no QUIC, which goes around an HTTP proxy; and WebRTC on no UDP but the
|
|
24
24
|
* proxy's, which carries none, so no ICE candidate is gathered and no STUN packet leaves. That last is
|
|
25
|
-
* `--webrtc-ip-handling-policy`: Chrome 155 and 156 ignore the `--force-` spelling (
|
|
26
|
-
*
|
|
27
|
-
* Stagehand's extension dialled: nothing in this browser reaches its own DevTools socket.
|
|
25
|
+
* `--webrtc-ip-handling-policy`: Chrome 155 and 156 ignore the `--force-` spelling (a STUN server on loopback
|
|
26
|
+
* was sent binding requests under it). Nothing in this browser reaches its own DevTools socket.
|
|
28
27
|
*/
|
|
29
28
|
export declare function chromeFence(proxy: string): string[];
|
|
30
29
|
/** Signal 0 only asks; EPERM means the process exists under another user. */
|
package/dist/chrome.js
CHANGED
|
@@ -1,11 +1,7 @@
|
|
|
1
|
-
// Launching a Chrome on this machine, the pieces the local browser (local.ts, local-supervisor.ts) is made of
|
|
2
|
-
//
|
|
3
|
-
// src/providers/cdp.ts alive, leadsOwnGroup, killTree, ownDebugger and singletonDir), so pi-browser can import them back
|
|
4
|
-
// once it depends on this package; and the fence and sandbox check of PR #1718's local Chrome for serve
|
|
5
|
-
// (packages/serve/src/local-chrome.ts chromeFence and checkChrome), less what was there for Stagehand's extension.
|
|
1
|
+
// Launching a Chrome on this machine, the pieces the local browser (local-chrome.ts, local-supervisor.ts) is made of: finding
|
|
2
|
+
// a Chrome, its process-group and singleton-lock handling, and the fence and sandbox check of a local Chrome.
|
|
6
3
|
//
|
|
7
|
-
// The library reads no environment:
|
|
8
|
-
// environment it is given.
|
|
4
|
+
// The library reads no environment: findChrome takes the environment it is given.
|
|
9
5
|
import { spawn } from "node:child_process";
|
|
10
6
|
import { constants, existsSync, readFileSync } from "node:fs";
|
|
11
7
|
import { access, mkdtemp, readFile, readlink, rm, stat } from "node:fs/promises";
|
|
@@ -23,7 +19,7 @@ const FIXED = [
|
|
|
23
19
|
"/Applications/Google Chrome.app/Contents/MacOS/Google Chrome",
|
|
24
20
|
"C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe",
|
|
25
21
|
];
|
|
26
|
-
/** The environment names a Chrome is found by:
|
|
22
|
+
/** The environment names a Chrome is found by: PI_BROWSER_CHROME, then the common CHROME_PATH. */
|
|
27
23
|
export const CHROME_ENV = ["PI_BROWSER_CHROME", "CHROME_PATH"];
|
|
28
24
|
/**
|
|
29
25
|
* The Chrome or Chromium on this machine: the one PI_BROWSER_CHROME or CHROME_PATH names in `env`, else the first on
|
|
@@ -71,9 +67,8 @@ export const CHROME_FLAGS = [
|
|
|
71
67
|
* each refused by the proxy after its lookup; no name or address resolved by Chrome itself but the proxy's, so a request
|
|
72
68
|
* a page sends around the proxy reaches nothing; no QUIC, which goes around an HTTP proxy; and WebRTC on no UDP but the
|
|
73
69
|
* proxy's, which carries none, so no ICE candidate is gathered and no STUN packet leaves. That last is
|
|
74
|
-
* `--webrtc-ip-handling-policy`: Chrome 155 and 156 ignore the `--force-` spelling (
|
|
75
|
-
*
|
|
76
|
-
* Stagehand's extension dialled: nothing in this browser reaches its own DevTools socket.
|
|
70
|
+
* `--webrtc-ip-handling-policy`: Chrome 155 and 156 ignore the `--force-` spelling (a STUN server on loopback
|
|
71
|
+
* was sent binding requests under it). Nothing in this browser reaches its own DevTools socket.
|
|
77
72
|
*/
|
|
78
73
|
export function chromeFence(proxy) {
|
|
79
74
|
const { hostname, port } = new URL(proxy);
|
|
Binary file
|
|
Binary file
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
// reads no environment: this module reads its own COMPUTER_BROWSERBASE_* names (never BROWSERBASE_API_KEY, which may
|
|
5
5
|
// hold another project's key) and passes each to create() as its option. The live switch is checked first, before any
|
|
6
6
|
// other setting is read. The run makes one session, shared by every case, and releases it.
|
|
7
|
-
import { BROWSER_IMAGE } from "../
|
|
7
|
+
import { BROWSER_IMAGE } from "../descriptors/shared.js";
|
|
8
8
|
import { create } from "../browserbase.js";
|
|
9
9
|
/** The environment this module reads, by name, each for the option its comment names, and the live switch. */
|
|
10
10
|
export const BROWSERBASE_ENV = {
|