@cloud411716/fancy-webnovel 1.0.46 → 3.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.
Files changed (271) hide show
  1. package/client.js +369 -0
  2. package/cordis.patch.yml +4 -13
  3. package/docs/douyin-plugin.md +118 -0
  4. package/docs/material-library/README.md +404 -0
  5. package/docs/material-library/import.js +437 -0
  6. package/docs/material-library/prompt.md +370 -0
  7. package/docs/material-library/queries.surql +94 -0
  8. package/docs/material-library/sample.jsonl +14 -0
  9. package/docs/material-library/schema.surql +46 -0
  10. package/docs/mcp-profile.example.yml +24 -0
  11. package/docs/story-graph-design.md +602 -0
  12. package/docs/story-graph-validation.md +81 -0
  13. package/docs/workspace-mcp.md +284 -0
  14. package/index.js +26 -38
  15. package/lib/atomic-write.js +109 -0
  16. package/lib/chapter-order.js +100 -0
  17. package/lib/chinese-num.js +111 -0
  18. package/lib/confirmation.js +80 -0
  19. package/lib/db-init.js +123 -0
  20. package/lib/deployed-state.js +141 -0
  21. package/lib/direction-labels.js +265 -0
  22. package/lib/error-codes.js +104 -0
  23. package/lib/infra.js +594 -0
  24. package/lib/llm-wiki.js +109 -0
  25. package/lib/material-db.js +149 -0
  26. package/lib/path-guard.js +150 -0
  27. package/lib/projects.js +400 -0
  28. package/lib/scraper-utils.js +305 -0
  29. package/lib/surreal-server.js +338 -0
  30. package/lib/topic-decision.js +860 -0
  31. package/lib/topic-fields.js +251 -0
  32. package/lib/topic-schema.js +225 -0
  33. package/lib/workspace.js +501 -0
  34. package/package.json +62 -5
  35. package/plugins/fancy-analyze/analyze-definitions.md +519 -0
  36. package/plugins/fancy-analyze/command-input.js +65 -0
  37. package/plugins/fancy-analyze/index.js +2024 -0
  38. package/plugins/fancy-analyze/ingest-coverage.js +107 -0
  39. package/plugins/fancy-analyze/schema-analyze.md +627 -0
  40. package/plugins/fancy-analyze/schema-write.md +178 -0
  41. package/plugins/fancy-analyze/scripts/append-summary-chapter.js +612 -0
  42. package/plugins/fancy-analyze/scripts/split_source.js +292 -0
  43. package/plugins/fancy-analyze/summary-progress.js +495 -0
  44. package/plugins/fancy-analyze/templates.js +1179 -0
  45. package/plugins/fancy-bootstrap/index.js +313 -0
  46. package/plugins/fancy-bootstrap/scripts/init.js +177 -0
  47. package/plugins/fancy-bootstrap/scripts/path-utils.js +21 -0
  48. package/plugins/fancy-bootstrap/templates.js +71 -0
  49. package/plugins/fancy-db/index.js +172 -0
  50. package/plugins/fancy-douyin/deploy.js +134 -0
  51. package/plugins/fancy-douyin/index.js +551 -0
  52. package/plugins/fancy-douyin/prompts/01-analysis-prompt.md +84 -0
  53. package/plugins/fancy-douyin/scripts/crawl-note.cjs +194 -0
  54. package/plugins/fancy-douyin/scripts/lib.cjs +197 -0
  55. package/plugins/fancy-douyin/scripts/scan-account.cjs +128 -0
  56. package/plugins/fancy-douyin/scripts/start-chrome.cjs +203 -0
  57. package/plugins/fancy-douyin/state.js +197 -0
  58. package/plugins/fancy-douyin/templates.js +127 -0
  59. package/plugins/fancy-export/exporter.js +805 -0
  60. package/plugins/fancy-export/index.js +63 -0
  61. package/plugins/fancy-import/import-core.js +1118 -0
  62. package/plugins/fancy-import/import-driver.js +604 -0
  63. package/plugins/fancy-import/index.js +738 -0
  64. package/plugins/fancy-import/prompts/01-import-prompt.md +131 -0
  65. package/plugins/fancy-import/templates.js +58 -0
  66. package/plugins/fancy-material/exporter.js +153 -0
  67. package/plugins/fancy-material/genres.js +256 -0
  68. package/plugins/fancy-material/index.js +835 -0
  69. package/plugins/fancy-material/inventory.js +143 -0
  70. package/plugins/fancy-material/kinds.js +313 -0
  71. package/plugins/fancy-material/queue.js +55 -0
  72. package/plugins/fancy-material/scripts/query.js +254 -0
  73. package/plugins/fancy-material/templates.js +258 -0
  74. package/plugins/fancy-material/tool-deploy.js +138 -0
  75. package/plugins/fancy-material/validate.js +149 -0
  76. package/plugins/fancy-project/index.js +133 -0
  77. package/plugins/fancy-scan/existing-block.js +100 -0
  78. package/plugins/fancy-scan/index.js +708 -0
  79. package/plugins/fancy-scan/prompts/01-analysis-prompt.md +302 -0
  80. package/plugins/fancy-scan/scripts/append-block.js +725 -0
  81. package/plugins/fancy-scan/scripts/report-validate.js +824 -0
  82. package/plugins/fancy-scan/scripts/run-scan.js +667 -0
  83. package/plugins/fancy-scan/scripts/scrapers/fanqie-rank-scraper.js +923 -0
  84. package/plugins/fancy-scan/scripts/scrapers/jjwxc-rank-scraper.js +469 -0
  85. package/plugins/fancy-scan/scripts/scrapers/qimao-rank-scraper.js +629 -0
  86. package/plugins/fancy-scan/templates/blocks.js +155 -0
  87. package/plugins/fancy-scan/templates/platforms.js +372 -0
  88. package/plugins/fancy-scan/templates/report-schema.js +90 -0
  89. package/plugins/fancy-scan/templates.js +694 -0
  90. package/plugins/fancy-scan/tool-deploy.js +335 -0
  91. package/plugins/fancy-scan/wait-turn.js +46 -0
  92. package/plugins/fancy-story-builder/db/README.md +106 -0
  93. package/plugins/fancy-story-builder/db/client.js +228 -0
  94. package/plugins/fancy-story-builder/db/commands.js +145 -0
  95. package/plugins/fancy-story-builder/db/ids.js +39 -0
  96. package/plugins/fancy-story-builder/db/import.js +798 -0
  97. package/plugins/fancy-story-builder/db/project.js +299 -0
  98. package/plugins/fancy-story-builder/db/query.js +361 -0
  99. package/plugins/fancy-story-builder/db/schema.js +417 -0
  100. package/plugins/fancy-story-builder/db/stage-sync.js +238 -0
  101. package/plugins/fancy-story-builder/deploy-manifest.js +239 -0
  102. package/plugins/fancy-story-builder/deslop-driver.js +821 -0
  103. package/plugins/fancy-story-builder/flesh-output.js +168 -0
  104. package/plugins/fancy-story-builder/index.js +6861 -0
  105. package/plugins/fancy-story-builder/patch-driver.js +390 -0
  106. package/plugins/fancy-story-builder/prompts/01-skeleton-prompt.md +563 -0
  107. package/plugins/fancy-story-builder/prompts/02-flesh-prompt.md +602 -0
  108. package/plugins/fancy-story-builder/prompts/03-rhythm-prompt.md +539 -0
  109. package/plugins/fancy-story-builder/prompts/04-writing-prompt.md +225 -0
  110. package/plugins/fancy-story-builder/prompts/05-deslop-prompt.md +96 -0
  111. package/plugins/fancy-story-builder/purpose-template.md +83 -0
  112. package/plugins/fancy-story-builder/references/formulas-rhythm.json +302 -0
  113. package/plugins/fancy-story-builder/references/formulas-skeleton.json +402 -0
  114. package/plugins/fancy-story-builder/references/genre-profiles.json +3514 -0
  115. package/plugins/fancy-story-builder/references/glossary.md +594 -0
  116. package/plugins/fancy-story-builder/references/index.md +581 -0
  117. package/plugins/fancy-story-builder/references//345/206/231/344/275/234/346/214/207/345/257/274/00-index.md +174 -0
  118. package/plugins/fancy-story-builder/references//345/206/231/344/275/234/346/214/207/345/257/274/01-scene.md +150 -0
  119. package/plugins/fancy-story-builder/references//345/206/231/344/275/234/346/214/207/345/257/274/02-chapter-workflow.md +99 -0
  120. package/plugins/fancy-story-builder/references//345/206/231/344/275/234/346/214/207/345/257/274/03-opening-chapter.md +104 -0
  121. package/plugins/fancy-story-builder/references//345/206/231/344/275/234/346/214/207/345/257/274/04-dialogue-mastery.md +274 -0
  122. package/plugins/fancy-story-builder/references//345/206/231/344/275/234/346/214/207/345/257/274/05-genre-formulas.md +432 -0
  123. package/plugins/fancy-story-builder/references//345/206/231/344/275/234/346/214/207/345/257/274/06-tone-ensemble.md +259 -0
  124. package/plugins/fancy-story-builder/references//345/206/231/344/275/234/346/214/207/345/257/274/07-ledger-audit.md +222 -0
  125. package/plugins/fancy-story-builder/references//345/206/231/344/275/234/346/214/207/345/257/274/08-diagnosis-revision.md +205 -0
  126. package/plugins/fancy-story-builder/references//345/206/231/344/275/234/346/214/207/345/257/274/09-reading-power.md +135 -0
  127. package/plugins/fancy-story-builder/references//345/206/231/344/275/234/346/214/207/345/257/274/10-deslop.md +129 -0
  128. package/plugins/fancy-story-builder/references//350/212/202/345/245/217/346/214/207/345/257/274/00-index.md +98 -0
  129. package/plugins/fancy-story-builder/references//350/212/202/345/245/217/346/214/207/345/257/274/01-upgrade-rhythm.md +300 -0
  130. package/plugins/fancy-story-builder/references//350/212/202/345/245/217/346/214/207/345/257/274/02-arc-rhythm.md +168 -0
  131. package/plugins/fancy-story-builder/references//350/212/202/345/245/217/346/214/207/345/257/274/03-highpoint-hooks.md +189 -0
  132. package/plugins/fancy-story-builder/references//350/212/202/345/245/217/346/214/207/345/257/274/04-satisfaction.md +226 -0
  133. package/plugins/fancy-story-builder/references//350/212/202/345/245/217/346/214/207/345/257/274/05-foreshadow-density.md +195 -0
  134. package/plugins/fancy-story-builder/references//350/212/202/345/245/217/346/214/207/345/257/274/06-chapter-outline.md +226 -0
  135. package/plugins/fancy-story-builder/references//350/212/202/345/245/217/346/214/207/345/257/274/07-audit.md +181 -0
  136. package/plugins/fancy-story-builder/references//350/241/200/350/202/211/346/214/207/345/257/274/00-index.md +100 -0
  137. package/plugins/fancy-story-builder/references//350/241/200/350/202/211/346/214/207/345/257/274/01-character.md +243 -0
  138. package/plugins/fancy-story-builder/references//350/241/200/350/202/211/346/214/207/345/257/274/02-worldbuilding.md +245 -0
  139. package/plugins/fancy-story-builder/references//350/241/200/350/202/211/346/214/207/345/257/274/03-relations.md +185 -0
  140. package/plugins/fancy-story-builder/references//350/241/200/350/202/211/346/214/207/345/257/274/04-emotion-methods.md +286 -0
  141. package/plugins/fancy-story-builder/references//350/241/200/350/202/211/346/214/207/345/257/274/05-audit.md +187 -0
  142. package/plugins/fancy-story-builder/references//351/252/250/346/236/266/346/214/207/345/257/274/00-index.md +91 -0
  143. package/plugins/fancy-story-builder/references//351/252/250/346/236/266/346/214/207/345/257/274/01-positioning.md +209 -0
  144. package/plugins/fancy-story-builder/references//351/252/250/346/236/266/346/214/207/345/257/274/02-packaging.md +135 -0
  145. package/plugins/fancy-story-builder/references//351/252/250/346/236/266/346/214/207/345/257/274/03-entities.md +305 -0
  146. package/plugins/fancy-story-builder/references//351/252/250/346/236/266/346/214/207/345/257/274/04-conflict.md +328 -0
  147. package/plugins/fancy-story-builder/references//351/252/250/346/236/266/346/214/207/345/257/274/05-quota.md +187 -0
  148. package/plugins/fancy-story-builder/references//351/252/250/346/236/266/346/214/207/345/257/274/06-audit.md +160 -0
  149. package/plugins/fancy-story-builder/rhythm-output.js +140 -0
  150. package/plugins/fancy-story-builder/schema-setting.json +1100 -0
  151. package/plugins/fancy-story-builder/schema-setting.md +232 -0
  152. package/plugins/fancy-story-builder/scripts/ai-patterns.js +619 -0
  153. package/plugins/fancy-story-builder/scripts/brief-validate.js +164 -0
  154. package/plugins/fancy-story-builder/scripts/check-guide-compliance.js +289 -0
  155. package/plugins/fancy-story-builder/scripts/content-quality.js +416 -0
  156. package/plugins/fancy-story-builder/scripts/deslop-validate.js +143 -0
  157. package/plugins/fancy-story-builder/scripts/error-classes.js +112 -0
  158. package/plugins/fancy-story-builder/scripts/flesh-validate.js +1312 -0
  159. package/plugins/fancy-story-builder/scripts/formula-engine.js +112 -0
  160. package/plugins/fancy-story-builder/scripts/graph-utils.js +1484 -0
  161. package/plugins/fancy-story-builder/scripts/label-coverage.js +171 -0
  162. package/plugins/fancy-story-builder/scripts/prompt-drift-check.js +441 -0
  163. package/plugins/fancy-story-builder/scripts/reading-power.js +298 -0
  164. package/plugins/fancy-story-builder/scripts/reference-log.js +110 -0
  165. package/plugins/fancy-story-builder/scripts/rhythm-validate.js +1312 -0
  166. package/plugins/fancy-story-builder/scripts/schema-pages.js +2668 -0
  167. package/plugins/fancy-story-builder/scripts/semantic-lint.js +381 -0
  168. package/plugins/fancy-story-builder/scripts/skeleton-quota.js +145 -0
  169. package/plugins/fancy-story-builder/scripts/skeleton-validate.js +1973 -0
  170. package/plugins/fancy-story-builder/scripts/stage-batches.js +1405 -0
  171. package/plugins/fancy-story-builder/scripts/stage-contract.js +429 -0
  172. package/plugins/fancy-story-builder/scripts/story-artifacts.js +933 -0
  173. package/plugins/fancy-story-builder/scripts/upstream-query.js +198 -0
  174. package/plugins/fancy-story-builder/scripts/v2/baseline.js +112 -0
  175. package/plugins/fancy-story-builder/scripts/v2/cast.js +517 -0
  176. package/plugins/fancy-story-builder/scripts/v2/context.js +434 -0
  177. package/plugins/fancy-story-builder/scripts/v2/contracts.js +493 -0
  178. package/plugins/fancy-story-builder/scripts/v2/core.js +419 -0
  179. package/plugins/fancy-story-builder/scripts/v2/engine.js +1068 -0
  180. package/plugins/fancy-story-builder/scripts/v2/export-markdown.js +794 -0
  181. package/plugins/fancy-story-builder/scripts/v2/genre.js +195 -0
  182. package/plugins/fancy-story-builder/scripts/v2/knowledge.js +321 -0
  183. package/plugins/fancy-story-builder/scripts/v2/macro.js +409 -0
  184. package/plugins/fancy-story-builder/scripts/v2/manifest.js +538 -0
  185. package/plugins/fancy-story-builder/scripts/v2/migrate.js +137 -0
  186. package/plugins/fancy-story-builder/scripts/v2/prompts.js +1335 -0
  187. package/plugins/fancy-story-builder/scripts/v2/rhythm.js +618 -0
  188. package/plugins/fancy-story-builder/scripts/v2/scale.js +843 -0
  189. package/plugins/fancy-story-builder/scripts/v2/snapshot.js +121 -0
  190. package/plugins/fancy-story-builder/scripts/v2/stages.js +273 -0
  191. package/plugins/fancy-story-builder/scripts/v2/writing.js +216 -0
  192. package/plugins/fancy-story-builder/scripts/writing-rules.js +167 -0
  193. package/plugins/fancy-story-builder/scripts/writing-validate.js +682 -0
  194. package/plugins/fancy-story-builder/skeleton-output.js +199 -0
  195. package/plugins/fancy-story-builder/stage-driver.js +1049 -0
  196. package/plugins/fancy-story-builder/storydb/README.md +271 -0
  197. package/plugins/fancy-story-builder/storydb/cli.js +458 -0
  198. package/plugins/fancy-story-builder/storydb/client.js +351 -0
  199. package/plugins/fancy-story-builder/storydb/compile.js +452 -0
  200. package/plugins/fancy-story-builder/storydb/context.js +288 -0
  201. package/plugins/fancy-story-builder/storydb/definitions.js +699 -0
  202. package/plugins/fancy-story-builder/storydb/driver.js +106 -0
  203. package/plugins/fancy-story-builder/storydb/dsh-tools.js +313 -0
  204. package/plugins/fancy-story-builder/storydb/export.js +175 -0
  205. package/plugins/fancy-story-builder/storydb/ids.js +31 -0
  206. package/plugins/fancy-story-builder/storydb/impact.js +170 -0
  207. package/plugins/fancy-story-builder/storydb/import.js +559 -0
  208. package/plugins/fancy-story-builder/storydb/mcp.js +99 -0
  209. package/plugins/fancy-story-builder/storydb/prompts.js +952 -0
  210. package/plugins/fancy-story-builder/storydb/stage-command.js +379 -0
  211. package/plugins/fancy-story-builder/storydb/stage.js +426 -0
  212. package/plugins/fancy-story-builder/storydb/store.js +891 -0
  213. package/plugins/fancy-story-builder/storydb/tools.js +592 -0
  214. package/plugins/fancy-story-builder/storydb/validation.js +183 -0
  215. package/plugins/fancy-story-builder/templates/v2/10-scale/scale-template.json +144 -0
  216. package/plugins/fancy-story-builder/templates/v2/20-macro/emotion-template.json +15 -0
  217. package/plugins/fancy-story-builder/templates/v2/20-macro/endings-template.json +16 -0
  218. package/plugins/fancy-story-builder/templates/v2/20-macro/lines-template.json +28 -0
  219. package/plugins/fancy-story-builder/templates/v2/20-macro/roles-template.json +10 -0
  220. package/plugins/fancy-story-builder/templates/v2/20-macro/story-template.json +19 -0
  221. package/plugins/fancy-story-builder/templates/v2/20-macro/volumes-template.json +24 -0
  222. package/plugins/fancy-story-builder/templates/v2/20-macro/world-template.json +24 -0
  223. package/plugins/fancy-story-builder/templates/v2/30-cast/characters-template.json +49 -0
  224. package/plugins/fancy-story-builder/templates/v2/30-cast/climaxes-template.json +14 -0
  225. package/plugins/fancy-story-builder/templates/v2/30-cast/conflicts-template.json +30 -0
  226. package/plugins/fancy-story-builder/templates/v2/30-cast/factions-template.json +18 -0
  227. package/plugins/fancy-story-builder/templates/v2/30-cast/foreshadows-template.json +29 -0
  228. package/plugins/fancy-story-builder/templates/v2/30-cast/information-template.json +29 -0
  229. package/plugins/fancy-story-builder/templates/v2/30-cast/items-template.json +13 -0
  230. package/plugins/fancy-story-builder/templates/v2/30-cast/locations-template.json +12 -0
  231. package/plugins/fancy-story-builder/templates/v2/30-cast/payoffs-template.json +16 -0
  232. package/plugins/fancy-story-builder/templates/v2/30-cast/relations-template.json +44 -0
  233. package/plugins/fancy-story-builder/templates/v2/40-rhythm/chapter-map-template.json +81 -0
  234. package/plugins/fancy-story-builder/templates/v2/40-rhythm/outline-template.json +162 -0
  235. package/plugins/fancy-story-builder/templates/v2/50-writing/ledger-template.json +46 -0
  236. package/plugins/fancy-story-builder/templates/v2/60-deslop/report-template.json +10 -0
  237. 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 +427 -0
  238. package/plugins/fancy-story-builder/templates//350/212/202/345/245/217//347/253/240/350/212/202/347/273/206/347/272/262/346/250/241/346/235/277.json +34 -0
  239. package/plugins/fancy-story-builder/templates//350/212/202/345/245/217//347/253/240/350/212/202/350/247/204/345/210/222/346/250/241/346/235/277.json +24 -0
  240. package/plugins/fancy-story-builder/templates//350/212/202/345/245/217//350/212/202/345/245/217/345/217/202/346/225/260/346/250/241/346/235/277.json +27 -0
  241. 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 +168 -0
  242. 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 +184 -0
  243. 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
  244. 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 +131 -0
  245. 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 +85 -0
  246. 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 +103 -0
  247. 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 +68 -0
  248. 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 +142 -0
  249. 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 +292 -0
  250. 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 +266 -0
  251. 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 +336 -0
  252. package/plugins/fancy-story-builder/templates//351/252/250/346/236/266//345/261/202/347/272/247/346/250/241/346/235/277.json +65 -0
  253. 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 +308 -0
  254. package/plugins/fancy-story-builder/templates.js +1446 -0
  255. package/plugins/fancy-story-builder/wiki-references.js +540 -0
  256. package/plugins/fancy-story-builder/writing-driver.js +1388 -0
  257. package/plugins/fancy-topic/index.js +1057 -0
  258. package/plugins/fancy-topic/prompts/01-exploration-prompt.md +115 -0
  259. package/plugins/fancy-topic/scripts/save-suggestion.js +219 -0
  260. package/plugins/fancy-topic/scripts/select-topic.js +317 -0
  261. package/plugins/fancy-topic/templates.js +476 -0
  262. package/plugins/fancy-topic/tool-deploy.js +200 -0
  263. package/commands/bootstrap/index.js +0 -164
  264. package/commands/bootstrap/init-project.js +0 -121
  265. package/commands/scan/index.js +0 -283
  266. package/commands/scan/platforms/fanqie.js +0 -146
  267. package/commands/scan/platforms/jinjiang.js +0 -143
  268. package/commands/scan/platforms/qidian.js +0 -102
  269. package/commands/scan/platforms/qimao.js +0 -127
  270. package/commands/scan/scraper.js +0 -195
  271. package/infra.js +0 -160
@@ -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
+ }