deep-read-summarize 0.3.18 → 0.4.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/lib/cache.js CHANGED
@@ -17,8 +17,8 @@ const path = require("path");
17
17
  const crypto = require("crypto");
18
18
 
19
19
  /**
20
- * 规范化输入:对 http(s) URL 做归一化(host 小写、去默认端口、去 fragment、
21
- * 去空 query 的 "?"、去尾部斜杠),避免同一 URL 的不同写法产生不同指纹;
20
+ * 规范化输入:只采用 URL 标准的 host、默认端口等归一化。
21
+ * 保留尾斜杠、空 query 与 fragment:它们可能选择不同文档、路由或阅读范围。
22
22
  * 非 URL(本地路径/纯文本)原样返回。
23
23
  */
24
24
  function normalizeInput(input) {
@@ -26,19 +26,11 @@ function normalizeInput(input) {
26
26
  if (!/^https?:\/\//i.test(s)) return s;
27
27
  try {
28
28
  const url = new URL(s);
29
- url.hash = "";
30
29
  url.hostname = url.hostname.toLowerCase();
31
30
  if ((url.protocol === "http:" && url.port === "80") || (url.protocol === "https:" && url.port === "443")) {
32
31
  url.port = "";
33
32
  }
34
- if (url.pathname.length > 1 && url.pathname.endsWith("/")) {
35
- url.pathname = url.pathname.slice(0, -1);
36
- }
37
- // 去掉 URL 尾部的空 query "?"(new URL("...doc?").search 返回 "",无法靠 search 判断,
38
- // 需检查 toString() 输出;同一 URL 的 "doc" 与 "doc?" 两种写法应共享指纹)
39
- let normalized = url.toString();
40
- if (normalized.endsWith("?")) normalized = normalized.slice(0, -1);
41
- return normalized;
33
+ return url.toString();
42
34
  } catch (e) {
43
35
  return s; // 非法 URL 原样处理
44
36
  }
@@ -51,7 +43,8 @@ function fingerprint(input, fileMeta) {
51
43
  const metaStr = fileMeta
52
44
  ? "|" + (fileMeta.size ?? "") + "|" + (fileMeta.mtimeMs ?? "")
53
45
  : "";
54
- return crypto.createHash("sha256").update(base + metaStr).digest("hex").slice(0, 16);
46
+ // v2 不复用旧版过度归一化后的记录,避免旧碰撞继续导致错误跳过。
47
+ return crypto.createHash("sha256").update("drs-input-v2|" + base + metaStr).digest("hex").slice(0, 16);
55
48
  }
56
49
 
57
50
  /** 从缓存文件读取已处理记录(JSONL) */
@@ -84,14 +77,34 @@ function hasProcessed(cacheFile, key, opts) {
84
77
  return true;
85
78
  }
86
79
 
87
- /** 标记已处理(追加 JSONL 记录);写缓存失败仅告警,不中断主流程 */
80
+ /**
81
+ * 标记已处理:每条记录前后带换行,以一次 O_APPEND write 写入。
82
+ * 前导换行隔开无结尾 LF 的合法旧记录和崩溃留下的残行;不读取/改写文件尾,
83
+ * 并发调用无需争抢恢复锁,也不会因进程崩溃留下永久残锁。
84
+ * 依赖本地文件系统的 append 写入语义,不提供网络文件系统事务保证。
85
+ * 写失败或短写仅告警并返回 false;残行由下一条记录的前导换行隔离。
86
+ */
88
87
  function markProcessed(cacheFile, key, outputPath, meta) {
89
88
  if (!key) return false;
90
89
  try {
91
90
  const dir = path.dirname(cacheFile);
92
91
  if (!fs.existsSync(dir)) fs.mkdirSync(dir, { recursive: true });
93
- const rec = { key, outputPath, ts: new Date().toISOString(), ...(meta || {}) };
94
- fs.appendFileSync(cacheFile, JSON.stringify(rec) + "\n", "utf8");
92
+ // 元数据只扩展记录,不能伪造指纹、产物路径、处理时间或 JSON 序列化行为。
93
+ const rec = Object.create(null);
94
+ for (const name of Object.keys(meta || {})) {
95
+ if (name !== "key" && name !== "outputPath" && name !== "ts" && name !== "toJSON") rec[name] = meta[name];
96
+ }
97
+ rec.key = key;
98
+ rec.outputPath = outputPath;
99
+ rec.ts = new Date().toISOString();
100
+ const record = Buffer.from("\n" + JSON.stringify(rec) + "\n", "utf8");
101
+ const fd = fs.openSync(cacheFile, "a", 0o600);
102
+ try {
103
+ const written = fs.writeSync(fd, record, 0, record.length, null);
104
+ if (written !== record.length) throw new Error("short append: " + written + "/" + record.length + " bytes");
105
+ } finally {
106
+ fs.closeSync(fd);
107
+ }
95
108
  return true;
96
109
  } catch (e) {
97
110
  console.warn("[cache] markProcessed failed:", e.message);
package/lib/options.js ADDED
@@ -0,0 +1,66 @@
1
+ 'use strict';
2
+
3
+ /** Kept self-contained: the exact same validator is embedded in the workflow. */
4
+ function normalizeWorkflowOptions(raw, behavior) {
5
+ const defaults = {
6
+ minWords: 2500, fastMode: false, maxChunks: 4, transcribe: true,
7
+ requireCitations: true, includeTimestamps: false, outputDir: './output',
8
+ tempDir: './.tmp', whisperModel: 'small', language: 'zh', device: 'cpu',
9
+ maxRetries: 0, frontmatter: 'obsidian'
10
+ };
11
+ const settings = behavior || {};
12
+ const tolerant = settings.mode === 'config';
13
+ const warn = typeof settings.warn === 'function' ? settings.warn : function () {};
14
+ const result = Object.assign({}, defaults);
15
+ function invalid(key, reason) {
16
+ if (!tolerant) throw new Error('[FATAL] options' + (key ? '.' + key : '') + ' 非法:' + reason);
17
+ warn('[deep-read-summarize] ignoring config' + (key ? '.' + key : '') + ' — ' + reason);
18
+ }
19
+ if (raw === undefined) return result;
20
+ if (raw === null || typeof raw !== 'object' || Array.isArray(raw)) {
21
+ invalid('', 'expected an options object');
22
+ return result;
23
+ }
24
+ const keys = Object.keys(defaults);
25
+ const unknown = Object.keys(raw).filter(function (key) { return keys.indexOf(key) < 0; });
26
+ if (unknown.length) {
27
+ if (!tolerant) throw new Error('[FATAL] options 未知配置键:' + unknown.join(', ') + '(allowed keys: ' + keys.join(', ') + ')');
28
+ warn('[deep-read-summarize] ignoring unknown config key(s): ' + unknown.join(', '));
29
+ warn('[deep-read-summarize] allowed keys: ' + keys.join(', '));
30
+ }
31
+ const models = [
32
+ 'tiny', 'tiny.en', 'base', 'base.en', 'small', 'small.en', 'medium', 'medium.en',
33
+ 'large', 'large-v1', 'large-v2', 'large-v3', 'large-v3-turbo', 'turbo',
34
+ 'distil-large-v2', 'distil-medium.en', 'distil-small.en', 'distil-large-v3'
35
+ ];
36
+ for (const key of keys) {
37
+ if (!Object.prototype.hasOwnProperty.call(raw, key) || raw[key] === undefined) continue;
38
+ let value = raw[key];
39
+ let reason = '';
40
+ if (key === 'minWords' || key === 'maxChunks' || key === 'maxRetries') {
41
+ const minimum = key === 'maxRetries' ? 0 : 1;
42
+ const maximum = key === 'minWords' ? 100000 : (key === 'maxRetries' ? 3 : Infinity);
43
+ if (typeof value !== 'number' || !Number.isFinite(value) || !Number.isInteger(value) || value < minimum || value > maximum) {
44
+ reason = 'expected a finite integer in ' + minimum + '..' + (maximum === Infinity ? '12 (larger values are capped)' : maximum);
45
+ } else if (key === 'maxChunks' && value > 12) {
46
+ if (tolerant) warn('[deep-read-summarize] config.maxChunks capped at 12 (received ' + value + ')');
47
+ value = 12;
48
+ }
49
+ } else if (typeof defaults[key] === 'boolean') {
50
+ if (typeof value !== 'boolean') reason = 'expected boolean';
51
+ } else if (typeof value !== 'string' || !value.trim() || /[\u0000\r\n]/.test(value)) {
52
+ reason = 'expected a non-empty string without NUL or line breaks';
53
+ } else if (key !== 'outputDir' && key !== 'tempDir') {
54
+ value = value.trim().toLowerCase();
55
+ if (key === 'frontmatter' && ['obsidian', 'generic', 'minimal'].indexOf(value) < 0) reason = 'expected obsidian|generic|minimal';
56
+ if (key === 'device' && ['cpu', 'auto', 'cuda'].indexOf(value) < 0) reason = 'expected cpu|auto|cuda';
57
+ if (key === 'whisperModel' && models.indexOf(value) < 0) reason = 'unsupported Whisper model';
58
+ if (key === 'language' && !/^[a-z]{2,3}$/.test(value)) reason = 'expected a two- or three-letter language code';
59
+ }
60
+ if (reason) invalid(key, reason);
61
+ else result[key] = value;
62
+ }
63
+ return result;
64
+ }
65
+
66
+ module.exports = { normalizeWorkflowOptions, DEFAULT_OPTIONS: normalizeWorkflowOptions() };
@@ -0,0 +1,197 @@
1
+ 'use strict';
2
+
3
+ const fs = require('node:fs');
4
+ const path = require('node:path');
5
+ const acorn = require('acorn');
6
+
7
+ // Acorn handles the regexp/division, template and modern syntax boundaries.
8
+ // The two-pass scope walk distinguishes free host APIs from local bindings and
9
+ // property names. Only literal relative CommonJS imports are bundled.
10
+ function analyze(source, filename) {
11
+ const comments = [];
12
+ let tree;
13
+ try { tree = acorn.parse(source, { ecmaVersion: 'latest', sourceType: 'commonjs', onComment: comments }); }
14
+ catch (error) { throw new Error('[FATAL] cannot parse parser module ' + filename + ': ' + error.message); }
15
+ const nodes = [];
16
+ const scopes = new WeakMap();
17
+ const bindings = new WeakSet();
18
+ const identifiers = new Set();
19
+ function scope(parent, kind) { return { parent, kind, names: new Set() }; }
20
+ function children(node, visit) {
21
+ for (const key of Object.keys(node)) {
22
+ const value = node[key];
23
+ if (Array.isArray(value)) {
24
+ for (const child of value) if (child && typeof child.type === 'string') visit(child, node, key);
25
+ } else if (value && typeof value.type === 'string') visit(value, node, key);
26
+ }
27
+ }
28
+ function bind(pattern, target) {
29
+ if (!pattern) return;
30
+ if (pattern.type === 'Identifier') { target.names.add(pattern.name); bindings.add(pattern); }
31
+ else if (pattern.type === 'RestElement') bind(pattern.argument, target);
32
+ else if (pattern.type === 'AssignmentPattern') bind(pattern.left, target);
33
+ else if (pattern.type === 'ArrayPattern') pattern.elements.forEach(item => bind(item, target));
34
+ else if (pattern.type === 'ObjectPattern') pattern.properties.forEach(item => bind(item.type === 'RestElement' ? item.argument : item.value, target));
35
+ }
36
+ function collect(node, active, parent, key) {
37
+ if (/^Function/.test(node.type) || node.type === 'ArrowFunctionExpression') {
38
+ if (node.type === 'FunctionDeclaration') bind(node.id, active);
39
+ const parameters = scope(active, 'parameters');
40
+ bind(node.id, parameters);
41
+ node.params.forEach(param => bind(param, parameters));
42
+ scopes.set(node, parameters);
43
+ nodes.push({ node, parent, key });
44
+ node.params.forEach(param => collect(param, parameters, node, 'params'));
45
+ if (node.id) collect(node.id, parameters, node, 'id');
46
+ collect(node.body, scope(parameters, 'function'), node, 'body');
47
+ return;
48
+ }
49
+ if (node.type === 'ClassDeclaration') bind(node.id, active);
50
+ if (node.type === 'ClassDeclaration' || node.type === 'ClassExpression') {
51
+ active = scope(active, 'block'); bind(node.id, active);
52
+ } else if (['BlockStatement', 'CatchClause', 'ForStatement', 'ForInStatement', 'ForOfStatement', 'SwitchStatement'].indexOf(node.type) >= 0) {
53
+ active = scope(active, 'block');
54
+ } else if (node.type === 'StaticBlock') active = scope(active, 'function');
55
+ if (node.type === 'CatchClause') bind(node.param, active);
56
+ if (node.type === 'VariableDeclaration') {
57
+ let target = active;
58
+ if (node.kind === 'var') while (target.parent && target.kind !== 'function' && target.kind !== 'program') target = target.parent;
59
+ node.declarations.forEach(declaration => bind(declaration.id, target));
60
+ }
61
+ scopes.set(node, active);
62
+ if (node.type === 'Identifier') identifiers.add(node.name);
63
+ nodes.push({ node, parent, key });
64
+ children(node, (child, owner, field) => collect(child, active, owner, field));
65
+ }
66
+ collect(tree, scope(null, 'program'), null, '');
67
+ function bound(node) {
68
+ for (let current = scopes.get(node); current; current = current.parent) if (current.names.has(node.name)) return true;
69
+ return false;
70
+ }
71
+ function reference(item) {
72
+ const { node, parent, key } = item;
73
+ if (node.type !== 'Identifier' || bindings.has(node)) return false;
74
+ if (!parent) return true;
75
+ if ((parent.type === 'MemberExpression' && key === 'property' && !parent.computed) ||
76
+ (['Property', 'MethodDefinition', 'PropertyDefinition'].indexOf(parent.type) >= 0 && key === 'key' && !parent.computed) ||
77
+ (['LabeledStatement', 'BreakStatement', 'ContinueStatement'].indexOf(parent.type) >= 0 && key === 'label')) return false;
78
+ return true;
79
+ }
80
+ return { nodes, comments, identifiers, bound, reference };
81
+ }
82
+
83
+ function standaloneParserSource(parser, name) {
84
+ let fn = Function.prototype.toString.call(parser.buildPrompt);
85
+ // Method shorthand is valid inside an object, but not after a property colon.
86
+ if (!/^(?:async\s+)?function\b/.test(fn) && /^(?:async\s+)?[\w$]+\s*\(/.test(fn)) {
87
+ fn = fn.replace(/^(async\s+)?/, function (_, asyncPrefix) { return (asyncPrefix || '') + 'function '; });
88
+ }
89
+ try { new Function('return (' + fn + ');'); }
90
+ catch (error) { throw new Error('[FATAL] cannot serialize parser ' + name + ': ' + error.message); }
91
+ const data = Object.create(null);
92
+ for (const key of Object.keys(parser)) {
93
+ if (typeof parser[key] !== 'function' && parser[key] !== undefined) data[key] = parser[key];
94
+ }
95
+ const fields = JSON.stringify(data).slice(1, -1);
96
+ return '{ buildPrompt: ' + fn + (fields ? ', ' + fields : '') + ' }';
97
+ }
98
+
99
+ /** Bundle local CommonJS modules while retaining each module's lexical scope. */
100
+ function buildParserBundle(registry) {
101
+ const modules = [];
102
+ const ids = new Map();
103
+ function addModule(filename) {
104
+ const resolved = fs.realpathSync(filename);
105
+ if (ids.has(resolved)) return ids.get(resolved);
106
+ const id = 'm' + modules.length;
107
+ ids.set(resolved, id);
108
+ const entry = { id, code: '', importName: '__drsLocalImport' };
109
+ modules.push(entry); // reserve before recursion, so CommonJS cycles work
110
+ const ext = path.extname(resolved).toLowerCase();
111
+ if (ext === '.json') {
112
+ const json = JSON.parse(fs.readFileSync(resolved, 'utf8').replace(/^\uFEFF/, ''));
113
+ entry.code = 'module.exports = JSON.parse(' + JSON.stringify(JSON.stringify(json)) + ');';
114
+ return id;
115
+ }
116
+ if (ext !== '.js' && ext !== '.cjs') throw new Error('[FATAL] parser module must be local CommonJS (.js/.cjs/.json): ' + resolved);
117
+ const source = fs.readFileSync(resolved, 'utf8').replace(/^\uFEFF/, '').replace(/^#![^\n]*/, '');
118
+ const scanned = analyze(source, resolved);
119
+ while (scanned.identifiers.has(entry.importName)) entry.importName += '_';
120
+ const edits = scanned.comments.map(function (comment) {
121
+ return { start: comment.start, end: comment.end, text: source.slice(comment.start, comment.end).replace(/[^\n]/g, ' ') };
122
+ });
123
+ const unavailable = ['process', 'Buffer', '__dirname', '__filename', 'global'];
124
+ for (const item of scanned.nodes) {
125
+ const current = item.node;
126
+ if (current.type === 'ImportExpression' || current.type === 'WithStatement') {
127
+ throw new Error('[FATAL] ' + (current.type === 'ImportExpression' ? 'dynamic import is unavailable' : 'with scopes are unsupported') + ' in parser bundle: ' + resolved);
128
+ }
129
+ if (current.type === 'MemberExpression' && current.object.type === 'Identifier' && !scanned.bound(current.object)) {
130
+ const owner = current.object.name;
131
+ const member = !current.computed && current.property.type === 'Identifier' ? current.property.name :
132
+ (current.computed && current.property.type === 'Literal' ? current.property.value : undefined);
133
+ if ((owner === 'globalThis' && (member === undefined || unavailable.indexOf(member) >= 0 || member === 'require')) ||
134
+ (owner === 'module' && member === 'require')) {
135
+ throw new Error('[FATAL] unavailable workflow API ' + owner + '.' + (member === undefined ? '<dynamic>' : member) + ' in parser module: ' + resolved);
136
+ }
137
+ }
138
+ if (!scanned.reference(item) || scanned.bound(current)) continue;
139
+ if (unavailable.indexOf(current.name) >= 0) {
140
+ throw new Error('[FATAL] parser module uses unavailable workflow global ' + current.name + ': ' + resolved);
141
+ }
142
+ if (current.name === 'eval' || current.name === 'Function') {
143
+ throw new Error('[FATAL] dynamic code generation is unsupported in parser bundle: ' + resolved);
144
+ }
145
+ if (current.name === 'globalThis' && (!item.parent || item.parent.type !== 'MemberExpression' || item.key !== 'object')) {
146
+ throw new Error('[FATAL] aliases of host globals are unsupported in parser bundle: ' + resolved);
147
+ }
148
+ if (current.name !== 'require') continue;
149
+ const call = item.parent;
150
+ const request = call && call.type === 'CallExpression' && item.key === 'callee' && !call.optional && call.arguments.length === 1 ? call.arguments[0] : null;
151
+ if (!request || request.type !== 'Literal' || typeof request.value !== 'string') {
152
+ throw new Error('[FATAL] parser imports must use require with a literal local path: ' + resolved);
153
+ }
154
+ const specifier = request.value;
155
+ if (!/^\.\.?[\\/]/.test(specifier)) {
156
+ throw new Error('[FATAL] unsupported parser dependency ' + JSON.stringify(specifier) + ' in ' + resolved + '; workflow bundles accept relative local modules only');
157
+ }
158
+ let dependency;
159
+ try { dependency = require.resolve(path.resolve(path.dirname(resolved), specifier)); }
160
+ catch (error) { throw new Error('[FATAL] cannot resolve parser dependency ' + specifier + ' in ' + resolved + ': ' + error.message); }
161
+ edits.push({ start: call.start, end: call.end, text: entry.importName + '(' + JSON.stringify(addModule(dependency)) + ')' });
162
+ }
163
+ edits.sort(function (a, b) { return b.start - a.start; });
164
+ let rewritten = source;
165
+ for (const edit of edits) rewritten = rewritten.slice(0, edit.start) + edit.text + rewritten.slice(edit.end);
166
+ try { new Function('module', 'exports', entry.importName, rewritten); }
167
+ catch (error) { throw new Error('[FATAL] cannot compile parser module ' + resolved + ': ' + error.message); }
168
+ entry.code = rewritten;
169
+ return id;
170
+ }
171
+ const parserEntries = Object.keys(registry.parsers).map(function (name) {
172
+ const parser = registry.parsers[name];
173
+ if (!parser || typeof parser.buildPrompt !== 'function') throw new Error('[FATAL] parser ' + name + ' must expose buildPrompt(input, options)');
174
+ const filename = typeof registry.sourceFor === 'function' ? registry.sourceFor(parser) : undefined;
175
+ const source = filename
176
+ ? '{ buildPrompt: function (input, options) { return __bundleLoad(' + JSON.stringify(addModule(filename)) + ').buildPrompt(input, options); } }'
177
+ : standaloneParserSource(parser, name);
178
+ return ' ' + (name === '__proto__' ? '[' + JSON.stringify(name) + ']' : JSON.stringify(name)) + ': ' + source;
179
+ });
180
+ return [
181
+ 'const __bundleFactories = {',
182
+ modules.map(function (entry) { return JSON.stringify(entry.id) + ': function (module, exports, ' + entry.importName + ') {\n' + entry.code + '\n}'; }).join(',\n'),
183
+ '};',
184
+ 'const __bundleCache = Object.create(null);',
185
+ 'function __bundleLoad(id) {',
186
+ ' if (__bundleCache[id]) return __bundleCache[id].exports;',
187
+ ' const module = { exports: {} };',
188
+ ' __bundleCache[id] = module;',
189
+ ' try { __bundleFactories[id].call(module.exports, module, module.exports, __bundleLoad); }',
190
+ ' catch (error) { delete __bundleCache[id]; throw error; }',
191
+ ' return module.exports;',
192
+ '}',
193
+ 'const __parsers = {', parserEntries.join(',\n'), '};'
194
+ ].join('\n');
195
+ }
196
+
197
+ module.exports = { buildParserBundle };
package/lib/quality.js ADDED
@@ -0,0 +1,218 @@
1
+ 'use strict';
2
+
3
+ // Both functions are self-contained so workflow.js can inline them into the
4
+ // coordination sandbox. They never evaluate YAML or read files themselves.
5
+ function checkNoteStructure(note, options) {
6
+ const opts = options || {};
7
+ const issues = [];
8
+ if (typeof note !== 'string' || !note.trim()) {
9
+ return { formatComplete: false, lengthMet: false, wordCount: 0, quotes: [], issues: ['笔记为空或不是字符串'] };
10
+ }
11
+ const text = note.trim().replace(/\r\n?/g, '\n');
12
+ const front = text.match(/^---[ \t]*\n([\s\S]*?)\n---[ \t]*(?:\n|$)/);
13
+ function scalar(raw) {
14
+ const value = raw.trim();
15
+ if (!value || /[\u0000-\u0008\u000b\u000c\u000e-\u001f\u007f]/.test(value)) throw new Error('missing or invalid scalar');
16
+ if (value[0] === '"') {
17
+ const parsed = JSON.parse(value);
18
+ if (typeof parsed !== 'string') throw new Error('expected string');
19
+ return parsed;
20
+ }
21
+ if (value[0] === "'") {
22
+ if (value.length < 2 || value[value.length - 1] !== "'" || /(^|[^'])'([^']|$)/.test(value.slice(1, -1))) throw new Error('invalid quoted scalar');
23
+ return value.slice(1, -1).replace(/''/g, "'");
24
+ }
25
+ if (value[0] === '[') {
26
+ if (value[value.length - 1] !== ']') throw new Error('invalid sequence');
27
+ const tokens = [];
28
+ let token = '', quote = '', escaped = false;
29
+ for (const character of value.slice(1, -1)) {
30
+ if (quote) {
31
+ token += character;
32
+ if (escaped) escaped = false;
33
+ else if (quote === '"' && character === '\\') escaped = true;
34
+ else if (character === quote) quote = '';
35
+ } else if (character === '"' || character === "'") {
36
+ quote = character; token += character;
37
+ } else if (character === ',') {
38
+ tokens.push(scalar(token)); token = '';
39
+ } else token += character;
40
+ }
41
+ if (quote) throw new Error('unclosed quote');
42
+ if (token.trim()) tokens.push(scalar(token));
43
+ return tokens;
44
+ }
45
+ if (/:(?:[ \t]|$)|[\[\]{},]|^[!&*|>@`%#]|^[-?:](?:[ \t]|$)/.test(value)) throw new Error('invalid template scalar');
46
+ const plain = value.replace(/[ \t]+#.*$/, '').trim();
47
+ // Template values are strings. YAML null/booleans/numbers must stay quoted
48
+ // instead of silently passing as a different native YAML type.
49
+ if (/^(?:null|~|true|false|[-+]?(?:0o[0-7]+|0x[0-9a-f]+|[0-9]+(?:\.[0-9]*)?(?:e[-+]?[0-9]+)?|\.[0-9]+(?:e[-+]?[0-9]+)?|\.inf)|\.nan)$/i.test(plain)) throw new Error('template strings must be quoted');
50
+ return plain;
51
+ }
52
+ function fields(lines) {
53
+ const values = Object.create(null);
54
+ for (const line of lines) {
55
+ if (!line.trim() || /^\s*#/.test(line)) continue;
56
+ const match = line.match(/^([A-Za-z][\w-]*):[ \t]*(.*)$/);
57
+ if (!match || Object.prototype.hasOwnProperty.call(values, match[1])) throw new Error('invalid or duplicate field');
58
+ values[match[1]] = scalar(match[2]);
59
+ }
60
+ return values;
61
+ }
62
+ let frontmatterValid = !!front;
63
+ if (!front) issues.push('缺少开头的 YAML frontmatter');
64
+ else {
65
+ try {
66
+ const actual = fields(front[1].split('\n'));
67
+ const expected = fields((opts.expectedFrontmatter || []).filter(line => line !== '---'));
68
+ for (const key of Object.keys(expected)) {
69
+ if (!Object.prototype.hasOwnProperty.call(actual, key) || JSON.stringify(actual[key]) !== JSON.stringify(expected[key])) {
70
+ frontmatterValid = false;
71
+ issues.push('frontmatter 字段缺失或不符合本次模板:' + key);
72
+ }
73
+ }
74
+ if (!Object.prototype.hasOwnProperty.call(actual, 'title') || !actual.title) {
75
+ frontmatterValid = false; issues.push('frontmatter 缺少有效 title');
76
+ }
77
+ } catch (error) {
78
+ frontmatterValid = false;
79
+ issues.push('frontmatter 不符合提供的标量/列表模板,或包含重复字段');
80
+ }
81
+ }
82
+ const body = front ? text.slice(front[0].length) : text;
83
+ // Ignore fenced examples when checking real headings and prose length.
84
+ const prose = [];
85
+ let fence = '', fenceLength = 0;
86
+ const visibleBody = body.replace(/<!--[\s\S]*?(?:-->|$)/g, '')
87
+ .replace(/<(script|style|noscript)\b[^>]*>[\s\S]*?(?:<\/\1\s*>|$)/gi, '');
88
+ for (const line of visibleBody.split('\n')) {
89
+ const marker = line.match(/^\s*(`{3,}|~{3,})/);
90
+ if (marker) {
91
+ if (!fence) { fence = marker[1][0]; fenceLength = marker[1].length; }
92
+ else if (marker[1][0] === fence && marker[1].length >= fenceLength && /^\s*$/.test(line.slice(marker[0].length))) fence = '';
93
+ continue;
94
+ }
95
+ if (!fence && !/^(?: {4}|\t)/.test(line)) prose.push(line);
96
+ }
97
+ const proseText = prose.join('\n');
98
+ let sectionsComplete = /^#[ \t]+\S/m.test(proseText) && !fence;
99
+ if (!/^#[ \t]+\S/m.test(proseText)) issues.push('缺少笔记一级标题');
100
+ if (fence) issues.push('存在未闭合的代码围栏');
101
+ const requiredCount = opts.fastMode ? 4 : 7;
102
+ for (let section = 1; section <= requiredCount; section++) {
103
+ const heading = new RegExp('^##[ \\t]+' + section + '(?:[.、.][ \\t]*|[ \\t]+)\\S', 'gm');
104
+ const found = proseText.match(heading) || [];
105
+ if (found.length !== 1) {
106
+ sectionsComplete = false;
107
+ issues.push('必要章节缺失或重复:第 ' + section + ' 节');
108
+ }
109
+ }
110
+ const countable = proseText.replace(/https?:\/\/[^\s)>\]]+/g, '');
111
+ const cjk = /[\p{Script=Han}\p{Script=Hiragana}\p{Script=Katakana}]/gu;
112
+ const ideographs = countable.match(cjk) || [];
113
+ const words = countable.replace(cjk, ' ').match(/[\p{L}\p{N}]+(?:['’\-][\p{L}\p{N}]+)*/gu) || [];
114
+ const wordCount = ideographs.length + words.length;
115
+ const lengthMet = wordCount >= opts.minWords;
116
+ if (!lengthMet) issues.push('正文篇幅不足:计数 ' + wordCount + ',要求至少 ' + opts.minWords + '(不计 frontmatter、代码块和链接地址)');
117
+ const quotes = [];
118
+ function inlineText(value) {
119
+ // Normalize display syntax only. Source evidence below remains literal so
120
+ // emphasis cannot make an invented quote match a different source phrase.
121
+ return value.replace(/!?\[([^\]\n]+)\]\([^\n)]*\)/g, '$1')
122
+ .replace(/(\*\*|__)(?=\S)([\s\S]*?\S)\1/g, '$2')
123
+ .replace(/(^|[^\w\\])([*_])(?=\S)([^\n]*?\S)\2/g, '$1$3')
124
+ .replace(/(`+)([^`\n]+)\1/g, '$2');
125
+ }
126
+ const quoteStart = prose.findIndex(line => /^##[ \t]+4(?:[.、.][ \t]*|[ \t]+)\S/.test(line));
127
+ if (quoteStart !== -1) {
128
+ const quoteLines = [];
129
+ for (const line of prose.slice(quoteStart + 1)) {
130
+ if (/^##[ \t]/.test(line)) break;
131
+ quoteLines.push(line);
132
+ }
133
+ for (const line of quoteLines) {
134
+ // A blockquote can contain a quoted sentence followed by its location.
135
+ // Prefer the explicit sentence so Markdown markers and locations do not become source text.
136
+ const expression = /“([^”\n]+)”|「([^」\n]+)」|"([^"\n]+)"/g;
137
+ let match, explicit = false;
138
+ while ((match = expression.exec(line))) {
139
+ explicit = true;
140
+ const quote = inlineText(match[1] || match[2] || match[3]).trim();
141
+ if (quote && quotes.indexOf(quote) === -1) quotes.push(quote);
142
+ }
143
+ if (!explicit && /^>[ \t]+/.test(line)) {
144
+ // A trailing, explicitly labelled locator belongs to the citation,
145
+ // not to the sentence. Do not remove arbitrary parenthetical prose.
146
+ const quote = inlineText(line.replace(/^(?:>[ \t]*)+/, ''))
147
+ .replace(/[ \t]*[((](?:第[ \t]*\d+[ \t]*(?:[-–][ \t]*\d+[ \t]*)?(?:行|页|章|节|段)|(?:lines?|pages?|chapter|section)[ \t]+\d+(?:[ \t]*[-–][ \t]*\d+)?|\d{1,2}:\d{2}(?::\d{2})?(?:\.\d+)?)[))][ \t]*$/i, '').trim();
148
+ if (quote && quotes.indexOf(quote) === -1) quotes.push(quote);
149
+ }
150
+ }
151
+ }
152
+ return { formatComplete: frontmatterValid && sectionsComplete, lengthMet, wordCount, quotes, visibleText: inlineText(proseText), issues };
153
+ }
154
+
155
+ function validateSourceCheck(check, context) {
156
+ const ctx = context || {};
157
+ const issues = [];
158
+ const gates = ['sourceChecked', 'coverage', 'citationsReal', 'terminologyConsistent', 'languageConsistent'];
159
+ const results = {};
160
+ for (const gate of gates) {
161
+ results[gate] = !!check && check[gate] === true;
162
+ if (!results[gate]) issues.push('原文核验未通过或缺失:' + gate);
163
+ }
164
+ if (check && Array.isArray(check.issues)) {
165
+ issues.push(...check.issues.filter(issue => typeof issue === 'string' && issue.trim()));
166
+ }
167
+ const plan = ctx.chunkPlan || [];
168
+ const chunkChecks = check && Array.isArray(check.chunkChecks) ? check.chunkChecks : [];
169
+ const seen = new Set();
170
+ for (const evidence of chunkChecks) {
171
+ const chunk = evidence && plan.find(item => item.id === evidence.chunkId);
172
+ if (!chunk || seen.has(evidence.chunkId) || evidence.startLine !== chunk.startLine || evidence.endLine !== chunk.endLine ||
173
+ typeof evidence.sourceExcerpt !== 'string' || !evidence.sourceExcerpt.trim() || evidence.covered !== true) {
174
+ results.coverage = false;
175
+ issues.push('分块核验证据无效、重复或未覆盖');
176
+ } else seen.add(evidence.chunkId);
177
+ }
178
+ if (plan.some(chunk => !seen.has(chunk.id))) {
179
+ results.coverage = false; issues.push('原文核验未逐一覆盖全部分块');
180
+ }
181
+ if (ctx.coverageComplete !== true) {
182
+ results.coverage = false; issues.push('获取结果缺少有效 totalLines,无法确认原文尾部是否完整覆盖');
183
+ }
184
+ if (ctx.sourceComplete === false) {
185
+ results.coverage = false;
186
+ issues.push('来源不完整:' + (ctx.textSource === 'desc' ? '仅有视频简介,不能代替完整字幕或逐字稿' : '视频来源未经确认为完整字幕或逐字稿'));
187
+ }
188
+ if (ctx.failedChunkIds && ctx.failedChunkIds.length) {
189
+ results.coverage = false; issues.push('精读仍有缺口,失败块:' + ctx.failedChunkIds.join(', '));
190
+ }
191
+ function normalize(value) { return typeof value === 'string' ? value.replace(/\s+/g, ' ').trim() : ''; }
192
+ const citationChecks = check && Array.isArray(check.citationChecks) ? check.citationChecks : [];
193
+ const requiredQuotes = (ctx.quotes || []).map(normalize);
194
+ const verifiedQuotes = [];
195
+ for (const citation of citationChecks) {
196
+ const chunk = citation && plan.find(item => item.id === citation.chunkId);
197
+ const quote = normalize(citation && citation.quote);
198
+ const valid = chunk && citation.verified === true && quote && requiredQuotes.includes(quote) &&
199
+ normalize(ctx.visibleNote === undefined ? ctx.note : ctx.visibleNote).includes(quote) &&
200
+ normalize(citation.sourceQuote).includes(quote) && typeof citation.location === 'string' && citation.location.trim() &&
201
+ Number.isInteger(citation.startLine) && Number.isInteger(citation.endLine) &&
202
+ citation.startLine >= chunk.startLine && citation.endLine >= citation.startLine && citation.endLine <= chunk.endLine;
203
+ if (!valid) {
204
+ results.citationsReal = false; issues.push('引用无法与笔记、原文摘录及分块行号对应');
205
+ } else verifiedQuotes.push(quote);
206
+ }
207
+ if (ctx.requireCitations && (requiredQuotes.length === 0 || verifiedQuotes.length === 0)) {
208
+ results.citationsReal = false; issues.push('未提交有效的原文引用核验记录');
209
+ }
210
+ for (const quote of ctx.quotes || []) {
211
+ if (!verifiedQuotes.some(verified => verified === normalize(quote))) {
212
+ results.citationsReal = false; issues.push('关键引用缺少对应的原文核验记录:' + quote);
213
+ }
214
+ }
215
+ return { passed: issues.length === 0 && gates.every(gate => results[gate]), checks: results, issues, chunkChecks, citationChecks };
216
+ }
217
+
218
+ module.exports = { checkNoteStructure, validateSourceCheck };