@waterwx/dsh-novel-forge 1.0.0-alpha → 1.1.0-alpha

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 (148) hide show
  1. package/README.md +231 -231
  2. package/lib/client.js +9118 -7818
  3. package/lib/index.js +316 -112
  4. package/lib/types/bookshelf.d.ts +5 -0
  5. package/lib/types/client/api.d.ts +6 -2
  6. package/lib/types/client/docx.d.ts +2 -0
  7. package/lib/types/client/locales.d.ts +5 -5
  8. package/lib/types/client/panel/AiTag.d.ts +1 -0
  9. package/lib/types/client/panel/AuthorHome.d.ts +11 -11
  10. package/lib/types/client/panel/CmdPalette.d.ts +12 -0
  11. package/lib/types/client/panel/LiveFeedLog.d.ts +3 -0
  12. package/lib/types/client/panel/NovelPanel.d.ts +5 -0
  13. package/lib/types/client/panel/PageHeader.d.ts +18 -0
  14. package/lib/types/client/panel/SettingsView.d.ts +12 -9
  15. package/lib/types/client/panel/ShelfView.d.ts +10 -1
  16. package/lib/types/client/panel/SubPage.d.ts +27 -0
  17. package/lib/types/client/panel/toast.d.ts +2 -0
  18. package/lib/types/client/panel/views.d.ts +25 -7
  19. package/lib/types/docx-core.d.ts +16 -0
  20. package/lib/types/docx.d.ts +2 -3
  21. package/lib/types/engine.d.ts +9 -9
  22. package/lib/types/index.d.ts +1 -1
  23. package/lib/types/migrate-output.d.ts +25 -0
  24. package/lib/types/prompting.d.ts +1 -26
  25. package/lib/types/protocol.d.ts +76 -31
  26. package/lib/types/routes.d.ts +4 -0
  27. package/lib/types/run.d.ts +2 -0
  28. package/package.json +120 -120
  29. package/src/assistant.ts +3 -3
  30. package/src/author-assets.ts +149 -147
  31. package/src/bookshelf.ts +187 -167
  32. package/src/client/api.ts +12 -3
  33. package/src/client/docx.ts +37 -69
  34. package/src/client/llmLive.ts +101 -101
  35. package/src/client/locales.ts +418 -418
  36. package/src/client/panel/AdaptModeView.tsx +535 -535
  37. package/src/client/panel/AiTag.tsx +7 -0
  38. package/src/client/panel/AssetsTab.tsx +448 -458
  39. package/src/client/panel/AssistantTab.tsx +35 -35
  40. package/src/client/panel/AuthorAssetsView.tsx +205 -207
  41. package/src/client/panel/AuthorHome.tsx +309 -234
  42. package/src/client/panel/BookAnalysisView.tsx +63 -60
  43. package/src/client/panel/CmdPalette.tsx +102 -0
  44. package/src/client/panel/CreateBookView.tsx +366 -371
  45. package/src/client/panel/DirectorView.tsx +23 -23
  46. package/src/client/panel/GlobalAssetLibraryView.tsx +168 -172
  47. package/src/client/panel/IdeaInspirationView.tsx +76 -71
  48. package/src/client/panel/ImportModal.tsx +302 -302
  49. package/src/client/panel/KnowledgeBaseView.tsx +125 -73
  50. package/src/client/panel/LiveFeedLog.tsx +67 -67
  51. package/src/client/panel/MarketRadarView.tsx +377 -374
  52. package/src/client/panel/ModelManager.tsx +205 -205
  53. package/src/client/panel/NovelPanel.tsx +2761 -2100
  54. package/src/client/panel/PageHeader.tsx +37 -0
  55. package/src/client/panel/ProgressConsole.tsx +63 -63
  56. package/src/client/panel/ReaderView.tsx +306 -306
  57. package/src/client/panel/RunPanel.tsx +223 -204
  58. package/src/client/panel/SettingsView.tsx +305 -265
  59. package/src/client/panel/ShelfView.tsx +266 -242
  60. package/src/client/panel/SubPage.tsx +42 -0
  61. package/src/client/panel/WorldTab.tsx +202 -202
  62. package/src/client/panel/panel.module.css +5764 -4693
  63. package/src/client/panel/toast.ts +51 -0
  64. package/src/client/panel/views.tsx +401 -346
  65. package/src/client/run-status.ts +143 -100
  66. package/src/docx-core.ts +53 -0
  67. package/src/docx.ts +44 -83
  68. package/src/engine.ts +15 -90
  69. package/src/index.ts +311 -298
  70. package/src/migrate-output.ts +76 -0
  71. package/src/novel-context.ts +286 -287
  72. package/src/prompting.ts +26 -159
  73. package/src/protocol.ts +79 -31
  74. package/src/routes.ts +123 -9
  75. package/src/run.ts +341 -338
  76. package/lib/types/client/panel/FlowGuide.d.ts +0 -22
  77. package/lib/types/client/panel/MangaRoleLibrary.d.ts +0 -20
  78. package/lib/types/client/panel/MangaWorkspace.d.ts +0 -12
  79. package/lib/types/client/panel/ModelPicker.d.ts +0 -11
  80. package/lib/types/client/panel/PropLibrary.d.ts +0 -8
  81. package/lib/types/client/panel/SceneLibrary.d.ts +0 -16
  82. package/lib/types/client/panel/StoryboardTab.d.ts +0 -19
  83. package/lib/types/client/panel/StyleCard.d.ts +0 -8
  84. package/lib/types/client/panel/StyleGallery.d.ts +0 -1
  85. package/lib/types/comic-presets.d.ts +0 -12
  86. package/lib/types/manga-genre-rules.d.ts +0 -22
  87. package/lib/types/shot-language.d.ts +0 -46
  88. package/lib/types/src/ai-scan.d.ts +0 -36
  89. package/lib/types/src/assets.d.ts +0 -37
  90. package/lib/types/src/assistant.d.ts +0 -45
  91. package/lib/types/src/author-assets.d.ts +0 -25
  92. package/lib/types/src/bookshelf.d.ts +0 -42
  93. package/lib/types/src/client/api.d.ts +0 -234
  94. package/lib/types/src/client/docx.d.ts +0 -15
  95. package/lib/types/src/client/index.d.ts +0 -14
  96. package/lib/types/src/client/locales.d.ts +0 -212
  97. package/lib/types/src/client/mount.d.ts +0 -9
  98. package/lib/types/src/client/panel/AdaptModeView.d.ts +0 -5
  99. package/lib/types/src/client/panel/AssetsTab.d.ts +0 -12
  100. package/lib/types/src/client/panel/AssistantTab.d.ts +0 -7
  101. package/lib/types/src/client/panel/AuthorAssetsView.d.ts +0 -4
  102. package/lib/types/src/client/panel/AuthorHome.d.ts +0 -32
  103. package/lib/types/src/client/panel/BookshelfBar.d.ts +0 -11
  104. package/lib/types/src/client/panel/CreateBookView.d.ts +0 -9
  105. package/lib/types/src/client/panel/FlowGuide.d.ts +0 -22
  106. package/lib/types/src/client/panel/GlobalAssetLibraryView.d.ts +0 -4
  107. package/lib/types/src/client/panel/ImportModal.d.ts +0 -7
  108. package/lib/types/src/client/panel/MangaRoleLibrary.d.ts +0 -20
  109. package/lib/types/src/client/panel/MangaWorkspace.d.ts +0 -12
  110. package/lib/types/src/client/panel/ModelManager.d.ts +0 -14
  111. package/lib/types/src/client/panel/NovelPanel.d.ts +0 -13
  112. package/lib/types/src/client/panel/ProgressConsole.d.ts +0 -17
  113. package/lib/types/src/client/panel/ProgressHomeView.d.ts +0 -5
  114. package/lib/types/src/client/panel/PropLibrary.d.ts +0 -8
  115. package/lib/types/src/client/panel/ReaderView.d.ts +0 -8
  116. package/lib/types/src/client/panel/ReasoningSection.d.ts +0 -10
  117. package/lib/types/src/client/panel/RunPanel.d.ts +0 -5
  118. package/lib/types/src/client/panel/SceneLibrary.d.ts +0 -16
  119. package/lib/types/src/client/panel/SettingsView.d.ts +0 -16
  120. package/lib/types/src/client/panel/ShelfView.d.ts +0 -15
  121. package/lib/types/src/client/panel/StoryboardTab.d.ts +0 -19
  122. package/lib/types/src/client/panel/StyleCard.d.ts +0 -8
  123. package/lib/types/src/client/panel/StyleGallery.d.ts +0 -1
  124. package/lib/types/src/client/panel/WorldTab.d.ts +0 -10
  125. package/lib/types/src/client/panel/controller.d.ts +0 -19
  126. package/lib/types/src/client/panel/helpers.d.ts +0 -18
  127. package/lib/types/src/client/panel/views.d.ts +0 -77
  128. package/lib/types/src/client/sidebar-entry.d.ts +0 -13
  129. package/lib/types/src/client/text.d.ts +0 -9
  130. package/lib/types/src/comic-presets.d.ts +0 -12
  131. package/lib/types/src/docx.d.ts +0 -19
  132. package/lib/types/src/engine.d.ts +0 -402
  133. package/lib/types/src/index.d.ts +0 -79
  134. package/lib/types/src/manga-genre-rules.d.ts +0 -22
  135. package/lib/types/src/novel-context.d.ts +0 -80
  136. package/lib/types/src/prompt-utils.d.ts +0 -35
  137. package/lib/types/src/protocol.d.ts +0 -2176
  138. package/lib/types/src/review-policy.d.ts +0 -97
  139. package/lib/types/src/routes.d.ts +0 -29
  140. package/lib/types/src/run.d.ts +0 -32
  141. package/lib/types/src/shot-language.d.ts +0 -46
  142. package/lib/types/src/story-beat-language.d.ts +0 -35
  143. package/lib/types/src/style-library.d.ts +0 -33
  144. package/lib/types/story-beat-language.d.ts +0 -35
  145. package/lib/types/style-library.d.ts +0 -33
  146. package/src/client/panel/BookshelfBar.tsx +0 -170
  147. package/src/client/panel/ProgressHomeView.tsx +0 -87
  148. package/src/prompt-utils.ts +0 -73
package/src/bookshelf.ts CHANGED
@@ -1,167 +1,187 @@
1
- /**
2
- * 书架(Bookshelf)— 多书管理:一本书记录一个独立输出目录。
3
- * 状态持久化到 ~/.dsh/dsh-novel-forge-bookshelf.json(跟随 dsh 配置惯例)。
4
- */
5
-
6
- import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs'
7
- import { homedir } from 'node:os'
8
- import { join } from 'node:path'
9
- import { randomBytes } from 'node:crypto'
10
- import type { BookEntry, BookshelfSnapshot } from './protocol.ts'
11
- import { loadProject } from './engine.ts'
12
-
13
- /** 书架配置文件路径。 */
14
- export function bookshelfFile(): string {
15
- return join(homedir(), '.dsh', 'dsh-novel-forge-bookshelf.json')
16
- }
17
-
18
- interface BookshelfStore {
19
- books: BookEntry[]
20
- activeBookId: string | null
21
- }
22
-
23
- function defaultStore(): BookshelfStore {
24
- return { books: [], activeBookId: null }
25
- }
26
-
27
- /** 读取书架(无则返回空)。 */
28
- export function loadBookshelf(): BookshelfStore {
29
- const file = bookshelfFile()
30
- if (!existsSync(file)) return defaultStore()
31
- try {
32
- let raw = readFileSync(file, 'utf8')
33
- if (raw.charCodeAt(0) === 0xFEFF) raw = raw.slice(1)
34
- const parsed = JSON.parse(raw) as BookshelfStore
35
- if (!Array.isArray(parsed.books)) return defaultStore()
36
- return { books: parsed.books, activeBookId: parsed.activeBookId ?? null }
37
- } catch {
38
- return defaultStore()
39
- }
40
- }
41
-
42
- /** 持久化书架。 */
43
- function saveBookshelf(store: BookshelfStore): void {
44
- const file = bookshelfFile()
45
- mkdirSync(join(homedir(), '.dsh'), { recursive: true })
46
- writeFileSync(file, JSON.stringify(store, null, 2), 'utf8')
47
- }
48
-
49
- /** 当前激活的书。 */
50
- export function activeBook(store: BookshelfStore): BookEntry | undefined {
51
- return store.books.find(b => b.id === store.activeBookId)
52
- }
53
-
54
- /** 书架快照(含每本书的进度摘要)。 */
55
- export function bookshelfSnapshot(store: BookshelfStore): BookshelfSnapshot {
56
- return {
57
- books: store.books.map(book => {
58
- const project = loadProject(book.outputDir)
59
- const done = project === undefined ? 0 : project.chapters.filter(c => c.status === 'approved' || c.status === 'written' || c.status === 'rejected').length
60
- const hasCover = project?.coverPath !== undefined && project.coverPath !== '' && existsSync(join(book.outputDir, project.coverPath))
61
- return {
62
- ...book,
63
- done,
64
- total: project?.chapters.length ?? 0,
65
- hasProject: project !== undefined,
66
- hasCover,
67
- blurb: project?.blurb,
68
- }
69
- }),
70
- activeBookId: store.activeBookId,
71
- }
72
- }
73
-
74
- /** 新建一本书(自动成为当前书)。 */
75
- export function createBook(bookName: string, outputDir: string): BookEntry {
76
- const store = loadBookshelf()
77
- const id = `book-${Date.now().toString(36)}-${randomBytes(3).toString('hex')}`
78
- const now = new Date().toISOString()
79
- const book: BookEntry = { id, bookName, outputDir, createdAt: now, updatedAt: now }
80
- store.books.push(book)
81
- store.activeBookId = id
82
- saveBookshelf(store)
83
- return book
84
- }
85
-
86
- /** 更新某本书的书名(开书向导导入大纲后书名以大纲首行为准)。 */
87
- export function renameBook(id: string, bookName: string): boolean {
88
- const store = loadBookshelf()
89
- const book = store.books.find(b => b.id === id)
90
- if (book === undefined) return false
91
- book.bookName = bookName
92
- book.updatedAt = new Date().toISOString()
93
- saveBookshelf(store)
94
- return true
95
- }
96
-
97
- /**
98
- * 播种:书架为空时,把指定输出目录下已有的项目自动登记为第一本书。
99
- * 兼容升级场景 —— 旧版插件直接在输出目录写项目,从未登记书架。
100
- * @param outputDir - 候选输出目录(通常为 settings 的默认输出目录)。
101
- * @returns 是否发生了播种。
102
- */
103
- export function seedBookshelfFromOutputDir(outputDir: string): boolean {
104
- const store = loadBookshelf()
105
- if (store.books.length > 0) return false
106
- if (!existsSync(outputDir)) return false
107
- // 有项目文件,或至少有章节文件,才视为"已有的书"。
108
- const hasProject = existsSync(join(outputDir, 'novel-project.json'))
109
- const hasChapters = existsSync(outputDir)
110
- if (!hasProject && !hasChapters) return false
111
- const project = loadProject(outputDir)
112
- const bookName = project?.bookName ?? outputDir.split(/[\\/]/).pop() ?? '未命名小说'
113
- createBook(bookName, outputDir)
114
- return true
115
- }
116
-
117
- /** 导入已有项目目录到书架:校验 novel-project.json,已存在则直接激活。 */
118
- export function importDir(outputDir: string): { book: BookEntry; existed: boolean } {
119
- const project = loadProject(outputDir)
120
- if (project === undefined) throw new Error(`目录中未找到有效的 novel-project.json:${outputDir}`)
121
- const store = loadBookshelf()
122
- const existed = store.books.find(b => b.outputDir === outputDir)
123
- if (existed !== undefined) {
124
- store.activeBookId = existed.id
125
- saveBookshelf(store)
126
- return { book: existed, existed: true }
127
- }
128
- const bookName = project.bookName !== '' ? project.bookName : (outputDir.split(/[\\/]/).pop() ?? '未命名小说')
129
- const book = createBook(bookName, outputDir)
130
- return { book, existed: false }
131
- }
132
-
133
- /** 激活一本书。 */
134
- export function activateBook(id: string): BookEntry | undefined {
135
- const store = loadBookshelf()
136
- const book = store.books.find(b => b.id === id)
137
- if (book === undefined) return undefined
138
- store.activeBookId = id
139
- book.updatedAt = new Date().toISOString()
140
- saveBookshelf(store)
141
- return book
142
- }
143
-
144
- /** 移除一本书。 */
145
- export function removeBook(id: string): boolean {
146
- const store = loadBookshelf()
147
- const idx = store.books.findIndex(b => b.id === id)
148
- if (idx === -1) return false
149
- store.books.splice(idx, 1)
150
- if (store.activeBookId === id) {
151
- store.activeBookId = store.books[0]?.id ?? null
152
- }
153
- saveBookshelf(store)
154
- return true
155
- }
156
-
157
- /** 当前书输出目录(无书架则 undefined,回退 settings)。 */
158
- export function activeBookOutputDir(): string | undefined {
159
- const book = activeBook(loadBookshelf())
160
- return book?.outputDir
161
- }
162
-
163
- /** 默认输出目录推断:~/.dsh/novels/书名。 */
164
- export function defaultOutputDirFor(bookName: string): string {
165
- const clean = bookName.replace(/[\\/:*?"<>|]/g, '').trim().slice(0, 40) || '未命名小说'
166
- return join(homedir(), '.dsh', 'novels', clean)
167
- }
1
+ /**
2
+ * 书架(Bookshelf)— 多书管理:一本书记录一个独立输出目录。
3
+ * 状态持久化到 ~/.dsh/dsh-novel-forge-bookshelf.json(跟随 dsh 配置惯例)。
4
+ */
5
+
6
+ import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs'
7
+ import { homedir } from 'node:os'
8
+ import { join } from 'node:path'
9
+ import { randomBytes } from 'node:crypto'
10
+ import type { BookEntry, BookshelfSnapshot } from './protocol.ts'
11
+ import { loadProject } from './engine.ts'
12
+
13
+ /** 书架配置文件路径。 */
14
+ export function bookshelfFile(): string {
15
+ return join(homedir(), '.dsh', 'dsh-novel-forge-bookshelf.json')
16
+ }
17
+
18
+ interface BookshelfStore {
19
+ books: BookEntry[]
20
+ activeBookId: string | null
21
+ }
22
+
23
+ function defaultStore(): BookshelfStore {
24
+ return { books: [], activeBookId: null }
25
+ }
26
+
27
+ /** 读取书架(无则返回空)。 */
28
+ export function loadBookshelf(): BookshelfStore {
29
+ const file = bookshelfFile()
30
+ if (!existsSync(file)) return defaultStore()
31
+ try {
32
+ let raw = readFileSync(file, 'utf8')
33
+ if (raw.charCodeAt(0) === 0xFEFF) raw = raw.slice(1)
34
+ const parsed = JSON.parse(raw) as BookshelfStore
35
+ if (!Array.isArray(parsed.books)) return defaultStore()
36
+ return { books: parsed.books, activeBookId: parsed.activeBookId ?? null }
37
+ } catch (error) {
38
+ // 书架文件损坏回退默认值会遮蔽数据丢失,留痕到服务端日志。
39
+ console.error(`[novel-forge] bookshelf file unreadable/corrupt: ${file} — ${(error as Error).message}`)
40
+ return defaultStore()
41
+ }
42
+ }
43
+
44
+ /** 持久化书架。 */
45
+ function saveBookshelf(store: BookshelfStore): void {
46
+ const file = bookshelfFile()
47
+ mkdirSync(join(homedir(), '.dsh'), { recursive: true })
48
+ writeFileSync(file, JSON.stringify(store, null, 2), 'utf8')
49
+ }
50
+
51
+ /** 当前激活的书。 */
52
+ export function activeBook(store: BookshelfStore): BookEntry | undefined {
53
+ return store.books.find(b => b.id === store.activeBookId)
54
+ }
55
+
56
+ /** 书架快照(含每本书的进度摘要)。 */
57
+ export function bookshelfSnapshot(store: BookshelfStore): BookshelfSnapshot {
58
+ return {
59
+ books: store.books.map(book => {
60
+ const project = loadProject(book.outputDir)
61
+ const done = project === undefined ? 0 : project.chapters.filter(c => c.status === 'approved' || c.status === 'written' || c.status === 'rejected').length
62
+ const hasCover = project?.coverPath !== undefined && project.coverPath !== '' && existsSync(join(book.outputDir, project.coverPath))
63
+ return {
64
+ ...book,
65
+ done,
66
+ total: project?.chapters.length ?? 0,
67
+ hasProject: project !== undefined,
68
+ hasCover,
69
+ blurb: project?.blurb,
70
+ }
71
+ }),
72
+ activeBookId: store.activeBookId,
73
+ }
74
+ }
75
+
76
+ /** 新建一本书(自动成为当前书)。 */
77
+ export function createBook(bookName: string, outputDir: string): BookEntry {
78
+ const store = loadBookshelf()
79
+ const id = `book-${Date.now().toString(36)}-${randomBytes(3).toString('hex')}`
80
+ const now = new Date().toISOString()
81
+ const book: BookEntry = { id, bookName, outputDir, createdAt: now, updatedAt: now }
82
+ store.books.push(book)
83
+ store.activeBookId = id
84
+ saveBookshelf(store)
85
+ return book
86
+ }
87
+
88
+ /** 更新某本书的书名(开书向导导入大纲后书名以大纲首行为准)。 */
89
+ export function renameBook(id: string, bookName: string): boolean {
90
+ const store = loadBookshelf()
91
+ const book = store.books.find(b => b.id === id)
92
+ if (book === undefined) return false
93
+ book.bookName = bookName
94
+ book.updatedAt = new Date().toISOString()
95
+ saveBookshelf(store)
96
+ return true
97
+ }
98
+
99
+ /**
100
+ * 播种:书架为空时,把指定输出目录下已有的项目自动登记为第一本书。
101
+ * 兼容升级场景 —— 旧版插件直接在输出目录写项目,从未登记书架。
102
+ * @param outputDir - 候选输出目录(通常为 settings 的默认输出目录)。
103
+ * @returns 是否发生了播种。
104
+ */
105
+ export function seedBookshelfFromOutputDir(outputDir: string): boolean {
106
+ const store = loadBookshelf()
107
+ if (store.books.length > 0) return false
108
+ if (!existsSync(outputDir)) return false
109
+ // 有项目文件,或至少有章节文件,才视为"已有的书"。
110
+ const hasProject = existsSync(join(outputDir, 'novel-project.json'))
111
+ const hasChapters = existsSync(outputDir)
112
+ if (!hasProject && !hasChapters) return false
113
+ const project = loadProject(outputDir)
114
+ const bookName = project?.bookName ?? outputDir.split(/[\\/]/).pop() ?? '未命名小说'
115
+ createBook(bookName, outputDir)
116
+ return true
117
+ }
118
+
119
+ /** 导入已有项目目录到书架:校验 novel-project.json,已存在则直接激活。 */
120
+ export function importDir(outputDir: string): { book: BookEntry; existed: boolean } {
121
+ const project = loadProject(outputDir)
122
+ if (project === undefined) throw new Error(`目录中未找到有效的 novel-project.json:${outputDir}`)
123
+ const store = loadBookshelf()
124
+ const existed = store.books.find(b => b.outputDir === outputDir)
125
+ if (existed !== undefined) {
126
+ store.activeBookId = existed.id
127
+ saveBookshelf(store)
128
+ return { book: existed, existed: true }
129
+ }
130
+ const bookName = project.bookName !== '' ? project.bookName : (outputDir.split(/[\\/]/).pop() ?? '未命名小说')
131
+ const book = createBook(bookName, outputDir)
132
+ return { book, existed: false }
133
+ }
134
+
135
+ /** 激活一本书。 */
136
+ export function activateBook(id: string): BookEntry | undefined {
137
+ const store = loadBookshelf()
138
+ const book = store.books.find(b => b.id === id)
139
+ if (book === undefined) return undefined
140
+ store.activeBookId = id
141
+ book.updatedAt = new Date().toISOString()
142
+ saveBookshelf(store)
143
+ return book
144
+ }
145
+
146
+ /** 移除一本书。 */
147
+ export function removeBook(id: string): boolean {
148
+ const store = loadBookshelf()
149
+ const idx = store.books.findIndex(b => b.id === id)
150
+ if (idx === -1) return false
151
+ store.books.splice(idx, 1)
152
+ if (store.activeBookId === id) {
153
+ store.activeBookId = store.books[0]?.id ?? null
154
+ }
155
+ saveBookshelf(store)
156
+ return true
157
+ }
158
+
159
+ /** 当前书输出目录(无书架则 undefined,回退 settings)。 */
160
+ export function activeBookOutputDir(): string | undefined {
161
+ const book = activeBook(loadBookshelf())
162
+ return book?.outputDir
163
+ }
164
+
165
+ /**
166
+ * 书目录重指向:把 outputDir 等于 from 的书条目全部改为 to(目录迁移联动)。
167
+ * @returns 重指向的条目数。
168
+ */
169
+ export function repointBookDirs(from: string, to: string): number {
170
+ const store = loadBookshelf()
171
+ let count = 0
172
+ for (const book of store.books) {
173
+ if (book.outputDir === from) {
174
+ book.outputDir = to
175
+ book.updatedAt = new Date().toISOString()
176
+ count += 1
177
+ }
178
+ }
179
+ if (count > 0) saveBookshelf(store)
180
+ return count
181
+ }
182
+
183
+ /** 默认输出目录推断:~/.dsh/novels/书名。 */
184
+ export function defaultOutputDirFor(bookName: string): string {
185
+ const clean = bookName.replace(/[\\/:*?"<>|]/g, '').trim().slice(0, 40) || '未命名小说'
186
+ return join(homedir(), '.dsh', 'novels', clean)
187
+ }
package/src/client/api.ts CHANGED
@@ -24,7 +24,6 @@ import {
24
24
  type JobFrame,
25
25
  type LoadOutlineResponse,
26
26
  type AdaptExecuteRequest,
27
- type AdaptExecuteResponse,
28
27
  type AdaptRewriteFrame,
29
28
  type AdaptMaterializeSaveRequest,
30
29
  type AdaptMaterializeSaveResponse,
@@ -168,6 +167,16 @@ export class NovelApi {
168
167
  return postJson<{ config: NovelConfig }>(NOVEL_API.config, patch)
169
168
  }
170
169
 
170
+ /** 输出目录迁移:dryRun=true 仅预览目录内容;确认时传 to 搬迁。 */
171
+ async moveOutputDir(to?: string, dryRun?: boolean): Promise<import('../protocol.ts').MoveOutputDirResponse> {
172
+ return postJson<import('../protocol.ts').MoveOutputDirResponse>(NOVEL_API.moveOutputDir, { to, dryRun })
173
+ }
174
+
175
+ /** 本书参数:不带 patch 读取;带 patch 合并保存(字段传 null = 回退全局)。 */
176
+ async bookSettings(patch?: Record<string, unknown>): Promise<import('../protocol.ts').BookSettingsResponse> {
177
+ return postJson<import('../protocol.ts').BookSettingsResponse>(NOVEL_API.bookSettings, patch === undefined ? {} : { patch })
178
+ }
179
+
171
180
  async openFolder(): Promise<void> {
172
181
  await fetch(NOVEL_API.openFolder, { method: 'POST', headers: { 'content-type': 'application/json' }, body: '{}' })
173
182
  }
@@ -362,7 +371,7 @@ export class NovelApi {
362
371
  return postJson<import('../protocol.ts').RolesResponse>(NOVEL_API.roles, req)
363
372
  }
364
373
 
365
- /** 小说简介:AI 生成/补全(partial 留空 = 全量),或手动保存。 */
374
+ /** 小说简介:生成/补全(partial 留空 = 全量),或手动保存。 */
366
375
  async blurb(action: 'generate' | 'save', text?: string, partial?: string): Promise<{ blurb: string }> {
367
376
  return postJson<{ blurb: string }>(NOVEL_API.blurb, { action, text, partial })
368
377
  }
@@ -384,7 +393,7 @@ export class NovelApi {
384
393
  return postJson<{ bookName: string }>(NOVEL_API.rename, { bookName })
385
394
  }
386
395
 
387
- /** 大世界:AI 提炼(generate)或手动保存(save)。 */
396
+ /** 大世界:提炼(generate)或手动保存(save)。 */
388
397
  async world(action: 'generate' | 'save', world?: import('../protocol.ts').WorldState): Promise<{ world: import('../protocol.ts').WorldState }> {
389
398
  return postJson<{ world: import('../protocol.ts').WorldState }>(NOVEL_API.world, { action, world })
390
399
  }
@@ -1,69 +1,37 @@
1
- /**
2
- * Browser-side docx outline extraction: a .docx is a zip whose
3
- * word/document.xml holds the body text in <w:t> runs inside <w:p> paragraphs.
4
- * Uses fflate (inlined into the client bundle) so the user can pick or drag a
5
- * docx without any server upload.
6
- *
7
- * Import from 'fflate/browser' (not 'fflate'): the default entry resolves to
8
- * the Node build (esm/index.mjs), which calls module.createRequire() for the
9
- * optional worker_threads path — inlining that into the browser bundle leaves
10
- * a bare require("module") the client-modules table cannot answer.
11
- */
12
-
13
- import { unzipSync, strFromU8 } from 'fflate/browser'
14
-
15
- /** Decode the handful of XML entities docx bodies actually use. */
16
- function decodeEntities(text: string): string {
17
- return text
18
- .replace(/&lt;/g, '<')
19
- .replace(/&gt;/g, '>')
20
- .replace(/&quot;/g, '"')
21
- .replace(/&apos;/g, "'")
22
- .replace(/&amp;/g, '&')
23
- .replace(/&nbsp;/g, ' ')
24
- }
25
-
26
- /** Extract plain text from a docx buffer: one line per <w:p> paragraph. */
27
- export function extractDocxTextFromBuffer(buffer: ArrayBuffer | Uint8Array): string {
28
- const bytes = buffer instanceof Uint8Array ? buffer : new Uint8Array(buffer)
29
- let files: ReturnType<typeof unzipSync>
30
- try {
31
- files = unzipSync(bytes)
32
- } catch (error) {
33
- throw new Error(`不是有效的 docx(zip 解压失败):${(error as Error).message}`)
34
- }
35
- const document = files['word/document.xml']
36
- if (document === undefined) {
37
- throw new Error('不是有效的 docx(缺少 word/document.xml)')
38
- }
39
- const xml = strFromU8(document)
40
-
41
- const paragraphs: string[] = []
42
- const parts = xml.split(/<w:p\b[^>]*>/)
43
- for (let i = 1; i < parts.length; i++) {
44
- const segment = parts[i]!
45
- const runs: string[] = []
46
- const runRe = /<w:t(?:\s[^>]*)?>([\s\S]*?)<\/w:t>|<w:tab\b[^>]*\/>|<w:br\b[^>]*\/>/g
47
- for (const match of segment.matchAll(runRe)) {
48
- if (match[0].startsWith('<w:tab')) {
49
- runs.push('\t')
50
- } else if (match[0].startsWith('<w:br')) {
51
- runs.push('\n')
52
- } else {
53
- runs.push(decodeEntities(match[1] ?? ''))
54
- }
55
- }
56
- paragraphs.push(runs.join('').replace(/\u00a0/g, ' ').trimEnd())
57
- }
58
-
59
- const text = paragraphs.join('\n').replace(/\n{3,}/g, '\n\n').trim()
60
- if (text.length === 0) {
61
- throw new Error('docx 中没有可提取的文本')
62
- }
63
- return text
64
- }
65
-
66
- /** Read a File as ArrayBuffer. */
67
- export function readFileAsArrayBuffer(file: File): Promise<ArrayBuffer> {
68
- return file.arrayBuffer()
69
- }
1
+ /**
2
+ * Browser-side docx outline extraction: a .docx is a zip whose
3
+ * word/document.xml holds the body text in <w:t> runs inside <w:p> paragraphs.
4
+ * Uses fflate (inlined into the client bundle) so the user can pick or drag a
5
+ * docx without any server upload.
6
+ *
7
+ * Import from 'fflate/browser' (not 'fflate'): the default entry resolves to
8
+ * the Node build (esm/index.mjs), which calls module.createRequire() for the
9
+ * optional worker_threads path — inlining that into the browser bundle leaves
10
+ * a bare require("module") the client-modules table cannot answer.
11
+ *
12
+ * XML 解析共享核心在 ../docx-core.ts(与服务端同源)。
13
+ */
14
+
15
+ import { unzipSync, strFromU8 } from 'fflate/browser'
16
+ import { extractDocxTextFromXml } from '../docx-core.ts'
17
+
18
+ /** Extract plain text from a docx buffer: one line per <w:p> paragraph. */
19
+ export function extractDocxTextFromBuffer(buffer: ArrayBuffer | Uint8Array): string {
20
+ const bytes = buffer instanceof Uint8Array ? buffer : new Uint8Array(buffer)
21
+ let files: ReturnType<typeof unzipSync>
22
+ try {
23
+ files = unzipSync(bytes)
24
+ } catch (error) {
25
+ throw new Error(`不是有效的 docx(zip 解压失败):${(error as Error).message}`)
26
+ }
27
+ const document = files['word/document.xml']
28
+ if (document === undefined) {
29
+ throw new Error('不是有效的 docx(缺少 word/document.xml)')
30
+ }
31
+ return extractDocxTextFromXml(strFromU8(document))
32
+ }
33
+
34
+ /** Read a File as ArrayBuffer. */
35
+ export function readFileAsArrayBuffer(file: File): Promise<ArrayBuffer> {
36
+ return file.arrayBuffer()
37
+ }