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/artifacts.js
ADDED
|
@@ -0,0 +1,263 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* dsh-workflow-execute — 产物收集(v0.5.2,观察制)。
|
|
5
|
+
*
|
|
6
|
+
* 只认会话里真实发生的文件写操作,不看工作流声明:
|
|
7
|
+
* - write / edit:tool/result 的 meta.diffs 带 hunk({path, oldText, newText},
|
|
8
|
+
* 上下文行同时出现在 old/new 里;write 新建文件时 diffs 为空数组)
|
|
9
|
+
* - str-replace-editor:结果不带 meta,从调用参数推(create / str_replace / insert;
|
|
10
|
+
* view 只读不算)
|
|
11
|
+
*
|
|
12
|
+
* 每路径累计 +行/-行(git diffstat 口径,上下文行不计)与 diff 文本块(超限截断);
|
|
13
|
+
* 台账条目 {path, additions, deletions, diff, exists, size, mtime}。
|
|
14
|
+
* 预览 /run-file 双重白名单:路径必须在产物列表内,且解析后必须落在运行工作区内。
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
const fsP = require('node:fs/promises')
|
|
18
|
+
const path = require('node:path')
|
|
19
|
+
const { StoreError } = require('./store.js')
|
|
20
|
+
|
|
21
|
+
/** 单次尝试最多记录的文件数 / 单文件 diff 文本上限 / 预览读取上限。 */
|
|
22
|
+
const MAX_FILES = 50
|
|
23
|
+
const MAX_DIFF_CHARS = 16 * 1024
|
|
24
|
+
const READ_MAX_BYTES = 1024 * 1024
|
|
25
|
+
|
|
26
|
+
/** 会写文件的工具 → 路径所在参数字段。 */
|
|
27
|
+
const WRITE_TOOLS = {
|
|
28
|
+
write: ['file_path'],
|
|
29
|
+
edit: ['file_path'],
|
|
30
|
+
'str-replace-editor': ['path'],
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function countLines(text) {
|
|
34
|
+
const s = String(text ?? '')
|
|
35
|
+
if (s === '') return 0
|
|
36
|
+
const lines = s.split('\n')
|
|
37
|
+
if (lines[lines.length - 1] === '') lines.pop()
|
|
38
|
+
return lines.length
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function parseArgs(raw) {
|
|
42
|
+
try {
|
|
43
|
+
const v = JSON.parse(String(raw || '{}'))
|
|
44
|
+
return v && typeof v === 'object' ? v : {}
|
|
45
|
+
} catch { return {} }
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** 结果 meta → hunk 列表;meta 缺席返回 null(区别于空数组)。 */
|
|
49
|
+
function hunkListFromMeta(meta) {
|
|
50
|
+
if (!meta || typeof meta !== 'object' || !Array.isArray(meta.diffs)) return null
|
|
51
|
+
return meta.diffs.filter((h) => h && typeof h === 'object' && typeof h.path === 'string')
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* hunk 的 oldText/newText 共享上下文行:去掉公共前后缀后,
|
|
56
|
+
* 剩下的 old 行即删除、new 行即新增(git diffstat 口径)。
|
|
57
|
+
*/
|
|
58
|
+
function diffHunkStat(oldText, newText) {
|
|
59
|
+
const o = String(oldText ?? '').split('\n')
|
|
60
|
+
const n = String(newText ?? '').split('\n')
|
|
61
|
+
let pre = 0
|
|
62
|
+
while (pre < o.length && pre < n.length && o[pre] === n[pre]) pre++
|
|
63
|
+
let suf = 0
|
|
64
|
+
while (suf < o.length - pre && suf < n.length - pre && o[o.length - 1 - suf] === n[n.length - 1 - suf]) suf++
|
|
65
|
+
return {
|
|
66
|
+
additions: n.length - pre - suf,
|
|
67
|
+
deletions: o.length - pre - suf,
|
|
68
|
+
del: o.slice(pre, o.length - suf),
|
|
69
|
+
add: n.slice(pre, n.length - suf),
|
|
70
|
+
ctx: o.slice(0, pre),
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** 一个 hunk → diff 文本块(带少量上下文,git 风格 -/+ 前缀)。 */
|
|
75
|
+
function formatHunk(p, st) {
|
|
76
|
+
const lines = [`@@ ${p} @@`]
|
|
77
|
+
const ctx = st.ctx || []
|
|
78
|
+
if (ctx.length > 2) {
|
|
79
|
+
lines.push(' ' + ctx[0])
|
|
80
|
+
if (ctx.length > 3) lines.push(' ⋯')
|
|
81
|
+
lines.push(' ' + ctx[ctx.length - 1])
|
|
82
|
+
} else for (const l of ctx) lines.push(' ' + l)
|
|
83
|
+
for (const l of st.del) lines.push('-' + l)
|
|
84
|
+
for (const l of st.add) lines.push('+' + l)
|
|
85
|
+
return lines.join('\n')
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** 新文件整体 → diff 文本块(前 40 行)。 */
|
|
89
|
+
function formatWholeNew(p, content) {
|
|
90
|
+
const lines = String(content ?? '').split('\n')
|
|
91
|
+
if (lines[lines.length - 1] === '') lines.pop()
|
|
92
|
+
const head = lines.slice(0, 40).map((l) => '+' + l)
|
|
93
|
+
if (lines.length > 40) head.push(`+⋯(共 ${lines.length} 行)`)
|
|
94
|
+
return `@@ ${p}(新文件)@@\n` + head.join('\n')
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* 会话事件 → 每文件写操作累计 Map<rawPath, {additions, deletions, diff[]}。
|
|
99
|
+
* tool/call 记 callId,tool/result 按 message.content[0].toolCallId 配对:
|
|
100
|
+
* 有 meta.diffs 逐 hunk 统计(write 新文件 diffs 为空 → 参数 content 全计 +);
|
|
101
|
+
* str-replace-editor 无 meta → 从参数推。
|
|
102
|
+
*/
|
|
103
|
+
function collectWriteOps(events, firstSeq) {
|
|
104
|
+
const calls = new Map()
|
|
105
|
+
const files = new Map()
|
|
106
|
+
const note = (p, additions, deletions, block) => {
|
|
107
|
+
if (!p || typeof p !== 'string') return
|
|
108
|
+
if (!files.has(p)) {
|
|
109
|
+
if (files.size >= MAX_FILES) return
|
|
110
|
+
files.set(p, { additions: 0, deletions: 0, diff: [] })
|
|
111
|
+
}
|
|
112
|
+
const acc = files.get(p)
|
|
113
|
+
acc.additions += additions
|
|
114
|
+
acc.deletions += deletions
|
|
115
|
+
if (block && acc.diff.join('\n').length < MAX_DIFF_CHARS) acc.diff.push(block)
|
|
116
|
+
}
|
|
117
|
+
for (const ev of events || []) {
|
|
118
|
+
if (!ev || ev.seq < firstSeq) continue
|
|
119
|
+
const d = ev.data || {}
|
|
120
|
+
if (ev.type === 'tool/call') {
|
|
121
|
+
if (d.callId) calls.set(d.callId, { name: d.name, arguments: d.arguments })
|
|
122
|
+
} else if (ev.type === 'tool/result') {
|
|
123
|
+
const block = Array.isArray(d.message && d.message.content) ? d.message.content[0] : undefined
|
|
124
|
+
const call = block && block.toolCallId ? calls.get(block.toolCallId) : undefined
|
|
125
|
+
if (!call || !WRITE_TOOLS[call.name]) continue
|
|
126
|
+
const hunks = hunkListFromMeta(d.meta)
|
|
127
|
+
if (hunks) {
|
|
128
|
+
for (const h of hunks) {
|
|
129
|
+
const st = diffHunkStat(h.oldText, h.newText)
|
|
130
|
+
note(h.path, st.additions, st.deletions, formatHunk(h.path, st))
|
|
131
|
+
}
|
|
132
|
+
if (hunks.length > 0 || call.name !== 'write') continue
|
|
133
|
+
const args = parseArgs(call.arguments)
|
|
134
|
+
const content = typeof args.content === 'string' ? args.content : ''
|
|
135
|
+
note(args.file_path, countLines(content), 0, formatWholeNew(String(args.file_path || ''), content))
|
|
136
|
+
} else if (call.name === 'str-replace-editor') {
|
|
137
|
+
const args = parseArgs(call.arguments)
|
|
138
|
+
const p = typeof args.path === 'string' ? args.path : ''
|
|
139
|
+
if (args.command === 'create') {
|
|
140
|
+
const text = typeof args.file_text === 'string' ? args.file_text : ''
|
|
141
|
+
note(p, countLines(text), 0, formatWholeNew(p, text))
|
|
142
|
+
} else if (args.command === 'str_replace') {
|
|
143
|
+
const st = diffHunkStat(String(args.old_str ?? ''), String(args.new_str ?? ''))
|
|
144
|
+
note(p, st.additions, st.deletions, formatHunk(p, st))
|
|
145
|
+
} else if (args.command === 'insert') {
|
|
146
|
+
const text = typeof args.new_str === 'string' ? args.new_str : ''
|
|
147
|
+
note(p, countLines(text), 0, formatHunk(p, { add: text === '' ? [] : text.split('\n'), del: [], ctx: [] }))
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
return files
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/** abs 是否落在 cwd 内(含 cwd 本身)。 */
|
|
156
|
+
function inside(cwd, abs) {
|
|
157
|
+
return abs === cwd || abs.startsWith(cwd + path.sep)
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/** 显示路径:cwd 内相对化,否则原样(绝对路径)。 */
|
|
161
|
+
function displayPath(cwd, abs) {
|
|
162
|
+
const rel = path.relative(cwd, abs)
|
|
163
|
+
return rel !== '' && !rel.startsWith('..') ? rel : abs
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* 写操作累计 → 产物条目(逐个 stat)。
|
|
168
|
+
* 工作区内的路径存相对路径,工作区外的写入保留绝对路径(环节常写到别的仓库,不能丢)。
|
|
169
|
+
* @returns Array<{ name, path, source:'written', additions, deletions, diff, exists, size?, mtime? }>
|
|
170
|
+
*/
|
|
171
|
+
async function resolveArtifactEntries({ cwd, written }) {
|
|
172
|
+
const list = []
|
|
173
|
+
for (const [raw, acc] of written instanceof Map ? written : []) {
|
|
174
|
+
const abs = path.resolve(cwd, raw)
|
|
175
|
+
const diffText = Array.isArray(acc.diff) ? acc.diff.join('\n') : String(acc.diff ?? '')
|
|
176
|
+
const entry = {
|
|
177
|
+
name: path.basename(abs),
|
|
178
|
+
path: displayPath(cwd, abs),
|
|
179
|
+
source: 'written',
|
|
180
|
+
additions: acc.additions,
|
|
181
|
+
deletions: acc.deletions,
|
|
182
|
+
diff: diffText.slice(0, MAX_DIFF_CHARS),
|
|
183
|
+
}
|
|
184
|
+
try {
|
|
185
|
+
const st = await fsP.stat(abs)
|
|
186
|
+
entry.exists = true
|
|
187
|
+
entry.size = st.size
|
|
188
|
+
entry.mtime = st.mtimeMs
|
|
189
|
+
} catch { entry.exists = false }
|
|
190
|
+
list.push(entry)
|
|
191
|
+
}
|
|
192
|
+
return list
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* 读取一个产物(文本预览 + 变更统计透传)。白名单校验:
|
|
197
|
+
* path 必须与该运行产物列表中的记录完全一致(列表只收本运行真实写过的文件)。
|
|
198
|
+
*/
|
|
199
|
+
async function readArtifactText({ cwd, artifacts, path: p, maxBytes = READ_MAX_BYTES }) {
|
|
200
|
+
const entry = (Array.isArray(artifacts) ? artifacts : []).find((a) => a && a.path === p)
|
|
201
|
+
if (!entry) throw new StoreError('not_found', '该路径不在本运行的产物列表中')
|
|
202
|
+
if (!entry.exists) throw new StoreError('not_found', '文件不存在或尚未生成')
|
|
203
|
+
const abs = path.resolve(cwd, entry.path)
|
|
204
|
+
const st = await fsP.stat(abs)
|
|
205
|
+
if (!st.isFile()) throw new StoreError('invalid_path', '不是常规文件')
|
|
206
|
+
const fh = await fsP.open(abs, 'r')
|
|
207
|
+
try {
|
|
208
|
+
const len = Math.min(st.size, maxBytes + 1)
|
|
209
|
+
const buf = Buffer.alloc(len)
|
|
210
|
+
await fh.read(buf, 0, len, 0)
|
|
211
|
+
const truncated = st.size > maxBytes
|
|
212
|
+
const view = buf.subarray(0, Math.min(len, maxBytes))
|
|
213
|
+
const binary = view.includes(0)
|
|
214
|
+
return {
|
|
215
|
+
path: entry.path, name: entry.name, size: st.size, mtime: st.mtimeMs,
|
|
216
|
+
truncated, binary,
|
|
217
|
+
content: binary ? undefined : view.toString('utf8'),
|
|
218
|
+
additions: entry.additions, deletions: entry.deletions, diff: entry.diff,
|
|
219
|
+
}
|
|
220
|
+
} finally { await fh.close() }
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/** 汇总运行里某环节声明的产物(links.expected_artifacts);linkId 为空则取全部。 */
|
|
224
|
+
function declaredArtifactsOf(snapshot, linkId) {
|
|
225
|
+
const out = []
|
|
226
|
+
const phases = snapshot && Array.isArray(snapshot.phases) ? snapshot.phases : []
|
|
227
|
+
for (const ph of phases) {
|
|
228
|
+
for (const ln of (ph && Array.isArray(ph.links) ? ph.links : [])) {
|
|
229
|
+
if (!ln || (linkId && ln.id !== linkId)) continue
|
|
230
|
+
for (const a of (Array.isArray(ln.expected_artifacts) ? ln.expected_artifacts : [])) out.push(a)
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
return out
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* 声明的产物 → 台账条目(按工作区实际文件补 exists/size/mtime)。
|
|
238
|
+
* 会话没用 write/edit 落盘时(例如 shell 重定向)观察不到写操作,但文件确实在,靠这里补齐。
|
|
239
|
+
*/
|
|
240
|
+
async function resolveDeclaredArtifacts({ cwd, declared }) {
|
|
241
|
+
const list = []
|
|
242
|
+
const seen = new Set()
|
|
243
|
+
for (const item of Array.isArray(declared) ? declared : []) {
|
|
244
|
+
const p = typeof item === 'string' ? item : item && item.path
|
|
245
|
+
if (!p || typeof p !== 'string' || seen.has(p)) continue
|
|
246
|
+
seen.add(p)
|
|
247
|
+
const entry = { path: p, name: (item && item.name) || p.split('/').pop(), additions: null, deletions: null, diff: '' }
|
|
248
|
+
try {
|
|
249
|
+
const st = await fsP.stat(path.resolve(cwd, p))
|
|
250
|
+
entry.exists = true
|
|
251
|
+
entry.size = st.size
|
|
252
|
+
entry.mtime = st.mtimeMs
|
|
253
|
+
} catch { entry.exists = false }
|
|
254
|
+
list.push(entry)
|
|
255
|
+
}
|
|
256
|
+
return list
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
module.exports = {
|
|
260
|
+
collectWriteOps, resolveArtifactEntries, readArtifactText,
|
|
261
|
+
declaredArtifactsOf, resolveDeclaredArtifacts,
|
|
262
|
+
countLines, diffHunkStat, inside, WRITE_TOOLS, MAX_DIFF_CHARS, READ_MAX_BYTES,
|
|
263
|
+
}
|