@howaboua/pi-browser 0.0.12 → 0.0.14

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 CHANGED
@@ -1,5 +1,15 @@
1
1
  # @howaboua/pi-browser
2
2
 
3
+ ## 0.0.14
4
+
5
+ - Updates use Pi's native changelog notices and `collapseChangelog` instead of custom suppression.
6
+ - Reading a page from a large browser result no longer rereads the whole result.
7
+ - Browser help clarifies when to ask before proceeding and how to keep references on the right host.
8
+
9
+ ## 0.0.13
10
+
11
+ - Fixed npm-installed Pi Browser blocking startup when Pi Codex Conversion is installed.
12
+
3
13
  ## 0.0.12
4
14
 
5
15
  - Added an API to start voice in local or configured remote browsers and report when the microphone is listening.
package/dist/extension.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { defineTool } from "@earendil-works/pi-coding-agent";
2
- import { Type } from "typebox";
2
+ import { obj, opt, str } from "@earendil-works/pi-schema";
3
3
  import { readBrowserRouteConfig, readBrowserVoiceOnly, } from "./src/browser/config.js";
4
4
  import { registerBrowserCommand } from "./src/browser/settings.js";
5
5
  /** Keep the normal browser graph out of Pi until the tool is enabled. */
@@ -25,5 +25,5 @@ export default async function browserExtension(pi) {
25
25
  return;
26
26
  }
27
27
  const { default: registerBrowser } = await import("./src/browser-registration.js");
28
- await registerBrowser(pi, { defineTool, Type });
28
+ await registerBrowser(pi, { defineTool, obj, opt, str });
29
29
  }
@@ -1624,6 +1624,7 @@ class BrowserCdpSession {
1624
1624
  // src/browser/artifacts.ts
1625
1625
  import {
1626
1626
  mkdir as mkdir2,
1627
+ open,
1627
1628
  readdir as readdir2,
1628
1629
  readFile,
1629
1630
  rm as rm2,
@@ -1635,11 +1636,12 @@ import { join as join3 } from "node:path";
1635
1636
  var RESULT_TTL_MS = 60 * 60 * 1000;
1636
1637
  var SCREENSHOT_TTL_MS = 24 * 60 * 60 * 1000;
1637
1638
  var TEXT_BUDGET_BYTES = 32000;
1639
+ var RESULT_READ_BYTES = 2 * (TEXT_BUDGET_BYTES - 2 + 1);
1638
1640
  function artifactDirectory() {
1639
1641
  return process.env["XDG_RUNTIME_DIR"] ? join3(process.env["XDG_RUNTIME_DIR"], "browser-tool") : join3(tmpdir(), `browser-tool-${process.getuid?.() ?? "user"}`);
1640
1642
  }
1641
- function resultPath(handle) {
1642
- return join3(artifactDirectory(), `result-${handle}.txt`);
1643
+ function resultPath(handle, legacy = false) {
1644
+ return join3(artifactDirectory(), `result-${handle}.${legacy ? "txt" : "utf16le"}`);
1643
1645
  }
1644
1646
  async function screenshotPath(request) {
1645
1647
  await mkdir2(artifactDirectory(), {
@@ -1655,13 +1657,12 @@ async function pruneArtifacts(now = Date.now()) {
1655
1657
  try {
1656
1658
  names = await readdir2(artifactDirectory());
1657
1659
  } catch (error) {
1658
- if (error && typeof error === "object" && "code" in error && error.code === "ENOENT") {
1660
+ if (isMissing(error))
1659
1661
  return 0;
1660
- }
1661
1662
  throw error;
1662
1663
  }
1663
1664
  let removed = 0;
1664
- for (const name of names.filter((candidate) => /^(?:result-[a-f0-9-]{36}\.txt|browser-.*\.png)$/.test(candidate))) {
1665
+ for (const name of names.filter((candidate) => /^(?:result-[a-f0-9-]{36}\.(?:txt|utf16le)|browser-.*\.png)$/.test(candidate))) {
1665
1666
  const path = join3(artifactDirectory(), name);
1666
1667
  try {
1667
1668
  const maxAge = name.startsWith("result-") ? RESULT_TTL_MS : SCREENSHOT_TTL_MS;
@@ -1670,32 +1671,49 @@ async function pruneArtifacts(now = Date.now()) {
1670
1671
  removed++;
1671
1672
  }
1672
1673
  } catch (error) {
1673
- if (!error || typeof error !== "object" || !("code" in error) || error.code !== "ENOENT") {
1674
+ if (!isMissing(error))
1674
1675
  throw error;
1675
- }
1676
1676
  }
1677
1677
  }
1678
1678
  return removed;
1679
1679
  }
1680
- function chunkForJson(value, offset = 0, budget = TEXT_BUDGET_BYTES) {
1680
+ function chunkForJson(value, offset = 0) {
1681
1681
  let bytes = 2;
1682
1682
  let end = offset;
1683
- for (const character of value.slice(offset)) {
1684
- const next = Buffer.byteLength(JSON.stringify(character)) - 2;
1685
- if (bytes + next > budget)
1683
+ while (end < value.length) {
1684
+ const point = value.codePointAt(end);
1685
+ const next = jsonCharacterBytes(point);
1686
+ if (bytes + next > TEXT_BUDGET_BYTES)
1686
1687
  break;
1687
1688
  bytes += next;
1688
- end += character.length;
1689
+ end += point > 65535 ? 2 : 1;
1689
1690
  }
1690
1691
  return { text: value.slice(offset, end), end };
1691
1692
  }
1693
+ function jsonCharacterBytes(point) {
1694
+ if (point < 32) {
1695
+ return point === 8 || point === 9 || point === 10 || point === 12 || point === 13 ? 2 : 6;
1696
+ }
1697
+ if (point === 34 || point === 92)
1698
+ return 2;
1699
+ if (point < 128)
1700
+ return 1;
1701
+ if (point < 2048)
1702
+ return 2;
1703
+ if (point >= 55296 && point <= 57343)
1704
+ return 6;
1705
+ return point <= 65535 ? 3 : 4;
1706
+ }
1692
1707
  async function cacheResult(value) {
1693
1708
  const handle = crypto.randomUUID();
1694
1709
  await mkdir2(artifactDirectory(), {
1695
1710
  recursive: true,
1696
1711
  mode: 448
1697
1712
  });
1698
- await writeFile2(resultPath(handle), value, { mode: 384 });
1713
+ await writeFile2(resultPath(handle), value, {
1714
+ encoding: "utf16le",
1715
+ mode: 384
1716
+ });
1699
1717
  return handle;
1700
1718
  }
1701
1719
  async function limitedText(base, field, value) {
@@ -1712,34 +1730,72 @@ async function limitedText(base, field, value) {
1712
1730
  next_offset: chunk.end
1713
1731
  };
1714
1732
  }
1733
+ function checkOffset(offset, length) {
1734
+ if (offset > length) {
1735
+ throw new Error(`offset ${offset} exceeds result length ${length}`);
1736
+ }
1737
+ }
1738
+ async function readResultPage(path, offset) {
1739
+ const file = await open(path, "r");
1740
+ try {
1741
+ const { size } = await file.stat();
1742
+ if (size % 2)
1743
+ throw new Error("Cached result is incomplete; rerun the original action");
1744
+ const length = size / 2;
1745
+ checkOffset(offset, length);
1746
+ const position = offset * 2;
1747
+ const bytes = Buffer.allocUnsafe(Math.min(size - position, RESULT_READ_BYTES));
1748
+ let used = 0;
1749
+ while (used < bytes.length) {
1750
+ const { bytesRead } = await file.read(bytes, used, bytes.length - used, position + used);
1751
+ if (!bytesRead)
1752
+ throw new Error("Cached result is incomplete; rerun the original action");
1753
+ used += bytesRead;
1754
+ }
1755
+ const chunk = chunkForJson(bytes.toString("utf16le"));
1756
+ return { text: chunk.text, end: offset + chunk.end, length };
1757
+ } finally {
1758
+ await file.close();
1759
+ }
1760
+ }
1761
+ function isMissing(error) {
1762
+ return !!error && typeof error === "object" && "code" in error && error.code === "ENOENT";
1763
+ }
1715
1764
  async function readCachedResult(request) {
1716
- let value;
1765
+ const { handle, offset } = request;
1766
+ let path = resultPath(handle);
1767
+ let page;
1717
1768
  try {
1718
- value = await readFile(resultPath(request.handle), "utf8");
1769
+ page = await readResultPage(path, offset);
1719
1770
  } catch (error) {
1720
- if (error && typeof error === "object" && "code" in error && error.code === "ENOENT") {
1721
- throw new Error(`result handle not found: ${request.handle}; check the host or rerun the original action`);
1771
+ if (!isMissing(error))
1772
+ throw error;
1773
+ path = resultPath(handle, true);
1774
+ let value;
1775
+ try {
1776
+ value = await readFile(path, "utf8");
1777
+ } catch (legacyError) {
1778
+ if (!isMissing(legacyError))
1779
+ throw legacyError;
1780
+ throw new Error(`result handle not found: ${handle}; check the host or rerun the original action`);
1722
1781
  }
1723
- throw error;
1782
+ checkOffset(offset, value.length);
1783
+ page = { ...chunkForJson(value, offset), length: value.length };
1724
1784
  }
1725
- if (request.offset > value.length) {
1726
- throw new Error(`offset ${request.offset} exceeds result length ${value.length}`);
1727
- }
1728
- const chunk = chunkForJson(value, request.offset);
1729
- const complete = chunk.end >= value.length;
1785
+ const complete = page.end >= page.length;
1730
1786
  if (complete) {
1731
- await rm2(resultPath(request.handle), { force: true });
1787
+ await rm2(path, { force: true });
1732
1788
  }
1733
1789
  return {
1734
- handle: request.handle,
1735
- offset: request.offset,
1736
- text: chunk.text,
1790
+ handle,
1791
+ offset,
1792
+ text: page.text,
1737
1793
  complete,
1738
- ...complete ? {} : { next_offset: chunk.end }
1794
+ ...complete ? {} : { next_offset: page.end }
1739
1795
  };
1740
1796
  }
1741
1797
  async function discardCachedResult(handle) {
1742
- await rm2(resultPath(handle), { force: true });
1798
+ await Promise.all([resultPath(handle), resultPath(handle, true)].map((path) => rm2(path, { force: true })));
1743
1799
  return { discarded: handle };
1744
1800
  }
1745
1801
 
@@ -1749,11 +1805,11 @@ function browserHelp(hosts = [], gpt = false) {
1749
1805
  return {
1750
1806
  input: "{action,...fields}",
1751
1807
  ...routed ? {
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"}`
1808
+ host: `${hosts.join("|")}${gpt ? " optional; user-named host→all calls; refs/handles stay with host" : " optional; keep the user-named host on every call and reuse refs or handles only on their host"}`
1753
1809
  } : {},
1754
1810
  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",
1811
+ safety: gpt ? "MUST ask before unfamiliar low-trust navigation or consequential action unless authorised. MUST NOT close shared browser" : "Unless authorised, MUST ask before unfamiliar low-trust navigation or consequential action. MUST NOT close a shared browser",
1812
+ batch: gpt ? "Independent only: {action_name:[{...fields}],...}; nonempty arrays; root host/response_length; omit item action" : "Batch independent actions only: {action_name:[{...fields}],...}. Nonempty arrays; host and response_length at root; omit item action",
1757
1813
  actions: {
1758
1814
  help: "",
1759
1815
  tabs: "query? offset? owned_only? -> ref_id title url owned",
@@ -1,16 +1,18 @@
1
- import { mkdir, readdir, readFile, rm, stat, writeFile, } from "node:fs/promises";
1
+ import { mkdir, open, readdir, readFile, rm, stat, writeFile, } from "node:fs/promises";
2
2
  import { tmpdir } from "node:os";
3
3
  import { join } from "node:path";
4
4
  const RESULT_TTL_MS = 60 * 60 * 1_000;
5
5
  const SCREENSHOT_TTL_MS = 24 * 60 * 60 * 1_000;
6
6
  const TEXT_BUDGET_BYTES = 32_000;
7
+ // At least one byte per UTF-16 unit, plus one unit of surrogate lookahead.
8
+ const RESULT_READ_BYTES = 2 * (TEXT_BUDGET_BYTES - 2 + 1);
7
9
  export function artifactDirectory() {
8
10
  return process.env["XDG_RUNTIME_DIR"]
9
11
  ? join(process.env["XDG_RUNTIME_DIR"], "browser-tool")
10
12
  : join(tmpdir(), `browser-tool-${process.getuid?.() ?? "user"}`);
11
13
  }
12
- function resultPath(handle) {
13
- return join(artifactDirectory(), `result-${handle}.txt`);
14
+ function resultPath(handle, legacy = false) {
15
+ return join(artifactDirectory(), `result-${handle}.${legacy ? "txt" : "utf16le"}`);
14
16
  }
15
17
  export async function screenshotPath(request) {
16
18
  await mkdir(artifactDirectory(), {
@@ -31,16 +33,12 @@ export async function pruneArtifacts(now = Date.now()) {
31
33
  names = await readdir(artifactDirectory());
32
34
  }
33
35
  catch (error) {
34
- if (error &&
35
- typeof error === "object" &&
36
- "code" in error &&
37
- error.code === "ENOENT") {
36
+ if (isMissing(error))
38
37
  return 0;
39
- }
40
38
  throw error;
41
39
  }
42
40
  let removed = 0;
43
- for (const name of names.filter((candidate) => /^(?:result-[a-f0-9-]{36}\.txt|browser-.*\.png)$/.test(candidate))) {
41
+ for (const name of names.filter((candidate) => /^(?:result-[a-f0-9-]{36}\.(?:txt|utf16le)|browser-.*\.png)$/.test(candidate))) {
44
42
  const path = join(artifactDirectory(), name);
45
43
  try {
46
44
  const maxAge = name.startsWith("result-")
@@ -52,35 +50,56 @@ export async function pruneArtifacts(now = Date.now()) {
52
50
  }
53
51
  }
54
52
  catch (error) {
55
- if (!error ||
56
- typeof error !== "object" ||
57
- !("code" in error) ||
58
- error.code !== "ENOENT") {
53
+ if (!isMissing(error))
59
54
  throw error;
60
- }
61
55
  }
62
56
  }
63
57
  return removed;
64
58
  }
65
- function chunkForJson(value, offset = 0, budget = TEXT_BUDGET_BYTES) {
59
+ function chunkForJson(value, offset = 0) {
66
60
  let bytes = 2;
67
61
  let end = offset;
68
- for (const character of value.slice(offset)) {
69
- const next = Buffer.byteLength(JSON.stringify(character)) - 2;
70
- if (bytes + next > budget)
62
+ while (end < value.length) {
63
+ const point = value.codePointAt(end);
64
+ const next = jsonCharacterBytes(point);
65
+ if (bytes + next > TEXT_BUDGET_BYTES)
71
66
  break;
72
67
  bytes += next;
73
- end += character.length;
68
+ end += point > 0xffff ? 2 : 1;
74
69
  }
75
70
  return { text: value.slice(offset, end), end };
76
71
  }
72
+ function jsonCharacterBytes(point) {
73
+ if (point < 0x20) {
74
+ return point === 8 ||
75
+ point === 9 ||
76
+ point === 10 ||
77
+ point === 12 ||
78
+ point === 13
79
+ ? 2
80
+ : 6;
81
+ }
82
+ if (point === 0x22 || point === 0x5c)
83
+ return 2;
84
+ if (point < 0x80)
85
+ return 1;
86
+ if (point < 0x800)
87
+ return 2;
88
+ if (point >= 0xd800 && point <= 0xdfff)
89
+ return 6;
90
+ return point <= 0xffff ? 3 : 4;
91
+ }
77
92
  async function cacheResult(value) {
78
93
  const handle = crypto.randomUUID();
79
94
  await mkdir(artifactDirectory(), {
80
95
  recursive: true,
81
96
  mode: 0o700,
82
97
  });
83
- await writeFile(resultPath(handle), value, { mode: 0o600 });
98
+ // Fixed-width code units allow seeks at every accepted JS string offset.
99
+ await writeFile(resultPath(handle), value, {
100
+ encoding: "utf16le",
101
+ mode: 0o600,
102
+ });
84
103
  return handle;
85
104
  }
86
105
  export async function limitedText(base, field, value) {
@@ -97,37 +116,78 @@ export async function limitedText(base, field, value) {
97
116
  next_offset: chunk.end,
98
117
  };
99
118
  }
119
+ function checkOffset(offset, length) {
120
+ if (offset > length) {
121
+ throw new Error(`offset ${offset} exceeds result length ${length}`);
122
+ }
123
+ }
124
+ async function readResultPage(path, offset) {
125
+ const file = await open(path, "r");
126
+ try {
127
+ const { size } = await file.stat();
128
+ if (size % 2)
129
+ throw new Error("Cached result is incomplete; rerun the original action");
130
+ const length = size / 2;
131
+ checkOffset(offset, length);
132
+ const position = offset * 2;
133
+ const bytes = Buffer.allocUnsafe(Math.min(size - position, RESULT_READ_BYTES));
134
+ let used = 0;
135
+ while (used < bytes.length) {
136
+ const { bytesRead } = await file.read(bytes, used, bytes.length - used, position + used);
137
+ if (!bytesRead)
138
+ throw new Error("Cached result is incomplete; rerun the original action");
139
+ used += bytesRead;
140
+ }
141
+ const chunk = chunkForJson(bytes.toString("utf16le"));
142
+ return { text: chunk.text, end: offset + chunk.end, length };
143
+ }
144
+ finally {
145
+ await file.close();
146
+ }
147
+ }
148
+ function isMissing(error) {
149
+ return (!!error &&
150
+ typeof error === "object" &&
151
+ "code" in error &&
152
+ error.code === "ENOENT");
153
+ }
100
154
  export async function readCachedResult(request) {
101
- let value;
155
+ const { handle, offset } = request;
156
+ let path = resultPath(handle);
157
+ let page;
102
158
  try {
103
- value = await readFile(resultPath(request.handle), "utf8");
159
+ page = await readResultPage(path, offset);
104
160
  }
105
161
  catch (error) {
106
- if (error &&
107
- typeof error === "object" &&
108
- "code" in error &&
109
- error.code === "ENOENT") {
110
- throw new Error(`result handle not found: ${request.handle}; check the host or rerun the original action`);
162
+ if (!isMissing(error))
163
+ throw error;
164
+ // Let handles issued by the previous worker finish during their short TTL.
165
+ path = resultPath(handle, true);
166
+ let value;
167
+ try {
168
+ value = await readFile(path, "utf8");
111
169
  }
112
- throw error;
113
- }
114
- if (request.offset > value.length) {
115
- throw new Error(`offset ${request.offset} exceeds result length ${value.length}`);
170
+ catch (legacyError) {
171
+ if (!isMissing(legacyError))
172
+ throw legacyError;
173
+ throw new Error(`result handle not found: ${handle}; check the host or rerun the original action`);
174
+ }
175
+ checkOffset(offset, value.length);
176
+ page = { ...chunkForJson(value, offset), length: value.length };
116
177
  }
117
- const chunk = chunkForJson(value, request.offset);
118
- const complete = chunk.end >= value.length;
178
+ const complete = page.end >= page.length;
119
179
  if (complete) {
120
- await rm(resultPath(request.handle), { force: true });
180
+ await rm(path, { force: true });
121
181
  }
122
182
  return {
123
- handle: request.handle,
124
- offset: request.offset,
125
- text: chunk.text,
183
+ handle,
184
+ offset,
185
+ text: page.text,
126
186
  complete,
127
- ...(complete ? {} : { next_offset: chunk.end }),
187
+ ...(complete ? {} : { next_offset: page.end }),
128
188
  };
129
189
  }
130
190
  export async function discardCachedResult(handle) {
131
- await rm(resultPath(handle), { force: true });
191
+ await Promise.all([resultPath(handle), resultPath(handle, true)].map((path) => rm(path, { force: true })));
132
192
  return { discarded: handle };
133
193
  }
@@ -4,18 +4,18 @@ export function browserHelp(hosts = [], gpt = false) {
4
4
  input: "{action,...fields}",
5
5
  ...(routed
6
6
  ? {
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"}`,
7
+ host: `${hosts.join("|")}${gpt ? " optional; user-named host→all calls; refs/handles stay with host" : " optional; keep the user-named host on every call and reuse refs or handles only on their host"}`,
8
8
  }
9
9
  : {}),
10
10
  refs: gpt
11
11
  ? "tabs→open; ref_id/id/cursors stay with result"
12
12
  : "tabs -> open; keep ref_id/id/cursors with their result",
13
13
  safety: gpt
14
- ? "Unfamiliar low-trust navigation/consequential action→ask UNLESS authorised; NEVER close shared browser"
15
- : "Ask before unfamiliar low-trust navigation or consequential action unless authorized; never close shared browser",
14
+ ? "MUST ask before unfamiliar low-trust navigation or consequential action unless authorised. MUST NOT close shared browser"
15
+ : "Unless authorised, MUST ask before unfamiliar low-trust navigation or consequential action. MUST NOT close a shared browser",
16
16
  batch: gpt
17
- ? "{action_name:[{...fields}],...}; nonempty arrays; omit item action; root host/response_length; independent ONLY"
18
- : "{action_name:[{...fields}],...}; nonempty arrays; items omit action; host/response_length at request root; independent only",
17
+ ? "Independent only: {action_name:[{...fields}],...}; nonempty arrays; root host/response_length; omit item action"
18
+ : "Batch independent actions only: {action_name:[{...fields}],...}. Nonempty arrays; host and response_length at root; omit item action",
19
19
  actions: {
20
20
  help: "",
21
21
  tabs: "query? offset? owned_only? -> ref_id title url owned",
@@ -1,6 +1,8 @@
1
1
  import type { defineTool, ExtensionAPI } from "@earendil-works/pi-coding-agent";
2
- import type { Type } from "typebox";
2
+ import type { obj, opt, str } from "@earendil-works/pi-schema";
3
3
  export default function browserExtension(pi: ExtensionAPI, sdk: {
4
4
  defineTool: typeof defineTool;
5
- Type: typeof Type;
5
+ obj: typeof obj;
6
+ opt: typeof opt;
7
+ str: typeof str;
6
8
  }): Promise<void>;
@@ -54,7 +54,7 @@ async function registerBrowserInCodeMode(pi, tool, sdk) {
54
54
  return registerCodeModeExtensionTools(pi, () => [
55
55
  adaptToolForCodeMode({
56
56
  ...tool,
57
- parameters: sdk.Type.Object({}, { additionalProperties: true }),
57
+ parameters: sdk.obj({}, { open: true }),
58
58
  }, {
59
59
  prepareInput: prepareBrowserInput,
60
60
  discovery: "help",
@@ -1,12 +1,14 @@
1
1
  import type { defineTool as definePiTool } from "@earendil-works/pi-coding-agent";
2
- import type { Type as SchemaType } from "typebox";
2
+ import type { obj as schemaObj, opt as schemaOpt, str as schemaStr } from "@earendil-works/pi-schema";
3
3
  import type { BrowserRuntime } from "./browser/runtime.js";
4
4
  export declare function prepareBrowserInput(input: unknown): {
5
5
  command: string;
6
6
  };
7
- export declare function createBrowserTool({ defineTool, Type, }: {
7
+ export declare function createBrowserTool({ defineTool, obj, opt, str, }: {
8
8
  defineTool: typeof definePiTool;
9
- Type: typeof SchemaType;
10
- }, runtime: BrowserRuntime, isGpt?: () => boolean): import("@earendil-works/pi-coding-agent").ToolDefinition<SchemaType.TObject<{
11
- command: SchemaType.TOptional<SchemaType.TString>;
9
+ obj: typeof schemaObj;
10
+ opt: typeof schemaOpt;
11
+ str: typeof schemaStr;
12
+ }, runtime: BrowserRuntime, isGpt?: () => boolean): import("@earendil-works/pi-coding-agent").ToolDefinition<import("@earendil-works/pi-schema").Schema<{
13
+ command?: string;
12
14
  }>, unknown, any> & import("@earendil-works/pi-coding-agent").ToolDefinition<any, any, any>;
@@ -19,14 +19,15 @@ export function prepareBrowserInput(input) {
19
19
  parseBrowserRequest(input);
20
20
  return { command: JSON.stringify(input) };
21
21
  }
22
- export function createBrowserTool({ defineTool, Type, }, runtime, isGpt = () => false) {
22
+ export function createBrowserTool({ defineTool, obj, opt, str, }, runtime, isGpt = () => false) {
23
+ const parameters = obj({
24
+ command: opt(str({ description: "help or JSON request" })),
25
+ }, { open: false });
23
26
  return defineTool({
24
27
  name: "browser",
25
28
  label: "Browser",
26
29
  description: "Control logged-in browser",
27
- parameters: Type.Object({
28
- command: Type.Optional(Type.String({ description: "help or JSON request" })),
29
- }, { additionalProperties: false }),
30
+ parameters,
30
31
  prepareArguments: prepareBrowserInput,
31
32
  async execute(_toolCallId, input, signal, onUpdate, ctx) {
32
33
  const request = parseBrowserRequest(input.command);
@@ -1,5 +1,4 @@
1
- import { Type } from "typebox";
2
1
  import type { BrowserRuntime } from "./browser/runtime.js";
3
- export declare function createBrowserTool(runtime: BrowserRuntime, isGpt?: () => boolean): import("@earendil-works/pi-coding-agent").ToolDefinition<Type.TObject<{
4
- command: Type.TOptional<Type.TString>;
2
+ export declare function createBrowserTool(runtime: BrowserRuntime, isGpt?: () => boolean): import("@earendil-works/pi-coding-agent").ToolDefinition<import("@earendil-works/pi-schema").Schema<{
3
+ command?: string;
5
4
  }>, unknown, any> & import("@earendil-works/pi-coding-agent").ToolDefinition<any, any, any>;
@@ -1,6 +1,6 @@
1
1
  import { defineTool } from "@earendil-works/pi-coding-agent";
2
- import { Type } from "typebox";
2
+ import { obj, opt, str } from "@earendil-works/pi-schema";
3
3
  import { createBrowserTool as createTool } from "./browser-tool-definition.js";
4
4
  export function createBrowserTool(runtime, isGpt = () => false) {
5
- return createTool({ defineTool, Type }, runtime, isGpt);
5
+ return createTool({ defineTool, obj, opt, str }, runtime, isGpt);
6
6
  }
package/extension.ts ADDED
@@ -0,0 +1,34 @@
1
+ import { defineTool, type ExtensionAPI } from "@earendil-works/pi-coding-agent";
2
+ import { obj, opt, str } from "@earendil-works/pi-schema";
3
+ import {
4
+ readBrowserRouteConfig,
5
+ readBrowserVoiceOnly,
6
+ } from "./src/browser/config.js";
7
+ import { registerBrowserCommand } from "./src/browser/settings.js";
8
+
9
+ /** Keep the normal browser graph out of Pi until the tool is enabled. */
10
+ export default async function browserExtension(
11
+ pi: ExtensionAPI,
12
+ ): Promise<void> {
13
+ let voiceOnly = false;
14
+ try {
15
+ voiceOnly = readBrowserVoiceOnly();
16
+ } catch {
17
+ // Normal registration reports invalid configuration and retains local access.
18
+ }
19
+ if (voiceOnly) {
20
+ registerBrowserCommand(pi);
21
+ try {
22
+ readBrowserRouteConfig();
23
+ } catch (error) {
24
+ const message = error instanceof Error ? error.message : String(error);
25
+ pi.on("session_start", (_event, ctx) => {
26
+ ctx.ui.notify(`${message}. Run /browser to repair it.`, "warning");
27
+ });
28
+ }
29
+ return;
30
+ }
31
+ const { default: registerBrowser } =
32
+ await import("./src/browser-registration.js");
33
+ await registerBrowser(pi, { defineTool, obj, opt, str });
34
+ }