deep-read-summarize 0.3.13 → 0.3.14

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 CHANGED
@@ -3,6 +3,32 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  The format is based on [Keep a Changelog](https://keepachangelog.com/).
5
5
 
6
+ ## [0.3.14] — 2026-10-17
7
+
8
+ 修四个实际缺陷,都属于「声明 / 约定与实际行为不一致」这一类。其中两处**用户可感知**,见「升级提醒」。
9
+
10
+ ### Fixed
11
+
12
+ - **`outputDir` / `tempDir` 的尾部分隔符处理不对称**:`tempFile` 只去掉尾部正斜杠,`mdPath` 只去掉一个反斜杠。于是 `outputDir: 'out/'` 会拼出 `out//Title.md`,`tempDir: '.tmp\'` 会拼出 `.tmp\/input.txt` —— **在 POSIX 上那个反斜杠会变成目录名的一部分**,抓取代理会去建一个实际不存在的目录。现在两者统一为「去掉任意长度的 `/` 或 `\`,再补一个分隔符」,与 `parsers/_prompt.js` 自己那套回退逻辑一致。
13
+ - **frontmatter 的 `created` 用的是 UTC 日期**:`new Date().toISOString().slice(0, 10)` 在 UTC+8 的 00:00–08:00 会写成前一天。改为取本地年月日。
14
+ - 两处改动都在 `workflow.js` 的 `RAW_SCRIPT` 里。该文件按既有教训**整体重建**(解码模板 → 精确替换 → 重新 JSON 编码 → 只替换那一条物理行),并断言成品脚本「只差这三处」、模板反向还原逐字一致、内联标记未被破坏。
15
+ - **插件级 config 会静默丢弃未知键**:`normalizeOptions` 只遍历 `DEFAULT_OPTIONS`,所以写进 `cordis.patch.yml` 的 `whisperModel` / `language` / `device` / `maxRetries` **既不生效也不告警**,拼错的键同样无声。现在这四个键进了白名单,未知键会告警并列出可用键。
16
+ - **README ×2 的 fixture 测试数停在 25**:实际为 35(两个文件各两处)。
17
+
18
+ ### Added
19
+
20
+ - 4 条回归测试(`node:test` 42 → 46):`outputDir` 尾分隔符归一、`tempDir` 尾分隔符归一、`created` 用本地日期(含「不得再用 UTC 切片」的源码断言)、config 白名单与未知键告警。
21
+ - 这几条做过**反证**:把旧版脚本取回来跑同一套断言,`out/`、`out\\`、`./.tmp\`、`./.tmp\\` 四个用例都会失败 —— 证明它们不是空跑。
22
+
23
+ ### Compatibility
24
+
25
+ - 无破坏性变更;依赖、`exports`、`files`、`engines` 均未动。frontmatter 仍是既有 9 个键。
26
+
27
+ ### 升级提醒
28
+
29
+ - **`created` 的日期可能比之前大一天**:这正是修复目的(本地日期而非 UTC)。UTC 偏移为负的用户在晚间会看到相反方向的一天差异。
30
+ - **带尾分隔符时产物路径字符串变干净了**:此前会产生 `out//Title.md` 这类路径。笔记实际落盘位置不变(多数文件系统把 `//` 当单斜杠),但依赖 `filePath` 字符串做幂等判断的调用方会看到不同的写法。
31
+
6
32
  ## [0.3.13] — 2026-10-11
7
33
 
8
34
  **元数据版本,无运行时改动**:生成的笔记与 0.3.12 一致,工作流脚本未动。本版只修一处**声明**问题。
package/README.en.md CHANGED
@@ -142,7 +142,7 @@ npm install # no third-party dependencies, just initialization
142
142
  ### 2. Verify the environment (offline, no API key needed)
143
143
 
144
144
  ```bash
145
- npm test # 25 fixture tests, all run offline
145
+ npm test # 35 fixture tests, all run offline
146
146
  ```
147
147
 
148
148
  Seeing `TOTAL: 25 passed, 0 failed` means the environment is ready.
@@ -293,7 +293,7 @@ A paper, 6 chunks, full mode: roughly 15–25k tokens. `fastMode` with a lower `
293
293
  ```bash
294
294
  npm install
295
295
  npm run lint # node --check on all JS
296
- npm test # fixture tests (25, all offline)
296
+ npm test # fixture tests (35, all offline)
297
297
  npm run validate # pre-release validation (including security checks)
298
298
  ```
299
299
 
package/README.md CHANGED
@@ -142,7 +142,7 @@ npm install # 无第三方依赖,仅初始化
142
142
  ### 2. 验证环境(离线,不需要任何 API key)
143
143
 
144
144
  ```bash
145
- npm test # 25 项 fixture 测试,全部离线跑通
145
+ npm test # 35 项 fixture 测试,全部离线跑通
146
146
  ```
147
147
 
148
148
  看到 `TOTAL: 25 passed, 0 failed` 即环境就绪。
@@ -293,7 +293,7 @@ parsers: book, paper, video, web
293
293
  ```bash
294
294
  npm install
295
295
  npm run lint # node --check 全部 JS
296
- npm test # fixture 测试(25 项,全部离线)
296
+ npm test # fixture 测试(35 项,全部离线)
297
297
  npm run validate # 发布前验证(含安全检查)
298
298
  ```
299
299
 
package/cordis.patch.yml CHANGED
@@ -22,6 +22,10 @@
22
22
  # transcribe: true # video: auto-transcribe when no subtitles (default: true)
23
23
  # requireCitations: true # key conclusions must cite source location (default: true)
24
24
  # includeTimestamps: false # video timestamps (default: false)
25
+ # whisperModel: 'small' # transcription model (default: small)
26
+ # language: 'zh' # transcription language (default: zh)
27
+ # device: 'cpu' # cpu | auto | cuda; GPU needs a CUDA runtime (default: cpu)
28
+ # maxRetries: 0 # quality-check retry rounds (default: 0)
25
29
  #
26
30
  # Idempotency is deliberately NOT a plugin config key: the workflow script runs
27
31
  # without a filesystem, so it cannot write a fingerprint record. Programmatic
package/docs/RELEASE.md CHANGED
@@ -114,7 +114,8 @@ npm view deep-read-summarize --prefer-online # 验证线
114
114
  | v0.3.10 | npm 已发布 | 修:失效的 uv 镜像、空转写被当成成功、发布文档过时 |
115
115
  | v0.3.11 | npm 已发布 | 文档:README 补「支持平台」;CI:三平台跑全套门禁 |
116
116
  | v0.3.12 | npm 已发布 | 文档:定位改为「标准 Markdown」;删 dsh.so 一节;macOS 真机验证转写通过 |
117
- | **v0.3.13** | **npm 已发布(当前)** | 声明:补 `engines.dsh`(此前的 `dsh.minVersion` 非公开字段,市场不读,判定一直是 `undeclared`) |
117
+ | v0.3.13 | npm 已发布 | 声明:补 `engines.dsh`(此前的 `dsh.minVersion` 非公开字段,市场不读,判定一直是 `undeclared`) |
118
+ | **v0.3.14** | **npm 已发布(当前)** | 修:路径尾分隔符、本地日期、config 白名单;README 测试数 |
118
119
  | v1.0.0 | 计划 | API 稳定后发布 |
119
120
 
120
121
  > 早期迭代用 0.x 表达不稳定;API 稳定后再发 1.0.0。
package/index.js CHANGED
@@ -33,7 +33,11 @@ const DEFAULT_OPTIONS = {
33
33
  requireCitations: true,
34
34
  includeTimestamps: false,
35
35
  outputDir: './output',
36
- tempDir: './.tmp'
36
+ tempDir: './.tmp',
37
+ whisperModel: 'small',
38
+ language: 'zh',
39
+ device: 'cpu',
40
+ maxRetries: 0
37
41
  };
38
42
 
39
43
  const OPTION_TYPES = {
@@ -44,7 +48,11 @@ const OPTION_TYPES = {
44
48
  requireCitations: 'boolean',
45
49
  includeTimestamps: 'boolean',
46
50
  outputDir: 'string',
47
- tempDir: 'string'
51
+ tempDir: 'string',
52
+ whisperModel: 'string',
53
+ language: 'string',
54
+ device: 'string',
55
+ maxRetries: 'number'
48
56
  };
49
57
 
50
58
  /**
@@ -64,6 +72,14 @@ function normalizeOptions(config) {
64
72
  }
65
73
  opts[key] = v;
66
74
  }
75
+ // 未知键此前是完全无声的:循环只遍历白名单,写错的键名既不生效也不告警。
76
+ var unknown = Object.keys(raw).filter(function (k) {
77
+ return !Object.prototype.hasOwnProperty.call(DEFAULT_OPTIONS, k);
78
+ });
79
+ if (unknown.length) {
80
+ console.warn('[deep-read-summarize] ignoring unknown config key(s): ' + unknown.join(', '));
81
+ console.warn('[deep-read-summarize] allowed keys: ' + Object.keys(DEFAULT_OPTIONS).join(', '));
82
+ }
67
83
  return opts;
68
84
  }
69
85
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "deep-read-summarize",
3
- "version": "0.3.13",
3
+ "version": "0.3.14",
4
4
  "description": "Deep reading & summarization for DSH: books/papers/videos/web → structured Markdown notes (Obsidian / Logseq / static sites). Plugin parsers, MapReduce deep-read, JSON Schema-constrained sub-tasks.",
5
5
  "type": "commonjs",
6
6
  "main": "index.js",
@@ -454,3 +454,65 @@ test('docs: RELEASE.md lists the version we are about to ship', () => {
454
454
  assert.ok(rel.indexOf('v' + ver) >= 0,
455
455
  'docs/RELEASE.md 的「当前版本速览」必须包含当前版本 v' + ver + '(每次发版都要更新,别再让它停在旧版本)');
456
456
  });
457
+
458
+ // ============================================================================
459
+ // 0.3.14 回归:路径拼接、本地日期、config 白名单
460
+ // ============================================================================
461
+
462
+ test('workflow: trailing separators in outputDir normalize to a single separator', async () => {
463
+ const BS = String.fromCharCode(92);
464
+ const cases = [
465
+ { dir: 'out/', expect: 'out/Sample.md' },
466
+ { dir: 'out//', expect: 'out/Sample.md' },
467
+ { dir: 'out' + BS, expect: 'out/Sample.md' },
468
+ { dir: 'out' + BS + BS, expect: 'out/Sample.md' },
469
+ { dir: './output/', expect: './output/Sample.md' }
470
+ ];
471
+ for (const c of cases) {
472
+ const r = await runScript({ input: 'x', type: 'paper', options: { outputDir: c.dir } }, makeAgent({}));
473
+ assert.strictEqual(r.result.filePath, c.expect,
474
+ 'outputDir ' + JSON.stringify(c.dir) + ' must normalize to ' + c.expect + ', got ' + r.result.filePath);
475
+ }
476
+ });
477
+
478
+ test('workflow: trailing separators in tempDir normalize into tempFile', async () => {
479
+ const BS = String.fromCharCode(92);
480
+ const cases = ['./.tmp/', './.tmp//', './.tmp' + BS, './.tmp' + BS + BS];
481
+ for (const dir of cases) {
482
+ const r = await runScript({ input: 'x', type: 'paper', options: { tempDir: dir } }, makeAgent({}));
483
+ const fetchCall = r.calls.find(function (c) { return c.label === '获取+分块'; });
484
+ assert.ok(fetchCall, 'the fetch phase must run');
485
+ assert.ok(fetchCall.prompt.indexOf('./.tmp/input.txt') >= 0,
486
+ 'tempDir ' + JSON.stringify(dir) + ' must normalize to ./.tmp/input.txt');
487
+ }
488
+ });
489
+
490
+ test('workflow: created comes from the local date, not the UTC slice', async () => {
491
+ assert.ok(!/toISOString\(\)\s*\.\s*slice\(0,\s*10\)/.test(wf.script),
492
+ 'the note date must not be sliced out of the UTC ISO string');
493
+ const r = await runScript({ input: 'x', type: 'paper', options: {} }, makeAgent({}));
494
+ const draft = r.calls.find(function (c) { return c.label === '汇总成稿'; }).prompt;
495
+ const m = draft.match(/created: (\d{4}-\d{2}-\d{2})/);
496
+ assert.ok(m, 'created: <date> must appear in the draft prompt');
497
+ const d = new Date();
498
+ const pad = function (n) { return ('0' + n).slice(-2); };
499
+ const local = d.getFullYear() + '-' + pad(d.getMonth() + 1) + '-' + pad(d.getDate());
500
+ assert.strictEqual(m[1], local, 'created must be the local date (UTC+8 evenings differ from UTC)');
501
+ });
502
+
503
+ test('plugin config: transcription keys are configurable and unknown keys warn', () => {
504
+ const opts = plugin.normalizeOptions({ whisperModel: 'medium', language: 'en', device: 'cpu', maxRetries: 2 });
505
+ assert.strictEqual(opts.whisperModel, 'medium', 'whisperModel must be settable per profile');
506
+ assert.strictEqual(opts.language, 'en', 'language must be settable per profile');
507
+ assert.strictEqual(opts.device, 'cpu');
508
+ assert.strictEqual(opts.maxRetries, 2, 'maxRetries must be settable per profile');
509
+
510
+ const seen = [];
511
+ const origWarn = console.warn;
512
+ console.warn = function (msg) { seen.push(String(msg)); };
513
+ try { plugin.normalizeOptions({ notAKey: 1 }); } finally { console.warn = origWarn; }
514
+ assert.ok(seen.some(function (m) { return m.indexOf('notAKey') >= 0; }),
515
+ 'an unknown config key must be reported, not silently dropped');
516
+ assert.ok(seen.some(function (m) { return m.indexOf('whisperModel') >= 0; }),
517
+ 'the warning should list the keys that are actually accepted');
518
+ });
package/workflow.js CHANGED
@@ -35,7 +35,7 @@ const meta = {
35
35
  };
36
36
 
37
37
  // ===== script(复制到 workflow 工具的 script 参数)=====
38
- const RAW_SCRIPT = "// ========== deep-read-summarize v3.0 ==========\n// 3 波次:解析器获取分块 → 并行精读 → 成稿+质量校验\n// 特性:解析器插件化 / 失败纪律(fatal vs 降级) / 成本控制(maxChunks 强制,块数=子代理数)\n\n// YAML 双引号标量:JSON.stringify 的转义规则是 YAML double-quoted 风格的子集,\n// 标题/作者里出现引号时不会再截断 frontmatter(旧版是裸拼 \"...\")。\nfunction yamlStr(v) { return JSON.stringify(v == null ? \"\" : String(v)); }\n\n// ---------- 0. 配置校验(fatal 级错误:必须终止) ----------\nconst input = (args && args.input || \"\").trim();\nconst forcedType = ((args && args.type) || \"auto\").toLowerCase();\nconst options = (args && args.options) || {};\nconst minWords = options.minWords || 2500;\nconst fastMode = !!options.fastMode;\nconst maxChunks = Math.max(1, Math.min(options.maxChunks || 4, 12));\nconst requireCitations = options.requireCitations !== false;\nconst includeTimestamps = !!options.includeTimestamps;\nconst maxRetries = options.maxRetries || 0;\nconst outputDir = options.outputDir || \"./output\";\nconst tempDir = options.tempDir || \"./.tmp\";\nconst tempFile = tempDir.replace(/[\\/]+$/, \"\") + \"/input.txt\";\nconst whisperModel = options.whisperModel || \"small\";\nconst transcribeLanguage = options.language || \"zh\";\nconst transcribeDevice = options.device || \"cpu\";\n\n// 幂等不在这里做:脚本沙箱没有文件系统,写不了指纹记录。\n// 需要幂等的调用方请用 lib/cache.js(fingerprint / hasProcessed / markProcessed)。\n\n// ---------- 1. 解析器表:构建时把解析器内联为 __parsers(自包含);\n// 宿主若注入 args._parsers 则优先使用 ----------\nconst parsers = args._parsers || {};\nconst parserNames = Object.keys(parsers);\n\n// 配置校验依赖解析器表,必须放在其后\nif (!input) throw new Error(\"[FATAL] args.input 缺失:请输入内容链接或文件路径\");\nif (parserNames.length === 0) throw new Error(\"[FATAL] 解析器表为空:脚本没有内联任何解析器\");\nif (forcedType !== \"auto\" && parserNames.indexOf(forcedType) === -1)\n throw new Error(\"[FATAL] args.type 非法:\" + forcedType + \"(可用:\" + [\"auto\"].concat(parserNames).join(\"|\") + \")\");\n\nphase(\"解析器选择\");\nconst kindNames = { book: \"书籍\", paper: \"学术论文\", video: \"视频\", web: \"网页/文章\" };\nconst kindFocus = {\n book: \"按章节主题展开核心论点、论证、关键例子、章节间关系,收集关键概念与可引用原文(含页码/章节)\",\n paper: \"四维精读:1)研究问题与方法 2)结论与贡献 3)局限与批判 4)与同类工作比较;收集术语与可引用原文(含章节/段落)\",\n video: \"按主题脉络组织,提取核心论点、证据、例子、可操作建议,收集关键概念与主讲人原话\" + (includeTimestamps ? \"(含时间戳定位)\" : \"(不标时间戳)\"),\n web: \"提取核心论点、结构拆解、关键概念、可引用原文\"\n};\n\n// ---------- 2. 波次1:解析器获取 + 分块 ----------\nphase(\"获取与分块\");\nconst fetchPrompt = (parsers[forcedType !== \"auto\" ? forcedType : \"web\"] || parsers.web || { buildPrompt: function() { return [\"未知解析器\"]; } })\n .buildPrompt(input, { tempDir: tempDir, tempFile: tempFile, maxChunks: maxChunks, transcribe: options.transcribe !== false, whisperModel: whisperModel, language: transcribeLanguage, device: transcribeDevice });\n// fetchSchema 由插件在构建时从 schemas/index.js 注入(单一真相,避免两份 schema 漂移)。\n\nconst fetched = await agent(fetchPrompt, { schema: fetchSchema, label: \"获取+分块\", phase: \"获取与分块\" });\n\n// 失败纪律:内容获取失败 = 可降级错误(返回失败结果,不抛 fatal)\nif (!fetched || !fetched.saved) {\n return { ok: false, stage: \"fetch\", fatal: false, message: (fetched && fetched.message) || \"获取失败(可降级:检查输入或手动提供文本)\" };\n}\n// 分块计划收敛:块数 = 子代理数,计划失控会顶穿 workflow 单次调用时限;\n// 同时挡掉倒置/越界的行号区间(原样透传会让 read 收到 limit<=0)。\nconst rawPlan = (fetched && fetched.chunkPlan) || [];\nif (rawPlan.length === 0) return { ok: false, stage: \"fetch\", fatal: false, message: \"分块计划为空\" };\nconst declaredLines = Math.floor(Number(fetched.totalLines) || 0);\nconst chunkPlan = [];\nfor (let i = 0; i < rawPlan.length && chunkPlan.length < maxChunks; i++) {\n const c = rawPlan[i] || {};\n const s = Math.floor(Number(c.startLine) || 0);\n const e = Math.floor(Number(c.endLine) || 0);\n if (s < 1 || e < s) continue;\n if (declaredLines > 0 && s > declaredLines) continue;\n chunkPlan.push({\n id: chunkPlan.length + 1,\n topic: String(c.topic || (\"块 \" + (chunkPlan.length + 1))),\n startLine: s,\n endLine: declaredLines > 0 ? Math.min(e, declaredLines) : e\n });\n}\nif (chunkPlan.length === 0) return { ok: false, stage: \"fetch\", fatal: false, message: \"分块计划无效:行号区间缺失或非法\" };\nif (chunkPlan.length < rawPlan.length) log(\"分块计划收敛:\" + rawPlan.length + \" -> \" + chunkPlan.length + \" 块(上限 \" + maxChunks + \")\");\n\n// 类型:auto 时只接受解析器表里真实存在的类型,否则退回回退解析器,\n// 避免 \"auto\" 这种非法值流进 frontmatter 的 type / tags。\nlet kind = forcedType;\nif (forcedType === \"auto\") {\n kind = (typeof fetched.kind === \"string\" && parserNames.indexOf(fetched.kind) !== -1)\n ? fetched.kind\n : (parserNames.indexOf(\"web\") !== -1 ? \"web\" : parserNames[0]);\n}\nconst metadata = (fetched && fetched.metadata) || {};\nconst textSource = (metadata && metadata.textSource) || \"unknown\";\nconst srcLabels = { desc: \"视频简介/desc\", subtitle: \"平台字幕\", transcription: \"完整语音转写\", manual: \"用户提供转写文本\" };\nconst srcLabel = srcLabels[textSource] || textSource;\nlog(\"[source] \" + textSource + \" -> \" + srcLabel);\nlog(\"类型:\" + kind + \" 行数:\" + fetched.totalLines + \" 块数:\" + chunkPlan.length);\n\n// ---------- 3. 波次2:并行精读(Map) ----------\nphase(\"并行精读\");\nconst chunkPrompts = chunkPlan.map(function (c) {\n return [\n \"请深度精读以下内容块,输出结构化精读笔记。\",\n \"内容类型:\" + (kindNames[kind] || kind) + \" | 本块主题:\" + c.topic,\n \"精读侧重:\" + (kindFocus[kind] || kindFocus.web),\n \"用 read 工具读取 \" + tempFile + \" 第 \" + c.startLine + \" 到 \" + c.endLine + \" 行(offset=\" + c.startLine + \", limit=\" + (c.endLine - c.startLine + 1) + \"),然后输出:\",\n \"## 本块主题:\" + c.topic,\n \"### 要点(详细展开,保留数据/例子/论证)\",\n \"### 关键概念(术语+解释)\",\n \"### 关键引用(最多2句原文+位置)\" + (requireCitations ? \",必须标注页码/章节/段落位置\" : \"\"),\n \"### 批判观察(如有)\"\n ].join(\"\\n\");\n});\n\nconst reads = [];\nconst readBatch = 3;\nfor (let bi = 0; bi < chunkPrompts.length; bi += readBatch) {\n const batch = chunkPrompts.slice(bi, bi + readBatch);\n const batchReads = await parallel(batch.map(function (p) {\n return function () { return agent(p, { label: \"精读块\", phase: \"并行精读\" }); };\n }));\n for (let k = 0; k < batchReads.length; k++) reads.push(batchReads[k]);\n}\nconst validReads = reads.filter(function (r) { return r && r.trim && r.trim().length > 0; });\n\n// 失败纪律:某块精读失败 = 可降级(跳过该块,标记缺口继续)\nconst failedChunks = chunkPlan.length - validReads.length;\nif (validReads.length === 0) return { ok: false, stage: \"read\", fatal: false, message: \"所有分块精读均失败\" };\nif (failedChunks > 0) log(\"警告:\" + failedChunks + \" 块精读失败,标记缺口继续成稿\");\nlog(\"精读完成:\" + validReads.length + \"/\" + chunkPlan.length);\n\n// ---------- 4. 波次3:成稿 + 内嵌质量自检(Reduce) ----------\nphase(\"汇总成稿\");\nconst today = new Date().toISOString().slice(0, 10);\n// 标题先折叠换行/制表与连续空白:否则会同时污染 .md 文件名和 frontmatter。\nconst finalTitle = String(metadata.title || \"\").replace(/[\\r\\n\\t]+/g, \" \").replace(/\\s+/g, \" \").trim() || \"未命名内容\";\n// 文件名清洗:去掉 Windows 非法字符;全部非法时兜底,避免写出 ./output/.md 这种隐藏文件。\nconst safeTitle = finalTitle.replace(/[\\\\/:*?\"<>|]/g, \" \").replace(/\\s+/g, \" \").trim() || \"未命名内容\";\nconst mdPath = outputDir.replace(/\\\\$/, \"\") + \"/\" + safeTitle + \".md\";\n\nconst extraSections = fastMode ? [] : [\n \"\", \"## 5. 批判性分析(论证强处/弱处/可质疑之处)\", \"\",\n \"## 6. 个人思考与应用(与已有知识联系、如何应用、行动要点)\", \"\",\n \"## 7. 延伸阅读(相关主题/作者其他作品/参考文献)\", \"\"\n];\n\nconst draftPrompt = [\n \"将以下多块精读结果整合为完整 Obsidian 精读笔记,只输出 Markdown(含 YAML frontmatter)。\",\n \"模板:\", \"---\",\n 'title: ' + yamlStr(\"《\" + finalTitle + \"》深度精读笔记\"),\n \"aliases: [\" + yamlStr(finalTitle) + \"]\",\n \"author: \" + yamlStr(metadata.author),\n \"year: \" + yamlStr(metadata.year),\n \"type: \" + yamlStr(kind),\n \"url: \" + yamlStr(metadata.url || input),\n \"tags: [精读, \" + (kindNames[kind] || kind) + \"]\",\n \"created: \" + today,\n \"status: 已完成\",\n \"---\",\n \"# 《\" + finalTitle + \"》深度精读笔记\", \"\",\n \"> 一句话核心概括。\", \"\",\n \"## 1. 概述(约500字:主题/背景/核心问题/总体结论)\", \"\",\n \"## 2. 结构拆解(按主题组织,含详细要点;关键结论标注原文出处)\", \"\",\n \"## 3. 关键概念与术语表(表格)\", \"\",\n \"## 4. 关键引用(仅关键结论\" + (requireCitations ? \",必须标注页码/章节/段落\" : \"\") + \")\",\n].concat(extraSections, [\n \"---\",\n \"*笔记整理于 \" + today + \"*\", \"\",\n \"硬性要求:总篇幅≥\" + minWords + \"字;语言与原文一致;术语统一;引用必须真实;视频\" + (includeTimestamps ? \"标注时间戳\" : \"不标注时间戳\") + \"。\",\n \"输出前自检:1)覆盖所有块 2)frontmatter完整 3)各小节齐全 4)引用真实 5)篇幅达标 6)语言一致。\",\n \"\",\n \"各块精读结果:\",\n validReads.map(function (r, i) { return \"\\\\n===== 块 \" + (i + 1) + \" =====\\\\n\" + r; }).join(\"\\\\n\")\n]).join(\"\\n\");\n\nlet draft = await agent(draftPrompt, { label: \"汇总成稿\", phase: \"汇总成稿\" });\nif (!draft || !draft.trim) return { ok: false, stage: \"draft\", fatal: false, message: \"成稿失败\" };\n\n// ---------- 5. 质量校验(轻量重试) ----------\nphase(\"质量校验\");\nlet verified = draft;\nlet passed = false;\nlet issues = [];\nfor (let round = 0; round <= maxRetries; round++) {\n const checkPrompt = [\n \"请对总结笔记做质量校验,逐项检查并输出 JSON:\",\n \"1.coverage 2.citationsReal 3.terminologyConsistent 4.formatComplete 5.lengthMet(≥\" + minWords + \"字) 6.languageConsistent\",\n \"输出:{ \\\"pass\\\": true/false, \\\"issues\\\": [\\\"问题\\\"] },只输出 JSON。\",\n \"笔记:\", verified\n ].join(\"\\n\");\n const checkSchema = { type: \"object\", properties: { pass: { type: \"boolean\" }, issues: { type: \"array\", items: { type: \"string\" } } }, required: [\"pass\",\"issues\"], additionalProperties: false };\n const check = await agent(checkPrompt, { schema: checkSchema, label: \"质量校验\", phase: \"质量校验\" });\n passed = !!(check && check.pass);\n issues = (check && check.issues) || [];\n log(\"校验:\" + (passed ? \"通过\" : \"发现问题 \" + issues.length + \" 条\"));\n if (passed) break;\n if (round < maxRetries) {\n const fixPrompt = [\n \"修正以下笔记以解决校验问题,输出完整修正版:\",\n issues.map(function (s) { return \"- \" + s; }).join(\"\\n\"),\n \"当前笔记:\", verified\n ].join(\"\\n\");\n verified = await agent(fixPrompt, { label: \"修正笔记\", phase: \"质量校验\" });\n }\n}\n\n// ---------- 结果 ----------\nreturn {\n ok: true, kind: kind, title: finalTitle,\n filePath: mdPath, chunksCount: chunkPlan.length, fastMode: fastMode,\n qualityPassed: passed, qualityIssues: issues,\n failedChunks: failedChunks,\n textSource: textSource,\n note: verified\n};";
38
+ const RAW_SCRIPT = "// ========== deep-read-summarize v3.0 ==========\n// 3 波次:解析器获取分块 → 并行精读 → 成稿+质量校验\n// 特性:解析器插件化 / 失败纪律(fatal vs 降级) / 成本控制(maxChunks 强制,块数=子代理数)\n\n// YAML 双引号标量:JSON.stringify 的转义规则是 YAML double-quoted 风格的子集,\n// 标题/作者里出现引号时不会再截断 frontmatter(旧版是裸拼 \"...\")。\nfunction yamlStr(v) { return JSON.stringify(v == null ? \"\" : String(v)); }\n\n// ---------- 0. 配置校验(fatal 级错误:必须终止) ----------\nconst input = (args && args.input || \"\").trim();\nconst forcedType = ((args && args.type) || \"auto\").toLowerCase();\nconst options = (args && args.options) || {};\nconst minWords = options.minWords || 2500;\nconst fastMode = !!options.fastMode;\nconst maxChunks = Math.max(1, Math.min(options.maxChunks || 4, 12));\nconst requireCitations = options.requireCitations !== false;\nconst includeTimestamps = !!options.includeTimestamps;\nconst maxRetries = options.maxRetries || 0;\nconst outputDir = options.outputDir || \"./output\";\nconst tempDir = options.tempDir || \"./.tmp\";\nconst tempFile = tempDir.replace(/[\\\\/]+$/, \"\") + \"/input.txt\";\nconst whisperModel = options.whisperModel || \"small\";\nconst transcribeLanguage = options.language || \"zh\";\nconst transcribeDevice = options.device || \"cpu\";\n\n// 幂等不在这里做:脚本沙箱没有文件系统,写不了指纹记录。\n// 需要幂等的调用方请用 lib/cache.js(fingerprint / hasProcessed / markProcessed)。\n\n// ---------- 1. 解析器表:构建时把解析器内联为 __parsers(自包含);\n// 宿主若注入 args._parsers 则优先使用 ----------\nconst parsers = args._parsers || {};\nconst parserNames = Object.keys(parsers);\n\n// 配置校验依赖解析器表,必须放在其后\nif (!input) throw new Error(\"[FATAL] args.input 缺失:请输入内容链接或文件路径\");\nif (parserNames.length === 0) throw new Error(\"[FATAL] 解析器表为空:脚本没有内联任何解析器\");\nif (forcedType !== \"auto\" && parserNames.indexOf(forcedType) === -1)\n throw new Error(\"[FATAL] args.type 非法:\" + forcedType + \"(可用:\" + [\"auto\"].concat(parserNames).join(\"|\") + \")\");\n\nphase(\"解析器选择\");\nconst kindNames = { book: \"书籍\", paper: \"学术论文\", video: \"视频\", web: \"网页/文章\" };\nconst kindFocus = {\n book: \"按章节主题展开核心论点、论证、关键例子、章节间关系,收集关键概念与可引用原文(含页码/章节)\",\n paper: \"四维精读:1)研究问题与方法 2)结论与贡献 3)局限与批判 4)与同类工作比较;收集术语与可引用原文(含章节/段落)\",\n video: \"按主题脉络组织,提取核心论点、证据、例子、可操作建议,收集关键概念与主讲人原话\" + (includeTimestamps ? \"(含时间戳定位)\" : \"(不标时间戳)\"),\n web: \"提取核心论点、结构拆解、关键概念、可引用原文\"\n};\n\n// ---------- 2. 波次1:解析器获取 + 分块 ----------\nphase(\"获取与分块\");\nconst fetchPrompt = (parsers[forcedType !== \"auto\" ? forcedType : \"web\"] || parsers.web || { buildPrompt: function() { return [\"未知解析器\"]; } })\n .buildPrompt(input, { tempDir: tempDir, tempFile: tempFile, maxChunks: maxChunks, transcribe: options.transcribe !== false, whisperModel: whisperModel, language: transcribeLanguage, device: transcribeDevice });\n// fetchSchema 由插件在构建时从 schemas/index.js 注入(单一真相,避免两份 schema 漂移)。\n\nconst fetched = await agent(fetchPrompt, { schema: fetchSchema, label: \"获取+分块\", phase: \"获取与分块\" });\n\n// 失败纪律:内容获取失败 = 可降级错误(返回失败结果,不抛 fatal)\nif (!fetched || !fetched.saved) {\n return { ok: false, stage: \"fetch\", fatal: false, message: (fetched && fetched.message) || \"获取失败(可降级:检查输入或手动提供文本)\" };\n}\n// 分块计划收敛:块数 = 子代理数,计划失控会顶穿 workflow 单次调用时限;\n// 同时挡掉倒置/越界的行号区间(原样透传会让 read 收到 limit<=0)。\nconst rawPlan = (fetched && fetched.chunkPlan) || [];\nif (rawPlan.length === 0) return { ok: false, stage: \"fetch\", fatal: false, message: \"分块计划为空\" };\nconst declaredLines = Math.floor(Number(fetched.totalLines) || 0);\nconst chunkPlan = [];\nfor (let i = 0; i < rawPlan.length && chunkPlan.length < maxChunks; i++) {\n const c = rawPlan[i] || {};\n const s = Math.floor(Number(c.startLine) || 0);\n const e = Math.floor(Number(c.endLine) || 0);\n if (s < 1 || e < s) continue;\n if (declaredLines > 0 && s > declaredLines) continue;\n chunkPlan.push({\n id: chunkPlan.length + 1,\n topic: String(c.topic || (\"块 \" + (chunkPlan.length + 1))),\n startLine: s,\n endLine: declaredLines > 0 ? Math.min(e, declaredLines) : e\n });\n}\nif (chunkPlan.length === 0) return { ok: false, stage: \"fetch\", fatal: false, message: \"分块计划无效:行号区间缺失或非法\" };\nif (chunkPlan.length < rawPlan.length) log(\"分块计划收敛:\" + rawPlan.length + \" -> \" + chunkPlan.length + \" 块(上限 \" + maxChunks + \")\");\n\n// 类型:auto 时只接受解析器表里真实存在的类型,否则退回回退解析器,\n// 避免 \"auto\" 这种非法值流进 frontmatter 的 type / tags。\nlet kind = forcedType;\nif (forcedType === \"auto\") {\n kind = (typeof fetched.kind === \"string\" && parserNames.indexOf(fetched.kind) !== -1)\n ? fetched.kind\n : (parserNames.indexOf(\"web\") !== -1 ? \"web\" : parserNames[0]);\n}\nconst metadata = (fetched && fetched.metadata) || {};\nconst textSource = (metadata && metadata.textSource) || \"unknown\";\nconst srcLabels = { desc: \"视频简介/desc\", subtitle: \"平台字幕\", transcription: \"完整语音转写\", manual: \"用户提供转写文本\" };\nconst srcLabel = srcLabels[textSource] || textSource;\nlog(\"[source] \" + textSource + \" -> \" + srcLabel);\nlog(\"类型:\" + kind + \" 行数:\" + fetched.totalLines + \" 块数:\" + chunkPlan.length);\n\n// ---------- 3. 波次2:并行精读(Map) ----------\nphase(\"并行精读\");\nconst chunkPrompts = chunkPlan.map(function (c) {\n return [\n \"请深度精读以下内容块,输出结构化精读笔记。\",\n \"内容类型:\" + (kindNames[kind] || kind) + \" | 本块主题:\" + c.topic,\n \"精读侧重:\" + (kindFocus[kind] || kindFocus.web),\n \"用 read 工具读取 \" + tempFile + \" 第 \" + c.startLine + \" 到 \" + c.endLine + \" 行(offset=\" + c.startLine + \", limit=\" + (c.endLine - c.startLine + 1) + \"),然后输出:\",\n \"## 本块主题:\" + c.topic,\n \"### 要点(详细展开,保留数据/例子/论证)\",\n \"### 关键概念(术语+解释)\",\n \"### 关键引用(最多2句原文+位置)\" + (requireCitations ? \",必须标注页码/章节/段落位置\" : \"\"),\n \"### 批判观察(如有)\"\n ].join(\"\\n\");\n});\n\nconst reads = [];\nconst readBatch = 3;\nfor (let bi = 0; bi < chunkPrompts.length; bi += readBatch) {\n const batch = chunkPrompts.slice(bi, bi + readBatch);\n const batchReads = await parallel(batch.map(function (p) {\n return function () { return agent(p, { label: \"精读块\", phase: \"并行精读\" }); };\n }));\n for (let k = 0; k < batchReads.length; k++) reads.push(batchReads[k]);\n}\nconst validReads = reads.filter(function (r) { return r && r.trim && r.trim().length > 0; });\n\n// 失败纪律:某块精读失败 = 可降级(跳过该块,标记缺口继续)\nconst failedChunks = chunkPlan.length - validReads.length;\nif (validReads.length === 0) return { ok: false, stage: \"read\", fatal: false, message: \"所有分块精读均失败\" };\nif (failedChunks > 0) log(\"警告:\" + failedChunks + \" 块精读失败,标记缺口继续成稿\");\nlog(\"精读完成:\" + validReads.length + \"/\" + chunkPlan.length);\n\n// ---------- 4. 波次3:成稿 + 内嵌质量自检(Reduce) ----------\nphase(\"汇总成稿\");\nconst todayDate = new Date();\nconst today = todayDate.getFullYear() + \"-\" + (\"0\" + (todayDate.getMonth() + 1)).slice(-2) + \"-\" + (\"0\" + todayDate.getDate()).slice(-2);\n// 标题先折叠换行/制表与连续空白:否则会同时污染 .md 文件名和 frontmatter。\nconst finalTitle = String(metadata.title || \"\").replace(/[\\r\\n\\t]+/g, \" \").replace(/\\s+/g, \" \").trim() || \"未命名内容\";\n// 文件名清洗:去掉 Windows 非法字符;全部非法时兜底,避免写出 ./output/.md 这种隐藏文件。\nconst safeTitle = finalTitle.replace(/[\\\\/:*?\"<>|]/g, \" \").replace(/\\s+/g, \" \").trim() || \"未命名内容\";\nconst mdPath = outputDir.replace(/[\\\\/]+$/, \"\") + \"/\" + safeTitle + \".md\";\n\nconst extraSections = fastMode ? [] : [\n \"\", \"## 5. 批判性分析(论证强处/弱处/可质疑之处)\", \"\",\n \"## 6. 个人思考与应用(与已有知识联系、如何应用、行动要点)\", \"\",\n \"## 7. 延伸阅读(相关主题/作者其他作品/参考文献)\", \"\"\n];\n\nconst draftPrompt = [\n \"将以下多块精读结果整合为完整 Obsidian 精读笔记,只输出 Markdown(含 YAML frontmatter)。\",\n \"模板:\", \"---\",\n 'title: ' + yamlStr(\"《\" + finalTitle + \"》深度精读笔记\"),\n \"aliases: [\" + yamlStr(finalTitle) + \"]\",\n \"author: \" + yamlStr(metadata.author),\n \"year: \" + yamlStr(metadata.year),\n \"type: \" + yamlStr(kind),\n \"url: \" + yamlStr(metadata.url || input),\n \"tags: [精读, \" + (kindNames[kind] || kind) + \"]\",\n \"created: \" + today,\n \"status: 已完成\",\n \"---\",\n \"# 《\" + finalTitle + \"》深度精读笔记\", \"\",\n \"> 一句话核心概括。\", \"\",\n \"## 1. 概述(约500字:主题/背景/核心问题/总体结论)\", \"\",\n \"## 2. 结构拆解(按主题组织,含详细要点;关键结论标注原文出处)\", \"\",\n \"## 3. 关键概念与术语表(表格)\", \"\",\n \"## 4. 关键引用(仅关键结论\" + (requireCitations ? \",必须标注页码/章节/段落\" : \"\") + \")\",\n].concat(extraSections, [\n \"---\",\n \"*笔记整理于 \" + today + \"*\", \"\",\n \"硬性要求:总篇幅≥\" + minWords + \"字;语言与原文一致;术语统一;引用必须真实;视频\" + (includeTimestamps ? \"标注时间戳\" : \"不标注时间戳\") + \"。\",\n \"输出前自检:1)覆盖所有块 2)frontmatter完整 3)各小节齐全 4)引用真实 5)篇幅达标 6)语言一致。\",\n \"\",\n \"各块精读结果:\",\n validReads.map(function (r, i) { return \"\\\\n===== 块 \" + (i + 1) + \" =====\\\\n\" + r; }).join(\"\\\\n\")\n]).join(\"\\n\");\n\nlet draft = await agent(draftPrompt, { label: \"汇总成稿\", phase: \"汇总成稿\" });\nif (!draft || !draft.trim) return { ok: false, stage: \"draft\", fatal: false, message: \"成稿失败\" };\n\n// ---------- 5. 质量校验(轻量重试) ----------\nphase(\"质量校验\");\nlet verified = draft;\nlet passed = false;\nlet issues = [];\nfor (let round = 0; round <= maxRetries; round++) {\n const checkPrompt = [\n \"请对总结笔记做质量校验,逐项检查并输出 JSON:\",\n \"1.coverage 2.citationsReal 3.terminologyConsistent 4.formatComplete 5.lengthMet(≥\" + minWords + \"字) 6.languageConsistent\",\n \"输出:{ \\\"pass\\\": true/false, \\\"issues\\\": [\\\"问题\\\"] },只输出 JSON。\",\n \"笔记:\", verified\n ].join(\"\\n\");\n const checkSchema = { type: \"object\", properties: { pass: { type: \"boolean\" }, issues: { type: \"array\", items: { type: \"string\" } } }, required: [\"pass\",\"issues\"], additionalProperties: false };\n const check = await agent(checkPrompt, { schema: checkSchema, label: \"质量校验\", phase: \"质量校验\" });\n passed = !!(check && check.pass);\n issues = (check && check.issues) || [];\n log(\"校验:\" + (passed ? \"通过\" : \"发现问题 \" + issues.length + \" 条\"));\n if (passed) break;\n if (round < maxRetries) {\n const fixPrompt = [\n \"修正以下笔记以解决校验问题,输出完整修正版:\",\n issues.map(function (s) { return \"- \" + s; }).join(\"\\n\"),\n \"当前笔记:\", verified\n ].join(\"\\n\");\n verified = await agent(fixPrompt, { label: \"修正笔记\", phase: \"质量校验\" });\n }\n}\n\n// ---------- 结果 ----------\nreturn {\n ok: true, kind: kind, title: finalTitle,\n filePath: mdPath, chunksCount: chunkPlan.length, fastMode: fastMode,\n qualityPassed: passed, qualityIssues: issues,\n failedChunks: failedChunks,\n textSource: textSource,\n note: verified\n};";
39
39
 
40
40
  // ===== parsers 注册表(运行时注入,实现插件化)=====
41
41
  const parsers = require("./parsers");