@waterwx/dsh-novel-forge 1.10.1 → 2.0.0
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 +287 -281
- package/lib/client.js +19572 -18127
- package/lib/index.js +12171 -6932
- package/lib/types/action-guard.d.ts +61 -0
- package/lib/types/ai-scan.d.ts +30 -0
- package/lib/types/assets.d.ts +20 -9
- package/lib/types/assistant.d.ts +29 -0
- package/lib/types/author-assets.d.ts +1 -3
- package/lib/types/bookshelf.d.ts +13 -3
- package/lib/types/client/api.d.ts +137 -21
- package/lib/types/client/chip-prefs.d.ts +36 -0
- package/lib/types/client/docx.d.ts +2 -2
- package/lib/types/client/index.d.ts +1 -1
- package/lib/types/client/llmLive.d.ts +31 -0
- package/lib/types/client/locales.d.ts +15 -70
- package/lib/types/client/mount.d.ts +0 -2
- package/lib/types/client/panel/AiTag.d.ts +1 -0
- package/lib/types/client/panel/AssistantTab.d.ts +4 -1
- package/lib/types/client/panel/AuthorHome.d.ts +13 -9
- package/lib/types/client/panel/BookAnalysisView.d.ts +4 -0
- package/lib/types/client/panel/CmdPalette.d.ts +12 -0
- package/lib/types/client/panel/CreateBookView.d.ts +5 -1
- package/lib/types/client/panel/DirectorView.d.ts +7 -0
- package/lib/types/client/panel/IdeaInspirationView.d.ts +6 -0
- package/lib/types/client/panel/KnowledgeBaseView.d.ts +4 -0
- package/lib/types/client/panel/LiveFeedLog.d.ts +13 -0
- package/lib/types/client/panel/MarketRadarView.d.ts +5 -0
- package/lib/types/client/panel/ModelManager.d.ts +2 -2
- package/lib/types/client/panel/NovelPanel.d.ts +6 -1
- package/lib/types/client/panel/PageHeader.d.ts +18 -0
- package/lib/types/client/panel/PromptSlotsView.d.ts +11 -0
- package/lib/types/client/panel/SettingsView.d.ts +12 -7
- package/lib/types/client/panel/ShelfView.d.ts +10 -1
- package/lib/types/client/panel/SubPage.d.ts +27 -0
- package/lib/types/client/panel/TensionView.d.ts +18 -0
- package/lib/types/client/panel/TimelineView.d.ts +18 -0
- package/lib/types/client/panel/helpers.d.ts +5 -3
- package/lib/types/client/panel/market-radar-state.d.ts +56 -0
- package/lib/types/client/panel/toast.d.ts +2 -0
- package/lib/types/client/panel/views.d.ts +21 -35
- package/lib/types/client/run-status.d.ts +21 -0
- package/lib/types/client/sidebar-entry.d.ts +0 -2
- package/lib/types/docx-core.d.ts +16 -0
- package/lib/types/docx.d.ts +2 -3
- package/lib/types/engine.d.ts +83 -79
- package/lib/types/global-assets.d.ts +6 -0
- package/lib/types/home.d.ts +11 -0
- package/lib/types/index.d.ts +27 -13
- package/lib/types/llm-live.d.ts +70 -0
- package/lib/types/llm-retry.d.ts +38 -0
- package/lib/types/market-radar-scan.d.ts +18 -0
- package/lib/types/market-radar-sources.d.ts +31 -0
- package/lib/types/migrate-output.d.ts +25 -0
- package/lib/types/model-capability.d.ts +93 -0
- package/lib/types/novel-context.d.ts +80 -0
- package/lib/types/output-budget.d.ts +86 -0
- package/lib/types/prompt-slots.d.ts +57 -0
- package/lib/types/prompting.d.ts +10 -0
- package/lib/types/protocol.d.ts +829 -522
- package/lib/types/revision.d.ts +76 -0
- package/lib/types/routes.d.ts +9 -3
- package/lib/types/run.d.ts +23 -1
- package/lib/types/settings-persist.d.ts +59 -0
- package/lib/types/snapshots.d.ts +46 -0
- package/lib/types/stage-contract.d.ts +58 -0
- package/lib/types/tension.d.ts +53 -0
- package/lib/types/timeline.d.ts +37 -0
- package/package.json +50 -28
- package/src/action-guard.ts +127 -0
- package/src/ai-scan.ts +155 -0
- package/src/assets.ts +1801 -535
- package/src/assistant.ts +1098 -770
- package/src/author-assets.ts +131 -147
- package/src/bookshelf.ts +193 -167
- package/src/client/api.ts +767 -607
- package/src/client/chip-prefs.ts +121 -0
- package/src/client/docx.ts +32 -69
- package/src/client/index.ts +3 -1
- package/src/client/llmLive.ts +111 -0
- package/src/client/locales.ts +15 -274
- package/src/client/mount.tsx +4 -3
- package/src/client/panel/AdaptModeView.tsx +535 -520
- package/src/client/panel/AiTag.tsx +7 -0
- package/src/client/panel/AssetsTab.tsx +448 -458
- package/src/client/panel/AssistantTab.tsx +355 -301
- package/src/client/panel/AuthorAssetsView.tsx +205 -207
- package/src/client/panel/AuthorHome.tsx +309 -202
- package/src/client/panel/BookAnalysisView.tsx +63 -0
- package/src/client/panel/CmdPalette.tsx +102 -0
- package/src/client/panel/CreateBookView.tsx +366 -352
- package/src/client/panel/DirectorView.tsx +200 -0
- package/src/client/panel/GlobalAssetLibraryView.tsx +168 -172
- package/src/client/panel/IdeaInspirationView.tsx +76 -0
- package/src/client/panel/ImportModal.tsx +302 -302
- package/src/client/panel/KnowledgeBaseView.tsx +125 -0
- package/src/client/panel/LiveFeedLog.tsx +204 -0
- package/src/client/panel/MarketRadarView.tsx +415 -0
- package/src/client/panel/ModelManager.tsx +205 -205
- package/src/client/panel/NovelPanel.tsx +5559 -4655
- package/src/client/panel/PageHeader.tsx +37 -0
- package/src/client/panel/ProgressConsole.tsx +63 -64
- package/src/client/panel/PromptSlotsView.tsx +97 -0
- package/src/client/panel/ReaderView.tsx +306 -306
- package/src/client/panel/RunPanel.tsx +56 -24
- package/src/client/panel/SettingsView.tsx +175 -112
- package/src/client/panel/ShelfView.tsx +63 -39
- package/src/client/panel/SubPage.tsx +42 -0
- package/src/client/panel/TensionView.tsx +212 -0
- package/src/client/panel/TimelineView.tsx +208 -0
- package/src/client/panel/WorldTab.tsx +202 -202
- package/src/client/panel/helpers.ts +7 -5
- package/src/client/panel/market-radar-state.ts +79 -0
- package/src/client/panel/panel.module.css +5767 -4199
- package/src/client/panel/toast.ts +51 -0
- package/src/client/panel/views.tsx +316 -314
- package/src/client/run-status.ts +171 -0
- package/src/client/sidebar-entry.ts +3 -3
- package/src/docx-core.ts +53 -0
- package/src/docx.ts +44 -83
- package/src/engine.ts +1610 -1541
- package/src/global-assets.ts +58 -0
- package/src/home.ts +33 -0
- package/src/index.ts +184 -103
- package/src/llm-live.ts +245 -0
- package/src/llm-retry.ts +77 -0
- package/src/market-radar-scan.ts +35 -0
- package/src/market-radar-sources.ts +173 -0
- package/src/migrate-output.ts +76 -0
- package/src/model-capability.ts +215 -0
- package/src/novel-context.ts +286 -0
- package/src/output-budget.ts +147 -0
- package/src/prompt-slots.ts +125 -0
- package/src/prompting.ts +26 -0
- package/src/protocol.ts +2454 -2148
- package/src/revision.ts +283 -0
- package/src/routes.ts +3795 -3359
- package/src/run.ts +113 -17
- package/src/settings-persist.ts +114 -0
- package/src/snapshots.ts +174 -0
- package/src/stage-contract.ts +186 -0
- package/src/tension.ts +251 -0
- package/src/timeline.ts +159 -0
- package/assets/styles/arcane-thick-3d.webp +0 -0
- package/assets/styles/chinese-ink.webp +0 -0
- package/assets/styles/clay-stopmotion-3d.webp +0 -0
- package/assets/styles/dark-ghibli.webp +0 -0
- package/assets/styles/dark-gothic-3d.webp +0 -0
- package/assets/styles/film-photo.webp +0 -0
- package/assets/styles/guofeng-heavy.webp +0 -0
- package/assets/styles/horror-game-3d.webp +0 -0
- package/assets/styles/japan-cel-dark.webp +0 -0
- package/assets/styles/korean-webtoon.webp +0 -0
- package/assets/styles/low-cyberpunk-3d.webp +0 -0
- package/assets/styles/nextgen-cg.webp +0 -0
- package/assets/styles/noir-film.webp +0 -0
- package/assets/styles/openworld-survival.webp +0 -0
- package/assets/styles/paper-cut-3d.webp +0 -0
- package/assets/styles/pixar-adult-3d.webp +0 -0
- package/assets/styles/pop-marvel-2d.webp +0 -0
- package/assets/styles/shinkai-anime.webp +0 -0
- package/assets/styles/tactical-stealth.webp +0 -0
- package/assets/styles/vaporwave-3d.webp +0 -0
- package/assets/styles/vhs90.webp +0 -0
- package/lib/types/client/panel/BookshelfBar.d.ts +0 -11
- package/lib/types/client/panel/FlowGuide.d.ts +0 -21
- package/lib/types/client/panel/MangaRoleLibrary.d.ts +0 -18
- package/lib/types/client/panel/MangaWorkspace.d.ts +0 -15
- package/lib/types/client/panel/ModelPicker.d.ts +0 -11
- package/lib/types/client/panel/ProgressHomeView.d.ts +0 -5
- package/lib/types/client/panel/SceneLibrary.d.ts +0 -14
- package/lib/types/client/panel/StoryboardTab.d.ts +0 -19
- package/lib/types/client/panel/StyleCard.d.ts +0 -8
- package/lib/types/client/panel/StyleGallery.d.ts +0 -1
- package/lib/types/comic-presets.d.ts +0 -12
- package/lib/types/shot-language.d.ts +0 -46
- package/lib/types/story-beat-language.d.ts +0 -35
- package/lib/types/style-library.d.ts +0 -33
- package/skills/chapter-batch/SKILL.md +0 -81
- package/src/client/mount.tsx._new +0 -97
- package/src/client/mount.tsx._nf +0 -97
- package/src/client/mount.tsx._tmp +0 -97
- package/src/client/panel/BookshelfBar.tsx +0 -170
- package/src/client/panel/FlowGuide.tsx +0 -83
- package/src/client/panel/MangaRoleLibrary.tsx +0 -810
- package/src/client/panel/MangaWorkspace.tsx +0 -359
- package/src/client/panel/ProgressHomeView.tsx +0 -87
- package/src/client/panel/SceneLibrary.tsx +0 -396
- package/src/client/panel/StoryboardTab.tsx +0 -466
- package/src/client/panel/StyleCard.tsx +0 -90
- package/src/client/panel/StyleGallery.tsx +0 -58
- package/src/comic-presets.ts +0 -67
- package/src/shot-language.ts +0 -181
- package/src/story-beat-language.ts +0 -130
- package/src/style-library.ts +0 -78
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 模型参数能力兼容层。
|
|
3
|
+
*
|
|
4
|
+
* 设计原则:**界面上保存的温度/思考档位是用户偏好,不是最终请求值。** 构造请求前
|
|
5
|
+
* 必须经过本层收敛到厂商允许的范围 —— 否则换模型、走自定义中转、或厂商改了约束,
|
|
6
|
+
* 就会出现「换个 provider 就报参数错误」这类故障。
|
|
7
|
+
*
|
|
8
|
+
* 两条硬要求:
|
|
9
|
+
* 1. **按模型身份判断,不按 provider 判断**。同一个模型经由自定义中转调用时,
|
|
10
|
+
* provider 名可能完全自定义,但参数约束属于模型本身;把判断写成
|
|
11
|
+
* `provider === '某个厂商' && ...` 会让走中转的调用绕过约束。
|
|
12
|
+
* 2. **约束要有出处**。每条规则的来源写在规则上,不靠猜。下列规则来自厂商官方
|
|
13
|
+
* 《模型参数参考》(https://platform.kimi.com/docs/api/models-overview.md):
|
|
14
|
+
* · kimi-k3 : temperature 固定 1.0;支持顶层 reasoning_effort
|
|
15
|
+
* · kimi-k2.7-code : temperature 固定 1.0;不支持 reasoning_effort(用 thinking,且不可禁用)
|
|
16
|
+
* · kimi-k2.6 : thinking 固定 1.0 / 非 thinking 固定 0.6;不支持 reasoning_effort
|
|
17
|
+
* 官方同时建议:这类模型**不要显式传入 temperature**。本层因此优先「不传」,
|
|
18
|
+
* 仅在调用方无法省略该参数时才钳到允许值。
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
/** 一次请求的参数约束。 */
|
|
22
|
+
export interface ModelCapability {
|
|
23
|
+
/** 该模型只接受这一个温度值(如 Kimi 全系),缺省表示不约束。 */
|
|
24
|
+
fixedTemperature?: number
|
|
25
|
+
/** 温度允许的下界(默认 0)。 */
|
|
26
|
+
minTemperature?: number
|
|
27
|
+
/** 温度允许的上界(默认 2,主流 OpenAI 兼容区间)。 */
|
|
28
|
+
maxTemperature?: number
|
|
29
|
+
/**
|
|
30
|
+
* 是否支持 OpenAI 兼容的 `reasoning_effort`。
|
|
31
|
+
* false 表示**不得下发**该参数(如 Kimi K2.x 用的是 `thinking`),下发会被厂商拒绝。
|
|
32
|
+
*/
|
|
33
|
+
supportsReasoning?: boolean
|
|
34
|
+
/**
|
|
35
|
+
* 推理控制方式:`reasoning_effort`(OpenAI 兼容顶层参数)或 `thinking`(Kimi K2.x 专属)。
|
|
36
|
+
* 运行时若适配层支持,应按此选择参数名;仅为诊断与后续适配保留。
|
|
37
|
+
*/
|
|
38
|
+
reasoningControl?: 'reasoning-effort' | 'thinking' | 'none'
|
|
39
|
+
/** 严格结构化输出必须关闭思考(覆盖用户偏好)。 */
|
|
40
|
+
reasoningForcedOff?: boolean
|
|
41
|
+
/** 命中的规则名,用于日志与测试断言。 */
|
|
42
|
+
rule: string
|
|
43
|
+
/** 约束出处(文档 URL 或说明),便于复核与更新。 */
|
|
44
|
+
source: string
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** 一道匹配规则:先命中先生效。 */
|
|
48
|
+
interface CapabilityRule {
|
|
49
|
+
/** 规则名。 */
|
|
50
|
+
rule: string
|
|
51
|
+
/** 约束出处。 */
|
|
52
|
+
source: string
|
|
53
|
+
/** 模型 id 归一化后是否命中。 */
|
|
54
|
+
matches: (canonical: string) => boolean
|
|
55
|
+
capability: Omit<ModelCapability, 'rule' | 'source'>
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const KIMI_SOURCE = 'https://platform.kimi.com/docs/api/models-overview.md'
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* 把模型 id 归一化为「模型身份」。
|
|
62
|
+
*
|
|
63
|
+
* 处理:去空白与首尾斜杠、小写、剥掉供应商前缀(`kimi/kimi-k3` → `kimi-k3`)。
|
|
64
|
+
* @param model - 原始模型 id。
|
|
65
|
+
* @returns 归一化后的身份(小写、无前缀)。
|
|
66
|
+
*/
|
|
67
|
+
export function canonicalModelId(model: string | undefined): string {
|
|
68
|
+
if (typeof model !== 'string') return ''
|
|
69
|
+
const trimmed = model.trim().toLowerCase().replace(/^\/+|\/+$/g, '')
|
|
70
|
+
if (trimmed === '') return ''
|
|
71
|
+
const segments = trimmed.split('/')
|
|
72
|
+
return segments[segments.length - 1] ?? ''
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const RULES: CapabilityRule[] = [
|
|
76
|
+
{
|
|
77
|
+
rule: 'kimi-k3-fixed-temperature',
|
|
78
|
+
source: KIMI_SOURCE,
|
|
79
|
+
// 只命中 k3 本身;k2.6 / k2.7 另立规则(约束不同)。
|
|
80
|
+
matches: (m) => /^kimi[-_.]?k-?3(?:[-_.]|$)/.test(m),
|
|
81
|
+
capability: { fixedTemperature: 1, supportsReasoning: true, reasoningControl: 'reasoning-effort' },
|
|
82
|
+
},
|
|
83
|
+
{
|
|
84
|
+
rule: 'kimi-k2.7-code-fixed-temperature',
|
|
85
|
+
source: KIMI_SOURCE,
|
|
86
|
+
matches: (m) => /^kimi[-_.]?k-?2[-_.]?7(?:[-_.]|$)/.test(m),
|
|
87
|
+
capability: {
|
|
88
|
+
fixedTemperature: 1,
|
|
89
|
+
// 不支持 reasoning_effort;思考由 thinking 控制且不可禁用。
|
|
90
|
+
supportsReasoning: false,
|
|
91
|
+
reasoningControl: 'thinking',
|
|
92
|
+
reasoningForcedOff: true,
|
|
93
|
+
},
|
|
94
|
+
},
|
|
95
|
+
{
|
|
96
|
+
rule: 'kimi-k2.6-temperature-by-thinking',
|
|
97
|
+
source: KIMI_SOURCE,
|
|
98
|
+
matches: (m) => /^kimi[-_.]?k-?2[-_.]?6(?:[-_.]|$)/.test(m),
|
|
99
|
+
capability: {
|
|
100
|
+
// 思考模式 1.0 / 非思考 0.6;本插件的结构化任务默认关闭思考,故取 0.6 不安全,
|
|
101
|
+
// 统一收敛到 1.0(思考档),避免"关思考却给 1.0"这类组合出错。
|
|
102
|
+
fixedTemperature: 1,
|
|
103
|
+
supportsReasoning: false,
|
|
104
|
+
reasoningControl: 'thinking',
|
|
105
|
+
},
|
|
106
|
+
},
|
|
107
|
+
{
|
|
108
|
+
rule: 'deepseek-reasoner-fixed-temperature',
|
|
109
|
+
source: 'OpenAI 兼容约定:DeepSeek R1/reasoner 不接受自定义 temperature',
|
|
110
|
+
matches: (m) => /^deepseek[-_.]?(r1|reasoner)(?:[-_.]|$)/.test(m),
|
|
111
|
+
capability: { fixedTemperature: 1, supportsReasoning: true, reasoningControl: 'reasoning-effort' },
|
|
112
|
+
},
|
|
113
|
+
]
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* 解析一条模型的能力约束。
|
|
117
|
+
* @param model - 模型 id(可能带供应商前缀)。
|
|
118
|
+
* @returns 命中的能力;无约束时返回 undefined。
|
|
119
|
+
*/
|
|
120
|
+
export function resolveModelCapability(model: string | undefined): ModelCapability | undefined {
|
|
121
|
+
const canonical = canonicalModelId(model)
|
|
122
|
+
if (canonical === '') return undefined
|
|
123
|
+
for (const rule of RULES) {
|
|
124
|
+
if (rule.matches(canonical)) return { ...rule.capability, rule: rule.rule, source: rule.source }
|
|
125
|
+
}
|
|
126
|
+
return undefined
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/** 一次待收敛的请求参数。 */
|
|
130
|
+
export interface RequestParams {
|
|
131
|
+
temperature: number
|
|
132
|
+
/** 请求方(含用户偏好)希望使用的思考档位;undefined 表示不涉及。 */
|
|
133
|
+
reasoningEffort?: 'off' | 'low' | 'high' | 'max'
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/** 收敛结果:最终请求值 + 发生了哪些调整(便于日志与诊断)。 */
|
|
137
|
+
export interface ConvergedParams {
|
|
138
|
+
/**
|
|
139
|
+
* 最终要下发的温度;`undefined` 表示**应当省略该参数**。
|
|
140
|
+
* 厂商建议这类模型不要显式传 temperature 时,本层返回 undefined。
|
|
141
|
+
*/
|
|
142
|
+
temperature: number | undefined
|
|
143
|
+
reasoningEffort?: 'off' | 'low' | 'high' | 'max'
|
|
144
|
+
/** 被本层改掉的项,形如 `temperature: 0.3 -> 省略`。 */
|
|
145
|
+
adjustments: string[]
|
|
146
|
+
/** 命中的规则名。 */
|
|
147
|
+
rule?: string
|
|
148
|
+
/** 约束出处。 */
|
|
149
|
+
source?: string
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* 把请求参数收敛到模型允许的范围。
|
|
154
|
+
*
|
|
155
|
+
* @param params - 调用方希望使用的参数。
|
|
156
|
+
* @param model - 实际要调用的模型 id(应传**实际模型**;换备用模型后需重算)。
|
|
157
|
+
* @param options - `structured: true` 表示这是严格结构化输出任务;
|
|
158
|
+
* `omitFixedTemperature: true` 表示调用方可以不传温度(推荐,厂商建议如此),
|
|
159
|
+
* 此时固定温度会收敛为 undefined 而不是钳到固定值。
|
|
160
|
+
* @returns 最终请求值与被调整项。
|
|
161
|
+
*/
|
|
162
|
+
export function convergeRequestParams(
|
|
163
|
+
params: RequestParams,
|
|
164
|
+
model: string | undefined,
|
|
165
|
+
options: { structured?: boolean; omitFixedTemperature?: boolean } = {},
|
|
166
|
+
): ConvergedParams {
|
|
167
|
+
const capability = resolveModelCapability(model)
|
|
168
|
+
const adjustments: string[] = []
|
|
169
|
+
let temperature: number | undefined = params.temperature
|
|
170
|
+
let reasoningEffort = params.reasoningEffort
|
|
171
|
+
|
|
172
|
+
if (capability?.fixedTemperature !== undefined) {
|
|
173
|
+
const fixed = capability.fixedTemperature
|
|
174
|
+
if (options.omitFixedTemperature === true) {
|
|
175
|
+
if (temperature !== undefined) {
|
|
176
|
+
adjustments.push(`temperature: ${temperature} -> 省略(${capability.rule} 固定 ${fixed},厂商建议不要显式传入)`)
|
|
177
|
+
}
|
|
178
|
+
temperature = undefined
|
|
179
|
+
} else if (temperature !== fixed) {
|
|
180
|
+
adjustments.push(`temperature: ${temperature} -> ${fixed}(${capability.rule} 固定值)`)
|
|
181
|
+
temperature = fixed
|
|
182
|
+
}
|
|
183
|
+
} else if (capability !== undefined) {
|
|
184
|
+
const min = capability.minTemperature ?? 0
|
|
185
|
+
const max = capability.maxTemperature ?? 2
|
|
186
|
+
const clamped = Math.min(max, Math.max(min, temperature ?? min))
|
|
187
|
+
if (clamped !== temperature) {
|
|
188
|
+
adjustments.push(`temperature: ${temperature} -> ${clamped}(${capability.rule} 取值范围)`)
|
|
189
|
+
temperature = clamped
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
// 不支持 reasoning_effort 的模型:不得下发该参数,否则厂商直接报错。
|
|
194
|
+
if (capability?.supportsReasoning === false && reasoningEffort !== undefined) {
|
|
195
|
+
if (reasoningEffort !== 'off') {
|
|
196
|
+
adjustments.push(`reasoningEffort: ${reasoningEffort} -> 省略(${capability.rule} 不支持该参数)`)
|
|
197
|
+
}
|
|
198
|
+
reasoningEffort = undefined
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
// 严格结构化任务:能力档案要求关闭思考时,覆盖用户偏好(输出稳定性优先)。
|
|
202
|
+
if (options.structured === true && capability?.reasoningForcedOff === true && reasoningEffort !== undefined) {
|
|
203
|
+
adjustments.push(`reasoningEffort: ${reasoningEffort} -> off(结构化任务强制关闭思考)`)
|
|
204
|
+
reasoningEffort = 'off'
|
|
205
|
+
// 该模型通过 thinking 控制且不可禁用:等价于不传任何推理参数。
|
|
206
|
+
if (capability.reasoningControl === 'thinking') reasoningEffort = undefined
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
return {
|
|
210
|
+
temperature,
|
|
211
|
+
reasoningEffort,
|
|
212
|
+
adjustments,
|
|
213
|
+
...(capability === undefined ? {} : { rule: capability.rule, source: capability.source }),
|
|
214
|
+
}
|
|
215
|
+
}
|
|
@@ -0,0 +1,286 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 统一上下文组装与相关设定检索
|
|
3
|
+
*
|
|
4
|
+
* 取代各阶段各自拼装 prompt,消除「生成看得到、审稿看不到」的断层。
|
|
5
|
+
* 所有阶段通过 buildChapterContext 获取同一来源的事实与设定。
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import type { ProjectState, ChapterPlan, StoryBible, RoleRecord, ChapterFact, Foreshadow, Plotline } from './protocol'
|
|
9
|
+
import { readFileSync, existsSync } from 'node:fs'
|
|
10
|
+
import { join } from 'node:path'
|
|
11
|
+
|
|
12
|
+
export interface ChapterContext {
|
|
13
|
+
/** 平台合规红线(永远全量) */
|
|
14
|
+
complianceRedLines: string[]
|
|
15
|
+
/** 本书写作红线(永远全量) */
|
|
16
|
+
bookRedLines: string[]
|
|
17
|
+
/** 当前章相关角色卡(全量信息) */
|
|
18
|
+
relevantRoles: Array<{ name: string; role: string; traits: string[]; goals?: string; knowledge?: string[] }>
|
|
19
|
+
/** 当前章相关道藏规则 */
|
|
20
|
+
relevantWorldRules: string[]
|
|
21
|
+
/** 活跃剧情线 */
|
|
22
|
+
activePlotlines: Plotline[]
|
|
23
|
+
/** 未回收伏笔 */
|
|
24
|
+
activeForeshadows: Foreshadow[]
|
|
25
|
+
/** 上一章结尾原文 */
|
|
26
|
+
prevChapterTail: string
|
|
27
|
+
/** 上一章摘要 */
|
|
28
|
+
prevChapterSummary?: string
|
|
29
|
+
/** 最近事实(近因记忆,top20) */
|
|
30
|
+
recentFacts: string[]
|
|
31
|
+
/** 相关旧事实(按本章 beats 检索,top15) */
|
|
32
|
+
relatedFacts: string[]
|
|
33
|
+
/** 当前卷大纲 */
|
|
34
|
+
currentVolumeOutline: string
|
|
35
|
+
/** 本章 beats */
|
|
36
|
+
beats: string
|
|
37
|
+
/** 本章标题 */
|
|
38
|
+
chapterTitle: string
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export interface ContextBuildOptions {
|
|
42
|
+
/** 阶段:writing/review/revise/polish/plan */
|
|
43
|
+
stage?: 'writing' | 'review' | 'revise' | 'polish' | 'plan'
|
|
44
|
+
/** 是否注入完整角色卡(false 时只注入角色名+定位) */
|
|
45
|
+
fullRoleCards?: boolean
|
|
46
|
+
/** 相关事实检索上限 */
|
|
47
|
+
relatedFactsLimit?: number
|
|
48
|
+
/** 最近事实数量 */
|
|
49
|
+
recentFactsLimit?: number
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* 相关事实检索:trigram 重合度 + 角色名命中加权 + 近因加权
|
|
54
|
+
* 从 generateChapterStream 抽取,扩展为通用函数。
|
|
55
|
+
*/
|
|
56
|
+
export function retrieveRelatedFacts(
|
|
57
|
+
facts: ChapterFact[],
|
|
58
|
+
beatsText: string,
|
|
59
|
+
roleNames: string[],
|
|
60
|
+
limit = 15,
|
|
61
|
+
): string[] {
|
|
62
|
+
if (facts.length === 0) return []
|
|
63
|
+
|
|
64
|
+
const trigrams = (s: string): Set<string> => {
|
|
65
|
+
const out = new Set<string>()
|
|
66
|
+
for (let i = 0; i + 3 <= s.length; i++) {
|
|
67
|
+
const tri = s.slice(i, i + 3)
|
|
68
|
+
if (tri.trim() !== '') out.add(tri)
|
|
69
|
+
}
|
|
70
|
+
return out
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const beatsTri = trigrams(beatsText)
|
|
74
|
+
const beatRoles = roleNames.filter(n => beatsText.includes(n))
|
|
75
|
+
|
|
76
|
+
const recentTexts = new Set(facts.slice(-20).map(f => f.text))
|
|
77
|
+
|
|
78
|
+
return facts
|
|
79
|
+
.map((f, idx) => {
|
|
80
|
+
const head = f.text.slice(0, 80)
|
|
81
|
+
let score = 0
|
|
82
|
+
for (const tri of trigrams(head)) if (beatsTri.has(tri)) score += 1
|
|
83
|
+
if (beatRoles.length > 0) {
|
|
84
|
+
for (const n of beatRoles) if (head.includes(n)) score += 8
|
|
85
|
+
}
|
|
86
|
+
// 近因加权:越新越优先(封顶 40 章)
|
|
87
|
+
score += Math.min(idx, 40) / 10
|
|
88
|
+
return { f, score }
|
|
89
|
+
})
|
|
90
|
+
.filter(x => x.score >= 3)
|
|
91
|
+
.sort((a, b) => b.score - a.score)
|
|
92
|
+
.slice(0, limit)
|
|
93
|
+
.map(x => `[第${x.f.chapterNo}章] ${x.f.text}`)
|
|
94
|
+
.filter(t => !recentTexts.has(t.slice(t.indexOf(']') + 2)))
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* 相关角色检索:beats 中出现的角色 + 主角/主要反派永远全量
|
|
99
|
+
*/
|
|
100
|
+
export function retrieveRelevantRoles(
|
|
101
|
+
bible: StoryBible | undefined,
|
|
102
|
+
roles: RoleRecord[] | undefined,
|
|
103
|
+
beatsText: string,
|
|
104
|
+
): ChapterContext['relevantRoles'] {
|
|
105
|
+
const result: ChapterContext['relevantRoles'] = []
|
|
106
|
+
const seen = new Set<string>()
|
|
107
|
+
|
|
108
|
+
// 主角/主要反派永远全量
|
|
109
|
+
const alwaysRoles = (bible?.characters ?? []).filter(c =>
|
|
110
|
+
c.role === 'protagonist' || c.role === 'antagonist'
|
|
111
|
+
)
|
|
112
|
+
for (const card of alwaysRoles) {
|
|
113
|
+
if (seen.has(card.name)) continue
|
|
114
|
+
seen.add(card.name)
|
|
115
|
+
result.push({
|
|
116
|
+
name: card.name,
|
|
117
|
+
role: card.role,
|
|
118
|
+
traits: card.traits,
|
|
119
|
+
goals: card.goals !== '' ? card.goals : undefined,
|
|
120
|
+
knowledge: Array.isArray(card.knowledge) && card.knowledge.length > 0 ? card.knowledge : undefined,
|
|
121
|
+
})
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// beats 中出现的角色
|
|
125
|
+
const beatRoles = (bible?.characters ?? []).filter(c => beatsText.includes(c.name))
|
|
126
|
+
for (const card of beatRoles) {
|
|
127
|
+
if (seen.has(card.name)) continue
|
|
128
|
+
seen.add(card.name)
|
|
129
|
+
result.push({
|
|
130
|
+
name: card.name,
|
|
131
|
+
role: card.role,
|
|
132
|
+
traits: card.traits,
|
|
133
|
+
goals: card.goals !== '' ? card.goals : undefined,
|
|
134
|
+
knowledge: Array.isArray(card.knowledge) && card.knowledge.length > 0 ? card.knowledge : undefined,
|
|
135
|
+
})
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
// 角色库补充(不在道藏里的)
|
|
139
|
+
for (const r of roles ?? []) {
|
|
140
|
+
if (seen.has(r.name)) continue
|
|
141
|
+
if (!beatsText.includes(r.name) && r.roleLabel !== 'protagonist' && r.roleLabel !== 'antagonist') continue
|
|
142
|
+
seen.add(r.name)
|
|
143
|
+
result.push({
|
|
144
|
+
name: r.name,
|
|
145
|
+
role: r.roleLabel,
|
|
146
|
+
traits: Array.isArray(r.traits) ? r.traits : [],
|
|
147
|
+
goals: r.goals !== undefined && r.goals !== '' ? r.goals : undefined,
|
|
148
|
+
})
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
return result
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* 构建章节统一上下文
|
|
156
|
+
*
|
|
157
|
+
* 各阶段调用此函数获取同一来源的事实与设定,消除上下文断层。
|
|
158
|
+
* 检索结果不足时自动回退全量,保证不丢失关键信息。
|
|
159
|
+
*/
|
|
160
|
+
export function buildChapterContext(
|
|
161
|
+
project: ProjectState,
|
|
162
|
+
chapter: ChapterPlan,
|
|
163
|
+
outputDir: string,
|
|
164
|
+
options: ContextBuildOptions = {},
|
|
165
|
+
): ChapterContext {
|
|
166
|
+
const {
|
|
167
|
+
fullRoleCards = true,
|
|
168
|
+
relatedFactsLimit = 15,
|
|
169
|
+
recentFactsLimit = 20,
|
|
170
|
+
} = options
|
|
171
|
+
|
|
172
|
+
const bible = project.bible
|
|
173
|
+
const allFacts = project.facts ?? []
|
|
174
|
+
const roleNames = (project.roles ?? []).map(r => r.name).filter((n): n is string => typeof n === 'string' && n !== '')
|
|
175
|
+
|
|
176
|
+
// 上一章结尾 + 摘要
|
|
177
|
+
let prevChapterTail = ''
|
|
178
|
+
let prevChapterSummary: string | undefined
|
|
179
|
+
const prev = project.chapters.find(c => c.no === chapter.no - 1)
|
|
180
|
+
if (prev?.file !== undefined) {
|
|
181
|
+
try {
|
|
182
|
+
const prevPath = join(outputDir, prev.file)
|
|
183
|
+
if (existsSync(prevPath)) {
|
|
184
|
+
const text = readFileSync(prevPath, 'utf8')
|
|
185
|
+
prevChapterTail = text.replace(/^#\s+.*$/m, '').trim().slice(-900)
|
|
186
|
+
}
|
|
187
|
+
} catch { /* 文件缺失时忽略 */ }
|
|
188
|
+
}
|
|
189
|
+
prevChapterSummary = prev?.summary
|
|
190
|
+
|
|
191
|
+
// 事实:最近 + 相关
|
|
192
|
+
const recentFacts = allFacts.slice(-recentFactsLimit).map(f => `[第${f.chapterNo}章] ${f.text}`)
|
|
193
|
+
const relatedFacts = retrieveRelatedFacts(allFacts, chapter.beats, roleNames, relatedFactsLimit)
|
|
194
|
+
|
|
195
|
+
// 角色:相关角色
|
|
196
|
+
const relevantRoles = retrieveRelevantRoles(bible, project.roles, chapter.beats)
|
|
197
|
+
|
|
198
|
+
// 道藏规则:全部(规则数量通常不多,全量注入更安全)
|
|
199
|
+
const relevantWorldRules = bible?.worldRules ?? []
|
|
200
|
+
|
|
201
|
+
// 活跃剧情线
|
|
202
|
+
const activePlotlines = (project.plotlines ?? []).filter(l =>
|
|
203
|
+
l.status === 'active' || l.status === 'paused'
|
|
204
|
+
)
|
|
205
|
+
|
|
206
|
+
// 未回收伏笔
|
|
207
|
+
const activeForeshadows = (project.foreshadows ?? []).filter(f =>
|
|
208
|
+
f.status === 'planted' || f.status === 'progressing'
|
|
209
|
+
)
|
|
210
|
+
|
|
211
|
+
// 当前卷大纲
|
|
212
|
+
const volumeNo = chapter.volume ?? 0
|
|
213
|
+
const volume = project.volumes?.find(v => v.no === volumeNo)
|
|
214
|
+
const currentVolumeOutline = volume !== undefined
|
|
215
|
+
? `第${volume.no}卷《${volume.title}》:${volume.summary}`
|
|
216
|
+
: ''
|
|
217
|
+
|
|
218
|
+
return {
|
|
219
|
+
complianceRedLines: [], // 由调用方注入 COMPLIANCE_REDLINES
|
|
220
|
+
bookRedLines: bible?.redLines ?? [],
|
|
221
|
+
relevantRoles: fullRoleCards ? relevantRoles : relevantRoles.map(r => ({ name: r.name, role: r.role, traits: [] })),
|
|
222
|
+
relevantWorldRules,
|
|
223
|
+
activePlotlines,
|
|
224
|
+
activeForeshadows,
|
|
225
|
+
prevChapterTail,
|
|
226
|
+
prevChapterSummary,
|
|
227
|
+
recentFacts,
|
|
228
|
+
relatedFacts,
|
|
229
|
+
currentVolumeOutline,
|
|
230
|
+
beats: chapter.beats,
|
|
231
|
+
chapterTitle: chapter.title,
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/**
|
|
236
|
+
* 将上下文渲染为 prompt 文本块
|
|
237
|
+
* 各阶段可按需选择注入哪些块。
|
|
238
|
+
*/
|
|
239
|
+
export function renderContextBlocks(ctx: ChapterContext): {
|
|
240
|
+
rolesBlock: string
|
|
241
|
+
worldRulesBlock: string
|
|
242
|
+
factsBlock: string
|
|
243
|
+
plotlinesBlock: string
|
|
244
|
+
foreshadowsBlock: string
|
|
245
|
+
continuityBlock: string
|
|
246
|
+
redLinesBlock: string
|
|
247
|
+
} {
|
|
248
|
+
const roleName = { protagonist: '主角', supporting: '配角', antagonist: '反派', other: '其他', female_lead: '女主', female_support: '女配', support: '配角', extra: '路人' } as Record<string, string>
|
|
249
|
+
|
|
250
|
+
const rolesBlock = ctx.relevantRoles.length > 0
|
|
251
|
+
? '==================== 相关角色卡 ====================\n' +
|
|
252
|
+
ctx.relevantRoles.map(r =>
|
|
253
|
+
`- ${r.name}(${roleName[r.role] ?? r.role}):${r.traits.join('、')}${r.goals ? `;目标:${r.goals}` : ''}${r.knowledge ? `\n 已知信息:${r.knowledge.join(';')}(未列出的该角色不知道)` : ''}`
|
|
254
|
+
).join('\n')
|
|
255
|
+
: ''
|
|
256
|
+
|
|
257
|
+
const worldRulesBlock = ctx.relevantWorldRules.length > 0
|
|
258
|
+
? '==================== 世界规则 ====================\n' + ctx.relevantWorldRules.map(r => `- ${r}`).join('\n')
|
|
259
|
+
: ''
|
|
260
|
+
|
|
261
|
+
const factsBlock = (ctx.recentFacts.length > 0 || ctx.relatedFacts.length > 0)
|
|
262
|
+
? '==================== 事实库(最近 + 相关) ====================\n' +
|
|
263
|
+
(ctx.recentFacts.length > 0 ? `【最近 ${ctx.recentFacts.length} 条】\n${ctx.recentFacts.join('\n')}` : '') +
|
|
264
|
+
(ctx.relatedFacts.length > 0 ? `\n【相关旧事实 ${ctx.relatedFacts.length} 条】\n${ctx.relatedFacts.join('\n')}` : '')
|
|
265
|
+
: ''
|
|
266
|
+
|
|
267
|
+
const plotlinesBlock = ctx.activePlotlines.length > 0
|
|
268
|
+
? '==================== 活跃剧情线 ====================\n' +
|
|
269
|
+
ctx.activePlotlines.map(l => `- [${l.kind}${l.status === 'paused' ? '·暂停' : ''}] ${l.name}:${l.goal}${l.progress ? `(进度:${l.progress})` : ''}`).join('\n')
|
|
270
|
+
: ''
|
|
271
|
+
|
|
272
|
+
const foreshadowsBlock = ctx.activeForeshadows.length > 0
|
|
273
|
+
? '==================== 活跃伏笔 ====================\n' +
|
|
274
|
+
ctx.activeForeshadows.map(f => `- [${f.status === 'planted' ? '已埋设' : '推进中'}] ${f.description}${f.targetChapter ? `(预计第${f.targetChapter}章回收)` : ''}`).join('\n')
|
|
275
|
+
: ''
|
|
276
|
+
|
|
277
|
+
const continuityBlock = ctx.prevChapterTail !== ''
|
|
278
|
+
? `==================== 上一章结尾(紧接此状态继续) ====================\n${ctx.prevChapterTail}`
|
|
279
|
+
: ''
|
|
280
|
+
|
|
281
|
+
const redLinesBlock = ctx.bookRedLines.length > 0
|
|
282
|
+
? '==================== 本书红线 ====================\n' + ctx.bookRedLines.map(r => `- ${r}`).join('\n')
|
|
283
|
+
: ''
|
|
284
|
+
|
|
285
|
+
return { rolesBlock, worldRulesBlock, factsBlock, plotlinesBlock, foreshadowsBlock, continuityBlock, redLinesBlock }
|
|
286
|
+
}
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 结构化输出预算与字段长度合同。
|
|
3
|
+
*
|
|
4
|
+
* 背景:本插件此前给每个 LLM 调用写死一个 `Math.max(config.maxTokens, N)`。短任务
|
|
5
|
+
* 与长任务拿到同一个额度,长任务(分卷、拆章、逐章规划)**额度不随规模增长**,
|
|
6
|
+
* 截断后只能抛「请增大 maxTokens」。这里把额度改成按规模推算,并给结构化字段
|
|
7
|
+
* 定长度上限,让预算花在结构上而不是被自由发挥吃光。
|
|
8
|
+
*
|
|
9
|
+
* 设计取舍:结构化输出的额度应当**随本次要产出的条目数增长**,并由硬上限兜住,
|
|
10
|
+
* 否则要么短任务浪费额度,要么长任务写到一半被截断。字段长度上限同时写进提示词
|
|
11
|
+
* 与解析端,两处共用一张表,避免漂移。
|
|
12
|
+
*
|
|
13
|
+
* 一条硬纪律:**不要用 JSON repair 去补被截断的结构**。repair 只能修已有结构,
|
|
14
|
+
* 补不出尚未输出的条目;在服务层手工补齐缺失项,等于把不完整的规划伪装成可用
|
|
15
|
+
* 资产。预算给够 + 输出够紧凑,才是正解。
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
/** 一次调用的输出规模描述:给出能决定输出体量的那个量。 */
|
|
19
|
+
export interface OutputScope {
|
|
20
|
+
/** 预期卷数(分卷/卷战略类)。 */
|
|
21
|
+
volumes?: number
|
|
22
|
+
/** 预期章节数(章节列表/批量拆章类)。 */
|
|
23
|
+
chapters?: number
|
|
24
|
+
/** 预期角色数(角色阵容/资产提炼类)。 */
|
|
25
|
+
characters?: number
|
|
26
|
+
/** 预期条目数(伏笔、题材、资产等清单类)。 */
|
|
27
|
+
items?: number
|
|
28
|
+
/** 需要覆盖的正文规模(字),用于拆书/反推这类按输入体量走的任务。 */
|
|
29
|
+
sourceChars?: number
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** 一类结构化任务的预算规格。 */
|
|
33
|
+
export interface OutputBudgetSpec {
|
|
34
|
+
/** 无视规模也要给到的下限。 */
|
|
35
|
+
floor: number
|
|
36
|
+
/** 规模为 0 时的起步额度。 */
|
|
37
|
+
base: number
|
|
38
|
+
/** 每单位规模追加的额度。 */
|
|
39
|
+
perUnit: number
|
|
40
|
+
/** 硬上限:超过它只会让模型啰嗦,不会让结构更完整。 */
|
|
41
|
+
ceiling: number
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** 结构化任务分组——按"输出里每个单位要写多少字段"分档,而不是按业务名。 */
|
|
45
|
+
export type OutputBudgetKind =
|
|
46
|
+
| 'volume-strategy'
|
|
47
|
+
| 'chapter-list'
|
|
48
|
+
| 'character-cast'
|
|
49
|
+
| 'item-list'
|
|
50
|
+
| 'analysis'
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* 数值口径:以**本插件真实产出字段的体积**折算,不是照搬参考项目的数字
|
|
54
|
+
* (它的卷战略字段与本插件的章节字段结构不同,直接套用会造成新的截断)。
|
|
55
|
+
*
|
|
56
|
+
* 折算假设:中文约 1.5-2 token/字;账本式 JSON 的键名与括号再占约 15%。
|
|
57
|
+
* 例如单章条目 = 标题(≤40) + 摘要(≤300) + 本章目标(≤200) + beats + 最多 4 条
|
|
58
|
+
* payoff 指令 → 约 250-400 字 → **约 350 token/章**。若字段合同调整,这里必须同步。
|
|
59
|
+
*/
|
|
60
|
+
const SPECS: Record<OutputBudgetKind, OutputBudgetSpec> = {
|
|
61
|
+
// 每卷:roleLabel(≤32) + coreReward(≤64) + escalationFocus(≤64) + uncertainty,
|
|
62
|
+
// 再加书级阶梯字段(readerRewardLadder/escalationLadder/midpointShift/notes)。
|
|
63
|
+
'volume-strategy': { floor: 1_800, base: 1_200, perUnit: 220, ceiling: 6_000 },
|
|
64
|
+
// 每章 ≈350 token(见上方折算说明),另留写作余量;上限对齐单次调用能力。
|
|
65
|
+
'chapter-list': { floor: 2_500, base: 2_000, perUnit: 350, ceiling: 20_000 },
|
|
66
|
+
// 每角色:名字 + roleLabel + 定位 + 关系 + 弧线 ≈150 token。
|
|
67
|
+
'character-cast': { floor: 1_500, base: 1_000, perUnit: 150, ceiling: 8_000 },
|
|
68
|
+
// 清单类,每条几个短字段。
|
|
69
|
+
'item-list': { floor: 1_000, base: 800, perUnit: 60, ceiling: 4_000 },
|
|
70
|
+
// 拆书/反推:按输入正文体量走,每千字约 120 token。
|
|
71
|
+
analysis: { floor: 3_000, base: 2_000, perUnit: 12, ceiling: 16_000 },
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* 取出与任务类型相关的那一个规模量。
|
|
76
|
+
* @param kind - 任务类型。
|
|
77
|
+
* @param scope - 规模描述。
|
|
78
|
+
* @returns 单位数;分析类返回千字单位(向上取整)。
|
|
79
|
+
*/
|
|
80
|
+
export function scopeUnits(kind: OutputBudgetKind, scope: OutputScope): number {
|
|
81
|
+
const positive = (value: number | undefined): number =>
|
|
82
|
+
typeof value === 'number' && Number.isFinite(value) && value > 0 ? value : 0
|
|
83
|
+
switch (kind) {
|
|
84
|
+
case 'volume-strategy':
|
|
85
|
+
return positive(scope.volumes)
|
|
86
|
+
case 'chapter-list':
|
|
87
|
+
return positive(scope.chapters)
|
|
88
|
+
case 'character-cast':
|
|
89
|
+
return positive(scope.characters)
|
|
90
|
+
case 'item-list':
|
|
91
|
+
return positive(scope.items)
|
|
92
|
+
case 'analysis':
|
|
93
|
+
// 输入正文每 1000 字算一个单位。
|
|
94
|
+
return Math.ceil(positive(scope.sourceChars) / 1_000)
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* 按规模推算输出额度。
|
|
100
|
+
* @param kind - 结构化任务类型。
|
|
101
|
+
* @param scope - 本次任务的规模(缺省时按最小规模处理)。
|
|
102
|
+
* @returns 建议的 `maxTokens`,落在该类型的 [floor, ceiling] 内。
|
|
103
|
+
*/
|
|
104
|
+
export function resolveOutputBudget(kind: OutputBudgetKind, scope: OutputScope = {}): number {
|
|
105
|
+
const spec = SPECS[kind]
|
|
106
|
+
const raw = spec.base + scopeUnits(kind, scope) * spec.perUnit
|
|
107
|
+
return Math.min(spec.ceiling, Math.max(spec.floor, Math.round(raw)))
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** 读取某类任务的规格(供诊断与测试断言)。 */
|
|
111
|
+
export function outputBudgetSpec(kind: OutputBudgetKind): OutputBudgetSpec {
|
|
112
|
+
return { ...SPECS[kind] }
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* 结构化字段长度合同 —— 与 prompt 里的长度声明必须逐项一致。
|
|
117
|
+
*
|
|
118
|
+
* 两边同步的理由:只写进 schema,模型不知道就会超长被拒;只写进 prompt,模型
|
|
119
|
+
* 偶尔超长没人拦。本表是单一数据源。
|
|
120
|
+
*/
|
|
121
|
+
export const STRUCTURED_FIELD_LIMITS = {
|
|
122
|
+
/** 分卷战略:书级阶梯类字段。 */
|
|
123
|
+
volumeLadder: 160,
|
|
124
|
+
/** 分卷战略:每卷定位标签。 */
|
|
125
|
+
volumeRoleLabel: 32,
|
|
126
|
+
/** 分卷战略:每卷核心回报 / 升级焦点。 */
|
|
127
|
+
volumeReward: 64,
|
|
128
|
+
/** 分卷战略:不确定项原因。 */
|
|
129
|
+
volumeUncertaintyReason: 64,
|
|
130
|
+
/** 章节计划:单章标题。 */
|
|
131
|
+
chapterTitle: 40,
|
|
132
|
+
/** 章节计划:单章摘要。 */
|
|
133
|
+
chapterSummary: 300,
|
|
134
|
+
/** 章节计划:本章目标。 */
|
|
135
|
+
chapterGoal: 200,
|
|
136
|
+
} as const
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* 把长度合同渲染成可直接塞进 system prompt 的一句话,避免 prompt 与表漂移。
|
|
140
|
+
* @param entries - 要声明的字段(描述 + 上限),按声明顺序渲染。
|
|
141
|
+
* @returns 形如「为保证输出完整:A 不超过 160 字;B 不超过 64 字。」的中文说明。
|
|
142
|
+
*/
|
|
143
|
+
export function renderLengthContract(entries: ReadonlyArray<readonly [string, number]>): string {
|
|
144
|
+
if (entries.length === 0) return ''
|
|
145
|
+
const parts = entries.map(([label, limit]) => `${label}不超过 ${limit} 字`)
|
|
146
|
+
return `为保证输出完整:${parts.join(';')}。每个字段只保留完成本阶段决策所需的信息,写完后立即结束输出,不要展开细纲或重复说明。`
|
|
147
|
+
}
|