@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,559 @@
1
+ /**
2
+ * Import legacy stage JSON into the story graph.
3
+ *
4
+ * This is the bridge for existing projects and for the old `build` commands.
5
+ * The LLM-native path is `storydb apply`; this importer is for bootstrapping.
6
+ * Each chapter/outline/text is its own change_set.
7
+ */
8
+
9
+ import fs from 'fs';
10
+ import path from 'path';
11
+ import { apply } from './store.js';
12
+
13
+ function readJson(file, fallback = null) {
14
+ try { return JSON.parse(fs.readFileSync(file, 'utf8')); } catch { return fallback; }
15
+ }
16
+
17
+ function asArray(value) {
18
+ return Array.isArray(value) ? value : [];
19
+ }
20
+
21
+ function stagePath(root, stage, ...parts) {
22
+ return path.join(root, '设定', stage, ...parts);
23
+ }
24
+
25
+ async function run(sdb, workflow, summary, ops, decisions = []) {
26
+ return apply(sdb, { workflow, summary, ops, decisions });
27
+ }
28
+
29
+ function idOf(value) {
30
+ return String(value || '').split(':').pop().replace(/[⟨⟩]/g, '');
31
+ }
32
+
33
+ // ---------------------------------------------------------------------------
34
+ // scale
35
+ // ---------------------------------------------------------------------------
36
+
37
+ export async function importScale(sdb, root) {
38
+ const scale = readJson(stagePath(root, '10-scale', 'scale.json'), null);
39
+ if (!scale) throw new Error('缺少 10-scale/scale.json');
40
+ const ops = [{ op: 'create', kind: 'scale', data: scale }];
41
+ for (const volume of asArray(scale.structure && scale.structure.volumes)) {
42
+ const vid = volume.volume_id || volume.id;
43
+ ops.push({
44
+ op: 'create',
45
+ kind: 'volume',
46
+ data: {
47
+ volume_id: vid,
48
+ title: volume.title,
49
+ chapter_start: volume.chapter_start,
50
+ chapter_end: volume.chapter_end,
51
+ goal: volume.goal,
52
+ core_conflict: volume.core_conflict,
53
+ emotional_arc: volume.emotional_arc,
54
+ ending_hook: volume.ending_hook,
55
+ },
56
+ });
57
+ ops.push({ op: 'link', edge: 'has_volume', from: 'story:main', to: 'volume:' + vid, data: {} });
58
+ for (const act of asArray(volume.acts)) {
59
+ const aid = act.act_id || act.id;
60
+ ops.push({
61
+ op: 'create',
62
+ kind: 'act',
63
+ data: {
64
+ act_id: aid,
65
+ volume_id: vid,
66
+ act_index: act.act_index,
67
+ chapter_start: act.chapter_start,
68
+ chapter_end: act.chapter_end,
69
+ goal: act.goal,
70
+ },
71
+ });
72
+ ops.push({ op: 'link', edge: 'has_act', from: 'volume:' + vid, to: 'act:' + aid, data: {} });
73
+ }
74
+ }
75
+ return run(sdb, 'scale', 'import scale', ops);
76
+ }
77
+
78
+ // ---------------------------------------------------------------------------
79
+ // macro
80
+ // ---------------------------------------------------------------------------
81
+
82
+ export async function importMacro(sdb, root) {
83
+ const dir = stagePath(root, '20-macro');
84
+ const world = readJson(path.join(dir, 'world.json'), {});
85
+ const story = readJson(path.join(dir, 'story.json'), {});
86
+ const volumes = readJson(path.join(dir, 'volumes.json'), { volumes: [] });
87
+ const lines = readJson(path.join(dir, 'lines.json'), { main_line: {}, branches: [] });
88
+ const endings = readJson(path.join(dir, 'endings.json'), { endings: [] });
89
+ const emotion = readJson(path.join(dir, 'emotion.json'), { volumes: [] });
90
+ const roles = readJson(path.join(dir, 'roles.json'), { roles: [] });
91
+
92
+ const ops = [
93
+ { op: 'create', kind: 'world_doc', data: world },
94
+ { op: 'create', kind: 'story_doc', data: story },
95
+ { op: 'create', kind: 'emotion_doc', data: { volumes: emotion.volumes || [] } },
96
+ ];
97
+
98
+ for (const volume of asArray(volumes.volumes)) {
99
+ const vid = volume.id || volume.volume_id;
100
+ ops.push({
101
+ op: 'create',
102
+ kind: 'volume',
103
+ data: {
104
+ volume_id: vid,
105
+ title: volume.title,
106
+ chapter_start: volume.chapter_start,
107
+ chapter_end: volume.chapter_end,
108
+ goal: volume.goal,
109
+ core_conflict: volume.core_conflict,
110
+ emotional_arc: volume.emotional_arc,
111
+ ending_hook: volume.ending_hook,
112
+ },
113
+ });
114
+ ops.push({ op: 'link', edge: 'has_volume', from: 'story:main', to: 'volume:' + vid, data: {} });
115
+ for (const act of asArray(volume.acts)) {
116
+ const aid = act.act_id || act.id;
117
+ ops.push({
118
+ op: 'create',
119
+ kind: 'act',
120
+ data: {
121
+ act_id: aid,
122
+ volume_id: vid,
123
+ act_index: act.act_index,
124
+ chapter_start: act.chapter_start,
125
+ chapter_end: act.chapter_end,
126
+ goal: act.goal,
127
+ climax: act.climax,
128
+ },
129
+ });
130
+ ops.push({ op: 'link', edge: 'has_act', from: 'volume:' + vid, to: 'act:' + aid, data: {} });
131
+ }
132
+ }
133
+
134
+ const main = lines.main_line || {};
135
+ if (main.id || main.name) {
136
+ const lineId = main.id || 'M001';
137
+ ops.push({
138
+ op: 'create',
139
+ kind: 'line',
140
+ data: {
141
+ line_id: lineId,
142
+ kind: 'main',
143
+ name: main.name || '主线',
144
+ goal: main.goal,
145
+ },
146
+ });
147
+ for (const node of asArray(main.nodes)) {
148
+ ops.push({
149
+ op: 'create',
150
+ kind: 'line_node',
151
+ data: {
152
+ node_id: node.id,
153
+ line_id: lineId,
154
+ volume: node.volume,
155
+ act: node.act,
156
+ goal: node.goal,
157
+ turn: node.turn,
158
+ },
159
+ });
160
+ }
161
+ }
162
+ for (const branch of asArray(lines.branches)) {
163
+ ops.push({
164
+ op: 'create',
165
+ kind: 'line',
166
+ data: {
167
+ line_id: branch.id,
168
+ kind: 'branch',
169
+ name: branch.name || branch.id,
170
+ goal: branch.goal,
171
+ start: branch.start,
172
+ turning: branch.turning,
173
+ resolution: branch.resolution,
174
+ interweaves: branch.interweaves || [],
175
+ },
176
+ });
177
+ }
178
+
179
+ for (const ending of asArray(endings.endings)) {
180
+ ops.push({
181
+ op: 'create',
182
+ kind: 'ending',
183
+ data: {
184
+ ending_id: ending.id,
185
+ level: ending.level,
186
+ type: ending.type,
187
+ direction: ending.direction,
188
+ required_foreshadow_recovery: ending.required_foreshadow_recovery || [],
189
+ },
190
+ });
191
+ }
192
+
193
+ for (const role of asArray(roles.roles)) {
194
+ ops.push({
195
+ op: 'create',
196
+ kind: 'role_slot',
197
+ data: {
198
+ role_id: role.role_id || role.id,
199
+ name: role.name || role.role_id || role.id || role.function,
200
+ slot_function: role.slot_function || role.function,
201
+ quota_key: role.quota_key,
202
+ },
203
+ });
204
+ }
205
+
206
+ return run(sdb, 'macro', 'import macro', ops);
207
+ }
208
+
209
+ // ---------------------------------------------------------------------------
210
+ // cast
211
+ // ---------------------------------------------------------------------------
212
+
213
+ const CAST_ENTITIES = [
214
+ ['characters', 'characters', 'character'],
215
+ ['factions', 'factions', 'faction'],
216
+ ['locations', 'locations', 'location'],
217
+ ['items', 'items', 'item'],
218
+ ['conflicts', 'conflicts', 'conflict'],
219
+ ['foreshadows', 'foreshadows', 'foreshadow'],
220
+ ['climaxes', 'climaxes', 'climax'],
221
+ ['payoffs', 'payoffs', 'payoff'],
222
+ ['information', 'information_gaps', 'information'],
223
+ ];
224
+
225
+ export async function importCast(sdb, root) {
226
+ const dir = stagePath(root, '30-cast');
227
+ const ops = [];
228
+ for (const [file, listKey, kind] of CAST_ENTITIES) {
229
+ const doc = readJson(path.join(dir, file + '.json'), null);
230
+ if (!doc) throw new Error('缺少 30-cast/' + file + '.json');
231
+ for (const row of asArray(doc[listKey])) {
232
+ const data = { ...row };
233
+ if (kind === 'information') data.info_name = row.info_name || row.name;
234
+ if (kind === 'item' && data.item_function === undefined && data.function !== undefined) {
235
+ data.item_function = data.function;
236
+ }
237
+ ops.push({ op: 'create', kind, data });
238
+ }
239
+ }
240
+
241
+ const rels = readJson(path.join(dir, 'relations.json'), { character_relations: [], faction_relations: [], memberships: [] });
242
+ for (const rel of asArray(rels.character_relations)) {
243
+ ops.push({
244
+ op: 'link', edge: 'relation',
245
+ from: 'character:' + idOf(rel.from),
246
+ to: 'character:' + idOf(rel.to),
247
+ data: { kind: rel.kind, initial_state: rel.initial_state, changes: rel.changes || [] },
248
+ });
249
+ }
250
+ for (const rel of asArray(rels.faction_relations)) {
251
+ ops.push({
252
+ op: 'link', edge: 'faction_relation',
253
+ from: 'faction:' + idOf(rel.from),
254
+ to: 'faction:' + idOf(rel.to),
255
+ data: { kind: rel.kind, initial_state: rel.initial_state, changes: rel.changes || [] },
256
+ });
257
+ }
258
+ for (const member of asArray(rels.memberships)) {
259
+ ops.push({
260
+ op: 'link', edge: 'membership',
261
+ from: 'character:' + idOf(member.character || member.from),
262
+ to: 'faction:' + idOf(member.faction || member.to),
263
+ data: { role: member.role },
264
+ });
265
+ }
266
+
267
+ const conflicts = readJson(path.join(dir, 'conflicts.json'), { conflicts: [] });
268
+ for (const conflict of asArray(conflicts.conflicts)) {
269
+ for (const participant of asArray(conflict.participants)) {
270
+ if (!idOf(participant).startsWith('C')) continue;
271
+ ops.push({
272
+ op: 'link', edge: 'conflict_participant',
273
+ from: 'conflict:' + idOf(conflict.id), to: 'character:' + idOf(participant), data: {},
274
+ });
275
+ }
276
+ }
277
+ const climaxes = readJson(path.join(dir, 'climaxes.json'), { climaxes: [] });
278
+ for (const climax of asArray(climaxes.climaxes)) {
279
+ for (const participant of asArray(climax.participants)) {
280
+ if (!idOf(participant).startsWith('C')) continue;
281
+ ops.push({
282
+ op: 'link', edge: 'climax_participant',
283
+ from: 'climax:' + idOf(climax.id), to: 'character:' + idOf(participant), data: {},
284
+ });
285
+ }
286
+ }
287
+ const foreshadows = readJson(path.join(dir, 'foreshadows.json'), { foreshadows: [] });
288
+ const payoffIds = new Set(asArray(readJson(path.join(dir, 'payoffs.json'), { payoffs: [] }).payoffs).map((row) => idOf(row.id)));
289
+ for (const fsRow of asArray(foreshadows.foreshadows)) {
290
+ if (!fsRow.payoff_target || !payoffIds.has(idOf(fsRow.payoff_target))) continue;
291
+ ops.push({
292
+ op: 'link', edge: 'foreshadow_payoff',
293
+ from: 'foreshadow:' + idOf(fsRow.id), to: 'payoff:' + idOf(fsRow.payoff_target),
294
+ data: { relation: fsRow.recovery_requirement },
295
+ });
296
+ }
297
+
298
+ return run(sdb, 'cast', 'import cast', ops);
299
+ }
300
+
301
+ // ---------------------------------------------------------------------------
302
+ // rhythm
303
+ // ---------------------------------------------------------------------------
304
+
305
+ function actionName(value) {
306
+ const map = {
307
+ 埋设: 'plant', 推进: 'progress', 强化: 'progress', 回收: 'reveal',
308
+ 铺垫: 'setup', 兑现: 'payoff', 收束: 'close',
309
+ created: 'created', progressed: 'progressed', resolved: 'resolved',
310
+ };
311
+ return map[value] || value;
312
+ }
313
+
314
+ export async function importRhythm(sdb, root, opts = {}) {
315
+ const map = readJson(stagePath(root, '40-rhythm', 'chapter_map.json'), null);
316
+ if (!map) throw new Error('缺少 40-rhythm/chapter_map.json');
317
+ const chapters = [...asArray(map.chapters)].sort((a, b) => Number(a.display_no) - Number(b.display_no));
318
+ const results = [];
319
+ for (const chapter of chapters) {
320
+ const no = Number(chapter.display_no);
321
+ const cid = String(chapter.chapter_id);
322
+ const ops = [
323
+ {
324
+ op: 'create', kind: 'chapter',
325
+ data: {
326
+ chapter_id: cid,
327
+ display_no: no,
328
+ title: chapter.title,
329
+ volume: chapter.volume,
330
+ act: chapter.act,
331
+ pov: chapter.pov,
332
+ time: chapter.time || {},
333
+ why: chapter.why || {},
334
+ how: chapter.how || {},
335
+ emotion: chapter.emotion || {},
336
+ hook: chapter.hook || {},
337
+ word_target: Number(chapter.word_target) || 0,
338
+ },
339
+ },
340
+ { op: 'link', edge: 'has_chapter', from: 'act:' + chapter.act, to: 'chapter:' + cid, data: {} },
341
+ ];
342
+ const cast = [...new Set(asArray(chapter.cast).map(idOf))];
343
+ for (const character of cast) {
344
+ ops.push({
345
+ op: 'link', edge: 'chapter_cast', from: 'chapter:' + cid, to: 'character:' + character,
346
+ data: { role: chapter.pov && idOf(chapter.pov) === character ? 'pov' : 'cast' },
347
+ });
348
+ }
349
+ if (chapter.pov && !cast.includes(idOf(chapter.pov))) {
350
+ ops.push({ op: 'link', edge: 'chapter_cast', from: 'chapter:' + cid, to: 'character:' + idOf(chapter.pov), data: { role: 'pov' } });
351
+ }
352
+ for (const character of asArray(chapter.mentioned).map(idOf)) {
353
+ ops.push({ op: 'link', edge: 'chapter_cast', from: 'chapter:' + cid, to: 'character:' + character, data: { role: 'mentioned' } });
354
+ }
355
+ for (const location of [...new Set(asArray(chapter.locations).map(idOf))]) {
356
+ ops.push({ op: 'link', edge: 'chapter_location', from: 'chapter:' + cid, to: 'location:' + location, data: {} });
357
+ }
358
+ for (const node of asArray(chapter.why && chapter.why.main_line_nodes).map(idOf)) {
359
+ ops.push({ op: 'link', edge: 'chapter_line_node', from: 'chapter:' + cid, to: 'line_node:' + node, data: {} });
360
+ }
361
+ for (const branch of asArray(chapter.why && chapter.why.branch_nodes).map(idOf)) {
362
+ ops.push({ op: 'link', edge: 'chapter_branch', from: 'chapter:' + cid, to: 'line:' + branch, data: {} });
363
+ }
364
+ for (const conflict of asArray(chapter.why && chapter.why.conflict).map(idOf)) {
365
+ ops.push({ op: 'link', edge: 'chapter_conflict', from: 'chapter:' + cid, to: 'conflict:' + conflict, data: {} });
366
+ }
367
+ for (const action of asArray(chapter.foreshadow_actions)) {
368
+ ops.push({
369
+ op: 'link', edge: 'foreshadow_action',
370
+ from: 'chapter:' + cid, to: 'foreshadow:' + idOf(action.id),
371
+ data: { action: actionName(action.action), chapter_no: no, detail: action.detail },
372
+ });
373
+ }
374
+ for (const action of asArray(chapter.payoff_actions)) {
375
+ ops.push({
376
+ op: 'link', edge: 'payoff_action',
377
+ from: 'chapter:' + cid, to: 'payoff:' + idOf(action.id),
378
+ data: { action: actionName(action.action), chapter_no: no, detail: action.detail },
379
+ });
380
+ }
381
+ for (const gap of asArray(chapter.information_gaps)) {
382
+ ops.push({
383
+ op: 'link', edge: 'info_action',
384
+ from: 'chapter:' + cid, to: 'information:' + idOf(gap.id),
385
+ data: { action: gap.state || 'progressed', chapter_no: no, detail: gap.detail },
386
+ });
387
+ }
388
+ for (const action of asArray(chapter.ending_actions)) {
389
+ ops.push({
390
+ op: 'link', edge: 'ending_action',
391
+ from: 'chapter:' + cid, to: 'ending:' + idOf(action.id),
392
+ data: { action: 'close', chapter_no: no, detail: action.detail },
393
+ });
394
+ }
395
+ for (const event of asArray(chapter.knowledge_events)) {
396
+ ops.push({
397
+ op: 'knowledge',
398
+ data: {
399
+ chapter_id: cid,
400
+ chapter_no: no,
401
+ subject_type: event.subject_type,
402
+ subject_id: event.subject_id,
403
+ character_id: idOf(event.character),
404
+ change: event.change,
405
+ coverage: event.coverage,
406
+ knowledge: event.knowledge,
407
+ how: event.how,
408
+ source_character: event.source_character,
409
+ scene: event.scene,
410
+ evidence: event.evidence,
411
+ },
412
+ });
413
+ }
414
+ results.push(await run(sdb, 'rhythm', 'import chapter ' + cid, ops));
415
+ if (opts.onProgress) opts.onProgress({ chapter: cid, no });
416
+ }
417
+ return { chapters: results.length, results };
418
+ }
419
+
420
+ // ---------------------------------------------------------------------------
421
+ // outline
422
+ // ---------------------------------------------------------------------------
423
+
424
+ export async function importOutlines(sdb, root) {
425
+ const dir = stagePath(root, '40-rhythm', 'outlines');
426
+ if (!fs.existsSync(dir)) throw new Error('缺少 40-rhythm/outlines/');
427
+ const names = fs.readdirSync(dir).filter((name) => name.endsWith('.json')).sort();
428
+ const results = [];
429
+ for (const name of names) {
430
+ const raw = readJson(path.join(dir, name), null);
431
+ if (!raw) continue;
432
+ const outline = raw.chapter || raw;
433
+ const cid = outline.chapter_id || path.basename(name, '.json');
434
+ const ops = [{
435
+ op: 'create', kind: 'outline',
436
+ data: {
437
+ chapter_id: cid,
438
+ number: outline.number,
439
+ title: outline.title,
440
+ volume: outline.volume,
441
+ act: outline.act,
442
+ word_target: outline.word_target,
443
+ pov: outline.pov,
444
+ time: outline.time,
445
+ why: outline.why,
446
+ continuity: outline.continuity,
447
+ coverage: outline.coverage,
448
+ must_write: outline.must_write || ['(待补)'],
449
+ must_not_write: outline.must_not_write || ['(待补)'],
450
+ ending: outline.ending,
451
+ },
452
+ }];
453
+ const scenes = asArray(outline.how && outline.how.scenes);
454
+ scenes.forEach((scene, index) => {
455
+ const sid = 'SC_' + cid + '_' + (index + 1);
456
+ ops.push({
457
+ op: 'create', kind: 'scene',
458
+ data: {
459
+ outline_id: cid,
460
+ scene_order: index + 1,
461
+ goal: scene.goal,
462
+ obstacle: scene.obstacle,
463
+ turn: scene.turn,
464
+ outcome: scene.outcome,
465
+ location: scene.location,
466
+ },
467
+ });
468
+ ops.push({ op: 'link', edge: 'outline_scene', from: 'outline:' + cid, to: 'scene:' + sid, data: { scene_order: index + 1 } });
469
+ });
470
+ for (const character of asArray(outline.cast)) {
471
+ const id = idOf(typeof character === 'string' ? character : character.id);
472
+ if (!id) continue;
473
+ ops.push({ op: 'link', edge: 'outline_cast', from: 'outline:' + cid, to: 'character:' + id, data: { role: typeof character === 'object' ? character.role_in_chapter : undefined } });
474
+ }
475
+ for (const location of asArray(outline.locations)) {
476
+ const lid = idOf(typeof location === 'string' ? location : location && location.id);
477
+ if (!lid) continue;
478
+ ops.push({ op: 'link', edge: 'outline_location', from: 'outline:' + cid, to: 'location:' + lid, data: {} });
479
+ }
480
+ for (const fsId of asArray(outline.coverage && outline.coverage.foreshadow).map(idOf)) {
481
+ ops.push({ op: 'link', edge: 'outline_foreshadow', from: 'outline:' + cid, to: 'foreshadow:' + fsId, data: {} });
482
+ }
483
+ results.push(await run(sdb, 'outline', 'import outline ' + cid, ops));
484
+ }
485
+ return { outlines: results.length, results };
486
+ }
487
+
488
+ // ---------------------------------------------------------------------------
489
+ // writing
490
+ // ---------------------------------------------------------------------------
491
+
492
+ export async function importWriting(sdb, root) {
493
+ const dir = stagePath(root, '50-writing');
494
+ const ledger = readJson(path.join(dir, 'ledger.json'), { chapters: [] });
495
+ const rows = asArray(ledger.chapters);
496
+ const chaptersDir = path.join(dir, 'chapters');
497
+ const fileNames = fs.existsSync(chaptersDir)
498
+ ? fs.readdirSync(chaptersDir).filter((name) => name.endsWith('.md')).sort()
499
+ : [];
500
+ const results = [];
501
+ for (const name of fileNames) {
502
+ const match = /第(\d+)章\.md$/.exec(name);
503
+ if (!match) continue;
504
+ const no = Number(match[1]);
505
+ const content = fs.readFileSync(path.join(chaptersDir, name), 'utf8');
506
+ const chapter = await sdb.one(
507
+ `SELECT chapter_id FROM chapter WHERE story = story:main AND display_no = $no LIMIT 1;`,
508
+ { no },
509
+ );
510
+ if (!chapter) continue;
511
+ const row = rows.find((r) => Number(r.ch) === no || r.chapter_id === chapter.chapter_id) || {};
512
+ const ops = [
513
+ {
514
+ op: 'create', kind: 'chapter_text',
515
+ data: {
516
+ chapter_id: chapter.chapter_id,
517
+ display_no: no,
518
+ content,
519
+ word_count: content.replace(/\s/g, '').length,
520
+ status: 'draft',
521
+ },
522
+ },
523
+ {
524
+ op: 'create', kind: 'handoff',
525
+ data: {
526
+ chapter_id: chapter.chapter_id,
527
+ end_location: (row.locations && row.locations[row.locations.length - 1]) || undefined,
528
+ end_time: row.time,
529
+ end_characters: row.cast || [],
530
+ open_threads: row.ending_hook ? [row.ending_hook] : [],
531
+ next_scene_hint: row.ending_hook,
532
+ continuity_notes: '',
533
+ status: 'draft',
534
+ },
535
+ },
536
+ ];
537
+ results.push(await run(sdb, 'writing', 'import text ' + chapter.chapter_id, ops));
538
+ }
539
+ return { chapters: results.length, results };
540
+ }
541
+
542
+ // ---------------------------------------------------------------------------
543
+ // dispatch
544
+ // ---------------------------------------------------------------------------
545
+
546
+ const IMPORTERS = {
547
+ scale: importScale,
548
+ macro: importMacro,
549
+ cast: importCast,
550
+ rhythm: importRhythm,
551
+ outline: importOutlines,
552
+ writing: importWriting,
553
+ };
554
+
555
+ export async function importStage(sdb, stage, root, opts = {}) {
556
+ const importer = IMPORTERS[stage];
557
+ if (!importer) throw new Error('未知导入阶段:' + stage);
558
+ return importer(sdb, root, opts);
559
+ }
@@ -0,0 +1,99 @@
1
+ #!/usr/bin/env node
2
+ /**
3
+ * MCP stdio server for fancy-webnovel.
4
+ *
5
+ * Start it with:
6
+ * node plugins/fancy-story-builder/storydb/mcp.js [--workspace /path/to/workspace]
7
+ *
8
+ * All tools are in `tools.js`. The server is intentionally project-agnostic:
9
+ * each tool accepts an optional `project` (name or UUID) and resolves the
10
+ * active project/cwd when omitted.
11
+ */
12
+
13
+ import readline from 'readline';
14
+ import { TOOLS, callTool, closeAll } from './tools.js';
15
+ import { flattenError } from './client.js';
16
+
17
+ const argv = process.argv.slice(2);
18
+ const flag = (name) => {
19
+ const idx = argv.indexOf('--' + name);
20
+ return idx >= 0 ? argv[idx + 1] : undefined;
21
+ };
22
+ const workspace = flag('workspace');
23
+ if (workspace) process.env.FANCY_HOME = workspace;
24
+
25
+ const SERVER_INFO = { name: 'fancy-webnovel', version: '3.0.0' };
26
+ const PROTOCOL_VERSION = '2024-11-05';
27
+
28
+ function send(message) {
29
+ process.stdout.write(JSON.stringify(message) + '\n');
30
+ }
31
+
32
+ function result(id, value) {
33
+ send({ jsonrpc: '2.0', id, result: value });
34
+ }
35
+
36
+ function rpcError(id, code, message, data) {
37
+ send({ jsonrpc: '2.0', id, error: { code, message, ...(data ? { data } : {}) } });
38
+ }
39
+
40
+ function toolContent(value, isError = false) {
41
+ return {
42
+ content: [{ type: 'text', text: JSON.stringify(value, null, 2) }],
43
+ ...(isError ? { isError: true } : {}),
44
+ };
45
+ }
46
+
47
+ const rl = readline.createInterface({ input: process.stdin, crlfDelay: Infinity });
48
+ rl.on('line', async (line) => {
49
+ const text = line.trim();
50
+ if (!text) return;
51
+ let msg;
52
+ try { msg = JSON.parse(text); } catch { return; }
53
+ const { id, method, params } = msg;
54
+
55
+ try {
56
+ if (method === 'initialize') {
57
+ result(id, {
58
+ protocolVersion: PROTOCOL_VERSION,
59
+ capabilities: { tools: {} },
60
+ serverInfo: SERVER_INFO,
61
+ });
62
+ return;
63
+ }
64
+ if (method === 'notifications/initialized') return;
65
+ if (method === 'ping') { result(id, {}); return; }
66
+ if (method === 'tools/list') {
67
+ result(id, { tools: TOOLS });
68
+ return;
69
+ }
70
+ if (method === 'tools/call') {
71
+ const name = params && params.name;
72
+ const args = params && params.arguments;
73
+ if (!name) {
74
+ result(id, toolContent({ ok: false, error: { code: 'MISSING_TOOL', message: '缺少 tool name' } }, true));
75
+ return;
76
+ }
77
+ const value = await callTool(name, typeof args === 'object' && args !== null ? args : {});
78
+ result(id, toolContent(value, value && value.ok === false));
79
+ return;
80
+ }
81
+ if (method === 'shutdown') {
82
+ await closeAll();
83
+ result(id, null);
84
+ return;
85
+ }
86
+ if (method === 'exit') {
87
+ await closeAll();
88
+ process.exit(0);
89
+ }
90
+ rpcError(id, -32601, '未知方法:' + method);
91
+ } catch (err) {
92
+ if (id !== undefined) {
93
+ result(id, toolContent({ ok: false, error: { code: 'MCP_TOOL_FAILED', message: flattenError(err) } }, true));
94
+ }
95
+ }
96
+ });
97
+
98
+ process.on('SIGTERM', async () => { await closeAll(); process.exit(0); });
99
+ process.on('SIGINT', async () => { await closeAll(); process.exit(0); });