@capyclaw/browser-workbench 0.1.22 → 0.1.24

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.
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "contractVersion": "1.3.0",
3
- "packageVersion": "0.1.22",
3
+ "packageVersion": "0.1.24",
4
4
  "toolCount": 18,
5
5
  "tools": [
6
6
  {
@@ -7,7 +7,7 @@
7
7
  "packages": {
8
8
  "@capyclaw/asset-service": {
9
9
  "role": "provider",
10
- "version": "^0.1.7",
10
+ "version": "^0.1.9",
11
11
  "path": ".",
12
12
  "provides": {
13
13
  "services": [
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@capyclaw/asset-service",
3
- "version": "0.1.7",
3
+ "version": "0.1.9",
4
4
  "description": "DSH 公共资产插件:sha256 对象仓库、/assets 路由、base64 上传与 content-type,向插件提供 ctx.assets。",
5
5
  "type": "module",
6
6
  "main": "./lib/host.mjs",
@@ -44,8 +44,8 @@
44
44
  "test": "node --test test/*.test.mjs"
45
45
  },
46
46
  "dependencies": {
47
- "@capyclaw/plugin-kit": "^0.2.0",
48
- "@capyclaw/foundation": "^0.1.1"
47
+ "@capyclaw/plugin-kit": "^0.2.12",
48
+ "@capyclaw/foundation": "^0.1.14"
49
49
  },
50
50
  "repository": {
51
51
  "type": "git",
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@capyclaw/document-workbench",
3
- "version": "0.2.5",
3
+ "version": "0.2.7",
4
4
  "description": "通用文档工作台框架:版本化文档会话、命令注册、桥路由、浏览器编辑器客户端与截图协议。",
5
5
  "type": "module",
6
6
  "main": "./src/index.mjs",
@@ -24,8 +24,8 @@
24
24
  "access": "public"
25
25
  },
26
26
  "dependencies": {
27
- "@capyclaw/plugin-kit": "^0.2.0",
28
- "@capyclaw/foundation": "^0.1.1"
27
+ "@capyclaw/plugin-kit": "^0.2.12",
28
+ "@capyclaw/foundation": "^0.1.14"
29
29
  },
30
30
  "files": [
31
31
  "src/",
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@capyclaw/foundation",
3
- "version": "0.1.12",
3
+ "version": "0.1.14",
4
4
  "description": "dsh 插件基石层:接口 ABI、插件加载器与内核服务 ctx.os。应用插件只声明接口,不写装配代码。",
5
5
  "type": "module",
6
6
  "main": "./lib/host.mjs",
@@ -43,7 +43,7 @@
43
43
  },
44
44
  "license": "MIT",
45
45
  "dependencies": {
46
- "@capyclaw/plugin-kit": "^0.2.0"
46
+ "@capyclaw/plugin-kit": "^0.2.12"
47
47
  },
48
48
  "repository": {
49
49
  "type": "git",
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@capyclaw/plugin-kit",
3
- "version": "0.2.10",
3
+ "version": "0.2.12",
4
4
  "description": "DSH 插件基础设施标准件:HTTP/MCP、单实例锁、参数校验、技能 provider、journal 存储、编辑器传输与租约。",
5
5
  "type": "module",
6
6
  "main": "./src/index.mjs",
@@ -7,7 +7,7 @@
7
7
  "packages": {
8
8
  "@capyclaw/service-host": {
9
9
  "role": "provider",
10
- "version": "^0.1.9",
10
+ "version": "^0.1.11",
11
11
  "path": ".",
12
12
  "provides": {
13
13
  "services": [
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@capyclaw/service-host",
3
- "version": "0.1.9",
3
+ "version": "0.1.11",
4
4
  "description": "DSH 公共服务宿主插件:统一 HTTP 路由、健康检查、指标、单实例锁、端口复用与静态资源,向插件提供 ctx.serviceHost。",
5
5
  "type": "module",
6
6
  "main": "./lib/host.mjs",
@@ -45,8 +45,8 @@
45
45
  "test": "node --test test/*.test.mjs"
46
46
  },
47
47
  "dependencies": {
48
- "@capyclaw/plugin-kit": "^0.2.9",
49
- "@capyclaw/foundation": "^0.1.1"
48
+ "@capyclaw/plugin-kit": "^0.2.12",
49
+ "@capyclaw/foundation": "^0.1.14"
50
50
  },
51
51
  "repository": {
52
52
  "type": "git",
@@ -7,7 +7,7 @@
7
7
  "packages": {
8
8
  "@capyclaw/tool-host": {
9
9
  "role": "provider",
10
- "version": "^0.3.3",
10
+ "version": "^0.3.5",
11
11
  "path": ".",
12
12
  "provides": {
13
13
  "services": [
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@capyclaw/tool-host",
3
- "version": "0.3.3",
3
+ "version": "0.3.5",
4
4
  "description": "DSH 公共工具面插件:能力 DSL、工具注册表、参数校验、契约报告、MCP 协议壳与 instructions 模板,向插件提供 ctx.toolHost。",
5
5
  "type": "module",
6
6
  "main": "./lib/host.mjs",
@@ -44,8 +44,8 @@
44
44
  "test": "node --test --test-isolation=none test/*.test.mjs"
45
45
  },
46
46
  "dependencies": {
47
- "@capyclaw/plugin-kit": "^0.2.0",
48
- "@capyclaw/foundation": "^0.1.1"
47
+ "@capyclaw/plugin-kit": "^0.2.12",
48
+ "@capyclaw/foundation": "^0.1.14"
49
49
  },
50
50
  "repository": {
51
51
  "type": "git",
@@ -28,6 +28,10 @@ export function createModeGate({ getWorkStore } = {}) {
28
28
  const domainByTool = new Map() // 原生工具名 → domain
29
29
  const domainBySuite = new Map() // 套件名 → domain(描述符发现面按 suite 兜底)
30
30
  const lockCache = new Map() // sid → kind(锁终生固化,一旦落定永不变更,可缓存)
31
+ // 规范工作模式种类:套件尚未 declare 时的兜底白名单(保证加载顺序鲁棒);
32
+ // 运行时与已注册 domain 取并集,容纳新增套件。防止拼错的 kind 把会话终生锁进
33
+ // 一个没有任何工具的域(会话砖化)。
34
+ const CANONICAL_KINDS = ['rsp', 'browser', 'ppt', 'canvas', 'capyclaw', 'works']
31
35
  let guardInstalled = false
32
36
  const guardDisposers = []
33
37
  let coreToolsRegistered = false
@@ -77,6 +81,19 @@ export function createModeGate({ getWorkStore } = {}) {
77
81
  return domainByTool.get(String(name)) || ''
78
82
  }
79
83
 
84
+ /** 当前可用的工作模式种类(规范白名单 ∪ 已注册 domain),排序去重。 */
85
+ function knownKinds() {
86
+ const set = new Set(CANONICAL_KINDS)
87
+ for (const d of domainBySuite.values()) set.add(d)
88
+ for (const d of domainByTool.values()) set.add(d)
89
+ return [...set].sort()
90
+ }
91
+
92
+ /** kind 是否为已知工作模式(语义校验,防砖化)。 */
93
+ function isKnownKind(kind) {
94
+ return knownKinds().includes(String(kind || ''))
95
+ }
96
+
80
97
  /**
81
98
  * DSH register 的 when 条件。未声明领域的工具返回 undefined(不挂 when,恒可见,
82
99
  * 也避免给内置/核心工具增加每步求值开销)。
@@ -125,6 +142,9 @@ export function createModeGate({ getWorkStore } = {}) {
125
142
  execute: async (args, exec) => {
126
143
  const kind = String(args?.kind || '').trim()
127
144
  if (!kind) return { ok: false, error: '需要 kind(要锁定的工作模式/领域)' }
145
+ if (!isKnownKind(kind)) {
146
+ return { ok: false, error: `未知的工作模式「${kind}」,未锁定。`, validKinds: knownKinds() }
147
+ }
128
148
  const sid = exec?.agent?.session?.id
129
149
  if (!sid) return { ok: false, error: '无法识别当前会话,不能锁定' }
130
150
  const ws = store()
@@ -268,5 +288,5 @@ export function createModeGate({ getWorkStore } = {}) {
268
288
  })
269
289
  }
270
290
 
271
- return { declareTools, domainOf, modeOf, isVisible, whenFor, guard, install, installPrompt, filterEntries }
291
+ return { declareTools, domainOf, modeOf, isVisible, whenFor, guard, install, installPrompt, filterEntries, knownKinds, isKnownKind }
272
292
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@capyclaw/browser-workbench",
3
- "version": "0.1.22",
3
+ "version": "0.1.24",
4
4
  "description": "浏览器操控工作台运行时:18 个 browser_* MCP 工具(导航/快照/点击/填写/按键/下拉/上传/滚动/批量连做/拖拽/标签页/截图)+ Playwright 引擎(有头可见、登录态持久化)+ 可视化工作台。",
5
5
  "type": "module",
6
6
  "main": "./plugin.mjs",
@@ -46,12 +46,12 @@
46
46
  "license": "MIT",
47
47
  "scripts": {},
48
48
  "dependencies": {
49
- "@capyclaw/asset-service": "^0.1.7",
50
- "@capyclaw/document-workbench": "^0.2.5",
51
- "@capyclaw/foundation": "^0.1.12",
52
- "@capyclaw/plugin-kit": "^0.2.10",
53
- "@capyclaw/service-host": "^0.1.9",
54
- "@capyclaw/tool-host": "^0.3.3",
49
+ "@capyclaw/asset-service": "^0.1.9",
50
+ "@capyclaw/document-workbench": "^0.2.7",
51
+ "@capyclaw/foundation": "^0.1.14",
52
+ "@capyclaw/plugin-kit": "^0.2.12",
53
+ "@capyclaw/service-host": "^0.1.11",
54
+ "@capyclaw/tool-host": "^0.3.5",
55
55
  "playwright": "^1.64.0"
56
56
  },
57
57
  "author": {
@@ -10,7 +10,7 @@ import { createCapabilityReport } from './shared/tool-host.mjs'
10
10
  export const CONTRACT_VERSION = '1.3.0'
11
11
 
12
12
  /** 工作台包版本。 */
13
- export const PACKAGE_VERSION = '0.1.22'
13
+ export const PACKAGE_VERSION = '0.1.24'
14
14
 
15
15
  /** 生成模型可读的能力报告。 */
16
16
  export function capabilityReport(args, workbenchUrl = '', runtime = {}) {
@@ -0,0 +1,100 @@
1
+ /**
2
+ * 跨平台浏览器内核兜底(单一真源,不为 macOS 分叉)。
3
+ *
4
+ * 背景:安装链走 `pnpm install --ignore-scripts`,会跳过 Playwright 的浏览器下载 postinstall。
5
+ * Windows 有系统 Edge/Chrome 兜底所以常常无感;macOS 全新机器若没装 Chrome、又没手动跑
6
+ * `npx playwright install chromium`,启动候选链就全失败,报「没有浏览器内核」(ENGINE_LAUNCH_FAILED)。
7
+ * Linux 干净系统同理。这里在引擎判明「缺内核」时自动跑一次 `playwright install chromium`,
8
+ * 然后让引擎重试候选链——任何平台开箱即用。
9
+ *
10
+ * 设计:纯函数 + 可注入 spawn/require,便于单测;真实安装只在启动失败且判明缺内核时触发,
11
+ * 每引擎只试一次(autoInstallTried),跨引擎并发共享同一次下载(inflight)。
12
+ * 用 process.execPath(宿主 node)跑 CLI,不依赖 PATH 里有 node/npx;继承 env 使
13
+ * PLAYWRIGHT_BROWSERS_PATH 等设置在「安装」与「启动查找」两侧一致。
14
+ */
15
+ import path from 'node:path'
16
+ import { spawn } from 'node:child_process'
17
+
18
+ /**
19
+ * 判明「没有浏览器内核」的启动错误(区别于撞 profile 锁 / 启动超时 / 导航失败)。
20
+ * Playwright 缺内核的典型文案:
21
+ * - "Executable doesn't exist at /path/..."(bundled chromium 未下载)
22
+ * - "Please run the following command to download new browsers: npx playwright install"
23
+ * - "Chromium distribution 'chrome' is not found at ..."(channel 找不到系统浏览器)
24
+ * 传入的通常是引擎候选明细串 `${label}: ${msg}`,label 含 chromium/chrome/msedge 字样。
25
+ */
26
+ export function isMissingBrowserError(message) {
27
+ const msg = String(message || '')
28
+ if (/Executable doesn't exist/i.test(msg)) return true
29
+ if (/download new browsers/i.test(msg)) return true
30
+ if (/playwright install/i.test(msg)) return true
31
+ if (/distribution\s+'[^']*'\s+is not found/i.test(msg)) return true
32
+ if (/is not found/i.test(msg) && /chromium|chrome|msedge|browser/i.test(msg)) return true
33
+ return false
34
+ }
35
+
36
+ /**
37
+ * 定位 Playwright CLI(install 子命令入口)。优先 require.resolve('<pkg>/cli.js'),
38
+ * 退回 package.json 同目录的 cli.js。patchright 驱动优先 patchright、回落 playwright。
39
+ * 注入 requireFn(createRequire 产物)便于单测;拿不到返回 ''。
40
+ */
41
+ export function resolvePlaywrightCli({ driver = '', requireFn } = {}) {
42
+ if (!requireFn || typeof requireFn.resolve !== 'function') return ''
43
+ const names = String(driver || '').toLowerCase() === 'patchright' ? ['patchright', 'playwright'] : ['playwright']
44
+ for (const name of names) {
45
+ try { return requireFn.resolve(`${name}/cli.js`) } catch { /* 换下一种定位 */ }
46
+ try { return path.join(path.dirname(requireFn.resolve(`${name}/package.json`)), 'cli.js') } catch { /* 没装这个包 */ }
47
+ }
48
+ return ''
49
+ }
50
+
51
+ /**
52
+ * 跑 `node cli.js install chromium`。注入 spawnFn 便于单测;真实用 process.execPath。
53
+ * stdout/stderr 收进日志(尾部 4000 字符)并可选 onLog 实时回报;超时强杀。
54
+ * 返回 { ok, code, log, timedOut }。ok 仅在退出码 0 且未超时时为真。
55
+ */
56
+ export function installChromium({ cliPath, nodePath = process.execPath, timeoutMs = 600000, onLog, spawnFn = spawn, env = process.env } = {}) {
57
+ return new Promise((resolve) => {
58
+ if (!cliPath) {
59
+ resolve({ ok: false, code: -1, log: '未找到 Playwright CLI(playwright/cli.js)', timedOut: false })
60
+ return
61
+ }
62
+ let child
63
+ try {
64
+ child = spawnFn(nodePath, [cliPath, 'install', 'chromium'], { env, stdio: ['ignore', 'pipe', 'pipe'] })
65
+ } catch (error) {
66
+ resolve({ ok: false, code: -1, log: String(error && error.message || error), timedOut: false })
67
+ return
68
+ }
69
+ const logs = []
70
+ let timedOut = false
71
+ let settled = false
72
+ const finish = (result) => { if (!settled) { settled = true; if (timer) clearTimeout(timer); resolve(result) } }
73
+ const timer = timeoutMs > 0 ? setTimeout(() => { timedOut = true; try { child.kill('SIGKILL') } catch { /* 已退出 */ } }, timeoutMs) : null
74
+ const collect = (chunk) => {
75
+ const s = String(chunk)
76
+ logs.push(s)
77
+ if (onLog) { try { onLog(s) } catch { /* 日志回报失败不影响安装 */ } }
78
+ }
79
+ if (child.stdout) child.stdout.on('data', collect)
80
+ if (child.stderr) child.stderr.on('data', collect)
81
+ child.on('error', (error) => finish({ ok: false, code: -1, log: String(error && error.message || error), timedOut }))
82
+ child.on('close', (code) => finish({ ok: code === 0 && !timedOut, code: code == null ? -1 : code, log: logs.join('').slice(-4000), timedOut }))
83
+ })
84
+ }
85
+
86
+ /* 跨引擎并发共享同一次下载:relay 每会话一个引擎,两个会话同时首启不该各下一份 Chromium。
87
+ 按 cliPath 键控;安装结束(成败皆然)即释放,下次缺内核仍可再试。 */
88
+ const inflight = new Map()
89
+
90
+ /** ensureBrowserInstalled:installChromium 的并发去重包装。 */
91
+ export function ensureBrowserInstalled(opts = {}) {
92
+ const key = opts.cliPath || 'default'
93
+ if (inflight.has(key)) return inflight.get(key)
94
+ const p = installChromium(opts).finally(() => inflight.delete(key))
95
+ inflight.set(key, p)
96
+ return p
97
+ }
98
+
99
+ /** 测试用:清空并发缓存。 */
100
+ export function _resetInflight() { inflight.clear() }
@@ -4,28 +4,28 @@
4
4
  "packages": {
5
5
  "service-host": {
6
6
  "package": "@capyclaw/service-host",
7
- "version": "0.1.9",
7
+ "version": "0.1.11",
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.3",
14
+ "version": "0.3.5",
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.7",
21
+ "version": "0.1.9",
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.8",
28
+ "version": "0.1.10",
29
29
  "apiVersion": "1.0.0",
30
30
  "spec": "@capyclaw/editor-host/src",
31
31
  "sha256": "c8b80dbd4f1b2bee9ef9001423a013031a21ea9a3b8897b5536fe6e5687f01b3"