@capyclaw/capyclaw-workbench 0.0.0-stage → 0.1.0

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 (109) hide show
  1. package/README.md +28 -2
  2. package/contract/capabilities.json +178 -0
  3. package/lib/descriptors.mjs +33 -0
  4. package/lib/host.mjs +102 -0
  5. package/lib/session-port.mjs +104 -0
  6. package/lib/tools.mjs +166 -0
  7. package/lib/workbench.mjs +133 -0
  8. package/node_modules/@capyclaw/foundation/README.md +51 -0
  9. package/node_modules/@capyclaw/foundation/abi/bridge.mjs +86 -0
  10. package/node_modules/@capyclaw/foundation/abi/errors.mjs +63 -0
  11. package/node_modules/@capyclaw/foundation/abi/index.mjs +35 -0
  12. package/node_modules/@capyclaw/foundation/abi/kinds.mjs +271 -0
  13. package/node_modules/@capyclaw/foundation/abi/loader.mjs +345 -0
  14. package/node_modules/@capyclaw/foundation/abi/schema.mjs +98 -0
  15. package/node_modules/@capyclaw/foundation/abi/spec.mjs +255 -0
  16. package/node_modules/@capyclaw/foundation/abi/validate.mjs +24 -0
  17. package/node_modules/@capyclaw/foundation/abi/version.mjs +82 -0
  18. package/node_modules/@capyclaw/foundation/cordis.patch.yml +5 -0
  19. package/node_modules/@capyclaw/foundation/kit/index.mjs +6 -0
  20. package/node_modules/@capyclaw/foundation/lib/adapters/client-panel.mjs +98 -0
  21. package/node_modules/@capyclaw/foundation/lib/adapters/host-event.mjs +35 -0
  22. package/node_modules/@capyclaw/foundation/lib/adapters/http-router.mjs +69 -0
  23. package/node_modules/@capyclaw/foundation/lib/adapters/lifecycle-hook.mjs +55 -0
  24. package/node_modules/@capyclaw/foundation/lib/adapters/own-server.mjs +113 -0
  25. package/node_modules/@capyclaw/foundation/lib/adapters/raw.mjs +68 -0
  26. package/node_modules/@capyclaw/foundation/lib/adapters/runtime-driver.mjs +113 -0
  27. package/node_modules/@capyclaw/foundation/lib/adapters/service-provider.mjs +77 -0
  28. package/node_modules/@capyclaw/foundation/lib/adapters/skill-provider.mjs +56 -0
  29. package/node_modules/@capyclaw/foundation/lib/adapters/tool-provider.mjs +50 -0
  30. package/node_modules/@capyclaw/foundation/lib/adapters/ui-panel.mjs +64 -0
  31. package/node_modules/@capyclaw/foundation/lib/ctx-service.mjs +42 -0
  32. package/node_modules/@capyclaw/foundation/lib/host/caps.mjs +34 -0
  33. package/node_modules/@capyclaw/foundation/lib/host/contract.mjs +85 -0
  34. package/node_modules/@capyclaw/foundation/lib/host/dsh.mjs +50 -0
  35. package/node_modules/@capyclaw/foundation/lib/host/memory.mjs +160 -0
  36. package/node_modules/@capyclaw/foundation/lib/host.mjs +23 -0
  37. package/node_modules/@capyclaw/foundation/lib/kernel.mjs +185 -0
  38. package/node_modules/@capyclaw/foundation/lib/skill/scan.mjs +87 -0
  39. package/node_modules/@capyclaw/foundation/lib/subsystems/events.mjs +60 -0
  40. package/node_modules/@capyclaw/foundation/lib/subsystems/log.mjs +51 -0
  41. package/node_modules/@capyclaw/foundation/lib/subsystems/net.mjs +188 -0
  42. package/node_modules/@capyclaw/foundation/package.json +53 -0
  43. package/node_modules/@capyclaw/foundation/runtime/index.mjs +20 -0
  44. package/node_modules/@capyclaw/plugin-kit/INTEGRATION.md +100 -0
  45. package/node_modules/@capyclaw/plugin-kit/README.md +70 -0
  46. package/node_modules/@capyclaw/plugin-kit/bin/sync.mjs +103 -0
  47. package/node_modules/@capyclaw/plugin-kit/package.json +69 -0
  48. package/node_modules/@capyclaw/plugin-kit/src/cli.mjs +56 -0
  49. package/node_modules/@capyclaw/plugin-kit/src/config.mjs +142 -0
  50. package/node_modules/@capyclaw/plugin-kit/src/editor/heartbeat-worker.js +129 -0
  51. package/node_modules/@capyclaw/plugin-kit/src/editor/lease.mjs +139 -0
  52. package/node_modules/@capyclaw/plugin-kit/src/editor/shadow.mjs +219 -0
  53. package/node_modules/@capyclaw/plugin-kit/src/editor/transport.js +13 -0
  54. package/node_modules/@capyclaw/plugin-kit/src/index.mjs +13 -0
  55. package/node_modules/@capyclaw/plugin-kit/src/pack.mjs +230 -0
  56. package/node_modules/@capyclaw/plugin-kit/src/runtime/context.mjs +128 -0
  57. package/node_modules/@capyclaw/plugin-kit/src/self-contained.mjs +307 -0
  58. package/node_modules/@capyclaw/plugin-kit/src/service/api.mjs +45 -0
  59. package/node_modules/@capyclaw/plugin-kit/src/service/http.mjs +113 -0
  60. package/node_modules/@capyclaw/plugin-kit/src/service/mcp-http.mjs +143 -0
  61. package/node_modules/@capyclaw/plugin-kit/src/service/server.mjs +131 -0
  62. package/node_modules/@capyclaw/plugin-kit/src/skills/provider.mjs +82 -0
  63. package/node_modules/@capyclaw/plugin-kit/src/store/blob-store.mjs +109 -0
  64. package/node_modules/@capyclaw/plugin-kit/src/store/content-store.mjs +17 -0
  65. package/node_modules/@capyclaw/plugin-kit/src/store/journal-store.mjs +89 -0
  66. package/node_modules/@capyclaw/plugin-kit/src/tools/result.mjs +41 -0
  67. package/node_modules/@capyclaw/plugin-kit/src/tools/validate.mjs +150 -0
  68. package/node_modules/@capyclaw/plugin-kit/src/tuning.mjs +82 -0
  69. package/node_modules/@capyclaw/plugin-kit/src/util/fs-lock.mjs +132 -0
  70. package/node_modules/@capyclaw/plugin-kit/src/util/http.mjs +130 -0
  71. package/node_modules/@capyclaw/plugin-kit/src/workspace/manager.mjs +656 -0
  72. package/node_modules/@capyclaw/plugin-kit/src/zip.mjs +181 -0
  73. package/node_modules/@capyclaw/service-host/README.md +86 -0
  74. package/node_modules/@capyclaw/service-host/cordis.patch.yml +4 -0
  75. package/node_modules/@capyclaw/service-host/dsh-suite.json +29 -0
  76. package/node_modules/@capyclaw/service-host/lib/host.mjs +22 -0
  77. package/node_modules/@capyclaw/service-host/package.json +60 -0
  78. package/node_modules/@capyclaw/service-host/src/index.mjs +571 -0
  79. package/node_modules/@capyclaw/service-host/src/tuning.mjs +63 -0
  80. package/node_modules/@capyclaw/suite-kit/README.md +238 -0
  81. package/node_modules/@capyclaw/suite-kit/bin/dsh-domain.mjs +98 -0
  82. package/node_modules/@capyclaw/suite-kit/bin/dsh-suite.mjs +174 -0
  83. package/node_modules/@capyclaw/suite-kit/client/embed.js +35 -0
  84. package/node_modules/@capyclaw/suite-kit/domain.schema.json +129 -0
  85. package/node_modules/@capyclaw/suite-kit/package.json +54 -0
  86. package/node_modules/@capyclaw/suite-kit/src/activation-registry.mjs +184 -0
  87. package/node_modules/@capyclaw/suite-kit/src/artifacts.mjs +131 -0
  88. package/node_modules/@capyclaw/suite-kit/src/capability.mjs +181 -0
  89. package/node_modules/@capyclaw/suite-kit/src/compose.mjs +201 -0
  90. package/node_modules/@capyclaw/suite-kit/src/domain.mjs +138 -0
  91. package/node_modules/@capyclaw/suite-kit/src/embed.mjs +81 -0
  92. package/node_modules/@capyclaw/suite-kit/src/index.mjs +10 -0
  93. package/node_modules/@capyclaw/suite-kit/src/manifest.mjs +263 -0
  94. package/node_modules/@capyclaw/suite-kit/src/orchestration.mjs +100 -0
  95. package/node_modules/@capyclaw/suite-kit/src/profile.mjs +584 -0
  96. package/node_modules/@capyclaw/suite-kit/src/surface.mjs +28 -0
  97. package/node_modules/@capyclaw/suite-kit/suite.schema.json +112 -0
  98. package/node_modules/@capyclaw/tool-host/README.md +56 -0
  99. package/node_modules/@capyclaw/tool-host/cordis.patch.yml +4 -0
  100. package/node_modules/@capyclaw/tool-host/dsh-suite.json +30 -0
  101. package/node_modules/@capyclaw/tool-host/lib/host.mjs +16 -0
  102. package/node_modules/@capyclaw/tool-host/package.json +59 -0
  103. package/node_modules/@capyclaw/tool-host/src/descriptors.mjs +142 -0
  104. package/node_modules/@capyclaw/tool-host/src/index.mjs +390 -0
  105. package/package.json +68 -4
  106. package/plugin.mjs +1 -0
  107. package/ui/app.js +114 -0
  108. package/ui/index.html +64 -0
  109. package/ui/style.css +54 -0
@@ -0,0 +1,112 @@
1
+ {
2
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
3
+ "$id": "https://dsh.local/schemas/suite.schema.json",
4
+ "title": "DSH Suite Manifest",
5
+ "type": "object",
6
+ "additionalProperties": false,
7
+ "required": ["name", "version", "packages", "rows"],
8
+ "properties": {
9
+ "$schema": { "type": "string" },
10
+ "name": { "type": "string", "pattern": "^[a-z0-9]+(?:-[a-z0-9]+)*$" },
11
+ "version": { "type": "string", "pattern": "^\\d+\\.\\d+\\.\\d+(?:-[0-9A-Za-z.-]+)?$" },
12
+ "title": { "type": "string" },
13
+ "description": { "type": "string" },
14
+ "components": {
15
+ "type": "array",
16
+ "items": { "type": "string", "minLength": 1 },
17
+ "description": "引用其他 suite 目录(相对本 suite 目录);加载时展开其 packages/rows/checks。"
18
+ },
19
+ "packages": {
20
+ "type": "object",
21
+ "minProperties": 1,
22
+ "additionalProperties": {
23
+ "type": "object",
24
+ "additionalProperties": false,
25
+ "required": ["role", "version"],
26
+ "properties": {
27
+ "role": {
28
+ "type": "string",
29
+ "enum": ["runtime", "skills", "provider", "ui", "bridge", "support"]
30
+ },
31
+ "version": { "type": "string", "minLength": 1 },
32
+ "path": { "type": "string", "minLength": 1 },
33
+ "required": { "type": "boolean" },
34
+ "requires": {
35
+ "type": "object",
36
+ "properties": {
37
+ "capabilities": {
38
+ "type": "object",
39
+ "additionalProperties": { "type": "string", "minLength": 1 }
40
+ }
41
+ },
42
+ "additionalProperties": { "type": "string", "minLength": 1 }
43
+ },
44
+ "provides": {
45
+ "type": "object",
46
+ "additionalProperties": false,
47
+ "properties": {
48
+ "tools": { "type": "array", "items": { "type": "string" } },
49
+ "skills": { "type": "array", "items": { "type": "string" } },
50
+ "services": { "type": "array", "items": { "type": "string" } },
51
+ "ports": { "type": "array", "items": { "type": "integer", "minimum": 1, "maximum": 65535 } },
52
+ "capabilities": {
53
+ "type": "object",
54
+ "additionalProperties": { "type": "string", "minLength": 1 }
55
+ }
56
+ }
57
+ }
58
+ }
59
+ }
60
+ },
61
+ "rows": {
62
+ "type": "array",
63
+ "minItems": 1,
64
+ "items": {
65
+ "type": "object",
66
+ "additionalProperties": false,
67
+ "required": ["id", "name"],
68
+ "properties": {
69
+ "id": { "type": "string", "pattern": "^[a-z0-9]+(?:-[a-z0-9]+)*$" },
70
+ "name": { "type": "string", "minLength": 1 },
71
+ "disabled": { "type": "boolean" },
72
+ "config": { "type": "object" }
73
+ }
74
+ }
75
+ },
76
+ "checks": {
77
+ "type": "array",
78
+ "items": {
79
+ "oneOf": [
80
+ {
81
+ "type": "object",
82
+ "additionalProperties": false,
83
+ "required": ["id", "type", "url"],
84
+ "properties": {
85
+ "id": { "type": "string" },
86
+ "type": { "const": "http" },
87
+ "url": { "type": "string", "format": "uri" },
88
+ "method": { "type": "string" },
89
+ "expectStatus": { "type": "integer" },
90
+ "expectBody": { "type": "object" },
91
+ "timeoutMs": { "type": "integer", "minimum": 100 }
92
+ }
93
+ },
94
+ {
95
+ "type": "object",
96
+ "additionalProperties": false,
97
+ "required": ["id", "type", "url", "tool", "major"],
98
+ "properties": {
99
+ "id": { "type": "string" },
100
+ "type": { "const": "contract" },
101
+ "url": { "type": "string", "format": "uri" },
102
+ "tool": { "type": "string" },
103
+ "major": { "type": "integer", "minimum": 0 },
104
+ "timeoutMs": { "type": "integer", "minimum": 100 }
105
+ }
106
+ }
107
+ ]
108
+ }
109
+ },
110
+ "tags": { "type": "array", "items": { "type": "string" } }
111
+ }
112
+ }
@@ -0,0 +1,56 @@
1
+ # @capyclaw/tool-host
2
+
3
+ DSH 公共工具面插件。把每个插件都会重复写的工具声明、注册表、参数校验、契约报告、MCP 协议壳和 instructions 模板收成一套。
4
+
5
+ ## 使用
6
+
7
+ ```js
8
+ export const inject = ['toolHost']
9
+
10
+ const echo = ctx.toolHost.defineTool({
11
+ name: 'demo_echo',
12
+ description: '回显文本',
13
+ inputSchema: { type: 'object', properties: { text: { type: 'string' } }, required: ['text'] },
14
+ handler: ({ text }) => ({ content: [{ type: 'text', text }] }),
15
+ })
16
+
17
+ const capabilities = [
18
+ ctx.toolHost.defineCapability({ id: 'demo', title: 'Demo', tools: [echo] }),
19
+ ]
20
+
21
+ const registry = ctx.toolHost.createRegistry(capabilities)
22
+ const mcp = ctx.toolHost.createMcpHandler({
23
+ registry,
24
+ instructions: ctx.toolHost.buildInstructions({ title: 'Demo', capabilities: registry.CAPABILITIES }),
25
+ })
26
+ ```
27
+
28
+ ## 原生 DSH 工具面
29
+
30
+ ```js
31
+ export const inject = ['toolHost']
32
+
33
+ export function apply(ctx) {
34
+ const registry = ctx.toolHost.createRegistry(capabilities)
35
+ const dispose = ctx.toolHost.registerWithDsh(ctx, registry, { namespace: 'demo' })
36
+ return dispose
37
+ }
38
+ ```
39
+
40
+ 注册后工具直接出现在 `ctx.tools`,不需要 loopback `/mcp` 和 `dsh-mcp-client` row。
41
+ MCP handler 仍保留给跨进程/外部客户端。
42
+
43
+ ## 提供什么
44
+
45
+ - `defineTool` / `defineCapability`:声明式能力 DSL
46
+ - `createRegistry`:校验 id/工具名/依赖/服务唯一性,派生 `TOOLS / TOOL_IMPL / TOOL_META / FEATURES / CAPABILITIES`
47
+ - `createToolDispatcher`:统一参数校验后分派
48
+ - `createMcpHandler`:MCP Streamable HTTP(initialize / ping / tools/list / tools/call)
49
+ - `createCapabilityReport`:契约报告
50
+ - `buildInstructions`:MCP server instructions 模板
51
+
52
+ ## 边界
53
+
54
+ - 不绑定具体插件;PPT 只是第一个消费者
55
+ - 不负责 HTTP 监听;和 `@capyclaw/service-host` 组合使用
56
+ - 不负责契约快照文件;由构建脚本调用 `registry.ctx.contract.report()` 生成
@@ -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.2.0",
5
+ "title": "Tool Host",
6
+ "description": "通用工具面:能力 DSL、注册表、校验、契约、MCP 壳、跨套件能力描述符注册/发现。",
7
+ "packages": {
8
+ "@capyclaw/tool-host": {
9
+ "role": "provider",
10
+ "version": "^0.2.0",
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,16 @@
1
+ /**
2
+ * @capyclaw/tool-host 插件:向同进程插件提供 ctx.toolHost。
3
+ */
4
+ import { defineDriver } from '@capyclaw/foundation/abi'
5
+ import { createToolHost } from '../src/index.mjs'
6
+
7
+ export const { name, inject, apply } = defineDriver({
8
+ name: 'tool-host',
9
+ service: 'toolHost',
10
+ apply(ctx, config = {}) {
11
+ const service = createToolHost(config)
12
+ const dispose = ctx.provide('toolHost', service)
13
+ console.log('[tool-host] ready')
14
+ return () => { if (typeof dispose === 'function') dispose() }
15
+ },
16
+ })
@@ -0,0 +1,59 @@
1
+ {
2
+ "name": "@capyclaw/tool-host",
3
+ "version": "0.2.0",
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,142 @@
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
+ /** 最小结构兜底:name/suite/controlMode/kind 必须在场且合法。深度校验走注入的 validate。 */
32
+ function structuralCheck(descriptor) {
33
+ const errors = []
34
+ if (!descriptor || typeof descriptor !== 'object') return ['descriptor 必须是对象']
35
+ if (!descriptor.name || typeof descriptor.name !== 'string') errors.push('缺少 name')
36
+ if (!descriptor.suite || typeof descriptor.suite !== 'string') errors.push('缺少 suite')
37
+ if (!descriptor.capability || typeof descriptor.capability !== 'string') errors.push('缺少 capability(能力语义)')
38
+ if (!CONTROL_MODES.has(descriptor.controlMode)) errors.push(`controlMode 必须是 interface|browser,收到 ${descriptor.controlMode}`)
39
+ if (!KINDS.has(descriptor.kind)) errors.push(`kind 必须是 read|write|control,收到 ${descriptor.kind}`)
40
+ return errors
41
+ }
42
+
43
+ /**
44
+ * @param {{ validate?: (d:object)=>{ok:boolean,errors?:string[]} }} [options]
45
+ * validate 可选注入(suite-kit 的深度校验器)。缺省只做结构兜底。
46
+ */
47
+ export function createDescriptorRegistry({ validate } = {}) {
48
+ /** @type {Map<string, {descriptor:object, suite:string, baseUrl:string, registeredAt:number}>} */
49
+ const entries = new Map()
50
+
51
+ function register(descriptor, { suite, baseUrl = '' } = {}) {
52
+ const merged = { ...descriptor, suite: descriptor?.suite || suite }
53
+ const errors = structuralCheck(merged)
54
+ if (errors.length) {
55
+ const err = new Error(`能力描述符不合法: ${errors.join('; ')}`)
56
+ err.code = 'DESCRIPTOR_INVALID'
57
+ throw err
58
+ }
59
+ if (typeof validate === 'function') {
60
+ const verdict = validate(merged)
61
+ if (verdict && verdict.ok === false) {
62
+ const err = new Error(`能力描述符不合法: ${(verdict.errors || []).join('; ')}`)
63
+ err.code = 'DESCRIPTOR_INVALID'
64
+ throw err
65
+ }
66
+ }
67
+ const entry = Object.freeze({
68
+ descriptor: Object.freeze({ ...merged }),
69
+ suite: merged.suite,
70
+ baseUrl: String(baseUrl || ''),
71
+ registeredAt: Date.now(),
72
+ })
73
+ // 同名幂等覆盖:套件热重载/重新激活时直接刷新,不产生重复。
74
+ entries.set(merged.name, entry)
75
+ return entry
76
+ }
77
+
78
+ function unregister(name) {
79
+ return entries.delete(name)
80
+ }
81
+
82
+ /**
83
+ * 按条件发现。produces/consumes 用类型词表匹配(含 `/*` 通配)。
84
+ * @param {{ name?:string, suite?:string, controlMode?:string, kind?:string,
85
+ * produces?:string, consumes?:string }} [query]
86
+ */
87
+ function discover(query = {}) {
88
+ const out = []
89
+ for (const entry of entries.values()) {
90
+ const d = entry.descriptor
91
+ if (query.name && d.name !== query.name) continue
92
+ if (query.suite && entry.suite !== query.suite) continue
93
+ if (query.controlMode && d.controlMode !== query.controlMode) continue
94
+ if (query.kind && d.kind !== query.kind) continue
95
+ if (query.produces && !anyTypeMatches(d.produces, query.produces)) continue
96
+ if (query.consumes && !anyTypeMatches(d.consumes, query.consumes)) continue
97
+ out.push(entry)
98
+ }
99
+ return out
100
+ }
101
+
102
+ function catalog() {
103
+ return [...entries.values()]
104
+ }
105
+
106
+ /**
107
+ * 表面投影(互联标准 v0.3 §C.7 / 单一真源):把描述符里的 surface 声明聚合成
108
+ * 「工作台候选」,一扇挂载路径一行。portal-hud/展示墙与编排器读的是**同一个**注册表,
109
+ * 因此「注册了一个带 surface 的能力」必然自动上墙,不会出现能力加了表上没有。
110
+ *
111
+ * 只认 surface.embedPath(以 / 开头);同路径去重(一个套件多个工具共享一扇窗)。
112
+ * 返回行形状与 portal-hud 注册行兼容:{id,title,path,suite,lastAt:''}。
113
+ */
114
+ function listSurfaces() {
115
+ const byPath = new Map()
116
+ for (const entry of entries.values()) {
117
+ const surface = entry.descriptor && entry.descriptor.surface
118
+ const embedPath = surface && typeof surface.embedPath === 'string' ? surface.embedPath : ''
119
+ if (!embedPath || !embedPath.startsWith('/')) continue
120
+ if (surface.embeddable === false && surface.standalone === false) continue
121
+ if (byPath.has(embedPath)) continue
122
+ const suite = entry.suite
123
+ byPath.set(embedPath, {
124
+ id: `${suite}-workbench`,
125
+ title: String(surface.title || '') || `${suite} 工作台`,
126
+ path: embedPath.endsWith('/') ? embedPath : `${embedPath}/`,
127
+ suite,
128
+ lastAt: '',
129
+ })
130
+ }
131
+ return [...byPath.values()]
132
+ }
133
+
134
+ return {
135
+ register,
136
+ unregister,
137
+ discover,
138
+ catalog,
139
+ listSurfaces,
140
+ get size() { return entries.size },
141
+ }
142
+ }