deep-read-summarize 0.3.6 → 0.3.9
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/CHANGELOG.md +97 -1
- package/README.en.md +314 -0
- package/README.md +41 -14
- package/cordis.patch.yml +14 -7
- package/docs/RELEASE.md +44 -7
- package/index.js +64 -8
- package/package.json +7 -5
- package/parsers/book.js +1 -1
- package/parsers/video.js +54 -18
- package/schemas/index.js +9 -2
- package/scripts/transcribe.js +306 -0
- package/skills/deep-read-summarize/SKILL.md +15 -9
- package/tests/index.test.js +319 -1
- package/tests/run-tests.js +166 -0
- package/workflow.js +89 -76
- package/scripts/transcribe.ps1 +0 -68
package/tests/index.test.js
CHANGED
|
@@ -77,4 +77,322 @@ test('skills bundle exists', () => {
|
|
|
77
77
|
test('fixture is self-authored public-domain', () => {
|
|
78
78
|
const fx = fs.readFileSync(path.join(__dirname, 'fixtures', 'fable.txt'), 'utf8');
|
|
79
79
|
assert.ok(fx.includes('released into the public domain'));
|
|
80
|
-
});
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
// ============================================================================
|
|
84
|
+
// workflow 运行时回归(0.3.8)
|
|
85
|
+
// 直接跑自包含脚本(mock 掉 agent / parallel / phase / log),覆盖本次修掉的
|
|
86
|
+
// P0:分块计划失控、负数读取区间、frontmatter 注入、幂等早返回丢数据。
|
|
87
|
+
// ============================================================================
|
|
88
|
+
const plugin = require('../index.js');
|
|
89
|
+
const wfScript = wf.script;
|
|
90
|
+
|
|
91
|
+
function makeAgent(cfg) {
|
|
92
|
+
cfg = cfg || {};
|
|
93
|
+
return function (prompt, o) {
|
|
94
|
+
const label = (o && o.label) || '';
|
|
95
|
+
if (label === '获取+分块') {
|
|
96
|
+
if (cfg.fetch !== undefined) return cfg.fetch;
|
|
97
|
+
return {
|
|
98
|
+
kind: cfg.kind || 'paper',
|
|
99
|
+
saved: true,
|
|
100
|
+
totalLines: cfg.totalLines === undefined ? 1000 : cfg.totalLines,
|
|
101
|
+
metadata: cfg.metadata || { title: cfg.title || 'Sample' },
|
|
102
|
+
chunkPlan: cfg.chunkPlan || [
|
|
103
|
+
{ id: 1, topic: 'T1', startLine: 1, endLine: 500 },
|
|
104
|
+
{ id: 2, topic: 'T2', startLine: 501, endLine: 1000 }
|
|
105
|
+
]
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
if (label === '汇总成稿') return cfg.note === undefined ? '# note' : cfg.note;
|
|
109
|
+
if (label === '质量校验') return { pass: cfg.pass !== false, issues: cfg.issues || [] };
|
|
110
|
+
return 'chunk text';
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
async function runScript(args, agentImpl, scriptText) {
|
|
115
|
+
const calls = [];
|
|
116
|
+
const fn = new Function('args', 'phase', 'agent', 'parallel', 'log',
|
|
117
|
+
'return (async () => { ' + (scriptText || wfScript) + ' })()');
|
|
118
|
+
const result = await fn(
|
|
119
|
+
args,
|
|
120
|
+
function () {},
|
|
121
|
+
function (p, o) { calls.push({ label: (o && o.label) || '', prompt: p }); return agentImpl(p, o); },
|
|
122
|
+
function (fns) { return Promise.all(fns.map(function (f) { return f(); })); },
|
|
123
|
+
function () {}
|
|
124
|
+
);
|
|
125
|
+
return { result: result, calls: calls };
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
function injectedFetchSchema(scriptText) {
|
|
129
|
+
const m = (scriptText || wfScript).match(/const fetchSchema = (\{[\s\S]*?\});\n/);
|
|
130
|
+
assert.ok(m, 'fetchSchema is not injected into the script');
|
|
131
|
+
return JSON.parse(m[1]);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
test('workflow: chunk plan is clamped to maxChunks', async () => {
|
|
135
|
+
const plan = [];
|
|
136
|
+
for (let i = 1; i <= 10; i++) plan.push({ id: i, topic: 'T' + i, startLine: (i - 1) * 100 + 1, endLine: i * 100 });
|
|
137
|
+
const r = await runScript({ input: 'x', type: 'paper', options: { maxChunks: 4 } }, makeAgent({ chunkPlan: plan }));
|
|
138
|
+
assert.strictEqual(r.result.chunksCount, 4, 'the plan must be clamped, not passed through');
|
|
139
|
+
assert.strictEqual(r.calls.filter(function (c) { return c.label === '精读块'; }).length, 4,
|
|
140
|
+
'one sub-agent per chunk - 10 chunks means 10 sub-agents');
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
test('workflow: inverted and out-of-range chunks are dropped', async () => {
|
|
144
|
+
const r = await runScript({ input: 'x', type: 'paper' }, makeAgent({
|
|
145
|
+
totalLines: 500,
|
|
146
|
+
chunkPlan: [
|
|
147
|
+
{ id: 1, topic: 'inverted', startLine: 900, endLine: 100 },
|
|
148
|
+
{ id: 2, topic: 'beyond-eof', startLine: 9000, endLine: 9500 },
|
|
149
|
+
{ id: 3, topic: 'ok', startLine: 1, endLine: 500 }
|
|
150
|
+
]
|
|
151
|
+
}));
|
|
152
|
+
assert.strictEqual(r.result.chunksCount, 1);
|
|
153
|
+
const readPrompt = r.calls.find(function (c) { return c.label === '精读块'; }).prompt;
|
|
154
|
+
assert.ok(!/limit=-/.test(readPrompt), 'read must never be handed a negative limit');
|
|
155
|
+
assert.ok(/limit=500/.test(readPrompt), 'the valid chunk must survive intact');
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
test('workflow: an all-invalid chunk plan degrades instead of running', async () => {
|
|
159
|
+
const r = await runScript({ input: 'x', type: 'paper' },
|
|
160
|
+
makeAgent({ chunkPlan: [{ id: 1, topic: 'bad', startLine: 900, endLine: 100 }] }));
|
|
161
|
+
assert.strictEqual(r.result.ok, false);
|
|
162
|
+
assert.strictEqual(r.result.fatal, false);
|
|
163
|
+
assert.strictEqual(r.result.stage, 'fetch');
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
test('workflow: frontmatter escapes quotes coming from the content title', async () => {
|
|
167
|
+
const r = await runScript({ input: 'https://example.com/x', type: 'paper' },
|
|
168
|
+
makeAgent({ title: 'The "Best" Method' }));
|
|
169
|
+
const draft = r.calls.find(function (c) { return c.label === '汇总成稿'; }).prompt;
|
|
170
|
+
assert.ok(!draft.includes('title: "《The "Best" Method》'), 'raw quotes must not reach the frontmatter');
|
|
171
|
+
assert.ok(draft.includes('title: "《The \\"Best\\" Method》深度精读笔记"'), 'title must be escaped');
|
|
172
|
+
assert.ok(/url: "https:\/\/example\.com\/x"/.test(draft), 'url must be a quoted scalar');
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
test('workflow: a title of nothing but illegal characters still yields a usable filename', async () => {
|
|
176
|
+
const r = await runScript({ input: 'x', type: 'web' }, makeAgent({ kind: 'web', title: '///' }));
|
|
177
|
+
assert.ok(!/\/\.md$/.test(r.result.filePath), 'must not write a hidden .md: ' + r.result.filePath);
|
|
178
|
+
assert.ok(/未命名内容\.md$/.test(r.result.filePath), r.result.filePath);
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
test('workflow: unknown kind under type=auto falls back to a registered type', async () => {
|
|
182
|
+
const r = await runScript({ input: 'x', type: 'auto' }, makeAgent({
|
|
183
|
+
fetch: { saved: true, totalLines: 10, chunkPlan: [{ id: 1, topic: 't', startLine: 1, endLine: 10 }] }
|
|
184
|
+
}));
|
|
185
|
+
assert.notStrictEqual(r.result.kind, 'auto');
|
|
186
|
+
assert.ok(Object.keys(wf.parsers.registry.parsers).indexOf(r.result.kind) !== -1, r.result.kind);
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
test('workflow: no idempotency early-return is left in the script', () => {
|
|
190
|
+
assert.ok(!wfScript.includes('_processedKeys'), 'the unreachable cache-hit branch must be gone');
|
|
191
|
+
assert.ok(!/cached: true/.test(wfScript), 'the empty-note early return must be gone');
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
test('workflow: transcribe=false actually changes the video prompt', () => {
|
|
195
|
+
const vp = wf.parsers.resolve('video');
|
|
196
|
+
const on = vp.buildPrompt('https://b23.tv/x', { tempDir: './.tmp' });
|
|
197
|
+
const off = vp.buildPrompt('https://b23.tv/x', { tempDir: './.tmp', transcribe: false });
|
|
198
|
+
assert.notStrictEqual(on, off, 'transcribe:false used to produce a byte-identical prompt');
|
|
199
|
+
assert.ok(on.includes('transcribe.js'), 'the prompt must point at the cross-platform script');
|
|
200
|
+
assert.ok(!on.includes('transcribe.ps1'), 'the Windows-only PowerShell script must be gone');
|
|
201
|
+
assert.ok(!off.includes('--model '), 'transcribe=false must not ask for a transcription run');
|
|
202
|
+
assert.ok(off.includes('转写已禁用'));
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
test('workflow: whisperModel and language reach the transcription command', () => {
|
|
206
|
+
const vp = wf.parsers.resolve('video');
|
|
207
|
+
assert.ok(vp.buildPrompt('u', { tempDir: './.tmp', whisperModel: 'medium', language: 'en' })
|
|
208
|
+
.includes('--device cpu --model medium --language en'), 'documented ASR options must be honoured');
|
|
209
|
+
assert.ok(vp.buildPrompt('u', { tempDir: './.tmp' }).includes('--device cpu --model small --language zh'),
|
|
210
|
+
'defaults stay small/zh');
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
test('workflow: the script forwards whisperModel and language to the parser', async () => {
|
|
214
|
+
const r = await runScript({ input: 'https://b23.tv/x', type: 'video', options: { whisperModel: 'base', language: 'ja' } },
|
|
215
|
+
makeAgent({ kind: 'video' }));
|
|
216
|
+
const fetchPrompt = r.calls.find(function (c) { return c.label === '获取+分块'; }).prompt;
|
|
217
|
+
assert.ok(fetchPrompt.includes('--model base --language ja'), 'options must survive the script -> parser boundary');
|
|
218
|
+
});
|
|
219
|
+
|
|
220
|
+
test('plugin: config is validated and folded into the skill args', () => {
|
|
221
|
+
const content = plugin.buildSkillContent({ outputDir: 'D:/vault/notes', maxChunks: 8 });
|
|
222
|
+
assert.ok(content.includes('D:/vault/notes'), 'config.outputDir must reach the args example');
|
|
223
|
+
assert.ok(content.includes('"maxChunks": 8'));
|
|
224
|
+
assert.strictEqual(plugin.normalizeOptions({ maxChunks: 'lots' }).maxChunks, 4, 'wrong type -> default, not crash');
|
|
225
|
+
assert.strictEqual(plugin.normalizeOptions({ options: { minWords: 4000 } }).minWords, 4000, 'nested form too');
|
|
226
|
+
const registered = [];
|
|
227
|
+
plugin.apply({ skills: { register: function (s) { registered.push(s); return function () {}; } } }, { minWords: 4000 });
|
|
228
|
+
assert.ok(registered[0].content.includes('"minWords": 4000'), 'apply() must pass config through');
|
|
229
|
+
});
|
|
230
|
+
|
|
231
|
+
test('workflow: the injected fetch schema comes from schemas/index.js', () => {
|
|
232
|
+
const injected = injectedFetchSchema();
|
|
233
|
+
assert.deepStrictEqual(injected.required, schemas.fetchResultSchema.required);
|
|
234
|
+
assert.strictEqual(injected.additionalProperties, schemas.fetchResultSchema.additionalProperties);
|
|
235
|
+
assert.deepStrictEqual(injected.properties.kind.enum, Object.keys(wf.parsers.registry.parsers));
|
|
236
|
+
assert.strictEqual((wfScript.match(/const fetchSchema = \{/g) || []).length, 1,
|
|
237
|
+
'no hand-copied duplicate may survive in the script');
|
|
238
|
+
});
|
|
239
|
+
|
|
240
|
+
test('workflow: a custom parser type is inlined and accepted end to end', async () => {
|
|
241
|
+
const reg = wf.parsers.registry;
|
|
242
|
+
const had = Object.prototype.hasOwnProperty.call(reg.parsers, 'podcast');
|
|
243
|
+
const original = reg.parsers.podcast;
|
|
244
|
+
reg.parsers.podcast = {
|
|
245
|
+
name: 'podcast',
|
|
246
|
+
types: ['podcast'],
|
|
247
|
+
buildPrompt: function (input) { return 'PODCAST PROMPT ' + input; }
|
|
248
|
+
};
|
|
249
|
+
try {
|
|
250
|
+
const regenerated = wf.buildSelfContainedScript();
|
|
251
|
+
assert.ok(regenerated.includes('"podcast": { buildPrompt:'), 'custom parser must be inlined');
|
|
252
|
+
assert.ok(injectedFetchSchema(regenerated).properties.kind.enum.includes('podcast'),
|
|
253
|
+
'the kind enum must follow the parser registry');
|
|
254
|
+
assert.ok(!regenerated.includes('auto|book|paper|video|web'),
|
|
255
|
+
'the type whitelist must not be hard-coded to the four built-ins');
|
|
256
|
+
|
|
257
|
+
const r = await runScript({ input: 'https://x.test/feed', type: 'podcast' },
|
|
258
|
+
makeAgent({ kind: 'podcast', totalLines: 20, chunkPlan: [{ id: 1, topic: 't', startLine: 1, endLine: 20 }] }),
|
|
259
|
+
regenerated);
|
|
260
|
+
assert.strictEqual(r.result.ok, true);
|
|
261
|
+
assert.strictEqual(r.result.kind, 'podcast');
|
|
262
|
+
} finally {
|
|
263
|
+
if (had) reg.parsers.podcast = original; else delete reg.parsers.podcast;
|
|
264
|
+
}
|
|
265
|
+
});
|
|
266
|
+
|
|
267
|
+
test('workflow: an unknown type is still rejected loudly', async () => {
|
|
268
|
+
await assert.rejects(
|
|
269
|
+
runScript({ input: 'x', type: 'nope' }, makeAgent({})),
|
|
270
|
+
/\[FATAL\] args\.type 非法/
|
|
271
|
+
);
|
|
272
|
+
});
|
|
273
|
+
|
|
274
|
+
test('cache: the utility is reachable through the package exports map', () => {
|
|
275
|
+
const pkg = require('../package.json');
|
|
276
|
+
assert.strictEqual(pkg.exports['./lib/cache'], './lib/cache.js');
|
|
277
|
+
assert.ok(fs.existsSync(path.join(__dirname, '..', 'lib', 'cache.js')));
|
|
278
|
+
});
|
|
279
|
+
|
|
280
|
+
// ============================================================================
|
|
281
|
+
// 跨平台转写引导脚本(0.3.9)
|
|
282
|
+
// transcribe.js 只在宿主 shell 里跑(不进 workflow 沙箱),平台差异集中在四个纯函数;
|
|
283
|
+
// 它们都接受注入的 platform/env/home,所以任意 OS 上都能跑全平台矩阵断言。
|
|
284
|
+
// ============================================================================
|
|
285
|
+
const transcribe = require('../scripts/transcribe.js');
|
|
286
|
+
|
|
287
|
+
test('transcribe: cache root matches the documented per-platform locations', () => {
|
|
288
|
+
// Windows 必须与 0.3.6-0.3.8 的 transcribe.ps1 完全一致:
|
|
289
|
+
// 老用户已经下过 484MB 模型,路径一变就要重下。
|
|
290
|
+
assert.strictEqual(
|
|
291
|
+
transcribe.cacheRoot({ platform: 'win32', env: { LOCALAPPDATA: 'C:/AppData/Local' }, home: 'C:/acct' }),
|
|
292
|
+
path.win32.join('C:/AppData/Local', 'deep-read-summarize'));
|
|
293
|
+
assert.strictEqual(
|
|
294
|
+
transcribe.cacheRoot({ platform: 'win32', env: {}, home: 'C:/acct' }),
|
|
295
|
+
path.win32.join('C:/acct/AppData/Local', 'deep-read-summarize'),
|
|
296
|
+
'LOCALAPPDATA absent -> fall back to the standard Windows location');
|
|
297
|
+
assert.strictEqual(transcribe.cacheRoot({ platform: 'darwin', env: {}, home: '/Users/me' }),
|
|
298
|
+
'/Users/me/Library/Caches/deep-read-summarize');
|
|
299
|
+
assert.strictEqual(transcribe.cacheRoot({ platform: 'linux', env: {}, home: '/home/me' }),
|
|
300
|
+
'/home/me/.cache/deep-read-summarize');
|
|
301
|
+
assert.strictEqual(transcribe.cacheRoot({ platform: 'linux', env: { XDG_CACHE_HOME: '/x' }, home: '/home/me' }),
|
|
302
|
+
'/x/deep-read-summarize', 'XDG_CACHE_HOME wins on Linux');
|
|
303
|
+
});
|
|
304
|
+
|
|
305
|
+
test('transcribe: venv interpreter path is per-platform', () => {
|
|
306
|
+
assert.strictEqual(transcribe.venvPython('/v', { platform: 'darwin' }), '/v/bin/python');
|
|
307
|
+
assert.strictEqual(transcribe.venvPython('/v', { platform: 'linux' }), '/v/bin/python');
|
|
308
|
+
assert.strictEqual(transcribe.venvPython('C:/v', { platform: 'win32' }),
|
|
309
|
+
path.win32.join('C:/v', 'Scripts', 'python.exe'));
|
|
310
|
+
});
|
|
311
|
+
|
|
312
|
+
test('transcribe: the uv install hint follows the platform', () => {
|
|
313
|
+
assert.ok(transcribe.uvInstallHint({ platform: 'darwin' }).includes('brew install uv'));
|
|
314
|
+
assert.ok(transcribe.uvInstallHint({ platform: 'win32' }).includes('winget install astral-sh.uv'));
|
|
315
|
+
assert.ok(transcribe.uvInstallHint({ platform: 'linux' }).includes('astral.sh/uv/install.sh'));
|
|
316
|
+
});
|
|
317
|
+
|
|
318
|
+
test('transcribe: uv discovery prefers PATH, then the usual install dirs', () => {
|
|
319
|
+
const has = (list) => (p) => list.indexOf(p) >= 0;
|
|
320
|
+
assert.strictEqual(
|
|
321
|
+
transcribe.findUv({ platform: 'linux', env: { PATH: '/usr/bin:/bin' }, home: '/home/me',
|
|
322
|
+
isFile: has(['/usr/bin/uv']) }), '/usr/bin/uv');
|
|
323
|
+
assert.strictEqual(
|
|
324
|
+
transcribe.findUv({ platform: 'darwin', env: { PATH: '/usr/bin' }, home: '/Users/me',
|
|
325
|
+
isFile: has(['/opt/homebrew/bin/uv']) }), '/opt/homebrew/bin/uv',
|
|
326
|
+
'Homebrew on Apple silicon must be found even when it is not on PATH');
|
|
327
|
+
assert.strictEqual(
|
|
328
|
+
transcribe.findUv({ platform: 'darwin', env: { PATH: '/usr/bin' }, home: '/Users/me',
|
|
329
|
+
isFile: has(['/Users/me/.local/bin/uv']) }), '/Users/me/.local/bin/uv');
|
|
330
|
+
assert.strictEqual(
|
|
331
|
+
transcribe.findUv({ platform: 'win32', env: { PATH: 'C:/bin' }, home: 'C:/acct',
|
|
332
|
+
isFile: has([path.win32.join('C:/bin', 'uv.exe')]) }), path.win32.join('C:/bin', 'uv.exe'));
|
|
333
|
+
assert.strictEqual(transcribe.findUv({ platform: 'darwin', env: { PATH: '/usr/bin' }, home: '/Users/me',
|
|
334
|
+
isFile: () => false }), null, 'no uv -> null (the caller degrades with an install hint)');
|
|
335
|
+
});
|
|
336
|
+
|
|
337
|
+
test('transcribe: CLI parsing accepts long, = and short forms', () => {
|
|
338
|
+
const a = transcribe.parseArgs(['--audio', 'a.m4a', '--out', 'o.txt', '--model', 'medium', '--language', 'en']);
|
|
339
|
+
assert.deepStrictEqual([a.audio, a.out, a.model, a.language], ['a.m4a', 'o.txt', 'medium', 'en']);
|
|
340
|
+
assert.strictEqual(a.device, 'cpu', 'CPU is the default: GPU needs a CUDA runtime we do not install');
|
|
341
|
+
|
|
342
|
+
const b = transcribe.parseArgs(['--audio=a.m4a', '--out=o.txt', '--device=auto']);
|
|
343
|
+
assert.deepStrictEqual([b.audio, b.out, b.device], ['a.m4a', 'o.txt', 'auto']);
|
|
344
|
+
|
|
345
|
+
const c = transcribe.parseArgs(['-a', 'a.m4a', '-o', 'o.txt', '-l', 'ja']);
|
|
346
|
+
assert.deepStrictEqual([c.audio, c.out, c.language], ['a.m4a', 'o.txt', 'ja']);
|
|
347
|
+
|
|
348
|
+
assert.strictEqual(transcribe.parseArgs(['--self-check']).selfCheck, true);
|
|
349
|
+
assert.deepStrictEqual(transcribe.parseArgs(['nonsense']).unknown, ['nonsense']);
|
|
350
|
+
});
|
|
351
|
+
|
|
352
|
+
test('transcribe: the runner never leaves a half-written transcript behind', () => {
|
|
353
|
+
// 调用方按「输出文件非空且稳定」判断转写完成,所以必须先收集全部分段再落盘,
|
|
354
|
+
// 否则中途失败会留下半截文本被当成成功。
|
|
355
|
+
assert.ok(transcribe.PY_RUNNER.includes('segments, info = run(device)'),
|
|
356
|
+
'segments must be collected before the file is opened');
|
|
357
|
+
assert.ok(/with open\(os\.environ\['DRS_OUT'\]/.test(transcribe.PY_RUNNER));
|
|
358
|
+
assert.strictEqual(transcribe.PY_RUNNER.indexOf('with open'),
|
|
359
|
+
transcribe.PY_RUNNER.lastIndexOf('with open'), 'exactly one write, after collection');
|
|
360
|
+
});
|
|
361
|
+
|
|
362
|
+
test('skill: the Windows-only transcription caveat is gone', () => {
|
|
363
|
+
const content = fs.readFileSync(path.join(__dirname, '..', 'skills', 'deep-read-summarize', 'SKILL.md'), 'utf8');
|
|
364
|
+
assert.ok(!content.includes('仅在 Windows 可用'), 'the macOS/Linux limitation must be lifted');
|
|
365
|
+
assert.ok(content.includes('transcribe.js'), 'the skill must point at the cross-platform script');
|
|
366
|
+
assert.ok(!content.includes('transcribe.ps1'), 'no stale reference to the PowerShell script');
|
|
367
|
+
});
|
|
368
|
+
|
|
369
|
+
test('video prompt: gives both Windows and POSIX ways to locate the script', () => {
|
|
370
|
+
const on = wf.parsers.resolve('video').buildPrompt('u', { tempDir: './.tmp' });
|
|
371
|
+
assert.ok(on.includes('Get-ChildItem') && on.includes('$HOME/.dsh'),
|
|
372
|
+
'the agent picks the locate command for its own OS');
|
|
373
|
+
assert.ok(on.includes('brew install yt-dlp') && on.includes('winget install yt-dlp'),
|
|
374
|
+
'both package managers must be offered');
|
|
375
|
+
assert.ok(!on.includes('--device cuda'), 'GPU is opt-in, never silently requested');
|
|
376
|
+
});
|
|
377
|
+
|
|
378
|
+
test('workflow: options.device reaches the transcription command', () => {
|
|
379
|
+
const vp = wf.parsers.resolve('video');
|
|
380
|
+
assert.ok(vp.buildPrompt('u', { tempDir: './.tmp' }).includes('--device cpu'),
|
|
381
|
+
'CPU is the default: GPU needs a CUDA runtime we deliberately do not install');
|
|
382
|
+
assert.ok(vp.buildPrompt('u', { tempDir: './.tmp', device: 'auto' }).includes('--device auto'),
|
|
383
|
+
'options.device must be honoured, not documented-and-ignored');
|
|
384
|
+
});
|
|
385
|
+
|
|
386
|
+
test('workflow: the script forwards device to the parser', async () => {
|
|
387
|
+
const r = await runScript({ input: 'https://b23.tv/x', type: 'video', options: { device: 'auto' } },
|
|
388
|
+
makeAgent({ kind: 'video' }));
|
|
389
|
+
const fetchPrompt = r.calls.find(function (c) { return c.label === '获取+分块'; }).prompt;
|
|
390
|
+
assert.ok(fetchPrompt.includes('--device auto'), 'options must survive the script -> parser boundary');
|
|
391
|
+
});
|
|
392
|
+
|
|
393
|
+
test('video prompt: the transcription block is a single numbered step', () => {
|
|
394
|
+
const on = wf.parsers.resolve('video').buildPrompt('u', { tempDir: './.tmp' });
|
|
395
|
+
assert.ok(!/[0-9]+\.\s+①/.test(on),
|
|
396
|
+
'the ①-⑥ sub-steps must not be numbered as a top-level step of their own');
|
|
397
|
+
assert.ok(/[0-9]+\. 转写执行/.test(on), 'the block still gets exactly one top-level number');
|
|
398
|
+
});
|
package/tests/run-tests.js
CHANGED
|
@@ -79,6 +79,34 @@ test('fixture fable.txt exists and is public-domain self-authored', () => {
|
|
|
79
79
|
assert.ok(!fx.includes('Copyright ©'));
|
|
80
80
|
});
|
|
81
81
|
|
|
82
|
+
// ---------- Test: plugin entry contract (Cordis) ----------
|
|
83
|
+
// Regression for 0.3.3: the entry exported only { load, workflow, parsers,
|
|
84
|
+
// schemas } - no apply method - so dsh rejected the plugin at startup
|
|
85
|
+
// ("invalid plugin, expect function or object with an \"apply\" method"),
|
|
86
|
+
// the web server exited immediately, and the launcher just sat waiting on a
|
|
87
|
+
// dead port. The entry must satisfy the Cordis contract, always.
|
|
88
|
+
const plugin = require('../index');
|
|
89
|
+
|
|
90
|
+
test('plugin entry satisfies the Cordis contract (function or object with apply)', () => {
|
|
91
|
+
const ok = typeof plugin === 'function' ||
|
|
92
|
+
(plugin && typeof plugin === 'object' && typeof plugin.apply === 'function');
|
|
93
|
+
assert.ok(ok, 'entry must be a function or an object with an apply method');
|
|
94
|
+
assert.ok(typeof plugin.apply === 'function', 'apply must be callable');
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
test('plugin entry carries name + inject for the bundle row', () => {
|
|
98
|
+
assert.ok(typeof plugin.name === 'string' && plugin.name !== '', 'name must be a non-empty string');
|
|
99
|
+
assert.ok(Array.isArray(plugin.inject) && plugin.inject.every((s) => typeof s === 'string'),
|
|
100
|
+
'inject must be an array of strings');
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
test('plugin entry keeps the legacy load/workflow/parsers exports', () => {
|
|
104
|
+
assert.ok(typeof plugin.load === 'function', 'legacy load() export missing');
|
|
105
|
+
assert.ok(plugin.workflow && plugin.workflow.meta, 'workflow export missing');
|
|
106
|
+
assert.ok(plugin.parsers && typeof plugin.parsers.resolve === 'function', 'parsers export missing');
|
|
107
|
+
assert.ok(plugin.schemas, 'schemas export missing');
|
|
108
|
+
});
|
|
109
|
+
|
|
82
110
|
// ---------- Test: security scanner (negative/positive samples) ----------
|
|
83
111
|
const { scanDir } = require('../scripts/security-check');
|
|
84
112
|
test('security scanner flags fake secrets and skips clean/ignored files', () => {
|
|
@@ -98,6 +126,144 @@ test('security scanner flags fake secrets and skips clean/ignored files', () =>
|
|
|
98
126
|
}
|
|
99
127
|
});
|
|
100
128
|
|
|
129
|
+
// ---------- Test: workflow script contract(DSH workflow 工具)----------
|
|
130
|
+
// 现行契约(0.1.2-rc.1):agent/parallel/pipeline/phase/log/args 六个钩子;agent() 选项只允许
|
|
131
|
+
// label/phase/schema/provider/model(其它选项会被引擎判为 fatal);schema 只允许
|
|
132
|
+
// type/properties/required/additionalProperties/items/enum/const/oneOf + 注解键;
|
|
133
|
+
// 脚本沙箱没有文件系统/网络/定时器/require。任何一条违反都会在真机上直接终止整条 workflow。
|
|
134
|
+
const workflow = require('../workflow');
|
|
135
|
+
const wfScript = workflow.script;
|
|
136
|
+
|
|
137
|
+
const ALLOWED_SCHEMA_KEYWORDS = [
|
|
138
|
+
'type', 'properties', 'required', 'additionalProperties', 'items', 'enum', 'const', 'oneOf',
|
|
139
|
+
'description', 'title', 'default', 'examples',
|
|
140
|
+
];
|
|
141
|
+
const ALLOWED_AGENT_OPTIONS = ['label', 'phase', 'schema', 'provider', 'model'];
|
|
142
|
+
|
|
143
|
+
/** 从脚本里取出 \`const XSchema = { ... }\` 的字面量并求值(脚本里的 schema 只是对象字面量)。 */
|
|
144
|
+
function extractSchemaLiterals(text) {
|
|
145
|
+
const out = [];
|
|
146
|
+
const re = /const (\w*[Ss]chema) = \{/g;
|
|
147
|
+
let m;
|
|
148
|
+
while ((m = re.exec(text)) !== null) {
|
|
149
|
+
const from = text.indexOf('{', m.index);
|
|
150
|
+
let depth = 0;
|
|
151
|
+
let i = from;
|
|
152
|
+
for (; i < text.length; i += 1) {
|
|
153
|
+
if (text[i] === '{') depth += 1;
|
|
154
|
+
else if (text[i] === '}') { depth -= 1; if (depth === 0) break; }
|
|
155
|
+
}
|
|
156
|
+
out.push({ name: m[1], value: eval('(' + text.slice(from, i + 1) + ')') }); // eslint-disable-line no-eval
|
|
157
|
+
}
|
|
158
|
+
return out;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* 递归校验一个 JSON Schema 节点:只允许 DSH 子集的关键字。
|
|
163
|
+
* 只有 schema 位置的关键字受限——\`properties\` 下的键名是数据字段名,不参与校验。
|
|
164
|
+
* @returns {string[]} 违规关键字(含路径)
|
|
165
|
+
*/
|
|
166
|
+
function unsupportedSchemaKeywords(node, path = '$') {
|
|
167
|
+
if (node === null || typeof node !== 'object' || Array.isArray(node)) return [];
|
|
168
|
+
const bad = [];
|
|
169
|
+
for (const key of Object.keys(node)) {
|
|
170
|
+
if (!ALLOWED_SCHEMA_KEYWORDS.includes(key)) bad.push(path + '.' + key);
|
|
171
|
+
}
|
|
172
|
+
if (node.properties && typeof node.properties === 'object') {
|
|
173
|
+
for (const [name, child] of Object.entries(node.properties)) {
|
|
174
|
+
bad.push(...unsupportedSchemaKeywords(child, path + '.properties.' + name));
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
if (node.items) bad.push(...unsupportedSchemaKeywords(node.items, path + '.items'));
|
|
178
|
+
if (Array.isArray(node.oneOf)) node.oneOf.forEach((child, i) => bad.push(...unsupportedSchemaKeywords(child, path + '.oneOf[' + i + ']')));
|
|
179
|
+
return bad;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
test('workflow: agent() schemas stay inside the DSH-supported subset', () => {
|
|
183
|
+
// 违反子集 = 引擎抛 UNSUPPORTED_SCHEMA = 整条 workflow 直接终止(不是降级)。
|
|
184
|
+
const schemas = extractSchemaLiterals(wfScript);
|
|
185
|
+
assert.ok(schemas.length >= 2, 'expected the fetch/check schemas, found ' + schemas.length);
|
|
186
|
+
for (const s of schemas) {
|
|
187
|
+
assert.deepStrictEqual(unsupportedSchemaKeywords(s.value), [],
|
|
188
|
+
s.name + ' uses unsupported schema keyword(s)');
|
|
189
|
+
}
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
test('workflow: the schema-subset checker actually rejects an unsupported keyword', () => {
|
|
193
|
+
// 反向断言:确保上面的“通过”不是因为校验器写空了。
|
|
194
|
+
const bad = unsupportedSchemaKeywords({ type: 'object', properties: { a: { type: 'string', minLength: 3 } } });
|
|
195
|
+
assert.deepStrictEqual(bad, ['$.properties.a.minLength']);
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
test('workflow: agent() options stay inside the engine whitelist', () => {
|
|
199
|
+
const re = /agent\([^,]*,\s*\{([^}]*)\}/g;
|
|
200
|
+
let m;
|
|
201
|
+
let calls = 0;
|
|
202
|
+
while ((m = re.exec(wfScript)) !== null) {
|
|
203
|
+
calls += 1;
|
|
204
|
+
const keys = [];
|
|
205
|
+
const keyRe = /([A-Za-z_][A-Za-z0-9_]*)\s*:/g;
|
|
206
|
+
let k;
|
|
207
|
+
while ((k = keyRe.exec(m[1])) !== null) keys.push(k[1]);
|
|
208
|
+
for (const key of keys) {
|
|
209
|
+
assert.ok(ALLOWED_AGENT_OPTIONS.includes(key),
|
|
210
|
+
'agent() option "' + key + '" is not supported by the engine (allowed: ' + ALLOWED_AGENT_OPTIONS.join(', ') + ')');
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
assert.ok(calls >= 5, 'expected the script agent() calls, found ' + calls);
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
test('workflow: the script never reaches for APIs the sandbox does not provide', () => {
|
|
217
|
+
const forbidden = [
|
|
218
|
+
[/\brequire\s*\(/, 'require()'],
|
|
219
|
+
[/\bprocess\s*\./, 'process'],
|
|
220
|
+
[/\bfs\s*\./, 'fs'],
|
|
221
|
+
[/\bfetch\s*\(/, 'fetch()'],
|
|
222
|
+
[/\bsetTimeout\s*\(/, 'setTimeout()'],
|
|
223
|
+
[/\bsetInterval\s*\(/, 'setInterval()'],
|
|
224
|
+
[/\b__dirname\b/, '__dirname'],
|
|
225
|
+
[/\bchild_process\b/, 'child_process'],
|
|
226
|
+
];
|
|
227
|
+
for (const [re, label] of forbidden) {
|
|
228
|
+
assert.ok(!re.test(wfScript), 'the workflow sandbox provides no ' + label);
|
|
229
|
+
}
|
|
230
|
+
});
|
|
231
|
+
|
|
232
|
+
test('workflow: the script self-contains every registered parser', () => {
|
|
233
|
+
// 内联的是注册表里的全部解析器(含 custom-parsers/ 的覆盖与自定义类型),
|
|
234
|
+
// 不是写死的四个 —— 断言跟着注册表走,注册表加了东西这里自动跟着收紧。
|
|
235
|
+
for (const name of Object.keys(workflow.parsers.registry.parsers)) {
|
|
236
|
+
assert.ok(wfScript.includes(JSON.stringify(name) + ': { buildPrompt:'),
|
|
237
|
+
'parser "' + name + '" is not inlined');
|
|
238
|
+
}
|
|
239
|
+
assert.ok(wfScript.includes('const parsers = args._parsers || __parsers;'), 'the args._parsers fallback is missing');
|
|
240
|
+
assert.ok(wfScript.includes('const fetchSchema = {'), 'the fetch schema must be injected, not hand-copied');
|
|
241
|
+
});
|
|
242
|
+
|
|
243
|
+
// ---------- Test: skill content(复用本文件顶部已有的 plugin 引用)----------
|
|
244
|
+
test('skill content carries meta + script + args and the write-back step', () => {
|
|
245
|
+
const content = plugin.buildSkillContent();
|
|
246
|
+
assert.ok(content.includes(workflow.meta.name), 'meta missing from the skill body');
|
|
247
|
+
assert.ok(content.includes(wfScript), 'the workflow script must be embedded verbatim');
|
|
248
|
+
assert.ok(content.includes('"input"'), 'the args example is missing');
|
|
249
|
+
// 脚本没有文件系统权限:技能必须明确要求主代理把 note 落盘,否则笔记会丢。
|
|
250
|
+
assert.ok(/filePath/.test(content) && /write/.test(content), 'the skill must tell the model to persist note -> filePath');
|
|
251
|
+
});
|
|
252
|
+
|
|
253
|
+
test('plugin entry satisfies the Cordis contract and registers one bundled skill', () => {
|
|
254
|
+
assert.strictEqual(plugin.name, 'deep-read-summarize');
|
|
255
|
+
assert.deepStrictEqual(plugin.inject, ['skills']);
|
|
256
|
+
assert.strictEqual(typeof plugin.apply, 'function');
|
|
257
|
+
const registered = [];
|
|
258
|
+
const ctx = { skills: { register: (skill) => { registered.push(skill); return () => {}; } } };
|
|
259
|
+
const dispose = plugin.apply(ctx, {});
|
|
260
|
+
assert.strictEqual(registered.length, 1);
|
|
261
|
+
assert.strictEqual(registered[0].name, 'deep-read-summarize');
|
|
262
|
+
assert.strictEqual(registered[0].source, 'bundled');
|
|
263
|
+
assert.ok(registered[0].content.length > 1000);
|
|
264
|
+
assert.strictEqual(typeof dispose, 'function');
|
|
265
|
+
});
|
|
266
|
+
|
|
101
267
|
// ---------- Run cache tests ----------
|
|
102
268
|
console.log('\n=== cache tests ===');
|
|
103
269
|
const cacheResult = require('./cache.test.js');
|