dsh-pdf-workbench 1.3.0 → 1.3.1

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.
Files changed (52) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +192 -163
  3. package/client.js +911 -223
  4. package/docs/RELEASE.md +110 -0
  5. package/icon.svg +6 -6
  6. package/index.js +18 -0
  7. package/package.json +1 -1
  8. package/screenshots.json +8 -8
  9. package/src/client/viewer.js +80 -80
  10. package/src/host/libraries.js +9 -12
  11. package/src/host/links.js +1 -1
  12. package/src/host/pending.js +1 -1
  13. package/src/host/routes.js +58 -12
  14. package/tools/AGENTS.md +33 -33
  15. package/tools/README.md +138 -138
  16. package/tools/auto_chapters.py +122 -122
  17. package/tools/dsfile.mjs +198 -198
  18. package/tools/export_chapter.py +108 -108
  19. package/tools/export_page.py +91 -91
  20. package/tools/md2overlay.py +200 -200
  21. package/tools/md2tex.py +422 -422
  22. package/tools/pdf_blocks.py +110 -110
  23. package/tools/pdf_fontsize.py +20 -20
  24. package/tools/pdf_merge.py +15 -15
  25. package/tools/pdf_pagesizes.py +8 -8
  26. package/tools/pdf_raster.py +21 -21
  27. package/tools/pdfkit.py +896 -896
  28. package/tools/realign_overlay.py +248 -248
  29. package/tools/rebuild_overlay.py +55 -55
  30. package/tools/rename_chapters.py +93 -93
  31. package/tools//350/247/204/350/214/203/00_/345/205/261/345/220/214/347/272/246/345/256/232.md +142 -142
  32. package/tools//350/247/204/350/214/203/01_/346/226/207/346/234/254/346/217/220/345/217/226.md +204 -204
  33. package/tools//350/247/204/350/214/203/02_/347/255/224/347/226/221.md +61 -61
  34. package/tools//350/247/204/350/214/203/03_/344/271/240/351/242/230/350/247/243/347/255/224.md +77 -77
  35. 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 -11
  36. package/vendor/cmaps/LICENSE.txt +36 -36
  37. package/vendor/fonts/excalifont/OFL.txt +98 -98
  38. package/vendor/fonts/zhhand/OFL.txt +99 -99
  39. package/vendor/pdf.min.mjs +28 -28
  40. package/vendor/pdf.worker.min.mjs +28 -28
  41. package/vendor/pdfjs-LICENSE.txt +177 -177
  42. package/vendor/rough/LICENSE +21 -21
  43. package/vendor/rough/rough.mjs +1 -1
  44. package/vendor/wasm/LICENSE_JBIG2 +196 -196
  45. package/vendor/wasm/LICENSE_OPENJPEG +39 -39
  46. package/vendor/wasm/LICENSE_PDFJS_JBIG2 +13 -13
  47. package/vendor/wasm/LICENSE_PDFJS_OPENJPEG +22 -22
  48. package/vendor/wasm/LICENSE_PDFJS_QCMS +19 -19
  49. package/vendor/wasm/LICENSE_QCMS +21 -21
  50. package/vendor/wasm/jbig2_nowasm_fallback.js +15 -15
  51. package/vendor/wasm/openjpeg_nowasm_fallback.js +17 -17
  52. package//345/256/211/350/243/205/344/270/216/345/210/206/345/217/221.md +2 -2
@@ -0,0 +1,110 @@
1
+ # 发版流程(固定版)
2
+
3
+ > 用户说「更新 / 发版 / 发布」时,**按本文执行**,不要重新设计流程。
4
+ > 日常修改仍然只本地提交;本流程只在明确发版时启动。
5
+
6
+ ## 0. 发版前
7
+
8
+ 1. 确认 `package.json` 版本:修复 +补丁位,新功能 +次版本位。
9
+ 2. `CHANGELOG.md` 顶部新增 `## vX.Y.Z`,写清改了什么。
10
+ 3. 如果界面有变化:更新 `README.md` 截图与 `screenshots.json`(市场从仓库读取,不需要为截图提 PR)。
11
+ 4. 跑检查:
12
+
13
+ ```powershell
14
+ cd math-desk
15
+ node --check client.js
16
+ node --check index.js
17
+ Get-ChildItem src\host\*.js | ForEach-Object { node --check $_.FullName }
18
+ cd ..
19
+ node .dsh-scratch\check-keys.mjs
20
+ node .dsh-scratch\check-deps.mjs
21
+ node .dsh-scratch\side-smoke.mjs
22
+ node .dsh-scratch\notes-test.mjs
23
+ node .dsh-scratch\session-seed-test.mjs
24
+ node .dsh-scratch\page-store-test.mjs
25
+ node .dsh-scratch\annotation-store-test.mjs
26
+ node .dsh-scratch\host-test.mjs
27
+ ```
28
+
29
+ ## 1. GitHub Release(默认渠道)
30
+
31
+ ```powershell
32
+ cd math-desk
33
+ powershell -ExecutionPolicy Bypass -File .\build-dist.ps1
34
+ # 产出 ..\dist\dsh-pdf-workbench-X.Y.Z.zip
35
+
36
+ git add -A
37
+ git commit -m "vX.Y.Z: <一句话>"
38
+ git tag vX.Y.Z
39
+ git push origin main
40
+ git push --tags origin
41
+ ```
42
+
43
+ 创建 GitHub Release `vX.Y.Z`,上传:
44
+
45
+ - `dsh-pdf-workbench-X.Y.Z.zip`(必须)
46
+ - 可选 `dsh-pdf-workbench-X.Y.Z.tgz`(干净 worktree 里 `npm pack`,作为市场 npm 失败时的回退)
47
+
48
+ Release 说明直接复制 `CHANGELOG` 的该版本条目。
49
+
50
+ ## 2. npm
51
+
52
+ npm 包名固定 **`dsh-pdf-workbench`**。必须从**干净 worktree / 干净 clone**发布,避免带上本机 `tools/py.cmd`、`tools/node.cmd`、`__pycache__`:
53
+
54
+ ```powershell
55
+ cd <工作区>\math-desk
56
+ git worktree add ..\dist\npm-release vX.Y.Z
57
+ cd ..\dist\npm-release
58
+ npm login --auth-type=web
59
+ npm stage publish --access public
60
+ ```
61
+
62
+ 然后批准:
63
+
64
+ - 网页:https://www.npmjs.com/settings/minggemofashi/staged
65
+ - 或:`npm stage approve <stage-id>`(真实 6 位 OTP)
66
+
67
+ 批准后验证:
68
+
69
+ ```powershell
70
+ npm view dsh-pdf-workbench@X.Y.Z version
71
+ ```
72
+
73
+ 清理:
74
+
75
+ ```powershell
76
+ cd <工作区>
77
+ git -C math-desk worktree remove ..\dist\npm-release
78
+ ```
79
+
80
+ > 注意:npm 的 granular token 不能代替 2FA 发布;必须交互登录。不要用字面量 `你的6位验证码`。
81
+
82
+ ## 3. 插件市场
83
+
84
+ 首次上架:向 `awesome-dsh-plugin` 提交 `data/plugins/<owner>__<repo>.yml`;本插件条目已提交(PR 保留)。
85
+
86
+ 首次之后:
87
+
88
+ - npm 包 `repository` 指向同一仓库时,dsh-market 自动做 npm 映射,**版本更新不需要再提 PR**;
89
+ - 只有改仓库地址、包名、分类或描述时才改 awesome 条目;
90
+ - 截图换图只需更新仓库 `screenshots.json`;
91
+ - 若 npm 发布失败,才回退到 tarball:上传新版本 `.tgz` 并更新条目的 `tarball` 字段(需要 PR)。
92
+
93
+ ## 4. 一致性检查
94
+
95
+ - `package.json` 版本 = tag = GitHub Release = npm 版本;
96
+ - Release 的 zip / tgz 能下载;
97
+ - `README.md` 安装命令版本正确;
98
+ - 远程 `main` 包含发版提交;
99
+ - npm 页面可见,market 能搜到(缓存可能有延迟)。
100
+
101
+ ## 5. 旧包处理(已完成)
102
+
103
+ 旧包 `dsh-pdf-math-desk` 已从 npm 删除(`npm view` 返回 404),无需再处理;不要重新发布这个名称。
104
+
105
+ ## 6. 不要做
106
+
107
+ - 不重新引入 `market/index.json`(收录由 awesome 列表负责);
108
+ - 不发 npm 前不更新市场条目的 npm 映射;
109
+ - 不用本机工作目录直接 `npm publish`(会带上未跟踪的包装脚本/pycache);
110
+ - 不在用户没说“发版”时升版本/tag/push/Release/npm。
package/icon.svg CHANGED
@@ -1,6 +1,6 @@
1
- <svg xmlns="http://www.w3.org/2000/svg" width="36" height="36" viewBox="0 0 24 24" role="img" aria-label="Math workbench">
2
- <rect x="2.8" y="6.8" width="4" height="13.6" rx="0.9" fill="#2f6f8f"/>
3
- <rect x="9.8" y="3.8" width="4" height="16.6" rx="0.9" fill="#3f8aa6"/>
4
- <rect x="16.8" y="8.6" width="4" height="11.8" rx="0.9" fill="#2a5f78"/>
5
- <path d="M2.2 21.4h19.6" stroke="#1f4a5c" stroke-width="1.4" stroke-linecap="round"/>
6
- </svg>
1
+ <svg xmlns="http://www.w3.org/2000/svg" width="36" height="36" viewBox="0 0 24 24" role="img" aria-label="Math workbench">
2
+ <rect x="2.8" y="6.8" width="4" height="13.6" rx="0.9" fill="#2f6f8f"/>
3
+ <rect x="9.8" y="3.8" width="4" height="16.6" rx="0.9" fill="#3f8aa6"/>
4
+ <rect x="16.8" y="8.6" width="4" height="11.8" rx="0.9" fill="#2a5f78"/>
5
+ <path d="M2.2 21.4h19.6" stroke="#1f4a5c" stroke-width="1.4" stroke-linecap="round"/>
6
+ </svg>
package/index.js CHANGED
@@ -123,6 +123,24 @@ export function apply(ctx, rawConfig) {
123
123
  contextPressure,
124
124
  digest: digestChapter,
125
125
  installToolkit: libraryDir => installToolkit(config.toolsDir, libraryDir),
126
+ /**
127
+ * The Workspaces a user can choose for conversations. The browser never
128
+ * creates one silently; it shows these and a create action.
129
+ * @returns {Promise<Array<{ id: string, title: string, path: string }>>} the list.
130
+ */
131
+ async listWorkspaces() {
132
+ const registry = ctx.get('workspaceRegistry')
133
+ if (registry === undefined) return []
134
+ try {
135
+ return registry.list().map(workspace => ({
136
+ id: String(workspace.id),
137
+ title: typeof workspace.title === 'string' ? workspace.title : '',
138
+ path: typeof workspace.path === 'string' ? workspace.path : '',
139
+ }))
140
+ } catch {
141
+ return []
142
+ }
143
+ },
126
144
  /**
127
145
  * Resolve the Workspace that owns the library directory, so the browser
128
146
  * creates sessions inside it instead of into an ungrouped cwd.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-pdf-workbench",
3
- "version": "1.3.0",
3
+ "version": "1.3.1",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "description": "PDF reading workbench for DSH: books and chapters, ask-as-you-read conversations, annotations, and a built-in pdf.js reader.",
package/screenshots.json CHANGED
@@ -1,8 +1,8 @@
1
- [
2
- "assets/screenshot-hero.jpg",
3
- "assets/screenshot-shelf.jpg",
4
- "assets/screenshot-reader.jpg",
5
- "assets/screenshot-bilingual.jpg",
6
- "assets/screenshot-overlay.jpg",
7
- "assets/screenshot-qa.jpg"
8
- ]
1
+ [
2
+ "assets/screenshot-hero.jpg",
3
+ "assets/screenshot-shelf.jpg",
4
+ "assets/screenshot-reader.jpg",
5
+ "assets/screenshot-bilingual.jpg",
6
+ "assets/screenshot-overlay.jpg",
7
+ "assets/screenshot-qa.jpg"
8
+ ]
@@ -1,80 +1,80 @@
1
- /**
2
- * PdfViewer — the unified reader face of the rewrite.
3
- *
4
- * One component owns: zoom, scroll container, page painting per layer, and the
5
- * mode layout (single / continuous / dual). Modes only choose which layers and
6
- * columns are rendered; painting and geometry stay identical, which is what the
7
- * old per-mode canvases kept getting wrong.
8
- *
9
- * This module is the extracted target: `client.js` will mount it once per
10
- * reader and per side preview, then the legacy paint paths can be deleted.
11
- *
12
- * Layers:
13
- * { kind: 'source', doc }
14
- * { kind: 'translation', doc }
15
- * { kind: 'masks', boxes } // reveal boxes over `translation`
16
- * { kind: 'blocks', blocks } // debug block overlay
17
- *
18
- * Geometry rules (from docs/READER-REWRITE.md):
19
- * - centered content uses `safe center`, so a zoomed page can still scroll to
20
- * its left edge;
21
- * - the overlay is ONE page-sized translation image clipped by a `clipPath` of
22
- * rectangle subpaths per mask (v0.49.2). A per-mask canvas drifted because a
23
- * zoomed page box is wider than the stage; `maskPageStyle` below is the old
24
- * per-canvas formula, kept only for reference;
25
- * - repainting is driven by (doc, page, scale, version), not by DOM mounting.
26
- */
27
-
28
- /** CSS injected once by the viewer's owner. */
29
- export const VIEWER_STYLE = `
30
- .mdv-root { position: relative; width: 100%; height: 100%; display: flex; flex-direction: column; min-height: 0; }
31
- .mdv-stage { flex: 1 1 auto; min-height: 0; overflow: auto; }
32
- .mdv-columns { display: flex; gap: 12px; width: 100%; height: 100%; }
33
- .mdv-column { flex: 1 1 0; min-width: 0; overflow: auto; }
34
- .mdv-page { position: relative; margin: 0 auto 18px; width: fit-content; }
35
- .mdv-canvas { display: block; }
36
- .mdv-layer { position: absolute; inset: 0; pointer-events: none; }
37
- .mdv-mask { position: absolute; overflow: hidden; border: 1px dashed rgba(40, 140, 235, .9); box-sizing: border-box; }
38
- .mdv-mask-page { position: absolute; max-width: none; }
39
- .mdv-block { position: absolute; border: 1px dashed rgba(235, 70, 70, .85); box-sizing: border-box; }
40
- .mdv-block[data-kind="heading"] { border-color: rgba(60, 130, 235, .95); }
41
- .mdv-block[data-kind="formula"] { border-color: rgba(40, 160, 90, .95); }
42
- `
43
-
44
- /**
45
- * Paint one page into a canvas at a given scale.
46
- * @param {object} options - doc, pageNumber, scale, canvas, signal.
47
- * @returns {Promise<void>} resolves when the page finished painting.
48
- */
49
- export async function paintPage(options) {
50
- const { doc, pageNumber, scale, canvas, signal } = options
51
- const page = await doc.getPage(pageNumber)
52
- if (signal !== undefined && signal.aborted) return
53
- const viewport = page.getViewport({ scale })
54
- let dpr = Math.min(2, Math.max(1, typeof window === 'undefined' ? 1 : window.devicePixelRatio || 1))
55
- if (viewport.width * dpr > 8000 || viewport.height * dpr > 8000) dpr = 1
56
- canvas.width = Math.max(1, Math.floor(viewport.width * dpr))
57
- canvas.height = Math.max(1, Math.floor(viewport.height * dpr))
58
- canvas.style.width = `${Math.floor(viewport.width)}px`
59
- canvas.style.height = `${Math.floor(viewport.height)}px`
60
- await page.render({
61
- canvasContext: canvas.getContext('2d'),
62
- viewport,
63
- transform: dpr !== 1 ? [dpr, 0, 0, dpr, 0, 0] : undefined,
64
- }).promise
65
- }
66
-
67
- /**
68
- * Compute the geometry a per-mask translated canvas used to need.
69
- * @deprecated Superseded by the single-image `clipPath` overlay (v0.49.2).
70
- * @param {{ x: number, y: number, w: number, h: number }} box - normalized box.
71
- * @returns {{ left: string, top: string, width: string, height: string }} CSS percentages.
72
- */
73
- export function maskPageStyle(box) {
74
- return {
75
- left: `${-(box.x / box.w) * 100}%`,
76
- top: `${-(box.y / box.h) * 100}%`,
77
- width: `${100 / box.w}%`,
78
- height: `${100 / box.h}%`,
79
- }
80
- }
1
+ /**
2
+ * PdfViewer — the unified reader face of the rewrite.
3
+ *
4
+ * One component owns: zoom, scroll container, page painting per layer, and the
5
+ * mode layout (single / continuous / dual). Modes only choose which layers and
6
+ * columns are rendered; painting and geometry stay identical, which is what the
7
+ * old per-mode canvases kept getting wrong.
8
+ *
9
+ * This module is the extracted target: `client.js` will mount it once per
10
+ * reader and per side preview, then the legacy paint paths can be deleted.
11
+ *
12
+ * Layers:
13
+ * { kind: 'source', doc }
14
+ * { kind: 'translation', doc }
15
+ * { kind: 'masks', boxes } // reveal boxes over `translation`
16
+ * { kind: 'blocks', blocks } // debug block overlay
17
+ *
18
+ * Geometry rules (from docs/READER-REWRITE.md):
19
+ * - centered content uses `safe center`, so a zoomed page can still scroll to
20
+ * its left edge;
21
+ * - the overlay is ONE page-sized translation image clipped by a `clipPath` of
22
+ * rectangle subpaths per mask (v0.49.2). A per-mask canvas drifted because a
23
+ * zoomed page box is wider than the stage; `maskPageStyle` below is the old
24
+ * per-canvas formula, kept only for reference;
25
+ * - repainting is driven by (doc, page, scale, version), not by DOM mounting.
26
+ */
27
+
28
+ /** CSS injected once by the viewer's owner. */
29
+ export const VIEWER_STYLE = `
30
+ .mdv-root { position: relative; width: 100%; height: 100%; display: flex; flex-direction: column; min-height: 0; }
31
+ .mdv-stage { flex: 1 1 auto; min-height: 0; overflow: auto; }
32
+ .mdv-columns { display: flex; gap: 12px; width: 100%; height: 100%; }
33
+ .mdv-column { flex: 1 1 0; min-width: 0; overflow: auto; }
34
+ .mdv-page { position: relative; margin: 0 auto 18px; width: fit-content; }
35
+ .mdv-canvas { display: block; }
36
+ .mdv-layer { position: absolute; inset: 0; pointer-events: none; }
37
+ .mdv-mask { position: absolute; overflow: hidden; border: 1px dashed rgba(40, 140, 235, .9); box-sizing: border-box; }
38
+ .mdv-mask-page { position: absolute; max-width: none; }
39
+ .mdv-block { position: absolute; border: 1px dashed rgba(235, 70, 70, .85); box-sizing: border-box; }
40
+ .mdv-block[data-kind="heading"] { border-color: rgba(60, 130, 235, .95); }
41
+ .mdv-block[data-kind="formula"] { border-color: rgba(40, 160, 90, .95); }
42
+ `
43
+
44
+ /**
45
+ * Paint one page into a canvas at a given scale.
46
+ * @param {object} options - doc, pageNumber, scale, canvas, signal.
47
+ * @returns {Promise<void>} resolves when the page finished painting.
48
+ */
49
+ export async function paintPage(options) {
50
+ const { doc, pageNumber, scale, canvas, signal } = options
51
+ const page = await doc.getPage(pageNumber)
52
+ if (signal !== undefined && signal.aborted) return
53
+ const viewport = page.getViewport({ scale })
54
+ let dpr = Math.min(2, Math.max(1, typeof window === 'undefined' ? 1 : window.devicePixelRatio || 1))
55
+ if (viewport.width * dpr > 8000 || viewport.height * dpr > 8000) dpr = 1
56
+ canvas.width = Math.max(1, Math.floor(viewport.width * dpr))
57
+ canvas.height = Math.max(1, Math.floor(viewport.height * dpr))
58
+ canvas.style.width = `${Math.floor(viewport.width)}px`
59
+ canvas.style.height = `${Math.floor(viewport.height)}px`
60
+ await page.render({
61
+ canvasContext: canvas.getContext('2d'),
62
+ viewport,
63
+ transform: dpr !== 1 ? [dpr, 0, 0, dpr, 0, 0] : undefined,
64
+ }).promise
65
+ }
66
+
67
+ /**
68
+ * Compute the geometry a per-mask translated canvas used to need.
69
+ * @deprecated Superseded by the single-image `clipPath` overlay (v0.49.2).
70
+ * @param {{ x: number, y: number, w: number, h: number }} box - normalized box.
71
+ * @returns {{ left: string, top: string, width: string, height: string }} CSS percentages.
72
+ */
73
+ export function maskPageStyle(box) {
74
+ return {
75
+ left: `${-(box.x / box.w) * 100}%`,
76
+ top: `${-(box.y / box.h) * 100}%`,
77
+ width: `${100 / box.w}%`,
78
+ height: `${100 / box.h}%`,
79
+ }
80
+ }
@@ -3,9 +3,9 @@
3
3
  *
4
4
  * One computer may hold several split libraries (different subjects, different
5
5
  * drives). Each entry is just the paths the rest of the plugin needs, kept in
6
- * `$DSH_HOME/pdfdesk/libraries.json` together with which one is active. The
7
- * profile config still supplies the first library, so an existing install keeps
8
- * working without touching its patch.
6
+ * `$DSH_HOME/pdfdesk/libraries.json` together with which one is active. An empty
7
+ * file means no library: the profile config is only a fallback for migration and
8
+ * tests, never a generated first library.
9
9
  *
10
10
  * Only the math layout is supported for now: the manual in the workbench tells a
11
11
  * user how a library must be split and named, and `mode` records that so a later
@@ -90,7 +90,7 @@ export class LibrariesStore {
90
90
  this.queue = Promise.resolve()
91
91
  }
92
92
 
93
- /** Load once; a damaged file falls back to the configured library. */
93
+ /** Load once; no libraries means an empty list, never a generated default. */
94
94
  async load() {
95
95
  let parsed = null
96
96
  try {
@@ -105,15 +105,12 @@ export class LibrariesStore {
105
105
  if (entry !== null && !items.some(item => item.id === entry.id)) items.push(entry)
106
106
  if (items.length >= MAX_LIBRARIES) break
107
107
  }
108
- if (items.length === 0) {
109
- items.push(this.fallbackEntry())
110
- }
111
108
  this.items = items
112
109
  const storedActive = parsed !== null && typeof parsed === 'object' && typeof parsed.activeId === 'string' ? parsed.activeId : null
113
- this.activeId = items.some(item => item.id === storedActive) ? storedActive : items[0].id
110
+ this.activeId = items.some(item => item.id === storedActive) ? storedActive : (items[0]?.id ?? null)
114
111
  }
115
112
 
116
- /** The profile's configured library, as the first entry. */
113
+ /** The profile's configured library. Kept for explicit migration/tests only. */
117
114
  fallbackEntry() {
118
115
  return {
119
116
  id: 'lib-default',
@@ -130,7 +127,7 @@ export class LibrariesStore {
130
127
 
131
128
  /**
132
129
  * Snapshot for bootstrap.
133
- * @returns {Promise<{ items: object[], activeId: string }>} the list and the active id.
130
+ * @returns {Promise<{ items: object[], activeId: string | null }>} the list and the active id.
134
131
  */
135
132
  async list() {
136
133
  await this.ready
@@ -207,8 +204,8 @@ export class LibrariesStore {
207
204
  await this.ready
208
205
  const next = this.items.filter(item => item.id !== id)
209
206
  if (next.length === this.items.length) return false
210
- this.items = next.length === 0 ? [this.fallbackEntry()] : next
211
- if (!this.items.some(item => item.id === this.activeId)) this.activeId = this.items[0].id
207
+ this.items = next
208
+ if (!this.items.some(item => item.id === this.activeId)) this.activeId = this.items[0]?.id ?? null
212
209
  await this.flush()
213
210
  return true
214
211
  }
package/src/host/links.js CHANGED
@@ -33,7 +33,7 @@ const BOOK_ID = /^[^\\/:*?"<>|]{1,200}$/
33
33
  const CODE = /^c\d{2,3}$/
34
34
 
35
35
  /** Modes a chapter groups its sessions by. */
36
- const MODES = new Set(['extract', 'qa', 'exercise'])
36
+ const MODES = new Set(['general', 'extract', 'qa', 'exercise'])
37
37
 
38
38
  /**
39
39
  * Keep one link if it is usable.
@@ -25,7 +25,7 @@ const SESSION_ID = /^[\w-]{1,80}$/
25
25
  const CODE = /^c\d{2,3}$/
26
26
 
27
27
  /** Modes the workbench creates. */
28
- const MODES = new Set(['extract', 'qa', 'exercise'])
28
+ const MODES = new Set(['general', 'extract', 'qa', 'exercise'])
29
29
 
30
30
  /**
31
31
  * Keep one entry if it is usable.
@@ -108,7 +108,7 @@ async function mutationBody(request, response) {
108
108
  /**
109
109
  * Build the prefix handler for the plugin's routes.
110
110
  * @param {{ libraryDir: string, fragmentDir: string, artifactDir: string, coverDir: string, indexPath: string, statePath: string, resumeRatio?: number, toolsDir?: string }} config - resolved paths and behavior (the profile's library is the fallback).
111
- * @param {{ resolveTarget?: () => Promise<{ workspaceId: string | null, cwd: string }>, pending?: { list: () => Promise<object>, put: Function, remove: Function }, links?: { list: () => Promise<Record<string, object[]>>, add: Function, remove: Function }, contextPressure?: (sessionId: string) => Promise<object | null>, libraries?: { list: Function, active: Function, add: Function, setActive: Function, remove: Function }, installToolkit?: (libraryDir: string) => Promise<object> }} [options] - host capabilities the routes expose.
111
+ * @param {{ resolveTarget?: () => Promise<{ workspaceId: string | null, cwd: string }>, listWorkspaces?: () => Promise<Array<{ id: string, title: string, path: string }>>, pending?: { list: Function, put: Function, remove: Function }, links?: { list: Function, add: Function, remove: Function }, contextPressure?: (sessionId: string) => Promise<object | null>, libraries?: { list: Function, active: Function, add: Function, setActive: Function, remove: Function }, installToolkit?: (libraryDir: string) => Promise<object> }} [options] - host capabilities the routes expose.
112
112
  * @returns {(request: import('node:http').IncomingMessage, response: import('node:http').ServerResponse) => Promise<void>} the handler.
113
113
  */
114
114
  export function createHandler(config, options = {}) {
@@ -204,8 +204,7 @@ async function deepseekKey() {
204
204
 
205
205
  async function activeLibrary() {
206
206
  if (libraries !== undefined && typeof libraries.active === 'function') {
207
- const active = await libraries.active()
208
- if (active !== null && active !== undefined) return active
207
+ return await libraries.active()
209
208
  }
210
209
  return {
211
210
  id: 'default',
@@ -274,6 +273,25 @@ async function activeLibrary() {
274
273
  const url = new URL(request.url ?? '/', 'http://localhost')
275
274
  const path = url.pathname.replace(/\/+$/, '')
276
275
 
276
+ // Features that need a catalog stop at a clear 409 instead of inventing a
277
+ // default library. Library management, settings, static assets and session
278
+ // bookkeeping remain available so the user can add the first library.
279
+ const libraryFree = path === `${PREFIX}/bootstrap`
280
+ || path.startsWith(`${PREFIX}/libraries`)
281
+ || path === `${PREFIX}/update`
282
+ || path === `${PREFIX}/qa-persona`
283
+ || path === `${PREFIX}/deepseek-key`
284
+ || path.startsWith(`${PREFIX}/vendor/`)
285
+ || path === `${PREFIX}/state`
286
+ || path === `${PREFIX}/pending`
287
+ || path === `${PREFIX}/pending/remove`
288
+ || path === `${PREFIX}/context`
289
+ || path === `${PREFIX}/purge`
290
+ if (!libraryFree && (libraries === undefined || (await libraries.active()) === null)) {
291
+ fail(response, 409, 'no-library', '还没有书库,请先在书库页添加一个')
292
+ return
293
+ }
294
+
277
295
  if (path === `${PREFIX}/qa-persona`) {
278
296
  if (request.method !== 'GET') {
279
297
  fail(response, 405, 'method-not-allowed', '这个接口只接受 GET')
@@ -296,9 +314,41 @@ async function activeLibrary() {
296
314
  return
297
315
  }
298
316
  // The Refresh button is a deliberate look: bypass the short reuse window.
299
- // A first run may have no library yet: an unreadable catalog must still
300
- // reach the browser so it can show the "add a library" page instead of a
301
- // dead error screen.
317
+ // A first run has no library: return an explicit empty payload so the
318
+ // browser shows the add-a-library page instead of a fabricated default.
319
+ const libraryList = libraries === undefined || typeof libraries.list !== 'function'
320
+ ? { items: [], activeId: null }
321
+ : await libraries.list()
322
+ const library = libraryList.items.length === 0 ? null : await activeLibrary()
323
+ const state = await readState(config.statePath)
324
+ if (library === null) {
325
+ sendJson(response, 200, {
326
+ ok: true,
327
+ version: PLUGIN_VERSION,
328
+ source: 'missing',
329
+ indexIgnored: false,
330
+ problem: null,
331
+ library: null,
332
+ libraries: libraryList,
333
+ libraryDir: '',
334
+ fragmentDir: '',
335
+ artifactDir: '',
336
+ indexPath: '',
337
+ covers: [],
338
+ target: { workspaceId: null, cwd: '' },
339
+ environment: { ...detectEnvironment(), apiKey: (await deepseekKey()) !== '' },
340
+ workspaces: typeof options.listWorkspaces === 'function' ? await options.listWorkspaces() : [],
341
+ resumeRatio: typeof config.resumeRatio === 'number' ? config.resumeRatio : 0.85,
342
+ pending: pending === undefined ? {} : await pending.list(),
343
+ links: links === undefined ? {} : await links.list(),
344
+ shelves: { hidden: [], shelves: [] },
345
+ books: [],
346
+ skipped: [],
347
+ warnings: [],
348
+ state,
349
+ })
350
+ return
351
+ }
302
352
  let fresh
303
353
  try {
304
354
  fresh = await catalog(true)
@@ -312,14 +362,9 @@ async function activeLibrary() {
312
362
  warnings: [],
313
363
  }
314
364
  }
315
- const library = await activeLibrary()
316
- const state = await readState(config.statePath)
317
365
  const target = typeof options.resolveTarget === 'function'
318
366
  ? await options.resolveTarget()
319
367
  : { workspaceId: null, cwd: library.libraryDir }
320
- const libraryList = libraries === undefined || typeof libraries.list !== 'function'
321
- ? { items: [library], activeId: library.id }
322
- : await libraries.list()
323
368
  // Every chapter carries its absolute PDF path once. A new conversation's
324
369
  // prompt can then name the file directly instead of paying for a search;
325
370
  // an unplaceable chapter (hand-edited catalog) simply has no path.
@@ -355,7 +400,8 @@ async function activeLibrary() {
355
400
  indexPath: library.indexPath,
356
401
  covers: await coverIds(library.coverDir),
357
402
  target,
358
- environment: detectEnvironment(),
403
+ environment: { ...detectEnvironment(), apiKey: (await deepseekKey()) !== '' },
404
+ workspaces: typeof options.listWorkspaces === 'function' ? await options.listWorkspaces() : [],
359
405
  resumeRatio: typeof config.resumeRatio === 'number' ? config.resumeRatio : 0.85,
360
406
  pending: pending === undefined ? {} : await pending.list(),
361
407
  links: links === undefined ? {} : await links.list(),
package/tools/AGENTS.md CHANGED
@@ -1,33 +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、故障对照表、成本与隐私都在那里。不要把规范全文抄进回答。
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、故障对照表、成本与隐私都在那里。不要把规范全文抄进回答。