@cloud411716/fancy-webnovel 2.2.0 → 3.0.3

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 (212) hide show
  1. package/README.md +65 -0
  2. package/client.js +201 -15
  3. package/cordis.patch.yml +5 -1
  4. package/docs/douyin-plugin.md +118 -0
  5. package/docs/material-library/README.md +404 -0
  6. package/docs/material-library/import.js +437 -0
  7. package/docs/material-library/prompt.md +370 -0
  8. package/docs/material-library/queries.surql +94 -0
  9. package/docs/material-library/sample.jsonl +14 -0
  10. package/docs/material-library/schema.surql +46 -0
  11. package/docs/mcp-profile.example.yml +24 -0
  12. package/docs/story-graph-design.md +602 -0
  13. package/docs/story-graph-validation.md +81 -0
  14. package/docs/workspace-mcp.md +284 -0
  15. package/index.js +12 -7
  16. package/lib/atomic-write.js +109 -0
  17. package/lib/chapter-order.js +100 -0
  18. package/lib/confirmation.js +80 -0
  19. package/lib/db-init.js +123 -0
  20. package/lib/deployed-state.js +11 -11
  21. package/lib/direction-labels.js +265 -0
  22. package/lib/error-codes.js +31 -31
  23. package/lib/infra.js +29 -4
  24. package/lib/material-db.js +149 -0
  25. package/lib/path-guard.js +150 -0
  26. package/lib/projects.js +400 -0
  27. package/lib/scraper-utils.js +140 -0
  28. package/lib/surreal-server.js +338 -0
  29. package/lib/topic-decision.js +860 -0
  30. package/lib/topic-fields.js +251 -0
  31. package/lib/topic-schema.js +225 -0
  32. package/lib/workspace.js +501 -0
  33. package/package.json +31 -7
  34. package/plugins/fancy-analyze/analyze-definitions.md +519 -0
  35. package/plugins/fancy-analyze/index.js +1128 -77
  36. package/plugins/fancy-analyze/ingest-coverage.js +107 -0
  37. package/plugins/fancy-analyze/schema-analyze.md +355 -107
  38. package/plugins/fancy-analyze/scripts/append-summary-chapter.js +612 -0
  39. package/plugins/fancy-analyze/summary-progress.js +71 -7
  40. package/plugins/fancy-analyze/templates.js +437 -139
  41. package/plugins/fancy-bootstrap/index.js +276 -131
  42. package/plugins/fancy-bootstrap/scripts/init.js +59 -25
  43. package/plugins/fancy-bootstrap/templates.js +35 -18
  44. package/plugins/fancy-db/index.js +172 -0
  45. package/plugins/fancy-douyin/deploy.js +134 -0
  46. package/plugins/fancy-douyin/index.js +551 -0
  47. package/plugins/fancy-douyin/prompts/01-analysis-prompt.md +84 -0
  48. package/plugins/fancy-douyin/scripts/crawl-note.cjs +194 -0
  49. package/plugins/fancy-douyin/scripts/lib.cjs +197 -0
  50. package/plugins/fancy-douyin/scripts/scan-account.cjs +128 -0
  51. package/plugins/fancy-douyin/scripts/start-chrome.cjs +203 -0
  52. package/plugins/fancy-douyin/state.js +197 -0
  53. package/plugins/fancy-douyin/templates.js +127 -0
  54. package/plugins/fancy-export/exporter.js +805 -0
  55. package/plugins/fancy-export/index.js +63 -0
  56. package/plugins/fancy-import/import-driver.js +29 -6
  57. package/plugins/fancy-import/index.js +13 -5
  58. package/plugins/fancy-import/prompts/01-import-prompt.md +39 -14
  59. package/plugins/fancy-import/templates.js +17 -8
  60. package/plugins/fancy-material/exporter.js +153 -0
  61. package/plugins/fancy-material/genres.js +256 -0
  62. package/plugins/fancy-material/index.js +835 -0
  63. package/plugins/fancy-material/inventory.js +145 -0
  64. package/plugins/fancy-material/kinds.js +313 -0
  65. package/plugins/fancy-material/queue.js +55 -0
  66. package/plugins/fancy-material/scripts/query.js +254 -0
  67. package/plugins/fancy-material/templates.js +258 -0
  68. package/plugins/fancy-material/tool-deploy.js +138 -0
  69. package/plugins/fancy-material/validate.js +149 -0
  70. package/plugins/fancy-project/index.js +133 -0
  71. package/plugins/fancy-scan/existing-block.js +100 -0
  72. package/plugins/fancy-scan/index.js +410 -21
  73. package/plugins/fancy-scan/prompts/01-analysis-prompt.md +302 -0
  74. package/plugins/fancy-scan/scripts/append-block.js +725 -0
  75. package/plugins/fancy-scan/scripts/report-validate.js +824 -0
  76. package/plugins/fancy-scan/scripts/run-scan.js +169 -22
  77. package/plugins/fancy-scan/scripts/scrapers/fanqie-rank-scraper.js +145 -16
  78. package/plugins/fancy-scan/scripts/scrapers/jjwxc-rank-scraper.js +179 -49
  79. package/plugins/fancy-scan/scripts/scrapers/qimao-rank-scraper.js +38 -20
  80. package/plugins/fancy-scan/templates/blocks.js +155 -0
  81. package/plugins/fancy-scan/templates/platforms.js +372 -0
  82. package/plugins/fancy-scan/templates/report-schema.js +90 -0
  83. package/plugins/fancy-scan/templates.js +545 -146
  84. package/plugins/fancy-scan/tool-deploy.js +335 -0
  85. package/plugins/fancy-scan/wait-turn.js +46 -0
  86. package/plugins/fancy-story-builder/db/README.md +106 -0
  87. package/plugins/fancy-story-builder/db/client.js +228 -0
  88. package/plugins/fancy-story-builder/db/commands.js +145 -0
  89. package/plugins/fancy-story-builder/db/ids.js +39 -0
  90. package/plugins/fancy-story-builder/db/import.js +798 -0
  91. package/plugins/fancy-story-builder/db/project.js +299 -0
  92. package/plugins/fancy-story-builder/db/query.js +361 -0
  93. package/plugins/fancy-story-builder/db/schema.js +417 -0
  94. package/plugins/fancy-story-builder/db/stage-sync.js +238 -0
  95. package/plugins/fancy-story-builder/flesh-output.js +19 -6
  96. package/plugins/fancy-story-builder/index.js +4105 -396
  97. package/plugins/fancy-story-builder/patch-driver.js +390 -0
  98. package/plugins/fancy-story-builder/prompts/01-skeleton-prompt.md +29 -19
  99. package/plugins/fancy-story-builder/prompts/02-flesh-prompt.md +72 -10
  100. package/plugins/fancy-story-builder/prompts/03-rhythm-prompt.md +40 -5
  101. package/plugins/fancy-story-builder/prompts/04-writing-prompt.md +37 -0
  102. package/plugins/fancy-story-builder/purpose-template.md +42 -66
  103. package/plugins/fancy-story-builder/references/formulas-skeleton.json +229 -56
  104. package/plugins/fancy-story-builder/references/genre-profiles.json +3514 -0
  105. package/plugins/fancy-story-builder/references/glossary.md +15 -6
  106. package/plugins/fancy-story-builder/references/index.md +97 -6
  107. package/plugins/fancy-story-builder/references//350/212/202/345/245/217/346/214/207/345/257/274/00-index.md +1 -1
  108. package/plugins/fancy-story-builder/references//350/212/202/345/245/217/346/214/207/345/257/274/07-audit.md +25 -7
  109. package/plugins/fancy-story-builder/references//350/241/200/350/202/211/346/214/207/345/257/274/01-character.md +5 -1
  110. package/plugins/fancy-story-builder/references//350/241/200/350/202/211/346/214/207/345/257/274/03-relations.md +6 -2
  111. package/plugins/fancy-story-builder/references//350/241/200/350/202/211/346/214/207/345/257/274/05-audit.md +15 -5
  112. package/plugins/fancy-story-builder/references//351/252/250/346/236/266/346/214/207/345/257/274/00-index.md +1 -1
  113. package/plugins/fancy-story-builder/references//351/252/250/346/236/266/346/214/207/345/257/274/01-positioning.md +2 -1
  114. package/plugins/fancy-story-builder/references//351/252/250/346/236/266/346/214/207/345/257/274/03-entities.md +1 -1
  115. package/plugins/fancy-story-builder/references//351/252/250/346/236/266/346/214/207/345/257/274/06-audit.md +19 -1
  116. package/plugins/fancy-story-builder/schema-setting.json +1100 -0
  117. package/plugins/fancy-story-builder/schema-setting.md +201 -293
  118. package/plugins/fancy-story-builder/scripts/brief-validate.js +164 -0
  119. package/plugins/fancy-story-builder/scripts/content-quality.js +4 -1
  120. package/plugins/fancy-story-builder/scripts/flesh-validate.js +135 -12
  121. package/plugins/fancy-story-builder/scripts/graph-utils.js +146 -47
  122. package/plugins/fancy-story-builder/scripts/reference-log.js +110 -0
  123. package/plugins/fancy-story-builder/scripts/rhythm-validate.js +100 -4
  124. package/plugins/fancy-story-builder/scripts/schema-pages.js +2668 -0
  125. package/plugins/fancy-story-builder/scripts/semantic-lint.js +5 -1
  126. package/plugins/fancy-story-builder/scripts/skeleton-validate.js +158 -17
  127. package/plugins/fancy-story-builder/scripts/stage-batches.js +36 -2
  128. package/plugins/fancy-story-builder/scripts/stage-contract.js +12 -0
  129. package/plugins/fancy-story-builder/scripts/story-artifacts.js +933 -0
  130. package/plugins/fancy-story-builder/scripts/upstream-query.js +1 -1
  131. package/plugins/fancy-story-builder/scripts/v2/baseline.js +112 -0
  132. package/plugins/fancy-story-builder/scripts/v2/cast.js +517 -0
  133. package/plugins/fancy-story-builder/scripts/v2/context.js +434 -0
  134. package/plugins/fancy-story-builder/scripts/v2/contracts.js +493 -0
  135. package/plugins/fancy-story-builder/scripts/v2/core.js +419 -0
  136. package/plugins/fancy-story-builder/scripts/v2/engine.js +1068 -0
  137. package/plugins/fancy-story-builder/scripts/v2/export-markdown.js +794 -0
  138. package/plugins/fancy-story-builder/scripts/v2/genre.js +195 -0
  139. package/plugins/fancy-story-builder/scripts/v2/knowledge.js +321 -0
  140. package/plugins/fancy-story-builder/scripts/v2/macro.js +409 -0
  141. package/plugins/fancy-story-builder/scripts/v2/manifest.js +538 -0
  142. package/plugins/fancy-story-builder/scripts/v2/migrate.js +137 -0
  143. package/plugins/fancy-story-builder/scripts/v2/prompts.js +1335 -0
  144. package/plugins/fancy-story-builder/scripts/v2/rhythm.js +618 -0
  145. package/plugins/fancy-story-builder/scripts/v2/scale.js +843 -0
  146. package/plugins/fancy-story-builder/scripts/v2/snapshot.js +121 -0
  147. package/plugins/fancy-story-builder/scripts/v2/stages.js +273 -0
  148. package/plugins/fancy-story-builder/scripts/v2/writing.js +216 -0
  149. package/plugins/fancy-story-builder/storydb/README.md +271 -0
  150. package/plugins/fancy-story-builder/storydb/cli.js +458 -0
  151. package/plugins/fancy-story-builder/storydb/client.js +351 -0
  152. package/plugins/fancy-story-builder/storydb/compile.js +452 -0
  153. package/plugins/fancy-story-builder/storydb/context.js +288 -0
  154. package/plugins/fancy-story-builder/storydb/definitions.js +699 -0
  155. package/plugins/fancy-story-builder/storydb/driver.js +106 -0
  156. package/plugins/fancy-story-builder/storydb/dsh-tools.js +313 -0
  157. package/plugins/fancy-story-builder/storydb/export.js +175 -0
  158. package/plugins/fancy-story-builder/storydb/ids.js +31 -0
  159. package/plugins/fancy-story-builder/storydb/impact.js +170 -0
  160. package/plugins/fancy-story-builder/storydb/import.js +559 -0
  161. package/plugins/fancy-story-builder/storydb/mcp.js +99 -0
  162. package/plugins/fancy-story-builder/storydb/prompts.js +952 -0
  163. package/plugins/fancy-story-builder/storydb/stage-command.js +379 -0
  164. package/plugins/fancy-story-builder/storydb/stage.js +426 -0
  165. package/plugins/fancy-story-builder/storydb/store.js +891 -0
  166. package/plugins/fancy-story-builder/storydb/tools.js +592 -0
  167. package/plugins/fancy-story-builder/storydb/validation.js +183 -0
  168. package/plugins/fancy-story-builder/templates/v2/10-scale/scale-template.json +144 -0
  169. package/plugins/fancy-story-builder/templates/v2/20-macro/emotion-template.json +15 -0
  170. package/plugins/fancy-story-builder/templates/v2/20-macro/endings-template.json +16 -0
  171. package/plugins/fancy-story-builder/templates/v2/20-macro/lines-template.json +28 -0
  172. package/plugins/fancy-story-builder/templates/v2/20-macro/roles-template.json +10 -0
  173. package/plugins/fancy-story-builder/templates/v2/20-macro/story-template.json +19 -0
  174. package/plugins/fancy-story-builder/templates/v2/20-macro/volumes-template.json +24 -0
  175. package/plugins/fancy-story-builder/templates/v2/20-macro/world-template.json +24 -0
  176. package/plugins/fancy-story-builder/templates/v2/30-cast/characters-template.json +49 -0
  177. package/plugins/fancy-story-builder/templates/v2/30-cast/climaxes-template.json +14 -0
  178. package/plugins/fancy-story-builder/templates/v2/30-cast/conflicts-template.json +30 -0
  179. package/plugins/fancy-story-builder/templates/v2/30-cast/factions-template.json +18 -0
  180. package/plugins/fancy-story-builder/templates/v2/30-cast/foreshadows-template.json +29 -0
  181. package/plugins/fancy-story-builder/templates/v2/30-cast/information-template.json +29 -0
  182. package/plugins/fancy-story-builder/templates/v2/30-cast/items-template.json +13 -0
  183. package/plugins/fancy-story-builder/templates/v2/30-cast/locations-template.json +12 -0
  184. package/plugins/fancy-story-builder/templates/v2/30-cast/payoffs-template.json +16 -0
  185. package/plugins/fancy-story-builder/templates/v2/30-cast/relations-template.json +44 -0
  186. package/plugins/fancy-story-builder/templates/v2/40-rhythm/chapter-map-template.json +81 -0
  187. package/plugins/fancy-story-builder/templates/v2/40-rhythm/outline-template.json +162 -0
  188. package/plugins/fancy-story-builder/templates/v2/50-writing/ledger-template.json +46 -0
  189. package/plugins/fancy-story-builder/templates/v2/60-deslop/report-template.json +10 -0
  190. package/plugins/fancy-story-builder/templates//350/212/202/345/245/217//346/227/266/345/272/217/346/230/240/345/260/204/346/250/241/346/235/277.json +267 -44
  191. package/plugins/fancy-story-builder/templates//350/241/200/350/202/211//344/270/226/347/225/214/350/247/202/350/247/204/345/210/231/346/250/241/346/235/277.json +121 -19
  192. package/plugins/fancy-story-builder/templates//350/241/200/350/202/211//345/205/263/351/224/256/351/201/223/345/205/267/346/250/241/346/235/277.json +129 -21
  193. package/plugins/fancy-story-builder/templates//350/241/200/350/202/211//345/205/266/344/273/226/345/256/236/344/275/223/346/250/241/346/235/277.json +62 -0
  194. package/plugins/fancy-story-builder/templates//350/241/200/350/202/211//345/206/262/347/252/201/346/250/241/346/235/277.json +76 -17
  195. package/plugins/fancy-story-builder/templates//350/241/200/350/202/211//345/211/247/346/203/205/346/250/241/346/235/277.json +56 -8
  196. package/plugins/fancy-story-builder/templates//350/241/200/350/202/211//345/212/277/345/212/233/346/250/241/346/235/277.json +63 -13
  197. package/plugins/fancy-story-builder/templates//350/241/200/350/202/211//345/234/260/347/202/271/346/250/241/346/235/277.json +34 -6
  198. package/plugins/fancy-story-builder/templates//350/241/200/350/202/211//347/273/223/345/261/200/346/250/241/346/235/277.json +81 -18
  199. package/plugins/fancy-story-builder/templates//350/241/200/350/202/211//350/247/222/350/211/262/346/250/241/346/235/277.json +202 -42
  200. package/plugins/fancy-story-builder/templates//351/252/250/346/236/266//345/205/263/347/263/273/346/250/241/346/235/277.json +160 -27
  201. package/plugins/fancy-story-builder/templates//351/252/250/346/236/266//345/256/236/344/275/223/346/250/241/346/235/277.json +217 -41
  202. package/plugins/fancy-story-builder/templates//351/252/250/346/236/266//346/241/206/346/236/266/346/250/241/346/235/277.json +215 -41
  203. package/plugins/fancy-story-builder/templates.js +272 -74
  204. package/plugins/fancy-story-builder/wiki-references.js +429 -93
  205. package/plugins/fancy-story-builder/writing-driver.js +336 -9
  206. package/plugins/fancy-topic/index.js +414 -61
  207. package/plugins/fancy-topic/prompts/01-exploration-prompt.md +115 -0
  208. package/plugins/fancy-topic/scripts/save-suggestion.js +219 -0
  209. package/plugins/fancy-topic/scripts/select-topic.js +106 -63
  210. package/plugins/fancy-topic/templates.js +225 -122
  211. package/plugins/fancy-topic/tool-deploy.js +200 -0
  212. package/lib/safe-rm.js +0 -94
@@ -27,7 +27,7 @@ const __dirname = path.dirname(fileURLToPath(import.meta.url));
27
27
  // 各阶段的文件名白名单(防止路径穿越)
28
28
  const STAGE_FILES = {
29
29
  骨架: ['框架', '实体', '关系', '层级'],
30
- 血肉: ['角色', '冲突', '势力', '剧情', '地点', '结局', '世界观规则', '关键道具'],
30
+ 血肉: ['角色', '其他实体', '冲突', '势力', '剧情', '地点', '结局', '世界观规则', '关键道具'],
31
31
  节奏: ['章节规划', '时序映射', '节奏参数', '章节细纲'],
32
32
  };
33
33
 
@@ -0,0 +1,112 @@
1
+ /**
2
+ * v2/baseline.js —— 00-baseline:固化选题、参考与公式版本。
3
+ */
4
+ import fs from 'node:fs';
5
+ import path from 'node:path';
6
+ import {
7
+ SCHEMA_VERSION,
8
+ ensureDir,
9
+ fileExists,
10
+ nowIso,
11
+ readJson,
12
+ stageDir,
13
+ writeJsonAtomic,
14
+ } from './core.js';
15
+
16
+ const INPUT_FIELDS = [
17
+ 'book_name', 'genre', 'platform', 'positioning', 'one_line_pitch', 'target_chapters', 'target_words',
18
+ 'core_conflict', 'reader_value', 'golden_finger', 'core_setting', 'selling_points',
19
+ 'hard_constraints', 'anti_tropes', 'protagonist', 'world_setting',
20
+ ];
21
+
22
+ export function buildBaselinePayload(topicData, formulasDoc, genreProfilesDoc) {
23
+ const topic = topicData || {};
24
+ const input = {
25
+ schema_version: SCHEMA_VERSION,
26
+ book_name: topic.book_name || topic.title || '未命名',
27
+ genre: topic.genre || '',
28
+ positioning: topic.positioning || '',
29
+ one_line_pitch: topic.one_line_pitch || '',
30
+ target_chapters: Number(topic.target_chapters) || 0,
31
+ target_words: Number(topic.target_words) || 0,
32
+ core_conflict: topic.core_conflict || '',
33
+ reader_value: topic.reader_value || '',
34
+ golden_finger: topic.golden_finger || '',
35
+ core_setting: topic.core_setting || '',
36
+ selling_points: Array.isArray(topic.selling_points) ? topic.selling_points : [],
37
+ hard_constraints: Array.isArray(topic.hard_constraints) ? topic.hard_constraints : [],
38
+ anti_tropes: Array.isArray(topic.anti_tropes) ? topic.anti_tropes : [],
39
+ protagonist: topic.protagonist || '',
40
+ world_setting: topic.world_setting || '',
41
+ created_at: nowIso(),
42
+ };
43
+ // 只保留已定义字段,避免把 undefined 写进 JSON
44
+ for (const key of Object.keys(input)) {
45
+ if (input[key] === undefined) delete input[key];
46
+ }
47
+ return {
48
+ input,
49
+ references: {
50
+ schema_version: SCHEMA_VERSION,
51
+ references: Array.isArray(topic.references) ? topic.references : [],
52
+ benchmark: topic.benchmark || null,
53
+ note: '对标与参考决策。没有时保持空数组。',
54
+ },
55
+ formulas: {
56
+ _meta: {
57
+ schema_version: SCHEMA_VERSION,
58
+ source: 'quantity-formulas',
59
+ version: '2026-11-06',
60
+ read_only: true,
61
+ },
62
+ ...formulasDoc,
63
+ },
64
+ genreProfiles: genreProfilesDoc || null,
65
+ };
66
+ }
67
+
68
+ /**
69
+ * 确保 00-baseline 存在。已存在时不覆盖,除非 opts.force=true。
70
+ */
71
+ export function ensureBaseline(projectRoot, topicData, formulasDoc, opts = {}) {
72
+ const dir = stageDir(projectRoot, '00-baseline');
73
+ ensureDir(dir);
74
+ const payload = buildBaselinePayload(topicData, formulasDoc, opts.genreProfilesDoc);
75
+ const files = ['input.json', 'references.json', 'formulas.json'];
76
+ if (payload.genreProfiles) files.push('genre-profiles.json');
77
+ const forceFiles = new Set(opts.forceFiles || []);
78
+ const written = [];
79
+ const pairs = {
80
+ 'input.json': payload.input,
81
+ 'references.json': payload.references,
82
+ 'formulas.json': payload.formulas,
83
+ 'genre-profiles.json': payload.genreProfiles,
84
+ };
85
+ for (const file of files) {
86
+ const target = path.join(dir, file);
87
+ if (!opts.force && !forceFiles.has(file) && fileExists(target)) continue;
88
+ writeJsonAtomic(target, pairs[file]);
89
+ written.push(file);
90
+ }
91
+ return { ok: true, dir, written };
92
+ }
93
+
94
+ export function loadBaseline(projectRoot) {
95
+ const dir = stageDir(projectRoot, '00-baseline');
96
+ return {
97
+ input: readJson(path.join(dir, 'input.json'), null),
98
+ references: readJson(path.join(dir, 'references.json'), null),
99
+ formulas: readJson(path.join(dir, 'formulas.json'), null),
100
+ };
101
+ }
102
+
103
+ export function loadFormulasDoc(projectRoot, fallbackPath) {
104
+ // references/ 是插件每次阶段启动时刷新的副本;优先它,避免旧项目一直用
105
+ // 00-baseline 里冻结的老公式。
106
+ const deployed = readJson(path.join(projectRoot, 'references', 'formulas-skeleton.json'), null);
107
+ if (deployed && deployed.formulas) return deployed;
108
+ const local = loadBaseline(projectRoot).formulas;
109
+ if (local && local.formulas) return local;
110
+ if (fallbackPath && fs.existsSync(fallbackPath)) return readJson(fallbackPath, null);
111
+ return null;
112
+ }
@@ -0,0 +1,517 @@
1
+ /**
2
+ * v2/cast.js —— 30-cast:人物、势力、地点、道具、冲突、伏笔、高潮、爽点、关系、信息差。
3
+ *
4
+ * 这一层只负责“谁、在哪里、为什么对抗、埋什么、怎么爽”。
5
+ * 章节落点留给 40-rhythm;正文留给 50-writing。
6
+ */
7
+ import {
8
+ asArray,
9
+ indexById,
10
+ v2Error,
11
+ } from './core.js';
12
+ import { validateInitialKnowledge } from './knowledge.js';
13
+
14
+ export const CAST_FILES = [
15
+ 'characters.json', 'factions.json', 'locations.json', 'items.json',
16
+ 'conflicts.json', 'foreshadows.json', 'climaxes.json', 'payoffs.json',
17
+ 'relations.json', 'information.json',
18
+ ];
19
+
20
+ const CHARACTER_TYPE_TO_QUOTA = {
21
+ '主角': 'protagonist',
22
+ '核心配角': 'core_supporting',
23
+ '贯穿级核心配角': 'core_supporting',
24
+ '卷级配角': 'volume_supporting',
25
+ '卷级重要配角': 'volume_supporting',
26
+ '核心反派': 'core_antagonist',
27
+ '贯穿级核心反派': 'core_antagonist',
28
+ '卷级反派': 'volume_antagonist',
29
+ };
30
+ const ROLE_FUNCTION_TO_QUOTA = {
31
+ '主角': 'protagonist',
32
+ '核心配角': 'core_supporting',
33
+ '贯穿级核心配角': 'core_supporting',
34
+ '卷级配角': 'volume_supporting',
35
+ '卷级重要配角': 'volume_supporting',
36
+ '核心反派': 'core_antagonist',
37
+ '贯穿级核心反派': 'core_antagonist',
38
+ '卷级反派': 'volume_antagonist',
39
+ };
40
+ const ITEM_CATEGORY = { core: 'core', 核心: 'core', branch: 'branch', 支线: 'branch', symbolic: 'symbolic', 象征: 'symbolic' };
41
+ const CONFLICT_TYPE = { core: 'core', 核心: 'core', phase: 'phase', 阶段: 'phase', branch: 'branch', 交织: 'branch' };
42
+ const SCALE_SIZE = {
43
+ small: 'small', 小: 'small', 小型: 'small',
44
+ medium: 'medium', 中: 'medium', 中型: 'medium',
45
+ large: 'large', 大: 'large', 大型: 'large',
46
+ };
47
+
48
+ function requireString(errors, obj, path, field, contract) {
49
+ const value = obj && obj[field];
50
+ if (typeof value !== 'string' || !value.trim()) {
51
+ errors.push(v2Error('CAST_REQUIRED_FIELD', {
52
+ path: path + '.' + field,
53
+ message: '缺少必填文本字段',
54
+ actual: value,
55
+ contract,
56
+ }));
57
+ }
58
+ }
59
+
60
+ function requireArray(errors, obj, path, field, contract, min = 1) {
61
+ const value = obj && obj[field];
62
+ if (!Array.isArray(value) || value.length < min) {
63
+ errors.push(v2Error('CAST_REQUIRED_FIELD', {
64
+ path: path + '.' + field,
65
+ message: '必须是至少 ' + min + ' 项的数组',
66
+ actual: Array.isArray(value) ? value.length : typeof value,
67
+ contract,
68
+ }));
69
+ }
70
+ }
71
+
72
+ function collectUnique(errors, rows, path, contract, prefix, pattern) {
73
+ const ids = new Set();
74
+ for (let i = 0; i < rows.length; i += 1) {
75
+ const row = rows[i] || {};
76
+ const id = row.id;
77
+ if (!id) {
78
+ errors.push(v2Error('CAST_ID_MISSING', { path: path + '[' + i + '].id', message: '缺少 id', contract }));
79
+ continue;
80
+ }
81
+ if ((pattern && !pattern.test(id)) || (!pattern && prefix && !new RegExp('^' + prefix + '\\d{3,}$').test(id))) {
82
+ errors.push(v2Error('CAST_ID_FORMAT', {
83
+ path: path + '[' + i + '].id',
84
+ message: 'ID 格式不符',
85
+ expected: pattern ? String(pattern) : prefix + '001',
86
+ actual: id,
87
+ contract,
88
+ }));
89
+ }
90
+ if (ids.has(id)) {
91
+ errors.push(v2Error('CAST_ID_DUP', { path: path + '[' + i + '].id', message: 'ID 重复:' + id, contract }));
92
+ }
93
+ ids.add(id);
94
+ }
95
+ return ids;
96
+ }
97
+
98
+ function countBy(rows, resolver) {
99
+ const out = {};
100
+ for (const row of rows) {
101
+ const key = resolver(row || {});
102
+ if (!key) continue;
103
+ out[key] = (out[key] || 0) + 1;
104
+ }
105
+ return out;
106
+ }
107
+
108
+ function compareQuota(errors, code, path, actual, expected, label, reference, contract) {
109
+ if (Number(actual) !== Number(expected)) {
110
+ errors.push(v2Error(code, {
111
+ path,
112
+ message: label + '数量与 scale 配额不一致',
113
+ expected: Number(expected),
114
+ actual: Number(actual),
115
+ contract,
116
+ reference,
117
+ }));
118
+ }
119
+ }
120
+
121
+ export function validateCast(cast, scale, macro, opts = {}) {
122
+ const errors = [];
123
+ const warnings = [];
124
+ const docs = cast || {};
125
+ const quotas = (scale && scale.quotas) || {};
126
+ const characters = asArray(docs.characters && docs.characters.characters);
127
+ const factions = asArray(docs.factions && docs.factions.factions);
128
+ const locations = asArray(docs.locations && docs.locations.locations);
129
+ const items = asArray(docs.items && docs.items.items);
130
+ const conflicts = asArray(docs.conflicts && docs.conflicts.conflicts);
131
+ const foreshadows = asArray(docs.foreshadows && docs.foreshadows.foreshadows);
132
+ const climaxes = asArray(docs.climaxes && docs.climaxes.climaxes);
133
+ const payoffs = asArray(docs.payoffs && docs.payoffs.payoffs);
134
+ const relations = docs.relations || {};
135
+ const characterRelations = asArray(relations.character_relations);
136
+ const factionRelations = asArray(relations.faction_relations);
137
+ const memberships = asArray(relations.memberships);
138
+ const information = asArray(docs.information && docs.information.information_gaps);
139
+ collectUnique(errors, characterRelations, 'relations.character_relations', '30-cast/README.md#relations', 'REL');
140
+ collectUnique(errors, factionRelations, 'relations.faction_relations', '30-cast/README.md#relations', 'FREL');
141
+ collectUnique(errors, memberships, 'relations.memberships', '30-cast/README.md#relations', 'MEM');
142
+
143
+ // ---- 唯一 ID ----
144
+ const charIds = collectUnique(errors, characters, 'characters.characters', '30-cast/README.md#characters', 'C');
145
+ const factionIds = collectUnique(errors, factions, 'factions.factions', '30-cast/README.md#factions', 'F');
146
+ const locationIds = collectUnique(errors, locations, 'locations.locations', '30-cast/README.md#locations', 'L');
147
+ const itemIds = collectUnique(errors, items, 'items.items', '30-cast/README.md#items', 'P');
148
+ const conflictIds = collectUnique(errors, conflicts, 'conflicts.conflicts', '30-cast/README.md#conflicts', 'X');
149
+ const foreshadowIds = collectUnique(errors, foreshadows, 'foreshadows.foreshadows', '30-cast/README.md#foreshadows', 'FS');
150
+ const climaxIds = collectUnique(errors, climaxes, 'climaxes.climaxes', '30-cast/README.md#climaxes', 'CL');
151
+ const payoffIds = collectUnique(errors, payoffs, 'payoffs.payoffs', '30-cast/README.md#payoffs', 'PL');
152
+ collectUnique(errors, information, 'information.information_gaps', '30-cast/README.md#information', null, /^IG-[A-Za-z0-9-]+$/);
153
+
154
+ // ---- 人物 ----
155
+ const expectedChar = {
156
+ protagonist: Number((quotas.characters && quotas.characters.protagonist) || 0),
157
+ core_supporting: Number((quotas.characters && quotas.characters.core_supporting) || 0),
158
+ volume_supporting: Number((quotas.characters && quotas.characters.volume_supporting) || 0),
159
+ core_antagonist: Number((quotas.characters && quotas.characters.core_antagonist) || 0),
160
+ volume_antagonist: Number((quotas.characters && quotas.characters.volume_antagonist) || 0),
161
+ };
162
+ const charTypeCounts = countBy(characters, (c) => CHARACTER_TYPE_TO_QUOTA[c.type || c.role]);
163
+ for (const [key, expected] of Object.entries(expectedChar)) {
164
+ compareQuota(errors, 'CAST_CHARACTER_QUOTA', 'characters.characters',
165
+ charTypeCounts[key] || 0, expected,
166
+ '角色(' + key + ')', '10-scale/scale.json#quotas.characters.' + key, '30-cast/README.md#characters');
167
+ }
168
+ const roleMap = new Map();
169
+ for (const r of asArray(macro && macro.roles && macro.roles.roles)) {
170
+ if (r && r.role_id) roleMap.set(r.role_id, r);
171
+ }
172
+ const usedRoles = new Map();
173
+ for (let i = 0; i < characters.length; i += 1) {
174
+ const c = characters[i] || {};
175
+ const p = 'characters.characters[' + i + ']';
176
+ requireString(errors, c, p, 'name', '30-cast/README.md#characters');
177
+ requireString(errors, c, p, 'type', '30-cast/README.md#characters');
178
+ requireString(errors, c, p, 'description', '30-cast/README.md#characters');
179
+ requireString(errors, c, p, 'flaw', '30-cast/README.md#characters');
180
+ requireString(errors, c, p, 'golden_finger', '30-cast/README.md#characters');
181
+ if (!CHARACTER_TYPE_TO_QUOTA[c.type || c.role]) {
182
+ errors.push(v2Error('CAST_CHARACTER_TYPE', {
183
+ path: p + '.type',
184
+ message: '未知人物类型',
185
+ expected: Object.keys(CHARACTER_TYPE_TO_QUOTA).join(' / '),
186
+ actual: c.type || c.role,
187
+ contract: '30-cast/README.md#characters',
188
+ }));
189
+ }
190
+ if (!c.faction || !factionIds.has(c.faction)) {
191
+ errors.push(v2Error('CAST_CHARACTER_FACTION', {
192
+ path: p + '.faction',
193
+ message: '人物必须引用已存在的势力',
194
+ actual: c.faction,
195
+ contract: '30-cast/README.md#characters',
196
+ reference: 'factions.factions',
197
+ }));
198
+ }
199
+ for (const f of ['short_term', 'medium_term', 'long_term']) {
200
+ if (!c.goal || typeof c.goal[f] !== 'string' || !c.goal[f].trim()) {
201
+ errors.push(v2Error('CAST_CHARACTER_GOAL', { path: p + '.goal.' + f, message: '缺少目标层级', contract: '30-cast/README.md#characters' }));
202
+ }
203
+ }
204
+ if (!c.information_state || typeof c.information_state !== 'object') {
205
+ errors.push(v2Error('CAST_CHARACTER_INFO_STATE', { path: p + '.information_state', message: '缺少信息状态', contract: '30-cast/README.md#characters' }));
206
+ } else {
207
+ requireArray(errors, c.information_state, p + '.information_state', 'known', '30-cast/README.md#characters', 1);
208
+ requireArray(errors, c.information_state, p + '.information_state', 'unknown', '30-cast/README.md#characters', 1);
209
+ }
210
+ if (!c.arc || typeof c.arc !== 'object' || !Array.isArray(c.arc.stages) || !c.arc.stages.length) {
211
+ errors.push(v2Error('CAST_CHARACTER_ARC', { path: p + '.arc', message: '人物必须有弧光阶段', contract: '30-cast/README.md#characters' }));
212
+ } else {
213
+ for (let j = 0; j < c.arc.stages.length; j += 1) {
214
+ const st = c.arc.stages[j] || {};
215
+ for (const f of ['name', 'question', 'planned_change']) {
216
+ requireString(errors, st, p + '.arc.stages[' + j + ']', f, '30-cast/README.md#characters');
217
+ }
218
+ }
219
+ }
220
+ if (!c.voice || typeof c.voice !== 'object' || !c.voice.catchphrase || !c.voice.rhythm) {
221
+ errors.push(v2Error('CAST_CHARACTER_VOICE', { path: p + '.voice', message: '人物必须有口头禅与语言节奏', contract: '30-cast/README.md#characters' }));
222
+ }
223
+ if (!c.role_slot) {
224
+ errors.push(v2Error('CAST_ROLE_SLOT_MISSING', { path: p + '.role_slot', message: '人物必须绑定宏观阶段的 role_slot', contract: '30-cast/README.md#characters' }));
225
+ } else {
226
+ const role = roleMap.get(c.role_slot);
227
+ if (!role) {
228
+ errors.push(v2Error('CAST_ROLE_SLOT_REF', {
229
+ path: p + '.role_slot', message: '引用了不存在的角色功能位:' + c.role_slot,
230
+ contract: '30-cast/README.md#characters', reference: '20-macro/roles.json#roles',
231
+ }));
232
+ } else {
233
+ const roleKey = ROLE_FUNCTION_TO_QUOTA[role.function];
234
+ const charKey = CHARACTER_TYPE_TO_QUOTA[c.type || c.role];
235
+ if (roleKey && charKey && roleKey !== charKey) {
236
+ errors.push(v2Error('CAST_ROLE_FUNCTION_MISMATCH', {
237
+ path: p + '.role_slot',
238
+ message: '角色类型与功能位不一致',
239
+ expected: role.function, actual: c.type,
240
+ contract: '30-cast/README.md#characters',
241
+ }));
242
+ }
243
+ }
244
+ usedRoles.set(c.role_slot, (usedRoles.get(c.role_slot) || 0) + 1);
245
+ }
246
+ }
247
+ for (const roleId of roleMap.keys()) {
248
+ const count = usedRoles.get(roleId) || 0;
249
+ if (count === 0) {
250
+ errors.push(v2Error('CAST_ROLE_SLOT_UNFILLED', {
251
+ path: 'characters.characters', message: '角色功能位没有被填充:' + roleId,
252
+ contract: '30-cast/README.md#characters', reference: '20-macro/roles.json#roles',
253
+ }));
254
+ } else if (count > 1) {
255
+ errors.push(v2Error('CAST_ROLE_SLOT_REUSED', {
256
+ path: 'characters.characters', message: '角色功能位被多个角色重复使用:' + roleId,
257
+ actual: count, contract: '30-cast/README.md#characters', reference: '20-macro/roles.json#roles',
258
+ }));
259
+ }
260
+ }
261
+
262
+ // ---- 势力/地点/道具/冲突/伏笔/高潮/爽点 ----
263
+ compareQuota(errors, 'CAST_FACTION_QUOTA', 'factions.factions', factions.length, quotas.factions, '势力', '10-scale/scale.json#quotas.factions', '30-cast/README.md#factions');
264
+ compareQuota(errors, 'CAST_LOCATION_QUOTA', 'locations.locations', locations.length, quotas.locations, '地点', '10-scale/scale.json#quotas.locations', '30-cast/README.md#locations');
265
+ for (let i = 0; i < factions.length; i += 1) {
266
+ const f = factions[i] || {};
267
+ for (const field of ['name', 'type', 'description', 'goal']) {
268
+ requireString(errors, f, 'factions.factions[' + i + ']', field, '30-cast/README.md#factions');
269
+ }
270
+ for (const field of ['resources', 'territory']) {
271
+ const value = f && f[field];
272
+ if (Array.isArray(value) ? value.length === 0 : !(typeof value === 'string' && value.trim())) {
273
+ errors.push(v2Error('CAST_REQUIRED_FIELD', { path: 'factions.factions[' + i + '].' + field, message: '必须是非空数组或文本', actual: value, contract: '30-cast/README.md#factions' }));
274
+ }
275
+ }
276
+ }
277
+ for (let i = 0; i < locations.length; i += 1) {
278
+ const l = locations[i] || {};
279
+ for (const field of ['name', 'type', 'description', 'narrative_function']) {
280
+ requireString(errors, l, 'locations.locations[' + i + ']', field, '30-cast/README.md#locations');
281
+ }
282
+ }
283
+ const itemCounts = countBy(items, (x) => ITEM_CATEGORY[x.category]);
284
+ for (const key of ['core', 'branch', 'symbolic']) {
285
+ compareQuota(errors, 'CAST_ITEM_QUOTA', 'items.items', itemCounts[key] || 0,
286
+ (quotas.items && quotas.items[key]) || 0, '道具(' + key + ')',
287
+ '10-scale/scale.json#quotas.items.' + key, '30-cast/README.md#items');
288
+ }
289
+ for (let i = 0; i < items.length; i += 1) {
290
+ const it = items[i] || {};
291
+ for (const field of ['name', 'category', 'description', 'function']) {
292
+ requireString(errors, it, 'items.items[' + i + ']', field, '30-cast/README.md#items');
293
+ }
294
+ if (!ITEM_CATEGORY[it.category]) {
295
+ errors.push(v2Error('CAST_ITEM_CATEGORY', { path: 'items.items[' + i + '].category', message: '道具类别非法', expected: 'core / branch / symbolic', actual: it.category, contract: '30-cast/README.md#items' }));
296
+ }
297
+ }
298
+ const conflictCounts = countBy(conflicts, (x) => CONFLICT_TYPE[x.type]);
299
+ for (const key of ['core', 'phase', 'branch']) {
300
+ compareQuota(errors, 'CAST_CONFLICT_QUOTA', 'conflicts.conflicts', conflictCounts[key] || 0,
301
+ (quotas.conflicts && quotas.conflicts[key]) || 0, '冲突(' + key + ')',
302
+ '10-scale/scale.json#quotas.conflicts.' + key, '30-cast/README.md#conflicts');
303
+ }
304
+ for (let i = 0; i < conflicts.length; i += 1) {
305
+ const x = conflicts[i] || {};
306
+ const p = 'conflicts.conflicts[' + i + ']';
307
+ for (const field of ['name', 'type', 'description', 'core_goal', 'stakes']) {
308
+ requireString(errors, x, p, field, '30-cast/README.md#conflicts');
309
+ }
310
+ if (!CONFLICT_TYPE[x.type]) {
311
+ errors.push(v2Error('CAST_CONFLICT_TYPE', { path: p + '.type', message: '冲突类型非法', expected: 'core / phase / branch', actual: x.type, contract: '30-cast/README.md#conflicts' }));
312
+ }
313
+ const path = asArray(x.escalation_path);
314
+ if (path.length < 2) {
315
+ errors.push(v2Error('CAST_CONFLICT_ESCALATION', { path: p + '.escalation_path', message: '冲突至少要有 2 个升级节点', contract: '30-cast/README.md#conflicts' }));
316
+ }
317
+ for (let j = 0; j < path.length; j += 1) {
318
+ for (const field of ['node_type', 'description']) requireString(errors, path[j] || {}, p + '.escalation_path[' + j + ']', field, '30-cast/README.md#conflicts');
319
+ }
320
+ if (!x.resolution_plan || !x.resolution_plan.method) {
321
+ errors.push(v2Error('CAST_CONFLICT_RESOLUTION', { path: p + '.resolution_plan', message: '冲突必须有解决方式', contract: '30-cast/README.md#conflicts' }));
322
+ }
323
+ const participants = asArray(x.participants);
324
+ if (!participants.length) {
325
+ errors.push(v2Error('CAST_CONFLICT_PARTICIPANTS', { path: p + '.participants', message: '冲突必须至少有一个参与者', contract: '30-cast/README.md#conflicts' }));
326
+ }
327
+ for (const id of participants) {
328
+ if (!charIds.has(id) && !factionIds.has(id) && !locationIds.has(id)) {
329
+ errors.push(v2Error('CAST_CONFLICT_PARTICIPANT_REF', { path: p + '.participants', message: '冲突参与者不存在:' + id, contract: '30-cast/README.md#conflicts' }));
330
+ }
331
+ }
332
+ }
333
+ const foreshadowCounts = countBy(foreshadows, (x) => SCALE_SIZE[x.type] || SCALE_SIZE[x.scale]);
334
+ for (const key of ['small', 'medium', 'large']) {
335
+ compareQuota(errors, 'CAST_FORESHADOW_QUOTA', 'foreshadows.foreshadows', foreshadowCounts[key] || 0,
336
+ (quotas.foreshadows && quotas.foreshadows[key]) || 0, '伏笔(' + key + ')',
337
+ '10-scale/scale.json#quotas.foreshadows.' + key, '30-cast/README.md#foreshadows');
338
+ }
339
+ for (let i = 0; i < foreshadows.length; i += 1) {
340
+ const f = foreshadows[i] || {};
341
+ const p = 'foreshadows.foreshadows[' + i + ']';
342
+ for (const field of ['name', 'type', 'setup', 'payoff_target', 'recovery_requirement']) {
343
+ requireString(errors, f, p, field, '30-cast/README.md#foreshadows');
344
+ }
345
+ if (!SCALE_SIZE[f.type] && !SCALE_SIZE[f.scale]) {
346
+ errors.push(v2Error('CAST_FORESHADOW_TYPE', { path: p + '.type', message: '伏笔级别非法', expected: '小 / 中 / 大', actual: f.type, contract: '30-cast/README.md#foreshadows' }));
347
+ }
348
+ if (f.scope && !['book', 'volume', 'arc', 'chapter'].includes(f.scope)) {
349
+ errors.push(v2Error('CAST_FORESHADOW_SCOPE', { path: p + '.scope', message: '伏笔 scope 非法', expected: 'book / volume / arc / chapter', actual: f.scope, contract: '30-cast/README.md#foreshadows' }));
350
+ }
351
+ requireArray(errors, f, p, 'clues', '30-cast/README.md#foreshadows', 1);
352
+ if (f.initial_knowledge !== undefined) {
353
+ const k = validateInitialKnowledge(f.initial_knowledge, { cast: docs, path: p + '.initial_knowledge' });
354
+ for (const e of k.errors) errors.push(v2Error(e.code, { ...e, contract: '30-cast/README.md#foreshadows' }));
355
+ for (const w of k.warnings) warnings.push(v2Error(w.code, { ...w, severity: 'warning', contract: '30-cast/README.md#foreshadows' }));
356
+ }
357
+ }
358
+ const climaxCounts = countBy(climaxes, (x) => SCALE_SIZE[x.type] || SCALE_SIZE[x.scale]);
359
+ for (const key of ['small', 'medium', 'large']) {
360
+ compareQuota(errors, 'CAST_CLIMAX_QUOTA', 'climaxes.climaxes', climaxCounts[key] || 0,
361
+ (quotas.climaxes && quotas.climaxes[key]) || 0, '高潮(' + key + ')',
362
+ '10-scale/scale.json#quotas.climaxes.' + key, '30-cast/README.md#climaxes');
363
+ }
364
+ for (let i = 0; i < climaxes.length; i += 1) {
365
+ const cl = climaxes[i] || {};
366
+ const p = 'climaxes.climaxes[' + i + ']';
367
+ for (const field of ['name', 'type', 'description', 'payoff']) {
368
+ requireString(errors, cl, p, field, '30-cast/README.md#climaxes');
369
+ }
370
+ for (const id of asArray(cl.participants)) {
371
+ if (!charIds.has(id) && !factionIds.has(id) && !locationIds.has(id)) {
372
+ errors.push(v2Error('CAST_CLIMAX_PARTICIPANT_REF', { path: p + '.participants', message: '高潮参与者不存在:' + id, contract: '30-cast/README.md#climaxes' }));
373
+ }
374
+ }
375
+ }
376
+ const payoffCounts = countBy(payoffs, (x) => SCALE_SIZE[x.scale] || SCALE_SIZE[x.type]);
377
+ for (const key of ['small', 'medium', 'large']) {
378
+ compareQuota(errors, 'CAST_PAYOFF_QUOTA', 'payoffs.payoffs', payoffCounts[key] || 0,
379
+ (quotas.pleasures && quotas.pleasures[key]) || 0, '爽点(' + key + ')',
380
+ '10-scale/scale.json#quotas.pleasures.' + key, '30-cast/README.md#payoffs');
381
+ }
382
+ for (let i = 0; i < payoffs.length; i += 1) {
383
+ const pl = payoffs[i] || {};
384
+ const p = 'payoffs.payoffs[' + i + ']';
385
+ for (const field of ['name', 'type', 'setup', 'payoff', 'emotional_effect']) {
386
+ requireString(errors, pl, p, field, '30-cast/README.md#payoffs');
387
+ }
388
+ for (const id of asArray(pl.required_setup)) {
389
+ if (!foreshadowIds.has(id) && !conflictIds.has(id) && !climaxIds.has(id) && !payoffIds.has(id)) {
390
+ // 允许设置一个尚未生成的冲突升级节点引用(X001-escalation-1)
391
+ if (!/^X\d{3}-escalation-\d+$/.test(String(id))) {
392
+ errors.push(v2Error('CAST_PAYOFF_SETUP_REF', { path: p + '.required_setup', message: '爽点铺垫引用不存在:' + id, contract: '30-cast/README.md#payoffs' }));
393
+ }
394
+ }
395
+ }
396
+ }
397
+
398
+ // ---- 关系 ----
399
+ compareQuota(errors, 'CAST_CHARACTER_RELATION_QUOTA', 'relations.character_relations', characterRelations.length,
400
+ quotas.relations && quotas.relations.character, '人物关系', '10-scale/scale.json#quotas.relations.character', '30-cast/README.md#relations');
401
+ compareQuota(errors, 'CAST_FACTION_RELATION_QUOTA', 'relations.faction_relations', factionRelations.length,
402
+ quotas.relations && quotas.relations.faction, '势力关系', '10-scale/scale.json#quotas.relations.faction', '30-cast/README.md#relations');
403
+ compareQuota(errors, 'CAST_MEMBERSHIP_QUOTA', 'relations.memberships', memberships.length,
404
+ quotas.relations && quotas.relations.membership, '势力成员', '10-scale/scale.json#quotas.relations.membership', '30-cast/README.md#relations');
405
+ for (let i = 0; i < characterRelations.length; i += 1) {
406
+ const r = characterRelations[i] || {};
407
+ const p = 'relations.character_relations[' + i + ']';
408
+ if (!charIds.has(r.from) || !charIds.has(r.to)) {
409
+ errors.push(v2Error('CAST_RELATION_REF', { path: p, message: '人物关系两端必须存在', actual: r.from + ' -> ' + r.to, contract: '30-cast/README.md#relations' }));
410
+ }
411
+ if (r.from === r.to) {
412
+ errors.push(v2Error('CAST_RELATION_SELF', { path: p, message: '人物关系不能指向自己', contract: '30-cast/README.md#relations' }));
413
+ }
414
+ for (const field of ['kind', 'initial_state']) requireString(errors, r, p, field, '30-cast/README.md#relations');
415
+ const changes = asArray(r.changes);
416
+ // 关系可以保持静态;只有写出来的变化节点才做结构校验。
417
+ for (let j = 0; j < changes.length; j += 1) {
418
+ for (const field of ['stage', 'trigger', 'description']) requireString(errors, changes[j] || {}, p + '.changes[' + j + ']', field, '30-cast/README.md#relations');
419
+ }
420
+ }
421
+ for (let i = 0; i < factionRelations.length; i += 1) {
422
+ const r = factionRelations[i] || {};
423
+ const p = 'relations.faction_relations[' + i + ']';
424
+ if (!factionIds.has(r.from) || !factionIds.has(r.to)) {
425
+ errors.push(v2Error('CAST_FACTION_RELATION_REF', { path: p, message: '势力关系两端必须存在', contract: '30-cast/README.md#relations' }));
426
+ }
427
+ if (r.from === r.to) errors.push(v2Error('CAST_FACTION_RELATION_SELF', { path: p, message: '势力关系不能指向自己', contract: '30-cast/README.md#relations' }));
428
+ for (const field of ['kind', 'initial_state']) requireString(errors, r, p, field, '30-cast/README.md#relations');
429
+ const changes = asArray(r.changes);
430
+ for (let j = 0; j < changes.length; j += 1) {
431
+ for (const field of ['stage', 'trigger', 'description']) requireString(errors, changes[j] || {}, p + '.changes[' + j + ']', field, '30-cast/README.md#relations');
432
+ }
433
+ }
434
+ for (let i = 0; i < memberships.length; i += 1) {
435
+ const m = memberships[i] || {};
436
+ const p = 'relations.memberships[' + i + ']';
437
+ if (!charIds.has(m.character) || !factionIds.has(m.faction)) {
438
+ errors.push(v2Error('CAST_MEMBERSHIP_REF', { path: p, message: '成员关系的人物/势力必须存在', contract: '30-cast/README.md#relations' }));
439
+ }
440
+ for (const field of ['position', 'kind']) requireString(errors, m, p, field, '30-cast/README.md#relations');
441
+ }
442
+
443
+ // ---- 信息差 ----
444
+ compareQuota(errors, 'CAST_INFORMATION_QUOTA', 'information.information_gaps', information.length,
445
+ quotas.information_gaps, '信息差', '10-scale/scale.json#quotas.information_gaps', '30-cast/README.md#information');
446
+ for (let i = 0; i < information.length; i += 1) {
447
+ const g = information[i] || {};
448
+ const p = 'information.information_gaps[' + i + ']';
449
+ for (const field of ['info_name', 'resolution_method', 'writing_ban']) requireString(errors, g, p, field, '30-cast/README.md#information');
450
+ if (!g.conflict || !conflictIds.has(g.conflict)) {
451
+ errors.push(v2Error('CAST_INFORMATION_CONFLICT', { path: p + '.conflict', message: '信息差必须关联已存在的冲突', actual: g.conflict, contract: '30-cast/README.md#information' }));
452
+ }
453
+ const knows = new Set([...asArray(g.who_knows), ...asArray(g.who_does_not_know)]);
454
+ if (!knows.size) {
455
+ errors.push(v2Error('CAST_INFORMATION_WHO', { path: p, message: '信息差必须说明谁知道/谁不知道', contract: '30-cast/README.md#information' }));
456
+ }
457
+ for (const id of knows) {
458
+ if (!charIds.has(id)) {
459
+ errors.push(v2Error('CAST_INFORMATION_WHO_REF', { path: p, message: '信息差引用了不存在的人物:' + id, contract: '30-cast/README.md#information' }));
460
+ }
461
+ }
462
+ if (g.initial_knowledge !== undefined) {
463
+ const k = validateInitialKnowledge(g.initial_knowledge, { cast: docs, path: p + '.initial_knowledge' });
464
+ for (const e of k.errors) errors.push(v2Error(e.code, { ...e, contract: '30-cast/README.md#information' }));
465
+ for (const w of k.warnings) warnings.push(v2Error(w.code, { ...w, severity: 'warning', contract: '30-cast/README.md#information' }));
466
+ const initial = new Map(asArray(g.initial_knowledge).map((row) => [row.character, row.state]));
467
+ for (const id of asArray(g.who_knows)) {
468
+ if (initial.has(id) && initial.get(id) === 'unknown') {
469
+ errors.push(v2Error('CAST_INFORMATION_KNOWLEDGE_CONFLICT', { path: p + '.initial_knowledge', message: 'who_knows 与 initial_knowledge 冲突:' + id + ' 被写成不知道', contract: '30-cast/README.md#information' }));
470
+ }
471
+ }
472
+ for (const id of asArray(g.who_does_not_know)) {
473
+ // false 表示“知道一个错误版本”,本质仍不知道真相,和 who_does_not_know 兼容。
474
+ if (initial.has(id) && !['unknown', 'false'].includes(initial.get(id))) {
475
+ errors.push(v2Error('CAST_INFORMATION_KNOWLEDGE_CONFLICT', { path: p + '.initial_knowledge', message: 'who_does_not_know 与 initial_knowledge 冲突:' + id + ' 被写成知道真相', contract: '30-cast/README.md#information' }));
476
+ }
477
+ }
478
+ }
479
+ }
480
+
481
+ // ---- 30-cast 不允许出现章节落点 ----
482
+ const scanChapterKeys = (value, prefix) => {
483
+ if (Array.isArray(value)) {
484
+ value.forEach((item, i) => scanChapterKeys(item, prefix + '[' + i + ']'));
485
+ return;
486
+ }
487
+ if (!value || typeof value !== 'object') return;
488
+ for (const [key, val] of Object.entries(value)) {
489
+ if (key === '_meta') continue;
490
+ const p = prefix ? prefix + '.' + key : key;
491
+ if (/^(chapter|chapter_id|chapter_no|ch)$/i.test(key) || /_chapter$/i.test(key)) {
492
+ errors.push(v2Error('CAST_CHAPTER_REF', {
493
+ path: p,
494
+ message: '30-cast 不允许写章节号;章节落点只能在 40-rhythm 分配',
495
+ contract: '30-cast/README.md#forbidden',
496
+ }));
497
+ }
498
+ scanChapterKeys(val, p);
499
+ }
500
+ };
501
+ scanChapterKeys(docs, '');
502
+
503
+ return { ok: errors.length === 0, errors, warnings, data: docs };
504
+ }
505
+
506
+ export function castIndexes(cast) {
507
+ return {
508
+ characters: indexById(asArray(cast && cast.characters && cast.characters.characters)),
509
+ factions: indexById(asArray(cast && cast.factions && cast.factions.factions)),
510
+ locations: indexById(asArray(cast && cast.locations && cast.locations.locations)),
511
+ items: indexById(asArray(cast && cast.items && cast.items.items)),
512
+ conflicts: indexById(asArray(cast && cast.conflicts && cast.conflicts.conflicts)),
513
+ foreshadows: indexById(asArray(cast && cast.foreshadows && cast.foreshadows.foreshadows)),
514
+ climaxes: indexById(asArray(cast && cast.climaxes && cast.climaxes.climaxes)),
515
+ payoffs: indexById(asArray(cast && cast.payoffs && cast.payoffs.payoffs)),
516
+ };
517
+ }