@parcha/agentrun-computer 0.0.0-stage → 0.1.0-beta.32

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.
Files changed (173) hide show
  1. package/LICENSE +202 -0
  2. package/NOTICE +8 -0
  3. package/README.md +2401 -2
  4. package/box/Dockerfile +69 -0
  5. package/box/agentrun-archil +168 -0
  6. package/box/build-daytona.py +26 -0
  7. package/box/build-e2b.mjs +32 -0
  8. package/box/build-modal.py +17 -0
  9. package/dist/actions.d.ts +18 -0
  10. package/dist/actions.js +139 -0
  11. package/dist/browser.d.ts +151 -0
  12. package/dist/browser.js +305 -0
  13. package/dist/browserbase.d.ts +139 -0
  14. package/dist/browserbase.js +408 -0
  15. package/dist/child.d.ts +40 -0
  16. package/dist/child.js +127 -0
  17. package/dist/client.d.ts +22 -0
  18. package/dist/client.js +136 -0
  19. package/dist/computerd-linux-amd64 +0 -0
  20. package/dist/computerd-linux-arm64 +0 -0
  21. package/dist/conformance/archil.d.ts +68 -0
  22. package/dist/conformance/archil.js +152 -0
  23. package/dist/conformance/browserbase.d.ts +35 -0
  24. package/dist/conformance/browserbase.js +45 -0
  25. package/dist/conformance/cases/apps.d.ts +2 -0
  26. package/dist/conformance/cases/apps.js +155 -0
  27. package/dist/conformance/cases/browser.d.ts +4 -0
  28. package/dist/conformance/cases/browser.js +105 -0
  29. package/dist/conformance/cases/drift.d.ts +2 -0
  30. package/dist/conformance/cases/drift.js +64 -0
  31. package/dist/conformance/cases/exec.d.ts +2 -0
  32. package/dist/conformance/cases/exec.js +125 -0
  33. package/dist/conformance/cases/files.d.ts +2 -0
  34. package/dist/conformance/cases/files.js +294 -0
  35. package/dist/conformance/cases/health.d.ts +2 -0
  36. package/dist/conformance/cases/health.js +71 -0
  37. package/dist/conformance/cases/index.d.ts +9 -0
  38. package/dist/conformance/cases/index.js +37 -0
  39. package/dist/conformance/cases/input.d.ts +12 -0
  40. package/dist/conformance/cases/input.js +109 -0
  41. package/dist/conformance/cases/isolation.d.ts +21 -0
  42. package/dist/conformance/cases/isolation.js +92 -0
  43. package/dist/conformance/cases/lifetime.d.ts +2 -0
  44. package/dist/conformance/cases/lifetime.js +64 -0
  45. package/dist/conformance/cases/mount.d.ts +2 -0
  46. package/dist/conformance/cases/mount.js +252 -0
  47. package/dist/conformance/cases/process.d.ts +2 -0
  48. package/dist/conformance/cases/process.js +240 -0
  49. package/dist/conformance/cases/provider.d.ts +2 -0
  50. package/dist/conformance/cases/provider.js +302 -0
  51. package/dist/conformance/cases/screen.d.ts +9 -0
  52. package/dist/conformance/cases/screen.js +230 -0
  53. package/dist/conformance/cases/settings.d.ts +57 -0
  54. package/dist/conformance/cases/settings.js +142 -0
  55. package/dist/conformance/cases/touch.d.ts +2 -0
  56. package/dist/conformance/cases/touch.js +117 -0
  57. package/dist/conformance/cases/util.d.ts +7 -0
  58. package/dist/conformance/cases/util.js +8 -0
  59. package/dist/conformance/cdp.d.ts +13 -0
  60. package/dist/conformance/cdp.js +88 -0
  61. package/dist/conformance/cli.d.ts +29 -0
  62. package/dist/conformance/cli.js +231 -0
  63. package/dist/conformance/daytona-sweep.d.ts +18 -0
  64. package/dist/conformance/daytona-sweep.js +118 -0
  65. package/dist/conformance/daytona.d.ts +83 -0
  66. package/dist/conformance/daytona.js +107 -0
  67. package/dist/conformance/docker.d.ts +38 -0
  68. package/dist/conformance/docker.js +70 -0
  69. package/dist/conformance/drift-daemon.d.ts +8 -0
  70. package/dist/conformance/drift-daemon.js +41 -0
  71. package/dist/conformance/e2b-diagnose.d.ts +16 -0
  72. package/dist/conformance/e2b-diagnose.js +61 -0
  73. package/dist/conformance/e2b-sweep.d.ts +18 -0
  74. package/dist/conformance/e2b-sweep.js +125 -0
  75. package/dist/conformance/e2b.d.ts +74 -0
  76. package/dist/conformance/e2b.js +111 -0
  77. package/dist/conformance/fixture.d.ts +65 -0
  78. package/dist/conformance/fixture.js +98 -0
  79. package/dist/conformance/index.d.ts +8 -0
  80. package/dist/conformance/index.js +8 -0
  81. package/dist/conformance/kernel.d.ts +31 -0
  82. package/dist/conformance/kernel.js +40 -0
  83. package/dist/conformance/kind.d.ts +23 -0
  84. package/dist/conformance/kind.js +58 -0
  85. package/dist/conformance/modal-sweep.d.ts +22 -0
  86. package/dist/conformance/modal-sweep.js +190 -0
  87. package/dist/conformance/modal.d.ts +69 -0
  88. package/dist/conformance/modal.js +76 -0
  89. package/dist/conformance/orgo.d.ts +41 -0
  90. package/dist/conformance/orgo.js +69 -0
  91. package/dist/conformance/phone-harness.d.ts +61 -0
  92. package/dist/conformance/phone-harness.js +241 -0
  93. package/dist/conformance/png.d.ts +16 -0
  94. package/dist/conformance/png.js +132 -0
  95. package/dist/conformance/report.d.ts +2 -0
  96. package/dist/conformance/report.js +86 -0
  97. package/dist/conformance/runner.d.ts +11 -0
  98. package/dist/conformance/runner.js +585 -0
  99. package/dist/conformance/tar.d.ts +13 -0
  100. package/dist/conformance/tar.js +105 -0
  101. package/dist/conformance/types.d.ts +257 -0
  102. package/dist/conformance/types.js +24 -0
  103. package/dist/daemon-copy.d.ts +40 -0
  104. package/dist/daemon-copy.js +259 -0
  105. package/dist/daytona.d.ts +178 -0
  106. package/dist/daytona.js +1484 -0
  107. package/dist/desktop.d.ts +24 -0
  108. package/dist/desktop.js +68 -0
  109. package/dist/docker/egress-main.d.ts +1 -0
  110. package/dist/docker/egress-main.js +68 -0
  111. package/dist/docker/egress-proxy.d.ts +53 -0
  112. package/dist/docker/egress-proxy.js +450 -0
  113. package/dist/docker/egress-table.d.ts +39 -0
  114. package/dist/docker/egress-table.js +197 -0
  115. package/dist/docker/egress.d.ts +109 -0
  116. package/dist/docker/egress.js +318 -0
  117. package/dist/docker/image.d.ts +49 -0
  118. package/dist/docker/image.js +302 -0
  119. package/dist/docker/stage.d.ts +21 -0
  120. package/dist/docker/stage.js +290 -0
  121. package/dist/docker/volume.d.ts +24 -0
  122. package/dist/docker/volume.js +67 -0
  123. package/dist/docker.d.ts +150 -0
  124. package/dist/docker.js +1200 -0
  125. package/dist/drift.d.ts +200 -0
  126. package/dist/drift.js +416 -0
  127. package/dist/e2b.d.ts +335 -0
  128. package/dist/e2b.js +1515 -0
  129. package/dist/entry.d.ts +2 -0
  130. package/dist/entry.js +24 -0
  131. package/dist/files.d.ts +50 -0
  132. package/dist/files.js +208 -0
  133. package/dist/index.d.ts +27 -0
  134. package/dist/index.js +26 -0
  135. package/dist/isolation.d.ts +29 -0
  136. package/dist/isolation.js +71 -0
  137. package/dist/janitor.d.ts +50 -0
  138. package/dist/janitor.js +135 -0
  139. package/dist/kernel.d.ts +113 -0
  140. package/dist/kernel.js +358 -0
  141. package/dist/kinds.d.ts +79 -0
  142. package/dist/kinds.js +133 -0
  143. package/dist/labels.d.ts +23 -0
  144. package/dist/labels.js +50 -0
  145. package/dist/modal.d.ts +302 -0
  146. package/dist/modal.js +1374 -0
  147. package/dist/mount-archil.d.ts +164 -0
  148. package/dist/mount-archil.js +471 -0
  149. package/dist/mount.d.ts +57 -0
  150. package/dist/mount.js +104 -0
  151. package/dist/orgo-channel.d.ts +96 -0
  152. package/dist/orgo-channel.js +391 -0
  153. package/dist/orgo.d.ts +139 -0
  154. package/dist/orgo.js +1006 -0
  155. package/dist/phone-harness.d.ts +185 -0
  156. package/dist/phone-harness.js +1420 -0
  157. package/dist/processes.d.ts +24 -0
  158. package/dist/processes.js +40 -0
  159. package/dist/protocol.d.ts +729 -0
  160. package/dist/protocol.js +54 -0
  161. package/dist/provider.d.ts +245 -0
  162. package/dist/provider.js +37 -0
  163. package/dist/providers.d.ts +68 -0
  164. package/dist/providers.js +25 -0
  165. package/dist/result.d.ts +15 -0
  166. package/dist/result.js +2 -0
  167. package/dist/screen-watch.d.ts +150 -0
  168. package/dist/screen-watch.js +490 -0
  169. package/dist/test-connection.d.ts +17 -0
  170. package/dist/test-connection.js +26 -0
  171. package/dist/wait.d.ts +23 -0
  172. package/dist/wait.js +65 -0
  173. package/package.json +62 -4
@@ -0,0 +1,305 @@
1
+ // What every browser provider shares: a browser is a vendor's browser session that answers CDP (kind browser, daemon
2
+ // none). The provider answers hello and ping itself, from the vendor's API; connect hands over the vendor's signed CDP
3
+ // URL, which a driver dials. That URL and the live view's are secrets: they live in memory (a UrlStore), and never reach
4
+ // a ref, a list, an error's words or a report.
5
+ import { randomBytes } from "node:crypto";
6
+ import { checkLifetime } from "./provider.js";
7
+ import { fail, ok } from "./result.js";
8
+ /** The one image a browser computer runs: the vendor's own Chromium. */
9
+ export const BROWSER_IMAGE = "chromium";
10
+ /** A key's or a value's longest, in characters: what both vendors keep of a metadata entry. */
11
+ const MAX_LABEL = 256;
12
+ export const memoryUrls = () => {
13
+ const urls = new Map();
14
+ return { get: (id) => urls.get(id), set: (id, url) => void urls.set(id, url), delete: (id) => void urls.delete(id) };
15
+ };
16
+ export const DEFAULT_CONFIRM = { polls: 6, intervalMs: 500 };
17
+ export const defaultSleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
18
+ /** A failure whose cause rides along out of sight: not enumerable, so it is in no JSON, no log line and no report. */
19
+ export function vendorFail(code, message, cause) {
20
+ const failed = fail(code, message);
21
+ if (cause !== undefined && !failed.ok)
22
+ Object.defineProperty(failed.error, "cause", { value: cause, enumerable: false });
23
+ return failed;
24
+ }
25
+ /** The error a vendor call threw, behind a failure vendorFail made; undefined for any other. */
26
+ export const causeOf = (result) => (result.ok ? undefined : result.error.cause);
27
+ /** The code a vendor's HTTP status reads as. */
28
+ export function statusCode(status, vendor) {
29
+ if (status === 401 || status === 403)
30
+ return "denied";
31
+ if (status === 404)
32
+ return "not_found";
33
+ if (status === 429)
34
+ return "limit";
35
+ return vendor;
36
+ }
37
+ /** Why labels are not ones a browser provider writes into a vendor's metadata, in words; undefined when they are. */
38
+ export function labelsProblem(labels) {
39
+ if (typeof labels !== "object" || labels === null || Array.isArray(labels))
40
+ return "labels must map names to strings";
41
+ for (const [key, value] of Object.entries(labels)) {
42
+ if (key === "" || key.length > MAX_LABEL || /[\0-\x1f'\\]/.test(key))
43
+ return `a label's name is 1 to ${MAX_LABEL} characters with no quote, backslash or control character, not ${JSON.stringify(key)}`;
44
+ if (typeof value !== "string")
45
+ return `the label ${key} must be a string`;
46
+ if (value.length > MAX_LABEL || value.includes("\0"))
47
+ return `the label ${key} is at most ${MAX_LABEL} characters with no NUL byte`;
48
+ }
49
+ return undefined;
50
+ }
51
+ /** A label a vendor's query can carry as it is: plain characters, so a quote never ends the query early. */
52
+ export const queryable = ([key, value]) => /^[A-Za-z0-9_.:-]+$/.test(key) && /^[A-Za-z0-9_.:-]+$/.test(value);
53
+ /** Whether a session's metadata carries every one of these labels. */
54
+ export const carries = (held, labels) => typeof held === "object" && held !== null && Object.entries(labels).every(([key, value]) => held[key] === value);
55
+ /** The labels a vendor's metadata holds, its string entries only. */
56
+ export const labelsOf = (held) => typeof held === "object" && held !== null ? Object.fromEntries(Object.entries(held).filter((entry) => typeof entry[1] === "string")) : {};
57
+ /**
58
+ * The label every create writes beside the spec's, a value of its own: a create whose reply is lost, or whose caller
59
+ * cancels it, finds the browser it made by this label alone and releases it. The provider's own: check() refuses it in a
60
+ * spec, and no list or attach shows it.
61
+ */
62
+ export const CREATE_LABEL = "agentrun.create";
63
+ /** Labels without the provider's create label. */
64
+ export const withoutCreateLabel = (labels) => {
65
+ const { [CREATE_LABEL]: _, ...rest } = labels;
66
+ return rest;
67
+ };
68
+ /** A create label's value: 16 hex digits, fresh per create. */
69
+ export const createLabelValue = () => randomBytes(8).toString("hex");
70
+ /**
71
+ * Whether a create that threw may still have made a browser: anything but the vendor's own 4xx answer (a refusal, which
72
+ * makes nothing). A request not answered, aborted, cut short, or answered 5xx or with a body that is no browser may
73
+ * have been admitted.
74
+ */
75
+ export function createUncertain(error) {
76
+ const status = error?.status;
77
+ return !(typeof status === "number" && status >= 400 && status < 500);
78
+ }
79
+ /** How long a lost create's browser is looked for, in all, and how often: a vendor may list it a moment late. */
80
+ export const RECOVER_MS = 30_000;
81
+ const RECOVER_TRIES = 3;
82
+ const RECOVER_GAP_MS = 2_000;
83
+ /**
84
+ * After a create whose outcome is unknown: the browsers carrying its create label, found under a signal of the
85
+ * recovery's own (the caller's may be what ended the create), each released. The words say what became of them, for the
86
+ * create's failure; they never carry a URL.
87
+ */
88
+ export async function recoverCreate(what, find, release, sleep) {
89
+ const signal = AbortSignal.timeout(RECOVER_MS);
90
+ let found = ok([]);
91
+ for (let tries = 0; tries < RECOVER_TRIES; tries++) {
92
+ if (tries > 0)
93
+ await sleep(RECOVER_GAP_MS);
94
+ found = await find(signal);
95
+ if (!found.ok || found.value.length > 0 || signal.aborted)
96
+ break;
97
+ }
98
+ if (!found.ok)
99
+ return `the ${what} it may have made could not be looked for (${found.error.message}); its labels let a sweep find it`;
100
+ if (found.value.length === 0)
101
+ return `no ${what} carries its create label, so none was made or the vendor does not list it yet; its labels let a sweep find it`;
102
+ const words = [];
103
+ for (const id of found.value) {
104
+ const released = await release(id);
105
+ words.push(released.ok ? `the ${what} it made (${id}) was released` : `the ${what} it made (${id}) was not released (${released.error.code}), and its labels let a sweep find it`);
106
+ }
107
+ return words.join("; ");
108
+ }
109
+ const SPEC_FIELDS = new Set(["image", "lifetimeMs", "network", "labels"]);
110
+ /**
111
+ * The checks every browser provider makes before a vendor call: the image is chromium, the lifetime is within the
112
+ * vendor's cap (and at least min), the network is open, the labels are ones the vendor's metadata holds, and no field a
113
+ * browser does not take (a size, a user, a workdir, a mount).
114
+ */
115
+ export function checkBrowser(spec, capabilities, vendor, minLifetimeMs = 1) {
116
+ const refusals = [];
117
+ const given = (typeof spec === "object" && spec !== null ? spec : {});
118
+ const { image, lifetimeMs, network, labels } = given;
119
+ if (image !== BROWSER_IMAGE)
120
+ refusals.push({ field: "image", message: `a ${vendor} computer runs ${vendor}'s own Chromium: image is "${BROWSER_IMAGE}", not ${JSON.stringify(image)}` });
121
+ const lifetime = checkLifetime(lifetimeMs, capabilities);
122
+ refusals.push(...lifetime);
123
+ if (lifetime.length === 0 && lifetimeMs < minLifetimeMs) {
124
+ refusals.push({ field: "lifetimeMs", message: `lifetimeMs is at least ${minLifetimeMs} ms, ${vendor}'s shortest` });
125
+ }
126
+ if (network !== "open")
127
+ refusals.push({ field: "network", message: `a ${vendor} browser reaches the open web: network is "open", not ${JSON.stringify(network)}` });
128
+ if (labels !== undefined) {
129
+ const problem = labelsProblem(labels);
130
+ if (problem)
131
+ refusals.push({ field: "labels", message: problem });
132
+ else if (Object.hasOwn(labels, CREATE_LABEL))
133
+ refusals.push({ field: "labels", message: `the label ${CREATE_LABEL} is the provider's own: each create writes it, so a lost create finds its browser` });
134
+ }
135
+ for (const [field, value] of Object.entries(given)) {
136
+ if (!SPEC_FIELDS.has(field) && value !== undefined)
137
+ refusals.push({ field, message: `a ${vendor} browser does not take ${field}` });
138
+ }
139
+ return refusals;
140
+ }
141
+ /** hello for a browser: the provider's version, linux, kind browser and its groups. The vendor reports no arch. */
142
+ export const browserHello = (version, groups) => ({ version, os: "linux", kind: "browser", arch: "unknown", groups: [...groups] });
143
+ /**
144
+ * The client of a browser's connection: it answers hello, and ping from the vendor's own read of the session (gone
145
+ * once it has ended); any other method is unsupported, in words, since a browser is driven over CDP.
146
+ */
147
+ export function browserClient(hello, alive) {
148
+ let closed = false;
149
+ return {
150
+ async call(method, _params, options = {}) {
151
+ if (closed)
152
+ return fail("gone", "the connection is closed");
153
+ if (method === "hello")
154
+ return ok(hello);
155
+ if (method === "ping") {
156
+ const now = await alive(options.signal);
157
+ if (!now.ok)
158
+ return now;
159
+ return now.value ? ok({}) : fail("gone", "the browser session has ended");
160
+ }
161
+ return fail("unsupported", `a browser computer answers hello and ping; ${method} is not one of its methods: drive it over CDP, at the URL connect gives`);
162
+ },
163
+ close() {
164
+ closed = true;
165
+ },
166
+ };
167
+ }
168
+ /**
169
+ * A release, confirmed: the request is sent twice at most, then the vendor's own read must say the session ended, polled
170
+ * up to confirm.polls times. Done means the vendor says it no longer runs, not that the request was sent; a session the
171
+ * vendor no longer knows ("gone") is released. A session still running is a failure carrying the request's own error.
172
+ */
173
+ export async function releaseConfirmed(o) {
174
+ let failure;
175
+ for (let attempt = 0; attempt < 2; attempt++) {
176
+ const asked = await o.request();
177
+ if (asked.ok) {
178
+ if (asked.value === "gone")
179
+ return ok(undefined);
180
+ failure = undefined;
181
+ break;
182
+ }
183
+ failure = asked;
184
+ }
185
+ for (let poll = 0;; poll++) {
186
+ const now = await o.running();
187
+ if (!now.ok)
188
+ return now;
189
+ if (!now.value)
190
+ return ok(undefined);
191
+ if (poll >= o.confirm.polls)
192
+ return failure ?? fail("timeout", o.stillRunning);
193
+ await o.sleep(o.confirm.intervalMs);
194
+ }
195
+ }
196
+ /**
197
+ * A vendor call that failed: the call (a method and a route template, never an id or a URL), the HTTP status when one
198
+ * came back, and the vendor's error code when it is a plain identifier. The vendor's own words are never copied: an echo
199
+ * of the request could carry a key.
200
+ */
201
+ export class BrowserVendorError extends Error {
202
+ status;
203
+ code;
204
+ constructor(vendor, call, status, code, options) {
205
+ super(status === undefined ? `${vendor} ${call} could not be reached` : `${vendor} ${call} answered ${status}${code ? ` (${code})` : ""}`, options);
206
+ this.name = "BrowserVendorError";
207
+ this.status = status;
208
+ this.code = code;
209
+ }
210
+ }
211
+ /** A vendor's error code, kept only when it is a plain identifier. */
212
+ export const plainCode = (code) => (typeof code === "string" && /^[a-z][a-z_.-]{0,47}$/i.test(code) ? code : undefined);
213
+ const isAbort = (error) => error?.name === "AbortError";
214
+ /**
215
+ * A vendor call's thrown error as a result: aborted, or the code its status reads as, in words that name the call and the
216
+ * status alone. The error itself rides along as the failure's hidden cause (vendorFail).
217
+ */
218
+ export function vendorFailure(vendor, name, call, error) {
219
+ if (isAbort(error))
220
+ return vendorFail("aborted", `${name} ${call} was aborted`, error);
221
+ const status = error?.status;
222
+ const code = typeof status === "number" ? statusCode(status, vendor) : vendor;
223
+ const words = error instanceof BrowserVendorError ? error.message : typeof status === "number" ? `${name} ${call} answered ${status}` : `${name} ${call} failed`;
224
+ return vendorFail(code, words, error);
225
+ }
226
+ /**
227
+ * Why a vendor's CDP URL is not one to hand a driver, in words that never carry it; undefined when it is. It must be wss:
228
+ * (the session's signing key rides it) on the vendor's own domain or a host under it: a vendor answer that named another
229
+ * host would send the driver, and the key, there. The REST call that gave it was https to the vendor's pinned API with
230
+ * redirects refused; this holds the CDP URL itself to the same.
231
+ */
232
+ export function cdpUrlProblem(url, name, domain) {
233
+ const wanted = `wss: on ${domain}`;
234
+ if (typeof url !== "string" || url === "")
235
+ return `${name} gave no CDP URL (one is ${wanted})`;
236
+ let parsed;
237
+ try {
238
+ parsed = new URL(url);
239
+ }
240
+ catch {
241
+ return `${name}'s CDP URL is not a URL (one is ${wanted})`;
242
+ }
243
+ if (parsed.protocol !== "wss:")
244
+ return `${name}'s CDP URL is ${parsed.protocol} (one is ${wanted})`;
245
+ const host = parsed.hostname.toLowerCase();
246
+ if (host !== domain && !host.endsWith(`.${domain}`))
247
+ return `${name}'s CDP URL is on another host (one is ${wanted})`;
248
+ return undefined;
249
+ }
250
+ export async function restCall(o, method, route, call, init = {}) {
251
+ const query = init.query?.toString();
252
+ let response;
253
+ try {
254
+ response = await o.fetch(`${o.base}${route}${query ? `?${query}` : ""}`, {
255
+ method,
256
+ headers: { ...o.headers, Accept: "application/json", ...(init.body === undefined ? {} : { "Content-Type": "application/json" }) },
257
+ ...(init.body === undefined ? {} : { body: JSON.stringify(init.body) }),
258
+ ...(init.signal ? { signal: init.signal } : {}),
259
+ redirect: "error",
260
+ });
261
+ }
262
+ catch (error) {
263
+ if (isAbort(error))
264
+ throw error;
265
+ throw new BrowserVendorError(o.vendor, call, undefined, undefined, { cause: error });
266
+ }
267
+ // A success is read whole: a body cut short or not JSON is a failure, never an empty answer (an empty list would let
268
+ // a release or a sweep trust that nothing is left).
269
+ let text = "";
270
+ try {
271
+ text = await response.text();
272
+ }
273
+ catch (error) {
274
+ if (isAbort(error))
275
+ throw error;
276
+ if (response.ok)
277
+ throw new BrowserVendorError(o.vendor, call, response.status, "unreadable_body", { cause: error });
278
+ }
279
+ let json = null;
280
+ try {
281
+ json = text ? JSON.parse(text) : null;
282
+ }
283
+ catch {
284
+ if (response.ok)
285
+ throw new BrowserVendorError(o.vendor, call, response.status, "not_json");
286
+ }
287
+ if (!response.ok && response.status !== 404)
288
+ throw new BrowserVendorError(o.vendor, call, response.status, plainCode(json?.code));
289
+ return { status: response.status, json, headers: response.headers };
290
+ }
291
+ /** An https base URL, with no trailing slash, so a key never crosses in the clear; throws in words on another. */
292
+ export function httpsBase(url, what) {
293
+ let parsed;
294
+ try {
295
+ parsed = new URL(url);
296
+ }
297
+ catch {
298
+ throw new Error(`${what} is not a URL: ${JSON.stringify(url)}`);
299
+ }
300
+ if (parsed.protocol !== "https:")
301
+ throw new Error(`${what} must be https, so the key never crosses in the clear: ${parsed.protocol}`);
302
+ return url.replace(/\/+$/, "");
303
+ }
304
+ /** A vendor's session id as a ref or attach gives it: a plain token, never a path. */
305
+ export const idProblem = (id, vendor) => typeof id === "string" && /^[A-Za-z0-9_.:-]{1,128}$/.test(id) ? undefined : `${typeof id === "string" ? JSON.stringify(id) : String(id)} is not a ${vendor} session's id`;
@@ -0,0 +1,139 @@
1
+ import { type BrowserStream, type ReleaseConfirm, type UrlStore } from "./browser.js";
2
+ import type { KindCapabilities } from "./kinds.js";
3
+ import type { MountKind } from "./mount.js";
4
+ import type { AttachedComputer, CallContext, ComputerCapabilities, ComputerConnection, ComputerInfo, ComputerProvider, ComputerRef, ComputerSpec, CreatedComputer, NetworkPolicy, NetworkSpec, Refusal } from "./provider.js";
5
+ import { type Result } from "./result.js";
6
+ export declare const BROWSERBASE_API_URL = "https://api.browserbase.com";
7
+ /** The domain a Browserbase CDP URL is on (wss://connect.browserbase.com, read live 11-08): cdpUrlProblem holds it there. */
8
+ export declare const BROWSERBASE_CDP_DOMAIN = "browserbase.com";
9
+ /** Browserbase's longest session: a timeout of 21,600 s. */
10
+ export declare const BROWSERBASE_MAX_LIFETIME_MS: number;
11
+ /** Browserbase's shortest session timeout: 60 s. */
12
+ export declare const BROWSERBASE_MIN_LIFETIME_MS = 60000;
13
+ /** The userMetadata key a label key is written under: each "." as "_", since Browserbase refuses a dot there. */
14
+ export declare const metadataKey: (labelKey: string) => string;
15
+ /** The label key a userMetadata key reads back as: each "_" as ".". */
16
+ export declare const labelKey: (metadataKey: string) => string;
17
+ /** Why labels cannot ride a Browserbase session's userMetadata, in words naming the label; undefined when they can. */
18
+ export declare function browserbaseLabelsProblem(labels: unknown): string | undefined;
19
+ /** A browser takes no mount. */
20
+ export declare const MOUNT: readonly MountKind[];
21
+ /** The one configuration it offers, the kind registry's entry (kinds.ts): a browser with a CDP target and a live view. */
22
+ export declare const CAPABILITIES: readonly KindCapabilities[];
23
+ /** A session as Browserbase answers it; only what the provider reads. */
24
+ export interface BrowserbaseSession {
25
+ id: string;
26
+ status?: string;
27
+ /** The signed CDP URL: a secret. */
28
+ connectUrl?: string;
29
+ userMetadata?: Record<string, unknown>;
30
+ expiresAt?: string;
31
+ }
32
+ /** A session's live view as GET /v1/sessions/{id}/debug answers it: every URL in it is a secret. */
33
+ export interface BrowserbaseDebug {
34
+ debuggerFullscreenUrl?: string;
35
+ debuggerUrl?: string;
36
+ pages?: Array<{
37
+ id?: string;
38
+ url?: string;
39
+ title?: string;
40
+ debuggerFullscreenUrl?: string;
41
+ }>;
42
+ }
43
+ /**
44
+ * The calls the provider makes, one Browserbase request each, never retried where a retry could open a second paid
45
+ * browser. A failure throws, with the HTTP status as `status` when one came back; the provider returns it as a result
46
+ * whose hidden cause is the thrown error, so a host that gave its own api rethrows its own error.
47
+ */
48
+ export interface BrowserbaseApi {
49
+ create(body: Record<string, unknown>, signal?: AbortSignal): Promise<BrowserbaseSession>;
50
+ /** undefined when Browserbase does not know the session (404). */
51
+ retrieve(id: string, signal?: AbortSignal): Promise<BrowserbaseSession | undefined>;
52
+ list(query: {
53
+ status: "RUNNING" | "PENDING";
54
+ q?: string;
55
+ }, signal?: AbortSignal): Promise<BrowserbaseSession[]>;
56
+ /** "gone" when Browserbase does not know the session (404). */
57
+ release(id: string, signal?: AbortSignal): Promise<"gone" | "asked">;
58
+ debug(id: string, signal?: AbortSignal): Promise<BrowserbaseDebug>;
59
+ }
60
+ /** Browserbase's v1 REST API over fetch: the default api. */
61
+ export declare function browserbaseRest({ apiKey, apiUrl, projectId, fetch: fetcher }: {
62
+ apiKey: string;
63
+ apiUrl?: string;
64
+ projectId?: string;
65
+ fetch?: typeof fetch;
66
+ }): BrowserbaseApi;
67
+ export interface BrowserbaseProviderOptions {
68
+ /** With apiUrl and fetch, the REST api is built from it; required unless api is given. */
69
+ apiKey?: string;
70
+ /** Named in every create and release; a key behind a credential proxy names none. */
71
+ projectId?: string;
72
+ apiUrl?: string;
73
+ fetch?: typeof fetch;
74
+ /** The calls, a host's own (pi-browser's SDK client); default the REST api. */
75
+ api?: BrowserbaseApi;
76
+ /** Where the signed connect URLs are held; default a Map of this provider's own. */
77
+ urls?: UrlStore;
78
+ confirm?: ReleaseConfirm;
79
+ sleep?: (ms: number) => Promise<void>;
80
+ }
81
+ /** What a host may add to a session's create body: Browserbase's own settings (proxies, browserSettings, region, extensionId, a context). */
82
+ export type BrowserbaseSessionSettings = Record<string, unknown>;
83
+ /** Computers that are Browserbase sessions. */
84
+ export declare class BrowserbaseProvider implements ComputerProvider {
85
+ #private;
86
+ readonly id = "browserbase";
87
+ readonly version: string;
88
+ constructor({ apiKey, projectId, apiUrl, fetch: fetcher, api, urls, confirm, sleep }: BrowserbaseProviderOptions);
89
+ /**
90
+ * A browser: Browserbase ends a session at its timeout (lifetime vendor), at most 6 h; it reaches the open web; each
91
+ * session is its own VM. Browserbase keeps a session's downloads, but the protocol has no method that reads them yet,
92
+ * so downloads is not declared.
93
+ */
94
+ capabilities(): ComputerCapabilities;
95
+ check(spec: ComputerSpec): Refusal[];
96
+ /**
97
+ * Makes a session at most once: with keepAlive, so a dropped CDP client never ends it, the lifetime as its timeout in
98
+ * whole seconds, and the labels as its userMetadata. settings are Browserbase's own create fields a host adds (a
99
+ * proxy, a viewport, an extension); what the protocol sets wins over them.
100
+ */
101
+ create(spec: ComputerSpec, { signal, settings }?: {
102
+ signal?: AbortSignal;
103
+ settings?: BrowserbaseSessionSettings;
104
+ }): Promise<Result<CreatedComputer>>;
105
+ /**
106
+ * Settings' test: one read of the project's running sessions (GET /v1/sessions?status=RUNNING), which makes nothing.
107
+ * A refused key is unauthorized and a Browserbase not reached unreachable; the words name the call, never the key.
108
+ */
109
+ testConnection({ signal }?: CallContext): Promise<Result<{
110
+ detail: string;
111
+ }>>;
112
+ attach(ref: {
113
+ id: string;
114
+ } | {
115
+ labels: Record<string, string>;
116
+ }, { signal }?: CallContext): Promise<Result<AttachedComputer>>;
117
+ list(labels: Record<string, string>, { signal }?: CallContext): Promise<Result<ComputerInfo[]>>;
118
+ /** Always open: a Browserbase browser reaches the open web, and a change is refused in words. */
119
+ network(ref: ComputerRef, policy?: NetworkSpec, { signal }?: CallContext): Promise<Result<NetworkPolicy>>;
120
+ /**
121
+ * The session's signed CDP URL, held since create or read from Browserbase, and a client that answers hello and ping.
122
+ * The session is read first, held URL or not: one that has ended, or that Browserbase no longer knows though its URL is
123
+ * held, is gone, never handed over. Dials nothing: the URL is the driver's to dial.
124
+ */
125
+ connect(ref: ComputerRef): Promise<Result<ComputerConnection>>;
126
+ /** Asks Browserbase to release the session and waits until it reads as ended; one already ended or unknown is released. */
127
+ release(ref: ComputerRef, how: "stop" | "delete", { signal }?: CallContext): Promise<Result<void>>;
128
+ /** The session's live view, which takes input: a view-only one is refused, never handed over as one. */
129
+ stream(ref: ComputerRef, options?: {
130
+ viewOnly?: boolean;
131
+ }, { signal }?: CallContext): Promise<Result<BrowserStream>>;
132
+ }
133
+ /**
134
+ * The provider, built from its options alone, the key included (options.apiKey, or options.api / options.request, a
135
+ * host's own transport); it reads no environment, and throws in words when the key is missing, never echoing one.
136
+ */
137
+ export declare function create(options: BrowserbaseProviderOptions): BrowserbaseProvider;
138
+ /** The conformance CLI's module for Browserbase: its live switch, its image and its notes. */
139
+ export declare const conformance: () => Promise<typeof import("./conformance/browserbase.js")>;