@howaboua/pi-browser 0.0.10 → 0.0.12
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 +11 -4
- package/README.md +11 -1
- package/biome.json +1 -1
- package/dist/extension.d.ts +3 -0
- package/dist/extension.js +29 -0
- package/dist/index.d.ts +1 -2
- package/dist/index.js +1 -100
- package/dist/remote-worker.js +137 -116
- package/dist/src/browser/config.d.ts +2 -0
- package/dist/src/browser/config.js +38 -5
- package/dist/src/browser/help.js +7 -7
- package/dist/src/browser/voice-start.d.ts +16 -0
- package/dist/src/browser/voice-start.js +110 -0
- package/dist/src/browser-registration.d.ts +6 -0
- package/dist/src/browser-registration.js +103 -0
- package/dist/src/browser-tool-definition.d.ts +12 -0
- package/dist/src/browser-tool-definition.js +63 -0
- package/dist/src/browser-tool.d.ts +1 -4
- package/dist/src/browser-tool.js +2 -62
- package/dist/src/cdp/session.d.ts +3 -8
- package/dist/src/cdp/session.js +1 -104
- package/dist/src/cdp/snapshot-tree.d.ts +8 -0
- package/dist/src/cdp/snapshot-tree.js +238 -0
- package/dist/src/cdp/snapshot.js +2 -234
- package/dist/src/cdp/tab-bridge.d.ts +25 -0
- package/dist/src/cdp/tab-bridge.js +107 -0
- package/index.ts +1 -127
- package/package.json +12 -8
- package/src/browser/config.ts +48 -7
- package/src/browser/help.ts +7 -7
- package/src/browser/voice-start.ts +154 -0
- package/src/browser-registration.ts +138 -0
- package/src/browser-tool-definition.ts +81 -0
- package/src/browser-tool.ts +3 -69
- package/src/cdp/session.ts +3 -145
- package/src/cdp/snapshot-tree.ts +291 -0
- package/src/cdp/snapshot.ts +5 -282
- package/src/cdp/tab-bridge.ts +149 -0
- package/tsconfig.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,16 @@
|
|
|
1
1
|
# @howaboua/pi-browser
|
|
2
2
|
|
|
3
|
+
## 0.0.12
|
|
4
|
+
|
|
5
|
+
- Added an API to start voice in local or configured remote browsers and report when the microphone is listening.
|
|
6
|
+
- Added a voice-only setting that hides browser automation tools from agents.
|
|
7
|
+
- Fixed TypeScript calls rejecting request objects while equivalent JSON strings worked.
|
|
8
|
+
|
|
9
|
+
## 0.0.11
|
|
10
|
+
|
|
11
|
+
- - Browser calls in Code and Notebook now return objects instead of JSON strings.
|
|
12
|
+
- GPT mode uses note-style browser help.
|
|
13
|
+
|
|
3
14
|
## 0.0.10
|
|
4
15
|
|
|
5
16
|
- Use denser browser help in PCC GPT prompt mode.
|
|
@@ -29,14 +40,12 @@
|
|
|
29
40
|
## 0.0.5
|
|
30
41
|
|
|
31
42
|
- Browser snapshots now include link destinations.
|
|
32
|
-
|
|
33
43
|
- Browser help and schemas now describe single-action requests, shared batch fields and JSON results with less repetition.
|
|
34
44
|
- Click errors now identify zero-sized browser viewports.
|
|
35
45
|
|
|
36
46
|
## 0.0.4
|
|
37
47
|
|
|
38
48
|
- Added session-owned background browser work and direct form controls.
|
|
39
|
-
|
|
40
49
|
- Fill or clear fields, select options, and set checkbox states, including indeterminate checkboxes.
|
|
41
50
|
- Press keys and shortcuts on the focused element.
|
|
42
51
|
- Wait for an element, page text, or URL with cancellation and a bounded timeout.
|
|
@@ -51,13 +60,11 @@
|
|
|
51
60
|
## 0.0.2
|
|
52
61
|
|
|
53
62
|
- Keep tool results actionable.
|
|
54
|
-
|
|
55
63
|
- Browser evaluation errors preserve JavaScript exception details instead of a generic “Uncaught”.
|
|
56
64
|
- Skill path inventories omit installed dependencies; reference reads list only the requested sources instead of repeating the full inventory.
|
|
57
65
|
|
|
58
66
|
## 0.0.1
|
|
59
67
|
|
|
60
68
|
- Initial release of Browser, a persistent typed CDP tool for normal Pi, Code Mode, and Notebook Mode.
|
|
61
|
-
|
|
62
69
|
- Inspect existing browser tabs, follow references, find text, click, type, capture screenshots, and use raw CDP when needed.
|
|
63
70
|
- Connect to local or configured remote hosts, with aliases and worker setup managed through `/browser`.
|
package/README.md
CHANGED
|
@@ -19,9 +19,11 @@ Pi Codex 3.0.52 or newer is optional. Without it, Browser remains a normal top-l
|
|
|
19
19
|
In normal Pi, call `browser` with `{}` for help, then send JSON request strings in `command`. Explicit `command: "help"` and existing object requests remain accepted. In Code or Notebook Mode, start with:
|
|
20
20
|
|
|
21
21
|
```js
|
|
22
|
-
await tools.browser()
|
|
22
|
+
await tools.browser();
|
|
23
23
|
```
|
|
24
24
|
|
|
25
|
+
Code and Notebook calls return objects directly. Remove `JSON.parse` around browser results in saved code and helpers.
|
|
26
|
+
|
|
25
27
|
All modes accept single-action and batched requests described by help. A common route is `tabs`, then `open`, then `click` or `type` with the returned `ref_id` and element ID.
|
|
26
28
|
|
|
27
29
|
Long Code and Notebook calls use the normal `exec` and `wait` lifecycle. Cancellation stops pending CDP work, though an already dispatched browser mutation may still take effect.
|
|
@@ -36,6 +38,14 @@ In Pi's interactive TUI, run `/browser`, add the SSH host names, identify the cu
|
|
|
36
38
|
|
|
37
39
|
Each name must be an existing SSH alias. On first use, the extension atomically installs or updates its managed worker at `~/.pi/agent/pi-browser-worker.mjs` on that host, then invokes it against the host's local CDP browser. No package installation is required on routed machines. Screenshots return through SCP and the remote artifact is removed. Keep `host` on follow-up calls that use a returned ref, screenshot, or continuation handle. Settings are stored in `pi-browser.json` under Pi's agent directory; `PI_BROWSER_CONFIG` overrides that storage path.
|
|
38
40
|
|
|
41
|
+
To use Pi Browser only for voice startup, add `"voiceOnly": true` to `pi-browser.json` and reload Pi. For a new file, the entire configuration can be:
|
|
42
|
+
|
|
43
|
+
```json
|
|
44
|
+
{ "voiceOnly": true }
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
The browser tool is unavailable to the agent, while voice startup and `/browser` host settings remain available. Keep any existing host routing fields in the same file. Remove the value or set it to `false` to restore the tool.
|
|
48
|
+
|
|
39
49
|
## Browser startup
|
|
40
50
|
|
|
41
51
|
The tool discovers CDP through `CDP_PORT`, port 9222, or `DevToolsActivePort`. Set `CDP_PORT_FILE` for a non-standard port file.
|
package/biome.json
CHANGED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { defineTool } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import { Type } from "typebox";
|
|
3
|
+
import { readBrowserRouteConfig, readBrowserVoiceOnly, } from "./src/browser/config.js";
|
|
4
|
+
import { registerBrowserCommand } from "./src/browser/settings.js";
|
|
5
|
+
/** Keep the normal browser graph out of Pi until the tool is enabled. */
|
|
6
|
+
export default async function browserExtension(pi) {
|
|
7
|
+
let voiceOnly = false;
|
|
8
|
+
try {
|
|
9
|
+
voiceOnly = readBrowserVoiceOnly();
|
|
10
|
+
}
|
|
11
|
+
catch {
|
|
12
|
+
// Normal registration reports invalid configuration and retains local access.
|
|
13
|
+
}
|
|
14
|
+
if (voiceOnly) {
|
|
15
|
+
registerBrowserCommand(pi);
|
|
16
|
+
try {
|
|
17
|
+
readBrowserRouteConfig();
|
|
18
|
+
}
|
|
19
|
+
catch (error) {
|
|
20
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
21
|
+
pi.on("session_start", (_event, ctx) => {
|
|
22
|
+
ctx.ui.notify(`${message}. Run /browser to repair it.`, "warning");
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
const { default: registerBrowser } = await import("./src/browser-registration.js");
|
|
28
|
+
await registerBrowser(pi, { defineTool, Type });
|
|
29
|
+
}
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -1,101 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
import { BrowserRuntime } from "./src/browser/runtime.js";
|
|
3
|
-
import { registerBrowserCommand } from "./src/browser/settings.js";
|
|
4
|
-
import { createBrowserTool, prepareBrowserInput } from "./src/browser-tool.js";
|
|
1
|
+
export { default } from "./extension.js";
|
|
5
2
|
export { createBrowserTool } from "./src/browser-tool.js";
|
|
6
|
-
const CODE_MODE_PACKAGE = "@howaboua/pi-codex-conversion";
|
|
7
|
-
const CODE_MODE_MODULE = `${CODE_MODE_PACKAGE}/code-mode`;
|
|
8
|
-
export default async function browserExtension(pi) {
|
|
9
|
-
let configError;
|
|
10
|
-
let routes;
|
|
11
|
-
try {
|
|
12
|
-
routes = loadBrowserRoutes();
|
|
13
|
-
}
|
|
14
|
-
catch (error) {
|
|
15
|
-
routes = new BrowserRoutes();
|
|
16
|
-
configError = error instanceof Error ? error.message : String(error);
|
|
17
|
-
}
|
|
18
|
-
const runtime = new BrowserRuntime(routes);
|
|
19
|
-
let gpt = false;
|
|
20
|
-
const tool = createBrowserTool(runtime, () => gpt);
|
|
21
|
-
registerBrowserCommand(pi);
|
|
22
|
-
pi.registerTool(tool);
|
|
23
|
-
try {
|
|
24
|
-
const { registerGptPromptMode } = await import("@howaboua/pi-codex-conversion/prompt-mode");
|
|
25
|
-
registerGptPromptMode(pi, (active) => {
|
|
26
|
-
gpt = active;
|
|
27
|
-
return [tool];
|
|
28
|
-
});
|
|
29
|
-
}
|
|
30
|
-
catch (error) {
|
|
31
|
-
if (!(error instanceof Error) ||
|
|
32
|
-
!("code" in error) ||
|
|
33
|
-
![
|
|
34
|
-
"ERR_MODULE_NOT_FOUND",
|
|
35
|
-
"MODULE_NOT_FOUND",
|
|
36
|
-
"ERR_PACKAGE_PATH_NOT_EXPORTED",
|
|
37
|
-
].includes(String(error.code)) ||
|
|
38
|
-
!error.message.includes(CODE_MODE_PACKAGE))
|
|
39
|
-
throw error;
|
|
40
|
-
}
|
|
41
|
-
const registration = await registerBrowserInCodeMode(pi, tool);
|
|
42
|
-
pi.on("session_shutdown", async () => {
|
|
43
|
-
registration?.unregister();
|
|
44
|
-
await runtime.close();
|
|
45
|
-
});
|
|
46
|
-
if (configError) {
|
|
47
|
-
pi.on("session_start", (_event, ctx) => {
|
|
48
|
-
ctx.ui.notify(`${configError}. Run /browser to repair it.`, "warning");
|
|
49
|
-
});
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
async function registerBrowserInCodeMode(pi, tool) {
|
|
53
|
-
try {
|
|
54
|
-
const { adaptToolForCodeMode, registerCodeModeExtensionTools } = await import("@howaboua/pi-codex-conversion/code-mode");
|
|
55
|
-
return registerCodeModeExtensionTools(pi, () => [
|
|
56
|
-
adaptToolForCodeMode(tool, {
|
|
57
|
-
kind: "freeform",
|
|
58
|
-
prepareInput: prepareBrowserInput,
|
|
59
|
-
discovery: "help",
|
|
60
|
-
}),
|
|
61
|
-
]);
|
|
62
|
-
}
|
|
63
|
-
catch (error) {
|
|
64
|
-
if (isMissingCodeModeExtension(error))
|
|
65
|
-
return undefined;
|
|
66
|
-
if (isOutdatedCodeModeExtension(error)) {
|
|
67
|
-
throw new Error("Update " +
|
|
68
|
-
CODE_MODE_PACKAGE +
|
|
69
|
-
" to 3.0.24 or newer to use Pi Browser with it", { cause: error });
|
|
70
|
-
}
|
|
71
|
-
throw error;
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
function isMissingCodeModeExtension(error) {
|
|
75
|
-
if (!error ||
|
|
76
|
-
typeof error !== "object" ||
|
|
77
|
-
!("code" in error) ||
|
|
78
|
-
!("message" in error) ||
|
|
79
|
-
typeof error.message !== "string") {
|
|
80
|
-
return false;
|
|
81
|
-
}
|
|
82
|
-
if (error.code !== "ERR_MODULE_NOT_FOUND" &&
|
|
83
|
-
error.code !== "MODULE_NOT_FOUND") {
|
|
84
|
-
return false;
|
|
85
|
-
}
|
|
86
|
-
const missing = error.message.match(/Cannot find (?:package|module) ['"]([^'"]+)['"]/)?.[1];
|
|
87
|
-
return missing === CODE_MODE_PACKAGE || missing === CODE_MODE_MODULE;
|
|
88
|
-
}
|
|
89
|
-
function isOutdatedCodeModeExtension(error) {
|
|
90
|
-
if (!error ||
|
|
91
|
-
typeof error !== "object" ||
|
|
92
|
-
!("code" in error) ||
|
|
93
|
-
!("message" in error) ||
|
|
94
|
-
typeof error.message !== "string")
|
|
95
|
-
return false;
|
|
96
|
-
return ((error.code === "ERR_PACKAGE_PATH_NOT_EXPORTED" ||
|
|
97
|
-
error.code === "ERR_UNSUPPORTED_DIR_IMPORT") &&
|
|
98
|
-
(error.message.includes(CODE_MODE_MODULE) ||
|
|
99
|
-
(error.message.includes("Package subpath './code-mode'") &&
|
|
100
|
-
error.message.includes(CODE_MODE_PACKAGE))));
|
|
101
|
-
}
|
package/dist/remote-worker.js
CHANGED
|
@@ -540,6 +540,9 @@ function normalizeBrowserRouteConfig(value) {
|
|
|
540
540
|
throw new Error("pi-browser config must be an object");
|
|
541
541
|
}
|
|
542
542
|
const input = value;
|
|
543
|
+
if (input.voiceOnly !== undefined && typeof input.voiceOnly !== "boolean") {
|
|
544
|
+
throw new Error("pi-browser voiceOnly must be a boolean");
|
|
545
|
+
}
|
|
543
546
|
const rawHosts = input.hosts ?? [];
|
|
544
547
|
if (!Array.isArray(rawHosts)) {
|
|
545
548
|
throw new Error("pi-browser config hosts must be an array");
|
|
@@ -564,21 +567,32 @@ function normalizeBrowserRouteConfig(value) {
|
|
|
564
567
|
return {
|
|
565
568
|
aliases,
|
|
566
569
|
hosts,
|
|
570
|
+
...input.voiceOnly === true ? { voiceOnly: true } : {},
|
|
567
571
|
remoteNodePath: remoteWord(input.remoteNodePath ?? DEFAULT_REMOTE_NODE_PATH, "pi-browser remoteNodePath")
|
|
568
572
|
};
|
|
569
573
|
}
|
|
570
|
-
function
|
|
574
|
+
function readBrowserConfig(path) {
|
|
571
575
|
let source;
|
|
572
576
|
try {
|
|
573
577
|
source = readFileSync(path, "utf8");
|
|
574
578
|
} catch (error) {
|
|
575
579
|
if (error && typeof error === "object" && "code" in error && error.code === "ENOENT") {
|
|
576
|
-
return
|
|
580
|
+
return;
|
|
577
581
|
}
|
|
578
582
|
throw error;
|
|
579
583
|
}
|
|
580
584
|
try {
|
|
581
|
-
return
|
|
585
|
+
return JSON.parse(source);
|
|
586
|
+
} catch (error) {
|
|
587
|
+
throw new Error(`Invalid pi-browser config at ${path}: ${error instanceof Error ? error.message : String(error)}`);
|
|
588
|
+
}
|
|
589
|
+
}
|
|
590
|
+
function readBrowserRouteConfig(path = browserConfigPath()) {
|
|
591
|
+
const value = readBrowserConfig(path);
|
|
592
|
+
if (value === undefined)
|
|
593
|
+
return defaultBrowserRouteConfig();
|
|
594
|
+
try {
|
|
595
|
+
return normalizeBrowserRouteConfig(value);
|
|
582
596
|
} catch (error) {
|
|
583
597
|
throw new Error(`Invalid pi-browser config at ${path}: ${error instanceof Error ? error.message : String(error)}`);
|
|
584
598
|
}
|
|
@@ -1056,6 +1070,106 @@ class CdpClient {
|
|
|
1056
1070
|
}
|
|
1057
1071
|
}
|
|
1058
1072
|
|
|
1073
|
+
// src/cdp/tab-bridge.ts
|
|
1074
|
+
var TAB_IDLE_MS = 20 * 60 * 1000;
|
|
1075
|
+
|
|
1076
|
+
class TabBridge {
|
|
1077
|
+
elementRefs = new Map;
|
|
1078
|
+
targetId;
|
|
1079
|
+
cdp;
|
|
1080
|
+
sessionId;
|
|
1081
|
+
tail = Promise.resolve();
|
|
1082
|
+
idleTimer;
|
|
1083
|
+
closed = false;
|
|
1084
|
+
backgroundRendering = false;
|
|
1085
|
+
onClose;
|
|
1086
|
+
constructor(targetId, cdp, sessionId, onClose) {
|
|
1087
|
+
this.targetId = targetId;
|
|
1088
|
+
this.cdp = cdp;
|
|
1089
|
+
this.sessionId = sessionId;
|
|
1090
|
+
this.onClose = onClose;
|
|
1091
|
+
this.resetIdle();
|
|
1092
|
+
cdp.onEvent("Target.targetDestroyed", (value) => {
|
|
1093
|
+
const event = asRecord(value, "target destroyed event");
|
|
1094
|
+
if (event["targetId"] === targetId)
|
|
1095
|
+
this.close(false);
|
|
1096
|
+
});
|
|
1097
|
+
cdp.onEvent("Target.detachedFromTarget", (value) => {
|
|
1098
|
+
const event = asRecord(value, "target detached event");
|
|
1099
|
+
if (event["sessionId"] === sessionId)
|
|
1100
|
+
this.close(false);
|
|
1101
|
+
});
|
|
1102
|
+
cdp.onClose(() => this.close(false));
|
|
1103
|
+
}
|
|
1104
|
+
static async connect(targetId, onClose, signal) {
|
|
1105
|
+
const cdp = new CdpClient;
|
|
1106
|
+
try {
|
|
1107
|
+
await cdp.connect(await getWebSocketUrl(), signal);
|
|
1108
|
+
const response = asRecord(await cdp.send("Target.attachToTarget", { targetId, flatten: true }, undefined, signal), "Target.attachToTarget response");
|
|
1109
|
+
if (typeof response["sessionId"] !== "string") {
|
|
1110
|
+
throw new Error("Chrome did not return a tab session");
|
|
1111
|
+
}
|
|
1112
|
+
return new TabBridge(targetId, cdp, response["sessionId"], onClose);
|
|
1113
|
+
} catch (error) {
|
|
1114
|
+
cdp.close();
|
|
1115
|
+
throw error;
|
|
1116
|
+
}
|
|
1117
|
+
}
|
|
1118
|
+
async run(refId, signal, action) {
|
|
1119
|
+
if (this.closed)
|
|
1120
|
+
throw new Error("Tab bridge is closed");
|
|
1121
|
+
let release;
|
|
1122
|
+
const turn = new Promise((resolveValue) => {
|
|
1123
|
+
release = resolveValue;
|
|
1124
|
+
});
|
|
1125
|
+
const previous = this.tail;
|
|
1126
|
+
this.tail = previous.then(() => turn, () => turn);
|
|
1127
|
+
try {
|
|
1128
|
+
await waitForTurn(previous, signal);
|
|
1129
|
+
if (this.closed)
|
|
1130
|
+
throw new Error("Tab bridge is closed");
|
|
1131
|
+
this.resetIdle();
|
|
1132
|
+
return await action({
|
|
1133
|
+
cdp: this.cdp,
|
|
1134
|
+
sessionId: this.sessionId,
|
|
1135
|
+
elementRefs: this.elementRefs,
|
|
1136
|
+
refId
|
|
1137
|
+
});
|
|
1138
|
+
} finally {
|
|
1139
|
+
release();
|
|
1140
|
+
}
|
|
1141
|
+
}
|
|
1142
|
+
async setBackgroundRendering(enabled, signal) {
|
|
1143
|
+
if (this.backgroundRendering === enabled)
|
|
1144
|
+
return;
|
|
1145
|
+
this.backgroundRendering = undefined;
|
|
1146
|
+
await this.cdp.send("Emulation.setFocusEmulationEnabled", { enabled }, this.sessionId, signal);
|
|
1147
|
+
this.backgroundRendering = enabled;
|
|
1148
|
+
}
|
|
1149
|
+
close(detach = true) {
|
|
1150
|
+
if (this.closed)
|
|
1151
|
+
return;
|
|
1152
|
+
this.closed = true;
|
|
1153
|
+
if (this.idleTimer)
|
|
1154
|
+
clearTimeout(this.idleTimer);
|
|
1155
|
+
this.elementRefs.clear();
|
|
1156
|
+
if (detach) {
|
|
1157
|
+
this.cdp.send("Target.detachFromTarget", { sessionId: this.sessionId }, undefined, undefined).catch(() => {
|
|
1158
|
+
return;
|
|
1159
|
+
}).finally(() => this.cdp.close());
|
|
1160
|
+
} else {
|
|
1161
|
+
this.cdp.close();
|
|
1162
|
+
}
|
|
1163
|
+
this.onClose();
|
|
1164
|
+
}
|
|
1165
|
+
resetIdle() {
|
|
1166
|
+
if (this.idleTimer)
|
|
1167
|
+
clearTimeout(this.idleTimer);
|
|
1168
|
+
this.idleTimer = setTimeout(() => this.close(), TAB_IDLE_MS);
|
|
1169
|
+
this.idleTimer.unref?.();
|
|
1170
|
+
}
|
|
1171
|
+
}
|
|
1172
|
+
|
|
1059
1173
|
// src/cdp/tabs.ts
|
|
1060
1174
|
import { createHash } from "node:crypto";
|
|
1061
1175
|
import { lstat, mkdir, readdir, rm, utimes, writeFile } from "node:fs/promises";
|
|
@@ -1314,105 +1428,6 @@ class BrowserTabs {
|
|
|
1314
1428
|
}
|
|
1315
1429
|
|
|
1316
1430
|
// src/cdp/session.ts
|
|
1317
|
-
var TAB_IDLE_MS = 20 * 60 * 1000;
|
|
1318
|
-
|
|
1319
|
-
class TabBridge {
|
|
1320
|
-
elementRefs = new Map;
|
|
1321
|
-
targetId;
|
|
1322
|
-
cdp;
|
|
1323
|
-
sessionId;
|
|
1324
|
-
tail = Promise.resolve();
|
|
1325
|
-
idleTimer;
|
|
1326
|
-
closed = false;
|
|
1327
|
-
backgroundRendering = false;
|
|
1328
|
-
onClose;
|
|
1329
|
-
constructor(targetId, cdp, sessionId, onClose) {
|
|
1330
|
-
this.targetId = targetId;
|
|
1331
|
-
this.cdp = cdp;
|
|
1332
|
-
this.sessionId = sessionId;
|
|
1333
|
-
this.onClose = onClose;
|
|
1334
|
-
this.resetIdle();
|
|
1335
|
-
cdp.onEvent("Target.targetDestroyed", (value) => {
|
|
1336
|
-
const event = asRecord(value, "target destroyed event");
|
|
1337
|
-
if (event["targetId"] === targetId)
|
|
1338
|
-
this.close(false);
|
|
1339
|
-
});
|
|
1340
|
-
cdp.onEvent("Target.detachedFromTarget", (value) => {
|
|
1341
|
-
const event = asRecord(value, "target detached event");
|
|
1342
|
-
if (event["sessionId"] === sessionId)
|
|
1343
|
-
this.close(false);
|
|
1344
|
-
});
|
|
1345
|
-
cdp.onClose(() => this.close(false));
|
|
1346
|
-
}
|
|
1347
|
-
static async connect(targetId, onClose, signal) {
|
|
1348
|
-
const cdp = new CdpClient;
|
|
1349
|
-
try {
|
|
1350
|
-
await cdp.connect(await getWebSocketUrl(), signal);
|
|
1351
|
-
const response = asRecord(await cdp.send("Target.attachToTarget", { targetId, flatten: true }, undefined, signal), "Target.attachToTarget response");
|
|
1352
|
-
if (typeof response["sessionId"] !== "string") {
|
|
1353
|
-
throw new Error("Chrome did not return a tab session");
|
|
1354
|
-
}
|
|
1355
|
-
return new TabBridge(targetId, cdp, response["sessionId"], onClose);
|
|
1356
|
-
} catch (error) {
|
|
1357
|
-
cdp.close();
|
|
1358
|
-
throw error;
|
|
1359
|
-
}
|
|
1360
|
-
}
|
|
1361
|
-
async run(refId, signal, action) {
|
|
1362
|
-
if (this.closed)
|
|
1363
|
-
throw new Error("Tab bridge is closed");
|
|
1364
|
-
let release;
|
|
1365
|
-
const turn = new Promise((resolveValue) => {
|
|
1366
|
-
release = resolveValue;
|
|
1367
|
-
});
|
|
1368
|
-
const previous = this.tail;
|
|
1369
|
-
this.tail = previous.then(() => turn, () => turn);
|
|
1370
|
-
try {
|
|
1371
|
-
await waitForTurn(previous, signal);
|
|
1372
|
-
if (this.closed)
|
|
1373
|
-
throw new Error("Tab bridge is closed");
|
|
1374
|
-
this.resetIdle();
|
|
1375
|
-
return await action({
|
|
1376
|
-
cdp: this.cdp,
|
|
1377
|
-
sessionId: this.sessionId,
|
|
1378
|
-
elementRefs: this.elementRefs,
|
|
1379
|
-
refId
|
|
1380
|
-
});
|
|
1381
|
-
} finally {
|
|
1382
|
-
release();
|
|
1383
|
-
}
|
|
1384
|
-
}
|
|
1385
|
-
async setBackgroundRendering(enabled, signal) {
|
|
1386
|
-
if (this.backgroundRendering === enabled)
|
|
1387
|
-
return;
|
|
1388
|
-
this.backgroundRendering = undefined;
|
|
1389
|
-
await this.cdp.send("Emulation.setFocusEmulationEnabled", { enabled }, this.sessionId, signal);
|
|
1390
|
-
this.backgroundRendering = enabled;
|
|
1391
|
-
}
|
|
1392
|
-
close(detach = true) {
|
|
1393
|
-
if (this.closed)
|
|
1394
|
-
return;
|
|
1395
|
-
this.closed = true;
|
|
1396
|
-
if (this.idleTimer)
|
|
1397
|
-
clearTimeout(this.idleTimer);
|
|
1398
|
-
this.elementRefs.clear();
|
|
1399
|
-
if (detach) {
|
|
1400
|
-
this.cdp.send("Target.detachFromTarget", { sessionId: this.sessionId }, undefined, undefined).catch(() => {
|
|
1401
|
-
return;
|
|
1402
|
-
}).finally(() => this.cdp.close());
|
|
1403
|
-
} else {
|
|
1404
|
-
this.cdp.close();
|
|
1405
|
-
}
|
|
1406
|
-
this.onClose();
|
|
1407
|
-
}
|
|
1408
|
-
resetIdle() {
|
|
1409
|
-
if (this.idleTimer)
|
|
1410
|
-
clearTimeout(this.idleTimer);
|
|
1411
|
-
this.idleTimer = setTimeout(() => this.close(), TAB_IDLE_MS);
|
|
1412
|
-
this.idleTimer.unref?.();
|
|
1413
|
-
}
|
|
1414
|
-
}
|
|
1415
|
-
|
|
1416
1431
|
class BrowserCdpSession {
|
|
1417
1432
|
closing;
|
|
1418
1433
|
root;
|
|
@@ -1732,13 +1747,13 @@ async function discardCachedResult(handle) {
|
|
|
1732
1747
|
function browserHelp(hosts = [], gpt = false) {
|
|
1733
1748
|
const routed = hosts.length > 0;
|
|
1734
1749
|
return {
|
|
1735
|
-
input:
|
|
1750
|
+
input: "{action,...fields}",
|
|
1736
1751
|
...routed ? {
|
|
1737
|
-
host: `${hosts.join("|")}${gpt ? " optional; user-named host→retain
|
|
1752
|
+
host: `${hosts.join("|")}${gpt ? " optional; user-named host→retain EVERY call+refs/handles" : " optional; keep a user-named host on every call and its refs/handles"}`
|
|
1738
1753
|
} : {},
|
|
1739
|
-
refs: "tabs -> open; keep ref_id/id/cursors with their result",
|
|
1740
|
-
safety: gpt ? "Unfamiliar low-trust navigation/consequential action→ask
|
|
1741
|
-
batch: gpt ? "{action_name:[{...fields}],...}; arrays
|
|
1754
|
+
refs: gpt ? "tabs→open; ref_id/id/cursors stay with result" : "tabs -> open; keep ref_id/id/cursors with their result",
|
|
1755
|
+
safety: gpt ? "Unfamiliar low-trust navigation/consequential action→ask UNLESS authorised; NEVER close shared browser" : "Ask before unfamiliar low-trust navigation or consequential action unless authorized; never close shared browser",
|
|
1756
|
+
batch: gpt ? "{action_name:[{...fields}],...}; nonempty arrays; omit item action; root host/response_length; independent ONLY" : "{action_name:[{...fields}],...}; nonempty arrays; items omit action; host/response_length at request root; independent only",
|
|
1742
1757
|
actions: {
|
|
1743
1758
|
help: "",
|
|
1744
1759
|
tabs: "query? offset? owned_only? -> ref_id title url owned",
|
|
@@ -2605,7 +2620,7 @@ async function waitForCondition(cdp, sessionId, condition, timeoutMs, signal) {
|
|
|
2605
2620
|
}
|
|
2606
2621
|
}
|
|
2607
2622
|
|
|
2608
|
-
// src/cdp/snapshot.ts
|
|
2623
|
+
// src/cdp/snapshot-tree.ts
|
|
2609
2624
|
var INTERACTIVE_ROLES = new Set([
|
|
2610
2625
|
"button",
|
|
2611
2626
|
"checkbox",
|
|
@@ -2722,14 +2737,8 @@ function interactiveText(role, name, value, state) {
|
|
|
2722
2737
|
].filter((entry) => entry !== undefined);
|
|
2723
2738
|
return `${role}${name ? ` ${name}` : ""}${value === "" || value == null ? "" : ` = ${JSON.stringify(value)}`}${states.length > 0 ? ` [${states.join(", ")}]` : ""}`;
|
|
2724
2739
|
}
|
|
2725
|
-
|
|
2726
|
-
const
|
|
2727
|
-
const responseLength = snapshotResponseLength(options.responseLength);
|
|
2728
|
-
const response = asRecord(await cdp.send("Accessibility.getFullAXTree", {}, sessionId, options.signal), "Accessibility response");
|
|
2729
|
-
if (!Array.isArray(response["nodes"])) {
|
|
2730
|
-
throw new Error("Accessibility response has no nodes");
|
|
2731
|
-
}
|
|
2732
|
-
const nodes = response["nodes"].map(axNode);
|
|
2740
|
+
function renderSnapshotTree(rawNodes, elementRefs) {
|
|
2741
|
+
const nodes = rawNodes.map(axNode);
|
|
2733
2742
|
const nodesById = new Map(nodes.map((node) => [node.nodeId, node]));
|
|
2734
2743
|
const childrenByParent = new Map;
|
|
2735
2744
|
for (const node of nodes) {
|
|
@@ -2820,6 +2829,18 @@ async function snapshotData(cdp, sessionId, elementRefs, options = {}) {
|
|
|
2820
2829
|
}
|
|
2821
2830
|
for (const node of nodes)
|
|
2822
2831
|
visit(node);
|
|
2832
|
+
return { lines, elements };
|
|
2833
|
+
}
|
|
2834
|
+
|
|
2835
|
+
// src/cdp/snapshot.ts
|
|
2836
|
+
async function snapshotData(cdp, sessionId, elementRefs, options = {}) {
|
|
2837
|
+
const lineno = positiveInteger(options.lineno ?? 1, "line cursor");
|
|
2838
|
+
const responseLength = snapshotResponseLength(options.responseLength);
|
|
2839
|
+
const response = asRecord(await cdp.send("Accessibility.getFullAXTree", {}, sessionId, options.signal), "Accessibility response");
|
|
2840
|
+
if (!Array.isArray(response["nodes"])) {
|
|
2841
|
+
throw new Error("Accessibility response has no nodes");
|
|
2842
|
+
}
|
|
2843
|
+
const { lines, elements } = renderSnapshotTree(response["nodes"], elementRefs);
|
|
2823
2844
|
const metadata = asRecord(await evaluate(cdp, sessionId, "({title: document.title, url: location.href})", options.signal), "page metadata");
|
|
2824
2845
|
const pattern = options.pattern?.toLowerCase();
|
|
2825
2846
|
const matching = pattern ? lines.filter((line) => line.text.toLowerCase().includes(pattern)) : lines;
|
|
@@ -2,10 +2,12 @@ export interface BrowserRouteConfig {
|
|
|
2
2
|
aliases: Record<string, string>;
|
|
3
3
|
hosts: string[];
|
|
4
4
|
remoteNodePath: string;
|
|
5
|
+
voiceOnly?: boolean;
|
|
5
6
|
}
|
|
6
7
|
export declare function browserConfigPath(): string;
|
|
7
8
|
export declare function defaultBrowserRouteConfig(): BrowserRouteConfig;
|
|
8
9
|
export declare function normalizeBrowserHostName(value: unknown, field?: string): string;
|
|
9
10
|
export declare function normalizeBrowserRouteConfig(value: unknown): BrowserRouteConfig;
|
|
11
|
+
export declare function readBrowserVoiceOnly(path?: string): boolean;
|
|
10
12
|
export declare function readBrowserRouteConfig(path?: string): BrowserRouteConfig;
|
|
11
13
|
export declare function writeBrowserRouteConfig(config: BrowserRouteConfig, path?: string): void;
|
|
@@ -35,6 +35,9 @@ export function normalizeBrowserRouteConfig(value) {
|
|
|
35
35
|
throw new Error("pi-browser config must be an object");
|
|
36
36
|
}
|
|
37
37
|
const input = value;
|
|
38
|
+
if (input.voiceOnly !== undefined && typeof input.voiceOnly !== "boolean") {
|
|
39
|
+
throw new Error("pi-browser voiceOnly must be a boolean");
|
|
40
|
+
}
|
|
38
41
|
const rawHosts = input.hosts ?? [];
|
|
39
42
|
if (!Array.isArray(rawHosts)) {
|
|
40
43
|
throw new Error("pi-browser config hosts must be an array");
|
|
@@ -59,10 +62,11 @@ export function normalizeBrowserRouteConfig(value) {
|
|
|
59
62
|
return {
|
|
60
63
|
aliases,
|
|
61
64
|
hosts,
|
|
65
|
+
...(input.voiceOnly === true ? { voiceOnly: true } : {}),
|
|
62
66
|
remoteNodePath: remoteWord(input.remoteNodePath ?? DEFAULT_REMOTE_NODE_PATH, "pi-browser remoteNodePath"),
|
|
63
67
|
};
|
|
64
68
|
}
|
|
65
|
-
|
|
69
|
+
function readBrowserConfig(path) {
|
|
66
70
|
let source;
|
|
67
71
|
try {
|
|
68
72
|
source = readFileSync(path, "utf8");
|
|
@@ -72,12 +76,36 @@ export function readBrowserRouteConfig(path = browserConfigPath()) {
|
|
|
72
76
|
typeof error === "object" &&
|
|
73
77
|
"code" in error &&
|
|
74
78
|
error.code === "ENOENT") {
|
|
75
|
-
return
|
|
79
|
+
return undefined;
|
|
76
80
|
}
|
|
77
81
|
throw error;
|
|
78
82
|
}
|
|
79
83
|
try {
|
|
80
|
-
return
|
|
84
|
+
return JSON.parse(source);
|
|
85
|
+
}
|
|
86
|
+
catch (error) {
|
|
87
|
+
throw new Error(`Invalid pi-browser config at ${path}: ${error instanceof Error ? error.message : String(error)}`);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
export function readBrowserVoiceOnly(path = browserConfigPath()) {
|
|
91
|
+
const value = readBrowserConfig(path);
|
|
92
|
+
if (value === undefined)
|
|
93
|
+
return false;
|
|
94
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
|
95
|
+
throw new Error(`Invalid pi-browser config at ${path}: pi-browser config must be an object`);
|
|
96
|
+
}
|
|
97
|
+
const voiceOnly = value.voiceOnly;
|
|
98
|
+
if (voiceOnly !== undefined && typeof voiceOnly !== "boolean") {
|
|
99
|
+
throw new Error(`Invalid pi-browser config at ${path}: pi-browser voiceOnly must be a boolean`);
|
|
100
|
+
}
|
|
101
|
+
return voiceOnly === true;
|
|
102
|
+
}
|
|
103
|
+
export function readBrowserRouteConfig(path = browserConfigPath()) {
|
|
104
|
+
const value = readBrowserConfig(path);
|
|
105
|
+
if (value === undefined)
|
|
106
|
+
return defaultBrowserRouteConfig();
|
|
107
|
+
try {
|
|
108
|
+
return normalizeBrowserRouteConfig(value);
|
|
81
109
|
}
|
|
82
110
|
catch (error) {
|
|
83
111
|
throw new Error(`Invalid pi-browser config at ${path}: ${error instanceof Error ? error.message : String(error)}`);
|
|
@@ -85,15 +113,20 @@ export function readBrowserRouteConfig(path = browserConfigPath()) {
|
|
|
85
113
|
}
|
|
86
114
|
export function writeBrowserRouteConfig(config, path = browserConfigPath()) {
|
|
87
115
|
const normalized = normalizeBrowserRouteConfig(config);
|
|
88
|
-
if (normalized.hosts.length === 0) {
|
|
116
|
+
if (normalized.hosts.length === 0 && !normalized.voiceOnly) {
|
|
89
117
|
rmSync(path, { force: true });
|
|
90
118
|
return;
|
|
91
119
|
}
|
|
92
120
|
const directory = dirname(path);
|
|
93
121
|
mkdirSync(directory, { recursive: true });
|
|
94
122
|
const temporary = join(directory, `.${basename(path)}.${process.pid}.${randomUUID()}.tmp`);
|
|
123
|
+
const stored = normalized.voiceOnly &&
|
|
124
|
+
normalized.hosts.length === 0 &&
|
|
125
|
+
normalized.remoteNodePath === DEFAULT_REMOTE_NODE_PATH
|
|
126
|
+
? { voiceOnly: true }
|
|
127
|
+
: normalized;
|
|
95
128
|
try {
|
|
96
|
-
writeFileSync(temporary, `${JSON.stringify(
|
|
129
|
+
writeFileSync(temporary, `${JSON.stringify(stored, null, 2)}\n`, {
|
|
97
130
|
encoding: "utf8",
|
|
98
131
|
flag: "wx",
|
|
99
132
|
mode: 0o600,
|
package/dist/src/browser/help.js
CHANGED
|
@@ -1,20 +1,20 @@
|
|
|
1
1
|
export function browserHelp(hosts = [], gpt = false) {
|
|
2
2
|
const routed = hosts.length > 0;
|
|
3
3
|
return {
|
|
4
|
-
input:
|
|
5
|
-
? "Request={action,...fields}; Pi {command:JSON.stringify(request)}; Code/Notebook JSON.stringify(request), JSON.parse(result)"
|
|
6
|
-
: "Request={action,...fields}; Pi: {command:JSON.stringify(request)}; Code/Notebook: JSON.stringify(request), JSON.parse(result)",
|
|
4
|
+
input: "{action,...fields}",
|
|
7
5
|
...(routed
|
|
8
6
|
? {
|
|
9
|
-
host: `${hosts.join("|")}${gpt ? " optional; user-named host→retain
|
|
7
|
+
host: `${hosts.join("|")}${gpt ? " optional; user-named host→retain EVERY call+refs/handles" : " optional; keep a user-named host on every call and its refs/handles"}`,
|
|
10
8
|
}
|
|
11
9
|
: {}),
|
|
12
|
-
refs:
|
|
10
|
+
refs: gpt
|
|
11
|
+
? "tabs→open; ref_id/id/cursors stay with result"
|
|
12
|
+
: "tabs -> open; keep ref_id/id/cursors with their result",
|
|
13
13
|
safety: gpt
|
|
14
|
-
? "Unfamiliar low-trust navigation/consequential action→ask
|
|
14
|
+
? "Unfamiliar low-trust navigation/consequential action→ask UNLESS authorised; NEVER close shared browser"
|
|
15
15
|
: "Ask before unfamiliar low-trust navigation or consequential action unless authorized; never close shared browser",
|
|
16
16
|
batch: gpt
|
|
17
|
-
? "{action_name:[{...fields}],...}; arrays
|
|
17
|
+
? "{action_name:[{...fields}],...}; nonempty arrays; omit item action; root host/response_length; independent ONLY"
|
|
18
18
|
: "{action_name:[{...fields}],...}; nonempty arrays; items omit action; host/response_length at request root; independent only",
|
|
19
19
|
actions: {
|
|
20
20
|
help: "",
|