deep-read-summarize 0.4.0 → 0.6.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/.env.example +1 -1
- package/CHANGELOG.md +46 -0
- package/README.en.md +36 -10
- package/README.md +36 -10
- package/SECURITY.md +2 -2
- package/cordis.patch.yml +6 -0
- package/docs/ASR_BENCHMARK.md +46 -0
- package/docs/RELEASE.md +16 -1
- package/index.js +36 -8
- package/lib/options.js +13 -5
- package/lib/parser-bundle.js +11 -1
- package/lib/read-cache.js +115 -0
- package/lib/transcript-cache.js +173 -0
- package/lib/workflow-script.txt +166 -19
- package/package.json +3 -2
- package/parsers/_prompt.js +1 -0
- package/parsers/video.js +5 -3
- package/schemas/index.js +12 -0
- package/scripts/chunk-balance.js +71 -0
- package/scripts/transcribe.js +107 -13
- package/skills/deep-read-summarize/SKILL.md +10 -6
- package/workflow.js +1 -1
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// Compute line-boundary candidates from the actual saved source. A line's
|
|
4
|
+
// character count is a cheap workload proxy, not a model tokenizer. One very
|
|
5
|
+
// long line remains whole so that the workflow's line citations stay valid.
|
|
6
|
+
const fs = require('node:fs');
|
|
7
|
+
|
|
8
|
+
function balanceText(text, maxGroups) {
|
|
9
|
+
if (!Number.isInteger(maxGroups) || maxGroups < 1 || maxGroups > 12) throw new Error('maxGroups must be an integer in 1..12');
|
|
10
|
+
const lines = text.split(/\r\n|\n|\r/);
|
|
11
|
+
if (lines.length > 1 && lines[lines.length - 1] === '') lines.pop();
|
|
12
|
+
const totalLines = lines.length;
|
|
13
|
+
const cumulative = [0];
|
|
14
|
+
for (const line of lines) {
|
|
15
|
+
let length = 0;
|
|
16
|
+
for (const _character of line) length++;
|
|
17
|
+
cumulative.push(cumulative[cumulative.length - 1] + Math.max(1, length));
|
|
18
|
+
}
|
|
19
|
+
const totalWeight = cumulative[totalLines];
|
|
20
|
+
const candidates = [];
|
|
21
|
+
for (let groups = 1; groups <= Math.min(maxGroups, totalLines); groups++) {
|
|
22
|
+
const endLines = [];
|
|
23
|
+
let previous = 0;
|
|
24
|
+
for (let group = 1; group < groups; group++) {
|
|
25
|
+
const min = previous + 1;
|
|
26
|
+
const max = totalLines - (groups - group);
|
|
27
|
+
const target = totalWeight * group / groups;
|
|
28
|
+
let low = min, high = max;
|
|
29
|
+
while (low < high) {
|
|
30
|
+
const middle = Math.floor((low + high) / 2);
|
|
31
|
+
if (cumulative[middle] < target) low = middle + 1;
|
|
32
|
+
else high = middle;
|
|
33
|
+
}
|
|
34
|
+
let boundary = low;
|
|
35
|
+
if (low > min && Math.abs(cumulative[low - 1] - target) <= Math.abs(cumulative[low] - target)) boundary = low - 1;
|
|
36
|
+
// Prefer the end of a paragraph within a small workload tolerance, so
|
|
37
|
+
// structure is preserved without creating markedly lopsided chunks.
|
|
38
|
+
const tolerance = totalWeight / groups * 0.05;
|
|
39
|
+
let bestParagraph = -1;
|
|
40
|
+
let bestDistance = Infinity;
|
|
41
|
+
for (let candidate = Math.max(min, boundary - 30); candidate <= Math.min(max, boundary + 30); candidate++) {
|
|
42
|
+
if (lines[candidate - 1].trim() !== '') continue;
|
|
43
|
+
const distance = Math.abs(cumulative[candidate] - target);
|
|
44
|
+
if (distance <= Math.abs(cumulative[boundary] - target) + tolerance && distance < bestDistance) {
|
|
45
|
+
bestParagraph = candidate;
|
|
46
|
+
bestDistance = distance;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
if (bestParagraph !== -1) boundary = bestParagraph;
|
|
50
|
+
endLines.push(boundary);
|
|
51
|
+
previous = boundary;
|
|
52
|
+
}
|
|
53
|
+
endLines.push(totalLines);
|
|
54
|
+
candidates.push({ groups, endLines });
|
|
55
|
+
}
|
|
56
|
+
return { totalLines, candidates };
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
if (require.main === module) {
|
|
60
|
+
try {
|
|
61
|
+
if (process.argv.length !== 4) throw new Error('usage: node chunk-balance.js <source-file> <max-groups>');
|
|
62
|
+
const file = process.argv[2];
|
|
63
|
+
const groups = Number(process.argv[3]);
|
|
64
|
+
process.stdout.write(JSON.stringify(balanceText(fs.readFileSync(file, 'utf8'), groups)) + '\n');
|
|
65
|
+
} catch (error) {
|
|
66
|
+
process.stderr.write('chunk balancing failed: ' + error.message + '\n');
|
|
67
|
+
process.exitCode = 1;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
module.exports = { balanceText };
|
package/scripts/transcribe.js
CHANGED
|
@@ -20,6 +20,7 @@ const { spawnSync } = require('node:child_process');
|
|
|
20
20
|
const fs = require('node:fs');
|
|
21
21
|
const os = require('node:os');
|
|
22
22
|
const path = require('node:path');
|
|
23
|
+
const transcriptCache = require('../lib/transcript-cache');
|
|
23
24
|
|
|
24
25
|
// 新环境使用 Python 3.12;ASR 主包固定为已验证版本。
|
|
25
26
|
// 复用环境不强制更换解释器;实际 Python / 传递依赖版本记录在 runtime.json。
|
|
@@ -175,19 +176,24 @@ function isFileAt(p) {
|
|
|
175
176
|
|
|
176
177
|
/** 解析命令行(支持 --k v、--k=v、-k v 三种写法)。 */
|
|
177
178
|
function parseArgs(argv) {
|
|
178
|
-
const out = { audio: '', out: '', model: 'small', language: 'zh', device: 'cpu',
|
|
179
|
+
const out = { audio: '', out: '', model: 'small', language: 'zh', device: 'cpu', cpuThreads: 0,
|
|
180
|
+
cache: true, refreshCache: false, clearCache: false, timestamps: false, selfCheck: false, help: false, unknown: [] };
|
|
179
181
|
const takes = {
|
|
180
182
|
'--audio': 'audio', '-a': 'audio',
|
|
181
183
|
'--out': 'out', '-o': 'out',
|
|
182
184
|
'--model': 'model', '-m': 'model',
|
|
183
185
|
'--language': 'language', '-l': 'language', '--lang': 'language',
|
|
184
|
-
'--device': 'device', '-d': 'device'
|
|
186
|
+
'--device': 'device', '-d': 'device', '--cpu-threads': 'cpuThreads'
|
|
185
187
|
};
|
|
186
188
|
const list = argv || [];
|
|
187
189
|
for (let i = 0; i < list.length; i++) {
|
|
188
190
|
const a = String(list[i]);
|
|
189
191
|
if (a === '--help' || a === '-h') { out.help = true; continue; }
|
|
190
192
|
if (a === '--self-check') { out.selfCheck = true; continue; }
|
|
193
|
+
if (a === '--no-cache' || a === '--cache=false') { out.cache = false; continue; }
|
|
194
|
+
if (a === '--cache' || a === '--cache=true') { out.cache = true; continue; }
|
|
195
|
+
if (a === '--refresh-cache') { out.refreshCache = true; continue; }
|
|
196
|
+
if (a === '--clear-cache') { out.clearCache = true; continue; }
|
|
191
197
|
if (a === '--timestamps' || a === '--timestamps=true') { out.timestamps = true; continue; }
|
|
192
198
|
if (a === '--no-timestamps' || a === '--timestamps=false') { out.timestamps = false; continue; }
|
|
193
199
|
if (takes[a]) {
|
|
@@ -210,7 +216,10 @@ const PY_RUNNER = [
|
|
|
210
216
|
"import sys",
|
|
211
217
|
"import json",
|
|
212
218
|
"import tempfile",
|
|
219
|
+
"import time",
|
|
213
220
|
"import importlib.metadata as metadata",
|
|
221
|
+
"runner_started = time.perf_counter()",
|
|
222
|
+
"timings = {'import': 0, 'modelLoad': 0, 'prepare': 0, 'inference': 0, 'write': 0}",
|
|
214
223
|
"",
|
|
215
224
|
"def bootstrap_required(code, reason, message):",
|
|
216
225
|
" status_file = os.environ.get('DRS_BOOTSTRAP_STATUS')",
|
|
@@ -233,6 +242,7 @@ const PY_RUNNER = [
|
|
|
233
242
|
" from faster_whisper import WhisperModel",
|
|
234
243
|
"except ModuleNotFoundError as exc:",
|
|
235
244
|
" bootstrap_required(" + DEPENDENCY_MISSING + ", 'missing-dependency', 'missing import dependency: %s' % exc)",
|
|
245
|
+
"timings['import'] = (time.perf_counter() - runner_started) * 1000",
|
|
236
246
|
"",
|
|
237
247
|
"runtime_versions = None",
|
|
238
248
|
"def record_runtime(device, phase):",
|
|
@@ -247,7 +257,7 @@ const PY_RUNNER = [
|
|
|
247
257
|
" runtime_versions[name] = metadata.version(name)",
|
|
248
258
|
" except metadata.PackageNotFoundError:",
|
|
249
259
|
" runtime_versions[name] = None",
|
|
250
|
-
" runtime = {'python': sys.version.split()[0], 'packages': runtime_versions, 'model': os.environ['DRS_MODEL'], 'device': device, 'computeType': 'int8', 'phase': phase}",
|
|
260
|
+
" runtime = {'python': sys.version.split()[0], 'packages': runtime_versions, 'model': os.environ['DRS_MODEL'], 'device': device, 'computeType': 'int8', 'cpuThreads': int(os.environ.get('DRS_CPU_THREADS', '0')), 'phase': phase}",
|
|
251
261
|
" temporary = None",
|
|
252
262
|
" try:",
|
|
253
263
|
" with tempfile.NamedTemporaryFile(mode='w', encoding='utf-8', dir=os.path.dirname(target), prefix='.runtime-', suffix='.json', delete=False) as f:",
|
|
@@ -265,12 +275,23 @@ const PY_RUNNER = [
|
|
|
265
275
|
" print('[transcribe] runtime python=%s faster-whisper=%s device=%s phase=%s' % (runtime['python'], runtime_versions['faster-whisper'], device, phase), file=sys.stderr)",
|
|
266
276
|
"",
|
|
267
277
|
"def load(device):",
|
|
268
|
-
" return WhisperModel(os.environ['DRS_MODEL'], device=device, compute_type='int8')",
|
|
278
|
+
" return WhisperModel(os.environ['DRS_MODEL'], device=device, compute_type='int8', cpu_threads=int(os.environ.get('DRS_CPU_THREADS', '0')))",
|
|
269
279
|
"",
|
|
270
280
|
"def run(device):",
|
|
271
|
-
"
|
|
281
|
+
" started = time.perf_counter()",
|
|
282
|
+
" try:",
|
|
283
|
+
" model = load(device)",
|
|
284
|
+
" finally:",
|
|
285
|
+
" timings['modelLoad'] += (time.perf_counter() - started) * 1000",
|
|
286
|
+
" started = time.perf_counter()",
|
|
272
287
|
" segs, info = model.transcribe(os.environ['DRS_AUDIO'], vad_filter=True, language=(os.environ.get('DRS_LANG') or None))",
|
|
273
|
-
"
|
|
288
|
+
" timings['prepare'] += (time.perf_counter() - started) * 1000",
|
|
289
|
+
" started = time.perf_counter()",
|
|
290
|
+
" try:",
|
|
291
|
+
" segments = [{'start': s.start, 'end': s.end, 'text': s.text.strip().replace('\\r', ' ').replace('\\n', ' ')} for s in segs if s.text.strip()]",
|
|
292
|
+
" finally:",
|
|
293
|
+
" timings['inference'] += (time.perf_counter() - started) * 1000",
|
|
294
|
+
" return segments, info, getattr(getattr(model, 'model', None), 'device', device)",
|
|
274
295
|
"",
|
|
275
296
|
"def timestamp(seconds):",
|
|
276
297
|
" millis = max(0, int(round(float(seconds) * 1000)))",
|
|
@@ -284,18 +305,19 @@ const PY_RUNNER = [
|
|
|
284
305
|
"device = os.environ.get('DRS_DEVICE') or 'cpu'",
|
|
285
306
|
"record_runtime(device, 'starting')",
|
|
286
307
|
"try:",
|
|
287
|
-
" segments, info = run(device)",
|
|
308
|
+
" segments, info, device = run(device)",
|
|
288
309
|
"except Exception as exc:",
|
|
289
310
|
" if device == 'cpu':",
|
|
290
311
|
" raise",
|
|
291
312
|
" print('[transcribe] device=%s failed (%s); retrying on cpu' % (device, exc), file=sys.stderr)",
|
|
292
313
|
" device = 'cpu'",
|
|
293
314
|
" record_runtime(device, 'starting')",
|
|
294
|
-
" segments, info = run('cpu')",
|
|
315
|
+
" segments, info, device = run('cpu')",
|
|
295
316
|
"record_runtime(device, 'completed')",
|
|
296
317
|
"",
|
|
297
318
|
"# DRS_OUT 是本次调用独有的暂存路径;Node 校验后才原子发布到最终路径。",
|
|
298
319
|
"# 先收集全部分段再落盘,中途推理失败不会留下半截文本。",
|
|
320
|
+
"write_started = time.perf_counter()",
|
|
299
321
|
"include_timestamps = os.environ.get('DRS_TIMESTAMPS') == '1'",
|
|
300
322
|
"lines = []",
|
|
301
323
|
"for segment in segments:",
|
|
@@ -307,6 +329,11 @@ const PY_RUNNER = [
|
|
|
307
329
|
" lines.append(text)",
|
|
308
330
|
"with open(os.environ['DRS_OUT'], 'w', encoding='utf-8', newline='\\n') as f:",
|
|
309
331
|
" f.write('\\n'.join(lines))",
|
|
332
|
+
"timings['write'] = (time.perf_counter() - write_started) * 1000",
|
|
333
|
+
"timings['total'] = (time.perf_counter() - runner_started) * 1000",
|
|
334
|
+
"if os.environ.get('DRS_SEGMENTS'):",
|
|
335
|
+
" with open(os.environ['DRS_SEGMENTS'], 'w', encoding='utf-8') as f:",
|
|
336
|
+
" json.dump({'segments': segments, 'device': device, 'cpuThreads': int(os.environ.get('DRS_CPU_THREADS', '0')), 'duration': getattr(info, 'duration', None), 'durationAfterVad': getattr(info, 'duration_after_vad', None), 'timings': timings}, f, ensure_ascii=False, allow_nan=False)",
|
|
310
337
|
""
|
|
311
338
|
].join("\n");
|
|
312
339
|
|
|
@@ -320,7 +347,9 @@ const USAGE = [
|
|
|
320
347
|
'',
|
|
321
348
|
'选项:--audio/-a --out/-o --model/-m(默认 small) --language/-l(默认 zh)',
|
|
322
349
|
' --device(默认 cpu;auto/cuda 需自备 CUDA 运行库,失败会自动回退 cpu)',
|
|
323
|
-
' --timestamps(逐段保留真实起止时间;默认关闭,关闭时仍按段换行)'
|
|
350
|
+
' --timestamps(逐段保留真实起止时间;默认关闭,关闭时仍按段换行)',
|
|
351
|
+
' --cpu-threads 0..64(0 保持库默认值);--no-cache 禁用逐字稿缓存',
|
|
352
|
+
' --refresh-cache 强制重转写并更新缓存;--clear-cache 仅清理逐字稿缓存'
|
|
324
353
|
].join('\n');
|
|
325
354
|
|
|
326
355
|
function fail(msg, code) {
|
|
@@ -350,6 +379,8 @@ function needsBootstrap(result, statusFile) {
|
|
|
350
379
|
}
|
|
351
380
|
|
|
352
381
|
function main(argv) {
|
|
382
|
+
const startedAt = Date.now();
|
|
383
|
+
const timing = { hash: 0, environment: 0, cacheLookup: 0, dependencyInstall: 0, python: 0, publish: 0, total: 0 };
|
|
353
384
|
const args = parseArgs(argv);
|
|
354
385
|
const c = context({});
|
|
355
386
|
const root = cacheRoot(c);
|
|
@@ -358,7 +389,6 @@ function main(argv) {
|
|
|
358
389
|
const uv = findUv(c);
|
|
359
390
|
|
|
360
391
|
if (args.help) { process.stdout.write(USAGE + '\n'); return 0; }
|
|
361
|
-
|
|
362
392
|
// --self-check:CI 与排查用,只报告解析结果,不产生任何副作用。
|
|
363
393
|
if (args.selfCheck) {
|
|
364
394
|
process.stdout.write(JSON.stringify({
|
|
@@ -376,7 +406,15 @@ function main(argv) {
|
|
|
376
406
|
return 0;
|
|
377
407
|
}
|
|
378
408
|
|
|
409
|
+
if (args.clearCache) {
|
|
410
|
+
try { transcriptCache.clear(root); process.stdout.write('[transcribe] transcript cache cleared\n'); return 0; }
|
|
411
|
+
catch (error) { return fail('无法清理逐字稿缓存:' + error.message); }
|
|
412
|
+
}
|
|
413
|
+
|
|
379
414
|
if (args.unknown.length) process.stderr.write('[transcribe] 忽略未知参数:' + args.unknown.join(', ') + '\n');
|
|
415
|
+
if (!/^\d+$/.test(String(args.cpuThreads))) return fail('--cpu-threads 必须为 0..64 的整数');
|
|
416
|
+
args.cpuThreads = Number(args.cpuThreads);
|
|
417
|
+
if (!Number.isInteger(args.cpuThreads) || args.cpuThreads < 0 || args.cpuThreads > 64) return fail('--cpu-threads 必须为 0..64 的整数');
|
|
380
418
|
if (!args.audio || !args.out) {
|
|
381
419
|
process.stderr.write(USAGE + '\n');
|
|
382
420
|
return fail('--audio 与 --out 都是必填的。');
|
|
@@ -401,28 +439,52 @@ function main(argv) {
|
|
|
401
439
|
// 与最终文件处于同一文件系统,rename 才能原子发布;每次调用独占暂存目录。
|
|
402
440
|
stagingDir = fs.mkdtempSync(path.join(path.dirname(outputPath), '.drs-transcribe-'));
|
|
403
441
|
const stagingOutput = path.join(stagingDir, 'transcript.txt');
|
|
442
|
+
const segmentsFile = path.join(stagingDir, 'segments.json');
|
|
443
|
+
const hashStarted = Date.now();
|
|
444
|
+
const audioHash = args.cache ? transcriptCache.hashFile(args.audio) : null;
|
|
445
|
+
timing.hash = Date.now() - hashStarted;
|
|
446
|
+
const lookupStarted = Date.now();
|
|
447
|
+
const runtimeBefore = args.cache ? transcriptCache.runtimeIdentity(root, args.model) : null;
|
|
448
|
+
const id = transcriptCache.identity(audioHash, args, runtimeBefore, PY_RUNNER);
|
|
449
|
+
const cached = args.cache && !args.refreshCache ? transcriptCache.read(root, id) : null;
|
|
450
|
+
timing.cacheLookup = Date.now() - lookupStarted;
|
|
451
|
+
if (cached) {
|
|
452
|
+
fs.writeFileSync(stagingOutput, transcriptCache.render(cached.segments, args.timestamps), 'utf8');
|
|
453
|
+
const publishStarted = Date.now();
|
|
454
|
+
fs.renameSync(stagingOutput, outputPath);
|
|
455
|
+
timing.publish = Date.now() - publishStarted;
|
|
456
|
+
timing.total = Date.now() - startedAt;
|
|
457
|
+
process.stdout.write('[transcribe] metrics ' + JSON.stringify({ cacheHit: true, device: cached.device,
|
|
458
|
+
cpuThreads: cached.cpuThreads, duration: cached.duration, timings: timing }) + '\n');
|
|
459
|
+
process.stdout.write('TRANSCRIBED ' + fs.statSync(outputPath).size + ' bytes to ' + args.out + '\nDONE\n');
|
|
460
|
+
return 0;
|
|
461
|
+
}
|
|
404
462
|
|
|
405
463
|
const childEnv = Object.assign({}, process.env);
|
|
464
|
+
const environmentStarted = Date.now();
|
|
406
465
|
|
|
407
466
|
// 1) venv:优先复用已装好的 3.12,没有才让 uv 装。
|
|
408
467
|
// 镜像只用于加速;失败就换直连重试 —— 镜像挂掉不该让用户彻底装不上。
|
|
409
|
-
if (!
|
|
468
|
+
if (!isFileAt(py)) {
|
|
410
469
|
if (uv === null) throw new Error('需要创建 Python 环境,但没有找到 uv。请先安装:' + uvInstallHint(c));
|
|
411
470
|
process.stdout.write('[transcribe] 创建 venv(Python ' + PY_VERSION + '):' + venvDir + '\n');
|
|
412
471
|
const attempts = venvEnvAttempts(childEnv, resolveUvMirror(process.env));
|
|
413
472
|
let lastErr = null;
|
|
473
|
+
let created = false;
|
|
414
474
|
for (let i = 0; i < attempts.length; i++) {
|
|
415
475
|
try {
|
|
416
476
|
runStep(uv, ['venv', '--python', PY_VERSION, venvDir], attempts[i].env, 'uv venv');
|
|
477
|
+
if (!isFileAt(py)) throw new Error('uv venv 未生成 Python 解释器文件:' + py);
|
|
478
|
+
created = true;
|
|
417
479
|
} catch (e) { lastErr = e; }
|
|
418
|
-
if (
|
|
480
|
+
if (created) break;
|
|
419
481
|
// 失败的尝试可能留下半个 venv 目录,重试前先清掉
|
|
420
482
|
try { fs.rmSync(venvDir, { recursive: true, force: true }); } catch (e) { /* 清不掉就让下一次尝试自己报错 */ }
|
|
421
483
|
if (i + 1 < attempts.length) {
|
|
422
484
|
process.stderr.write('[transcribe] 用 ' + attempts[i].label + ' 创建 venv 失败,改用 ' + attempts[i + 1].label + ' 重试…\n');
|
|
423
485
|
}
|
|
424
486
|
}
|
|
425
|
-
if (!
|
|
487
|
+
if (!created) throw (lastErr || new Error('uv venv 失败:' + venvDir));
|
|
426
488
|
}
|
|
427
489
|
|
|
428
490
|
// 2) 健康环境直接运行;不另开 Python 重复 import,也不要求 uv 仍在 PATH。
|
|
@@ -437,6 +499,8 @@ function main(argv) {
|
|
|
437
499
|
childEnv.DRS_LANG = args.language;
|
|
438
500
|
childEnv.DRS_DEVICE = args.device;
|
|
439
501
|
childEnv.DRS_TIMESTAMPS = args.timestamps ? '1' : '0';
|
|
502
|
+
childEnv.DRS_CPU_THREADS = String(args.cpuThreads);
|
|
503
|
+
childEnv.DRS_SEGMENTS = segmentsFile;
|
|
440
504
|
childEnv.DRS_FASTER_WHISPER_VERSION = FASTER_WHISPER_VERSION;
|
|
441
505
|
childEnv.DRS_RUNTIME = path.join(root, 'runtime.json');
|
|
442
506
|
const bootstrapStatus = path.join(stagingDir, 'bootstrap-status.json');
|
|
@@ -444,6 +508,8 @@ function main(argv) {
|
|
|
444
508
|
|
|
445
509
|
const pyScript = path.join(stagingDir, 'transcribe_run.py');
|
|
446
510
|
fs.writeFileSync(pyScript, PY_RUNNER, 'utf8');
|
|
511
|
+
timing.environment = Date.now() - environmentStarted;
|
|
512
|
+
const pythonStarted = Date.now();
|
|
447
513
|
|
|
448
514
|
// 3) 转写(首次会下载模型到 HF_HOME,之后缓存复用)。
|
|
449
515
|
// 只有导入缺失/明确版本不符才允许自举并重试一次,解码/CUDA/推理失败不重装。
|
|
@@ -451,19 +517,47 @@ function main(argv) {
|
|
|
451
517
|
if (needsBootstrap(result, bootstrapStatus)) {
|
|
452
518
|
if (uv === null) throw new Error('需要安装 faster-whisper==' + FASTER_WHISPER_VERSION + ',但没有找到 uv。请先安装:' + uvInstallHint(c));
|
|
453
519
|
process.stdout.write('[transcribe] 安装 faster-whisper==' + FASTER_WHISPER_VERSION + '(清华镜像)…\n');
|
|
520
|
+
const installStarted = Date.now();
|
|
454
521
|
runStep(uv, ['pip', 'install', '--python', py, 'faster-whisper==' + FASTER_WHISPER_VERSION, '-i', PYPI_MIRROR], childEnv, 'faster-whisper 安装');
|
|
522
|
+
timing.dependencyInstall = Date.now() - installStarted;
|
|
455
523
|
fs.rmSync(bootstrapStatus, { force: true });
|
|
456
524
|
runStep(py, [pyScript], childEnv, '转写');
|
|
457
525
|
} else {
|
|
458
526
|
checkStep(result, py, '转写');
|
|
459
527
|
}
|
|
528
|
+
timing.python = Date.now() - pythonStarted - timing.dependencyInstall;
|
|
460
529
|
|
|
461
530
|
const bytes = fs.statSync(stagingOutput).size;
|
|
462
531
|
const verdict = judgeTranscript(bytes);
|
|
463
532
|
if (!verdict.ok) {
|
|
464
533
|
return fail(verdict.message, verdict.code);
|
|
465
534
|
}
|
|
535
|
+
let report = null;
|
|
536
|
+
try {
|
|
537
|
+
const candidate = JSON.parse(fs.readFileSync(segmentsFile, 'utf8'));
|
|
538
|
+
if (transcriptCache.validSegments(candidate.segments) &&
|
|
539
|
+
transcriptCache.render(candidate.segments, args.timestamps) === fs.readFileSync(stagingOutput, 'utf8')) report = candidate;
|
|
540
|
+
} catch (error) { /* A missing diagnostic must not discard a valid transcript. */ }
|
|
541
|
+
const publishStarted = Date.now();
|
|
466
542
|
fs.renameSync(stagingOutput, outputPath);
|
|
543
|
+
timing.publish = Date.now() - publishStarted;
|
|
544
|
+
if (args.cache && report) {
|
|
545
|
+
try {
|
|
546
|
+
const runtimeAfter = transcriptCache.runtimeIdentity(root, args.model);
|
|
547
|
+
// Do not cache audio modified during inference, or a model/environment
|
|
548
|
+
// changed concurrently. A newly bootstrapped environment is eligible.
|
|
549
|
+
if ((!runtimeBefore || runtimeBefore === runtimeAfter) && audioHash === transcriptCache.hashFile(args.audio)) {
|
|
550
|
+
const stored = transcriptCache.write(root, transcriptCache.identity(audioHash, args, runtimeAfter, PY_RUNNER), report);
|
|
551
|
+
if (!stored) process.stderr.write('[transcribe] transcript cache unavailable; current result retained\n');
|
|
552
|
+
}
|
|
553
|
+
} catch (error) {
|
|
554
|
+
process.stderr.write('[transcribe] transcript cache unavailable; current result retained\n');
|
|
555
|
+
}
|
|
556
|
+
}
|
|
557
|
+
timing.total = Date.now() - startedAt;
|
|
558
|
+
process.stdout.write('[transcribe] metrics ' + JSON.stringify({ cacheHit: false, device: report && report.device,
|
|
559
|
+
cpuThreads: args.cpuThreads, duration: report && report.duration, timings: timing,
|
|
560
|
+
runnerTimings: report && report.timings }) + '\n');
|
|
467
561
|
process.stdout.write('TRANSCRIBED ' + bytes + ' bytes to ' + args.out + '\n');
|
|
468
562
|
process.stdout.write('DONE\n');
|
|
469
563
|
return 0;
|
|
@@ -23,7 +23,7 @@ whenToUse: 用户提供一本书、论文(arXiv/PDF)、视频链接(YouTub
|
|
|
23
23
|
## 输入
|
|
24
24
|
|
|
25
25
|
用户提供:内容链接或本地文件路径。
|
|
26
|
-
可选参数:`type`(auto/book/paper/video/web)、`options`(minWords / fastMode / maxChunks / maxRetries / requireCitations / includeTimestamps / transcribe / whisperModel / language / device / outputDir / tempDir / frontmatter)。
|
|
26
|
+
可选参数:`type`(auto/book/paper/video/web)、`options`(minWords / fastMode / maxChunks / readConcurrency / parallelQuality / readCache / readCacheIdentity / maxRetries / requireCitations / includeTimestamps / transcribe / whisperModel / language / device / cpuThreads / transcriptCache / outputDir / tempDir / frontmatter)。
|
|
27
27
|
|
|
28
28
|
优先调用插件提供的 `deep_read_summarize` 工具,参数为 `{ input, type, options }`。如果当前宿主没有这个工具,按技能末尾提供的 meta / script / args 调用 `workflow` 工具。两种入口执行相同的获取、精读与质量核验。
|
|
29
29
|
|
|
@@ -39,16 +39,18 @@ whenToUse: 用户提供一本书、论文(arXiv/PDF)、视频链接(YouTub
|
|
|
39
39
|
1. **配置校验**:缺 input 或 type 非法 → 报 FATAL 终止
|
|
40
40
|
2. **幂等检查(由你执行,脚本不做)**:脚本在沙箱内没有文件系统,写不了指纹记录。落盘前先看目标 `filePath` 是否已有同名笔记——已存在就问用户是覆盖还是跳过,别默默重跑一遍烧 token。(要在程序里做去重,可用插件导出的 `lib/cache.js`:fingerprint / hasProcessed / markProcessed。)
|
|
41
41
|
3. **解析器选择**:按类型从 parsers 注册表选解析器(book/paper/video/web)。`auto` 对明确视频链接、arXiv 论文链接、电子书扩展名直接分流;同站的图文/频道/帮助页、PDF/未知网页/文本等模糊输入先轻量识别,不能固定当作网页。识别失败返回 `stage: "detect"`,再请用户显式指定类型
|
|
42
|
-
4. **波次1 获取+分块**:解析器获取全文 → 写入本次独立的临时子目录 → 生成分块计划(JSON Schema 校验)。`tempDir` 是根目录,不要复用其他 `run-*` 任务的 input/audio/transcript
|
|
43
|
-
5. **波次2 并行精读**:每个分块一个子代理深度精读(Map
|
|
42
|
+
4. **波次1 获取+分块**:解析器获取全文 → 写入本次独立的临时子目录 → 生成分块计划(JSON Schema 校验)。`tempDir` 是根目录,不要复用其他 `run-*` 任务的 input/audio/transcript;规范化全部区间,优先用已保存文本的字符数均衡工作量(兼顾相近的段落边界);统计缺失或无效时按行数均衡,始终保持从第 1 行到 totalLines 的完整覆盖,不得截断原文
|
|
43
|
+
5. **波次2 并行精读**:每个分块一个子代理深度精读(Map),最多 `readConcurrency` 个槽位同时工作(默认 3,可按宿主能力调整),空闲即领取下一块,输出保持分块顺序
|
|
44
44
|
6. **波次3 合并成稿**:整合为完整笔记(Reduce),内嵌质量自检
|
|
45
|
-
7. **质量校验**:程序检查 frontmatter 模板、必要章节和篇幅;核验代理必须读取本次原文,逐块提供摘录、对第 4
|
|
45
|
+
7. **质量校验**:程序检查 frontmatter 模板、必要章节和篇幅;核验代理必须读取本次原文,逐块提供摘录、对第 4 节逐字引用提供原句与行号。`parallelQuality: true` 时多分块由独立代理并行核验各块及全部显式引文,另由代理检查全局术语与语言一致性;默认沿用原核验路径,`readConcurrency` 同时限制并行核验并发。不得只看笔记或仅返回 pass。`qualityEvidence` 可供回查;核验失败必须如实报告。`maxRetries` 为 0–3,修订失败、空结果或结构退化时保留上一份有效成稿。缺总行数、精读失败、仅简介或未知视频来源等问题不能靠改写笔记消除,不进行徒劳全文修订
|
|
46
46
|
8. **落盘(必须,别跳过)**:精读工具只返回 `note`(笔记正文)与 `filePath`(建议路径)——**脚本本身没有文件系统权限**。拿到结果后要用 `write` 工具把 `note` 原样写入 `filePath`(目录不存在就先创建);不写就等于笔记丢了,用户只会看到一段返回文本
|
|
47
47
|
9. **如实交代结果**:`ok: false` 要说明卡在哪一步(`stage`)和原因,别拿半成品冒充成品;`qualityPassed: false`、`qualityIssues` 非空、或 `failedChunks > 0`(有块精读失败、笔记存在缺口)时,必须在回复里点出来
|
|
48
48
|
|
|
49
|
+
跨次精读缓存默认关闭,仅插件工具入口支持;启用 `readCache` 时必须填写 `readCacheIdentity`(供应商、固定模型版本及输出相关设置)。身份由调用者保证,变更时必须更换;缓存明文保存在 `tempDir/.read-cache-v1`,可能包含原文摘录,命中后仍独立核验原文。
|
|
50
|
+
|
|
49
51
|
## 输出
|
|
50
52
|
|
|
51
|
-
精读工具返回 `{ ok, filePath, note, qualityPassed, qualityIssues, qualityChecks, qualityEvidence, failedChunks, coverageComplete, sourceComplete, textSource, timings }`。`coverageComplete: false` 表示缺少有效总行数,无法确认尾部覆盖;`sourceComplete: false` 表示视频仅有简介或来源未确认,笔记会保留来源限制且质量降级。来源完整性仍依据获取代理声明,不能当作视频真实内容已全部证明。`timings` 提供 detect/fetch/read/draft/quality/total
|
|
53
|
+
精读工具返回 `{ ok, filePath, note, qualityPassed, qualityIssues, qualityChecks, qualityEvidence, failedChunks, coverageComplete, sourceComplete, textSource, timings, readTimings }`。`coverageComplete: false` 表示缺少有效总行数,无法确认尾部覆盖;`sourceComplete: false` 表示视频仅有简介或来源未确认,笔记会保留来源限制且质量降级。来源完整性仍依据获取代理声明,不能当作视频真实内容已全部证明。`timings` 提供 detect/fetch/read/draft/quality/total 毫秒耗时;`readTimings` 提供精读块的行号、毫秒耗时、成功状态及缓存命中状态,不能由此推测未提供的 token 用量。
|
|
52
54
|
|
|
53
55
|
> ⚠️ **`note` 就是完整笔记正文,`filePath` 只是建议路径**:脚本运行在无文件系统的沙箱里(DSH 的 workflow 契约:脚本只负责协调子代理),**它不会替你写文件**。必须由你(主代理)在拿到结果后用 `write` 工具把它写到 `filePath`——默认 `./output/`,可用 `options.outputDir` 指向 Obsidian 仓库或静态站点的内容目录。
|
|
54
56
|
|
|
@@ -81,7 +83,9 @@ status: 已完成
|
|
|
81
83
|
3. 都不行 → 降级提示用户手动提供转写文本,或退回 desc 作背景;**绝不阻塞、绝不自动装重依赖**。
|
|
82
84
|
- `options.transcribe`:**默认 true**(无字幕自动转写);设为 `false` 则跳过转写(只用字幕/desc 或降级)。
|
|
83
85
|
- **转写工具链**:脚本自举,本机/用户一致——`uv` 建 Python 3.12 环境 + 清华镜像装 `faster-whisper` + `HF_ENDPOINT=https://hf-mirror.com` 下模型并缓存;脚本是 **Node** 写的,三平台**同一份实现、同一条命令**(`node <脚本路径> --audio <音频> --out <txt>`);转写默认 **CPU**(`options.device` 默认 `cpu`),GPU 需自备 CUDA 运行库(设 `options.device: "auto"`,失败自动回退 CPU);faster-whisper 内置 PyAV 解码音频,**无需单独 ffmpeg**。
|
|
84
|
-
- **⚠️ 首次转写会先下载 small 模型(约 484MB,hf-mirror,非 GitHub
|
|
86
|
+
- **⚠️ 首次转写会先下载 small 模型(约 484MB,hf-mirror,非 GitHub),可能较慢**——执行转写前**先向用户说明**这是正常的一次性下载;之后复用模型文件,新音频仍需推理,不保证秒开。
|
|
87
|
+
- `options.transcriptCache` 默认 true:只有脚本核对音频内容、受管理的模型快照、环境和解码配置后才可复用逐字稿,命中也必须确认本次进程成功并发布本次产物,不能直接使用别的任务的旧文件。缓存含本地文本、有效期 30 天、总量上限 128 MiB;`--clear-cache` 只清理逐字稿。设 false 禁用读写,`--refresh-cache` 可强制重转写。缓存不会跳过精读和独立核验。
|
|
88
|
+
- `options.cpuThreads` 为 0–64 的整数,默认 0 保持库默认策略。记录转写日志的 metrics:cacheHit、实际设备、音频时长与阶段耗时;命中缓存时不要引用上次推理耗时,也不要由此推测 token。笔记核验不能证明音轨听写准确,线程或 GPU 调优需单独比对音频,不自行降低模型或解码门槛。
|
|
85
89
|
- **质量/速度**:small/int8 + VAD 静音过滤,中文质量可用且 CPU 友好;有字幕视频不转写(快)。装不上/失败则降级,不静默。
|
|
86
90
|
- YouTube 用 yt-dlp 抓 CC;未装时**绝不下载 exe 二进制**(GitHub 直连易卡死),按系统装(Windows `winget install yt-dlp.yt-dlp` / macOS `brew install yt-dlp` / 任意平台 `pip install -U yt-dlp`);仍不可用则转写或降级。
|
|
87
91
|
- 运行 yt-dlp 加 `--socket-timeout 15 --retries 3` 防超时。
|
package/workflow.js
CHANGED
|
@@ -77,7 +77,7 @@ function buildSelfContainedScript(parserRegistry = parsers.registry) {
|
|
|
77
77
|
if (!RAW_SCRIPT.includes(marker)) {
|
|
78
78
|
throw new Error("workflow script marker not found: " + marker);
|
|
79
79
|
}
|
|
80
|
-
const runtimePaths = { transcribeScript: fs.realpathSync(path.join(__dirname, 'scripts', 'transcribe.js')) };
|
|
80
|
+
const runtimePaths = { transcribeScript: fs.realpathSync(path.join(__dirname, 'scripts', 'transcribe.js')), chunkBalanceScript: fs.realpathSync(path.join(__dirname, 'scripts', 'chunk-balance.js')) };
|
|
81
81
|
return 'const __runtimePaths = ' + JSON.stringify(runtimePaths) + ';\n' +
|
|
82
82
|
'const normalizeWorkflowOptions = ' + normalizeWorkflowOptions.toString() + ';\n' +
|
|
83
83
|
'const __quality = { checkNoteStructure: ' + quality.checkNoteStructure.toString() +
|