@waterwx/dsh-novel-forge 1.0.0 → 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 (125) hide show
  1. package/README.md +231 -206
  2. package/lib/client.js +55052 -4393
  3. package/lib/index.js +12013 -2524
  4. package/lib/types/ai-scan.d.ts +36 -0
  5. package/lib/types/assets.d.ts +19 -4
  6. package/lib/types/assistant.d.ts +6 -0
  7. package/lib/types/author-assets.d.ts +25 -0
  8. package/lib/types/bookshelf.d.ts +11 -1
  9. package/lib/types/client/api.d.ts +210 -4
  10. package/lib/types/client/docx.d.ts +2 -0
  11. package/lib/types/client/index.d.ts +1 -1
  12. package/lib/types/client/llmLive.d.ts +33 -0
  13. package/lib/types/client/locales.d.ts +102 -29
  14. package/lib/types/client/panel/AdaptModeView.d.ts +5 -0
  15. package/lib/types/client/panel/AiTag.d.ts +1 -0
  16. package/lib/types/client/panel/AuthorAssetsView.d.ts +4 -0
  17. package/lib/types/client/panel/AuthorHome.d.ts +34 -0
  18. package/lib/types/client/panel/BookAnalysisView.d.ts +4 -0
  19. package/lib/types/client/panel/CmdPalette.d.ts +12 -0
  20. package/lib/types/client/panel/CreateBookView.d.ts +13 -0
  21. package/lib/types/client/panel/DirectorView.d.ts +7 -0
  22. package/lib/types/client/panel/GlobalAssetLibraryView.d.ts +4 -0
  23. package/lib/types/client/panel/IdeaInspirationView.d.ts +6 -0
  24. package/lib/types/client/panel/ImportModal.d.ts +7 -0
  25. package/lib/types/client/panel/KnowledgeBaseView.d.ts +4 -0
  26. package/lib/types/client/panel/LiveFeedLog.d.ts +8 -0
  27. package/lib/types/client/panel/MarketRadarView.d.ts +5 -0
  28. package/lib/types/client/panel/ModelManager.d.ts +14 -0
  29. package/lib/types/client/panel/NovelPanel.d.ts +6 -1
  30. package/lib/types/client/panel/PageHeader.d.ts +18 -0
  31. package/lib/types/client/panel/ProgressConsole.d.ts +17 -0
  32. package/lib/types/client/panel/ProgressHomeView.d.ts +5 -0
  33. package/lib/types/client/panel/ReaderView.d.ts +8 -0
  34. package/lib/types/client/panel/ReasoningSection.d.ts +10 -0
  35. package/lib/types/client/panel/RunPanel.d.ts +5 -0
  36. package/lib/types/client/panel/SettingsView.d.ts +19 -0
  37. package/lib/types/client/panel/ShelfView.d.ts +24 -0
  38. package/lib/types/client/panel/SubPage.d.ts +27 -0
  39. package/lib/types/client/panel/WorldTab.d.ts +10 -0
  40. package/lib/types/client/panel/helpers.d.ts +10 -0
  41. package/lib/types/client/panel/toast.d.ts +2 -0
  42. package/lib/types/client/panel/views.d.ts +95 -0
  43. package/lib/types/client/run-status.d.ts +19 -0
  44. package/lib/types/client/text.d.ts +9 -0
  45. package/lib/types/docx-core.d.ts +16 -0
  46. package/lib/types/docx.d.ts +2 -3
  47. package/lib/types/engine.d.ts +228 -5
  48. package/lib/types/global-assets.d.ts +6 -0
  49. package/lib/types/index.d.ts +24 -2
  50. package/lib/types/llm-live.d.ts +44 -0
  51. package/lib/types/market-radar-scan.d.ts +19 -0
  52. package/lib/types/market-radar-sources.d.ts +31 -0
  53. package/lib/types/migrate-output.d.ts +25 -0
  54. package/lib/types/novel-context.d.ts +80 -0
  55. package/lib/types/prompt-utils.d.ts +35 -0
  56. package/lib/types/prompting.d.ts +10 -0
  57. package/lib/types/protocol.d.ts +1418 -7
  58. package/lib/types/review-policy.d.ts +97 -0
  59. package/lib/types/routes.d.ts +4 -0
  60. package/lib/types/run.d.ts +34 -0
  61. package/package.json +120 -93
  62. package/scripts/patch-schemastery.mjs +47 -0
  63. package/src/ai-scan.ts +179 -0
  64. package/src/assets.ts +1774 -518
  65. package/src/assistant.ts +915 -568
  66. package/src/author-assets.ts +149 -0
  67. package/src/bookshelf.ts +187 -148
  68. package/src/client/api.ts +675 -289
  69. package/src/client/docx.ts +37 -69
  70. package/src/client/index.ts +3 -1
  71. package/src/client/llmLive.ts +101 -0
  72. package/src/client/locales.ts +418 -271
  73. package/src/client/mount.tsx +1 -1
  74. package/src/client/panel/AdaptModeView.tsx +535 -0
  75. package/src/client/panel/AiTag.tsx +7 -0
  76. package/src/client/panel/AssetsTab.tsx +448 -456
  77. package/src/client/panel/AssistantTab.tsx +339 -188
  78. package/src/client/panel/AuthorAssetsView.tsx +206 -0
  79. package/src/client/panel/AuthorHome.tsx +309 -0
  80. package/src/client/panel/BookAnalysisView.tsx +63 -0
  81. package/src/client/panel/CmdPalette.tsx +102 -0
  82. package/src/client/panel/CreateBookView.tsx +366 -0
  83. package/src/client/panel/DirectorView.tsx +200 -0
  84. package/src/client/panel/GlobalAssetLibraryView.tsx +168 -0
  85. package/src/client/panel/IdeaInspirationView.tsx +76 -0
  86. package/src/client/panel/ImportModal.tsx +303 -0
  87. package/src/client/panel/KnowledgeBaseView.tsx +125 -0
  88. package/src/client/panel/LiveFeedLog.tsx +67 -0
  89. package/src/client/panel/MarketRadarView.tsx +377 -0
  90. package/src/client/panel/ModelManager.tsx +205 -0
  91. package/src/client/panel/NovelPanel.tsx +5374 -2159
  92. package/src/client/panel/PageHeader.tsx +37 -0
  93. package/src/client/panel/ProgressConsole.tsx +63 -0
  94. package/src/client/panel/ReaderView.tsx +306 -0
  95. package/src/client/panel/ReasoningSection.tsx +31 -0
  96. package/src/client/panel/RunPanel.tsx +223 -0
  97. package/src/client/panel/SettingsView.tsx +305 -0
  98. package/src/client/panel/ShelfView.tsx +266 -0
  99. package/src/client/panel/SubPage.tsx +42 -0
  100. package/src/client/panel/WorldTab.tsx +202 -0
  101. package/src/client/panel/helpers.ts +50 -0
  102. package/src/client/panel/panel.module.css +5764 -1698
  103. package/src/client/panel/toast.ts +51 -0
  104. package/src/client/panel/views.tsx +401 -0
  105. package/src/client/run-status.ts +143 -0
  106. package/src/client/sidebar-entry.ts +1 -1
  107. package/src/client/text.ts +28 -0
  108. package/src/docx-core.ts +53 -0
  109. package/src/docx.ts +44 -83
  110. package/src/engine.ts +4155 -1233
  111. package/src/global-assets.ts +59 -0
  112. package/src/index.ts +311 -184
  113. package/src/llm-live.ts +68 -0
  114. package/src/market-radar-scan.ts +41 -0
  115. package/src/market-radar-sources.ts +173 -0
  116. package/src/migrate-output.ts +76 -0
  117. package/src/novel-context.ts +286 -0
  118. package/src/prompting.ts +26 -0
  119. package/src/protocol.ts +2046 -623
  120. package/src/review-policy.ts +161 -0
  121. package/src/routes.ts +3391 -1172
  122. package/src/run.ts +341 -0
  123. package/lib/client.js.map +0 -1
  124. package/lib/index.js.map +0 -1
  125. package/src/client/panel/BookshelfBar.tsx +0 -170
@@ -0,0 +1,102 @@
1
+ /**
2
+ * CmdPalette — 全局命令面板(⌘K / Ctrl+K,R3 微交互)。
3
+ *
4
+ * 纯受控组件:open/onClose 由 NovelPanel 管理,actions 由 NovelPanel 组装
5
+ * (Tab 跳转 / 浮窗开关 / 设置直达)。键盘:↑↓ 选择、Enter 执行、Esc 关闭。
6
+ * 样式走 panel.module.css 的 .cmd* 家族,完全跟随当前主题令牌。
7
+ */
8
+ import { useEffect, useMemo, useRef, useState } from 'react'
9
+ import type { ReactElement } from 'react'
10
+ import css from './panel.module.css'
11
+
12
+ export interface CmdAction {
13
+ icon: string
14
+ label: string
15
+ hint?: string
16
+ run: () => void
17
+ }
18
+
19
+ export function CmdPalette({ open, onClose, actions }: {
20
+ open: boolean
21
+ onClose: () => void
22
+ actions: CmdAction[]
23
+ }): ReactElement | null {
24
+ const [query, setQuery] = useState('')
25
+ const [active, setActive] = useState(0)
26
+ const inputRef = useRef<HTMLInputElement | null>(null)
27
+ const listRef = useRef<HTMLUListElement | null>(null)
28
+
29
+ const filtered = useMemo(() => {
30
+ const q = query.trim().toLowerCase()
31
+ if (q === '') return actions
32
+ return actions.filter(a => a.label.toLowerCase().includes(q) || (a.hint ?? '').toLowerCase().includes(q))
33
+ }, [actions, query])
34
+
35
+ // 打开时重置并聚焦;query/结果变化时钳制选中项
36
+ useEffect(() => {
37
+ if (open) {
38
+ setQuery('')
39
+ setActive(0)
40
+ window.setTimeout(() => inputRef.current?.focus(), 30)
41
+ }
42
+ }, [open])
43
+ useEffect(() => { setActive(0) }, [query])
44
+ useEffect(() => {
45
+ if (active >= filtered.length) setActive(Math.max(0, filtered.length - 1))
46
+ }, [filtered, active])
47
+
48
+ if (!open) return null
49
+
50
+ const execute = (a: CmdAction | undefined): void => {
51
+ if (a === undefined) return
52
+ onClose()
53
+ a.run()
54
+ }
55
+
56
+ const onKeyDown = (e: React.KeyboardEvent): void => {
57
+ if (e.key === 'Escape') { e.preventDefault(); onClose(); return }
58
+ if (e.key === 'ArrowDown') { e.preventDefault(); setActive(i => Math.min(i + 1, filtered.length - 1)); return }
59
+ if (e.key === 'ArrowUp') { e.preventDefault(); setActive(i => Math.max(i - 1, 0)); return }
60
+ if (e.key === 'Enter') { e.preventDefault(); execute(filtered[active]); return }
61
+ }
62
+
63
+ return (
64
+ <div
65
+ className={css.cmdMask}
66
+ onMouseDown={e => { if (e.target === e.currentTarget) onClose() }}
67
+ onKeyDown={onKeyDown}
68
+ >
69
+ <div className={css.cmdBox} role="dialog" aria-modal="true" aria-label="命令面板">
70
+ <input
71
+ ref={inputRef}
72
+ className={css.cmdInput}
73
+ value={query}
74
+ placeholder="搜索命令:跳转、生成、审稿、设置……"
75
+ onChange={e => setQuery(e.target.value)}
76
+ spellCheck={false}
77
+ />
78
+ {filtered.length === 0 ? (
79
+ <div className={css.cmdEmpty}>没有匹配的命令</div>
80
+ ) : (
81
+ <ul className={css.cmdList} ref={listRef} role="listbox">
82
+ {filtered.map((a, i) => (
83
+ <li
84
+ key={a.label}
85
+ className={css.cmdItem}
86
+ data-active={i === active ? '' : undefined}
87
+ role="option"
88
+ aria-selected={i === active}
89
+ onMouseMove={() => setActive(i)}
90
+ onClick={() => execute(a)}
91
+ >
92
+ <span className={css.cmdIcon}>{a.icon}</span>
93
+ <b>{a.label}</b>
94
+ {a.hint !== undefined && <span className={css.cmdHint}>{a.hint}</span>}
95
+ </li>
96
+ ))}
97
+ </ul>
98
+ )}
99
+ </div>
100
+ </div>
101
+ )
102
+ }
@@ -0,0 +1,366 @@
1
+ /**
2
+ * 开书向导:独立页面视图 —— 书名 + 大纲(选择 docx / 拖拽 / 粘贴),
3
+ * 实时书名识别与题材提示,开书即建项目并进入工作台。
4
+ * 另含「想法 → AI 大纲」:输入一句话想法生成 2-3 个方案,可暂留换批,选中后回填大纲框。
5
+ */
6
+ import { useRef, useState } from 'react'
7
+ import type { NovelApi } from '../api.ts'
8
+ import { setCurrentBook } from '../api.ts'
9
+ import type { OutlineCandidate } from '../../protocol.ts'
10
+ import { extractDocxTextFromBuffer } from '../docx.ts'
11
+ import css from './panel.module.css'
12
+ import { SubPage } from './SubPage.tsx'
13
+
14
+ /** 从大纲首行推断书名(与服务端 inferBookName 一致,供实时预览)。 */
15
+ function inferBookNamePreview(outline: string): string {
16
+ const line = outline.split('\n').map(l => l.trim()).find(l => l.length > 0)
17
+ if (line === undefined) return ''
18
+ return line.replace(/^《/, '').replace(/》.*$/, '').slice(0, 40)
19
+ }
20
+
21
+ /** 简单题材识别(提示用)。 */
22
+ function guessGenre(outline: string): string | null {
23
+ const map: Array<[string, string[]]> = [
24
+ ['仙侠修真', ['仙', '修', '灵根', '元婴', '宗门', '飞升']],
25
+ ['都市', ['都市', '公司', '外卖', '职场', '总裁']],
26
+ ['玄幻', ['斗气', '魂力', '大陆', '斗罗', '神']],
27
+ ['悬疑', ['悬疑', '密室', '案件', '推理', '凶']],
28
+ ['科幻', ['机甲', '星舰', 'AI', '未来', '星际']],
29
+ ['历史', ['朝代', '皇帝', '将军', '古代', '王朝']],
30
+ ['游戏', ['游戏', '副本', '装备', '等级', '职业']],
31
+ ]
32
+ for (const [genre, keywords] of map) {
33
+ if (keywords.some(k => outline.includes(k))) return genre
34
+ }
35
+ return null
36
+ }
37
+
38
+ /** 开书向导页。 */
39
+ export function CreateBookView({
40
+ api,
41
+ onBack,
42
+ onCreated,
43
+ initialIdea,
44
+ initialName,
45
+ }: {
46
+ api: NovelApi
47
+ /** 返回书架。 */
48
+ onBack: () => void
49
+ /** 开书成功:进入新书工作台。 */
50
+ onCreated: (id: string) => void
51
+ /** 从「创意灵感」带过来的起步想法(预填到「一句话想法」输入框)。 */
52
+ initialIdea?: string
53
+ /** 从「创意灵感」带过来的初始书名。 */
54
+ initialName?: string
55
+ }) {
56
+ const [name, setName] = useState(initialName ?? '')
57
+ const [outlineText, setOutlineText] = useState('')
58
+ const [outlineName, setOutlineName] = useState('')
59
+ const [dragActive, setDragActive] = useState(false)
60
+ const [busy, setBusy] = useState(false)
61
+ const [error, setError] = useState('')
62
+ const outlineFileRef = useRef<HTMLInputElement | null>(null)
63
+
64
+ // ---- 想法 → AI 大纲状态 ----
65
+ const hasPrefill = initialIdea !== undefined && initialIdea.trim() !== ''
66
+ const [ideaOpen, setIdeaOpen] = useState(hasPrefill)
67
+ const [idea, setIdea] = useState(initialIdea ?? '')
68
+ const [suggesting, setSuggesting] = useState(false)
69
+ const [candidates, setCandidates] = useState<OutlineCandidate[]>([])
70
+ const [pinned, setPinned] = useState<string[]>([])
71
+ /** 回填后提示(如「已填入方案A,可继续修改」)。 */
72
+ const [fillNotice, setFillNotice] = useState('')
73
+ /** 选定方案自带的题材(创建后自动预填到本书创作资产)。 */
74
+ const [selectedGenre, setSelectedGenre] = useState('')
75
+
76
+ const autoName = inferBookNamePreview(outlineText)
77
+ const effectiveName = name.trim() !== '' ? name.trim() : autoName
78
+ const genre = guessGenre(outlineText)
79
+
80
+ const handlePickOutlineFile = async (file: File | undefined): Promise<void> => {
81
+ if (file === undefined) return
82
+ try {
83
+ const buffer = await file.arrayBuffer()
84
+ const text = extractDocxTextFromBuffer(buffer)
85
+ if (text.length < 50) {
86
+ setError('大纲内容过短(<50 字符),请检查文件')
87
+ return
88
+ }
89
+ setOutlineText(text)
90
+ setOutlineName(file.name)
91
+ if (name.trim() === '') setName(inferBookNamePreview(text))
92
+ setError('')
93
+ } catch (err) {
94
+ setError(`读取大纲失败:${(err as Error).message}`)
95
+ }
96
+ }
97
+
98
+ /** 生成/换批:只补未暂留的空槽;exclude 传已暂留方案的卖点方向。 */
99
+ const handleSuggest = async (): Promise<void> => {
100
+ if (idea.trim().length < 10) {
101
+ setError('想法太短(<10 字):至少写一句完整想法,如「男主穿越修仙界靠做菜无敌」')
102
+ return
103
+ }
104
+ setSuggesting(true)
105
+ setError('')
106
+ try {
107
+ const exclude = candidates
108
+ .filter(c => pinned.includes(c.id))
109
+ .map(c => `${c.bookName}:${c.sellingPoint}${c.genre !== '' ? `(${c.genre})` : ''}`)
110
+ const count = Math.max(1, 3 - pinned.length)
111
+ const result = await api.outlineSuggest(idea.trim(), count, exclude)
112
+ // 换批合并:已暂留的原地保留,新生成的补充进空槽。
113
+ setCandidates(prev => {
114
+ const kept = prev.filter(c => pinned.includes(c.id))
115
+ return [...kept, ...result.candidates].slice(0, 3)
116
+ })
117
+ setFillNotice('')
118
+ } catch (err) {
119
+ setError((err as Error).message)
120
+ } finally {
121
+ setSuggesting(false)
122
+ }
123
+ }
124
+
125
+ /** 暂留/取消暂留。 */
126
+ const togglePin = (id: string): void => {
127
+ setPinned(prev => prev.includes(id) ? prev.filter(x => x !== id) : [...prev, id])
128
+ }
129
+
130
+ /** 选中方案:回填大纲框 + 书名,滚动到上方。 */
131
+ const handlePick = (candidate: OutlineCandidate): void => {
132
+ setOutlineText(candidate.outline)
133
+ setName(candidate.bookName)
134
+ setSelectedGenre(candidate.genre ?? '')
135
+ setFillNotice(`已填入方案《${candidate.bookName}》(${candidate.genre}),可继续修改后开书`)
136
+ setIdeaOpen(false)
137
+ window.scrollTo({ top: 0, behavior: 'smooth' })
138
+ }
139
+
140
+ const handleCreate = async (): Promise<void> => {
141
+ if (effectiveName === '') {
142
+ setError('请填写书名,或提供大纲自动识别')
143
+ return
144
+ }
145
+ setBusy(true)
146
+ setError('')
147
+ try {
148
+ const snapshot = await api.bookCreate(effectiveName, undefined, outlineText.trim() !== '' ? outlineText.trim() : undefined)
149
+ const created = snapshot.books.find(b => b.id === snapshot.activeBookId)
150
+ if (created !== undefined) {
151
+ // 选定方案自带题材,或从大纲关键词识别到题材 → 创建后自动预填到本书创作资产(失败不阻断开书)。
152
+ const detectedGenre = selectedGenre.trim() !== '' ? selectedGenre.trim() : (guessGenre(outlineText) ?? '')
153
+ if (detectedGenre !== '') {
154
+ try {
155
+ setCurrentBook(created.id)
156
+ await api.patchAssets({ genre: { name: detectedGenre, description: '', children: [] } })
157
+ } catch { /* ignore */ }
158
+ }
159
+ onCreated(created.id)
160
+ } else {
161
+ setError('开书失败:未找到新书')
162
+ }
163
+ } catch (err) {
164
+ setError((err as Error).message)
165
+ } finally {
166
+ setBusy(false)
167
+ }
168
+ }
169
+
170
+ return (
171
+ <SubPage
172
+ title="开书向导"
173
+ meta="把一份大纲「编译」成一本完整的小说"
174
+ back={{ label: '书架', onClick: onBack }}
175
+ >
176
+ <div className={css.createBookInner}>
177
+ {error !== '' && (
178
+ <div className={css.card} style={{ borderColor: 'var(--nf-error)', padding: 'var(--nf-space-8) var(--nf-space-12)' }}>
179
+ <span style={{ color: 'var(--nf-error)', fontSize: 'var(--nf-fs-12)' }}>{error}</span>
180
+ </div>
181
+ )}
182
+
183
+ {fillNotice !== '' && (
184
+ <div className={css.card} style={{ borderColor: 'var(--nf-success)', padding: 'var(--nf-space-8) var(--nf-space-12)' }}>
185
+ <span style={{ color: 'var(--nf-success)', fontSize: 'var(--nf-fs-12)' }}> {fillNotice}</span>
186
+ </div>
187
+ )}
188
+
189
+ <div className={css.field}>
190
+ <label className={css.fieldLabel}>书名</label>
191
+ <input
192
+ className={css.input}
193
+ placeholder={autoName !== '' ? `自动识别:${autoName}` : '输入书名(提供大纲后自动识别)'}
194
+ value={name}
195
+ onChange={e => { setName(e.target.value) }}
196
+ onKeyDown={e => { if (e.key === 'Enter') void handleCreate() }}
197
+ autoFocus
198
+ />
199
+ </div>
200
+
201
+ <div className={css.field}>
202
+ <label className={css.fieldLabel}>大纲</label>
203
+ <div
204
+ className={`${css.dropzone} ${dragActive ? css.dropzoneActive : ''}`}
205
+ role="button"
206
+ tabIndex={0}
207
+ aria-label="选择或拖入 docx 大纲文件"
208
+ onClick={() => { outlineFileRef.current?.click() }}
209
+ onKeyDown={e => { if (e.key === 'Enter' || e.key === ' ') { e.preventDefault(); outlineFileRef.current?.click() } }}
210
+ onDragOver={e => { e.preventDefault(); setDragActive(true) }}
211
+ onDragLeave={() => { setDragActive(false) }}
212
+ onDrop={e => {
213
+ e.preventDefault()
214
+ setDragActive(false)
215
+ void handlePickOutlineFile(e.dataTransfer.files?.[0])
216
+ }}
217
+ >
218
+ <span className={css.dropzoneIcon}></span>
219
+ <span>{outlineName !== '' ? `已选择:${outlineName}` : '点击选择 docx 大纲,或将文件拖到这里'}</span>
220
+ <span className={css.meta}>推荐提供大纲:开书即建立项目,书名自动识别</span>
221
+ <input
222
+ ref={outlineFileRef}
223
+ type="file"
224
+ accept=".docx,application/vnd.openxmlformats-officedocument.wordprocessingml.document"
225
+ style={{ display: 'none' }}
226
+ onChange={e => {
227
+ void handlePickOutlineFile(e.target.files?.[0])
228
+ e.target.value = ''
229
+ }}
230
+ />
231
+ </div>
232
+ <textarea
233
+ className={css.textarea}
234
+ style={{ minHeight: 130 }}
235
+ placeholder="或直接粘贴大纲文本(50 字以上)…"
236
+ value={outlineText}
237
+ onChange={e => { setOutlineText(e.target.value) }}
238
+ spellCheck={false}
239
+ />
240
+ </div>
241
+
242
+ {(outlineText.trim().length > 0 || effectiveName !== '') && (
243
+ <div className={css.row} style={{ flexWrap: 'wrap' }}>
244
+ {outlineText.trim().length > 0 && <span className={css.meta}>大纲 {outlineText.length} 字</span>}
245
+ {effectiveName !== '' && <span className={css.meta}>书名:{effectiveName}</span>}
246
+ {genre !== null && <span className={css.meta}>题材:{genre}</span>}
247
+ </div>
248
+ )}
249
+
250
+ {/* 想法 → AI 大纲(可折叠) */}
251
+ <div className={css.ideaCard}>
252
+ <button
253
+ type="button"
254
+ className={css.ideaToggle}
255
+ onClick={() => { setIdeaOpen(v => !v) }}
256
+ aria-expanded={ideaOpen}
257
+ >
258
+ {ideaOpen ? '▾' : '▸'} 没有大纲?用一句话想法让 生成
259
+ </button>
260
+ {ideaOpen && (
261
+ <div style={{ display: 'flex', flexDirection: 'column', gap: 'var(--nf-space-8)' }}>
262
+ <textarea
263
+ className={css.textarea}
264
+ style={{ minHeight: 60 }}
265
+ placeholder="例如:现代外卖员被雷劈穿越到修仙世界,靠祖传古玉捡漏发育,苟着苟着成了大佬…(≥50 字)"
266
+ value={idea}
267
+ onChange={e => { setIdea(e.target.value) }}
268
+ spellCheck={false}
269
+ />
270
+ <div className={css.row} style={{ flexWrap: 'wrap' }}>
271
+ <button
272
+ type="button"
273
+ className={`${css.button} ${css.buttonSmall} ${css.buttonPrimary}`}
274
+ disabled={suggesting || idea.trim().length < 10}
275
+ onClick={() => { void handleSuggest() }}
276
+ title="生成 3 个方向不同的大纲方案供选择(约消耗 6-8k token)"
277
+ >
278
+ {suggesting ? '⏳ 生成中…' : candidates.length === 0 ? ' 生成大纲方案' : `↻ 换一批(${Math.max(1, 3 - pinned.length)} 个)`}
279
+ </button>
280
+ {candidates.length > 0 && (
281
+ <span className={css.meta}>
282
+ 已暂留 {pinned.length}/3 · 换批保留已暂留,只补新方案
283
+ </span>
284
+ )}
285
+ </div>
286
+
287
+ {/* 方案卡 */}
288
+ {candidates.length > 0 && (
289
+ <div style={{ display: 'flex', flexDirection: 'column', gap: 'var(--nf-space-8)' }}>
290
+ {candidates.map(candidate => {
291
+ const isPinned = pinned.includes(candidate.id)
292
+ return (
293
+ <div
294
+ key={candidate.id}
295
+ className={css.ideaCandidate}
296
+ style={isPinned ? { borderColor: 'var(--nf-accent)', boxShadow: '0 0 0 2px var(--nf-accent-soft)' } : undefined}
297
+ >
298
+ <div className={css.row} style={{ justifyContent: 'space-between', flexWrap: 'wrap', gap: 'var(--nf-space-6)' }}>
299
+ <span style={{ display: 'flex', alignItems: 'center', gap: 'var(--nf-space-8)', flexWrap: 'wrap' }}>
300
+ <b>《{candidate.bookName}》</b>
301
+ {candidate.genre !== '' && <span className={css.badge} style={{ borderColor: 'var(--nf-accent)', color: 'var(--nf-accent)' }}>{candidate.genre}</span>}
302
+ {isPinned && <span className={css.badge} style={{ borderColor: 'var(--nf-warn)', color: 'var(--nf-warn)' }}>★ 已暂留</span>}
303
+ </span>
304
+ <span style={{ display: 'flex', gap: 'var(--nf-space-6)' }}>
305
+ <button
306
+ type="button"
307
+ className={`${css.button} ${css.buttonSmall} ${isPinned ? '' : css.buttonPrimary}`}
308
+ onClick={() => { togglePin(candidate.id) }}
309
+ title={isPinned ? '取消暂留:下次换批会覆盖此槽位' : '暂留此方案:换批时保留,继续对比新方案'}
310
+ >
311
+ {isPinned ? '★ 取消暂留' : '☆ 暂留'}
312
+ </button>
313
+ <button
314
+ type="button"
315
+ className={`${css.button} ${css.buttonSmall} ${css.buttonPrimary}`}
316
+ onClick={() => { handlePick(candidate) }}
317
+ title="将这份大纲填入上方大纲框(可继续修改后开书)"
318
+ >
319
+ 选这个
320
+ </button>
321
+ </span>
322
+ </div>
323
+ {candidate.sellingPoint !== '' && (
324
+ <div className={css.meta}><b>卖点:</b>{candidate.sellingPoint}</div>
325
+ )}
326
+ <div
327
+ className={css.meta}
328
+ style={{
329
+ display: '-webkit-box',
330
+ WebkitLineClamp: 4,
331
+ WebkitBoxOrient: 'vertical',
332
+ overflow: 'hidden',
333
+ whiteSpace: 'pre-wrap',
334
+ }}
335
+ title={candidate.outline}
336
+ >
337
+ {candidate.outline}
338
+ </div>
339
+ </div>
340
+ )
341
+ })}
342
+ {pinned.length >= 3 && (
343
+ <span className={css.meta}>已暂留全部方案——直接挑一个「选这个」即可;换批需先取消某个暂留。</span>
344
+ )}
345
+ </div>
346
+ )}
347
+ </div>
348
+ )}
349
+ </div>
350
+
351
+ <button
352
+ type="button"
353
+ className={`${css.button} ${css.buttonPrimary}`}
354
+ style={{ width: '100%', padding: 'var(--nf-space-10) 0', fontSize: 'var(--nf-fs-14)' }}
355
+ disabled={busy || effectiveName === ''}
356
+ onClick={() => { void handleCreate() }}
357
+ >
358
+ 开书并进入工作台
359
+ </button>
360
+ <span className={css.meta} style={{ textAlign: 'center' }}>
361
+ 未提供大纲也能开书,稍后可在大纲页导入
362
+ </span>
363
+ </div>
364
+ </SubPage>
365
+ )
366
+ }
@@ -0,0 +1,200 @@
1
+ /**
2
+ * 自动编辑:基于全书上下文,给出下一卷/阶段编排建议 + 修复再平衡。
3
+ * 生成后提供「傻瓜式」后续:一条条采纳 → 加入剧情线 / 加入待办,一键复制。
4
+ */
5
+ import { useEffect, useState } from 'react'
6
+ import type { NovelApi } from '../api.ts'
7
+ import type { DirectorAdvice, DirectorTodo } from '../../protocol.ts'
8
+ import css from './panel.module.css'
9
+
10
+ export default function DirectorView({ api, todos, onTodosChange }: { api: NovelApi; todos: DirectorTodo[]; onTodosChange?: (todos: DirectorTodo[]) => void }): JSX.Element {
11
+ const [focus, setFocus] = useState('')
12
+ const [busy, setBusy] = useState(false)
13
+ const [error, setError] = useState('')
14
+ const [result, setResult] = useState<DirectorAdvice | null>(null)
15
+ const [adopted, setAdopted] = useState<Set<string>>(new Set())
16
+ const [todosBusy, setTodosBusy] = useState(false)
17
+
18
+ const refreshTodos = async (): Promise<void> => {
19
+ try {
20
+ const r = await api.directorTodosList()
21
+ onTodosChange?.(r.todos)
22
+ } catch { /* ignore */ }
23
+ }
24
+
25
+ useEffect(() => { void refreshTodos() }, [])
26
+
27
+ const markAdopted = (key: string): void => {
28
+ setAdopted(prev => new Set(prev).add(key))
29
+ }
30
+
31
+ const run = async (): Promise<void> => {
32
+ setBusy(true); setError('')
33
+ try {
34
+ const r = await api.director(focus.trim())
35
+ setResult(r.result)
36
+ void refreshTodos()
37
+ } catch (e) { setError((e as Error).message) } finally { setBusy(false) }
38
+ }
39
+
40
+ const adoptArc = async (s: string): Promise<void> => {
41
+ try {
42
+ await api.plotlines({ op: 'add', line: { id: '', name: s.slice(0, 16), kind: 'branch', goal: s, progress: '', status: 'active', chapters: [], createdAt: '' } })
43
+ markAdopted('arc:' + s)
44
+ } catch (e) { setError('加入剧情线失败:' + ((e as Error).message)) }
45
+ }
46
+
47
+ const adoptTodo = async (s: string, source: 'risk' | 'fix'): Promise<void> => {
48
+ try {
49
+ const r = await api.directorTodosAdd(s, source)
50
+ onTodosChange?.(r.todos)
51
+ markAdopted('todo:' + source + ':' + s)
52
+ } catch (e) { setError('加入待办失败:' + ((e as Error).message)) }
53
+ }
54
+
55
+ const toggleTodo = async (id: string): Promise<void> => {
56
+ setTodosBusy(true)
57
+ try {
58
+ const r = await api.directorTodosToggle(id)
59
+ onTodosChange?.(r.todos)
60
+ } catch (e) { setError((e as Error).message) } finally { setTodosBusy(false) }
61
+ }
62
+
63
+ const removeTodo = async (id: string): Promise<void> => {
64
+ setTodosBusy(true)
65
+ try {
66
+ const r = await api.directorTodosRemove(id)
67
+ onTodosChange?.(r.todos)
68
+ } catch (e) { setError((e as Error).message) } finally { setTodosBusy(false) }
69
+ }
70
+
71
+ const copyAll = async (): Promise<void> => {
72
+ if (result === null) return
73
+ const text = [
74
+ '【自动编辑】',
75
+ `总体判断:${result.summary}`,
76
+ '',
77
+ '下一阶段节点:',
78
+ ...(result.nextArc ?? []).map(s => `- ${s}`),
79
+ '',
80
+ `节奏板:${result.pacing}`,
81
+ '',
82
+ '风险提示:',
83
+ ...(result.risks ?? []).map(s => `- ${s}`),
84
+ '',
85
+ '需要修复/再平衡:',
86
+ ...(result.fixes ?? []).map(s => `- ${s}`),
87
+ ].join('\n')
88
+ try { await navigator.clipboard.writeText(text); setError('') } catch (e) { setError('复制失败:' + ((e as Error).message)) }
89
+ }
90
+
91
+ return (
92
+ <div className={css.authorPageBodyFlow}>
93
+ <div className={`${css.card} ${css.settingsCard}`} style={{ gap: 'var(--nf-space-10)' }}>
94
+ <span className={css.cardTitle}> 自动编辑</span>
95
+ <span className={css.meta}>结合本书分卷/剧情线/伏笔/最近事实,给出下一阶段编排 + 修复再平衡。可什么都不填直接生成。</span>
96
+ <textarea className={css.input} style={{ minHeight: 90, resize: 'vertical' }} value={focus} onChange={e => setFocus(e.target.value)} placeholder="可选:聚焦某个方向,如「主角成长节奏」「反派压迫感」「感情线处理」「下一卷转折」…" />
97
+ <div style={{ display: 'flex', alignItems: 'center', gap: 'var(--nf-space-8)', flexWrap: 'wrap' }}>
98
+ <button type="button" className={`${css.button} ${css.buttonPrimary}`} disabled={busy} onClick={() => { void run() }}>
99
+ {busy ? '编排中…' : '生成编排建议'}
100
+ </button>
101
+ {result !== null && (
102
+ <button type="button" className={css.button} onClick={() => { void copyAll() }}> 一键复制</button>
103
+ )}
104
+ </div>
105
+ </div>
106
+
107
+ {error !== '' && <div style={{ color: 'var(--nf-error)', fontSize: 'var(--nf-fs-14)' }}> {error}</div>}
108
+
109
+ {/* 傻瓜式指引:生成后置顶 */}
110
+ {result !== null && (
111
+ <div className={`${css.card} ${css.settingsCard}`} style={{ gap: 'var(--nf-space-6)' }}>
112
+ <div style={{ fontSize: 'var(--nf-fs-14)', fontWeight: 600 }}> 这份建议怎么用(三步)</div>
113
+ <ol style={{ margin: 0, paddingLeft: 'var(--nf-space-18)', display: 'grid', gap: 'var(--nf-space-4)', fontSize: 'var(--nf-fs-14)' }}>
114
+ <li>先看「风险 / 修复」,点每条旁的 <b>加入待办</b>,记下来逐个处理。</li>
115
+ <li>「下一阶段节点」每条可 <b>加入剧情线</b>,直接把下一步方向写进本书。</li>
116
+ <li>「节奏板」对照你当前写到的地方,看该加快还是放慢;不确定就点 <b>一键复制</b> 保存。</li>
117
+ </ol>
118
+ </div>
119
+ )}
120
+
121
+ {result !== null && (
122
+ <div style={{ display: 'grid', gap: 'var(--nf-space-10)' }}>
123
+ <div className={`${css.card} ${css.settingsCard}`} style={{ gap: 'var(--nf-space-6)' }}>
124
+ <div style={{ fontSize: 'var(--nf-fs-16)', fontWeight: 600 }}>总体判断</div>
125
+ <div style={{ fontSize: 'var(--nf-fs-14)' }}>{result.summary}</div>
126
+ </div>
127
+
128
+ <div style={{ display: 'grid', gridTemplateColumns: 'repeat(auto-fit, minmax(300px, 1fr))', gap: 'var(--nf-space-10)' }}>
129
+ <div className={`${css.card} ${css.settingsCard}`} style={{ gap: 'var(--nf-space-6)' }}>
130
+ <div style={{ fontSize: 'var(--nf-fs-16)', fontWeight: 600 }}>下一阶段剧情节点</div>
131
+ <ol style={{ margin: 0, paddingLeft: 'var(--nf-space-18)', display: 'grid', gap: 'var(--nf-space-6)', fontSize: 'var(--nf-fs-14)' }}>
132
+ {(result.nextArc ?? []).map((s, i) => (
133
+ <li key={i} style={{ display: 'flex', gap: 'var(--nf-space-8)', alignItems: 'center', flexWrap: 'wrap' }}>
134
+ <span style={{ flex: 1, minWidth: 160 }}>{s}</span>
135
+ <button type="button" className={`${css.button} ${css.buttonSmall}`} disabled={adopted.has('arc:' + s)} onClick={() => { void adoptArc(s) }}>
136
+ {adopted.has('arc:' + s) ? '已加入' : '采纳 → 加入剧情线'}
137
+ </button>
138
+ </li>
139
+ ))}
140
+ </ol>
141
+ </div>
142
+ <div className={`${css.card} ${css.settingsCard}`} style={{ gap: 'var(--nf-space-6)' }}>
143
+ <div style={{ fontSize: 'var(--nf-fs-16)', fontWeight: 600 }}>节奏板</div>
144
+ <div style={{ fontSize: 'var(--nf-fs-14)' }}>{result.pacing}</div>
145
+ </div>
146
+ </div>
147
+
148
+ <div style={{ display: 'grid', gridTemplateColumns: 'repeat(auto-fit, minmax(300px, 1fr))', gap: 'var(--nf-space-10)' }}>
149
+ <div className={`${css.card} ${css.settingsCard}`} style={{ gap: 'var(--nf-space-6)' }}>
150
+ <div style={{ fontSize: 'var(--nf-fs-16)', fontWeight: 600 }}>风险提示</div>
151
+ <ul style={{ margin: 0, paddingLeft: 'var(--nf-space-18)', display: 'grid', gap: 'var(--nf-space-6)', fontSize: 'var(--nf-fs-14)' }}>
152
+ {(result.risks ?? []).map((s, i) => (
153
+ <li key={i} style={{ display: 'flex', gap: 'var(--nf-space-8)', alignItems: 'center', flexWrap: 'wrap' }}>
154
+ <span style={{ flex: 1, minWidth: 160 }}>{s}</span>
155
+ <button type="button" className={`${css.button} ${css.buttonSmall}`} disabled={adopted.has('todo:risk:' + s)} onClick={() => { void adoptTodo(s, 'risk') }}>
156
+ {adopted.has('todo:risk:' + s) ? '已加入' : '采纳 → 加入待办'}
157
+ </button>
158
+ </li>
159
+ ))}
160
+ </ul>
161
+ </div>
162
+ <div className={`${css.card} ${css.settingsCard}`} style={{ gap: 'var(--nf-space-6)' }}>
163
+ <div style={{ fontSize: 'var(--nf-fs-16)', fontWeight: 600 }}>需要修复/再平衡</div>
164
+ <ul style={{ margin: 0, paddingLeft: 'var(--nf-space-18)', display: 'grid', gap: 'var(--nf-space-6)', fontSize: 'var(--nf-fs-14)' }}>
165
+ {(result.fixes ?? []).map((s, i) => (
166
+ <li key={i} style={{ display: 'flex', gap: 'var(--nf-space-8)', alignItems: 'center', flexWrap: 'wrap' }}>
167
+ <span style={{ flex: 1, minWidth: 160 }}>{s}</span>
168
+ <button type="button" className={`${css.button} ${css.buttonSmall}`} disabled={adopted.has('todo:fix:' + s)} onClick={() => { void adoptTodo(s, 'fix') }}>
169
+ {adopted.has('todo:fix:' + s) ? '已加入' : '采纳 → 加入待办'}
170
+ </button>
171
+ </li>
172
+ ))}
173
+ </ul>
174
+ </div>
175
+ </div>
176
+
177
+ {/* 已采纳待办清单(傻瓜式:勾选处理,做完删掉) */}
178
+ <div className={`${css.card} ${css.settingsCard}`} style={{ gap: 'var(--nf-space-6)' }}>
179
+ <div style={{ fontSize: 'var(--nf-fs-16)', fontWeight: 600 }}> 编辑待办(已采纳)</div>
180
+ {todos.length === 0 ? (
181
+ <span className={css.meta}>还没有。点上面「加入待办」,把风险/修复记到这里,做完勾掉。这些也会出现在工作流主页的「待办队列」。</span>
182
+ ) : (
183
+ <ul style={{ margin: 0, paddingLeft: 0, listStyle: 'none', display: 'grid', gap: 'var(--nf-space-6)', fontSize: 'var(--nf-fs-14)' }}>
184
+ {todos.map(t => (
185
+ <li key={t.id} style={{ display: 'flex', alignItems: 'center', gap: 8 }}>
186
+ <label style={{ display: 'flex', alignItems: 'center', gap: 'var(--nf-space-6)', flex: 1, cursor: 'pointer' }}>
187
+ <input type="checkbox" checked={t.done} disabled={todosBusy} onChange={() => { void toggleTodo(t.id) }} />
188
+ <span style={{ textDecoration: t.done ? 'line-through' : 'none', color: t.done ? 'var(--nf-text-2)' : undefined }}>{t.text}</span>
189
+ </label>
190
+ <button type="button" className={`${css.button} ${css.buttonSmall}`} title="删除" onClick={() => { void removeTodo(t.id) }}>×</button>
191
+ </li>
192
+ ))}
193
+ </ul>
194
+ )}
195
+ </div>
196
+ </div>
197
+ )}
198
+ </div>
199
+ )
200
+ }