@hmharness/browser 0.0.0-stage → 0.23.22
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/dist/cdp.d.ts +60 -0
- package/dist/cdp.js +189 -0
- package/dist/index.d.ts +15 -0
- package/dist/index.js +15 -0
- package/dist/lifecycle.d.ts +38 -0
- package/dist/lifecycle.js +189 -0
- package/dist/page.d.ts +48 -0
- package/dist/page.js +112 -0
- package/dist/registry.d.ts +37 -0
- package/dist/registry.js +141 -0
- package/dist/tools.d.ts +10 -0
- package/dist/tools.js +284 -0
- package/dist/trust.d.ts +45 -0
- package/dist/trust.js +88 -0
- package/package.json +29 -4
- package/README.md +0 -3
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
export declare const BROWSEROS_INSTALL_URL = "https://www.browseros.com";
|
|
2
|
+
export declare const BROWSEROS_REPO_URL = "https://github.com/browseros-ai/BrowserOS";
|
|
3
|
+
export interface DiscoveredBrowser {
|
|
4
|
+
id: string;
|
|
5
|
+
command: string;
|
|
6
|
+
product: 'browseros';
|
|
7
|
+
origin: 'install-local' | 'PATH' | 'config';
|
|
8
|
+
/** a real Chromium binary is >100MB; a broken shim/stub is not */
|
|
9
|
+
healthy: boolean;
|
|
10
|
+
unhealthyReason?: string;
|
|
11
|
+
/** from `--version` when the platform prints it (Windows GUI subsystem
|
|
12
|
+
* stays silent — silence is not unhealthiness for a browser binary) */
|
|
13
|
+
version?: string;
|
|
14
|
+
}
|
|
15
|
+
/** Default per-user install roots for the classic BrowserOS build.
|
|
16
|
+
* Real-machine layout (2026-10, Win): %LOCALAPPDATA%\BrowserOS\Application\
|
|
17
|
+
* with the executable named CHROME.EXE (Chromium's own name — the product
|
|
18
|
+
* docs say "BrowserOS.exe" but the installed tree keeps chrome.exe);
|
|
19
|
+
* BrowserOS.exe is probed too in case other installers rename it. */
|
|
20
|
+
export declare function browserosRoots(): string[];
|
|
21
|
+
/** Default user-data-dir of the user's DAILY BrowserOS (detection only). */
|
|
22
|
+
export declare function browserosUserDataDir(): string;
|
|
23
|
+
export declare function discoverBrowsers(force?: boolean, opts?: {
|
|
24
|
+
roots?: string[];
|
|
25
|
+
executablePath?: string;
|
|
26
|
+
}): DiscoveredBrowser[];
|
|
27
|
+
export interface RunningDetection {
|
|
28
|
+
running: boolean;
|
|
29
|
+
/** managed CDP port of the daily instance (default 9100) */
|
|
30
|
+
cdpPort?: number;
|
|
31
|
+
/** MCP proxy port of the daily instance (default 9000) */
|
|
32
|
+
mcpPort?: number;
|
|
33
|
+
}
|
|
34
|
+
/** Detect the user's DAILY BrowserOS: Chromium writes its managed ports
|
|
35
|
+
* into Local State (`browseros.server.cdp_port` / `.proxy_port`). Purely
|
|
36
|
+
* informational — hmh never attaches to this instance. */
|
|
37
|
+
export declare function detectRunning(userDataDir?: string): Promise<RunningDetection>;
|
package/dist/registry.js
ADDED
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @hmharness/browser - BrowserOS discovery + running-instance detection
|
|
3
|
+
*
|
|
4
|
+
* BrowserOS (browseros-ai/BrowserOS) is a Chromium fork, so the control
|
|
5
|
+
* plane is the Chrome DevTools Protocol it inherits: launch a DEDICATED
|
|
6
|
+
* instance with --remote-debugging-port + own profile, drive it over the
|
|
7
|
+
* loopback. Discovery is install-roots + PATH (+ config override) — the
|
|
8
|
+
* lsp registry pattern. Automatic downloads are explicitly NOT done here
|
|
9
|
+
* (supply chain; source allowlist + sha256 pinning is trust.ts's job).
|
|
10
|
+
*
|
|
11
|
+
* A user's DAILY BrowserOS is detected (Local State prefs carry its
|
|
12
|
+
* managed CDP port, default 9100, and MCP proxy port, default 9000) but
|
|
13
|
+
* never driven: hmh launches its own instance instead.
|
|
14
|
+
*/
|
|
15
|
+
import { spawnSync } from 'node:child_process';
|
|
16
|
+
import { existsSync, readFileSync, statSync } from 'node:fs';
|
|
17
|
+
import { basename, dirname, join } from 'node:path';
|
|
18
|
+
import { homedir } from 'node:os';
|
|
19
|
+
export const BROWSEROS_INSTALL_URL = 'https://www.browseros.com';
|
|
20
|
+
export const BROWSEROS_REPO_URL = 'https://github.com/browseros-ai/BrowserOS';
|
|
21
|
+
/** Default per-user install roots for the classic BrowserOS build.
|
|
22
|
+
* Real-machine layout (2026-10, Win): %LOCALAPPDATA%\BrowserOS\Application\
|
|
23
|
+
* with the executable named CHROME.EXE (Chromium's own name — the product
|
|
24
|
+
* docs say "BrowserOS.exe" but the installed tree keeps chrome.exe);
|
|
25
|
+
* BrowserOS.exe is probed too in case other installers rename it. */
|
|
26
|
+
export function browserosRoots() {
|
|
27
|
+
const la = process.env.LOCALAPPDATA;
|
|
28
|
+
const pf = process.env.ProgramFiles;
|
|
29
|
+
const pf86 = process.env['ProgramFiles(x86)'];
|
|
30
|
+
if (process.platform === 'win32') {
|
|
31
|
+
const bases = [la, pf, pf86].filter(Boolean);
|
|
32
|
+
const out = [];
|
|
33
|
+
for (const b of bases) {
|
|
34
|
+
out.push(join(b, 'BrowserOS', 'Application', 'chrome.exe'));
|
|
35
|
+
out.push(join(b, 'BrowserOS', 'Application', 'BrowserOS.exe'));
|
|
36
|
+
}
|
|
37
|
+
return out;
|
|
38
|
+
}
|
|
39
|
+
if (process.platform === 'darwin') {
|
|
40
|
+
return ['/Applications/BrowserOS.app/Contents/MacOS/BrowserOS'];
|
|
41
|
+
}
|
|
42
|
+
return ['/usr/lib/browseros/browseros', '/opt/browseros/browseros', join(homedir(), '.local', 'bin', 'browseros')];
|
|
43
|
+
}
|
|
44
|
+
/** Default user-data-dir of the user's DAILY BrowserOS (detection only). */
|
|
45
|
+
export function browserosUserDataDir() {
|
|
46
|
+
if (process.platform === 'win32') {
|
|
47
|
+
return join(process.env.LOCALAPPDATA ?? join(homedir(), 'AppData', 'Local'), 'BrowserOS', 'User Data');
|
|
48
|
+
}
|
|
49
|
+
if (process.platform === 'darwin') {
|
|
50
|
+
return join(homedir(), 'Library', 'Application Support', 'BrowserOS');
|
|
51
|
+
}
|
|
52
|
+
return join(homedir(), '.config', 'browser-os');
|
|
53
|
+
}
|
|
54
|
+
function which(cmd) {
|
|
55
|
+
const probe = process.platform === 'win32'
|
|
56
|
+
? spawnSync('cmd.exe', ['/d', '/s', '/c', `where ${cmd}`], { encoding: 'utf8', timeout: 5_000, windowsHide: true })
|
|
57
|
+
: spawnSync('which', [cmd], { encoding: 'utf8', timeout: 5_000 });
|
|
58
|
+
if (probe.status !== 0)
|
|
59
|
+
return null;
|
|
60
|
+
const first = (probe.stdout || '').split('\n').map((l) => l.trim()).filter(Boolean)[0];
|
|
61
|
+
return first ?? null;
|
|
62
|
+
}
|
|
63
|
+
/** Health = a plausible Chromium binary on disk. The binary is NEVER
|
|
64
|
+
* EXECUTED for probing: on Windows, chrome-family `--version` LAUNCHES
|
|
65
|
+
* THE BROWSER (GUI-subsystem quirk — observed popping a full BrowserOS
|
|
66
|
+
* instance on every `hmh` startup). Version info, when wanted, is parsed
|
|
67
|
+
* from the versioned directory that ships beside the executable
|
|
68
|
+
* (Application/<ver>/chrome.exe layout). */
|
|
69
|
+
function healthProbe(command) {
|
|
70
|
+
try {
|
|
71
|
+
const size = statSync(command).size;
|
|
72
|
+
if (size < 1_000_000)
|
|
73
|
+
return { healthy: false, reason: `suspiciously small for a browser binary (${size} bytes)` };
|
|
74
|
+
}
|
|
75
|
+
catch {
|
|
76
|
+
return { healthy: false, reason: 'not readable on disk' };
|
|
77
|
+
}
|
|
78
|
+
let version;
|
|
79
|
+
try {
|
|
80
|
+
const dir = dirname(command);
|
|
81
|
+
const m = basename(dir).match(/^\d+\.\d+\.\d+\.\d+$/);
|
|
82
|
+
if (m)
|
|
83
|
+
version = m[0];
|
|
84
|
+
}
|
|
85
|
+
catch { /* version display is optional */ }
|
|
86
|
+
return { healthy: true, version };
|
|
87
|
+
}
|
|
88
|
+
/** Discover BrowserOS on THIS machine (install roots > PATH > config
|
|
89
|
+
* override). Memoized; force re-probes. */
|
|
90
|
+
let discoveryCache = null;
|
|
91
|
+
export function discoverBrowsers(force = false, opts = {}) {
|
|
92
|
+
if (!force && discoveryCache)
|
|
93
|
+
return discoveryCache;
|
|
94
|
+
const out = [];
|
|
95
|
+
const push = (command, origin) => {
|
|
96
|
+
const h = healthProbe(command);
|
|
97
|
+
out.push({ id: 'browseros', command, product: 'browseros', origin, healthy: h.healthy, unhealthyReason: h.reason, version: h.version });
|
|
98
|
+
};
|
|
99
|
+
// 1. config override — the user wrote this path deliberately, highest precedence
|
|
100
|
+
if (opts.executablePath && existsSync(opts.executablePath))
|
|
101
|
+
push(opts.executablePath, 'config');
|
|
102
|
+
// 2. official install layout
|
|
103
|
+
for (const root of opts.roots ?? browserosRoots()) {
|
|
104
|
+
if (existsSync(root))
|
|
105
|
+
push(root, 'install-local');
|
|
106
|
+
}
|
|
107
|
+
// 3. PATH
|
|
108
|
+
const onPath = which('browseros') ?? (process.platform === 'win32' ? which('BrowserOS.exe') : null);
|
|
109
|
+
if (onPath)
|
|
110
|
+
push(onPath, 'PATH');
|
|
111
|
+
// first (preferred) source wins; config never duplicates a discovered copy
|
|
112
|
+
const seen = new Set();
|
|
113
|
+
discoveryCache = out.filter((b) => (seen.has(b.command) ? false : (seen.add(b.command), true)));
|
|
114
|
+
return discoveryCache;
|
|
115
|
+
}
|
|
116
|
+
/** Detect the user's DAILY BrowserOS: Chromium writes its managed ports
|
|
117
|
+
* into Local State (`browseros.server.cdp_port` / `.proxy_port`). Purely
|
|
118
|
+
* informational — hmh never attaches to this instance. */
|
|
119
|
+
export async function detectRunning(userDataDir = browserosUserDataDir()) {
|
|
120
|
+
let cdpPort = 9100;
|
|
121
|
+
let mcpPort = 9000;
|
|
122
|
+
try {
|
|
123
|
+
const localState = JSON.parse(readFileSync(join(userDataDir, 'Local State'), 'utf8'));
|
|
124
|
+
if (localState.browseros?.server?.cdp_port)
|
|
125
|
+
cdpPort = localState.browseros.server.cdp_port;
|
|
126
|
+
if (localState.browseros?.server?.proxy_port)
|
|
127
|
+
mcpPort = localState.browseros.server.proxy_port;
|
|
128
|
+
}
|
|
129
|
+
catch {
|
|
130
|
+
return { running: false };
|
|
131
|
+
}
|
|
132
|
+
// any HTTP answer (even 404/405) proves a listener; only a network error proves absence
|
|
133
|
+
for (const port of [cdpPort, mcpPort]) {
|
|
134
|
+
try {
|
|
135
|
+
await fetch(`http://127.0.0.1:${port}/json/version`, { signal: AbortSignal.timeout(1_000) });
|
|
136
|
+
return { running: true, cdpPort, mcpPort };
|
|
137
|
+
}
|
|
138
|
+
catch { /* try the other port */ }
|
|
139
|
+
}
|
|
140
|
+
return { running: false };
|
|
141
|
+
}
|
package/dist/tools.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { Tool } from '@hmharness/kernel';
|
|
2
|
+
export interface BrowserToolContext {
|
|
3
|
+
workspaceRoot: string;
|
|
4
|
+
/** HMH_HOME — required by the source-trust guard; when absent the
|
|
5
|
+
* browser refuses to launch (honest failure, no silent skip) */
|
|
6
|
+
home?: string;
|
|
7
|
+
}
|
|
8
|
+
export declare function browserTools(ctx: BrowserToolContext): Tool[];
|
|
9
|
+
/** Stop the driven instance + drop the live client (shutdown path). */
|
|
10
|
+
export declare function shutdownBrowser(): Promise<void>;
|
package/dist/tools.js
ADDED
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @hmharness/browser - agent tools (the browser_* family)
|
|
3
|
+
*
|
|
4
|
+
* Drives the OWNED BrowserOS instance: navigate → snapshot (refs) →
|
|
5
|
+
* click/type → verify, plus read/screenshot/scroll/tabs. The instance is
|
|
6
|
+
* auto-started on first use (dedicated profile, loopback CDP; the user's
|
|
7
|
+
* daily browser is never touched) and survives the agent — the user can
|
|
8
|
+
* watch the window and log in when a site demands it.
|
|
9
|
+
*
|
|
10
|
+
* Workflow hint baked into descriptions: browser_snapshot BEFORE acting,
|
|
11
|
+
* browser_screenshot + see_image when layout/visual state matters.
|
|
12
|
+
*/
|
|
13
|
+
import { mkdir, writeFile } from 'node:fs/promises';
|
|
14
|
+
import { join } from 'node:path';
|
|
15
|
+
import { loadConfig } from '@hmharness/kernel';
|
|
16
|
+
import { CdpBrowser } from "./cdp.js";
|
|
17
|
+
import { clientForInstance } from "./lifecycle.js";
|
|
18
|
+
import { SNAPSHOT_EXPR, READ_EXPR, clickExpr, typeExpr, scrollExpr, parseSnapshot, parseRead, formatSnapshot } from "./page.js";
|
|
19
|
+
let live = null;
|
|
20
|
+
/** Ensure a browser is driven and an active tab is pinned.
|
|
21
|
+
*
|
|
22
|
+
* Two modes:
|
|
23
|
+
* - HOST ATTACH (HMH_BROWSER_ATTACH=<port>): when hmharness runs INSIDE a
|
|
24
|
+
* host browser (acp-serve under BrowserOS), browser_* drives the user's
|
|
25
|
+
* REAL tabs — their pages, their logins — over the host's managed CDP
|
|
26
|
+
* port. That is the entire point of an in-browser agent; a second
|
|
27
|
+
* browser window popping up instead is a bug, not safety.
|
|
28
|
+
* - OWNED instance (default): dedicated profile under HMH_HOME — the
|
|
29
|
+
* standalone-CLI safety design; the user's daily browser is never
|
|
30
|
+
* touched. */
|
|
31
|
+
async function ensure(ctx) {
|
|
32
|
+
if (!live) {
|
|
33
|
+
const attachPort = Number(process.env.HMH_BROWSER_ATTACH ?? 0);
|
|
34
|
+
if (Number.isInteger(attachPort) && attachPort > 0) {
|
|
35
|
+
const host = new CdpBrowser({ port: attachPort });
|
|
36
|
+
if (await host.up()) {
|
|
37
|
+
const tabs = (await host.tabs()).filter((t) => !t.url.startsWith('chrome-extension://'));
|
|
38
|
+
const page = tabs.find((t) => !t.url.startsWith('about:')) ?? tabs[0];
|
|
39
|
+
live = { client: host, targetId: page?.targetId ?? (await host.openTab('about:blank')) };
|
|
40
|
+
}
|
|
41
|
+
else {
|
|
42
|
+
// we were told to drive the HOST browser and it is not reachable —
|
|
43
|
+
// falling back to a dedicated instance would pop a second window
|
|
44
|
+
// next to the one the user is talking from; refuse instead
|
|
45
|
+
throw new Error(`host browser CDP port ${attachPort} unreachable (HMH_BROWSER_ATTACH set) — the host's managed debugging port is off`);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
if (live && (await live.client.up())) {
|
|
50
|
+
const tabs = await live.client.tabs();
|
|
51
|
+
if (tabs.some((t) => t.targetId === live.targetId))
|
|
52
|
+
return live;
|
|
53
|
+
if (tabs.length > 0) {
|
|
54
|
+
live.targetId = tabs[0].targetId;
|
|
55
|
+
return live;
|
|
56
|
+
}
|
|
57
|
+
live.targetId = await live.client.openTab('about:blank');
|
|
58
|
+
return live;
|
|
59
|
+
}
|
|
60
|
+
if (!ctx.home) {
|
|
61
|
+
throw new Error('browser tools need HMH_HOME (source-trust guard) — set HMH_HOME or run via the hmh CLI');
|
|
62
|
+
}
|
|
63
|
+
const cfg = await loadConfig().catch(() => undefined);
|
|
64
|
+
const { client } = await clientForInstance(ctx.home, {
|
|
65
|
+
executablePath: cfg?.browser?.executablePath,
|
|
66
|
+
port: cfg?.browser?.cdpPort,
|
|
67
|
+
headless: cfg?.browser?.headless,
|
|
68
|
+
implicit: true, // agent auto-start: a headless instance must not outlive its host
|
|
69
|
+
});
|
|
70
|
+
const tabs = await client.tabs();
|
|
71
|
+
const targetId = tabs.length > 0 ? tabs[0].targetId : await client.openTab('about:blank');
|
|
72
|
+
live = { client, targetId };
|
|
73
|
+
return live;
|
|
74
|
+
}
|
|
75
|
+
const WRAP = (err) => ({
|
|
76
|
+
output: String(err instanceof Error ? err.message : err).slice(0, 400),
|
|
77
|
+
isError: true,
|
|
78
|
+
});
|
|
79
|
+
function httpUrl(u) {
|
|
80
|
+
const t = String(u ?? '').trim();
|
|
81
|
+
return /^https?:\/\//i.test(t) ? t : null;
|
|
82
|
+
}
|
|
83
|
+
function refArg(v) {
|
|
84
|
+
const n = Number(v);
|
|
85
|
+
return Number.isInteger(n) && n > 0 && n < 10_000 ? n : null;
|
|
86
|
+
}
|
|
87
|
+
export function browserTools(ctx) {
|
|
88
|
+
return [
|
|
89
|
+
{
|
|
90
|
+
name: 'browser_navigate',
|
|
91
|
+
description: 'Navigate the BrowserOS tab to an http(s) URL, wait for load. First use auto-starts a DEDICATED BrowserOS instance (own profile; the user\'s daily browser is untouched). After navigating, call browser_snapshot to see the interactive elements.',
|
|
92
|
+
parameters: { type: 'object', properties: { url: { type: 'string', description: 'http(s) URL' } }, required: ['url'] },
|
|
93
|
+
async execute(args) {
|
|
94
|
+
try {
|
|
95
|
+
const url = httpUrl(String(args.url ?? ''));
|
|
96
|
+
if (!url)
|
|
97
|
+
return { output: 'only http(s) URLs', isError: true };
|
|
98
|
+
const { client, targetId } = await ensure(ctx);
|
|
99
|
+
await client.navigate(targetId, url);
|
|
100
|
+
const tabs = await client.tabs();
|
|
101
|
+
const tab = tabs.find((t) => t.targetId === targetId);
|
|
102
|
+
return { output: `navigated: ${tab?.title || '(loading)'}\n${tab?.url || url}\nnext: browser_snapshot` };
|
|
103
|
+
}
|
|
104
|
+
catch (err) {
|
|
105
|
+
return WRAP(err);
|
|
106
|
+
}
|
|
107
|
+
},
|
|
108
|
+
},
|
|
109
|
+
{
|
|
110
|
+
name: 'browser_snapshot',
|
|
111
|
+
description: 'List the visible interactive elements of the current page with stable refs ([N] tag text). Click/type target these refs. Re-snapshot after any navigation or DOM change. For visual/layout questions use browser_screenshot + see_image instead.',
|
|
112
|
+
parameters: { type: 'object', properties: {} },
|
|
113
|
+
async execute() {
|
|
114
|
+
try {
|
|
115
|
+
const { client, targetId } = await ensure(ctx);
|
|
116
|
+
const snap = parseSnapshot(await client.evaluate(targetId, SNAPSHOT_EXPR));
|
|
117
|
+
return { output: formatSnapshot(snap) };
|
|
118
|
+
}
|
|
119
|
+
catch (err) {
|
|
120
|
+
return WRAP(err);
|
|
121
|
+
}
|
|
122
|
+
},
|
|
123
|
+
},
|
|
124
|
+
{
|
|
125
|
+
name: 'browser_click',
|
|
126
|
+
description: 'Click an element by its browser_snapshot ref. After the click, re-snapshot (or browser_screenshot) to see the new state.',
|
|
127
|
+
parameters: { type: 'object', properties: { ref: { type: 'number', description: 'element ref from browser_snapshot' } }, required: ['ref'] },
|
|
128
|
+
async execute(args) {
|
|
129
|
+
try {
|
|
130
|
+
const ref = refArg(args.ref);
|
|
131
|
+
if (!ref)
|
|
132
|
+
return { output: 'ref must be an integer from browser_snapshot', isError: true };
|
|
133
|
+
const { client, targetId } = await ensure(ctx);
|
|
134
|
+
const r = await client.evaluate(targetId, clickExpr(ref));
|
|
135
|
+
if (r === 'E_NO_REF')
|
|
136
|
+
return { output: `ref ${ref} not on this page (stale after navigation/DOM change) — browser_snapshot again`, isError: true };
|
|
137
|
+
return { output: `clicked [${ref}] — re-snapshot to see the result` };
|
|
138
|
+
}
|
|
139
|
+
catch (err) {
|
|
140
|
+
return WRAP(err);
|
|
141
|
+
}
|
|
142
|
+
},
|
|
143
|
+
},
|
|
144
|
+
{
|
|
145
|
+
name: 'browser_type',
|
|
146
|
+
description: 'Type text into an input/textarea/contenteditable by its browser_snapshot ref (native setter + input/change events — React/Vue compatible). submit=true presses submit (form.requestSubmit, else Enter).',
|
|
147
|
+
parameters: {
|
|
148
|
+
type: 'object',
|
|
149
|
+
properties: {
|
|
150
|
+
ref: { type: 'number', description: 'element ref from browser_snapshot' },
|
|
151
|
+
text: { type: 'string', description: 'text to type (replaces current value)' },
|
|
152
|
+
submit: { type: 'boolean', description: 'submit the form after typing (default false)' },
|
|
153
|
+
},
|
|
154
|
+
required: ['ref', 'text'],
|
|
155
|
+
},
|
|
156
|
+
async execute(args) {
|
|
157
|
+
try {
|
|
158
|
+
const ref = refArg(args.ref);
|
|
159
|
+
if (!ref)
|
|
160
|
+
return { output: 'ref must be an integer from browser_snapshot', isError: true };
|
|
161
|
+
const text = String(args.text ?? '');
|
|
162
|
+
const { client, targetId } = await ensure(ctx);
|
|
163
|
+
const r = await client.evaluate(targetId, typeExpr(ref, text, Boolean(args.submit)));
|
|
164
|
+
if (r === 'E_NO_REF')
|
|
165
|
+
return { output: `ref ${ref} not on this page (stale after navigation/DOM change) — browser_snapshot again`, isError: true };
|
|
166
|
+
return { output: `typed into [${ref}]${args.submit ? ' + submitted' : ''}` };
|
|
167
|
+
}
|
|
168
|
+
catch (err) {
|
|
169
|
+
return WRAP(err);
|
|
170
|
+
}
|
|
171
|
+
},
|
|
172
|
+
},
|
|
173
|
+
{
|
|
174
|
+
name: 'browser_read',
|
|
175
|
+
description: 'Read the current page as text (title, URL, visible innerText, capped ~12k chars). Prefer this over screenshot for content extraction; use browser_screenshot for layout/visual state.',
|
|
176
|
+
parameters: { type: 'object', properties: {} },
|
|
177
|
+
async execute() {
|
|
178
|
+
try {
|
|
179
|
+
const { client, targetId } = await ensure(ctx);
|
|
180
|
+
const r = parseRead(await client.evaluate(targetId, READ_EXPR));
|
|
181
|
+
return { output: `${r.title}\n${r.url}${r.truncated ? ' (text truncated)' : ''}\n\n${r.text}` };
|
|
182
|
+
}
|
|
183
|
+
catch (err) {
|
|
184
|
+
return WRAP(err);
|
|
185
|
+
}
|
|
186
|
+
},
|
|
187
|
+
},
|
|
188
|
+
{
|
|
189
|
+
name: 'browser_scroll',
|
|
190
|
+
description: 'Scroll the current page: down | up | top | bottom (step ~600px). Snapshot again after scrolling — new elements become visible and get refs.',
|
|
191
|
+
parameters: { type: 'object', properties: { direction: { type: 'string', description: 'down (default) | up | top | bottom' } } },
|
|
192
|
+
async execute(args) {
|
|
193
|
+
try {
|
|
194
|
+
const { client, targetId } = await ensure(ctx);
|
|
195
|
+
const r = await client.evaluate(targetId, scrollExpr(String(args.direction ?? 'down')));
|
|
196
|
+
return { output: `scrolled (${String(r)}) — browser_snapshot again for newly visible elements` };
|
|
197
|
+
}
|
|
198
|
+
catch (err) {
|
|
199
|
+
return WRAP(err);
|
|
200
|
+
}
|
|
201
|
+
},
|
|
202
|
+
},
|
|
203
|
+
{
|
|
204
|
+
name: 'browser_screenshot',
|
|
205
|
+
description: 'Screenshot the current page to a PNG file; returns the path — then call see_image with it to actually look. Use when layout/visual state matters; for text content prefer browser_read.',
|
|
206
|
+
parameters: { type: 'object', properties: {} },
|
|
207
|
+
async execute() {
|
|
208
|
+
try {
|
|
209
|
+
const home = ctx.home;
|
|
210
|
+
if (!home)
|
|
211
|
+
return { output: 'screenshot needs HMH_HOME for file storage', isError: true };
|
|
212
|
+
const { client, targetId } = await ensure(ctx);
|
|
213
|
+
const png = await client.screenshot(targetId);
|
|
214
|
+
const dir = join(home, 'screenshots');
|
|
215
|
+
await mkdir(dir, { recursive: true });
|
|
216
|
+
const file = join(dir, `browser-${Date.now()}.png`);
|
|
217
|
+
await writeFile(file, png);
|
|
218
|
+
return { output: `${file} — call see_image with this path to look at it` };
|
|
219
|
+
}
|
|
220
|
+
catch (err) {
|
|
221
|
+
return WRAP(err);
|
|
222
|
+
}
|
|
223
|
+
},
|
|
224
|
+
},
|
|
225
|
+
{
|
|
226
|
+
name: 'browser_tabs',
|
|
227
|
+
description: 'Manage tabs of the driven browser: list (default) | new [url] | activate <target_id> | close <target_id>. Navigate/click/type/read always act on the ACTIVE tab.',
|
|
228
|
+
parameters: {
|
|
229
|
+
type: 'object',
|
|
230
|
+
properties: {
|
|
231
|
+
action: { type: 'string', description: 'list | new | activate | close' },
|
|
232
|
+
target_id: { type: 'string', description: 'tab id (list/new output) for activate/close' },
|
|
233
|
+
url: { type: 'string', description: 'opening URL for action=new' },
|
|
234
|
+
},
|
|
235
|
+
},
|
|
236
|
+
async execute(args) {
|
|
237
|
+
try {
|
|
238
|
+
const action = String(args.action ?? 'list');
|
|
239
|
+
const { client } = await ensure(ctx);
|
|
240
|
+
if (action === 'list') {
|
|
241
|
+
const tabs = await client.tabs();
|
|
242
|
+
return { output: tabs.length + ' tab(s)\n' + tabs.map((t) => ` ${t.targetId}${t.targetId === live?.targetId ? ' *active*' : ''} ${t.title.slice(0, 60)} — ${t.url.slice(0, 100)}`).join('\n') };
|
|
243
|
+
}
|
|
244
|
+
if (action === 'new') {
|
|
245
|
+
const url = args.url ? httpUrl(String(args.url)) ?? 'about:blank' : 'about:blank';
|
|
246
|
+
const targetId = await client.openTab(url);
|
|
247
|
+
if (live)
|
|
248
|
+
live.targetId = targetId;
|
|
249
|
+
await client.activateTab(targetId).catch(() => undefined);
|
|
250
|
+
return { output: `new tab ${targetId} (now active): ${url}` };
|
|
251
|
+
}
|
|
252
|
+
const id = String(args.target_id ?? '');
|
|
253
|
+
if (!id)
|
|
254
|
+
return { output: `action ${action} needs target_id (see browser_tabs list)`, isError: true };
|
|
255
|
+
if (action === 'activate') {
|
|
256
|
+
await client.activateTab(id);
|
|
257
|
+
if (live)
|
|
258
|
+
live.targetId = id;
|
|
259
|
+
return { output: `activated ${id}` };
|
|
260
|
+
}
|
|
261
|
+
if (action === 'close') {
|
|
262
|
+
await client.closeTab(id);
|
|
263
|
+
if (live?.targetId === id) {
|
|
264
|
+
const tabs = await client.tabs();
|
|
265
|
+
live.targetId = tabs[0]?.targetId ?? (await client.openTab('about:blank'));
|
|
266
|
+
}
|
|
267
|
+
return { output: `closed ${id}` };
|
|
268
|
+
}
|
|
269
|
+
return { output: `unknown action '${action}' (list | new | activate | close)`, isError: true };
|
|
270
|
+
}
|
|
271
|
+
catch (err) {
|
|
272
|
+
return WRAP(err);
|
|
273
|
+
}
|
|
274
|
+
},
|
|
275
|
+
},
|
|
276
|
+
];
|
|
277
|
+
}
|
|
278
|
+
/** Stop the driven instance + drop the live client (shutdown path). */
|
|
279
|
+
export async function shutdownBrowser() {
|
|
280
|
+
if (live) {
|
|
281
|
+
await live.client.close().catch(() => undefined);
|
|
282
|
+
live = null;
|
|
283
|
+
}
|
|
284
|
+
}
|
package/dist/trust.d.ts
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
export interface BrowserTrustEntry {
|
|
2
|
+
id: string;
|
|
3
|
+
command: string;
|
|
4
|
+
sha256: string;
|
|
5
|
+
origin: string;
|
|
6
|
+
trustedAt: string;
|
|
7
|
+
autoTrusted: boolean;
|
|
8
|
+
}
|
|
9
|
+
export interface BrowserTrustStore {
|
|
10
|
+
kind: 'hmharness-browser-trust';
|
|
11
|
+
version: 1;
|
|
12
|
+
entries: Record<string, BrowserTrustEntry>;
|
|
13
|
+
}
|
|
14
|
+
export declare function browserTrustPath(home: string): string;
|
|
15
|
+
/** sha256 of the browser binary (the executable file itself). */
|
|
16
|
+
export declare function browserHash(command: string): Promise<string>;
|
|
17
|
+
export interface BrowserTrustVerdict {
|
|
18
|
+
trusted: boolean;
|
|
19
|
+
reason: string;
|
|
20
|
+
/** set when the id is trusted but the BINARY changed (tamper case) */
|
|
21
|
+
tampered?: {
|
|
22
|
+
pinned: string;
|
|
23
|
+
actual: string;
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
/** Check whether a discovered browser may be launched. */
|
|
27
|
+
export declare function checkBrowserTrust(home: string, browser: {
|
|
28
|
+
id: string;
|
|
29
|
+
command: string;
|
|
30
|
+
origin: string;
|
|
31
|
+
}): Promise<BrowserTrustVerdict>;
|
|
32
|
+
/** Explicit user trust: pins the CURRENT hash. */
|
|
33
|
+
export declare function trustBrowser(home: string, browser: {
|
|
34
|
+
id: string;
|
|
35
|
+
command: string;
|
|
36
|
+
origin: string;
|
|
37
|
+
}): Promise<{
|
|
38
|
+
ok: boolean;
|
|
39
|
+
error?: string;
|
|
40
|
+
sha256?: string;
|
|
41
|
+
}>;
|
|
42
|
+
/** Revoke trust for a browser id. */
|
|
43
|
+
export declare function untrustBrowser(home: string, id: string): Promise<boolean>;
|
|
44
|
+
/** Read-only view for `hmh browser status`. */
|
|
45
|
+
export declare function listBrowserTrust(home: string): Promise<BrowserTrustEntry[]>;
|
package/dist/trust.js
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @hmharness/browser - source trust (Capability OS supply-chain guard,
|
|
3
|
+
* same contract as @hmharness/lsp trust, applied to the browser binary)
|
|
4
|
+
*
|
|
5
|
+
* Launching BrowserOS means executing a binary we discovered on the
|
|
6
|
+
* machine — with a profile the agent drives. The rule: 来源白名单 + hash.
|
|
7
|
+
*
|
|
8
|
+
* - the binary gets a sha256 PIN at trust time
|
|
9
|
+
* - install-local (official layout) and config (user wrote the path
|
|
10
|
+
* deliberately) auto-trust ONCE — the pin still applies
|
|
11
|
+
* - PATH binaries require explicit one-time trust (hmh browser trust)
|
|
12
|
+
* - a trusted binary whose hash CHANGED is refused (tamper detection),
|
|
13
|
+
* with both hashes shown; a genuine BrowserOS self-update also trips
|
|
14
|
+
* this — re-trust knowingly
|
|
15
|
+
*
|
|
16
|
+
* The trust store lives in HMH_HOME/cognitive/browser-trust.json — plain
|
|
17
|
+
* JSON, user-inspectable, no telemetry.
|
|
18
|
+
*/
|
|
19
|
+
import { createHash } from 'node:crypto';
|
|
20
|
+
import { readFile, writeFile, mkdir } from 'node:fs/promises';
|
|
21
|
+
import { join } from 'node:path';
|
|
22
|
+
export function browserTrustPath(home) {
|
|
23
|
+
return join(home, 'cognitive', 'browser-trust.json');
|
|
24
|
+
}
|
|
25
|
+
async function loadTrust(home) {
|
|
26
|
+
try {
|
|
27
|
+
const j = JSON.parse(await readFile(browserTrustPath(home), 'utf8'));
|
|
28
|
+
if (j && j.kind === 'hmharness-browser-trust' && j.entries && typeof j.entries === 'object')
|
|
29
|
+
return j;
|
|
30
|
+
}
|
|
31
|
+
catch { /* absent/corrupt = empty store */ }
|
|
32
|
+
return { kind: 'hmharness-browser-trust', version: 1, entries: {} };
|
|
33
|
+
}
|
|
34
|
+
/** sha256 of the browser binary (the executable file itself). */
|
|
35
|
+
export async function browserHash(command) {
|
|
36
|
+
const buf = await readFile(command);
|
|
37
|
+
return createHash('sha256').update(buf).digest('hex');
|
|
38
|
+
}
|
|
39
|
+
/** Check whether a discovered browser may be launched. */
|
|
40
|
+
export async function checkBrowserTrust(home, browser) {
|
|
41
|
+
const store = await loadTrust(home);
|
|
42
|
+
const entry = store.entries[browser.id];
|
|
43
|
+
const actual = await browserHash(browser.command).catch(() => '');
|
|
44
|
+
if (!entry) {
|
|
45
|
+
if (browser.origin === 'install-local' || browser.origin === 'config') {
|
|
46
|
+
// official install layout / deliberate config path: auto-trust once
|
|
47
|
+
const pinned = { id: browser.id, command: browser.command, sha256: actual, origin: browser.origin, trustedAt: new Date().toISOString(), autoTrusted: true };
|
|
48
|
+
const next = { ...store, entries: { ...store.entries, [browser.id]: pinned } };
|
|
49
|
+
await mkdir(join(home, 'cognitive'), { recursive: true });
|
|
50
|
+
await writeFile(browserTrustPath(home), JSON.stringify(next, null, 1), 'utf8');
|
|
51
|
+
return { trusted: true, reason: `auto-trusted (${browser.origin}), sha256 pinned ${actual.slice(0, 12)}…` };
|
|
52
|
+
}
|
|
53
|
+
return { trusted: false, reason: `PATH binary not trusted yet — run: hmh browser trust ${browser.id}` };
|
|
54
|
+
}
|
|
55
|
+
if (entry.sha256 && actual && entry.sha256 !== actual) {
|
|
56
|
+
return {
|
|
57
|
+
trusted: false,
|
|
58
|
+
reason: `binary CHANGED since trust — pinned ${entry.sha256.slice(0, 12)}…, actual ${actual.slice(0, 12)}…; a BrowserOS self-update also changes the hash: re-trust only if you know why it changed`,
|
|
59
|
+
tampered: { pinned: entry.sha256, actual },
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
return { trusted: true, reason: `trusted since ${entry.trustedAt.slice(0, 10)} (${entry.autoTrusted ? 'auto' : 'user'}), hash matches` };
|
|
63
|
+
}
|
|
64
|
+
/** Explicit user trust: pins the CURRENT hash. */
|
|
65
|
+
export async function trustBrowser(home, browser) {
|
|
66
|
+
const sha = await browserHash(browser.command).catch(() => '');
|
|
67
|
+
if (!sha)
|
|
68
|
+
return { ok: false, error: 'cannot hash the browser binary (unreadable?)' };
|
|
69
|
+
const store = await loadTrust(home);
|
|
70
|
+
store.entries[browser.id] = { id: browser.id, command: browser.command, sha256: sha, origin: browser.origin, trustedAt: new Date().toISOString(), autoTrusted: false };
|
|
71
|
+
await mkdir(join(home, 'cognitive'), { recursive: true });
|
|
72
|
+
await writeFile(browserTrustPath(home), JSON.stringify(store, null, 1), 'utf8');
|
|
73
|
+
return { ok: true, sha256: sha };
|
|
74
|
+
}
|
|
75
|
+
/** Revoke trust for a browser id. */
|
|
76
|
+
export async function untrustBrowser(home, id) {
|
|
77
|
+
const store = await loadTrust(home);
|
|
78
|
+
if (!store.entries[id])
|
|
79
|
+
return false;
|
|
80
|
+
delete store.entries[id];
|
|
81
|
+
await writeFile(browserTrustPath(home), JSON.stringify(store, null, 1), 'utf8');
|
|
82
|
+
return true;
|
|
83
|
+
}
|
|
84
|
+
/** Read-only view for `hmh browser status`. */
|
|
85
|
+
export async function listBrowserTrust(home) {
|
|
86
|
+
const store = await loadTrust(home);
|
|
87
|
+
return Object.values(store.entries);
|
|
88
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,31 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hmharness/browser",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.23.22",
|
|
4
|
+
"description": "hmharness BrowserOS driver — discovers, trust-pins and launches the BrowserOS AI browser (Chromium fork), then drives it over the Chrome DevTools Protocol. Client only, zero dependencies, dedicated profile: the user's daily browser is never touched.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"default": "./dist/index.js"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"scripts": {
|
|
15
|
+
"build": "tsc -p tsconfig.build.json"
|
|
16
|
+
},
|
|
17
|
+
"dependencies": {
|
|
18
|
+
"@hmharness/kernel": "0.23.22"
|
|
19
|
+
},
|
|
20
|
+
"files": [
|
|
21
|
+
"dist"
|
|
22
|
+
],
|
|
23
|
+
"license": "MIT",
|
|
24
|
+
"repository": {
|
|
25
|
+
"type": "git",
|
|
26
|
+
"url": "git+https://github.com/swsgbl/hmharness.git"
|
|
27
|
+
},
|
|
28
|
+
"engines": {
|
|
29
|
+
"node": ">=22"
|
|
30
|
+
}
|
|
31
|
+
}
|
package/README.md
DELETED