@parcha/pi-browser 0.1.0-beta.35 → 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 +12 -0
- package/README.md +17 -22
- package/dist/core/driving-guard.d.ts +1 -1
- package/dist/core/driving-guard.js +1 -1
- package/dist/core/host.d.ts +1 -12
- package/dist/core/images.d.ts +32 -4
- package/dist/core/images.js +81 -16
- package/dist/core/tools.js +4 -1
- package/dist/core/vault.d.ts +0 -10
- package/dist/core/vault.js +0 -13
- package/dist/driver/cdp.js +3 -3
- package/dist/driver/page-driver.js +7 -3
- package/dist/durable.d.ts +5 -0
- package/dist/durable.js +24 -5
- package/dist/index.d.ts +1 -0
- package/dist/index.js +3 -0
- 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 +4 -3
- package/dist/testing/backend.js +6 -5
- 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 +2 -15
- package/dist/providers/browserbase-net.d.ts +0 -12
- package/dist/providers/browserbase-net.js +0 -95
- package/dist/providers/browserbase.d.ts +0 -138
- package/dist/providers/browserbase.js +0 -294
- 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/providers/kernel.js
DELETED
|
@@ -1,291 +0,0 @@
|
|
|
1
|
-
// The key lives in this module's memory only, and the CDP URL (it carries Kernel's token) is held here, never in a ref.
|
|
2
|
-
// No error carries the key, the URL or Kernel's text: only the call, the status and the code.
|
|
3
|
-
import { createHash, randomUUID } from "node:crypto";
|
|
4
|
-
import { readFile } from "node:fs/promises";
|
|
5
|
-
import { isRefusal } from "../core/failures.js";
|
|
6
|
-
import { directFetch } from "./http.js";
|
|
7
|
-
import { claimWriter, freeWriter, isWriter, noteWriter } from "./profiles.js";
|
|
8
|
-
import { accountKey, assertTag, defaultSleep, DEFAULT_CONFIRM, extensionArchive, kernelHoldings, nonEmpty, releaseAndConfirm, tagValue, untilAborted, uploadOnce } from "./shared.js";
|
|
9
|
-
export const KERNEL_BASE_URL = "https://api.onkernel.com";
|
|
10
|
-
export const KERNEL_IDLE_MIN_S = 10;
|
|
11
|
-
export const KERNEL_IDLE_MAX_S = 259_200;
|
|
12
|
-
// Kernel ends a session after `timeout_seconds` with no CDP or live-view connection and sets no wall-clock limit, so
|
|
13
|
-
// the lifetime cap is its largest idle timeout.
|
|
14
|
-
const CAPS = { timeoutModel: "inactivity", maxLifetimeS: KERNEL_IDLE_MAX_S, survivesDisconnect: true, releaseIsAsync: false, profiles: true, extension: "uploaded-per-launch", settings: { proxies: false, verified: false, geolocation: false, region: false, context: false } };
|
|
15
|
-
/** The call and the HTTP status, plus Kernel's error code when it is a plain identifier. Kernel's message is never
|
|
16
|
-
* copied: an echo of the request could carry the key or a URL. */
|
|
17
|
-
export class KernelError extends Error {
|
|
18
|
-
status;
|
|
19
|
-
constructor(call, status, code, options) {
|
|
20
|
-
super(status === undefined ? `Kernel ${call} could not be reached` : `Kernel ${call} answered ${status}${code ? ` (${code})` : ""}`, options);
|
|
21
|
-
this.name = "KernelError";
|
|
22
|
-
this.status = status;
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
const CODE = /^[a-z][a-z_.-]{0,47}$/i;
|
|
26
|
-
export function kernelCreateBody(spec, options = {}) {
|
|
27
|
-
const tags = Object.fromEntries(Object.entries(spec.metadata).map(([k, v]) => [k, tagValue(v)]));
|
|
28
|
-
return {
|
|
29
|
-
headless: options.headless ?? false,
|
|
30
|
-
stealth: options.stealth ?? false,
|
|
31
|
-
// Explicit, never Kernel's default (60 s): a crashed host's browser stops costing money on its own while custody's
|
|
32
|
-
// release is pending.
|
|
33
|
-
timeout_seconds: Math.min(KERNEL_IDLE_MAX_S, Math.max(KERNEL_IDLE_MIN_S, Math.round(spec.idleTimeoutS) || KERNEL_IDLE_MIN_S)),
|
|
34
|
-
viewport: { width: spec.viewport.width, height: spec.viewport.height },
|
|
35
|
-
tags: { ...tags, agentrun_tag: spec.tag },
|
|
36
|
-
...(spec.profile ? { profile: { name: spec.profile.name, save_changes: spec.profile.save } } : {}),
|
|
37
|
-
...(options.extension ? { extensions: [options.extension] } : {}),
|
|
38
|
-
};
|
|
39
|
-
}
|
|
40
|
-
const { cdpUrls, liveUrls, uploads } = kernelHoldings;
|
|
41
|
-
export function kernelProvider(options = {}) {
|
|
42
|
-
const env = options.env ?? process.env;
|
|
43
|
-
const apiKey = nonEmpty(env.KERNEL_API_KEY);
|
|
44
|
-
const baseUrl = (nonEmpty(env.KERNEL_BASE_URL) || KERNEL_BASE_URL).replace(/\/+$/, "");
|
|
45
|
-
const account = accountKey(baseUrl, apiKey);
|
|
46
|
-
const held = (id) => `${account}|${id}`;
|
|
47
|
-
const sleep = options.sleep ?? defaultSleep;
|
|
48
|
-
const confirm = options.confirm ?? DEFAULT_CONFIRM;
|
|
49
|
-
let client = null;
|
|
50
|
-
const http = () => (client ??= options.fetch ? Promise.resolve(options.fetch) : directFetch());
|
|
51
|
-
/** One request, never retried: a create served twice would be a second paid browser. On failure only Kernel's `code`
|
|
52
|
-
* survives; a transport failure keeps its cause chain under a message that names the call alone. */
|
|
53
|
-
async function call(method, route, init = {}) {
|
|
54
|
-
if (!apiKey)
|
|
55
|
-
throw new Error("the kernel provider needs KERNEL_API_KEY in the env it is given; none is set");
|
|
56
|
-
const name = `${method} ${route.replace(/\/browsers\/[^/?]+/, "/browsers/{id}").replace(/\/extensions\/[^/?]+/, "/extensions/{name}").replace(/\/profiles\/[^/?]+/, "/profiles/{name}")}`;
|
|
57
|
-
const headers = { Authorization: `Bearer ${apiKey}`, Accept: "application/json" };
|
|
58
|
-
let body;
|
|
59
|
-
if (init.raw) {
|
|
60
|
-
headers["Content-Type"] = init.raw.type;
|
|
61
|
-
body = init.raw.body;
|
|
62
|
-
}
|
|
63
|
-
else if (init.body !== undefined) {
|
|
64
|
-
headers["Content-Type"] = "application/json";
|
|
65
|
-
body = JSON.stringify(init.body);
|
|
66
|
-
}
|
|
67
|
-
let response;
|
|
68
|
-
try {
|
|
69
|
-
const query = init.query?.toString();
|
|
70
|
-
response = await (await http())(`${baseUrl}${route}${query ? `?${query}` : ""}`, { method, headers, ...(body !== undefined ? { body } : {}), ...(init.signal ? { signal: init.signal } : {}) });
|
|
71
|
-
}
|
|
72
|
-
catch (error) {
|
|
73
|
-
if (error?.name === "AbortError")
|
|
74
|
-
throw error;
|
|
75
|
-
throw new KernelError(name, undefined, undefined, { cause: error });
|
|
76
|
-
}
|
|
77
|
-
// A success must be read whole: a body that breaks off or is not JSON is a failure, never an empty answer (an empty
|
|
78
|
-
// session list would let a release finish without deleting).
|
|
79
|
-
let text = "";
|
|
80
|
-
try {
|
|
81
|
-
text = await response.text();
|
|
82
|
-
}
|
|
83
|
-
catch (error) {
|
|
84
|
-
if (error?.name === "AbortError")
|
|
85
|
-
throw error;
|
|
86
|
-
if (response.ok)
|
|
87
|
-
throw new KernelError(name, response.status, "unreadable_body", { cause: error });
|
|
88
|
-
}
|
|
89
|
-
let json = null;
|
|
90
|
-
try {
|
|
91
|
-
json = text ? JSON.parse(text) : null;
|
|
92
|
-
}
|
|
93
|
-
catch {
|
|
94
|
-
if (response.ok)
|
|
95
|
-
throw new KernelError(name, response.status, "not_json");
|
|
96
|
-
}
|
|
97
|
-
if (!response.ok && response.status !== 404)
|
|
98
|
-
throw new KernelError(name, response.status, typeof json?.code === "string" && CODE.test(json.code) ? json.code : undefined);
|
|
99
|
-
return { status: response.status, json, headers: response.headers };
|
|
100
|
-
}
|
|
101
|
-
/** Null only when the whole list was read and none matches: a list cut short is a failure, since a missed copy means
|
|
102
|
-
* a refused upload or a duplicate. */
|
|
103
|
-
const storedWith = async (sha) => {
|
|
104
|
-
for (let offset = 0, pages = 0; pages < 100; pages += 1) {
|
|
105
|
-
const page = await call("GET", "/extensions", { query: new URLSearchParams({ limit: "100", offset: String(offset) }) });
|
|
106
|
-
if (!Array.isArray(page.json))
|
|
107
|
-
throw new KernelError("GET /extensions", page.status, "not_a_list");
|
|
108
|
-
const found = page.json.find((e) => e?.checksum === sha);
|
|
109
|
-
if (found)
|
|
110
|
-
return nonEmpty(found.name) ? { name: nonEmpty(found.name) } : { id: nonEmpty(found.id) };
|
|
111
|
-
if (page.headers.get("x-has-more") !== "true")
|
|
112
|
-
return null;
|
|
113
|
-
const next = Number(page.headers.get("x-next-offset"));
|
|
114
|
-
if (!Number.isInteger(next) || next <= offset)
|
|
115
|
-
throw new KernelError("GET /extensions", page.status, "no_next_offset");
|
|
116
|
-
offset = next;
|
|
117
|
-
}
|
|
118
|
-
throw new Error("Kernel kept answering more pages of stored extensions");
|
|
119
|
-
};
|
|
120
|
-
/** A stored extension with the same bytes serves whatever its name (a plan may store only one, perhaps another
|
|
121
|
-
* tool's); otherwise one upload per process and account, named by its SHA-256, and a failed one is forgotten. */
|
|
122
|
-
const extension = () => options.extension ? options.extension() : uploadOnce(uploads, account, async () => {
|
|
123
|
-
const zip = await readFile(extensionArchive());
|
|
124
|
-
const sha = createHash("sha256").update(zip).digest("hex");
|
|
125
|
-
const stored = await storedWith(sha);
|
|
126
|
-
if (stored)
|
|
127
|
-
return stored;
|
|
128
|
-
const name = `agentrun-stagehand-${sha.slice(0, 16)}`;
|
|
129
|
-
const boundary = `agentrun-${randomUUID()}`;
|
|
130
|
-
const part = (headers) => Buffer.from(`--${boundary}\r\n${headers}\r\n\r\n`);
|
|
131
|
-
const body = Buffer.concat([
|
|
132
|
-
part(`Content-Disposition: form-data; name="name"`), Buffer.from(`${name}\r\n`),
|
|
133
|
-
part(`Content-Disposition: form-data; name="file"; filename="stagehand-extension.zip"\r\nContent-Type: application/zip`), zip, Buffer.from(`\r\n--${boundary}--\r\n`),
|
|
134
|
-
]);
|
|
135
|
-
try {
|
|
136
|
-
const done = await call("POST", "/extensions", { raw: { body, type: `multipart/form-data; boundary=${boundary}` } });
|
|
137
|
-
if (done.status === 404)
|
|
138
|
-
throw new KernelError("POST /extensions", 404);
|
|
139
|
-
}
|
|
140
|
-
catch (error) {
|
|
141
|
-
// Another process stored the same archive meanwhile: that copy serves.
|
|
142
|
-
const raced = error.status === 409 ? await storedWith(sha) : null;
|
|
143
|
-
if (raced)
|
|
144
|
-
return raced;
|
|
145
|
-
throw error;
|
|
146
|
-
}
|
|
147
|
-
return { name };
|
|
148
|
-
});
|
|
149
|
-
const lookup = async (id, signal) => call("GET", `/browsers/${encodeURIComponent(id)}`, { signal });
|
|
150
|
-
const forget = (id) => { cdpUrls.delete(held(id)); liveUrls.delete(held(id)); };
|
|
151
|
-
const status = async (ref, signal) => {
|
|
152
|
-
const found = await lookup(ref.id, signal);
|
|
153
|
-
const state = found.status === 404 ? "gone" : found.json?.deleted_at ? "stopped" : "running";
|
|
154
|
-
if (state !== "running")
|
|
155
|
-
forget(ref.id);
|
|
156
|
-
return state;
|
|
157
|
-
};
|
|
158
|
-
const running = (ref) => async (signal) => { const now = await status(ref, signal); return now === "running" || now === "pending"; };
|
|
159
|
-
/** A browser can only name a profile Kernel knows. Creating it first is safe to race: a 409 means another process just did. */
|
|
160
|
-
const ensureProfile = async (name, signal) => {
|
|
161
|
-
if ((await call("GET", `/profiles/${encodeURIComponent(name)}`, { signal })).status !== 404)
|
|
162
|
-
return;
|
|
163
|
-
try {
|
|
164
|
-
await call("POST", "/profiles", { body: { name }, signal });
|
|
165
|
-
}
|
|
166
|
-
catch (error) {
|
|
167
|
-
if (error.status !== 409)
|
|
168
|
-
throw error;
|
|
169
|
-
}
|
|
170
|
-
};
|
|
171
|
-
/** The saved profile each live session of this provider runs on, by tag: what a session vault may keep. */
|
|
172
|
-
const profilesUsed = new Map();
|
|
173
|
-
return {
|
|
174
|
-
name: "kernel",
|
|
175
|
-
caps: CAPS,
|
|
176
|
-
vault: { kind: "context", provider: "kernel", contextIdsInClear: false, contextOf: (ref) => profilesUsed.get(ref.tag) ?? null },
|
|
177
|
-
async create(spec, signal) {
|
|
178
|
-
// The upload is shared by every create on the account: a cancelled caller stops waiting, the upload goes on for
|
|
179
|
-
// the rest.
|
|
180
|
-
signal?.throwIfAborted();
|
|
181
|
-
if (spec.profile)
|
|
182
|
-
profilesUsed.set(spec.tag, spec.profile.name);
|
|
183
|
-
if (spec.profile?.save)
|
|
184
|
-
await claimWriter(`${account}|profile:${spec.profile.name}`, spec.profile.name, spec.tag, `${spec.metadata.run}|${spec.metadata.label}`, signal);
|
|
185
|
-
let body;
|
|
186
|
-
try {
|
|
187
|
-
if (spec.profile)
|
|
188
|
-
await ensureProfile(spec.profile.name, signal);
|
|
189
|
-
body = kernelCreateBody(spec, { stealth: options.stealth, headless: options.headless, extension: await untilAborted(extension(), signal) });
|
|
190
|
-
}
|
|
191
|
-
catch (error) {
|
|
192
|
-
freeWriter(spec.tag);
|
|
193
|
-
throw error;
|
|
194
|
-
}
|
|
195
|
-
// At most once: a lost answer is found again by its tag (findByTag), never by a second create. A failed send keeps the
|
|
196
|
-
// profile's claim until findByTag has looked: the browser may exist. A refusal (401, 403) made none, so it frees the claim.
|
|
197
|
-
const created = await call("POST", "/browsers", { body, signal }).catch((error) => {
|
|
198
|
-
if (isRefusal(error))
|
|
199
|
-
freeWriter(spec.tag);
|
|
200
|
-
throw error;
|
|
201
|
-
});
|
|
202
|
-
const id = nonEmpty(created.json?.session_id);
|
|
203
|
-
const url = nonEmpty(created.json?.cdp_ws_url);
|
|
204
|
-
if (created.status === 404 || !id || !url)
|
|
205
|
-
throw new KernelError("POST /browsers", created.status, "no_session");
|
|
206
|
-
cdpUrls.set(held(id), url);
|
|
207
|
-
noteWriter(spec.tag, running({ id, tag: spec.tag }));
|
|
208
|
-
const live = nonEmpty(created.json?.browser_live_view_url);
|
|
209
|
-
if (live)
|
|
210
|
-
liveUrls.set(held(id), live);
|
|
211
|
-
return { id, tag: spec.tag };
|
|
212
|
-
},
|
|
213
|
-
async findByTag(tag, signal) {
|
|
214
|
-
assertTag(tag);
|
|
215
|
-
// The filter is Kernel's, the match is ours: a lane that ignores it must never hand back someone else's session.
|
|
216
|
-
// Every page must answer: custody releases exactly these ids, so a failed page throws.
|
|
217
|
-
const out = [];
|
|
218
|
-
const seen = new Set();
|
|
219
|
-
for (let offset = 0, pages = 0; pages < 100; pages += 1) {
|
|
220
|
-
const query = new URLSearchParams({ status: "active", [`tags[agentrun_tag]`]: tag, limit: "100", offset: String(offset) });
|
|
221
|
-
const page = await call("GET", "/browsers", { query, signal });
|
|
222
|
-
// A list is never "not found": a 404 here is a wrong base URL, and an empty answer would orphan the tag's
|
|
223
|
-
// sessions.
|
|
224
|
-
if (page.status === 404)
|
|
225
|
-
throw new KernelError("GET /browsers", 404);
|
|
226
|
-
if (!Array.isArray(page.json))
|
|
227
|
-
throw new KernelError("GET /browsers", page.status, "not_a_list");
|
|
228
|
-
const rows = page.json;
|
|
229
|
-
for (const row of rows) {
|
|
230
|
-
const id = nonEmpty(row?.session_id);
|
|
231
|
-
if (id && row?.tags?.agentrun_tag === tag && !row?.deleted_at && !seen.has(id)) {
|
|
232
|
-
seen.add(id);
|
|
233
|
-
out.push({ id, tag });
|
|
234
|
-
}
|
|
235
|
-
}
|
|
236
|
-
// Kernel pages by headers: X-Has-More, and X-Next-Offset as the next page's absolute start.
|
|
237
|
-
if (page.headers.get("x-has-more") !== "true") {
|
|
238
|
-
if (!out.length)
|
|
239
|
-
freeWriter(tag);
|
|
240
|
-
for (const ref of out)
|
|
241
|
-
if (isWriter(ref.tag))
|
|
242
|
-
noteWriter(ref.tag, running(ref));
|
|
243
|
-
return out;
|
|
244
|
-
}
|
|
245
|
-
// More pages with no usable next offset would truncate the set custody releases: a failure, never a partial
|
|
246
|
-
// answer.
|
|
247
|
-
const next = Number(page.headers.get("x-next-offset"));
|
|
248
|
-
if (!Number.isInteger(next) || next <= offset)
|
|
249
|
-
throw new Error("Kernel reported more sessions for the tag without the next page's offset");
|
|
250
|
-
offset = next;
|
|
251
|
-
}
|
|
252
|
-
throw new Error("Kernel kept answering more pages of sessions for one tag");
|
|
253
|
-
},
|
|
254
|
-
status,
|
|
255
|
-
async attach(ref, signal) {
|
|
256
|
-
let url = cdpUrls.get(held(ref.id));
|
|
257
|
-
if (!url) {
|
|
258
|
-
const found = await lookup(ref.id, signal);
|
|
259
|
-
url = found.status === 404 || found.json?.deleted_at ? "" : nonEmpty(found.json?.cdp_ws_url);
|
|
260
|
-
if (!url)
|
|
261
|
-
throw new Error("Kernel session is not available for connection");
|
|
262
|
-
cdpUrls.set(held(ref.id), url);
|
|
263
|
-
}
|
|
264
|
-
return { sdkCdpUrl: url };
|
|
265
|
-
},
|
|
266
|
-
async liveView(ref) {
|
|
267
|
-
try {
|
|
268
|
-
let url = liveUrls.get(held(ref.id));
|
|
269
|
-
if (!url) {
|
|
270
|
-
const found = await lookup(ref.id);
|
|
271
|
-
url = found.status === 404 || found.json?.deleted_at ? "" : nonEmpty(found.json?.browser_live_view_url);
|
|
272
|
-
}
|
|
273
|
-
return url ? { fullscreen: url, framed: null, pages: [] } : null;
|
|
274
|
-
}
|
|
275
|
-
catch {
|
|
276
|
-
return null;
|
|
277
|
-
}
|
|
278
|
-
},
|
|
279
|
-
async release(ref, signal) {
|
|
280
|
-
profilesUsed.delete(ref.tag);
|
|
281
|
-
await releaseAndConfirm({
|
|
282
|
-
request: async () => ((await call("DELETE", `/browsers/${encodeURIComponent(ref.id)}`, { signal })).status === 404 ? "gone" : undefined),
|
|
283
|
-
status: () => status(ref, signal),
|
|
284
|
-
forget: () => { forget(ref.id); freeWriter(ref.tag); },
|
|
285
|
-
confirm,
|
|
286
|
-
sleep,
|
|
287
|
-
stillRunning: `Kernel session ${ref.id} still running after its delete`,
|
|
288
|
-
});
|
|
289
|
-
},
|
|
290
|
-
};
|
|
291
|
-
}
|
|
File without changes
|
|
File without changes
|