dsh-workflow-execute 0.0.0-stage → 1.0.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/LICENSE +21 -0
- package/README.md +36 -2
- package/client/bundle.js +3647 -0
- package/client/index.js +3360 -0
- package/cordis.patch.yml +13 -0
- package/package.json +62 -4
- package/scripts/build-client.mjs +85 -0
- package/scripts/fix-tests.mjs +71 -0
- package/scripts/make-yaml-bundle.mjs +21 -0
- package/scripts/patch-client-comp.mjs +196 -0
- package/scripts/patch-client-exec.mjs +147 -0
- package/scripts/patch-client-i18n.mjs +43 -0
- package/scripts/patch-flow.mjs +205 -0
- package/scripts/yaml-bundle.js +139 -0
- package/src/archive.js +81 -0
- package/src/artifacts.js +263 -0
- package/src/execution.js +746 -0
- package/src/index.js +212 -0
- package/src/prompt.js +24 -0
- package/src/routes.js +324 -0
- package/src/runs.js +237 -0
- package/src/store.js +482 -0
- package/src/tools.js +197 -0
- package/src/validate.js +258 -0
- package/src/zip.js +85 -0
package/src/store.js
ADDED
|
@@ -0,0 +1,482 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* dsh-workflow-execute — 工作流库存储(host 侧)。
|
|
5
|
+
*
|
|
6
|
+
* 两个根目录:我的库(可写)+ 内置库(只读)。递归扫描 *.yaml / *.yml(深度可配,
|
|
7
|
+
* 跳过点目录含 .trash),逐文件解析 + 校验,建内存索引。fs.watch 监听两根,
|
|
8
|
+
* 200ms 防抖后重扫;索引指纹(id+hash 集合)变化才 revision++ 并广播——
|
|
9
|
+
* 客户端收到变更帧后全量 refetch(taskboard 同款数据流)。
|
|
10
|
+
*
|
|
11
|
+
* 写路径只对我的库开放:原子写(临时文件 + rename)、baseHash 乐观锁、
|
|
12
|
+
* 删除移入 .trash。所有相对路径经 safeRel 白名单校验,禁 ..、绝对路径、点目录。
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
const fs = require('node:fs')
|
|
16
|
+
const fsP = require('node:fs/promises')
|
|
17
|
+
const { createHash, randomUUID } = require('node:crypto')
|
|
18
|
+
const path = require('node:path')
|
|
19
|
+
const YAML = require('yaml')
|
|
20
|
+
const { parseProcessYaml, validateProcess, extractMeta } = require('./validate.js')
|
|
21
|
+
|
|
22
|
+
const YAML_EXT = new Set(['.yaml', '.yml'])
|
|
23
|
+
const TRASH_DIR = '.trash'
|
|
24
|
+
const DEFAULT_MAX_DEPTH = 3
|
|
25
|
+
const DEFAULT_MAX_FILE_BYTES = 1024 * 1024
|
|
26
|
+
const WATCH_DEBOUNCE_MS = 200
|
|
27
|
+
// 文件名(不含扩展名):不含路径分隔符/控制字符,不以点开头,≤ 80 字符
|
|
28
|
+
const NAME_RE = /^(?![.])[^\\/\0\r\n]{1,80}$/
|
|
29
|
+
|
|
30
|
+
class StoreError extends Error {
|
|
31
|
+
constructor(code, message, extra) {
|
|
32
|
+
super(message)
|
|
33
|
+
this.code = code
|
|
34
|
+
if (extra) Object.assign(this, extra)
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const sha1 = (buf) => createHash('sha1').update(buf).digest('hex').slice(0, 16)
|
|
39
|
+
const toPosix = (p) => p.split(path.sep).join('/')
|
|
40
|
+
|
|
41
|
+
/** 校验并规范化相对路径(posix 分隔);不合法抛 invalid_path。 */
|
|
42
|
+
function safeRel(rel) {
|
|
43
|
+
const s = String(rel || '').replace(/\\/g, '/').replace(/^\/+/, '')
|
|
44
|
+
if (s === '' || s.includes('\0')) throw new StoreError('invalid_path', '路径不能为空')
|
|
45
|
+
const parts = s.split('/')
|
|
46
|
+
for (const part of parts) {
|
|
47
|
+
if (part === '' || part === '.' || part === '..') throw new StoreError('invalid_path', `非法路径 "${rel}"`)
|
|
48
|
+
if (part.startsWith('.')) throw new StoreError('invalid_path', `路径不能包含点目录/点文件 "${part}"`)
|
|
49
|
+
}
|
|
50
|
+
if (!YAML_EXT.has(path.posix.extname(s).toLowerCase())) throw new StoreError('invalid_path', '只接受 .yaml / .yml 文件')
|
|
51
|
+
return parts.join('/')
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** 校验工作流文件名(不含扩展名)。 */
|
|
55
|
+
function safeName(name) {
|
|
56
|
+
const s = String(name || '').trim()
|
|
57
|
+
if (!NAME_RE.test(s) || s === '..' || s === '.') throw new StoreError('invalid_name', `非法名称 "${name}":不能为空、不能含 / 或 \\、不能以点开头、≤ 80 字符`)
|
|
58
|
+
return s
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** 可选子目录(如 software)。返回 posix 形式或 ''。 */
|
|
62
|
+
function safeDir(dir) {
|
|
63
|
+
const s = String(dir || '').replace(/\\/g, '/').replace(/^\/+|\/+$/g, '')
|
|
64
|
+
if (s === '') return ''
|
|
65
|
+
for (const part of s.split('/')) {
|
|
66
|
+
if (part === '' || part === '.' || part === '..' || part.startsWith('.')) throw new StoreError('invalid_path', `非法目录 "${dir}"`)
|
|
67
|
+
}
|
|
68
|
+
return s
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function parseId(id) {
|
|
72
|
+
const s = String(id || '')
|
|
73
|
+
const idx = s.indexOf(':')
|
|
74
|
+
if (idx <= 0) throw new StoreError('invalid_id', `非法 id "${id}"`)
|
|
75
|
+
const source = s.slice(0, idx)
|
|
76
|
+
if (source !== 'user' && source !== 'bundled') throw new StoreError('invalid_id', `未知来源 "${source}"`)
|
|
77
|
+
return { source, relPath: safeRel(s.slice(idx + 1)) }
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
async function atomicWrite(absPath, content) {
|
|
81
|
+
await fsP.mkdir(path.dirname(absPath), { recursive: true })
|
|
82
|
+
const tmp = `${absPath}.tmp-${process.pid}-${Math.random().toString(36).slice(2, 8)}`
|
|
83
|
+
await fsP.writeFile(tmp, content, 'utf8')
|
|
84
|
+
await fsP.rename(tmp, absPath)
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** 用 Document API 改 process.name / display_name / guid,注释与键序不丢。 */
|
|
88
|
+
function rewriteIdentity(yamlText, { name, displayName, guid }) {
|
|
89
|
+
const doc = YAML.parseDocument(yamlText)
|
|
90
|
+
if (doc.errors.length > 0) return yamlText
|
|
91
|
+
if (!doc.has('process') || !YAML.isMap(doc.get('process', true))) return yamlText
|
|
92
|
+
if (name !== undefined) doc.setIn(['process', 'name'], name)
|
|
93
|
+
if (displayName !== undefined) doc.setIn(['process', 'display_name'], displayName)
|
|
94
|
+
if (guid !== undefined) doc.setIn(['process', 'guid'], guid)
|
|
95
|
+
return doc.toString()
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
class ProcessStore {
|
|
99
|
+
/**
|
|
100
|
+
* @param {object} opts
|
|
101
|
+
* @param {() => {user: string, bundled: string}} opts.roots 当前根目录(可随设置变化)
|
|
102
|
+
* @param {() => {maxDepth?: number}} [opts.options]
|
|
103
|
+
* @param {{info?: Function, warn?: Function}} [opts.logger]
|
|
104
|
+
*/
|
|
105
|
+
constructor(opts) {
|
|
106
|
+
this.rootsFn = opts.roots
|
|
107
|
+
this.optionsFn = opts.options || (() => ({}))
|
|
108
|
+
this.logger = opts.logger || console
|
|
109
|
+
this.maxFileBytes = opts.maxFileBytes || DEFAULT_MAX_FILE_BYTES
|
|
110
|
+
this.revision = 0
|
|
111
|
+
this.index = new Map()
|
|
112
|
+
this.fingerprint = ''
|
|
113
|
+
this.listeners = new Set()
|
|
114
|
+
this.queue = Promise.resolve()
|
|
115
|
+
this.watchers = []
|
|
116
|
+
this.debounceTimer = null
|
|
117
|
+
this.lastError = undefined
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
roots() {
|
|
121
|
+
const r = this.rootsFn()
|
|
122
|
+
return { user: path.resolve(r.user), bundled: path.resolve(r.bundled) }
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
subscribe(fn) {
|
|
126
|
+
this.listeners.add(fn)
|
|
127
|
+
return () => { this.listeners.delete(fn) }
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
emit() {
|
|
131
|
+
for (const fn of this.listeners) {
|
|
132
|
+
try { fn(this.revision) } catch (e) { this.logger.warn && this.logger.warn(`dsh-workflow-execute: listener failed: ${e && e.message}`) }
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* 串行扫描队列:每次 load() 排一个**新**扫描,await 到的一定是「始于调用之后」的
|
|
138
|
+
* 扫描结果——写操作(save/rename/remove/import)之后 await load(),响应返回时索引
|
|
139
|
+
* 必然已含本次变更(在飞合并方案做不到:改名前的扫描看不到新文件,响应会漏条目)。
|
|
140
|
+
*/
|
|
141
|
+
load() {
|
|
142
|
+
const run = this.queue.then(() => this.scan())
|
|
143
|
+
this.queue = run.then(() => {}, () => {})
|
|
144
|
+
return run
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
async scan() {
|
|
148
|
+
const roots = this.roots()
|
|
149
|
+
const { maxDepth = DEFAULT_MAX_DEPTH } = this.optionsFn()
|
|
150
|
+
const next = new Map()
|
|
151
|
+
for (const source of ['user', 'bundled']) {
|
|
152
|
+
const root = roots[source]
|
|
153
|
+
const files = await this.walk(root, '', 0, maxDepth)
|
|
154
|
+
for (const rel of files) {
|
|
155
|
+
const entry = await this.readEntry(source, root, rel)
|
|
156
|
+
if (entry) next.set(entry.id, entry)
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
const fp = [...next.keys()].sort().map((id) => `${id}@${next.get(id).hash}`).join('|')
|
|
160
|
+
const changed = fp !== this.fingerprint
|
|
161
|
+
this.index = next
|
|
162
|
+
this.fingerprint = fp
|
|
163
|
+
this.lastError = undefined
|
|
164
|
+
if (changed || this.revision === 0) {
|
|
165
|
+
this.revision += 1
|
|
166
|
+
this.emit()
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
async walk(root, rel, depth, maxDepth) {
|
|
171
|
+
let dirents
|
|
172
|
+
try { dirents = await fsP.readdir(rel ? path.join(root, rel) : root, { withFileTypes: true }) } catch { return [] }
|
|
173
|
+
const out = []
|
|
174
|
+
for (const d of dirents) {
|
|
175
|
+
if (d.name.startsWith('.')) continue
|
|
176
|
+
const childRel = rel ? `${rel}/${d.name}` : d.name
|
|
177
|
+
if (d.isDirectory()) {
|
|
178
|
+
if (depth + 1 < maxDepth) out.push(...await this.walk(root, childRel, depth + 1, maxDepth))
|
|
179
|
+
} else if (d.isFile() && YAML_EXT.has(path.extname(d.name).toLowerCase())) {
|
|
180
|
+
out.push(childRel)
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
return out.sort()
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
async readEntry(source, root, rel) {
|
|
187
|
+
const abs = path.join(root, ...rel.split('/'))
|
|
188
|
+
let stat
|
|
189
|
+
try { stat = await fsP.stat(abs) } catch { return null }
|
|
190
|
+
if (stat.size > this.maxFileBytes) {
|
|
191
|
+
return this.entryFrom(source, rel, abs, stat, '', { errors: [{ code: 'too_large', message: `文件超过 ${this.maxFileBytes} 字节`, path: '', line: 1 }], warnings: [], meta: extractMeta(undefined) })
|
|
192
|
+
}
|
|
193
|
+
const text = await fsP.readFile(abs, 'utf8')
|
|
194
|
+
const parsed = parseProcessYaml(text)
|
|
195
|
+
const result = validateProcess(parsed)
|
|
196
|
+
return this.entryFrom(source, rel, abs, stat, text, result)
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
entryFrom(source, rel, abs, stat, text, result) {
|
|
200
|
+
const meta = result.meta
|
|
201
|
+
const base = path.posix.basename(rel).replace(/\.ya?ml$/i, '')
|
|
202
|
+
return {
|
|
203
|
+
id: `${source}:${rel}`,
|
|
204
|
+
source,
|
|
205
|
+
relPath: rel,
|
|
206
|
+
absPath: abs,
|
|
207
|
+
fileName: base,
|
|
208
|
+
name: meta.name || base,
|
|
209
|
+
guid: meta.guid,
|
|
210
|
+
display_name: meta.display_name,
|
|
211
|
+
description: meta.description,
|
|
212
|
+
category: meta.category,
|
|
213
|
+
complexity: meta.complexity,
|
|
214
|
+
version: meta.version,
|
|
215
|
+
phaseCount: meta.phaseCount,
|
|
216
|
+
linkCount: meta.linkCount,
|
|
217
|
+
mtime: Math.round(stat.mtimeMs),
|
|
218
|
+
size: stat.size,
|
|
219
|
+
hash: sha1(text),
|
|
220
|
+
errors: result.errors.length,
|
|
221
|
+
warnings: result.warnings.length,
|
|
222
|
+
diagnostics: { errors: result.errors, warnings: result.warnings },
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/** 列表快照(不含 absPath/diagnostics 全文)。 */
|
|
227
|
+
snapshot() {
|
|
228
|
+
const roots = this.roots()
|
|
229
|
+
const processes = [...this.index.values()].map((e) => {
|
|
230
|
+
const { absPath, diagnostics, ...rest } = e
|
|
231
|
+
return rest
|
|
232
|
+
}).sort((a, b) => (a.source === b.source ? a.relPath.localeCompare(b.relPath) : a.source === 'user' ? -1 : 1))
|
|
233
|
+
return { revision: this.revision, roots, processes, lastError: this.lastError }
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
entry(id) {
|
|
237
|
+
const e = this.index.get(String(id))
|
|
238
|
+
if (!e) throw new StoreError('not_found', `工作流不存在:${id}`)
|
|
239
|
+
return e
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/** 按 id / name / guid 找条目(工具用)。 */
|
|
243
|
+
find(ref) {
|
|
244
|
+
const s = String(ref || '')
|
|
245
|
+
if (this.index.has(s)) return this.index.get(s)
|
|
246
|
+
const all = [...this.index.values()]
|
|
247
|
+
return all.find((e) => e.guid && e.guid === s)
|
|
248
|
+
|| all.find((e) => e.source === 'user' && (e.name === s || e.fileName === s))
|
|
249
|
+
|| all.find((e) => e.name === s || e.fileName === s)
|
|
250
|
+
|| all.find((e) => e.display_name === s)
|
|
251
|
+
|| undefined
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
async get(id) {
|
|
255
|
+
const e = this.entry(id)
|
|
256
|
+
let text
|
|
257
|
+
try { text = await fsP.readFile(e.absPath, 'utf8') } catch { throw new StoreError('not_found', `文件已不存在:${e.relPath}`) }
|
|
258
|
+
const hash = sha1(text)
|
|
259
|
+
let entry = e
|
|
260
|
+
if (hash !== e.hash) {
|
|
261
|
+
// 索引落后于磁盘:就地刷新这一条并触发重扫
|
|
262
|
+
const stat = await fsP.stat(e.absPath)
|
|
263
|
+
entry = this.entryFrom(e.source, e.relPath, e.absPath, stat, text, validateProcess(parseProcessYaml(text)))
|
|
264
|
+
this.index.set(entry.id, entry)
|
|
265
|
+
void this.load()
|
|
266
|
+
}
|
|
267
|
+
const parsed = parseProcessYaml(text)
|
|
268
|
+
const { absPath, diagnostics, ...meta } = entry
|
|
269
|
+
return { meta, yaml: text, parsed: parsed.data, diagnostics: entry.diagnostics, hash }
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
async validate(yamlText) {
|
|
273
|
+
const result = validateProcess(parseProcessYaml(String(yamlText ?? '')))
|
|
274
|
+
return { errors: result.errors, warnings: result.warnings, meta: result.meta }
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
userAbs(rel) {
|
|
278
|
+
return path.join(this.roots().user, ...rel.split('/'))
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
async exists(abs) {
|
|
282
|
+
try { await fsP.access(abs); return true } catch { return false }
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
/**
|
|
286
|
+
* 保存到我的库。
|
|
287
|
+
* mode=create:{ name, dir?, yaml } 新文件,重名 409 exists
|
|
288
|
+
* mode=update:{ id, yaml, baseHash? } 已有文件,baseHash 不符 409 conflict
|
|
289
|
+
* mode=copy :{ fromId, name, dir? } 复制(我的/内置 → 我的),换 name + 新 guid
|
|
290
|
+
* 校验有 errors 一律 400 invalid(返回 diagnostics)。
|
|
291
|
+
*/
|
|
292
|
+
async save(input) {
|
|
293
|
+
const mode = input.mode || (input.id ? 'update' : 'create')
|
|
294
|
+
let rel
|
|
295
|
+
let text
|
|
296
|
+
let expectHash
|
|
297
|
+
if (mode === 'update') {
|
|
298
|
+
const { source, relPath } = parseId(input.id)
|
|
299
|
+
if (source !== 'user') throw new StoreError('readonly', '内置库只读,请先「复制到我的库」')
|
|
300
|
+
rel = relPath
|
|
301
|
+
text = String(input.yaml ?? '')
|
|
302
|
+
expectHash = input.baseHash
|
|
303
|
+
} else if (mode === 'create') {
|
|
304
|
+
const name = safeName(input.name)
|
|
305
|
+
const dir = safeDir(input.dir)
|
|
306
|
+
rel = `${dir ? dir + '/' : ''}${name}.yaml`
|
|
307
|
+
text = String(input.yaml ?? '')
|
|
308
|
+
} else if (mode === 'copy') {
|
|
309
|
+
const from = this.entry(input.fromId)
|
|
310
|
+
const name = safeName(input.name)
|
|
311
|
+
const dir = safeDir(input.dir !== undefined ? input.dir : (from.source === 'user' ? path.posix.dirname(from.relPath) : ''))
|
|
312
|
+
rel = `${dir && dir !== '.' ? dir + '/' : ''}${name}.yaml`
|
|
313
|
+
const src = await fsP.readFile(from.absPath, 'utf8')
|
|
314
|
+
text = rewriteIdentity(src, { name, guid: randomUUID() })
|
|
315
|
+
} else {
|
|
316
|
+
throw new StoreError('invalid_input', `未知 mode "${mode}"`)
|
|
317
|
+
}
|
|
318
|
+
const abs = this.userAbs(rel)
|
|
319
|
+
const result = await this.validate(text)
|
|
320
|
+
if (result.errors.length > 0) {
|
|
321
|
+
throw new StoreError('invalid', '工作流校验未通过,未保存', { diagnostics: { errors: result.errors, warnings: result.warnings } })
|
|
322
|
+
}
|
|
323
|
+
if (mode === 'update') {
|
|
324
|
+
let current
|
|
325
|
+
try { current = await fsP.readFile(abs, 'utf8') } catch { throw new StoreError('not_found', `文件已不存在:${rel}`) }
|
|
326
|
+
if (expectHash && sha1(current) !== expectHash) {
|
|
327
|
+
throw new StoreError('conflict', '文件已被外部修改(可能是其他编辑器),请重新加载后再保存', { currentHash: sha1(current) })
|
|
328
|
+
}
|
|
329
|
+
} else if (await this.exists(abs)) {
|
|
330
|
+
throw new StoreError('exists', `已存在同名工作流:${rel}`)
|
|
331
|
+
}
|
|
332
|
+
await atomicWrite(abs, text)
|
|
333
|
+
await this.load()
|
|
334
|
+
const id = `user:${rel}`
|
|
335
|
+
return { id, hash: sha1(text), diagnostics: { errors: [], warnings: result.warnings }, meta: result.meta }
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
/**
|
|
339
|
+
* 重命名 = 改显示名(process.display_name):界面各处(列表 / 详情标题 / 运行名)
|
|
340
|
+
* 展示的都是显示名。文件名与 name 标识保持不变,因此 id 稳定、已发起的运行不受影响。
|
|
341
|
+
*/
|
|
342
|
+
async rename(id, newDisplayName) {
|
|
343
|
+
const { source, relPath } = parseId(id)
|
|
344
|
+
if (source !== 'user') throw new StoreError('readonly', '内置库只读')
|
|
345
|
+
const displayName = String(newDisplayName || '').trim()
|
|
346
|
+
if (displayName === '') throw new StoreError('invalid_input', '显示名不能为空')
|
|
347
|
+
if (displayName.length > 120) throw new StoreError('invalid_input', '显示名过长(≤120 字)')
|
|
348
|
+
const abs = this.userAbs(relPath)
|
|
349
|
+
let text
|
|
350
|
+
try { text = await fsP.readFile(abs, 'utf8') } catch { throw new StoreError('not_found', `文件已不存在:${relPath}`) }
|
|
351
|
+
const next = rewriteIdentity(text, { displayName })
|
|
352
|
+
await atomicWrite(abs, next)
|
|
353
|
+
await this.load()
|
|
354
|
+
return { id, hash: sha1(next) }
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
/** 删除 = 移入 .trash(带时间戳),返回回收站相对路径。 */
|
|
358
|
+
async remove(id) {
|
|
359
|
+
const { source, relPath } = parseId(id)
|
|
360
|
+
if (source !== 'user') throw new StoreError('readonly', '内置库只读')
|
|
361
|
+
const abs = this.userAbs(relPath)
|
|
362
|
+
if (!(await this.exists(abs))) throw new StoreError('not_found', `文件已不存在:${relPath}`)
|
|
363
|
+
const stamp = new Date().toISOString().replace(/[:.]/g, '-')
|
|
364
|
+
const trashRel = `${TRASH_DIR}/${path.posix.basename(relPath).replace(/\.ya?ml$/i, '')}-${stamp}.yaml`
|
|
365
|
+
const trashAbs = path.join(this.roots().user, ...trashRel.split('/'))
|
|
366
|
+
await fsP.mkdir(path.dirname(trashAbs), { recursive: true })
|
|
367
|
+
await fsP.rename(abs, trashAbs)
|
|
368
|
+
await this.load()
|
|
369
|
+
return { trashed: trashRel }
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
/**
|
|
373
|
+
* 批量导入到我的库。files: [{ name, content }];name 可带 .yaml 后缀或子目录。
|
|
374
|
+
* 返回每个文件的结果:created / overwritten / skipped(exists) / invalid。
|
|
375
|
+
*/
|
|
376
|
+
async importFiles(files, { overwrite = false } = {}) {
|
|
377
|
+
if (!Array.isArray(files) || files.length === 0) throw new StoreError('invalid_input', '没有要导入的文件')
|
|
378
|
+
if (files.length > 200) throw new StoreError('invalid_input', '一次最多导入 200 个文件')
|
|
379
|
+
const results = []
|
|
380
|
+
let changed = false
|
|
381
|
+
for (const f of files) {
|
|
382
|
+
const raw = String(f && f.name || '').replace(/\\/g, '/')
|
|
383
|
+
const stripped = raw.replace(/\.ya?ml$/i, '')
|
|
384
|
+
const item = { name: raw }
|
|
385
|
+
try {
|
|
386
|
+
const dir = safeDir(path.posix.dirname(stripped) === '.' ? '' : path.posix.dirname(stripped))
|
|
387
|
+
const base = safeName(path.posix.basename(stripped))
|
|
388
|
+
const rel = `${dir ? dir + '/' : ''}${base}.yaml`
|
|
389
|
+
const text = String(f.content ?? '')
|
|
390
|
+
if (Buffer.byteLength(text, 'utf8') > this.maxFileBytes) throw new StoreError('invalid', '文件过大')
|
|
391
|
+
const result = await this.validate(text)
|
|
392
|
+
if (result.errors.length > 0) {
|
|
393
|
+
results.push({ ...item, status: 'invalid', errors: result.errors.slice(0, 5) })
|
|
394
|
+
continue
|
|
395
|
+
}
|
|
396
|
+
const abs = this.userAbs(rel)
|
|
397
|
+
const existed = await this.exists(abs)
|
|
398
|
+
if (existed && !overwrite) { results.push({ ...item, status: 'exists', id: `user:${rel}` }); continue }
|
|
399
|
+
await atomicWrite(abs, text)
|
|
400
|
+
changed = true
|
|
401
|
+
results.push({ ...item, status: existed ? 'overwritten' : 'created', id: `user:${rel}`, warnings: result.warnings.length })
|
|
402
|
+
} catch (e) {
|
|
403
|
+
results.push({ ...item, status: 'invalid', errors: [{ code: e.code || 'error', message: String(e && e.message || e) }] })
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
if (changed) await this.load()
|
|
407
|
+
return results
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
/** 导出全部:[{ name: 'user/te.yaml', data: Buffer, mtime }] */
|
|
411
|
+
async exportEntries(filter) {
|
|
412
|
+
const out = []
|
|
413
|
+
for (const e of this.index.values()) {
|
|
414
|
+
if (filter && !filter(e)) continue
|
|
415
|
+
try {
|
|
416
|
+
const data = await fsP.readFile(e.absPath)
|
|
417
|
+
out.push({ name: `${e.source}/${e.relPath}`, data, mtime: e.mtime })
|
|
418
|
+
} catch { /* 文件刚被删:跳过 */ }
|
|
419
|
+
}
|
|
420
|
+
return out
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
// ── fs.watch ──
|
|
424
|
+
startWatch() {
|
|
425
|
+
this.stopWatch()
|
|
426
|
+
const roots = this.roots()
|
|
427
|
+
for (const source of ['user', 'bundled']) {
|
|
428
|
+
const root = roots[source]
|
|
429
|
+
try {
|
|
430
|
+
if (!fs.existsSync(root)) {
|
|
431
|
+
if (source === 'user') fs.mkdirSync(root, { recursive: true })
|
|
432
|
+
else continue
|
|
433
|
+
}
|
|
434
|
+
const w = fs.watch(root, { recursive: true, persistent: false }, () => this.scheduleReload())
|
|
435
|
+
w.on('error', () => { /* 目录被删等:下次 reload 时重建 */ })
|
|
436
|
+
this.watchers.push(w)
|
|
437
|
+
} catch (e) {
|
|
438
|
+
this.logger.warn && this.logger.warn(`dsh-workflow-execute: watch ${root} failed: ${e && e.message}`)
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
stopWatch() {
|
|
444
|
+
for (const w of this.watchers) { try { w.close() } catch {} }
|
|
445
|
+
this.watchers = []
|
|
446
|
+
if (this.debounceTimer) { clearTimeout(this.debounceTimer); this.debounceTimer = null }
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
scheduleReload() {
|
|
450
|
+
if (this.debounceTimer) clearTimeout(this.debounceTimer)
|
|
451
|
+
this.debounceTimer = setTimeout(() => { this.debounceTimer = null; void this.load() }, WATCH_DEBOUNCE_MS)
|
|
452
|
+
if (typeof this.debounceTimer.unref === 'function') this.debounceTimer.unref()
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
/** 根目录变化后:重建 watcher + 重扫。 */
|
|
456
|
+
async rerootAndReload() {
|
|
457
|
+
this.startWatch()
|
|
458
|
+
this.fingerprint = '' // 强制 revision++
|
|
459
|
+
await this.load()
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
dispose() {
|
|
463
|
+
this.stopWatch()
|
|
464
|
+
this.listeners.clear()
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
/** 引用库解析:目录名即名字。 */
|
|
469
|
+
async function collectDirNames(dirs) {
|
|
470
|
+
const out = new Set()
|
|
471
|
+
let any = false
|
|
472
|
+
for (const dir of dirs) {
|
|
473
|
+
try {
|
|
474
|
+
const dirents = await fsP.readdir(dir, { withFileTypes: true })
|
|
475
|
+
any = true
|
|
476
|
+
for (const d of dirents) if (d.isDirectory() && !d.name.startsWith('.')) out.add(d.name)
|
|
477
|
+
} catch { /* 库不存在 */ }
|
|
478
|
+
}
|
|
479
|
+
return any ? out : null
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
module.exports = { ProcessStore, StoreError, safeRel, safeName, safeDir, parseId, rewriteIdentity, collectDirNames, sha1, TRASH_DIR }
|
package/src/tools.js
ADDED
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* dsh-workflow-execute — agent 工具(host 侧)。
|
|
5
|
+
*
|
|
6
|
+
* 工具对象形状与 dsh-tools 注册表兼容(taskboard sdk.ts 同款结构,零 @deepseek-ai
|
|
7
|
+
* 运行时依赖):{ name, description, parameters(JSON Schema 子集), output:{schema:{},
|
|
8
|
+
* render}, execute(args, exec) }。ctx.tools.register(tool) 返回 disposer。
|
|
9
|
+
*
|
|
10
|
+
* workflow_list / workflow_get / workflow_validate 只读;workflow_save 只写我的库,
|
|
11
|
+
* 校验不过拒绝,重名且未 overwrite 拒绝。
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
function text(s) { return [{ type: 'text', text: s }] }
|
|
15
|
+
|
|
16
|
+
function argError(msg) { return new Error(`Error: invalid arguments: ${msg}`) }
|
|
17
|
+
|
|
18
|
+
function summaryOf(e) {
|
|
19
|
+
return {
|
|
20
|
+
id: e.id,
|
|
21
|
+
source: e.source,
|
|
22
|
+
name: e.name,
|
|
23
|
+
display_name: e.display_name || undefined,
|
|
24
|
+
category: e.category || undefined,
|
|
25
|
+
complexity: e.complexity || undefined,
|
|
26
|
+
version: e.version || undefined,
|
|
27
|
+
phases: e.phaseCount,
|
|
28
|
+
links: e.linkCount,
|
|
29
|
+
errors: e.errors || 0,
|
|
30
|
+
warnings: e.warnings || 0,
|
|
31
|
+
description: e.description ? e.description.slice(0, 160) : undefined,
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** 结构摘要:阶段 → 环节(id/name/executor/expert/gates/flow),供 agent 不读全文也能按工作流推进。 */
|
|
36
|
+
function outlineOf(parsed) {
|
|
37
|
+
if (!parsed || typeof parsed !== 'object') return []
|
|
38
|
+
const phases = Array.isArray(parsed.phases) ? parsed.phases : []
|
|
39
|
+
return phases.map((ph) => ({
|
|
40
|
+
id: ph && ph.id,
|
|
41
|
+
name: ph && ph.name,
|
|
42
|
+
acceptance_criteria: ph && typeof ph.acceptance_criteria === 'string' ? ph.acceptance_criteria.trim() : undefined,
|
|
43
|
+
links: (ph && Array.isArray(ph.links) ? ph.links : []).map((ln) => ({
|
|
44
|
+
id: ln && ln.id,
|
|
45
|
+
name: ln && ln.name,
|
|
46
|
+
executor: ln && ln.executor || undefined,
|
|
47
|
+
expert: ln && (ln.expert_name || ln.expert) || undefined,
|
|
48
|
+
skills: ln && Array.isArray(ln.skills) && ln.skills.length ? ln.skills : undefined,
|
|
49
|
+
gates: ln && Array.isArray(ln.gates) ? ln.gates.map((g) => (g && g.name) || g) : undefined,
|
|
50
|
+
on_success: ln && ln.on_success || undefined,
|
|
51
|
+
on_gate_fail: ln && ln.on_gate_fail || undefined,
|
|
52
|
+
max_rework: ln && ln.max_rework !== undefined ? ln.max_rework : undefined,
|
|
53
|
+
prompt_head: ln && typeof ln.prompt === 'string' ? ln.prompt.trim().slice(0, 120) : undefined,
|
|
54
|
+
})),
|
|
55
|
+
}))
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* @param toolsService ctx.tools(有 register)
|
|
60
|
+
* @param deps { store }
|
|
61
|
+
* @returns disposers[]
|
|
62
|
+
*/
|
|
63
|
+
function registerTools(toolsService, { store }) {
|
|
64
|
+
const disposers = []
|
|
65
|
+
const register = (tool) => {
|
|
66
|
+
const d = toolsService.register(tool)
|
|
67
|
+
if (typeof d === 'function') disposers.push(d)
|
|
68
|
+
else if (d && typeof d.dispose === 'function') disposers.push(() => d.dispose())
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
register({
|
|
72
|
+
name: 'workflow_list',
|
|
73
|
+
description: 'List 工作流 (workflow templates) available in dsh: user library (writable) and bundled library (read-only). '
|
|
74
|
+
+ 'Returns compact summaries (id, name, display_name, category, complexity, phases, links). '
|
|
75
|
+
+ 'Call this first when the user mentions 工作流 / process template / 按工作流执行.',
|
|
76
|
+
parameters: {
|
|
77
|
+
type: 'object',
|
|
78
|
+
properties: {
|
|
79
|
+
source: { type: 'string', enum: ['user', 'bundled'], description: 'Only this library (default both).' },
|
|
80
|
+
category: { type: 'string', description: 'Filter by process.category (exact).' },
|
|
81
|
+
query: { type: 'string', description: 'Case-insensitive substring over name / display_name / description.' },
|
|
82
|
+
},
|
|
83
|
+
},
|
|
84
|
+
output: {
|
|
85
|
+
schema: {},
|
|
86
|
+
render(_args, value) { return text(JSON.stringify(value, null, 2)) },
|
|
87
|
+
},
|
|
88
|
+
async execute(args) {
|
|
89
|
+
if (store.revision === 0) await store.load()
|
|
90
|
+
const q = typeof args.query === 'string' ? args.query.trim().toLowerCase() : ''
|
|
91
|
+
const rows = store.snapshot().processes.filter((e) =>
|
|
92
|
+
(!args.source || e.source === args.source)
|
|
93
|
+
&& (!args.category || e.category === args.category)
|
|
94
|
+
&& (q === '' || `${e.name} ${e.display_name} ${e.description}`.toLowerCase().includes(q)))
|
|
95
|
+
return { count: rows.length, processes: rows.map(summaryOf) }
|
|
96
|
+
},
|
|
97
|
+
})
|
|
98
|
+
|
|
99
|
+
register({
|
|
100
|
+
name: 'workflow_get',
|
|
101
|
+
description: 'Read one 工作流 by id (e.g. "user:te.yaml"), name, or guid. format=summary (default) returns meta + phase/link outline; '
|
|
102
|
+
+ 'format=yaml returns the full YAML text. Use the outline to drive work phase by phase: one link = one deliverable, '
|
|
103
|
+
+ 'run its gates, and on failure follow on_gate_fail (break / <link id> / goto:<link id>) up to max_rework times.',
|
|
104
|
+
parameters: {
|
|
105
|
+
type: 'object',
|
|
106
|
+
properties: {
|
|
107
|
+
ref: { type: 'string', description: 'id, name, or guid of the process.' },
|
|
108
|
+
format: { type: 'string', enum: ['summary', 'yaml'], description: 'summary (default) or yaml.' },
|
|
109
|
+
},
|
|
110
|
+
required: ['ref'],
|
|
111
|
+
},
|
|
112
|
+
output: {
|
|
113
|
+
schema: {},
|
|
114
|
+
render(args, value) {
|
|
115
|
+
if (args.format === 'yaml' && typeof value.yaml === 'string') return text(value.yaml)
|
|
116
|
+
return text(JSON.stringify(value, null, 2))
|
|
117
|
+
},
|
|
118
|
+
},
|
|
119
|
+
async execute(args) {
|
|
120
|
+
if (typeof args.ref !== 'string' || args.ref.trim() === '') throw argError('ref is required')
|
|
121
|
+
if (store.revision === 0) await store.load()
|
|
122
|
+
const entry = store.find(args.ref.trim())
|
|
123
|
+
if (!entry) throw new Error(`Error: not_found: no process matches "${args.ref}"`)
|
|
124
|
+
const item = await store.get(entry.id)
|
|
125
|
+
if (args.format === 'yaml') return { id: item.meta.id, yaml: item.yaml }
|
|
126
|
+
return {
|
|
127
|
+
...summaryOf(item.meta),
|
|
128
|
+
limits: item.parsed && item.parsed.limits ? item.parsed.limits : undefined,
|
|
129
|
+
abnormal_handler: item.parsed && item.parsed.abnormal_handler ? item.parsed.abnormal_handler : undefined,
|
|
130
|
+
outline: outlineOf(item.parsed),
|
|
131
|
+
diagnostics: item.diagnostics,
|
|
132
|
+
}
|
|
133
|
+
},
|
|
134
|
+
})
|
|
135
|
+
|
|
136
|
+
register({
|
|
137
|
+
name: 'workflow_validate',
|
|
138
|
+
description: 'Validate a 工作流 YAML text with the same checker the UI uses. Returns errors (block saving: syntax, missing process.name, '
|
|
139
|
+
+ 'empty phases, duplicate ids, dangling on_gate_fail targets) and warnings (missing guid/version, unknown keys, unknown expert/skill). '
|
|
140
|
+
+ 'Call before workflow_save.',
|
|
141
|
+
parameters: {
|
|
142
|
+
type: 'object',
|
|
143
|
+
properties: { yaml: { type: 'string', description: 'Full YAML text of the process.' } },
|
|
144
|
+
required: ['yaml'],
|
|
145
|
+
},
|
|
146
|
+
output: {
|
|
147
|
+
schema: {},
|
|
148
|
+
render(_args, value) { return text(JSON.stringify(value, null, 2)) },
|
|
149
|
+
},
|
|
150
|
+
async execute(args) {
|
|
151
|
+
if (typeof args.yaml !== 'string') throw argError('yaml is required')
|
|
152
|
+
const r = await store.validate(args.yaml)
|
|
153
|
+
return { ok: r.errors.length === 0, errors: r.errors, warnings: r.warnings, meta: r.meta }
|
|
154
|
+
},
|
|
155
|
+
})
|
|
156
|
+
|
|
157
|
+
register({
|
|
158
|
+
name: 'workflow_save',
|
|
159
|
+
description: 'Save a 工作流 YAML into the USER library (never the bundled one). name becomes the file name (<name>.yaml); '
|
|
160
|
+
+ 'validation errors reject the save; an existing file is only replaced when overwrite=true. Returns the new id.',
|
|
161
|
+
parameters: {
|
|
162
|
+
type: 'object',
|
|
163
|
+
properties: {
|
|
164
|
+
name: { type: 'string', description: 'File name without extension (also expected as process.name).' },
|
|
165
|
+
yaml: { type: 'string', description: 'Full YAML text.' },
|
|
166
|
+
dir: { type: 'string', description: 'Optional sub-directory inside the user library, e.g. "software".' },
|
|
167
|
+
overwrite: { type: 'boolean', description: 'Replace an existing file with the same name (default false).' },
|
|
168
|
+
},
|
|
169
|
+
required: ['name', 'yaml'],
|
|
170
|
+
},
|
|
171
|
+
output: {
|
|
172
|
+
schema: {},
|
|
173
|
+
render(_args, value) { return text(JSON.stringify(value, null, 2)) },
|
|
174
|
+
},
|
|
175
|
+
async execute(args) {
|
|
176
|
+
if (typeof args.name !== 'string' || typeof args.yaml !== 'string') throw argError('name and yaml are required')
|
|
177
|
+
if (store.revision === 0) await store.load()
|
|
178
|
+
const dir = typeof args.dir === 'string' ? args.dir : ''
|
|
179
|
+
const rel = `${dir ? dir.replace(/^\/+|\/+$/g, '') + '/' : ''}${args.name.trim()}.yaml`
|
|
180
|
+
const existing = store.index.get(`user:${rel}`)
|
|
181
|
+
try {
|
|
182
|
+
if (existing) {
|
|
183
|
+
if (args.overwrite !== true) throw new Error(`Error: exists: user:${rel} already exists; pass overwrite=true to replace it`)
|
|
184
|
+
return await store.save({ mode: 'update', id: existing.id, yaml: args.yaml })
|
|
185
|
+
}
|
|
186
|
+
return await store.save({ mode: 'create', name: args.name, dir, yaml: args.yaml })
|
|
187
|
+
} catch (e) {
|
|
188
|
+
if (e && e.diagnostics) throw new Error(`Error: invalid: ${e.message}: ${e.diagnostics.errors.map((d) => `${d.line ? 'L' + d.line + ' ' : ''}${d.message}`).join('; ')}`)
|
|
189
|
+
throw e
|
|
190
|
+
}
|
|
191
|
+
},
|
|
192
|
+
})
|
|
193
|
+
|
|
194
|
+
return disposers
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
module.exports = { registerTools, summaryOf, outlineOf }
|