dsh-seal-scripts 0.5.0 → 0.5.2
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.en.md +15 -2
- package/README.md +15 -2
- package/lib/client.js +878 -23
- package/lib/core/assembly.js +8 -8
- package/lib/core/host-limits.js +5 -5
- package/lib/core/index.js +30 -30
- package/lib/core/inject.js +1 -1
- package/lib/core/lang.js +418 -75
- package/lib/core/locale.js +1 -1
- package/lib/core/memory-file.js +2 -5
- package/lib/core/migrate.js +1 -1
- package/lib/core/path-guard.js +4 -4
- package/lib/core/schema-runner.js +10 -10
- package/lib/core/schema-tools.js +88 -88
- package/lib/core/schema.js +49 -61
- package/lib/core/search.js +1 -1
- package/lib/core/settings.js +1 -1
- package/lib/core/store.js +1 -1
- package/lib/core/tool-cache.js +133 -0
- package/lib/core/tool-transfer.js +14 -14
- package/lib/core/tool.js +71 -71
- package/lib/core/zip.js +35 -35
- package/lib/core/zstd-js.js +50 -50
- package/lib/core/zstd.js +1 -1
- package/lib/host/capabilities.js +3 -3
- package/lib/host/compact-delete.js +11 -11
- package/lib/host/delete.js +16 -16
- package/lib/host/host-contract.js +2 -2
- package/lib/host/http-util.js +1 -1
- package/lib/host/lifecycle.js +2 -2
- package/lib/host/register.js +3 -3
- package/lib/host/routes/memories.js +7 -7
- package/lib/host/routes/sessions.js +8 -8
- package/lib/host/routes/tools.js +41 -6
- package/package.json +1 -1
package/lib/core/memory-file.js
CHANGED
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
|
|
8
8
|
import { randomBytes } from 'node:crypto'
|
|
9
9
|
import {
|
|
10
|
-
headerLabels, headerAliases, sectionTitle,
|
|
10
|
+
headerLabels, headerAliases, sectionTitle, sectionLangOf,
|
|
11
11
|
coversValue, branchValue, toolCallsValue, lockedValue
|
|
12
12
|
} from './lang.js'
|
|
13
13
|
|
|
@@ -184,10 +184,7 @@ export function setField(text, field, value) {
|
|
|
184
184
|
return null
|
|
185
185
|
}
|
|
186
186
|
|
|
187
|
-
|
|
188
|
-
function sectionLangOf(text) {
|
|
189
|
-
return /^##\s*(Summary|Original|Diary)\s*$/m.test(String(text ?? '')) ? 'en' : 'zh'
|
|
190
|
-
}
|
|
187
|
+
/* sectionLangOf 搬进了 lang.js(0.5.2:8 种语言都要认)。 */
|
|
191
188
|
|
|
192
189
|
/**
|
|
193
190
|
* 换掉一个分区的内容,其余字节不动。标题跟着这份文件自己的语言走。
|
package/lib/core/migrate.js
CHANGED
|
@@ -86,7 +86,7 @@ export async function migrateIndex(dir, workspace) {
|
|
|
86
86
|
}, null, 2) + '\n')
|
|
87
87
|
written.push(entry.file)
|
|
88
88
|
}
|
|
89
|
-
if (written.length !== valid.length) throw new Error('
|
|
89
|
+
if (written.length !== valid.length) throw new Error('sidecar count does not match the old index entry count')
|
|
90
90
|
} catch (err) {
|
|
91
91
|
for (const f of written) await fsp.rm(sidecarPath(dir, f), { force: true }).catch(() => {})
|
|
92
92
|
throw err
|
package/lib/core/path-guard.js
CHANGED
|
@@ -22,14 +22,14 @@ import path from 'node:path'
|
|
|
22
22
|
* @returns {string} 绝对路径
|
|
23
23
|
*/
|
|
24
24
|
export function assertInside(dir, raw, options = {}) {
|
|
25
|
-
const label = options.label ?? '
|
|
25
|
+
const label = options.label ?? 'this directory'
|
|
26
26
|
const text = String(raw ?? '').trim()
|
|
27
|
-
if (text === '') throw new Error('
|
|
27
|
+
if (text === '') throw new Error('file name must not be empty')
|
|
28
28
|
const base = path.resolve(dir)
|
|
29
29
|
const abs = path.resolve(base, text)
|
|
30
30
|
const rel = path.relative(base, abs)
|
|
31
31
|
if (rel === '' || rel === '..' || rel.startsWith('..' + path.sep) || path.isAbsolute(rel)) {
|
|
32
|
-
throw new Error('
|
|
32
|
+
throw new Error('only files inside ' + label + ' can be touched: ' + text)
|
|
33
33
|
}
|
|
34
34
|
return abs
|
|
35
35
|
}
|
|
@@ -37,7 +37,7 @@ export function assertInside(dir, raw, options = {}) {
|
|
|
37
37
|
/** 必须是 .md(回顾文件)。 */
|
|
38
38
|
export function assertMarkdown(abs, raw) {
|
|
39
39
|
if (!path.extname(abs).toLowerCase().endsWith('.md')) {
|
|
40
|
-
throw new Error('
|
|
40
|
+
throw new Error('not a review file: ' + String(raw ?? ''))
|
|
41
41
|
}
|
|
42
42
|
return abs
|
|
43
43
|
}
|
|
@@ -62,7 +62,7 @@ export function interpreterOf(run) {
|
|
|
62
62
|
return { cmd: process.env.ComSpec || 'cmd.exe', args: ['/d', '/c'] }
|
|
63
63
|
}
|
|
64
64
|
default:
|
|
65
|
-
throw new Error('
|
|
65
|
+
throw new Error('unknown run mode: ' + String(run))
|
|
66
66
|
}
|
|
67
67
|
}
|
|
68
68
|
|
|
@@ -89,17 +89,17 @@ export async function runScript({
|
|
|
89
89
|
}) {
|
|
90
90
|
const name = String(step ?? '').trim()
|
|
91
91
|
const file = resolveInside(dir, name)
|
|
92
|
-
if (!fs.existsSync(file)) throw new Error('
|
|
92
|
+
if (!fs.existsSync(file)) throw new Error('this toolScript has no such script: ' + name)
|
|
93
93
|
|
|
94
94
|
const scripts = Array.isArray(manifest?.scripts) ? manifest.scripts : []
|
|
95
95
|
const meta = scripts.find((s) => s && s.name === name)
|
|
96
|
-
if (!meta) throw new Error('
|
|
96
|
+
if (!meta) throw new Error('this file is not in the toolScript manifest: ' + name)
|
|
97
97
|
const hashes = manifest && typeof manifest.hashes === 'object' && manifest.hashes !== null ? manifest.hashes : {}
|
|
98
98
|
const expected = typeof hashes[name] === 'string' ? hashes[name] : ''
|
|
99
99
|
const actual = sha256OfFile(file)
|
|
100
100
|
if (expected !== '' && expected !== actual && !allowDrift) {
|
|
101
|
-
throw new Error('
|
|
102
|
-
'
|
|
101
|
+
throw new Error('script sha256 does not match the manifest, refusing to run (' + name + '). ' +
|
|
102
|
+
'If you just edited it by hand, reinstall the toolScript (session_tool_create_write + replace), or pass allowDrift: true explicitly this time.')
|
|
103
103
|
}
|
|
104
104
|
|
|
105
105
|
const host = limits ?? readHostLimits()
|
|
@@ -210,7 +210,7 @@ function spawnCollect({ cmd, argv, cwd, env, timeout, cap, stdin, dshHome, label
|
|
|
210
210
|
errFd = fs.openSync(paths.stderrFile, 'w')
|
|
211
211
|
} catch (err) {
|
|
212
212
|
for (const fd of [outFd, errFd]) if (typeof fd === 'number') { try { fs.closeSync(fd) } catch { /* 忽略 */ } }
|
|
213
|
-
reject(new Error('
|
|
213
|
+
reject(new Error('failed to create the run log: ' + (err && err.message ? err.message : String(err))))
|
|
214
214
|
return
|
|
215
215
|
}
|
|
216
216
|
|
|
@@ -225,7 +225,7 @@ function spawnCollect({ cmd, argv, cwd, env, timeout, cap, stdin, dshHome, label
|
|
|
225
225
|
stdio: ['pipe', outFd, errFd]
|
|
226
226
|
})
|
|
227
227
|
} catch (err) {
|
|
228
|
-
reject(new Error('
|
|
228
|
+
reject(new Error('cannot start interpreter ' + cmd + ': ' + (err && err.message ? err.message : String(err))))
|
|
229
229
|
return
|
|
230
230
|
} finally {
|
|
231
231
|
// 子进程已经 dup 了自己的那一份,父进程这边立刻关掉,免得句柄泄漏。
|
|
@@ -306,8 +306,8 @@ function spawnCollect({ cmd, argv, cwd, env, timeout, cap, stdin, dshHome, label
|
|
|
306
306
|
if (done) return
|
|
307
307
|
done = true
|
|
308
308
|
const message = err && err.code === 'ENOENT'
|
|
309
|
-
? '
|
|
310
|
-
: '
|
|
309
|
+
? 'interpreter not found: ' + cmd + (cmd === 'bash' ? ' (this toolScript is written in bash; bash is not on PATH on this machine — rewrite it as a node script or install bash first)' : '')
|
|
310
|
+
: 'failed to start: ' + (err && err.message ? err.message : String(err))
|
|
311
311
|
reject(new Error(message))
|
|
312
312
|
})
|
|
313
313
|
|
|
@@ -363,7 +363,7 @@ function sleep(ms) {
|
|
|
363
363
|
*/
|
|
364
364
|
export async function killRun(pid, { graceMs = KILL_GRACE_MS } = {}) {
|
|
365
365
|
const n = Number(pid)
|
|
366
|
-
if (!Number.isInteger(n) || n <= 0) throw new Error('killRun
|
|
366
|
+
if (!Number.isInteger(n) || n <= 0) throw new Error('killRun needs a positive pid: ' + String(pid))
|
|
367
367
|
const signal = (s) => {
|
|
368
368
|
try { process.kill(-n, s); return true } catch { /* 进程组可能已经没了 */ }
|
|
369
369
|
try { process.kill(n, s); return true } catch { return false }
|
package/lib/core/schema-tools.js
CHANGED
|
@@ -62,7 +62,7 @@ export async function readBodyOf({ dshHome, cwd, file }) {
|
|
|
62
62
|
const abs = resolveMemoryFile(dshHome, cwd, file)
|
|
63
63
|
const text = await fsp.readFile(abs, 'utf8')
|
|
64
64
|
const parsed = parseMemory(text)
|
|
65
|
-
if (!parsed.id) throw new Error('
|
|
65
|
+
if (!parsed.id) throw new Error('this file has no review section markers, so it is not a review file: ' + file)
|
|
66
66
|
return {
|
|
67
67
|
file: path.basename(abs),
|
|
68
68
|
path: abs,
|
|
@@ -77,12 +77,12 @@ export async function locateMemory(args, { dshHome, cwd }) {
|
|
|
77
77
|
const hasFile = typeof args.file === 'string' && args.file.trim() !== ''
|
|
78
78
|
const hasDate = args.date !== undefined
|
|
79
79
|
if (!hasFile && !hasDate) {
|
|
80
|
-
throw new Error('
|
|
80
|
+
throw new Error('pass either file (a review file name) or date (YYYY-MM-DD, first review by default) to locate a review')
|
|
81
81
|
}
|
|
82
|
-
if (hasFile && hasDate) throw new Error('file
|
|
82
|
+
if (hasFile && hasDate) throw new Error('file and date are mutually exclusive')
|
|
83
83
|
if (hasFile) return String(args.file).trim()
|
|
84
84
|
if (!/^\d{4}-\d{2}-\d{2}$/.test(String(args.date).trim())) {
|
|
85
|
-
throw new Error('date
|
|
85
|
+
throw new Error('date must be YYYY-MM-DD (local date): ' + String(args.date))
|
|
86
86
|
}
|
|
87
87
|
return resolveMemoryByDay({
|
|
88
88
|
dshHome, cwd,
|
|
@@ -223,21 +223,21 @@ export function outlineOf(blocks, { maxItems = Infinity } = {}) {
|
|
|
223
223
|
/** 骨架渲染成人读得懂的几段文字。 */
|
|
224
224
|
export function renderOutline({ file, body, outline, filePath }) {
|
|
225
225
|
const lines = []
|
|
226
|
-
lines.push('
|
|
227
|
-
lines.push('
|
|
228
|
-
'
|
|
226
|
+
lines.push('Original outline · ' + file + ' (' + filePath + ')')
|
|
227
|
+
lines.push('Body ' + Buffer.byteLength(body, 'utf8') + ' bytes / ' + body.split('\n').length + ' lines' +
|
|
228
|
+
'; ' + outline.calls + ' tool calls (' + outline.failures + ' failed); ' + outline.distinct + ' distinct after dedup')
|
|
229
229
|
lines.push('')
|
|
230
230
|
for (const g of outline.groups) {
|
|
231
|
-
lines.push('▸ ' + g.tool + ' ·
|
|
232
|
-
' ·
|
|
231
|
+
lines.push('▸ ' + g.tool + ' · ' + g.count + ' calls' + (g.failed > 0 ? ' (' + g.failed + ' failed)' : '') +
|
|
232
|
+
' · ' + g.distinct + ' distinct' + (g.omitted > 0 ? ' (showing ' + g.shown + ', the rest omitted)' : ''))
|
|
233
233
|
for (const it of g.items) {
|
|
234
|
-
lines.push(' L' + it.line + (it.failed ? ' [
|
|
234
|
+
lines.push(' L' + it.line + (it.failed ? ' [failed]' : '') + ' ' + (it.firstArg || '(no args)') +
|
|
235
235
|
(it.count > 1 ? ' ×' + it.count : ''))
|
|
236
236
|
}
|
|
237
237
|
lines.push('')
|
|
238
238
|
}
|
|
239
|
-
lines.push('
|
|
240
|
-
lines.push('
|
|
239
|
+
lines.push('To see one call\'s full args and result: mode=block, line=<the number after L above>.')
|
|
240
|
+
lines.push('To read the whole segment verbatim: mode=raw (paged by cursor). To seal it: install the scripts as a toolScript.')
|
|
241
241
|
return lines.join('\n')
|
|
242
242
|
}
|
|
243
243
|
|
|
@@ -245,35 +245,35 @@ export function renderOutline({ file, body, outline, filePath }) {
|
|
|
245
245
|
|
|
246
246
|
/** 读这一侧的参数校验;写那一侧另有 validateWriteArgs(0.3.3 拆开)。 */
|
|
247
247
|
function validateReadArgs(args) {
|
|
248
|
-
if (args === null || typeof args !== 'object' || Array.isArray(args)) throw new Error('
|
|
248
|
+
if (args === null || typeof args !== 'object' || Array.isArray(args)) throw new Error('arguments must be an object')
|
|
249
249
|
const mode = args.mode === undefined ? 'outline' : String(args.mode)
|
|
250
|
-
if (!['outline', 'raw', 'block'].includes(mode)) throw new Error('mode
|
|
250
|
+
if (!['outline', 'raw', 'block'].includes(mode)) throw new Error('mode accepts only outline / raw / block: ' + mode)
|
|
251
251
|
if (args.toolScript !== undefined || args.replace !== undefined) {
|
|
252
|
-
throw new Error('
|
|
252
|
+
throw new Error('this tool is read-only; use ' + TOOL_CREATE_WRITE_NAME + ' to install a toolScript')
|
|
253
253
|
}
|
|
254
|
-
if (args.cursor !== undefined && (!Number.isInteger(args.cursor) || args.cursor < 0)) throw new Error('cursor
|
|
254
|
+
if (args.cursor !== undefined && (!Number.isInteger(args.cursor) || args.cursor < 0)) throw new Error('cursor is a 0-based character offset')
|
|
255
255
|
// 0.4.11:去掉 1000~200000 的死区间——分页只要求「是个正整数」。
|
|
256
256
|
if (args.chars !== undefined && (!Number.isInteger(args.chars) || args.chars <= 0)) {
|
|
257
|
-
throw new Error('chars
|
|
257
|
+
throw new Error('chars must be a positive integer (characters per page, default 40000)')
|
|
258
258
|
}
|
|
259
|
-
if (args.line !== undefined && (!Number.isInteger(args.line) || args.line < 1)) throw new Error('line
|
|
260
|
-
if (args.span !== undefined && (!Number.isInteger(args.span) || args.span < 0)) throw new Error('span
|
|
261
|
-
if (args.maxItems !== undefined && (!Number.isInteger(args.maxItems) || args.maxItems < 1)) throw new Error('maxItems
|
|
259
|
+
if (args.line !== undefined && (!Number.isInteger(args.line) || args.line < 1)) throw new Error('line is a 1-based body line number (use the number after L in the outline)')
|
|
260
|
+
if (args.span !== undefined && (!Number.isInteger(args.span) || args.span < 0)) throw new Error('span must be an integer from 0 (extra lines in a block)')
|
|
261
|
+
if (args.maxItems !== undefined && (!Number.isInteger(args.maxItems) || args.maxItems < 1)) throw new Error('maxItems must be a positive integer')
|
|
262
262
|
return mode
|
|
263
263
|
}
|
|
264
264
|
|
|
265
265
|
/** 写盘那一侧的返回文本。 */
|
|
266
266
|
function renderWriteResult(res) {
|
|
267
267
|
const lines = [
|
|
268
|
-
'
|
|
269
|
-
'
|
|
270
|
-
'
|
|
271
|
-
...res.files.map((f) => ' · ' + f.name + '
|
|
272
|
-
res.backup ? '
|
|
273
|
-
'
|
|
268
|
+
'Installed toolScript: ' + res.id,
|
|
269
|
+
'Directory: ' + res.dir,
|
|
270
|
+
'Files:',
|
|
271
|
+
...res.files.map((f) => ' · ' + f.name + ' (' + f.kind + ' · ' + f.bytes + ' bytes · sha256 ' + f.sha256.slice(0, 12) + '…)'),
|
|
272
|
+
res.backup ? 'Previous version kept at: ' + res.backup : '',
|
|
273
|
+
'Index: ' + res.indexPath,
|
|
274
274
|
'',
|
|
275
|
-
'
|
|
276
|
-
'
|
|
275
|
+
'Ready to run: session_tool_call({ toolScript: "' + res.id + '" }); add dryRun: true to see the commands first.',
|
|
276
|
+
'This toolScript\'s name and purpose enter the system prompt (the toolSequence section) on the next assembly.'
|
|
277
277
|
]
|
|
278
278
|
return lines.filter((l) => l !== '').join('\n')
|
|
279
279
|
}
|
|
@@ -333,7 +333,7 @@ export function buildToolCreateReadDefinition({ dshHome, logger }) {
|
|
|
333
333
|
async execute(args, exec) {
|
|
334
334
|
const mode = validateReadArgs(args)
|
|
335
335
|
const cwd = exec?.agent?.session?.header?.cwd
|
|
336
|
-
if (typeof cwd !== 'string' || cwd === '') throw new Error(TOOL_CREATE_READ_NAME + '
|
|
336
|
+
if (typeof cwd !== 'string' || cwd === '') throw new Error(TOOL_CREATE_READ_NAME + ' needs to know the current workspace')
|
|
337
337
|
const file = await locateMemory(args, { dshHome, cwd })
|
|
338
338
|
const one = await readBodyOf({ dshHome, cwd, file })
|
|
339
339
|
const body = one.body
|
|
@@ -358,26 +358,26 @@ export function buildToolCreateReadDefinition({ dshHome, logger }) {
|
|
|
358
358
|
|
|
359
359
|
// ── 读:单块展开 ──
|
|
360
360
|
if (mode === 'block') {
|
|
361
|
-
if (args.line === undefined) throw new Error('block
|
|
361
|
+
if (args.line === undefined) throw new Error('block requires line (the number after L in the outline)')
|
|
362
362
|
const lines = body.split('\n')
|
|
363
|
-
if (args.line > lines.length) throw new Error('line
|
|
363
|
+
if (args.line > lines.length) throw new Error('line exceeds the body line count (' + lines.length + '): ' + args.line)
|
|
364
364
|
const target = pickBlockAt(blocks, args.line)
|
|
365
|
-
if (!target) throw new Error('L' + args.line + '
|
|
365
|
+
if (!target) throw new Error('no tool call near L' + args.line + '; use mode=outline to see the outline first')
|
|
366
366
|
const span = args.span === undefined ? 0 : Math.max(0, Number(args.span) || 0)
|
|
367
367
|
const from = target.line
|
|
368
368
|
const to = Math.min(lines.length, Math.max(target.endLine, args.line) + span)
|
|
369
369
|
const slice = lines.slice(from - 1, to).join('\n')
|
|
370
370
|
const head = 'L' + from + '–' + to + ' · ' + target.name +
|
|
371
|
-
(target.failed ? ' ·
|
|
371
|
+
(target.failed ? ' · [this call failed]' : (target.status ? ' · ' + target.status : ' · no result (execution interrupted)'))
|
|
372
372
|
const parts = [head, '', slice]
|
|
373
373
|
// 结果区:◂ 之前那一段。和块内的缩进段一致时就不再抄一遍(同一段字不写两次)。
|
|
374
374
|
const same = target.resultText.length === target.argLines.length &&
|
|
375
375
|
target.resultText.every((l, i) => l === target.argLines[i])
|
|
376
376
|
if (target.resultText.length > 0 && !same) {
|
|
377
377
|
const capped = target.resultText.slice(0, 40)
|
|
378
|
-
parts.push('', '
|
|
378
|
+
parts.push('', '【Result】the content right before ◂ (the previous call\'s output may share the same indentation and cannot be separated textually):')
|
|
379
379
|
parts.push(capped.join('\n'))
|
|
380
|
-
if (target.resultText.length > capped.length) parts.push('
|
|
380
|
+
if (target.resultText.length > capped.length) parts.push('… (' + (target.resultText.length - capped.length) + ' more lines; use mode=raw to fetch them by line number)')
|
|
381
381
|
}
|
|
382
382
|
return jsonSafe({
|
|
383
383
|
mode: 'block', file: one.file, path: one.path,
|
|
@@ -406,17 +406,17 @@ export function buildToolCreateReadDefinition({ dshHome, logger }) {
|
|
|
406
406
|
|
|
407
407
|
/** 写这一侧的参数校验:只收 toolScript / replace / 可选定位(定位只用来记出处)。 */
|
|
408
408
|
function validateWriteArgs(args) {
|
|
409
|
-
if (args === null || typeof args !== 'object' || Array.isArray(args)) throw new Error('
|
|
410
|
-
if (args.toolScript === undefined) throw new Error('
|
|
411
|
-
if (args.toolScript === null || typeof args.toolScript !== 'object' || Array.isArray(args.toolScript)) throw new Error('toolScript
|
|
409
|
+
if (args === null || typeof args !== 'object' || Array.isArray(args)) throw new Error('arguments must be an object')
|
|
410
|
+
if (args.toolScript === undefined) throw new Error('toolScript is required: { id, title, purpose, entry, scripts }')
|
|
411
|
+
if (args.toolScript === null || typeof args.toolScript !== 'object' || Array.isArray(args.toolScript)) throw new Error('toolScript must be an object')
|
|
412
412
|
for (const k of ['mode', 'cursor', 'chars', 'line', 'span', 'maxItems']) {
|
|
413
|
-
if (args[k] !== undefined) throw new Error('
|
|
413
|
+
if (args[k] !== undefined) throw new Error('installing a toolScript does not take ' + k + ': that is an argument of ' + TOOL_CREATE_READ_NAME)
|
|
414
414
|
}
|
|
415
|
-
if (args.file !== undefined && typeof args.file !== 'string') throw new Error('file
|
|
416
|
-
if (args.file !== undefined && args.date !== undefined) throw new Error('file
|
|
417
|
-
if (args.date !== undefined && !/^\d{4}-\d{2}-\d{2}$/.test(String(args.date))) throw new Error('date
|
|
418
|
-
if (args.nth !== undefined && (!Number.isInteger(args.nth) || args.nth < 1)) throw new Error('nth
|
|
419
|
-
if (args.replace !== undefined && typeof args.replace !== 'boolean') throw new Error('replace
|
|
415
|
+
if (args.file !== undefined && typeof args.file !== 'string') throw new Error('file must be a string (only pass it to record provenance)')
|
|
416
|
+
if (args.file !== undefined && args.date !== undefined) throw new Error('file and date are mutually exclusive')
|
|
417
|
+
if (args.date !== undefined && !/^\d{4}-\d{2}-\d{2}$/.test(String(args.date))) throw new Error('date must be YYYY-MM-DD')
|
|
418
|
+
if (args.nth !== undefined && (!Number.isInteger(args.nth) || args.nth < 1)) throw new Error('nth must be an integer starting at 1')
|
|
419
|
+
if (args.replace !== undefined && typeof args.replace !== 'boolean') throw new Error('replace must be a boolean')
|
|
420
420
|
}
|
|
421
421
|
|
|
422
422
|
/**
|
|
@@ -512,7 +512,7 @@ export function buildToolCreateWriteDefinition({ dshHome, logger }) {
|
|
|
512
512
|
const spec = { ...args.toolScript }
|
|
513
513
|
if (args.file !== undefined || args.date !== undefined) {
|
|
514
514
|
const cwd = exec?.agent?.session?.header?.cwd
|
|
515
|
-
if (typeof cwd !== 'string' || cwd === '') throw new Error(TOOL_CREATE_WRITE_NAME + '
|
|
515
|
+
if (typeof cwd !== 'string' || cwd === '') throw new Error(TOOL_CREATE_WRITE_NAME + ' needs the current workspace when file/date is given')
|
|
516
516
|
const mem = await locateMemory(args, { dshHome, cwd })
|
|
517
517
|
const kept = spec.provenance && typeof spec.provenance === 'object' ? spec.provenance : {}
|
|
518
518
|
spec.provenance = { ...kept, memory: typeof kept.memory === 'string' && kept.memory !== '' ? kept.memory : mem }
|
|
@@ -558,7 +558,7 @@ function resolveRunCwd(value, { cwd, dir }) {
|
|
|
558
558
|
const abs = path.resolve(raw)
|
|
559
559
|
const inWorkspace = inside(cwd, abs)
|
|
560
560
|
const inToolScript = inside(dir, abs)
|
|
561
|
-
if (!inWorkspace && !inToolScript) throw new Error('cwd
|
|
561
|
+
if (!inWorkspace && !inToolScript) throw new Error('cwd must be workspace / toolScript, or a path inside either: ' + raw)
|
|
562
562
|
return abs
|
|
563
563
|
}
|
|
564
564
|
|
|
@@ -570,14 +570,14 @@ function inside(a, b) {
|
|
|
570
570
|
|
|
571
571
|
/** 校验一次调用的形状。 */
|
|
572
572
|
function validateCallShape(call, where) {
|
|
573
|
-
if (call === null || typeof call !== 'object' || Array.isArray(call)) throw new Error(where + '
|
|
574
|
-
if (typeof call.toolScript !== 'string' || call.toolScript.trim() === '') throw new Error(where + 'toolScript
|
|
573
|
+
if (call === null || typeof call !== 'object' || Array.isArray(call)) throw new Error(where + 'must be an object')
|
|
574
|
+
if (typeof call.toolScript !== 'string' || call.toolScript.trim() === '') throw new Error(where + 'toolScript must not be empty')
|
|
575
575
|
if (call.args !== undefined && (!Array.isArray(call.args) || call.args.some((a) => typeof a !== 'string' && typeof a !== 'number'))) {
|
|
576
|
-
throw new Error(where + 'args
|
|
576
|
+
throw new Error(where + 'args must be an array of strings')
|
|
577
577
|
}
|
|
578
|
-
if (call.stdin !== undefined && typeof call.stdin !== 'string') throw new Error(where + 'stdin
|
|
579
|
-
if (call.timeoutMs !== undefined && !Number.isInteger(call.timeoutMs)) throw new Error(where + 'timeoutMs
|
|
580
|
-
if (call.cwd !== undefined && typeof call.cwd !== 'string') throw new Error(where + 'cwd
|
|
578
|
+
if (call.stdin !== undefined && typeof call.stdin !== 'string') throw new Error(where + 'stdin must be a string')
|
|
579
|
+
if (call.timeoutMs !== undefined && !Number.isInteger(call.timeoutMs)) throw new Error(where + 'timeoutMs must be an integer')
|
|
580
|
+
if (call.cwd !== undefined && typeof call.cwd !== 'string') throw new Error(where + 'cwd must be a string')
|
|
581
581
|
}
|
|
582
582
|
|
|
583
583
|
/**
|
|
@@ -593,12 +593,12 @@ function validateCallShape(call, where) {
|
|
|
593
593
|
*/
|
|
594
594
|
function missingToolScriptMessage(id, detail) {
|
|
595
595
|
return [
|
|
596
|
-
'
|
|
597
|
-
'
|
|
598
|
-
'
|
|
599
|
-
'
|
|
600
|
-
'
|
|
601
|
-
'
|
|
596
|
+
'【Call blocked before anything ran】the toolSequence has no toolScript named ' + id + '.',
|
|
597
|
+
'Underlying cause: ' + detail,
|
|
598
|
+
'Why the list (system prompt) still shows it: the list is frozen per session — installing or removing toolScripts mid-session does not touch the system prompt, ' +
|
|
599
|
+
'otherwise the whole conversation would be re-sent as uncached input. So a deleted script can keep appearing in the list until the list is refetched or the session changes.',
|
|
600
|
+
'What to do: call ' + TOOL_CALL_TOOL_NAME + ' with no arguments to see the real list on disk; ' +
|
|
601
|
+
'to make this session refetch the list from disk, toggle "When the list is refreshed" once in Settings, or start a new session.'
|
|
602
602
|
].join('\n')
|
|
603
603
|
}
|
|
604
604
|
|
|
@@ -617,17 +617,17 @@ function planOne({ req, manifest, dir }, { cwd, args, allowDrift }) {
|
|
|
617
617
|
const step = req.step === undefined ? manifest.entry : String(req.step)
|
|
618
618
|
const script = scripts.find((s) => s && s.name === step)
|
|
619
619
|
if (!script) {
|
|
620
|
-
throw new Error('
|
|
620
|
+
throw new Error('this toolScript has no such script: ' + step + ' (available: ' + scripts.map((s) => s.name).join(', ') + ')')
|
|
621
621
|
}
|
|
622
622
|
const file = resolveInside(dir, step)
|
|
623
623
|
if (!fsSync.existsSync(file)) {
|
|
624
|
-
throw new Error('
|
|
624
|
+
throw new Error('the manifest lists this script but the file is missing on disk: ' + step + ' (reinstall this toolScript; do not delete files by hand)')
|
|
625
625
|
}
|
|
626
626
|
const hashes = manifest.hashes && typeof manifest.hashes === 'object' ? manifest.hashes : {}
|
|
627
627
|
const expected = typeof hashes[step] === 'string' ? hashes[step] : ''
|
|
628
628
|
if (expected !== '' && !allowDrift && sha256OfFile(file) !== expected) {
|
|
629
|
-
throw new Error('
|
|
630
|
-
'
|
|
629
|
+
throw new Error('script sha256 does not match the manifest, refusing to run (' + step + '). ' +
|
|
630
|
+
'If you just edited it by hand, reinstall the toolScript (' + TOOL_CREATE_WRITE_NAME + ' + replace), or pass allowDrift: true explicitly this time.')
|
|
631
631
|
}
|
|
632
632
|
// 未知执行方式:run 只收 bash / node / ps1 / cmd;安装时已校验,这里是兜底。
|
|
633
633
|
interpreterOf(script.run)
|
|
@@ -641,8 +641,8 @@ function planOne({ req, manifest, dir }, { cwd, args, allowDrift }) {
|
|
|
641
641
|
: (Array.isArray(manifest.params) ? manifest.params : [])
|
|
642
642
|
const argvArgs = (req.args ?? []).map(String)
|
|
643
643
|
if (declared.length > 0 && argvArgs.length > declared.length) {
|
|
644
|
-
throw new Error('
|
|
645
|
-
'
|
|
644
|
+
throw new Error('got ' + argvArgs.length + ' arguments, but ' + step + ' declares only ' + declared.length +
|
|
645
|
+
' (' + declared.join(' ') + '); extras would shift positions. Put them in the toolScript params instead of forcing them in')
|
|
646
646
|
}
|
|
647
647
|
return { step, script, file, runCwd, timeoutMs, argvArgs }
|
|
648
648
|
}
|
|
@@ -717,20 +717,20 @@ export function buildToolCallToolDefinition({ dshHome, logger }) {
|
|
|
717
717
|
render: (_args, value) => [{ type: 'text', text: value.text ?? '' }]
|
|
718
718
|
},
|
|
719
719
|
async execute(args, exec) {
|
|
720
|
-
if (args === null || typeof args !== 'object' || Array.isArray(args)) throw new Error('
|
|
720
|
+
if (args === null || typeof args !== 'object' || Array.isArray(args)) throw new Error('arguments must be an object')
|
|
721
721
|
const cwd = exec?.agent?.session?.header?.cwd
|
|
722
|
-
if (typeof cwd !== 'string' || cwd === '') throw new Error(TOOL_CALL_TOOL_NAME + '
|
|
722
|
+
if (typeof cwd !== 'string' || cwd === '') throw new Error(TOOL_CALL_TOOL_NAME + ' needs to know the current workspace')
|
|
723
723
|
|
|
724
724
|
// 不带参数:列名单(注入被关掉时也能看见有什么)
|
|
725
725
|
if (args.toolScript === undefined && args.calls === undefined) {
|
|
726
726
|
const list = await listToolScripts(dshHome)
|
|
727
727
|
const text = list.length === 0
|
|
728
|
-
? '
|
|
729
|
-
: ['
|
|
730
|
-
'· ' + b.id + '
|
|
731
|
-
(b.params.length > 0 ? '
|
|
732
|
-
(b.steps.length > 0 ? '
|
|
733
|
-
'', '
|
|
728
|
+
? 'The toolSequence is empty. To install a toolScript, write the chain you just ran with ' + TOOL_CREATE_WRITE_NAME + ' as a toolScript (commands can come from the current context, or be recovered from a review original with ' + TOOL_CREATE_READ_NAME + ').'
|
|
729
|
+
: ['ToolSequence · ' + list.length + ' toolScript(s):', ...list.map((b) =>
|
|
730
|
+
'· ' + b.id + ' (' + b.title + '): ' + b.purpose + '|entry ' + b.entry +
|
|
731
|
+
(b.params.length > 0 ? '|args ' + b.params.join(' ') : '') +
|
|
732
|
+
(b.steps.length > 0 ? '|steps ' + b.steps.join(' → ') : '')),
|
|
733
|
+
'', 'Run: session_tool_call({ toolScript: "<id>" }); add dryRun: true to see the commands first.'].join('\n')
|
|
734
734
|
return jsonSafe({ mode: 'list', text })
|
|
735
735
|
}
|
|
736
736
|
|
|
@@ -738,9 +738,9 @@ export function buildToolCallToolDefinition({ dshHome, logger }) {
|
|
|
738
738
|
const requests = []
|
|
739
739
|
if (Array.isArray(args.calls)) {
|
|
740
740
|
if (args.toolScript !== undefined || args.step !== undefined) {
|
|
741
|
-
throw new Error('
|
|
741
|
+
throw new Error('with calls, do not also pass top-level toolScript / step: each item carries its own')
|
|
742
742
|
}
|
|
743
|
-
if (args.calls.length === 0) throw new Error('calls
|
|
743
|
+
if (args.calls.length === 0) throw new Error('calls must not be an empty array')
|
|
744
744
|
// 0.4.11:去掉「一次最多 20 条」的死上线,按给的顺序全跑。
|
|
745
745
|
args.calls.forEach((c, i) => { validateCallShape(c, 'calls[' + i + ']:'); requests.push(c) })
|
|
746
746
|
} else {
|
|
@@ -798,9 +798,9 @@ export function buildToolCallToolDefinition({ dshHome, logger }) {
|
|
|
798
798
|
} catch (err) {
|
|
799
799
|
// 只有真跑才会暴露的错(解释器不在、spawn 失败):补上现场后照样抛,不吞。
|
|
800
800
|
const done = runs.length > 0
|
|
801
|
-
? '\n\
|
|
801
|
+
? '\n\nthe ' + runs.length + ' call(s) that finished first in this invocation (output preserved):\n' + renderRuns({ runs, dryRun: false, skipped: 0 })
|
|
802
802
|
: ''
|
|
803
|
-
throw new Error('
|
|
803
|
+
throw new Error('this call did not start, so the whole invocation counts as failed: ' + p.id + ' / ' + p.step + '\n' +
|
|
804
804
|
String(err?.message ?? err) + done, { cause: err })
|
|
805
805
|
}
|
|
806
806
|
// 0.4.11:超时不再杀进程,转后台继续跑——这不是失败,是「还没跑完」。
|
|
@@ -824,9 +824,9 @@ export function buildToolCallToolDefinition({ dshHome, logger }) {
|
|
|
824
824
|
const crashed = runs.filter((r) => r.status === 'fail')
|
|
825
825
|
if (crashed.length > 0) {
|
|
826
826
|
const names = crashed.map((r) => r.toolScript + ' / ' + r.step +
|
|
827
|
-
'
|
|
827
|
+
'(exit ' + (r.exitCode === null ? 'null' : r.exitCode) + (r.timedOut ? ', killed on timeout' : '') + ')').join(', ')
|
|
828
828
|
throw new Error(
|
|
829
|
-
'
|
|
829
|
+
'【Invocation failed】' + crashed.length + ' call(s) did not run: ' + names + '\n\n' +
|
|
830
830
|
renderRuns({ runs, dryRun: false, skipped })
|
|
831
831
|
)
|
|
832
832
|
}
|
|
@@ -854,44 +854,44 @@ export function buildToolCallToolDefinition({ dshHome, logger }) {
|
|
|
854
854
|
/** 跑完之后的回报文本:一条一块,失败先说清楚在哪一步。 */
|
|
855
855
|
export function renderRuns({ runs, dryRun, skipped }) {
|
|
856
856
|
const lines = []
|
|
857
|
-
if (dryRun) lines.push('
|
|
857
|
+
if (dryRun) lines.push('Echo only, nothing executed (dryRun) — check the argv before running for real.')
|
|
858
858
|
for (const r of runs) {
|
|
859
859
|
if (r.status === 'dry') {
|
|
860
860
|
lines.push('▸ ' + r.toolScript + ' / ' + r.step + '(' + r.run + ')')
|
|
861
861
|
lines.push(' argv: ' + JSON.stringify(r.argv))
|
|
862
|
-
lines.push(' cwd: ' + r.cwd + '
|
|
862
|
+
lines.push(' cwd: ' + r.cwd + ' timeout: ' + r.timeoutMs + 'ms output cap: ' + r.maxOutputBytes + ' bytes')
|
|
863
863
|
if (r.limits) lines.push(' ' + r.limits)
|
|
864
864
|
continue
|
|
865
865
|
}
|
|
866
866
|
if (r.status === 'error') {
|
|
867
|
-
lines.push('✗ ' + r.toolScript + (r.step ? (' / ' + r.step) : '') + '
|
|
867
|
+
lines.push('✗ ' + r.toolScript + (r.step ? (' / ' + r.step) : '') + ' did not start: ' + r.error)
|
|
868
868
|
continue
|
|
869
869
|
}
|
|
870
870
|
if (r.status === 'background') {
|
|
871
871
|
// 0.4.10 及以前这里是「超时已终止」;0.4.11 起进程活着,输出继续往日志里写。
|
|
872
|
-
lines.push('⏳ ' + r.toolScript + ' / ' + r.step + '
|
|
872
|
+
lines.push('⏳ ' + r.toolScript + ' / ' + r.step + ' 【still running after ' + r.timeoutMs +
|
|
873
873
|
'ms; moved to background】pid ' + r.pid)
|
|
874
|
-
lines.push('
|
|
874
|
+
lines.push(' It was not killed; it keeps running and will keep writing to this file:')
|
|
875
875
|
lines.push(' ' + r.logFile + (r.stderrFile ? '(stderr: ' + r.stderrFile + ')' : ''))
|
|
876
876
|
const head = String(r.stdout ?? '').replace(/\s+$/, '')
|
|
877
|
-
if (head !== '') lines.push(' ——
|
|
877
|
+
if (head !== '') lines.push(' —— output so far ——', head)
|
|
878
878
|
const errHead = String(r.stderr ?? '').replace(/\s+$/, '')
|
|
879
879
|
if (errHead !== '') lines.push(' —— stderr ——', errHead)
|
|
880
|
-
lines.push('
|
|
880
|
+
lines.push(' To follow it: read that file (tail it); to stop it: kill ' + r.pid)
|
|
881
881
|
continue
|
|
882
882
|
}
|
|
883
883
|
const head = (r.status === 'ok' ? '✔ ' : '✗ ') + r.toolScript + ' / ' + r.step +
|
|
884
884
|
' exit ' + (r.exitCode === null ? 'null' : r.exitCode) +
|
|
885
885
|
' ' + r.durationMs + 'ms' +
|
|
886
|
-
(r.drift ? '
|
|
886
|
+
(r.drift ? ' 【script hash differs from the manifest; explicitly allowed this time】' : '')
|
|
887
887
|
lines.push(head)
|
|
888
888
|
const out = String(r.stdout ?? '').replace(/\s+$/, '')
|
|
889
889
|
const err = String(r.stderr ?? '').replace(/\s+$/, '')
|
|
890
890
|
if (out !== '') lines.push(out)
|
|
891
891
|
if (err !== '') lines.push('--- stderr ---', err)
|
|
892
|
-
if (r.truncated) lines.push('
|
|
892
|
+
if (r.truncated) lines.push('(output truncated; full content: ' + (r.spill || 'spill failed') + ')')
|
|
893
893
|
}
|
|
894
|
-
if (skipped > 0) lines.push(
|
|
894
|
+
if (skipped > 0) lines.push(skipped + ' call(s) did not run (an earlier one failed, so stopOnError took effect).')
|
|
895
895
|
return lines.join('\n')
|
|
896
896
|
}
|
|
897
897
|
|