@howaboua/pi-browser 0.0.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/CHANGELOG.md +9 -0
- package/LICENSE +22 -0
- package/README.md +47 -0
- package/biome.json +31 -0
- package/changelog.ts +337 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +82 -0
- package/dist/remote-worker.d.ts +2 -0
- package/dist/remote-worker.js +3005 -0
- package/dist/src/browser/artifacts.d.ts +13 -0
- package/dist/src/browser/artifacts.js +133 -0
- package/dist/src/browser/bounds.d.ts +4 -0
- package/dist/src/browser/bounds.js +129 -0
- package/dist/src/browser/config.d.ts +11 -0
- package/dist/src/browser/config.js +106 -0
- package/dist/src/browser/help.d.ts +1 -0
- package/dist/src/browser/help.js +27 -0
- package/dist/src/browser/launcher.d.ts +8 -0
- package/dist/src/browser/launcher.js +169 -0
- package/dist/src/browser/operation-executor.d.ts +9 -0
- package/dist/src/browser/operation-executor.js +112 -0
- package/dist/src/browser/operation.d.ts +92 -0
- package/dist/src/browser/operation.js +20 -0
- package/dist/src/browser/parse-operation.d.ts +6 -0
- package/dist/src/browser/parse-operation.js +248 -0
- package/dist/src/browser/remote-helper.d.ts +3 -0
- package/dist/src/browser/remote-helper.js +114 -0
- package/dist/src/browser/remote-process.d.ts +7 -0
- package/dist/src/browser/remote-process.js +78 -0
- package/dist/src/browser/remote.d.ts +4 -0
- package/dist/src/browser/remote.js +73 -0
- package/dist/src/browser/request.d.ts +8 -0
- package/dist/src/browser/request.js +80 -0
- package/dist/src/browser/routes.d.ts +16 -0
- package/dist/src/browser/routes.js +40 -0
- package/dist/src/browser/runtime.d.ts +17 -0
- package/dist/src/browser/runtime.js +61 -0
- package/dist/src/browser/settings.d.ts +2 -0
- package/dist/src/browser/settings.js +164 -0
- package/dist/src/browser/worker-server.d.ts +2 -0
- package/dist/src/browser/worker-server.js +241 -0
- package/dist/src/browser-parameters.d.ts +42 -0
- package/dist/src/browser-parameters.js +113 -0
- package/dist/src/browser-tool.d.ts +48 -0
- package/dist/src/browser-tool.js +71 -0
- package/dist/src/cdp/actions/click.d.ts +4 -0
- package/dist/src/cdp/actions/click.js +56 -0
- package/dist/src/cdp/actions/element.d.ts +22 -0
- package/dist/src/cdp/actions/element.js +126 -0
- package/dist/src/cdp/actions/page.d.ts +7 -0
- package/dist/src/cdp/actions/page.js +127 -0
- package/dist/src/cdp/actions/screenshot.d.ts +10 -0
- package/dist/src/cdp/actions/screenshot.js +96 -0
- package/dist/src/cdp/actions/selector.d.ts +2 -0
- package/dist/src/cdp/actions/selector.js +41 -0
- package/dist/src/cdp/actions/type.d.ts +3 -0
- package/dist/src/cdp/actions/type.js +138 -0
- package/dist/src/cdp/client.d.ts +20 -0
- package/dist/src/cdp/client.js +248 -0
- package/dist/src/cdp/discovery.d.ts +7 -0
- package/dist/src/cdp/discovery.js +153 -0
- package/dist/src/cdp/evaluate.d.ts +3 -0
- package/dist/src/cdp/evaluate.js +31 -0
- package/dist/src/cdp/pages.d.ts +3 -0
- package/dist/src/cdp/pages.js +40 -0
- package/dist/src/cdp/serial.d.ts +1 -0
- package/dist/src/cdp/serial.js +18 -0
- package/dist/src/cdp/session.d.ts +25 -0
- package/dist/src/cdp/session.js +246 -0
- package/dist/src/cdp/snapshot-contract.d.ts +32 -0
- package/dist/src/cdp/snapshot-contract.js +23 -0
- package/dist/src/cdp/snapshot.d.ts +3 -0
- package/dist/src/cdp/snapshot.js +205 -0
- package/dist/src/cdp/types.d.ts +20 -0
- package/dist/src/cdp/types.js +14 -0
- package/dist/src/cdp/url.d.ts +1 -0
- package/dist/src/cdp/url.js +12 -0
- package/index.ts +115 -0
- package/package.json +93 -0
- package/remote-worker.ts +58 -0
- package/src/browser/artifacts.ts +178 -0
- package/src/browser/bounds.ts +159 -0
- package/src/browser/config.ts +162 -0
- package/src/browser/help.ts +30 -0
- package/src/browser/launcher.ts +208 -0
- package/src/browser/operation-executor.ts +251 -0
- package/src/browser/operation.ts +105 -0
- package/src/browser/parse-operation.ts +272 -0
- package/src/browser/remote-helper.ts +137 -0
- package/src/browser/remote-process.ts +85 -0
- package/src/browser/remote.ts +120 -0
- package/src/browser/request.ts +95 -0
- package/src/browser/routes.ts +69 -0
- package/src/browser/runtime.ts +84 -0
- package/src/browser/settings.ts +219 -0
- package/src/browser/worker-server.ts +274 -0
- package/src/browser-parameters.ts +118 -0
- package/src/browser-tool.ts +88 -0
- package/src/cdp/actions/click.ts +126 -0
- package/src/cdp/actions/element.ts +234 -0
- package/src/cdp/actions/page.ts +219 -0
- package/src/cdp/actions/screenshot.ts +176 -0
- package/src/cdp/actions/selector.ts +63 -0
- package/src/cdp/actions/type.ts +224 -0
- package/src/cdp/client.ts +286 -0
- package/src/cdp/discovery.ts +198 -0
- package/src/cdp/evaluate.ts +55 -0
- package/src/cdp/pages.ts +60 -0
- package/src/cdp/serial.ts +23 -0
- package/src/cdp/session.ts +345 -0
- package/src/cdp/snapshot-contract.ts +63 -0
- package/src/cdp/snapshot.ts +273 -0
- package/src/cdp/types.ts +49 -0
- package/src/cdp/url.ts +11 -0
- package/tsconfig.json +7 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
# @howaboua/pi-browser
|
|
2
|
+
|
|
3
|
+
## 0.0.1
|
|
4
|
+
|
|
5
|
+
- Initial release of Browser, a persistent typed CDP tool for normal Pi, Code Mode, and Notebook Mode.
|
|
6
|
+
|
|
7
|
+
- Inspect existing browser tabs, follow references, find text, click, type, capture screenshots, and use raw CDP when needed.
|
|
8
|
+
- Connect to local or configured remote hosts, with aliases and worker setup managed through `/browser`.
|
|
9
|
+
|
package/LICENSE
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 pasky
|
|
4
|
+
Copyright (c) 2026 Igor Warzocha
|
|
5
|
+
|
|
6
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
in the Software without restriction, including without limitation the rights
|
|
9
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
furnished to do so, subject to the following conditions:
|
|
12
|
+
|
|
13
|
+
The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
copies or substantial portions of the Software.
|
|
15
|
+
|
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
# pi-browser
|
|
2
|
+
|
|
3
|
+
One logged-in browser tool for ordinary Pi, Code Mode and Notebook Mode.
|
|
4
|
+
|
|
5
|
+
The extension is the TypeScript counterpart of Pi Codex's browser custom tool. It keeps a persistent typed CDP session for the local browser while preserving the same help-first actions, SSH host routing, batching, accessibility references, bounded continuations and screenshot paths.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
pi install npm:@howaboua/pi-browser
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
Requires Node.js 22.19 or newer and a Chrome-family browser with remote debugging enabled at `chrome://inspect/#remote-debugging`.
|
|
14
|
+
|
|
15
|
+
Pi Codex 3.0.24 or newer is optional. Without it, Browser remains a normal top-level Pi tool. Do not load Pi Codex's example `browser.toml` custom tool alongside this extension.
|
|
16
|
+
|
|
17
|
+
## Use
|
|
18
|
+
|
|
19
|
+
In normal Pi, call `browser` with `action: "help"` before first use. In Code or Notebook Mode, start with:
|
|
20
|
+
|
|
21
|
+
```js
|
|
22
|
+
await tools.browser("help")
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
Normal Pi exposes the single-action contract. Code and Notebook Mode also accept batched freeform requests. A common route is `tabs`, then `open`, then `click` or `type` with the returned `ref_id` and element ID.
|
|
26
|
+
|
|
27
|
+
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.
|
|
28
|
+
|
|
29
|
+
## Host routing
|
|
30
|
+
|
|
31
|
+
In Pi's interactive TUI, run `/browser`, add the SSH host names, identify the current machine, then save. Pi reloads the extension with the corresponding `host` choices. Advanced settings expose the remote Node command, which defaults to `node`.
|
|
32
|
+
|
|
33
|
+
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.
|
|
34
|
+
|
|
35
|
+
## Browser startup
|
|
36
|
+
|
|
37
|
+
The tool discovers CDP through `CDP_PORT`, port 9222, or `DevToolsActivePort`. Set `CDP_PORT_FILE` for a non-standard port file.
|
|
38
|
+
|
|
39
|
+
The `start` action runs on the selected host and can launch Chromium through a Linux systemd user session. Override the executable with `CDP_BROWSER` and the profile with `CDP_PROFILE_DIRECTORY`. On other systems, start the browser normally with remote debugging enabled.
|
|
40
|
+
|
|
41
|
+
## Boundaries
|
|
42
|
+
|
|
43
|
+
Keep each result's `ref_id` and element IDs together. Continue truncated output with the returned line, offset or result handle.
|
|
44
|
+
|
|
45
|
+
Ask before unfamiliar low-trust navigation or consequential external actions such as sending, posting, purchasing, uploading, deleting or changing account settings, unless the user already authorized the action. Never close a shared browser after a task.
|
|
46
|
+
|
|
47
|
+
The CDP implementation is based on [pasky/chrome-cdp-skill](https://github.com/pasky/chrome-cdp-skill).
|
package/biome.json
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "https://biomejs.dev/schemas/2.5.10/schema.json",
|
|
3
|
+
"vcs": {
|
|
4
|
+
"enabled": true,
|
|
5
|
+
"clientKind": "git",
|
|
6
|
+
"useIgnoreFile": false
|
|
7
|
+
},
|
|
8
|
+
"files": {
|
|
9
|
+
"includes": [
|
|
10
|
+
"**",
|
|
11
|
+
"!node_modules",
|
|
12
|
+
"!dist",
|
|
13
|
+
"!coverage",
|
|
14
|
+
"!package-lock.json"
|
|
15
|
+
]
|
|
16
|
+
},
|
|
17
|
+
"formatter": {
|
|
18
|
+
"enabled": true,
|
|
19
|
+
"indentStyle": "tab"
|
|
20
|
+
},
|
|
21
|
+
"linter": {
|
|
22
|
+
"enabled": false
|
|
23
|
+
},
|
|
24
|
+
"javascript": {
|
|
25
|
+
"formatter": {
|
|
26
|
+
"quoteStyle": "double",
|
|
27
|
+
"semicolons": "always",
|
|
28
|
+
"trailingCommas": "all"
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
}
|
package/changelog.ts
ADDED
|
@@ -0,0 +1,337 @@
|
|
|
1
|
+
// Generated extension packages copy this file during changelog:extensions.
|
|
2
|
+
|
|
3
|
+
import { randomUUID } from "node:crypto";
|
|
4
|
+
import { readFileSync } from "node:fs";
|
|
5
|
+
import { mkdir, readFile, rename, rm, stat, writeFile } from "node:fs/promises";
|
|
6
|
+
import { dirname, join } from "node:path";
|
|
7
|
+
import { fileURLToPath } from "node:url";
|
|
8
|
+
import {
|
|
9
|
+
DynamicBorder,
|
|
10
|
+
type ExtensionAPI,
|
|
11
|
+
getAgentDir,
|
|
12
|
+
getMarkdownTheme,
|
|
13
|
+
} from "@earendil-works/pi-coding-agent";
|
|
14
|
+
import { Container, Markdown, Spacer, Text } from "@earendil-works/pi-tui";
|
|
15
|
+
|
|
16
|
+
const ENTRY_TYPE = "@howaboua/pi-stuff/changelog";
|
|
17
|
+
const REGISTRATION_CHANNEL = "@howaboua/pi-stuff/changelog/register/v1";
|
|
18
|
+
const STATE_FILENAME = "howaboua-pi-stuff-changelog.json";
|
|
19
|
+
const SUPPRESS_KEY = "suppress";
|
|
20
|
+
const LOCK_STALE_MS = 30_000;
|
|
21
|
+
const LOCK_TIMEOUT_MS = 2_000;
|
|
22
|
+
|
|
23
|
+
interface PackageMetadata {
|
|
24
|
+
name: string;
|
|
25
|
+
version: string;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
interface PackageRegistration extends PackageMetadata {
|
|
29
|
+
changelogPath: string;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
interface RegistrationEvent {
|
|
33
|
+
accept: () => void;
|
|
34
|
+
registration: PackageRegistration;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
interface ChangelogEntry {
|
|
38
|
+
content: string;
|
|
39
|
+
version: string;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
interface ChangelogEntryData {
|
|
43
|
+
markdown: string;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
type ChangelogState = Record<string, boolean | string>;
|
|
47
|
+
|
|
48
|
+
function packageRegistration(): PackageRegistration {
|
|
49
|
+
const packageDirectory = fileURLToPath(new URL(".", import.meta.url));
|
|
50
|
+
const metadata = JSON.parse(
|
|
51
|
+
readFileSync(join(packageDirectory, "package.json"), "utf8"),
|
|
52
|
+
) as Partial<PackageMetadata>;
|
|
53
|
+
if (typeof metadata.name !== "string" || typeof metadata.version !== "string")
|
|
54
|
+
throw new Error(`Invalid package metadata in ${packageDirectory}`);
|
|
55
|
+
return {
|
|
56
|
+
name: metadata.name,
|
|
57
|
+
version: metadata.version,
|
|
58
|
+
changelogPath: join(packageDirectory, "CHANGELOG.md"),
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function isRegistrationEvent(value: unknown): value is RegistrationEvent {
|
|
63
|
+
if (!value || typeof value !== "object") return false;
|
|
64
|
+
const event = value as Partial<RegistrationEvent>;
|
|
65
|
+
return (
|
|
66
|
+
typeof event.accept === "function" &&
|
|
67
|
+
typeof event.registration?.name === "string" &&
|
|
68
|
+
typeof event.registration.version === "string" &&
|
|
69
|
+
typeof event.registration.changelogPath === "string"
|
|
70
|
+
);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function parseChangelog(markdown: string): ChangelogEntry[] {
|
|
74
|
+
const entries: ChangelogEntry[] = [];
|
|
75
|
+
let currentLines: string[] = [];
|
|
76
|
+
let currentVersion: string | undefined;
|
|
77
|
+
|
|
78
|
+
for (const line of markdown.split("\n")) {
|
|
79
|
+
const version = line.match(/^##\s+\[?(\d+\.\d+\.\d+)\]?(?:\s.*)?$/)?.[1];
|
|
80
|
+
if (version) {
|
|
81
|
+
if (currentVersion)
|
|
82
|
+
entries.push({
|
|
83
|
+
content: currentLines.join("\n").trim(),
|
|
84
|
+
version: currentVersion,
|
|
85
|
+
});
|
|
86
|
+
currentVersion = version;
|
|
87
|
+
currentLines = [line];
|
|
88
|
+
} else if (currentVersion) {
|
|
89
|
+
currentLines.push(line);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
if (currentVersion)
|
|
94
|
+
entries.push({
|
|
95
|
+
content: currentLines.join("\n").trim(),
|
|
96
|
+
version: currentVersion,
|
|
97
|
+
});
|
|
98
|
+
return entries;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function compareVersions(left: string, right: string): number {
|
|
102
|
+
const leftParts = left.split(".").map(Number);
|
|
103
|
+
const rightParts = right.split(".").map(Number);
|
|
104
|
+
for (let index = 0; index < 3; index++) {
|
|
105
|
+
const difference = (leftParts[index] ?? 0) - (rightParts[index] ?? 0);
|
|
106
|
+
if (difference !== 0) return difference;
|
|
107
|
+
}
|
|
108
|
+
return 0;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function isStableVersion(version: string): boolean {
|
|
112
|
+
return /^\d+\.\d+\.\d+$/.test(version);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function statePath(): string {
|
|
116
|
+
return join(getAgentDir(), STATE_FILENAME);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
async function readState(path: string): Promise<ChangelogState> {
|
|
120
|
+
let text: string;
|
|
121
|
+
try {
|
|
122
|
+
text = await readFile(path, "utf8");
|
|
123
|
+
} catch (error) {
|
|
124
|
+
if ((error as NodeJS.ErrnoException).code === "ENOENT") return {};
|
|
125
|
+
throw error;
|
|
126
|
+
}
|
|
127
|
+
const parsed = JSON.parse(text) as unknown;
|
|
128
|
+
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed))
|
|
129
|
+
throw new Error(`${path} must contain a JSON object`);
|
|
130
|
+
for (const [name, value] of Object.entries(parsed)) {
|
|
131
|
+
if (name === SUPPRESS_KEY) {
|
|
132
|
+
if (typeof value !== "boolean")
|
|
133
|
+
throw new Error(`${path} ${SUPPRESS_KEY} must be a boolean`);
|
|
134
|
+
} else if (typeof value !== "string") {
|
|
135
|
+
throw new Error(`${path} version for ${name} must be a string`);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
return parsed as ChangelogState;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
async function acquireLock(path: string): Promise<() => Promise<void>> {
|
|
142
|
+
const deadline = Date.now() + LOCK_TIMEOUT_MS;
|
|
143
|
+
while (true) {
|
|
144
|
+
try {
|
|
145
|
+
await mkdir(path);
|
|
146
|
+
return () => rm(path, { force: true, recursive: true });
|
|
147
|
+
} catch (error) {
|
|
148
|
+
if ((error as NodeJS.ErrnoException).code !== "EEXIST") throw error;
|
|
149
|
+
const info = await stat(path).catch(() => undefined);
|
|
150
|
+
if (info && Date.now() - info.mtimeMs > LOCK_STALE_MS) {
|
|
151
|
+
await rm(path, { force: true, recursive: true });
|
|
152
|
+
continue;
|
|
153
|
+
}
|
|
154
|
+
if (Date.now() >= deadline)
|
|
155
|
+
throw new Error(`Timed out waiting for ${path}`);
|
|
156
|
+
await new Promise((resolve) => setTimeout(resolve, 20));
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
async function writeState(path: string, state: ChangelogState): Promise<void> {
|
|
162
|
+
await mkdir(dirname(path), { mode: 0o700, recursive: true });
|
|
163
|
+
const temporaryPath = `${path}.${process.pid}.${randomUUID()}.tmp`;
|
|
164
|
+
try {
|
|
165
|
+
const ordered = Object.fromEntries(
|
|
166
|
+
Object.entries(state).sort(([left], [right]) =>
|
|
167
|
+
left.localeCompare(right),
|
|
168
|
+
),
|
|
169
|
+
);
|
|
170
|
+
await writeFile(temporaryPath, `${JSON.stringify(ordered, null, 2)}\n`, {
|
|
171
|
+
encoding: "utf8",
|
|
172
|
+
mode: 0o600,
|
|
173
|
+
});
|
|
174
|
+
await rename(temporaryPath, path);
|
|
175
|
+
} catch (error) {
|
|
176
|
+
await rm(temporaryPath, { force: true });
|
|
177
|
+
throw error;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
function packageMarkdown(
|
|
182
|
+
registration: PackageRegistration,
|
|
183
|
+
entries: ChangelogEntry[],
|
|
184
|
+
): string {
|
|
185
|
+
const body = (entry: ChangelogEntry) =>
|
|
186
|
+
entry.content
|
|
187
|
+
.replace(/^##[^\n]*\n*/, "")
|
|
188
|
+
.replace(/^###\s+(?:Changes|Minor Changes|Patch Changes)\s*\n*/gim, "")
|
|
189
|
+
.replace(/^###\s+(.+)$/gm, "**$1**");
|
|
190
|
+
if (entries.length === 1) {
|
|
191
|
+
const [entry] = entries;
|
|
192
|
+
if (!entry) return "";
|
|
193
|
+
return `## ${registration.name} ${entry.version}\n\n${body(entry)}`;
|
|
194
|
+
}
|
|
195
|
+
return [
|
|
196
|
+
`## ${registration.name}`,
|
|
197
|
+
...entries.map((entry) => `**${entry.version}**\n\n${body(entry)}`),
|
|
198
|
+
].join("\n\n");
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
async function claimUpdates(
|
|
202
|
+
registrations: Iterable<PackageRegistration>,
|
|
203
|
+
): Promise<{ errors: string[]; markdown?: string }> {
|
|
204
|
+
const stableRegistrations = [...registrations]
|
|
205
|
+
.filter((registration) => isStableVersion(registration.version))
|
|
206
|
+
.sort((left, right) => left.name.localeCompare(right.name));
|
|
207
|
+
if (stableRegistrations.length === 0) return { errors: [] };
|
|
208
|
+
const path = statePath();
|
|
209
|
+
await mkdir(dirname(path), { mode: 0o700, recursive: true });
|
|
210
|
+
const release = await acquireLock(`${path}.lock`);
|
|
211
|
+
try {
|
|
212
|
+
const state = await readState(path);
|
|
213
|
+
const errors: string[] = [];
|
|
214
|
+
const sections: string[] = [];
|
|
215
|
+
let changed = false;
|
|
216
|
+
if (state[SUPPRESS_KEY] === true) {
|
|
217
|
+
for (const registration of stableRegistrations) {
|
|
218
|
+
const seenVersion = state[registration.name];
|
|
219
|
+
if (
|
|
220
|
+
typeof seenVersion !== "string" ||
|
|
221
|
+
compareVersions(registration.version, seenVersion) > 0
|
|
222
|
+
) {
|
|
223
|
+
state[registration.name] = registration.version;
|
|
224
|
+
changed = true;
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
if (changed) await writeState(path, state);
|
|
228
|
+
return { errors };
|
|
229
|
+
}
|
|
230
|
+
for (const registration of stableRegistrations) {
|
|
231
|
+
const seenVersion = state[registration.name];
|
|
232
|
+
if (
|
|
233
|
+
typeof seenVersion === "string" &&
|
|
234
|
+
compareVersions(registration.version, seenVersion) <= 0
|
|
235
|
+
)
|
|
236
|
+
continue;
|
|
237
|
+
try {
|
|
238
|
+
const entries = parseChangelog(
|
|
239
|
+
await readFile(registration.changelogPath, "utf8"),
|
|
240
|
+
).filter(
|
|
241
|
+
(entry) =>
|
|
242
|
+
compareVersions(entry.version, registration.version) <= 0 &&
|
|
243
|
+
(typeof seenVersion === "string"
|
|
244
|
+
? compareVersions(entry.version, seenVersion) > 0
|
|
245
|
+
: entry.version === registration.version),
|
|
246
|
+
);
|
|
247
|
+
if (entries.length === 0) {
|
|
248
|
+
errors.push(
|
|
249
|
+
`No ${registration.version} changelog entry found for ${registration.name}`,
|
|
250
|
+
);
|
|
251
|
+
continue;
|
|
252
|
+
}
|
|
253
|
+
sections.push(packageMarkdown(registration, entries));
|
|
254
|
+
state[registration.name] = registration.version;
|
|
255
|
+
changed = true;
|
|
256
|
+
} catch (error) {
|
|
257
|
+
errors.push(
|
|
258
|
+
`Could not read the ${registration.name} changelog: ${error instanceof Error ? error.message : String(error)}`,
|
|
259
|
+
);
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
if (changed) await writeState(path, state);
|
|
263
|
+
return {
|
|
264
|
+
errors,
|
|
265
|
+
...(sections.length > 0 ? { markdown: sections.join("\n\n") } : {}),
|
|
266
|
+
};
|
|
267
|
+
} finally {
|
|
268
|
+
await release();
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
function registerCoordinator(
|
|
273
|
+
pi: ExtensionAPI,
|
|
274
|
+
registration: PackageRegistration,
|
|
275
|
+
): void {
|
|
276
|
+
let accepted = false;
|
|
277
|
+
pi.events.emit(REGISTRATION_CHANNEL, {
|
|
278
|
+
registration,
|
|
279
|
+
accept: () => {
|
|
280
|
+
accepted = true;
|
|
281
|
+
},
|
|
282
|
+
} satisfies RegistrationEvent);
|
|
283
|
+
if (accepted) return;
|
|
284
|
+
|
|
285
|
+
const registrations = new Map([[registration.name, registration]]);
|
|
286
|
+
pi.events.on(REGISTRATION_CHANNEL, (value) => {
|
|
287
|
+
if (!isRegistrationEvent(value)) return;
|
|
288
|
+
registrations.set(value.registration.name, value.registration);
|
|
289
|
+
value.accept();
|
|
290
|
+
});
|
|
291
|
+
pi.registerEntryRenderer<ChangelogEntryData>(
|
|
292
|
+
ENTRY_TYPE,
|
|
293
|
+
(entry, _options, theme) => {
|
|
294
|
+
if (!entry.data) return undefined;
|
|
295
|
+
const container = new Container();
|
|
296
|
+
container.addChild(new DynamicBorder());
|
|
297
|
+
container.addChild(
|
|
298
|
+
new Text(theme.bold(theme.fg("accent", "What's New")), 1, 0),
|
|
299
|
+
);
|
|
300
|
+
container.addChild(new Spacer(1));
|
|
301
|
+
container.addChild(
|
|
302
|
+
new Markdown(entry.data.markdown.trim(), 1, 0, getMarkdownTheme()),
|
|
303
|
+
);
|
|
304
|
+
container.addChild(new Spacer(1));
|
|
305
|
+
container.addChild(new DynamicBorder());
|
|
306
|
+
return container;
|
|
307
|
+
},
|
|
308
|
+
);
|
|
309
|
+
pi.on("session_start", async (event, ctx) => {
|
|
310
|
+
if (event.reason === "reload" || ctx.mode !== "tui") return;
|
|
311
|
+
if (
|
|
312
|
+
ctx.sessionManager
|
|
313
|
+
.getEntries()
|
|
314
|
+
.some((entry) =>
|
|
315
|
+
["branch_summary", "compaction", "message"].includes(entry.type),
|
|
316
|
+
)
|
|
317
|
+
)
|
|
318
|
+
return;
|
|
319
|
+
try {
|
|
320
|
+
const update = await claimUpdates(registrations.values());
|
|
321
|
+
if (update.markdown)
|
|
322
|
+
pi.appendEntry<ChangelogEntryData>(ENTRY_TYPE, {
|
|
323
|
+
markdown: update.markdown,
|
|
324
|
+
});
|
|
325
|
+
for (const error of update.errors) ctx.ui.notify(error, "warning");
|
|
326
|
+
} catch (error) {
|
|
327
|
+
ctx.ui.notify(
|
|
328
|
+
`Could not update Howaboua changelog state: ${error instanceof Error ? error.message : String(error)}`,
|
|
329
|
+
"warning",
|
|
330
|
+
);
|
|
331
|
+
}
|
|
332
|
+
});
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
export default function howabouaPackageChangelog(pi: ExtensionAPI): void {
|
|
336
|
+
registerCoordinator(pi, packageRegistration());
|
|
337
|
+
}
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { BrowserRoutes, loadBrowserRoutes } from "./src/browser/routes.js";
|
|
2
|
+
import { BrowserRuntime } from "./src/browser/runtime.js";
|
|
3
|
+
import { registerBrowserCommand } from "./src/browser/settings.js";
|
|
4
|
+
import { createBrowserTool, prepareBrowserCodeModeInput, } from "./src/browser-tool.js";
|
|
5
|
+
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
|
+
const tool = createBrowserTool(runtime);
|
|
20
|
+
registerBrowserCommand(pi);
|
|
21
|
+
pi.registerTool(tool);
|
|
22
|
+
const registration = await registerBrowserInCodeMode(pi, tool, runtime.hosts.length > 0);
|
|
23
|
+
pi.on("session_shutdown", () => {
|
|
24
|
+
registration?.unregister();
|
|
25
|
+
runtime.close();
|
|
26
|
+
});
|
|
27
|
+
if (configError) {
|
|
28
|
+
pi.on("session_start", (_event, ctx) => {
|
|
29
|
+
ctx.ui.notify(`${configError}. Run /browser to repair it.`, "warning");
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
async function registerBrowserInCodeMode(pi, tool, routed) {
|
|
34
|
+
try {
|
|
35
|
+
const { adaptToolForCodeMode, registerCodeModeExtensionTools } = await import("@howaboua/pi-codex-conversion/code-mode");
|
|
36
|
+
return registerCodeModeExtensionTools(pi, () => [
|
|
37
|
+
adaptToolForCodeMode(tool, {
|
|
38
|
+
kind: "freeform",
|
|
39
|
+
prepareInput: prepareBrowserCodeModeInput,
|
|
40
|
+
usage: `await tools.browser("help"); await tools.browser(JSON.stringify({ action: "tabs" })) // Logged-in ${routed ? "routed " : "local "}browser with web__run refs; ask before consequential external actions`,
|
|
41
|
+
}),
|
|
42
|
+
]);
|
|
43
|
+
}
|
|
44
|
+
catch (error) {
|
|
45
|
+
if (isMissingCodeModeExtension(error))
|
|
46
|
+
return undefined;
|
|
47
|
+
if (isOutdatedCodeModeExtension(error)) {
|
|
48
|
+
throw new Error("Update " +
|
|
49
|
+
CODE_MODE_PACKAGE +
|
|
50
|
+
" to 3.0.24 or newer to use Pi Browser with it", { cause: error });
|
|
51
|
+
}
|
|
52
|
+
throw error;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
function isMissingCodeModeExtension(error) {
|
|
56
|
+
if (!error ||
|
|
57
|
+
typeof error !== "object" ||
|
|
58
|
+
!("code" in error) ||
|
|
59
|
+
!("message" in error) ||
|
|
60
|
+
typeof error.message !== "string") {
|
|
61
|
+
return false;
|
|
62
|
+
}
|
|
63
|
+
if (error.code !== "ERR_MODULE_NOT_FOUND" &&
|
|
64
|
+
error.code !== "MODULE_NOT_FOUND") {
|
|
65
|
+
return false;
|
|
66
|
+
}
|
|
67
|
+
const missing = error.message.match(/Cannot find (?:package|module) ['"]([^'"]+)['"]/)?.[1];
|
|
68
|
+
return missing === CODE_MODE_PACKAGE || missing === CODE_MODE_MODULE;
|
|
69
|
+
}
|
|
70
|
+
function isOutdatedCodeModeExtension(error) {
|
|
71
|
+
if (!error ||
|
|
72
|
+
typeof error !== "object" ||
|
|
73
|
+
!("code" in error) ||
|
|
74
|
+
!("message" in error) ||
|
|
75
|
+
typeof error.message !== "string")
|
|
76
|
+
return false;
|
|
77
|
+
return ((error.code === "ERR_PACKAGE_PATH_NOT_EXPORTED" ||
|
|
78
|
+
error.code === "ERR_UNSUPPORTED_DIR_IMPORT") &&
|
|
79
|
+
(error.message.includes(CODE_MODE_MODULE) ||
|
|
80
|
+
(error.message.includes("Package subpath './code-mode'") &&
|
|
81
|
+
error.message.includes(CODE_MODE_PACKAGE))));
|
|
82
|
+
}
|