@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.
- package/README.md +28 -2
- package/contract/capabilities.json +178 -0
- package/lib/descriptors.mjs +33 -0
- package/lib/host.mjs +102 -0
- package/lib/session-port.mjs +104 -0
- package/lib/tools.mjs +166 -0
- package/lib/workbench.mjs +133 -0
- package/node_modules/@capyclaw/foundation/README.md +51 -0
- package/node_modules/@capyclaw/foundation/abi/bridge.mjs +86 -0
- package/node_modules/@capyclaw/foundation/abi/errors.mjs +63 -0
- package/node_modules/@capyclaw/foundation/abi/index.mjs +35 -0
- package/node_modules/@capyclaw/foundation/abi/kinds.mjs +271 -0
- package/node_modules/@capyclaw/foundation/abi/loader.mjs +345 -0
- package/node_modules/@capyclaw/foundation/abi/schema.mjs +98 -0
- package/node_modules/@capyclaw/foundation/abi/spec.mjs +255 -0
- package/node_modules/@capyclaw/foundation/abi/validate.mjs +24 -0
- package/node_modules/@capyclaw/foundation/abi/version.mjs +82 -0
- package/node_modules/@capyclaw/foundation/cordis.patch.yml +5 -0
- package/node_modules/@capyclaw/foundation/kit/index.mjs +6 -0
- package/node_modules/@capyclaw/foundation/lib/adapters/client-panel.mjs +98 -0
- package/node_modules/@capyclaw/foundation/lib/adapters/host-event.mjs +35 -0
- package/node_modules/@capyclaw/foundation/lib/adapters/http-router.mjs +69 -0
- package/node_modules/@capyclaw/foundation/lib/adapters/lifecycle-hook.mjs +55 -0
- package/node_modules/@capyclaw/foundation/lib/adapters/own-server.mjs +113 -0
- package/node_modules/@capyclaw/foundation/lib/adapters/raw.mjs +68 -0
- package/node_modules/@capyclaw/foundation/lib/adapters/runtime-driver.mjs +113 -0
- package/node_modules/@capyclaw/foundation/lib/adapters/service-provider.mjs +77 -0
- package/node_modules/@capyclaw/foundation/lib/adapters/skill-provider.mjs +56 -0
- package/node_modules/@capyclaw/foundation/lib/adapters/tool-provider.mjs +50 -0
- package/node_modules/@capyclaw/foundation/lib/adapters/ui-panel.mjs +64 -0
- package/node_modules/@capyclaw/foundation/lib/ctx-service.mjs +42 -0
- package/node_modules/@capyclaw/foundation/lib/host/caps.mjs +34 -0
- package/node_modules/@capyclaw/foundation/lib/host/contract.mjs +85 -0
- package/node_modules/@capyclaw/foundation/lib/host/dsh.mjs +50 -0
- package/node_modules/@capyclaw/foundation/lib/host/memory.mjs +160 -0
- package/node_modules/@capyclaw/foundation/lib/host.mjs +23 -0
- package/node_modules/@capyclaw/foundation/lib/kernel.mjs +185 -0
- package/node_modules/@capyclaw/foundation/lib/skill/scan.mjs +87 -0
- package/node_modules/@capyclaw/foundation/lib/subsystems/events.mjs +60 -0
- package/node_modules/@capyclaw/foundation/lib/subsystems/log.mjs +51 -0
- package/node_modules/@capyclaw/foundation/lib/subsystems/net.mjs +188 -0
- package/node_modules/@capyclaw/foundation/package.json +53 -0
- package/node_modules/@capyclaw/foundation/runtime/index.mjs +20 -0
- package/node_modules/@capyclaw/plugin-kit/INTEGRATION.md +100 -0
- package/node_modules/@capyclaw/plugin-kit/README.md +70 -0
- package/node_modules/@capyclaw/plugin-kit/bin/sync.mjs +103 -0
- package/node_modules/@capyclaw/plugin-kit/package.json +69 -0
- package/node_modules/@capyclaw/plugin-kit/src/cli.mjs +56 -0
- package/node_modules/@capyclaw/plugin-kit/src/config.mjs +142 -0
- package/node_modules/@capyclaw/plugin-kit/src/editor/heartbeat-worker.js +129 -0
- package/node_modules/@capyclaw/plugin-kit/src/editor/lease.mjs +139 -0
- package/node_modules/@capyclaw/plugin-kit/src/editor/shadow.mjs +219 -0
- package/node_modules/@capyclaw/plugin-kit/src/editor/transport.js +13 -0
- package/node_modules/@capyclaw/plugin-kit/src/index.mjs +13 -0
- package/node_modules/@capyclaw/plugin-kit/src/pack.mjs +230 -0
- package/node_modules/@capyclaw/plugin-kit/src/runtime/context.mjs +128 -0
- package/node_modules/@capyclaw/plugin-kit/src/self-contained.mjs +307 -0
- package/node_modules/@capyclaw/plugin-kit/src/service/api.mjs +45 -0
- package/node_modules/@capyclaw/plugin-kit/src/service/http.mjs +113 -0
- package/node_modules/@capyclaw/plugin-kit/src/service/mcp-http.mjs +143 -0
- package/node_modules/@capyclaw/plugin-kit/src/service/server.mjs +131 -0
- package/node_modules/@capyclaw/plugin-kit/src/skills/provider.mjs +82 -0
- package/node_modules/@capyclaw/plugin-kit/src/store/blob-store.mjs +109 -0
- package/node_modules/@capyclaw/plugin-kit/src/store/content-store.mjs +17 -0
- package/node_modules/@capyclaw/plugin-kit/src/store/journal-store.mjs +89 -0
- package/node_modules/@capyclaw/plugin-kit/src/tools/result.mjs +41 -0
- package/node_modules/@capyclaw/plugin-kit/src/tools/validate.mjs +150 -0
- package/node_modules/@capyclaw/plugin-kit/src/tuning.mjs +82 -0
- package/node_modules/@capyclaw/plugin-kit/src/util/fs-lock.mjs +132 -0
- package/node_modules/@capyclaw/plugin-kit/src/util/http.mjs +130 -0
- package/node_modules/@capyclaw/plugin-kit/src/workspace/manager.mjs +656 -0
- package/node_modules/@capyclaw/plugin-kit/src/zip.mjs +181 -0
- package/node_modules/@capyclaw/service-host/README.md +86 -0
- package/node_modules/@capyclaw/service-host/cordis.patch.yml +4 -0
- package/node_modules/@capyclaw/service-host/dsh-suite.json +29 -0
- package/node_modules/@capyclaw/service-host/lib/host.mjs +22 -0
- package/node_modules/@capyclaw/service-host/package.json +60 -0
- package/node_modules/@capyclaw/service-host/src/index.mjs +571 -0
- package/node_modules/@capyclaw/service-host/src/tuning.mjs +63 -0
- package/node_modules/@capyclaw/suite-kit/README.md +238 -0
- package/node_modules/@capyclaw/suite-kit/bin/dsh-domain.mjs +98 -0
- package/node_modules/@capyclaw/suite-kit/bin/dsh-suite.mjs +174 -0
- package/node_modules/@capyclaw/suite-kit/client/embed.js +35 -0
- package/node_modules/@capyclaw/suite-kit/domain.schema.json +129 -0
- package/node_modules/@capyclaw/suite-kit/package.json +54 -0
- package/node_modules/@capyclaw/suite-kit/src/activation-registry.mjs +184 -0
- package/node_modules/@capyclaw/suite-kit/src/artifacts.mjs +131 -0
- package/node_modules/@capyclaw/suite-kit/src/capability.mjs +181 -0
- package/node_modules/@capyclaw/suite-kit/src/compose.mjs +201 -0
- package/node_modules/@capyclaw/suite-kit/src/domain.mjs +138 -0
- package/node_modules/@capyclaw/suite-kit/src/embed.mjs +81 -0
- package/node_modules/@capyclaw/suite-kit/src/index.mjs +10 -0
- package/node_modules/@capyclaw/suite-kit/src/manifest.mjs +263 -0
- package/node_modules/@capyclaw/suite-kit/src/orchestration.mjs +100 -0
- package/node_modules/@capyclaw/suite-kit/src/profile.mjs +584 -0
- package/node_modules/@capyclaw/suite-kit/src/surface.mjs +28 -0
- package/node_modules/@capyclaw/suite-kit/suite.schema.json +112 -0
- package/node_modules/@capyclaw/tool-host/README.md +56 -0
- package/node_modules/@capyclaw/tool-host/cordis.patch.yml +4 -0
- package/node_modules/@capyclaw/tool-host/dsh-suite.json +30 -0
- package/node_modules/@capyclaw/tool-host/lib/host.mjs +16 -0
- package/node_modules/@capyclaw/tool-host/package.json +59 -0
- package/node_modules/@capyclaw/tool-host/src/descriptors.mjs +142 -0
- package/node_modules/@capyclaw/tool-host/src/index.mjs +390 -0
- package/package.json +68 -4
- package/plugin.mjs +1 -0
- package/ui/app.js +114 -0
- package/ui/index.html +64 -0
- package/ui/style.css +54 -0
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@capyclaw/suite-kit",
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "DSH suite/domain 清单校验与组合工具:manifest 校验、compose、profile 生成、domain 解析、统一能力描述符、编排闸/授权、嵌入握手。",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./src/index.mjs",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": "./src/index.mjs",
|
|
9
|
+
"./domain": "./src/domain.mjs",
|
|
10
|
+
"./compose": "./src/compose.mjs",
|
|
11
|
+
"./manifest": "./src/manifest.mjs",
|
|
12
|
+
"./capability": "./src/capability.mjs",
|
|
13
|
+
"./orchestration": "./src/orchestration.mjs",
|
|
14
|
+
"./embed": "./src/embed.mjs",
|
|
15
|
+
"./surface": "./src/surface.mjs",
|
|
16
|
+
"./client/embed.js": "./client/embed.js",
|
|
17
|
+
"./profile": "./src/profile.mjs",
|
|
18
|
+
"./artifacts": "./src/artifacts.mjs",
|
|
19
|
+
"./schema": "./suite.schema.json",
|
|
20
|
+
"./package.json": "./package.json",
|
|
21
|
+
"./domain-schema": "./domain.schema.json"
|
|
22
|
+
},
|
|
23
|
+
"bin": {
|
|
24
|
+
"dsh-suite": "./bin/dsh-suite.mjs",
|
|
25
|
+
"dsh-domain": "./bin/dsh-domain.mjs"
|
|
26
|
+
},
|
|
27
|
+
"scripts": {
|
|
28
|
+
"test": "node --test test/*.test.mjs"
|
|
29
|
+
},
|
|
30
|
+
"files": [
|
|
31
|
+
"README.md",
|
|
32
|
+
"bin/",
|
|
33
|
+
"client/",
|
|
34
|
+
"domain.schema.json",
|
|
35
|
+
"src/",
|
|
36
|
+
"suite.schema.json"
|
|
37
|
+
],
|
|
38
|
+
"engines": {
|
|
39
|
+
"node": ">=18"
|
|
40
|
+
},
|
|
41
|
+
"license": "MIT",
|
|
42
|
+
"publishConfig": {
|
|
43
|
+
"access": "public"
|
|
44
|
+
},
|
|
45
|
+
"repository": {
|
|
46
|
+
"type": "git",
|
|
47
|
+
"url": "git+https://github.com/Maple496/capyclaw.git",
|
|
48
|
+
"directory": "C/packages/capyclaw-suite-kit"
|
|
49
|
+
},
|
|
50
|
+
"bugs": {
|
|
51
|
+
"url": "https://github.com/Maple496/capyclaw/issues"
|
|
52
|
+
},
|
|
53
|
+
"homepage": "https://github.com/Maple496/capyclaw/tree/main/C/packages/capyclaw-suite-kit#readme"
|
|
54
|
+
}
|
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 从 suite 组合结果编译 launch-engine 的 capability 注册表。
|
|
3
|
+
*
|
|
4
|
+
* 「实现一个功能要注册哪些组件」的唯一事实来源是 dsh-suite.json;
|
|
5
|
+
* 这里把它编译成运行期激活面(@capyclaw/launch-engine)直接消费的注册表,
|
|
6
|
+
* 不再手写第二种「要求 → 组件」映射。映射规则:
|
|
7
|
+
*
|
|
8
|
+
* - 每个包 = 一个激活 capability(name = 包名),条件是它有 row、拥有服务进程、
|
|
9
|
+
* 或是组合内其他包 required capability 的提供方(纯 support 依赖不单独成能力)
|
|
10
|
+
* entries = 该包的 rows(entryId = include:<rowId>)
|
|
11
|
+
* requires = 包 requires.capabilities 解析到的提供方包(组合内)
|
|
12
|
+
* services = 套件 services.json 里 cmd 指向该包 node_modules/<pkg>/ 的条目
|
|
13
|
+
* - 每个套件 = 一个聚合 capability(name = 套件名;bundle 级开关)
|
|
14
|
+
* bundles = [{ name: 套件名 }](installSuites 把套件名写进 dsh.profile.bundles)
|
|
15
|
+
* requires = 该套件下所有已注册的包 capability
|
|
16
|
+
* entries/services = 无法归属到具体包的行与服务
|
|
17
|
+
*
|
|
18
|
+
* services 编译时展开 ${PACKAGE_DIR}(注册表落在 profile 目录,不能依赖
|
|
19
|
+
* broker 清单装载期的展开基准),剥掉 portFrom/dirFrom(那是 runtime
|
|
20
|
+
* 物化期的分配提示,不是激活语义);${PORT} 原样留给 broker 仲裁。
|
|
21
|
+
*/
|
|
22
|
+
import fs from 'node:fs'
|
|
23
|
+
import path from 'node:path'
|
|
24
|
+
|
|
25
|
+
export const ACTIVATION_REGISTRY_FILENAME = 'launch-engine.registry.json'
|
|
26
|
+
const ENTRY_PREFIX = 'include:'
|
|
27
|
+
|
|
28
|
+
function readSuiteServices(suiteDir) {
|
|
29
|
+
const file = path.join(suiteDir, 'services.json')
|
|
30
|
+
if (!fs.existsSync(file)) return []
|
|
31
|
+
try {
|
|
32
|
+
const raw = JSON.parse(fs.readFileSync(file, 'utf8'))
|
|
33
|
+
return Array.isArray(raw?.services) ? raw.services : []
|
|
34
|
+
} catch {
|
|
35
|
+
return []
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function serviceOwnerPackage(service, packageNames) {
|
|
40
|
+
const cmd = Array.isArray(service?.cmd) ? service.cmd.join(' ') : ''
|
|
41
|
+
for (const name of packageNames) {
|
|
42
|
+
const needle = `node_modules/${name}/`
|
|
43
|
+
if (cmd.includes(needle)) return name
|
|
44
|
+
}
|
|
45
|
+
return null
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** 展开 services.json 的 ${PACKAGE_DIR}。两种规格:
|
|
49
|
+
* - 新代:cmd 形如 ${PACKAGE_DIR}/node_modules/<pkg>/…——${PACKAGE_DIR} 指
|
|
50
|
+
* 持有 node_modules 的套件根(services.json 所在目录),不是 owner 包自身目录;
|
|
51
|
+
* - 旧代:cmd 形如 ${PACKAGE_DIR}/server/relay.mjs——${PACKAGE_DIR} 指 owner 包目录。 */
|
|
52
|
+
function toActivationService(service, { packageDir, suiteDir }) {
|
|
53
|
+
const joined = (service.cmd || []).join(' ')
|
|
54
|
+
const base = joined.includes('node_modules/') ? suiteDir : packageDir
|
|
55
|
+
const cmd = (service.cmd || []).map((part) => String(part).replaceAll('${PACKAGE_DIR}', base))
|
|
56
|
+
const next = { ...service, cmd }
|
|
57
|
+
delete next.portFrom
|
|
58
|
+
delete next.dirFrom
|
|
59
|
+
return next
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function packageDirOf(pkg) {
|
|
63
|
+
if (!pkg?.dir || !pkg?.entry?.path) return null
|
|
64
|
+
return path.resolve(pkg.dir, pkg.entry.path).replace(/\\/g, '/')
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* @param {{suites: {dir:string, manifest:object}[], composed: object, requiredCapabilities?: string[]}} input
|
|
69
|
+
* suites 已加载(components 已展开)的套件;composed = composeSuites 输出
|
|
70
|
+
* requiredCapabilities 额外的能力名(如 domain runtime.requires.capabilities),
|
|
71
|
+
* 其提供方也应进入注册表
|
|
72
|
+
* @returns {{registry: {version:1, capabilities:object}, warnings: string[]}}
|
|
73
|
+
*/
|
|
74
|
+
export function buildActivationRegistry({ suites, composed, requiredCapabilities = [] }) {
|
|
75
|
+
const warnings = []
|
|
76
|
+
const capabilities = {}
|
|
77
|
+
const packages = composed?.packages || []
|
|
78
|
+
const packageNames = packages.map((pkg) => pkg.name)
|
|
79
|
+
const capProvider = new Map((composed?.capabilities || []).map((item) => [item.capability, item.package]))
|
|
80
|
+
|
|
81
|
+
/* 组合内被别人 required 的能力的提供方:即使没有 row(纯服务进程形态,
|
|
82
|
+
* 如 capyclaw-ppt-workbench)也必须成为 capability,否则依赖图断链。 */
|
|
83
|
+
const demandedProviders = new Set()
|
|
84
|
+
for (const pkg of packages) {
|
|
85
|
+
for (const capability of Object.keys(pkg.entry.requires?.capabilities || {})) {
|
|
86
|
+
const provider = capProvider.get(capability)
|
|
87
|
+
if (provider) demandedProviders.add(provider)
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
for (const capability of requiredCapabilities) {
|
|
91
|
+
const provider = capProvider.get(String(capability).split('@')[0])
|
|
92
|
+
if (provider) demandedProviders.add(provider)
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
const rowsByPackage = new Map()
|
|
96
|
+
const orphanRowsBySuite = new Map()
|
|
97
|
+
for (const row of composed?.rows || []) {
|
|
98
|
+
if (row.disabled) continue
|
|
99
|
+
if (packageNames.includes(row.name)) {
|
|
100
|
+
rowsByPackage.set(row.name, [...(rowsByPackage.get(row.name) || []), row])
|
|
101
|
+
} else {
|
|
102
|
+
const suite = row._suite || ''
|
|
103
|
+
orphanRowsBySuite.set(suite, [...(orphanRowsBySuite.get(suite) || []), row])
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
const packageServices = new Map()
|
|
108
|
+
for (const suite of suites || []) {
|
|
109
|
+
for (const service of readSuiteServices(suite.dir)) {
|
|
110
|
+
const owner = serviceOwnerPackage(service, packageNames)
|
|
111
|
+
if (owner) packageServices.set(owner, [...(packageServices.get(owner) || []), { service, suiteDir: (suite.dir || '').replace(/\\/g, '/') }])
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
for (const pkg of packages) {
|
|
116
|
+
const rows = rowsByPackage.get(pkg.name) || []
|
|
117
|
+
const services = packageServices.get(pkg.name) || []
|
|
118
|
+
if (!rows.length && !services.length && !demandedProviders.has(pkg.name)) continue
|
|
119
|
+
const requires = []
|
|
120
|
+
for (const capability of Object.keys(pkg.entry.requires?.capabilities || {})) {
|
|
121
|
+
const provider = capProvider.get(capability)
|
|
122
|
+
if (!provider || provider === pkg.name || requires.includes(provider)) continue
|
|
123
|
+
if (!packageNames.includes(provider)) continue
|
|
124
|
+
requires.push(provider)
|
|
125
|
+
}
|
|
126
|
+
capabilities[pkg.name] = {
|
|
127
|
+
name: pkg.name,
|
|
128
|
+
title: pkg.name,
|
|
129
|
+
requires,
|
|
130
|
+
bundles: [],
|
|
131
|
+
entries: rows.map((row) => ({ entryId: ENTRY_PREFIX + row.id, enabled: true })),
|
|
132
|
+
services: services.map(({ service, suiteDir }) => toActivationService(service, {
|
|
133
|
+
packageDir: packageDirOf(pkg) || suiteDir,
|
|
134
|
+
suiteDir,
|
|
135
|
+
})),
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
for (const suite of suites || []) {
|
|
140
|
+
const name = suite.manifest.name
|
|
141
|
+
const dir = (suite.dir || '').replace(/\\/g, '/')
|
|
142
|
+
if (capabilities[name]) {
|
|
143
|
+
warnings.push(`capability 名冲突: 套件 ${name} 与已有包能力重名,聚合能力改用 suite:<name>`)
|
|
144
|
+
}
|
|
145
|
+
const aggregateName = capabilities[name] ? `suite:${name}` : name
|
|
146
|
+
const unmatched = []
|
|
147
|
+
|
|
148
|
+
for (const service of readSuiteServices(suite.dir)) {
|
|
149
|
+
const owner = serviceOwnerPackage(service, packageNames)
|
|
150
|
+
/* 已在包能力里挂载的服务不重复;只有无主或主人未成能力的才归套件聚合。 */
|
|
151
|
+
if (owner && capabilities[owner]) continue
|
|
152
|
+
unmatched.push(toActivationService(service, { packageDir: dir, suiteDir: dir }))
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
const requires = []
|
|
156
|
+
for (const pkg of packages) {
|
|
157
|
+
if (pkg.suite !== name) continue
|
|
158
|
+
if (capabilities[pkg.name] && !requires.includes(pkg.name)) requires.push(pkg.name)
|
|
159
|
+
}
|
|
160
|
+
const orphanRows = orphanRowsBySuite.get(name) || []
|
|
161
|
+
if (orphanRows.length) {
|
|
162
|
+
warnings.push(`${name}: 行 [${orphanRows.map((row) => row.id).join(', ')}] 引用的包不在组合里(外部已安装),归入套件聚合能力`)
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
capabilities[aggregateName] = {
|
|
166
|
+
name: aggregateName,
|
|
167
|
+
title: suite.manifest.title || name,
|
|
168
|
+
requires,
|
|
169
|
+
bundles: [{ name, enabled: true }],
|
|
170
|
+
entries: orphanRows.map((row) => ({ entryId: ENTRY_PREFIX + row.id, enabled: true })),
|
|
171
|
+
services: unmatched,
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
return { registry: { version: 1, capabilities }, warnings }
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/** 安装期落盘:<profile>/launch-engine.registry.json。 */
|
|
179
|
+
export function writeActivationRegistry(profileDir, { suites, composed }) {
|
|
180
|
+
const { registry, warnings } = buildActivationRegistry({ suites, composed })
|
|
181
|
+
const file = path.join(profileDir, ACTIVATION_REGISTRY_FILENAME)
|
|
182
|
+
fs.writeFileSync(file, JSON.stringify(registry, null, 2) + '\n')
|
|
183
|
+
return { file, registry, warnings, capabilities: Object.keys(registry.capabilities) }
|
|
184
|
+
}
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 公共插件版本化 artifact store。
|
|
3
|
+
*
|
|
4
|
+
* 目录:
|
|
5
|
+
* <root>/index.json
|
|
6
|
+
* <root>/<safeName>/<version>/package/...
|
|
7
|
+
*
|
|
8
|
+
* 版本化安装:installSuites({ packageSpecs: ['@capyclaw/asset-service@0.1.3'] })
|
|
9
|
+
* 回滚:installSuites({ rollbackTo: '<profile>/dsh-composition.lock.json.bak-...' })
|
|
10
|
+
*/
|
|
11
|
+
import crypto from 'node:crypto'
|
|
12
|
+
import fs from 'node:fs'
|
|
13
|
+
import os from 'node:os'
|
|
14
|
+
import path from 'node:path'
|
|
15
|
+
|
|
16
|
+
export function defaultArtifactRoot() {
|
|
17
|
+
return process.env.DSH_ARTIFACT_STORE || path.join(os.homedir(), '.dsh-store', 'artifacts')
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function safeName(name) {
|
|
21
|
+
return String(name).replace(/[@/\\]/g, '__')
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function readJsonSafe(file) {
|
|
25
|
+
try { return JSON.parse(fs.readFileSync(file, 'utf8')) } catch { return null }
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function walk(dir, base = dir) {
|
|
29
|
+
const out = []
|
|
30
|
+
for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
|
|
31
|
+
const full = path.join(dir, entry.name)
|
|
32
|
+
if (entry.isDirectory()) out.push(...walk(full, base))
|
|
33
|
+
else out.push(path.relative(base, full).split('\\').join('/'))
|
|
34
|
+
}
|
|
35
|
+
return out.sort()
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function treeHash(dir) {
|
|
39
|
+
const hash = crypto.createHash('sha256')
|
|
40
|
+
for (const rel of walk(dir)) {
|
|
41
|
+
hash.update(rel)
|
|
42
|
+
hash.update(fs.readFileSync(path.join(dir, rel)))
|
|
43
|
+
}
|
|
44
|
+
return hash.digest('hex')
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function copyPackageDir(src, dest) {
|
|
48
|
+
fs.mkdirSync(dest, { recursive: true })
|
|
49
|
+
for (const rel of walk(src)) {
|
|
50
|
+
if (rel === 'node_modules' || rel.startsWith('node_modules/')) continue
|
|
51
|
+
if (rel === '.git' || rel.startsWith('.git/')) continue
|
|
52
|
+
if (rel === 'test' || rel.startsWith('test/')) continue
|
|
53
|
+
if (rel === 'coverage' || rel.startsWith('coverage/')) continue
|
|
54
|
+
const to = path.join(dest, rel)
|
|
55
|
+
fs.mkdirSync(path.dirname(to), { recursive: true })
|
|
56
|
+
fs.copyFileSync(path.join(src, rel), to)
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export function artifactIndexPath(root = defaultArtifactRoot()) {
|
|
61
|
+
return path.join(root, 'index.json')
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export function readArtifactIndex(root = defaultArtifactRoot()) {
|
|
65
|
+
return readJsonSafe(artifactIndexPath(root)) || { version: 1, packages: {} }
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function writeArtifactIndex(root, index) {
|
|
69
|
+
fs.mkdirSync(root, { recursive: true })
|
|
70
|
+
fs.writeFileSync(artifactIndexPath(root), JSON.stringify(index, null, 2) + '\n')
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* 打包一个包目录到 artifact store。
|
|
75
|
+
* @param {string} packageDir
|
|
76
|
+
* @param {{ root?: string, force?: boolean }} options
|
|
77
|
+
*/
|
|
78
|
+
export function packPackage(packageDir, { root = defaultArtifactRoot(), force = false } = {}) {
|
|
79
|
+
const manifest = readJsonSafe(path.join(packageDir, 'package.json'))
|
|
80
|
+
if (!manifest?.name || !manifest?.version) throw new Error(`artifact pack: ${packageDir} 缺少 package.json name/version`)
|
|
81
|
+
const dest = path.join(root, safeName(manifest.name), manifest.version, 'package')
|
|
82
|
+
if (fs.existsSync(dest) && !force) throw new Error(`artifact 已存在: ${manifest.name}@${manifest.version}(加 --force 覆盖)`)
|
|
83
|
+
fs.rmSync(dest, { recursive: true, force: true })
|
|
84
|
+
copyPackageDir(packageDir, dest)
|
|
85
|
+
const meta = {
|
|
86
|
+
name: manifest.name,
|
|
87
|
+
version: manifest.version,
|
|
88
|
+
apiVersion: manifest.dsh?.apiVersion || null,
|
|
89
|
+
dependencies: manifest.dependencies || {},
|
|
90
|
+
dir: dest,
|
|
91
|
+
sha256: treeHash(dest),
|
|
92
|
+
packedAt: new Date().toISOString(),
|
|
93
|
+
}
|
|
94
|
+
const index = readArtifactIndex(root)
|
|
95
|
+
index.packages[manifest.name] ||= {}
|
|
96
|
+
index.packages[manifest.name][manifest.version] = meta
|
|
97
|
+
writeArtifactIndex(root, index)
|
|
98
|
+
return meta
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* @param {string} spec name 或 name@version
|
|
103
|
+
* @param {{ root?: string }} options
|
|
104
|
+
*/
|
|
105
|
+
export function resolvePackageSpec(spec, { root = defaultArtifactRoot() } = {}) {
|
|
106
|
+
if (!spec || typeof spec !== 'string') throw new Error('artifact resolve: spec 必填')
|
|
107
|
+
const at = spec.lastIndexOf('@')
|
|
108
|
+
const name = at > 0 ? spec.slice(0, at) : spec
|
|
109
|
+
const version = at > 0 ? spec.slice(at + 1) : ''
|
|
110
|
+
const index = readArtifactIndex(root)
|
|
111
|
+
const versions = index.packages[name]
|
|
112
|
+
if (!versions) throw new Error(`artifact 不存在: ${name}(root=${root})`)
|
|
113
|
+
if (version) {
|
|
114
|
+
const hit = versions[version]
|
|
115
|
+
if (!hit) throw new Error(`artifact 版本不存在: ${spec};可用: ${Object.keys(versions).sort().join(', ')}`)
|
|
116
|
+
return hit
|
|
117
|
+
}
|
|
118
|
+
const latest = Object.keys(versions).sort((a, b) => a.localeCompare(b, undefined, { numeric: true })).at(-1)
|
|
119
|
+
return versions[latest]
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
export function listArtifacts({ root = defaultArtifactRoot() } = {}) {
|
|
123
|
+
const index = readArtifactIndex(root)
|
|
124
|
+
return Object.entries(index.packages).flatMap(([name, versions]) =>
|
|
125
|
+
Object.values(versions).map((meta) => ({ name, version: meta.version, apiVersion: meta.apiVersion, sha256: meta.sha256, dir: meta.dir })))
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
export function recordFromLock(lock, { root = defaultArtifactRoot() } = {}) {
|
|
129
|
+
if (!lock?.packages) throw new Error('artifact recordFromLock: lock 缺 packages')
|
|
130
|
+
return Object.entries(lock.packages).map(([name, meta]) => resolvePackageSpec(`${name}@${meta.version}`, { root }))
|
|
131
|
+
}
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 统一能力描述符(docs/suite-interop-standard.md §A.4 / §B.6)——公共契约层。
|
|
3
|
+
*
|
|
4
|
+
* 编排器面向「能力」而非「模式/套件」:描述符只出现产物类型,不出现对方套件。
|
|
5
|
+
* 本模块是纯函数校验/匹配器,不 import 任何具体套件;各套件契约(browser-contract.json、
|
|
6
|
+
* ppt-contract.json …)经 fromContractTool 映射成统一描述符后注册/发现。
|
|
7
|
+
*
|
|
8
|
+
* 双模态(标准 §A.5):surface 声明呈现面(独立 app / 可嵌入子窗口),
|
|
9
|
+
* 控制面不分模式——同一描述符在两种呈现下被同一套工具操控。
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
export const CONTROL_MODES = Object.freeze(['interface', 'browser'])
|
|
13
|
+
export const KINDS = Object.freeze(['read', 'write', 'control'])
|
|
14
|
+
export const RISK_LEVELS = Object.freeze(['low', 'high'])
|
|
15
|
+
|
|
16
|
+
/** 类型词表(B.6):住公共层、版本化;新增类型=加词条,不改套件。 */
|
|
17
|
+
export const TYPE_VOCAB = Object.freeze([
|
|
18
|
+
'artifact:deck', 'artifact:canvas',
|
|
19
|
+
'artifact:file/pptx', 'artifact:file/docx', 'artifact:file/xlsx', 'artifact:file/pdf',
|
|
20
|
+
'artifact:file/html', 'artifact:file/zip',
|
|
21
|
+
'artifact:image/png', 'artifact:image/jpeg', 'artifact:image/webp',
|
|
22
|
+
'artifact:dataset/json', 'artifact:dataset/csv',
|
|
23
|
+
'artifact:text/markdown', 'artifact:text/plain',
|
|
24
|
+
'artifact:media/video', 'artifact:media/audio',
|
|
25
|
+
'meta:title', 'meta:desc', 'meta:tags',
|
|
26
|
+
'result:url', 'result:status',
|
|
27
|
+
// 会话/设置域(capyclaw-suite 子会话能力;标准 v0.3 扩词表)。
|
|
28
|
+
'session:subsession', 'session:turn', 'session:list', 'session:transcript',
|
|
29
|
+
'settings:snapshot', 'settings:patch',
|
|
30
|
+
// 编排域(conductor 套件;标准 v0.3 扩词表)。
|
|
31
|
+
'meta:plan', 'meta:capabilities',
|
|
32
|
+
])
|
|
33
|
+
|
|
34
|
+
/** 逻辑产物类型 → work-store 信封短类型(envelope.mjs ARTIFACT_TYPES)。
|
|
35
|
+
* 逻辑文档(artifact:deck/canvas)无 raw 信封映射:交接的永远是导出后的 raw asset。 */
|
|
36
|
+
export const ENVELOPE_TYPE_MAP = Object.freeze({
|
|
37
|
+
'artifact:file/pptx': 'pptx', 'artifact:file/docx': 'docx', 'artifact:file/xlsx': 'xlsx',
|
|
38
|
+
'artifact:file/pdf': 'pdf', 'artifact:file/html': 'html', 'artifact:file/zip': 'zip',
|
|
39
|
+
'artifact:image/png': 'image', 'artifact:image/jpeg': 'image', 'artifact:image/webp': 'image',
|
|
40
|
+
'artifact:dataset/json': 'json', 'artifact:dataset/csv': 'csv',
|
|
41
|
+
'artifact:text/markdown': 'markdown', 'artifact:text/plain': 'text',
|
|
42
|
+
'artifact:media/video': 'video', 'artifact:media/audio': 'audio',
|
|
43
|
+
})
|
|
44
|
+
|
|
45
|
+
export function envelopeTypeFor(artifactType) {
|
|
46
|
+
return ENVELOPE_TYPE_MAP[String(artifactType)] ?? null
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const TYPE_RE = /^(artifact|meta|result|session|settings):[A-Za-z0-9_.-]+(?:\/[A-Za-z0-9_.*-]+)*$/
|
|
50
|
+
const NAME_RE = /^[A-Za-z][A-Za-z0-9_.-]*$/
|
|
51
|
+
|
|
52
|
+
/** 类型匹配:精确相等,或模式以 `/*` 结尾做前缀匹配(artifact:file/* ⊇ artifact:file/pptx)。 */
|
|
53
|
+
export function typeMatches(pattern, type) {
|
|
54
|
+
const p = String(pattern), t = String(type)
|
|
55
|
+
if (p === t) return true
|
|
56
|
+
if (p.endsWith('/*')) return t.startsWith(p.slice(0, -1))
|
|
57
|
+
return false
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** 缺省补齐(不改动入参)。 */
|
|
61
|
+
export function normalizeCapabilityDescriptor(input = {}) {
|
|
62
|
+
const d = { ...input }
|
|
63
|
+
d.consumes = Array.isArray(d.consumes) ? [...d.consumes] : []
|
|
64
|
+
d.produces = Array.isArray(d.produces) ? [...d.produces] : []
|
|
65
|
+
d.auth = d.auth ?? null
|
|
66
|
+
d.risk = d.risk ?? 'low'
|
|
67
|
+
d.confirm = d.confirm ?? false
|
|
68
|
+
d.surface = { standalone: true, embeddable: false, ...(d.surface || {}) }
|
|
69
|
+
return d
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** 校验单个描述符 → {ok, errors, warnings}(与 validateSuiteManifest 同风格)。 */
|
|
73
|
+
export function validateCapabilityDescriptor(input, options = {}) {
|
|
74
|
+
const errors = []
|
|
75
|
+
const warnings = []
|
|
76
|
+
const label = options.label ? `${options.label}: ` : ''
|
|
77
|
+
const add = (list, message) => list.push(label + message)
|
|
78
|
+
|
|
79
|
+
if (!input || typeof input !== 'object' || Array.isArray(input)) {
|
|
80
|
+
return { ok: false, errors: [label + '描述符必须是 object'], warnings }
|
|
81
|
+
}
|
|
82
|
+
const d = input
|
|
83
|
+
if (typeof d.name !== 'string' || !NAME_RE.test(d.name)) add(errors, 'name 必须是非空标识符(字母开头,可含 . _ -)')
|
|
84
|
+
if (!CONTROL_MODES.includes(d.controlMode)) add(errors, `controlMode 必须是 ${CONTROL_MODES.join('|')},收到: ${d.controlMode}`)
|
|
85
|
+
if (!KINDS.includes(d.kind)) add(errors, `kind 必须是 ${KINDS.join('|')},收到: ${d.kind}`)
|
|
86
|
+
if (typeof d.capability !== 'string' || !d.capability) add(errors, 'capability 必须是非空字符串(能力语义,如 artifact.export)')
|
|
87
|
+
|
|
88
|
+
for (const key of ['consumes', 'produces']) {
|
|
89
|
+
const list = d[key]
|
|
90
|
+
if (list == null) continue
|
|
91
|
+
if (!Array.isArray(list)) { add(errors, `${key} 必须是字符串数组`); continue }
|
|
92
|
+
for (const t of list) {
|
|
93
|
+
if (typeof t !== 'string' || !TYPE_RE.test(t)) { add(errors, `${key} 含非法类型词条: ${t}`); continue }
|
|
94
|
+
if (t.includes('*') && !t.endsWith('/*')) add(errors, `${key} 通配只允许结尾 /*: ${t}`)
|
|
95
|
+
if (!t.includes('*') && !TYPE_VOCAB.includes(t)) add(warnings, `${key} 词条不在当前词表(可扩展,建议登记): ${t}`)
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
if (d.auth != null && (typeof d.auth !== 'string' || !d.auth)) add(errors, 'auth 必须是 null 或非空字符串(如 bilibili:login)')
|
|
100
|
+
if (!RISK_LEVELS.includes(d.risk ?? 'low')) add(errors, `risk 必须是 ${RISK_LEVELS.join('|')}`)
|
|
101
|
+
if (d.confirm != null && typeof d.confirm !== 'boolean') add(errors, 'confirm 必须是 boolean')
|
|
102
|
+
if ((d.risk ?? 'low') === 'high' && d.confirm !== true) add(errors, 'risk:high(不可逆外部动作)强制 confirm:true')
|
|
103
|
+
|
|
104
|
+
if (d.inputSchema != null && (typeof d.inputSchema !== 'object' || Array.isArray(d.inputSchema))) {
|
|
105
|
+
add(errors, 'inputSchema 必须是 object')
|
|
106
|
+
}
|
|
107
|
+
if (d.since != null && (typeof d.since !== 'string' || !d.since)) add(errors, 'since 必须是非空字符串(semver)')
|
|
108
|
+
if (d.suite != null && (typeof d.suite !== 'string' || !d.suite)) add(errors, 'suite 必须是非空字符串')
|
|
109
|
+
|
|
110
|
+
if (d.surface != null) {
|
|
111
|
+
if (typeof d.surface !== 'object' || Array.isArray(d.surface)) add(errors, 'surface 必须是 object')
|
|
112
|
+
else {
|
|
113
|
+
const s = d.surface
|
|
114
|
+
for (const key of ['standalone', 'embeddable']) {
|
|
115
|
+
if (s[key] != null && typeof s[key] !== 'boolean') add(errors, `surface.${key} 必须是 boolean`)
|
|
116
|
+
}
|
|
117
|
+
if (s.embeddable === true && (typeof s.embedPath !== 'string' || !s.embedPath.startsWith('/'))) {
|
|
118
|
+
add(errors, 'surface.embeddable:true 必须给 embedPath(同源本地绝对路径,如 /capyclaw/ppt)')
|
|
119
|
+
}
|
|
120
|
+
if (s.embedPath != null && (typeof s.embedPath !== 'string' || !s.embedPath.startsWith('/'))) {
|
|
121
|
+
add(errors, 'surface.embedPath 必须以 / 开头(同源本地地址)')
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
return { ok: errors.length === 0, errors, warnings }
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/** 校验能力目录:逐个合法 + name 全局唯一。 */
|
|
129
|
+
export function validateCapabilityCatalog(list) {
|
|
130
|
+
const errors = []
|
|
131
|
+
const warnings = []
|
|
132
|
+
if (!Array.isArray(list)) return { ok: false, errors: ['能力目录必须是数组'], warnings }
|
|
133
|
+
const seen = new Set()
|
|
134
|
+
for (const [i, d] of list.entries()) {
|
|
135
|
+
const r = validateCapabilityDescriptor(d, { label: `#${i}${d?.name ? `(${d.name})` : ''}` })
|
|
136
|
+
errors.push(...r.errors)
|
|
137
|
+
warnings.push(...r.warnings)
|
|
138
|
+
if (d && typeof d.name === 'string') {
|
|
139
|
+
if (seen.has(d.name)) errors.push(`能力名重复: ${d.name}`)
|
|
140
|
+
seen.add(d.name)
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
return { ok: errors.length === 0, errors, warnings }
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* 现有套件契约工具 → 统一描述符。
|
|
148
|
+
* tool 形如 browser-contract.json 的 {name, kind, since, capability, description, inputSchema}。
|
|
149
|
+
* 缺省:interface 模式 auth=null;browser 模式 auth='browser:session';risk=low/confirm=false。
|
|
150
|
+
* 发布类能力必须显式传 risk:'high', confirm:true(校验器强制)。
|
|
151
|
+
*/
|
|
152
|
+
export function fromContractTool(tool, options = {}) {
|
|
153
|
+
const controlMode = options.controlMode || 'interface'
|
|
154
|
+
return normalizeCapabilityDescriptor({
|
|
155
|
+
name: tool.name,
|
|
156
|
+
kind: tool.kind,
|
|
157
|
+
since: tool.since,
|
|
158
|
+
description: tool.description,
|
|
159
|
+
inputSchema: tool.inputSchema,
|
|
160
|
+
capability: options.capability || tool.capability || `${options.suite || 'suite'}.${tool.name}`,
|
|
161
|
+
suite: options.suite,
|
|
162
|
+
controlMode,
|
|
163
|
+
auth: options.auth !== undefined ? options.auth : (controlMode === 'browser' ? 'browser:session' : null),
|
|
164
|
+
risk: options.risk ?? 'low',
|
|
165
|
+
confirm: options.confirm ?? false,
|
|
166
|
+
consumes: options.consumes ?? [],
|
|
167
|
+
produces: options.produces ?? [],
|
|
168
|
+
surface: options.surface,
|
|
169
|
+
})
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/** 链式匹配:descriptor 的 consumes 是否全部能被 producedTypes 覆盖(空 consumes 恒真)。 */
|
|
173
|
+
export function canConsume(descriptor, producedTypes = []) {
|
|
174
|
+
const consumes = Array.isArray(descriptor?.consumes) ? descriptor.consumes : []
|
|
175
|
+
return consumes.every((t) => producedTypes.some((p) => typeMatches(t, p)))
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/** 发现:目录里谁能产出该类型(编排器按类型找能力,不写死套件/工具名)。 */
|
|
179
|
+
export function findProducers(catalog, type) {
|
|
180
|
+
return (catalog || []).filter((d) => Array.isArray(d.produces) && d.produces.some((p) => typeMatches(p, type)))
|
|
181
|
+
}
|