@capyclaw/suite-kit 0.0.0-stage → 0.2.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 +238 -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/package.json +53 -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/index.mjs +10 -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 +112 -0
package/src/manifest.mjs
ADDED
|
@@ -0,0 +1,263 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Suite manifest:读取与静态校验。
|
|
3
|
+
*
|
|
4
|
+
* manifest 是组合的唯一真源;cordis.patch.yml 由 rows 生成,不手写。
|
|
5
|
+
*/
|
|
6
|
+
import { existsSync, readFileSync, realpathSync, statSync } from 'node:fs'
|
|
7
|
+
import { dirname, isAbsolute, join, relative, resolve } from 'node:path'
|
|
8
|
+
|
|
9
|
+
export const SUITE_FILENAMES = ['dsh-suite.json', 'suite.json']
|
|
10
|
+
export const ROLES = new Set(['runtime', 'skills', 'provider', 'ui', 'bridge', 'support'])
|
|
11
|
+
const NAME_RE = /^[a-z0-9]+(?:-[a-z0-9]+)*$/
|
|
12
|
+
const VERSION_RE = /^\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?$/
|
|
13
|
+
const RANGE_RE = /^(?:\^|~|>=|<=|>|<|=)?\s*\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?$/
|
|
14
|
+
|
|
15
|
+
export function isValidRange(range) {
|
|
16
|
+
if (typeof range !== 'string') return false
|
|
17
|
+
const value = range.trim()
|
|
18
|
+
if (value === '*' || value === 'latest') return true
|
|
19
|
+
return RANGE_RE.test(value)
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** 判断 exact 版本是否满足简单 range(^ ~ >= <= > < = 或精确)。 */
|
|
23
|
+
export function versionSatisfies(range, version) {
|
|
24
|
+
if (!isValidRange(range) || !VERSION_RE.test(String(version || ''))) return false
|
|
25
|
+
const raw = String(range).trim()
|
|
26
|
+
if (raw === '*' || raw === 'latest') return true
|
|
27
|
+
const match = /^(\^|~|>=|<=|>|<|=)?\s*(\d+)\.(\d+)\.(\d+)/.exec(raw)
|
|
28
|
+
if (!match) return false
|
|
29
|
+
const [, op = '=', rawMajor, rawMinor, rawPatch] = match
|
|
30
|
+
const major = Number(rawMajor)
|
|
31
|
+
const minor = Number(rawMinor)
|
|
32
|
+
const patch = Number(rawPatch)
|
|
33
|
+
const [vm, vn, vp] = String(version).split('.').map(Number)
|
|
34
|
+
const cmp = (vm - major) || (vn - minor) || (vp - patch)
|
|
35
|
+
if (op === '^') return cmp >= 0 && vm === major
|
|
36
|
+
if (op === '~') return cmp >= 0 && vm === major && vn === minor
|
|
37
|
+
if (op === '>=') return cmp >= 0
|
|
38
|
+
if (op === '<=') return cmp <= 0
|
|
39
|
+
if (op === '>') return cmp > 0
|
|
40
|
+
if (op === '<') return cmp < 0
|
|
41
|
+
return cmp === 0
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export function readJson(file) {
|
|
45
|
+
return JSON.parse(readFileSync(file, 'utf8'))
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** 从文件或目录定位 suite manifest。 */
|
|
49
|
+
export function findSuiteManifest(input) {
|
|
50
|
+
let abs = resolve(input)
|
|
51
|
+
/* junction/symlink 安装时 components 的相对路径必须以真实目录为基准,
|
|
52
|
+
* 否则 ../../../../packages/* 会解析到 profile 的 node_modules 外面。 */
|
|
53
|
+
try { abs = realpathSync(abs) } catch { /* 不存在时保留原路径,下面给出准确报错 */ }
|
|
54
|
+
if (!existsSync(abs)) throw new Error(`suite 路径不存在: ${abs}`)
|
|
55
|
+
if (statSync(abs).isFile()) return abs
|
|
56
|
+
for (const name of SUITE_FILENAMES) {
|
|
57
|
+
const file = join(abs, name)
|
|
58
|
+
if (existsSync(file)) return file
|
|
59
|
+
}
|
|
60
|
+
throw new Error(`目录里没有 ${SUITE_FILENAMES.join(' / ')}: ${abs}`)
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export function loadSuiteManifest(input, options = {}) {
|
|
64
|
+
const file = findSuiteManifest(input)
|
|
65
|
+
const dir = dirname(file)
|
|
66
|
+
const raw = readJson(file)
|
|
67
|
+
const components = Array.isArray(raw.components) ? raw.components : []
|
|
68
|
+
if (options.expand === false || !components.length) {
|
|
69
|
+
return { file, dir, manifest: raw }
|
|
70
|
+
}
|
|
71
|
+
const seen = options.seen || new Set()
|
|
72
|
+
if (seen.has(file)) throw new Error(`suite components 循环引用: ${file}`)
|
|
73
|
+
seen.add(file)
|
|
74
|
+
const manifest = {
|
|
75
|
+
...raw,
|
|
76
|
+
packages: { ...(raw.packages || {}) },
|
|
77
|
+
rows: [],
|
|
78
|
+
checks: [...(raw.checks || [])],
|
|
79
|
+
}
|
|
80
|
+
const rawRows = [...(raw.rows || [])]
|
|
81
|
+
for (const component of components) {
|
|
82
|
+
if (typeof component !== 'string' || !component) throw new Error(`${file}: components 必须是字符串数组`)
|
|
83
|
+
const loaded = loadSuiteManifest(resolve(dir, component), { seen })
|
|
84
|
+
for (const [name, entry] of Object.entries(loaded.manifest.packages || {})) {
|
|
85
|
+
if (manifest.packages[name]) {
|
|
86
|
+
if (manifest.packages[name].version !== entry.version || manifest.packages[name].role !== entry.role) {
|
|
87
|
+
throw new Error(`suite components 包冲突: ${name}(${manifest.name})`)
|
|
88
|
+
}
|
|
89
|
+
continue
|
|
90
|
+
}
|
|
91
|
+
const path = entry.path ? relative(dir, resolve(loaded.dir, entry.path)) : entry.path
|
|
92
|
+
manifest.packages[name] = { ...entry, path }
|
|
93
|
+
}
|
|
94
|
+
manifest.rows.push(...(loaded.manifest.rows || []))
|
|
95
|
+
manifest.checks.push(...(loaded.manifest.checks || []))
|
|
96
|
+
}
|
|
97
|
+
/* 组件行必须先于本 suite 行:功能插件在 apply 时需要 ctx.* 已就绪。 */
|
|
98
|
+
manifest.rows.push(...rawRows)
|
|
99
|
+
delete manifest.components
|
|
100
|
+
return { file, dir, manifest }
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** 取 semver range 的 major;无法判断时返回 null。 */
|
|
104
|
+
export function majorOf(range) {
|
|
105
|
+
if (typeof range !== 'string') return null
|
|
106
|
+
if (range.trim() === '*' || range.trim() === 'latest') return null
|
|
107
|
+
const match = /(?:^|[^\d])(\d+)(?:\.\d+)?/.exec(range)
|
|
108
|
+
return match ? Number(match[1]) : null
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
export function validateSuiteManifest(manifest, options = {}) {
|
|
112
|
+
const errors = []
|
|
113
|
+
const warnings = []
|
|
114
|
+
const add = (list, message) => list.push(message)
|
|
115
|
+
const baseDir = options.baseDir || null
|
|
116
|
+
|
|
117
|
+
if (!manifest || typeof manifest !== 'object' || Array.isArray(manifest)) {
|
|
118
|
+
return { ok: false, errors: ['manifest 必须是 object'], warnings }
|
|
119
|
+
}
|
|
120
|
+
if (!NAME_RE.test(manifest.name || '')) add(errors, 'name 必须是 kebab-case')
|
|
121
|
+
if (!VERSION_RE.test(manifest.version || '')) add(errors, 'version 必须是 semver(如 2.0.0)')
|
|
122
|
+
if (manifest.title != null && typeof manifest.title !== 'string') add(errors, 'title 必须是字符串')
|
|
123
|
+
if (manifest.description != null && typeof manifest.description !== 'string') add(errors, 'description 必须是字符串')
|
|
124
|
+
if (manifest.components != null) {
|
|
125
|
+
if (!Array.isArray(manifest.components) || manifest.components.some((item) => typeof item !== 'string' || !item)) {
|
|
126
|
+
add(errors, 'components 必须是字符串数组')
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
const packages = manifest.packages
|
|
131
|
+
if (!packages || typeof packages !== 'object' || Array.isArray(packages) || Object.keys(packages).length === 0) {
|
|
132
|
+
add(errors, 'packages 必须是非空 object')
|
|
133
|
+
} else {
|
|
134
|
+
for (const [name, pkg] of Object.entries(packages)) {
|
|
135
|
+
if (!name || typeof name !== 'string') add(errors, 'packages 的键必须是包名')
|
|
136
|
+
if (!pkg || typeof pkg !== 'object' || Array.isArray(pkg)) {
|
|
137
|
+
add(errors, `packages.${name} 必须是 object`)
|
|
138
|
+
continue
|
|
139
|
+
}
|
|
140
|
+
if (!ROLES.has(pkg.role)) add(errors, `packages.${name}.role 非法: ${pkg.role}`)
|
|
141
|
+
if (typeof pkg.version !== 'string' || !pkg.version) add(errors, `packages.${name}.version 必须是非空字符串`)
|
|
142
|
+
else if (!isValidRange(pkg.version)) add(errors, `packages.${name}.version 不是合法 semver range: ${pkg.version}`)
|
|
143
|
+
if (pkg.path != null && (typeof pkg.path !== 'string' || !pkg.path)) add(errors, `packages.${name}.path 必须是非空字符串`)
|
|
144
|
+
if (baseDir && typeof pkg.path === 'string' && pkg.path) {
|
|
145
|
+
const dir = resolve(baseDir, pkg.path)
|
|
146
|
+
if (!existsSync(join(dir, 'package.json'))) {
|
|
147
|
+
add(errors, `packages.${name}.path 指向的目录没有 package.json: ${dir}`)
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
if (pkg.required != null && typeof pkg.required !== 'boolean') add(errors, `packages.${name}.required 必须是 boolean`)
|
|
151
|
+
if (pkg.requires != null) {
|
|
152
|
+
if (typeof pkg.requires !== 'object' || Array.isArray(pkg.requires)) add(errors, `packages.${name}.requires 必须是 object`)
|
|
153
|
+
else for (const [dep, range] of Object.entries(pkg.requires)) {
|
|
154
|
+
if (dep === 'capabilities') {
|
|
155
|
+
if (typeof range !== 'object' || Array.isArray(range)) {
|
|
156
|
+
add(errors, `packages.${name}.requires.capabilities 必须是 object`)
|
|
157
|
+
} else for (const [cap, capRange] of Object.entries(range)) {
|
|
158
|
+
if (typeof capRange !== 'string' || !capRange) add(errors, `packages.${name}.requires.capabilities.${cap} 必须是非空字符串`)
|
|
159
|
+
else if (!isValidRange(capRange)) add(errors, `packages.${name}.requires.capabilities.${cap} 不是合法 semver range: ${capRange}`)
|
|
160
|
+
}
|
|
161
|
+
} else if (typeof range !== 'string' || !range) {
|
|
162
|
+
add(errors, `packages.${name}.requires.${dep} 必须是非空字符串`)
|
|
163
|
+
} else if (!isValidRange(range)) {
|
|
164
|
+
add(errors, `packages.${name}.requires.${dep} 不是合法 semver range: ${range}`)
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
if (pkg.provides != null) {
|
|
169
|
+
if (typeof pkg.provides !== 'object' || Array.isArray(pkg.provides)) {
|
|
170
|
+
add(errors, `packages.${name}.provides 必须是 object`)
|
|
171
|
+
} else {
|
|
172
|
+
for (const key of ['tools', 'skills', 'services']) {
|
|
173
|
+
if (pkg.provides[key] != null && (!Array.isArray(pkg.provides[key]) || pkg.provides[key].some((v) => typeof v !== 'string'))) {
|
|
174
|
+
add(errors, `packages.${name}.provides.${key} 必须是 string[]`)
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
if (pkg.provides.capabilities != null) {
|
|
178
|
+
if (typeof pkg.provides.capabilities !== 'object' || Array.isArray(pkg.provides.capabilities)) {
|
|
179
|
+
add(errors, `packages.${name}.provides.capabilities 必须是 object`)
|
|
180
|
+
} else for (const [cap, version] of Object.entries(pkg.provides.capabilities)) {
|
|
181
|
+
if (typeof version !== 'string' || !version) add(errors, `packages.${name}.provides.capabilities.${cap} 必须是非空字符串`)
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
if (pkg.provides.ports != null && (!Array.isArray(pkg.provides.ports) || pkg.provides.ports.some((v) => !Number.isInteger(v) || v < 1 || v > 65535))) {
|
|
185
|
+
add(errors, `packages.${name}.provides.ports 必须是 1..65535 的整数数组`)
|
|
186
|
+
} else if (Array.isArray(pkg.provides.ports)) {
|
|
187
|
+
const seenPorts = new Set()
|
|
188
|
+
for (const port of pkg.provides.ports) {
|
|
189
|
+
if (seenPorts.has(port)) add(errors, `packages.${name}.provides.ports 重复: ${port}`)
|
|
190
|
+
seenPorts.add(port)
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
const rows = manifest.rows
|
|
199
|
+
if (!Array.isArray(rows) || rows.length === 0) {
|
|
200
|
+
add(errors, 'rows 必须是非空数组')
|
|
201
|
+
} else {
|
|
202
|
+
const ids = new Set()
|
|
203
|
+
for (const row of rows) {
|
|
204
|
+
if (!row || typeof row !== 'object' || Array.isArray(row)) {
|
|
205
|
+
add(errors, 'rows 的每一项必须是 object')
|
|
206
|
+
continue
|
|
207
|
+
}
|
|
208
|
+
if (!NAME_RE.test(row.id || '')) add(errors, `row.id 非法: ${row.id}`)
|
|
209
|
+
if (ids.has(row.id)) add(errors, `row.id 重复: ${row.id}`)
|
|
210
|
+
ids.add(row.id)
|
|
211
|
+
if (typeof row.name !== 'string' || !row.name) add(errors, `row ${row.id} 缺少 name`)
|
|
212
|
+
if (row.disabled != null && typeof row.disabled !== 'boolean') add(errors, `row ${row.id}.disabled 必须是 boolean`)
|
|
213
|
+
if (row.config != null && (typeof row.config !== 'object' || Array.isArray(row.config))) add(errors, `row ${row.id}.config 必须是 object`)
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
if (manifest.checks != null) {
|
|
218
|
+
if (!Array.isArray(manifest.checks)) add(errors, 'checks 必须是数组')
|
|
219
|
+
else {
|
|
220
|
+
const ids = new Set()
|
|
221
|
+
for (const check of manifest.checks) {
|
|
222
|
+
if (!check || typeof check !== 'object' || Array.isArray(check)) {
|
|
223
|
+
add(errors, 'checks 的每一项必须是 object')
|
|
224
|
+
continue
|
|
225
|
+
}
|
|
226
|
+
if (!check.id || ids.has(check.id)) add(errors, `check.id 缺失或重复: ${check.id}`)
|
|
227
|
+
ids.add(check.id)
|
|
228
|
+
if (check.type === 'http') {
|
|
229
|
+
if (typeof check.url !== 'string' || !check.url) add(errors, `check ${check.id} 缺少 url`)
|
|
230
|
+
} else if (check.type === 'contract') {
|
|
231
|
+
if (typeof check.url !== 'string' || !check.url) add(errors, `check ${check.id} 缺少 url`)
|
|
232
|
+
if (typeof check.tool !== 'string' || !check.tool) add(errors, `check ${check.id} 缺少 tool`)
|
|
233
|
+
if (!Number.isInteger(check.major) || check.major < 0) add(errors, `check ${check.id}.major 必须是非负整数`)
|
|
234
|
+
} else {
|
|
235
|
+
add(errors, `check ${check.id} 的 type 非法: ${check.type}`)
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
/* 包内 rows 的 name 不一定在 packages 里(可以是官方 mcp-client),只给提示。 */
|
|
242
|
+
if (packages && rows) {
|
|
243
|
+
for (const row of rows) {
|
|
244
|
+
if (row && typeof row.name === 'string' && !packages[row.name]) {
|
|
245
|
+
warnings.push(`row ${row.id} 引用外部包 ${row.name}(不在 packages 里,按已安装处理)`)
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
return { ok: errors.length === 0, errors, warnings }
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
export function assertValidSuiteManifest(manifest, label = 'suite') {
|
|
254
|
+
const result = validateSuiteManifest(manifest)
|
|
255
|
+
if (!result.ok) throw new Error(`${label} 校验失败:\n- ${result.errors.join('\n- ')}`)
|
|
256
|
+
return result
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
/** 解析 packages.<name>.path 为绝对目录。 */
|
|
260
|
+
export function packageDirOf(manifestDir, name, entry) {
|
|
261
|
+
if (!entry || typeof entry.path !== 'string') return null
|
|
262
|
+
return isAbsolute(entry.path) ? entry.path : resolve(manifestDir, entry.path)
|
|
263
|
+
}
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 编排闸 / 授权 Mandate / 统一事件(docs/suite-interop-standard.md §C)。
|
|
3
|
+
*
|
|
4
|
+
* 编排器是「平级作品」:它只是一个声明了调度类能力的套件,结构上与 ppt/browser 平等,
|
|
5
|
+
* 可替换(自研或 dsh-superagent 参考实现)、可嵌套、可被嵌入。套件不认识编排器。
|
|
6
|
+
* 本模块是给任意编排器复用的纯函数原语,不 import 任何套件。
|
|
7
|
+
*
|
|
8
|
+
* 参考吸收:deepseek-ai/dsh-superagent-prototype 的 Ticket + 不可变 Mandate 授权、
|
|
9
|
+
* 「只在人必须行动时说话」、外部看门狗。这里是其契约层的最小实现。
|
|
10
|
+
*/
|
|
11
|
+
import { randomBytes } from 'node:crypto'
|
|
12
|
+
|
|
13
|
+
export const EVENT_KINDS = Object.freeze(['progress', 'artifact', 'confirm', 'login', 'result', 'error'])
|
|
14
|
+
|
|
15
|
+
const id = (prefix) => `${prefix}_${randomBytes(10).toString('hex')}`
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* 闸评估:能力执行前的唯一入口。
|
|
19
|
+
* ctx: { authenticated?, mandate?, now?, sessionId?, stepId?, artifactIds?, preview? }
|
|
20
|
+
* 返回 {decision:'proceed'|'confirm'|'login', ...}:
|
|
21
|
+
* - auth 必需且未确认登录 → login(会话内暂停该步,登录后续跑,不崩链)
|
|
22
|
+
* - confirm/risk:high 且无有效 Mandate → confirm(强制停下等用户,不可悄悄发布)
|
|
23
|
+
* - 其余 → proceed
|
|
24
|
+
*/
|
|
25
|
+
export function evaluateGate(descriptor, ctx = {}) {
|
|
26
|
+
const now = ctx.now ?? Date.now()
|
|
27
|
+
if (!descriptor || typeof descriptor !== 'object') {
|
|
28
|
+
return { decision: 'error', reason: '缺少能力描述符' }
|
|
29
|
+
}
|
|
30
|
+
if (descriptor.auth && ctx.authenticated !== true) {
|
|
31
|
+
return { decision: 'login', auth: descriptor.auth, capability: descriptor.name, reason: `需要登录态 ${descriptor.auth}(未确认登录,暂停该步)` }
|
|
32
|
+
}
|
|
33
|
+
const needsConfirm = descriptor.confirm === true || descriptor.risk === 'high'
|
|
34
|
+
if (needsConfirm) {
|
|
35
|
+
if (ctx.mandate && mandateCovers(ctx.mandate, descriptor, { now, sessionId: ctx.sessionId, artifactIds: ctx.artifactIds })) {
|
|
36
|
+
return { decision: 'proceed', mandateId: ctx.mandate.mandateId, capability: descriptor.name }
|
|
37
|
+
}
|
|
38
|
+
return {
|
|
39
|
+
decision: 'confirm',
|
|
40
|
+
capability: descriptor.name,
|
|
41
|
+
gate: Object.freeze({
|
|
42
|
+
gateId: id('gate'), kind: 'confirm', capability: descriptor.name,
|
|
43
|
+
risk: descriptor.risk ?? 'low', sessionId: ctx.sessionId ?? null, stepId: ctx.stepId ?? null,
|
|
44
|
+
preview: ctx.preview ?? null, createdAt: now,
|
|
45
|
+
}),
|
|
46
|
+
reason: '不可逆/高风险动作:执行前强制用户确认',
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
return { decision: 'proceed', capability: descriptor.name }
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* 不可变授权(用户在确认闸上「接受」后签发)。一旦签发不可修改;
|
|
54
|
+
* 过期/换会话/换产物即失效。scope.artifactIds 非空时把授权钉死在具体产物上。
|
|
55
|
+
*/
|
|
56
|
+
export function createMandate({ capability, grantedBy, sessionId, scope = {}, ttlMs = null, now = Date.now() } = {}) {
|
|
57
|
+
if (typeof capability !== 'string' || !capability) throw new Error('createMandate: capability 必填')
|
|
58
|
+
if (typeof grantedBy !== 'string' || !grantedBy) throw new Error('createMandate: grantedBy 必填(谁授的权)')
|
|
59
|
+
if (typeof sessionId !== 'string' || !sessionId) throw new Error('createMandate: sessionId 必填(授权不跨会话)')
|
|
60
|
+
if (ttlMs != null && (!Number.isFinite(ttlMs) || ttlMs <= 0)) throw new Error('createMandate: ttlMs 必须为正数或 null')
|
|
61
|
+
return Object.freeze({
|
|
62
|
+
mandateId: id('mdt'),
|
|
63
|
+
capability,
|
|
64
|
+
grantedBy,
|
|
65
|
+
sessionId,
|
|
66
|
+
scope: Object.freeze({ ...scope }),
|
|
67
|
+
grantedAt: now,
|
|
68
|
+
expiresAt: ttlMs == null ? null : now + ttlMs,
|
|
69
|
+
})
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** Mandate 是否覆盖本次调用:能力对得上 + 同会话 + 未过期 + 产物在授权范围内。 */
|
|
73
|
+
export function mandateCovers(mandate, descriptor, { now = Date.now(), sessionId = null, artifactIds = [] } = {}) {
|
|
74
|
+
if (!mandate || !descriptor) return false
|
|
75
|
+
if (mandate.capability !== descriptor.name && mandate.capability !== descriptor.capability) return false
|
|
76
|
+
if (mandate.sessionId && sessionId && mandate.sessionId !== sessionId) return false
|
|
77
|
+
if (mandate.expiresAt != null && now >= mandate.expiresAt) return false
|
|
78
|
+
const scoped = mandate.scope?.artifactIds
|
|
79
|
+
if (Array.isArray(scoped) && scoped.length > 0) {
|
|
80
|
+
if (!Array.isArray(artifactIds) || artifactIds.length === 0) return false
|
|
81
|
+
if (!artifactIds.every((a) => scoped.includes(a))) return false
|
|
82
|
+
}
|
|
83
|
+
return true
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** 统一事件流(§C.5):接口步骤与浏览器步骤同构,都发这种事件到会话视图。 */
|
|
87
|
+
export function createEvent({ kind, sessionId = null, stepId = null, capability = null, payload = null, now = Date.now() } = {}) {
|
|
88
|
+
if (!EVENT_KINDS.includes(kind)) throw new Error(`createEvent: kind 必须是 ${EVENT_KINDS.join('|')},收到: ${kind}`)
|
|
89
|
+
return Object.freeze({ eventId: id('evt'), kind, sessionId, stepId, capability, payload, at: now })
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** 步骤模型(§C.2):能力调用 + 输入/输出引用 + 状态。两种操控模式同构。 */
|
|
93
|
+
export function createStep({ capability, controlMode, inputs = [], outputs = [], sessionId = null, status = 'pending' } = {}) {
|
|
94
|
+
if (typeof capability !== 'string' || !capability) throw new Error('createStep: capability 必填')
|
|
95
|
+
return Object.freeze({
|
|
96
|
+
stepId: id('step'), capability, controlMode: controlMode ?? null,
|
|
97
|
+
inputs: Object.freeze([...inputs]), outputs: Object.freeze([...outputs]),
|
|
98
|
+
sessionId, status,
|
|
99
|
+
})
|
|
100
|
+
}
|