@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.
- package/README.md +231 -231
- package/lib/client.js +9118 -7818
- package/lib/index.js +316 -112
- package/lib/types/bookshelf.d.ts +5 -0
- package/lib/types/client/api.d.ts +6 -2
- package/lib/types/client/docx.d.ts +2 -0
- package/lib/types/client/locales.d.ts +5 -5
- package/lib/types/client/panel/AiTag.d.ts +1 -0
- package/lib/types/client/panel/AuthorHome.d.ts +11 -11
- package/lib/types/client/panel/CmdPalette.d.ts +12 -0
- package/lib/types/client/panel/LiveFeedLog.d.ts +3 -0
- package/lib/types/client/panel/NovelPanel.d.ts +5 -0
- package/lib/types/client/panel/PageHeader.d.ts +18 -0
- package/lib/types/client/panel/SettingsView.d.ts +12 -9
- package/lib/types/client/panel/ShelfView.d.ts +10 -1
- package/lib/types/client/panel/SubPage.d.ts +27 -0
- package/lib/types/client/panel/toast.d.ts +2 -0
- package/lib/types/client/panel/views.d.ts +25 -7
- package/lib/types/docx-core.d.ts +16 -0
- package/lib/types/docx.d.ts +2 -3
- package/lib/types/engine.d.ts +9 -9
- package/lib/types/index.d.ts +1 -1
- package/lib/types/migrate-output.d.ts +25 -0
- package/lib/types/prompting.d.ts +1 -26
- package/lib/types/protocol.d.ts +76 -31
- package/lib/types/routes.d.ts +4 -0
- package/lib/types/run.d.ts +2 -0
- package/package.json +120 -120
- package/src/assistant.ts +3 -3
- package/src/author-assets.ts +149 -147
- package/src/bookshelf.ts +187 -167
- package/src/client/api.ts +12 -3
- package/src/client/docx.ts +37 -69
- package/src/client/llmLive.ts +101 -101
- package/src/client/locales.ts +418 -418
- package/src/client/panel/AdaptModeView.tsx +535 -535
- package/src/client/panel/AiTag.tsx +7 -0
- package/src/client/panel/AssetsTab.tsx +448 -458
- package/src/client/panel/AssistantTab.tsx +35 -35
- package/src/client/panel/AuthorAssetsView.tsx +205 -207
- package/src/client/panel/AuthorHome.tsx +309 -234
- package/src/client/panel/BookAnalysisView.tsx +63 -60
- package/src/client/panel/CmdPalette.tsx +102 -0
- package/src/client/panel/CreateBookView.tsx +366 -371
- package/src/client/panel/DirectorView.tsx +23 -23
- package/src/client/panel/GlobalAssetLibraryView.tsx +168 -172
- package/src/client/panel/IdeaInspirationView.tsx +76 -71
- package/src/client/panel/ImportModal.tsx +302 -302
- package/src/client/panel/KnowledgeBaseView.tsx +125 -73
- package/src/client/panel/LiveFeedLog.tsx +67 -67
- package/src/client/panel/MarketRadarView.tsx +377 -374
- package/src/client/panel/ModelManager.tsx +205 -205
- package/src/client/panel/NovelPanel.tsx +2761 -2100
- package/src/client/panel/PageHeader.tsx +37 -0
- package/src/client/panel/ProgressConsole.tsx +63 -63
- package/src/client/panel/ReaderView.tsx +306 -306
- package/src/client/panel/RunPanel.tsx +223 -204
- package/src/client/panel/SettingsView.tsx +305 -265
- package/src/client/panel/ShelfView.tsx +266 -242
- package/src/client/panel/SubPage.tsx +42 -0
- package/src/client/panel/WorldTab.tsx +202 -202
- package/src/client/panel/panel.module.css +5764 -4693
- package/src/client/panel/toast.ts +51 -0
- package/src/client/panel/views.tsx +401 -346
- package/src/client/run-status.ts +143 -100
- package/src/docx-core.ts +53 -0
- package/src/docx.ts +44 -83
- package/src/engine.ts +15 -90
- package/src/index.ts +311 -298
- package/src/migrate-output.ts +76 -0
- package/src/novel-context.ts +286 -287
- package/src/prompting.ts +26 -159
- package/src/protocol.ts +79 -31
- package/src/routes.ts +123 -9
- package/src/run.ts +341 -338
- package/lib/types/client/panel/FlowGuide.d.ts +0 -22
- package/lib/types/client/panel/MangaRoleLibrary.d.ts +0 -20
- package/lib/types/client/panel/MangaWorkspace.d.ts +0 -12
- package/lib/types/client/panel/ModelPicker.d.ts +0 -11
- package/lib/types/client/panel/PropLibrary.d.ts +0 -8
- package/lib/types/client/panel/SceneLibrary.d.ts +0 -16
- package/lib/types/client/panel/StoryboardTab.d.ts +0 -19
- package/lib/types/client/panel/StyleCard.d.ts +0 -8
- package/lib/types/client/panel/StyleGallery.d.ts +0 -1
- package/lib/types/comic-presets.d.ts +0 -12
- package/lib/types/manga-genre-rules.d.ts +0 -22
- package/lib/types/shot-language.d.ts +0 -46
- package/lib/types/src/ai-scan.d.ts +0 -36
- package/lib/types/src/assets.d.ts +0 -37
- package/lib/types/src/assistant.d.ts +0 -45
- package/lib/types/src/author-assets.d.ts +0 -25
- package/lib/types/src/bookshelf.d.ts +0 -42
- package/lib/types/src/client/api.d.ts +0 -234
- package/lib/types/src/client/docx.d.ts +0 -15
- package/lib/types/src/client/index.d.ts +0 -14
- package/lib/types/src/client/locales.d.ts +0 -212
- package/lib/types/src/client/mount.d.ts +0 -9
- package/lib/types/src/client/panel/AdaptModeView.d.ts +0 -5
- package/lib/types/src/client/panel/AssetsTab.d.ts +0 -12
- package/lib/types/src/client/panel/AssistantTab.d.ts +0 -7
- package/lib/types/src/client/panel/AuthorAssetsView.d.ts +0 -4
- package/lib/types/src/client/panel/AuthorHome.d.ts +0 -32
- package/lib/types/src/client/panel/BookshelfBar.d.ts +0 -11
- package/lib/types/src/client/panel/CreateBookView.d.ts +0 -9
- package/lib/types/src/client/panel/FlowGuide.d.ts +0 -22
- package/lib/types/src/client/panel/GlobalAssetLibraryView.d.ts +0 -4
- package/lib/types/src/client/panel/ImportModal.d.ts +0 -7
- package/lib/types/src/client/panel/MangaRoleLibrary.d.ts +0 -20
- package/lib/types/src/client/panel/MangaWorkspace.d.ts +0 -12
- package/lib/types/src/client/panel/ModelManager.d.ts +0 -14
- package/lib/types/src/client/panel/NovelPanel.d.ts +0 -13
- package/lib/types/src/client/panel/ProgressConsole.d.ts +0 -17
- package/lib/types/src/client/panel/ProgressHomeView.d.ts +0 -5
- package/lib/types/src/client/panel/PropLibrary.d.ts +0 -8
- package/lib/types/src/client/panel/ReaderView.d.ts +0 -8
- package/lib/types/src/client/panel/ReasoningSection.d.ts +0 -10
- package/lib/types/src/client/panel/RunPanel.d.ts +0 -5
- package/lib/types/src/client/panel/SceneLibrary.d.ts +0 -16
- package/lib/types/src/client/panel/SettingsView.d.ts +0 -16
- package/lib/types/src/client/panel/ShelfView.d.ts +0 -15
- package/lib/types/src/client/panel/StoryboardTab.d.ts +0 -19
- package/lib/types/src/client/panel/StyleCard.d.ts +0 -8
- package/lib/types/src/client/panel/StyleGallery.d.ts +0 -1
- package/lib/types/src/client/panel/WorldTab.d.ts +0 -10
- package/lib/types/src/client/panel/controller.d.ts +0 -19
- package/lib/types/src/client/panel/helpers.d.ts +0 -18
- package/lib/types/src/client/panel/views.d.ts +0 -77
- package/lib/types/src/client/sidebar-entry.d.ts +0 -13
- package/lib/types/src/client/text.d.ts +0 -9
- package/lib/types/src/comic-presets.d.ts +0 -12
- package/lib/types/src/docx.d.ts +0 -19
- package/lib/types/src/engine.d.ts +0 -402
- package/lib/types/src/index.d.ts +0 -79
- package/lib/types/src/manga-genre-rules.d.ts +0 -22
- package/lib/types/src/novel-context.d.ts +0 -80
- package/lib/types/src/prompt-utils.d.ts +0 -35
- package/lib/types/src/protocol.d.ts +0 -2176
- package/lib/types/src/review-policy.d.ts +0 -97
- package/lib/types/src/routes.d.ts +0 -29
- package/lib/types/src/run.d.ts +0 -32
- package/lib/types/src/shot-language.d.ts +0 -46
- package/lib/types/src/story-beat-language.d.ts +0 -35
- package/lib/types/src/style-library.d.ts +0 -33
- package/lib/types/story-beat-language.d.ts +0 -35
- package/lib/types/style-library.d.ts +0 -33
- package/src/client/panel/BookshelfBar.tsx +0 -170
- package/src/client/panel/ProgressHomeView.tsx +0 -87
- 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
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
const
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
const
|
|
79
|
-
const
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
book
|
|
92
|
-
book
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
*
|
|
101
|
-
*
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
const
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
const
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
store.
|
|
139
|
-
book
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
store.books.
|
|
150
|
-
if (
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
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
|
-
/**
|
|
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
|
-
/**
|
|
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
|
}
|
package/src/client/docx.ts
CHANGED
|
@@ -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
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
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
|
+
}
|