@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,429 @@
1
+ /**
2
+ * stage-contract.js v1.0
3
+ * 产物契约的分节抽取 + 骨架相关关系筛选。
4
+ *
5
+ * 为什么需要分节:
6
+ * 血肉提示词原本 38.5KB,是一份"LLM 自己开车"的执行手册。
7
+ * 改成插件驱动之后,每轮只需要带:总纲 + 任务输入 + 铁律 + 本文件的字段契约
8
+ * + 质量正反例 + (重试时)错误码速查。按小节抽取能让每轮提示词降到
9
+ * 十几 KB,同时保留全部质量约束。
10
+ *
11
+ * 契约文件里的分节标记形如:<!-- contract:file:角色 -->
12
+ */
13
+
14
+ // 分节标记:<!-- contract:名字 -->
15
+ // 名字允许中文(例如 file:角色),所以用"非空白非尖括号"而不是 ASCII 类。
16
+ const MARKER_RE = /<!--\s*contract:([^\s>]+)\s*-->/g;
17
+
18
+ /**
19
+ * 把契约文件切成小节。
20
+ *
21
+ * @param {string} content 契约文件全文
22
+ * @returns {Object} { head, order, sections }
23
+ */
24
+ export function splitContractSections(content) {
25
+ const text = typeof content === 'string' ? content : '';
26
+ const order = [];
27
+ const sections = {};
28
+ let head = '';
29
+ let current = null;
30
+ let buffer = '';
31
+ let lastIndex = 0;
32
+
33
+ const re = new RegExp(MARKER_RE.source, 'g');
34
+ let m;
35
+ while ((m = re.exec(text)) !== null) {
36
+ const chunk = text.slice(lastIndex, m.index);
37
+ if (current === null) head += chunk;
38
+ else buffer += chunk;
39
+ if (current !== null) {
40
+ sections[current] = buffer.trim();
41
+ buffer = '';
42
+ }
43
+ current = m[1];
44
+ order.push(current);
45
+ lastIndex = re.lastIndex;
46
+ }
47
+ const tail = text.slice(lastIndex);
48
+ if (current === null) head += tail;
49
+ else {
50
+ buffer += tail;
51
+ sections[current] = buffer.trim();
52
+ }
53
+
54
+ return { head: head.trim(), order: order, sections: sections };
55
+ }
56
+
57
+ /**
58
+ * 按名字取若干小节并拼成一段提示词。
59
+ *
60
+ * @param {string} content 契约文件全文
61
+ * @param {string[]} names 小节名,按给定顺序拼接
62
+ * @returns {Object} { text, missing }
63
+ */
64
+ export function pickContractSections(content, names) {
65
+ const split = splitContractSections(content);
66
+ const parts = [];
67
+ const missing = [];
68
+ for (const name of names || []) {
69
+ const sec = split.sections[name];
70
+ if (sec === undefined || sec === '') {
71
+ missing.push(name);
72
+ continue;
73
+ }
74
+ parts.push(sec);
75
+ }
76
+ return { text: parts.join('\n\n---\n\n'), missing: missing };
77
+ }
78
+
79
+ // ============================================================
80
+ // 骨架关系筛选
81
+ // ============================================================
82
+
83
+ /**
84
+ * 每张关系表用哪些字段装 id。
85
+ * match —— 命中任一字段即算"相关"
86
+ * anchor —— 有 anchor 时,必须命中 anchor 里的某个字段才算相关
87
+ * (用于关系方向:character_relations 的 from 是关系发起方,
88
+ * 只有 from 落在本批里,血肉才需要写这条关系)
89
+ */
90
+ const RELATION_TABLES = {
91
+ character_relations: { match: ['from', 'to'], anchor: ['from'] },
92
+ other_relations: { match: ['from', 'to'], anchor: null },
93
+ faction_membership: { match: ['character', 'faction'], anchor: null },
94
+ faction_relations: { match: ['from', 'to'], anchor: ['from'] },
95
+ conflict_participants: { match: ['conflict', 'protagonist_side', 'main_antagonist', 'secondary_antagonist', 'third_party'], anchor: null },
96
+ branch_protagonist: { match: ['branch', 'character'], anchor: null },
97
+ item_owner: { match: ['item', 'owner_id'], anchor: null },
98
+ location_conflict: { match: ['location', 'conflicts'], anchor: null },
99
+ ending_character: { match: ['ending', 'character'], anchor: null },
100
+ main_line_branch_relation: { match: ['main_line', 'branch'], anchor: null }
101
+ };
102
+
103
+ /**
104
+ * 从一条关系记录里抽出所有 id 形态的值(字符串或字符串数组)。
105
+ */
106
+ function relationIds(record, fields) {
107
+ const out = [];
108
+ for (const f of fields) {
109
+ const v = record[f];
110
+ if (typeof v === 'string' && v) out.push(v);
111
+ else if (Array.isArray(v)) {
112
+ for (const x of v) if (typeof x === 'string' && x) out.push(x);
113
+ }
114
+ }
115
+ return out;
116
+ }
117
+
118
+ /**
119
+ * 筛出与本批实体相关的关系,并渲染成提示词可读的清单。
120
+ *
121
+ * @param {Object} relations 骨架 关系.json 的内容
122
+ * @param {Object} idToName id -> name 映射(来自骨架 实体.json)
123
+ * @param {string[]} ids 本批涉及的 id 集合
124
+ * @returns {Object} { lines: string[], count: number }
125
+ */
126
+ export function collectRelevantRelations(relations, idToName, ids) {
127
+ const idSet = new Set(ids || []);
128
+ const names = idToName || {};
129
+ const lines = [];
130
+
131
+ if (!relations || typeof relations !== 'object') return { lines: lines, count: 0 };
132
+
133
+ for (const table of Object.keys(RELATION_TABLES)) {
134
+ const spec = RELATION_TABLES[table];
135
+ if (!spec) continue;
136
+ const rows = relations[table];
137
+ if (!Array.isArray(rows)) continue;
138
+
139
+ const hits = [];
140
+ for (const row of rows) {
141
+ if (!row || typeof row !== 'object') continue;
142
+ const matchIds = relationIds(row, spec.match);
143
+ const matched = matchIds.filter((x) => idSet.has(x));
144
+ if (matched.length === 0) continue;
145
+ if (spec.anchor) {
146
+ const anchorIds = relationIds(row, spec.anchor);
147
+ const anchorHit = anchorIds.some((x) => idSet.has(x));
148
+ if (!anchorHit) continue;
149
+ }
150
+ hits.push({ row: row, ids: matchIds });
151
+ }
152
+
153
+ if (hits.length === 0) continue;
154
+
155
+ lines.push('**' + table + '**(' + hits.length + ' 条)');
156
+ for (const hit of hits) {
157
+ const parts = [];
158
+ for (const key of Object.keys(hit.row)) {
159
+ const v = hit.row[key];
160
+ if (typeof v === 'string') {
161
+ parts.push(key + '=' + v + nameSuffix(v, names));
162
+ } else if (Array.isArray(v)) {
163
+ parts.push(key + '=[' + v.map((x) => (typeof x === 'string' ? x + nameSuffix(x, names) : String(x))).join(', ') + ']');
164
+ } else if (v !== null && typeof v === 'object') {
165
+ parts.push(key + '=' + JSON.stringify(v));
166
+ } else {
167
+ parts.push(key + '=' + String(v));
168
+ }
169
+ }
170
+ // 表名写在行内,模型逐行读时不用回头对照上一行的标题
171
+ lines.push('- [' + table + '] ' + parts.join(';'));
172
+ }
173
+ }
174
+
175
+ return { lines: lines, count: lines.length };
176
+ }
177
+
178
+ function nameSuffix(id, names) {
179
+ if (!names || !names[id]) return '';
180
+ return '(' + names[id] + ')';
181
+ }
182
+
183
+ /**
184
+ * 从骨架 实体.json 里建立 id -> name 映射。
185
+ */
186
+ export function buildIdToName(skeletonEntities) {
187
+ const map = {};
188
+ if (!skeletonEntities || typeof skeletonEntities !== 'object') return map;
189
+ for (const key of Object.keys(skeletonEntities)) {
190
+ const list = skeletonEntities[key];
191
+ if (!Array.isArray(list)) continue;
192
+ for (const e of list) {
193
+ if (!e || typeof e !== 'object') continue;
194
+ if (e.id && e.name) map[e.id] = e.name;
195
+ }
196
+ }
197
+ return map;
198
+ }
199
+
200
+ // ============================================================
201
+ // 节奏阶段:本批章号的真源信息
202
+ // ============================================================
203
+
204
+ /**
205
+ * 把"本批章号在时序映射里的对应信息"渲染成提示词可读的行。
206
+ *
207
+ * 为什么必须给:节奏阶段的章节号唯一真源是 `时序映射.json`,
208
+ * 而 `章节细纲.json` 的 hook_head / hook_tail 必须与 hook_timeline
209
+ * 中该章的钩子对齐(校验器按章比对)。模型自己编章号 / 编钩子
210
+ * 必然对不上,只能把真源原样喂给它。
211
+ *
212
+ * @param {Object} opts
213
+ * @param {Object} opts.timeline 已合并的 时序映射.json
214
+ * @param {number[]} opts.chapters 本批要写的章号
215
+ * @param {Object} [opts.idToName] id -> name(骨架实体)
216
+ * @param {Object} [opts.climaxTypes] climax_id -> type(区分"大高潮")
217
+ * @returns {Object} { lines: string[], count: number }
218
+ */
219
+ export function buildRhythmChapterRefs(opts) {
220
+ const o = opts || {};
221
+ const timeline = o.timeline && typeof o.timeline === 'object' ? o.timeline : {};
222
+ const chapters = Array.isArray(o.chapters) ? o.chapters : [];
223
+ const names = o.idToName || {};
224
+ const climaxTypes = o.climaxTypes || {};
225
+ const lines = [];
226
+
227
+ const hookByCh = new Map();
228
+ for (const h of (timeline.hook_timeline || [])) {
229
+ if (h && typeof h.ch === 'number') hookByCh.set(h.ch, h);
230
+ }
231
+ const climaxByCh = new Map();
232
+ for (const c of (timeline.climax_timeline || [])) {
233
+ if (c && typeof c.chapter === 'number') {
234
+ if (!climaxByCh.has(c.chapter)) climaxByCh.set(c.chapter, []);
235
+ climaxByCh.get(c.chapter).push(c);
236
+ }
237
+ }
238
+ const endingByCh = new Map();
239
+ for (const e of (timeline.ending_timeline || [])) {
240
+ if (e && typeof e.chapter === 'number') {
241
+ if (!endingByCh.has(e.chapter)) endingByCh.set(e.chapter, []);
242
+ endingByCh.get(e.chapter).push(e);
243
+ }
244
+ }
245
+ const fsByCh = new Map();
246
+ for (const f of (timeline.foreshadow_timeline || [])) {
247
+ if (!f || typeof f !== 'object') continue;
248
+ for (const [ch, kind] of [[f.buried_chapter, '埋'], [f.recovered_chapter, '收']]) {
249
+ if (typeof ch !== 'number') continue;
250
+ if (!fsByCh.has(ch)) fsByCh.set(ch, []);
251
+ fsByCh.get(ch).push(kind + ' ' + f.fs_id + nameOf(f.fs_id, names));
252
+ }
253
+ }
254
+ const mainNodesByCh = new Map();
255
+ for (const m of (timeline.main_line_timeline || [])) {
256
+ for (const n of (m && m.key_nodes) || []) {
257
+ if (!n || typeof n.chapter !== 'number') continue;
258
+ if (!mainNodesByCh.has(n.chapter)) mainNodesByCh.set(n.chapter, []);
259
+ mainNodesByCh.get(n.chapter).push({ main_id: m.main_id, event_ref: n.event_ref });
260
+ }
261
+ }
262
+ const relationshipByCh = new Map();
263
+ for (const r of (timeline.relationship_timeline || [])) {
264
+ if (!r || typeof r.start_chapter !== 'number') continue;
265
+ if (!relationshipByCh.has(r.start_chapter)) relationshipByCh.set(r.start_chapter, []);
266
+ relationshipByCh.get(r.start_chapter).push(r);
267
+ }
268
+ const branchByCh = new Map();
269
+ for (const b of (timeline.branch_timeline || [])) {
270
+ if (!b || typeof b !== 'object') continue;
271
+ for (const [ch, kind] of [[b.start, '起'], [b.turning, '转'], [b.resolution, '收']]) {
272
+ if (typeof ch !== 'number') continue;
273
+ if (!branchByCh.has(ch)) branchByCh.set(ch, []);
274
+ branchByCh.get(ch).push(kind + ' ' + b.branch_id + nameOf(b.branch_id, names));
275
+ }
276
+ }
277
+
278
+ for (const ch of chapters) {
279
+ const parts = [];
280
+ const hook = hookByCh.get(ch);
281
+ if (hook) {
282
+ parts.push('钩子 头=' + str(hook.hook_head) + ' 尾=' + str(hook.hook_tail) + ' 强度=' + str(hook.hook_strength));
283
+ }
284
+ for (const c of (climaxByCh.get(ch) || [])) {
285
+ const type = climaxTypes[c.climax_id] ? '(' + climaxTypes[c.climax_id] + ')' : '';
286
+ parts.push('高潮 ' + c.climax_id + nameOf(c.climax_id, names) + type);
287
+ }
288
+ for (const e of (endingByCh.get(ch) || [])) {
289
+ parts.push('结局 ' + e.ending_id + nameOf(e.ending_id, names));
290
+ }
291
+ for (const n of (mainNodesByCh.get(ch) || [])) {
292
+ parts.push('主线节点 ' + n.main_id + nameOf(n.main_id, names) + ':' + str(n.event_ref));
293
+ }
294
+ for (const r of (relationshipByCh.get(ch) || [])) {
295
+ parts.push('关系阶段 ' + r.from + nameOf(r.from, names) + ' → ' + r.to + nameOf(r.to, names) +
296
+ ':' + str(r.stage) + ' / ' + str(r.state) +
297
+ (r.trigger_event ? '(触发:' + str(r.trigger_event) + ')' : ''));
298
+ }
299
+ for (const b of (branchByCh.get(ch) || [])) {
300
+ parts.push('支线 ' + b);
301
+ }
302
+ for (const f of (fsByCh.get(ch) || [])) {
303
+ parts.push('伏笔 ' + f);
304
+ }
305
+ if (!parts.length) {
306
+ parts.push('(时序映射里本章没有钩子/高潮/结局/主线节点记录)');
307
+ }
308
+ lines.push('- 第 ' + ch + ' 章:' + parts.join(';'));
309
+ }
310
+
311
+ return { lines: lines, count: lines.length };
312
+ }
313
+
314
+ function str(v) {
315
+ if (v === null || v === undefined) return '';
316
+ return String(v);
317
+ }
318
+
319
+ function nameOf(id, names) {
320
+ if (!id || !names || !names[id]) return '';
321
+ return '(' + names[id] + ')';
322
+ }
323
+
324
+ /**
325
+ * 渲染"上一批收尾",让模型接着上一批往下写。
326
+ *
327
+ * 章节是按时间推进的:第 N 章要承接第 N-1 章。模型看不到上一批的产物
328
+ * (分片是各自独立的文件),不给收尾就会写成互不相关的片段。
329
+ *
330
+ * @param {Object} opts
331
+ * @param {Object} [opts.shard] 上一批的分片内容(已解析的 JSON)
332
+ * @param {string[]} [opts.arrayKeys] 分片里的数组名(按优先级)
333
+ * @returns {string|null} 收尾描述;取不到返回 null
334
+ */
335
+ export function buildRhythmPrevTail(opts) {
336
+ const o = opts || {};
337
+ const shard = o.shard;
338
+ if (!shard || typeof shard !== 'object') return null;
339
+
340
+ // 收尾只挑"承上启下"用得上的字段
341
+ const FIELDS = [
342
+ ['ch', '章'],
343
+ ['volume', '卷'],
344
+ ['act', '幕'],
345
+ ['scene_focus', '场景重点'],
346
+ ['function_main', '主功能'],
347
+ ['function_aux', '辅功能'],
348
+ ['hook_head', '钩子头'],
349
+ ['hook_tail', '钩子尾'],
350
+ ['hook_strength', '钩子强度']
351
+ ];
352
+
353
+ let last = null;
354
+ for (const key of o.arrayKeys || []) {
355
+ const arr = shard[key];
356
+ if (Array.isArray(arr) && arr.length) {
357
+ last = arr[arr.length - 1];
358
+ break;
359
+ }
360
+ }
361
+ if (!last || typeof last !== 'object') return null;
362
+
363
+ const parts = [];
364
+ for (const [k, label] of FIELDS) {
365
+ if (last[k] === undefined || last[k] === null || last[k] === '') continue;
366
+ parts.push(label + '=' + String(last[k]));
367
+ }
368
+ if (!parts.length) return null;
369
+ return '上一批最后一条:' + parts.join(',');
370
+ }
371
+
372
+ /**
373
+ * 渲染"卷与幕 + 卷章数配额"。
374
+ *
375
+ * 为什么必须给:`章节规划.json` 的每一章都要填 `volume` 与 `act`,
376
+ * 而这两个值是**受约束的枚举**:
377
+ * - volume 必须是骨架 `层级.json` 里的 V001 / V002 …
378
+ * - act 必须是该卷 `层级.json` 的 acts 里出现过的幕序号
379
+ * (校验器 RHYTHM_ACT_INVALID 逐条查)
380
+ * - 每卷章数要接近 `框架.json` 的 volume_ratio(RHYTHM_VOLUME_DIST)
381
+ * 模型看不到这三张表就只能瞎编,编出来的卷号 100% 触发 RHYTHM_REF_MISSING。
382
+ *
383
+ * @param {Object} opts
384
+ * @param {Object} opts.hierarchy 骨架 层级.json
385
+ * @param {Object} opts.framework 骨架 框架.json
386
+ * @returns {Object} { lines: string[], count: number }
387
+ */
388
+ export function buildRhythmSkeletonOutline(opts) {
389
+ const o = opts || {};
390
+ const hierarchy = o.hierarchy && typeof o.hierarchy === 'object' ? o.hierarchy : {};
391
+ const framework = o.framework && typeof o.framework === 'object' ? o.framework : {};
392
+ const lines = [];
393
+
394
+ // 卷 → 该卷的幕序号集合
395
+ const actsByVolume = new Map();
396
+ for (const a of (hierarchy.acts || [])) {
397
+ if (!a || !a.volume || typeof a.act_index !== 'number') continue;
398
+ if (!actsByVolume.has(a.volume)) actsByVolume.set(a.volume, new Set());
399
+ actsByVolume.get(a.volume).add(a.act_index);
400
+ }
401
+
402
+ const volumes = Array.isArray(hierarchy.volumes) ? hierarchy.volumes : [];
403
+ if (volumes.length) {
404
+ lines.push('卷与幕(骨架 层级.json,volume 只能取这里的 id,act 只能取该卷的幕序号):');
405
+ for (const v of volumes) {
406
+ if (!v || !v.id) continue;
407
+ const acts = actsByVolume.get(v.id);
408
+ const actText = acts && acts.size ? [...acts].sort((a, b) => a - b).join('、') : '(骨架未定义)';
409
+ lines.push('- ' + v.id + (v.name ? '(' + v.name + ')' : '') + ':幕 ' + actText);
410
+ }
411
+ }
412
+
413
+ const ratios = Array.isArray(framework.volume_ratio) ? framework.volume_ratio : [];
414
+ if (ratios.length) {
415
+ const T = framework.capacity && typeof framework.capacity.target_chapters === 'number'
416
+ ? framework.capacity.target_chapters
417
+ : null;
418
+ lines.push('');
419
+ lines.push('卷章数配额(骨架 框架.json volume_ratio' + (T ? ',T=' + T : '') + ',实际章数与之相差超过 1 章会报 warning):');
420
+ for (const r of ratios) {
421
+ if (!r || !r.volume_id) continue;
422
+ const pct = typeof r.chapter_ratio === 'number' ? Math.round(r.chapter_ratio * 100) + '%' : String(r.chapter_ratio);
423
+ const approx = T ? '(约 ' + Math.round(T * r.chapter_ratio) + ' 章)' : '';
424
+ lines.push('- ' + r.volume_id + ':' + pct + approx);
425
+ }
426
+ }
427
+
428
+ return { lines: lines, count: lines.length };
429
+ }