@capyclaw/suite-kit 0.0.0-stage → 0.2.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.
- package/README.md +289 -3
- package/bin/dsh-domain.mjs +98 -0
- package/bin/dsh-suite.mjs +174 -0
- package/client/embed.js +35 -0
- package/domain.schema.json +129 -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 +71 -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 +132 -0
- package/node_modules/@capyclaw/plugin-kit/src/editor/heartbeat.mjs +26 -0
- package/node_modules/@capyclaw/plugin-kit/src/editor/lease.mjs +60 -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 +150 -0
- package/node_modules/@capyclaw/plugin-kit/src/service/server.mjs +131 -0
- package/node_modules/@capyclaw/plugin-kit/src/service/static.mjs +79 -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 +57 -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 +664 -0
- package/node_modules/@capyclaw/plugin-kit/src/zip.mjs +181 -0
- package/package.json +59 -4
- package/src/activation-registry.mjs +184 -0
- package/src/artifacts.mjs +131 -0
- package/src/capability.mjs +181 -0
- package/src/compose.mjs +201 -0
- package/src/domain.mjs +138 -0
- package/src/embed.mjs +81 -0
- package/src/host.mjs +230 -0
- package/src/index.mjs +24 -0
- package/src/manifest.mjs +263 -0
- package/src/orchestration.mjs +100 -0
- package/src/profile.mjs +584 -0
- package/src/surface.mjs +28 -0
- package/suite.schema.json +128 -0
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 极简跨平台 ZIP 读写(store + deflate 读,store 写;无外部依赖)——全仓单一真源。
|
|
3
|
+
*
|
|
4
|
+
* 之前三处实现各持一份(plugin-development 写 / tools-plugin-release 读 /
|
|
5
|
+
* @capyclaw/suite-kit zip-read 读),本模块合并之。旧位置保留兼容 shim。
|
|
6
|
+
*
|
|
7
|
+
* 写入:store 模式(内容不压缩,跨平台稳定,产物可被任何标准读取器读)。
|
|
8
|
+
* 读取:支持 store(0) 与 deflate(8);接受 Buffer 或文件路径——本地文件与
|
|
9
|
+
* 远端下载源共用同一条校验/解包路径。
|
|
10
|
+
*/
|
|
11
|
+
import fs from 'node:fs'
|
|
12
|
+
import path from 'node:path'
|
|
13
|
+
import zlib from 'node:zlib'
|
|
14
|
+
|
|
15
|
+
/* ------------------------------------------------------------------ 写入 */
|
|
16
|
+
|
|
17
|
+
const CRC_TABLE = (() => {
|
|
18
|
+
const table = new Uint32Array(256)
|
|
19
|
+
for (let n = 0; n < 256; n++) {
|
|
20
|
+
let c = n
|
|
21
|
+
for (let k = 0; k < 8; k++) c = (c & 1) ? (0xedb88320 ^ (c >>> 1)) : (c >>> 1)
|
|
22
|
+
table[n] = c >>> 0
|
|
23
|
+
}
|
|
24
|
+
return table
|
|
25
|
+
})()
|
|
26
|
+
|
|
27
|
+
function crc32(buffer) {
|
|
28
|
+
let crc = 0xffffffff
|
|
29
|
+
for (let i = 0; i < buffer.length; i++) crc = CRC_TABLE[(crc ^ buffer[i]) & 0xff] ^ (crc >>> 8)
|
|
30
|
+
return (crc ^ 0xffffffff) >>> 0
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function dosDateTime(date = new Date()) {
|
|
34
|
+
const year = Math.max(1980, date.getFullYear())
|
|
35
|
+
const time = ((date.getHours() & 0x1f) << 11) | ((date.getMinutes() & 0x3f) << 5) | (Math.floor(date.getSeconds() / 2) & 0x1f)
|
|
36
|
+
const day = (((year - 1980) & 0x7f) << 9) | (((date.getMonth() + 1) & 0x0f) << 5) | (date.getDate() & 0x1f)
|
|
37
|
+
return { time, day }
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function walkFiles(dir, base = dir) {
|
|
41
|
+
const out = []
|
|
42
|
+
for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
|
|
43
|
+
const full = path.join(dir, entry.name)
|
|
44
|
+
if (entry.isDirectory()) out.push(...walkFiles(full, base))
|
|
45
|
+
else if (entry.isFile()) out.push(path.relative(base, full).replace(/\\/g, '/'))
|
|
46
|
+
}
|
|
47
|
+
return out.sort()
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** 把目录写成 store 模式 ZIP。返回打包的相对路径列表。 */
|
|
51
|
+
export function writeZipFromDir(sourceDir, outFile) {
|
|
52
|
+
const files = walkFiles(sourceDir)
|
|
53
|
+
const now = dosDateTime()
|
|
54
|
+
const localParts = []
|
|
55
|
+
const centralParts = []
|
|
56
|
+
let offset = 0
|
|
57
|
+
|
|
58
|
+
for (const name of files) {
|
|
59
|
+
const data = fs.readFileSync(path.join(sourceDir, name))
|
|
60
|
+
const nameBytes = Buffer.from(name, 'utf8')
|
|
61
|
+
const crc = crc32(data)
|
|
62
|
+
|
|
63
|
+
const local = Buffer.alloc(30)
|
|
64
|
+
local.writeUInt32LE(0x04034b50, 0)
|
|
65
|
+
local.writeUInt16LE(20, 4)
|
|
66
|
+
local.writeUInt16LE(0x0800, 6)
|
|
67
|
+
local.writeUInt16LE(0, 8)
|
|
68
|
+
local.writeUInt16LE(now.time, 10)
|
|
69
|
+
local.writeUInt16LE(now.day, 12)
|
|
70
|
+
local.writeUInt32LE(crc, 14)
|
|
71
|
+
local.writeUInt32LE(data.length, 18)
|
|
72
|
+
local.writeUInt32LE(data.length, 22)
|
|
73
|
+
local.writeUInt16LE(nameBytes.length, 26)
|
|
74
|
+
local.writeUInt16LE(0, 28)
|
|
75
|
+
localParts.push(local, nameBytes, data)
|
|
76
|
+
|
|
77
|
+
const central = Buffer.alloc(46)
|
|
78
|
+
central.writeUInt32LE(0x02014b50, 0)
|
|
79
|
+
central.writeUInt16LE(20, 4)
|
|
80
|
+
central.writeUInt16LE(20, 6)
|
|
81
|
+
central.writeUInt16LE(0x0800, 8)
|
|
82
|
+
central.writeUInt16LE(0, 10)
|
|
83
|
+
central.writeUInt16LE(now.time, 12)
|
|
84
|
+
central.writeUInt16LE(now.day, 14)
|
|
85
|
+
central.writeUInt32LE(crc, 16)
|
|
86
|
+
central.writeUInt32LE(data.length, 20)
|
|
87
|
+
central.writeUInt32LE(data.length, 24)
|
|
88
|
+
central.writeUInt16LE(nameBytes.length, 28)
|
|
89
|
+
central.writeUInt16LE(0, 30)
|
|
90
|
+
central.writeUInt16LE(0, 32)
|
|
91
|
+
central.writeUInt16LE(0, 34)
|
|
92
|
+
central.writeUInt16LE(0, 36)
|
|
93
|
+
central.writeUInt32LE(0, 38)
|
|
94
|
+
central.writeUInt32LE(offset, 42)
|
|
95
|
+
centralParts.push(central, nameBytes)
|
|
96
|
+
|
|
97
|
+
offset += local.length + nameBytes.length + data.length
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
const centralBuffer = Buffer.concat(centralParts)
|
|
101
|
+
const eocd = Buffer.alloc(22)
|
|
102
|
+
eocd.writeUInt32LE(0x06054b50, 0)
|
|
103
|
+
eocd.writeUInt16LE(0, 4)
|
|
104
|
+
eocd.writeUInt16LE(0, 6)
|
|
105
|
+
eocd.writeUInt16LE(files.length, 8)
|
|
106
|
+
eocd.writeUInt16LE(files.length, 10)
|
|
107
|
+
eocd.writeUInt32LE(centralBuffer.length, 12)
|
|
108
|
+
eocd.writeUInt32LE(offset, 16)
|
|
109
|
+
eocd.writeUInt16LE(0, 20)
|
|
110
|
+
|
|
111
|
+
fs.mkdirSync(path.dirname(outFile), { recursive: true })
|
|
112
|
+
fs.writeFileSync(outFile, Buffer.concat([...localParts, centralBuffer, eocd]))
|
|
113
|
+
return files
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/* ------------------------------------------------------------------ 读取 */
|
|
117
|
+
|
|
118
|
+
function locateEocd(data, label) {
|
|
119
|
+
for (let i = data.length - 22; i >= 0; i--) {
|
|
120
|
+
if (data.readUInt32LE(i) === 0x06054b50) return i
|
|
121
|
+
}
|
|
122
|
+
throw new Error(`不是有效的 ZIP: ${label}`)
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* 列出 ZIP 全部条目名。
|
|
127
|
+
* @param {Buffer|string} input - ZIP 字节或文件路径
|
|
128
|
+
*/
|
|
129
|
+
export function listZipEntryNames(input, label = 'zip') {
|
|
130
|
+
const data = Buffer.isBuffer(input) ? input : fs.readFileSync(input)
|
|
131
|
+
const eocd = locateEocd(data, label)
|
|
132
|
+
const count = data.readUInt16LE(eocd + 10)
|
|
133
|
+
let offset = data.readUInt32LE(eocd + 16)
|
|
134
|
+
const names = []
|
|
135
|
+
for (let i = 0; i < count; i++) {
|
|
136
|
+
if (data.readUInt32LE(offset) !== 0x02014b50) throw new Error(`ZIP 中央目录损坏: ${label}`)
|
|
137
|
+
const nameLength = data.readUInt16LE(offset + 28)
|
|
138
|
+
const extraLength = data.readUInt16LE(offset + 30)
|
|
139
|
+
const commentLength = data.readUInt16LE(offset + 32)
|
|
140
|
+
names.push(data.subarray(offset + 46, offset + 46 + nameLength).toString('utf8'))
|
|
141
|
+
offset += 46 + nameLength + extraLength + commentLength
|
|
142
|
+
}
|
|
143
|
+
return names
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* 从 ZIP 读单个条目的原始字节;不存在返回 undefined。
|
|
148
|
+
* @param {Buffer|string} input - ZIP 字节或文件路径
|
|
149
|
+
*/
|
|
150
|
+
export function readZipEntryBuffer(input, entryName, label = 'zip') {
|
|
151
|
+
const data = Buffer.isBuffer(input) ? input : fs.readFileSync(input)
|
|
152
|
+
const eocd = locateEocd(data, label)
|
|
153
|
+
const count = data.readUInt16LE(eocd + 10)
|
|
154
|
+
let offset = data.readUInt32LE(eocd + 16)
|
|
155
|
+
for (let i = 0; i < count; i++) {
|
|
156
|
+
if (data.readUInt32LE(offset) !== 0x02014b50) throw new Error(`ZIP 中央目录损坏: ${label}`)
|
|
157
|
+
const method = data.readUInt16LE(offset + 10)
|
|
158
|
+
const compSize = data.readUInt32LE(offset + 20)
|
|
159
|
+
const nameLength = data.readUInt16LE(offset + 28)
|
|
160
|
+
const extraLength = data.readUInt16LE(offset + 30)
|
|
161
|
+
const commentLength = data.readUInt16LE(offset + 32)
|
|
162
|
+
const localOffset = data.readUInt32LE(offset + 42)
|
|
163
|
+
const name = data.subarray(offset + 46, offset + 46 + nameLength).toString('utf8')
|
|
164
|
+
if (name === entryName) {
|
|
165
|
+
const localNameLength = data.readUInt16LE(localOffset + 26)
|
|
166
|
+
const localExtraLength = data.readUInt16LE(localOffset + 28)
|
|
167
|
+
const start = localOffset + 30 + localNameLength + localExtraLength
|
|
168
|
+
const body = data.subarray(start, start + compSize)
|
|
169
|
+
if (method === 0) return body
|
|
170
|
+
if (method === 8) return zlib.inflateRawSync(body)
|
|
171
|
+
throw new Error(`不支持的 ZIP 压缩方式 ${method}: ${label}`)
|
|
172
|
+
}
|
|
173
|
+
offset += 46 + nameLength + extraLength + commentLength
|
|
174
|
+
}
|
|
175
|
+
return undefined
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/** 读取 ZIP 中央目录条目名(文件路径版;打包自检/测试用)。 */
|
|
179
|
+
export function listZipEntries(file) {
|
|
180
|
+
return listZipEntryNames(fs.readFileSync(file), file)
|
|
181
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,61 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@capyclaw/suite-kit",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.2.1",
|
|
4
|
+
"description": "DSH suite/domain 标准件(双重身份:构建期库 + 运行时库,暂不拆包,见 README「双重身份」)。构建期:manifest 校验、compose、profile 生成、domain 解析、artifact 打包;运行时:统一能力描述符、编排闸/授权、嵌入握手与 surface 挂载。",
|
|
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
|
+
"./host": "./src/host.mjs",
|
|
17
|
+
"./client/embed.js": "./client/embed.js",
|
|
18
|
+
"./profile": "./src/profile.mjs",
|
|
19
|
+
"./artifacts": "./src/artifacts.mjs",
|
|
20
|
+
"./schema": "./suite.schema.json",
|
|
21
|
+
"./package.json": "./package.json",
|
|
22
|
+
"./domain-schema": "./domain.schema.json"
|
|
23
|
+
},
|
|
24
|
+
"bin": {
|
|
25
|
+
"dsh-suite": "./bin/dsh-suite.mjs",
|
|
26
|
+
"dsh-domain": "./bin/dsh-domain.mjs"
|
|
27
|
+
},
|
|
28
|
+
"scripts": {
|
|
29
|
+
"test": "node --test test/*.test.mjs"
|
|
30
|
+
},
|
|
31
|
+
"files": [
|
|
32
|
+
"README.md",
|
|
33
|
+
"bin/",
|
|
34
|
+
"client/",
|
|
35
|
+
"domain.schema.json",
|
|
36
|
+
"src/",
|
|
37
|
+
"suite.schema.json"
|
|
38
|
+
],
|
|
39
|
+
"engines": {
|
|
40
|
+
"node": ">=18"
|
|
41
|
+
},
|
|
42
|
+
"dependencies": {
|
|
43
|
+
"@capyclaw/plugin-kit": "^0.2.6"
|
|
44
|
+
},
|
|
45
|
+
"license": "MIT",
|
|
46
|
+
"publishConfig": {
|
|
47
|
+
"access": "public"
|
|
48
|
+
},
|
|
49
|
+
"repository": {
|
|
50
|
+
"type": "git",
|
|
51
|
+
"url": "git+https://github.com/Maple496/capyclaw.git",
|
|
52
|
+
"directory": "C/packages/capyclaw-suite-kit"
|
|
53
|
+
},
|
|
54
|
+
"bugs": {
|
|
55
|
+
"url": "https://github.com/Maple496/capyclaw/issues"
|
|
56
|
+
},
|
|
57
|
+
"homepage": "https://github.com/Maple496/capyclaw/tree/main/C/packages/capyclaw-suite-kit#readme",
|
|
58
|
+
"bundledDependencies": [
|
|
59
|
+
"@capyclaw/plugin-kit"
|
|
60
|
+
]
|
|
61
|
+
}
|
|
@@ -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
|
+
}
|