deep-read-summarize 0.3.18 → 0.3.19

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.
@@ -3,7 +3,7 @@
3
3
  // transcribe.js — faster-whisper 转写自举脚本(插件内部,跨平台:Windows/macOS/Linux)
4
4
  // ============================================================================
5
5
  // 用法(三平台完全一致,不需要 pwsh / bash 之别):
6
- // node transcribe.js --audio <音频文件> --out <输出txt> [--model small] [--language zh] [--device cpu|auto|cuda]
6
+ // node transcribe.js --audio <音频文件> --out <输出txt> [--model small] [--language zh] [--device cpu|auto|cuda] [--timestamps]
7
7
  // node transcribe.js --self-check # 只打印解析出的路径,不安装、不下载、不联网
8
8
  //
9
9
  // 为什么是 Node 而不是 PowerShell / shell:
@@ -171,7 +171,7 @@ function isFileAt(p) {
171
171
 
172
172
  /** 解析命令行(支持 --k v、--k=v、-k v 三种写法)。 */
173
173
  function parseArgs(argv) {
174
- const out = { audio: '', out: '', model: 'small', language: 'zh', device: 'cpu', selfCheck: false, help: false, unknown: [] };
174
+ const out = { audio: '', out: '', model: 'small', language: 'zh', device: 'cpu', timestamps: false, selfCheck: false, help: false, unknown: [] };
175
175
  const takes = {
176
176
  '--audio': 'audio', '-a': 'audio',
177
177
  '--out': 'out', '-o': 'out',
@@ -184,6 +184,8 @@ function parseArgs(argv) {
184
184
  const a = String(list[i]);
185
185
  if (a === '--help' || a === '-h') { out.help = true; continue; }
186
186
  if (a === '--self-check') { out.selfCheck = true; continue; }
187
+ if (a === '--timestamps' || a === '--timestamps=true') { out.timestamps = true; continue; }
188
+ if (a === '--no-timestamps' || a === '--timestamps=false') { out.timestamps = false; continue; }
187
189
  if (takes[a]) {
188
190
  const v = list[i + 1];
189
191
  if (v === undefined || String(v).indexOf('-') === 0) { out.unknown.push(a); continue; }
@@ -198,7 +200,7 @@ function parseArgs(argv) {
198
200
  return out;
199
201
  }
200
202
 
201
- /** 运行时 Python 片段:分段文本以空格连接成一行落盘;device 默认 cpu,非 cpu 失败自动回退。 */
203
+ /** 运行时 Python 片段:逐段换行,可保留真实起止时间;device 默认 cpu,非 cpu 失败自动回退。 */
202
204
  const PY_RUNNER = [
203
205
  "import os",
204
206
  "import sys",
@@ -210,7 +212,14 @@ const PY_RUNNER = [
210
212
  "def run(device):",
211
213
  " model = load(device)",
212
214
  " segs, info = model.transcribe(os.environ['DRS_AUDIO'], vad_filter=True, language=(os.environ.get('DRS_LANG') or None))",
213
- " return [s.text.strip() for s in segs], info",
215
+ " return [{'start': s.start, 'end': s.end, 'text': s.text.strip()} for s in segs], info",
216
+ "",
217
+ "def timestamp(seconds):",
218
+ " millis = max(0, int(round(float(seconds) * 1000)))",
219
+ " hours, millis = divmod(millis, 3600000)",
220
+ " minutes, millis = divmod(millis, 60000)",
221
+ " seconds, millis = divmod(millis, 1000)",
222
+ " return '%02d:%02d:%02d.%03d' % (hours, minutes, seconds, millis)",
214
223
  "",
215
224
  "# device 默认 cpu:ctranslate2 只要探测到 NVIDIA 显卡就会在 auto 下选 CUDA,",
216
225
  "# 而多数机器没装 cuBLAS/cuDNN(cublas64_12.dll),会在推理时硬失败(实测 RTX 3050 复现)。",
@@ -225,8 +234,17 @@ const PY_RUNNER = [
225
234
  "",
226
235
  "# DRS_OUT 是本次调用独有的暂存路径;Node 校验后才原子发布到最终路径。",
227
236
  "# 先收集全部分段再落盘,中途推理失败不会留下半截文本。",
228
- "with open(os.environ['DRS_OUT'], 'w', encoding='utf-8') as f:",
229
- " f.write(' '.join([x for x in segments if x]))",
237
+ "include_timestamps = os.environ.get('DRS_TIMESTAMPS') == '1'",
238
+ "lines = []",
239
+ "for segment in segments:",
240
+ " text = segment['text'].replace('\\r', ' ').replace('\\n', ' ')",
241
+ " if not text:",
242
+ " continue",
243
+ " if include_timestamps:",
244
+ " text = '[%s --> %s] %s' % (timestamp(segment['start']), timestamp(segment['end']), text)",
245
+ " lines.append(text)",
246
+ "with open(os.environ['DRS_OUT'], 'w', encoding='utf-8', newline='\\n') as f:",
247
+ " f.write('\\n'.join(lines))",
230
248
  ""
231
249
  ].join("\n");
232
250
 
@@ -235,11 +253,12 @@ const PY_RUNNER = [
235
253
  // ---------------------------------------------------------------------------
236
254
 
237
255
  const USAGE = [
238
- '用法:node transcribe.js --audio <音频文件> --out <输出txt> [--model small] [--language zh] [--device cpu|auto|cuda]',
256
+ '用法:node transcribe.js --audio <音频文件> --out <输出txt> [--model small] [--language zh] [--device cpu|auto|cuda] [--timestamps]',
239
257
  ' node transcribe.js --self-check # 只打印解析出的路径,不安装不下载',
240
258
  '',
241
259
  '选项:--audio/-a --out/-o --model/-m(默认 small) --language/-l(默认 zh)',
242
- ' --device(默认 cpu;auto/cuda 需自备 CUDA 运行库,失败会自动回退 cpu)'
260
+ ' --device(默认 cpu;auto/cuda 需自备 CUDA 运行库,失败会自动回退 cpu)',
261
+ ' --timestamps(逐段保留真实起止时间;默认关闭,关闭时仍按段换行)'
243
262
  ].join('\n');
244
263
 
245
264
  function fail(msg, code) {
@@ -346,6 +365,7 @@ function main(argv) {
346
365
  childEnv.DRS_MODEL = args.model;
347
366
  childEnv.DRS_LANG = args.language;
348
367
  childEnv.DRS_DEVICE = args.device;
368
+ childEnv.DRS_TIMESTAMPS = args.timestamps ? '1' : '0';
349
369
 
350
370
  const pyScript = path.join(stagingDir, 'transcribe_run.py');
351
371
  fs.writeFileSync(pyScript, PY_RUNNER, 'utf8');
@@ -40,13 +40,13 @@ whenToUse: 用户提供一本书、论文(arXiv/PDF)、视频链接(YouTub
40
40
  4. **波次1 获取+分块**:解析器获取全文 → 写入本次独立的临时子目录 → 生成分块计划(JSON Schema 校验)。`tempDir` 是根目录,不要复用其他 `run-*` 任务的 input/audio/transcript;分块超过上限时合并相邻区间,保持从第 1 行到 totalLines 的完整覆盖
41
41
  5. **波次2 并行精读**:每个分块一个子代理深度精读(Map)
42
42
  6. **波次3 合并成稿**:整合为完整笔记(Reduce),内嵌质量自检
43
- 7. **质量校验**:可选重试(maxRetries),检查覆盖度/引用真实性/术语一致/格式完整/篇幅/语言
43
+ 7. **质量校验**:程序检查 frontmatter 模板、必要章节和篇幅;核验代理必须读取本次原文,逐块提供摘录、对第 4 节逐字引用提供原句与行号。不得只看笔记或仅返回 pass。`qualityEvidence` 可供回查;核验失败必须如实报告。`maxRetries` 为 0–3,修订失败、空结果或结构退化时保留上一份有效成稿
44
44
  8. **落盘(必须,别跳过)**:workflow 工具只返回 `note`(笔记正文)与 `filePath`(建议路径)——**脚本本身没有文件系统权限**。拿到结果后要用 `write` 工具把 `note` 原样写入 `filePath`(目录不存在就先创建);不写就等于笔记丢了,用户只会看到一段返回文本
45
45
  9. **如实交代结果**:`ok: false` 要说明卡在哪一步(`stage`)和原因,别拿半成品冒充成品;`qualityPassed: false`、`qualityIssues` 非空、或 `failedChunks > 0`(有块精读失败、笔记存在缺口)时,必须在回复里点出来
46
46
 
47
47
  ## 输出
48
48
 
49
- workflow 返回 `{ ok, filePath, note, qualityPassed, qualityIssues, failedChunks, coverageComplete, textSource }`。`coverageComplete: false` 表示获取结果缺少有效总行数,无法确认尾部覆盖;此时质量状态也会降级,必须向用户说明。
49
+ workflow 返回 `{ ok, filePath, note, qualityPassed, qualityIssues, qualityChecks, qualityEvidence, failedChunks, coverageComplete, textSource }`。`coverageComplete: false` 表示获取结果缺少有效总行数,无法确认尾部覆盖;此时质量状态也会降级,必须向用户说明。
50
50
 
51
51
  > ⚠️ **`note` 就是完整笔记正文,`filePath` 只是建议路径**:脚本运行在无文件系统的沙箱里(DSH 的 workflow 契约:脚本只负责协调子代理),**它不会替你写文件**。必须由你(主代理)在拿到结果后用 `write` 工具把它写到 `filePath`——默认 `./output/`,可用 `options.outputDir` 指向 Obsidian 仓库或静态站点的内容目录。
52
52
 
@@ -85,6 +85,7 @@ status: 已完成
85
85
  - 运行 yt-dlp 加 `--socket-timeout 15 --retries 3` 防超时。
86
86
  - **跨平台**:Windows / macOS / Linux **同等支持**(0.3.9 起)。无字幕自动转写走 `scripts/transcribe.js`(Node + `uv` + Python 3.12),三平台命令一致:`node <脚本路径> --audio <音频> --out <txt> [--device cpu]`;脚本定位命令按系统选(Windows `Get-ChildItem -Path $env:USERPROFILE/.dsh -Recurse -Filter transcribe.js`,macOS/Linux `find "$HOME/.dsh" -name transcribe.js`)。缓存目录按平台取:Windows `%LOCALAPPDATA%\deep-read-summarize`、macOS `~/Library/Caches/deep-read-summarize`、Linux `$XDG_CACHE_HOME/deep-read-summarize`。若 `node` 不在 PATH(macOS 从图形界面启动时常见)则直接降级,不要为此安装 Node。
87
87
  - 默认不标时间戳(`options.includeTimestamps` 可开)
88
+ - 开启时间戳时保留字幕 cue 或转写分段的真实起止时间,输出 `[HH:MM:SS.mmm --> HH:MM:SS.mmm]`;关闭时仍逐段换行。原文没有时间信息时注明无法定位,不得推算或编造。
88
89
 
89
90
  ## 参考
90
91
 
package/workflow.js CHANGED
@@ -3,7 +3,7 @@
3
3
  // Version: 3.0.0
4
4
  // ============================================================================
5
5
  // 【用法】meta → workflow meta;script → workflow script;
6
- // args: { "input": "<url|path>", "type": "auto", "options": { "minWords": 2500, "fastMode": false, "maxChunks": 4, "transcribe": true, "requireCitations": true, "includeTimestamps": false, "cache": true, "outputDir": "./output", "tempDir": "./.tmp" } }
6
+ // args: { "input": "<url|path>", "type": "auto", "options": { "minWords": 2500, "fastMode": false, "maxChunks": 4, "transcribe": true, "requireCitations": true, "includeTimestamps": false, "outputDir": "./output", "tempDir": "./.tmp" } }
7
7
  // ============================================================================
8
8
 
9
9
  const meta = {
@@ -44,8 +44,10 @@ const RAW_SCRIPT = fs.readFileSync(path.join(__dirname, "lib", "workflow-script.
44
44
 
45
45
  // ===== parsers 注册表(运行时注入,实现插件化)=====
46
46
  const parsers = require("./parsers");
47
- const promptImpl = require("./parsers/_prompt");
48
47
  const schemas = require("./schemas");
48
+ const { buildParserBundle } = require("./lib/parser-bundle");
49
+ const { normalizeWorkflowOptions } = require("./lib/options");
50
+ const quality = require("./lib/quality");
49
51
  const parserRegistry = parsers.registry;
50
52
 
51
53
  /**
@@ -60,12 +62,6 @@ const parserRegistry = parsers.registry;
60
62
  * 保持单一真相,不再和脚本里的副本各自漂移。
61
63
  */
62
64
  function buildSelfContainedScript() {
63
- const parserSource = (name) =>
64
- parserRegistry.parsers[name].buildPrompt
65
- .toString()
66
- // buildPrompt 闭包引用模块级 prompt(parsers/_prompt),内联后改指 __prompt
67
- .replace(/\bprompt\.buildFetchPrompt\b/g, "__prompt.buildFetchPrompt");
68
-
69
65
  const parserNames = Object.keys(parserRegistry.parsers);
70
66
  // kind 的合法取值跟着解析器表走(含自定义类型),不再写死四个内置类型。
71
67
  const fetchSchema = JSON.parse(JSON.stringify(schemas.fetchResultSchema));
@@ -74,12 +70,7 @@ function buildSelfContainedScript() {
74
70
  }
75
71
 
76
72
  const impl = [
77
- "const __prompt = { buildFetchPrompt: " + promptImpl.buildFetchPrompt.toString() + " };",
78
- "const __parsers = {",
79
- parserNames
80
- .map((name) => " " + JSON.stringify(name) + ": { buildPrompt: " + parserSource(name) + " }")
81
- .join(",\n"),
82
- "};",
73
+ buildParserBundle(parserRegistry),
83
74
  "const fetchSchema = " + JSON.stringify(fetchSchema) + ";"
84
75
  ].join("\n");
85
76
 
@@ -87,9 +78,12 @@ function buildSelfContainedScript() {
87
78
  if (!RAW_SCRIPT.includes(marker)) {
88
79
  throw new Error("workflow script marker not found: " + marker);
89
80
  }
90
- return RAW_SCRIPT.replace(marker, impl + "\nconst parsers = args._parsers || __parsers;");
81
+ return 'const normalizeWorkflowOptions = ' + normalizeWorkflowOptions.toString() + ';\n' +
82
+ 'const __quality = { checkNoteStructure: ' + quality.checkNoteStructure.toString() +
83
+ ', validateSourceCheck: ' + quality.validateSourceCheck.toString() + ' };\n' +
84
+ RAW_SCRIPT.replace(marker, impl + "\nconst parsers = args._parsers || __parsers;");
91
85
  }
92
86
 
93
87
  const script = buildSelfContainedScript();
94
88
 
95
- module.exports = { meta, script, parsers, buildSelfContainedScript };
89
+ module.exports = { meta, script, parsers, buildSelfContainedScript };