@howaboua/pi-browser 0.0.13 → 0.0.15
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 -0
- package/dist/extension.js +2 -2
- package/dist/remote-worker.js +90 -34
- package/dist/src/browser/artifacts.js +99 -39
- package/dist/src/browser/help.d.ts +1 -1
- package/dist/src/browser/help.js +5 -11
- package/dist/src/browser/settings.js +3 -6
- package/dist/src/browser-registration.d.ts +4 -2
- package/dist/src/browser-registration.js +2 -21
- package/dist/src/browser-tool-definition.d.ts +7 -5
- package/dist/src/browser-tool-definition.js +6 -8
- package/dist/src/browser-tool.d.ts +2 -3
- package/dist/src/browser-tool.js +3 -3
- package/extension.ts +4 -5
- package/package.json +93 -94
- package/src/browser/artifacts.ts +114 -48
- package/src/browser/help.ts +6 -11
- package/src/browser/settings.ts +9 -6
- package/src/browser-registration.ts +10 -27
- package/src/browser-tool-definition.ts +30 -17
- package/src/browser-tool.ts +3 -6
- package/biome.json +0 -31
- package/changelog.ts +0 -337
package/package.json
CHANGED
|
@@ -1,96 +1,95 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
}
|
|
2
|
+
"name": "@howaboua/pi-browser",
|
|
3
|
+
"version": "0.0.15",
|
|
4
|
+
"description": "Logged-in browser inspection and control for Pi, Code Mode and Notebook Mode.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"author": "Igor Warzocha",
|
|
8
|
+
"homepage": "https://github.com/IgorWarzocha/howaboua-pi-stuff/tree/main/packages/pi-browser#readme",
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "git+https://github.com/IgorWarzocha/howaboua-pi-stuff.git",
|
|
12
|
+
"directory": "packages/pi-browser"
|
|
13
|
+
},
|
|
14
|
+
"bugs": {
|
|
15
|
+
"url": "https://github.com/IgorWarzocha/howaboua-pi-stuff/issues"
|
|
16
|
+
},
|
|
17
|
+
"keywords": [
|
|
18
|
+
"pi-package",
|
|
19
|
+
"pi-extension",
|
|
20
|
+
"pi",
|
|
21
|
+
"browser",
|
|
22
|
+
"chrome",
|
|
23
|
+
"cdp",
|
|
24
|
+
"code-mode"
|
|
25
|
+
],
|
|
26
|
+
"files": [
|
|
27
|
+
"dist/**/*.js",
|
|
28
|
+
"dist/**/*.d.ts",
|
|
29
|
+
"extension.ts",
|
|
30
|
+
"index.ts",
|
|
31
|
+
"remote-worker.ts",
|
|
32
|
+
"src",
|
|
33
|
+
"README.md",
|
|
34
|
+
"LICENSE",
|
|
35
|
+
"CHANGELOG.md",
|
|
36
|
+
"tsconfig.json"
|
|
37
|
+
],
|
|
38
|
+
"scripts": {
|
|
39
|
+
"build": "node -e \"require('node:fs').rmSync('dist', { recursive: true, force: true })\" && tsc -p tsconfig.build.json && bun build remote-worker.ts --target=node --format=esm --outfile=dist/remote-worker.js --banner='// @howaboua/pi-browser managed worker'",
|
|
40
|
+
"check": "bun run lint && bun run typecheck",
|
|
41
|
+
"prepack": "bun run check && bun run build",
|
|
42
|
+
"typecheck": "tsrs --noEmit",
|
|
43
|
+
"lint": "bun run format:check && oxlint --type-aware",
|
|
44
|
+
"pack:dry": "npm pack --dry-run",
|
|
45
|
+
"format:check": "oxfmt --check '**/*.{js,cjs,mjs,jsx,ts,cts,mts,tsx,json,jsonc,json5,css}'"
|
|
46
|
+
},
|
|
47
|
+
"peerDependencies": {
|
|
48
|
+
"@howaboua/pi-codex-conversion": ">=5.1.0",
|
|
49
|
+
"@earendil-works/pi-ai": ">=1.0.0",
|
|
50
|
+
"@earendil-works/pi-coding-agent": ">=1.5.0",
|
|
51
|
+
"@earendil-works/pi-tui": ">=1.0.0"
|
|
52
|
+
},
|
|
53
|
+
"peerDependenciesMeta": {
|
|
54
|
+
"@howaboua/pi-codex-conversion": {
|
|
55
|
+
"optional": true
|
|
56
|
+
}
|
|
57
|
+
},
|
|
58
|
+
"devDependencies": {
|
|
59
|
+
"@howaboua/pi-codex-conversion": "5.1.0",
|
|
60
|
+
"@earendil-works/pi-ai": "1.6.0",
|
|
61
|
+
"@earendil-works/pi-coding-agent": "1.6.0",
|
|
62
|
+
"@types/node": "26.6.6",
|
|
63
|
+
"typescript": "7.0.2",
|
|
64
|
+
"oxfmt": "0.79.0"
|
|
65
|
+
},
|
|
66
|
+
"pi": {
|
|
67
|
+
"extensions": [
|
|
68
|
+
"./extension.ts"
|
|
69
|
+
]
|
|
70
|
+
},
|
|
71
|
+
"engines": {
|
|
72
|
+
"node": ">=22.19.0"
|
|
73
|
+
},
|
|
74
|
+
"bin": {
|
|
75
|
+
"pi-browser-worker": "./dist/remote-worker.js"
|
|
76
|
+
},
|
|
77
|
+
"main": "./dist/index.js",
|
|
78
|
+
"types": "./dist/index.d.ts",
|
|
79
|
+
"exports": {
|
|
80
|
+
"./voice-start": {
|
|
81
|
+
"types": "./dist/src/browser/voice-start.d.ts",
|
|
82
|
+
"import": "./dist/src/browser/voice-start.js"
|
|
83
|
+
},
|
|
84
|
+
".": {
|
|
85
|
+
"types": "./dist/index.d.ts",
|
|
86
|
+
"import": "./dist/index.js"
|
|
87
|
+
}
|
|
88
|
+
},
|
|
89
|
+
"publishConfig": {
|
|
90
|
+
"access": "public"
|
|
91
|
+
},
|
|
92
|
+
"dependencies": {
|
|
93
|
+
"@earendil-works/pi-schema": "1.6.0"
|
|
94
|
+
}
|
|
96
95
|
}
|
package/src/browser/artifacts.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import {
|
|
2
2
|
mkdir,
|
|
3
|
+
open,
|
|
3
4
|
readdir,
|
|
4
5
|
readFile,
|
|
5
6
|
rm,
|
|
@@ -12,6 +13,8 @@ import { join } from "node:path";
|
|
|
12
13
|
const RESULT_TTL_MS = 60 * 60 * 1_000;
|
|
13
14
|
const SCREENSHOT_TTL_MS = 24 * 60 * 60 * 1_000;
|
|
14
15
|
const TEXT_BUDGET_BYTES = 32_000;
|
|
16
|
+
// At least one byte per UTF-16 unit, plus one unit of surrogate lookahead.
|
|
17
|
+
const RESULT_READ_BYTES = 2 * (TEXT_BUDGET_BYTES - 2 + 1);
|
|
15
18
|
|
|
16
19
|
export function artifactDirectory(): string {
|
|
17
20
|
return process.env["XDG_RUNTIME_DIR"]
|
|
@@ -19,8 +22,11 @@ export function artifactDirectory(): string {
|
|
|
19
22
|
: join(tmpdir(), `browser-tool-${process.getuid?.() ?? "user"}`);
|
|
20
23
|
}
|
|
21
24
|
|
|
22
|
-
function resultPath(handle: string): string {
|
|
23
|
-
return join(
|
|
25
|
+
function resultPath(handle: string, legacy = false): string {
|
|
26
|
+
return join(
|
|
27
|
+
artifactDirectory(),
|
|
28
|
+
`result-${handle}.${legacy ? "txt" : "utf16le"}`,
|
|
29
|
+
);
|
|
24
30
|
}
|
|
25
31
|
|
|
26
32
|
export async function screenshotPath(request: {
|
|
@@ -50,19 +56,14 @@ export async function pruneArtifacts(now = Date.now()): Promise<number> {
|
|
|
50
56
|
try {
|
|
51
57
|
names = await readdir(artifactDirectory());
|
|
52
58
|
} catch (error) {
|
|
53
|
-
if (
|
|
54
|
-
error &&
|
|
55
|
-
typeof error === "object" &&
|
|
56
|
-
"code" in error &&
|
|
57
|
-
error.code === "ENOENT"
|
|
58
|
-
) {
|
|
59
|
-
return 0;
|
|
60
|
-
}
|
|
59
|
+
if (isMissing(error)) return 0;
|
|
61
60
|
throw error;
|
|
62
61
|
}
|
|
63
62
|
let removed = 0;
|
|
64
63
|
for (const name of names.filter((candidate) =>
|
|
65
|
-
/^(?:result-[a-f0-9-]{36}\.txt|browser-.*\.png)$/.test(
|
|
64
|
+
/^(?:result-[a-f0-9-]{36}\.(?:txt|utf16le)|browser-.*\.png)$/.test(
|
|
65
|
+
candidate,
|
|
66
|
+
),
|
|
66
67
|
)) {
|
|
67
68
|
const path = join(artifactDirectory(), name);
|
|
68
69
|
try {
|
|
@@ -74,14 +75,7 @@ export async function pruneArtifacts(now = Date.now()): Promise<number> {
|
|
|
74
75
|
removed++;
|
|
75
76
|
}
|
|
76
77
|
} catch (error) {
|
|
77
|
-
if (
|
|
78
|
-
!error ||
|
|
79
|
-
typeof error !== "object" ||
|
|
80
|
-
!("code" in error) ||
|
|
81
|
-
error.code !== "ENOENT"
|
|
82
|
-
) {
|
|
83
|
-
throw error;
|
|
84
|
-
}
|
|
78
|
+
if (!isMissing(error)) throw error;
|
|
85
79
|
}
|
|
86
80
|
}
|
|
87
81
|
return removed;
|
|
@@ -90,26 +84,47 @@ export async function pruneArtifacts(now = Date.now()): Promise<number> {
|
|
|
90
84
|
function chunkForJson(
|
|
91
85
|
value: string,
|
|
92
86
|
offset = 0,
|
|
93
|
-
budget = TEXT_BUDGET_BYTES,
|
|
94
87
|
): { text: string; end: number } {
|
|
95
88
|
let bytes = 2;
|
|
96
89
|
let end = offset;
|
|
97
|
-
|
|
98
|
-
const
|
|
99
|
-
|
|
90
|
+
while (end < value.length) {
|
|
91
|
+
const point = value.codePointAt(end)!;
|
|
92
|
+
const next = jsonCharacterBytes(point);
|
|
93
|
+
if (bytes + next > TEXT_BUDGET_BYTES) break;
|
|
100
94
|
bytes += next;
|
|
101
|
-
end +=
|
|
95
|
+
end += point > 0xffff ? 2 : 1;
|
|
102
96
|
}
|
|
103
97
|
return { text: value.slice(offset, end), end };
|
|
104
98
|
}
|
|
105
99
|
|
|
100
|
+
function jsonCharacterBytes(point: number): number {
|
|
101
|
+
if (point < 0x20) {
|
|
102
|
+
return point === 8 ||
|
|
103
|
+
point === 9 ||
|
|
104
|
+
point === 10 ||
|
|
105
|
+
point === 12 ||
|
|
106
|
+
point === 13
|
|
107
|
+
? 2
|
|
108
|
+
: 6;
|
|
109
|
+
}
|
|
110
|
+
if (point === 0x22 || point === 0x5c) return 2;
|
|
111
|
+
if (point < 0x80) return 1;
|
|
112
|
+
if (point < 0x800) return 2;
|
|
113
|
+
if (point >= 0xd800 && point <= 0xdfff) return 6;
|
|
114
|
+
return point <= 0xffff ? 3 : 4;
|
|
115
|
+
}
|
|
116
|
+
|
|
106
117
|
async function cacheResult(value: string): Promise<string> {
|
|
107
118
|
const handle = crypto.randomUUID();
|
|
108
119
|
await mkdir(artifactDirectory(), {
|
|
109
120
|
recursive: true,
|
|
110
121
|
mode: 0o700,
|
|
111
122
|
});
|
|
112
|
-
|
|
123
|
+
// Fixed-width code units allow seeks at every accepted JS string offset.
|
|
124
|
+
await writeFile(resultPath(handle), value, {
|
|
125
|
+
encoding: "utf16le",
|
|
126
|
+
mode: 0o600,
|
|
127
|
+
});
|
|
113
128
|
return handle;
|
|
114
129
|
}
|
|
115
130
|
|
|
@@ -131,48 +146,99 @@ export async function limitedText(
|
|
|
131
146
|
};
|
|
132
147
|
}
|
|
133
148
|
|
|
149
|
+
function checkOffset(offset: number, length: number): void {
|
|
150
|
+
if (offset > length) {
|
|
151
|
+
throw new Error(`offset ${offset} exceeds result length ${length}`);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
async function readResultPage(path: string, offset: number) {
|
|
156
|
+
const file = await open(path, "r");
|
|
157
|
+
try {
|
|
158
|
+
const { size } = await file.stat();
|
|
159
|
+
if (size % 2)
|
|
160
|
+
throw new Error("Cached result is incomplete; rerun the original action");
|
|
161
|
+
const length = size / 2;
|
|
162
|
+
checkOffset(offset, length);
|
|
163
|
+
const position = offset * 2;
|
|
164
|
+
const bytes = Buffer.allocUnsafe(
|
|
165
|
+
Math.min(size - position, RESULT_READ_BYTES),
|
|
166
|
+
);
|
|
167
|
+
let used = 0;
|
|
168
|
+
while (used < bytes.length) {
|
|
169
|
+
const { bytesRead } = await file.read(
|
|
170
|
+
bytes,
|
|
171
|
+
used,
|
|
172
|
+
bytes.length - used,
|
|
173
|
+
position + used,
|
|
174
|
+
);
|
|
175
|
+
if (!bytesRead)
|
|
176
|
+
throw new Error(
|
|
177
|
+
"Cached result is incomplete; rerun the original action",
|
|
178
|
+
);
|
|
179
|
+
used += bytesRead;
|
|
180
|
+
}
|
|
181
|
+
const chunk = chunkForJson(bytes.toString("utf16le"));
|
|
182
|
+
return { text: chunk.text, end: offset + chunk.end, length };
|
|
183
|
+
} finally {
|
|
184
|
+
await file.close();
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
function isMissing(error: unknown): boolean {
|
|
189
|
+
return (
|
|
190
|
+
!!error &&
|
|
191
|
+
typeof error === "object" &&
|
|
192
|
+
"code" in error &&
|
|
193
|
+
error.code === "ENOENT"
|
|
194
|
+
);
|
|
195
|
+
}
|
|
196
|
+
|
|
134
197
|
export async function readCachedResult(request: {
|
|
135
198
|
handle: string;
|
|
136
199
|
offset: number;
|
|
137
200
|
}): Promise<Record<string, unknown>> {
|
|
138
|
-
|
|
201
|
+
const { handle, offset } = request;
|
|
202
|
+
let path = resultPath(handle);
|
|
203
|
+
let page;
|
|
139
204
|
try {
|
|
140
|
-
|
|
205
|
+
page = await readResultPage(path, offset);
|
|
141
206
|
} catch (error) {
|
|
142
|
-
if (
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
207
|
+
if (!isMissing(error)) throw error;
|
|
208
|
+
// Let handles issued by the previous worker finish during their short TTL.
|
|
209
|
+
path = resultPath(handle, true);
|
|
210
|
+
let value: string;
|
|
211
|
+
try {
|
|
212
|
+
value = await readFile(path, "utf8");
|
|
213
|
+
} catch (legacyError) {
|
|
214
|
+
if (!isMissing(legacyError)) throw legacyError;
|
|
148
215
|
throw new Error(
|
|
149
|
-
`result handle not found: ${
|
|
216
|
+
`result handle not found: ${handle}; check the host or rerun the original action`,
|
|
150
217
|
);
|
|
151
218
|
}
|
|
152
|
-
|
|
219
|
+
checkOffset(offset, value.length);
|
|
220
|
+
page = { ...chunkForJson(value, offset), length: value.length };
|
|
153
221
|
}
|
|
154
|
-
|
|
155
|
-
throw new Error(
|
|
156
|
-
`offset ${request.offset} exceeds result length ${value.length}`,
|
|
157
|
-
);
|
|
158
|
-
}
|
|
159
|
-
const chunk = chunkForJson(value, request.offset);
|
|
160
|
-
const complete = chunk.end >= value.length;
|
|
222
|
+
const complete = page.end >= page.length;
|
|
161
223
|
if (complete) {
|
|
162
|
-
await rm(
|
|
224
|
+
await rm(path, { force: true });
|
|
163
225
|
}
|
|
164
226
|
return {
|
|
165
|
-
handle
|
|
166
|
-
offset
|
|
167
|
-
text:
|
|
227
|
+
handle,
|
|
228
|
+
offset,
|
|
229
|
+
text: page.text,
|
|
168
230
|
complete,
|
|
169
|
-
...(complete ? {} : { next_offset:
|
|
231
|
+
...(complete ? {} : { next_offset: page.end }),
|
|
170
232
|
};
|
|
171
233
|
}
|
|
172
234
|
|
|
173
235
|
export async function discardCachedResult(
|
|
174
236
|
handle: string,
|
|
175
237
|
): Promise<Record<string, unknown>> {
|
|
176
|
-
await
|
|
238
|
+
await Promise.all(
|
|
239
|
+
[resultPath(handle), resultPath(handle, true)].map((path) =>
|
|
240
|
+
rm(path, { force: true }),
|
|
241
|
+
),
|
|
242
|
+
);
|
|
177
243
|
return { discarded: handle };
|
|
178
244
|
}
|
package/src/browser/help.ts
CHANGED
|
@@ -1,24 +1,19 @@
|
|
|
1
1
|
export function browserHelp(
|
|
2
2
|
hosts: readonly string[] = [],
|
|
3
|
-
gpt = false,
|
|
4
3
|
): Record<string, unknown> {
|
|
5
4
|
const routed = hosts.length > 0;
|
|
6
5
|
return {
|
|
7
6
|
input: "{action,...fields}",
|
|
8
7
|
...(routed
|
|
9
8
|
? {
|
|
10
|
-
host: `${hosts.join("|")}
|
|
9
|
+
host: `${hosts.join("|")} optional; user-named host→all calls; refs/handles stay with host`,
|
|
11
10
|
}
|
|
12
11
|
: {}),
|
|
13
|
-
refs:
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
: "Ask before unfamiliar low-trust navigation or consequential action unless authorized; never close shared browser",
|
|
19
|
-
batch: gpt
|
|
20
|
-
? "{action_name:[{...fields}],...}; nonempty arrays; omit item action; root host/response_length; independent ONLY"
|
|
21
|
-
: "{action_name:[{...fields}],...}; nonempty arrays; items omit action; host/response_length at request root; independent only",
|
|
12
|
+
refs: "tabs→open; ref_id/id/cursors stay with result",
|
|
13
|
+
safety:
|
|
14
|
+
"MUST ask before unfamiliar low-trust navigation or consequential action unless authorised. MUST NOT close shared browser",
|
|
15
|
+
batch:
|
|
16
|
+
"Independent only: {action_name:[{...fields}],...}; nonempty arrays; root host/response_length; omit item action",
|
|
22
17
|
actions: {
|
|
23
18
|
help: "",
|
|
24
19
|
tabs: "query? offset? owned_only? -> ref_id title url owned",
|
package/src/browser/settings.ts
CHANGED
|
@@ -111,7 +111,10 @@ async function manageAdvanced(
|
|
|
111
111
|
]);
|
|
112
112
|
if (!selected || selected === BACK) return config;
|
|
113
113
|
const value = (
|
|
114
|
-
await ctx.ui.input(
|
|
114
|
+
await ctx.ui.input(
|
|
115
|
+
"Node.js executable on SSH hosts",
|
|
116
|
+
config.remoteNodePath,
|
|
117
|
+
)
|
|
115
118
|
)?.trim();
|
|
116
119
|
if (!value) continue;
|
|
117
120
|
try {
|
|
@@ -150,7 +153,7 @@ async function openBrowserSettings(
|
|
|
150
153
|
if (!config) return;
|
|
151
154
|
while (true) {
|
|
152
155
|
const hosts = `Hosts · ${config.hosts.length ? config.hosts.join(", ") : "local only"}`;
|
|
153
|
-
const machine = `
|
|
156
|
+
const machine = `Local host name for routing · ${currentMachineRoute(config) ?? "unnamed local"}`;
|
|
154
157
|
const selected = await ctx.ui.select("Browser settings", [
|
|
155
158
|
hosts,
|
|
156
159
|
machine,
|
|
@@ -164,10 +167,10 @@ async function openBrowserSettings(
|
|
|
164
167
|
continue;
|
|
165
168
|
}
|
|
166
169
|
if (selected === machine) {
|
|
167
|
-
const route = await ctx.ui.select(
|
|
168
|
-
"
|
|
169
|
-
...config.hosts,
|
|
170
|
-
|
|
170
|
+
const route = await ctx.ui.select(
|
|
171
|
+
"Which listed host should open locally instead of over SSH?",
|
|
172
|
+
["Unnamed local browser", ...config.hosts],
|
|
173
|
+
);
|
|
171
174
|
if (route) {
|
|
172
175
|
config = setCurrentMachineRoute(
|
|
173
176
|
config,
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { defineTool, ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
2
|
-
import type {
|
|
2
|
+
import type { obj, opt, str } from "@earendil-works/pi-schema";
|
|
3
3
|
import { BrowserRoutes, loadBrowserRoutes } from "./browser/routes.js";
|
|
4
4
|
import { BrowserRuntime } from "./browser/runtime.js";
|
|
5
5
|
import { registerBrowserCommand } from "./browser/settings.js";
|
|
@@ -13,7 +13,12 @@ const CODE_MODE_MODULE = `${CODE_MODE_PACKAGE}/code-mode`;
|
|
|
13
13
|
|
|
14
14
|
export default async function browserExtension(
|
|
15
15
|
pi: ExtensionAPI,
|
|
16
|
-
sdk: {
|
|
16
|
+
sdk: {
|
|
17
|
+
defineTool: typeof defineTool;
|
|
18
|
+
obj: typeof obj;
|
|
19
|
+
opt: typeof opt;
|
|
20
|
+
str: typeof str;
|
|
21
|
+
},
|
|
17
22
|
): Promise<void> {
|
|
18
23
|
let configError: string | undefined;
|
|
19
24
|
let routes: BrowserRoutes;
|
|
@@ -24,31 +29,9 @@ export default async function browserExtension(
|
|
|
24
29
|
configError = error instanceof Error ? error.message : String(error);
|
|
25
30
|
}
|
|
26
31
|
const runtime = new BrowserRuntime(routes);
|
|
27
|
-
|
|
28
|
-
const tool = createBrowserTool(sdk, runtime, () => gpt);
|
|
32
|
+
const tool = createBrowserTool(sdk, runtime);
|
|
29
33
|
registerBrowserCommand(pi);
|
|
30
34
|
pi.registerTool(tool);
|
|
31
|
-
try {
|
|
32
|
-
const { registerGptPromptMode } = await import(
|
|
33
|
-
"@howaboua/pi-codex-conversion/prompt-mode"
|
|
34
|
-
);
|
|
35
|
-
registerGptPromptMode(pi, (active) => {
|
|
36
|
-
gpt = active;
|
|
37
|
-
return [tool];
|
|
38
|
-
});
|
|
39
|
-
} catch (error) {
|
|
40
|
-
if (
|
|
41
|
-
!(error instanceof Error) ||
|
|
42
|
-
!("code" in error) ||
|
|
43
|
-
![
|
|
44
|
-
"ERR_MODULE_NOT_FOUND",
|
|
45
|
-
"MODULE_NOT_FOUND",
|
|
46
|
-
"ERR_PACKAGE_PATH_NOT_EXPORTED",
|
|
47
|
-
].includes(String(error.code)) ||
|
|
48
|
-
!error.message.includes(CODE_MODE_PACKAGE)
|
|
49
|
-
)
|
|
50
|
-
throw error;
|
|
51
|
-
}
|
|
52
35
|
|
|
53
36
|
const registration = await registerBrowserInCodeMode(pi, tool, sdk);
|
|
54
37
|
pi.on("session_shutdown", async () => {
|
|
@@ -65,7 +48,7 @@ export default async function browserExtension(
|
|
|
65
48
|
async function registerBrowserInCodeMode(
|
|
66
49
|
pi: ExtensionAPI,
|
|
67
50
|
tool: ReturnType<typeof createBrowserTool>,
|
|
68
|
-
sdk: {
|
|
51
|
+
sdk: { obj: typeof obj },
|
|
69
52
|
) {
|
|
70
53
|
try {
|
|
71
54
|
const { adaptToolForCodeMode, registerCodeModeExtensionTools } =
|
|
@@ -74,7 +57,7 @@ async function registerBrowserInCodeMode(
|
|
|
74
57
|
adaptToolForCodeMode(
|
|
75
58
|
{
|
|
76
59
|
...tool,
|
|
77
|
-
parameters: sdk.
|
|
60
|
+
parameters: sdk.obj({}, { open: true }),
|
|
78
61
|
},
|
|
79
62
|
{
|
|
80
63
|
prepareInput: prepareBrowserInput,
|
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
import type { defineTool as definePiTool } from "@earendil-works/pi-coding-agent";
|
|
2
|
-
import type {
|
|
3
|
-
|
|
2
|
+
import type {
|
|
3
|
+
Static,
|
|
4
|
+
obj as schemaObj,
|
|
5
|
+
opt as schemaOpt,
|
|
6
|
+
str as schemaStr,
|
|
7
|
+
} from "@earendil-works/pi-schema";
|
|
4
8
|
import { isRecordValue } from "./browser/parse-operation.js";
|
|
5
9
|
import { parseBrowserRequest } from "./browser/request.js";
|
|
6
10
|
import type { BrowserRuntime } from "./browser/runtime.js";
|
|
@@ -27,27 +31,38 @@ export function prepareBrowserInput(input: unknown): { command: string } {
|
|
|
27
31
|
export function createBrowserTool(
|
|
28
32
|
{
|
|
29
33
|
defineTool,
|
|
30
|
-
|
|
31
|
-
|
|
34
|
+
obj,
|
|
35
|
+
opt,
|
|
36
|
+
str,
|
|
37
|
+
}: {
|
|
38
|
+
defineTool: typeof definePiTool;
|
|
39
|
+
obj: typeof schemaObj;
|
|
40
|
+
opt: typeof schemaOpt;
|
|
41
|
+
str: typeof schemaStr;
|
|
42
|
+
},
|
|
32
43
|
runtime: BrowserRuntime,
|
|
33
|
-
isGpt = () => false,
|
|
34
44
|
) {
|
|
45
|
+
const parameters = obj(
|
|
46
|
+
{
|
|
47
|
+
command: opt(str({ description: "help or JSON request" })),
|
|
48
|
+
},
|
|
49
|
+
{ open: false },
|
|
50
|
+
);
|
|
35
51
|
return defineTool({
|
|
36
52
|
name: "browser",
|
|
37
53
|
label: "Browser",
|
|
38
54
|
description: "Control logged-in browser",
|
|
39
|
-
parameters
|
|
40
|
-
{
|
|
41
|
-
command: Type.Optional(
|
|
42
|
-
Type.String({ description: "help or JSON request" }),
|
|
43
|
-
),
|
|
44
|
-
},
|
|
45
|
-
{ additionalProperties: false },
|
|
46
|
-
),
|
|
55
|
+
parameters,
|
|
47
56
|
prepareArguments: prepareBrowserInput,
|
|
48
|
-
async execute(
|
|
57
|
+
async execute(
|
|
58
|
+
_toolCallId,
|
|
59
|
+
input: Static<typeof parameters>,
|
|
60
|
+
signal,
|
|
61
|
+
onUpdate,
|
|
62
|
+
ctx,
|
|
63
|
+
) {
|
|
49
64
|
const request = parseBrowserRequest(input.command);
|
|
50
|
-
|
|
65
|
+
const result = await runtime.execute(request, {
|
|
51
66
|
ownerId: ctx.sessionManager.getSessionId(),
|
|
52
67
|
signal: signal ?? new AbortController().signal,
|
|
53
68
|
onOperation(operation, index, total) {
|
|
@@ -70,8 +85,6 @@ export function createBrowserTool(
|
|
|
70
85
|
});
|
|
71
86
|
},
|
|
72
87
|
});
|
|
73
|
-
if (isGpt() && "help" in request)
|
|
74
|
-
result = browserHelp(runtime.hosts, true);
|
|
75
88
|
return {
|
|
76
89
|
content: [{ type: "text", text: JSON.stringify(result) }],
|
|
77
90
|
details: result,
|
package/src/browser-tool.ts
CHANGED
|
@@ -1,11 +1,8 @@
|
|
|
1
1
|
import { defineTool } from "@earendil-works/pi-coding-agent";
|
|
2
|
-
import {
|
|
2
|
+
import { obj, opt, str } from "@earendil-works/pi-schema";
|
|
3
3
|
import type { BrowserRuntime } from "./browser/runtime.js";
|
|
4
4
|
import { createBrowserTool as createTool } from "./browser-tool-definition.js";
|
|
5
5
|
|
|
6
|
-
export function createBrowserTool(
|
|
7
|
-
|
|
8
|
-
isGpt = () => false,
|
|
9
|
-
) {
|
|
10
|
-
return createTool({ defineTool, Type }, runtime, isGpt);
|
|
6
|
+
export function createBrowserTool(runtime: BrowserRuntime) {
|
|
7
|
+
return createTool({ defineTool, obj, opt, str }, runtime);
|
|
11
8
|
}
|