@capyclaw/browser-suite 0.2.38 → 0.2.40
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/composition.lock.json +23 -23
- package/contract/browser-contract.json +1 -1
- package/dsh-suite.json +26 -16
- package/node_modules/@capyclaw/asset-service/dsh-suite.json +1 -1
- package/node_modules/@capyclaw/asset-service/package.json +1 -1
- package/node_modules/@capyclaw/asset-service/src/index.mjs +4 -3
- package/node_modules/@capyclaw/browser-skill-api/contract/browser-contract.json +1 -1
- package/node_modules/@capyclaw/browser-skill-api/package.json +1 -1
- package/node_modules/@capyclaw/browser-skill-api/skills/browser-api/references/tool-contract.md +1 -1
- package/node_modules/@capyclaw/browser-skill-browser/contract/browser-contract.json +1 -1
- package/node_modules/@capyclaw/browser-skill-browser/package.json +1 -1
- package/node_modules/@capyclaw/browser-skill-ops/contract/browser-contract.json +1 -1
- package/node_modules/@capyclaw/browser-skill-ops/package.json +1 -1
- package/node_modules/@capyclaw/browser-workbench/contract/browser-contract.json +1 -1
- package/node_modules/@capyclaw/browser-workbench/package.json +7 -7
- package/node_modules/@capyclaw/browser-workbench/plugin.mjs +13 -1
- package/node_modules/@capyclaw/browser-workbench/server/activate-log.mjs +66 -0
- package/node_modules/@capyclaw/browser-workbench/server/contract.mjs +1 -1
- package/node_modules/@capyclaw/browser-workbench/server/embedded-host.mjs +13 -1
- package/node_modules/@capyclaw/browser-workbench/server/engine.mjs +164 -122
- package/node_modules/@capyclaw/browser-workbench/server/routes/api.mjs +2 -2
- package/node_modules/@capyclaw/browser-workbench/server/shared/SHARED_MANIFEST.json +4 -4
- package/node_modules/@capyclaw/browser-workbench/ui/app.js +87 -10
- package/node_modules/@capyclaw/document-workbench/package.json +6 -3
- package/node_modules/@capyclaw/document-workbench/src/client/index.mjs +56 -3
- package/node_modules/@capyclaw/document-workbench/src/client/visibility.mjs +112 -0
- package/node_modules/@capyclaw/document-workbench/src/model/manifest.mjs +6 -4
- package/node_modules/@capyclaw/document-workbench/src/model/ops.mjs +7 -2
- package/node_modules/@capyclaw/document-workbench/src/session/index.mjs +8 -0
- package/node_modules/@capyclaw/document-workbench/src/store/file-backend.mjs +17 -10
- package/node_modules/@capyclaw/document-workbench/src/store/projection.mjs +39 -0
- package/node_modules/@capyclaw/document-workbench/src/testing/index.mjs +14 -11
- package/node_modules/@capyclaw/foundation/abi/assets.mjs +26 -0
- package/node_modules/@capyclaw/foundation/abi/bridge.mjs +3 -1
- package/node_modules/@capyclaw/foundation/abi/index.mjs +15 -0
- package/node_modules/@capyclaw/foundation/abi/visibility.mjs +90 -0
- package/node_modules/@capyclaw/foundation/lib/adapters/lifecycle-hook.mjs +21 -4
- package/node_modules/@capyclaw/foundation/lib/host/caps.mjs +5 -1
- package/node_modules/@capyclaw/foundation/lib/host/contract.mjs +5 -2
- package/node_modules/@capyclaw/foundation/lib/host/dsh.mjs +4 -0
- package/node_modules/@capyclaw/foundation/lib/kernel.mjs +13 -3
- package/node_modules/@capyclaw/foundation/package.json +1 -1
- package/node_modules/@capyclaw/plugin-kit/package.json +4 -1
- package/node_modules/@capyclaw/plugin-kit/src/editor/heartbeat-worker.js +5 -2
- package/node_modules/@capyclaw/plugin-kit/src/editor/heartbeat.mjs +26 -0
- package/node_modules/@capyclaw/plugin-kit/src/ledger.mjs +151 -0
- package/node_modules/@capyclaw/plugin-kit/src/service/static.mjs +79 -0
- package/node_modules/@capyclaw/plugin-kit/src/workspace/manager.mjs +13 -5
- package/node_modules/@capyclaw/portal/package.json +1 -1
- package/node_modules/@capyclaw/service-broker/README.md +1 -1
- package/node_modules/@capyclaw/service-broker/lib/host.mjs +11 -6
- package/node_modules/@capyclaw/service-broker/lib/skills-client.mjs +13 -5
- package/node_modules/@capyclaw/service-broker/package.json +1 -1
- package/node_modules/@capyclaw/service-host/dsh-suite.json +1 -1
- package/node_modules/@capyclaw/service-host/package.json +2 -2
- package/node_modules/@capyclaw/service-host/src/index.mjs +45 -27
- package/node_modules/@capyclaw/tool-host/dsh-suite.json +1 -1
- package/node_modules/@capyclaw/tool-host/lib/host.mjs +15 -4
- package/node_modules/@capyclaw/tool-host/package.json +1 -1
- package/node_modules/@capyclaw/tool-host/src/index.mjs +19 -5
- package/node_modules/@capyclaw/tool-host/src/mode-gate.mjs +71 -3
- package/package.json +12 -12
|
@@ -24,6 +24,7 @@ import { mulberry32, randInt, resolveHumanProfile, bezierPath, moveDurationMs, m
|
|
|
24
24
|
import { openInput, closeInput } from './input.mjs'
|
|
25
25
|
import { readVault, writeVault, vaultSummary } from './login-vault.mjs'
|
|
26
26
|
import { createGovernor } from './governor.mjs'
|
|
27
|
+
import { isMissingBrowserError, resolvePlaywrightCli, ensureBrowserInstalled } from './ensure-browser.mjs'
|
|
27
28
|
|
|
28
29
|
/* --------------------------------------------------------------------------
|
|
29
30
|
* 配置解析(纯函数,可单测)
|
|
@@ -95,6 +96,13 @@ export const ENGINE_CONFIG_DEFAULTS = Object.freeze({
|
|
|
95
96
|
actionRate: 8,
|
|
96
97
|
backoffBaseMs: 4000,
|
|
97
98
|
backoffMaxMs: 60000,
|
|
99
|
+
/* 跨平台内核兜底:启动候选链全失败且判明「没有浏览器内核」时,自动跑一次
|
|
100
|
+
`playwright install chromium` 再重试(macOS 全新安装最常见——安装链 --ignore-scripts
|
|
101
|
+
跳过浏览器下载、又没有系统 Edge 兜底)。单一真源,任何平台开箱即用。
|
|
102
|
+
DSH_BROWSER_AUTO_INSTALL=false 关闭(回到只给安装提示的旧行为)。 */
|
|
103
|
+
autoInstallBrowser: true,
|
|
104
|
+
/* 自动安装 Chromium 的超时(ms):下载 ~150MB,慢网络给足 10 分钟;超时强杀并回落安装提示。 */
|
|
105
|
+
autoInstallTimeoutMs: 600000,
|
|
98
106
|
})
|
|
99
107
|
|
|
100
108
|
const ENV_FIELDS = [
|
|
@@ -131,6 +139,8 @@ const ENV_FIELDS = [
|
|
|
131
139
|
['DSH_BROWSER_ACTION_RATE', 'actionRate', 'number'],
|
|
132
140
|
['DSH_BROWSER_BACKOFF_BASE_MS', 'backoffBaseMs', 'number'],
|
|
133
141
|
['DSH_BROWSER_BACKOFF_MAX_MS', 'backoffMaxMs', 'number'],
|
|
142
|
+
['DSH_BROWSER_AUTO_INSTALL', 'autoInstallBrowser', 'boolean'],
|
|
143
|
+
['DSH_BROWSER_AUTO_INSTALL_TIMEOUT_MS', 'autoInstallTimeoutMs', 'number'],
|
|
134
144
|
]
|
|
135
145
|
|
|
136
146
|
/* 这几个数字字段允许 0(idleTimeoutMs=0 禁用空闲回收;maxRunningEngines=0 不限;
|
|
@@ -631,7 +641,7 @@ export function dshAuthWallNote(text) {
|
|
|
631
641
|
return '这是 DSH Web 自身的鉴权页(401):受控浏览器没有宿主浏览器的认证 cookie,不是目标网站故障。不要重试、不要试图登录宿主 GUI;请让用户在宿主浏览器打开 dsh web 打印的带 token 地址查看界面,受控浏览器继续用于互联网任务'
|
|
632
642
|
}
|
|
633
643
|
|
|
634
|
-
export async function createBrowserEngine({ hostDir, config, playwright, processOps } = {}) { if (!hostDir) throw new Error('createBrowserEngine: 缺少 hostDir')
|
|
644
|
+
export async function createBrowserEngine({ hostDir, config, playwright, processOps, installBrowser } = {}) { if (!hostDir) throw new Error('createBrowserEngine: 缺少 hostDir')
|
|
635
645
|
/* 局部配置必须能覆盖默认值:传 {headless:true} 时 viewport 等未给的键要落到默认,
|
|
636
646
|
否则 launchPersistentContext 收到 viewport.width=undefined 直接起不来。 */
|
|
637
647
|
const cfg = config ? { ...ENGINE_CONFIG_DEFAULTS, ...config } : resolveEngineConfig()
|
|
@@ -653,6 +663,7 @@ export async function createBrowserEngine({ hostDir, config, playwright, process
|
|
|
653
663
|
/* ---- 运行态 ---- */
|
|
654
664
|
let context = null
|
|
655
665
|
let launching = null
|
|
666
|
+
let autoInstallTried = false // 本引擎是否已自动装过一次 Chromium(防缺内核→装→仍缺的死循环)
|
|
656
667
|
let channelUsed = ''
|
|
657
668
|
let detectedUa = '' // 本次启动实际使用的 UA(真实内核版本,只去掉了 Headless 一词)
|
|
658
669
|
let detectedUaFull = '' // 内核完整版本(CDP product,如 156.0.8078.4):UA 字符串按 Chrome 规则削减成 major.0.0.0,完整值只用于 UA-CH
|
|
@@ -963,131 +974,155 @@ export async function createBrowserEngine({ hostDir, config, playwright, process
|
|
|
963
974
|
const cachedUa = cfg.stealth ? readUaCache(hostDir) : null
|
|
964
975
|
let uaHint = cachedUa ? cachedUa.ua : ''
|
|
965
976
|
let uaFullHint = cachedUa ? cachedUa.build : ''
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
candidates.push({ label: '
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
const
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
const
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
977
|
+
/* 候选链抽成可重跑的函数:首轮全失败且判明「缺内核」时,自动装一次 Chromium 后再调它重试。 */
|
|
978
|
+
const runCandidates = async () => {
|
|
979
|
+
const candidates = []
|
|
980
|
+
if (cfg.executablePath) candidates.push({ label: `executablePath(${cfg.executablePath})`, options: { executablePath: cfg.executablePath } })
|
|
981
|
+
if (cfg.channel) candidates.push({ label: `channel:${cfg.channel}`, options: { channel: cfg.channel } })
|
|
982
|
+
/* 实测(bench/probe-stealth-modes.mjs):Playwright 默认的 headless 走 chromium_headless_shell,
|
|
983
|
+
淘宝 baxia 会直接判定异常并下发 punish 框;`channel: 'chromium'` 是完整浏览器的新无头模式,
|
|
984
|
+
同一台机器上 punish 从 4 个降到 0。所以反检测模式下优先完整浏览器。 */
|
|
985
|
+
if (cfg.stealth) candidates.push({ label: 'channel:chromium', options: { channel: 'chromium' } })
|
|
986
|
+
candidates.push({ label: 'chromium', options: {} })
|
|
987
|
+
if (process.platform === 'win32') {
|
|
988
|
+
candidates.push({ label: 'channel:msedge', options: { channel: 'msedge' } })
|
|
989
|
+
candidates.push({ label: 'channel:chrome', options: { channel: 'chrome' } })
|
|
990
|
+
} else {
|
|
991
|
+
candidates.push({ label: 'channel:chrome', options: { channel: 'chrome' } })
|
|
992
|
+
candidates.push({ label: 'channel:msedge', options: { channel: 'msedge' } })
|
|
993
|
+
}
|
|
994
|
+
const seen = new Set()
|
|
995
|
+
for (const candidate of candidates) {
|
|
996
|
+
const key = candidate.label
|
|
997
|
+
if (seen.has(key)) continue
|
|
998
|
+
seen.add(key)
|
|
999
|
+
/* 每个候选最多试两次:首次撞 profile 锁(ProcessSingleton)时强制清扫
|
|
1000
|
+
占用该 profile 的进程(无论父进程死活——profile 每会话独占,撞锁即冲突),
|
|
1001
|
+
然后原样重试一次。其余错误直接记入明细换下一个候选。 */
|
|
1002
|
+
/* 每个候选最多重起一次(换 UA)。计数放在 pass 循环外:放里面会被 pass 递增重置,
|
|
1003
|
+
变成「每轮都重起」,启动时间翻几倍。 */
|
|
1004
|
+
let relaunched = false
|
|
1005
|
+
for (let pass = 0; pass < 2; pass += 1) {
|
|
1006
|
+
const launchUa = uaHint
|
|
1007
|
+
try {
|
|
1008
|
+
const ctx = await launchAttempt(pw.chromium.launchPersistentContext(profileDir, {
|
|
1009
|
+
headless: cfg.headless,
|
|
1010
|
+
viewport: { width: cfg.viewportWidth, height: cfg.viewportHeight },
|
|
1011
|
+
/* 反自动化:去掉 --enable-automation(它会让 navigator.webdriver 为 true、
|
|
1012
|
+
并在窗口上挂「正在受到自动测试软件控制」条),关掉 Blink 的自动化特征,
|
|
1013
|
+
并把 UA 换成与 JS 侧一致的正牌 Chrome(HeadlessChrome 是最显眼的标记)。 */
|
|
1014
|
+
...(cfg.stealth ? {
|
|
1015
|
+
ignoreDefaultArgs: ['--enable-automation'],
|
|
1016
|
+
/* 口径一致:Accept-Language / 时区 / 屏幕尺寸都要和 JS 侧补丁报的对上。
|
|
1017
|
+
locale 决定 Accept-Language 头与 navigator.language;timezoneId 用宿主时区
|
|
1018
|
+
(和 Intl 报的一致);screen 给一个常见桌面分辨率(无头默认屏幕=视口,很假)。
|
|
1019
|
+
window-size = 视口 + 一点窗口边框,让 outerHeight > innerHeight 像真窗口。 */
|
|
1020
|
+
locale: 'zh-CN',
|
|
1021
|
+
timezoneId: (() => { try { return Intl.DateTimeFormat().resolvedOptions().timeZone || 'Asia/Shanghai' } catch { return 'Asia/Shanghai' } })(),
|
|
1022
|
+
screen: { width: 1920, height: 1080 },
|
|
1023
|
+
args: ['--disable-blink-features=AutomationControlled', `--window-size=${cfg.viewportWidth},${cfg.viewportHeight + 100}`],
|
|
1024
|
+
...(uaHint ? { userAgent: uaHint } : {}),
|
|
1025
|
+
} : {}),
|
|
1026
|
+
...candidate.options,
|
|
1027
|
+
}))
|
|
1028
|
+
context = ctx
|
|
1029
|
+
channelUsed = candidate.label
|
|
1030
|
+
/* 真实内核 UA 对齐:缓存里的版本必须与本次启动的内核一致。
|
|
1031
|
+
首启无缓存、缓存是假的、或换了 channel 都会不一致——那就带真实 UA 重起一次,
|
|
1032
|
+
让 HTTP 头与 JS 侧报同一个版本(每个候选只重起一次,避免冷启动死循环)。 */
|
|
1033
|
+
if (cfg.stealth) {
|
|
1034
|
+
const probe = ctx.pages()[0] || (typeof ctx.newPage === 'function' ? await ctx.newPage() : null)
|
|
1035
|
+
/* UA 探测也在启动关键路径上:CDP/evaluate 卡死时 8s 封顶,降级为「无真实 UA」。 */
|
|
1036
|
+
let real = null
|
|
1037
|
+
if (probe) { try { real = await withTimeout(detectRealUserAgent(probe), 8000, '真实 UA 探测') } catch { real = null } }
|
|
1038
|
+
if (real) {
|
|
1039
|
+
detectedUa = real.ua
|
|
1040
|
+
detectedUaFull = real.build
|
|
1041
|
+
uaHint = real.ua
|
|
1042
|
+
uaFullHint = real.build
|
|
1043
|
+
writeUaCache(hostDir, real.ua, real.build)
|
|
1044
|
+
} else if (uaHint) {
|
|
1045
|
+
detectedUa = uaHint
|
|
1046
|
+
}
|
|
1047
|
+
/* 判据是「本次实际用出去的 UA」:拿它和真实值比,才不会每轮都判成不一致。 */
|
|
1048
|
+
if (!relaunched && real && launchUa !== real.ua) {
|
|
1049
|
+
relaunched = true
|
|
1050
|
+
context = null
|
|
1051
|
+
try { await closeAttempt(ctx) } catch { /* 已关/超时:下面重起会撞锁再强清 */ }
|
|
1052
|
+
continue /* 同一候选带真实 UA 重起一次 */
|
|
1053
|
+
}
|
|
1033
1054
|
}
|
|
1034
|
-
/*
|
|
1035
|
-
if (
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1055
|
+
/* 指纹补丁必须在任何导航之前注入:addInitScript 对之后创建的文档生效。 */
|
|
1056
|
+
if (cfg.stealth && typeof ctx.addInitScript === 'function') {
|
|
1057
|
+
try {
|
|
1058
|
+
await ctx.addInitScript({
|
|
1059
|
+
content: stealthInitScript({
|
|
1060
|
+
platform: process.platform === 'win32' ? 'Win32' : process.platform === 'darwin' ? 'MacIntel' : 'Linux x86_64',
|
|
1061
|
+
languages: ['zh-CN', 'zh', 'en-US', 'en'],
|
|
1062
|
+
ua: uaHint || undefined,
|
|
1063
|
+
uaFull: uaFullHint || undefined,
|
|
1064
|
+
screen: { width: 1920, height: 1080 },
|
|
1065
|
+
}),
|
|
1066
|
+
})
|
|
1067
|
+
} catch { /* 老驱动不支持:stealth 降级,不影响可用性 */ }
|
|
1040
1068
|
}
|
|
1069
|
+
/* 先把保险箱里的 cookie 注回去,再注册页面与恢复标签:
|
|
1070
|
+
恢复出来的页面一加载就是已登录状态,不会先闪一下登录页。
|
|
1071
|
+
注入失败(cookie 全过期/坏数据)不影响启动。 */
|
|
1072
|
+
if (cfg.loginVault && typeof ctx.addCookies === 'function') {
|
|
1073
|
+
try {
|
|
1074
|
+
const cookies = readVault(hostDir)
|
|
1075
|
+
vaultInjected = cookies.length
|
|
1076
|
+
if (cookies.length) await ctx.addCookies(cookies)
|
|
1077
|
+
} catch { vaultInjected = 0 }
|
|
1078
|
+
}
|
|
1079
|
+
for (const page of ctx.pages()) { registerPage(page); await applyUaHints(page) }
|
|
1080
|
+
ctx.on('page', (page) => { registerPage(page); activeTabId = tabIds.get(page); void applyUaHints(page) })
|
|
1081
|
+
if (!activeTabId && tabs.size) activeTabId = [...tabs.keys()][0]
|
|
1082
|
+
await restoreJournal(ctx)
|
|
1083
|
+
return ctx
|
|
1084
|
+
} catch (error) {
|
|
1085
|
+
const msg = String(error && error.message || error)
|
|
1086
|
+
/* 撞 profile 锁或启动超时:都可能有一个占着 profile 的进程在挡路
|
|
1087
|
+
(超时往往是半启动的浏览器卡住了)。强清占用者后重试本候选一次。 */
|
|
1088
|
+
const timedOut = !!(error && error.code === 'TIMEOUT')
|
|
1089
|
+
if (pass === 0 && cfg.reapOrphans && (isProfileLockError(msg) || timedOut)) {
|
|
1090
|
+
try {
|
|
1091
|
+
const swept = await reapOrphanBrowsers({ profileDirs: [profileDir], processOps: ops, force: true })
|
|
1092
|
+
counters.orphansKilled += swept.killed.length
|
|
1093
|
+
if (swept.killed.length) continue // 清掉了占用者 → 重试本候选
|
|
1094
|
+
} catch { /* 清扫失败 → 按原错误记录 */ }
|
|
1095
|
+
}
|
|
1096
|
+
attempts.push(`${candidate.label}: ${msg.split('\n')[0].slice(0, 200)}`)
|
|
1097
|
+
break
|
|
1041
1098
|
}
|
|
1042
|
-
/* 指纹补丁必须在任何导航之前注入:addInitScript 对之后创建的文档生效。 */
|
|
1043
|
-
if (cfg.stealth && typeof ctx.addInitScript === 'function') {
|
|
1044
|
-
try {
|
|
1045
|
-
await ctx.addInitScript({
|
|
1046
|
-
content: stealthInitScript({
|
|
1047
|
-
platform: process.platform === 'win32' ? 'Win32' : process.platform === 'darwin' ? 'MacIntel' : 'Linux x86_64',
|
|
1048
|
-
languages: ['zh-CN', 'zh', 'en-US', 'en'],
|
|
1049
|
-
ua: uaHint || undefined,
|
|
1050
|
-
uaFull: uaFullHint || undefined,
|
|
1051
|
-
screen: { width: 1920, height: 1080 },
|
|
1052
|
-
}),
|
|
1053
|
-
})
|
|
1054
|
-
} catch { /* 老驱动不支持:stealth 降级,不影响可用性 */ }
|
|
1055
|
-
}
|
|
1056
|
-
/* 先把保险箱里的 cookie 注回去,再注册页面与恢复标签:
|
|
1057
|
-
恢复出来的页面一加载就是已登录状态,不会先闪一下登录页。
|
|
1058
|
-
注入失败(cookie 全过期/坏数据)不影响启动。 */
|
|
1059
|
-
if (cfg.loginVault && typeof ctx.addCookies === 'function') {
|
|
1060
|
-
try {
|
|
1061
|
-
const cookies = readVault(hostDir)
|
|
1062
|
-
vaultInjected = cookies.length
|
|
1063
|
-
if (cookies.length) await ctx.addCookies(cookies)
|
|
1064
|
-
} catch { vaultInjected = 0 }
|
|
1065
|
-
}
|
|
1066
|
-
for (const page of ctx.pages()) { registerPage(page); await applyUaHints(page) }
|
|
1067
|
-
ctx.on('page', (page) => { registerPage(page); activeTabId = tabIds.get(page); void applyUaHints(page) })
|
|
1068
|
-
if (!activeTabId && tabs.size) activeTabId = [...tabs.keys()][0]
|
|
1069
|
-
await restoreJournal(ctx)
|
|
1070
|
-
return ctx
|
|
1071
|
-
} catch (error) {
|
|
1072
|
-
const msg = String(error && error.message || error)
|
|
1073
|
-
/* 撞 profile 锁或启动超时:都可能有一个占着 profile 的进程在挡路
|
|
1074
|
-
(超时往往是半启动的浏览器卡住了)。强清占用者后重试本候选一次。 */
|
|
1075
|
-
const timedOut = !!(error && error.code === 'TIMEOUT')
|
|
1076
|
-
if (pass === 0 && cfg.reapOrphans && (isProfileLockError(msg) || timedOut)) {
|
|
1077
|
-
try {
|
|
1078
|
-
const swept = await reapOrphanBrowsers({ profileDirs: [profileDir], processOps: ops, force: true })
|
|
1079
|
-
counters.orphansKilled += swept.killed.length
|
|
1080
|
-
if (swept.killed.length) continue // 清掉了占用者 → 重试本候选
|
|
1081
|
-
} catch { /* 清扫失败 → 按原错误记录 */ }
|
|
1082
|
-
}
|
|
1083
|
-
attempts.push(`${candidate.label}: ${msg.split('\n')[0].slice(0, 200)}`)
|
|
1084
|
-
break
|
|
1085
1099
|
}
|
|
1086
1100
|
}
|
|
1101
|
+
return null
|
|
1087
1102
|
}
|
|
1103
|
+
let ctx = await runCandidates()
|
|
1104
|
+
/* 跨平台内核兜底(单一真源,见 ensure-browser.mjs):全候选失败且明细里判明「没有浏览器
|
|
1105
|
+
内核」时,自动跑一次 `playwright install chromium` 再重试候选链。macOS 全新安装最常见
|
|
1106
|
+
(安装链 --ignore-scripts 跳过浏览器下载、无系统 Edge 兜底);Windows/Linux 干净系统同样受益。
|
|
1107
|
+
每引擎只试一次;撞锁/超时等非缺内核错误不触发安装。DSH_BROWSER_AUTO_INSTALL=false 关闭。 */
|
|
1108
|
+
let installNote = ''
|
|
1109
|
+
if (!ctx && cfg.autoInstallBrowser && !autoInstallTried && attempts.some((a) => isMissingBrowserError(a))) {
|
|
1110
|
+
autoInstallTried = true
|
|
1111
|
+
const cliPath = resolvePlaywrightCli({ driver: cfg.stealthDriver, requireFn: engineRequire })
|
|
1112
|
+
process.stderr.write('[browser] 未检测到浏览器内核,自动安装 Playwright Chromium(一次性;DSH_BROWSER_AUTO_INSTALL=false 可关闭)…\n')
|
|
1113
|
+
const res = await (installBrowser || ensureBrowserInstalled)({ cliPath, timeoutMs: cfg.autoInstallTimeoutMs })
|
|
1114
|
+
if (res.ok) {
|
|
1115
|
+
process.stderr.write('[browser] Chromium 安装完成,重试启动。\n')
|
|
1116
|
+
attempts.length = 0
|
|
1117
|
+
ctx = await runCandidates()
|
|
1118
|
+
} else {
|
|
1119
|
+
installNote = `自动安装 Chromium 失败(${res.timedOut ? '超时' : '退出码 ' + res.code}):${String(res.log).slice(-300)}。`
|
|
1120
|
+
}
|
|
1121
|
+
}
|
|
1122
|
+
if (ctx) return ctx
|
|
1088
1123
|
throw engineError('ENGINE_LAUNCH_FAILED',
|
|
1089
1124
|
`浏览器启动失败(已尝试 ${attempts.length} 种方式)`,
|
|
1090
|
-
|
|
1125
|
+
`${installNote}安装 Playwright Chromium(npx playwright install chromium)或系统 Chrome/Edge 后重试。尝试明细:${attempts.join(';')}`)
|
|
1091
1126
|
})()
|
|
1092
1127
|
try {
|
|
1093
1128
|
return await launching
|
|
@@ -1784,6 +1819,8 @@ export async function createBrowserEngine({ hostDir, config, playwright, process
|
|
|
1784
1819
|
const current = caster
|
|
1785
1820
|
if (!current) return
|
|
1786
1821
|
caster = null
|
|
1822
|
+
/* 摘掉 attachCast 注册的 close 监听,否则每次切页/停播都遗留一个 once 闭包。 */
|
|
1823
|
+
try { if (current.closeHandler) current.page.off?.('close', current.closeHandler) } catch { /* 页面已关闭 */ }
|
|
1787
1824
|
try { current.session.removeAllListeners?.('Page.screencastFrame') } catch { /* 已 detach */ }
|
|
1788
1825
|
try { await current.session.send('Page.stopScreencast') } catch { /* 页面已关闭 */ }
|
|
1789
1826
|
try { await current.session.detach() } catch { /* 已断开 */ }
|
|
@@ -1801,7 +1838,10 @@ export async function createBrowserEngine({ hostDir, config, playwright, process
|
|
|
1801
1838
|
await stopCast()
|
|
1802
1839
|
const session = await context.newCDPSession(page)
|
|
1803
1840
|
const tabId = activeTabId
|
|
1804
|
-
|
|
1841
|
+
const closeHandler = () => {
|
|
1842
|
+
if (caster && caster.session === session) stopCast().catch(() => {})
|
|
1843
|
+
}
|
|
1844
|
+
caster = { page, tabId, session, closeHandler }
|
|
1805
1845
|
session.on('Page.screencastFrame', (event) => {
|
|
1806
1846
|
if (!caster || caster.session !== session || !frameSubs.size) return
|
|
1807
1847
|
/* 先 ack 再广播:ack 是「可以给下一帧了」的背压阀,晚 ack 会直接掉帧率。 */
|
|
@@ -1813,9 +1853,7 @@ export async function createBrowserEngine({ hostDir, config, playwright, process
|
|
|
1813
1853
|
height: Number(event.metadata?.deviceHeight) || cfg.viewportHeight,
|
|
1814
1854
|
})
|
|
1815
1855
|
})
|
|
1816
|
-
page.once('close',
|
|
1817
|
-
if (caster && caster.session === session) stopCast().catch(() => {})
|
|
1818
|
-
})
|
|
1856
|
+
page.once('close', closeHandler)
|
|
1819
1857
|
await session.send('Page.startScreencast', {
|
|
1820
1858
|
format: 'jpeg',
|
|
1821
1859
|
quality: Math.max(10, Math.min(Number(cfg.screencastQuality) || 60, 95)),
|
|
@@ -1933,6 +1971,10 @@ export async function createBrowserEngine({ hostDir, config, playwright, process
|
|
|
1933
1971
|
/* 无论是否落盘,都先掐掉防抖计时器:避免关闭后残留计时器再写一次日志。 */
|
|
1934
1972
|
if (journalTimer) { clearTimeout(journalTimer); journalTimer = null }
|
|
1935
1973
|
if (vaultTimer) { clearTimeout(vaultTimer); vaultTimer = null }
|
|
1974
|
+
/* 停播轮询计时器与帧订阅一并清空:stopCast 只摘当前 caster,
|
|
1975
|
+
castTimer 若还在跑会继续 attachCast,frameSubs 残留会让回收后仍以为有观众。 */
|
|
1976
|
+
if (castTimer !== null) { clearInterval(castTimer); castTimer = null }
|
|
1977
|
+
frameSubs.clear()
|
|
1936
1978
|
if (!context) {
|
|
1937
1979
|
if (!persist) { try { fs.rmSync(tabsFile, { force: true }) } catch { /* 忽略 */ } }
|
|
1938
1980
|
return false
|
|
@@ -184,7 +184,7 @@ export function createApiRoutes({ engine: fallbackEngine, getEngine, embedded })
|
|
|
184
184
|
return true
|
|
185
185
|
},
|
|
186
186
|
|
|
187
|
-
/* POST /api/control { action: 'close' | 'headless', value? }
|
|
187
|
+
/* POST /api/control { action: 'close' | 'resume' | 'headless', value? }
|
|
188
188
|
* headless 切完会关掉运行中的浏览器,下次动作按新模式重启(登录态保留)。 */
|
|
189
189
|
async (req, res, u) => {
|
|
190
190
|
if (u.pathname !== '/api/control' || req.method !== 'POST') return false
|
|
@@ -208,7 +208,7 @@ export function createApiRoutes({ engine: fallbackEngine, getEngine, embedded })
|
|
|
208
208
|
}
|
|
209
209
|
send(req, res, 400, {
|
|
210
210
|
ok: false,
|
|
211
|
-
error: { code: 'ARG_INVALID', message: `不支持 ${action || '(空)'}`, hint: '可用:close / headless' },
|
|
211
|
+
error: { code: 'ARG_INVALID', message: `不支持 ${action || '(空)'}`, hint: '可用:close / resume / headless' },
|
|
212
212
|
})
|
|
213
213
|
} catch (error) {
|
|
214
214
|
sendEngineError(req, res, error)
|
|
@@ -4,28 +4,28 @@
|
|
|
4
4
|
"packages": {
|
|
5
5
|
"service-host": {
|
|
6
6
|
"package": "@capyclaw/service-host",
|
|
7
|
-
"version": "0.1.
|
|
7
|
+
"version": "0.1.9",
|
|
8
8
|
"apiVersion": "1.0.0",
|
|
9
9
|
"spec": "@capyclaw/service-host/src",
|
|
10
10
|
"sha256": "af0a57d779bfe6a66dc935320178d591ef799a7e037f7a5823cdfa1fe5878eeb"
|
|
11
11
|
},
|
|
12
12
|
"tool-host": {
|
|
13
13
|
"package": "@capyclaw/tool-host",
|
|
14
|
-
"version": "0.3.
|
|
14
|
+
"version": "0.3.3",
|
|
15
15
|
"apiVersion": "1.1.0",
|
|
16
16
|
"spec": "@capyclaw/tool-host/src",
|
|
17
17
|
"sha256": "23256dbcd5cbee0b96b33c97e4eb4e74d0e169cbdde52f91d4526f2fc0f60d3e"
|
|
18
18
|
},
|
|
19
19
|
"asset-service": {
|
|
20
20
|
"package": "@capyclaw/asset-service",
|
|
21
|
-
"version": "0.1.
|
|
21
|
+
"version": "0.1.7",
|
|
22
22
|
"apiVersion": "1.0.0",
|
|
23
23
|
"spec": "@capyclaw/asset-service/src",
|
|
24
24
|
"sha256": "311a83e95d56dbb69b739d61aced0889b64e85eddd95b883e322e0fa5be8bd6f"
|
|
25
25
|
},
|
|
26
26
|
"editor-host": {
|
|
27
27
|
"package": "@capyclaw/editor-host",
|
|
28
|
-
"version": "0.1.
|
|
28
|
+
"version": "0.1.8",
|
|
29
29
|
"apiVersion": "1.0.0",
|
|
30
30
|
"spec": "@capyclaw/editor-host/src",
|
|
31
31
|
"sha256": "c8b80dbd4f1b2bee9ef9001423a013031a21ea9a3b8897b5536fe6e5687f01b3"
|
|
@@ -27,6 +27,7 @@
|
|
|
27
27
|
lastFrameAt: 0,
|
|
28
28
|
hasFrame: false,
|
|
29
29
|
fallbackTimer: null,
|
|
30
|
+
hintTimer: null,
|
|
30
31
|
statusBusy: false,
|
|
31
32
|
tabsSig: null,
|
|
32
33
|
}
|
|
@@ -81,12 +82,15 @@
|
|
|
81
82
|
els.placeholder.hidden = true
|
|
82
83
|
els.frame.classList.add('on')
|
|
83
84
|
els.hint.hidden = false
|
|
84
|
-
/* 提示只在第一帧后露 5
|
|
85
|
-
|
|
85
|
+
/* 提示只在第一帧后露 5 秒,之后让位给画面本身。句柄存起来,hideFrame 时取消,
|
|
86
|
+
避免画面已隐藏后旧定时器又把 hint 状态改回去(竞态)。 */
|
|
87
|
+
if (state.hintTimer) clearTimeout(state.hintTimer)
|
|
88
|
+
state.hintTimer = setTimeout(function () { els.hint.hidden = true; state.hintTimer = null }, 5000)
|
|
86
89
|
}
|
|
87
90
|
|
|
88
91
|
function hideFrame() {
|
|
89
92
|
state.hasFrame = false
|
|
93
|
+
if (state.hintTimer) { clearTimeout(state.hintTimer); state.hintTimer = null }
|
|
90
94
|
els.frame.classList.remove('on')
|
|
91
95
|
els.frame.removeAttribute('src')
|
|
92
96
|
els.placeholder.hidden = false
|
|
@@ -107,6 +111,9 @@
|
|
|
107
111
|
|
|
108
112
|
function connectLive() {
|
|
109
113
|
if (live) return
|
|
114
|
+
/* 渲染面挂起时不建帧订阅:SSE 关闭使 watching=frameSubs.size 归零,
|
|
115
|
+
* 引擎可空闲回收(标准 §5 的期望行为)。恢复前台由 startRenderLoops 重开。 */
|
|
116
|
+
if (renderSuspended) return
|
|
110
117
|
if (typeof EventSource !== 'function') { startFallback(); return }
|
|
111
118
|
try {
|
|
112
119
|
live = new EventSource(apiPath('/api/live'))
|
|
@@ -126,6 +133,7 @@
|
|
|
126
133
|
/* SSE 被中间层吃掉(或桌面 webview 不支持)时,退回 1.2s 截图,至少还能看。 */
|
|
127
134
|
function startFallback() {
|
|
128
135
|
if (state.fallbackTimer !== null) return
|
|
136
|
+
if (renderSuspended) return
|
|
129
137
|
state.fallbackTimer = setInterval(function () {
|
|
130
138
|
if (!state.running) return
|
|
131
139
|
fetch(apiPath('/api/screenshot')).then(function (r) {
|
|
@@ -148,6 +156,72 @@
|
|
|
148
156
|
state.fallbackTimer = null
|
|
149
157
|
}
|
|
150
158
|
|
|
159
|
+
/* ---------- 表面可见性(渲染面生命周期,标准 §2/§5) ---------- */
|
|
160
|
+
/* 本 UI 无构建、原样拷贝部署(build-plugin.mjs 的 copyDirs),不能 import 共享包,
|
|
161
|
+
* 故内联一个最小 visibility 订阅器(纯订阅,无桥)。协议常量真源:
|
|
162
|
+
* C/packages/capyclaw-foundation/abi/visibility.mjs —— 改那边必须同步这里。
|
|
163
|
+
* iframe 内 document.hidden 只反映顶层 tab,被宿主藏起来不触发 visibilitychange,
|
|
164
|
+
* 表面可见性只能由宿主 postMessage 告知;hidden 必须挂起渲染面。 */
|
|
165
|
+
var VISIBILITY_MESSAGE = 'capyclaw:workbench:visibility'
|
|
166
|
+
var VISIBILITY_ACK_MESSAGE = 'capyclaw:workbench:visibility-ack'
|
|
167
|
+
var VISIBILITY_STATES = ['foreground', 'background', 'hidden', 'detached']
|
|
168
|
+
|
|
169
|
+
var renderSuspended = false
|
|
170
|
+
var statusTimer = null
|
|
171
|
+
var watchdogTimer = null
|
|
172
|
+
|
|
173
|
+
/* watchdog:SSE 断流超过 3s 且浏览器在跑 → 退回截图轮询。 */
|
|
174
|
+
function watchdogTick() {
|
|
175
|
+
if (!state.running) { stopFallback(); return }
|
|
176
|
+
if (state.fallbackTimer !== null) return
|
|
177
|
+
if (Date.now() - state.lastFrameAt > 3000) startFallback()
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/* 渲染面循环的唯一起停点:句柄 null-guard,快速切换不会叠出重复定时器。
|
|
181
|
+
* 起 = 状态轮询 + SSE 看门狗 + 帧订阅(SSE,无 SSE 时兜底截屏)。 */
|
|
182
|
+
function startRenderLoops() {
|
|
183
|
+
if (statusTimer === null) statusTimer = setInterval(pollStatus, 2000)
|
|
184
|
+
if (watchdogTimer === null) watchdogTimer = setInterval(watchdogTick, 2000)
|
|
185
|
+
connectLive()
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/* 停 = 状态轮询 + SSE 看门狗 + 兜底截屏 + 帧订阅全清。SSE 关闭使
|
|
189
|
+
* watching=frameSubs.size 归零 → 引擎可空闲回收(期望行为,标准 §5)。 */
|
|
190
|
+
function stopRenderLoops() {
|
|
191
|
+
if (statusTimer !== null) { clearInterval(statusTimer); statusTimer = null }
|
|
192
|
+
if (watchdogTimer !== null) { clearInterval(watchdogTimer); watchdogTimer = null }
|
|
193
|
+
stopFallback()
|
|
194
|
+
if (live) { try { live.close() } catch (e) { /* ignore */ } live = null }
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/* 幂等:同态重复消息不重启/重停。hidden/detached 挂起渲染面;
|
|
198
|
+
* foreground/background 恢复(background 只是失焦、仍可见,渲染面保持运转)。 */
|
|
199
|
+
function setRenderVisibility(next) {
|
|
200
|
+
var suspend = next === 'hidden' || next === 'detached'
|
|
201
|
+
if (suspend === renderSuspended) return
|
|
202
|
+
renderSuspended = suspend
|
|
203
|
+
if (suspend) { stopRenderLoops(); return }
|
|
204
|
+
startRenderLoops()
|
|
205
|
+
/* 回前台立即补一次状态:隐藏期间引擎可能已被空闲回收,
|
|
206
|
+
* 靠这次 pollStatus 的 restorable→resume 把上次的页面找回来。 */
|
|
207
|
+
pollStatus()
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
function onVisibilityMessage(event) {
|
|
211
|
+
/* 只认同源宿主(浮标/portal 与本页同 origin);跨源消息一律丢弃。 */
|
|
212
|
+
if (event.origin !== location.origin) return
|
|
213
|
+
var data = event.data
|
|
214
|
+
if (!data || data.type !== VISIBILITY_MESSAGE) return
|
|
215
|
+
if (VISIBILITY_STATES.indexOf(data.state) === -1) return
|
|
216
|
+
setRenderVisibility(data.state)
|
|
217
|
+
try {
|
|
218
|
+
if (event.source) {
|
|
219
|
+
event.source.postMessage({ type: VISIBILITY_ACK_MESSAGE, state: data.state, sid: data.sid }, event.origin)
|
|
220
|
+
}
|
|
221
|
+
} catch (e) { /* 宿主已关闭等场景忽略 */ }
|
|
222
|
+
}
|
|
223
|
+
window.addEventListener('message', onVisibilityMessage)
|
|
224
|
+
|
|
151
225
|
/* ---------- 坐标映射与输入回放 ---------- */
|
|
152
226
|
|
|
153
227
|
/** object-fit: contain 的内容框 → 页面 CSS 像素坐标。 */
|
|
@@ -433,15 +507,18 @@
|
|
|
433
507
|
.catch(function () { /* 恢复失败不阻塞工作台 */ })
|
|
434
508
|
}
|
|
435
509
|
})
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
510
|
+
/* 渲染面循环统一由 startRenderLoops 起(状态轮询 2s + SSE 看门狗 2s + 帧订阅),
|
|
511
|
+
* 宿主 visibility 消息 hidden 时经 stopRenderLoops 全停、foreground 时重建。 */
|
|
512
|
+
startRenderLoops()
|
|
513
|
+
|
|
514
|
+
/* 真实卸载:清渲染面循环并摘订阅监听(新增定时器/监听皆可清理);
|
|
515
|
+
* bfcache 冻结(persisted)保持原样——定时器被冻结而非销毁,恢复时自会续跑。 */
|
|
516
|
+
window.addEventListener('pagehide', function (event) {
|
|
517
|
+
if (event && event.persisted) return
|
|
518
|
+
stopRenderLoops()
|
|
519
|
+
window.removeEventListener('message', onVisibilityMessage)
|
|
520
|
+
})
|
|
443
521
|
|
|
444
|
-
connectLive()
|
|
445
522
|
// 提示条:画面出现后第一次点击就藏起来
|
|
446
523
|
els.frame.addEventListener('focus', function () { els.hint.hidden = true }, { once: true })
|
|
447
524
|
})()
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@capyclaw/document-workbench",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.5",
|
|
4
4
|
"description": "通用文档工作台框架:版本化文档会话、命令注册、桥路由、浏览器编辑器客户端与截图协议。",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./src/index.mjs",
|
|
@@ -13,16 +13,19 @@
|
|
|
13
13
|
"./ops": "./src/ops/index.mjs",
|
|
14
14
|
"./bridge": "./src/bridge/index.mjs",
|
|
15
15
|
"./client": "./src/client/index.mjs",
|
|
16
|
+
"./client/visibility": "./src/client/visibility.mjs",
|
|
16
17
|
"./screenshot": "./src/client/screenshot.mjs",
|
|
17
18
|
"./testing": "./src/testing/index.mjs",
|
|
18
19
|
"./package.json": "./package.json",
|
|
19
|
-
"./store/file-backend": "./src/store/file-backend.mjs"
|
|
20
|
+
"./store/file-backend": "./src/store/file-backend.mjs",
|
|
21
|
+
"./store/projection": "./src/store/projection.mjs"
|
|
20
22
|
},
|
|
21
23
|
"publishConfig": {
|
|
22
24
|
"access": "public"
|
|
23
25
|
},
|
|
24
26
|
"dependencies": {
|
|
25
|
-
"@capyclaw/plugin-kit": "^0.2.0"
|
|
27
|
+
"@capyclaw/plugin-kit": "^0.2.0",
|
|
28
|
+
"@capyclaw/foundation": "^0.1.1"
|
|
26
29
|
},
|
|
27
30
|
"files": [
|
|
28
31
|
"src/",
|