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,166 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Sessions created from the workbench but not yet used.
|
|
3
|
+
*
|
|
4
|
+
* A workbench session is named `[<book> cNN] <mode>` only when its first turn
|
|
5
|
+
* starts. Until then it stays an ordinary, untitled blank: reusing it from
|
|
6
|
+
* another surface cannot inherit a misleading title, and the workbench can
|
|
7
|
+
* archive it when it is still unused. This registry is the durable intent
|
|
8
|
+
* behind that delay.
|
|
9
|
+
*
|
|
10
|
+
* @module dsh-pdf-workbench/pending
|
|
11
|
+
*/
|
|
12
|
+
import { readFile, rename, mkdir, writeFile } from 'node:fs/promises'
|
|
13
|
+
import { dirname } from 'node:path'
|
|
14
|
+
|
|
15
|
+
/** File format version. */
|
|
16
|
+
const VERSION = 1
|
|
17
|
+
|
|
18
|
+
/** Fields kept per entry. */
|
|
19
|
+
const MAX_TITLE = 200
|
|
20
|
+
|
|
21
|
+
/** A session id: the only shape this store accepts. */
|
|
22
|
+
const SESSION_ID = /^[\w-]{1,80}$/
|
|
23
|
+
|
|
24
|
+
/** A chapter code; mirrors the catalog's canonical form. */
|
|
25
|
+
const CODE = /^c\d{2,3}$/
|
|
26
|
+
|
|
27
|
+
/** Modes the workbench creates. */
|
|
28
|
+
const MODES = new Set(['extract', 'qa', 'exercise'])
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Keep one entry if it is usable.
|
|
32
|
+
* @param {unknown} value - stored entry.
|
|
33
|
+
* @returns {{ title: string, bookId: string, code: string, mode: string, createdAt: number } | null} the entry.
|
|
34
|
+
*/
|
|
35
|
+
function sanitizeEntry(value) {
|
|
36
|
+
if (value === null || typeof value !== 'object') return null
|
|
37
|
+
const title = typeof value.title === 'string' ? value.title.trim() : ''
|
|
38
|
+
const bookId = typeof value.bookId === 'string' ? value.bookId : ''
|
|
39
|
+
const code = typeof value.code === 'string' ? value.code : ''
|
|
40
|
+
const mode = typeof value.mode === 'string' ? value.mode : ''
|
|
41
|
+
if (title === '' || title.length > MAX_TITLE) return null
|
|
42
|
+
if (bookId === '' || bookId.length > 200) return null
|
|
43
|
+
if (!CODE.test(code)) return null
|
|
44
|
+
if (!MODES.has(mode)) return null
|
|
45
|
+
const createdAt = typeof value.createdAt === 'number' && Number.isFinite(value.createdAt) ? value.createdAt : 0
|
|
46
|
+
return { title, bookId, code, mode, createdAt }
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Read the pending registry; a damaged file reads as empty.
|
|
51
|
+
* @param {string} file - absolute path of `pending.json`.
|
|
52
|
+
* @returns {Promise<Record<string, object>>} entries by session id.
|
|
53
|
+
*/
|
|
54
|
+
async function readFileEntries(file) {
|
|
55
|
+
let text
|
|
56
|
+
try {
|
|
57
|
+
text = await readFile(file, 'utf8')
|
|
58
|
+
} catch {
|
|
59
|
+
return {}
|
|
60
|
+
}
|
|
61
|
+
let parsed
|
|
62
|
+
try {
|
|
63
|
+
parsed = JSON.parse(text)
|
|
64
|
+
} catch {
|
|
65
|
+
return {}
|
|
66
|
+
}
|
|
67
|
+
const raw = parsed !== null && typeof parsed === 'object' && parsed.entries !== null && typeof parsed.entries === 'object'
|
|
68
|
+
? parsed.entries
|
|
69
|
+
: {}
|
|
70
|
+
const entries = {}
|
|
71
|
+
for (const [sessionId, value] of Object.entries(raw)) {
|
|
72
|
+
if (!SESSION_ID.test(sessionId)) continue
|
|
73
|
+
const entry = sanitizeEntry(value)
|
|
74
|
+
if (entry !== null) entries[sessionId] = entry
|
|
75
|
+
}
|
|
76
|
+
return entries
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Durable registry of created-but-unused workbench sessions.
|
|
81
|
+
*/
|
|
82
|
+
export class PendingStore {
|
|
83
|
+
/**
|
|
84
|
+
* @param {string} file - absolute path of `pending.json`.
|
|
85
|
+
*/
|
|
86
|
+
constructor(file) {
|
|
87
|
+
this.file = file
|
|
88
|
+
this.entries = {}
|
|
89
|
+
this.ready = this.load()
|
|
90
|
+
this.queue = Promise.resolve()
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** Load once; later failures read as empty. */
|
|
94
|
+
async load() {
|
|
95
|
+
this.entries = await readFileEntries(this.file)
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Snapshot for one bootstrap response.
|
|
100
|
+
* @returns {Promise<Record<string, object>>} entries by session id.
|
|
101
|
+
*/
|
|
102
|
+
async list() {
|
|
103
|
+
await this.ready
|
|
104
|
+
return { ...this.entries }
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Record one created session. The write is serialized with removals.
|
|
109
|
+
* @param {string} sessionId - the session.
|
|
110
|
+
* @param {object} value - title/bookId/code/mode.
|
|
111
|
+
* @returns {Promise<void>} completion after durability.
|
|
112
|
+
*/
|
|
113
|
+
async put(sessionId, value) {
|
|
114
|
+
if (!SESSION_ID.test(sessionId)) throw new Error('sessionId 不合法')
|
|
115
|
+
const entry = sanitizeEntry({ ...value, createdAt: Date.now() })
|
|
116
|
+
if (entry === null) throw new Error('pending 记录不完整')
|
|
117
|
+
await this.ready
|
|
118
|
+
this.entries = { ...this.entries, [sessionId]: entry }
|
|
119
|
+
this.queue = this.queue.then(() => this.flush()).catch(() => {})
|
|
120
|
+
await this.queue
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Read one entry without removing it.
|
|
125
|
+
* @param {string} sessionId - the session.
|
|
126
|
+
* @returns {Promise<object | null>} the entry.
|
|
127
|
+
*/
|
|
128
|
+
async get(sessionId) {
|
|
129
|
+
await this.ready
|
|
130
|
+
return this.entries[sessionId] ?? null
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Remove one session, returning the entry when it was pending.
|
|
135
|
+
* @param {string} sessionId - the session.
|
|
136
|
+
* @returns {Promise<object | null>} the removed entry.
|
|
137
|
+
*/
|
|
138
|
+
async take(sessionId) {
|
|
139
|
+
await this.ready
|
|
140
|
+
const entry = this.entries[sessionId]
|
|
141
|
+
if (entry === undefined) return null
|
|
142
|
+
const next = { ...this.entries }
|
|
143
|
+
delete next[sessionId]
|
|
144
|
+
this.entries = next
|
|
145
|
+
this.queue = this.queue.then(() => this.flush()).catch(() => {})
|
|
146
|
+
await this.queue
|
|
147
|
+
return entry
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Remove one session without needing the entry.
|
|
152
|
+
* @param {string} sessionId - the session.
|
|
153
|
+
* @returns {Promise<void>} completion.
|
|
154
|
+
*/
|
|
155
|
+
async remove(sessionId) {
|
|
156
|
+
await this.take(sessionId)
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/** Persist atomically. */
|
|
160
|
+
async flush() {
|
|
161
|
+
await mkdir(dirname(this.file), { recursive: true })
|
|
162
|
+
const temporary = `${this.file}.${process.pid}.tmp`
|
|
163
|
+
await writeFile(temporary, `${JSON.stringify({ version: VERSION, entries: this.entries }, null, 2)}\n`, 'utf8')
|
|
164
|
+
await rename(temporary, this.file)
|
|
165
|
+
}
|
|
166
|
+
}
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The last context pressure seen for each session.
|
|
3
|
+
*
|
|
4
|
+
* "New" from a chapter should continue that chapter's conversation instead of
|
|
5
|
+
* leaving a new one behind on every click; it must start a continuation once the
|
|
6
|
+
* conversation is nearly full. The live answer comes from `tokenMeter`, which
|
|
7
|
+
* needs a live Session object — so the Host records the pressure it sees at the
|
|
8
|
+
* end of every turn here, and a session that is no longer live still answers
|
|
9
|
+
* from this snapshot.
|
|
10
|
+
*
|
|
11
|
+
* @module dsh-pdf-workbench/pressure
|
|
12
|
+
*/
|
|
13
|
+
import { mkdir, readFile, rename, writeFile } from 'node:fs/promises'
|
|
14
|
+
import { dirname } from 'node:path'
|
|
15
|
+
|
|
16
|
+
/** File format version. */
|
|
17
|
+
const VERSION = 1
|
|
18
|
+
|
|
19
|
+
/** Session ids kept at most; older entries are dropped on write. */
|
|
20
|
+
const MAX_ENTRIES = 4000
|
|
21
|
+
|
|
22
|
+
/** A session id: the only shape this store accepts. */
|
|
23
|
+
const SESSION_ID = /^[\w-]{1,80}$/
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Keep one measurement if it is usable.
|
|
27
|
+
* @param {unknown} value - stored measurement.
|
|
28
|
+
* @returns {{ tokens: number, window: number, at: number } | null} the entry.
|
|
29
|
+
*/
|
|
30
|
+
function sanitize(value) {
|
|
31
|
+
if (value === null || typeof value !== 'object') return null
|
|
32
|
+
const tokens = typeof value.tokens === 'number' && Number.isFinite(value.tokens) && value.tokens >= 0 ? value.tokens : -1
|
|
33
|
+
const window = typeof value.window === 'number' && Number.isFinite(value.window) && value.window > 0 ? value.window : 0
|
|
34
|
+
if (tokens < 0 || window === 0) return null
|
|
35
|
+
const at = typeof value.at === 'number' && Number.isFinite(value.at) ? value.at : Date.now()
|
|
36
|
+
return { tokens, window, at }
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Durable per-session context pressure snapshots.
|
|
41
|
+
*/
|
|
42
|
+
export class PressureStore {
|
|
43
|
+
/**
|
|
44
|
+
* @param {string} file - absolute path of `pressure.json`.
|
|
45
|
+
*/
|
|
46
|
+
constructor(file) {
|
|
47
|
+
this.file = file
|
|
48
|
+
this.entries = {}
|
|
49
|
+
this.ready = this.load()
|
|
50
|
+
this.queue = Promise.resolve()
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** Load once; a damaged file reads as empty. */
|
|
54
|
+
async load() {
|
|
55
|
+
let text
|
|
56
|
+
try {
|
|
57
|
+
text = await readFile(this.file, 'utf8')
|
|
58
|
+
} catch {
|
|
59
|
+
return
|
|
60
|
+
}
|
|
61
|
+
let parsed
|
|
62
|
+
try {
|
|
63
|
+
parsed = JSON.parse(text)
|
|
64
|
+
} catch {
|
|
65
|
+
return
|
|
66
|
+
}
|
|
67
|
+
const raw = parsed !== null && typeof parsed === 'object' && parsed.entries !== null && typeof parsed.entries === 'object'
|
|
68
|
+
? parsed.entries
|
|
69
|
+
: {}
|
|
70
|
+
const entries = {}
|
|
71
|
+
for (const [sessionId, value] of Object.entries(raw)) {
|
|
72
|
+
if (!SESSION_ID.test(sessionId)) continue
|
|
73
|
+
const entry = sanitize(value)
|
|
74
|
+
if (entry !== null) entries[sessionId] = entry
|
|
75
|
+
}
|
|
76
|
+
this.entries = entries
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* One session's last snapshot.
|
|
81
|
+
* @param {string} sessionId - the session.
|
|
82
|
+
* @returns {Promise<{ tokens: number, window: number, at: number } | null>} the entry.
|
|
83
|
+
*/
|
|
84
|
+
async get(sessionId) {
|
|
85
|
+
await this.ready
|
|
86
|
+
return this.entries[sessionId] ?? null
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Record one measurement. Writes are serialized.
|
|
91
|
+
* @param {string} sessionId - the session.
|
|
92
|
+
* @param {{ tokens: number, window: number }} value - the measurement.
|
|
93
|
+
* @returns {Promise<void>} completion after durability.
|
|
94
|
+
*/
|
|
95
|
+
async put(sessionId, value) {
|
|
96
|
+
if (!SESSION_ID.test(sessionId)) return
|
|
97
|
+
const entry = sanitize({ ...value, at: Date.now() })
|
|
98
|
+
if (entry === null) return
|
|
99
|
+
await this.ready
|
|
100
|
+
this.entries = { ...this.entries, [sessionId]: entry }
|
|
101
|
+
const keys = Object.keys(this.entries)
|
|
102
|
+
if (keys.length > MAX_ENTRIES) {
|
|
103
|
+
// Drop the oldest snapshots rather than growing without bound.
|
|
104
|
+
const trimmed = {}
|
|
105
|
+
for (const key of keys.sort((left, right) => this.entries[right].at - this.entries[left].at).slice(0, MAX_ENTRIES)) {
|
|
106
|
+
trimmed[key] = this.entries[key]
|
|
107
|
+
}
|
|
108
|
+
this.entries = trimmed
|
|
109
|
+
}
|
|
110
|
+
this.queue = this.queue.then(() => this.flush()).catch(() => {})
|
|
111
|
+
await this.queue
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Forget one session.
|
|
116
|
+
* @param {string} sessionId - the session.
|
|
117
|
+
* @returns {Promise<void>} completion.
|
|
118
|
+
*/
|
|
119
|
+
async forget(sessionId) {
|
|
120
|
+
if (!SESSION_ID.test(sessionId)) return
|
|
121
|
+
await this.ready
|
|
122
|
+
if (this.entries[sessionId] === undefined) return
|
|
123
|
+
const next = { ...this.entries }
|
|
124
|
+
delete next[sessionId]
|
|
125
|
+
this.entries = next
|
|
126
|
+
this.queue = this.queue.then(() => this.flush()).catch(() => {})
|
|
127
|
+
await this.queue
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/** Persist atomically. */
|
|
131
|
+
async flush() {
|
|
132
|
+
await mkdir(dirname(this.file), { recursive: true })
|
|
133
|
+
const temporary = `${this.file}.${process.pid}.tmp`
|
|
134
|
+
await writeFile(temporary, `${JSON.stringify({ version: VERSION, entries: this.entries }, null, 2)}\n`, 'utf8')
|
|
135
|
+
await rename(temporary, this.file)
|
|
136
|
+
}
|
|
137
|
+
}
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Remove the empty session records the workbench itself left behind.
|
|
3
|
+
*
|
|
4
|
+
* The workbench creates a session for "New", and a session the user never
|
|
5
|
+
* writes into is a header-only log. Archiving hides such a record but keeps its
|
|
6
|
+
* bytes and its slot in the archived set forever, and DSH exposes no delete
|
|
7
|
+
* API. This module is the narrow exception: it only touches a session that is
|
|
8
|
+
* archived, not live in this process, attached to the library directory, and
|
|
9
|
+
* has never started a turn. Everything else is left alone.
|
|
10
|
+
*
|
|
11
|
+
* @module dsh-pdf-workbench/purge
|
|
12
|
+
*/
|
|
13
|
+
import { rm, rmdir } from 'node:fs/promises'
|
|
14
|
+
import { basename, dirname } from 'node:path'
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Compare two Windows paths loosely.
|
|
18
|
+
* @param {unknown} left - first path.
|
|
19
|
+
* @param {unknown} right - second path.
|
|
20
|
+
* @returns {boolean} whether they name the same directory.
|
|
21
|
+
*/
|
|
22
|
+
function sameDirectory(left, right) {
|
|
23
|
+
if (typeof left !== 'string' || typeof right !== 'string' || left === '' || right === '') return false
|
|
24
|
+
const normalize = value => value.replace(/[\\/]+$/g, '').replace(/\//g, '\\').toLowerCase()
|
|
25
|
+
return normalize(left) === normalize(right)
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Purge archived, never-started sessions that belong to the library directory.
|
|
30
|
+
* @param {import('@deepseek-ai/cordis').Context} ctx - host context.
|
|
31
|
+
* @param {{ libraryDir: string }} config - resolved paths.
|
|
32
|
+
* @param {{ remove: (id: string) => Promise<void> } | undefined} pending - pending-intent store.
|
|
33
|
+
* @param {{ forget: (id: string) => Promise<void> } | undefined} [links] - extra-placement store.
|
|
34
|
+
* @param {{ forget: (id: string) => Promise<void> } | undefined} [pressure] - context-pressure snapshot store.
|
|
35
|
+
* @returns {Promise<{ purged: number }>} how many stored sessions were removed.
|
|
36
|
+
*/
|
|
37
|
+
export async function purgeArchivedBlanks(ctx, config, pending, links, pressure) {
|
|
38
|
+
const registry = ctx.get('workspaceRegistry')
|
|
39
|
+
const persistence = ctx.get('sessionPersistence')
|
|
40
|
+
const query = ctx.get('sessionQuery')
|
|
41
|
+
if (registry === undefined || persistence === undefined || query === undefined) return { purged: 0 }
|
|
42
|
+
const archived = Array.isArray(registry.archivedSessionIds) ? registry.archivedSessionIds : []
|
|
43
|
+
const sessions = ctx.get('sessions')
|
|
44
|
+
let purged = 0
|
|
45
|
+
for (const id of archived) {
|
|
46
|
+
// A live session may belong to an open conversation: never touch it.
|
|
47
|
+
if (sessions !== undefined && sessions.get(id) !== undefined) continue
|
|
48
|
+
let snapshot
|
|
49
|
+
try {
|
|
50
|
+
snapshot = await persistence.stat(id)
|
|
51
|
+
} catch {
|
|
52
|
+
continue
|
|
53
|
+
}
|
|
54
|
+
if (snapshot === null || snapshot === undefined || typeof snapshot !== 'object' || snapshot.location === undefined) {
|
|
55
|
+
// The record is already gone; drop any intent that still names it.
|
|
56
|
+
if (pending !== undefined) await pending.remove(id).catch(() => {})
|
|
57
|
+
if (links !== undefined) await links.forget(id).catch(() => {})
|
|
58
|
+
if (pressure !== undefined) await pressure.forget(id).catch(() => {})
|
|
59
|
+
continue
|
|
60
|
+
}
|
|
61
|
+
if (!sameDirectory(snapshot.header?.cwd, config.libraryDir)) continue
|
|
62
|
+
let log
|
|
63
|
+
try {
|
|
64
|
+
log = await query.readSession(id)
|
|
65
|
+
} catch {
|
|
66
|
+
continue
|
|
67
|
+
}
|
|
68
|
+
if (!Array.isArray(log.events)) continue
|
|
69
|
+
// One turn is enough to make the session worth keeping.
|
|
70
|
+
if (log.events.some(event => event?.type === 'turn/start')) continue
|
|
71
|
+
const file = snapshot.location.path
|
|
72
|
+
if (typeof file !== 'string' || file === '') continue
|
|
73
|
+
// Only ever remove a file that lives under this session's own directory.
|
|
74
|
+
if (!file.includes(id)) continue
|
|
75
|
+
try {
|
|
76
|
+
await rm(file, { force: true })
|
|
77
|
+
} catch {
|
|
78
|
+
continue
|
|
79
|
+
}
|
|
80
|
+
const directory = dirname(file)
|
|
81
|
+
if (basename(directory) === id) {
|
|
82
|
+
try {
|
|
83
|
+
await rmdir(directory)
|
|
84
|
+
} catch {
|
|
85
|
+
// A lingering sibling file keeps the directory: the log itself is gone.
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
if (pending !== undefined) await pending.remove(id).catch(() => {})
|
|
89
|
+
if (links !== undefined) await links.forget(id).catch(() => {})
|
|
90
|
+
if (pressure !== undefined) await pressure.forget(id).catch(() => {})
|
|
91
|
+
// Drop the dead id from the archive set so it stops occupying that list too.
|
|
92
|
+
try {
|
|
93
|
+
await registry.unarchiveSession(id)
|
|
94
|
+
} catch {
|
|
95
|
+
// The registry keeps a harmless dead id when this fails.
|
|
96
|
+
}
|
|
97
|
+
purged += 1
|
|
98
|
+
}
|
|
99
|
+
return { purged }
|
|
100
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Render one Markdown artifact to PDF with the bundled `tools/md2tex.py`.
|
|
3
|
+
* The tool is optional: it needs Python and XeLaTeX, and the route reports a
|
|
4
|
+
* readable error when either is missing instead of touching the runtime.
|
|
5
|
+
*
|
|
6
|
+
* The child process is spawned with `stdio: 'ignore'`: under the DSH sandbox
|
|
7
|
+
* piped stdio is denied, and the result is proven by the output file anyway.
|
|
8
|
+
* @module dsh-pdf-workbench/render
|
|
9
|
+
*/
|
|
10
|
+
import { execFile } from 'node:child_process'
|
|
11
|
+
import { stat } from 'node:fs/promises'
|
|
12
|
+
|
|
13
|
+
const PYTHON_CANDIDATES = [process.env.MATH_DESK_PYTHON, 'python', 'py'].filter(Boolean)
|
|
14
|
+
|
|
15
|
+
function run(python, args, env) {
|
|
16
|
+
return new Promise((resolve, reject) => {
|
|
17
|
+
execFile(python, args, { timeout: 180000, windowsHide: true, stdio: 'ignore', env: { ...process.env, ...env } }, error => {
|
|
18
|
+
if (error === null) resolve()
|
|
19
|
+
else reject(new Error(`${python}: ${error.message}`))
|
|
20
|
+
})
|
|
21
|
+
})
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* @param {string} script - absolute path of `tools/md2tex.py`.
|
|
26
|
+
* @param {string} mdPath - source Markdown.
|
|
27
|
+
* @param {string} outPath - destination PDF.
|
|
28
|
+
* @param {Record<string, string>} [env] - extra environment variables.
|
|
29
|
+
* @returns {Promise<string>} the PDF path.
|
|
30
|
+
*/
|
|
31
|
+
export async function renderMarkdown(script, mdPath, outPath, env = {}) {
|
|
32
|
+
let lastError = 'no Python found'
|
|
33
|
+
for (const python of PYTHON_CANDIDATES) {
|
|
34
|
+
try {
|
|
35
|
+
await run(python, [script, mdPath, outPath], env)
|
|
36
|
+
await stat(outPath)
|
|
37
|
+
return outPath
|
|
38
|
+
} catch (error) {
|
|
39
|
+
lastError = error instanceof Error ? error.message : String(error)
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
throw new Error(lastError)
|
|
43
|
+
}
|