@capyclaw/wall-suite 0.0.0-stage → 0.1.1

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 (156) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +20 -2
  3. package/composition.lock.json +48 -0
  4. package/contract/wall-contract.json +8 -0
  5. package/cordis.patch.yml +6 -0
  6. package/dsh-suite.json +128 -0
  7. package/lib/index.mjs +6 -0
  8. package/node_modules/@capyclaw/asset-service/README.md +48 -0
  9. package/node_modules/@capyclaw/asset-service/cordis.patch.yml +4 -0
  10. package/node_modules/@capyclaw/asset-service/dsh-suite.json +29 -0
  11. package/node_modules/@capyclaw/asset-service/lib/host.mjs +14 -0
  12. package/node_modules/@capyclaw/asset-service/package.json +59 -0
  13. package/node_modules/@capyclaw/asset-service/src/index.mjs +310 -0
  14. package/node_modules/@capyclaw/foundation/README.md +51 -0
  15. package/node_modules/@capyclaw/foundation/abi/assets.mjs +26 -0
  16. package/node_modules/@capyclaw/foundation/abi/bridge.mjs +88 -0
  17. package/node_modules/@capyclaw/foundation/abi/errors.mjs +63 -0
  18. package/node_modules/@capyclaw/foundation/abi/index.mjs +50 -0
  19. package/node_modules/@capyclaw/foundation/abi/kinds.mjs +271 -0
  20. package/node_modules/@capyclaw/foundation/abi/loader.mjs +352 -0
  21. package/node_modules/@capyclaw/foundation/abi/schema.mjs +98 -0
  22. package/node_modules/@capyclaw/foundation/abi/spec.mjs +258 -0
  23. package/node_modules/@capyclaw/foundation/abi/validate.mjs +24 -0
  24. package/node_modules/@capyclaw/foundation/abi/version.mjs +140 -0
  25. package/node_modules/@capyclaw/foundation/abi/visibility.mjs +90 -0
  26. package/node_modules/@capyclaw/foundation/cordis.patch.yml +5 -0
  27. package/node_modules/@capyclaw/foundation/kit/index.mjs +6 -0
  28. package/node_modules/@capyclaw/foundation/lib/adapters/client-panel.mjs +98 -0
  29. package/node_modules/@capyclaw/foundation/lib/adapters/host-event.mjs +35 -0
  30. package/node_modules/@capyclaw/foundation/lib/adapters/http-router.mjs +69 -0
  31. package/node_modules/@capyclaw/foundation/lib/adapters/lifecycle-hook.mjs +72 -0
  32. package/node_modules/@capyclaw/foundation/lib/adapters/own-server.mjs +113 -0
  33. package/node_modules/@capyclaw/foundation/lib/adapters/raw.mjs +69 -0
  34. package/node_modules/@capyclaw/foundation/lib/adapters/runtime-driver.mjs +120 -0
  35. package/node_modules/@capyclaw/foundation/lib/adapters/service-provider.mjs +77 -0
  36. package/node_modules/@capyclaw/foundation/lib/adapters/skill-provider.mjs +56 -0
  37. package/node_modules/@capyclaw/foundation/lib/adapters/tool-provider.mjs +50 -0
  38. package/node_modules/@capyclaw/foundation/lib/adapters/ui-panel.mjs +64 -0
  39. package/node_modules/@capyclaw/foundation/lib/ctx-service.mjs +42 -0
  40. package/node_modules/@capyclaw/foundation/lib/host/caps.mjs +38 -0
  41. package/node_modules/@capyclaw/foundation/lib/host/contract.mjs +88 -0
  42. package/node_modules/@capyclaw/foundation/lib/host/dsh.mjs +54 -0
  43. package/node_modules/@capyclaw/foundation/lib/host/memory.mjs +187 -0
  44. package/node_modules/@capyclaw/foundation/lib/host.mjs +23 -0
  45. package/node_modules/@capyclaw/foundation/lib/kernel.mjs +195 -0
  46. package/node_modules/@capyclaw/foundation/lib/skill/scan.mjs +115 -0
  47. package/node_modules/@capyclaw/foundation/lib/subsystems/events.mjs +60 -0
  48. package/node_modules/@capyclaw/foundation/lib/subsystems/log.mjs +51 -0
  49. package/node_modules/@capyclaw/foundation/lib/subsystems/net.mjs +198 -0
  50. package/node_modules/@capyclaw/foundation/package.json +53 -0
  51. package/node_modules/@capyclaw/foundation/runtime/index.mjs +20 -0
  52. package/node_modules/@capyclaw/plugin-kit/INTEGRATION.md +100 -0
  53. package/node_modules/@capyclaw/plugin-kit/README.md +70 -0
  54. package/node_modules/@capyclaw/plugin-kit/bin/sync.mjs +103 -0
  55. package/node_modules/@capyclaw/plugin-kit/package.json +71 -0
  56. package/node_modules/@capyclaw/plugin-kit/src/cli.mjs +56 -0
  57. package/node_modules/@capyclaw/plugin-kit/src/config.mjs +142 -0
  58. package/node_modules/@capyclaw/plugin-kit/src/editor/heartbeat-worker.js +132 -0
  59. package/node_modules/@capyclaw/plugin-kit/src/editor/heartbeat.mjs +26 -0
  60. package/node_modules/@capyclaw/plugin-kit/src/editor/lease.mjs +60 -0
  61. package/node_modules/@capyclaw/plugin-kit/src/editor/shadow.mjs +219 -0
  62. package/node_modules/@capyclaw/plugin-kit/src/editor/transport.js +13 -0
  63. package/node_modules/@capyclaw/plugin-kit/src/index.mjs +13 -0
  64. package/node_modules/@capyclaw/plugin-kit/src/pack.mjs +230 -0
  65. package/node_modules/@capyclaw/plugin-kit/src/runtime/context.mjs +128 -0
  66. package/node_modules/@capyclaw/plugin-kit/src/self-contained.mjs +307 -0
  67. package/node_modules/@capyclaw/plugin-kit/src/service/api.mjs +45 -0
  68. package/node_modules/@capyclaw/plugin-kit/src/service/http.mjs +113 -0
  69. package/node_modules/@capyclaw/plugin-kit/src/service/mcp-http.mjs +150 -0
  70. package/node_modules/@capyclaw/plugin-kit/src/service/server.mjs +131 -0
  71. package/node_modules/@capyclaw/plugin-kit/src/service/static.mjs +79 -0
  72. package/node_modules/@capyclaw/plugin-kit/src/skills/provider.mjs +82 -0
  73. package/node_modules/@capyclaw/plugin-kit/src/store/blob-store.mjs +109 -0
  74. package/node_modules/@capyclaw/plugin-kit/src/store/content-store.mjs +17 -0
  75. package/node_modules/@capyclaw/plugin-kit/src/store/journal-store.mjs +89 -0
  76. package/node_modules/@capyclaw/plugin-kit/src/tools/result.mjs +57 -0
  77. package/node_modules/@capyclaw/plugin-kit/src/tools/validate.mjs +150 -0
  78. package/node_modules/@capyclaw/plugin-kit/src/tuning.mjs +82 -0
  79. package/node_modules/@capyclaw/plugin-kit/src/util/fs-lock.mjs +132 -0
  80. package/node_modules/@capyclaw/plugin-kit/src/util/http.mjs +130 -0
  81. package/node_modules/@capyclaw/plugin-kit/src/workspace/manager.mjs +664 -0
  82. package/node_modules/@capyclaw/plugin-kit/src/zip.mjs +181 -0
  83. package/node_modules/@capyclaw/portal/README.md +70 -0
  84. package/node_modules/@capyclaw/portal/lib/index.mjs +387 -0
  85. package/node_modules/@capyclaw/portal/lib/ledger.mjs +194 -0
  86. package/node_modules/@capyclaw/portal/lib/portal-agent.ps1 +811 -0
  87. package/node_modules/@capyclaw/portal/lib/portal-darwin.mjs +183 -0
  88. package/node_modules/@capyclaw/portal/package.json +40 -0
  89. package/node_modules/@capyclaw/service-broker/README.md +104 -0
  90. package/node_modules/@capyclaw/service-broker/cordis.patch.yml +8 -0
  91. package/node_modules/@capyclaw/service-broker/dsh-suite.json +21 -0
  92. package/node_modules/@capyclaw/service-broker/lib/control.mjs +209 -0
  93. package/node_modules/@capyclaw/service-broker/lib/discovery.mjs +109 -0
  94. package/node_modules/@capyclaw/service-broker/lib/engine.mjs +1290 -0
  95. package/node_modules/@capyclaw/service-broker/lib/host.mjs +187 -0
  96. package/node_modules/@capyclaw/service-broker/lib/manifest.mjs +262 -0
  97. package/node_modules/@capyclaw/service-broker/lib/recovery.mjs +275 -0
  98. package/node_modules/@capyclaw/service-broker/lib/skills-client.mjs +48 -0
  99. package/node_modules/@capyclaw/service-broker/lib/spec.mjs +78 -0
  100. package/node_modules/@capyclaw/service-broker/package.json +65 -0
  101. package/node_modules/@capyclaw/service-broker/server.mjs +81 -0
  102. package/node_modules/@capyclaw/service-broker/service.schema.json +62 -0
  103. package/node_modules/@capyclaw/service-broker/services.json +3 -0
  104. package/node_modules/@capyclaw/service-host/README.md +86 -0
  105. package/node_modules/@capyclaw/service-host/cordis.patch.yml +4 -0
  106. package/node_modules/@capyclaw/service-host/dsh-suite.json +29 -0
  107. package/node_modules/@capyclaw/service-host/lib/host.mjs +22 -0
  108. package/node_modules/@capyclaw/service-host/package.json +60 -0
  109. package/node_modules/@capyclaw/service-host/src/index.mjs +570 -0
  110. package/node_modules/@capyclaw/service-host/src/tuning.mjs +63 -0
  111. package/node_modules/@capyclaw/tool-host/README.md +56 -0
  112. package/node_modules/@capyclaw/tool-host/cordis.patch.yml +4 -0
  113. package/node_modules/@capyclaw/tool-host/dsh-suite.json +30 -0
  114. package/node_modules/@capyclaw/tool-host/lib/host.mjs +46 -0
  115. package/node_modules/@capyclaw/tool-host/package.json +59 -0
  116. package/node_modules/@capyclaw/tool-host/src/admit-images.mjs +80 -0
  117. package/node_modules/@capyclaw/tool-host/src/caller-session.mjs +25 -0
  118. package/node_modules/@capyclaw/tool-host/src/descriptors.mjs +166 -0
  119. package/node_modules/@capyclaw/tool-host/src/index.mjs +461 -0
  120. package/node_modules/@capyclaw/tool-host/src/mode-gate.mjs +272 -0
  121. package/node_modules/@capyclaw/wall-workbench/README.md +58 -0
  122. package/node_modules/@capyclaw/wall-workbench/contract/wall-contract.json +8 -0
  123. package/node_modules/@capyclaw/wall-workbench/lib/host.mjs +1 -0
  124. package/node_modules/@capyclaw/wall-workbench/package.json +63 -0
  125. package/node_modules/@capyclaw/wall-workbench/plugin.mjs +13 -0
  126. package/node_modules/@capyclaw/wall-workbench/server/activate-log.mjs +66 -0
  127. package/node_modules/@capyclaw/wall-workbench/server/constants.mjs +12 -0
  128. package/node_modules/@capyclaw/wall-workbench/server/contract.mjs +12 -0
  129. package/node_modules/@capyclaw/wall-workbench/server/embedded-host.mjs +162 -0
  130. package/node_modules/@capyclaw/wall-workbench/server/infra/http.mjs +2 -0
  131. package/node_modules/@capyclaw/wall-workbench/server/infra/tuning.mjs +2 -0
  132. package/node_modules/@capyclaw/wall-workbench/server/relay.mjs +68 -0
  133. package/node_modules/@capyclaw/wall-workbench/server/routes/claim.mjs +46 -0
  134. package/node_modules/@capyclaw/wall-workbench/server/routes/workbench.mjs +29 -0
  135. package/node_modules/@capyclaw/wall-workbench/server/shared/SHARED_MANIFEST.json +70 -0
  136. package/node_modules/@capyclaw/wall-workbench/server/shared/asset-service.mjs +2 -0
  137. package/node_modules/@capyclaw/wall-workbench/server/shared/editor-host.mjs +2 -0
  138. package/node_modules/@capyclaw/wall-workbench/server/shared/kit/editor/lease.mjs +2 -0
  139. package/node_modules/@capyclaw/wall-workbench/server/shared/kit/service/api.mjs +2 -0
  140. package/node_modules/@capyclaw/wall-workbench/server/shared/kit/service/http.mjs +2 -0
  141. package/node_modules/@capyclaw/wall-workbench/server/shared/kit/service/mcp-http.mjs +2 -0
  142. package/node_modules/@capyclaw/wall-workbench/server/shared/kit/tools/validate.mjs +2 -0
  143. package/node_modules/@capyclaw/wall-workbench/server/shared/kit/tuning.mjs +2 -0
  144. package/node_modules/@capyclaw/wall-workbench/server/shared/kit/util/fs-lock.mjs +2 -0
  145. package/node_modules/@capyclaw/wall-workbench/server/shared/service-host.mjs +2 -0
  146. package/node_modules/@capyclaw/wall-workbench/server/shared/tool-host.mjs +2 -0
  147. package/node_modules/@capyclaw/wall-workbench/server/shared/tuning.mjs +2 -0
  148. package/node_modules/@capyclaw/wall-workbench/server/tools/capabilities/index.mjs +2 -0
  149. package/node_modules/@capyclaw/wall-workbench/server/tools/index.mjs +12 -0
  150. package/node_modules/@capyclaw/wall-workbench/server/tools.mjs +2 -0
  151. package/node_modules/@capyclaw/wall-workbench/server/workbench-framework.mjs +339 -0
  152. package/node_modules/@capyclaw/wall-workbench/ui/app.js +225 -0
  153. package/node_modules/@capyclaw/wall-workbench/ui/index.html +20 -0
  154. package/node_modules/@capyclaw/wall-workbench/ui/style.css +77 -0
  155. package/package.json +72 -4
  156. package/services.json +3 -0
@@ -0,0 +1,4 @@
1
+ # 由 @capyclaw/tool-host 提供;功能插件通过 inject: ['toolHost'] 或 ctx.get('toolHost') 使用。
2
+ - insert:
3
+ - id: tool-host
4
+ name: "@capyclaw/tool-host"
@@ -0,0 +1,30 @@
1
+ {
2
+ "$schema": "../capyclaw-suite-kit/suite.schema.json",
3
+ "name": "dsh-tool-host-suite",
4
+ "version": "0.3.0",
5
+ "title": "Tool Host",
6
+ "description": "通用工具面:能力 DSL、注册表、校验、契约、MCP 壳、跨套件能力描述符注册/发现。",
7
+ "packages": {
8
+ "@capyclaw/tool-host": {
9
+ "role": "provider",
10
+ "version": "^0.3.2",
11
+ "path": ".",
12
+ "provides": {
13
+ "services": [
14
+ "toolHost"
15
+ ],
16
+ "capabilities": {
17
+ "tools.registry": "1.0.0",
18
+ "tools.descriptors": "1.1.0"
19
+ }
20
+ }
21
+ }
22
+ },
23
+ "rows": [
24
+ {
25
+ "id": "tool-host",
26
+ "name": "@capyclaw/tool-host"
27
+ }
28
+ ],
29
+ "checks": []
30
+ }
@@ -0,0 +1,46 @@
1
+ /**
2
+ * @capyclaw/tool-host 插件:向同进程插件提供 ctx.toolHost。
3
+ *
4
+ * 同时落地“会话模式锁”这一基础设施能力:
5
+ * · 把 work-store 惰性解析器注入 toolHost(模式锁的存储真源);
6
+ * · 给 DSH tools 服务挂全局 guard(异领域调用硬拒兜底);
7
+ * · 注册框架核心工具 mode_lock / mode_status(无领域,恒可见)——显式锁定入口;
8
+ * · 给 DSH systemPrompt 挂动态感知 section(锁定即告知模型主区工作台与交付路径)。
9
+ * 核心工具与 guard 的注册统一走 modeGate.install(tools)(幂等):apply 时 best-effort 挂一次,
10
+ * 若那一刻 tools 服务尚未就绪,首个套件注册(registerWithDsh / bridge host)会再调 install 补上,
11
+ * 故不依赖加载顺序。感知闸同理走 modeGate.installPrompt(systemPrompt)。
12
+ * 所有经 toolHost.registerWithDsh / bridge host 注册的领域能力自动受锁约束,套件零接线。
13
+ */
14
+ import { defineDriver } from '@capyclaw/foundation/abi'
15
+ import { createToolHost } from '../src/index.mjs'
16
+
17
+ const safeGet = (ctx, name) => {
18
+ try { return ctx.reflect?.get?.(name) ?? ctx[name] } catch { return null }
19
+ }
20
+
21
+ export const { name, inject, apply } = defineDriver({
22
+ name: 'tool-host',
23
+ service: 'toolHost',
24
+ apply(ctx, config = {}) {
25
+ const service = createToolHost({ ...config, getWorkStore: () => safeGet(ctx, 'workStore') })
26
+ const dispose = ctx.provide('toolHost', service)
27
+
28
+ /* 模式锁兜底闸 + 核心锁定工具(幂等)。tools 缺席则降级:稍后套件注册时经 install 补挂。
29
+ * install 返回引用计数 disposer,必须随 driver 卸载释放,否则核心工具/guard 永驻。 */
30
+ const tools = safeGet(ctx, 'tools')
31
+ const gateDispose = (tools && typeof tools.register === 'function') ? service.modeGate.install(tools) : null
32
+
33
+ /* 感知闸(幂等):锁定会话的系统提示里注入主区工作台感知。systemPrompt 缺席则降级,
34
+ * 稍后套件注册时经 installPrompt 补挂。disposer 同样必须随 driver 卸载释放。 */
35
+ const systemPrompt = safeGet(ctx, 'systemPrompt')
36
+ const promptDispose = (systemPrompt && typeof systemPrompt.section === 'function')
37
+ ? service.modeGate.installPrompt(systemPrompt) : null
38
+
39
+ console.log('[tool-host] ready (mode-gate enabled)')
40
+ return () => {
41
+ if (typeof promptDispose === 'function') promptDispose()
42
+ if (typeof gateDispose === 'function') gateDispose()
43
+ if (typeof dispose === 'function') dispose()
44
+ }
45
+ },
46
+ })
@@ -0,0 +1,59 @@
1
+ {
2
+ "name": "@capyclaw/tool-host",
3
+ "version": "0.3.2",
4
+ "description": "DSH 公共工具面插件:能力 DSL、工具注册表、参数校验、契约报告、MCP 协议壳与 instructions 模板,向插件提供 ctx.toolHost。",
5
+ "type": "module",
6
+ "main": "./lib/host.mjs",
7
+ "exports": {
8
+ ".": "./lib/host.mjs",
9
+ "./src": "./src/index.mjs",
10
+ "./package.json": "./package.json"
11
+ },
12
+ "engines": {
13
+ "node": ">=18",
14
+ "dsh": "^0.2.0-rc.2"
15
+ },
16
+ "publishConfig": {
17
+ "access": "public"
18
+ },
19
+ "dsh": {
20
+ "bundle": {
21
+ "patch": "./cordis.patch.yml"
22
+ },
23
+ "suite": "./dsh-suite.json",
24
+ "apiVersion": "1.1.0",
25
+ "category": "service",
26
+ "platform": "both",
27
+ "driver": {
28
+ "service": "toolHost"
29
+ }
30
+ },
31
+ "meta": {
32
+ "title": "公共服务工具面",
33
+ "description": "能力 DSL、工具注册表、参数校验、契约报告与 MCP 协议壳,向插件提供 ctx.toolHost"
34
+ },
35
+ "files": [
36
+ "lib/",
37
+ "src/",
38
+ "cordis.patch.yml",
39
+ "dsh-suite.json",
40
+ "README.md"
41
+ ],
42
+ "license": "MIT",
43
+ "scripts": {
44
+ "test": "node --test --test-isolation=none test/*.test.mjs"
45
+ },
46
+ "dependencies": {
47
+ "@capyclaw/plugin-kit": "^0.2.0",
48
+ "@capyclaw/foundation": "^0.1.1"
49
+ },
50
+ "repository": {
51
+ "type": "git",
52
+ "url": "git+https://github.com/Maple496/capyclaw.git",
53
+ "directory": "C/packages/capyclaw-tool-host"
54
+ },
55
+ "bugs": {
56
+ "url": "https://github.com/Maple496/capyclaw/issues"
57
+ },
58
+ "homepage": "https://github.com/Maple496/capyclaw/tree/main/C/packages/capyclaw-tool-host#readme"
59
+ }
@@ -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
+ }
@@ -0,0 +1,166 @@
1
+ /**
2
+ * 能力描述符注册表(互联标准 v0.2 §A.4 / §E「broker 注册发现」的公共落点)。
3
+ *
4
+ * 定位:tool-host 已是公共工具面(defineCapability/createRegistry),这里再补一层
5
+ * 「跨套件能力发现」——每个套件把自己的能力描述符 + 调用地址登记进来,编排器
6
+ * (conductor 这类平级作品)按 produces/consumes/controlMode 动态发现,不硬 import 任何套件。
7
+ *
8
+ * 解耦口径(标准 §1):
9
+ * - 注册表只认「描述符 + 地址」,不认套件实现;套件之间零互依赖。
10
+ * - 描述符的深度校验交给注入的 validate(通常是 @capyclaw/suite-kit/capability 的
11
+ * validateCapabilityDescriptor);本模块只做最小结构兜底,保持零依赖、可单测。
12
+ * - baseUrl 是套件的调用入口(service-host 的 app.url,embedded 模式也有值),
13
+ * 编排器据此走 HTTP MCP tools/call,而不是 ctx.tools.execute(后者需要 Agent scope,太重)。
14
+ */
15
+
16
+ const CONTROL_MODES = new Set(['interface', 'browser'])
17
+ const KINDS = new Set(['read', 'write', 'control'])
18
+
19
+ /** 结尾 `/*` 前缀通配:`artifact:image/*` 命中 `artifact:image/png`。 */
20
+ function typeMatches(pattern, type) {
21
+ if (!pattern || !type) return false
22
+ if (pattern === type) return true
23
+ if (pattern.endsWith('/*')) return type.startsWith(pattern.slice(0, -1))
24
+ return false
25
+ }
26
+
27
+ function anyTypeMatches(patterns, type) {
28
+ return (patterns || []).some((p) => typeMatches(p, type))
29
+ }
30
+
31
+ /** 双向通配匹配:声明侧或查询侧任一带 `/*` 通配都要能命中。
32
+ * 旧实现只把声明当 pattern、查询当 type,查询侧通配(conductor 问「谁产出
33
+ * artifact:image/*」)永远不命中具体类型。 */
34
+ function typesOverlap(declPatterns, queryType) {
35
+ return (declPatterns || []).some((p) => typeMatches(p, queryType) || typeMatches(queryType, p))
36
+ }
37
+
38
+ /** 深冻结:描述符的 produces/consumes/surface/provides 等嵌套结构也要不可变,
39
+ * 否则注册后仍能被外部改写,注册表的「单一真源」失真。 */
40
+ function deepFreeze(value) {
41
+ if (!value || typeof value !== 'object' || Object.isFrozen(value)) return value
42
+ Object.freeze(value)
43
+ for (const key of Object.keys(value)) deepFreeze(value[key])
44
+ return value
45
+ }
46
+
47
+ /** 最小结构兜底:name/suite/controlMode/kind 必须在场且合法。深度校验走注入的 validate。 */
48
+ function structuralCheck(descriptor) {
49
+ const errors = []
50
+ if (!descriptor || typeof descriptor !== 'object') return ['descriptor 必须是对象']
51
+ if (!descriptor.name || typeof descriptor.name !== 'string') errors.push('缺少 name')
52
+ if (!descriptor.suite || typeof descriptor.suite !== 'string') errors.push('缺少 suite')
53
+ if (!descriptor.capability || typeof descriptor.capability !== 'string') errors.push('缺少 capability(能力语义)')
54
+ if (!CONTROL_MODES.has(descriptor.controlMode)) errors.push(`controlMode 必须是 interface|browser,收到 ${descriptor.controlMode}`)
55
+ if (!KINDS.has(descriptor.kind)) errors.push(`kind 必须是 read|write|control,收到 ${descriptor.kind}`)
56
+ return errors
57
+ }
58
+
59
+ /**
60
+ * @param {{ validate?: (d:object)=>{ok:boolean,errors?:string[]} }} [options]
61
+ * validate 可选注入(suite-kit 的深度校验器)。缺省只做结构兜底。
62
+ */
63
+ export function createDescriptorRegistry({ validate } = {}) {
64
+ /** @type {Map<string, {descriptor:object, suite:string, baseUrl:string, registeredAt:number}>} */
65
+ const entries = new Map()
66
+
67
+ function register(descriptor, { suite, baseUrl = '', force = false } = {}) {
68
+ const merged = { ...descriptor, suite: descriptor?.suite || suite }
69
+ const errors = structuralCheck(merged)
70
+ if (errors.length) {
71
+ const err = new Error(`能力描述符不合法: ${errors.join('; ')}`)
72
+ err.code = 'DESCRIPTOR_INVALID'
73
+ throw err
74
+ }
75
+ if (typeof validate === 'function') {
76
+ const verdict = validate(merged)
77
+ if (verdict && verdict.ok === false) {
78
+ const err = new Error(`能力描述符不合法: ${(verdict.errors || []).join('; ')}`)
79
+ err.code = 'DESCRIPTOR_INVALID'
80
+ throw err
81
+ }
82
+ }
83
+ /* owner 校验:同名描述符已属另一套件时拒绝静默覆盖(防跨套件抢占);
84
+ * 同套件热重载刷新放行,确需跨套件替换须显式 force。 */
85
+ const existing = entries.get(merged.name)
86
+ if (existing && existing.suite !== merged.suite && !force) {
87
+ const err = new Error(`能力描述符 ${merged.name} 已属套件 ${existing.suite},套件 ${merged.suite} 不能覆盖(如确需替换请传 force)`)
88
+ err.code = 'DESCRIPTOR_OWNER_CONFLICT'
89
+ throw err
90
+ }
91
+ const entry = Object.freeze({
92
+ descriptor: deepFreeze({ ...merged }),
93
+ suite: merged.suite,
94
+ baseUrl: String(baseUrl || ''),
95
+ registeredAt: Date.now(),
96
+ })
97
+ // 同名幂等覆盖:套件热重载/重新激活时直接刷新,不产生重复。
98
+ entries.set(merged.name, entry)
99
+ return entry
100
+ }
101
+
102
+ function unregister(name) {
103
+ return entries.delete(name)
104
+ }
105
+
106
+ /**
107
+ * 按条件发现。produces/consumes 用类型词表匹配(含 `/*` 通配)。
108
+ * @param {{ name?:string, suite?:string, controlMode?:string, kind?:string,
109
+ * produces?:string, consumes?:string }} [query]
110
+ */
111
+ function discover(query = {}) {
112
+ const out = []
113
+ for (const entry of entries.values()) {
114
+ const d = entry.descriptor
115
+ if (query.name && d.name !== query.name) continue
116
+ if (query.suite && entry.suite !== query.suite) continue
117
+ if (query.controlMode && d.controlMode !== query.controlMode) continue
118
+ if (query.kind && d.kind !== query.kind) continue
119
+ if (query.produces && !typesOverlap(d.produces, query.produces)) continue
120
+ if (query.consumes && !typesOverlap(d.consumes, query.consumes)) continue
121
+ out.push(entry)
122
+ }
123
+ return out
124
+ }
125
+
126
+ function catalog() {
127
+ return [...entries.values()]
128
+ }
129
+
130
+ /**
131
+ * 表面投影(互联标准 v0.3 §C.7 / 单一真源):把描述符里的 surface 声明聚合成
132
+ * 「工作台候选」,一扇挂载路径一行。portal-hud/展示墙与编排器读的是**同一个**注册表,
133
+ * 因此「注册了一个带 surface 的能力」必然自动上墙,不会出现能力加了表上没有。
134
+ *
135
+ * 只认 surface.embedPath(以 / 开头);同路径去重(一个套件多个工具共享一扇窗)。
136
+ * 返回行形状与 portal-hud 注册行兼容:{id,title,path,suite,lastAt:''}。
137
+ */
138
+ function listSurfaces() {
139
+ const byPath = new Map()
140
+ for (const entry of entries.values()) {
141
+ const surface = entry.descriptor && entry.descriptor.surface
142
+ const embedPath = surface && typeof surface.embedPath === 'string' ? surface.embedPath : ''
143
+ if (!embedPath || !embedPath.startsWith('/')) continue
144
+ if (surface.embeddable === false && surface.standalone === false) continue
145
+ if (byPath.has(embedPath)) continue
146
+ const suite = entry.suite
147
+ byPath.set(embedPath, {
148
+ id: `${suite}-workbench`,
149
+ title: String(surface.title || '') || `${suite} 工作台`,
150
+ path: embedPath.endsWith('/') ? embedPath : `${embedPath}/`,
151
+ suite,
152
+ lastAt: '',
153
+ })
154
+ }
155
+ return [...byPath.values()]
156
+ }
157
+
158
+ return {
159
+ register,
160
+ unregister,
161
+ discover,
162
+ catalog,
163
+ listSurfaces,
164
+ get size() { return entries.size },
165
+ }
166
+ }