@li-canghai/dsh-paper 0.0.0-stage → 0.8.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/LICENSE +21 -0
- package/README.md +137 -2
- package/cordis.patch.yml +4 -0
- package/lib/annotate-tool.js +334 -0
- package/lib/arxiv-id.js +74 -0
- package/lib/asset-routes.js +210 -0
- package/lib/bibliography-formats.js +126 -0
- package/lib/bibliography-import.js +105 -0
- package/lib/bibliography-service.js +43 -0
- package/lib/bibliography.js +42 -0
- package/lib/block-authors.js +292 -0
- package/lib/block-cache.js +39 -0
- package/lib/blocks-markdown.js +157 -0
- package/lib/blocks-reconcile.js +218 -0
- package/lib/blocks.js +1344 -0
- package/lib/citation-metadata.js +260 -0
- package/lib/citation-styles.js +120 -0
- package/lib/client-parts/01-pure-helpers.js +2607 -0
- package/lib/client.js +9569 -0
- package/lib/code-source.js +68 -0
- package/lib/concurrency-gate.js +91 -0
- package/lib/context.js +233 -0
- package/lib/csl/SOURCES.md +10 -0
- package/lib/csl/gb-t-7714.csl +441 -0
- package/lib/csl/ieee.csl +520 -0
- package/lib/csl/locales-zh-CN.xml +438 -0
- package/lib/csl/nature.csl +190 -0
- package/lib/discussion.js +400 -0
- package/lib/documents.js +67 -0
- package/lib/export.js +848 -0
- package/lib/file-lock.js +177 -0
- package/lib/gloss.js +181 -0
- package/lib/glossary.js +279 -0
- package/lib/index.js +3862 -0
- package/lib/inline-html.js +53 -0
- package/lib/install-dependency.js +120 -0
- package/lib/item-ops.js +83 -0
- package/lib/job-facade.js +58 -0
- package/lib/json-store.js +54 -0
- package/lib/library-model.js +132 -0
- package/lib/library-service.js +180 -0
- package/lib/library-tool.js +982 -0
- package/lib/library.js +814 -0
- package/lib/markdown-assets.js +138 -0
- package/lib/markdown-literals.js +93 -0
- package/lib/metadata-enrichment.js +516 -0
- package/lib/metadata-fetch.js +146 -0
- package/lib/metadata-node.js +158 -0
- package/lib/metadata-proxy.ps1 +35 -0
- package/lib/mineru-config.js +95 -0
- package/lib/mineru-sdk-helper.py +123 -0
- package/lib/mineru-sdk.js +182 -0
- package/lib/mineru.js +268 -0
- package/lib/mirror-export.js +240 -0
- package/lib/outline.js +296 -0
- package/lib/page-image-helper.py +294 -0
- package/lib/page-image.js +454 -0
- package/lib/paper-metadata.js +242 -0
- package/lib/parse-export.js +631 -0
- package/lib/pdf-analysis-tool.js +750 -0
- package/lib/pdf-metadata-helper.py +75 -0
- package/lib/pdf-metadata.js +141 -0
- package/lib/pdf-page.js +183 -0
- package/lib/pdf-scan.js +134 -0
- package/lib/pdf-text.js +68 -0
- package/lib/picker.js +305 -0
- package/lib/portable.js +379 -0
- package/lib/purge.js +170 -0
- package/lib/read-tool.js +660 -0
- package/lib/reader-store.js +497 -0
- package/lib/reading-order.js +207 -0
- package/lib/rename-document.js +480 -0
- package/lib/rename.js +372 -0
- package/lib/renderer-status.js +161 -0
- package/lib/resolver.js +1152 -0
- package/lib/result-archive.js +157 -0
- package/lib/revisions.js +330 -0
- package/lib/search-database.js +62 -0
- package/lib/search-index.js +601 -0
- package/lib/search-scan.js +185 -0
- package/lib/search-source.js +23 -0
- package/lib/search-terms.js +539 -0
- package/lib/section-audit.js +209 -0
- package/lib/selection-message.js +332 -0
- package/lib/serial-tail.js +21 -0
- package/lib/slicing.js +425 -0
- package/lib/source-resolver.js +581 -0
- package/lib/stem-repair.js +189 -0
- package/lib/storage.js +562 -0
- package/lib/store-paths.js +253 -0
- package/lib/table-gfm.js +236 -0
- package/lib/tasks.js +270 -0
- package/lib/tex-align.js +509 -0
- package/lib/tex-blocks.js +653 -0
- package/lib/tex-fetch.js +455 -0
- package/lib/tex-figures.js +140 -0
- package/lib/tex-inline.js +166 -0
- package/lib/tex-merge.js +279 -0
- package/lib/tex-pipeline.js +242 -0
- package/lib/tex-prepare.js +746 -0
- package/lib/tex-tables.js +210 -0
- package/lib/text-similarity.js +85 -0
- package/lib/tool-contract.js +86 -0
- package/lib/tool-host.js +1058 -0
- package/lib/tool-identity.js +214 -0
- package/lib/translatable-text.js +48 -0
- package/lib/translate-prompts.js +709 -0
- package/lib/translate-queue.js +418 -0
- package/lib/translate-task.js +158 -0
- package/lib/translate.js +909 -0
- package/lib/update-check.js +438 -0
- package/lib/update-contracts.js +75 -0
- package/lib/usage.js +282 -0
- package/package.json +55 -4
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 模型侧「指认一篇论文」的解析器。
|
|
3
|
+
*
|
|
4
|
+
* 🔴 为什么需要它:注入上下文里**没有** documentId —— `<dsh-paper-reference>` 只给
|
|
5
|
+
* `pdf-name`、`markdown-path` 与章节树。工具若坚持要 64 位 id,模型只能从路径里抠,
|
|
6
|
+
* 或者干脆编一个;`Paper_annotate` 原来的描述写「从注入的论文上下文里取」其实取不到。
|
|
7
|
+
* 所以对模型统一只暴露一个字符串 `paper`,由本模块折成唯一的文档记录。
|
|
8
|
+
*
|
|
9
|
+
* 纯函数、无 IO:调用方先取 snapshot,再把 `documents` 传进来。
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
/** 文档 id 是 PDF 内容的 sha256 十六进制串。 */
|
|
13
|
+
export const DOCUMENT_ID_PATTERN = /^[0-9a-f]{64}$/i
|
|
14
|
+
|
|
15
|
+
/** 一次最多列几个候选:够模型认出来即可,不必把整库倒出来。 */
|
|
16
|
+
export const MAX_CANDIDATES = 5
|
|
17
|
+
|
|
18
|
+
function textOf(value) {
|
|
19
|
+
return typeof value === 'string' ? value : ''
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const lower = value => textOf(value).trim().toLowerCase()
|
|
23
|
+
const canonical = value => lower(value).normalize('NFC')
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* 去掉成对引号与首尾空白。
|
|
27
|
+
*
|
|
28
|
+
* 与 `documents.js` 的 `inspectPdf` 同口径:资源管理器「复制文件地址」给的就是带引号的串,
|
|
29
|
+
* 模型转述路径时也常顺手加引号。只有两端成对才剥,单边引号属畸形输入,原样留给匹配去失败。
|
|
30
|
+
*
|
|
31
|
+
* @param {unknown} value 模型给的原始值
|
|
32
|
+
* @returns {string} 归一化后的引用
|
|
33
|
+
*/
|
|
34
|
+
export function normalizeReference(value) {
|
|
35
|
+
const raw = textOf(value).trim()
|
|
36
|
+
if (raw.length >= 2) {
|
|
37
|
+
const first = raw[0]
|
|
38
|
+
const last = raw[raw.length - 1]
|
|
39
|
+
if ((first === '"' && last === '"') || (first === "'" && last === "'")) return raw.slice(1, -1).trim()
|
|
40
|
+
}
|
|
41
|
+
return raw
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* 取文件名部分。
|
|
46
|
+
*
|
|
47
|
+
* 不 import `node:path`:POSIX 的 `basename` 会把 `C:\\papers\\a.pdf` 整个当成一个名字,
|
|
48
|
+
* 而这个插件的路径全部来自 Windows。两种分隔符都认,跨平台测试也给同一个答案。
|
|
49
|
+
*
|
|
50
|
+
* @param {unknown} value 路径或文件名
|
|
51
|
+
* @returns {string} 最后一段
|
|
52
|
+
*/
|
|
53
|
+
export function baseName(value) {
|
|
54
|
+
const text = textOf(value)
|
|
55
|
+
const cut = Math.max(text.lastIndexOf('\\'), text.lastIndexOf('/'))
|
|
56
|
+
return cut === -1 ? text : text.slice(cut + 1)
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* 去扩展名的主名。`.pdf` 与 `.md` 都剥:模型的两种常见写法都要能命中同一条记录。
|
|
61
|
+
*
|
|
62
|
+
* @param {unknown} value 路径或文件名
|
|
63
|
+
* @returns {string} 主名
|
|
64
|
+
*/
|
|
65
|
+
export function stemOf(value) {
|
|
66
|
+
const base = baseName(value)
|
|
67
|
+
return base.replace(/\.(?:pdf|md)$/i, '')
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* 标点归一:保留字母、字形标记、数字与技术符号,其他连续段折成单个空格。
|
|
72
|
+
*
|
|
73
|
+
* 🔴 为什么匹配不能只做 `toLowerCase`:用户与模型写的**文件夹名**里,冒号、连字符、全角字符
|
|
74
|
+
* 会各写各的 —— 磁盘上是 `2023 - Generative Agents- Interactive Simulacra`(冒号被规范化成
|
|
75
|
+
* 连字符),模型照论文标题写成 `Generative Agents: Interactive Simulacra`。
|
|
76
|
+
* 只比小写就得报「找不到」(实测:按文件夹名寻址一直失败)。
|
|
77
|
+
* 归一之后 `:` / `-` / `—` / 全角变体都等价,词序不变;C++ 与 C# 等名字仍有区别。
|
|
78
|
+
*/
|
|
79
|
+
export function normalizePunctuation(value) {
|
|
80
|
+
return textOf(value).normalize('NFKC').toLowerCase().replace(/[<>|*]|[^\p{L}\p{M}\p{N}\p{S}#]+/gu, ' ').replace(/\s+/g, ' ').trim()
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** 一条记录的全部「可被指认的名字」;记录内部去重,避免同一串被数两次。 */
|
|
84
|
+
function keysOf(document, normalize = lower) {
|
|
85
|
+
const keys = new Set()
|
|
86
|
+
const push = value => {
|
|
87
|
+
const text = normalize(value)
|
|
88
|
+
if (text !== '') keys.add(text)
|
|
89
|
+
}
|
|
90
|
+
push(document?.id)
|
|
91
|
+
push(document?.pdfName)
|
|
92
|
+
push(stemOf(document?.pdfName))
|
|
93
|
+
push(document?.pdfPath)
|
|
94
|
+
push(document?.markdownPath)
|
|
95
|
+
push(stemOf(document?.markdownPath))
|
|
96
|
+
push(document?.title)
|
|
97
|
+
push(document?.sourcePdfPath)
|
|
98
|
+
push(baseName(document?.sourcePdfPath))
|
|
99
|
+
push(stemOf(document?.sourcePdfPath))
|
|
100
|
+
// 🔴 论文在用户工作区里的**文件夹名**(`<分类>/2023 - 标题/`)也必须能用来指认 ——
|
|
101
|
+
// 用户看的是资源管理器里的目录名,那是最自然的写法。取 sourceDir 的最后一段。
|
|
102
|
+
push(baseName(document?.sourceDir))
|
|
103
|
+
push(document?.sourceDir)
|
|
104
|
+
return keys
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* 把「一条论文」折成 {@link identifyDocument} 认的记录形状。**调用方不必自己拼。**
|
|
109
|
+
*
|
|
110
|
+
* 🔴 为什么要收成一个函数:判据里每多一个可匹配的名字(`sourceDir` 的文件夹名尤其),
|
|
111
|
+
* 就必须在**每一处**构造记录的地方同步加一行。实测漏过两次 —— `Paper_library` 与
|
|
112
|
+
* `Paper_read` 各自手拼一份,只补了一处,于是「按文件夹名找不到」在一个工具上好了、
|
|
113
|
+
* 在另一个上照旧。收成一处之后,加字段只有这一个落点。
|
|
114
|
+
*
|
|
115
|
+
* 接受三种来源(state 记录 / 库视图行 / 读工具的条目):同名字段含义一致,
|
|
116
|
+
* id 两种键名都认(记录用 `id`,库行用 `documentId`)。
|
|
117
|
+
*
|
|
118
|
+
* @param {unknown} entry 任意一条论文来源。
|
|
119
|
+
* @returns {object | null} 记录形状;不是对象时返回 null(调用方自行过滤)。
|
|
120
|
+
*/
|
|
121
|
+
export function identityRecordOf(entry) {
|
|
122
|
+
if (entry === null || typeof entry !== 'object') return null
|
|
123
|
+
return {
|
|
124
|
+
id: entry.id ?? entry.documentId,
|
|
125
|
+
pdfName: entry.pdfName,
|
|
126
|
+
pdfPath: entry.pdfPath,
|
|
127
|
+
markdownPath: entry.markdownPath,
|
|
128
|
+
title: entry.title,
|
|
129
|
+
sourceDir: typeof entry.sourceDir === 'string' ? entry.sourceDir : '',
|
|
130
|
+
sourcePdfPath: typeof entry.sourcePdfPath === 'string' ? entry.sourcePdfPath : '',
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/** 候选的一句话形式:篇名在前(模型一眼能认),id 在后(下一轮可直接用它)。 */
|
|
135
|
+
export function describeCandidate(document) {
|
|
136
|
+
const id = textOf(document?.id)
|
|
137
|
+
const name = textOf(document?.pdfName) || textOf(document?.title)
|
|
138
|
+
if (name === '') return id
|
|
139
|
+
return id === '' ? name : `${name} — ${id}`
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
function listOf(candidates) {
|
|
143
|
+
return candidates.slice(0, MAX_CANDIDATES).map(document => `- ${describeCandidate(document)}`).join('\n')
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
const EMPTY_REFERENCE = '没有给出论文:paper 可以是 64 位 id、PDF 文件名(可省 .pdf)或 Markdown 绝对路径。'
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* 把模型给的引用折成唯一的一篇文档。
|
|
150
|
+
*
|
|
151
|
+
* 两轮匹配:
|
|
152
|
+
* 1. **精确**——id / 文件名 / 主名 / PDF 路径 / Markdown 路径 / 标题,全部大小写不敏感;
|
|
153
|
+
* 2. **包含**——精确全落空时,用主名与标题做子串匹配(模型常写标题的前半截,或把年份作者省了)。
|
|
154
|
+
*
|
|
155
|
+
* 两轮都只接受唯一命中:多于一篇时**不猜**,把候选交回去让模型自己挑;一篇都没有时说明
|
|
156
|
+
* 它可以写什么。子串轮必然更容易多命中,所以它的候选提示比精确轮更重要。
|
|
157
|
+
*
|
|
158
|
+
* @param {ReadonlyArray<object>} documents 文献库里的文档记录
|
|
159
|
+
* @param {unknown} reference 模型给的 paper 值
|
|
160
|
+
* @returns {{ document: object } | { error: string, candidates?: object[] }}
|
|
161
|
+
*/
|
|
162
|
+
export function identifyDocument(documents, reference) {
|
|
163
|
+
const needle = lower(normalizeReference(reference))
|
|
164
|
+
const list = Array.isArray(documents) ? documents.filter(row => row !== null && typeof row === 'object') : []
|
|
165
|
+
if (needle === '') return { code: 'PAPER_REQUIRED', error: EMPTY_REFERENCE }
|
|
166
|
+
|
|
167
|
+
// Literal names precede canonical Unicode and punctuation aliases in each matching tier.
|
|
168
|
+
const canonicalNeedle = canonical(needle)
|
|
169
|
+
const foldedNeedle = normalizePunctuation(reference)
|
|
170
|
+
const idAddress = /^[0-9a-f]{8,64}$/i.test(needle)
|
|
171
|
+
const pathAddress = /^(?:[a-z]:[\\/]|[\\/]|\.{1,2}[\\/])/i.test(needle)
|
|
172
|
+
const pathKey = value => lower(value).replaceAll('\\', '/').replace(/\/{2,}/g, '/')
|
|
173
|
+
let exact = list.filter(document => {
|
|
174
|
+
if (idAddress) return DOCUMENT_ID_PATTERN.test(document.id) && lower(document.id).startsWith(needle)
|
|
175
|
+
if (pathAddress) return [document.pdfPath, document.markdownPath, document.sourcePdfPath, document.sourceDir]
|
|
176
|
+
.some(path => typeof path === 'string' && pathKey(path) === pathKey(needle))
|
|
177
|
+
return keysOf(document).has(needle)
|
|
178
|
+
})
|
|
179
|
+
if (exact.length === 0 && !idAddress && !pathAddress) {
|
|
180
|
+
exact = list.filter(document => keysOf(document, canonical).has(canonicalNeedle))
|
|
181
|
+
}
|
|
182
|
+
if (exact.length === 0 && !idAddress && !pathAddress && foldedNeedle !== '') {
|
|
183
|
+
exact = list.filter(document => keysOf(document, normalizePunctuation).has(foldedNeedle))
|
|
184
|
+
}
|
|
185
|
+
if (exact.length === 1) return { document: exact[0] }
|
|
186
|
+
if (exact.length > 1) {
|
|
187
|
+
return { code: 'AMBIGUOUS_PAPER', error: `「${needle}」匹配到 ${exact.length} 篇论文,请写得更具体(或用 Paper_library 查):\n${listOf(exact)}`, candidates: exact }
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
// Explicit addresses never fall back to a title belonging to a different paper.
|
|
191
|
+
let partial = idAddress || pathAddress ? [] : list.filter(document => {
|
|
192
|
+
const stem = lower(stemOf(document?.pdfName))
|
|
193
|
+
const title = lower(document?.title)
|
|
194
|
+
const folder = lower(baseName(document?.sourceDir))
|
|
195
|
+
return [stem, title, folder].some(value => value !== '' && value.includes(needle))
|
|
196
|
+
})
|
|
197
|
+
if (partial.length === 0 && !idAddress && !pathAddress) {
|
|
198
|
+
partial = list.filter(document => [stemOf(document?.pdfName), document?.title, baseName(document?.sourceDir)]
|
|
199
|
+
.some(value => canonical(value).includes(canonicalNeedle)))
|
|
200
|
+
}
|
|
201
|
+
if (partial.length === 0 && !idAddress && !pathAddress && foldedNeedle !== '') {
|
|
202
|
+
partial = list.filter(document => [document?.pdfName, document?.title, baseName(document?.sourceDir)]
|
|
203
|
+
.some(value => {
|
|
204
|
+
const folded = normalizePunctuation(value)
|
|
205
|
+
return folded !== '' && folded.includes(foldedNeedle)
|
|
206
|
+
}))
|
|
207
|
+
}
|
|
208
|
+
if (partial.length === 1) return { document: partial[0] }
|
|
209
|
+
if (partial.length > 1) {
|
|
210
|
+
return { code: 'AMBIGUOUS_PAPER', error: `「${needle}」匹配到 ${partial.length} 篇论文,请写得更具体(或用 Paper_library 查):\n${listOf(partial)}`, candidates: partial }
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
return { code: 'PAPER_NOT_FOUND', error: `文献库里找不到「${needle}」。paper 可以写 64 位 id、PDF 文件名(可省 .pdf)、Markdown 绝对路径或标题片段;先用 Paper_library 查一下也行。` }
|
|
214
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 一个块**送进翻译管线**的文本 —— 载荷、待译判定、源哈希三条路共用这一份口径。
|
|
3
|
+
*
|
|
4
|
+
* 🔴 存在的理由(实测反馈):翻译载荷过去只取 `block.sourceText`,而**表格块的 sourceText 只是
|
|
5
|
+
* caption**,正文在 `html`(MinerU 的 `table_body`)里。于是表格正文从来没有进过管线:
|
|
6
|
+
* 它既不会被翻译,还会被 `pendingBlocksOf` 当成「空块」跳过 —— 用户看到的是一整张原文表。
|
|
7
|
+
*
|
|
8
|
+
* 表格的可翻译文本 = `caption + GFM 表格`:
|
|
9
|
+
* - 只给 GFM、不给 `<table>` 原串:模型对 HTML 表格的改写不可控,而 GFM 是它的自然方言;
|
|
10
|
+
* - caption 照旧在最前面(它本来就要翻,不能因为加了表体而丢掉);
|
|
11
|
+
* - 转不出 GFM(没有 `<tr>`、宽度为 0)时退回 caption —— 宁可只翻题注,也不给一张错列的表。
|
|
12
|
+
*
|
|
13
|
+
* 代码块**不**走这条:代码里的标识符不该被翻译,它只翻 caption(与之前一致)。
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import { plainInlineText } from './inline-html.js'
|
|
17
|
+
import { tableToGfm } from './table-gfm.js'
|
|
18
|
+
|
|
19
|
+
/** 块对象的判据:数组、null、字符串都不算。 */
|
|
20
|
+
function isBlock(value) {
|
|
21
|
+
return value !== null && typeof value === 'object' && !Array.isArray(value)
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* 送进翻译管线的文本(已折行内标签、已含表格正文)。非块入参返回空串。
|
|
26
|
+
*
|
|
27
|
+
* @param {unknown} block 译文块
|
|
28
|
+
* @returns {string}
|
|
29
|
+
*/
|
|
30
|
+
export function translatableTextOf(block) {
|
|
31
|
+
if (!isBlock(block)) return ''
|
|
32
|
+
const caption = typeof block.sourceText === 'string' ? plainInlineText(block.sourceText).trim() : ''
|
|
33
|
+
if (block.type !== 'table') return caption
|
|
34
|
+
const gfm = tableToGfm(block.html)
|
|
35
|
+
if (gfm === '') return caption
|
|
36
|
+
return caption === '' ? gfm : caption + '\n\n' + gfm
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** 这个块有没有可翻译的内容(口径与 {@link translatableTextOf} 完全一致)。 */
|
|
40
|
+
export function hasTranslatableBlock(block) {
|
|
41
|
+
return translatableTextOf(block).trim() !== ''
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** 块的表格正文(GFM)。不是表格、或转不出来时为空串 —— 译文的结构校验要它。 */
|
|
45
|
+
export function tableBodyOf(block) {
|
|
46
|
+
if (!isBlock(block) || block.type !== 'table') return ''
|
|
47
|
+
return tableToGfm(block.html)
|
|
48
|
+
}
|