@waterwx/dsh-novel-forge 0.1.0 → 1.0.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 +162 -111
- package/cordis.patch.yml +1 -1
- package/lib/client.js +54165 -2666
- package/lib/index.js +11895 -2211
- package/lib/types/ai-scan.d.ts +36 -0
- package/lib/types/assets.d.ts +19 -4
- package/lib/types/assistant.d.ts +6 -0
- package/lib/types/author-assets.d.ts +25 -0
- package/lib/types/bookshelf.d.ts +8 -1
- package/lib/types/client/api.d.ts +235 -3
- package/lib/types/client/index.d.ts +1 -1
- package/lib/types/client/llmLive.d.ts +33 -0
- package/lib/types/client/locales.d.ts +101 -28
- package/lib/types/client/panel/AdaptModeView.d.ts +5 -0
- package/lib/types/client/panel/AssetsTab.d.ts +6 -1
- package/lib/types/client/panel/AuthorAssetsView.d.ts +4 -0
- package/lib/types/client/panel/AuthorHome.d.ts +34 -0
- package/lib/types/client/panel/BookAnalysisView.d.ts +4 -0
- package/lib/types/client/panel/CreateBookView.d.ts +13 -0
- package/lib/types/client/panel/DirectorView.d.ts +7 -0
- package/lib/types/client/panel/FlowGuide.d.ts +22 -0
- package/lib/types/client/panel/GlobalAssetLibraryView.d.ts +4 -0
- package/lib/types/client/panel/IdeaInspirationView.d.ts +6 -0
- package/lib/types/client/panel/ImportModal.d.ts +7 -0
- package/lib/types/client/panel/KnowledgeBaseView.d.ts +4 -0
- package/lib/types/client/panel/LiveFeedLog.d.ts +5 -0
- package/lib/types/client/panel/MangaRoleLibrary.d.ts +20 -0
- package/lib/types/client/panel/MangaWorkspace.d.ts +12 -0
- package/lib/types/client/panel/MarketRadarView.d.ts +5 -0
- package/lib/types/client/panel/ModelManager.d.ts +14 -0
- package/lib/types/client/panel/ModelPicker.d.ts +11 -0
- package/lib/types/client/panel/NovelPanel.d.ts +1 -1
- package/lib/types/client/panel/ProgressConsole.d.ts +17 -0
- package/lib/types/client/panel/ProgressHomeView.d.ts +5 -0
- package/lib/types/client/panel/PropLibrary.d.ts +8 -0
- package/lib/types/client/panel/ReaderView.d.ts +8 -0
- package/lib/types/client/panel/ReasoningSection.d.ts +10 -0
- package/lib/types/client/panel/RunPanel.d.ts +5 -0
- package/lib/types/client/panel/SceneLibrary.d.ts +16 -0
- package/lib/types/client/panel/SettingsView.d.ts +16 -0
- package/lib/types/client/panel/ShelfView.d.ts +15 -0
- package/lib/types/client/panel/StoryboardTab.d.ts +19 -0
- package/lib/types/client/panel/StyleCard.d.ts +8 -0
- package/lib/types/client/panel/StyleGallery.d.ts +1 -0
- package/lib/types/client/panel/WorldTab.d.ts +10 -0
- package/lib/types/client/panel/helpers.d.ts +10 -0
- package/lib/types/client/panel/views.d.ts +77 -0
- package/lib/types/client/run-status.d.ts +19 -0
- package/lib/types/client/text.d.ts +9 -0
- package/lib/types/comic-presets.d.ts +12 -0
- package/lib/types/engine.d.ts +250 -8
- package/lib/types/global-assets.d.ts +6 -0
- package/lib/types/index.d.ts +24 -2
- package/lib/types/llm-live.d.ts +44 -0
- package/lib/types/manga-genre-rules.d.ts +22 -0
- package/lib/types/market-radar-scan.d.ts +19 -0
- package/lib/types/market-radar-sources.d.ts +31 -0
- package/lib/types/novel-context.d.ts +80 -0
- package/lib/types/prompt-utils.d.ts +35 -0
- package/lib/types/prompting.d.ts +35 -0
- package/lib/types/protocol.d.ts +1453 -5
- package/lib/types/review-policy.d.ts +97 -0
- package/lib/types/run.d.ts +32 -0
- package/lib/types/shot-language.d.ts +46 -0
- package/lib/types/src/ai-scan.d.ts +36 -0
- package/lib/types/src/assets.d.ts +37 -0
- package/lib/types/src/assistant.d.ts +45 -0
- package/lib/types/src/author-assets.d.ts +25 -0
- package/lib/types/src/bookshelf.d.ts +42 -0
- package/lib/types/src/client/api.d.ts +234 -0
- package/lib/types/src/client/docx.d.ts +15 -0
- package/lib/types/src/client/index.d.ts +14 -0
- package/lib/types/src/client/locales.d.ts +212 -0
- package/lib/types/src/client/mount.d.ts +9 -0
- package/lib/types/src/client/panel/AdaptModeView.d.ts +5 -0
- package/lib/types/src/client/panel/AssetsTab.d.ts +12 -0
- package/lib/types/src/client/panel/AssistantTab.d.ts +7 -0
- package/lib/types/src/client/panel/AuthorAssetsView.d.ts +4 -0
- package/lib/types/src/client/panel/AuthorHome.d.ts +32 -0
- package/lib/types/src/client/panel/BookshelfBar.d.ts +11 -0
- package/lib/types/src/client/panel/CreateBookView.d.ts +9 -0
- package/lib/types/src/client/panel/FlowGuide.d.ts +22 -0
- package/lib/types/src/client/panel/GlobalAssetLibraryView.d.ts +4 -0
- package/lib/types/src/client/panel/ImportModal.d.ts +7 -0
- package/lib/types/src/client/panel/MangaRoleLibrary.d.ts +20 -0
- package/lib/types/src/client/panel/MangaWorkspace.d.ts +12 -0
- package/lib/types/src/client/panel/ModelManager.d.ts +14 -0
- package/lib/types/src/client/panel/NovelPanel.d.ts +13 -0
- package/lib/types/src/client/panel/ProgressConsole.d.ts +17 -0
- package/lib/types/src/client/panel/ProgressHomeView.d.ts +5 -0
- package/lib/types/src/client/panel/PropLibrary.d.ts +8 -0
- package/lib/types/src/client/panel/ReaderView.d.ts +8 -0
- package/lib/types/src/client/panel/ReasoningSection.d.ts +10 -0
- package/lib/types/src/client/panel/RunPanel.d.ts +5 -0
- package/lib/types/src/client/panel/SceneLibrary.d.ts +16 -0
- package/lib/types/src/client/panel/SettingsView.d.ts +16 -0
- package/lib/types/src/client/panel/ShelfView.d.ts +15 -0
- package/lib/types/src/client/panel/StoryboardTab.d.ts +19 -0
- package/lib/types/src/client/panel/StyleCard.d.ts +8 -0
- package/lib/types/src/client/panel/StyleGallery.d.ts +1 -0
- package/lib/types/src/client/panel/WorldTab.d.ts +10 -0
- package/lib/types/src/client/panel/controller.d.ts +19 -0
- package/lib/types/src/client/panel/helpers.d.ts +18 -0
- package/lib/types/src/client/panel/views.d.ts +77 -0
- package/lib/types/src/client/sidebar-entry.d.ts +13 -0
- package/lib/types/src/client/text.d.ts +9 -0
- package/lib/types/src/comic-presets.d.ts +12 -0
- package/lib/types/src/docx.d.ts +19 -0
- package/lib/types/src/engine.d.ts +402 -0
- package/lib/types/src/index.d.ts +79 -0
- package/lib/types/src/manga-genre-rules.d.ts +22 -0
- package/lib/types/src/novel-context.d.ts +80 -0
- package/lib/types/src/prompt-utils.d.ts +35 -0
- package/lib/types/src/protocol.d.ts +2176 -0
- package/lib/types/src/review-policy.d.ts +97 -0
- package/lib/types/src/routes.d.ts +29 -0
- package/lib/types/src/run.d.ts +32 -0
- package/lib/types/src/shot-language.d.ts +46 -0
- package/lib/types/src/story-beat-language.d.ts +35 -0
- package/lib/types/src/style-library.d.ts +33 -0
- package/lib/types/story-beat-language.d.ts +35 -0
- package/lib/types/style-library.d.ts +33 -0
- package/package.json +57 -42
- package/scripts/patch-schemastery.mjs +47 -0
- package/src/ai-scan.ts +179 -0
- package/src/assets.ts +1774 -518
- package/src/assistant.ts +915 -547
- package/src/author-assets.ts +147 -0
- package/src/bookshelf.ts +32 -2
- package/src/client/api.ts +666 -254
- package/src/client/index.ts +3 -1
- package/src/client/llmLive.ts +101 -0
- package/src/client/locales.ts +178 -31
- package/src/client/mount.tsx +1 -1
- package/src/client/panel/AdaptModeView.tsx +535 -0
- package/src/client/panel/AssetsTab.tsx +168 -51
- package/src/client/panel/AssistantTab.tsx +339 -188
- package/src/client/panel/AuthorAssetsView.tsx +208 -0
- package/src/client/panel/AuthorHome.tsx +234 -0
- package/src/client/panel/BookAnalysisView.tsx +60 -0
- package/src/client/panel/BookshelfBar.tsx +63 -9
- package/src/client/panel/CreateBookView.tsx +371 -0
- package/src/client/panel/DirectorView.tsx +200 -0
- package/src/client/panel/GlobalAssetLibraryView.tsx +172 -0
- package/src/client/panel/IdeaInspirationView.tsx +71 -0
- package/src/client/panel/ImportModal.tsx +303 -0
- package/src/client/panel/KnowledgeBaseView.tsx +73 -0
- package/src/client/panel/LiveFeedLog.tsx +67 -0
- package/src/client/panel/MarketRadarView.tsx +374 -0
- package/src/client/panel/ModelManager.tsx +205 -0
- package/src/client/panel/NovelPanel.tsx +4713 -990
- package/src/client/panel/ProgressConsole.tsx +63 -0
- package/src/client/panel/ProgressHomeView.tsx +87 -0
- package/src/client/panel/ReaderView.tsx +306 -0
- package/src/client/panel/ReasoningSection.tsx +31 -0
- package/src/client/panel/RunPanel.tsx +204 -0
- package/src/client/panel/SettingsView.tsx +265 -0
- package/src/client/panel/ShelfView.tsx +242 -0
- package/src/client/panel/WorldTab.tsx +202 -0
- package/src/client/panel/helpers.ts +50 -0
- package/src/client/panel/panel.module.css +4325 -526
- package/src/client/panel/views.tsx +346 -0
- package/src/client/run-status.ts +100 -0
- package/src/client/sidebar-entry.ts +1 -1
- package/src/client/text.ts +28 -0
- package/src/engine.ts +4230 -1019
- package/src/global-assets.ts +59 -0
- package/src/index.ts +123 -9
- package/src/llm-live.ts +68 -0
- package/src/market-radar-scan.ts +41 -0
- package/src/market-radar-sources.ts +173 -0
- package/src/novel-context.ts +287 -0
- package/src/prompt-utils.ts +73 -0
- package/src/prompting.ts +159 -0
- package/src/protocol.ts +1998 -539
- package/src/review-policy.ts +161 -0
- package/src/routes.ts +3277 -955
- package/src/run.ts +338 -0
- package/lib/client.js.map +0 -1
- package/lib/index.js.map +0 -1
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 全局资源库(跨书可复用的自定义题材/推进模式)。
|
|
3
|
+
* 雷达「同步底座到全局资源库」写入此处;/assets 返回时与内置库合并展示。
|
|
4
|
+
*/
|
|
5
|
+
import { homedir } from 'node:os'
|
|
6
|
+
import { join } from 'node:path'
|
|
7
|
+
import { mkdirSync, readFileSync, writeFileSync } from 'node:fs'
|
|
8
|
+
import type { GenreNode, ProgressionMode } from './protocol.ts'
|
|
9
|
+
|
|
10
|
+
interface GlobalAssets {
|
|
11
|
+
genres: GenreNode[]
|
|
12
|
+
modes: ProgressionMode[]
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const FILE = join(homedir(), '.dsh', 'novel-forge-global-assets.json')
|
|
16
|
+
let cache: GlobalAssets | null = null
|
|
17
|
+
|
|
18
|
+
function load(): GlobalAssets {
|
|
19
|
+
if (cache !== null) return cache
|
|
20
|
+
try {
|
|
21
|
+
const raw = readFileSync(FILE, 'utf8')
|
|
22
|
+
const parsed = JSON.parse(raw) as Partial<GlobalAssets>
|
|
23
|
+
cache = { genres: Array.isArray(parsed.genres) ? parsed.genres : [], modes: Array.isArray(parsed.modes) ? parsed.modes : [] }
|
|
24
|
+
} catch {
|
|
25
|
+
cache = { genres: [], modes: [] }
|
|
26
|
+
}
|
|
27
|
+
return cache
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function persist(): void {
|
|
31
|
+
if (cache === null) return
|
|
32
|
+
mkdirSync(join(homedir(), '.dsh'), { recursive: true })
|
|
33
|
+
writeFileSync(FILE, JSON.stringify(cache, null, 2), 'utf8')
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function globalGenreLibrary(): GenreNode[] {
|
|
37
|
+
return load().genres
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function globalProgressionLibrary(): ProgressionMode[] {
|
|
41
|
+
return load().modes
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** 新增/已有则跳过;返回 true=新增。 */
|
|
45
|
+
export function addGlobalGenre(g: GenreNode): boolean {
|
|
46
|
+
const lib = load()
|
|
47
|
+
if (lib.genres.some(x => x.name === g.name)) return false
|
|
48
|
+
lib.genres.push({ ...g, children: g.children ?? [] })
|
|
49
|
+
persist()
|
|
50
|
+
return true
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export function addGlobalMode(m: ProgressionMode): boolean {
|
|
54
|
+
const lib = load()
|
|
55
|
+
if (lib.modes.some(x => x.name === m.name)) return false
|
|
56
|
+
lib.modes.push({ ...m, payoffs: m.payoffs ?? [], risks: m.risks ?? [], primary: m.primary ?? false })
|
|
57
|
+
persist()
|
|
58
|
+
return true
|
|
59
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -7,8 +7,10 @@
|
|
|
7
7
|
* changes.
|
|
8
8
|
*/
|
|
9
9
|
|
|
10
|
+
import { homedir } from 'node:os'
|
|
11
|
+
import { join } from 'node:path'
|
|
10
12
|
import type { Context } from '@deepseek-ai/cordis'
|
|
11
|
-
import {
|
|
13
|
+
import type {} from '@deepseek-ai/dsh-settings'
|
|
12
14
|
import z from 'schemastery'
|
|
13
15
|
import type {} from '@deepseek-ai/dsh-host-webserver'
|
|
14
16
|
import type {} from '@deepseek-ai/dsh-llm'
|
|
@@ -21,14 +23,14 @@ import { activeBookOutputDir } from './bookshelf.ts'
|
|
|
21
23
|
export const name = 'novel-forge'
|
|
22
24
|
|
|
23
25
|
/** Services required before the novel-forge surfaces can mount. */
|
|
24
|
-
export const inject = ['webServer', 'llm', 'systemPrompt']
|
|
26
|
+
export const inject = ['webServer', 'llm', 'systemPrompt', 'settings']
|
|
25
27
|
|
|
26
28
|
/**
|
|
27
29
|
* Settings namespace of the novel-forge capability — the section the web
|
|
28
30
|
* settings surface edits. Spelled here rather than imported: the browser half
|
|
29
31
|
* spells the same value and must not depend on a Host package.
|
|
30
32
|
*/
|
|
31
|
-
export const NOVEL_SETTINGS_NAMESPACE =
|
|
33
|
+
export const NOVEL_SETTINGS_NAMESPACE = 'dsh-novel-forge' as const
|
|
32
34
|
|
|
33
35
|
/** Plugin config, validated by the same-named schemastery schema. */
|
|
34
36
|
export interface Config {
|
|
@@ -44,6 +46,16 @@ export interface Config {
|
|
|
44
46
|
provider?: string
|
|
45
47
|
/** LLM model id. */
|
|
46
48
|
model?: string
|
|
49
|
+
/** 任务级模型路由:正文生成模型(留空则跟随 model)。 */
|
|
50
|
+
generateModel?: string
|
|
51
|
+
/** 任务级模型路由:审稿模型(留空则跟随 model)。 */
|
|
52
|
+
reviewModel?: string
|
|
53
|
+
/** 任务级模型路由:AI 复核/质检模型(留空则跟随 model)。 */
|
|
54
|
+
auditModel?: string
|
|
55
|
+
/** LLM reasoning effort (off/low/high/max). */
|
|
56
|
+
reasoningEffort?: 'off' | 'low' | 'high' | 'max'
|
|
57
|
+
/** 分析类任务(提炼/拆书/反推大纲等)的推理档位;默认 low。 */
|
|
58
|
+
analysisReasoning?: 'off' | 'low' | 'high' | 'max'
|
|
47
59
|
/** Target characters per chapter. */
|
|
48
60
|
chapterChars?: number
|
|
49
61
|
/** Max output tokens per chapter call. */
|
|
@@ -52,37 +64,64 @@ export interface Config {
|
|
|
52
64
|
reviewPassScore?: number
|
|
53
65
|
/** Whether generation auto-runs review after writing. */
|
|
54
66
|
autoReview?: boolean
|
|
67
|
+
/** Whether generation auto-runs the author review (hook/continuity/trend). */
|
|
68
|
+
autoAuthorReview?: boolean
|
|
69
|
+
/** 修订/润色产出草稿后自动附带一次 AI 审查(默认开,可在设置页关闭省 token)。 */
|
|
70
|
+
autoReviewAfterRevise?: boolean
|
|
71
|
+
/** 自定义背景图(URL / dataURL / 服务端路径引用)。 */
|
|
72
|
+
themeBackground?: string
|
|
73
|
+
/** 自定义背景遮罩/模糊强度 0-80。 */
|
|
74
|
+
themeBackgroundBlur?: number
|
|
75
|
+
/** 玻璃透明度 0-100(100=当前原样)。 */
|
|
76
|
+
themeOpacity?: number
|
|
77
|
+
/** 是否启用改编模式(默认关闭)。 */
|
|
78
|
+
enableAdaptMode?: boolean
|
|
55
79
|
}
|
|
56
80
|
|
|
57
81
|
export const Config: z<Config> = z.object({
|
|
58
82
|
announceToAgent: z.boolean().default(true),
|
|
59
83
|
enabled: z.boolean().default(true),
|
|
60
|
-
outlinePath: z.string().default('
|
|
61
|
-
outputDir: z.string().default('
|
|
84
|
+
outlinePath: z.string().default(''),
|
|
85
|
+
outputDir: z.string().default(join(homedir(), '.dsh', 'novels')),
|
|
62
86
|
provider: z.string().default('deepseek-official'),
|
|
63
87
|
model: z.string().default('deepseek-v4-flash'),
|
|
88
|
+
generateModel: z.string().default(''),
|
|
89
|
+
reviewModel: z.string().default(''),
|
|
90
|
+
auditModel: z.string().default(''),
|
|
91
|
+
reasoningEffort: z.union(['off', 'low', 'high', 'max']).default('off'),
|
|
92
|
+
analysisReasoning: z.union(['off', 'low', 'high', 'max']).default('low'),
|
|
64
93
|
chapterChars: z.number().default(3500),
|
|
65
94
|
maxTokens: z.number().default(12000),
|
|
66
95
|
reviewPassScore: z.number().default(70),
|
|
67
96
|
autoReview: z.boolean().default(true),
|
|
97
|
+
autoAuthorReview: z.boolean().default(true),
|
|
98
|
+
autoReviewAfterRevise: z.boolean().default(true),
|
|
99
|
+
themeBackground: z.string().default(''),
|
|
100
|
+
themeBackgroundBlur: z.number().default(0),
|
|
101
|
+
themeOpacity: z.number().default(100),
|
|
102
|
+
enableAdaptMode: z.boolean().default(true),
|
|
68
103
|
})
|
|
69
104
|
|
|
70
105
|
/** Schema defaults, re-read for hand-built test contexts. */
|
|
71
106
|
const DEFAULT_ANNOUNCE = true
|
|
72
|
-
const DEFAULT_OUTLINE_PATH = '
|
|
73
|
-
const DEFAULT_OUTPUT_DIR = '
|
|
107
|
+
const DEFAULT_OUTLINE_PATH = ''
|
|
108
|
+
const DEFAULT_OUTPUT_DIR = join(homedir(), '.dsh', 'novels')
|
|
74
109
|
const DEFAULT_PROVIDER = 'deepseek-official'
|
|
75
110
|
const DEFAULT_MODEL = 'deepseek-v4-flash'
|
|
111
|
+
const DEFAULT_REASONING_EFFORT = 'off' as const
|
|
112
|
+
const DEFAULT_ANALYSIS_REASONING = 'low' as const
|
|
76
113
|
const DEFAULT_CHAPTER_CHARS = 3500
|
|
77
114
|
const DEFAULT_MAX_TOKENS = 12000
|
|
78
115
|
const DEFAULT_REVIEW_PASS_SCORE = 70
|
|
79
116
|
const DEFAULT_AUTO_REVIEW = true
|
|
117
|
+
const DEFAULT_AUTO_AUTHOR_REVIEW = true
|
|
118
|
+
const DEFAULT_AUTO_REVIEW_AFTER_REVISE = true
|
|
80
119
|
|
|
81
120
|
/** Order of the announcement section within the tool-guidance band. */
|
|
82
121
|
const SECTION_ORDER = 160
|
|
83
122
|
|
|
84
123
|
/** Model-facing announcement: plugin presence, capabilities, and limits. */
|
|
85
|
-
export const NOVEL_GUIDANCE = '本机已安装 dsh-novel-forge 插件(AI 编译小说工作台):侧边栏「小说工坊」入口。能力:读取 docx
|
|
124
|
+
export const NOVEL_GUIDANCE = '本机已安装 dsh-novel-forge 插件(AI 编译小说工作台):侧边栏「小说工坊」入口。能力:读取 docx 大纲或粘贴大纲文本;用 LLM 提炼道藏(人设/世界观/金手指规则/写作红线);生成卷计划与章节计划;逐章调用 LLM 生成 3000-4000 字正文并保存为 Markdown(默认输出到用户主目录 ~/.dsh/novels);每章自动生成摘要(叙事记忆)、自动 AI 审稿(人设/设定/红线/文笔/爽点/逻辑),支持按审稿意见重写、去 AI 味润色、暗线(伏笔)管理、批量连写与全本导出(txt/md)。限制:生成消耗 LLM API 额度;输出目录与模型可在插件设置中修改;章节正文质量取决于大纲完整度。用户提到「小说 / 大纲 / 写小说 / 章节 / 审稿 / 润色」时即指本插件,请据此协作。'
|
|
86
125
|
|
|
87
126
|
/** Resolve a config-like value into the full runtime config. */
|
|
88
127
|
export function resolveConfig(value: Partial<Config> | undefined): NovelConfig {
|
|
@@ -91,10 +130,21 @@ export function resolveConfig(value: Partial<Config> | undefined): NovelConfig {
|
|
|
91
130
|
outputDir: value?.outputDir ?? DEFAULT_OUTPUT_DIR,
|
|
92
131
|
provider: value?.provider ?? DEFAULT_PROVIDER,
|
|
93
132
|
model: value?.model ?? DEFAULT_MODEL,
|
|
133
|
+
generateModel: value?.generateModel,
|
|
134
|
+
reviewModel: value?.reviewModel,
|
|
135
|
+
auditModel: value?.auditModel,
|
|
136
|
+
reasoningEffort: value?.reasoningEffort ?? DEFAULT_REASONING_EFFORT,
|
|
137
|
+
analysisReasoning: value?.analysisReasoning ?? DEFAULT_ANALYSIS_REASONING,
|
|
94
138
|
chapterChars: value?.chapterChars ?? DEFAULT_CHAPTER_CHARS,
|
|
95
139
|
maxTokens: value?.maxTokens ?? DEFAULT_MAX_TOKENS,
|
|
96
140
|
reviewPassScore: value?.reviewPassScore ?? DEFAULT_REVIEW_PASS_SCORE,
|
|
97
141
|
autoReview: value?.autoReview ?? DEFAULT_AUTO_REVIEW,
|
|
142
|
+
autoAuthorReview: value?.autoAuthorReview ?? DEFAULT_AUTO_AUTHOR_REVIEW,
|
|
143
|
+
autoReviewAfterRevise: value?.autoReviewAfterRevise ?? DEFAULT_AUTO_REVIEW_AFTER_REVISE,
|
|
144
|
+
themeBackground: value?.themeBackground ?? '',
|
|
145
|
+
themeBackgroundBlur: value?.themeBackgroundBlur ?? 0,
|
|
146
|
+
themeOpacity: value?.themeOpacity ?? 100,
|
|
147
|
+
enableAdaptMode: value?.enableAdaptMode ?? true,
|
|
98
148
|
}
|
|
99
149
|
}
|
|
100
150
|
|
|
@@ -123,10 +173,21 @@ export function apply(ctx: Context, config?: Config): void {
|
|
|
123
173
|
if (patch.outputDir !== undefined) next.outputDir = patch.outputDir
|
|
124
174
|
if (patch.provider !== undefined) next.provider = patch.provider
|
|
125
175
|
if (patch.model !== undefined) next.model = patch.model
|
|
176
|
+
if (patch.generateModel !== undefined) next.generateModel = patch.generateModel
|
|
177
|
+
if (patch.reviewModel !== undefined) next.reviewModel = patch.reviewModel
|
|
178
|
+
if (patch.auditModel !== undefined) next.auditModel = patch.auditModel
|
|
179
|
+
if (patch.reasoningEffort !== undefined) next.reasoningEffort = patch.reasoningEffort
|
|
180
|
+
if (patch.analysisReasoning !== undefined) next.analysisReasoning = patch.analysisReasoning
|
|
126
181
|
if (patch.chapterChars !== undefined) next.chapterChars = patch.chapterChars
|
|
127
182
|
if (patch.maxTokens !== undefined) next.maxTokens = patch.maxTokens
|
|
128
183
|
if (patch.reviewPassScore !== undefined) next.reviewPassScore = patch.reviewPassScore
|
|
129
184
|
if (patch.autoReview !== undefined) next.autoReview = patch.autoReview
|
|
185
|
+
if (patch.autoAuthorReview !== undefined) next.autoAuthorReview = patch.autoAuthorReview
|
|
186
|
+
if (patch.autoReviewAfterRevise !== undefined) next.autoReviewAfterRevise = patch.autoReviewAfterRevise
|
|
187
|
+
if (patch.themeBackground !== undefined) next.themeBackground = patch.themeBackground
|
|
188
|
+
if (patch.themeBackgroundBlur !== undefined) next.themeBackgroundBlur = patch.themeBackgroundBlur
|
|
189
|
+
if (patch.themeOpacity !== undefined) next.themeOpacity = patch.themeOpacity
|
|
190
|
+
if (patch.enableAdaptMode !== undefined) next.enableAdaptMode = patch.enableAdaptMode
|
|
130
191
|
// Persist through the settings seam when available; otherwise keep in memory.
|
|
131
192
|
// (ctx.get is the non-strict service access — no inject requirement, same
|
|
132
193
|
// pattern installSettingsSection itself uses.)
|
|
@@ -171,7 +232,7 @@ export function apply(ctx: Context, config?: Config): void {
|
|
|
171
232
|
void value
|
|
172
233
|
}
|
|
173
234
|
|
|
174
|
-
|
|
235
|
+
ctx.settings.installSection(ctx, NOVEL_SETTINGS_NAMESPACE, Config, config ?? {}, {
|
|
175
236
|
setSource: (source) => {
|
|
176
237
|
current = source
|
|
177
238
|
sync()
|
|
@@ -179,6 +240,59 @@ export function apply(ctx: Context, config?: Config): void {
|
|
|
179
240
|
onChange: sync,
|
|
180
241
|
})
|
|
181
242
|
|
|
243
|
+
// 运行时 skill:章节批量生产与值守处理(只在本插件环境可用)。
|
|
244
|
+
// 通过 ctx.get('skills') 获取注册表(服务存在时注册,缺失则跳过,不影响其他能力)。
|
|
245
|
+
const skillsService = ctx.get('skills') as { register?: (skill: { name: string; provider: string; description: string; content: string }) => () => void } | undefined
|
|
246
|
+
// [disabled 2026-09-05] 运行时 skill 注册按用户要求暂时关闭(排查 .length 报错)。
|
|
247
|
+
const enableRuntimeSkill: boolean = false
|
|
248
|
+
if (enableRuntimeSkill && skillsService !== undefined && skillsService.register !== undefined) {
|
|
249
|
+
const disposeSkill = skillsService.register({
|
|
250
|
+
name: 'novel-forge-chapter-batch',
|
|
251
|
+
provider: 'dsh-novel-forge',
|
|
252
|
+
description: '批量生成小说章节并值守处理审稿未过的章节(豁免/按意见修订/验证模式/重新生成),依赖 dsh-novel-forge 的 /api/dsh-novel-forge/* 路由。',
|
|
253
|
+
content: [
|
|
254
|
+
'# 小说章节批量生产与值守处理',
|
|
255
|
+
'',
|
|
256
|
+
'## 0. 值守纪律(最高优先级,违反即空转)',
|
|
257
|
+
'1. 启动后台任务后立即停下,不轮询、不连续 wait、不做无意义动作——后台任务完成时运行时自动通知。',
|
|
258
|
+
'2. 只有收到「任务完成」通知或用户新消息时,才继续下一步(读结果→处理未过→汇报)。',
|
|
259
|
+
'3. 等待期间可做与当前任务无关的其他有用工作,但不得为等任务而空转。',
|
|
260
|
+
'4. 任务因 web 重启中断时:待 3080 恢复后,复位卡死章节(generating/error → reset)再续跑,并向用户说明中断原因。',
|
|
261
|
+
'',
|
|
262
|
+
'## 1. 前置检查',
|
|
263
|
+
'1. `GET /api/dsh-novel-forge/status` 确认服务在跑,读项目章节数。',
|
|
264
|
+
'2. 列出目标区间 pending 章节号,避免重复生成已处理章节。',
|
|
265
|
+
'3. **串行执行,禁止并行写 project.json**(并发会互相覆盖状态,实测教训)。',
|
|
266
|
+
'',
|
|
267
|
+
'## 2. 批量生成',
|
|
268
|
+
'逐章串行调用 `POST /generate` `{ chapterNo, skipReview: false }`(走完整质量门:生成→摘要+编年录→审稿→复盘)。',
|
|
269
|
+
'响应为 NDJSON,找 review 帧取 score/passed。每章 2-5 分钟;失败重试 1 次;后台跑并告知预计时长。',
|
|
270
|
+
'用 Node 脚本(fetch 逐章循环),避免 PowerShell 转义坑,用完删除。',
|
|
271
|
+
'',
|
|
272
|
+
'## 3. 未过章节分级处理',
|
|
273
|
+
'### A. 无 high → 豁免通过:`POST /chapter/approve`(主观项不磨)',
|
|
274
|
+
'### B. 有 high 且明确 → 按意见修订 + 验证模式:',
|
|
275
|
+
'1. 拼指令 `按审稿意见修订(优先处理):\n[severity] item → suggestion`(high 优先,无 high 取前 3 medium)',
|
|
276
|
+
'2. `POST /rewrite` 产草稿;3. `POST /chapter/check` 带 `previousReport` 走验证模式(只核对原意见解决+只挑新增 high)',
|
|
277
|
+
'4. 判定 `passed || 无 high` 可接受;5. `POST /draft/apply` 带 passed 报告落盘 approved',
|
|
278
|
+
'6. 不可接受 → 第二轮修订(指令更精确)→ 再验证;每章最多修 2 轮,仍不过保留草稿待人工',
|
|
279
|
+
'### C. 结构性 high(修订改不好)→ `POST /draft/discard` 后 `POST /generate` 重写,剩主观项再走 A',
|
|
280
|
+
'### D. error 状态 → `POST /chapter/reset` 后 `POST /generate`',
|
|
281
|
+
'注意:approved 但 review 有 high 的保留不动;修订指令越精确越有效(具体数字/行为一次就过)。',
|
|
282
|
+
'',
|
|
283
|
+
'## 4. 收尾',
|
|
284
|
+
'`GET /status` 验证目标区间全 approved;可选刷新剧情线(`POST /plotlines` op=refresh,注意只读最近 8 章摘要);汇报通过/未过/失败与遗留项。',
|
|
285
|
+
'',
|
|
286
|
+
'## 5. 已知陷阱',
|
|
287
|
+
'- 并发写 project.json 互相覆盖 → 必须串行',
|
|
288
|
+
'- rewrite 草稿可能偶发不落盘 → 应用前检查 pendingDraft,不存在则重跑',
|
|
289
|
+
'- PowerShell 调 API 引号/中文转义是坑 → 用 Node 脚本',
|
|
290
|
+
'- 验证模式 500 偶发 → 重试一次',
|
|
291
|
+
].join('\n'),
|
|
292
|
+
})
|
|
293
|
+
ctx.effect(() => disposeSkill, 'dsh-novel-forge: skill')
|
|
294
|
+
}
|
|
295
|
+
|
|
182
296
|
// Initial registration from the composition entry.
|
|
183
297
|
sync()
|
|
184
298
|
}
|
package/src/llm-live.ts
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* LLM 实况 feed(对齐上游「AI 创作实况 / LIVE LOG」的轻量实现)。
|
|
3
|
+
*
|
|
4
|
+
* 以「每次 LLM 调用」为粒度,把生命周期事件(开始/流式正文/阶段变化/完成)推给订阅者,
|
|
5
|
+
* 并提供环形缓冲供新连上的客户端重放。不持久化(与上游缓存不同,如需成 cache 再扩展)。
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
export type LlmLivePhase = 'requesting' | 'streaming' | 'completed' | 'failed'
|
|
9
|
+
|
|
10
|
+
export interface LlmLiveFrame {
|
|
11
|
+
type: 'session_started' | 'output_delta' | 'reasoning_delta' | 'phase_changed' | 'session_completed'
|
|
12
|
+
/** 由服务端生成,形如 ll-<n>。 */
|
|
13
|
+
sessionId: string
|
|
14
|
+
/** 本次调用用途(如 正文生成/审稿/章节规划)。 */
|
|
15
|
+
label?: string
|
|
16
|
+
/** 使用模型(用于展示)。 */
|
|
17
|
+
model?: string
|
|
18
|
+
/** 事件时间(ISO)。 */
|
|
19
|
+
at: string
|
|
20
|
+
/** session_started / 部分帧:交互上下文。 */
|
|
21
|
+
context?: { taskId?: string; interactionId: string }
|
|
22
|
+
/** output_delta / reasoning_delta:本次增量文本。 */
|
|
23
|
+
content?: string
|
|
24
|
+
/** session 累计字符数。 */
|
|
25
|
+
totalChars?: number
|
|
26
|
+
/** reasoning 累计字符数。 */
|
|
27
|
+
totalReasoningChars?: number
|
|
28
|
+
/** phase_changed:新阶段。 */
|
|
29
|
+
phase?: LlmLivePhase
|
|
30
|
+
/** phase_changed:阶段描述。 */
|
|
31
|
+
phaseMessage?: string
|
|
32
|
+
/** session_completed:最终预览(截断)。 */
|
|
33
|
+
preview?: string
|
|
34
|
+
/** session_completed:失败信息。 */
|
|
35
|
+
error?: string
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const MAX_BUFFER = 400
|
|
39
|
+
let buffer: LlmLiveFrame[] = []
|
|
40
|
+
type Listener = (frame: LlmLiveFrame) => void
|
|
41
|
+
let listeners = new Set<Listener>()
|
|
42
|
+
let seq = 0
|
|
43
|
+
|
|
44
|
+
export function nextSessionId(): string {
|
|
45
|
+
seq++
|
|
46
|
+
return `ll-${Date.now().toString(36)}-${seq}`
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export function subscribeLiveFeed(listener: Listener): () => void {
|
|
50
|
+
// 先重放现有缓冲(新连接能立即看到最近调用),再进入实时。
|
|
51
|
+
for (const frame of buffer) listener(frame)
|
|
52
|
+
listeners.add(listener)
|
|
53
|
+
return () => { listeners.delete(listener) }
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export function emitLive(frame: LlmLiveFrame): void {
|
|
57
|
+
buffer.push(frame)
|
|
58
|
+
if (buffer.length > MAX_BUFFER) buffer = buffer.slice(-MAX_BUFFER)
|
|
59
|
+
for (const l of listeners) l(frame)
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export function liveLatest(): LlmLiveFrame[] {
|
|
63
|
+
return [...buffer]
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export function clearLiveFeed(): void {
|
|
67
|
+
buffer = []
|
|
68
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 热门题材雷达:真实榜单扫榜(聚合各平台榜单源,容错)。
|
|
3
|
+
*/
|
|
4
|
+
import { collectMarketSource, MARKET_RADAR_SOURCES, type CollectedRankingItem, type MarketRadarPlatform, type MarketRadarListSource } from './market-radar-sources.ts'
|
|
5
|
+
|
|
6
|
+
export interface MarketRadarGroup {
|
|
7
|
+
platform: MarketRadarPlatform
|
|
8
|
+
platformLabel: string
|
|
9
|
+
listKey: string
|
|
10
|
+
listLabel: string
|
|
11
|
+
status: 'ok' | 'error'
|
|
12
|
+
error?: string
|
|
13
|
+
items: CollectedRankingItem[]
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export interface MarketScanResult {
|
|
17
|
+
scannedAt: string
|
|
18
|
+
groups: MarketRadarGroup[]
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
let lastScan: MarketScanResult | null = null
|
|
22
|
+
|
|
23
|
+
export function getLastMarketScan(): MarketScanResult | null {
|
|
24
|
+
return lastScan
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export async function scanMarketRanking(platforms?: string[]): Promise<MarketScanResult> {
|
|
28
|
+
const wanted = new Set<string>(platforms?.length ? platforms : ['fanqie', 'qidian', 'jinjiang'])
|
|
29
|
+
const sources = MARKET_RADAR_SOURCES.filter(s => wanted.has(s.platform))
|
|
30
|
+
const groups: MarketRadarGroup[] = []
|
|
31
|
+
await Promise.all(sources.map(async (source: MarketRadarListSource) => {
|
|
32
|
+
try {
|
|
33
|
+
const items = await collectMarketSource(source)
|
|
34
|
+
groups.push({ platform: source.platform, platformLabel: source.platformLabel, listKey: source.listKey, listLabel: source.listLabel, status: 'ok', items })
|
|
35
|
+
} catch (error) {
|
|
36
|
+
groups.push({ platform: source.platform, platformLabel: source.platformLabel, listKey: source.listKey, listLabel: source.listLabel, status: 'error', error: (error as Error).message, items: [] })
|
|
37
|
+
}
|
|
38
|
+
}))
|
|
39
|
+
lastScan = { scannedAt: new Date().toISOString(), groups }
|
|
40
|
+
return lastScan
|
|
41
|
+
}
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 「热门题材雷达」真实榜单抓取源(移植自 AI-Novel-Writing-Assistant marketRadarSources)。
|
|
3
|
+
* 用全局 fetch 抓取公开榜单移动版页面,正则抽取元数据;无第三方依赖。
|
|
4
|
+
*/
|
|
5
|
+
export type MarketRadarPlatform = 'fanqie' | 'qidian' | 'jinjiang'
|
|
6
|
+
|
|
7
|
+
export interface MarketRadarListSource {
|
|
8
|
+
platform: MarketRadarPlatform
|
|
9
|
+
platformLabel: string
|
|
10
|
+
listKey: string
|
|
11
|
+
listLabel: string
|
|
12
|
+
channel: string
|
|
13
|
+
sourceUrl: string
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export interface CollectedRankingItem {
|
|
17
|
+
rank: number
|
|
18
|
+
title: string
|
|
19
|
+
author?: string
|
|
20
|
+
category?: string
|
|
21
|
+
tags: string[]
|
|
22
|
+
synopsis?: string
|
|
23
|
+
heatLabel?: string
|
|
24
|
+
serialStatus?: string
|
|
25
|
+
sourceUrl: string
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export const MARKET_RADAR_SOURCES: MarketRadarListSource[] = [
|
|
29
|
+
{ platform: 'fanqie', platformLabel: '番茄小说', listKey: 'reading', listLabel: '阅读榜', channel: 'general', sourceUrl: 'https://fanqienovel.com/rank' },
|
|
30
|
+
{ platform: 'fanqie', platformLabel: '番茄小说', listKey: 'new_book', listLabel: '新书榜', channel: 'general', sourceUrl: 'https://fanqienovel.com/rank/1_1' },
|
|
31
|
+
{ platform: 'qidian', platformLabel: '起点中文网', listKey: 'hotsales', listLabel: '畅销榜', channel: 'male', sourceUrl: 'https://m.qidian.com/rank/hotsales/' },
|
|
32
|
+
{ platform: 'qidian', platformLabel: '起点中文网', listKey: 'monthly_ticket', listLabel: '月票榜', channel: 'male', sourceUrl: 'https://m.qidian.com/rank/yuepiao/' },
|
|
33
|
+
{ platform: 'qidian', platformLabel: '起点中文网', listKey: 'new_book', listLabel: '新书榜', channel: 'male', sourceUrl: 'https://m.qidian.com/rank/' },
|
|
34
|
+
{ platform: 'jinjiang', platformLabel: '晋江文学城', listKey: 'monthly', listLabel: '月度榜', channel: 'female', sourceUrl: 'https://m.jjwxc.net/rank/naturalmore/5' },
|
|
35
|
+
{ platform: 'jinjiang', platformLabel: '晋江文学城', listKey: 'quarterly', listLabel: '季度榜', channel: 'female', sourceUrl: 'https://m.jjwxc.net/rank/naturalmore/6' },
|
|
36
|
+
{ platform: 'jinjiang', platformLabel: '晋江文学城', listKey: 'new_author', listLabel: '新晋作者榜', channel: 'female', sourceUrl: 'https://m.jjwxc.net/rank/naturalmore/29' },
|
|
37
|
+
]
|
|
38
|
+
|
|
39
|
+
const NAMED_ENTITIES: Record<string, string> = {
|
|
40
|
+
amp: '&', apos: "'", gt: '>', lt: '<', nbsp: ' ', quot: '"',
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function decodeEntities(value: string): string {
|
|
44
|
+
return value.replace(/&(#x?[0-9a-f]+|[a-z]+);/gi, (_, key: string) => {
|
|
45
|
+
if (key.startsWith('#')) {
|
|
46
|
+
const hexadecimal = key[1]?.toLowerCase() === 'x'
|
|
47
|
+
const code = Number.parseInt(key.slice(hexadecimal ? 2 : 1), hexadecimal ? 16 : 10)
|
|
48
|
+
return Number.isFinite(code) ? String.fromCodePoint(code) : ''
|
|
49
|
+
}
|
|
50
|
+
return NAMED_ENTITIES[key.toLowerCase()] ?? ''
|
|
51
|
+
})
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function plainText(value: string | undefined): string {
|
|
55
|
+
return decodeEntities((value ?? '').replace(/<[^>]*>/g, ' ')).replace(/\s+/g, ' ').trim()
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function absoluteUrl(base: string, value: string): string {
|
|
59
|
+
return new URL(value.startsWith('//') ? `https:${value}` : value, base).toString()
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function extractTags(synopsis: string): string[] {
|
|
63
|
+
const bracketed = synopsis.match(/^[【〖\[]([^】〗\]]+)[】〗\]]/g) ?? []
|
|
64
|
+
return bracketed.flatMap((part) => plainText(part).replace(/^[【〖\[]|[】〗\]]$/g, '').split(/[++、|]/))
|
|
65
|
+
.map((tag) => tag.trim()).filter(Boolean).slice(0, 12)
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export function hasPrivateUseCharacters(value: string | null | undefined): boolean {
|
|
69
|
+
return /[\uE000-\uF8FF]/u.test(value ?? '')
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export function parseFanqieRanking(html: string, source: MarketRadarListSource): CollectedRankingItem[] {
|
|
73
|
+
const blocks = html.match(/<div class="rank-book-item">[\s\S]*?(?=<div class="rank-book-item">|<\/main>|<footer|<\/body>)/g) ?? []
|
|
74
|
+
return blocks.slice(0, 30).flatMap((block, index) => {
|
|
75
|
+
const titleMatch = block.match(/<div class="title">\s*<a href="([^"]+)"[^>]*>([\s\S]*?)<\/a>/)
|
|
76
|
+
if (!titleMatch) return []
|
|
77
|
+
const synopsis = plainText(block.match(/<div class="desc abstract[^>]*>([\s\S]*?)<\/div>/)?.[1])
|
|
78
|
+
const footer = plainText(block.match(/<div class="book-item-footer">([\s\S]*?)<\/div>/)?.[1])
|
|
79
|
+
return [{
|
|
80
|
+
rank: Number(block.match(/book-item-index"><h1>(\d+)<\/h1>/)?.[1] ?? index + 1),
|
|
81
|
+
title: plainText(titleMatch[2]),
|
|
82
|
+
author: plainText(block.match(/<div class="author">[\s\S]*?<span[^>]*>([\s\S]*?)<\/span>/)?.[1]),
|
|
83
|
+
tags: extractTags(synopsis),
|
|
84
|
+
synopsis: synopsis.slice(0, 800),
|
|
85
|
+
heatLabel: footer.match(/在读[^\s,。]*/)?.[0],
|
|
86
|
+
serialStatus: footer.match(/连载中|已完结/)?.[0],
|
|
87
|
+
sourceUrl: absoluteUrl(source.sourceUrl, titleMatch[1]),
|
|
88
|
+
}]
|
|
89
|
+
})
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export function parseFanqieDetail(html: string, item: CollectedRankingItem): CollectedRankingItem {
|
|
93
|
+
const title = plainText(html.match(/<h1[^>]*>([\s\S]*?)<\/h1>/)?.[1])
|
|
94
|
+
const author = plainText(html.match(/author-name-text[^>]*>([\s\S]*?)<\/a>/)?.[1])
|
|
95
|
+
const synopsis = plainText(html.match(/<div class="page-abstract-content"[^>]*>\s*<p[^>]*>([\s\S]*?)<\/p>/)?.[1])
|
|
96
|
+
if (!title || hasPrivateUseCharacters(title)) throw new Error('作品详情页未提供可读书名')
|
|
97
|
+
return {
|
|
98
|
+
...item,
|
|
99
|
+
title,
|
|
100
|
+
author: author && !hasPrivateUseCharacters(author) ? author : undefined,
|
|
101
|
+
synopsis: synopsis && !hasPrivateUseCharacters(synopsis) ? synopsis.slice(0, 800) : undefined,
|
|
102
|
+
tags: synopsis && !hasPrivateUseCharacters(synopsis) ? extractTags(synopsis) : [],
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export function parseQidianRanking(html: string, source: MarketRadarListSource): CollectedRankingItem[] {
|
|
107
|
+
const escapedLabel = source.listLabel.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
|
|
108
|
+
const itemPattern = new RegExp(`<a[^>]+href="([^"]*\\/book\\/[^"?]+)[^"]*"[^>]*>[\\s\\S]*?<h2[^>]+title="${escapedLabel}第(\\d+)位"[^>]*>([\\s\\S]*?)<\\/h2>[\\s\\S]*?<p[^>]*class="[^"]*subTitle[^"]*"[^>]*>([\\s\\S]*?)<\\/p>[\\s\\S]*?<\\/a>`, 'g')
|
|
109
|
+
return Array.from(html.matchAll(itemPattern)).slice(0, 30).map((match) => {
|
|
110
|
+
const subTitle = plainText(match[4])
|
|
111
|
+
const parts = subTitle.split('·').map((part) => part.trim()).filter(Boolean)
|
|
112
|
+
return {
|
|
113
|
+
rank: Number(match[2]),
|
|
114
|
+
title: plainText(match[3]),
|
|
115
|
+
author: parts[0],
|
|
116
|
+
category: parts[1],
|
|
117
|
+
tags: parts[1] ? [parts[1]] : [],
|
|
118
|
+
heatLabel: parts[2],
|
|
119
|
+
sourceUrl: absoluteUrl(source.sourceUrl, match[1]),
|
|
120
|
+
}
|
|
121
|
+
})
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
export function parseJinjiangRanking(html: string, source: MarketRadarListSource): CollectedRankingItem[] {
|
|
125
|
+
const items = Array.from(html.matchAll(/<li[^>]*>\s*<a href="(\/book2\/\d+)"[^>]*>([\s\S]*?)<\/a>[\s\S]*?<\/li>/g))
|
|
126
|
+
return items.slice(0, 30).map((match, index) => ({
|
|
127
|
+
rank: index + 1,
|
|
128
|
+
title: plainText(match[2]),
|
|
129
|
+
tags: [],
|
|
130
|
+
sourceUrl: absoluteUrl(source.sourceUrl, match[1]),
|
|
131
|
+
}))
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
async function fetchHtml(url: string): Promise<string> {
|
|
135
|
+
const response = await fetch(url, {
|
|
136
|
+
headers: { 'user-agent': 'Mozilla/5.0 (compatible; AI-Novel-Market-Radar/1.0; public-ranking-metadata-only)' },
|
|
137
|
+
signal: AbortSignal.timeout(20_000),
|
|
138
|
+
})
|
|
139
|
+
if (!response.ok) throw new Error(`榜单页面返回 HTTP ${response.status}`)
|
|
140
|
+
const bytes = await response.arrayBuffer()
|
|
141
|
+
const headerCharset = response.headers.get('content-type')?.match(/charset=([^;]+)/i)?.[1]?.trim()
|
|
142
|
+
const metaProbe = new TextDecoder('latin1').decode(bytes.slice(0, 4096))
|
|
143
|
+
const metaCharset = metaProbe.match(/charset\s*=\s*["']?([\w-]+)/i)?.[1]
|
|
144
|
+
const charset = headerCharset || metaCharset || 'utf-8'
|
|
145
|
+
return new TextDecoder(charset).decode(bytes)
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
async function hydrateFanqieItems(items: CollectedRankingItem[]): Promise<CollectedRankingItem[]> {
|
|
149
|
+
const hydrated: CollectedRankingItem[] = []
|
|
150
|
+
for (let offset = 0; offset < items.length; offset += 4) {
|
|
151
|
+
const batch = await Promise.all(items.slice(offset, offset + 4).map(async (item) => {
|
|
152
|
+
const containsObfuscatedText = [item.title, item.author, item.synopsis].some(hasPrivateUseCharacters)
|
|
153
|
+
if (!containsObfuscatedText) return item
|
|
154
|
+
try { return parseFanqieDetail(await fetchHtml(item.sourceUrl), item) }
|
|
155
|
+
catch { return null }
|
|
156
|
+
}))
|
|
157
|
+
hydrated.push(...batch.filter((item): item is CollectedRankingItem => item !== null))
|
|
158
|
+
}
|
|
159
|
+
return hydrated
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
export async function collectMarketSource(source: MarketRadarListSource): Promise<CollectedRankingItem[]> {
|
|
163
|
+
const html = await fetchHtml(source.sourceUrl)
|
|
164
|
+
const parsers: Record<MarketRadarPlatform, (value: string, item: MarketRadarListSource) => CollectedRankingItem[]> = {
|
|
165
|
+
fanqie: parseFanqieRanking,
|
|
166
|
+
qidian: parseQidianRanking,
|
|
167
|
+
jinjiang: parseJinjiangRanking,
|
|
168
|
+
}
|
|
169
|
+
const parsed = parsers[source.platform](html, source)
|
|
170
|
+
const items = source.platform === 'fanqie' ? await hydrateFanqieItems(parsed) : parsed
|
|
171
|
+
if (items.length === 0) throw new Error('榜单页面结构可能已变化,未识别到公开作品元数据')
|
|
172
|
+
return items
|
|
173
|
+
}
|