@edlsh/browse-core 0.4.0 → 0.4.1

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 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
+ - A persistent profile's cached extension service workers are dropped before a launch whenever the bundled Stagehand extension changed, because Chrome otherwise keeps running the worker of the SDK that first launched the profile. Logins and extension data are kept.
18
19
  - `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.
19
20
  - `ClientInference` supplies Stagehand's default inference. Without it, unconfigured AI calls use Browserbase Model Gateway.
20
21
  - `settings.ts` holds the per-profile `/browse` settings file (`browserbase-ai.json`): `readSettings`, `writeSetting`, and the model-role helpers.
@@ -4,3 +4,12 @@
4
4
  * file is not JSON Chrome would read.
5
5
  */
6
6
  export declare function nameChromeProfile(userDataDir: string, name: string): Promise<void>;
7
+ /**
8
+ * Chrome caches an extension's service worker script in the profile and keeps running that copy after the extension's
9
+ * files change, even when the extension is loaded again. A persistent profile would so run the Stagehand extension of
10
+ * the SDK that first launched it, until a newer SDK refuses its protocol. When the bundled extension (its archive)
11
+ * differs from the one the profile last launched with, this drops the profile's service worker storage; Chrome then
12
+ * registers every extension's worker again from its files. Logins and extension data are kept. Skipped while a live
13
+ * Chrome holds the profile.
14
+ */
15
+ export declare function refreshExtensionWorkers(userDataDir: string, extensionArchive: Uint8Array): Promise<void>;
@@ -1,4 +1,5 @@
1
- import { lstat, mkdir, readFile, rename, writeFile } from "node:fs/promises";
1
+ import { createHash } from "node:crypto";
2
+ import { lstat, mkdir, readFile, readlink, rename, rm, writeFile } from "node:fs/promises";
2
3
  import { join } from "node:path";
3
4
  import { z } from "zod";
4
5
  /** The JSON files Chrome keeps the default profile's name in; every other key is kept as Chrome wrote it. */
@@ -50,3 +51,38 @@ export async function nameChromeProfile(userDataDir, name) {
50
51
  await replace(statePath, { ...state.data, profile: { ...profile, info_cache: { ...cache, Default: { ...entry, name, is_using_default_name: false } } } });
51
52
  await replace(preferencesPath, { ...preferences.data, profile: { ...preferences.data.profile, name, using_default_name: false } });
52
53
  }
54
+ /** Whether a live Chrome holds the user data dir. SingletonLock links to `<host>-<pid>` and outlives a Chrome that was killed. */
55
+ async function heldByChrome(userDataDir) {
56
+ const holder = await readlink(join(userDataDir, "SingletonLock")).catch(() => undefined);
57
+ if (holder === undefined)
58
+ return false;
59
+ const pid = Number(holder.slice(holder.lastIndexOf("-") + 1));
60
+ if (!Number.isInteger(pid) || pid <= 0)
61
+ return true;
62
+ try {
63
+ process.kill(pid, 0);
64
+ return true;
65
+ }
66
+ catch (error) {
67
+ return error instanceof Error && "code" in error && error.code === "EPERM";
68
+ }
69
+ }
70
+ /**
71
+ * Chrome caches an extension's service worker script in the profile and keeps running that copy after the extension's
72
+ * files change, even when the extension is loaded again. A persistent profile would so run the Stagehand extension of
73
+ * the SDK that first launched it, until a newer SDK refuses its protocol. When the bundled extension (its archive)
74
+ * differs from the one the profile last launched with, this drops the profile's service worker storage; Chrome then
75
+ * registers every extension's worker again from its files. Logins and extension data are kept. Skipped while a live
76
+ * Chrome holds the profile.
77
+ */
78
+ export async function refreshExtensionWorkers(userDataDir, extensionArchive) {
79
+ if (await heldByChrome(userDataDir))
80
+ return;
81
+ const marker = join(userDataDir, "stagehand-extension.sha256");
82
+ const digest = createHash("sha256").update(extensionArchive).digest("hex");
83
+ if (await readJson(marker) === digest)
84
+ return;
85
+ await rm(join(userDataDir, "Default", "Service Worker"), { recursive: true, force: true });
86
+ await mkdir(userDataDir, { recursive: true, mode: 0o700 });
87
+ await writeFile(marker, digest, { mode: 0o600 });
88
+ }
package/dist/session.js CHANGED
@@ -10,7 +10,7 @@ import { z } from "zod";
10
10
  import { askNeeds, askPage, DECIDE_TEXT } from "./ask.js";
11
11
  import { guardEndpoint } from "./cdp-guard.js";
12
12
  import { HarRecorder } from "./har.js";
13
- import { nameChromeProfile } from "./chrome-profile.js";
13
+ import { nameChromeProfile, refreshExtensionWorkers } 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";
@@ -1023,6 +1023,7 @@ export class BrowseSession {
1023
1023
  else {
1024
1024
  // The avatar and profile menu then say which harness owns the window; a profile Chrome cannot rename still launches.
1025
1025
  await nameChromeProfile(userDataDir, `${this.identity.name} browse`).catch(() => undefined);
1026
+ await refreshExtensionWorkers(userDataDir, await getExtensionArchive());
1026
1027
  quitPort = await freePort();
1027
1028
  browser = await localBrowser.launch({
1028
1029
  headless: p.headless ?? false, viewport: { width: 1280, height: 800 },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@edlsh/browse-core",
3
- "version": "0.4.0",
3
+ "version": "0.4.1",
4
4
  "description": "Host-neutral browser core (Browserbase, local Chrome, remote CDP, Stagehand) shared by omp-browse, pi-browse, and hermes-browse",
5
5
  "keywords": [
6
6
  "browser",