@xiaoqiong0v0/opencode-browser-tool 1.9.0 → 1.10.2

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.
@@ -5,6 +5,7 @@
5
5
  // 1.9.0 会话隔离:每个 shell 实例注册自己的 `/notify/<sid>`,通知按 URL 里的 sid 路由到
6
6
  // 对应会话的 drain —— 彻底取代旧的「最后活跃会话」(last-writer-wins)投递。
7
7
  import { createServer } from "node:http";
8
+ import { log } from "./logger.js";
8
9
  let server = null;
9
10
  let port = 0;
10
11
  /** 收到通知时的回调:参数为通知 URL 里的 sid(空串 = 未带 sid 的旧格式 /notify) */
@@ -23,7 +24,18 @@ export async function startNotifyServer(handler) {
23
24
  return;
24
25
  }
25
26
  // 从路径提取 sid:/notify/<sid>;裸 /notify(旧格式)→ sid = ""
26
- const sid = url.startsWith("/notify/") ? decodeURIComponent(url.slice("/notify/".length)) : "";
27
+ // 非法百分号编码不能让异常冒到 request listener 外(会打崩进程),按无 sid 处理
28
+ let sid = "";
29
+ if (url.startsWith("/notify/")) {
30
+ try {
31
+ sid = decodeURIComponent(url.slice("/notify/".length));
32
+ }
33
+ catch {
34
+ sid = "";
35
+ // 公共 logger 无 warn()(只有 info/error),降级用 info 并显式标注 [warn]
36
+ log.info(`[notify][warn] malformed sid in url (undecodable), treated as empty: ${url.slice(0, 120)}`);
37
+ }
38
+ }
27
39
  // 只读取/丢弃最小 payload,避免大内容经此通道
28
40
  let bytes = 0;
29
41
  req.on("data", (c) => {
@@ -54,10 +66,6 @@ export async function startNotifyServer(handler) {
54
66
  });
55
67
  });
56
68
  }
57
- /** 未带 sid 的基础通知地址(仅兼容旧用法;新代码请用 getNotifyUrlForSession) */
58
- export function getNotifyUrl() {
59
- return server && port ? `http://127.0.0.1:${port}/notify` : null;
60
- }
61
69
  /** 某会话的通知地址:每个 shell 实例注册各自这条 → 通知可按 sid 归属 */
62
70
  export function getNotifyUrlForSession(sid) {
63
71
  return server && port ? `http://127.0.0.1:${port}/notify/${encodeURIComponent(sid)}` : null;
@@ -0,0 +1,73 @@
1
+ /**
2
+ * 模型可见文本的最后一道闸:把本机绝对路径掩码成 `<path>`。
3
+ *
4
+ * - 只用于**即将返回给模型**的文本(工具返回值、状态文案、抛给模型的错误 message)。
5
+ * - 插件日志(logger / stdout / stderr)不走本函数,原始路径仍完整保留在日志里,便于取证。
6
+ * - 这里是兜底而非唯一防线:各出口应尽量只输出「环境变量名 / 配置项名」,本函数负责拦住
7
+ * 底层 fs/fetch 错误里意外夹带的路径。
8
+ *
9
+ * 为什么不用正则去猜错误文案:
10
+ * 错误分类(鉴权 / 连接 / 服务端 / 响应非 JSON)由 `client.ts` 的 HTTP 原语按状态码与
11
+ * 抛错点**显式标记**(`HttpOutcome` / `ConnectionError`),不依赖 message 措辞;本函数只做
12
+ * 已决定要展示的文本的路径脱敏,不推断错误语义、不改变调用方的分支判断。两者分工明确:
13
+ * 分类在 HTTP 层,脱敏在展示层。若在这里用正则猜「这是不是路径/是不是某个错误」,跨运行
14
+ * 时(Node/Bun)文案差异必然漏判,反而更难维护。
15
+ */
16
+ /** 本机绝对路径的统一占位符 */
17
+ const PATH_PLACEHOLDER = "<path>";
18
+ /** 默认截断长度(字符数,含省略号) */
19
+ const DEFAULT_MAX_LEN = 300;
20
+ /** 截断标记 */
21
+ const ELLIPSIS = "…";
22
+ /** URL 先用占位符保护,避免后面的路径规则误伤 `https://host/a/b` 里的 `/a/b` */
23
+ const URL_RE = /\b(?:https?|wss?|ftp):\/\/[A-Za-z0-9\-._~:/?#[\]@!$&()*+,;=%]+/gi;
24
+ /** 引号内的绝对路径(允许含空格):只替换内容,保留引号 */
25
+ const QUOTED_PATH_RE = /(["'])((?:[A-Za-z]:[\\/]|\\\\|\/)[^"'\r\n]+)\1/g;
26
+ /** Windows UNC(反斜杠形式,如 `\\server\share\x`;也覆盖 `\\?\C:\...`) */
27
+ const UNC_RE = /\\\\[^\s'"<>|,;、)\]]+/g;
28
+ /** Windows UNC 的正斜杠形式(`//server/share/x`);URL 已提前保护 */
29
+ const UNC_FWD_RE = /(?<![\w:/])\/\/[A-Za-z0-9._$-]+\/[^\s'"<>|,;、)\]]+/g;
30
+ /** Windows 盘符绝对路径(`C:\x` / `C:/x`);负向前瞻排除 URL scheme 里的 `s:/` */
31
+ const WIN_ABS_RE = /(?<![\w:/])[A-Za-z]:[\\/][^\s'"<>|,;、)\]]*/g;
32
+ /** POSIX 绝对路径(至少两段,如 `/home/u/x`、`/tmp/a/b`、`/etc/passwd`);不匹配相对片段 `a/b` */
33
+ const POSIX_ABS_RE = /(?<![\w.:/~-])\/(?:[A-Za-z0-9._~+@%-]+\/)+[A-Za-z0-9._~+@%-]*/g;
34
+ /** 接口路径前缀(`/api/...`、`/notify/...`):属于诊断信息而非本机路径,POSIX 匹配命中后原样保留 */
35
+ const KEEP_API_PATH_RE = /^\/(?:api|notify)\//;
36
+ /** URL 保护占位符(控制字符不会出现在正常文案里) */
37
+ const URL_TOKEN_RE = /\u0001(\d+)\u0002/g;
38
+ /**
39
+ * 把文本中的本机绝对路径掩码为 `<path>`,并按需截断。
40
+ *
41
+ * 覆盖 Windows 盘符路径、UNC、POSIX 绝对路径;URL、版本号、相对路径(`a/b`、`dist/index.js`)、
42
+ * 以 `/api/`、`/notify/` 开头的接口路径与已存在的 `<path>` 保持不变。幂等:对同一文本重复调用结果稳定。
43
+ *
44
+ * @param text 模型可见文本
45
+ * @param maxLen 截断上限(字符数,含省略号),默认 300
46
+ * @returns 脱敏(并按需截断)后的文本
47
+ */
48
+ export function sanitizeForModel(text, maxLen = DEFAULT_MAX_LEN) {
49
+ if (!text)
50
+ return text;
51
+ // 1) 先保护 URL(恢复时原样放回)
52
+ const urls = [];
53
+ let out = text.replace(URL_RE, (m) => {
54
+ const token = `\u0001${urls.length}\u0002`;
55
+ urls.push(m);
56
+ return token;
57
+ });
58
+ // 2) 引号内路径(可能含空格)→ 保留引号,只换内容
59
+ out = out.replace(QUOTED_PATH_RE, (_m, quote) => `${quote}${PATH_PLACEHOLDER}${quote}`);
60
+ // 3) 无引号路径:UNC → 盘符 → POSIX
61
+ out = out.replace(UNC_RE, PATH_PLACEHOLDER);
62
+ out = out.replace(UNC_FWD_RE, PATH_PLACEHOLDER);
63
+ out = out.replace(WIN_ABS_RE, PATH_PLACEHOLDER);
64
+ // POSIX:以 /api/、/notify/ 开头的接口路径原样保留,其余绝对路径掩码
65
+ out = out.replace(POSIX_ABS_RE, (m) => (KEEP_API_PATH_RE.test(m) ? m : PATH_PLACEHOLDER));
66
+ // 4) 还原 URL
67
+ out = out.replace(URL_TOKEN_RE, (_m, index) => urls[Number(index)] ?? _m);
68
+ // 5) 截断(保留省略号,总长度不超过 maxLen)
69
+ const limit = Number.isFinite(maxLen) && maxLen > 0 ? Math.floor(maxLen) : DEFAULT_MAX_LEN;
70
+ if (out.length > limit)
71
+ out = `${out.slice(0, Math.max(0, limit - 1))}${ELLIPSIS}`;
72
+ return out;
73
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xiaoqiong0v0/opencode-browser-tool",
3
- "version": "1.9.0",
3
+ "version": "1.10.2",
4
4
  "description": "opencode 浏览器工具插件:WebView2 外壳渲染 + 零注入视觉元素选取面板",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {