@liang-hz/agent-in-chrome 0.55.2 → 0.55.5
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/README.en.md +3 -3
- package/README.md +3 -3
- package/extension/manifest.json +1 -1
- package/extension/sw.js +91 -25
- package/mcp/agent-identity.mjs +293 -61
- package/mcp/cdp/browser-launch.mjs +160 -48
- package/mcp/server.mjs +4 -22
- package/native-host/host.mjs +28 -0
- package/package.json +1 -1
- package/scripts/borrow-login.mjs +6 -2
- package/scripts/e2e-sweep.mjs +3 -4
- package/scripts/e2e.mjs +77 -36
- package/scripts/install.mjs +70 -4
- package/scripts/launcher-spawn.mjs +22 -0
- package/scripts/pack-crx.mjs +2 -1
- package/scripts/test-agents.mjs +55 -22
- package/scripts/test-e2e-sweep.mjs +7 -2
- package/scripts/test-identity.mjs +307 -84
- package/scripts/test-install-skill.mjs +37 -2
- package/scripts/test-protocol.mjs +102 -1
- package/scripts/test-security.mjs +38 -17
- package/scripts/test-sw.mjs +103 -18
|
@@ -126,7 +126,11 @@ function firstExisting(cands) {
|
|
|
126
126
|
|
|
127
127
|
/*
|
|
128
128
|
* 找浏览器。优先级就是「越像用户日常那台越好」:
|
|
129
|
-
* 正式版 Chrome → Beta/Canary → Chromium /
|
|
129
|
+
* 正式版 Chrome → Beta/Canary → Chromium / Edge(指纹会差,最后才用)。
|
|
130
|
+
*
|
|
131
|
+
* Windows 的候选按安装范围排:系统级(Program Files)→ 32 位系统级(Program Files
|
|
132
|
+
* (x86),官方默认位置)→ 每用户(%LocalAppData%)。Beta/Canary/Dev 各有
|
|
133
|
+
* 自己的目录名(Chrome Beta / Chrome SxS / Chrome Dev),Canary 只发每用户安装。
|
|
130
134
|
*/
|
|
131
135
|
export function findBrowser() {
|
|
132
136
|
const explicit = process.env.AGENT_IN_CHROME_BIN;
|
|
@@ -136,6 +140,10 @@ export function findBrowser() {
|
|
|
136
140
|
}
|
|
137
141
|
|
|
138
142
|
const mac = process.platform === "darwin";
|
|
143
|
+
const win = process.platform === "win32";
|
|
144
|
+
const PF = process.env.ProgramFiles || "C:\\Program Files";
|
|
145
|
+
const PF86 = process.env["ProgramFiles(x86)"] || "C:\\Program Files (x86)";
|
|
146
|
+
const LAD = process.env.LocalAppData || path.join(HOME, "AppData", "Local");
|
|
139
147
|
const stableCands = mac
|
|
140
148
|
? [
|
|
141
149
|
["/Applications/Google Chrome.app/Contents/MacOS/Google Chrome", "正式版 Chrome"],
|
|
@@ -143,11 +151,21 @@ export function findBrowser() {
|
|
|
143
151
|
["/Applications/Google Chrome Beta.app/Contents/MacOS/Google Chrome Beta", "Chrome Beta"],
|
|
144
152
|
["/Applications/Google Chrome Canary.app/Contents/MacOS/Google Chrome Canary", "Chrome Canary"],
|
|
145
153
|
]
|
|
146
|
-
:
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
154
|
+
: win
|
|
155
|
+
? [
|
|
156
|
+
[path.join(PF, "Google", "Chrome", "Application", "chrome.exe"), "正式版 Chrome"],
|
|
157
|
+
[path.join(PF86, "Google", "Chrome", "Application", "chrome.exe"), "正式版 Chrome"],
|
|
158
|
+
[path.join(LAD, "Google", "Chrome", "Application", "chrome.exe"), "正式版 Chrome"],
|
|
159
|
+
[path.join(PF, "Google", "Chrome Beta", "Application", "chrome.exe"), "Chrome Beta"],
|
|
160
|
+
[path.join(PF86, "Google", "Chrome Beta", "Application", "chrome.exe"), "Chrome Beta"],
|
|
161
|
+
[path.join(LAD, "Google", "Chrome Beta", "Application", "chrome.exe"), "Chrome Beta"],
|
|
162
|
+
[path.join(LAD, "Google", "Chrome SxS", "Application", "chrome.exe"), "Chrome Canary"],
|
|
163
|
+
]
|
|
164
|
+
: [
|
|
165
|
+
["/usr/bin/google-chrome", "正式版 Chrome"],
|
|
166
|
+
["/usr/bin/google-chrome-stable", "正式版 Chrome"],
|
|
167
|
+
["/opt/google/chrome/chrome", "正式版 Chrome"],
|
|
168
|
+
];
|
|
151
169
|
for (const [bin, kind] of stableCands) if (fs.existsSync(bin)) return { bin, kind, stable: true };
|
|
152
170
|
|
|
153
171
|
const fallback = mac
|
|
@@ -158,22 +176,49 @@ export function findBrowser() {
|
|
|
158
176
|
"Chrome for Testing",
|
|
159
177
|
],
|
|
160
178
|
]
|
|
161
|
-
:
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
179
|
+
: win
|
|
180
|
+
? [
|
|
181
|
+
[path.join(LAD, "Chromium", "Application", "chrome.exe"), "Chromium"],
|
|
182
|
+
[path.join(PF, "Chromium", "Application", "chrome.exe"), "Chromium"],
|
|
183
|
+
[path.join(PF86, "Microsoft", "Edge", "Application", "msedge.exe"), "Microsoft Edge"],
|
|
184
|
+
[path.join(PF, "Microsoft", "Edge", "Application", "msedge.exe"), "Microsoft Edge"],
|
|
185
|
+
[path.join(LAD, "Microsoft", "Edge", "Application", "msedge.exe"), "Microsoft Edge"],
|
|
186
|
+
]
|
|
187
|
+
: [
|
|
188
|
+
["/usr/bin/chromium", "Chromium"],
|
|
189
|
+
["/usr/bin/chromium-browser", "Chromium"],
|
|
190
|
+
];
|
|
165
191
|
for (const [bin, kind] of fallback) if (fs.existsSync(bin)) return { bin, kind, stable: false };
|
|
166
192
|
|
|
167
193
|
throw new Error(
|
|
168
194
|
"找不到浏览器。CLI/headless 模式要用本机的 Chrome(指纹就该是它本来的样子):\n" +
|
|
169
195
|
(mac
|
|
170
196
|
? " macOS:装 Google Chrome 到 /Applications 即可\n"
|
|
171
|
-
:
|
|
197
|
+
: win
|
|
198
|
+
? " Windows:装 Google Chrome(默认装到 Program Files 即可),或用 AGENT_IN_CHROME_BIN 指路径\n"
|
|
199
|
+
: " Linux:apt install google-chrome-stable,或用 AGENT_IN_CHROME_BIN 指路径\n") +
|
|
172
200
|
"也可以用 AGENT_IN_CHROME_BIN 指定任意 Chromium 系二进制。"
|
|
173
201
|
);
|
|
174
202
|
}
|
|
175
203
|
|
|
176
204
|
const versionCache = new Map();
|
|
205
|
+
function winMajorFromSiblingDir(bin) {
|
|
206
|
+
try {
|
|
207
|
+
const dirs = fs
|
|
208
|
+
.readdirSync(path.dirname(bin), { withFileTypes: true })
|
|
209
|
+
.filter((d) => d.isDirectory() && /^\d+\.\d+\.\d+\.\d+$/.test(d.name))
|
|
210
|
+
.map((d) => d.name)
|
|
211
|
+
.sort((a, b) => {
|
|
212
|
+
const pa = a.split(".").map(Number);
|
|
213
|
+
const pb = b.split(".").map(Number);
|
|
214
|
+
for (let i = 0; i < 4; i++) if (pa[i] !== pb[i]) return pb[i] - pa[i];
|
|
215
|
+
return 0;
|
|
216
|
+
});
|
|
217
|
+
return (dirs[0] || "").split(".")[0] || "";
|
|
218
|
+
} catch {
|
|
219
|
+
return "";
|
|
220
|
+
}
|
|
221
|
+
}
|
|
177
222
|
function browserMajorVersion(bin) {
|
|
178
223
|
let key = bin;
|
|
179
224
|
try {
|
|
@@ -181,10 +226,14 @@ function browserMajorVersion(bin) {
|
|
|
181
226
|
} catch {}
|
|
182
227
|
if (versionCache.has(key)) return versionCache.get(key);
|
|
183
228
|
let major = "";
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
229
|
+
if (process.platform === "win32") {
|
|
230
|
+
major = winMajorFromSiblingDir(bin);
|
|
231
|
+
} else {
|
|
232
|
+
try {
|
|
233
|
+
const out = execFileSync(bin, ["--version"], { encoding: "utf8" });
|
|
234
|
+
major = (out.match(/(\d+)\.\d+\.\d+\.\d+/) || [])[1] || "";
|
|
235
|
+
} catch {}
|
|
236
|
+
}
|
|
188
237
|
versionCache.set(key, major);
|
|
189
238
|
return major;
|
|
190
239
|
}
|
|
@@ -207,19 +256,15 @@ function headedUserAgent(bin) {
|
|
|
207
256
|
*/
|
|
208
257
|
export const OWNER_FLAG = "--aic-owner";
|
|
209
258
|
|
|
210
|
-
/*
|
|
211
|
-
* 从浏览器进程的命令行里读出它的 `--user-data-dir`,读不到给 null。
|
|
212
|
-
*
|
|
213
|
-
* 值一直取到下一个 ` --` 开头的旗标为止:路径里带空格(macOS 的
|
|
214
|
-
* `Application Support` 之流)不能把它截成半截。我们自己 spawn 的命令行里
|
|
215
|
-
* user-data-dir 后面永远还有别的旗标(--remote-debugging-port 紧跟其后),
|
|
216
|
-
* 所以这个边界总是存在。
|
|
217
|
-
*/
|
|
218
259
|
export function profileDirFrom(cmd) {
|
|
219
|
-
const m = /--user-data-dir=(.+?)(?=\s--|$)/.exec(String(cmd || ""));
|
|
220
|
-
return m ? m[1] : null;
|
|
260
|
+
const m = /--user-data-dir=(.+?)(?=\s--|$)/.exec(String(cmd || "").trim());
|
|
261
|
+
return m ? m[1].replace(/^"|"$/g, "") : null;
|
|
221
262
|
}
|
|
222
263
|
|
|
264
|
+
/* 目录等:Windows 的路径不分大小写(agent-identity 同款判据),POSIX 按字面 */
|
|
265
|
+
const dirEq = (a, b) =>
|
|
266
|
+
process.platform === "win32" ? String(a).toLowerCase() === String(b).toLowerCase() : a === b;
|
|
267
|
+
|
|
223
268
|
/*
|
|
224
269
|
* 从进程列表里挑出「我们起的、而且**没人要了**」的浏览器,返回它们的 pid。
|
|
225
270
|
*
|
|
@@ -245,7 +290,7 @@ export function orphansFrom(procs, isAlive, { readRecord = null, excludeProfile
|
|
|
245
290
|
if (!Number.isInteger(owner) || owner <= 0) continue;
|
|
246
291
|
if (isAlive(owner)) continue;
|
|
247
292
|
const profile = profileDirFrom(cmd);
|
|
248
|
-
if (excludeProfile && profile
|
|
293
|
+
if (excludeProfile && profile && dirEq(profile, excludeProfile)) continue;
|
|
249
294
|
if (readRecord && profile) {
|
|
250
295
|
const rec = readRecord(profile);
|
|
251
296
|
if (rec && Number(rec.pid) === Number(p.pid)) {
|
|
@@ -260,6 +305,36 @@ export function orphansFrom(procs, isAlive, { readRecord = null, excludeProfile
|
|
|
260
305
|
|
|
261
306
|
/* 本机进程快照,只取 pid + 完整命令行 */
|
|
262
307
|
export function psSnapshot() {
|
|
308
|
+
if (process.platform === "win32") {
|
|
309
|
+
const tmp = path.join(os.tmpdir(), `aic-ps-${process.pid}-${Math.random().toString(36).slice(2)}.txt`);
|
|
310
|
+
const ps = path.join(process.env.SystemRoot || "C:\\Windows", "System32", "WindowsPowerShell", "v1.0", "powershell.exe");
|
|
311
|
+
const script =
|
|
312
|
+
`$ProgressPreference = 'SilentlyContinue'\n` +
|
|
313
|
+
`Get-CimInstance Win32_Process -Property ProcessId,CommandLine | ForEach-Object {\n` +
|
|
314
|
+
` if ($_.CommandLine) { $_.ProcessId.ToString() + "\`t" + ($_.CommandLine -replace "[\`r\`n]", " ") }\n` +
|
|
315
|
+
`} | Out-File -FilePath ${JSON.stringify(tmp)} -Encoding utf8`;
|
|
316
|
+
try {
|
|
317
|
+
execFileSync(ps, ["-NoProfile", "-NonInteractive", "-EncodedCommand", Buffer.from(script, "utf16le").toString("base64")], {
|
|
318
|
+
stdio: ["ignore", "ignore", "ignore"],
|
|
319
|
+
timeout: 30_000,
|
|
320
|
+
windowsHide: true,
|
|
321
|
+
});
|
|
322
|
+
return fs
|
|
323
|
+
.readFileSync(tmp, "utf8")
|
|
324
|
+
.replace(/^/, "")
|
|
325
|
+
.split("\n")
|
|
326
|
+
.map((l) => /^(\d+)\t(.*)$/.exec(l.trim()))
|
|
327
|
+
.filter(Boolean)
|
|
328
|
+
.map((m) => ({ pid: Number(m[1]), cmd: m[2] }));
|
|
329
|
+
} catch (e) {
|
|
330
|
+
log(`Windows 进程表取不到(${String(e?.message || e).split("\n")[0].slice(0, 120)}):` + "孤儿清扫和认领这一拍看不到别的进程");
|
|
331
|
+
return [];
|
|
332
|
+
} finally {
|
|
333
|
+
try {
|
|
334
|
+
fs.unlinkSync(tmp);
|
|
335
|
+
} catch {}
|
|
336
|
+
}
|
|
337
|
+
}
|
|
263
338
|
try {
|
|
264
339
|
const out = execFileSync("/bin/ps", ["-eo", "pid=,command="], { encoding: "utf8", maxBuffer: 8 << 20 });
|
|
265
340
|
return out
|
|
@@ -376,6 +451,7 @@ export async function sweepOrphansAsync({
|
|
|
376
451
|
}
|
|
377
452
|
|
|
378
453
|
function canGroupKill(pid, procs) {
|
|
454
|
+
if (process.platform === "win32") return false;
|
|
379
455
|
const cmd = (procs || []).find((p) => Number(p?.pid) === Number(pid))?.cmd;
|
|
380
456
|
const profile = cmd ? profileDirFrom(String(cmd)) : null;
|
|
381
457
|
if (!profile) return true;
|
|
@@ -596,14 +672,6 @@ function clearPid() {
|
|
|
596
672
|
dropLegacyIfOurs();
|
|
597
673
|
}
|
|
598
674
|
|
|
599
|
-
/*
|
|
600
|
-
* 这个 pid 此刻是不是一个正跑着**本 profile** 的浏览器。
|
|
601
|
-
*
|
|
602
|
-
* 两道都必要:pid 会被系统复用(`kill(pid,0)` 只说明「有个进程」,不说明是哪个),
|
|
603
|
-
* 而认 `--user-data-dir=<本 profile>` 才说明是「跑我们这个 profile 的」。
|
|
604
|
-
* 这是所有「要不要把信号发给它」的最后一道闸——别的 profile 的浏览器命令行里写的是
|
|
605
|
-
* 别的目录,过不了这一道,所以一份写错/别人的记录也带不动我们去杀它。
|
|
606
|
-
*/
|
|
607
675
|
function pidRunsThisProfile(pid, procs = null) {
|
|
608
676
|
if (!pid) return false;
|
|
609
677
|
try {
|
|
@@ -611,18 +679,44 @@ function pidRunsThisProfile(pid, procs = null) {
|
|
|
611
679
|
} catch {
|
|
612
680
|
return false;
|
|
613
681
|
}
|
|
682
|
+
const mine = (cmd) => {
|
|
683
|
+
const dir = profileDirFrom(cmd);
|
|
684
|
+
return !!dir && dirEq(dir, PROFILE_DIR);
|
|
685
|
+
};
|
|
614
686
|
if (procs) {
|
|
615
687
|
const hit = procs.find((x) => x.pid === Number(pid));
|
|
616
|
-
return !!hit && hit.cmd
|
|
688
|
+
return !!hit && mine(hit.cmd);
|
|
689
|
+
}
|
|
690
|
+
if (process.platform === "win32") {
|
|
691
|
+
const cmd = winProcCmd(pid);
|
|
692
|
+
return cmd != null && mine(cmd);
|
|
617
693
|
}
|
|
618
694
|
try {
|
|
619
695
|
const cmd = execFileSync("/bin/ps", ["-p", String(pid), "-o", "command="], { encoding: "utf8" });
|
|
620
|
-
return cmd
|
|
696
|
+
return mine(cmd);
|
|
621
697
|
} catch {
|
|
622
698
|
return false;
|
|
623
699
|
}
|
|
624
700
|
}
|
|
625
701
|
|
|
702
|
+
function winProcCmd(pid) {
|
|
703
|
+
const ps = path.join(process.env.SystemRoot || "C:\\Windows", "System32", "WindowsPowerShell", "v1.0", "powershell.exe");
|
|
704
|
+
const script =
|
|
705
|
+
`$ErrorActionPreference='SilentlyContinue';` +
|
|
706
|
+
`$c=(Get-CimInstance Win32_Process -Filter "ProcessId=${Number(pid)}").CommandLine;` +
|
|
707
|
+
`ConvertTo-Json -Compress -InputObject @($c)`;
|
|
708
|
+
const out = execFileSync(ps, ["-NoProfile", "-NonInteractive", "-EncodedCommand", Buffer.from(script, "utf16le").toString("base64")], {
|
|
709
|
+
encoding: "utf8",
|
|
710
|
+
timeout: 8000,
|
|
711
|
+
windowsHide: true,
|
|
712
|
+
maxBuffer: 4 * 1024 * 1024,
|
|
713
|
+
stdio: ["ignore", "pipe", "ignore"],
|
|
714
|
+
});
|
|
715
|
+
const rows = JSON.parse(String(out || "[]").trim() || "[]");
|
|
716
|
+
const cmd = Array.isArray(rows) ? rows[0] : rows;
|
|
717
|
+
return typeof cmd === "string" && cmd ? cmd : null;
|
|
718
|
+
}
|
|
719
|
+
|
|
626
720
|
/*
|
|
627
721
|
* **我们给这个 profile 起的**那个浏览器还活着吗;活着就返回它的记录(pid 已核对过)。
|
|
628
722
|
*
|
|
@@ -643,13 +737,29 @@ export function aliveBrowser() {
|
|
|
643
737
|
|
|
644
738
|
/*
|
|
645
739
|
* 谁正占着这个 profile(返回它的 pid,没人占返回 null)。
|
|
646
|
-
* **Chrome 自己按 profile 记的,不是我们记的**:读
|
|
647
|
-
*
|
|
648
|
-
*
|
|
740
|
+
* **macOS/Linux 上 Chrome 自己按 profile 记的,不是我们记的**:读
|
|
741
|
+
* `<profile>/SingletonLock` 这个指向「主机名-pid」的符号链接。Chrome 一个
|
|
742
|
+
* `--user-data-dir` 只准一个实例,第二个起来会输掉 ProcessSingleton 当场自杀。
|
|
649
743
|
* 这里只探活,**不核对是不是我们起的**——它要回答的是「被谁占着」,答案得包括
|
|
650
744
|
* 用户自己开的那种。拿它当发信号的目标时,核对由调用方补。
|
|
745
|
+
*
|
|
746
|
+
* Windows 上没有这个符号链接:Chrome 的 ProcessSingleton 在那边是一个命名 mutex +
|
|
747
|
+
* 隐藏窗口,pid 从外面查不出来(process_singleton_win.cc)。改从进程表认:谁的
|
|
748
|
+
* 命令行里带着 `--user-data-dir=<本 profile>`,谁就是占用者;优先挑没有 `--type=`
|
|
749
|
+
* 的——Chrome 的子进程(renderer/gpu/crashpad)命令行里同样带着这个旗标,只有
|
|
750
|
+
* 主进程没有。快照由调用方传(ensureBrowser 本来就拉了一拍)或现拉一拍。
|
|
651
751
|
*/
|
|
652
|
-
function profileOwnerPid() {
|
|
752
|
+
function profileOwnerPid(procs = null) {
|
|
753
|
+
if (process.platform === "win32") {
|
|
754
|
+
const snap = procs || psSnapshot();
|
|
755
|
+
const hits = snap
|
|
756
|
+
.filter((p) => {
|
|
757
|
+
const dir = profileDirFrom(p.cmd);
|
|
758
|
+
return dir && dirEq(dir, PROFILE_DIR) && pidAlive(p.pid);
|
|
759
|
+
})
|
|
760
|
+
.sort((a, b) => Number(/(^|\s)--type=/.test(a.cmd)) - Number(/(^|\s)--type=/.test(b.cmd)));
|
|
761
|
+
return hits[0]?.pid ?? null;
|
|
762
|
+
}
|
|
653
763
|
let target = null;
|
|
654
764
|
try {
|
|
655
765
|
target = fs.readlinkSync(path.join(PROFILE_DIR, "SingletonLock"));
|
|
@@ -675,7 +785,7 @@ function profileOwnerPid() {
|
|
|
675
785
|
* 本人用 GUID 确认身份。**于是「认领到别的 profile 的浏览器」从结构上不成立**。
|
|
676
786
|
*/
|
|
677
787
|
async function adoptRunning({ headless, procs = null }) {
|
|
678
|
-
const owner = profileOwnerPid();
|
|
788
|
+
const owner = profileOwnerPid(procs);
|
|
679
789
|
const ep0 = readEndpoint();
|
|
680
790
|
if (!owner && !ep0) return null;
|
|
681
791
|
|
|
@@ -809,8 +919,8 @@ export async function ensureBrowser({ headless = HEADLESS } = {}) {
|
|
|
809
919
|
);
|
|
810
920
|
}
|
|
811
921
|
try {
|
|
812
|
-
const holder = profileOwnerPid();
|
|
813
|
-
if (holder && pidRunsThisProfile(holder))
|
|
922
|
+
const holder = profileOwnerPid(procs0);
|
|
923
|
+
if (holder && pidRunsThisProfile(holder, procs0))
|
|
814
924
|
throw new Error(
|
|
815
925
|
`这个 profile 已经被另一个浏览器实例占着(pid=${holder}):${PROFILE_DIR}\n` +
|
|
816
926
|
`而当前是管道传输,它的 CDP 只有起它的那个进程用得上——认领不了,也不能另起一个\n` +
|
|
@@ -942,15 +1052,17 @@ async function coldStart({ headless }) {
|
|
|
942
1052
|
rotateIfBig(BROWSER_LOG);
|
|
943
1053
|
const out = fs.openSync(BROWSER_LOG, "a");
|
|
944
1054
|
const child = USE_PIPE
|
|
945
|
-
? spawn(bin, browserArgs({ headless, bin }), { stdio: ["ignore", out, out, "pipe", "pipe"] })
|
|
946
|
-
: spawn(bin, browserArgs({ headless, bin }), { stdio: ["ignore", out, out], detached: true });
|
|
1055
|
+
? spawn(bin, browserArgs({ headless, bin }), { stdio: ["ignore", out, out, "pipe", "pipe"], windowsHide: true })
|
|
1056
|
+
: spawn(bin, browserArgs({ headless, bin }), { stdio: ["ignore", out, out], detached: true, windowsHide: true });
|
|
947
1057
|
if (!USE_PIPE) child.unref();
|
|
948
1058
|
try {
|
|
949
1059
|
fs.closeSync(out);
|
|
950
1060
|
} catch {}
|
|
951
1061
|
|
|
952
1062
|
const ep = USE_PIPE ? await pipeEndpoint(child) : await waitForEndpoint();
|
|
953
|
-
|
|
1063
|
+
const childRuns =
|
|
1064
|
+
process.platform === "win32" ? child.exitCode === null && child.signalCode === null : pidRunsThisProfile(child.pid);
|
|
1065
|
+
if (!childRuns) {
|
|
954
1066
|
log(`冷启动的孩子 pid=${child.pid} 已不在(输掉了 ProcessSingleton),转认领真正的占用者`);
|
|
955
1067
|
const winner = await adoptRunning({ headless });
|
|
956
1068
|
if (winner) return winner;
|
|
@@ -1012,7 +1124,7 @@ export function stopBrowser({ force = false } = {}) {
|
|
|
1012
1124
|
log(`不收浏览器 pid=${rec.pid}:它归 pid=${mgr} 管,那个进程还活着`);
|
|
1013
1125
|
return false;
|
|
1014
1126
|
}
|
|
1015
|
-
const groupKill = rec.transport !== "pipe";
|
|
1127
|
+
const groupKill = process.platform !== "win32" && rec.transport !== "pipe";
|
|
1016
1128
|
try {
|
|
1017
1129
|
if (groupKill) process.kill(-rec.pid, "SIGTERM");
|
|
1018
1130
|
else process.kill(rec.pid, "SIGTERM");
|
|
@@ -1085,7 +1197,7 @@ export async function stopBrowserGracefully({ timeoutMs = 10_000, force = true }
|
|
|
1085
1197
|
if (stillAlive) {
|
|
1086
1198
|
log(`pid=${pid} 没理 SIGTERM,升级到 SIGKILL`);
|
|
1087
1199
|
try {
|
|
1088
|
-
if (rec.transport !== "pipe") process.kill(-pid, "SIGKILL");
|
|
1200
|
+
if (process.platform !== "win32" && rec.transport !== "pipe") process.kill(-pid, "SIGKILL");
|
|
1089
1201
|
else process.kill(pid, "SIGKILL");
|
|
1090
1202
|
} catch {
|
|
1091
1203
|
try {
|
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, codexThreadId, workbuddyConversationId, isOpencodeClient } from "./agent-identity.mjs";
|
|
31
|
+
import { probeLocal, readSessionIdentity, composeAgent, procSource, readClaudeSubagent, readDshSubagent, 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.
|
|
53
|
+
const VERSION = "0.55.5";
|
|
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"];
|
|
@@ -66,27 +66,8 @@ const TOOLS_TTL_MS = 60_000;
|
|
|
66
66
|
const DISCOVER_TTL_MS = 600_000;
|
|
67
67
|
|
|
68
68
|
/*
|
|
69
|
-
* Windows
|
|
69
|
+
* Windows 的 CLI/headless 模式(现已支持)。
|
|
70
70
|
*/
|
|
71
|
-
if (CDP_LAUNCH && process.platform === "win32") {
|
|
72
|
-
process.stderr.write(
|
|
73
|
-
[
|
|
74
|
-
"[agent-in-chrome] Windows 上还不支持 CLI/headless 模式(AGENT_IN_CHROME_LAUNCH=1),已拒绝启动。",
|
|
75
|
-
"",
|
|
76
|
-
"为什么不支持:这个模式要自己起浏览器、也要自己收回来,而「收回来」依赖的三个数据源",
|
|
77
|
-
" (ps 找子进程、按 profile 认领、SingletonLock 符号链接)在 Windows 上全部失效。",
|
|
78
|
-
" 实测结果是 server 退出后留下一堆 chrome.exe,它们的调试端口没有任何访问控制、仍在应答,",
|
|
79
|
-
" 任何本机进程都能接管那个带着你登录态的浏览器。所以宁可不启动。",
|
|
80
|
-
" 设 AGENT_IN_CHROME_BIN 也绕不过去 —— 它绕过的只是「找不到浏览器」,不是这条。",
|
|
81
|
-
"",
|
|
82
|
-
"插件模式不受影响:去掉 AGENT_IN_CHROME_LAUNCH(和 AGENT_IN_CHROME_BIN)重启即可,",
|
|
83
|
-
" 它驱动的是你自己那个装了扩展的 Chrome。装没装好用 npx @liang-hz/agent-in-chrome check 自检。",
|
|
84
|
-
"",
|
|
85
|
-
"跟进:README 的「平台」一节,以及 mcp/cdp/browser-launch.mjs 里的进程管理注释。",
|
|
86
|
-
].join("\n") + "\n"
|
|
87
|
-
);
|
|
88
|
-
process.exit(1);
|
|
89
|
-
}
|
|
90
71
|
|
|
91
72
|
function defaultSockName() {
|
|
92
73
|
if (!CDP_LAUNCH) return "agent-in-chrome.sock";
|
|
@@ -471,6 +452,7 @@ function agentForFrame() {
|
|
|
471
452
|
let task = null;
|
|
472
453
|
try {
|
|
473
454
|
if (LOCAL_ID && LAST_TOOL_USE) task = readClaudeSubagent({ "claudecode/toolUseId": LAST_TOOL_USE }, LOCAL_ID);
|
|
455
|
+
if (!task && LOCAL_ID) task = readDshSubagent(LAST_META, LOCAL_ID);
|
|
474
456
|
} catch {}
|
|
475
457
|
if (!agent) return task ? { task: task.label, taskType: task.type } : null;
|
|
476
458
|
return { ...agent, task: task?.label || null, taskType: task?.type || null };
|
package/native-host/host.mjs
CHANGED
|
@@ -138,6 +138,9 @@ function authToken() {
|
|
|
138
138
|
let sock = null;
|
|
139
139
|
let sockBuf = "";
|
|
140
140
|
let reconnectTimer = null;
|
|
141
|
+
let outboundReady = false;
|
|
142
|
+
let outboundQueue = [];
|
|
143
|
+
const OUTBOUND_QUEUE_MAX = 64;
|
|
141
144
|
let lastExtHello = null;
|
|
142
145
|
let authWarned = false;
|
|
143
146
|
|
|
@@ -159,6 +162,12 @@ function toAgent(obj) {
|
|
|
159
162
|
}
|
|
160
163
|
}
|
|
161
164
|
|
|
165
|
+
function flushOutbound() {
|
|
166
|
+
while (outboundReady && outboundQueue.length && sock && !sock.destroyed) {
|
|
167
|
+
if (!toAgent(outboundQueue.shift())) break;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
162
171
|
let lastDropWhy = null;
|
|
163
172
|
let dropRepeats = 0;
|
|
164
173
|
function flushDropRepeats() {
|
|
@@ -203,6 +212,10 @@ function connectSocket() {
|
|
|
203
212
|
if (sock !== s) return;
|
|
204
213
|
if (!toAgent(frame)) return;
|
|
205
214
|
if (lastExtHello) toAgent(lastExtHello);
|
|
215
|
+
if (LEGACY_TOKEN_HELLO_OK) {
|
|
216
|
+
outboundReady = true;
|
|
217
|
+
flushOutbound();
|
|
218
|
+
}
|
|
206
219
|
};
|
|
207
220
|
let dropWarned = false;
|
|
208
221
|
|
|
@@ -220,6 +233,7 @@ function connectSocket() {
|
|
|
220
233
|
s.on("connect", () => {
|
|
221
234
|
sock = s;
|
|
222
235
|
sockBuf = "";
|
|
236
|
+
outboundReady = false;
|
|
223
237
|
flushDropRepeats();
|
|
224
238
|
lastDropWhy = null;
|
|
225
239
|
reconnectDelay = RECONNECT_MS;
|
|
@@ -261,6 +275,8 @@ function connectSocket() {
|
|
|
261
275
|
impostor = true;
|
|
262
276
|
drop("对面拿不出反向证明:占着这个端点的不是主(令牌没有发出去)");
|
|
263
277
|
}
|
|
278
|
+
outboundReady = true;
|
|
279
|
+
flushOutbound();
|
|
264
280
|
continue;
|
|
265
281
|
}
|
|
266
282
|
if (msg.type === "error" && msg.code === "unauthorized") {
|
|
@@ -292,6 +308,7 @@ function connectSocket() {
|
|
|
292
308
|
dropped = true;
|
|
293
309
|
clearTimeout(hsTimer);
|
|
294
310
|
if (sock === s) sock = null;
|
|
311
|
+
outboundReady = false;
|
|
295
312
|
s.destroy();
|
|
296
313
|
noteDrop(why);
|
|
297
314
|
notifyAgentLink(false);
|
|
@@ -344,6 +361,17 @@ function onFromExtension(msg) {
|
|
|
344
361
|
}
|
|
345
362
|
return;
|
|
346
363
|
}
|
|
364
|
+
if (sock && !sock.destroyed && !outboundReady) {
|
|
365
|
+
if (outboundQueue.length >= OUTBOUND_QUEUE_MAX) {
|
|
366
|
+
const stale = outboundQueue.shift();
|
|
367
|
+
log(
|
|
368
|
+
`握手未完成,出站积压超过 ${OUTBOUND_QUEUE_MAX} 条,丢弃最早的一帧:`,
|
|
369
|
+
stale.type || "无 type"
|
|
370
|
+
);
|
|
371
|
+
}
|
|
372
|
+
outboundQueue.push(msg);
|
|
373
|
+
return;
|
|
374
|
+
}
|
|
347
375
|
if (!toAgent(msg)) {
|
|
348
376
|
log("丢弃(agent 未连接):", msg.type, msg.id || "");
|
|
349
377
|
}
|
package/package.json
CHANGED
package/scripts/borrow-login.mjs
CHANGED
|
@@ -29,6 +29,7 @@ import os from "node:os";
|
|
|
29
29
|
import path from "node:path";
|
|
30
30
|
import { fileURLToPath, pathToFileURL } from "node:url";
|
|
31
31
|
import { scriptCmd, checkCmd, reinstallCmd } from "../mcp/cli-invocation.mjs";
|
|
32
|
+
import { launcherNeedsShell, cmdq } from "./launcher-spawn.mjs";
|
|
32
33
|
|
|
33
34
|
const ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..");
|
|
34
35
|
|
|
@@ -394,13 +395,16 @@ const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
|
|
|
394
395
|
function bridgeServerCmd() {
|
|
395
396
|
const override = process.env.AGENT_IN_CHROME_BORROW_SERVER;
|
|
396
397
|
if (override) return override.endsWith(".mjs") ? [process.execPath, [override]] : [override, []];
|
|
397
|
-
const installed = path.join(os.homedir(), ".agent-in-chrome", "agent-in-chrome", "mcp-launcher.sh");
|
|
398
|
+
const installed = path.join(os.homedir(), ".agent-in-chrome", "agent-in-chrome", process.platform === "win32" ? "mcp-launcher.bat" : "mcp-launcher.sh");
|
|
398
399
|
if (fs.existsSync(installed)) return [installed, []];
|
|
399
400
|
return [process.execPath, [path.join(ROOT, "mcp", "server.mjs")]];
|
|
400
401
|
}
|
|
401
402
|
|
|
402
403
|
function mcpClient(cmd, args, env, label) {
|
|
403
|
-
const p =
|
|
404
|
+
const p =
|
|
405
|
+
launcherNeedsShell(cmd)
|
|
406
|
+
? spawn(cmdq(cmd), { stdio: ["pipe", "pipe", "pipe"], env, shell: true, windowsHide: true })
|
|
407
|
+
: spawn(cmd, args, { stdio: ["pipe", "pipe", "pipe"], env });
|
|
404
408
|
let errText = "";
|
|
405
409
|
p.stderr.setEncoding("utf8");
|
|
406
410
|
p.stderr.on("data", (c) => {
|
package/scripts/e2e-sweep.mjs
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { spawn } from "node:child_process";
|
|
2
1
|
import os from "node:os";
|
|
3
2
|
import path from "node:path";
|
|
4
3
|
import fs from "node:fs";
|
|
5
4
|
import { pathToFileURL } from "node:url";
|
|
5
|
+
import { defaultMcpLauncher, spawnMcpLauncher } from "./launcher-spawn.mjs";
|
|
6
6
|
|
|
7
7
|
export const E2E_PREFIX = "e2e-";
|
|
8
8
|
|
|
@@ -69,9 +69,8 @@ function pidAlive(pid) {
|
|
|
69
69
|
|
|
70
70
|
function sweepClient(sessionId) {
|
|
71
71
|
const launcher =
|
|
72
|
-
process.env.AGENT_IN_CHROME_E2E_LAUNCHER ||
|
|
73
|
-
|
|
74
|
-
const p = spawn(launcher, [], {
|
|
72
|
+
process.env.AGENT_IN_CHROME_E2E_LAUNCHER || defaultMcpLauncher();
|
|
73
|
+
const p = spawnMcpLauncher(launcher, {
|
|
75
74
|
stdio: ["pipe", "pipe", "pipe"],
|
|
76
75
|
env: { ...process.env, AGENT_IN_CHROME_SESSION_ID: sessionId },
|
|
77
76
|
});
|