@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,390 @@
1
+ /**
2
+ * patch-driver.js —— 用户意见 → llm_wiki 影响分析 → DSH LLM 修订
3
+ *
4
+ * 设计原则:
5
+ * - 影响分析先于修改;分析结果不可靠时按“有影响”处理,要求用户确认。
6
+ * - patch 只改已生成阶段的事实源 JSON,不创建未生成阶段,不直接改 wiki。
7
+ * - 修改后按阶段级联校验:改骨架就校验骨架+血肉+节奏;只改血肉就校验血肉+节奏。
8
+ * - 纯函数(提示词、解析、阶段检测)与网络/LLM 调用分离,方便测试。
9
+ */
10
+ import { existsSync, readFileSync, readdirSync } from 'node:fs';
11
+ import { join } from 'node:path';
12
+ import { referenceLogPromptBlock } from './scripts/reference-log.js';
13
+
14
+ export const PATCH_STAGE_ORDER = ['skeleton', 'flesh', 'rhythm'];
15
+ export const PATCH_STAGE_LABEL = { skeleton: '骨架', flesh: '血肉', rhythm: '节奏', writing: '写作' };
16
+ export const PATCH_STAGE_DIR = { skeleton: '骨架', flesh: '血肉', rhythm: '节奏', writing: '写作' };
17
+
18
+ const SRC = ['设定', 'raw', 'sources'];
19
+
20
+ export function patchStageDir(projectRoot, stage) {
21
+ return join(projectRoot, ...SRC, PATCH_STAGE_DIR[stage] || stage);
22
+ }
23
+
24
+ function jsonFilesIn(dir) {
25
+ if (!existsSync(dir)) return [];
26
+ try {
27
+ return readdirSync(dir)
28
+ .filter((name) => name.endsWith('.json') && !name.startsWith('.'))
29
+ .sort()
30
+ .map((name) => join(dir, name));
31
+ } catch {
32
+ return [];
33
+ }
34
+ }
35
+
36
+ /** 当前已经产出、可以被 patch 的结构化阶段。 */
37
+ export function detectGeneratedStages(projectRoot) {
38
+ const stages = [];
39
+ if (existsSync(join(patchStageDir(projectRoot, 'skeleton'), '实体.json'))) stages.push('skeleton');
40
+ if (jsonFilesIn(patchStageDir(projectRoot, 'flesh')).length > 0) stages.push('flesh');
41
+ if (jsonFilesIn(patchStageDir(projectRoot, 'rhythm')).length > 0) stages.push('rhythm');
42
+ return stages;
43
+ }
44
+
45
+ /** 写作阶段只提示,不自动 patch。 */
46
+ export function hasWritingFiles(projectRoot) {
47
+ const dir = patchStageDir(projectRoot, 'writing');
48
+ if (!existsSync(dir)) return false;
49
+ try {
50
+ return readdirSync(dir).some((name) => /^第\d+章\.md$/.test(name));
51
+ } catch {
52
+ return false;
53
+ }
54
+ }
55
+
56
+ /** patch 的事实源文件:只收各阶段顶层 JSON,派生文件(细纲/档案)不算。 */
57
+ export function collectPatchFiles(projectRoot, stages) {
58
+ const out = [];
59
+ for (const stage of stages || []) {
60
+ if (PATCH_STAGE_ORDER.indexOf(stage) < 0) continue;
61
+ for (const file of jsonFilesIn(patchStageDir(projectRoot, stage))) out.push(file);
62
+ }
63
+ return out;
64
+ }
65
+
66
+ /** 文件快照:path → 内容。 */
67
+ export function snapshotPatchFiles(files) {
68
+ const map = new Map();
69
+ for (const file of files || []) {
70
+ try { map.set(file, readFileSync(file, 'utf-8')); } catch { /* ignore */ }
71
+ }
72
+ return map;
73
+ }
74
+
75
+ /** 和快照相比,哪些阶段发生了实际变化(含新增 JSON 文件)。 */
76
+ export function changedPatchStages(projectRoot, stages, before) {
77
+ const changed = [];
78
+ for (const stage of stages || []) {
79
+ const dir = patchStageDir(projectRoot, stage);
80
+ const files = jsonFilesIn(dir);
81
+ const prefix = dir + '/';
82
+ const deleted = before
83
+ ? [...before.keys()].some((file) => file.startsWith(prefix) && !existsSync(file))
84
+ : false;
85
+ const modified = files.some((file) => {
86
+ if (!before || !before.has(file)) return true;
87
+ try { return readFileSync(file, 'utf-8') !== before.get(file); } catch { return true; }
88
+ });
89
+ if (deleted || modified) changed.push(stage);
90
+ }
91
+ return changed;
92
+ }
93
+
94
+ /** 修改意见 → 影响分析提示词。 */
95
+ export function buildImpactAnalysisPrompt(opts) {
96
+ const o = opts || {};
97
+ const userRequest = String(o.userRequest || '').trim();
98
+ const stages = Array.isArray(o.generatedStages) ? o.generatedStages : [];
99
+ return [
100
+ '你是小说设定影响分析器。用户提出了一条对当前故事的修改意见,请判断它会带来什么影响。',
101
+ '',
102
+ '## 用户意见',
103
+ userRequest,
104
+ '',
105
+ '## 当前已生成阶段',
106
+ stages.length ? stages.map((s) => '- ' + (PATCH_STAGE_LABEL[s] || s)).join('\n') : '- (无)',
107
+ '',
108
+ '## 你的任务',
109
+ '只基于当前 LLM Wiki 中真实存在的页面分析,不要发明页面里没有的设定。',
110
+ '重点检查:',
111
+ '- 角色关系类型与关系阶段;',
112
+ '- 冲突参与方、主线/支线;',
113
+ '- 伏笔的埋设与回收;',
114
+ '- 章节时间线、角色状态、信息差;',
115
+ '- 已写正文是否出现与该修改相反的既成事实。',
116
+ '',
117
+ '## 输出格式(必须是 JSON,不要只写散文)',
118
+ '```json',
119
+ '{',
120
+ ' "verdict": "ok" | "impact" | "unknown",',
121
+ ' "confidence": "high" | "medium" | "low",',
122
+ ' "summary": "一句话结论",',
123
+ ' "effects": ["具体影响 1", "具体影响 2"],',
124
+ ' "affected_pages": ["页面 ID 或标题"],',
125
+ ' "affected_stages": ["skeleton", "flesh", "rhythm", "writing"],',
126
+ ' "recommendation": "建议怎么处理"',
127
+ '}',
128
+ '```',
129
+ '',
130
+ '规则:',
131
+ '- 只有确认没有任何矛盾、没有下游变化时,才允许 verdict="ok"。',
132
+ '- 不确定时必须 verdict="unknown",不能猜 ok。',
133
+ '- 如果修改会改变关系,必须检查双方关系类型、关系阶段、章节落点和正文。',
134
+ '- 如果修改只影响措辞、不影响事实和下游引用,才可以 ok。',
135
+ ].join('\n');
136
+ }
137
+
138
+ /** llm_wiki /chat 的深度分析请求体。 */
139
+ export function buildImpactAnalysisRequest(opts) {
140
+ const o = opts || {};
141
+ return {
142
+ message: buildImpactAnalysisPrompt(o),
143
+ mode: 'deep',
144
+ retrievalMode: 'smart',
145
+ tools: { wiki: true, web: false, anytxt: false },
146
+ topK: 10,
147
+ includeContent: true,
148
+ history: [],
149
+ historyExplicit: true,
150
+ persistSession: false,
151
+ stream: false,
152
+ };
153
+ }
154
+
155
+ /** 从文本里提取第一个完整 JSON 对象(容忍代码块/前后说明)。 */
156
+ export function extractFirstJsonObject(text) {
157
+ const source = String(text || '');
158
+ for (let start = source.indexOf('{'); start >= 0; start = source.indexOf('{', start + 1)) {
159
+ let depth = 0;
160
+ let inString = false;
161
+ let escaped = false;
162
+ for (let i = start; i < source.length; i++) {
163
+ const ch = source[i];
164
+ if (inString) {
165
+ if (escaped) { escaped = false; continue; }
166
+ if (ch === '\\') { escaped = true; continue; }
167
+ if (ch === '"') inString = false;
168
+ continue;
169
+ }
170
+ if (ch === '"') { inString = true; continue; }
171
+ if (ch === '{') depth += 1;
172
+ else if (ch === '}') {
173
+ depth -= 1;
174
+ if (depth === 0) {
175
+ try { return JSON.parse(source.slice(start, i + 1)); } catch { break; }
176
+ }
177
+ }
178
+ }
179
+ }
180
+ return null;
181
+ }
182
+
183
+ function asStringArray(value) {
184
+ const format = (x) => {
185
+ if (x === null || x === undefined) return '';
186
+ if (typeof x === 'string') return x.trim();
187
+ if (typeof x === 'object') {
188
+ try { return JSON.stringify(x); } catch { return String(x); }
189
+ }
190
+ return String(x);
191
+ };
192
+ if (Array.isArray(value)) return value.map(format).filter(Boolean);
193
+ if (typeof value === 'string' && value.trim()) return [value.trim()];
194
+ if (value && typeof value === 'object') return [format(value)];
195
+ return [];
196
+ }
197
+
198
+ function normalizeVerdict(value) {
199
+ const v = String(value || '').trim().toLowerCase();
200
+ if (v === 'ok' || v === 'no-impact' || v === 'no_impact' || v === 'no impact' || v === 'pass' || v === '无影响') return 'ok';
201
+ if (v === 'impact' || v === 'affected' || v === 'change' || v === '有影响') return 'impact';
202
+ return 'unknown';
203
+ }
204
+
205
+ function normalizeConfidence(value) {
206
+ const v = String(value || '').trim().toLowerCase();
207
+ if (v === 'high' || v === 'medium' || v === 'low') return v;
208
+ if (v === '高' || v === '高置信') return 'high';
209
+ if (v === '中' || v === '中置信') return 'medium';
210
+ if (v === '低' || v === '低置信') return 'low';
211
+ return 'medium';
212
+ }
213
+
214
+ /**
215
+ * 解析影响分析答案。
216
+ * JSON 优先;没有 JSON 时接受 PATCH_IMPACT/VERDICT 标记。
217
+ * 无法解析时返回 unknown(宁可多让用户确认一次,也不自动改错)。
218
+ */
219
+ export function parseImpactAnalysis(text) {
220
+ const raw = String(text || '').trim();
221
+ const json = extractFirstJsonObject(raw);
222
+ if (json && typeof json === 'object') {
223
+ return {
224
+ verdict: normalizeVerdict(json.verdict || json.status || json.result),
225
+ confidence: normalizeConfidence(json.confidence),
226
+ summary: String(json.summary || json.why || '').trim(),
227
+ effects: asStringArray(json.effects || json.impact || json.impacts),
228
+ affectedPages: asStringArray(json.affected_pages || json.affectedPages),
229
+ affectedStages: asStringArray(json.affected_stages || json.affectedStages)
230
+ .map((s) => ({ 骨架: 'skeleton', 血肉: 'flesh', 节奏: 'rhythm', 写作: 'writing', 正文: 'writing' }[s] || s))
231
+ .filter((s) => PATCH_STAGE_ORDER.indexOf(s) >= 0 || s === 'writing'),
232
+ recommendation: String(json.recommendation || '').trim(),
233
+ raw,
234
+ };
235
+ }
236
+
237
+ const marker = /(?:PATCH_IMPACT|VERDICT|影响结论)\s*[::]\s*(OK|IMPACT|UNKNOWN|无影响|有影响|不确定)/i.exec(raw);
238
+ if (marker) {
239
+ const v = marker[1].toLowerCase();
240
+ return {
241
+ verdict: (v === 'ok' || v === '无影响') ? 'ok' : (v === 'unknown' || v === '不确定' ? 'unknown' : 'impact'),
242
+ confidence: 'medium',
243
+ summary: raw.slice(0, 300),
244
+ effects: [],
245
+ affectedPages: [],
246
+ affectedStages: [],
247
+ recommendation: '',
248
+ raw,
249
+ };
250
+ }
251
+
252
+ return {
253
+ verdict: 'unknown',
254
+ confidence: 'low',
255
+ summary: raw ? raw.slice(0, 300) : '影响分析没有返回内容',
256
+ effects: [],
257
+ affectedPages: [],
258
+ affectedStages: [],
259
+ recommendation: '影响分析结果无法解析,请人工确认后再修改。',
260
+ raw,
261
+ };
262
+ }
263
+
264
+ /** 只有明确的 ok + high 才自动改;其余都让用户确认。 */
265
+ export function shouldAutoApplyImpact(impact) {
266
+ return !!impact
267
+ && impact.verdict === 'ok'
268
+ && impact.confidence === 'high'
269
+ && (!impact.effects || impact.effects.length === 0)
270
+ && (!impact.affectedPages || impact.affectedPages.length === 0)
271
+ && (!impact.affectedStages || impact.affectedStages.length === 0);
272
+ }
273
+
274
+ export function formatImpactForUser(impact) {
275
+ const i = impact || {};
276
+ if (i.verdict === 'ok') {
277
+ return '影响分析:未发现明确的下游影响。' + (i.summary ? '\n\n' + i.summary : '');
278
+ }
279
+ const effects = Array.isArray(i.effects) ? i.effects : [];
280
+ const affectedPages = Array.isArray(i.affectedPages) ? i.affectedPages : [];
281
+ const affectedStages = Array.isArray(i.affectedStages) ? i.affectedStages : [];
282
+ const lines = ['影响分析:这次修改可能会影响以下内容。'];
283
+ if (i.summary) lines.push('', i.summary);
284
+ if (effects.length) {
285
+ lines.push('', '具体影响:');
286
+ for (const x of effects) lines.push('- ' + x);
287
+ }
288
+ if (affectedPages.length) {
289
+ lines.push('', '涉及页面:' + affectedPages.join('、'));
290
+ }
291
+ if (affectedStages.length) {
292
+ lines.push('', '涉及阶段:' + affectedStages.map((s) => PATCH_STAGE_LABEL[s] || s).join('、'));
293
+ }
294
+ if (i.recommendation) lines.push('', '建议:' + i.recommendation);
295
+ if (!effects.length && !affectedPages.length && i.summary) {
296
+ lines.push('', '(模型没有列出具体条目,建议按“有影响”处理。)');
297
+ }
298
+ return lines.join('\n');
299
+ }
300
+
301
+ function stageScopeLines(opts) {
302
+ const o = opts || {};
303
+ const stages = Array.isArray(o.generatedStages) ? o.generatedStages : [];
304
+ const patchStages = Array.isArray(o.patchStages) ? o.patchStages : [];
305
+ const lines = [];
306
+ lines.push('已生成阶段:' + (stages.length ? stages.map((s) => PATCH_STAGE_LABEL[s] || s).join('、') : '无'));
307
+ lines.push('本次允许修改阶段:' + (patchStages.length ? patchStages.map((s) => PATCH_STAGE_LABEL[s] || s).join('、') : '无'));
308
+ if (!stages.includes('rhythm')) {
309
+ lines.push('节奏阶段尚未生成:**不要创建节奏目录或节奏 JSON**。');
310
+ }
311
+ if (o.hasWriting) {
312
+ lines.push('写作阶段已有正文:**不要改正文**;如有影响,在报告里列出受影响章节,由用户另行处理。');
313
+ }
314
+ return lines;
315
+ }
316
+
317
+ /** DSH LLM 的 patch 提示词。 */
318
+ export function buildPatchPrompt(opts) {
319
+ const o = opts || {};
320
+ const userRequest = String(o.userRequest || '').trim();
321
+ const impact = o.impact || null;
322
+ const patchStages = Array.isArray(o.patchStages) ? o.patchStages : [];
323
+ const validationErrors = Array.isArray(o.validationErrors) ? o.validationErrors : [];
324
+ const attempt = o.attempt || 1;
325
+ const lines = [];
326
+ lines.push('# 本轮任务:按用户意见修订已生成的故事设定');
327
+ lines.push('');
328
+ lines.push('这是第 ' + attempt + ' 轮 patch。');
329
+ lines.push('');
330
+ lines.push('## 用户意见');
331
+ lines.push(userRequest);
332
+ lines.push('');
333
+ if (impact) {
334
+ lines.push('## llm_wiki 影响分析');
335
+ lines.push(formatImpactForUser(impact));
336
+ lines.push('');
337
+ }
338
+ lines.push('## 阶段边界(硬约束)');
339
+ lines.push(stageScopeLines(o));
340
+ lines.push('');
341
+ lines.push('只允许修改这些阶段的事实源 JSON:');
342
+ for (const stage of patchStages) {
343
+ lines.push('- `' + join('设定/raw/sources', PATCH_STAGE_DIR[stage]) + '/`(' + (PATCH_STAGE_LABEL[stage] || stage) + ')');
344
+ }
345
+ lines.push('');
346
+ lines.push('## 允许改的文件');
347
+ for (const file of (o.files || [])) {
348
+ lines.push('- `' + file + '`');
349
+ }
350
+ lines.push('');
351
+ lines.push('## 修改规则');
352
+ lines.push('1. 只改与用户意见直接相关的最小字段;不要重写整个文件,不要删无关条目。');
353
+ lines.push('2. 保留所有 id、name、枚举、章节号、已有引用;新增/改名会制造下游错误。');
354
+ lines.push('3. 关系类型改变时,必须同步骨架 `关系.json` 和血肉 `角色.json` 的 `relationships[]`。');
355
+ lines.push('4. 关系阶段改变时,只有在节奏阶段已生成时才改 `时序映射.json` 的 `relationship_timeline`;未生成时不要创建。');
356
+ lines.push('5. 血肉阶段不要写章节号;章节落点属于节奏阶段。');
357
+ lines.push('6. 不要修改 `设定/wiki/`、`模板/`、`references/`、校验脚本、`档案/`、`节奏/细纲/`。');
358
+ lines.push('7. 不要修改写作正文(`写作/第NNN章.md`)。');
359
+ lines.push('8. 改完就停,不要运行校验脚本;插件会跑阶段校验器并把错误回喂。');
360
+ lines.push('');
361
+ if (validationErrors.length) {
362
+ lines.push('## 上一轮校验错误(只修这些)');
363
+ lines.push('');
364
+ for (const e of validationErrors) {
365
+ lines.push('- [' + (e.code || 'ERROR') + '] ' + (e.stage ? '[' + (PATCH_STAGE_LABEL[e.stage] || e.stage) + '] ' : '') +
366
+ (e.path || '(无路径)') + ':' + (e.message || '') + (e.hint ? '(' + e.hint + ')' : ''));
367
+ }
368
+ lines.push('');
369
+ lines.push('**只修上面点名的字段**,其余内容保持原样。');
370
+ lines.push('');
371
+ }
372
+ lines.push(referenceLogPromptBlock('patch', { type: 'patch' }, {
373
+ allowedFileText: '除本阶段允许修改的事实源 JSON 外,`设定/.reference-decisions.md` 是本轮唯一允许写的额外文件;不要修改 wiki、模板、references 目录或其它阶段。',
374
+ }));
375
+ lines.push('## 交付');
376
+ lines.push('');
377
+ lines.push('改完用一句话说明改了哪些文件/字段,然后停下。');
378
+ return lines.join('\n');
379
+ }
380
+
381
+ /** 修改后需要校验的阶段:从最早被改的结构化阶段,到所有已生成的下游阶段。 */
382
+ export function buildPatchValidationStages(changedStages, generatedStages) {
383
+ const generated = Array.isArray(generatedStages) ? generatedStages : [];
384
+ const changed = Array.isArray(changedStages) ? changedStages : [];
385
+ const structured = PATCH_STAGE_ORDER.filter((s) => generated.includes(s));
386
+ const changedStructured = changed.filter((s) => PATCH_STAGE_ORDER.includes(s));
387
+ if (!structured.length || !changedStructured.length) return [];
388
+ const earliest = Math.min(...changedStructured.map((s) => PATCH_STAGE_ORDER.indexOf(s)));
389
+ return structured.filter((s) => PATCH_STAGE_ORDER.indexOf(s) >= earliest);
390
+ }