@hanzo/bot 2026.3.8 → 2026.3.10

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.
Files changed (161) hide show
  1. package/dist/{audio-preflight-D_s-peid.js → audio-preflight-BnfuyvxO.js} +4 -4
  2. package/dist/{audio-preflight-BEc8i-bS.js → audio-preflight-CpAXC_Ct.js} +4 -4
  3. package/dist/{audio-transcription-runner-X1KzI7dF.js → audio-transcription-runner-CAOjjGxN.js} +1 -1
  4. package/dist/{audio-transcription-runner-BePCnZfw.js → audio-transcription-runner-GcMnO6sT.js} +1 -1
  5. package/dist/build-info.json +3 -3
  6. package/dist/bundled/boot-md/handler.js +6 -6
  7. package/dist/bundled/session-memory/handler.js +6 -6
  8. package/dist/canvas-host/a2ui/.bundle.hash +1 -1
  9. package/dist/{chrome-B24-8NDM.js → chrome--CFg5C_H.js} +8 -8
  10. package/dist/{chrome-C7OwLtx9.js → chrome-jCt9JCU8.js} +8 -8
  11. package/dist/{cloud-connect-CknfBF39.js → cloud-connect-6kdj8st_.js} +1 -1
  12. package/dist/{deliver-D8dBbzpu.js → deliver-BVtVDxwX.js} +1 -1
  13. package/dist/{deliver-DudaV86i.js → deliver-DmfS4khs.js} +1 -1
  14. package/dist/{deliver-runtime-C76IMU4W.js → deliver-runtime-G0G5orrZ.js} +3 -3
  15. package/dist/{deliver-runtime-qDmQqiF-.js → deliver-runtime-PxJvVUhh.js} +3 -3
  16. package/dist/{deps-send-whatsapp.runtime-Cv_awFtm.js → deps-send-whatsapp.runtime-8bLqjmui.js} +7 -7
  17. package/dist/{deps-send-whatsapp.runtime-Cq-TLsJw.js → deps-send-whatsapp.runtime-CrzuaVhC.js} +7 -7
  18. package/dist/entry.js +15 -8
  19. package/dist/extensionAPI.js +6 -6
  20. package/dist/{image-nUHQF6BX.js → image-BdZcUz8M.js} +1 -1
  21. package/dist/{image-BOybyCis.js → image-DSK1hSSV.js} +1 -1
  22. package/dist/{image-runtime-B5M_-diF.js → image-runtime-ueqmfx1a.js} +3 -3
  23. package/dist/{image-runtime-y4msd5bn.js → image-runtime-xqxW2PQA.js} +3 -3
  24. package/dist/llm-slug-generator.js +6 -6
  25. package/dist/{local-launch-C2RER-G3.js → local-launch-BJpBAIR5.js} +37 -33
  26. package/dist/{pi-embedded-BHXPs-Ix.js → pi-embedded-DBn841N-.js} +24 -24
  27. package/dist/{pi-embedded-DvWHP6Nn.js → pi-embedded-DYc6emwb.js} +24 -24
  28. package/dist/{pi-embedded-helpers-xIXwvwuE.js → pi-embedded-helpers-BtnBVL-4.js} +3 -3
  29. package/dist/{pi-embedded-helpers-Ck1qEeMH.js → pi-embedded-helpers-DLm1Mtr2.js} +3 -3
  30. package/dist/plugin-sdk/accounts-B8qv93DH.js +35 -0
  31. package/dist/plugin-sdk/accounts-D2p8t4UO.js +288 -0
  32. package/dist/plugin-sdk/accounts-D96D1U9M.js +46 -0
  33. package/dist/plugin-sdk/active-listener-Mha1rAbh.js +50 -0
  34. package/dist/plugin-sdk/api-key-rotation-D0aZfxXH.js +181 -0
  35. package/dist/plugin-sdk/audio-preflight-D3y8mHJa.js +69 -0
  36. package/dist/plugin-sdk/audio-transcription-runner-j0mQXKSH.js +2205 -0
  37. package/dist/plugin-sdk/audit-membership-runtime-D-Ni2WDc.js +58 -0
  38. package/dist/plugin-sdk/channel-activity-VpA3MxPb.js +94 -0
  39. package/dist/plugin-sdk/channel-web-BP3vDdim.js +2256 -0
  40. package/dist/plugin-sdk/chrome-5jJIDTj0.js +2447 -0
  41. package/dist/plugin-sdk/commands-registry-BVKCdwN_.js +1125 -0
  42. package/dist/plugin-sdk/config-CudVTZDi.js +18200 -0
  43. package/dist/plugin-sdk/deliver-4NrmrRKu.js +1744 -0
  44. package/dist/plugin-sdk/deliver-runtime-CB4wXMTH.js +32 -0
  45. package/dist/plugin-sdk/deps-send-discord.runtime-Di8ELKED.js +23 -0
  46. package/dist/plugin-sdk/deps-send-imessage.runtime-CWWtApbz.js +22 -0
  47. package/dist/plugin-sdk/deps-send-signal.runtime-C6BGyoIY.js +21 -0
  48. package/dist/plugin-sdk/deps-send-slack.runtime-DgtITBuc.js +19 -0
  49. package/dist/plugin-sdk/deps-send-telegram.runtime-DyY4XRxh.js +24 -0
  50. package/dist/plugin-sdk/deps-send-whatsapp.runtime-LKiQNFcF.js +57 -0
  51. package/dist/plugin-sdk/diagnostic-DCPixRez.js +319 -0
  52. package/dist/plugin-sdk/errors-D5bA02--.js +54 -0
  53. package/dist/plugin-sdk/fetch-guard-n0LVdzZL.js +156 -0
  54. package/dist/plugin-sdk/fs-safe-IQ0H7rVD.js +352 -0
  55. package/dist/plugin-sdk/image-n-R2HcNg.js +2314 -0
  56. package/dist/plugin-sdk/image-ops-BpYDXC6N.js +584 -0
  57. package/dist/plugin-sdk/image-runtime-CbCl82B8.js +25 -0
  58. package/dist/plugin-sdk/index.js +50 -50
  59. package/dist/plugin-sdk/ir-DsMX3GcS.js +1296 -0
  60. package/dist/plugin-sdk/local-roots-DR-lR22p.js +186 -0
  61. package/dist/plugin-sdk/logger-2A0UE34q.js +1163 -0
  62. package/dist/plugin-sdk/login-CxFTtHEi.js +57 -0
  63. package/dist/plugin-sdk/login-qr-BCkrf1Zx.js +320 -0
  64. package/dist/plugin-sdk/manager-ClUgSFkG.js +3943 -0
  65. package/dist/plugin-sdk/manager-runtime-MWzYCRyH.js +15 -0
  66. package/dist/plugin-sdk/outbound-Dqs8L8QW.js +212 -0
  67. package/dist/plugin-sdk/outbound-attachment-CPfpUcdI.js +19 -0
  68. package/dist/plugin-sdk/path-alias-guards-LILr7Hrs.js +43 -0
  69. package/dist/plugin-sdk/paths-CfGmXu9A.js +166 -0
  70. package/dist/plugin-sdk/pi-embedded-helpers-CeC8GbRi.js +9731 -0
  71. package/dist/plugin-sdk/pi-model-discovery-DycOMKYh.js +134 -0
  72. package/dist/plugin-sdk/pi-model-discovery-runtime-C64BYe5F.js +8 -0
  73. package/dist/plugin-sdk/pi-tools.before-tool-call.runtime-C-HNtPSw.js +354 -0
  74. package/dist/plugin-sdk/plugins-2gQWMmUN.js +1205 -0
  75. package/dist/plugin-sdk/proxy-fetch-sX3-xzX1.js +38 -0
  76. package/dist/plugin-sdk/pw-ai-D7FTxM3C.js +1938 -0
  77. package/dist/plugin-sdk/qmd-manager-Da3Jq30m.js +1608 -0
  78. package/dist/plugin-sdk/query-expansion-B5Z0In1U.js +1014 -0
  79. package/dist/plugin-sdk/redact-85H1J7mo.js +319 -0
  80. package/dist/plugin-sdk/reply-DmCyOPxV.js +102224 -0
  81. package/dist/plugin-sdk/resolve-outbound-target-y0Sp7gsM.js +40 -0
  82. package/dist/plugin-sdk/run-with-concurrency-CFRxflYW.js +1994 -0
  83. package/dist/plugin-sdk/runtime-whatsapp-login.runtime-fgm84Rdh.js +10 -0
  84. package/dist/plugin-sdk/runtime-whatsapp-outbound.runtime-DpMuLd_h.js +19 -0
  85. package/dist/plugin-sdk/send-BQvcPd54.js +3135 -0
  86. package/dist/plugin-sdk/send-Bplfz7UW.js +540 -0
  87. package/dist/plugin-sdk/send-C8gdhoLP.js +414 -0
  88. package/dist/plugin-sdk/send-CTOVZqmi.js +2602 -0
  89. package/dist/plugin-sdk/send-Cld7xlxq.js +503 -0
  90. package/dist/plugin-sdk/session-D4k86ARy.js +169 -0
  91. package/dist/plugin-sdk/signal.js +2 -2
  92. package/dist/plugin-sdk/skill-commands-BfHvtJx2.js +353 -0
  93. package/dist/plugin-sdk/skills-BrE5Yb5o.js +1428 -0
  94. package/dist/plugin-sdk/slash-commands.runtime-UpSrdY-a.js +13 -0
  95. package/dist/plugin-sdk/slash-dispatch.runtime-C-Ymizf2.js +52 -0
  96. package/dist/plugin-sdk/slash-skill-commands.runtime-Bb9wo3w0.js +16 -0
  97. package/dist/plugin-sdk/ssrf-CbvrROKN.js +202 -0
  98. package/dist/plugin-sdk/store-8XS_isi_.js +81 -0
  99. package/dist/plugin-sdk/subagent-registry-runtime-Cl3jJKM1.js +52 -0
  100. package/dist/plugin-sdk/tables-BhfDBQ58.js +55 -0
  101. package/dist/plugin-sdk/target-errors-0DW3k-Ae.js +195 -0
  102. package/dist/plugin-sdk/thinking-DE2FCBnv.js +1209 -0
  103. package/dist/plugin-sdk/tokens-C2tJ8uXs.js +52 -0
  104. package/dist/plugin-sdk/tool-images-B1I6LEp7.js +274 -0
  105. package/dist/plugin-sdk/web-BHzDLmns.js +56 -0
  106. package/dist/plugin-sdk/whatsapp-actions-BoAH0BAS.js +80 -0
  107. package/dist/{pw-ai-DweqbnMJ.js → pw-ai-C-Sy12jT.js} +1 -1
  108. package/dist/{pw-ai-DsYmOxCp.js → pw-ai-pJMhS79V.js} +1 -1
  109. package/dist/{run-main-CgFUs81l.js → run-main-JI9-1g4u.js} +2 -2
  110. package/dist/{slash-dispatch.runtime-D28-UnsO.js → slash-dispatch.runtime-DLP2IeNv.js} +6 -6
  111. package/dist/{slash-dispatch.runtime-DzpJjr3K.js → slash-dispatch.runtime-Vp6IDoCc.js} +6 -6
  112. package/dist/{subagent-registry-runtime-a7xfwPB8.js → subagent-registry-runtime-BlAI3eqU.js} +6 -6
  113. package/dist/{subagent-registry-runtime-Bt-LYyrB.js → subagent-registry-runtime-COKZwsHd.js} +6 -6
  114. package/dist/{web-CREcqhe9.js → web-BEuMJbx-.js} +6 -6
  115. package/dist/{web-IBqHOVI2.js → web-BvId86u4.js} +6 -6
  116. package/extensions/acpx/package.json +1 -1
  117. package/extensions/bluebubbles/package.json +1 -1
  118. package/extensions/ci-fix-loop/package.json +1 -1
  119. package/extensions/continuous-learning/package.json +1 -1
  120. package/extensions/copilot-proxy/package.json +1 -1
  121. package/extensions/diagnostics-otel/package.json +1 -1
  122. package/extensions/diffs/package.json +1 -1
  123. package/extensions/discord/package.json +1 -1
  124. package/extensions/feishu/package.json +1 -1
  125. package/extensions/flow/package.json +1 -1
  126. package/extensions/google-antigravity-auth/package.json +1 -1
  127. package/extensions/google-gemini-cli-auth/package.json +1 -1
  128. package/extensions/googlechat/package.json +1 -1
  129. package/extensions/imessage/package.json +1 -1
  130. package/extensions/irc/package.json +1 -1
  131. package/extensions/line/package.json +1 -1
  132. package/extensions/llm-task/package.json +1 -1
  133. package/extensions/lobster/package.json +1 -1
  134. package/extensions/matrix/CHANGELOG.md +10 -0
  135. package/extensions/matrix/package.json +1 -1
  136. package/extensions/mattermost/package.json +1 -1
  137. package/extensions/memory-core/package.json +1 -1
  138. package/extensions/memory-lancedb/package.json +1 -1
  139. package/extensions/minimax-portal-auth/package.json +1 -1
  140. package/extensions/msteams/CHANGELOG.md +10 -0
  141. package/extensions/msteams/package.json +1 -1
  142. package/extensions/nextcloud-talk/package.json +1 -1
  143. package/extensions/nostr/CHANGELOG.md +10 -0
  144. package/extensions/nostr/package.json +1 -1
  145. package/extensions/open-prose/package.json +1 -1
  146. package/extensions/self-improvement/package.json +1 -1
  147. package/extensions/signal/package.json +1 -1
  148. package/extensions/slack/package.json +1 -1
  149. package/extensions/synology-chat/package.json +1 -1
  150. package/extensions/telegram/package.json +1 -1
  151. package/extensions/tlon/package.json +1 -1
  152. package/extensions/twitch/CHANGELOG.md +10 -0
  153. package/extensions/twitch/package.json +1 -1
  154. package/extensions/voice-call/CHANGELOG.md +10 -0
  155. package/extensions/voice-call/package.json +1 -1
  156. package/extensions/whatsapp/package.json +1 -1
  157. package/extensions/zalo/CHANGELOG.md +10 -0
  158. package/extensions/zalo/package.json +1 -1
  159. package/extensions/zalouser/CHANGELOG.md +10 -0
  160. package/extensions/zalouser/package.json +1 -1
  161. package/package.json +2 -1
@@ -0,0 +1,2447 @@
1
+ import { R as isNotFoundPathError, z as isPathInside } from "./run-with-concurrency-CFRxflYW.js";
2
+ import { Cr as secretRefKey, Jr as normalizeSecretInputString, Q as normalizeIpAddress, X as isLoopbackIpAddress, Yr as resolveSecretInputRef, Z as isPrivateOrLoopbackIpAddress, gr as resolveSecretRefValues, n as loadConfig } from "./config-CudVTZDi.js";
3
+ import { X as resolvePreferredOpenClawTmpDir, a as createSubsystemLogger, f as CONFIG_DIR } from "./logger-2A0UE34q.js";
4
+ import { a as isErrno } from "./errors-D5bA02--.js";
5
+ import { i as openFileWithinRoot, t as SafeOpenError } from "./fs-safe-IQ0H7rVD.js";
6
+ import { c as resolvePinnedHostnameWithPolicy, s as isPrivateNetworkAllowedByPolicy, u as hasProxyEnvConfigured } from "./ssrf-CbvrROKN.js";
7
+ import fs from "node:fs";
8
+ import path from "node:path";
9
+ import os from "node:os";
10
+ import fs$1 from "node:fs/promises";
11
+ import { createHmac } from "node:crypto";
12
+ import { execFileSync, spawn } from "node:child_process";
13
+ import { Buffer as Buffer$1 } from "node:buffer";
14
+ import http, { createServer } from "node:http";
15
+ import https from "node:https";
16
+ import WebSocket, { WebSocketServer } from "ws";
17
+ import net from "node:net";
18
+
19
+ //#region src/infra/ws.ts
20
+ function rawDataToString(data, encoding = "utf8") {
21
+ if (typeof data === "string") return data;
22
+ if (Buffer$1.isBuffer(data)) return data.toString(encoding);
23
+ if (Array.isArray(data)) return Buffer$1.concat(data).toString(encoding);
24
+ if (data instanceof ArrayBuffer) return Buffer$1.from(data).toString(encoding);
25
+ return Buffer$1.from(String(data)).toString(encoding);
26
+ }
27
+
28
+ //#endregion
29
+ //#region src/gateway/net.ts
30
+ function isLoopbackAddress(ip) {
31
+ return isLoopbackIpAddress(ip);
32
+ }
33
+ /**
34
+ * Returns true if the IP belongs to a private or loopback network range.
35
+ * Private ranges: RFC1918, link-local, ULA IPv6, and CGNAT (100.64/10), plus loopback.
36
+ */
37
+ function isPrivateOrLoopbackAddress(ip) {
38
+ return isPrivateOrLoopbackIpAddress(ip);
39
+ }
40
+ function normalizeIp(ip) {
41
+ return normalizeIpAddress(ip);
42
+ }
43
+ /**
44
+ * Check if a hostname or IP refers to the local machine.
45
+ * Handles: localhost, 127.x.x.x, ::1, [::1], ::ffff:127.x.x.x
46
+ * Note: 0.0.0.0 and :: are NOT loopback - they bind to all interfaces.
47
+ */
48
+ function isLoopbackHost(host) {
49
+ const parsed = parseHostForAddressChecks(host);
50
+ if (!parsed) return false;
51
+ if (parsed.isLocalhost) return true;
52
+ return isLoopbackAddress(parsed.unbracketedHost);
53
+ }
54
+ /**
55
+ * Check if a hostname or IP refers to a private or loopback address.
56
+ * Handles the same hostname formats as isLoopbackHost, but also accepts
57
+ * RFC 1918, link-local, CGNAT, and IPv6 ULA/link-local addresses.
58
+ */
59
+ function isPrivateOrLoopbackHost(host) {
60
+ const parsed = parseHostForAddressChecks(host);
61
+ if (!parsed) return false;
62
+ if (parsed.isLocalhost) return true;
63
+ const normalized = normalizeIp(parsed.unbracketedHost);
64
+ if (!normalized || !isPrivateOrLoopbackAddress(normalized)) return false;
65
+ if (net.isIP(normalized) === 6) {
66
+ if (normalized.startsWith("ff")) return false;
67
+ if (normalized === "::") return false;
68
+ }
69
+ return true;
70
+ }
71
+ function parseHostForAddressChecks(host) {
72
+ if (!host) return null;
73
+ const normalizedHost = host.trim().toLowerCase();
74
+ if (normalizedHost === "localhost") return {
75
+ isLocalhost: true,
76
+ unbracketedHost: normalizedHost
77
+ };
78
+ return {
79
+ isLocalhost: false,
80
+ unbracketedHost: normalizedHost.startsWith("[") && normalizedHost.endsWith("]") ? normalizedHost.slice(1, -1) : normalizedHost
81
+ };
82
+ }
83
+ /**
84
+ * Security check for WebSocket URLs (CWE-319: Cleartext Transmission of Sensitive Information).
85
+ *
86
+ * Returns true if the URL is secure for transmitting data:
87
+ * - wss:// (TLS) is always secure
88
+ * - ws:// is secure only for loopback addresses by default
89
+ * - optional break-glass: private ws:// can be enabled for trusted networks
90
+ *
91
+ * All other ws:// URLs are considered insecure because both credentials
92
+ * AND chat/conversation data would be exposed to network interception.
93
+ */
94
+ function isSecureWebSocketUrl(url, opts) {
95
+ let parsed;
96
+ try {
97
+ parsed = new URL(url);
98
+ } catch {
99
+ return false;
100
+ }
101
+ if (parsed.protocol === "wss:") return true;
102
+ if (parsed.protocol !== "ws:") return false;
103
+ if (isLoopbackHost(parsed.hostname)) return true;
104
+ if (opts?.allowPrivateWs) {
105
+ if (isPrivateOrLoopbackHost(parsed.hostname)) return true;
106
+ const hostForIpCheck = parsed.hostname.startsWith("[") && parsed.hostname.endsWith("]") ? parsed.hostname.slice(1, -1) : parsed.hostname;
107
+ return net.isIP(hostForIpCheck) === 0;
108
+ }
109
+ return false;
110
+ }
111
+
112
+ //#endregion
113
+ //#region src/browser/constants.ts
114
+ const DEFAULT_OPENCLAW_BROWSER_ENABLED = true;
115
+ const DEFAULT_BROWSER_EVALUATE_ENABLED = true;
116
+ const DEFAULT_OPENCLAW_BROWSER_COLOR = "#FF4500";
117
+ const DEFAULT_OPENCLAW_BROWSER_PROFILE_NAME = "openclaw";
118
+ const DEFAULT_BROWSER_DEFAULT_PROFILE_NAME = "openclaw";
119
+ const DEFAULT_AI_SNAPSHOT_MAX_CHARS = 8e4;
120
+ const DEFAULT_AI_SNAPSHOT_EFFICIENT_MAX_CHARS = 1e4;
121
+ const DEFAULT_AI_SNAPSHOT_EFFICIENT_DEPTH = 6;
122
+
123
+ //#endregion
124
+ //#region src/browser/form-fields.ts
125
+ const DEFAULT_FILL_FIELD_TYPE = "text";
126
+ function normalizeBrowserFormFieldRef(value) {
127
+ return typeof value === "string" ? value.trim() : "";
128
+ }
129
+ function normalizeBrowserFormFieldType(value) {
130
+ return (typeof value === "string" ? value.trim() : "") || DEFAULT_FILL_FIELD_TYPE;
131
+ }
132
+ function normalizeBrowserFormFieldValue(value) {
133
+ return typeof value === "string" || typeof value === "number" || typeof value === "boolean" ? value : void 0;
134
+ }
135
+ function normalizeBrowserFormField(record) {
136
+ const ref = normalizeBrowserFormFieldRef(record.ref);
137
+ if (!ref) return null;
138
+ const type = normalizeBrowserFormFieldType(record.type);
139
+ const value = normalizeBrowserFormFieldValue(record.value);
140
+ return value === void 0 ? {
141
+ ref,
142
+ type
143
+ } : {
144
+ ref,
145
+ type,
146
+ value
147
+ };
148
+ }
149
+
150
+ //#endregion
151
+ //#region src/browser/paths.ts
152
+ const DEFAULT_BROWSER_TMP_DIR = resolvePreferredOpenClawTmpDir();
153
+ const DEFAULT_TRACE_DIR = DEFAULT_BROWSER_TMP_DIR;
154
+ const DEFAULT_DOWNLOAD_DIR = path.join(DEFAULT_BROWSER_TMP_DIR, "downloads");
155
+ const DEFAULT_UPLOAD_DIR = path.join(DEFAULT_BROWSER_TMP_DIR, "uploads");
156
+ function invalidPath(scopeLabel) {
157
+ return {
158
+ ok: false,
159
+ error: `Invalid path: must stay within ${scopeLabel}`
160
+ };
161
+ }
162
+ async function resolveRealPathIfExists(targetPath) {
163
+ try {
164
+ return await fs$1.realpath(targetPath);
165
+ } catch {
166
+ return;
167
+ }
168
+ }
169
+ async function resolveTrustedRootRealPath(rootDir) {
170
+ try {
171
+ const rootLstat = await fs$1.lstat(rootDir);
172
+ if (!rootLstat.isDirectory() || rootLstat.isSymbolicLink()) return;
173
+ return await fs$1.realpath(rootDir);
174
+ } catch {
175
+ return;
176
+ }
177
+ }
178
+ async function validateCanonicalPathWithinRoot(params) {
179
+ try {
180
+ const candidateLstat = await fs$1.lstat(params.candidatePath);
181
+ if (candidateLstat.isSymbolicLink()) return "invalid";
182
+ if (params.expect === "directory" && !candidateLstat.isDirectory()) return "invalid";
183
+ if (params.expect === "file" && !candidateLstat.isFile()) return "invalid";
184
+ if (params.expect === "file" && candidateLstat.nlink > 1) return "invalid";
185
+ const candidateRealPath = await fs$1.realpath(params.candidatePath);
186
+ return isPathInside(params.rootRealPath, candidateRealPath) ? "ok" : "invalid";
187
+ } catch (err) {
188
+ return isNotFoundPathError(err) ? "not-found" : "invalid";
189
+ }
190
+ }
191
+ function resolvePathWithinRoot(params) {
192
+ const root = path.resolve(params.rootDir);
193
+ const raw = params.requestedPath.trim();
194
+ if (!raw) {
195
+ if (!params.defaultFileName) return {
196
+ ok: false,
197
+ error: "path is required"
198
+ };
199
+ return {
200
+ ok: true,
201
+ path: path.join(root, params.defaultFileName)
202
+ };
203
+ }
204
+ const resolved = path.resolve(root, raw);
205
+ const rel = path.relative(root, resolved);
206
+ if (!rel || rel.startsWith("..") || path.isAbsolute(rel)) return {
207
+ ok: false,
208
+ error: `Invalid path: must stay within ${params.scopeLabel}`
209
+ };
210
+ return {
211
+ ok: true,
212
+ path: resolved
213
+ };
214
+ }
215
+ async function resolveWritablePathWithinRoot(params) {
216
+ const lexical = resolvePathWithinRoot(params);
217
+ if (!lexical.ok) return lexical;
218
+ const rootRealPath = await resolveTrustedRootRealPath(path.resolve(params.rootDir));
219
+ if (!rootRealPath) return invalidPath(params.scopeLabel);
220
+ const requestedPath = lexical.path;
221
+ if (await validateCanonicalPathWithinRoot({
222
+ rootRealPath,
223
+ candidatePath: path.dirname(requestedPath),
224
+ expect: "directory"
225
+ }) !== "ok") return invalidPath(params.scopeLabel);
226
+ if (await validateCanonicalPathWithinRoot({
227
+ rootRealPath,
228
+ candidatePath: requestedPath,
229
+ expect: "file"
230
+ }) === "invalid") return invalidPath(params.scopeLabel);
231
+ return lexical;
232
+ }
233
+ async function resolveExistingPathsWithinRoot(params) {
234
+ return await resolveCheckedPathsWithinRoot({
235
+ ...params,
236
+ allowMissingFallback: true
237
+ });
238
+ }
239
+ async function resolveStrictExistingPathsWithinRoot(params) {
240
+ return await resolveCheckedPathsWithinRoot({
241
+ ...params,
242
+ allowMissingFallback: false
243
+ });
244
+ }
245
+ async function resolveCheckedPathsWithinRoot(params) {
246
+ const rootDir = path.resolve(params.rootDir);
247
+ const rootRealPath = await resolveRealPathIfExists(rootDir);
248
+ const isInRoot = (relativePath) => Boolean(relativePath) && !relativePath.startsWith("..") && !path.isAbsolute(relativePath);
249
+ const resolveExistingRelativePath = async (requestedPath) => {
250
+ const raw = requestedPath.trim();
251
+ const lexicalPathResult = resolvePathWithinRoot({
252
+ rootDir,
253
+ requestedPath,
254
+ scopeLabel: params.scopeLabel
255
+ });
256
+ if (lexicalPathResult.ok) return {
257
+ ok: true,
258
+ relativePath: path.relative(rootDir, lexicalPathResult.path),
259
+ fallbackPath: lexicalPathResult.path
260
+ };
261
+ if (!rootRealPath || !raw || !path.isAbsolute(raw)) return lexicalPathResult;
262
+ try {
263
+ const resolvedExistingPath = await fs$1.realpath(raw);
264
+ const relativePath = path.relative(rootRealPath, resolvedExistingPath);
265
+ if (!isInRoot(relativePath)) return lexicalPathResult;
266
+ return {
267
+ ok: true,
268
+ relativePath,
269
+ fallbackPath: resolvedExistingPath
270
+ };
271
+ } catch {
272
+ return lexicalPathResult;
273
+ }
274
+ };
275
+ const resolvedPaths = [];
276
+ for (const raw of params.requestedPaths) {
277
+ const pathResult = await resolveExistingRelativePath(raw);
278
+ if (!pathResult.ok) return {
279
+ ok: false,
280
+ error: pathResult.error
281
+ };
282
+ let opened;
283
+ try {
284
+ opened = await openFileWithinRoot({
285
+ rootDir,
286
+ relativePath: pathResult.relativePath
287
+ });
288
+ resolvedPaths.push(opened.realPath);
289
+ } catch (err) {
290
+ if (params.allowMissingFallback && err instanceof SafeOpenError && err.code === "not-found") {
291
+ resolvedPaths.push(pathResult.fallbackPath);
292
+ continue;
293
+ }
294
+ if (err instanceof SafeOpenError && err.code === "outside-workspace") return {
295
+ ok: false,
296
+ error: `File is outside ${params.scopeLabel}`
297
+ };
298
+ return {
299
+ ok: false,
300
+ error: `Invalid path: must stay within ${params.scopeLabel} and be a regular non-symlink file`
301
+ };
302
+ } finally {
303
+ await opened?.handle.close().catch(() => {});
304
+ }
305
+ }
306
+ return {
307
+ ok: true,
308
+ paths: resolvedPaths
309
+ };
310
+ }
311
+
312
+ //#endregion
313
+ //#region src/browser/cdp-proxy-bypass.ts
314
+ /**
315
+ * Proxy bypass for CDP (Chrome DevTools Protocol) localhost connections.
316
+ *
317
+ * When HTTP_PROXY / HTTPS_PROXY / ALL_PROXY environment variables are set,
318
+ * CDP connections to localhost/127.0.0.1 can be incorrectly routed through
319
+ * the proxy, causing browser control to fail.
320
+ *
321
+ * @see https://github.com/nicepkg/openclaw/issues/31219
322
+ */
323
+ /** HTTP agent that never uses a proxy — for localhost CDP connections. */
324
+ const directHttpAgent = new http.Agent();
325
+ const directHttpsAgent = new https.Agent();
326
+ /**
327
+ * Returns a plain (non-proxy) agent for WebSocket or HTTP connections
328
+ * when the target is a loopback address. Returns `undefined` otherwise
329
+ * so callers fall through to their default behaviour.
330
+ */
331
+ function getDirectAgentForCdp(url) {
332
+ try {
333
+ const parsed = new URL(url);
334
+ if (isLoopbackHost(parsed.hostname)) return parsed.protocol === "https:" || parsed.protocol === "wss:" ? directHttpsAgent : directHttpAgent;
335
+ } catch {}
336
+ }
337
+ /**
338
+ * Returns `true` when any proxy-related env var is set that could
339
+ * interfere with loopback connections.
340
+ */
341
+ function hasProxyEnv() {
342
+ return hasProxyEnvConfigured();
343
+ }
344
+ const LOOPBACK_ENTRIES = "localhost,127.0.0.1,[::1]";
345
+ function noProxyAlreadyCoversLocalhost() {
346
+ const current = process.env.NO_PROXY || process.env.no_proxy || "";
347
+ return current.includes("localhost") && current.includes("127.0.0.1") && current.includes("[::1]");
348
+ }
349
+ function isLoopbackCdpUrl(url) {
350
+ try {
351
+ return isLoopbackHost(new URL(url).hostname);
352
+ } catch {
353
+ return false;
354
+ }
355
+ }
356
+ var NoProxyLeaseManager = class {
357
+ constructor() {
358
+ this.leaseCount = 0;
359
+ this.snapshot = null;
360
+ }
361
+ acquire(url) {
362
+ if (!isLoopbackCdpUrl(url) || !hasProxyEnv()) return null;
363
+ if (this.leaseCount === 0 && !noProxyAlreadyCoversLocalhost()) {
364
+ const noProxy = process.env.NO_PROXY;
365
+ const noProxyLower = process.env.no_proxy;
366
+ const current = noProxy || noProxyLower || "";
367
+ const applied = current ? `${current},${LOOPBACK_ENTRIES}` : LOOPBACK_ENTRIES;
368
+ process.env.NO_PROXY = applied;
369
+ process.env.no_proxy = applied;
370
+ this.snapshot = {
371
+ noProxy,
372
+ noProxyLower,
373
+ applied
374
+ };
375
+ }
376
+ this.leaseCount += 1;
377
+ let released = false;
378
+ return () => {
379
+ if (released) return;
380
+ released = true;
381
+ this.release();
382
+ };
383
+ }
384
+ release() {
385
+ if (this.leaseCount <= 0) return;
386
+ this.leaseCount -= 1;
387
+ if (this.leaseCount > 0 || !this.snapshot) return;
388
+ const { noProxy, noProxyLower, applied } = this.snapshot;
389
+ const currentNoProxy = process.env.NO_PROXY;
390
+ const currentNoProxyLower = process.env.no_proxy;
391
+ if (currentNoProxy === applied && (currentNoProxyLower === applied || currentNoProxyLower === void 0)) {
392
+ if (noProxy !== void 0) process.env.NO_PROXY = noProxy;
393
+ else delete process.env.NO_PROXY;
394
+ if (noProxyLower !== void 0) process.env.no_proxy = noProxyLower;
395
+ else delete process.env.no_proxy;
396
+ }
397
+ this.snapshot = null;
398
+ }
399
+ };
400
+ const noProxyLeaseManager = new NoProxyLeaseManager();
401
+ /**
402
+ * Scoped NO_PROXY bypass for loopback CDP URLs.
403
+ *
404
+ * This wrapper only mutates env vars for loopback destinations. On restore,
405
+ * it avoids clobbering external NO_PROXY changes that happened while calls
406
+ * were in-flight.
407
+ */
408
+ async function withNoProxyForCdpUrl(url, fn) {
409
+ const release = noProxyLeaseManager.acquire(url);
410
+ try {
411
+ return await fn();
412
+ } finally {
413
+ release?.();
414
+ }
415
+ }
416
+
417
+ //#endregion
418
+ //#region src/browser/cdp-timeouts.ts
419
+ const CDP_HTTP_REQUEST_TIMEOUT_MS = 1500;
420
+ const CDP_WS_HANDSHAKE_TIMEOUT_MS = 5e3;
421
+ const CDP_JSON_NEW_TIMEOUT_MS = 1500;
422
+ const CHROME_REACHABILITY_TIMEOUT_MS = 500;
423
+ const CHROME_WS_READY_TIMEOUT_MS = 800;
424
+ const CHROME_BOOTSTRAP_PREFS_TIMEOUT_MS = 1e4;
425
+ const CHROME_BOOTSTRAP_EXIT_TIMEOUT_MS = 5e3;
426
+ const CHROME_LAUNCH_READY_WINDOW_MS = 15e3;
427
+ const CHROME_LAUNCH_READY_POLL_MS = 200;
428
+ const CHROME_STOP_TIMEOUT_MS = 2500;
429
+ const CHROME_STOP_PROBE_TIMEOUT_MS = 200;
430
+ const CHROME_STDERR_HINT_MAX_CHARS = 2e3;
431
+ const PROFILE_HTTP_REACHABILITY_TIMEOUT_MS = 300;
432
+ const PROFILE_WS_REACHABILITY_MIN_TIMEOUT_MS = 200;
433
+ const PROFILE_WS_REACHABILITY_MAX_TIMEOUT_MS = 2e3;
434
+ const PROFILE_ATTACH_RETRY_TIMEOUT_MS = 1200;
435
+ const PROFILE_POST_RESTART_WS_TIMEOUT_MS = 600;
436
+ function normalizeTimeoutMs(value) {
437
+ if (typeof value !== "number" || !Number.isFinite(value)) return;
438
+ return Math.max(1, Math.floor(value));
439
+ }
440
+ function resolveCdpReachabilityTimeouts(params) {
441
+ const normalized = normalizeTimeoutMs(params.timeoutMs);
442
+ if (params.profileIsLoopback) {
443
+ const httpTimeoutMs = normalized ?? PROFILE_HTTP_REACHABILITY_TIMEOUT_MS;
444
+ return {
445
+ httpTimeoutMs,
446
+ wsTimeoutMs: Math.max(PROFILE_WS_REACHABILITY_MIN_TIMEOUT_MS, Math.min(PROFILE_WS_REACHABILITY_MAX_TIMEOUT_MS, httpTimeoutMs * 2))
447
+ };
448
+ }
449
+ if (normalized !== void 0) return {
450
+ httpTimeoutMs: Math.max(normalized, params.remoteHttpTimeoutMs),
451
+ wsTimeoutMs: Math.max(normalized * 2, params.remoteHandshakeTimeoutMs)
452
+ };
453
+ return {
454
+ httpTimeoutMs: params.remoteHttpTimeoutMs,
455
+ wsTimeoutMs: params.remoteHandshakeTimeoutMs
456
+ };
457
+ }
458
+
459
+ //#endregion
460
+ //#region src/browser/extension-relay-auth.ts
461
+ const RELAY_TOKEN_CONTEXT = "openclaw-extension-relay-v1";
462
+ const DEFAULT_RELAY_PROBE_TIMEOUT_MS = 500;
463
+ const OPENCLAW_RELAY_BROWSER = "OpenClaw/extension-relay";
464
+ var SecretRefUnavailableError = class extends Error {
465
+ constructor(..._args) {
466
+ super(..._args);
467
+ this.isSecretRefUnavailable = true;
468
+ }
469
+ };
470
+ function trimToUndefined(value) {
471
+ if (typeof value !== "string") return;
472
+ const trimmed = value.trim();
473
+ return trimmed.length > 0 ? trimmed : void 0;
474
+ }
475
+ async function resolveGatewayAuthToken() {
476
+ const envToken = process.env.OPENCLAW_GATEWAY_TOKEN?.trim() || process.env.CLAWDBOT_GATEWAY_TOKEN?.trim();
477
+ if (envToken) return envToken;
478
+ try {
479
+ const cfg = loadConfig();
480
+ const tokenRef = resolveSecretInputRef({
481
+ value: cfg.gateway?.auth?.token,
482
+ defaults: cfg.secrets?.defaults
483
+ }).ref;
484
+ if (tokenRef) {
485
+ const refLabel = `${tokenRef.source}:${tokenRef.provider}:${tokenRef.id}`;
486
+ try {
487
+ const resolvedToken = trimToUndefined((await resolveSecretRefValues([tokenRef], {
488
+ config: cfg,
489
+ env: process.env
490
+ })).get(secretRefKey(tokenRef)));
491
+ if (resolvedToken) return resolvedToken;
492
+ } catch {}
493
+ throw new SecretRefUnavailableError(`extension relay requires a resolved gateway token, but gateway.auth.token SecretRef is unavailable (${refLabel}). Set OPENCLAW_GATEWAY_TOKEN or resolve your secret provider.`);
494
+ }
495
+ const configToken = normalizeSecretInputString(cfg.gateway?.auth?.token);
496
+ if (configToken) return configToken;
497
+ } catch (err) {
498
+ if (err instanceof SecretRefUnavailableError) throw err;
499
+ }
500
+ return null;
501
+ }
502
+ function deriveRelayAuthToken(gatewayToken, port) {
503
+ return createHmac("sha256", gatewayToken).update(`${RELAY_TOKEN_CONTEXT}:${port}`).digest("hex");
504
+ }
505
+ async function resolveRelayAcceptedTokensForPort(port) {
506
+ const gatewayToken = await resolveGatewayAuthToken();
507
+ if (!gatewayToken) throw new Error("extension relay requires gateway auth token (set gateway.auth.token or OPENCLAW_GATEWAY_TOKEN)");
508
+ const relayToken = deriveRelayAuthToken(gatewayToken, port);
509
+ if (relayToken === gatewayToken) return [relayToken];
510
+ return [relayToken, gatewayToken];
511
+ }
512
+ async function resolveRelayAuthTokenForPort(port) {
513
+ return (await resolveRelayAcceptedTokensForPort(port))[0];
514
+ }
515
+ async function probeAuthenticatedOpenClawRelay(params) {
516
+ const ctrl = new AbortController();
517
+ const timer = setTimeout(() => ctrl.abort(), params.timeoutMs ?? DEFAULT_RELAY_PROBE_TIMEOUT_MS);
518
+ try {
519
+ const versionUrl = new URL("/json/version", `${params.baseUrl}/`).toString();
520
+ const res = await fetch(versionUrl, {
521
+ signal: ctrl.signal,
522
+ headers: { [params.relayAuthHeader]: params.relayAuthToken }
523
+ });
524
+ if (!res.ok) return false;
525
+ const body = await res.json();
526
+ return (typeof body?.Browser === "string" ? body.Browser.trim() : "") === OPENCLAW_RELAY_BROWSER;
527
+ } catch {
528
+ return false;
529
+ } finally {
530
+ clearTimeout(timer);
531
+ }
532
+ }
533
+
534
+ //#endregion
535
+ //#region src/browser/extension-relay.ts
536
+ const RELAY_AUTH_HEADER = "x-openclaw-relay-token";
537
+ const DEFAULT_EXTENSION_RECONNECT_GRACE_MS = 2e4;
538
+ const DEFAULT_EXTENSION_COMMAND_RECONNECT_WAIT_MS = 3e3;
539
+ function headerValue(value) {
540
+ if (!value) return;
541
+ if (Array.isArray(value)) return value[0];
542
+ return value;
543
+ }
544
+ function getHeader(req, name) {
545
+ return headerValue(req.headers[name.toLowerCase()]);
546
+ }
547
+ function getRelayAuthTokenFromRequest(req, url) {
548
+ const headerToken = getHeader(req, RELAY_AUTH_HEADER)?.trim();
549
+ if (headerToken) return headerToken;
550
+ const queryToken = url?.searchParams.get("token")?.trim();
551
+ if (queryToken) return queryToken;
552
+ }
553
+ function parseUrlPort(parsed) {
554
+ const port = parsed.port?.trim() !== "" ? Number(parsed.port) : parsed.protocol === "https:" ? 443 : 80;
555
+ if (!Number.isFinite(port) || port <= 0 || port > 65535) return null;
556
+ return port;
557
+ }
558
+ function parseBaseUrl(raw) {
559
+ const parsed = new URL(raw.trim().replace(/\/$/, ""));
560
+ if (parsed.protocol !== "http:" && parsed.protocol !== "https:") throw new Error(`extension relay cdpUrl must be http(s), got ${parsed.protocol}`);
561
+ const host = parsed.hostname;
562
+ const port = parseUrlPort(parsed);
563
+ if (!port) throw new Error(`extension relay cdpUrl has invalid port: ${parsed.port || "(empty)"}`);
564
+ return {
565
+ host,
566
+ port,
567
+ baseUrl: parsed.toString().replace(/\/$/, "")
568
+ };
569
+ }
570
+ function text(res, status, bodyText) {
571
+ const body = Buffer.from(bodyText);
572
+ res.write(`HTTP/1.1 ${status} ${status === 200 ? "OK" : "ERR"}\r\nContent-Type: text/plain; charset=utf-8\r
573
+ Content-Length: ${body.length}\r\nConnection: close\r
574
+ \r
575
+ `);
576
+ res.write(body);
577
+ res.end();
578
+ }
579
+ function rejectUpgrade(socket, status, bodyText) {
580
+ text(socket, status, bodyText);
581
+ try {
582
+ socket.destroy();
583
+ } catch {}
584
+ }
585
+ function envMsOrDefault(name, fallback) {
586
+ const raw = process.env[name];
587
+ if (!raw || raw.trim() === "") return fallback;
588
+ const parsed = Number.parseInt(raw, 10);
589
+ if (!Number.isFinite(parsed) || parsed <= 0) return fallback;
590
+ return parsed;
591
+ }
592
+ const relayRuntimeByPort = /* @__PURE__ */ new Map();
593
+ const relayInitByPort = /* @__PURE__ */ new Map();
594
+ function isAddrInUseError(err) {
595
+ return typeof err === "object" && err !== null && "code" in err && err.code === "EADDRINUSE";
596
+ }
597
+ function relayAuthTokenForUrl(url) {
598
+ try {
599
+ const parsed = new URL(url);
600
+ if (!isLoopbackHost(parsed.hostname)) return null;
601
+ const port = parseUrlPort(parsed);
602
+ if (!port) return null;
603
+ return relayRuntimeByPort.get(port)?.relayAuthToken ?? null;
604
+ } catch {
605
+ return null;
606
+ }
607
+ }
608
+ function getChromeExtensionRelayAuthHeaders(url) {
609
+ const token = relayAuthTokenForUrl(url);
610
+ if (!token) return {};
611
+ return { [RELAY_AUTH_HEADER]: token };
612
+ }
613
+ async function ensureChromeExtensionRelayServer(opts) {
614
+ const info = parseBaseUrl(opts.cdpUrl);
615
+ if (!isLoopbackHost(info.host)) throw new Error(`extension relay requires loopback cdpUrl host (got ${info.host})`);
616
+ const existing = relayRuntimeByPort.get(info.port);
617
+ if (existing) return existing.server;
618
+ const inFlight = relayInitByPort.get(info.port);
619
+ if (inFlight) return await inFlight;
620
+ const extensionReconnectGraceMs = envMsOrDefault("OPENCLAW_EXTENSION_RELAY_RECONNECT_GRACE_MS", DEFAULT_EXTENSION_RECONNECT_GRACE_MS);
621
+ const extensionCommandReconnectWaitMs = envMsOrDefault("OPENCLAW_EXTENSION_RELAY_COMMAND_RECONNECT_WAIT_MS", DEFAULT_EXTENSION_COMMAND_RECONNECT_WAIT_MS);
622
+ const initPromise = (async () => {
623
+ const relayAuthToken = await resolveRelayAuthTokenForPort(info.port);
624
+ const relayAuthTokens = new Set(await resolveRelayAcceptedTokensForPort(info.port));
625
+ let extensionWs = null;
626
+ const cdpClients = /* @__PURE__ */ new Set();
627
+ const connectedTargets = /* @__PURE__ */ new Map();
628
+ const extensionConnected = () => extensionWs?.readyState === WebSocket.OPEN;
629
+ const hasConnectedTargets = () => connectedTargets.size > 0;
630
+ let extensionDisconnectCleanupTimer = null;
631
+ const extensionReconnectWaiters = /* @__PURE__ */ new Set();
632
+ const flushExtensionReconnectWaiters = (connected) => {
633
+ if (extensionReconnectWaiters.size === 0) return;
634
+ const waiters = Array.from(extensionReconnectWaiters);
635
+ extensionReconnectWaiters.clear();
636
+ for (const waiter of waiters) waiter(connected);
637
+ };
638
+ const clearExtensionDisconnectCleanupTimer = () => {
639
+ if (!extensionDisconnectCleanupTimer) return;
640
+ clearTimeout(extensionDisconnectCleanupTimer);
641
+ extensionDisconnectCleanupTimer = null;
642
+ };
643
+ const closeCdpClientsAfterExtensionDisconnect = () => {
644
+ connectedTargets.clear();
645
+ for (const client of cdpClients) try {
646
+ client.close(1011, "extension disconnected");
647
+ } catch {}
648
+ cdpClients.clear();
649
+ flushExtensionReconnectWaiters(false);
650
+ };
651
+ const scheduleExtensionDisconnectCleanup = () => {
652
+ clearExtensionDisconnectCleanupTimer();
653
+ extensionDisconnectCleanupTimer = setTimeout(() => {
654
+ extensionDisconnectCleanupTimer = null;
655
+ if (extensionConnected()) return;
656
+ closeCdpClientsAfterExtensionDisconnect();
657
+ }, extensionReconnectGraceMs);
658
+ };
659
+ const waitForExtensionReconnect = async (timeoutMs) => {
660
+ if (extensionConnected()) return true;
661
+ return await new Promise((resolve) => {
662
+ let settled = false;
663
+ const waiter = (connected) => {
664
+ if (settled) return;
665
+ settled = true;
666
+ clearTimeout(timer);
667
+ extensionReconnectWaiters.delete(waiter);
668
+ resolve(connected);
669
+ };
670
+ const timer = setTimeout(() => {
671
+ waiter(false);
672
+ }, timeoutMs);
673
+ extensionReconnectWaiters.add(waiter);
674
+ });
675
+ };
676
+ const pendingExtension = /* @__PURE__ */ new Map();
677
+ let nextExtensionId = 1;
678
+ const sendToExtension = async (payload) => {
679
+ const ws = extensionWs;
680
+ if (!ws || ws.readyState !== WebSocket.OPEN) throw new Error("Chrome extension not connected");
681
+ ws.send(JSON.stringify(payload));
682
+ return await new Promise((resolve, reject) => {
683
+ const timer = setTimeout(() => {
684
+ pendingExtension.delete(payload.id);
685
+ reject(/* @__PURE__ */ new Error(`extension request timeout: ${payload.params.method}`));
686
+ }, 3e4);
687
+ pendingExtension.set(payload.id, {
688
+ resolve,
689
+ reject,
690
+ timer
691
+ });
692
+ });
693
+ };
694
+ const broadcastToCdpClients = (evt) => {
695
+ const msg = JSON.stringify(evt);
696
+ for (const ws of cdpClients) {
697
+ if (ws.readyState !== WebSocket.OPEN) continue;
698
+ ws.send(msg);
699
+ }
700
+ };
701
+ const sendResponseToCdp = (ws, res) => {
702
+ if (ws.readyState !== WebSocket.OPEN) return;
703
+ ws.send(JSON.stringify(res));
704
+ };
705
+ const dropConnectedTargetSession = (sessionId) => {
706
+ const existing = connectedTargets.get(sessionId);
707
+ if (!existing) return;
708
+ connectedTargets.delete(sessionId);
709
+ return existing;
710
+ };
711
+ const dropConnectedTargetsByTargetId = (targetId) => {
712
+ const removed = [];
713
+ for (const [sessionId, target] of connectedTargets) {
714
+ if (target.targetId !== targetId) continue;
715
+ connectedTargets.delete(sessionId);
716
+ removed.push(target);
717
+ }
718
+ return removed;
719
+ };
720
+ const broadcastDetachedTarget = (target, targetId) => {
721
+ broadcastToCdpClients({
722
+ method: "Target.detachedFromTarget",
723
+ params: {
724
+ sessionId: target.sessionId,
725
+ targetId: targetId ?? target.targetId
726
+ },
727
+ sessionId: target.sessionId
728
+ });
729
+ };
730
+ const isMissingTargetError = (err) => {
731
+ const message = (err instanceof Error ? err.message : String(err)).toLowerCase();
732
+ return message.includes("target not found") || message.includes("no target with given id") || message.includes("session not found") || message.includes("cannot find session");
733
+ };
734
+ const pruneStaleTargetsFromCommandFailure = (cmd, err) => {
735
+ if (!isMissingTargetError(err)) return;
736
+ if (cmd.sessionId) {
737
+ const removed = dropConnectedTargetSession(cmd.sessionId);
738
+ if (removed) {
739
+ broadcastDetachedTarget(removed);
740
+ return;
741
+ }
742
+ }
743
+ const params = cmd.params ?? {};
744
+ const targetId = typeof params.targetId === "string" ? params.targetId : void 0;
745
+ if (!targetId) return;
746
+ const removedTargets = dropConnectedTargetsByTargetId(targetId);
747
+ for (const removed of removedTargets) broadcastDetachedTarget(removed, targetId);
748
+ };
749
+ const ensureTargetEventsForClient = (ws, mode) => {
750
+ for (const target of connectedTargets.values()) if (mode === "autoAttach") ws.send(JSON.stringify({
751
+ method: "Target.attachedToTarget",
752
+ params: {
753
+ sessionId: target.sessionId,
754
+ targetInfo: {
755
+ ...target.targetInfo,
756
+ attached: true
757
+ },
758
+ waitingForDebugger: false
759
+ }
760
+ }));
761
+ else ws.send(JSON.stringify({
762
+ method: "Target.targetCreated",
763
+ params: { targetInfo: {
764
+ ...target.targetInfo,
765
+ attached: true
766
+ } }
767
+ }));
768
+ };
769
+ const routeCdpCommand = async (cmd) => {
770
+ switch (cmd.method) {
771
+ case "Browser.getVersion": return {
772
+ protocolVersion: "1.3",
773
+ product: "Chrome/OpenClaw-Extension-Relay",
774
+ revision: "0",
775
+ userAgent: "OpenClaw-Extension-Relay",
776
+ jsVersion: "V8"
777
+ };
778
+ case "Browser.setDownloadBehavior": return {};
779
+ case "Target.setAutoAttach":
780
+ case "Target.setDiscoverTargets": return {};
781
+ case "Target.getTargets": return { targetInfos: Array.from(connectedTargets.values()).map((t) => ({
782
+ ...t.targetInfo,
783
+ attached: true
784
+ })) };
785
+ case "Target.getTargetInfo": {
786
+ const params = cmd.params ?? {};
787
+ const targetId = typeof params.targetId === "string" ? params.targetId : void 0;
788
+ if (targetId) {
789
+ for (const t of connectedTargets.values()) if (t.targetId === targetId) return { targetInfo: t.targetInfo };
790
+ }
791
+ if (cmd.sessionId && connectedTargets.has(cmd.sessionId)) {
792
+ const t = connectedTargets.get(cmd.sessionId);
793
+ if (t) return { targetInfo: t.targetInfo };
794
+ }
795
+ return { targetInfo: Array.from(connectedTargets.values())[0]?.targetInfo };
796
+ }
797
+ case "Target.attachToTarget": {
798
+ const params = cmd.params ?? {};
799
+ const targetId = typeof params.targetId === "string" ? params.targetId : void 0;
800
+ if (!targetId) throw new Error("targetId required");
801
+ for (const t of connectedTargets.values()) if (t.targetId === targetId) return { sessionId: t.sessionId };
802
+ throw new Error("target not found");
803
+ }
804
+ default: return await sendToExtension({
805
+ id: nextExtensionId++,
806
+ method: "forwardCDPCommand",
807
+ params: {
808
+ method: cmd.method,
809
+ sessionId: cmd.sessionId,
810
+ params: cmd.params
811
+ }
812
+ });
813
+ }
814
+ };
815
+ const server = createServer((req, res) => {
816
+ const url = new URL(req.url ?? "/", info.baseUrl);
817
+ const path = url.pathname;
818
+ const origin = getHeader(req, "origin");
819
+ const isChromeExtensionOrigin = typeof origin === "string" && origin.startsWith("chrome-extension://");
820
+ if (isChromeExtensionOrigin && origin) {
821
+ res.setHeader("Access-Control-Allow-Origin", origin);
822
+ res.setHeader("Vary", "Origin");
823
+ }
824
+ if (req.method === "OPTIONS") {
825
+ if (origin && !isChromeExtensionOrigin) {
826
+ res.writeHead(403);
827
+ res.end("Forbidden");
828
+ return;
829
+ }
830
+ const requestedHeaders = (getHeader(req, "access-control-request-headers") ?? "").split(",").map((header) => header.trim().toLowerCase()).filter((header) => header.length > 0);
831
+ const allowedHeaders = new Set([
832
+ "content-type",
833
+ RELAY_AUTH_HEADER,
834
+ ...requestedHeaders
835
+ ]);
836
+ res.writeHead(204, {
837
+ "Access-Control-Allow-Origin": origin ?? "*",
838
+ "Access-Control-Allow-Methods": "GET, PUT, POST, OPTIONS",
839
+ "Access-Control-Allow-Headers": Array.from(allowedHeaders).join(", "),
840
+ "Access-Control-Max-Age": "86400",
841
+ Vary: "Origin, Access-Control-Request-Headers"
842
+ });
843
+ res.end();
844
+ return;
845
+ }
846
+ if (path.startsWith("/json")) {
847
+ const token = getRelayAuthTokenFromRequest(req, url);
848
+ if (!token || !relayAuthTokens.has(token)) {
849
+ res.writeHead(401);
850
+ res.end("Unauthorized");
851
+ return;
852
+ }
853
+ }
854
+ if (req.method === "HEAD" && path === "/") {
855
+ res.writeHead(200);
856
+ res.end();
857
+ return;
858
+ }
859
+ if (path === "/") {
860
+ res.writeHead(200, { "Content-Type": "text/plain; charset=utf-8" });
861
+ res.end("OK");
862
+ return;
863
+ }
864
+ if (path === "/extension/status") {
865
+ res.writeHead(200, { "Content-Type": "application/json" });
866
+ res.end(JSON.stringify({ connected: extensionConnected() }));
867
+ return;
868
+ }
869
+ const cdpWsUrl = `${`ws://${req.headers.host?.trim() || `${info.host}:${info.port}`}`}/cdp`;
870
+ if ((path === "/json/version" || path === "/json/version/") && (req.method === "GET" || req.method === "PUT")) {
871
+ const payload = {
872
+ Browser: "OpenClaw/extension-relay",
873
+ "Protocol-Version": "1.3"
874
+ };
875
+ if (extensionConnected() || hasConnectedTargets()) payload.webSocketDebuggerUrl = cdpWsUrl;
876
+ res.writeHead(200, { "Content-Type": "application/json" });
877
+ res.end(JSON.stringify(payload));
878
+ return;
879
+ }
880
+ if (new Set([
881
+ "/json",
882
+ "/json/",
883
+ "/json/list",
884
+ "/json/list/"
885
+ ]).has(path) && (req.method === "GET" || req.method === "PUT")) {
886
+ const list = Array.from(connectedTargets.values()).map((t) => ({
887
+ id: t.targetId,
888
+ type: t.targetInfo.type ?? "page",
889
+ title: t.targetInfo.title ?? "",
890
+ description: t.targetInfo.title ?? "",
891
+ url: t.targetInfo.url ?? "",
892
+ webSocketDebuggerUrl: cdpWsUrl,
893
+ devtoolsFrontendUrl: `/devtools/inspector.html?ws=${cdpWsUrl.replace("ws://", "")}`
894
+ }));
895
+ res.writeHead(200, { "Content-Type": "application/json" });
896
+ res.end(JSON.stringify(list));
897
+ return;
898
+ }
899
+ const handleTargetActionRoute = (match, cdpMethod) => {
900
+ if (!match || req.method !== "GET" && req.method !== "PUT") return false;
901
+ let targetId = "";
902
+ try {
903
+ targetId = decodeURIComponent(match[1] ?? "").trim();
904
+ } catch {
905
+ res.writeHead(400);
906
+ res.end("invalid targetId encoding");
907
+ return true;
908
+ }
909
+ if (!targetId) {
910
+ res.writeHead(400);
911
+ res.end("targetId required");
912
+ return true;
913
+ }
914
+ (async () => {
915
+ try {
916
+ await sendToExtension({
917
+ id: nextExtensionId++,
918
+ method: "forwardCDPCommand",
919
+ params: {
920
+ method: cdpMethod,
921
+ params: { targetId }
922
+ }
923
+ });
924
+ } catch {}
925
+ })();
926
+ res.writeHead(200);
927
+ res.end("OK");
928
+ return true;
929
+ };
930
+ if (handleTargetActionRoute(path.match(/^\/json\/activate\/(.+)$/), "Target.activateTarget")) return;
931
+ if (handleTargetActionRoute(path.match(/^\/json\/close\/(.+)$/), "Target.closeTarget")) return;
932
+ res.writeHead(404);
933
+ res.end("not found");
934
+ });
935
+ const wssExtension = new WebSocketServer({ noServer: true });
936
+ const wssCdp = new WebSocketServer({ noServer: true });
937
+ server.on("upgrade", (req, socket, head) => {
938
+ const url = new URL(req.url ?? "/", info.baseUrl);
939
+ const pathname = url.pathname;
940
+ const remote = req.socket.remoteAddress;
941
+ if (!isLoopbackAddress(remote)) {
942
+ rejectUpgrade(socket, 403, "Forbidden");
943
+ return;
944
+ }
945
+ const origin = headerValue(req.headers.origin);
946
+ if (origin && !origin.startsWith("chrome-extension://")) {
947
+ rejectUpgrade(socket, 403, "Forbidden: invalid origin");
948
+ return;
949
+ }
950
+ if (pathname === "/extension") {
951
+ const token = getRelayAuthTokenFromRequest(req, url);
952
+ if (!token || !relayAuthTokens.has(token)) {
953
+ rejectUpgrade(socket, 401, "Unauthorized");
954
+ return;
955
+ }
956
+ if (extensionWs && extensionWs.readyState !== WebSocket.OPEN) {
957
+ try {
958
+ extensionWs.terminate();
959
+ } catch {}
960
+ extensionWs = null;
961
+ }
962
+ if (extensionConnected()) {
963
+ rejectUpgrade(socket, 409, "Extension already connected");
964
+ return;
965
+ }
966
+ wssExtension.handleUpgrade(req, socket, head, (ws) => {
967
+ wssExtension.emit("connection", ws, req);
968
+ });
969
+ return;
970
+ }
971
+ if (pathname === "/cdp") {
972
+ const token = getRelayAuthTokenFromRequest(req, url);
973
+ if (!token || !relayAuthTokens.has(token)) {
974
+ rejectUpgrade(socket, 401, "Unauthorized");
975
+ return;
976
+ }
977
+ wssCdp.handleUpgrade(req, socket, head, (ws) => {
978
+ wssCdp.emit("connection", ws, req);
979
+ });
980
+ return;
981
+ }
982
+ rejectUpgrade(socket, 404, "Not Found");
983
+ });
984
+ wssExtension.on("connection", (ws) => {
985
+ extensionWs = ws;
986
+ clearExtensionDisconnectCleanupTimer();
987
+ flushExtensionReconnectWaiters(true);
988
+ const ping = setInterval(() => {
989
+ if (ws.readyState !== WebSocket.OPEN) return;
990
+ ws.send(JSON.stringify({ method: "ping" }));
991
+ }, 5e3);
992
+ ws.on("message", (data) => {
993
+ if (extensionWs !== ws) return;
994
+ let parsed = null;
995
+ try {
996
+ parsed = JSON.parse(rawDataToString(data));
997
+ } catch {
998
+ return;
999
+ }
1000
+ if (parsed && typeof parsed === "object" && "id" in parsed && typeof parsed.id === "number") {
1001
+ const pending = pendingExtension.get(parsed.id);
1002
+ if (!pending) return;
1003
+ pendingExtension.delete(parsed.id);
1004
+ clearTimeout(pending.timer);
1005
+ if ("error" in parsed && typeof parsed.error === "string" && parsed.error.trim()) pending.reject(new Error(parsed.error));
1006
+ else pending.resolve(parsed.result);
1007
+ return;
1008
+ }
1009
+ if (parsed && typeof parsed === "object" && "method" in parsed) {
1010
+ if (parsed.method === "pong") return;
1011
+ if (parsed.method !== "forwardCDPEvent") return;
1012
+ const evt = parsed;
1013
+ const method = evt.params?.method;
1014
+ const params = evt.params?.params;
1015
+ const sessionId = evt.params?.sessionId;
1016
+ if (!method || typeof method !== "string") return;
1017
+ if (method === "Target.attachedToTarget") {
1018
+ const attached = params ?? {};
1019
+ if ((attached?.targetInfo?.type ?? "page") !== "page") return;
1020
+ if (attached?.sessionId && attached?.targetInfo?.targetId) {
1021
+ const prev = connectedTargets.get(attached.sessionId);
1022
+ const nextTargetId = attached.targetInfo.targetId;
1023
+ const prevTargetId = prev?.targetId;
1024
+ const changedTarget = Boolean(prev && prevTargetId && prevTargetId !== nextTargetId);
1025
+ connectedTargets.set(attached.sessionId, {
1026
+ sessionId: attached.sessionId,
1027
+ targetId: nextTargetId,
1028
+ targetInfo: attached.targetInfo
1029
+ });
1030
+ if (changedTarget && prevTargetId) broadcastToCdpClients({
1031
+ method: "Target.detachedFromTarget",
1032
+ params: {
1033
+ sessionId: attached.sessionId,
1034
+ targetId: prevTargetId
1035
+ },
1036
+ sessionId: attached.sessionId
1037
+ });
1038
+ if (!prev || changedTarget) broadcastToCdpClients({
1039
+ method,
1040
+ params,
1041
+ sessionId
1042
+ });
1043
+ return;
1044
+ }
1045
+ }
1046
+ if (method === "Target.detachedFromTarget") {
1047
+ const detached = params ?? {};
1048
+ if (detached?.sessionId) dropConnectedTargetSession(detached.sessionId);
1049
+ else if (detached?.targetId) dropConnectedTargetsByTargetId(detached.targetId);
1050
+ broadcastToCdpClients({
1051
+ method,
1052
+ params,
1053
+ sessionId
1054
+ });
1055
+ return;
1056
+ }
1057
+ if (method === "Target.targetDestroyed" || method === "Target.targetCrashed") {
1058
+ const targetEvent = params ?? {};
1059
+ if (targetEvent.targetId) dropConnectedTargetsByTargetId(targetEvent.targetId);
1060
+ broadcastToCdpClients({
1061
+ method,
1062
+ params,
1063
+ sessionId
1064
+ });
1065
+ return;
1066
+ }
1067
+ if (method === "Target.targetInfoChanged") {
1068
+ const targetInfo = (params ?? {})?.targetInfo;
1069
+ const targetId = targetInfo?.targetId;
1070
+ if (targetId && (targetInfo?.type ?? "page") === "page") for (const [sid, target] of connectedTargets) {
1071
+ if (target.targetId !== targetId) continue;
1072
+ connectedTargets.set(sid, {
1073
+ ...target,
1074
+ targetInfo: {
1075
+ ...target.targetInfo,
1076
+ ...targetInfo
1077
+ }
1078
+ });
1079
+ }
1080
+ }
1081
+ broadcastToCdpClients({
1082
+ method,
1083
+ params,
1084
+ sessionId
1085
+ });
1086
+ }
1087
+ });
1088
+ ws.on("close", () => {
1089
+ clearInterval(ping);
1090
+ if (extensionWs !== ws) return;
1091
+ extensionWs = null;
1092
+ for (const [, pending] of pendingExtension) {
1093
+ clearTimeout(pending.timer);
1094
+ pending.reject(/* @__PURE__ */ new Error("extension disconnected"));
1095
+ }
1096
+ pendingExtension.clear();
1097
+ scheduleExtensionDisconnectCleanup();
1098
+ });
1099
+ });
1100
+ wssCdp.on("connection", (ws) => {
1101
+ cdpClients.add(ws);
1102
+ ws.on("message", async (data) => {
1103
+ let cmd = null;
1104
+ try {
1105
+ cmd = JSON.parse(rawDataToString(data));
1106
+ } catch {
1107
+ return;
1108
+ }
1109
+ if (!cmd || typeof cmd !== "object") return;
1110
+ if (typeof cmd.id !== "number" || typeof cmd.method !== "string") return;
1111
+ if (!extensionConnected()) {
1112
+ if (!await waitForExtensionReconnect(extensionCommandReconnectWaitMs) || !extensionConnected()) {
1113
+ sendResponseToCdp(ws, {
1114
+ id: cmd.id,
1115
+ sessionId: cmd.sessionId,
1116
+ error: { message: "Extension not connected" }
1117
+ });
1118
+ return;
1119
+ }
1120
+ }
1121
+ try {
1122
+ const result = await routeCdpCommand(cmd);
1123
+ if (cmd.method === "Target.setAutoAttach" && !cmd.sessionId) ensureTargetEventsForClient(ws, "autoAttach");
1124
+ if (cmd.method === "Target.setDiscoverTargets") {
1125
+ if ((cmd.params ?? {}).discover === true) ensureTargetEventsForClient(ws, "discover");
1126
+ }
1127
+ if (cmd.method === "Target.attachToTarget") {
1128
+ const params = cmd.params ?? {};
1129
+ const targetId = typeof params.targetId === "string" ? params.targetId : void 0;
1130
+ if (targetId) {
1131
+ const target = Array.from(connectedTargets.values()).find((t) => t.targetId === targetId);
1132
+ if (target) ws.send(JSON.stringify({
1133
+ method: "Target.attachedToTarget",
1134
+ params: {
1135
+ sessionId: target.sessionId,
1136
+ targetInfo: {
1137
+ ...target.targetInfo,
1138
+ attached: true
1139
+ },
1140
+ waitingForDebugger: false
1141
+ }
1142
+ }));
1143
+ }
1144
+ }
1145
+ sendResponseToCdp(ws, {
1146
+ id: cmd.id,
1147
+ sessionId: cmd.sessionId,
1148
+ result
1149
+ });
1150
+ } catch (err) {
1151
+ pruneStaleTargetsFromCommandFailure(cmd, err);
1152
+ sendResponseToCdp(ws, {
1153
+ id: cmd.id,
1154
+ sessionId: cmd.sessionId,
1155
+ error: { message: err instanceof Error ? err.message : String(err) }
1156
+ });
1157
+ }
1158
+ });
1159
+ ws.on("close", () => {
1160
+ cdpClients.delete(ws);
1161
+ });
1162
+ });
1163
+ try {
1164
+ await new Promise((resolve, reject) => {
1165
+ server.listen(info.port, info.host, () => resolve());
1166
+ server.once("error", reject);
1167
+ });
1168
+ } catch (err) {
1169
+ if (isAddrInUseError(err) && await probeAuthenticatedOpenClawRelay({
1170
+ baseUrl: info.baseUrl,
1171
+ relayAuthHeader: RELAY_AUTH_HEADER,
1172
+ relayAuthToken
1173
+ })) {
1174
+ const existingRelay = {
1175
+ host: info.host,
1176
+ port: info.port,
1177
+ baseUrl: info.baseUrl,
1178
+ cdpWsUrl: `ws://${info.host}:${info.port}/cdp`,
1179
+ extensionConnected: () => false,
1180
+ stop: async () => {
1181
+ relayRuntimeByPort.delete(info.port);
1182
+ }
1183
+ };
1184
+ relayRuntimeByPort.set(info.port, {
1185
+ server: existingRelay,
1186
+ relayAuthToken
1187
+ });
1188
+ return existingRelay;
1189
+ }
1190
+ throw err;
1191
+ }
1192
+ const port = server.address()?.port ?? info.port;
1193
+ const host = info.host;
1194
+ const relay = {
1195
+ host,
1196
+ port,
1197
+ baseUrl: `${new URL(info.baseUrl).protocol}//${host}:${port}`,
1198
+ cdpWsUrl: `ws://${host}:${port}/cdp`,
1199
+ extensionConnected,
1200
+ stop: async () => {
1201
+ relayRuntimeByPort.delete(port);
1202
+ clearExtensionDisconnectCleanupTimer();
1203
+ flushExtensionReconnectWaiters(false);
1204
+ for (const [, pending] of pendingExtension) {
1205
+ clearTimeout(pending.timer);
1206
+ pending.reject(/* @__PURE__ */ new Error("server stopping"));
1207
+ }
1208
+ pendingExtension.clear();
1209
+ try {
1210
+ extensionWs?.close(1001, "server stopping");
1211
+ } catch {}
1212
+ for (const ws of cdpClients) try {
1213
+ ws.close(1001, "server stopping");
1214
+ } catch {}
1215
+ await new Promise((resolve) => {
1216
+ server.close(() => resolve());
1217
+ });
1218
+ wssExtension.close();
1219
+ wssCdp.close();
1220
+ }
1221
+ };
1222
+ relayRuntimeByPort.set(port, {
1223
+ server: relay,
1224
+ relayAuthToken
1225
+ });
1226
+ return relay;
1227
+ })();
1228
+ relayInitByPort.set(info.port, initPromise);
1229
+ try {
1230
+ return await initPromise;
1231
+ } finally {
1232
+ relayInitByPort.delete(info.port);
1233
+ }
1234
+ }
1235
+ async function stopChromeExtensionRelayServer(opts) {
1236
+ const info = parseBaseUrl(opts.cdpUrl);
1237
+ const existing = relayRuntimeByPort.get(info.port);
1238
+ if (!existing) return false;
1239
+ await existing.server.stop();
1240
+ return true;
1241
+ }
1242
+
1243
+ //#endregion
1244
+ //#region src/browser/cdp.helpers.ts
1245
+ function getHeadersWithAuth(url, headers = {}) {
1246
+ const mergedHeaders = {
1247
+ ...getChromeExtensionRelayAuthHeaders(url),
1248
+ ...headers
1249
+ };
1250
+ try {
1251
+ const parsed = new URL(url);
1252
+ if (Object.keys(mergedHeaders).some((key) => key.toLowerCase() === "authorization")) return mergedHeaders;
1253
+ if (parsed.username || parsed.password) {
1254
+ const auth = Buffer.from(`${parsed.username}:${parsed.password}`).toString("base64");
1255
+ return {
1256
+ ...mergedHeaders,
1257
+ Authorization: `Basic ${auth}`
1258
+ };
1259
+ }
1260
+ } catch {}
1261
+ return mergedHeaders;
1262
+ }
1263
+ function appendCdpPath(cdpUrl, path) {
1264
+ const url = new URL(cdpUrl);
1265
+ url.pathname = `${url.pathname.replace(/\/$/, "")}${path.startsWith("/") ? path : `/${path}`}`;
1266
+ return url.toString();
1267
+ }
1268
+ function createCdpSender(ws) {
1269
+ let nextId = 1;
1270
+ const pending = /* @__PURE__ */ new Map();
1271
+ const send = (method, params, sessionId) => {
1272
+ const id = nextId++;
1273
+ const msg = {
1274
+ id,
1275
+ method,
1276
+ params,
1277
+ sessionId
1278
+ };
1279
+ ws.send(JSON.stringify(msg));
1280
+ return new Promise((resolve, reject) => {
1281
+ pending.set(id, {
1282
+ resolve,
1283
+ reject
1284
+ });
1285
+ });
1286
+ };
1287
+ const closeWithError = (err) => {
1288
+ for (const [, p] of pending) p.reject(err);
1289
+ pending.clear();
1290
+ try {
1291
+ ws.close();
1292
+ } catch {}
1293
+ };
1294
+ ws.on("error", (err) => {
1295
+ closeWithError(err instanceof Error ? err : new Error(String(err)));
1296
+ });
1297
+ ws.on("message", (data) => {
1298
+ try {
1299
+ const parsed = JSON.parse(rawDataToString(data));
1300
+ if (typeof parsed.id !== "number") return;
1301
+ const p = pending.get(parsed.id);
1302
+ if (!p) return;
1303
+ pending.delete(parsed.id);
1304
+ if (parsed.error?.message) {
1305
+ p.reject(new Error(parsed.error.message));
1306
+ return;
1307
+ }
1308
+ p.resolve(parsed.result);
1309
+ } catch {}
1310
+ });
1311
+ ws.on("close", () => {
1312
+ closeWithError(/* @__PURE__ */ new Error("CDP socket closed"));
1313
+ });
1314
+ return {
1315
+ send,
1316
+ closeWithError
1317
+ };
1318
+ }
1319
+ async function fetchJson(url, timeoutMs = CDP_HTTP_REQUEST_TIMEOUT_MS, init) {
1320
+ return await (await fetchCdpChecked(url, timeoutMs, init)).json();
1321
+ }
1322
+ async function fetchCdpChecked(url, timeoutMs = CDP_HTTP_REQUEST_TIMEOUT_MS, init) {
1323
+ const ctrl = new AbortController();
1324
+ const t = setTimeout(ctrl.abort.bind(ctrl), timeoutMs);
1325
+ try {
1326
+ const headers = getHeadersWithAuth(url, init?.headers || {});
1327
+ const res = await withNoProxyForCdpUrl(url, () => fetch(url, {
1328
+ ...init,
1329
+ headers,
1330
+ signal: ctrl.signal
1331
+ }));
1332
+ if (!res.ok) throw new Error(`HTTP ${res.status}`);
1333
+ return res;
1334
+ } finally {
1335
+ clearTimeout(t);
1336
+ }
1337
+ }
1338
+ async function fetchOk(url, timeoutMs = CDP_HTTP_REQUEST_TIMEOUT_MS, init) {
1339
+ await fetchCdpChecked(url, timeoutMs, init);
1340
+ }
1341
+ function openCdpWebSocket(wsUrl, opts) {
1342
+ const headers = getHeadersWithAuth(wsUrl, opts?.headers ?? {});
1343
+ const handshakeTimeoutMs = typeof opts?.handshakeTimeoutMs === "number" && Number.isFinite(opts.handshakeTimeoutMs) ? Math.max(1, Math.floor(opts.handshakeTimeoutMs)) : CDP_WS_HANDSHAKE_TIMEOUT_MS;
1344
+ const agent = getDirectAgentForCdp(wsUrl);
1345
+ return new WebSocket(wsUrl, {
1346
+ handshakeTimeout: handshakeTimeoutMs,
1347
+ ...Object.keys(headers).length ? { headers } : {},
1348
+ ...agent ? { agent } : {}
1349
+ });
1350
+ }
1351
+ async function withCdpSocket(wsUrl, fn, opts) {
1352
+ const ws = openCdpWebSocket(wsUrl, opts);
1353
+ const { send, closeWithError } = createCdpSender(ws);
1354
+ const openPromise = new Promise((resolve, reject) => {
1355
+ ws.once("open", () => resolve());
1356
+ ws.once("error", (err) => reject(err));
1357
+ ws.once("close", () => reject(/* @__PURE__ */ new Error("CDP socket closed")));
1358
+ });
1359
+ try {
1360
+ await openPromise;
1361
+ } catch (err) {
1362
+ closeWithError(err instanceof Error ? err : new Error(String(err)));
1363
+ throw err;
1364
+ }
1365
+ try {
1366
+ return await fn(send);
1367
+ } catch (err) {
1368
+ closeWithError(err instanceof Error ? err : new Error(String(err)));
1369
+ throw err;
1370
+ } finally {
1371
+ try {
1372
+ ws.close();
1373
+ } catch {}
1374
+ }
1375
+ }
1376
+
1377
+ //#endregion
1378
+ //#region src/browser/navigation-guard.ts
1379
+ const NETWORK_NAVIGATION_PROTOCOLS = new Set(["http:", "https:"]);
1380
+ const SAFE_NON_NETWORK_URLS = new Set(["about:blank"]);
1381
+ function isAllowedNonNetworkNavigationUrl(parsed) {
1382
+ return SAFE_NON_NETWORK_URLS.has(parsed.href);
1383
+ }
1384
+ var InvalidBrowserNavigationUrlError = class extends Error {
1385
+ constructor(message) {
1386
+ super(message);
1387
+ this.name = "InvalidBrowserNavigationUrlError";
1388
+ }
1389
+ };
1390
+ function withBrowserNavigationPolicy(ssrfPolicy) {
1391
+ return ssrfPolicy ? { ssrfPolicy } : {};
1392
+ }
1393
+ async function assertBrowserNavigationAllowed(opts) {
1394
+ const rawUrl = String(opts.url ?? "").trim();
1395
+ if (!rawUrl) throw new InvalidBrowserNavigationUrlError("url is required");
1396
+ let parsed;
1397
+ try {
1398
+ parsed = new URL(rawUrl);
1399
+ } catch {
1400
+ throw new InvalidBrowserNavigationUrlError(`Invalid URL: ${rawUrl}`);
1401
+ }
1402
+ if (!NETWORK_NAVIGATION_PROTOCOLS.has(parsed.protocol)) {
1403
+ if (isAllowedNonNetworkNavigationUrl(parsed)) return;
1404
+ throw new InvalidBrowserNavigationUrlError(`Navigation blocked: unsupported protocol "${parsed.protocol}"`);
1405
+ }
1406
+ if (hasProxyEnvConfigured() && !isPrivateNetworkAllowedByPolicy(opts.ssrfPolicy)) throw new InvalidBrowserNavigationUrlError("Navigation blocked: strict browser SSRF policy cannot be enforced while env proxy variables are set");
1407
+ await resolvePinnedHostnameWithPolicy(parsed.hostname, {
1408
+ lookupFn: opts.lookupFn,
1409
+ policy: opts.ssrfPolicy
1410
+ });
1411
+ }
1412
+ /**
1413
+ * Best-effort post-navigation guard for final page URLs.
1414
+ * Only validates network URLs (http/https) and about:blank to avoid false
1415
+ * positives on browser-internal error pages (e.g. chrome-error://).
1416
+ */
1417
+ async function assertBrowserNavigationResultAllowed(opts) {
1418
+ const rawUrl = String(opts.url ?? "").trim();
1419
+ if (!rawUrl) return;
1420
+ let parsed;
1421
+ try {
1422
+ parsed = new URL(rawUrl);
1423
+ } catch {
1424
+ return;
1425
+ }
1426
+ if (NETWORK_NAVIGATION_PROTOCOLS.has(parsed.protocol) || isAllowedNonNetworkNavigationUrl(parsed)) await assertBrowserNavigationAllowed(opts);
1427
+ }
1428
+
1429
+ //#endregion
1430
+ //#region src/browser/cdp.ts
1431
+ function normalizeCdpWsUrl(wsUrl, cdpUrl) {
1432
+ const ws = new URL(wsUrl);
1433
+ const cdp = new URL(cdpUrl);
1434
+ if (isLoopbackHost(ws.hostname) && !isLoopbackHost(cdp.hostname)) {
1435
+ ws.hostname = cdp.hostname;
1436
+ const cdpPort = cdp.port || (cdp.protocol === "https:" ? "443" : "80");
1437
+ if (cdpPort) ws.port = cdpPort;
1438
+ ws.protocol = cdp.protocol === "https:" ? "wss:" : "ws:";
1439
+ }
1440
+ if (cdp.protocol === "https:" && ws.protocol === "ws:") ws.protocol = "wss:";
1441
+ if (!ws.username && !ws.password && (cdp.username || cdp.password)) {
1442
+ ws.username = cdp.username;
1443
+ ws.password = cdp.password;
1444
+ }
1445
+ for (const [key, value] of cdp.searchParams.entries()) if (!ws.searchParams.has(key)) ws.searchParams.append(key, value);
1446
+ return ws.toString();
1447
+ }
1448
+ async function captureScreenshot(opts) {
1449
+ return await withCdpSocket(opts.wsUrl, async (send) => {
1450
+ await send("Page.enable");
1451
+ let clip;
1452
+ if (opts.fullPage) {
1453
+ const metrics = await send("Page.getLayoutMetrics");
1454
+ const size = metrics?.cssContentSize ?? metrics?.contentSize;
1455
+ const width = Number(size?.width ?? 0);
1456
+ const height = Number(size?.height ?? 0);
1457
+ if (width > 0 && height > 0) clip = {
1458
+ x: 0,
1459
+ y: 0,
1460
+ width,
1461
+ height,
1462
+ scale: 1
1463
+ };
1464
+ }
1465
+ const format = opts.format ?? "png";
1466
+ const quality = format === "jpeg" ? Math.max(0, Math.min(100, Math.round(opts.quality ?? 85))) : void 0;
1467
+ const base64 = (await send("Page.captureScreenshot", {
1468
+ format,
1469
+ ...quality !== void 0 ? { quality } : {},
1470
+ fromSurface: true,
1471
+ captureBeyondViewport: true,
1472
+ ...clip ? { clip } : {}
1473
+ }))?.data;
1474
+ if (!base64) throw new Error("Screenshot failed: missing data");
1475
+ return Buffer.from(base64, "base64");
1476
+ });
1477
+ }
1478
+ async function createTargetViaCdp(opts) {
1479
+ await assertBrowserNavigationAllowed({
1480
+ url: opts.url,
1481
+ ...withBrowserNavigationPolicy(opts.ssrfPolicy)
1482
+ });
1483
+ const version = await fetchJson(appendCdpPath(opts.cdpUrl, "/json/version"), 1500);
1484
+ const wsUrlRaw = String(version?.webSocketDebuggerUrl ?? "").trim();
1485
+ const wsUrl = wsUrlRaw ? normalizeCdpWsUrl(wsUrlRaw, opts.cdpUrl) : "";
1486
+ if (!wsUrl) throw new Error("CDP /json/version missing webSocketDebuggerUrl");
1487
+ return await withCdpSocket(wsUrl, async (send) => {
1488
+ const created = await send("Target.createTarget", { url: opts.url });
1489
+ const targetId = String(created?.targetId ?? "").trim();
1490
+ if (!targetId) throw new Error("CDP Target.createTarget returned no targetId");
1491
+ return { targetId };
1492
+ });
1493
+ }
1494
+ function axValue(v) {
1495
+ if (!v || typeof v !== "object") return "";
1496
+ const value = v.value;
1497
+ if (typeof value === "string") return value;
1498
+ if (typeof value === "number" || typeof value === "boolean") return String(value);
1499
+ return "";
1500
+ }
1501
+ function formatAriaSnapshot(nodes, limit) {
1502
+ const byId = /* @__PURE__ */ new Map();
1503
+ for (const n of nodes) if (n.nodeId) byId.set(n.nodeId, n);
1504
+ const referenced = /* @__PURE__ */ new Set();
1505
+ for (const n of nodes) for (const c of n.childIds ?? []) referenced.add(c);
1506
+ const root = nodes.find((n) => n.nodeId && !referenced.has(n.nodeId)) ?? nodes[0];
1507
+ if (!root?.nodeId) return [];
1508
+ const out = [];
1509
+ const stack = [{
1510
+ id: root.nodeId,
1511
+ depth: 0
1512
+ }];
1513
+ while (stack.length && out.length < limit) {
1514
+ const popped = stack.pop();
1515
+ if (!popped) break;
1516
+ const { id, depth } = popped;
1517
+ const n = byId.get(id);
1518
+ if (!n) continue;
1519
+ const role = axValue(n.role);
1520
+ const name = axValue(n.name);
1521
+ const value = axValue(n.value);
1522
+ const description = axValue(n.description);
1523
+ const ref = `ax${out.length + 1}`;
1524
+ out.push({
1525
+ ref,
1526
+ role: role || "unknown",
1527
+ name: name || "",
1528
+ ...value ? { value } : {},
1529
+ ...description ? { description } : {},
1530
+ ...typeof n.backendDOMNodeId === "number" ? { backendDOMNodeId: n.backendDOMNodeId } : {},
1531
+ depth
1532
+ });
1533
+ const children = (n.childIds ?? []).filter((c) => byId.has(c));
1534
+ for (let i = children.length - 1; i >= 0; i--) {
1535
+ const child = children[i];
1536
+ if (child) stack.push({
1537
+ id: child,
1538
+ depth: depth + 1
1539
+ });
1540
+ }
1541
+ }
1542
+ return out;
1543
+ }
1544
+ async function snapshotAria(opts) {
1545
+ const limit = Math.max(1, Math.min(2e3, Math.floor(opts.limit ?? 500)));
1546
+ return await withCdpSocket(opts.wsUrl, async (send) => {
1547
+ await send("Accessibility.enable").catch(() => {});
1548
+ const res = await send("Accessibility.getFullAXTree");
1549
+ return { nodes: formatAriaSnapshot(Array.isArray(res?.nodes) ? res.nodes : [], limit) };
1550
+ });
1551
+ }
1552
+
1553
+ //#endregion
1554
+ //#region src/browser/chrome.executables.ts
1555
+ const CHROMIUM_BUNDLE_IDS = new Set([
1556
+ "com.google.Chrome",
1557
+ "com.google.Chrome.beta",
1558
+ "com.google.Chrome.canary",
1559
+ "com.google.Chrome.dev",
1560
+ "com.brave.Browser",
1561
+ "com.brave.Browser.beta",
1562
+ "com.brave.Browser.nightly",
1563
+ "com.microsoft.Edge",
1564
+ "com.microsoft.EdgeBeta",
1565
+ "com.microsoft.EdgeDev",
1566
+ "com.microsoft.EdgeCanary",
1567
+ "org.chromium.Chromium",
1568
+ "com.vivaldi.Vivaldi",
1569
+ "com.operasoftware.Opera",
1570
+ "com.operasoftware.OperaGX",
1571
+ "com.yandex.desktop.yandex-browser",
1572
+ "company.thebrowser.Browser"
1573
+ ]);
1574
+ const CHROMIUM_DESKTOP_IDS = new Set([
1575
+ "google-chrome.desktop",
1576
+ "google-chrome-beta.desktop",
1577
+ "google-chrome-unstable.desktop",
1578
+ "brave-browser.desktop",
1579
+ "microsoft-edge.desktop",
1580
+ "microsoft-edge-beta.desktop",
1581
+ "microsoft-edge-dev.desktop",
1582
+ "microsoft-edge-canary.desktop",
1583
+ "chromium.desktop",
1584
+ "chromium-browser.desktop",
1585
+ "vivaldi.desktop",
1586
+ "vivaldi-stable.desktop",
1587
+ "opera.desktop",
1588
+ "opera-gx.desktop",
1589
+ "yandex-browser.desktop",
1590
+ "org.chromium.Chromium.desktop"
1591
+ ]);
1592
+ const CHROMIUM_EXE_NAMES = new Set([
1593
+ "chrome.exe",
1594
+ "msedge.exe",
1595
+ "brave.exe",
1596
+ "brave-browser.exe",
1597
+ "chromium.exe",
1598
+ "vivaldi.exe",
1599
+ "opera.exe",
1600
+ "launcher.exe",
1601
+ "yandex.exe",
1602
+ "yandexbrowser.exe",
1603
+ "google chrome",
1604
+ "google chrome canary",
1605
+ "brave browser",
1606
+ "microsoft edge",
1607
+ "chromium",
1608
+ "chrome",
1609
+ "brave",
1610
+ "msedge",
1611
+ "brave-browser",
1612
+ "google-chrome",
1613
+ "google-chrome-stable",
1614
+ "google-chrome-beta",
1615
+ "google-chrome-unstable",
1616
+ "microsoft-edge",
1617
+ "microsoft-edge-beta",
1618
+ "microsoft-edge-dev",
1619
+ "microsoft-edge-canary",
1620
+ "chromium-browser",
1621
+ "vivaldi",
1622
+ "vivaldi-stable",
1623
+ "opera",
1624
+ "opera-stable",
1625
+ "opera-gx",
1626
+ "yandex-browser"
1627
+ ]);
1628
+ function exists$1(filePath) {
1629
+ try {
1630
+ return fs.existsSync(filePath);
1631
+ } catch {
1632
+ return false;
1633
+ }
1634
+ }
1635
+ function execText(command, args, timeoutMs = 1200, maxBuffer = 1024 * 1024) {
1636
+ try {
1637
+ const output = execFileSync(command, args, {
1638
+ timeout: timeoutMs,
1639
+ encoding: "utf8",
1640
+ maxBuffer
1641
+ });
1642
+ return String(output ?? "").trim() || null;
1643
+ } catch {
1644
+ return null;
1645
+ }
1646
+ }
1647
+ function inferKindFromIdentifier(identifier) {
1648
+ const id = identifier.toLowerCase();
1649
+ if (id.includes("brave")) return "brave";
1650
+ if (id.includes("edge")) return "edge";
1651
+ if (id.includes("chromium")) return "chromium";
1652
+ if (id.includes("canary")) return "canary";
1653
+ if (id.includes("opera") || id.includes("vivaldi") || id.includes("yandex") || id.includes("thebrowser")) return "chromium";
1654
+ return "chrome";
1655
+ }
1656
+ function inferKindFromExecutableName(name) {
1657
+ const lower = name.toLowerCase();
1658
+ if (lower.includes("brave")) return "brave";
1659
+ if (lower.includes("edge") || lower.includes("msedge")) return "edge";
1660
+ if (lower.includes("chromium")) return "chromium";
1661
+ if (lower.includes("canary") || lower.includes("sxs")) return "canary";
1662
+ if (lower.includes("opera") || lower.includes("vivaldi") || lower.includes("yandex")) return "chromium";
1663
+ return "chrome";
1664
+ }
1665
+ function detectDefaultChromiumExecutable(platform) {
1666
+ if (platform === "darwin") return detectDefaultChromiumExecutableMac();
1667
+ if (platform === "linux") return detectDefaultChromiumExecutableLinux();
1668
+ if (platform === "win32") return detectDefaultChromiumExecutableWindows();
1669
+ return null;
1670
+ }
1671
+ function detectDefaultChromiumExecutableMac() {
1672
+ const bundleId = detectDefaultBrowserBundleIdMac();
1673
+ if (!bundleId || !CHROMIUM_BUNDLE_IDS.has(bundleId)) return null;
1674
+ const appPathRaw = execText("/usr/bin/osascript", ["-e", `POSIX path of (path to application id "${bundleId}")`]);
1675
+ if (!appPathRaw) return null;
1676
+ const appPath = appPathRaw.trim().replace(/\/$/, "");
1677
+ const exeName = execText("/usr/bin/defaults", [
1678
+ "read",
1679
+ path.join(appPath, "Contents", "Info"),
1680
+ "CFBundleExecutable"
1681
+ ]);
1682
+ if (!exeName) return null;
1683
+ const exePath = path.join(appPath, "Contents", "MacOS", exeName.trim());
1684
+ if (!exists$1(exePath)) return null;
1685
+ return {
1686
+ kind: inferKindFromIdentifier(bundleId),
1687
+ path: exePath
1688
+ };
1689
+ }
1690
+ function detectDefaultBrowserBundleIdMac() {
1691
+ const plistPath = path.join(os.homedir(), "Library/Preferences/com.apple.LaunchServices/com.apple.launchservices.secure.plist");
1692
+ if (!exists$1(plistPath)) return null;
1693
+ const handlersRaw = execText("/usr/bin/plutil", [
1694
+ "-extract",
1695
+ "LSHandlers",
1696
+ "json",
1697
+ "-o",
1698
+ "-",
1699
+ "--",
1700
+ plistPath
1701
+ ], 2e3, 5 * 1024 * 1024);
1702
+ if (!handlersRaw) return null;
1703
+ let handlers;
1704
+ try {
1705
+ handlers = JSON.parse(handlersRaw);
1706
+ } catch {
1707
+ return null;
1708
+ }
1709
+ if (!Array.isArray(handlers)) return null;
1710
+ const resolveScheme = (scheme) => {
1711
+ let candidate = null;
1712
+ for (const entry of handlers) {
1713
+ if (!entry || typeof entry !== "object") continue;
1714
+ const record = entry;
1715
+ if (record.LSHandlerURLScheme !== scheme) continue;
1716
+ const role = typeof record.LSHandlerRoleAll === "string" && record.LSHandlerRoleAll || typeof record.LSHandlerRoleViewer === "string" && record.LSHandlerRoleViewer || null;
1717
+ if (role) candidate = role;
1718
+ }
1719
+ return candidate;
1720
+ };
1721
+ return resolveScheme("http") ?? resolveScheme("https");
1722
+ }
1723
+ function detectDefaultChromiumExecutableLinux() {
1724
+ const desktopId = execText("xdg-settings", ["get", "default-web-browser"]) || execText("xdg-mime", [
1725
+ "query",
1726
+ "default",
1727
+ "x-scheme-handler/http"
1728
+ ]);
1729
+ if (!desktopId) return null;
1730
+ const trimmed = desktopId.trim();
1731
+ if (!CHROMIUM_DESKTOP_IDS.has(trimmed)) return null;
1732
+ const desktopPath = findDesktopFilePath(trimmed);
1733
+ if (!desktopPath) return null;
1734
+ const execLine = readDesktopExecLine(desktopPath);
1735
+ if (!execLine) return null;
1736
+ const command = extractExecutableFromExecLine(execLine);
1737
+ if (!command) return null;
1738
+ const resolved = resolveLinuxExecutablePath(command);
1739
+ if (!resolved) return null;
1740
+ const exeName = path.posix.basename(resolved).toLowerCase();
1741
+ if (!CHROMIUM_EXE_NAMES.has(exeName)) return null;
1742
+ return {
1743
+ kind: inferKindFromExecutableName(exeName),
1744
+ path: resolved
1745
+ };
1746
+ }
1747
+ function detectDefaultChromiumExecutableWindows() {
1748
+ const progId = readWindowsProgId();
1749
+ const command = (progId ? readWindowsCommandForProgId(progId) : null) || readWindowsCommandForProgId("http");
1750
+ if (!command) return null;
1751
+ const exePath = extractWindowsExecutablePath(expandWindowsEnvVars(command));
1752
+ if (!exePath) return null;
1753
+ if (!exists$1(exePath)) return null;
1754
+ const exeName = path.win32.basename(exePath).toLowerCase();
1755
+ if (!CHROMIUM_EXE_NAMES.has(exeName)) return null;
1756
+ return {
1757
+ kind: inferKindFromExecutableName(exeName),
1758
+ path: exePath
1759
+ };
1760
+ }
1761
+ function findDesktopFilePath(desktopId) {
1762
+ const candidates = [
1763
+ path.join(os.homedir(), ".local", "share", "applications", desktopId),
1764
+ path.join("/usr/local/share/applications", desktopId),
1765
+ path.join("/usr/share/applications", desktopId),
1766
+ path.join("/var/lib/snapd/desktop/applications", desktopId)
1767
+ ];
1768
+ for (const candidate of candidates) if (exists$1(candidate)) return candidate;
1769
+ return null;
1770
+ }
1771
+ function readDesktopExecLine(desktopPath) {
1772
+ try {
1773
+ const lines = fs.readFileSync(desktopPath, "utf8").split(/\r?\n/);
1774
+ for (const line of lines) if (line.startsWith("Exec=")) return line.slice(5).trim();
1775
+ } catch {}
1776
+ return null;
1777
+ }
1778
+ function extractExecutableFromExecLine(execLine) {
1779
+ const tokens = splitExecLine(execLine);
1780
+ for (const token of tokens) {
1781
+ if (!token) continue;
1782
+ if (token === "env") continue;
1783
+ if (token.includes("=") && !token.startsWith("/") && !token.includes("\\")) continue;
1784
+ return token.replace(/^["']|["']$/g, "");
1785
+ }
1786
+ return null;
1787
+ }
1788
+ function splitExecLine(line) {
1789
+ const tokens = [];
1790
+ let current = "";
1791
+ let inQuotes = false;
1792
+ let quoteChar = "";
1793
+ for (let i = 0; i < line.length; i += 1) {
1794
+ const ch = line[i];
1795
+ if ((ch === "\"" || ch === "'") && (!inQuotes || ch === quoteChar)) {
1796
+ if (inQuotes) {
1797
+ inQuotes = false;
1798
+ quoteChar = "";
1799
+ } else {
1800
+ inQuotes = true;
1801
+ quoteChar = ch;
1802
+ }
1803
+ continue;
1804
+ }
1805
+ if (!inQuotes && /\s/.test(ch)) {
1806
+ if (current) {
1807
+ tokens.push(current);
1808
+ current = "";
1809
+ }
1810
+ continue;
1811
+ }
1812
+ current += ch;
1813
+ }
1814
+ if (current) tokens.push(current);
1815
+ return tokens;
1816
+ }
1817
+ function resolveLinuxExecutablePath(command) {
1818
+ const cleaned = command.trim().replace(/%[a-zA-Z]/g, "");
1819
+ if (!cleaned) return null;
1820
+ if (cleaned.startsWith("/")) return cleaned;
1821
+ const resolved = execText("which", [cleaned], 800);
1822
+ return resolved ? resolved.trim() : null;
1823
+ }
1824
+ function readWindowsProgId() {
1825
+ const output = execText("reg", [
1826
+ "query",
1827
+ "HKCU\\Software\\Microsoft\\Windows\\Shell\\Associations\\UrlAssociations\\http\\UserChoice",
1828
+ "/v",
1829
+ "ProgId"
1830
+ ]);
1831
+ if (!output) return null;
1832
+ return output.match(/ProgId\s+REG_\w+\s+(.+)$/im)?.[1]?.trim() || null;
1833
+ }
1834
+ function readWindowsCommandForProgId(progId) {
1835
+ const output = execText("reg", [
1836
+ "query",
1837
+ progId === "http" ? "HKCR\\http\\shell\\open\\command" : `HKCR\\${progId}\\shell\\open\\command`,
1838
+ "/ve"
1839
+ ]);
1840
+ if (!output) return null;
1841
+ return output.match(/REG_\w+\s+(.+)$/im)?.[1]?.trim() || null;
1842
+ }
1843
+ function expandWindowsEnvVars(value) {
1844
+ return value.replace(/%([^%]+)%/g, (_match, name) => {
1845
+ const key = String(name ?? "").trim();
1846
+ return key ? process.env[key] ?? `%${key}%` : _match;
1847
+ });
1848
+ }
1849
+ function extractWindowsExecutablePath(command) {
1850
+ const quoted = command.match(/"([^"]+\\.exe)"/i);
1851
+ if (quoted?.[1]) return quoted[1];
1852
+ const unquoted = command.match(/([^\\s]+\\.exe)/i);
1853
+ if (unquoted?.[1]) return unquoted[1];
1854
+ return null;
1855
+ }
1856
+ function findFirstExecutable(candidates) {
1857
+ for (const candidate of candidates) if (exists$1(candidate.path)) return candidate;
1858
+ return null;
1859
+ }
1860
+ function findChromeExecutableMac() {
1861
+ return findFirstExecutable([
1862
+ {
1863
+ kind: "chrome",
1864
+ path: "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"
1865
+ },
1866
+ {
1867
+ kind: "chrome",
1868
+ path: path.join(os.homedir(), "Applications/Google Chrome.app/Contents/MacOS/Google Chrome")
1869
+ },
1870
+ {
1871
+ kind: "brave",
1872
+ path: "/Applications/Brave Browser.app/Contents/MacOS/Brave Browser"
1873
+ },
1874
+ {
1875
+ kind: "brave",
1876
+ path: path.join(os.homedir(), "Applications/Brave Browser.app/Contents/MacOS/Brave Browser")
1877
+ },
1878
+ {
1879
+ kind: "edge",
1880
+ path: "/Applications/Microsoft Edge.app/Contents/MacOS/Microsoft Edge"
1881
+ },
1882
+ {
1883
+ kind: "edge",
1884
+ path: path.join(os.homedir(), "Applications/Microsoft Edge.app/Contents/MacOS/Microsoft Edge")
1885
+ },
1886
+ {
1887
+ kind: "chromium",
1888
+ path: "/Applications/Chromium.app/Contents/MacOS/Chromium"
1889
+ },
1890
+ {
1891
+ kind: "chromium",
1892
+ path: path.join(os.homedir(), "Applications/Chromium.app/Contents/MacOS/Chromium")
1893
+ },
1894
+ {
1895
+ kind: "canary",
1896
+ path: "/Applications/Google Chrome Canary.app/Contents/MacOS/Google Chrome Canary"
1897
+ },
1898
+ {
1899
+ kind: "canary",
1900
+ path: path.join(os.homedir(), "Applications/Google Chrome Canary.app/Contents/MacOS/Google Chrome Canary")
1901
+ }
1902
+ ]);
1903
+ }
1904
+ function findChromeExecutableLinux() {
1905
+ return findFirstExecutable([
1906
+ {
1907
+ kind: "chrome",
1908
+ path: "/usr/bin/google-chrome"
1909
+ },
1910
+ {
1911
+ kind: "chrome",
1912
+ path: "/usr/bin/google-chrome-stable"
1913
+ },
1914
+ {
1915
+ kind: "chrome",
1916
+ path: "/usr/bin/chrome"
1917
+ },
1918
+ {
1919
+ kind: "brave",
1920
+ path: "/usr/bin/brave-browser"
1921
+ },
1922
+ {
1923
+ kind: "brave",
1924
+ path: "/usr/bin/brave-browser-stable"
1925
+ },
1926
+ {
1927
+ kind: "brave",
1928
+ path: "/usr/bin/brave"
1929
+ },
1930
+ {
1931
+ kind: "brave",
1932
+ path: "/snap/bin/brave"
1933
+ },
1934
+ {
1935
+ kind: "edge",
1936
+ path: "/usr/bin/microsoft-edge"
1937
+ },
1938
+ {
1939
+ kind: "edge",
1940
+ path: "/usr/bin/microsoft-edge-stable"
1941
+ },
1942
+ {
1943
+ kind: "chromium",
1944
+ path: "/usr/bin/chromium"
1945
+ },
1946
+ {
1947
+ kind: "chromium",
1948
+ path: "/usr/bin/chromium-browser"
1949
+ },
1950
+ {
1951
+ kind: "chromium",
1952
+ path: "/snap/bin/chromium"
1953
+ }
1954
+ ]);
1955
+ }
1956
+ function findChromeExecutableWindows() {
1957
+ const localAppData = process.env.LOCALAPPDATA ?? "";
1958
+ const programFiles = process.env.ProgramFiles ?? "C:\\Program Files";
1959
+ const programFilesX86 = process.env["ProgramFiles(x86)"] ?? "C:\\Program Files (x86)";
1960
+ const joinWin = path.win32.join;
1961
+ const candidates = [];
1962
+ if (localAppData) {
1963
+ candidates.push({
1964
+ kind: "chrome",
1965
+ path: joinWin(localAppData, "Google", "Chrome", "Application", "chrome.exe")
1966
+ });
1967
+ candidates.push({
1968
+ kind: "brave",
1969
+ path: joinWin(localAppData, "BraveSoftware", "Brave-Browser", "Application", "brave.exe")
1970
+ });
1971
+ candidates.push({
1972
+ kind: "edge",
1973
+ path: joinWin(localAppData, "Microsoft", "Edge", "Application", "msedge.exe")
1974
+ });
1975
+ candidates.push({
1976
+ kind: "chromium",
1977
+ path: joinWin(localAppData, "Chromium", "Application", "chrome.exe")
1978
+ });
1979
+ candidates.push({
1980
+ kind: "canary",
1981
+ path: joinWin(localAppData, "Google", "Chrome SxS", "Application", "chrome.exe")
1982
+ });
1983
+ }
1984
+ candidates.push({
1985
+ kind: "chrome",
1986
+ path: joinWin(programFiles, "Google", "Chrome", "Application", "chrome.exe")
1987
+ });
1988
+ candidates.push({
1989
+ kind: "chrome",
1990
+ path: joinWin(programFilesX86, "Google", "Chrome", "Application", "chrome.exe")
1991
+ });
1992
+ candidates.push({
1993
+ kind: "brave",
1994
+ path: joinWin(programFiles, "BraveSoftware", "Brave-Browser", "Application", "brave.exe")
1995
+ });
1996
+ candidates.push({
1997
+ kind: "brave",
1998
+ path: joinWin(programFilesX86, "BraveSoftware", "Brave-Browser", "Application", "brave.exe")
1999
+ });
2000
+ candidates.push({
2001
+ kind: "edge",
2002
+ path: joinWin(programFiles, "Microsoft", "Edge", "Application", "msedge.exe")
2003
+ });
2004
+ candidates.push({
2005
+ kind: "edge",
2006
+ path: joinWin(programFilesX86, "Microsoft", "Edge", "Application", "msedge.exe")
2007
+ });
2008
+ return findFirstExecutable(candidates);
2009
+ }
2010
+ function resolveBrowserExecutableForPlatform(resolved, platform) {
2011
+ if (resolved.executablePath) {
2012
+ if (!exists$1(resolved.executablePath)) throw new Error(`browser.executablePath not found: ${resolved.executablePath}`);
2013
+ return {
2014
+ kind: "custom",
2015
+ path: resolved.executablePath
2016
+ };
2017
+ }
2018
+ const detected = detectDefaultChromiumExecutable(platform);
2019
+ if (detected) return detected;
2020
+ if (platform === "darwin") return findChromeExecutableMac();
2021
+ if (platform === "linux") return findChromeExecutableLinux();
2022
+ if (platform === "win32") return findChromeExecutableWindows();
2023
+ return null;
2024
+ }
2025
+
2026
+ //#endregion
2027
+ //#region src/infra/ports-lsof.ts
2028
+ const LSOF_CANDIDATES = process.platform === "darwin" ? ["/usr/sbin/lsof", "/usr/bin/lsof"] : ["/usr/bin/lsof", "/usr/sbin/lsof"];
2029
+ function resolveLsofCommandSync() {
2030
+ for (const candidate of LSOF_CANDIDATES) try {
2031
+ fs.accessSync(candidate, fs.constants.X_OK);
2032
+ return candidate;
2033
+ } catch {}
2034
+ return "lsof";
2035
+ }
2036
+
2037
+ //#endregion
2038
+ //#region src/infra/ports-probe.ts
2039
+ async function tryListenOnPort(params) {
2040
+ const listenOptions = { port: params.port };
2041
+ if (params.host) listenOptions.host = params.host;
2042
+ if (typeof params.exclusive === "boolean") listenOptions.exclusive = params.exclusive;
2043
+ await new Promise((resolve, reject) => {
2044
+ const tester = net.createServer().once("error", (err) => reject(err)).once("listening", () => {
2045
+ tester.close(() => resolve());
2046
+ }).listen(listenOptions);
2047
+ });
2048
+ }
2049
+
2050
+ //#endregion
2051
+ //#region src/infra/ports.ts
2052
+ var PortInUseError = class extends Error {
2053
+ constructor(port, details) {
2054
+ super(`Port ${port} is already in use.`);
2055
+ this.name = "PortInUseError";
2056
+ this.port = port;
2057
+ this.details = details;
2058
+ }
2059
+ };
2060
+ async function ensurePortAvailable(port) {
2061
+ try {
2062
+ await tryListenOnPort({ port });
2063
+ } catch (err) {
2064
+ if (isErrno(err) && err.code === "EADDRINUSE") throw new PortInUseError(port);
2065
+ throw err;
2066
+ }
2067
+ }
2068
+
2069
+ //#endregion
2070
+ //#region src/browser/chrome.profile-decoration.ts
2071
+ function decoratedMarkerPath(userDataDir) {
2072
+ return path.join(userDataDir, ".openclaw-profile-decorated");
2073
+ }
2074
+ function safeReadJson(filePath) {
2075
+ try {
2076
+ if (!fs.existsSync(filePath)) return null;
2077
+ const raw = fs.readFileSync(filePath, "utf-8");
2078
+ const parsed = JSON.parse(raw);
2079
+ if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed)) return null;
2080
+ return parsed;
2081
+ } catch {
2082
+ return null;
2083
+ }
2084
+ }
2085
+ function safeWriteJson(filePath, data) {
2086
+ fs.mkdirSync(path.dirname(filePath), { recursive: true });
2087
+ fs.writeFileSync(filePath, JSON.stringify(data, null, 2));
2088
+ }
2089
+ function setDeep(obj, keys, value) {
2090
+ let node = obj;
2091
+ for (const key of keys.slice(0, -1)) {
2092
+ const next = node[key];
2093
+ if (typeof next !== "object" || next === null || Array.isArray(next)) node[key] = {};
2094
+ node = node[key];
2095
+ }
2096
+ node[keys[keys.length - 1] ?? ""] = value;
2097
+ }
2098
+ function parseHexRgbToSignedArgbInt(hex) {
2099
+ const cleaned = hex.trim().replace(/^#/, "");
2100
+ if (!/^[0-9a-fA-F]{6}$/.test(cleaned)) return null;
2101
+ const argbUnsigned = 255 << 24 | Number.parseInt(cleaned, 16);
2102
+ return argbUnsigned > 2147483647 ? argbUnsigned - 4294967296 : argbUnsigned;
2103
+ }
2104
+ function isProfileDecorated(userDataDir, desiredName, desiredColorHex) {
2105
+ const desiredColorInt = parseHexRgbToSignedArgbInt(desiredColorHex);
2106
+ const localStatePath = path.join(userDataDir, "Local State");
2107
+ const preferencesPath = path.join(userDataDir, "Default", "Preferences");
2108
+ const profile = safeReadJson(localStatePath)?.profile;
2109
+ const infoCache = typeof profile === "object" && profile !== null && !Array.isArray(profile) ? profile.info_cache : null;
2110
+ const info = typeof infoCache === "object" && infoCache !== null && !Array.isArray(infoCache) && typeof infoCache.Default === "object" && infoCache.Default !== null && !Array.isArray(infoCache.Default) ? infoCache.Default : null;
2111
+ const prefs = safeReadJson(preferencesPath);
2112
+ const browserTheme = (() => {
2113
+ const browser = prefs?.browser;
2114
+ const theme = typeof browser === "object" && browser !== null && !Array.isArray(browser) ? browser.theme : null;
2115
+ return typeof theme === "object" && theme !== null && !Array.isArray(theme) ? theme : null;
2116
+ })();
2117
+ const autogeneratedTheme = (() => {
2118
+ const autogenerated = prefs?.autogenerated;
2119
+ const theme = typeof autogenerated === "object" && autogenerated !== null && !Array.isArray(autogenerated) ? autogenerated.theme : null;
2120
+ return typeof theme === "object" && theme !== null && !Array.isArray(theme) ? theme : null;
2121
+ })();
2122
+ const nameOk = typeof info?.name === "string" ? info.name === desiredName : true;
2123
+ if (desiredColorInt == null) return nameOk;
2124
+ const localSeedOk = typeof info?.profile_color_seed === "number" ? info.profile_color_seed === desiredColorInt : false;
2125
+ const prefOk = typeof browserTheme?.user_color2 === "number" && browserTheme.user_color2 === desiredColorInt || typeof autogeneratedTheme?.color === "number" && autogeneratedTheme.color === desiredColorInt;
2126
+ return nameOk && localSeedOk && prefOk;
2127
+ }
2128
+ /**
2129
+ * Best-effort profile decoration (name + lobster-orange). Chrome preference keys
2130
+ * vary by version; we keep this conservative and idempotent.
2131
+ */
2132
+ function decorateOpenClawProfile(userDataDir, opts) {
2133
+ const desiredName = opts?.name ?? DEFAULT_OPENCLAW_BROWSER_PROFILE_NAME;
2134
+ const desiredColor = (opts?.color ?? DEFAULT_OPENCLAW_BROWSER_COLOR).toUpperCase();
2135
+ const desiredColorInt = parseHexRgbToSignedArgbInt(desiredColor);
2136
+ const localStatePath = path.join(userDataDir, "Local State");
2137
+ const preferencesPath = path.join(userDataDir, "Default", "Preferences");
2138
+ const localState = safeReadJson(localStatePath) ?? {};
2139
+ setDeep(localState, [
2140
+ "profile",
2141
+ "info_cache",
2142
+ "Default",
2143
+ "name"
2144
+ ], desiredName);
2145
+ setDeep(localState, [
2146
+ "profile",
2147
+ "info_cache",
2148
+ "Default",
2149
+ "shortcut_name"
2150
+ ], desiredName);
2151
+ setDeep(localState, [
2152
+ "profile",
2153
+ "info_cache",
2154
+ "Default",
2155
+ "user_name"
2156
+ ], desiredName);
2157
+ setDeep(localState, [
2158
+ "profile",
2159
+ "info_cache",
2160
+ "Default",
2161
+ "profile_color"
2162
+ ], desiredColor);
2163
+ setDeep(localState, [
2164
+ "profile",
2165
+ "info_cache",
2166
+ "Default",
2167
+ "user_color"
2168
+ ], desiredColor);
2169
+ if (desiredColorInt != null) {
2170
+ setDeep(localState, [
2171
+ "profile",
2172
+ "info_cache",
2173
+ "Default",
2174
+ "profile_color_seed"
2175
+ ], desiredColorInt);
2176
+ setDeep(localState, [
2177
+ "profile",
2178
+ "info_cache",
2179
+ "Default",
2180
+ "profile_highlight_color"
2181
+ ], desiredColorInt);
2182
+ setDeep(localState, [
2183
+ "profile",
2184
+ "info_cache",
2185
+ "Default",
2186
+ "default_avatar_fill_color"
2187
+ ], desiredColorInt);
2188
+ setDeep(localState, [
2189
+ "profile",
2190
+ "info_cache",
2191
+ "Default",
2192
+ "default_avatar_stroke_color"
2193
+ ], desiredColorInt);
2194
+ }
2195
+ safeWriteJson(localStatePath, localState);
2196
+ const prefs = safeReadJson(preferencesPath) ?? {};
2197
+ setDeep(prefs, ["profile", "name"], desiredName);
2198
+ setDeep(prefs, ["profile", "profile_color"], desiredColor);
2199
+ setDeep(prefs, ["profile", "user_color"], desiredColor);
2200
+ if (desiredColorInt != null) {
2201
+ setDeep(prefs, [
2202
+ "autogenerated",
2203
+ "theme",
2204
+ "color"
2205
+ ], desiredColorInt);
2206
+ setDeep(prefs, [
2207
+ "browser",
2208
+ "theme",
2209
+ "user_color2"
2210
+ ], desiredColorInt);
2211
+ }
2212
+ safeWriteJson(preferencesPath, prefs);
2213
+ try {
2214
+ fs.writeFileSync(decoratedMarkerPath(userDataDir), `${Date.now()}\n`, "utf-8");
2215
+ } catch {}
2216
+ }
2217
+ function ensureProfileCleanExit(userDataDir) {
2218
+ const preferencesPath = path.join(userDataDir, "Default", "Preferences");
2219
+ const prefs = safeReadJson(preferencesPath) ?? {};
2220
+ setDeep(prefs, ["exit_type"], "Normal");
2221
+ setDeep(prefs, ["exited_cleanly"], true);
2222
+ safeWriteJson(preferencesPath, prefs);
2223
+ }
2224
+
2225
+ //#endregion
2226
+ //#region src/browser/chrome.ts
2227
+ const log = createSubsystemLogger("browser").child("chrome");
2228
+ function exists(filePath) {
2229
+ try {
2230
+ return fs.existsSync(filePath);
2231
+ } catch {
2232
+ return false;
2233
+ }
2234
+ }
2235
+ function resolveBrowserExecutable(resolved) {
2236
+ return resolveBrowserExecutableForPlatform(resolved, process.platform);
2237
+ }
2238
+ function resolveOpenClawUserDataDir(profileName = DEFAULT_OPENCLAW_BROWSER_PROFILE_NAME) {
2239
+ return path.join(CONFIG_DIR, "browser", profileName, "user-data");
2240
+ }
2241
+ function cdpUrlForPort(cdpPort) {
2242
+ return `http://127.0.0.1:${cdpPort}`;
2243
+ }
2244
+ async function isChromeReachable(cdpUrl, timeoutMs = CHROME_REACHABILITY_TIMEOUT_MS) {
2245
+ const version = await fetchChromeVersion(cdpUrl, timeoutMs);
2246
+ return Boolean(version);
2247
+ }
2248
+ async function fetchChromeVersion(cdpUrl, timeoutMs = CHROME_REACHABILITY_TIMEOUT_MS) {
2249
+ const ctrl = new AbortController();
2250
+ const t = setTimeout(ctrl.abort.bind(ctrl), timeoutMs);
2251
+ try {
2252
+ const data = await (await fetchCdpChecked(appendCdpPath(cdpUrl, "/json/version"), timeoutMs, { signal: ctrl.signal })).json();
2253
+ if (!data || typeof data !== "object") return null;
2254
+ return data;
2255
+ } catch {
2256
+ return null;
2257
+ } finally {
2258
+ clearTimeout(t);
2259
+ }
2260
+ }
2261
+ async function getChromeWebSocketUrl(cdpUrl, timeoutMs = CHROME_REACHABILITY_TIMEOUT_MS) {
2262
+ const version = await fetchChromeVersion(cdpUrl, timeoutMs);
2263
+ const wsUrl = String(version?.webSocketDebuggerUrl ?? "").trim();
2264
+ if (!wsUrl) return null;
2265
+ return normalizeCdpWsUrl(wsUrl, cdpUrl);
2266
+ }
2267
+ async function canRunCdpHealthCommand(wsUrl, timeoutMs = CHROME_WS_READY_TIMEOUT_MS) {
2268
+ return await new Promise((resolve) => {
2269
+ const ws = openCdpWebSocket(wsUrl, { handshakeTimeoutMs: timeoutMs });
2270
+ let settled = false;
2271
+ const onMessage = (raw) => {
2272
+ if (settled) return;
2273
+ let parsed = null;
2274
+ try {
2275
+ parsed = JSON.parse(rawDataToString(raw));
2276
+ } catch {
2277
+ return;
2278
+ }
2279
+ if (parsed?.id !== 1) return;
2280
+ finish(Boolean(parsed.result && typeof parsed.result === "object"));
2281
+ };
2282
+ const finish = (value) => {
2283
+ if (settled) return;
2284
+ settled = true;
2285
+ clearTimeout(timer);
2286
+ ws.off("message", onMessage);
2287
+ try {
2288
+ ws.close();
2289
+ } catch {}
2290
+ resolve(value);
2291
+ };
2292
+ const timer = setTimeout(() => {
2293
+ try {
2294
+ ws.terminate();
2295
+ } catch {}
2296
+ finish(false);
2297
+ }, Math.max(50, timeoutMs + 25));
2298
+ ws.once("open", () => {
2299
+ try {
2300
+ ws.send(JSON.stringify({
2301
+ id: 1,
2302
+ method: "Browser.getVersion"
2303
+ }));
2304
+ } catch {
2305
+ finish(false);
2306
+ }
2307
+ });
2308
+ ws.on("message", onMessage);
2309
+ ws.once("error", () => {
2310
+ finish(false);
2311
+ });
2312
+ ws.once("close", () => {
2313
+ finish(false);
2314
+ });
2315
+ });
2316
+ }
2317
+ async function isChromeCdpReady(cdpUrl, timeoutMs = CHROME_REACHABILITY_TIMEOUT_MS, handshakeTimeoutMs = CHROME_WS_READY_TIMEOUT_MS) {
2318
+ const wsUrl = await getChromeWebSocketUrl(cdpUrl, timeoutMs);
2319
+ if (!wsUrl) return false;
2320
+ return await canRunCdpHealthCommand(wsUrl, handshakeTimeoutMs);
2321
+ }
2322
+ async function launchOpenClawChrome(resolved, profile) {
2323
+ if (!profile.cdpIsLoopback) throw new Error(`Profile "${profile.name}" is remote; cannot launch local Chrome.`);
2324
+ await ensurePortAvailable(profile.cdpPort);
2325
+ const exe = resolveBrowserExecutable(resolved);
2326
+ if (!exe) throw new Error("No supported browser found (Chrome/Brave/Edge/Chromium on macOS, Linux, or Windows).");
2327
+ const userDataDir = resolveOpenClawUserDataDir(profile.name);
2328
+ fs.mkdirSync(userDataDir, { recursive: true });
2329
+ const needsDecorate = !isProfileDecorated(userDataDir, profile.name, (profile.color ?? DEFAULT_OPENCLAW_BROWSER_COLOR).toUpperCase());
2330
+ const spawnOnce = () => {
2331
+ const args = [
2332
+ `--remote-debugging-port=${profile.cdpPort}`,
2333
+ `--user-data-dir=${userDataDir}`,
2334
+ "--no-first-run",
2335
+ "--no-default-browser-check",
2336
+ "--disable-sync",
2337
+ "--disable-background-networking",
2338
+ "--disable-component-update",
2339
+ "--disable-features=Translate,MediaRouter",
2340
+ "--disable-session-crashed-bubble",
2341
+ "--hide-crash-restore-bubble",
2342
+ "--password-store=basic"
2343
+ ];
2344
+ if (resolved.headless) {
2345
+ args.push("--headless=new");
2346
+ args.push("--disable-gpu");
2347
+ }
2348
+ if (resolved.noSandbox) {
2349
+ args.push("--no-sandbox");
2350
+ args.push("--disable-setuid-sandbox");
2351
+ }
2352
+ if (process.platform === "linux") args.push("--disable-dev-shm-usage");
2353
+ if (resolved.extraArgs.length > 0) args.push(...resolved.extraArgs);
2354
+ args.push("about:blank");
2355
+ return spawn(exe.path, args, {
2356
+ stdio: "pipe",
2357
+ env: {
2358
+ ...process.env,
2359
+ HOME: os.homedir()
2360
+ }
2361
+ });
2362
+ };
2363
+ const startedAt = Date.now();
2364
+ const localStatePath = path.join(userDataDir, "Local State");
2365
+ const preferencesPath = path.join(userDataDir, "Default", "Preferences");
2366
+ if (!exists(localStatePath) || !exists(preferencesPath)) {
2367
+ const bootstrap = spawnOnce();
2368
+ const deadline = Date.now() + CHROME_BOOTSTRAP_PREFS_TIMEOUT_MS;
2369
+ while (Date.now() < deadline) {
2370
+ if (exists(localStatePath) && exists(preferencesPath)) break;
2371
+ await new Promise((r) => setTimeout(r, 100));
2372
+ }
2373
+ try {
2374
+ bootstrap.kill("SIGTERM");
2375
+ } catch {}
2376
+ const exitDeadline = Date.now() + CHROME_BOOTSTRAP_EXIT_TIMEOUT_MS;
2377
+ while (Date.now() < exitDeadline) {
2378
+ if (bootstrap.exitCode != null) break;
2379
+ await new Promise((r) => setTimeout(r, 50));
2380
+ }
2381
+ }
2382
+ if (needsDecorate) try {
2383
+ decorateOpenClawProfile(userDataDir, {
2384
+ name: profile.name,
2385
+ color: profile.color
2386
+ });
2387
+ log.info(`🦞 openclaw browser profile decorated (${profile.color})`);
2388
+ } catch (err) {
2389
+ log.warn(`openclaw browser profile decoration failed: ${String(err)}`);
2390
+ }
2391
+ try {
2392
+ ensureProfileCleanExit(userDataDir);
2393
+ } catch (err) {
2394
+ log.warn(`openclaw browser clean-exit prefs failed: ${String(err)}`);
2395
+ }
2396
+ const proc = spawnOnce();
2397
+ const stderrChunks = [];
2398
+ const onStderr = (chunk) => {
2399
+ stderrChunks.push(chunk);
2400
+ };
2401
+ proc.stderr?.on("data", onStderr);
2402
+ const readyDeadline = Date.now() + CHROME_LAUNCH_READY_WINDOW_MS;
2403
+ while (Date.now() < readyDeadline) {
2404
+ if (await isChromeReachable(profile.cdpUrl)) break;
2405
+ await new Promise((r) => setTimeout(r, CHROME_LAUNCH_READY_POLL_MS));
2406
+ }
2407
+ if (!await isChromeReachable(profile.cdpUrl)) {
2408
+ const stderrOutput = Buffer.concat(stderrChunks).toString("utf8").trim();
2409
+ const stderrHint = stderrOutput ? `\nChrome stderr:\n${stderrOutput.slice(0, CHROME_STDERR_HINT_MAX_CHARS)}` : "";
2410
+ const sandboxHint = process.platform === "linux" && !resolved.noSandbox ? "\nHint: If running in a container or as root, try setting browser.noSandbox: true in config." : "";
2411
+ try {
2412
+ proc.kill("SIGKILL");
2413
+ } catch {}
2414
+ throw new Error(`Failed to start Chrome CDP on port ${profile.cdpPort} for profile "${profile.name}".${sandboxHint}${stderrHint}`);
2415
+ }
2416
+ proc.stderr?.off("data", onStderr);
2417
+ stderrChunks.length = 0;
2418
+ const pid = proc.pid ?? -1;
2419
+ log.info(`🦞 openclaw browser started (${exe.kind}) profile "${profile.name}" on 127.0.0.1:${profile.cdpPort} (pid ${pid})`);
2420
+ return {
2421
+ pid,
2422
+ exe,
2423
+ userDataDir,
2424
+ cdpPort: profile.cdpPort,
2425
+ startedAt,
2426
+ proc
2427
+ };
2428
+ }
2429
+ async function stopOpenClawChrome(running, timeoutMs = CHROME_STOP_TIMEOUT_MS) {
2430
+ const proc = running.proc;
2431
+ if (proc.killed) return;
2432
+ try {
2433
+ proc.kill("SIGTERM");
2434
+ } catch {}
2435
+ const start = Date.now();
2436
+ while (Date.now() - start < timeoutMs) {
2437
+ if (!proc.exitCode && proc.killed) break;
2438
+ if (!await isChromeReachable(cdpUrlForPort(running.cdpPort), CHROME_STOP_PROBE_TIMEOUT_MS)) return;
2439
+ await new Promise((r) => setTimeout(r, 100));
2440
+ }
2441
+ try {
2442
+ proc.kill("SIGKILL");
2443
+ } catch {}
2444
+ }
2445
+
2446
+ //#endregion
2447
+ export { DEFAULT_DOWNLOAD_DIR as A, DEFAULT_AI_SNAPSHOT_MAX_CHARS as B, ensureChromeExtensionRelayServer as C, PROFILE_POST_RESTART_WS_TIMEOUT_MS as D, PROFILE_ATTACH_RETRY_TIMEOUT_MS as E, resolveWritablePathWithinRoot as F, DEFAULT_OPENCLAW_BROWSER_PROFILE_NAME as G, DEFAULT_BROWSER_EVALUATE_ENABLED as H, DEFAULT_FILL_FIELD_TYPE as I, rawDataToString as J, isLoopbackHost as K, normalizeBrowserFormField as L, DEFAULT_UPLOAD_DIR as M, resolveExistingPathsWithinRoot as N, resolveCdpReachabilityTimeouts as O, resolveStrictExistingPathsWithinRoot as P, DEFAULT_AI_SNAPSHOT_EFFICIENT_DEPTH as R, withCdpSocket as S, CDP_JSON_NEW_TIMEOUT_MS as T, DEFAULT_OPENCLAW_BROWSER_COLOR as U, DEFAULT_BROWSER_DEFAULT_PROFILE_NAME as V, DEFAULT_OPENCLAW_BROWSER_ENABLED as W, withBrowserNavigationPolicy as _, resolveOpenClawUserDataDir as a, fetchOk as b, resolveBrowserExecutableForPlatform as c, formatAriaSnapshot as d, normalizeCdpWsUrl as f, assertBrowserNavigationResultAllowed as g, assertBrowserNavigationAllowed as h, launchOpenClawChrome as i, DEFAULT_TRACE_DIR as j, withNoProxyForCdpUrl as k, captureScreenshot as l, InvalidBrowserNavigationUrlError as m, isChromeCdpReady as n, stopOpenClawChrome as o, snapshotAria as p, isSecureWebSocketUrl as q, isChromeReachable as r, resolveLsofCommandSync as s, getChromeWebSocketUrl as t, createTargetViaCdp as u, appendCdpPath as v, stopChromeExtensionRelayServer as w, getHeadersWithAuth as x, fetchJson as y, DEFAULT_AI_SNAPSHOT_EFFICIENT_MAX_CHARS as z };