@capyclaw/ppt-suite 2.5.68 → 2.5.70

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 (148) hide show
  1. package/composition.lock.json +5 -5
  2. package/contract/ppt-contract.json +27 -1
  3. package/dsh-suite.json +5 -5
  4. package/node_modules/@capyclaw/asset-service/src/index.mjs +6 -1
  5. package/node_modules/@capyclaw/foundation/abi/loader.mjs +8 -1
  6. package/node_modules/@capyclaw/foundation/abi/spec.mjs +5 -2
  7. package/node_modules/@capyclaw/foundation/abi/version.mjs +63 -5
  8. package/node_modules/@capyclaw/foundation/lib/host/memory.mjs +29 -2
  9. package/node_modules/@capyclaw/foundation/lib/subsystems/net.mjs +12 -2
  10. package/node_modules/@capyclaw/plugin-kit/src/editor/lease.mjs +3 -1
  11. package/node_modules/@capyclaw/ppt-editor/contract/ppt-contract.json +27 -1
  12. package/node_modules/@capyclaw/ppt-skill-api/contract/ppt-contract.json +27 -1
  13. package/node_modules/@capyclaw/ppt-skill-api/skills/pptist-api/references/tool-contract.md +1 -1
  14. package/node_modules/@capyclaw/ppt-skill-proposal/contract/ppt-contract.json +27 -1
  15. package/node_modules/@capyclaw/ppt-workbench/contract/ppt-contract.json +27 -1
  16. package/node_modules/@capyclaw/ppt-workbench/package.json +2 -2
  17. package/node_modules/@capyclaw/ppt-workbench/server/contract.mjs +1 -1
  18. package/node_modules/@capyclaw/ppt-workbench/server/embedded-host.mjs +11 -1
  19. package/node_modules/@capyclaw/ppt-workbench/server/shared/SHARED_MANIFEST.json +1 -1
  20. package/node_modules/@capyclaw/ppt-workbench/server/workbench-framework.mjs +34 -0
  21. package/node_modules/@capyclaw/service-broker/lib/control.mjs +23 -7
  22. package/node_modules/@capyclaw/service-broker/lib/engine.mjs +60 -6
  23. package/node_modules/@capyclaw/service-broker/lib/manifest.mjs +15 -2
  24. package/node_modules/@capyclaw/service-broker/lib/skills-client.mjs +17 -5
  25. package/node_modules/@capyclaw/service-host/src/index.mjs +9 -1
  26. package/node_modules/@capyclaw/tool-host/dsh-suite.json +2 -2
  27. package/node_modules/@capyclaw/tool-host/lib/host.mjs +23 -4
  28. package/node_modules/@capyclaw/tool-host/package.json +1 -1
  29. package/node_modules/@capyclaw/tool-host/src/admit-images.mjs +80 -0
  30. package/node_modules/@capyclaw/tool-host/src/caller-session.mjs +25 -0
  31. package/node_modules/@capyclaw/tool-host/src/index.mjs +45 -14
  32. package/node_modules/@capyclaw/tool-host/src/mode-gate.mjs +204 -0
  33. package/node_modules/playwright-core/LICENSE +202 -0
  34. package/node_modules/playwright-core/NOTICE +5 -0
  35. package/node_modules/playwright-core/README.md +3 -0
  36. package/node_modules/playwright-core/ThirdPartyNotices.txt +13 -0
  37. package/node_modules/playwright-core/bin/install_media_pack.ps1 +5 -0
  38. package/node_modules/playwright-core/bin/install_webkit_wsl.ps1 +33 -0
  39. package/node_modules/playwright-core/bin/reinstall_chrome_beta_linux.sh +42 -0
  40. package/node_modules/playwright-core/bin/reinstall_chrome_beta_mac.sh +13 -0
  41. package/node_modules/playwright-core/bin/reinstall_chrome_beta_win.ps1 +24 -0
  42. package/node_modules/playwright-core/bin/reinstall_chrome_stable_linux.sh +42 -0
  43. package/node_modules/playwright-core/bin/reinstall_chrome_stable_mac.sh +12 -0
  44. package/node_modules/playwright-core/bin/reinstall_chrome_stable_win.ps1 +24 -0
  45. package/node_modules/playwright-core/bin/reinstall_msedge_beta_linux.sh +48 -0
  46. package/node_modules/playwright-core/bin/reinstall_msedge_beta_mac.sh +11 -0
  47. package/node_modules/playwright-core/bin/reinstall_msedge_beta_win.ps1 +23 -0
  48. package/node_modules/playwright-core/bin/reinstall_msedge_dev_linux.sh +48 -0
  49. package/node_modules/playwright-core/bin/reinstall_msedge_dev_mac.sh +11 -0
  50. package/node_modules/playwright-core/bin/reinstall_msedge_dev_win.ps1 +23 -0
  51. package/node_modules/playwright-core/bin/reinstall_msedge_stable_linux.sh +48 -0
  52. package/node_modules/playwright-core/bin/reinstall_msedge_stable_mac.sh +11 -0
  53. package/node_modules/playwright-core/bin/reinstall_msedge_stable_win.ps1 +24 -0
  54. package/node_modules/playwright-core/browsers.json +52 -0
  55. package/node_modules/playwright-core/cli.js +22 -0
  56. package/node_modules/playwright-core/index.d.ts +17 -0
  57. package/node_modules/playwright-core/index.js +17 -0
  58. package/node_modules/playwright-core/index.mjs +28 -0
  59. package/node_modules/playwright-core/lib/bootstrap.js +149 -0
  60. package/node_modules/playwright-core/lib/coreBundle.js +78041 -0
  61. package/node_modules/playwright-core/lib/entry/cliDaemon.js +5 -0
  62. package/node_modules/playwright-core/lib/entry/dashboardApp.js +3 -0
  63. package/node_modules/playwright-core/lib/entry/mcp.js +10 -0
  64. package/node_modules/playwright-core/lib/entry/oopBrowserDownload.js +3 -0
  65. package/node_modules/playwright-core/lib/package.js +50 -0
  66. package/node_modules/playwright-core/lib/server/chromium/appIcon.png +0 -0
  67. package/node_modules/playwright-core/lib/server/electron/loader.js +123 -0
  68. package/node_modules/playwright-core/lib/serverRegistry.js +1939 -0
  69. package/node_modules/playwright-core/lib/serverRegistry.js.LICENSE +65 -0
  70. package/node_modules/playwright-core/lib/tools/cli-client/channelSessions.js +141 -0
  71. package/node_modules/playwright-core/lib/tools/cli-client/cli.js +7 -0
  72. package/node_modules/playwright-core/lib/tools/cli-client/help.json +808 -0
  73. package/node_modules/playwright-core/lib/tools/cli-client/minimist.js +128 -0
  74. package/node_modules/playwright-core/lib/tools/cli-client/output.js +352 -0
  75. package/node_modules/playwright-core/lib/tools/cli-client/program.js +422 -0
  76. package/node_modules/playwright-core/lib/tools/cli-client/registry.js +180 -0
  77. package/node_modules/playwright-core/lib/tools/cli-client/session.js +261 -0
  78. package/node_modules/playwright-core/lib/tools/dashboard/appIcon.png +0 -0
  79. package/node_modules/playwright-core/lib/tools/skills/playwright-cli/SKILL.md +493 -0
  80. package/node_modules/playwright-core/lib/tools/skills/playwright-cli/references/element-attributes.md +23 -0
  81. package/node_modules/playwright-core/lib/tools/skills/playwright-cli/references/playwright-tests.md +39 -0
  82. package/node_modules/playwright-core/lib/tools/skills/playwright-cli/references/pr-attachments.md +60 -0
  83. package/node_modules/playwright-core/lib/tools/skills/playwright-cli/references/request-mocking.md +87 -0
  84. package/node_modules/playwright-core/lib/tools/skills/playwright-cli/references/running-code.md +245 -0
  85. package/node_modules/playwright-core/lib/tools/skills/playwright-cli/references/session-management.md +227 -0
  86. package/node_modules/playwright-core/lib/tools/skills/playwright-cli/references/storage-state.md +275 -0
  87. package/node_modules/playwright-core/lib/tools/skills/playwright-cli/references/test-generation.md +433 -0
  88. package/node_modules/playwright-core/lib/tools/skills/playwright-cli/references/tracing.md +139 -0
  89. package/node_modules/playwright-core/lib/tools/skills/playwright-cli/references/video-recording.md +216 -0
  90. package/node_modules/playwright-core/lib/tools/skills/playwright-component-testing/SKILL.md +136 -0
  91. package/node_modules/playwright-core/lib/tools/skills/playwright-component-testing/references/gallery-spec.md +144 -0
  92. package/node_modules/playwright-core/lib/tools/skills/playwright-component-testing/references/migration.md +89 -0
  93. package/node_modules/playwright-core/lib/tools/skills/playwright-component-testing/references/react.md +69 -0
  94. package/node_modules/playwright-core/lib/tools/skills/playwright-component-testing/references/typing.md +173 -0
  95. package/node_modules/playwright-core/lib/tools/skills/playwright-component-testing/references/vue.md +77 -0
  96. package/node_modules/playwright-core/lib/tools/skills/playwright-trace/SKILL.md +174 -0
  97. package/node_modules/playwright-core/lib/tools/utils/extension.js +112 -0
  98. package/node_modules/playwright-core/lib/tools/utils/socketConnection.js +108 -0
  99. package/node_modules/playwright-core/lib/utilsBundle.js +86078 -0
  100. package/node_modules/playwright-core/lib/utilsBundle.js.LICENSE +1890 -0
  101. package/node_modules/playwright-core/lib/vite/dashboard/assets/codeMirrorModule--QdMvsKi.css +1 -0
  102. package/node_modules/playwright-core/lib/vite/dashboard/assets/codeMirrorModule-CQ8RZGBm.js +32 -0
  103. package/node_modules/playwright-core/lib/vite/dashboard/assets/codicon-DCmgc-ay.ttf +0 -0
  104. package/node_modules/playwright-core/lib/vite/dashboard/assets/firefox-1bWoP6pv.svg +1 -0
  105. package/node_modules/playwright-core/lib/vite/dashboard/assets/firefox-beta-k3eOH_eK.svg +1 -0
  106. package/node_modules/playwright-core/lib/vite/dashboard/assets/firefox-nightly-Cp5nfeDT.svg +1 -0
  107. package/node_modules/playwright-core/lib/vite/dashboard/assets/index-AwgwcYie.css +1 -0
  108. package/node_modules/playwright-core/lib/vite/dashboard/assets/index-hRvO4yU2.js +12 -0
  109. package/node_modules/playwright-core/lib/vite/dashboard/assets/safari-na3_-uQk.svg +1 -0
  110. package/node_modules/playwright-core/lib/vite/dashboard/index.html +29 -0
  111. package/node_modules/playwright-core/lib/vite/dashboard/playwright-logo.svg +24 -0
  112. package/node_modules/playwright-core/lib/vite/htmlReport/index.html +16 -0
  113. package/node_modules/playwright-core/lib/vite/htmlReport/report.css +2 -0
  114. package/node_modules/playwright-core/lib/vite/htmlReport/report.js +32 -0
  115. package/node_modules/playwright-core/lib/vite/recorder/assets/codeMirrorModule--QdMvsKi.css +1 -0
  116. package/node_modules/playwright-core/lib/vite/recorder/assets/codeMirrorModule-DK72OMRi.js +32 -0
  117. package/node_modules/playwright-core/lib/vite/recorder/assets/codicon-DCmgc-ay.ttf +0 -0
  118. package/node_modules/playwright-core/lib/vite/recorder/assets/index-BZpYJZ-P.css +1 -0
  119. package/node_modules/playwright-core/lib/vite/recorder/assets/index-Cn0mmDuv.js +129 -0
  120. package/node_modules/playwright-core/lib/vite/recorder/index.html +29 -0
  121. package/node_modules/playwright-core/lib/vite/recorder/playwright-logo.svg +9 -0
  122. package/node_modules/playwright-core/lib/vite/traceViewer/assets/codeMirrorModule-BEB2e8M3.js +32 -0
  123. package/node_modules/playwright-core/lib/vite/traceViewer/assets/defaultSettingsView-D-g2vrvf.js +184 -0
  124. package/node_modules/playwright-core/lib/vite/traceViewer/assets/urlMatch-L3liM589.js +1 -0
  125. package/node_modules/playwright-core/lib/vite/traceViewer/assets/xtermModule-B794rD2Z.js +7 -0
  126. package/node_modules/playwright-core/lib/vite/traceViewer/codeMirrorModule.-QdMvsKi.css +1 -0
  127. package/node_modules/playwright-core/lib/vite/traceViewer/codicon.DCmgc-ay.ttf +0 -0
  128. package/node_modules/playwright-core/lib/vite/traceViewer/defaultSettingsView.C8BeHuxd.css +1 -0
  129. package/node_modules/playwright-core/lib/vite/traceViewer/index.B_TqY17P.css +1 -0
  130. package/node_modules/playwright-core/lib/vite/traceViewer/index.html +44 -0
  131. package/node_modules/playwright-core/lib/vite/traceViewer/index.oNry3Hjv.js +1 -0
  132. package/node_modules/playwright-core/lib/vite/traceViewer/manifest.webmanifest +16 -0
  133. package/node_modules/playwright-core/lib/vite/traceViewer/playwright-logo.svg +9 -0
  134. package/node_modules/playwright-core/lib/vite/traceViewer/snapshot.B_Jk1wbt.js +1 -0
  135. package/node_modules/playwright-core/lib/vite/traceViewer/snapshot.html +10 -0
  136. package/node_modules/playwright-core/lib/vite/traceViewer/sw.bundle.js +4 -0
  137. package/node_modules/playwright-core/lib/vite/traceViewer/uiMode.CYwC_gH1.js +5 -0
  138. package/node_modules/playwright-core/lib/vite/traceViewer/uiMode.CyMfwkXJ.css +1 -0
  139. package/node_modules/playwright-core/lib/vite/traceViewer/uiMode.html +18 -0
  140. package/node_modules/playwright-core/lib/vite/traceViewer/xtermModule.kHJ-D0s7.css +1 -0
  141. package/node_modules/playwright-core/lib/webp_codec.LICENSE +173 -0
  142. package/node_modules/playwright-core/lib/webp_codec.wasm +0 -0
  143. package/node_modules/playwright-core/lib/xdg-open +1267 -0
  144. package/node_modules/playwright-core/package.json +34 -0
  145. package/node_modules/playwright-core/types/protocol.d.ts +24776 -0
  146. package/node_modules/playwright-core/types/structs.d.ts +52 -0
  147. package/node_modules/playwright-core/types/types.d.ts +27167 -0
  148. package/package.json +4 -4
@@ -1,16 +1,35 @@
1
1
  /**
2
2
  * @capyclaw/tool-host 插件:向同进程插件提供 ctx.toolHost。
3
+ *
4
+ * 同时落地“会话模式锁”这一基础设施能力:
5
+ * · 把 work-store 惰性解析器注入 toolHost(模式锁的存储真源);
6
+ * · 给 DSH tools 服务挂全局 guard(异领域调用硬拒兜底);
7
+ * · 注册框架核心工具 mode_lock / mode_status(无领域,恒可见)——显式锁定入口。
8
+ * 核心工具与 guard 的注册统一走 modeGate.install(tools)(幂等):apply 时 best-effort 挂一次,
9
+ * 若那一刻 tools 服务尚未就绪,首个套件注册(registerWithDsh / bridge host)会再调 install 补上,
10
+ * 故不依赖加载顺序。所有经 toolHost.registerWithDsh / bridge host 注册的领域能力自动受锁约束,套件零接线。
3
11
  */
4
12
  import { defineDriver } from '@capyclaw/foundation/abi'
5
13
  import { createToolHost } from '../src/index.mjs'
6
14
 
15
+ const safeGet = (ctx, name) => {
16
+ try { return ctx.reflect?.get?.(name) ?? ctx[name] } catch { return null }
17
+ }
18
+
7
19
  export const { name, inject, apply } = defineDriver({
8
20
  name: 'tool-host',
9
21
  service: 'toolHost',
10
22
  apply(ctx, config = {}) {
11
- const service = createToolHost(config)
23
+ const service = createToolHost({ ...config, getWorkStore: () => safeGet(ctx, 'workStore') })
12
24
  const dispose = ctx.provide('toolHost', service)
13
- console.log('[tool-host] ready')
14
- return () => { if (typeof dispose === 'function') dispose() }
25
+
26
+ /* 模式锁兜底闸 + 核心锁定工具(幂等)。tools 缺席则降级:稍后套件注册时经 install 补挂。 */
27
+ const tools = safeGet(ctx, 'tools')
28
+ if (tools && typeof tools.register === 'function') service.modeGate.install(tools)
29
+
30
+ console.log('[tool-host] ready (mode-gate enabled)')
31
+ return () => {
32
+ if (typeof dispose === 'function') dispose()
33
+ }
15
34
  },
16
- })
35
+ })
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@capyclaw/tool-host",
3
- "version": "0.2.0",
3
+ "version": "0.3.0",
4
4
  "description": "DSH 公共工具面插件:能力 DSL、工具注册表、参数校验、契约报告、MCP 协议壳与 instructions 模板,向插件提供 ctx.toolHost。",
5
5
  "type": "module",
6
6
  "main": "./lib/host.mjs",
@@ -0,0 +1,80 @@
1
+ /**
2
+ * 工具结果图像准入 —— DSH 原生工具面专用。
3
+ *
4
+ * 背景(2026-11-22 会话中断根因):DSH 的 ImageBlock 契约要求
5
+ * `{ type:'image', attachment: ImageAttachmentRef }`(见 @deepseek-ai/dsh-llm
6
+ * types.ts)。但部分工作台工具(rsp_screen / browser_screenshot 等)为了「图片直接
7
+ * 回给模型」返回了 MCP wire 形态的裸 base64 块 `{ type:'image', data, mimeType }`。
8
+ * 原生面 `dsh-tools/batch.ts` 把 result.content 原样写进 session 日志、不做形状校验,
9
+ * 于是坏块永久留在历史里;下一步构建请求时序列化器读 `block.attachment.attachmentId`
10
+ * 抛 TypeError(deepseek 侧包成 TRANSPORT,qwen4 侧冒泡成 UNKNOWN),该会话此后每一轮
11
+ * 都在发出前崩掉。
12
+ *
13
+ * 官方工具(read_image / mcp-client / office)都是自己先 `attachments.saveImage`
14
+ * 再回 attachment ref。这里在注册层补上同一道准入:把裸 base64 图像块持久化成
15
+ * attachment ref 再交给 DSH,一处修复覆盖所有 capyclaw 原生工具。
16
+ *
17
+ * 注意:只用于原生面(registerWithDsh)。MCP 面(createMcpHandler/dispatcher)必须
18
+ * 保留 `{data,mimeType}` wire 形态——那是 MCP 协议本身,由 dsh-mcp-client 负责落盘。
19
+ */
20
+
21
+ /** 是否是一个尚未准入的裸 base64 图像块(缺 attachment、带 string data)。 */
22
+ function isRawImageBlock(block) {
23
+ return !!block && block.type === 'image' && !block.attachment && typeof block.data === 'string'
24
+ }
25
+
26
+ /**
27
+ * 从执行上下文 / 注册上下文解析 attachments 服务。
28
+ * agent 的会话级 ctx 与 read_image 用的是同一棵上下文树,attachments 必在其上;
29
+ * 注册 ctx 作为兜底。cordis 未 inject 时 get 可能抛错,逐一吞掉换下一个候选。
30
+ * @returns {object|null} 带 saveImage 的 AttachmentStore,或 null。
31
+ */
32
+ export function resolveAttachments(exec, ctx) {
33
+ for (const candidate of [exec?.agent?.ctx, ctx]) {
34
+ if (!candidate) continue
35
+ try {
36
+ const viaGet = typeof candidate.get === 'function' ? candidate.get('attachments') : undefined
37
+ const service = viaGet ?? candidate.attachments ?? candidate.reflect?.get?.('attachments')
38
+ if (service && typeof service.saveImage === 'function') return service
39
+ } catch { /* 该上下文取不到,换下一个候选 */ }
40
+ }
41
+ return null
42
+ }
43
+
44
+ /**
45
+ * 把工具结果 content 里的裸 base64 图像块准入为持久 attachment ref。
46
+ * 非图像块、已带 attachment 的图像块原样保留;无图像块时零开销直接返回原数组。
47
+ *
48
+ * 降级原则:宁可丢图也不毒化会话——attachments 缺席或 saveImage 失败时,把该图像块
49
+ * 换成一条说明性 text 块(工具通常另有一条带落盘路径的 text 块,信息不丢)。
50
+ *
51
+ * @param {Array} content 归一化后的 MCP content 块数组。
52
+ * @param {object|null} attachments AttachmentStore(resolveAttachments 的结果)。
53
+ * @param {{ name?: string }} [opts] 可选显示名(写入 ref.name,绝不解释为路径)。
54
+ * @returns {Promise<Array>} 准入后的 content 块数组。
55
+ */
56
+ export async function admitToolResultImages(content, attachments, { name } = {}) {
57
+ if (!Array.isArray(content)) return content
58
+ if (!content.some(isRawImageBlock)) return content
59
+
60
+ const out = []
61
+ for (const block of content) {
62
+ if (!isRawImageBlock(block)) { out.push(block); continue }
63
+
64
+ const mediaType = block.mediaType || block.mimeType || 'image/png'
65
+ if (!attachments) {
66
+ out.push({ type: 'text', text: `[image omitted: no attachment service mounted; ${mediaType}]` })
67
+ continue
68
+ }
69
+ try {
70
+ const data = Buffer.from(block.data, 'base64')
71
+ const ref = await attachments.saveImage({ data, mediaType, ...(name ? { name } : {}) })
72
+ out.push({ type: 'image', attachment: ref })
73
+ } catch (error) {
74
+ const code = error?.code || error?.name || 'ERROR'
75
+ const message = String(error?.message || error)
76
+ out.push({ type: 'text', text: `[image omitted: attachment save failed (${code}): ${message}]` })
77
+ }
78
+ }
79
+ return out
80
+ }
@@ -0,0 +1,25 @@
1
+ /**
2
+ * 调用者会话上下文(框架级 ALS)。
3
+ *
4
+ * registerWithDsh 把每个原生工具的执行包进 runWithCallerSession(sid, …),
5
+ * 于是工具 handler 执行期间(哪怕 handler 只拿到 (args, ctx)、丢了 exec),
6
+ * 框架内部任何一层都能用 currentCallerSession() 取回“是谁在调我”。
7
+ *
8
+ * 用途:会话模式锁在能力发现面(discoverCapabilities)按调用者会话过滤异领域
9
+ * 描述符——conductor_discover 这类 read 工具无需自己接线 exec 即自动受约束。
10
+ *
11
+ * 无会话(HTTP/MCP 管理面、墙聚合视图)时返回 '',调用方据此降级为不过滤。
12
+ */
13
+ import { AsyncLocalStorage } from 'node:async_hooks'
14
+
15
+ const als = new AsyncLocalStorage()
16
+
17
+ /** 在指定调用者会话上下文里执行 fn(同步/异步均可,ALS 随 await 传播)。 */
18
+ export function runWithCallerSession(sessionId, fn) {
19
+ return als.run(String(sessionId || ''), fn)
20
+ }
21
+
22
+ /** 当前调用者会话 id;不在工具调用上下文里时返回 ''。 */
23
+ export function currentCallerSession() {
24
+ return als.getStore() || ''
25
+ }
@@ -12,8 +12,14 @@ import { validateToolArguments } from '@capyclaw/plugin-kit/tools/validate'
12
12
  import { normalizeToolResult, resultText } from '@capyclaw/plugin-kit/tools/result'
13
13
  import { createServiceApi } from '@capyclaw/plugin-kit/service/api'
14
14
  import { createDescriptorRegistry } from './descriptors.mjs'
15
+ import { createModeGate } from './mode-gate.mjs'
16
+ import { runWithCallerSession, currentCallerSession } from './caller-session.mjs'
17
+ import { admitToolResultImages, resolveAttachments } from './admit-images.mjs'
15
18
 
19
+ export { admitToolResultImages, resolveAttachments } from './admit-images.mjs'
16
20
  export { createDescriptorRegistry } from './descriptors.mjs'
21
+ export { createModeGate } from './mode-gate.mjs'
22
+ export { runWithCallerSession, currentCallerSession } from './caller-session.mjs'
17
23
 
18
24
  const KINDS = new Set(['read', 'write', 'control'])
19
25
 
@@ -23,12 +29,13 @@ export function defineTool({
23
29
  inputSchema = { type: 'object', properties: {}, additionalProperties: false },
24
30
  kind = 'write',
25
31
  since = '1.0.0',
32
+ domain = '',
26
33
  handler,
27
34
  } = {}) {
28
35
  if (!name || typeof name !== 'string') throw new Error('defineTool 需要 name')
29
36
  if (typeof handler !== 'function') throw new Error(`工具 ${name} 需要 handler`)
30
37
  if (!KINDS.has(kind)) throw new Error(`工具 ${name} 的 kind 非法: ${kind}`)
31
- return Object.freeze({ name, description, inputSchema, kind, since, handler })
38
+ return Object.freeze({ name, description, inputSchema, kind, since, domain: String(domain || ''), handler })
32
39
  }
33
40
 
34
41
  export function defineCapability({
@@ -40,6 +47,7 @@ export function defineCapability({
40
47
  dependsOn = [],
41
48
  provides = {},
42
49
  features = [],
50
+ domain = '',
43
51
  tools = [],
44
52
  } = {}) {
45
53
  if (!id || typeof id !== 'string') throw new Error('defineCapability 需要 id')
@@ -53,6 +61,7 @@ export function defineCapability({
53
61
  dependsOn: Object.freeze([...dependsOn]),
54
62
  provides: Object.freeze({ ...provides }),
55
63
  features: Object.freeze([...features]),
64
+ domain: String(domain || ''),
56
65
  tools: Object.freeze([...tools]),
57
66
  })
58
67
  }
@@ -99,7 +108,7 @@ function selectCapabilities(capabilities, enabled) {
99
108
  * @param {Array} capabilities
100
109
  * @param {{ enabled?: string[], ports?: object, report?: Function, reserved?: string[] }} options
101
110
  */
102
- export function createRegistry(capabilities, { enabled = null, ports = null, report = null, reserved = [] } = {}) {
111
+ export function createRegistry(capabilities, { enabled = null, ports = null, report = null, reserved = [], domain = '' } = {}) {
103
112
  validateCapabilities(capabilities)
104
113
  const selected = selectCapabilities(capabilities, enabled)
105
114
 
@@ -145,11 +154,12 @@ export function createRegistry(capabilities, { enabled = null, ports = null, rep
145
154
  title: cap.title,
146
155
  description: cap.description,
147
156
  tags: [...cap.tags],
157
+ domain: cap.domain || domain || '',
148
158
  tools: cap.tools.map((tool) => tool.name),
149
159
  })
150
160
  for (const tool of cap.tools) {
151
161
  TOOLS.push({ name: tool.name, description: tool.description, inputSchema: tool.inputSchema })
152
- TOOL_META[tool.name] = { kind: tool.kind, since: tool.since, capability: cap.id }
162
+ TOOL_META[tool.name] = { kind: tool.kind, since: tool.since, capability: cap.id, domain: tool.domain || cap.domain || domain || '' }
153
163
  TOOL_IMPL[tool.name] = (args) => tool.handler(args || {}, ctx)
154
164
  }
155
165
  }
@@ -174,7 +184,7 @@ export function createRegistry(capabilities, { enabled = null, ports = null, rep
174
184
  if (typeof tool.handler !== 'function') throw new Error(`工具 ${tool.name} 需要 handler`)
175
185
  if (TOOL_IMPL[tool.name]) throw new Error(`工具已存在: ${tool.name}`)
176
186
  TOOLS.push({ name: tool.name, description: tool.description || '', inputSchema: tool.inputSchema || { type: 'object', properties: {} } })
177
- TOOL_META[tool.name] = { kind: tool.kind || 'write', since: tool.since || '1.0.0', capability: capabilityId }
187
+ TOOL_META[tool.name] = { kind: tool.kind || 'write', since: tool.since || '1.0.0', capability: capabilityId, domain: tool.domain || domain || '' }
178
188
  TOOL_IMPL[tool.name] = (args) => tool.handler(args || {}, ctx)
179
189
  return true
180
190
  }
@@ -341,17 +351,27 @@ export function buildInstructions({
341
351
  *
342
352
  * @param {object} ctx DSH cordis context
343
353
  * @param {object} registry createRegistry() 的返回值
344
- * @param {{ namespace?: string }} options
354
+ * @param {{ namespace?: string, domain?: string, modeGate?: object }} options
355
+ * domain:本 registry 的兜底领域(工具/能力未各自声明时用);
356
+ * modeGate:会话模式锁闸(createToolHost 服务级自动注入;独立调用可显式传)。
345
357
  * @returns {() => void} 反注册全部工具
346
358
  */
347
- export function registerWithDsh(ctx, registry, { namespace = '' } = {}) {
359
+ export function registerWithDsh(ctx, registry, { namespace = '', domain = '', modeGate = null } = {}) {
348
360
  if (!ctx?.tools?.register) throw new Error('registerWithDsh: 当前 DSH 没有 ctx.tools.register')
361
+ /* 领域工具一旦注册即声明给模式锁,并挂全局 guard 兜底(幂等)。 */
362
+ if (modeGate) modeGate.install(ctx.tools)
349
363
  const disposers = []
350
364
  try {
351
365
  for (const tool of registry?.TOOLS || []) {
352
366
  const impl = registry.TOOL_IMPL?.[tool.name]
353
367
  if (typeof impl !== 'function') throw new Error(`registerWithDsh: 工具缺少实现 ${tool.name}`)
354
368
  const name = namespace ? `${namespace}_${tool.name}` : tool.name
369
+ const toolDomain = registry.TOOL_META?.[tool.name]?.domain || domain || ''
370
+ let when
371
+ if (modeGate && toolDomain) {
372
+ modeGate.declareTools(toolDomain, [name])
373
+ when = modeGate.whenFor(name)
374
+ }
355
375
  disposers.push(ctx.tools.register({
356
376
  name,
357
377
  description: tool.description || '',
@@ -364,16 +384,20 @@ export function registerWithDsh(ctx, registry, { namespace = '' } = {}) {
364
384
  ? value.content
365
385
  : normalizeToolResult(value).content),
366
386
  },
367
- execute: async (args, exec) => {
387
+ execute: async (args, exec) => runWithCallerSession(exec?.agent?.session?.id, async () => {
368
388
  const normalized = normalizeToolResult(await impl(args, exec))
369
389
  /* DSH 原生面成功恒 isError:false,失败必须 throw 才走错误路径(B 轴 P0-1)。
370
390
  * 把工具自报的 isError:true 转成 throw,与 MCP 面的失败语义对齐。 */
371
391
  if (normalized.isError === true) {
372
392
  throw new Error(resultText(normalized) || `工具 ${name} 执行失败`)
373
393
  }
374
- return normalized
375
- },
376
- }))
394
+ /* 图像准入:裸 base64 图像块(rsp_screen/browser_screenshot 等)必须先持久化成
395
+ * attachment ref 再入 session 日志,否则下一步序列化读 block.attachment.attachmentId
396
+ * 抛错、永久毒化该会话(2026-11-22 根因)。MCP 面不走这里,保留 wire 形态。 */
397
+ const content = await admitToolResultImages(normalized.content, resolveAttachments(exec, ctx), { name })
398
+ return { ...normalized, content }
399
+ }),
400
+ }, when ? { when } : {}))
377
401
  }
378
402
  } catch (error) {
379
403
  /* 全有或全无:任一工具注册失败,逆序释放已注册的,避免部分注册泄漏(B 轴 P0-6)。 */
@@ -392,9 +416,12 @@ export function registerWithDsh(ctx, registry, { namespace = '' } = {}) {
392
416
  export function createToolHost(config = {}) {
393
417
  // 跨套件能力发现注册表(互联标准 §A.4/§E)。深度校验器可选注入(suite-kit)。
394
418
  const descriptors = createDescriptorRegistry({ validate: config.validateDescriptor })
419
+ // 会话模式锁:全服务共享一个闸实例(所有套件的注册都经此)。getWorkStore 惰性解析
420
+ // (driver 注入 () => safeGet(ctx,'workStore'));底座缺席则降级为恒可见、不拒。
421
+ const modeGate = createModeGate({ getWorkStore: config.getWorkStore })
395
422
  return {
396
- api: createServiceApi('toolHost', '1.1.0', ['dsl', 'registry', 'validate', 'contract', 'mcp', 'instructions', 'native-tools', 'descriptors']),
397
- version: '0.2.0',
423
+ api: createServiceApi('toolHost', '1.2.0', ['dsl', 'registry', 'validate', 'contract', 'mcp', 'instructions', 'native-tools', 'descriptors', 'mode-gate']),
424
+ version: '0.3.0',
398
425
  defineTool,
399
426
  defineCapability,
400
427
  createRegistry: (capabilities, options = {}) => createRegistry(capabilities, { ...config, ...options }),
@@ -402,12 +429,16 @@ export function createToolHost(config = {}) {
402
429
  createToolDispatcher,
403
430
  createCapabilityReport,
404
431
  buildInstructions,
405
- registerWithDsh,
432
+ // 服务级注册自动带会话模式锁(when 可见性 + guard 硬拒 + 调用者会话 ALS),套件零接线。
433
+ registerWithDsh: (ctx, registry, options = {}) => registerWithDsh(ctx, registry, { modeGate, ...options }),
406
434
  validateToolArguments,
435
+ modeGate,
407
436
  // 能力描述符注册/发现(编排器按类型动态发现,套件零互依赖)。
408
437
  registerCapabilityDescriptor: (descriptor, options) => descriptors.register(descriptor, options),
409
438
  unregisterCapabilityDescriptor: (name) => descriptors.unregister(name),
410
- discoverCapabilities: (query) => descriptors.discover(query),
439
+ // 发现面按调用者会话过滤:原生工具调用里自动从 ALS 取会话(conductor_discover 无需接线);
440
+ // HTTP/墙等管理面无会话 → 不过滤(全局视图)。query.sessionId 可显式覆盖。
441
+ discoverCapabilities: (query = {}) => modeGate.filterEntries(descriptors.discover(query), query.sessionId || currentCallerSession()),
411
442
  capabilityCatalog: () => descriptors.catalog(),
412
443
  listSurfaces: () => descriptors.listSurfaces(),
413
444
  }
@@ -0,0 +1,204 @@
1
+ /**
2
+ * 会话模式锁 —— 工具面标准框架的强制闸(基础设施能力,非各套件自接)。
3
+ *
4
+ * 设计目标(用户诉求):锁定一种工作模式(如 rsp)后,本会话只看得见、调得动
5
+ * 该领域的工具;其余已注册能力(browser_* 等)从工具声明里消失,且即便被强行
6
+ * 调用也在分发层硬拒。所有经 tool-host 注册的能力自动受此约束。
7
+ *
8
+ * 三道原生闸里的两道在此落地:
9
+ * · when(可见性):registerWithDsh / bridge host 给每个领域工具挂 whenFor(name),
10
+ * DSH 每步 resolve() 求值,false 同时从声明与分发中排除该工具。
11
+ * · guard(分发硬拒,兜底):install(tools) 挂一个全局 ToolGuard,异领域调用返回
12
+ * denial。单调不可翻回,兜住任何绕过 when 的路径。
13
+ *
14
+ * 领域来源:TOOL_META.domain(defineTool/defineCapability/createRegistry 声明)。
15
+ * 存储真源:work-store.getSessionMode(sid)(终生固化,无 unlock)。
16
+ * 降级:无 workStore / 无 sid / 未锁定 → 恒可见、不拒(与既有“底座缺席不启用”一致)。
17
+ * 无 domain 的工具(conductor/capyclaw/框架核心 mode_*)恒可见,不受锁限制。
18
+ */
19
+
20
+ /**
21
+ * @param {{ getWorkStore?: () => object|null }} deps
22
+ */
23
+ export function createModeGate({ getWorkStore } = {}) {
24
+ const domainByTool = new Map() // 原生工具名 → domain
25
+ const domainBySuite = new Map() // 套件名 → domain(描述符发现面按 suite 兜底)
26
+ const lockCache = new Map() // sid → kind(锁终生固化,一旦落定永不变更,可缓存)
27
+ let guardInstalled = false
28
+ const guardDisposers = []
29
+ let coreToolsRegistered = false
30
+ const coreDisposers = []
31
+
32
+ function store() {
33
+ try { return typeof getWorkStore === 'function' ? getWorkStore() : null } catch { return null }
34
+ }
35
+
36
+ /** 当前会话锁定的工作模式 kind;未锁定/底座缺席/无 sid → null。 */
37
+ function modeOf(sessionId) {
38
+ const sid = String(sessionId || '').trim()
39
+ if (!sid) return null
40
+ if (lockCache.has(sid)) return lockCache.get(sid)
41
+ const ws = store()
42
+ if (!ws || typeof ws.getSessionMode !== 'function') return null
43
+ let rec = null
44
+ try { rec = ws.getSessionMode(sid) } catch { return null }
45
+ const kind = rec && rec.kind ? String(rec.kind) : null
46
+ if (kind) lockCache.set(sid, kind) // 只缓存已落定的锁;null 不缓存(可能稍后被认领)
47
+ return kind
48
+ }
49
+
50
+ /** 领域工具在指定会话里是否可见。无 domain / 未锁定 / 同领域 → 可见。 */
51
+ function isVisible(domain, sessionId) {
52
+ if (!domain) return true
53
+ const lock = modeOf(sessionId)
54
+ return !lock || domain === lock
55
+ }
56
+
57
+ /** 声明一组工具属于某领域(registerWithDsh / bridge host 注册时调用)。 */
58
+ function declareTools(domain, names = [], suite = domain) {
59
+ const d = String(domain || '').trim()
60
+ if (!d) return
61
+ for (const name of names || []) {
62
+ if (name) domainByTool.set(String(name), d)
63
+ }
64
+ if (suite) domainBySuite.set(String(suite), d)
65
+ }
66
+
67
+ /** 某工具的领域;未声明 → ''(恒可见)。 */
68
+ function domainOf(name) {
69
+ return domainByTool.get(String(name)) || ''
70
+ }
71
+
72
+ /**
73
+ * DSH register 的 when 条件。未声明领域的工具返回 undefined(不挂 when,恒可见,
74
+ * 也避免给内置/核心工具增加每步求值开销)。
75
+ * @returns {((context: object) => boolean)|undefined}
76
+ */
77
+ function whenFor(name) {
78
+ const domain = domainOf(name)
79
+ if (!domain) return undefined
80
+ return (context) => isVisible(domain, context?.agent?.session?.id)
81
+ }
82
+
83
+ /**
84
+ * 全局 ToolGuard(分发硬拒兜底)。非领域工具立即放行(不读 store,内置工具零开销);
85
+ * 领域工具在异领域锁定的会话里返回 denial。
86
+ * @returns {{reason: string, info: object}|undefined}
87
+ */
88
+ function guard(execution) {
89
+ const name = execution?.name
90
+ const domain = domainByTool.get(String(name))
91
+ if (!domain) return undefined
92
+ const lock = modeOf(execution?.agent?.session?.id)
93
+ if (!lock || domain === lock) return undefined
94
+ return {
95
+ reason: `本会话已锁定为「${lock}」工作模式,「${name}」属于「${domain}」领域,已被框架屏蔽。`
96
+ + `请只用「${lock}」领域的工具完成任务;确需其他领域,请新开一个会话。`,
97
+ info: { name: 'SessionModeLocked', code: 'SESSION_MODE_LOCKED', reason: `locked to ${lock}, tool domain ${domain}` },
98
+ }
99
+ }
100
+
101
+ /** 框架核心工具 mode_lock / mode_status(无领域,恒可见)——显式锁定入口与状态查询。
102
+ * 存储经 modeGate 的 getWorkStore 解析(不依赖插件 ctx),故可在任何 tools 就绪时机注册。 */
103
+ function coreTools() {
104
+ const textRender = (_args, value) => [{ type: 'text', text: typeof value === 'string' ? value : JSON.stringify(value, null, 2) }]
105
+ const OUTPUT_SCHEMA = { type: 'object', additionalProperties: true }
106
+ const modeLock = {
107
+ name: 'mode_lock',
108
+ description: '把当前会话锁定到一种工作模式(领域),如 rsp/browser/ppt/canvas。锁定后本会话只看得见、调得动该领域的工具,'
109
+ + '其余领域能力被框架从工具面屏蔽并在分发层硬拒。锁终生固化、不可解锁;确需其他领域请新开一个会话。',
110
+ parameters: {
111
+ type: 'object',
112
+ properties: { kind: { type: 'string', description: '要锁定的工作模式/领域,如 rsp、browser、ppt、canvas' } },
113
+ required: ['kind'],
114
+ additionalProperties: false,
115
+ },
116
+ output: { schema: OUTPUT_SCHEMA, render: textRender },
117
+ execute: async (args, exec) => {
118
+ const kind = String(args?.kind || '').trim()
119
+ if (!kind) return { ok: false, error: '需要 kind(要锁定的工作模式/领域)' }
120
+ const sid = exec?.agent?.session?.id
121
+ if (!sid) return { ok: false, error: '无法识别当前会话,不能锁定' }
122
+ const ws = store()
123
+ if (!ws || typeof ws.claimSessionMode !== 'function') return { ok: false, error: 'work-store 底座缺席,无法锁定' }
124
+ const res = ws.claimSessionMode(sid, kind, 'mode-lock', { explicit: true })
125
+ if (res && res.ok === false) {
126
+ return { ok: false, lockedTo: res.lockedTo, error: `本会话已固化为「${res.lockedTo}」模式,不能改为「${kind}」。确需其他领域请新开一个会话。` }
127
+ }
128
+ return {
129
+ ok: true, kind: res.kind, fresh: !!res.fresh, explicit: true,
130
+ message: res.fresh ? `已锁定为「${res.kind}」工作模式,本会话此后只用该领域工具。` : `本会话已处于「${res.kind}」工作模式。`,
131
+ }
132
+ },
133
+ }
134
+ const modeStatus = {
135
+ name: 'mode_status',
136
+ description: '查询当前会话的工作模式锁状态:是否锁定、锁定到哪个领域、由谁何时认领。未锁定时所有领域工具可见。',
137
+ parameters: { type: 'object', properties: {}, additionalProperties: false },
138
+ output: { schema: OUTPUT_SCHEMA, render: textRender },
139
+ execute: async (_args, exec) => {
140
+ const sid = exec?.agent?.session?.id
141
+ const ws = store()
142
+ if (!ws || typeof ws.getSessionMode !== 'function') return { locked: false, reason: 'work-store 底座缺席' }
143
+ if (!sid) return { locked: false, reason: '无法识别当前会话' }
144
+ const rec = ws.getSessionMode(sid)
145
+ if (!rec) return { locked: false, kind: null, message: '本会话未锁定工作模式,所有领域工具可见。' }
146
+ return {
147
+ locked: true, kind: rec.kind, claimedBy: rec.claimedBy || '', claimedAt: rec.claimedAt || '', explicit: !!rec.explicit,
148
+ message: `本会话已锁定为「${rec.kind}」工作模式,其余领域工具已被屏蔽。`,
149
+ }
150
+ },
151
+ }
152
+ return [modeLock, modeStatus]
153
+ }
154
+
155
+ /** 把 guard + 核心锁定工具挂到 DSH tools 服务(幂等)。每个套件在 tools 就绪时都会调用,
156
+ * 故核心工具注册不依赖 tool-host driver apply 那一刻 tools 是否已就绪(加载顺序鲁棒)。返回 disposer。 */
157
+ function install(tools) {
158
+ if (!tools) return () => {}
159
+ if (!guardInstalled && typeof tools.guard === 'function') {
160
+ try {
161
+ const dispose = tools.guard(guard)
162
+ if (typeof dispose === 'function') guardDisposers.push(dispose)
163
+ guardInstalled = true
164
+ } catch { /* 底座不支持 guard 时降级:仅 when 生效 */ }
165
+ }
166
+ if (!coreToolsRegistered && typeof tools.register === 'function') {
167
+ for (const tool of coreTools()) {
168
+ try {
169
+ const d = tools.register(tool)
170
+ if (typeof d === 'function') coreDisposers.push(d)
171
+ } catch (err) {
172
+ console.warn(`[tool-host] 注册核心工具 ${tool.name} 失败:`, err?.message || err)
173
+ }
174
+ }
175
+ coreToolsRegistered = true
176
+ }
177
+ return () => {
178
+ for (const dispose of guardDisposers.splice(0)) {
179
+ try { dispose() } catch { /* 忽略重复释放 */ }
180
+ }
181
+ for (const dispose of coreDisposers.splice(0)) {
182
+ try { dispose() } catch { /* 忽略重复释放 */ }
183
+ }
184
+ guardInstalled = false
185
+ coreToolsRegistered = false
186
+ }
187
+ }
188
+
189
+ /**
190
+ * 能力发现面过滤:锁定会话里剔除异领域描述符(conductor_discover 自动受约束)。
191
+ * 无 domain 的描述符(编排/核心基建)保留。未锁定 → 原样返回。
192
+ */
193
+ function filterEntries(entries, sessionId) {
194
+ const lock = modeOf(sessionId)
195
+ if (!lock) return entries
196
+ return (entries || []).filter((entry) => {
197
+ const d = entry?.descriptor || {}
198
+ const domain = domainByTool.get(String(d.tool || d.name)) || domainBySuite.get(String(entry?.suite)) || ''
199
+ return !domain || domain === lock
200
+ })
201
+ }
202
+
203
+ return { declareTools, domainOf, modeOf, isVisible, whenFor, guard, install, filterEntries }
204
+ }