@li-canghai/dsh-paper 0.8.3 → 0.8.7
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/README.md +1 -0
- package/THIRD_PARTY_NOTICES.md +41 -30
- package/lib/asset-routes.js +49 -2
- package/lib/client-parts/01-pure-helpers.js +197 -2
- package/lib/client.js +660 -187
- package/lib/fonts/NotoSansSC-common.woff2 +0 -0
- package/lib/fonts/NotoSerifSC-common.woff2 +0 -0
- package/lib/fonts/OFL.txt +93 -0
- package/lib/gloss.js +21 -6
- package/lib/index.js +20 -1
- package/lib/pdf-page.js +95 -13
- package/lib/translate-prompts.js +279 -12
- package/lib/translate.js +8 -1
- package/package.json +2 -2
package/lib/translate-prompts.js
CHANGED
|
@@ -97,8 +97,8 @@ function idOf(block) {
|
|
|
97
97
|
// ============ 导出 1:构造提示词 ============
|
|
98
98
|
|
|
99
99
|
// 输出要求按**协议**分岔。json 是默认(通用模型),与加这个开关之前逐字节一致;
|
|
100
|
-
// lines 给专用机器翻译模型(如本地 hy-mt2)用 —— 它们不吐 JSON
|
|
101
|
-
//
|
|
100
|
+
// lines 给专用机器翻译模型(如本地 hy-mt2 / Hy-MT3)用 —— 它们不吐 JSON 数组,只吐「每行一条」
|
|
101
|
+
// 的方言,详见 parseLineDialect 与 parseNdjsonDialect 里记的实测。
|
|
102
102
|
const OUTPUT_RULES = {
|
|
103
103
|
json: [
|
|
104
104
|
'只输出一个 JSON 数组,不要任何解释文字,不要 Markdown 围栏。',
|
|
@@ -469,15 +469,13 @@ const LINE_TAIL = /^[\]"'\s]+/
|
|
|
469
469
|
function parseLineDialect(text, expected) {
|
|
470
470
|
const translations = []
|
|
471
471
|
const seen = new Set()
|
|
472
|
-
/**
|
|
472
|
+
/** 最近一条**已成条**的译文;表格行与公式块往它身上接续行。 */
|
|
473
473
|
let current = null
|
|
474
474
|
for (const line of String(text).split(/\r?\n/)) {
|
|
475
475
|
const rest = line.replace(LINE_HEAD, '')
|
|
476
476
|
const match = LINE_ID.exec(rest)
|
|
477
|
-
// 续行只认表格行(行首 `|`):表格译文本身就是多行,第一行跟在 id 后面,其余行紧跟其后。
|
|
478
|
-
// 🔴 只收 `|` 行 —— 其他多行内容照旧丢弃,这条改动不能放宽「每行一条」的既有语义。
|
|
479
477
|
if (match === null || !expected.set.has(match[0].trim())) {
|
|
480
|
-
if (current !== null &&
|
|
478
|
+
if (current !== null && continuesCurrent(line, current.zh)) current.zh += '\n' + line.trim()
|
|
481
479
|
continue
|
|
482
480
|
}
|
|
483
481
|
// 正则没有捕获组:匹配到的整段就是 id 本体(`[^\[\]"]+`)。
|
|
@@ -489,6 +487,121 @@ function parseLineDialect(text, expected) {
|
|
|
489
487
|
current = { id, zh }
|
|
490
488
|
translations.push(current)
|
|
491
489
|
}
|
|
490
|
+
for (const item of translations) item.zh = stripStrayCloseBracket(item.zh)
|
|
491
|
+
return translations
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
/**
|
|
495
|
+
* 这一行是不是**上一条译文的续行**。
|
|
496
|
+
*
|
|
497
|
+
* 两条判据,都不放宽「每行一条」:一条没接完的译文才收续行,接完就停。
|
|
498
|
+
*
|
|
499
|
+
* 1. **表格行**(行首 `|`):表格译文本身就是多行,第一行跟在 id 后面,其余行紧跟其后。
|
|
500
|
+
* 2. **显示公式块**(`$$` 未配平):实测 2026-11-27 真实论文(AgentPoison,Hy-MT3)——
|
|
501
|
+
* `["p4-e1"] $$` 之后公式体与收尾 `$$` 各占一行:
|
|
502
|
+
*
|
|
503
|
+
* ```text
|
|
504
|
+
* ["p4-e1"] $$
|
|
505
|
+
* \mathbb {E} _ {q \sim \pi_ {q}} [ \mathbb {1} (… = a _ {m}) ],\tag{1}
|
|
506
|
+
* $$
|
|
507
|
+
* ```
|
|
508
|
+
*
|
|
509
|
+
* 🔴 只认 `|` 行时这三行只剩第一行,译文变成字面上的 `$$` —— 而它非空、结构校验也过得去,
|
|
510
|
+
* 于是被当成**成功译文**写进 translation.json。那不是「翻译失败」,是静默的数据损坏:
|
|
511
|
+
* 读者拿到一对空公式符,而原文里是一整条公式。公式块必须按未配平的 `$$` 收全。
|
|
512
|
+
*
|
|
513
|
+
* 判据用 `$$` 出现次数的**奇偶**而不是「行首是不是 `$$`」:模型也会写成
|
|
514
|
+
* `["p4-e1"] $$
|
|
515
|
+
…
|
|
516
|
+
$$` 或整条公式挤在一行(那行 `$$` 成对,不需要续行)。
|
|
517
|
+
*
|
|
518
|
+
* @param {string} line 原始行(未剥壳)
|
|
519
|
+
* @param {string} sofar 该条已经收下的译文
|
|
520
|
+
* @returns {boolean}
|
|
521
|
+
*/
|
|
522
|
+
function continuesCurrent(line, sofar) {
|
|
523
|
+
if (/^\s*\|/.test(line)) return true
|
|
524
|
+
return mathOpen(sofar)
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
/** 显示公式(`$$`)是否还没收尾 —— 奇偶即可,不必解析内容。 */
|
|
528
|
+
function mathOpen(text) {
|
|
529
|
+
return (String(text).split('$$').length - 1) % 2 === 1
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
/**
|
|
533
|
+
* 剥掉译文末尾**多出来的**收尾方括号。
|
|
534
|
+
*
|
|
535
|
+
* 实测(同上,Hy-MT3 行方言):模型把行首标记的收尾写到了正文后面 ——
|
|
536
|
+
* `["p4-p5"] 其中 $a _ { b }$ 表示…不同。]`,正文末尾多一个 `]`。
|
|
537
|
+
* 它与行首的 `["p4-p5"]` 是配对的,剥行首时只剥掉了一半。
|
|
538
|
+
*
|
|
539
|
+
* 🔴 只在**确实多出一个**时才剥(`]` 比 `[` 多、且串尾就是 `]`)。正文自身成对的方括号
|
|
540
|
+
* 必须原样保留:引用编号 `[39]`、占位符 `[Your definition here]` 都在译文里正常出现。
|
|
541
|
+
*
|
|
542
|
+
* @param {string} zh
|
|
543
|
+
* @returns {string}
|
|
544
|
+
*/
|
|
545
|
+
function stripStrayCloseBracket(zh) {
|
|
546
|
+
let text = String(zh)
|
|
547
|
+
while (text.endsWith(']')) {
|
|
548
|
+
const open = (text.match(/\[/g) ?? []).length
|
|
549
|
+
const close = (text.match(/\]/g) ?? []).length
|
|
550
|
+
if (close <= open) break
|
|
551
|
+
text = text.slice(0, -1).trimEnd()
|
|
552
|
+
}
|
|
553
|
+
return text
|
|
554
|
+
}
|
|
555
|
+
|
|
556
|
+
/**
|
|
557
|
+
* 按 **NDJSON 行方言**解析回复:每行一个完整的 `{"id":…,"zh":…}` 对象。
|
|
558
|
+
*
|
|
559
|
+
* 🔴 专用机器翻译模型在「要 JSON 数组」的指令下常常**省掉外层方括号**,改成一行一条记录。
|
|
560
|
+
* 实测(本地 tencent/Hy-MT3-7B-GGUF,Unsloth Studio,2026-11-27)7 块的整批回复是
|
|
561
|
+
*
|
|
562
|
+
* ```text
|
|
563
|
+
* {"id":"p1-h1","zh":"Attention Is All You Need"}
|
|
564
|
+
* {"id":"p1-p1","zh":"主流序列转换模型基于复杂的循环或卷积神经网络…"}
|
|
565
|
+
* ```
|
|
566
|
+
*
|
|
567
|
+
* 逐行合法,合起来却不是 JSON 数组 —— `parseArraySlice` 只能报 `no array found`,
|
|
568
|
+
* 于是整批判失败、译文一条都收不回来。
|
|
569
|
+
*
|
|
570
|
+
* 收它**不会**削弱 JSON 路径:调用方先试数组、再试行 id 方言、最后才到这里;三条路径汇合到
|
|
571
|
+
* 同一段收敛逻辑,重复 id / 空译文 / 不在期望列表的判据只有一份。
|
|
572
|
+
*
|
|
573
|
+
* 只认**整行就是一个对象**的行(前后可有空白):模型顺手加的解释文字、围栏标记、行首 `[id]`
|
|
574
|
+
* 前缀都不吞进来 —— 逐行 `JSON.parse` 只要有一处宽松解析就会把正文里的花括号当成条目。
|
|
575
|
+
*
|
|
576
|
+
* 🔴 与 {@link parseLineDialect} 不同,这里**不按期望 id 过滤**:一条合法 NDJSON 记录必须同时有
|
|
577
|
+
* 字符串 id 与 zh,正文里的花括号短语凑不出这个形状;而过滤放给 {@link collectTranslations}
|
|
578
|
+
* 才能把「id 不在期望列表」记进 unexpected(行方言靠期望集合挡引用编号,两者判据不同源)。
|
|
579
|
+
*
|
|
580
|
+
* @param {string} text
|
|
581
|
+
* @returns {Array<{id: string, zh: string}>} 按出现顺序
|
|
582
|
+
*/
|
|
583
|
+
function parseNdjsonDialect(text) {
|
|
584
|
+
const translations = []
|
|
585
|
+
// 同一处字节问题在 NDJSON 里照样存在(`{"zh":"$\m…"}`),逐行修再解析。
|
|
586
|
+
const source = repairIllegalJsonEscapes(text)
|
|
587
|
+
for (const line of source.split(/\r?\n/)) {
|
|
588
|
+
// 🔴 **行尾逗号要剥**:同一模型同一档会吐出两种形态 —— 不带逗号(7 块测试批次)
|
|
589
|
+
// 与带逗号(6 块批次:`{…},\n{…},\n{…}`,只差外层方括号)。后者若不剥,
|
|
590
|
+
// 除最后一行外全部认不出(实测 6 块只认领到 1 块)。
|
|
591
|
+
const trimmed = line.trim().replace(/,$/, '')
|
|
592
|
+
// 围栏行(```json / ```)与解释文字不是条目;只有整行对象才进解析。
|
|
593
|
+
if (!trimmed.startsWith('{') || !trimmed.endsWith('}')) continue
|
|
594
|
+
let parsed
|
|
595
|
+
try {
|
|
596
|
+
parsed = JSON.parse(trimmed)
|
|
597
|
+
} catch {
|
|
598
|
+
continue
|
|
599
|
+
}
|
|
600
|
+
if (parsed === null || typeof parsed !== 'object' || Array.isArray(parsed)) continue
|
|
601
|
+
if (typeof parsed.id !== 'string' || typeof parsed.zh !== 'string') continue
|
|
602
|
+
// NDJSON 同样经过 JSON.parse,同样会被解码 —— 与 JSON 数组那条路共用同一个还原。
|
|
603
|
+
translations.push({ id: parsed.id, zh: restoreDecodedLatex(parsed.zh) })
|
|
604
|
+
}
|
|
492
605
|
return translations
|
|
493
606
|
}
|
|
494
607
|
|
|
@@ -521,6 +634,148 @@ function normalizeExpected(expectedIds) {
|
|
|
521
634
|
return { list, set }
|
|
522
635
|
}
|
|
523
636
|
|
|
637
|
+
/**
|
|
638
|
+
* 把 `JSON.parse` 顺手解码掉的 LaTeX 反斜杠还原回来。
|
|
639
|
+
*
|
|
640
|
+
* 🔴 这是**静默的数据损坏**,比解析失败严重得多(2026-11-27 真实论文实测)。
|
|
641
|
+
*
|
|
642
|
+
* JSON 字符串里 `\t` `\b` `\f` 是**合法转义**,而模型在 zh 字段里写 LaTeX 时不会做二次转义
|
|
643
|
+
* (它以为那是纯文本)。于是下面三条全部「解析成功、译文非空、状态 ok」,公式却已经烂了:
|
|
644
|
+
*
|
|
645
|
+
* ```text
|
|
646
|
+
* $\top$ → 制表符 + "op" ($\top$ 是转置符号)
|
|
647
|
+
* $\bar{x}$ → 退格符 + "ar{x}" (\bar 是上划线)
|
|
648
|
+
* $\frac{1}{2}$ → 换页符 + "rac{1}{2}"(\frac 是分式)
|
|
649
|
+
* ```
|
|
650
|
+
*
|
|
651
|
+
* 渲染出来是乱码,而日志里一个字都不会提 —— 只有把译文与原文逐字对拍才发现得了。
|
|
652
|
+
*
|
|
653
|
+
* ## 为什么只还原这三个
|
|
654
|
+
*
|
|
655
|
+
* 判据是「这个控制字符在译文里**永远不可能是真的**」:
|
|
656
|
+
*
|
|
657
|
+
* - 制表符、退格、换页在论文译文里没有任何合法用途(GFM 表格也不用制表符对齐);
|
|
658
|
+
* - 而 `\n` 与 `\r` 是**真实需要**的:公式块的 `$$\n…\n$$` 就是真换行,还原成字面量反而毁掉它。
|
|
659
|
+
* ⚠️ 代价是 `\nabla` `\nu` `\neq` `\rho` `\right` 这类以 n / r 开头的命令仍会被解码。
|
|
660
|
+
* 这一条**没有安全的自动修法** —— `\n` 既可能是换行也可能是 `\nabla`,两种解释都合法,
|
|
661
|
+
* 猜错就是把公式改成另一条公式。宁可留着这个已知缺口,也不做不可靠的猜测。
|
|
662
|
+
*
|
|
663
|
+
* 实测依据:AgentPoison 那篇 264 个块里,原文含控制字符的是 **0 个** —— 所以「译文里出现
|
|
664
|
+
* 这三个控制字符」本身就是一个干净的损坏信号,不会误伤正常内容。
|
|
665
|
+
*
|
|
666
|
+
* @param {string} zh
|
|
667
|
+
* @returns {string}
|
|
668
|
+
*/
|
|
669
|
+
function restoreDecodedLatex(zh) {
|
|
670
|
+
if (typeof zh !== 'string' || zh === '') return zh
|
|
671
|
+
if (!DECODED_LATEX_CONTROL.test(zh)) return zh
|
|
672
|
+
let text = zh
|
|
673
|
+
for (const [from, to] of DECODED_LATEX) text = text.split(from).join(to)
|
|
674
|
+
return text
|
|
675
|
+
}
|
|
676
|
+
|
|
677
|
+
/**
|
|
678
|
+
* 把模型回复里**不合法的 JSON 转义**补成合法转义,让 `JSON.parse` 能读。
|
|
679
|
+
*
|
|
680
|
+
* 🔴 这不是「容错解析」,是**修字面 bug**:模型在 zh 字段里写 LaTeX 时**不做二次转义**,
|
|
681
|
+
* 而 LaTeX 命令全是反斜杠开头。真实回复(AgentPoison,Hy-MT3,2026-11-27)统计:
|
|
682
|
+
*
|
|
683
|
+
* ```text
|
|
684
|
+
* \m 27 处 \c 5 处 \h 4 处 \s 3 处 \p 3 处
|
|
685
|
+
* \{ 2 处 \} 2 处 … 合计 52 处非法转义
|
|
686
|
+
* ```
|
|
687
|
+
*
|
|
688
|
+
* `\m` `\c` `\h` 这些**根本不是合法的 JSON 转义**,`JSON.parse` 当场抛
|
|
689
|
+
* `Bad escaped character`。于是整批 19 个块判失败,一条译文都收不回来 —— 而模型其实
|
|
690
|
+
* 全都译对了,只是那一处字节不合规。
|
|
691
|
+
*
|
|
692
|
+
* ## 两件事,判据都无歧义
|
|
693
|
+
*
|
|
694
|
+
* 1. **非法转义**(`\m` `\{` 这类):除了「补一个反斜杠」没有别种解释。合法转义只有
|
|
695
|
+
* `" \\ / b f n r t u`,其余一律是写坏的 LaTeX。`u` 只在后四位是十六进制时才放行。
|
|
696
|
+
* 2. **字符串里的裸控制字符**(模型直接写了个真换行,而 JSON 要求写成 `\n`):
|
|
697
|
+
* 它只能是「本来就想表达这个控制字符」,按 JSON 规则转义回去。
|
|
698
|
+
*
|
|
699
|
+
* ## 为什么敢动字节
|
|
700
|
+
*
|
|
701
|
+
* 只修**确定非法**的位置;合法转义一个都不碰,所以对正常回复是恒等变换(有测试钉住)。
|
|
702
|
+
* 修完仍解析不出的,照旧 Fail Closed —— 这条路径不会让坏数据混进来。
|
|
703
|
+
*
|
|
704
|
+
* ⚠️ `b f n r t` 这五个**合法但歧义**的转义**不在这里处理**:`\top` 和「制表符+op」是同一
|
|
705
|
+
* 字节序列,改它等于猜。它们由 {@link restoreDecodedLatex} 在解析后按「译文里不可能有控制
|
|
706
|
+
* 字符」这条依据还原。
|
|
707
|
+
*
|
|
708
|
+
* @param {string} text
|
|
709
|
+
* @returns {string}
|
|
710
|
+
*/
|
|
711
|
+
function repairIllegalJsonEscapes(text) {
|
|
712
|
+
const source = String(text)
|
|
713
|
+
let out = ''
|
|
714
|
+
let inString = false
|
|
715
|
+
for (let index = 0; index < source.length; index += 1) {
|
|
716
|
+
const ch = source[index]
|
|
717
|
+
const code = ch.charCodeAt(0)
|
|
718
|
+
if (!inString) {
|
|
719
|
+
out += ch
|
|
720
|
+
if (ch === '"') inString = true
|
|
721
|
+
continue
|
|
722
|
+
}
|
|
723
|
+
if (ch === '"') {
|
|
724
|
+
inString = false
|
|
725
|
+
out += ch
|
|
726
|
+
continue
|
|
727
|
+
}
|
|
728
|
+
// 字符串字面量里的裸控制字符:JSON 要求转义书写,补上。
|
|
729
|
+
if (code < 0x20) {
|
|
730
|
+
if (ch === '\n') out += '\\n'
|
|
731
|
+
else if (ch === '\r') out += '\\r'
|
|
732
|
+
else if (ch === '\t') out += '\\t'
|
|
733
|
+
else out += '\\u' + code.toString(16).padStart(4, '0')
|
|
734
|
+
continue
|
|
735
|
+
}
|
|
736
|
+
if (ch !== '\\') {
|
|
737
|
+
out += ch
|
|
738
|
+
continue
|
|
739
|
+
}
|
|
740
|
+
const next = source[index + 1]
|
|
741
|
+
if (next === undefined) {
|
|
742
|
+
out += ch
|
|
743
|
+
continue
|
|
744
|
+
}
|
|
745
|
+
// 🔴 转义的**成对反斜杠要一起吃掉**:`\\\\` 是合法转义,它的第二个反斜杠不是新转义的
|
|
746
|
+
// 开头。只消费第一个的话,第二个会被判成「非法转义」再补一个,把 `\\\\mathbb` 变成
|
|
747
|
+
// `\\\\\\mathbb` —— 一个完全合法的回复反而被改坏。
|
|
748
|
+
if (next === '\\') {
|
|
749
|
+
out += ch + next
|
|
750
|
+
index += 1
|
|
751
|
+
continue
|
|
752
|
+
}
|
|
753
|
+
if (next === 'u' && /^[0-9a-fA-F]{4}$/.test(source.slice(index + 2, index + 6))) {
|
|
754
|
+
out += ch
|
|
755
|
+
continue
|
|
756
|
+
}
|
|
757
|
+
if (JSON_ESCAPE_LETTERS.has(next)) {
|
|
758
|
+
out += ch
|
|
759
|
+
continue
|
|
760
|
+
}
|
|
761
|
+
// 非法转义:补一个反斜杠,让它变成字面量的反斜杠 + 原字符。
|
|
762
|
+
out += ch + ch
|
|
763
|
+
}
|
|
764
|
+
return out
|
|
765
|
+
}
|
|
766
|
+
|
|
767
|
+
/** JSON 规定的单字符转义字母。`u` 另判(要跟四位十六进制)。 */
|
|
768
|
+
const JSON_ESCAPE_LETTERS = new Set(['"', '\\', '/', 'b', 'f', 'n', 'r', 't'])
|
|
769
|
+
|
|
770
|
+
/** 三个「译文里永远不可能是真的」控制字符:退格、换页、制表。 */
|
|
771
|
+
const DECODED_LATEX_CONTROL = /[\u0008\u000c\u0009]/
|
|
772
|
+
/** 还原表。顺序无关:替换出来的都是反斜杠 + 字母,不会再撞上控制字符。 */
|
|
773
|
+
const DECODED_LATEX = [
|
|
774
|
+
['\u0008', '\\b'],
|
|
775
|
+
['\u000c', '\\f'],
|
|
776
|
+
['\u0009', '\\t'],
|
|
777
|
+
]
|
|
778
|
+
|
|
524
779
|
function failure(reason, expected, notes) {
|
|
525
780
|
notes.push(reason)
|
|
526
781
|
return {
|
|
@@ -550,7 +805,11 @@ export function parseBatchReply(text, expectedIds) {
|
|
|
550
805
|
|
|
551
806
|
let parsed = null
|
|
552
807
|
let reason = 'no array found'
|
|
553
|
-
|
|
808
|
+
// 🔴 先修非法转义再解析:模型写 LaTeX 时不给反斜杠做二次转义,`\m` `\{` 这类在 JSON 里
|
|
809
|
+
// 直接抛 Bad escaped character,整批 19 块判失败 —— 而模型其实全都译对了。
|
|
810
|
+
// 只动**确定非法**的位置,合法转义一个不碰,对正常回复是恒等变换。
|
|
811
|
+
const repaired = repairIllegalJsonEscapes(text)
|
|
812
|
+
for (const candidate of candidateTexts(repaired)) {
|
|
554
813
|
const attempt = parseArraySlice(candidate)
|
|
555
814
|
if (attempt.items) {
|
|
556
815
|
parsed = attempt.items
|
|
@@ -558,12 +817,17 @@ export function parseBatchReply(text, expectedIds) {
|
|
|
558
817
|
}
|
|
559
818
|
reason = attempt.reason
|
|
560
819
|
}
|
|
561
|
-
// JSON
|
|
562
|
-
//
|
|
820
|
+
// JSON 数组解析不出来时,依次退一步认两种**行方言**(专用翻译模型):
|
|
821
|
+
// 1. `["<块id>"] 译文`(Hy-MT2 的自然方言,见 parseLineDialect);
|
|
822
|
+
// 2. 每行一个 `{"id":…,"zh":…}` 对象(Hy-MT3 在「要数组」指令下的真实形态,见 parseNdjsonDialect)。
|
|
823
|
+
// 三种路径的条目形状一致,汇合到同一段收敛逻辑 —— 重复 id、不在期望列表、
|
|
824
|
+
// id/zh 非字符串的判据只有一份。
|
|
563
825
|
if (!parsed) {
|
|
564
826
|
const lines = parseLineDialect(text, expected)
|
|
565
|
-
if (lines.length
|
|
566
|
-
|
|
827
|
+
if (lines.length > 0) return collectTranslations(lines, expected, notes)
|
|
828
|
+
const records = parseNdjsonDialect(text)
|
|
829
|
+
if (records.length === 0) return failure(reason, expected.list, notes)
|
|
830
|
+
return collectTranslations(records, expected, notes)
|
|
567
831
|
}
|
|
568
832
|
return collectTranslations(parsed, expected, notes)
|
|
569
833
|
}
|
|
@@ -675,7 +939,10 @@ function collectTranslations(parsed, expected, notes) {
|
|
|
675
939
|
if (!unexpected.includes(item.id)) unexpected.push(item.id)
|
|
676
940
|
continue
|
|
677
941
|
}
|
|
678
|
-
|
|
942
|
+
// 🔴 JSON 数组与 NDJSON 两条路径都在这里汇合,而两条都经过 `JSON.parse` ——
|
|
943
|
+
// 被解码成控制字符的 LaTeX 反斜杠必须在**唯一出口**还原,否则 json 档会静默写坏公式。
|
|
944
|
+
// 行方言不经过 JSON 解码,还原函数对它无副作用(没有控制字符就直接返回)。
|
|
945
|
+
translations.push({ id: item.id, zh: restoreDecodedLatex(item.zh) })
|
|
679
946
|
}
|
|
680
947
|
|
|
681
948
|
const missing = expected.list.filter((id) => !seen.has(id))
|
package/lib/translate.js
CHANGED
|
@@ -477,9 +477,16 @@ async function attemptBatch(batch, deps, acquireImages, messages) {
|
|
|
477
477
|
// (实测:终止原因明确是 max-tokens、正文只有半句,结果仍是 translated:1 / status:ok)。
|
|
478
478
|
// 终止原因为空(适配器不发 finish)时不作判断,保持与加这条规则之前一致。
|
|
479
479
|
if (finish !== '' && finish !== 'stop') {
|
|
480
|
+
// max-tokens 与其余终止原因分开说:这一条几乎总是**模型没在答**(或块太长),
|
|
481
|
+
// 而不是网络或额度问题 —— 实测(本地 Hy-MT3,6000 字符的单块)它会把预算全花在
|
|
482
|
+
// 续写与复述上,吐回 28 770 字符(原文的 4.8 倍)后才被截断,这一个块空跑 65 秒。
|
|
483
|
+
// 用户看到「终止原因 length」不知道下一步做什么,这里直接把可操作的两条写出来。
|
|
484
|
+
const hint = finish === 'length' || finish === 'max-tokens'
|
|
485
|
+
? ';模型把输出预算用光了:多半是它在复述或续写而不是作答。把设置页的「范围」改成「前 N 页」缩小单批原文,或换一个更守格式的模型'
|
|
486
|
+
: ''
|
|
480
487
|
return {
|
|
481
488
|
translations: [],
|
|
482
|
-
detail: `回复没有正常结束(终止原因 ${finish}
|
|
489
|
+
detail: `回复没有正常结束(终止原因 ${finish}),不收取被截断的译文${hint};${replyDigest(raw, finish)}`,
|
|
483
490
|
malformed: true,
|
|
484
491
|
missing: [...batch.blockIds],
|
|
485
492
|
usage,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@li-canghai/dsh-paper",
|
|
3
|
-
"version": "0.8.
|
|
3
|
+
"version": "0.8.7",
|
|
4
4
|
"description": "PDF transcription with stable block ids, folder scanning up to two sub-levels, and session-scoped paper context with drag-select batch injection and removal for DSH.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "lib/index.js",
|
|
@@ -59,4 +59,4 @@
|
|
|
59
59
|
]
|
|
60
60
|
}
|
|
61
61
|
}
|
|
62
|
-
}
|
|
62
|
+
}
|