@liang-hz/agent-in-chrome 0.55.0 → 0.55.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.
package/mcp/config.mjs CHANGED
@@ -154,35 +154,79 @@ export function saveSync(value, file = CONFIG_FILE) {
154
154
  *
155
155
  * 防抖:一次保存常常连着来好几个事件(rename 前后、元数据),不合并的话
156
156
  * 一次改动会发好几条 listChanged,客户端跟着重拉好几次工具表。
157
+ *
158
+ * 另外还带一段**有界引导轮询**:`fs.watch` 在 macOS 上要过一小会儿才真的开始监听,
159
+ * 这段窗口里的写入原生事件一条都不会有,靠比对文件指纹补上。收到第一个原生事件
160
+ * (证明流活了)或超过上限就自动停,之后一律走原生事件。
157
161
  */
158
- export function watch(onChange, { file = CONFIG_FILE, debounceMs = 80 } = {}) {
162
+ const BOOTSTRAP_POLL_MS = 200;
163
+ const BOOTSTRAP_MAX_MS = 10_000;
164
+
165
+ export function watch(onChange, { file = CONFIG_FILE, debounceMs = 80, _watchImpl = fs.watch } = {}) {
159
166
  const dir = path.dirname(file);
160
167
  const base = path.basename(file);
161
168
  try {
162
169
  fs.mkdirSync(dir, { recursive: true });
163
170
  } catch {}
164
171
 
172
+ const fingerprint = () => {
173
+ try {
174
+ const s = fs.statSync(file);
175
+ return `${s.ino}:${s.size}:${s.mtimeMs}`;
176
+ } catch {
177
+ return "absent";
178
+ }
179
+ };
180
+
165
181
  let timer = null;
166
182
  let stopped = false;
183
+ let poll = null;
184
+ let fp = fingerprint();
167
185
  let w;
186
+
187
+ const schedule = () => {
188
+ if (stopped) return;
189
+ if (timer) clearTimeout(timer);
190
+ timer = setTimeout(() => {
191
+ timer = null;
192
+ if (!stopped) onChange(loadSync(file));
193
+ }, debounceMs);
194
+ };
195
+ const stopPoll = () => {
196
+ if (poll) clearInterval(poll);
197
+ poll = null;
198
+ };
199
+
168
200
  try {
169
- w = fs.watch(dir, { persistent: false }, (_type, name) => {
201
+ w = _watchImpl(dir, { persistent: false }, (_type, name) => {
202
+ stopPoll();
170
203
  if (name && name !== base) return;
171
- if (timer) clearTimeout(timer);
172
- timer = setTimeout(() => {
173
- timer = null;
174
- if (!stopped) onChange(loadSync(file));
175
- }, debounceMs);
204
+ fp = fingerprint();
205
+ schedule();
176
206
  });
177
207
  } catch (e) {
178
208
  return { ok: false, error: e, stop() {} };
179
209
  }
180
210
  w.unref?.();
211
+
212
+ const bootedAt = Date.now();
213
+ poll = setInterval(() => {
214
+ if (stopped) return stopPoll();
215
+ const now = fingerprint();
216
+ if (now !== fp) {
217
+ fp = now;
218
+ schedule();
219
+ }
220
+ if (Date.now() - bootedAt >= BOOTSTRAP_MAX_MS) stopPoll();
221
+ }, BOOTSTRAP_POLL_MS);
222
+ poll.unref?.();
223
+
181
224
  return {
182
225
  ok: true,
183
226
  stop() {
184
227
  stopped = true;
185
228
  if (timer) clearTimeout(timer);
229
+ stopPoll();
186
230
  try {
187
231
  w.close();
188
232
  } catch {}
package/mcp/server.mjs CHANGED
@@ -28,7 +28,7 @@ import {
28
28
  import { createUpdateNotice } from "./update-notice.mjs";
29
29
  import { foregroundGuard } from "./foreground-guard.mjs";
30
30
  import { deriveSessionId, pruneClaims } from "./session-id.mjs";
31
- import { probeLocal, readSessionIdentity, composeAgent, procSource, readClaudeSubagent, antigravityConversationId } from "./agent-identity.mjs";
31
+ import { probeLocal, readSessionIdentity, composeAgent, procSource, readClaudeSubagent, antigravityConversationId, codexThreadId, workbuddyConversationId, isOpencodeClient } from "./agent-identity.mjs";
32
32
  import * as cfgFile from "./config.mjs";
33
33
  import { TOOL_TIER, TIER_RANK, REVEAL_REQUIRES_FULL, PARAM_REQUIRES_FULL } from "./tool-tiers.mjs";
34
34
  import {
@@ -50,7 +50,7 @@ import {
50
50
  import crypto from "node:crypto";
51
51
 
52
52
  const NAME = "agent-in-chrome";
53
- const VERSION = "0.55.0";
53
+ const VERSION = "0.55.2";
54
54
  /** 本进程起来的时刻。CDP 模式下工具层就在本进程里,它也是「工具层代码的加载时刻」 */
55
55
  const PROCESS_STARTED_AT = Date.now();
56
56
  const SUPPORTED_PROTOCOLS = ["2025-11-25", "2025-06-18", "2025-03-26", "2024-11-05"];
@@ -427,10 +427,19 @@ let lastTitle = null;
427
427
  let LAST_META = null;
428
428
  let lastMetaKey = null;
429
429
  let LAST_TOOL_USE = null;
430
- function noteRpcMeta(m) {
430
+ let LAST_CALL = null;
431
+ function noteRpcMeta(m, tool) {
432
+ if (typeof tool === "string" && tool) LAST_CALL = { tool, at: Date.now() };
433
+ if (LAST_CALL && isOpencodeClient(LOCAL_ID?.clientCommand)) titleAt = 0;
431
434
  if (!m || typeof m !== "object") return;
432
435
  if (typeof m["claudecode/toolUseId"] === "string") LAST_TOOL_USE = m["claudecode/toolUseId"];
433
- const ids = [m.session_id, m.chatSessionId, m["antigravity.google/conversation_id"]].map((v) => v || "");
436
+ const ids = [
437
+ m.session_id,
438
+ m.chatSessionId,
439
+ antigravityConversationId(m),
440
+ codexThreadId(m),
441
+ workbuddyConversationId(m),
442
+ ].map((v) => v || "");
434
443
  if (!ids.some(Boolean)) return;
435
444
  const key = ids.join("|");
436
445
  LAST_META = m;
@@ -448,7 +457,7 @@ function refreshAgentInfo({ force = false } = {}) {
448
457
  if (!force && AGENT_INFO && Date.now() - titleAt < ttl) return AGENT_INFO;
449
458
  titleAt = Date.now();
450
459
  try {
451
- const idn = readSessionIdentity(LOCAL_ID, { meta: LAST_META });
460
+ const idn = readSessionIdentity(LOCAL_ID, { meta: LAST_META, tool: LAST_CALL?.tool, at: LAST_CALL?.at });
452
461
  if (idn.title) lastTitle = idn.title;
453
462
  if (idn.workspace) lastWs = { name: idn.workspace, kind: idn.workspaceKind };
454
463
  } catch {}
@@ -3101,7 +3110,7 @@ async function handle(msg) {
3101
3110
  }
3102
3111
  }
3103
3112
  const { sid, sidFrom, sidCaller } = sidFor(params?._meta);
3104
- noteRpcMeta(params?._meta);
3113
+ noteRpcMeta(params?._meta, params?.name);
3105
3114
  const key = id === undefined || id === null ? null : String(id);
3106
3115
  const track = beginCall(key, params?.name, params?._meta);
3107
3116
  const ctx = { sid, sidFrom, sidCaller, rpcMeta: params?._meta, onBridgeCall: track.onBridgeCall };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@liang-hz/agent-in-chrome",
3
- "version": "0.55.0",
3
+ "version": "0.55.2",
4
4
  "type": "module",
5
5
  "license": "Apache-2.0",
6
6
  "engines": {
@@ -511,6 +511,7 @@ export function buildAgents({ home = os.homedir(), platform = process.platform,
511
511
  : path.join(home, ".config", "Claude", "claude_desktop_config.json");
512
512
  const workbuddyConfig = path.join(home, ".workbuddy", "mcp.json");
513
513
  const workbuddyLegacyConfig = path.join(home, ".workbuddy", ".mcp.json");
514
+ const workbuddyAiConfig = path.join(home, ".workbuddy-ai", "mcp.json");
514
515
  const zcodeConfig = path.join(home, ".zcode", "cli", "config.json");
515
516
  const opencodeConfig = path.join(home, ".config", "opencode", "opencode.json");
516
517
  const kimiConfig = path.join(home, ".kimi", "mcp.json");
@@ -672,6 +673,16 @@ export function buildAgents({ home = os.homedir(), platform = process.platform,
672
673
  uninstallJsonEntry("WorkBuddy", workbuddyLegacyConfig);
673
674
  },
674
675
  },
676
+ {
677
+ id: "workbuddy-ai",
678
+ name: "WorkBuddy AI",
679
+ detected: fs.existsSync(path.join(home, ".workbuddy-ai")),
680
+ target: workbuddyAiConfig,
681
+ preview: previewJson(workbuddyEntry),
682
+ install: () => installJsonEntry("WorkBuddy AI", workbuddyAiConfig, workbuddyEntry),
683
+ check: () => checkJsonEntry("WorkBuddy AI", workbuddyAiConfig, workbuddyEntry, guiCheck),
684
+ uninstall: () => uninstallJsonEntry("WorkBuddy AI", workbuddyAiConfig),
685
+ },
675
686
  {
676
687
  id: "zcode",
677
688
  name: "ZCode",
package/scripts/e2e.mjs CHANGED
@@ -2718,7 +2718,10 @@ if (process.platform === "win32") {
2718
2718
  const QOD = path.join(K_HOME, ".qoder", "settings.json");
2719
2719
  const QODCN = path.join(K_HOME, ".qoder-cn", "settings.json");
2720
2720
  const QIDECN = path.join(K_HOME, ".qoder-cn", "mcp.json");
2721
- const QHOME_CN = path.join(K_HOME, "Library", "Application Support", "QoderCN", "SharedClientCache", "mcp.json");
2721
+ const QHOME_CN =
2722
+ process.platform === "darwin"
2723
+ ? path.join(K_HOME, "Library", "Application Support", "QoderCN", "SharedClientCache", "mcp.json")
2724
+ : path.join(K_HOME, ".config", "QoderCN", "SharedClientCache", "mcp.json");
2722
2725
  const TRAECN =
2723
2726
  process.platform === "darwin"
2724
2727
  ? path.join(K_HOME, "Library", "Application Support", "Trae CN", "User", "mcp.json")
@@ -1352,9 +1352,9 @@ export const FLAGS_USAGE = `用法:node scripts/install.mjs [--check | --unins
1352
1352
  --uninstall 卸载:移除安装写入的一切(agent 配置只摘我们自己的键,改前有备份)
1353
1353
  --yes / -y 跳过逐个确认
1354
1354
  --agents=a,b 只注册这些 agent。可用 id 见 --check 的输出,当前是:
1355
- claude-code、claude-desktop、workbuddy、zcode、opencode、kimi、
1356
- gemini、antigravity、qoder-cli、qoder-cli-cn、qoder-ide-cn、
1357
- trae-cn、qoder-ide、trae、deepseek-harness、codex
1355
+ claude-code、claude-desktop、workbuddy、workbuddy-ai、zcode、
1356
+ opencode、kimi、gemini、antigravity、qoder-cli、qoder-cli-cn、
1357
+ qoder-ide-cn、trae-cn、qoder-ide、trae、deepseek-harness、codex
1358
1358
  --no-agents 跳过 agent 注册
1359
1359
  --print-config[=json|toml|claude-cli]
1360
1360
  只把安装事实(路径、启动方式、扩展目录、各 agent 状态)打到 stdout,
@@ -33,6 +33,7 @@ const GUARDED = [
33
33
  path.join(REAL_HOME, ".gemini", "settings.json"),
34
34
  path.join(REAL_HOME, ".gemini", "config", "mcp_config.json"),
35
35
  path.join(REAL_HOME, ".workbuddy", "mcp.json"),
36
+ path.join(REAL_HOME, ".workbuddy-ai", "mcp.json"),
36
37
  path.join(REAL_HOME, ".qoder", "settings.json"),
37
38
  path.join(REAL_HOME, ".qoder-cn", "settings.json"),
38
39
  ];
@@ -828,7 +829,7 @@ console.log("\n\x1b[1mWindows 的 GUI 条目形状:cmd /c\x1b[0m");
828
829
  const p = list.find((a) => a.id === id)?.preview;
829
830
  return JSON.stringify((typeof p === "function" ? p() : p) || []);
830
831
  };
831
- for (const id of ["claude-desktop", "workbuddy", "zcode", "antigravity", "qoder-ide", "trae", "qoder-ide-cn", "trae-cn"]) {
832
+ for (const id of ["claude-desktop", "workbuddy", "workbuddy-ai", "zcode", "antigravity", "qoder-ide", "trae", "qoder-ide-cn", "trae-cn"]) {
832
833
  const p = entryOf(winAgents, id);
833
834
  check(`${id}:win32 上是 cmd /c 包一层`, p.includes('\\"command\\": \\"cmd\\"') && p.includes("/c") && p.includes("mcp-launcher.bat"), p.slice(0, 200));
834
835
  }
@@ -839,7 +840,7 @@ console.log("\n\x1b[1mWindows 的 GUI 条目形状:cmd /c\x1b[0m");
839
840
  entryOf(winAgents, "deepseek-harness")
840
841
  );
841
842
  check("CLI 客户端不受影响(还是 node + server.mjs)", entryOf(winAgents, "kimi").includes('\\"command\\": \\"node\\"'), entryOf(winAgents, "kimi"));
842
- for (const id of ["claude-desktop", "workbuddy", "zcode", "trae"]) {
843
+ for (const id of ["claude-desktop", "workbuddy", "workbuddy-ai", "zcode", "trae"]) {
843
844
  const p = entryOf(darwinAgents, id);
844
845
  check(`${id}:darwin 形状不变(command 就是启动器本身)`, p.includes("/rt/mcp-launcher.sh") && !p.includes("cmd"), p.slice(0, 200));
845
846
  }
@@ -943,6 +944,53 @@ console.log("\n\x1b[1m探测:没有证据就不算装着\x1b[0m");
943
944
  fs.rmSync(fakeBin, { recursive: true, force: true });
944
945
  }
945
946
 
947
+ console.log("\n\x1b[1mWorkBuddy AI:与国内版互不串写\x1b[0m");
948
+ {
949
+ const cnRel = ".workbuddy/mcp.json";
950
+ const aiRel = ".workbuddy-ai/mcp.json";
951
+ const launcher = path.join(ROOT, "bin", "agent-in-chrome.mjs");
952
+ const home = makeHome([
953
+ [cnRel, canonicalJson({ mcpServers: { keepme: { command: "keep" } } })],
954
+ [".workbuddy-ai/settings.json", "{}"],
955
+ ]);
956
+
957
+ const r = await quiet(() => runAgents("install", { ...opts(home), agents: ["workbuddy", "workbuddy-ai"] }));
958
+ const ai = JSON.parse(fs.readFileSync(path.join(home, aiRel), "utf8"));
959
+ const cn = JSON.parse(fs.readFileSync(path.join(home, cnRel), "utf8"));
960
+ check("国际版写进 ~/.workbuddy-ai/mcp.json", ai.mcpServers?.["agent-in-chrome"]?.command === launcher, JSON.stringify(ai));
961
+ check(
962
+ "国际版条目形状与国内版逐字相同(type/disabled 都带)",
963
+ JSON.stringify(ai.mcpServers?.["agent-in-chrome"]) ===
964
+ JSON.stringify({ type: "stdio", command: launcher, args: [], disabled: false }),
965
+ JSON.stringify(ai.mcpServers?.["agent-in-chrome"])
966
+ );
967
+ check(
968
+ "两家各写各的文件,国内版原有的键没被碰",
969
+ cn.mcpServers?.["agent-in-chrome"]?.command === launcher && cn.mcpServers?.keepme?.command === "keep",
970
+ JSON.stringify(cn)
971
+ );
972
+ check(
973
+ "两家都报装上了",
974
+ r.value.installed.includes("workbuddy") && r.value.installed.includes("workbuddy-ai"),
975
+ JSON.stringify(r.value)
976
+ );
977
+
978
+ const ids = (h) =>
979
+ buildAgents({ home: h, platform: "linux", serverPath: SERVER_PATH, mcpLauncher: launcher })
980
+ .filter((a) => a.detected)
981
+ .map((a) => a.id);
982
+ const cnOnly = ids(makeHome([[cnRel, "{}"]]));
983
+ const aiOnly = ids(makeHome([[aiRel, "{}"]]));
984
+ check("只有 ~/.workbuddy 时不认国际版", cnOnly.includes("workbuddy") && !cnOnly.includes("workbuddy-ai"), cnOnly.join(","));
985
+ check("只有 ~/.workbuddy-ai 时不认国内版", aiOnly.includes("workbuddy-ai") && !aiOnly.includes("workbuddy"), aiOnly.join(","));
986
+
987
+ await quiet(() => runAgents("uninstall", { ...opts(home), agents: ["workbuddy-ai"] }));
988
+ const aiAfter = JSON.parse(fs.readFileSync(path.join(home, aiRel), "utf8"));
989
+ const cnAfter = JSON.parse(fs.readFileSync(path.join(home, cnRel), "utf8"));
990
+ check("卸载摘掉国际版那条", aiAfter.mcpServers?.["agent-in-chrome"] === undefined, JSON.stringify(aiAfter));
991
+ check("卸载没碰国内版那条", cnAfter.mcpServers?.["agent-in-chrome"]?.command === launcher, JSON.stringify(cnAfter));
992
+ }
993
+
946
994
  function diffHint(a, b) {
947
995
  if (a === b) return "";
948
996
  for (let i = 0; i < Math.max(a.length, b.length); i++)
@@ -24,6 +24,16 @@ const check = (name, cond, extra = "") => {
24
24
  }
25
25
  };
26
26
 
27
+ const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
28
+ const waitUntil = async (cond, timeout = 3000, step = 20) => {
29
+ const t0 = Date.now();
30
+ while (Date.now() - t0 < timeout) {
31
+ if (cond()) return true;
32
+ await sleep(step);
33
+ }
34
+ return cond();
35
+ };
36
+
27
37
  console.log("\n\x1b[1m配置校验\x1b[0m");
28
38
  {
29
39
  const d = cfg.normalize({});
@@ -104,28 +114,108 @@ console.log("\n\x1b[1m热更新(watch)\x1b[0m");
104
114
  check("watch 起得来", w.ok);
105
115
 
106
116
  cfg.saveSync({ tools: { profile: "observe" } }, file);
107
- await new Promise((r) => setTimeout(r, 250));
117
+ await waitUntil(() => seen.at(-1)?.value?.tools.profile === "observe");
108
118
  check("文件从无到有能被监听到", seen.length >= 1 && seen.at(-1)?.value?.tools.profile === "observe");
109
119
 
110
120
  cfg.saveSync({ tools: { profile: "observe", disable: ["browser_find"] } }, file);
111
- await new Promise((r) => setTimeout(r, 250));
121
+ await waitUntil(() => seen.at(-1)?.value?.tools.disable?.includes("browser_find"));
112
122
  check("原子写(rename 换 inode)也能被监听到", seen.at(-1)?.value?.tools.disable?.includes("browser_find"));
113
123
 
114
124
  const n = seen.length;
115
125
  fs.writeFileSync(file, "{ 坏掉的");
116
- await new Promise((r) => setTimeout(r, 250));
126
+ await waitUntil(() => seen.length > n);
117
127
  check("坏配置也会回调(调用方据此失败关闭)", seen.length > n);
118
128
  check("坏配置的 value 是 null,不是默认值", seen.at(-1)?.value === null);
119
129
 
120
130
  w.stop();
121
131
  const after = seen.length;
122
132
  cfg.saveSync({ tools: { profile: "full" } }, file);
123
- await new Promise((r) => setTimeout(r, 250));
133
+ await sleep(250);
124
134
  check("stop() 之后不再回调", seen.length === after);
125
135
 
126
136
  fs.rmSync(dir, { recursive: true, force: true });
127
137
  }
128
138
 
139
+ console.log("\n\x1b[1m引导轮询(fs.watch 建流窗口)\x1b[0m");
140
+ {
141
+ const deadWatch = () => ({ close() {}, unref() {} });
142
+
143
+ const dir = fs.mkdtempSync(path.join(os.tmpdir(), "aic-boot-"));
144
+ const file = path.join(dir, "config.json");
145
+ const seen = [];
146
+ const w = cfg.watch((loaded) => seen.push(loaded), { file, debounceMs: 30, _watchImpl: deadWatch });
147
+ check("注入假 watcher 时 watch 仍报 ok", w.ok);
148
+
149
+ cfg.saveSync({ tools: { profile: "observe" } }, file);
150
+ await waitUntil(() => seen.at(-1)?.value?.tools.profile === "observe");
151
+ check("原生事件一条都不来时,文件从无到有仍被引导轮询捞到", seen.at(-1)?.value?.tools.profile === "observe");
152
+
153
+ cfg.saveSync({ tools: { profile: "observe", disable: ["browser_batch"] } }, file);
154
+ await waitUntil(() => seen.at(-1)?.value?.tools.disable?.includes("browser_batch"));
155
+ check(
156
+ "原生事件一条都不来时,原子写换 inode 也被引导轮询捞到",
157
+ seen.at(-1)?.value?.tools.disable?.includes("browser_batch")
158
+ );
159
+
160
+ w.stop();
161
+ const after = seen.length;
162
+ cfg.saveSync({ tools: { profile: "full" } }, file);
163
+ await sleep(600);
164
+ check("stop() 之后引导轮询也停", seen.length === after);
165
+
166
+ fs.rmSync(dir, { recursive: true, force: true });
167
+ }
168
+
169
+ {
170
+ let fire = null;
171
+ const manualWatch = (_dir, _opts, cb) => {
172
+ fire = cb;
173
+ return {
174
+ close() {
175
+ fire = null;
176
+ },
177
+ unref() {},
178
+ };
179
+ };
180
+
181
+ const dir = fs.mkdtempSync(path.join(os.tmpdir(), "aic-boot2-"));
182
+ const file = path.join(dir, "config.json");
183
+ const seen = [];
184
+ const w = cfg.watch((loaded) => seen.push(loaded), { file, debounceMs: 30, _watchImpl: manualWatch });
185
+
186
+ cfg.saveSync({ tools: { profile: "observe" } }, file);
187
+ fire("rename", "config.json");
188
+ await waitUntil(() => seen.length >= 1);
189
+ check("手动触发的原生事件能回调", seen.length >= 1 && seen.at(-1)?.value?.tools.profile === "observe");
190
+
191
+ const after = seen.length;
192
+ cfg.saveSync({ tools: { profile: "full" } }, file);
193
+ await sleep(600);
194
+ check("收到第一个原生事件后轮询即停(600ms 内不再有回调)", seen.length === after);
195
+
196
+ w.stop();
197
+ fs.rmSync(dir, { recursive: true, force: true });
198
+ }
199
+
200
+ {
201
+ let lost = 0;
202
+ const N = 40;
203
+ for (let i = 0; i < N; i++) {
204
+ const dir = fs.mkdtempSync(path.join(os.tmpdir(), "aic-race-"));
205
+ const file = path.join(dir, "config.json");
206
+ let got = false;
207
+ const w = cfg.watch(() => {
208
+ got = true;
209
+ }, { file, debounceMs: 30 });
210
+ cfg.saveSync({ tools: { profile: "observe" } }, file);
211
+ await waitUntil(() => got, 1000);
212
+ if (!got) lost++;
213
+ w.stop();
214
+ fs.rmSync(dir, { recursive: true, force: true });
215
+ }
216
+ check(`watch() 后立刻写 ${N} 次,一次都不丢`, lost === 0, `丢了 ${lost} 次`);
217
+ }
218
+
129
219
  function startServer(dir) {
130
220
  const p = spawn(process.execPath, [SERVER], {
131
221
  stdio: ["pipe", "pipe", "ignore"],