@edlsh/browse-core 0.2.2 → 0.3.0
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 +1 -0
- package/dist/broker-protocol.d.ts +1 -1
- package/dist/browser-cookies.d.ts +28 -0
- package/dist/browser-cookies.js +34 -0
- package/dist/session.d.ts +6 -2
- package/dist/session.js +29 -22
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -15,6 +15,7 @@ const output = await browser.run(browserParameters.parse({ action: "start", back
|
|
|
15
15
|
|
|
16
16
|
- `HostIdentity.name` appears in user-facing messages ("Use the MyHost model…") and names persistent local Chrome profiles `<name> browse` before each launch, so the toolbar avatar shows which harness owns a window. `integration` tags Browserbase session metadata, names the HAR `creator`, and prefixes the default HAR temp directory.
|
|
17
17
|
- `nameChromeProfile(userDataDir, name)` sets that name on a profile the host launches itself; it leaves a profile Chrome holds untouched.
|
|
18
|
+
- `BrowseSessionOptions.cookies` takes a `CookieProvider` (`list`, `find`, `read`) that `cookie_profiles` and `cookie_import` read through; the default, `localCookieProvider`, reads this Mac's installed browsers. A host whose user's browsers are on another machine supplies its own (hermes-browse reads the MacBook's Helium through a cookie service). Imports with a custom provider go into Browserbase or private launches only, since shared browsers run in a broker that reads its own machine's browsers.
|
|
18
19
|
- `ClientInference` supplies Stagehand's default inference. Without it, unconfigured AI calls use Browserbase Model Gateway.
|
|
19
20
|
- `settings.ts` holds the per-profile `/browse` settings file (`browserbase-ai.json`): `readSettings`, `writeSetting`, and the model-role helpers.
|
|
20
21
|
|
|
@@ -98,3 +98,31 @@ export declare function readBrowserCookies(source: CookieSource, profile: string
|
|
|
98
98
|
readPassword?: (signal: AbortSignal) => Promise<string>;
|
|
99
99
|
cacheKey?: boolean;
|
|
100
100
|
}): Promise<BrowserCookies>;
|
|
101
|
+
/** One browser and its profiles, as cookie_profiles lists them. */
|
|
102
|
+
export interface CookieBrowser {
|
|
103
|
+
browser: BrowserId;
|
|
104
|
+
name: string;
|
|
105
|
+
profiles: {
|
|
106
|
+
profile: string;
|
|
107
|
+
name?: string;
|
|
108
|
+
}[];
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Where cookie_import and cookie_profiles read cookies. The default reads this Mac's browsers; a host whose browsers are
|
|
112
|
+
* elsewhere (Hermes reading the MacBook's Helium from Linux) supplies its own. Errors it throws reach the agent as written.
|
|
113
|
+
*/
|
|
114
|
+
export interface CookieProvider {
|
|
115
|
+
/** Browsers and profiles cookies can come from, optionally only one browser. */
|
|
116
|
+
list(browser: BrowserId | undefined, signal: AbortSignal): Promise<CookieBrowser[]>;
|
|
117
|
+
/** Profiles holding importable cookies for the domains, most first. */
|
|
118
|
+
find(domains: string[], browser: BrowserId | undefined, signal: AbortSignal): Promise<BrowserProfileMatch[]>;
|
|
119
|
+
/** The profile's unexpired cookies for the domains, decrypted. */
|
|
120
|
+
read(browser: BrowserId, profile: string, domains: string[], signal: AbortSignal): Promise<BrowserCookies>;
|
|
121
|
+
}
|
|
122
|
+
/** A cookie source refusal with a stable code, which the session reports as a tool error. */
|
|
123
|
+
export declare class CookieSourceError extends Error {
|
|
124
|
+
readonly code: string;
|
|
125
|
+
constructor(code: string, message: string);
|
|
126
|
+
}
|
|
127
|
+
/** This Mac's installed browsers; the Keychain key is unlocked once per process and kept in memory. */
|
|
128
|
+
export declare const localCookieProvider: CookieProvider;
|
package/dist/browser-cookies.js
CHANGED
|
@@ -259,3 +259,37 @@ export async function readBrowserCookies(source, profile, domains, signal, optio
|
|
|
259
259
|
key?.fill(0);
|
|
260
260
|
}
|
|
261
261
|
}
|
|
262
|
+
/** Installed supported browsers, optionally only the requested one. */
|
|
263
|
+
async function localSources(browser) {
|
|
264
|
+
const installed = await installedBrowsers();
|
|
265
|
+
const sources = browser ? installed.filter(source => source.browser === browser) : installed;
|
|
266
|
+
if (sources.length)
|
|
267
|
+
return sources;
|
|
268
|
+
const supported = BROWSERS.map(entry => entry.name).join(", ");
|
|
269
|
+
throw new CookieSourceError("browser_not_installed", browser
|
|
270
|
+
? `${BROWSERS.find(entry => entry.id === browser).name} is not installed in /Applications or ~/Applications. Call cookie_profiles to see the installed browsers.`
|
|
271
|
+
: `No supported browser is installed in /Applications or ~/Applications. Supported browsers: ${supported}.`);
|
|
272
|
+
}
|
|
273
|
+
/** A cookie source refusal with a stable code, which the session reports as a tool error. */
|
|
274
|
+
export class CookieSourceError extends Error {
|
|
275
|
+
code;
|
|
276
|
+
constructor(code, message) {
|
|
277
|
+
super(message);
|
|
278
|
+
this.code = code;
|
|
279
|
+
this.name = "CookieSourceError";
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
/** This Mac's installed browsers; the Keychain key is unlocked once per process and kept in memory. */
|
|
283
|
+
export const localCookieProvider = {
|
|
284
|
+
async list(browser) {
|
|
285
|
+
return Promise.all((await localSources(browser)).map(async (source) => ({ browser: source.browser, name: source.name,
|
|
286
|
+
profiles: (await listBrowserProfiles(source)).map(({ profile, name }) => name ? { profile, name } : { profile }) })));
|
|
287
|
+
},
|
|
288
|
+
async find(domains, browser, signal) {
|
|
289
|
+
return findBrowserProfiles(domains, signal, await localSources(browser));
|
|
290
|
+
},
|
|
291
|
+
async read(browser, profile, domains, signal) {
|
|
292
|
+
const [source] = await localSources(browser);
|
|
293
|
+
return readBrowserCookies(source, profile, domains, signal, { cacheKey: true });
|
|
294
|
+
},
|
|
295
|
+
};
|
package/dist/session.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { Stagehand } from "@browserbasehq/stagehand";
|
|
2
2
|
import type { AIParams, BrowserParams } from "./schema.ts";
|
|
3
|
+
import { type CookieProvider } from "./browser-cookies.ts";
|
|
3
4
|
import { type HostClassify } from "./classifier.ts";
|
|
4
5
|
export interface BrowseConfig {
|
|
5
6
|
apiKey?: string;
|
|
@@ -82,6 +83,8 @@ export interface BrowseSessionOptions {
|
|
|
82
83
|
* conversations in other processes can use them at the same time. Default true; the broker turns it off for itself.
|
|
83
84
|
*/
|
|
84
85
|
profileBroker?: boolean;
|
|
86
|
+
/** Where cookie_import and cookie_profiles read cookies; default, this Mac's installed browsers. */
|
|
87
|
+
cookies?: CookieProvider;
|
|
85
88
|
}
|
|
86
89
|
/** A page script that answered a native dialog, as reported in tool output. */
|
|
87
90
|
export interface AnsweredDialog {
|
|
@@ -148,6 +151,7 @@ export declare class BrowseSession {
|
|
|
148
151
|
/** Cleared for the process lifetime once Browserbase refuses keepAlive for this plan. */
|
|
149
152
|
private keepAliveAllowed;
|
|
150
153
|
private readonly profileBroker;
|
|
154
|
+
private readonly cookies;
|
|
151
155
|
/** This conversation's calls run in the broker that owns its shared browser, until close. */
|
|
152
156
|
private delegate?;
|
|
153
157
|
/** Stagehand's runtime extension, uploaded once per tool instance instead of on every launch. */
|
|
@@ -249,8 +253,8 @@ export declare class BrowseSession {
|
|
|
249
253
|
private reference;
|
|
250
254
|
private target;
|
|
251
255
|
private output;
|
|
252
|
-
/**
|
|
253
|
-
private
|
|
256
|
+
/** A cookie provider call, with its refusals as tool errors. */
|
|
257
|
+
private fromCookies;
|
|
254
258
|
/** The browser profile to import from: the one given, or the only one with importable cookies for the domains; several leave the choice to the agent's task context. */
|
|
255
259
|
private cookieProfile;
|
|
256
260
|
private browse;
|
package/dist/session.js
CHANGED
|
@@ -14,7 +14,7 @@ import { nameChromeProfile } from "./chrome-profile.js";
|
|
|
14
14
|
import { BrokerConnection } from "./broker-client.js";
|
|
15
15
|
import { randomUUID } from "node:crypto";
|
|
16
16
|
import { pageOperation, tabsOperation } from "./page-operations.js";
|
|
17
|
-
import {
|
|
17
|
+
import { CookieSourceError, localCookieProvider, normalizeCookieDomains } from "./browser-cookies.js";
|
|
18
18
|
import { fitsAction, JEV_MIN_CONFIDENCE, jevChooser, pickTarget, treeCandidates } from "./jev.js";
|
|
19
19
|
import { CLEF, classifierName, ClassifierRefusal, classifierSpec, takesImages, VERCEL_JEV, vercelClassifier, WORKERS_AI_JEV, workersAiClassifier } from "./classifier.js";
|
|
20
20
|
const LOOPBACK = new Set(["localhost", "127.0.0.1", "[::1]"]);
|
|
@@ -394,6 +394,7 @@ export class BrowseSession {
|
|
|
394
394
|
/** Cleared for the process lifetime once Browserbase refuses keepAlive for this plan. */
|
|
395
395
|
keepAliveAllowed = true;
|
|
396
396
|
profileBroker;
|
|
397
|
+
cookies;
|
|
397
398
|
/** This conversation's calls run in the broker that owns its shared browser, until close. */
|
|
398
399
|
delegate;
|
|
399
400
|
/** Stagehand's runtime extension, uploaded once per tool instance instead of on every launch. */
|
|
@@ -408,6 +409,7 @@ export class BrowseSession {
|
|
|
408
409
|
this.hostClassify = hostClassify;
|
|
409
410
|
this.identity = identity;
|
|
410
411
|
this.profileBroker = options.profileBroker ?? true;
|
|
412
|
+
this.cookies = options.cookies ?? localCookieProvider;
|
|
411
413
|
this.stagehandModel = inference ? { model: { generate: inference.generate } } : {};
|
|
412
414
|
this.describeInference = inference?.describe ?? (async () => "gateway-auto");
|
|
413
415
|
}
|
|
@@ -533,6 +535,10 @@ export class BrowseSession {
|
|
|
533
535
|
if (!this.delegate && !this.session && this.profileBroker && !ai && OPENS_BROWSER.has(params.action)) {
|
|
534
536
|
// SAFETY: only browse actions open a browser, and browse_ai calls were excluded above.
|
|
535
537
|
const key = await this.sharedKey(params, context);
|
|
538
|
+
// The broker reads cookies with its own machine's browsers, not a host's provider.
|
|
539
|
+
if (key && params.action === "cookie_import" && this.cookies !== localCookieProvider) {
|
|
540
|
+
throw new BrowseToolError("unsupported_backend", "This host's cookie source imports into Browserbase sessions only. Start with backend \"browserbase\", then cookie_import.");
|
|
541
|
+
}
|
|
536
542
|
if (key) {
|
|
537
543
|
const connection = await BrokerConnection.for(key);
|
|
538
544
|
const conv = randomUUID();
|
|
@@ -540,6 +546,9 @@ export class BrowseSession {
|
|
|
540
546
|
this.delegate = { conv, owner: this.owner(context), connection };
|
|
541
547
|
}
|
|
542
548
|
}
|
|
549
|
+
if (this.delegate && !ai && params.action === "cookie_import" && this.cookies !== localCookieProvider) {
|
|
550
|
+
throw new BrowseToolError("unsupported_backend", "This host's cookie source imports into Browserbase sessions only. Close, start with backend \"browserbase\", then cookie_import.");
|
|
551
|
+
}
|
|
543
552
|
if (this.delegate)
|
|
544
553
|
return this.forward(params, context, ai);
|
|
545
554
|
const attempt = () => ai ? this.ai(params, context) : this.browse(params, context);
|
|
@@ -1364,28 +1373,27 @@ export class BrowseSession {
|
|
|
1364
1373
|
...(data === undefined ? {} : { data }),
|
|
1365
1374
|
};
|
|
1366
1375
|
}
|
|
1367
|
-
/**
|
|
1368
|
-
async
|
|
1369
|
-
|
|
1370
|
-
|
|
1371
|
-
|
|
1372
|
-
|
|
1373
|
-
|
|
1374
|
-
|
|
1375
|
-
|
|
1376
|
-
|
|
1376
|
+
/** A cookie provider call, with its refusals as tool errors. */
|
|
1377
|
+
async fromCookies(read) {
|
|
1378
|
+
try {
|
|
1379
|
+
return await read();
|
|
1380
|
+
}
|
|
1381
|
+
catch (error) {
|
|
1382
|
+
if (error instanceof CookieSourceError)
|
|
1383
|
+
throw new BrowseToolError(error.code, error.message);
|
|
1384
|
+
throw error;
|
|
1385
|
+
}
|
|
1377
1386
|
}
|
|
1378
1387
|
/** The browser profile to import from: the one given, or the only one with importable cookies for the domains; several leave the choice to the agent's task context. */
|
|
1379
1388
|
async cookieProfile(p, signal) {
|
|
1380
|
-
const sources = await this.cookieSources(p.browser);
|
|
1381
1389
|
if (p.browser && p.profile)
|
|
1382
|
-
return {
|
|
1383
|
-
const matches = (await
|
|
1390
|
+
return { browser: p.browser, profile: p.profile };
|
|
1391
|
+
const matches = (await this.fromCookies(() => this.cookies.find(p.domains, p.browser, signal))).filter(match => !p.profile || match.profile === p.profile);
|
|
1384
1392
|
if (matches.length === 1)
|
|
1385
|
-
return {
|
|
1386
|
-
const names = sources.map(source => source.name).join(", ");
|
|
1393
|
+
return { browser: matches[0].browser, profile: matches[0].profile };
|
|
1387
1394
|
if (!matches.length) {
|
|
1388
|
-
|
|
1395
|
+
const names = (await this.fromCookies(() => this.cookies.list(p.browser, signal))).map(browser => browser.name).join(", ");
|
|
1396
|
+
throw new BrowseToolError("cookies_not_found", `No ${p.profile ? `${p.profile} profile` : "profile"} in the user's browsers (${names}) has unexpired cookies for these domains, so the user is not signed in there. Ask the user to sign in to the site in their browser for this task and tell you when done, then call cookie_import again and reload. Do not use live_view.`);
|
|
1389
1397
|
}
|
|
1390
1398
|
const listed = matches.map(match => `${match.browser} ${match.profile}${match.name ? ` (${match.name})` : ""}: ${match.cookies} cookies`).join("; ");
|
|
1391
1399
|
throw new BrowseToolError("cookie_profile_ambiguous", `Several browser profiles have cookies for these domains: ${listed}. Retry with browser and profile set to the one matching the task's account or organization; if the task does not say, ask the user which one.`);
|
|
@@ -1395,8 +1403,7 @@ export class BrowseSession {
|
|
|
1395
1403
|
const budget = operationTimeout(p, false);
|
|
1396
1404
|
const pageTimeout = Math.ceil(Math.max(budget - 2_000, budget / 2));
|
|
1397
1405
|
if (p.action === "cookie_profiles") {
|
|
1398
|
-
const browsers = await
|
|
1399
|
-
profiles: (await listBrowserProfiles(source)).map(({ profile, name }) => name ? { profile, name } : { profile }) })));
|
|
1406
|
+
const browsers = await this.fromCookies(() => this.cookies.list(p.browser, context.signal));
|
|
1400
1407
|
return { action: p.action, sessionId: null, data: { browsers } };
|
|
1401
1408
|
}
|
|
1402
1409
|
if (p.action === "status") {
|
|
@@ -1464,8 +1471,8 @@ export class BrowseSession {
|
|
|
1464
1471
|
throw new BrowseToolError("unsupported_backend", "live_view is Browserbase-only. Use the visible local browser window for user hand-off, then take a new snapshot.");
|
|
1465
1472
|
}
|
|
1466
1473
|
if (cookieSource) {
|
|
1467
|
-
const {
|
|
1468
|
-
const imported = await
|
|
1474
|
+
const { browser, profile } = cookieSource;
|
|
1475
|
+
const imported = await this.fromCookies(() => this.cookies.read(browser, profile, p.domains, context.signal));
|
|
1469
1476
|
// Imported values stay redacted from this binding's later output, e.g. evaluate reading document.cookie.
|
|
1470
1477
|
// ponytail: values under 8 characters are too generic to redact without mangling ordinary output.
|
|
1471
1478
|
for (const { value } of imported.cookies)
|
|
@@ -1485,7 +1492,7 @@ export class BrowseSession {
|
|
|
1485
1492
|
session.snapshots.clear();
|
|
1486
1493
|
}
|
|
1487
1494
|
}
|
|
1488
|
-
return this.output(p.action, session, undefined, { browser
|
|
1495
|
+
return this.output(p.action, session, undefined, { browser, profile, domains: imported.domains,
|
|
1489
1496
|
matched: imported.matched, imported: imported.cookies.length, skipped: imported.skipped,
|
|
1490
1497
|
reloadRequired: imported.cookies.length > 0, persistedOnClose: Boolean(session.contextId || session.quitPort || session.shared?.quitPort) });
|
|
1491
1498
|
}
|
package/package.json
CHANGED