dsh-pdf-workbench 0.0.0-stage → 1.3.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 +162 -2
- package/assets/screenshot-bilingual.jpg +0 -0
- package/assets/screenshot-hero.jpg +0 -0
- package/assets/screenshot-overlay.jpg +0 -0
- package/assets/screenshot-qa.jpg +0 -0
- package/assets/screenshot-reader.jpg +0 -0
- package/assets/screenshot-shelf.jpg +0 -0
- package/client.js +13438 -0
- package/cordis.patch.yml +13 -0
- package/docs/DETAIL.md +69 -0
- package/docs/READER-REWRITE.md +95 -0
- package/docs/ROADMAP.md +172 -0
- package/docs/SIDE-PREVIEW.md +271 -0
- package/docs/TESTING.md +109 -0
- package/docs/TRANSLATION.md +167 -0
- package/icon.svg +6 -0
- package/index.js +192 -0
- package/locale/en.json +7 -0
- package/locale/zh.json +7 -0
- package/package.json +64 -6
- package/samples/manifest.json +52 -0
- package/samples/test/serre_c01_p2-3.pdf +0 -0
- package/samples//345/210/206/347/211/207//344/270/255/345/233/275/345/211/251/344/275/231/345/256/232/347/220/206//344/270/255/345/233/275/345/211/251/344/275/231/345/256/232/347/220/206_c00_/345/211/215/347/275/256_/345/260/201/351/235/242/347/233/256/345/275/225.pdf +0 -0
- package/samples//345/210/206/347/211/207//344/270/255/345/233/275/345/211/251/344/275/231/345/256/232/347/220/206//344/270/255/345/233/275/345/211/251/344/275/231/345/256/232/347/220/206_c01_/347/254/2540/347/253/240_/345/257/274/350/257/273.pdf +0 -0
- package/samples//345/210/206/347/211/207//344/270/255/345/233/275/345/211/251/344/275/231/345/256/232/347/220/206//344/270/255/345/233/275/345/211/251/344/275/231/345/256/232/347/220/206_c02_/347/254/2541/347/253/240_/345/217/244/351/242/230_/345/255/231/345/255/220/347/256/227/347/273/217/345/210/260/347/247/246/344/271/235/351/237/266.pdf +0 -0
- package/samples//345/210/206/347/211/207//344/270/255/345/233/275/345/211/251/344/275/231/345/256/232/347/220/206//344/270/255/345/233/275/345/211/251/344/275/231/345/256/232/347/220/206_c03_/347/254/2542/347/253/240_/346/225/264/346/225/260/347/211/210/346/234/254.pdf +0 -0
- package/samples//345/210/206/347/211/207//344/270/255/345/233/275/345/211/251/344/275/231/345/256/232/347/220/206//344/270/255/345/233/275/345/211/251/344/275/231/345/256/232/347/220/206_c04_/347/254/2543/347/253/240_/347/216/257/350/256/272/347/211/210/346/234/254.pdf +0 -0
- package/samples//345/210/206/347/211/207//344/270/255/345/233/275/345/211/251/344/275/231/345/256/232/347/220/206//344/270/255/345/233/275/345/211/251/344/275/231/345/256/232/347/220/206_c05_/347/254/2544/347/253/240_/345/244/232/351/241/271/345/274/217/347/211/210/346/234/254.pdf +0 -0
- package/samples//345/210/206/347/211/207//344/270/255/345/233/275/345/211/251/344/275/231/345/256/232/347/220/206//344/270/255/345/233/275/345/211/251/344/275/231/345/256/232/347/220/206_c06_/347/254/2545/347/253/240_/347/276/244/350/256/272/347/211/210/346/234/254.pdf +0 -0
- package/samples//345/210/206/347/211/207//344/270/255/345/233/275/345/211/251/344/275/231/345/256/232/347/220/206//344/270/255/345/233/275/345/211/251/344/275/231/345/256/232/347/220/206_c07_/347/254/2546/347/253/240_/347/273/237/344/270/200/347/273/223/346/236/204.pdf +0 -0
- package/samples//345/210/206/347/211/207//344/270/255/345/233/275/345/211/251/344/275/231/345/256/232/347/220/206//344/270/255/345/233/275/345/211/251/344/275/231/345/256/232/347/220/206_c08_/347/254/2547/347/253/240_/345/233/236/346/234/233.pdf +0 -0
- package/samples//345/210/206/347/211/207//344/270/255/345/233/275/345/211/251/344/275/231/345/256/232/347/220/206//344/270/255/345/233/275/345/211/251/344/275/231/345/256/232/347/220/206_c90_/351/231/204/345/275/225_/346/225/231/346/235/220/351/241/265/347/240/201/347/254/246/345/217/267/350/241/250/350/207/252/346/265/213.pdf +0 -0
- package/screenshots.json +8 -0
- package/src/client/viewer.js +80 -0
- package/src/host/annotations.js +145 -0
- package/src/host/artifacts.js +317 -0
- package/src/host/blocks.js +44 -0
- package/src/host/config.js +112 -0
- package/src/host/digest.js +156 -0
- package/src/host/environment.js +42 -0
- package/src/host/http.js +124 -0
- package/src/host/index-source.js +488 -0
- package/src/host/libraries.js +224 -0
- package/src/host/links.js +201 -0
- package/src/host/pdf-source.js +122 -0
- package/src/host/pending.js +166 -0
- package/src/host/pressure.js +137 -0
- package/src/host/purge.js +100 -0
- package/src/host/render.js +43 -0
- package/src/host/routes.js +1732 -0
- package/src/host/shelf-groups.js +109 -0
- package/src/host/state.js +128 -0
- package/src/host/toolkit.js +54 -0
- package/src/host/translate-blocks.js +132 -0
- package/src/host/translate-job.js +316 -0
- package/src/host/translate.js +94 -0
- package/src/host/update.js +134 -0
- package/tools/AGENTS.md +33 -0
- package/tools/README.md +138 -0
- package/tools/auto_chapters.py +122 -0
- package/tools/dsfile.mjs +198 -0
- package/tools/export_chapter.py +108 -0
- package/tools/export_page.py +91 -0
- package/tools/md2overlay.py +200 -0
- package/tools/md2tex.py +422 -0
- package/tools/pdf_blocks.py +110 -0
- package/tools/pdf_fontsize.py +20 -0
- package/tools/pdf_merge.py +15 -0
- package/tools/pdf_pagesizes.py +8 -0
- package/tools/pdf_raster.py +21 -0
- package/tools/pdfkit.py +896 -0
- package/tools/realign_overlay.py +248 -0
- package/tools/rebuild_overlay.py +55 -0
- package/tools/rename_chapters.py +93 -0
- package/tools//350/247/204/350/214/203/00_/345/205/261/345/220/214/347/272/246/345/256/232.md +142 -0
- package/tools//350/247/204/350/214/203/01_/346/226/207/346/234/254/346/217/220/345/217/226.md +204 -0
- package/tools//350/247/204/350/214/203/02_/347/255/224/347/226/221.md +61 -0
- package/tools//350/247/204/350/214/203/03_/344/271/240/351/242/230/350/247/243/347/255/224.md +77 -0
- package/tools//350/247/204/350/214/203/api/346/217/220/347/244/272/350/257/215_/346/217/220/345/217/226.md +11 -0
- package/vendor/cmaps/78-EUC-H.bcmap +0 -0
- package/vendor/cmaps/78-EUC-V.bcmap +0 -0
- package/vendor/cmaps/78-H.bcmap +0 -0
- package/vendor/cmaps/78-RKSJ-H.bcmap +0 -0
- package/vendor/cmaps/78-RKSJ-V.bcmap +0 -0
- package/vendor/cmaps/78-V.bcmap +0 -0
- package/vendor/cmaps/78ms-RKSJ-H.bcmap +0 -0
- package/vendor/cmaps/78ms-RKSJ-V.bcmap +0 -0
- package/vendor/cmaps/83pv-RKSJ-H.bcmap +0 -0
- package/vendor/cmaps/90ms-RKSJ-H.bcmap +0 -0
- package/vendor/cmaps/90ms-RKSJ-V.bcmap +0 -0
- package/vendor/cmaps/90msp-RKSJ-H.bcmap +0 -0
- package/vendor/cmaps/90msp-RKSJ-V.bcmap +0 -0
- package/vendor/cmaps/90pv-RKSJ-H.bcmap +0 -0
- package/vendor/cmaps/90pv-RKSJ-V.bcmap +0 -0
- package/vendor/cmaps/Add-H.bcmap +0 -0
- package/vendor/cmaps/Add-RKSJ-H.bcmap +0 -0
- package/vendor/cmaps/Add-RKSJ-V.bcmap +0 -0
- package/vendor/cmaps/Add-V.bcmap +0 -0
- package/vendor/cmaps/Adobe-CNS1-0.bcmap +0 -0
- package/vendor/cmaps/Adobe-CNS1-1.bcmap +0 -0
- package/vendor/cmaps/Adobe-CNS1-2.bcmap +0 -0
- package/vendor/cmaps/Adobe-CNS1-3.bcmap +0 -0
- package/vendor/cmaps/Adobe-CNS1-4.bcmap +0 -0
- package/vendor/cmaps/Adobe-CNS1-5.bcmap +0 -0
- package/vendor/cmaps/Adobe-CNS1-6.bcmap +0 -0
- package/vendor/cmaps/Adobe-CNS1-UCS2.bcmap +0 -0
- package/vendor/cmaps/Adobe-GB1-0.bcmap +0 -0
- package/vendor/cmaps/Adobe-GB1-1.bcmap +0 -0
- package/vendor/cmaps/Adobe-GB1-2.bcmap +0 -0
- package/vendor/cmaps/Adobe-GB1-3.bcmap +0 -0
- package/vendor/cmaps/Adobe-GB1-4.bcmap +0 -0
- package/vendor/cmaps/Adobe-GB1-5.bcmap +0 -0
- package/vendor/cmaps/Adobe-GB1-UCS2.bcmap +0 -0
- package/vendor/cmaps/Adobe-Japan1-0.bcmap +0 -0
- package/vendor/cmaps/Adobe-Japan1-1.bcmap +0 -0
- package/vendor/cmaps/Adobe-Japan1-2.bcmap +0 -0
- package/vendor/cmaps/Adobe-Japan1-3.bcmap +0 -0
- package/vendor/cmaps/Adobe-Japan1-4.bcmap +0 -0
- package/vendor/cmaps/Adobe-Japan1-5.bcmap +0 -0
- package/vendor/cmaps/Adobe-Japan1-6.bcmap +0 -0
- package/vendor/cmaps/Adobe-Japan1-UCS2.bcmap +0 -0
- package/vendor/cmaps/Adobe-Korea1-0.bcmap +0 -0
- package/vendor/cmaps/Adobe-Korea1-1.bcmap +0 -0
- package/vendor/cmaps/Adobe-Korea1-2.bcmap +0 -0
- package/vendor/cmaps/Adobe-Korea1-UCS2.bcmap +0 -0
- package/vendor/cmaps/B5-H.bcmap +0 -0
- package/vendor/cmaps/B5-V.bcmap +0 -0
- package/vendor/cmaps/B5pc-H.bcmap +0 -0
- package/vendor/cmaps/B5pc-V.bcmap +0 -0
- package/vendor/cmaps/CNS-EUC-H.bcmap +0 -0
- package/vendor/cmaps/CNS-EUC-V.bcmap +0 -0
- package/vendor/cmaps/CNS1-H.bcmap +0 -0
- package/vendor/cmaps/CNS1-V.bcmap +0 -0
- package/vendor/cmaps/CNS2-H.bcmap +0 -0
- package/vendor/cmaps/CNS2-V.bcmap +3 -0
- package/vendor/cmaps/ETHK-B5-H.bcmap +0 -0
- package/vendor/cmaps/ETHK-B5-V.bcmap +0 -0
- package/vendor/cmaps/ETen-B5-H.bcmap +0 -0
- package/vendor/cmaps/ETen-B5-V.bcmap +0 -0
- package/vendor/cmaps/ETenms-B5-H.bcmap +3 -0
- package/vendor/cmaps/ETenms-B5-V.bcmap +0 -0
- package/vendor/cmaps/EUC-H.bcmap +0 -0
- package/vendor/cmaps/EUC-V.bcmap +0 -0
- package/vendor/cmaps/Ext-H.bcmap +0 -0
- package/vendor/cmaps/Ext-RKSJ-H.bcmap +0 -0
- package/vendor/cmaps/Ext-RKSJ-V.bcmap +0 -0
- package/vendor/cmaps/Ext-V.bcmap +0 -0
- package/vendor/cmaps/GB-EUC-H.bcmap +0 -0
- package/vendor/cmaps/GB-EUC-V.bcmap +0 -0
- package/vendor/cmaps/GB-H.bcmap +4 -0
- package/vendor/cmaps/GB-V.bcmap +0 -0
- package/vendor/cmaps/GBK-EUC-H.bcmap +0 -0
- package/vendor/cmaps/GBK-EUC-V.bcmap +0 -0
- package/vendor/cmaps/GBK2K-H.bcmap +0 -0
- package/vendor/cmaps/GBK2K-V.bcmap +0 -0
- package/vendor/cmaps/GBKp-EUC-H.bcmap +0 -0
- package/vendor/cmaps/GBKp-EUC-V.bcmap +0 -0
- package/vendor/cmaps/GBT-EUC-H.bcmap +0 -0
- package/vendor/cmaps/GBT-EUC-V.bcmap +0 -0
- package/vendor/cmaps/GBT-H.bcmap +0 -0
- package/vendor/cmaps/GBT-V.bcmap +0 -0
- package/vendor/cmaps/GBTpc-EUC-H.bcmap +0 -0
- package/vendor/cmaps/GBTpc-EUC-V.bcmap +0 -0
- package/vendor/cmaps/GBpc-EUC-H.bcmap +0 -0
- package/vendor/cmaps/GBpc-EUC-V.bcmap +0 -0
- package/vendor/cmaps/H.bcmap +0 -0
- package/vendor/cmaps/HKdla-B5-H.bcmap +0 -0
- package/vendor/cmaps/HKdla-B5-V.bcmap +0 -0
- package/vendor/cmaps/HKdlb-B5-H.bcmap +0 -0
- package/vendor/cmaps/HKdlb-B5-V.bcmap +0 -0
- package/vendor/cmaps/HKgccs-B5-H.bcmap +0 -0
- package/vendor/cmaps/HKgccs-B5-V.bcmap +0 -0
- package/vendor/cmaps/HKm314-B5-H.bcmap +0 -0
- package/vendor/cmaps/HKm314-B5-V.bcmap +0 -0
- package/vendor/cmaps/HKm471-B5-H.bcmap +0 -0
- package/vendor/cmaps/HKm471-B5-V.bcmap +0 -0
- package/vendor/cmaps/HKscs-B5-H.bcmap +0 -0
- package/vendor/cmaps/HKscs-B5-V.bcmap +0 -0
- package/vendor/cmaps/Hankaku.bcmap +0 -0
- package/vendor/cmaps/Hiragana.bcmap +0 -0
- package/vendor/cmaps/KSC-EUC-H.bcmap +0 -0
- package/vendor/cmaps/KSC-EUC-V.bcmap +0 -0
- package/vendor/cmaps/KSC-H.bcmap +0 -0
- package/vendor/cmaps/KSC-Johab-H.bcmap +0 -0
- package/vendor/cmaps/KSC-Johab-V.bcmap +0 -0
- package/vendor/cmaps/KSC-V.bcmap +0 -0
- package/vendor/cmaps/KSCms-UHC-H.bcmap +0 -0
- package/vendor/cmaps/KSCms-UHC-HW-H.bcmap +0 -0
- package/vendor/cmaps/KSCms-UHC-HW-V.bcmap +0 -0
- package/vendor/cmaps/KSCms-UHC-V.bcmap +0 -0
- package/vendor/cmaps/KSCpc-EUC-H.bcmap +0 -0
- package/vendor/cmaps/KSCpc-EUC-V.bcmap +0 -0
- package/vendor/cmaps/Katakana.bcmap +0 -0
- package/vendor/cmaps/LICENSE.txt +36 -0
- package/vendor/cmaps/NWP-H.bcmap +0 -0
- package/vendor/cmaps/NWP-V.bcmap +0 -0
- package/vendor/cmaps/RKSJ-H.bcmap +0 -0
- package/vendor/cmaps/RKSJ-V.bcmap +0 -0
- package/vendor/cmaps/Roman.bcmap +0 -0
- package/vendor/cmaps/UniCNS-UCS2-H.bcmap +0 -0
- package/vendor/cmaps/UniCNS-UCS2-V.bcmap +0 -0
- package/vendor/cmaps/UniCNS-UTF16-H.bcmap +0 -0
- package/vendor/cmaps/UniCNS-UTF16-V.bcmap +0 -0
- package/vendor/cmaps/UniCNS-UTF32-H.bcmap +0 -0
- package/vendor/cmaps/UniCNS-UTF32-V.bcmap +0 -0
- package/vendor/cmaps/UniCNS-UTF8-H.bcmap +0 -0
- package/vendor/cmaps/UniCNS-UTF8-V.bcmap +0 -0
- package/vendor/cmaps/UniGB-UCS2-H.bcmap +0 -0
- package/vendor/cmaps/UniGB-UCS2-V.bcmap +0 -0
- package/vendor/cmaps/UniGB-UTF16-H.bcmap +0 -0
- package/vendor/cmaps/UniGB-UTF16-V.bcmap +0 -0
- package/vendor/cmaps/UniGB-UTF32-H.bcmap +0 -0
- package/vendor/cmaps/UniGB-UTF32-V.bcmap +0 -0
- package/vendor/cmaps/UniGB-UTF8-H.bcmap +0 -0
- package/vendor/cmaps/UniGB-UTF8-V.bcmap +0 -0
- package/vendor/cmaps/UniJIS-UCS2-H.bcmap +0 -0
- package/vendor/cmaps/UniJIS-UCS2-HW-H.bcmap +0 -0
- package/vendor/cmaps/UniJIS-UCS2-HW-V.bcmap +0 -0
- package/vendor/cmaps/UniJIS-UCS2-V.bcmap +0 -0
- package/vendor/cmaps/UniJIS-UTF16-H.bcmap +0 -0
- package/vendor/cmaps/UniJIS-UTF16-V.bcmap +0 -0
- package/vendor/cmaps/UniJIS-UTF32-H.bcmap +0 -0
- package/vendor/cmaps/UniJIS-UTF32-V.bcmap +0 -0
- package/vendor/cmaps/UniJIS-UTF8-H.bcmap +0 -0
- package/vendor/cmaps/UniJIS-UTF8-V.bcmap +0 -0
- package/vendor/cmaps/UniJIS2004-UTF16-H.bcmap +0 -0
- package/vendor/cmaps/UniJIS2004-UTF16-V.bcmap +0 -0
- package/vendor/cmaps/UniJIS2004-UTF32-H.bcmap +0 -0
- package/vendor/cmaps/UniJIS2004-UTF32-V.bcmap +0 -0
- package/vendor/cmaps/UniJIS2004-UTF8-H.bcmap +0 -0
- package/vendor/cmaps/UniJIS2004-UTF8-V.bcmap +0 -0
- package/vendor/cmaps/UniJISPro-UCS2-HW-V.bcmap +0 -0
- package/vendor/cmaps/UniJISPro-UCS2-V.bcmap +0 -0
- package/vendor/cmaps/UniJISPro-UTF8-V.bcmap +0 -0
- package/vendor/cmaps/UniJISX0213-UTF32-H.bcmap +0 -0
- package/vendor/cmaps/UniJISX0213-UTF32-V.bcmap +0 -0
- package/vendor/cmaps/UniJISX02132004-UTF32-H.bcmap +0 -0
- package/vendor/cmaps/UniJISX02132004-UTF32-V.bcmap +0 -0
- package/vendor/cmaps/UniKS-UCS2-H.bcmap +0 -0
- package/vendor/cmaps/UniKS-UCS2-V.bcmap +0 -0
- package/vendor/cmaps/UniKS-UTF16-H.bcmap +0 -0
- package/vendor/cmaps/UniKS-UTF16-V.bcmap +0 -0
- package/vendor/cmaps/UniKS-UTF32-H.bcmap +0 -0
- package/vendor/cmaps/UniKS-UTF32-V.bcmap +0 -0
- package/vendor/cmaps/UniKS-UTF8-H.bcmap +0 -0
- package/vendor/cmaps/UniKS-UTF8-V.bcmap +0 -0
- package/vendor/cmaps/V.bcmap +0 -0
- package/vendor/cmaps/WP-Symbol.bcmap +0 -0
- package/vendor/fonts/excalifont/Excalifont-Regular-349fac6ca4700ffec595a7150a0d1e1d.woff2 +0 -0
- package/vendor/fonts/excalifont/Excalifont-Regular-3f2c5db56cc93c5a6873b1361d730c16.woff2 +0 -0
- package/vendor/fonts/excalifont/Excalifont-Regular-41b173a47b57366892116a575a43e2b6.woff2 +0 -0
- package/vendor/fonts/excalifont/Excalifont-Regular-623ccf21b21ef6b3a0d87738f77eb071.woff2 +0 -0
- package/vendor/fonts/excalifont/Excalifont-Regular-a88b72a24fb54c9f94e3b5fdaa7481c9.woff2 +0 -0
- package/vendor/fonts/excalifont/Excalifont-Regular-b9dcf9d2e50a1eaf42fc664b50a3fd0d.woff2 +0 -0
- package/vendor/fonts/excalifont/Excalifont-Regular-be310b9bcd4f1a43f571c46df7809174.woff2 +0 -0
- package/vendor/fonts/excalifont/OFL.txt +98 -0
- package/vendor/fonts/zhhand/OFL.txt +99 -0
- package/vendor/fonts/zhhand/Xiaolai-Regular.ttf +0 -0
- package/vendor/pdf.min.mjs +29 -0
- package/vendor/pdf.worker.min.mjs +29 -0
- package/vendor/pdfjs-LICENSE.txt +177 -0
- package/vendor/rough/LICENSE +21 -0
- package/vendor/rough/rough.mjs +1 -0
- package/vendor/wasm/LICENSE_JBIG2 +196 -0
- package/vendor/wasm/LICENSE_OPENJPEG +39 -0
- package/vendor/wasm/LICENSE_PDFJS_JBIG2 +13 -0
- package/vendor/wasm/LICENSE_PDFJS_OPENJPEG +22 -0
- package/vendor/wasm/LICENSE_PDFJS_QCMS +19 -0
- package/vendor/wasm/LICENSE_QCMS +21 -0
- package/vendor/wasm/jbig2.wasm +0 -0
- package/vendor/wasm/jbig2_nowasm_fallback.js +15 -0
- package/vendor/wasm/openjpeg.wasm +0 -0
- package/vendor/wasm/openjpeg_nowasm_fallback.js +17 -0
- package/vendor/wasm/qcms_bg.wasm +0 -0
- package/vendor/wasm/quickjs-eval.wasm +0 -0
- package//345/256/211/350/243/205/344/270/216/345/210/206/345/217/221.md +54 -0
|
@@ -0,0 +1,316 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Chapter translation as a Host-owned background job.
|
|
3
|
+
*
|
|
4
|
+
* The client starts it; the Host rasterizes the chapter PDF with PyMuPDF and
|
|
5
|
+
* calls the vision model page by page, so closing the reader or switching
|
|
6
|
+
* conversations no longer cancels the work.
|
|
7
|
+
* @module dsh-pdf-workbench/translate-job
|
|
8
|
+
*/
|
|
9
|
+
import { existsSync } from 'node:fs'
|
|
10
|
+
import { execFile } from 'node:child_process'
|
|
11
|
+
import { mkdir, mkdtemp, readFile, readdir, rm, writeFile } from 'node:fs/promises'
|
|
12
|
+
import { tmpdir } from 'node:os'
|
|
13
|
+
import { basename, join } from 'node:path'
|
|
14
|
+
import { renderMarkdown } from './render.js'
|
|
15
|
+
import { translatePage } from './translate.js'
|
|
16
|
+
import { translatePageBlocks } from './translate-blocks.js'
|
|
17
|
+
|
|
18
|
+
const PYTHONS = [process.env.MATH_DESK_PYTHON, 'python', 'py'].filter(Boolean)
|
|
19
|
+
const CONCURRENCY = 3
|
|
20
|
+
|
|
21
|
+
function rasterize(script, source, outDir) {
|
|
22
|
+
return new Promise((resolve, reject) => {
|
|
23
|
+
let last = 'no Python found'
|
|
24
|
+
const tryNext = index => {
|
|
25
|
+
if (index >= PYTHONS.length) {
|
|
26
|
+
reject(new Error(last))
|
|
27
|
+
return
|
|
28
|
+
}
|
|
29
|
+
const python = PYTHONS[index]
|
|
30
|
+
execFile(python, [script, source, outDir, '140'], { timeout: 180000, windowsHide: true, stdio: 'ignore' }, error => {
|
|
31
|
+
if (error === null) resolve()
|
|
32
|
+
else {
|
|
33
|
+
last = `${python}: ${error.message}`
|
|
34
|
+
tryNext(index + 1)
|
|
35
|
+
}
|
|
36
|
+
})
|
|
37
|
+
}
|
|
38
|
+
tryNext(0)
|
|
39
|
+
})
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function capture(script, args, env) {
|
|
43
|
+
return new Promise((resolve, reject) => {
|
|
44
|
+
const tryNext = index => {
|
|
45
|
+
if (index >= PYTHONS.length) {
|
|
46
|
+
reject(new Error('没有可用的 Python'))
|
|
47
|
+
return
|
|
48
|
+
}
|
|
49
|
+
execFile(
|
|
50
|
+
PYTHONS[index],
|
|
51
|
+
[script, ...args],
|
|
52
|
+
{ timeout: 180000, windowsHide: true, maxBuffer: 8 * 1024 * 1024, env: { ...process.env, ...(env ?? {}) } },
|
|
53
|
+
(error, stdout) => {
|
|
54
|
+
if (error === null) resolve(stdout)
|
|
55
|
+
else tryNext(index + 1)
|
|
56
|
+
}
|
|
57
|
+
)
|
|
58
|
+
}
|
|
59
|
+
tryNext(0)
|
|
60
|
+
})
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** Body font class for the chapter's own point size. */
|
|
64
|
+
function fontClassOf(fontPt) {
|
|
65
|
+
if (fontPt >= 12.5) return '12pt'
|
|
66
|
+
if (fontPt >= 11.2) return '11pt'
|
|
67
|
+
if (fontPt >= 10) return '10pt'
|
|
68
|
+
return '9pt'
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Start a chapter translation; resolves nothing, writes progress to `jobPath`.
|
|
73
|
+
* @param {{ rasterScript: string, md2texScript: string, apiKey: string, source: string, outPath: string, jobPath: string, target: 'zh'|'en', book: string, code: string }} options - job inputs.
|
|
74
|
+
* @returns {Promise<string>} the job id.
|
|
75
|
+
*/
|
|
76
|
+
export async function startChapterJob(options) {
|
|
77
|
+
const jobId = `tr-${Date.now().toString(36)}`
|
|
78
|
+
const state = {
|
|
79
|
+
id: jobId,
|
|
80
|
+
status: 'running',
|
|
81
|
+
book: options.book,
|
|
82
|
+
code: options.code,
|
|
83
|
+
target: options.target,
|
|
84
|
+
total: 0,
|
|
85
|
+
done: 0,
|
|
86
|
+
failed: 0,
|
|
87
|
+
failedPages: [],
|
|
88
|
+
pdf: '',
|
|
89
|
+
error: '',
|
|
90
|
+
startedAt: Date.now(),
|
|
91
|
+
}
|
|
92
|
+
await mkdir(options.jobDir, { recursive: true })
|
|
93
|
+
const jobPath = join(options.jobDir, `${jobId}.json`)
|
|
94
|
+
await writeFile(jobPath, JSON.stringify(state), 'utf8')
|
|
95
|
+
const save = () => writeFile(jobPath, JSON.stringify(state), 'utf8')
|
|
96
|
+
// Token accounting for the whole chapter: the API reports usage per call, so
|
|
97
|
+
// every page adds its share and the reader can see what a chapter cost.
|
|
98
|
+
const addUsage = usage => {
|
|
99
|
+
state.tokens = state.tokens ?? { input: 0, output: 0, total: 0 }
|
|
100
|
+
state.tokens.input += usage.input
|
|
101
|
+
state.tokens.output += usage.output
|
|
102
|
+
state.tokens.total += usage.total
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
void (async () => {
|
|
106
|
+
let temporary = null
|
|
107
|
+
let savedMarkdown = ''
|
|
108
|
+
try {
|
|
109
|
+
temporary = await mkdtemp(join(tmpdir(), 'pdfdesk-translate-'))
|
|
110
|
+
await rasterize(options.rasterScript, options.source, temporary)
|
|
111
|
+
const files = (await readdir(temporary)).filter(name => name.endsWith('.png')).sort()
|
|
112
|
+
state.total = files.length
|
|
113
|
+
await save()
|
|
114
|
+
const chunks = new Array(files.length)
|
|
115
|
+
let next = 0
|
|
116
|
+
const worker = async () => {
|
|
117
|
+
while (next < files.length) {
|
|
118
|
+
if (existsSync(join(options.jobDir, `${jobId}.cancel`))) {
|
|
119
|
+
throw new Error('翻译已取消')
|
|
120
|
+
}
|
|
121
|
+
const index = next
|
|
122
|
+
next += 1
|
|
123
|
+
// Resume: a page whose markdown is already cached costs nothing, so a
|
|
124
|
+
// chapter interrupted by a crash or a failed page finishes where it
|
|
125
|
+
// stopped instead of re-translating everything.
|
|
126
|
+
if (options.resume === true && typeof options.cacheDir === 'string') {
|
|
127
|
+
try {
|
|
128
|
+
const cached = await readFile(join(options.cacheDir, `page-${String(index + 1).padStart(3, '0')}.md`), 'utf8')
|
|
129
|
+
if (cached.trim() !== '') {
|
|
130
|
+
chunks[index] = cached
|
|
131
|
+
state.done += 1
|
|
132
|
+
await save()
|
|
133
|
+
continue
|
|
134
|
+
}
|
|
135
|
+
} catch {
|
|
136
|
+
// no cached page: translate it below
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
const image = `data:image/png;base64,${(await readFile(join(temporary, files[index]))).toString('base64')}`
|
|
140
|
+
let pageMarkdown = ''
|
|
141
|
+
let blockError = ''
|
|
142
|
+
if (typeof options.cacheDir === 'string' && typeof options.toolsDir === 'string') {
|
|
143
|
+
try {
|
|
144
|
+
const raw = await capture(join(options.toolsDir, 'pdf_blocks.py'), [options.source, String(index + 1)])
|
|
145
|
+
const blocks = JSON.parse(raw)
|
|
146
|
+
if (Array.isArray(blocks) && blocks.length > 0) {
|
|
147
|
+
const translated = await translatePageBlocks({
|
|
148
|
+
apiKey: options.apiKey,
|
|
149
|
+
image,
|
|
150
|
+
blocks,
|
|
151
|
+
target: options.target,
|
|
152
|
+
onUsage: addUsage,
|
|
153
|
+
})
|
|
154
|
+
const name = `page-${String(index + 1).padStart(3, '0')}.blocks.json`
|
|
155
|
+
pageMarkdown = translated.map(block => block.md).filter(part => part !== '').join('\n\n')
|
|
156
|
+
if (pageMarkdown.trim() !== '') {
|
|
157
|
+
await writeFile(join(options.cacheDir, name), JSON.stringify(translated), 'utf8').catch(() => {})
|
|
158
|
+
} else {
|
|
159
|
+
await rm(join(options.cacheDir, name), { force: true }).catch(() => {})
|
|
160
|
+
blockError = '模型没有返回任何块的译文'
|
|
161
|
+
}
|
|
162
|
+
} else {
|
|
163
|
+
blockError = '这一页没有解析出文本块'
|
|
164
|
+
}
|
|
165
|
+
} catch (error) {
|
|
166
|
+
// Falling back to whole-page translation is not silent any more:
|
|
167
|
+
// the chapter still translates, but the overlay can only be a
|
|
168
|
+
// reflow, so the job says which page lost block alignment.
|
|
169
|
+
blockError = String(error !== null && error !== undefined && error.message ? error.message : error)
|
|
170
|
+
pageMarkdown = ''
|
|
171
|
+
}
|
|
172
|
+
if (blockError !== '') {
|
|
173
|
+
state.blockErrors = Array.from(new Set([...(state.blockErrors ?? []), `第 ${index + 1} 页:${blockError}`]))
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
if (pageMarkdown.trim() === '') {
|
|
177
|
+
try {
|
|
178
|
+
pageMarkdown = await translatePage({ apiKey: options.apiKey, image, target: options.target, onUsage: addUsage })
|
|
179
|
+
} catch {
|
|
180
|
+
// One page failing must not throw away the pages that worked: keep
|
|
181
|
+
// the page empty, remember it, and let the chapter finish.
|
|
182
|
+
pageMarkdown = ''
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
if (pageMarkdown.trim() === '') {
|
|
186
|
+
state.failed = (state.failed ?? 0) + 1
|
|
187
|
+
state.failedPages = Array.from(new Set([...(state.failedPages ?? []), index + 1]))
|
|
188
|
+
}
|
|
189
|
+
chunks[index] = pageMarkdown
|
|
190
|
+
state.done += 1
|
|
191
|
+
await save()
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
await Promise.all(Array.from({ length: Math.min(CONCURRENCY, files.length) }, () => worker()))
|
|
195
|
+
if (chunks.every(chunk => chunk.trim() === '')) {
|
|
196
|
+
throw new Error('所有页面都没有返回译文,请检查 API Key 与网络')
|
|
197
|
+
}
|
|
198
|
+
if (typeof options.cacheDir === 'string' && options.cacheDir !== '') {
|
|
199
|
+
await mkdir(options.cacheDir, { recursive: true })
|
|
200
|
+
for (let index = 0; index < chunks.length; index += 1) {
|
|
201
|
+
// A failed page keeps whatever it had before instead of being
|
|
202
|
+
// overwritten with an empty file.
|
|
203
|
+
if (chunks[index].trim() === '') continue
|
|
204
|
+
const name = `page-${String(index + 1).padStart(3, '0')}.md`
|
|
205
|
+
await writeFile(join(options.cacheDir, name), chunks[index], 'utf8').catch(() => {})
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
const markdown = chunks.join('\n\n\\newpage\n\n')
|
|
209
|
+
savedMarkdown = markdown
|
|
210
|
+
if (typeof options.overlayPath === 'string' && typeof options.toolsDir === 'string') {
|
|
211
|
+
try {
|
|
212
|
+
const sizes = JSON.parse(await capture(join(options.toolsDir, 'pdf_pagesizes.py'), [options.source]))
|
|
213
|
+
let fontPt = 0
|
|
214
|
+
try {
|
|
215
|
+
fontPt = Number(JSON.parse(await capture(join(options.toolsDir, 'pdf_fontsize.py'), [options.source]))) || 0
|
|
216
|
+
} catch {
|
|
217
|
+
fontPt = 0
|
|
218
|
+
}
|
|
219
|
+
const fontClass = fontPt >= 12.5 ? '12pt' : fontPt >= 11.2 ? '11pt' : fontPt >= 10 ? '10pt' : '9pt'
|
|
220
|
+
const parts = []
|
|
221
|
+
const reflowPages = []
|
|
222
|
+
for (let index = 0; index < chunks.length; index += 1) {
|
|
223
|
+
const size = sizes[index] ?? sizes[sizes.length - 1] ?? [595, 842]
|
|
224
|
+
const miniMd = join(tmpdir(), `md-${jobId}-${index}.md`)
|
|
225
|
+
const miniPdf = join(tmpdir(), `pg-${jobId}-${index}.pdf`)
|
|
226
|
+
let positioned = false
|
|
227
|
+
if (typeof options.cacheDir === 'string' && options.cacheDir !== '') {
|
|
228
|
+
try {
|
|
229
|
+
const blocksFile = join(options.cacheDir, `page-${String(index + 1).padStart(3, '0')}.blocks.json`)
|
|
230
|
+
await capture(join(options.toolsDir, 'md2overlay.py'), [
|
|
231
|
+
blocksFile,
|
|
232
|
+
miniPdf,
|
|
233
|
+
String(size[0]),
|
|
234
|
+
String(size[1]),
|
|
235
|
+
fontClass,
|
|
236
|
+
])
|
|
237
|
+
positioned = true
|
|
238
|
+
} catch {
|
|
239
|
+
positioned = false
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
if (!positioned) {
|
|
243
|
+
reflowPages.push(index + 1)
|
|
244
|
+
await writeFile(miniMd, chunks[index], 'utf8')
|
|
245
|
+
await renderMarkdown(options.md2texScript, miniMd, miniPdf, {
|
|
246
|
+
MATH_DESK_NO_TITLE: '1',
|
|
247
|
+
MATH_DESK_PAPER: `${size[0] / 72},${size[1] / 72}`,
|
|
248
|
+
MATH_DESK_FONTSIZE: fontClass,
|
|
249
|
+
})
|
|
250
|
+
}
|
|
251
|
+
parts.push(miniPdf)
|
|
252
|
+
}
|
|
253
|
+
await capture(join(options.toolsDir, 'pdf_merge.py'), [options.overlayPath, ...parts])
|
|
254
|
+
state.overlay = basename(options.overlayPath)
|
|
255
|
+
// The overlay is only positionally aligned when every page came from
|
|
256
|
+
// its block boxes. Say so instead of letting the boxes look broken.
|
|
257
|
+
if (reflowPages.length > 0) state.overlayReflowPages = reflowPages
|
|
258
|
+
} catch {
|
|
259
|
+
// The overlay PDF is optional; the natural reading PDF still exists.
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
const title = basename(options.outPath, '.pdf').replace(/_translated$/i, '')
|
|
263
|
+
// The reading PDF also uses the chapter's own paper size and body size, so
|
|
264
|
+
// the bilingual columns read at the same scale on both sides.
|
|
265
|
+
let paper = ''
|
|
266
|
+
let fontClass = ''
|
|
267
|
+
if (typeof options.toolsDir === 'string') {
|
|
268
|
+
try {
|
|
269
|
+
const sizes = JSON.parse(await capture(join(options.toolsDir, 'pdf_pagesizes.py'), [options.source]))
|
|
270
|
+
const size = Array.isArray(sizes) ? sizes[0] : null
|
|
271
|
+
if (Array.isArray(size) && size.length === 2) paper = `${size[0] / 72},${size[1] / 72}`
|
|
272
|
+
} catch {
|
|
273
|
+
paper = ''
|
|
274
|
+
}
|
|
275
|
+
try {
|
|
276
|
+
fontClass = fontClassOf(Number(JSON.parse(await capture(join(options.toolsDir, 'pdf_fontsize.py'), [options.source]))) || 0)
|
|
277
|
+
} catch {
|
|
278
|
+
fontClass = ''
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
const markdownPath = join(tmpdir(), `${title}.md`)
|
|
282
|
+
await writeFile(markdownPath, markdown, 'utf8')
|
|
283
|
+
try {
|
|
284
|
+
await renderMarkdown(
|
|
285
|
+
options.md2texScript,
|
|
286
|
+
markdownPath,
|
|
287
|
+
options.outPath,
|
|
288
|
+
{
|
|
289
|
+
// The translated pages keep the book's headings; a generated one
|
|
290
|
+
// would print the chapter's file name on top of page 1.
|
|
291
|
+
MATH_DESK_NO_TITLE: '1',
|
|
292
|
+
...(paper === '' ? {} : { MATH_DESK_PAPER: paper }),
|
|
293
|
+
...(fontClass === '' ? {} : { MATH_DESK_FONTSIZE: fontClass }),
|
|
294
|
+
}
|
|
295
|
+
)
|
|
296
|
+
} finally {
|
|
297
|
+
await rm(markdownPath, { force: true })
|
|
298
|
+
}
|
|
299
|
+
state.status = 'done'
|
|
300
|
+
state.pdf = basename(options.outPath)
|
|
301
|
+
} catch (error) {
|
|
302
|
+
state.status = 'error'
|
|
303
|
+
state.error = String(error !== null && error !== undefined && error.message ? error.message : error)
|
|
304
|
+
if (savedMarkdown !== '') {
|
|
305
|
+
const keep = join(options.jobDir, `${jobId}.md`)
|
|
306
|
+
await writeFile(keep, savedMarkdown, 'utf8').catch(() => {})
|
|
307
|
+
state.error = `${state.error}(译文已保留:${keep})`
|
|
308
|
+
}
|
|
309
|
+
} finally {
|
|
310
|
+
await save()
|
|
311
|
+
if (temporary !== null) await rm(temporary, { recursive: true, force: true })
|
|
312
|
+
}
|
|
313
|
+
})()
|
|
314
|
+
|
|
315
|
+
return jobId
|
|
316
|
+
}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Translate one rendered PDF page with the DeepSeek vision API.
|
|
3
|
+
*
|
|
4
|
+
* The page is sent as a JPEG data URL; the model returns Markdown with LaTeX
|
|
5
|
+
* formulas preserved. The caller renders that Markdown with `md2tex.py`.
|
|
6
|
+
* @module dsh-pdf-workbench/translate
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
const API_URL = 'https://api.deepseek.com/chat/completions'
|
|
10
|
+
const MODEL = 'deepseek-flash'
|
|
11
|
+
|
|
12
|
+
function promptFor(target) {
|
|
13
|
+
const direction = target === 'zh' ? 'Simplified Chinese' : 'English'
|
|
14
|
+
return [
|
|
15
|
+
'You are a mathematics textbook translator.',
|
|
16
|
+
`Translate the readable text of this page into ${direction}.`,
|
|
17
|
+
'Preserve every mathematical formula exactly, using LaTeX: inline formulas as $...$, display formulas as $$...$$.',
|
|
18
|
+
'Do not translate variable names, theorem numbers, or formula content; only translate natural language.',
|
|
19
|
+
'Keep the document structure: headings, paragraphs, lists, theorem/lemma labels.',
|
|
20
|
+
'Return Markdown only, with no commentary and no code fences.',
|
|
21
|
+
].join(' ')
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Normalize the model output so XeLaTeX can compile it: balanced display
|
|
26
|
+
* math, LaTeX bracket forms converted, and an unclosed `$$` closed.
|
|
27
|
+
* @param {string} markdown - raw model output.
|
|
28
|
+
* @returns {string} compile-friendly Markdown.
|
|
29
|
+
*/
|
|
30
|
+
function balanceMath(markdown) {
|
|
31
|
+
let text = markdown
|
|
32
|
+
.replace(/\\\[/g, '$$')
|
|
33
|
+
.replace(/\\\]/g, '$$')
|
|
34
|
+
.replace(/\\\(/g, '$')
|
|
35
|
+
.replace(/\\\)/g, '$')
|
|
36
|
+
// Split on display delimiters: odd chunks are inside `$$...$$`, so their
|
|
37
|
+
// inline `$` must go. An unmatched trailing display block is cleaned too and
|
|
38
|
+
// then closed.
|
|
39
|
+
const parts = text.split('$$')
|
|
40
|
+
text = parts
|
|
41
|
+
.map((part, index) => (index % 2 === 1 ? part.replace(/(?<!\\)\$/g, '') : part))
|
|
42
|
+
.join('$$')
|
|
43
|
+
if (parts.length % 2 === 0) text = `${text}\n\n$$`
|
|
44
|
+
return text
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* @param {{ apiKey: string, image: string, target: 'zh'|'en', signal?: AbortSignal }} options - key, JPEG data URL, target language.
|
|
49
|
+
* @returns {Promise<string>} Markdown produced by the model.
|
|
50
|
+
*/
|
|
51
|
+
export async function translatePage(options) {
|
|
52
|
+
if (typeof options.apiKey !== 'string' || options.apiKey === '') {
|
|
53
|
+
throw new Error('缺少 DeepSeek API Key:请在插件配置里设置 deepseekApiKey,或设置环境变量 DEEPSEEK_API_KEY')
|
|
54
|
+
}
|
|
55
|
+
const response = await fetch(API_URL, {
|
|
56
|
+
method: 'POST',
|
|
57
|
+
headers: {
|
|
58
|
+
'Content-Type': 'application/json',
|
|
59
|
+
Authorization: `Bearer ${options.apiKey}`,
|
|
60
|
+
},
|
|
61
|
+
body: JSON.stringify({
|
|
62
|
+
model: MODEL,
|
|
63
|
+
temperature: 0.2,
|
|
64
|
+
messages: [
|
|
65
|
+
{
|
|
66
|
+
role: 'user',
|
|
67
|
+
content: [
|
|
68
|
+
{ type: 'text', text: promptFor(options.target) },
|
|
69
|
+
{ type: 'image_url', image_url: { url: options.image } },
|
|
70
|
+
],
|
|
71
|
+
},
|
|
72
|
+
],
|
|
73
|
+
}),
|
|
74
|
+
signal: options.signal,
|
|
75
|
+
})
|
|
76
|
+
if (!response.ok) {
|
|
77
|
+
const detail = await response.text().catch(() => '')
|
|
78
|
+
throw new Error(`DeepSeek API ${response.status}: ${detail.slice(0, 300)}`)
|
|
79
|
+
}
|
|
80
|
+
const data = await response.json()
|
|
81
|
+
if (typeof options.onUsage === 'function' && data !== null && typeof data === 'object' && data.usage) {
|
|
82
|
+
const usage = data.usage
|
|
83
|
+
options.onUsage({
|
|
84
|
+
input: Number(usage.prompt_tokens) || 0,
|
|
85
|
+
output: Number(usage.completion_tokens) || 0,
|
|
86
|
+
total: Number(usage.total_tokens) || 0,
|
|
87
|
+
})
|
|
88
|
+
}
|
|
89
|
+
const content = data?.choices?.[0]?.message?.content
|
|
90
|
+
if (typeof content !== 'string' || content.trim() === '') {
|
|
91
|
+
throw new Error('DeepSeek API 返回了空内容')
|
|
92
|
+
}
|
|
93
|
+
return balanceMath(content.trim())
|
|
94
|
+
}
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Ask GitHub Releases for the newest tag, from the Host.
|
|
3
|
+
*
|
|
4
|
+
* The browser's fetch could not reach GitHub on a proxied desktop (the Electron
|
|
5
|
+
* renderer does not follow the system proxy, and direct access is blocked in
|
|
6
|
+
* some networks), so the Host does the request and tries the usual local proxy
|
|
7
|
+
* ports before falling back to a direct connection. Loopback-only route, no
|
|
8
|
+
* credentials, one GET per click.
|
|
9
|
+
*
|
|
10
|
+
* @module dsh-pdf-workbench/update
|
|
11
|
+
*/
|
|
12
|
+
import http from 'node:http'
|
|
13
|
+
import https from 'node:https'
|
|
14
|
+
import tls from 'node:tls'
|
|
15
|
+
|
|
16
|
+
/** Repository whose Releases are compared. */
|
|
17
|
+
export const REPO = 'minggemofashi/dsh-pdf-workbench'
|
|
18
|
+
|
|
19
|
+
/** Where the release page lives, for the "new version" notice. */
|
|
20
|
+
export const RELEASES_URL = `https://github.com/${REPO}/releases`
|
|
21
|
+
|
|
22
|
+
const API_PATH = `/repos/${REPO}/releases/latest`
|
|
23
|
+
const API_HOST = 'api.github.com'
|
|
24
|
+
const TIMEOUT = 8000
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Local proxy ports worth trying when nothing is configured.
|
|
28
|
+
* @returns {string[]} candidate proxy URLs.
|
|
29
|
+
*/
|
|
30
|
+
function proxyCandidates(configured) {
|
|
31
|
+
const list = []
|
|
32
|
+
if (typeof configured === 'string' && configured.trim() !== '') list.push(configured.trim())
|
|
33
|
+
for (const key of ['HTTPS_PROXY', 'https_proxy', 'HTTP_PROXY', 'http_proxy']) {
|
|
34
|
+
if (typeof process.env[key] === 'string' && process.env[key].trim() !== '') list.push(process.env[key].trim())
|
|
35
|
+
}
|
|
36
|
+
list.push('http://127.0.0.1:7890', 'http://127.0.0.1:7897', 'http://127.0.0.1:10809')
|
|
37
|
+
return [...new Set(list)]
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* One HTTPS GET, optionally through a CONNECT proxy.
|
|
42
|
+
* @param {string} url - absolute URL.
|
|
43
|
+
* @param {string | null} proxy - proxy URL, or null for a direct request.
|
|
44
|
+
* @returns {Promise<{ status: number, body: Buffer }>} the response.
|
|
45
|
+
*/
|
|
46
|
+
function requestOnce(url, proxy) {
|
|
47
|
+
return new Promise((resolve, reject) => {
|
|
48
|
+
const target = new URL(url)
|
|
49
|
+
const fail = error => reject(error)
|
|
50
|
+
const finish = (status, body) => resolve({ status, body })
|
|
51
|
+
if (proxy === null) {
|
|
52
|
+
const request = https.get({
|
|
53
|
+
host: target.hostname,
|
|
54
|
+
path: `${target.pathname}${target.search}`,
|
|
55
|
+
headers: { accept: 'application/vnd.github+json', 'user-agent': 'dsh-pdf-workbench' },
|
|
56
|
+
timeout: TIMEOUT,
|
|
57
|
+
}, response => {
|
|
58
|
+
const chunks = []
|
|
59
|
+
response.on('data', chunk => chunks.push(chunk))
|
|
60
|
+
response.on('end', () => finish(response.statusCode ?? 0, Buffer.concat(chunks)))
|
|
61
|
+
response.on('error', fail)
|
|
62
|
+
})
|
|
63
|
+
request.on('timeout', () => request.destroy(new Error('timeout')))
|
|
64
|
+
request.on('error', fail)
|
|
65
|
+
return
|
|
66
|
+
}
|
|
67
|
+
const proxyUrl = new URL(proxy)
|
|
68
|
+
const connect = http.request({
|
|
69
|
+
host: proxyUrl.hostname,
|
|
70
|
+
port: Number(proxyUrl.port || 80),
|
|
71
|
+
method: 'CONNECT',
|
|
72
|
+
path: `${target.hostname}:443`,
|
|
73
|
+
timeout: TIMEOUT,
|
|
74
|
+
})
|
|
75
|
+
connect.on('timeout', () => connect.destroy(new Error('proxy timeout')))
|
|
76
|
+
connect.on('error', fail)
|
|
77
|
+
connect.on('connect', (response, socket) => {
|
|
78
|
+
if (response.statusCode !== 200) {
|
|
79
|
+
fail(new Error(`proxy CONNECT ${response.statusCode}`))
|
|
80
|
+
return
|
|
81
|
+
}
|
|
82
|
+
const secure = tls.connect({ socket, servername: target.hostname }, () => {
|
|
83
|
+
secure.write(`GET ${target.pathname}${target.search} HTTP/1.1\r\n`
|
|
84
|
+
+ `Host: ${target.hostname}\r\n`
|
|
85
|
+
+ 'Accept: application/vnd.github+json\r\n'
|
|
86
|
+
+ 'User-Agent: dsh-pdf-workbench\r\n'
|
|
87
|
+
+ 'Accept-Encoding: identity\r\n'
|
|
88
|
+
+ 'Connection: close\r\n\r\n')
|
|
89
|
+
})
|
|
90
|
+
secure.on('error', fail)
|
|
91
|
+
const chunks = []
|
|
92
|
+
secure.on('data', chunk => chunks.push(chunk))
|
|
93
|
+
secure.on('end', () => {
|
|
94
|
+
const buffer = Buffer.concat(chunks)
|
|
95
|
+
const split = buffer.indexOf('\r\n\r\n')
|
|
96
|
+
if (split < 0) {
|
|
97
|
+
fail(new Error('bad proxy response'))
|
|
98
|
+
return
|
|
99
|
+
}
|
|
100
|
+
const statusLine = buffer.subarray(0, split).toString('latin1').split('\r\n')[0] ?? ''
|
|
101
|
+
const match = /\s(\d{3})\s/.exec(statusLine)
|
|
102
|
+
finish(match === null ? 0 : Number(match[1]), buffer.subarray(split + 4))
|
|
103
|
+
})
|
|
104
|
+
})
|
|
105
|
+
connect.end()
|
|
106
|
+
})
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Fetch the newest release tag.
|
|
111
|
+
* @param {string} [configuredProxy] - `config.updateProxy`, when set.
|
|
112
|
+
* @returns {Promise<{ ok: true, latest: string, url: string, via: string } | { ok: false, error: string }>} the answer.
|
|
113
|
+
*/
|
|
114
|
+
export async function checkForUpdate(configuredProxy) {
|
|
115
|
+
const url = `https://${API_HOST}${API_PATH}`
|
|
116
|
+
const attempts = [...proxyCandidates(configuredProxy).map(proxy => ({ proxy, via: proxy })), { proxy: null, via: 'direct' }]
|
|
117
|
+
let lastError = ''
|
|
118
|
+
for (const attempt of attempts) {
|
|
119
|
+
try {
|
|
120
|
+
const response = await requestOnce(url, attempt.proxy)
|
|
121
|
+
if (response.status === 404) return { ok: true, latest: '', url: RELEASES_URL, via: attempt.via }
|
|
122
|
+
if (response.status !== 200) {
|
|
123
|
+
lastError = `HTTP ${response.status}`
|
|
124
|
+
continue
|
|
125
|
+
}
|
|
126
|
+
const payload = JSON.parse(response.body.toString('utf8'))
|
|
127
|
+
const latest = typeof payload.tag_name === 'string' ? payload.tag_name : ''
|
|
128
|
+
return { ok: true, latest, url: RELEASES_URL, via: attempt.via }
|
|
129
|
+
} catch (error) {
|
|
130
|
+
lastError = error instanceof Error ? error.message : String(error)
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
return { ok: false, error: lastError }
|
|
134
|
+
}
|
package/tools/AGENTS.md
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
# 数学课本工作区
|
|
2
|
+
|
|
3
|
+
用户会用**平常的话**下任务,不需要记任何编号。按意图直接做:不要先复述流程,不要列选项让用户选。
|
|
4
|
+
|
|
5
|
+
## 按意图分发
|
|
6
|
+
|
|
7
|
+
| 用户说的话像这样 | 当作 | 动手前先读 |
|
|
8
|
+
| --- | --- | --- |
|
|
9
|
+
| 提取 / 原文 / 抄一下 / 给我这几页 / 转成 LaTeX | **提取原文** | `规范/01_文本提取.md` |
|
|
10
|
+
| 为什么 / 怎么回事 / 这步怎么来的 / 看不懂 / 依据在哪 | **答疑** | `规范/02_答疑.md` |
|
|
11
|
+
| 做一下 / 解一下 / 答案 / 对一下 / 习题 | **做习题** | `规范/03_习题解答.md` |
|
|
12
|
+
| 其它:整理书库、拆书、查工具、改规范 | 直接办 | 按需读 |
|
|
13
|
+
|
|
14
|
+
判断规则:
|
|
15
|
+
|
|
16
|
+
- 只给了书名 + 页码、没说干什么 → **默认提取原文**,末尾只问一句「要不要顺带讲讲这段」。
|
|
17
|
+
- 一句话同时像答疑和习题(「这题为什么这样做」)→ 先讲原理,再给答案。
|
|
18
|
+
- 真判断不了,直接问一句,**不要列 1/2/3/4 选项**。
|
|
19
|
+
- `01`/`02`/`03`/`04` 仍可用,但只是可选别名,不必记。
|
|
20
|
+
|
|
21
|
+
## 硬规则(任何任务都适用)
|
|
22
|
+
|
|
23
|
+
1. **找书**:`工具\py.cmd 工具\pdfkit.py lookup <关键词> --chapter 4`(按章,首选)或 `--page 121`(按页)。
|
|
24
|
+
**分片优先,分片没有才退回全本**。用户按章说事时用 `--chapter`,中文数字(四)也认;返回多本候选时补一个区分词(如「白皮书」)。
|
|
25
|
+
2. **页码是 PDF 物理页**(= 分片文件名里 `_97-138` 那种数字)。书上印刷页码可能差几页,交付时写 `物理 p.100(书页 92)`。
|
|
26
|
+
3. **公式**:行内 `$…$`;行间 `$$` 独占行。禁止 `\[` `\]` `\(` `\)`,也不用 `\begin{equation}` 之类环境。
|
|
27
|
+
4. 答疑和习题只在对话里输出,**不落文件**;提取输出**一个**代码块 + 三行以内说明。
|
|
28
|
+
5. 提取交付前必须跑 `pdfkit.py verify <文件> --tex`,拿到 `ok: true` 才算完。
|
|
29
|
+
6. 命令一律走 `工具\py.cmd`、`工具\node.cmd`(本机专用包装器,不进 git;别的机器用 `python`/`node` 直接跑)。
|
|
30
|
+
|
|
31
|
+
## 这里只是索引
|
|
32
|
+
|
|
33
|
+
上面够你起步。真要动手时读对应的 `规范/0X_*.md`——完整 SOP、故障对照表、成本与隐私都在那里。不要把规范全文抄进回答。
|