@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,51 @@
1
+ /**
2
+ * Toast — 完成时刻的轻确认(R3 微交互)。
3
+ *
4
+ * 函数式挂载到 document.body(与 run-status 芯片同一套模式:CSS Module 类 +
5
+ * 自带令牌 + body[data-ds-dark-theme] 跟随宿主深浅色)。不依赖 React。
6
+ *
7
+ * 接入点:NovelPanel.pushProgress(kind='done'|'error') 自动弹 toast,
8
+ * 全站「已保存 / 生成完毕 / 审稿完成」等反馈统一获得确认时刻。
9
+ * 约束:最多同时 3 条(超过丢弃最旧),reduced-motion 下无入场动画(CSS 兜底)。
10
+ */
11
+ import css from './panel.module.css'
12
+
13
+ let host: HTMLDivElement | null = null
14
+ const LIVE_MAX = 3
15
+ const TTL = 2800
16
+
17
+ function ensureHost(): HTMLDivElement {
18
+ if (host !== null && host.isConnected) return host
19
+ host = document.createElement('div')
20
+ host.className = css.nfToastHost ?? 'nf-toast-host'
21
+ document.body.appendChild(host)
22
+ return host
23
+ }
24
+
25
+ /** 弹一条 toast。kind: 'done'(翡翠勾)/ 'error'(红叉)/ 'info'(中性)。 */
26
+ export function showToast(text: string, kind: 'done' | 'error' | 'info' = 'done'): void {
27
+ try {
28
+ const root = ensureHost()
29
+ // 超量丢弃最旧
30
+ while (root.children.length >= LIVE_MAX) root.firstElementChild?.remove()
31
+
32
+ const el = document.createElement('div')
33
+ el.className = css.nfToast ?? 'nf-toast'
34
+ el.setAttribute('data-kind', kind)
35
+ el.setAttribute('role', 'status')
36
+ el.setAttribute('aria-live', 'polite')
37
+
38
+ const tick = document.createElement('span')
39
+ tick.className = css.nfToastTick ?? 'nf-toast-tick'
40
+ tick.textContent = kind === 'error' ? '✕' : '✓'
41
+ const label = document.createElement('span')
42
+ label.textContent = text
43
+ el.append(tick, label)
44
+ root.appendChild(el)
45
+
46
+ window.setTimeout(() => {
47
+ el.classList.add(css.nfToastOut ?? 'nf-toast-out')
48
+ window.setTimeout(() => el.remove(), 320)
49
+ }, TTL)
50
+ } catch { /* DOM 不可用时静默降级(极端宿主环境) */ }
51
+ }
@@ -0,0 +1,401 @@
1
+ /**
2
+ * 纯展示组件(views):所有状态与事件处理器都留在 NovelPanel,
3
+ * 这里只接收 props 渲染。便于单独维护与复用。
4
+ */
5
+ import { useCallback, useEffect, useRef, useState } from 'react'
6
+ import type { ReactElement } from 'react'
7
+ import type { AuditIssue, Plotline, PlotlineHealthReport, PlotlinePlan, RoleRecord, RoleStatusCard } from '../../protocol.ts'
8
+ import { tt, ROLE_LABELS, roleColor, kindLabel, plotlineStatusLabel, plotlineStatusColor } from './helpers.ts'
9
+ import css from './panel.module.css'
10
+
11
+ /** 滑轨胶囊导航(B5-5):active 高亮为滑动指示块,随选中项平移。 */
12
+ export function SlideNav<T extends string>(props: {
13
+ ariaLabel: string
14
+ items: ReadonlyArray<{ id: T; label: ReactElement | string }>
15
+ active: T
16
+ onSelect: (id: T) => void
17
+ }): JSX.Element {
18
+ const { ariaLabel, items, active, onSelect } = props
19
+ const ref = useRef<HTMLDivElement | null>(null)
20
+ const [slider, setSlider] = useState<{ left: number; width: number } | null>(null)
21
+ const update = useCallback(() => {
22
+ const el = ref.current
23
+ if (el === null) return
24
+ const btn = el.querySelector<HTMLButtonElement>('button[data-active=""]')
25
+ if (btn === null) { setSlider(null); return }
26
+ const left = btn.offsetLeft
27
+ const width = btn.offsetWidth
28
+ setSlider(prev => (prev !== null && prev.left === left && prev.width === width ? prev : { left, width }))
29
+ }, [])
30
+ // 每次渲染后测量(set 前做等值比较,不会引发循环)。
31
+ useEffect(() => { update() })
32
+ useEffect(() => {
33
+ const el = ref.current
34
+ if (el === null) return
35
+ const ro = new ResizeObserver(() => { update() })
36
+ ro.observe(el)
37
+ return () => { ro.disconnect() }
38
+ }, [update])
39
+ return (
40
+ <div ref={ref} className={`${css.pillNav} ${css.pillNavSliding}`} role="tablist" aria-label={ariaLabel}>
41
+ {slider !== null && (
42
+ <span
43
+ className={css.pillNavSlider}
44
+ style={{ transform: `translateX(${slider.left}px)`, width: `${slider.width}px` }}
45
+ aria-hidden
46
+ />
47
+ )}
48
+ {items.map(it => (
49
+ <button
50
+ key={it.id}
51
+ type="button"
52
+ role="tab"
53
+ aria-selected={active === it.id}
54
+ data-active={active === it.id ? '' : undefined}
55
+ className={css.pillNavPill}
56
+ onClick={() => { onSelect(it.id) }}
57
+ >
58
+ {it.label}
59
+ </button>
60
+ ))}
61
+ </div>
62
+ )
63
+ }
64
+
65
+ /** 统计格:状态摘要条 / 资产健康通用。 */
66
+ export function StatCell(props: {
67
+ label: string
68
+ value: string
69
+ detail: string
70
+ /** 值颜色(可选)。 */
71
+ valueColor?: string
72
+ /** 值字号覆盖(如长文本用 13)。 */
73
+ valueFontSize?: number
74
+ /** detail 悬浮提示(可选)。 */
75
+ detailTitle?: string
76
+ }): ReactElement {
77
+ return (
78
+ <div className={css.assetStat}>
79
+ <span className={css.assetStatLabel}>{props.label}</span>
80
+ <span className={css.assetStatValue} style={{ color: props.valueColor, fontSize: props.valueFontSize }}>
81
+ {props.value}
82
+ </span>
83
+ <span className={css.assetStatDetail} title={props.detailTitle}>{props.detail}</span>
84
+ </div>
85
+ )
86
+ }
87
+
88
+ /** 待办队列行。 */
89
+ export function TodoRow(props: {
90
+ tone: 'danger' | 'warning' | 'info' | 'success'
91
+ title: string
92
+ description: string
93
+ actionLabel: string
94
+ disabled: boolean
95
+ onAction: () => void
96
+ }): ReactElement {
97
+ return (
98
+ <div className={`${css.todoItem} ${props.tone === 'danger' ? css.todoDanger : props.tone === 'warning' ? css.todoWarning : css.todoInfo}`}>
99
+ <span className={css.todoText}>
100
+ {props.title}
101
+ {props.description !== '' && <span className={css.meta}> — {props.description}</span>}
102
+ </span>
103
+ <button type="button" className={`${css.button} ${css.buttonSmall}`} disabled={props.disabled} onClick={props.onAction}>
104
+ {props.actionLabel}
105
+ </button>
106
+ </div>
107
+ )
108
+ }
109
+
110
+ /** 全书质检问题行。 */
111
+ export function AuditIssueRow(props: {
112
+ issue: AuditIssue
113
+ disabled: boolean
114
+ onFix: () => void
115
+ }): ReactElement {
116
+ const { issue } = props
117
+ return (
118
+ <div className={`${css.todoItem} ${issue.severity === 'high' ? css.todoDanger : issue.severity === 'medium' ? css.todoWarning : css.todoInfo}`}>
119
+ <span className={css.todoText}>
120
+ <span>
121
+ {issue.chapterNo > 0 ? `第 ${issue.chapterNo} 章` : '未定位章节'} · [{issue.severity}] {issue.item}
122
+ </span>
123
+ {issue.suggestion !== '' && <span className={css.meta}>建议:{issue.suggestion}</span>}
124
+ </span>
125
+ {issue.chapterNo > 0 && (
126
+ <button type="button" className={`${css.button} ${css.buttonSmall}`} disabled={props.disabled} onClick={props.onFix}>
127
+ 去修订
128
+ </button>
129
+ )}
130
+ </div>
131
+ )
132
+ }
133
+
134
+ /** 剧情线卡片(列表主体)。 */
135
+ export function PlotlineCard(props: {
136
+ line: Plotline
137
+ disabled: boolean
138
+ onRefresh: () => void
139
+ onEdit: () => void
140
+ onRemove: () => void
141
+ }): ReactElement {
142
+ const { line } = props
143
+ return (
144
+ <div key={line.id} style={{ border: '1px solid var(--nf-border)', borderRadius: 'var(--nf-radius-10)', padding: 'var(--nf-space-8) var(--nf-space-12)', fontSize: 'var(--nf-fs-12)', display: 'flex', flexDirection: 'column', gap: 'var(--nf-space-4)' }}>
145
+ <div className={css.row} style={{ justifyContent: 'space-between', flexWrap: 'wrap' }}>
146
+ <span style={{ display: 'flex', alignItems: 'center', gap: 'var(--nf-space-6)', flexWrap: 'wrap' }}>
147
+ <b>{line.name}</b>
148
+ <span className={css.badge} style={{ borderColor: 'var(--nf-accent)', color: 'var(--nf-accent)' }}>{kindLabel(line.kind)}</span>
149
+ <span className={css.badge} style={{ borderColor: plotlineStatusColor(line.status), color: plotlineStatusColor(line.status) }}>{plotlineStatusLabel(line.status)}</span>
150
+ </span>
151
+ <span style={{ display: 'flex', gap: 'var(--nf-space-6)' }}>
152
+ <button
153
+ type="button"
154
+ className={`${css.button} ${css.buttonSmall}`}
155
+ disabled={props.disabled}
156
+ onClick={props.onRefresh}
157
+ title="AI 结合编年录与章节摘要,自动更新这条线的当前进度"
158
+ >
159
+ ↻ 刷新进度
160
+ </button>
161
+ <button type="button" className={`${css.button} ${css.buttonSmall}`} disabled={props.disabled} onClick={props.onEdit}>
162
+ {tt('plotlines.edit')}
163
+ </button>
164
+ <button type="button" className={`${css.button} ${css.buttonSmall}`} disabled={props.disabled} onClick={props.onRemove}>
165
+ {tt('plotlines.remove')}
166
+ </button>
167
+ </span>
168
+ </div>
169
+ {line.goal !== '' && <div className={css.meta}><b>{tt('plotlines.goal')}:</b>{line.goal}</div>}
170
+ {line.progress !== '' && <div className={css.meta}><b>{tt('plotlines.progress')}:</b>{line.progress}</div>}
171
+ <div className={css.meta}>
172
+ {tt('plotlines.chapters')}:{line.chapters.length > 0 ? line.chapters.map(n => `第${n}章`).join('、') : '—'}
173
+ </div>
174
+ </div>
175
+ )
176
+ }
177
+
178
+ /** AI 候选角色行(提炼结果,可采纳/修改后采纳)。 */
179
+ export function RoleCandidateRow(props: {
180
+ candidate: RoleRecord
181
+ disabled: boolean
182
+ onAdopt: () => void
183
+ onEdit: () => void
184
+ }): ReactElement {
185
+ const { candidate: r } = props
186
+ const label = ROLE_LABELS[r.roleLabel] ?? r.roleLabel
187
+ const color = roleColor(r.roleLabel)
188
+ return (
189
+ <div key={r.name} style={{ border: '1px solid var(--nf-border)', borderRadius: 'var(--nf-radius-8)', padding: 'var(--nf-space-6) var(--nf-space-10)', fontSize: 'var(--nf-fs-12)' }}>
190
+ <div className={css.row} style={{ justifyContent: 'space-between', flexWrap: 'wrap' }}>
191
+ <span style={{ display: 'flex', alignItems: 'center', gap: 'var(--nf-space-6)', flexWrap: 'wrap' }}>
192
+ <b>{r.name}</b>
193
+ <span className={css.badge} style={{ borderColor: color, color }}>{label}</span>
194
+ {r.identity !== '' && <span className={css.meta}>{r.identity}</span>}
195
+ </span>
196
+ <span style={{ display: 'flex', gap: 'var(--nf-space-6)' }}>
197
+ <button type="button" className={`${css.button} ${css.buttonSmall} ${css.buttonPrimary}`} disabled={props.disabled} onClick={props.onAdopt}>
198
+ + 采纳
199
+ </button>
200
+ <button type="button" className={`${css.button} ${css.buttonSmall}`} disabled={props.disabled} onClick={props.onEdit} title="修改后再采纳(候选列表保留)">
201
+ 修改后采纳
202
+ </button>
203
+ </span>
204
+ </div>
205
+ {r.goals !== '' && <div className={css.meta}>目标:{r.goals}</div>}
206
+ {r.relations.length > 0 && <div className={css.meta}>关系:{r.relations.join('、')}</div>}
207
+ </div>
208
+ )
209
+ }
210
+
211
+ /** 已收录角色卡(含从编年录刷新的当前状态行)。 */
212
+ export function RoleCard(props: {
213
+ role: RoleRecord
214
+ status?: RoleStatusCard
215
+ disabled: boolean
216
+ onEdit: () => void
217
+ onRemove: () => void
218
+ }): ReactElement {
219
+ const { role: r, status: st } = props
220
+ const label = ROLE_LABELS[r.roleLabel] ?? r.roleLabel
221
+ const color = roleColor(r.roleLabel)
222
+ return (
223
+ <div key={r.name} style={{ border: '1px solid var(--nf-border)', borderRadius: 'var(--nf-radius-8)', padding: 'var(--nf-space-6) var(--nf-space-10)', fontSize: 'var(--nf-fs-12)' }}>
224
+ <div className={css.row} style={{ justifyContent: 'space-between', flexWrap: 'wrap' }}>
225
+ <span style={{ display: 'flex', alignItems: 'center', gap: 'var(--nf-space-6)', flexWrap: 'wrap' }}>
226
+ <b>{r.name}</b>
227
+ <span className={css.badge} style={{ borderColor: color, color }}>{label}</span>
228
+ {r.identity !== '' && <span className={css.meta}>{r.identity}</span>}
229
+ </span>
230
+ <span style={{ display: 'flex', gap: 'var(--nf-space-6)' }}>
231
+ <button type="button" className={`${css.button} ${css.buttonSmall}`} disabled={props.disabled} onClick={props.onEdit}>编辑</button>
232
+ <button type="button" className={`${css.button} ${css.buttonSmall}`} disabled={props.disabled} onClick={props.onRemove}>删除</button>
233
+ </span>
234
+ </div>
235
+ {r.goals !== '' && <div className={css.meta}>目标:{r.goals}</div>}
236
+ {st !== undefined && (
237
+ <div className={css.meta} style={{ color: 'var(--nf-accent)' }}>
238
+ <b>当前状态:</b>{st.status !== '' ? st.status : '(编年录暂无该角色记录)'}
239
+ {' · 出场 '}{st.appearances} 次 · 最近 第 {st.lastChapter} 章
240
+ </div>
241
+ )}
242
+ {r.relations.length > 0 && <div className={css.meta}>关系:{r.relations.join('、')}</div>}
243
+ {r.arc.length > 0 && <div className={css.meta}>成长线:{r.arc.join(' → ')}</div>}
244
+ {r.knowledge.length > 0 && <div className={css.meta}>知情度:{r.knowledge.join(';')}</div>}
245
+ </div>
246
+ )
247
+ }
248
+
249
+ /** 剧情健康检查报告面板。 */
250
+ export function PlotlineHealthPanel(props: {
251
+ report: PlotlineHealthReport
252
+ disabled: boolean
253
+ onPlan: () => void
254
+ onClose: () => void
255
+ }): ReactElement {
256
+ const { report } = props
257
+ return (
258
+ <div style={{ display: 'flex', flexDirection: 'column', gap: 'var(--nf-space-6)', border: '1px solid var(--nf-info)', borderRadius: 'var(--nf-radius-12)', padding: 'var(--nf-space-10)' }}>
259
+ <div className={css.row} style={{ justifyContent: 'space-between', flexWrap: 'wrap' }}>
260
+ <b> 剧情健康检查</b>
261
+ <span style={{ display: 'flex', gap: 'var(--nf-space-8)' }}>
262
+ <button
263
+ type="button"
264
+ className={`${css.button} ${css.buttonSmall}`}
265
+ disabled={props.disabled}
266
+ onClick={props.onPlan}
267
+ title="基于本次诊断生成下一阶段剧情方案"
268
+ >
269
+ 基于此设计方案
270
+ </button>
271
+ <button type="button" className={`${css.button} ${css.buttonSmall}`} onClick={props.onClose}>收起</button>
272
+ </span>
273
+ </div>
274
+ <div style={{ fontSize: 'var(--nf-fs-12)', fontWeight: 700, color: 'var(--nf-accent)' }}>判定:{report.verdict}</div>
275
+ {report.timing !== '' && <div className={css.meta}><b>建议时机:</b>{report.timing}</div>}
276
+ {report.reasons.length > 0 && (
277
+ <div style={{ display: 'flex', flexDirection: 'column', gap: 'var(--nf-space-2)', fontSize: 'var(--nf-fs-12)' }}>
278
+ {report.reasons.map((r, i) => <div key={i} className={css.meta}>· {r}</div>)}
279
+ </div>
280
+ )}
281
+ {report.lines.length > 0 && (
282
+ <div style={{ display: 'flex', flexDirection: 'column', gap: 'var(--nf-space-4)', fontSize: 'var(--nf-fs-12)' }}>
283
+ {report.lines.map((l, i) => {
284
+ const color = l.health === 'ok' ? 'var(--nf-success)' : l.health === 'warning' ? 'var(--nf-warn)' : 'var(--nf-error)'
285
+ const label = l.health === 'ok' ? '健康' : l.health === 'warning' ? '预警' : '搁置过久'
286
+ return (
287
+ <div key={i} style={{ display: 'flex', gap: 'var(--nf-space-8)', alignItems: 'flex-start' }}>
288
+ <span className={css.badge} style={{ borderColor: color, color, flex: 'none', marginTop: 'var(--nf-space-2)' }}>{label}</span>
289
+ <span className={css.meta}><b>{l.name}</b>:{l.note}</span>
290
+ </div>
291
+ )
292
+ })}
293
+ </div>
294
+ )}
295
+ </div>
296
+ )
297
+ }
298
+
299
+ /** 剧情方案面板。 */
300
+ export function PlotlinePlanPanel(props: {
301
+ plan: PlotlinePlan
302
+ disabled: boolean
303
+ onAdopt: (suggestion: Plotline) => void
304
+ onClose: () => void
305
+ }): ReactElement {
306
+ const { plan } = props
307
+ return (
308
+ <div style={{ display: 'flex', flexDirection: 'column', gap: 'var(--nf-space-6)', border: '1px solid var(--nf-accent)', borderRadius: 'var(--nf-radius-12)', padding: 'var(--nf-space-10)' }}>
309
+ <div className={css.row} style={{ justifyContent: 'space-between', flexWrap: 'wrap' }}>
310
+ <b> 剧情方案</b>
311
+ <button type="button" className={`${css.button} ${css.buttonSmall}`} onClick={props.onClose}>收起</button>
312
+ </div>
313
+ {plan.direction !== '' && (
314
+ <div className={css.meta} style={{ fontSize: 'var(--nf-fs-12)' }}><b>下一阶段方向:</b>{plan.direction}</div>
315
+ )}
316
+ {plan.suggestions.length > 0 && (
317
+ <div style={{ display: 'flex', flexDirection: 'column', gap: 'var(--nf-space-4)', fontSize: 'var(--nf-fs-12)' }}>
318
+ {plan.suggestions.map((s, i) => (
319
+ <div key={i} style={{ border: '1px solid var(--nf-border)', borderRadius: 'var(--nf-radius-8)', padding: 'var(--nf-space-6) var(--nf-space-10)' }}>
320
+ <div className={css.row} style={{ justifyContent: 'space-between', flexWrap: 'wrap' }}>
321
+ <span style={{ display: 'flex', alignItems: 'center', gap: 'var(--nf-space-6)', flexWrap: 'wrap' }}>
322
+ <b>{s.name}</b>
323
+ <span className={css.badge} style={{ borderColor: 'var(--nf-accent)', color: 'var(--nf-accent)' }}>{kindLabel(s.kind)}</span>
324
+ </span>
325
+ <button type="button" className={`${css.button} ${css.buttonSmall} ${css.buttonPrimary}`} disabled={props.disabled} onClick={() => { props.onAdopt(s) }}>
326
+ + 采纳
327
+ </button>
328
+ </div>
329
+ {s.goal !== '' && <div className={css.meta}>{s.goal}</div>}
330
+ {s.progress !== '' && <div className={css.meta}>初始进度:{s.progress}</div>}
331
+ </div>
332
+ ))}
333
+ </div>
334
+ )}
335
+ </div>
336
+ )
337
+ }
338
+
339
+ /** 建议剧情线面板。 */
340
+ export function PlotlineSuggestionPanel(props: {
341
+ suggestions: Plotline[]
342
+ disabled: boolean
343
+ onAdopt: (suggestion: Plotline) => void
344
+ onClose: () => void
345
+ }): ReactElement {
346
+ return (
347
+ <div style={{ display: 'flex', flexDirection: 'column', gap: 'var(--nf-space-6)', border: '1px solid var(--nf-info)', borderRadius: 'var(--nf-radius-12)', padding: 'var(--nf-space-10)' }}>
348
+ <div className={css.row} style={{ justifyContent: 'space-between', flexWrap: 'wrap' }}>
349
+ <b> 建议({props.suggestions.length} 条)</b>
350
+ <button type="button" className={`${css.button} ${css.buttonSmall}`} onClick={props.onClose}>收起</button>
351
+ </div>
352
+ {props.suggestions.length === 0 && <span className={css.meta}>没有候选线。</span>}
353
+ {props.suggestions.map((s, i) => (
354
+ <div key={i} style={{ border: '1px solid var(--nf-border)', borderRadius: 'var(--nf-radius-8)', padding: 'var(--nf-space-6) var(--nf-space-10)', fontSize: 'var(--nf-fs-12)' }}>
355
+ <div className={css.row} style={{ justifyContent: 'space-between', flexWrap: 'wrap' }}>
356
+ <span style={{ display: 'flex', alignItems: 'center', gap: 'var(--nf-space-6)', flexWrap: 'wrap' }}>
357
+ <b>{s.name}</b>
358
+ <span className={css.badge} style={{ borderColor: 'var(--nf-accent)', color: 'var(--nf-accent)' }}>{kindLabel(s.kind)}</span>
359
+ </span>
360
+ <button type="button" className={`${css.button} ${css.buttonSmall} ${css.buttonPrimary}`} disabled={props.disabled} onClick={() => { props.onAdopt(s) }}>
361
+ + 采纳
362
+ </button>
363
+ </div>
364
+ {s.goal !== '' && <div className={css.meta}>{s.goal}</div>}
365
+ </div>
366
+ ))}
367
+ </div>
368
+ )
369
+ }
370
+
371
+ /** 空状态基元:图标 + 标题 + 可选提示。compact 变体用于列表/卡片内嵌场景。 */
372
+ export function EmptyState(props: {
373
+ icon?: string
374
+ title: string
375
+ hint?: string
376
+ compact?: boolean
377
+ }): ReactElement {
378
+ return (
379
+ <div className={css.emptyState} data-compact={props.compact === true ? 'true' : undefined}>
380
+ {props.icon !== undefined && <span className={css.emptyStateIcon}>{props.icon}</span>}
381
+ <span className={css.emptyStateTitle}>{props.title}</span>
382
+ {props.hint !== undefined && <span className={css.emptyStateHint}>{props.hint}</span>}
383
+ </div>
384
+ )
385
+ }
386
+
387
+ /** 骨架屏基元:rows 条微光占位行;widths 指定各行宽度档位,循环取用。 */
388
+ export function SkeletonLines(props: {
389
+ rows?: number
390
+ widths?: Array<'full' | 'wide' | 'half'>
391
+ }): ReactElement {
392
+ const rows = props.rows ?? 3
393
+ const widths = props.widths ?? ['full', 'wide', 'half']
394
+ return (
395
+ <div className={css.skeletonLines} aria-hidden="true">
396
+ {Array.from({ length: rows }, (_, i) => (
397
+ <span key={i} className={css.skeletonLine} data-w={widths[i % widths.length]} />
398
+ ))}
399
+ </div>
400
+ )
401
+ }
@@ -0,0 +1,143 @@
1
+ /**
2
+ * 运行状态芯片:固定在页面顶部的「小说工坊生成状态」小徽标。
3
+ * 轮询 /run/status,实时显示 生成中/暂停/完成/停止/出错 + 当前章号;
4
+ * 点击可打开/关闭小说工坊面板。独立 DOM 注入,不触碰主面板逻辑,挂载失败静默降级。
5
+ *
6
+ * P0 重写说明(原实现的三个问题):
7
+ * 1. 整块背景染状态色 + 固定白字 → 浅色主题下「白字配亮底」对比度崩坏。
8
+ * 现在改为「玻璃底 + 主题前景色文字 + 左侧状态色点」,状态只由色点承载,
9
+ * 文字永远跟随主题,任何深浅色下都可读。
10
+ * 2. 样式写死在 JS 的 inline style 里,切主题纹丝不动。
11
+ * 现在改用 CSS Module class,配色走 --nf-chip-* 令牌,
12
+ * 由 body[data-ds-dark-theme] 跟随宿主深浅色。
13
+ * 3. role="button" 却没有 tabindex、没有键盘处理,键盘用户完全用不了。
14
+ * 现在补 tabindex + Enter/Space 激活 + aria-label/live。
15
+ */
16
+ import type { NovelApi } from './api.ts'
17
+ import type { PanelController } from './panel/controller.ts'
18
+ import type { RunState } from '../protocol.ts'
19
+ import css from './panel/panel.module.css'
20
+
21
+ const REFRESH_MS = 4000
22
+
23
+ /** 状态文案。状态语义由左侧色点承载,这里不再放 emoji(避免与色点重复表意)。 */
24
+ function label(s: RunState): string {
25
+ const no = s.currentNo
26
+ switch (s.status) {
27
+ case 'running': return `小说工坊 生成中 · 第${no}章`
28
+ case 'paused': return `小说工坊 已暂停 · 第${no}章`
29
+ case 'done': return `小说工坊 批完成 ${s.startNo}–${s.endNo}`
30
+ case 'stopped': return `小说工坊 已停止 · 第${no}章`
31
+ case 'error': return `小说工坊 出错 · 第${no}章`
32
+ default: return '小说工坊'
33
+ }
34
+ }
35
+
36
+ /** 生成一个固定顶部的状态芯片并开始轮询。返回 dispose。 */
37
+ export function mountRunStatus(controller: PanelController, api: NovelApi): () => void {
38
+ const chip = document.createElement('div')
39
+ chip.className = css.runChip
40
+ chip.setAttribute('role', 'button')
41
+ // P0: role="button" 必须可聚焦、可键盘激活,否则对键盘/读屏用户等同不存在。
42
+ chip.setAttribute('tabindex', '0')
43
+ chip.setAttribute('title', '小说工坊生成状态(点击打开工坊)')
44
+ // 状态是异步轮询出来的,用 aria-live 让读屏用户也能感知变化。
45
+ chip.setAttribute('aria-live', 'polite')
46
+
47
+ const dot = document.createElement('span')
48
+ dot.className = css.runChipDot
49
+ // 纯装饰,颜色信息已由文字表达,不要让读屏重复播报。
50
+ dot.setAttribute('aria-hidden', 'true')
51
+
52
+ const text = document.createElement('span')
53
+ text.className = css.runChipText
54
+
55
+ chip.append(dot, text)
56
+
57
+ const toggle = (): void => { controller.toggle() }
58
+
59
+ // 自由拖拽 + 位置记忆(与 AI 进度浮窗一致)
60
+ const POS_KEY = 'dsh-novel-forge.runChip.pos'
61
+ let dragging = false, moved = false, sx = 0, sy = 0, ox = 0, oy = 0
62
+ const readPos = (): { x: number; y: number } | null => {
63
+ try { const p = window.localStorage.getItem(POS_KEY); return p === null ? null : JSON.parse(p) as { x: number; y: number } } catch { return null }
64
+ }
65
+ const writePos = (x: number, y: number): void => {
66
+ try { window.localStorage.setItem(POS_KEY, JSON.stringify({ x, y })) } catch { /* ignore */ }
67
+ }
68
+ const clampX = (x: number): number => Math.max(0, Math.min(Math.max(0, window.innerWidth - 40), x))
69
+ const clampY = (y: number): number => Math.max(0, Math.min(Math.max(0, window.innerHeight - 40), y))
70
+ const applyPos = (x: number, y: number): void => {
71
+ chip.style.left = clampX(x) + 'px'
72
+ chip.style.top = clampY(y) + 'px'
73
+ chip.style.right = 'auto'
74
+ }
75
+ chip.addEventListener('pointerdown', (e) => {
76
+ if (e.button !== 0) return
77
+ dragging = true; moved = false; sx = e.clientX; sy = e.clientY
78
+ ox = chip.offsetLeft; oy = chip.offsetTop
79
+ try { chip.setPointerCapture(e.pointerId) } catch { /* ignore */ }
80
+ })
81
+ chip.addEventListener('pointermove', (e) => {
82
+ if (!dragging) return
83
+ const dx = e.clientX - sx, dy = e.clientY - sy
84
+ if (Math.abs(dx) > 3 || Math.abs(dy) > 3) moved = true
85
+ if (moved) {
86
+ applyPos(ox + dx, oy + dy)
87
+ writePos(chip.offsetLeft, chip.offsetTop)
88
+ }
89
+ })
90
+ const endDrag = (): void => { dragging = false }
91
+ chip.addEventListener('pointerup', endDrag)
92
+ chip.addEventListener('pointercancel', endDrag)
93
+ // 拖拽后松开不触发开合
94
+ chip.addEventListener('click', (e) => {
95
+ if (moved) { e.preventDefault(); e.stopImmediatePropagation(); moved = false; return }
96
+ toggle()
97
+ })
98
+ chip.addEventListener('keydown', (e) => {
99
+ if (e.key === 'Enter' || e.key === ' ' || e.key === 'Spacebar') {
100
+ e.preventDefault()
101
+ toggle()
102
+ }
103
+ })
104
+
105
+ document.body.appendChild(chip)
106
+
107
+ // 恢复上次拖拽位置(若曾移动过)
108
+ const saved = readPos()
109
+ if (saved !== null) applyPos(saved.x, saved.y)
110
+
111
+ let disposed = false
112
+ let timer: number | undefined
113
+
114
+ const render = (s: RunState | null): void => {
115
+ if (s === null) {
116
+ chip.dataset.visible = 'false'
117
+ return
118
+ }
119
+ let body = label(s)
120
+ if (s.status === 'running' && s.stats.error > 0) body += ` · ×${s.stats.error}`
121
+ text.textContent = body
122
+ chip.dataset.status = s.status
123
+ chip.dataset.visible = 'true'
124
+ chip.setAttribute('aria-label', body)
125
+ }
126
+
127
+ const tick = async (): Promise<void> => {
128
+ if (disposed) return
129
+ try {
130
+ const s = await api.runStatus()
131
+ render(s)
132
+ } catch { /* best-effort */ }
133
+ if (!disposed) timer = window.setTimeout(() => { void tick() }, REFRESH_MS)
134
+ }
135
+
136
+ void tick()
137
+
138
+ return () => {
139
+ disposed = true
140
+ if (timer !== undefined) window.clearTimeout(timer)
141
+ chip.remove()
142
+ }
143
+ }
@@ -11,7 +11,7 @@ import css from './panel/panel.module.css'
11
11
  export const ENTRY_SELECTOR = '[data-dsh-novelforge-entry]'
12
12
 
13
13
  /** Inline icon: an open book / writing glyph. */
14
- const ICON = '<svg viewBox="0 0 16 16" width="14" height="14" fill="none" stroke="currentColor" stroke-width="1.3" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M2.5 3.5h4a2 2 0 0 1 2 2v7a2 2 0 0 0-2-2h-4z"/><path d="M13.5 3.5h-4a2 2 0 0 0-2 2v7a2 2 0 0 1 2-2h4z"/><path d="M8 5.5v7"/></svg>'
14
+ const ICON = '<svg viewBox="0 0 24 24" width="16" height="16" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M12.67 19a2 2 0 0 0 1.416-.588l6.154-6.172a6 6 0 0 0-8.49-8.49L5.586 9.914A2 2 0 0 0 5 11.328V18a1 1 0 0 0 1 1z"/><path d="M16 8 2 22"/><path d="M17.5 15H9"/></svg>'
15
15
 
16
16
  /** Find the sidebar shell root element. */
17
17
  function sidebarRoot(): HTMLElement | undefined {
@@ -0,0 +1,28 @@
1
+ /**
2
+ * 客户端文件文本智能解码:兼容 UTF-8 BOM / UTF-16 / UTF-8 / GB18030(GBK)。
3
+ * 浏览器 File.text() 固定按 UTF-8 解码,GBK 中文网文 txt 会乱码,这里兜底。
4
+ */
5
+
6
+ /**
7
+ * 读取一个 File 的文本,按编码探测兜底解码。
8
+ * @returns 解码后的文本。
9
+ */
10
+ export async function readFileTextSmart(file: File): Promise<string> {
11
+ const buf = await file.arrayBuffer()
12
+ const u8 = new Uint8Array(buf)
13
+ // UTF-8 BOM
14
+ if (u8.length >= 3 && u8[0] === 0xEF && u8[1] === 0xBB && u8[2] === 0xBF) {
15
+ return new TextDecoder('utf-8').decode(u8.subarray(3))
16
+ }
17
+ // UTF-16 LE / BE
18
+ if (u8.length >= 2 && u8[0] === 0xFF && u8[1] === 0xFE) return new TextDecoder('utf-16le').decode(u8.subarray(2))
19
+ if (u8.length >= 2 && u8[0] === 0xFE && u8[1] === 0xFF) return new TextDecoder('utf-16be').decode(u8.subarray(2))
20
+ // UTF-8(宽松),含替换符则回退 GB18030
21
+ const utf8 = new TextDecoder('utf-8', { fatal: false }).decode(u8)
22
+ if (!utf8.includes('\uFFFD')) return utf8
23
+ try {
24
+ return new TextDecoder('gb18030').decode(u8)
25
+ } catch {
26
+ return utf8
27
+ }
28
+ }