@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.
- package/client.js +369 -0
- package/cordis.patch.yml +4 -13
- package/docs/douyin-plugin.md +118 -0
- package/docs/material-library/README.md +404 -0
- package/docs/material-library/import.js +437 -0
- package/docs/material-library/prompt.md +370 -0
- package/docs/material-library/queries.surql +94 -0
- package/docs/material-library/sample.jsonl +14 -0
- package/docs/material-library/schema.surql +46 -0
- package/docs/mcp-profile.example.yml +24 -0
- package/docs/story-graph-design.md +602 -0
- package/docs/story-graph-validation.md +81 -0
- package/docs/workspace-mcp.md +284 -0
- package/index.js +26 -38
- package/lib/atomic-write.js +109 -0
- package/lib/chapter-order.js +100 -0
- package/lib/chinese-num.js +111 -0
- package/lib/confirmation.js +80 -0
- package/lib/db-init.js +123 -0
- package/lib/deployed-state.js +141 -0
- package/lib/direction-labels.js +265 -0
- package/lib/error-codes.js +104 -0
- package/lib/infra.js +594 -0
- package/lib/llm-wiki.js +109 -0
- package/lib/material-db.js +149 -0
- package/lib/path-guard.js +150 -0
- package/lib/projects.js +400 -0
- package/lib/scraper-utils.js +305 -0
- package/lib/surreal-server.js +338 -0
- package/lib/topic-decision.js +860 -0
- package/lib/topic-fields.js +251 -0
- package/lib/topic-schema.js +225 -0
- package/lib/workspace.js +501 -0
- package/package.json +62 -5
- package/plugins/fancy-analyze/analyze-definitions.md +519 -0
- package/plugins/fancy-analyze/command-input.js +65 -0
- package/plugins/fancy-analyze/index.js +2024 -0
- package/plugins/fancy-analyze/ingest-coverage.js +107 -0
- package/plugins/fancy-analyze/schema-analyze.md +627 -0
- package/plugins/fancy-analyze/schema-write.md +178 -0
- package/plugins/fancy-analyze/scripts/append-summary-chapter.js +612 -0
- package/plugins/fancy-analyze/scripts/split_source.js +292 -0
- package/plugins/fancy-analyze/summary-progress.js +495 -0
- package/plugins/fancy-analyze/templates.js +1179 -0
- package/plugins/fancy-bootstrap/index.js +313 -0
- package/plugins/fancy-bootstrap/scripts/init.js +177 -0
- package/plugins/fancy-bootstrap/scripts/path-utils.js +21 -0
- package/plugins/fancy-bootstrap/templates.js +71 -0
- package/plugins/fancy-db/index.js +172 -0
- package/plugins/fancy-douyin/deploy.js +134 -0
- package/plugins/fancy-douyin/index.js +551 -0
- package/plugins/fancy-douyin/prompts/01-analysis-prompt.md +84 -0
- package/plugins/fancy-douyin/scripts/crawl-note.cjs +194 -0
- package/plugins/fancy-douyin/scripts/lib.cjs +197 -0
- package/plugins/fancy-douyin/scripts/scan-account.cjs +128 -0
- package/plugins/fancy-douyin/scripts/start-chrome.cjs +203 -0
- package/plugins/fancy-douyin/state.js +197 -0
- package/plugins/fancy-douyin/templates.js +127 -0
- package/plugins/fancy-export/exporter.js +805 -0
- package/plugins/fancy-export/index.js +63 -0
- package/plugins/fancy-import/import-core.js +1118 -0
- package/plugins/fancy-import/import-driver.js +604 -0
- package/plugins/fancy-import/index.js +738 -0
- package/plugins/fancy-import/prompts/01-import-prompt.md +131 -0
- package/plugins/fancy-import/templates.js +58 -0
- package/plugins/fancy-material/exporter.js +153 -0
- package/plugins/fancy-material/genres.js +256 -0
- package/plugins/fancy-material/index.js +835 -0
- package/plugins/fancy-material/inventory.js +143 -0
- package/plugins/fancy-material/kinds.js +313 -0
- package/plugins/fancy-material/queue.js +55 -0
- package/plugins/fancy-material/scripts/query.js +254 -0
- package/plugins/fancy-material/templates.js +258 -0
- package/plugins/fancy-material/tool-deploy.js +138 -0
- package/plugins/fancy-material/validate.js +149 -0
- package/plugins/fancy-project/index.js +133 -0
- package/plugins/fancy-scan/existing-block.js +100 -0
- package/plugins/fancy-scan/index.js +708 -0
- package/plugins/fancy-scan/prompts/01-analysis-prompt.md +302 -0
- package/plugins/fancy-scan/scripts/append-block.js +725 -0
- package/plugins/fancy-scan/scripts/report-validate.js +824 -0
- package/plugins/fancy-scan/scripts/run-scan.js +667 -0
- package/plugins/fancy-scan/scripts/scrapers/fanqie-rank-scraper.js +923 -0
- package/plugins/fancy-scan/scripts/scrapers/jjwxc-rank-scraper.js +469 -0
- package/plugins/fancy-scan/scripts/scrapers/qimao-rank-scraper.js +629 -0
- package/plugins/fancy-scan/templates/blocks.js +155 -0
- package/plugins/fancy-scan/templates/platforms.js +372 -0
- package/plugins/fancy-scan/templates/report-schema.js +90 -0
- package/plugins/fancy-scan/templates.js +694 -0
- package/plugins/fancy-scan/tool-deploy.js +335 -0
- package/plugins/fancy-scan/wait-turn.js +46 -0
- package/plugins/fancy-story-builder/db/README.md +106 -0
- package/plugins/fancy-story-builder/db/client.js +228 -0
- package/plugins/fancy-story-builder/db/commands.js +145 -0
- package/plugins/fancy-story-builder/db/ids.js +39 -0
- package/plugins/fancy-story-builder/db/import.js +798 -0
- package/plugins/fancy-story-builder/db/project.js +299 -0
- package/plugins/fancy-story-builder/db/query.js +361 -0
- package/plugins/fancy-story-builder/db/schema.js +417 -0
- package/plugins/fancy-story-builder/db/stage-sync.js +238 -0
- package/plugins/fancy-story-builder/deploy-manifest.js +239 -0
- package/plugins/fancy-story-builder/deslop-driver.js +821 -0
- package/plugins/fancy-story-builder/flesh-output.js +168 -0
- package/plugins/fancy-story-builder/index.js +6861 -0
- package/plugins/fancy-story-builder/patch-driver.js +390 -0
- package/plugins/fancy-story-builder/prompts/01-skeleton-prompt.md +563 -0
- package/plugins/fancy-story-builder/prompts/02-flesh-prompt.md +602 -0
- package/plugins/fancy-story-builder/prompts/03-rhythm-prompt.md +539 -0
- package/plugins/fancy-story-builder/prompts/04-writing-prompt.md +225 -0
- package/plugins/fancy-story-builder/prompts/05-deslop-prompt.md +96 -0
- package/plugins/fancy-story-builder/purpose-template.md +83 -0
- package/plugins/fancy-story-builder/references/formulas-rhythm.json +302 -0
- package/plugins/fancy-story-builder/references/formulas-skeleton.json +402 -0
- package/plugins/fancy-story-builder/references/genre-profiles.json +3514 -0
- package/plugins/fancy-story-builder/references/glossary.md +594 -0
- package/plugins/fancy-story-builder/references/index.md +581 -0
- package/plugins/fancy-story-builder/references//345/206/231/344/275/234/346/214/207/345/257/274/00-index.md +174 -0
- package/plugins/fancy-story-builder/references//345/206/231/344/275/234/346/214/207/345/257/274/01-scene.md +150 -0
- package/plugins/fancy-story-builder/references//345/206/231/344/275/234/346/214/207/345/257/274/02-chapter-workflow.md +99 -0
- package/plugins/fancy-story-builder/references//345/206/231/344/275/234/346/214/207/345/257/274/03-opening-chapter.md +104 -0
- package/plugins/fancy-story-builder/references//345/206/231/344/275/234/346/214/207/345/257/274/04-dialogue-mastery.md +274 -0
- package/plugins/fancy-story-builder/references//345/206/231/344/275/234/346/214/207/345/257/274/05-genre-formulas.md +432 -0
- package/plugins/fancy-story-builder/references//345/206/231/344/275/234/346/214/207/345/257/274/06-tone-ensemble.md +259 -0
- package/plugins/fancy-story-builder/references//345/206/231/344/275/234/346/214/207/345/257/274/07-ledger-audit.md +222 -0
- package/plugins/fancy-story-builder/references//345/206/231/344/275/234/346/214/207/345/257/274/08-diagnosis-revision.md +205 -0
- package/plugins/fancy-story-builder/references//345/206/231/344/275/234/346/214/207/345/257/274/09-reading-power.md +135 -0
- package/plugins/fancy-story-builder/references//345/206/231/344/275/234/346/214/207/345/257/274/10-deslop.md +129 -0
- package/plugins/fancy-story-builder/references//350/212/202/345/245/217/346/214/207/345/257/274/00-index.md +98 -0
- package/plugins/fancy-story-builder/references//350/212/202/345/245/217/346/214/207/345/257/274/01-upgrade-rhythm.md +300 -0
- package/plugins/fancy-story-builder/references//350/212/202/345/245/217/346/214/207/345/257/274/02-arc-rhythm.md +168 -0
- package/plugins/fancy-story-builder/references//350/212/202/345/245/217/346/214/207/345/257/274/03-highpoint-hooks.md +189 -0
- package/plugins/fancy-story-builder/references//350/212/202/345/245/217/346/214/207/345/257/274/04-satisfaction.md +226 -0
- package/plugins/fancy-story-builder/references//350/212/202/345/245/217/346/214/207/345/257/274/05-foreshadow-density.md +195 -0
- package/plugins/fancy-story-builder/references//350/212/202/345/245/217/346/214/207/345/257/274/06-chapter-outline.md +226 -0
- package/plugins/fancy-story-builder/references//350/212/202/345/245/217/346/214/207/345/257/274/07-audit.md +181 -0
- package/plugins/fancy-story-builder/references//350/241/200/350/202/211/346/214/207/345/257/274/00-index.md +100 -0
- package/plugins/fancy-story-builder/references//350/241/200/350/202/211/346/214/207/345/257/274/01-character.md +243 -0
- package/plugins/fancy-story-builder/references//350/241/200/350/202/211/346/214/207/345/257/274/02-worldbuilding.md +245 -0
- package/plugins/fancy-story-builder/references//350/241/200/350/202/211/346/214/207/345/257/274/03-relations.md +185 -0
- package/plugins/fancy-story-builder/references//350/241/200/350/202/211/346/214/207/345/257/274/04-emotion-methods.md +286 -0
- package/plugins/fancy-story-builder/references//350/241/200/350/202/211/346/214/207/345/257/274/05-audit.md +187 -0
- package/plugins/fancy-story-builder/references//351/252/250/346/236/266/346/214/207/345/257/274/00-index.md +91 -0
- package/plugins/fancy-story-builder/references//351/252/250/346/236/266/346/214/207/345/257/274/01-positioning.md +209 -0
- package/plugins/fancy-story-builder/references//351/252/250/346/236/266/346/214/207/345/257/274/02-packaging.md +135 -0
- package/plugins/fancy-story-builder/references//351/252/250/346/236/266/346/214/207/345/257/274/03-entities.md +305 -0
- package/plugins/fancy-story-builder/references//351/252/250/346/236/266/346/214/207/345/257/274/04-conflict.md +328 -0
- package/plugins/fancy-story-builder/references//351/252/250/346/236/266/346/214/207/345/257/274/05-quota.md +187 -0
- package/plugins/fancy-story-builder/references//351/252/250/346/236/266/346/214/207/345/257/274/06-audit.md +160 -0
- package/plugins/fancy-story-builder/rhythm-output.js +140 -0
- package/plugins/fancy-story-builder/schema-setting.json +1100 -0
- package/plugins/fancy-story-builder/schema-setting.md +232 -0
- package/plugins/fancy-story-builder/scripts/ai-patterns.js +619 -0
- package/plugins/fancy-story-builder/scripts/brief-validate.js +164 -0
- package/plugins/fancy-story-builder/scripts/check-guide-compliance.js +289 -0
- package/plugins/fancy-story-builder/scripts/content-quality.js +416 -0
- package/plugins/fancy-story-builder/scripts/deslop-validate.js +143 -0
- package/plugins/fancy-story-builder/scripts/error-classes.js +112 -0
- package/plugins/fancy-story-builder/scripts/flesh-validate.js +1312 -0
- package/plugins/fancy-story-builder/scripts/formula-engine.js +112 -0
- package/plugins/fancy-story-builder/scripts/graph-utils.js +1484 -0
- package/plugins/fancy-story-builder/scripts/label-coverage.js +171 -0
- package/plugins/fancy-story-builder/scripts/prompt-drift-check.js +441 -0
- package/plugins/fancy-story-builder/scripts/reading-power.js +298 -0
- package/plugins/fancy-story-builder/scripts/reference-log.js +110 -0
- package/plugins/fancy-story-builder/scripts/rhythm-validate.js +1312 -0
- package/plugins/fancy-story-builder/scripts/schema-pages.js +2668 -0
- package/plugins/fancy-story-builder/scripts/semantic-lint.js +381 -0
- package/plugins/fancy-story-builder/scripts/skeleton-quota.js +145 -0
- package/plugins/fancy-story-builder/scripts/skeleton-validate.js +1973 -0
- package/plugins/fancy-story-builder/scripts/stage-batches.js +1405 -0
- package/plugins/fancy-story-builder/scripts/stage-contract.js +429 -0
- package/plugins/fancy-story-builder/scripts/story-artifacts.js +933 -0
- package/plugins/fancy-story-builder/scripts/upstream-query.js +198 -0
- package/plugins/fancy-story-builder/scripts/v2/baseline.js +112 -0
- package/plugins/fancy-story-builder/scripts/v2/cast.js +517 -0
- package/plugins/fancy-story-builder/scripts/v2/context.js +434 -0
- package/plugins/fancy-story-builder/scripts/v2/contracts.js +493 -0
- package/plugins/fancy-story-builder/scripts/v2/core.js +419 -0
- package/plugins/fancy-story-builder/scripts/v2/engine.js +1068 -0
- package/plugins/fancy-story-builder/scripts/v2/export-markdown.js +794 -0
- package/plugins/fancy-story-builder/scripts/v2/genre.js +195 -0
- package/plugins/fancy-story-builder/scripts/v2/knowledge.js +321 -0
- package/plugins/fancy-story-builder/scripts/v2/macro.js +409 -0
- package/plugins/fancy-story-builder/scripts/v2/manifest.js +538 -0
- package/plugins/fancy-story-builder/scripts/v2/migrate.js +137 -0
- package/plugins/fancy-story-builder/scripts/v2/prompts.js +1335 -0
- package/plugins/fancy-story-builder/scripts/v2/rhythm.js +618 -0
- package/plugins/fancy-story-builder/scripts/v2/scale.js +843 -0
- package/plugins/fancy-story-builder/scripts/v2/snapshot.js +121 -0
- package/plugins/fancy-story-builder/scripts/v2/stages.js +273 -0
- package/plugins/fancy-story-builder/scripts/v2/writing.js +216 -0
- package/plugins/fancy-story-builder/scripts/writing-rules.js +167 -0
- package/plugins/fancy-story-builder/scripts/writing-validate.js +682 -0
- package/plugins/fancy-story-builder/skeleton-output.js +199 -0
- package/plugins/fancy-story-builder/stage-driver.js +1049 -0
- package/plugins/fancy-story-builder/storydb/README.md +271 -0
- package/plugins/fancy-story-builder/storydb/cli.js +458 -0
- package/plugins/fancy-story-builder/storydb/client.js +351 -0
- package/plugins/fancy-story-builder/storydb/compile.js +452 -0
- package/plugins/fancy-story-builder/storydb/context.js +288 -0
- package/plugins/fancy-story-builder/storydb/definitions.js +699 -0
- package/plugins/fancy-story-builder/storydb/driver.js +106 -0
- package/plugins/fancy-story-builder/storydb/dsh-tools.js +313 -0
- package/plugins/fancy-story-builder/storydb/export.js +175 -0
- package/plugins/fancy-story-builder/storydb/ids.js +31 -0
- package/plugins/fancy-story-builder/storydb/impact.js +170 -0
- package/plugins/fancy-story-builder/storydb/import.js +559 -0
- package/plugins/fancy-story-builder/storydb/mcp.js +99 -0
- package/plugins/fancy-story-builder/storydb/prompts.js +952 -0
- package/plugins/fancy-story-builder/storydb/stage-command.js +379 -0
- package/plugins/fancy-story-builder/storydb/stage.js +426 -0
- package/plugins/fancy-story-builder/storydb/store.js +891 -0
- package/plugins/fancy-story-builder/storydb/tools.js +592 -0
- package/plugins/fancy-story-builder/storydb/validation.js +183 -0
- package/plugins/fancy-story-builder/templates/v2/10-scale/scale-template.json +144 -0
- package/plugins/fancy-story-builder/templates/v2/20-macro/emotion-template.json +15 -0
- package/plugins/fancy-story-builder/templates/v2/20-macro/endings-template.json +16 -0
- package/plugins/fancy-story-builder/templates/v2/20-macro/lines-template.json +28 -0
- package/plugins/fancy-story-builder/templates/v2/20-macro/roles-template.json +10 -0
- package/plugins/fancy-story-builder/templates/v2/20-macro/story-template.json +19 -0
- package/plugins/fancy-story-builder/templates/v2/20-macro/volumes-template.json +24 -0
- package/plugins/fancy-story-builder/templates/v2/20-macro/world-template.json +24 -0
- package/plugins/fancy-story-builder/templates/v2/30-cast/characters-template.json +49 -0
- package/plugins/fancy-story-builder/templates/v2/30-cast/climaxes-template.json +14 -0
- package/plugins/fancy-story-builder/templates/v2/30-cast/conflicts-template.json +30 -0
- package/plugins/fancy-story-builder/templates/v2/30-cast/factions-template.json +18 -0
- package/plugins/fancy-story-builder/templates/v2/30-cast/foreshadows-template.json +29 -0
- package/plugins/fancy-story-builder/templates/v2/30-cast/information-template.json +29 -0
- package/plugins/fancy-story-builder/templates/v2/30-cast/items-template.json +13 -0
- package/plugins/fancy-story-builder/templates/v2/30-cast/locations-template.json +12 -0
- package/plugins/fancy-story-builder/templates/v2/30-cast/payoffs-template.json +16 -0
- package/plugins/fancy-story-builder/templates/v2/30-cast/relations-template.json +44 -0
- package/plugins/fancy-story-builder/templates/v2/40-rhythm/chapter-map-template.json +81 -0
- package/plugins/fancy-story-builder/templates/v2/40-rhythm/outline-template.json +162 -0
- package/plugins/fancy-story-builder/templates/v2/50-writing/ledger-template.json +46 -0
- package/plugins/fancy-story-builder/templates/v2/60-deslop/report-template.json +10 -0
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- package/plugins/fancy-story-builder/templates.js +1446 -0
- package/plugins/fancy-story-builder/wiki-references.js +540 -0
- package/plugins/fancy-story-builder/writing-driver.js +1388 -0
- package/plugins/fancy-topic/index.js +1057 -0
- package/plugins/fancy-topic/prompts/01-exploration-prompt.md +115 -0
- package/plugins/fancy-topic/scripts/save-suggestion.js +219 -0
- package/plugins/fancy-topic/scripts/select-topic.js +317 -0
- package/plugins/fancy-topic/templates.js +476 -0
- package/plugins/fancy-topic/tool-deploy.js +200 -0
- package/commands/bootstrap/index.js +0 -164
- package/commands/bootstrap/init-project.js +0 -121
- package/commands/scan/index.js +0 -283
- package/commands/scan/platforms/fanqie.js +0 -146
- package/commands/scan/platforms/jinjiang.js +0 -143
- package/commands/scan/platforms/qidian.js +0 -102
- package/commands/scan/platforms/qimao.js +0 -127
- package/commands/scan/scraper.js +0 -195
- package/infra.js +0 -160
|
@@ -0,0 +1,604 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* import-driver.js —— 导入阶段:插件驱动、逐轮反推
|
|
3
|
+
*
|
|
4
|
+
* 和写作阶段同一套骨架:**插件当司机,LLM 只按指令写文件;脚本当裁判。**
|
|
5
|
+
*
|
|
6
|
+
* 导入和正向四阶段的差别在因果方向:
|
|
7
|
+
* 正向:先定 quota → 照配额造实体 → 排章节 → 写正文
|
|
8
|
+
* 反向:旧稿已经在那里 → 反推出实体 → **反推出 quota** → 报告还缺什么
|
|
9
|
+
*
|
|
10
|
+
* 所以配额这一环必须由脚本算,不能让模型填:
|
|
11
|
+
* 模型的"估计"和校验器的口径对不上,导入完就会校验失败。
|
|
12
|
+
*
|
|
13
|
+
* 本模块不直接依赖 dsh 运行时:sendPrompt / waitIdle / validateFile 都由
|
|
14
|
+
* 调用方注入,因此可以在没有 LLM 的情况下用测试夹具驱动。
|
|
15
|
+
*/
|
|
16
|
+
import fs from 'node:fs';
|
|
17
|
+
import path from 'node:path';
|
|
18
|
+
|
|
19
|
+
import { makeWaitForTurn } from '#story-builder/writing-driver.js';
|
|
20
|
+
import { countChapterChars } from '#story-builder/scripts/content-quality.js';
|
|
21
|
+
import { chapterFileName } from './import-core.js';
|
|
22
|
+
|
|
23
|
+
/** 单轮最大尝试次数(含首次) */
|
|
24
|
+
export const IMPORT_MAX_ATTEMPTS = 3;
|
|
25
|
+
|
|
26
|
+
/** 注入正文的材料上限(每章取前 N 字,避免上下文爆掉) */
|
|
27
|
+
export const IMPORT_EXCERPT_CHARS = 800;
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* 导入要反推的六轮产物。
|
|
31
|
+
*
|
|
32
|
+
* 2026-10-27 修正:注释和报告文案一直写"四轮",实际是六轮
|
|
33
|
+
* (input / entities / relations / flesh / rhythm / ledger)——
|
|
34
|
+
* 用户照着报告数轮次对不上,以为自己漏了流程。
|
|
35
|
+
*
|
|
36
|
+
* 文件名与模板名**必须和 story-builder 的校验器一致** ——
|
|
37
|
+
* 校验器是按固定文件名找产物的,名字对不上就是"文件不存在"。
|
|
38
|
+
*/
|
|
39
|
+
export const IMPORT_ROUNDS = [
|
|
40
|
+
{
|
|
41
|
+
// 输入快照必须**先**出来:书名 / 题材 / 核心冲突 / 硬约束
|
|
42
|
+
// 脚本算不出来(那是书的"自我认知",只能从旧稿里读),
|
|
43
|
+
// 而框架.json 的 input_snapshot 是必填项,空值会被 REQUIRED 拦下。
|
|
44
|
+
id: 'input',
|
|
45
|
+
stage: '输入快照',
|
|
46
|
+
dir: '导入资料',
|
|
47
|
+
files: [
|
|
48
|
+
{ name: '输入快照.json', tpl: null, type: '输入快照' }
|
|
49
|
+
],
|
|
50
|
+
validator: null,
|
|
51
|
+
templateDir: null
|
|
52
|
+
},
|
|
53
|
+
{
|
|
54
|
+
// 实体先写:配额是**从实体条数反推**出来的,
|
|
55
|
+
// 所以必须先有实体,脚本才能算出配额。
|
|
56
|
+
id: 'entities',
|
|
57
|
+
stage: '骨架·实体',
|
|
58
|
+
dir: '骨架',
|
|
59
|
+
files: [
|
|
60
|
+
{ name: '实体.json', tpl: '实体模板.json', type: '实体' }
|
|
61
|
+
],
|
|
62
|
+
validator: 'skeleton-validate.js',
|
|
63
|
+
templateDir: '骨架'
|
|
64
|
+
},
|
|
65
|
+
{
|
|
66
|
+
// 关系与层级要照着配额写(关系条数由配额决定),所以排在实体之后。
|
|
67
|
+
id: 'relations',
|
|
68
|
+
stage: '骨架·关系层级',
|
|
69
|
+
dir: '骨架',
|
|
70
|
+
files: [
|
|
71
|
+
{ name: '关系.json', tpl: '关系模板.json', type: '关系' },
|
|
72
|
+
{ name: '层级.json', tpl: '层级模板.json', type: '层级' }
|
|
73
|
+
],
|
|
74
|
+
validator: 'skeleton-validate.js',
|
|
75
|
+
templateDir: '骨架'
|
|
76
|
+
},
|
|
77
|
+
{
|
|
78
|
+
id: 'flesh',
|
|
79
|
+
stage: '血肉',
|
|
80
|
+
dir: '血肉',
|
|
81
|
+
files: [
|
|
82
|
+
{ name: '角色.json', tpl: '角色模板.json', type: '角色' },
|
|
83
|
+
{ name: '冲突.json', tpl: '冲突模板.json', type: '冲突' },
|
|
84
|
+
{ name: '势力.json', tpl: '势力模板.json', type: '势力' },
|
|
85
|
+
{ name: '剧情.json', tpl: '剧情模板.json', type: '剧情' },
|
|
86
|
+
{ name: '地点.json', tpl: '地点模板.json', type: '地点' },
|
|
87
|
+
{ name: '结局.json', tpl: '结局模板.json', type: '结局' },
|
|
88
|
+
{ name: '世界观规则.json', tpl: '世界观规则模板.json', type: '世界观规则' },
|
|
89
|
+
{ name: '关键道具.json', tpl: '关键道具模板.json', type: '关键道具' }
|
|
90
|
+
],
|
|
91
|
+
validator: 'flesh-validate.js',
|
|
92
|
+
templateDir: '血肉'
|
|
93
|
+
},
|
|
94
|
+
{
|
|
95
|
+
id: 'rhythm',
|
|
96
|
+
stage: '节奏',
|
|
97
|
+
dir: '节奏',
|
|
98
|
+
files: [
|
|
99
|
+
{ name: '章节规划.json', tpl: '章节规划模板.json', type: '章节规划' },
|
|
100
|
+
{ name: '时序映射.json', tpl: '时序映射模板.json', type: '时序映射' },
|
|
101
|
+
{ name: '节奏参数.json', tpl: '节奏参数模板.json', type: '节奏参数' },
|
|
102
|
+
{ name: '章节细纲.json', tpl: '章节细纲模板.json', type: '章节细纲' }
|
|
103
|
+
],
|
|
104
|
+
validator: 'rhythm-validate.js',
|
|
105
|
+
templateDir: '节奏'
|
|
106
|
+
},
|
|
107
|
+
{
|
|
108
|
+
id: 'ledger',
|
|
109
|
+
stage: '台账',
|
|
110
|
+
dir: '写作/台账',
|
|
111
|
+
files: [
|
|
112
|
+
{ name: '章台账.json', tpl: null, type: '章台账' },
|
|
113
|
+
{ name: '人物台账.json', tpl: null, type: '人物台账' },
|
|
114
|
+
{ name: '伏笔台账.json', tpl: null, type: '伏笔台账' },
|
|
115
|
+
{ name: '时间线台账.json', tpl: null, type: '时间线台账' }
|
|
116
|
+
],
|
|
117
|
+
validator: null,
|
|
118
|
+
templateDir: null
|
|
119
|
+
}
|
|
120
|
+
];
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* 导入阶段的目录布局。
|
|
124
|
+
* @param {string} projectRoot
|
|
125
|
+
*/
|
|
126
|
+
export function importDirs(projectRoot) {
|
|
127
|
+
const src = path.join(projectRoot, '设定', 'raw', 'sources');
|
|
128
|
+
return {
|
|
129
|
+
sources: src,
|
|
130
|
+
writing: path.join(src, '写作'),
|
|
131
|
+
ledger: path.join(src, '写作', '台账'),
|
|
132
|
+
skeleton: path.join(src, '骨架'),
|
|
133
|
+
flesh: path.join(src, '血肉'),
|
|
134
|
+
rhythm: path.join(src, '节奏'),
|
|
135
|
+
material: path.join(src, '导入资料'),
|
|
136
|
+
reports: path.join(projectRoot, '导入报告')
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* 挑出要注入提示词的正文摘要。
|
|
142
|
+
*
|
|
143
|
+
* 不把全文塞进提示词:T=800 的旧稿全文进上下文会直接爆掉。
|
|
144
|
+
* 每章取开头一段,够模型认出这是哪一章、什么语气、谁在说话。
|
|
145
|
+
*
|
|
146
|
+
* @param {Array<{ch:number,title:string,body:string}>} chapters
|
|
147
|
+
* @param {number} [maxChapters]
|
|
148
|
+
* @param {number} [charsPerChapter]
|
|
149
|
+
* @returns {Array<{ch:number,title:string,excerpt:string}>}
|
|
150
|
+
*/
|
|
151
|
+
export function pickExcerpts(chapters, maxChapters = 12, charsPerChapter = IMPORT_EXCERPT_CHARS) {
|
|
152
|
+
const out = [];
|
|
153
|
+
const list = chapters || [];
|
|
154
|
+
// 均匀取样:开头几章 + 结尾几章 + 中间均匀分布
|
|
155
|
+
let picks = [];
|
|
156
|
+
if (list.length <= maxChapters) {
|
|
157
|
+
picks = list.map((c) => c.ch);
|
|
158
|
+
} else {
|
|
159
|
+
const step = (list.length - 1) / (maxChapters - 1);
|
|
160
|
+
for (let i = 0; i < maxChapters; i++) {
|
|
161
|
+
picks.push(list[Math.round(i * step)].ch);
|
|
162
|
+
}
|
|
163
|
+
picks = Array.from(new Set(picks));
|
|
164
|
+
}
|
|
165
|
+
const byCh = new Map(list.map((c) => [c.ch, c]));
|
|
166
|
+
for (const ch of picks) {
|
|
167
|
+
const c = byCh.get(ch);
|
|
168
|
+
if (!c) continue;
|
|
169
|
+
out.push({
|
|
170
|
+
ch: c.ch,
|
|
171
|
+
title: c.title,
|
|
172
|
+
excerpt: String(c.body || '').slice(0, charsPerChapter)
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
return out;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* 把配额对象摊成「字段 → 数量」的列表(给提示词渲染表格用)。
|
|
180
|
+
*
|
|
181
|
+
* @param {Object} quota
|
|
182
|
+
* @returns {Array<[string, number]>}
|
|
183
|
+
*/
|
|
184
|
+
export function flattenQuota(quota) {
|
|
185
|
+
const out = [];
|
|
186
|
+
const walk = (node, prefix) => {
|
|
187
|
+
if (!node || typeof node !== 'object') return;
|
|
188
|
+
for (const k of Object.keys(node)) {
|
|
189
|
+
const v = node[k];
|
|
190
|
+
const path = prefix ? prefix + '.' + k : k;
|
|
191
|
+
if (typeof v === 'number') out.push([path, v]);
|
|
192
|
+
else if (v && typeof v === 'object') walk(v, path);
|
|
193
|
+
}
|
|
194
|
+
};
|
|
195
|
+
walk(quota, '');
|
|
196
|
+
return out;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* 构建一轮的派活提示词。
|
|
201
|
+
*
|
|
202
|
+
* @param {Object} o
|
|
203
|
+
* @param {Object} o.round IMPORT_ROUNDS 的一项
|
|
204
|
+
* @param {number} o.attempt 第几次尝试
|
|
205
|
+
* @param {Array} o.errors 上一轮的校验错误
|
|
206
|
+
* @param {Object} o.context { chapterCount, settingCount, excerpts, dirs }
|
|
207
|
+
* @param {string} [o.contract] 导入执行须知
|
|
208
|
+
* @returns {string}
|
|
209
|
+
*/
|
|
210
|
+
export function buildRoundPrompt(o) {
|
|
211
|
+
const round = o.round;
|
|
212
|
+
const ctx = o.context || {};
|
|
213
|
+
// 项目根**绝对路径**:读文件、写文件都要用它。
|
|
214
|
+
//
|
|
215
|
+
// 为什么必须给绝对路径:LLM 的工作目录未必是项目根。
|
|
216
|
+
// 用相对路径(`设定/raw/sources/骨架/实体.json`)时,产物会落到
|
|
217
|
+
// LLM 当前目录下的 `设定/...` 下 —— 插件在项目根下找不到文件,
|
|
218
|
+
// 只会报一句"文件没有生成",模型完全不知道自己写歪了。
|
|
219
|
+
const projectRoot = ctx.projectRoot || '';
|
|
220
|
+
const abs = (rel) => (projectRoot ? projectRoot + '/' + rel : rel);
|
|
221
|
+
const parts = [];
|
|
222
|
+
|
|
223
|
+
if (o.contract) {
|
|
224
|
+
parts.push(o.contract.trim().replace(/\$\{projectRoot\}/g, projectRoot || '(项目根)'));
|
|
225
|
+
parts.push('');
|
|
226
|
+
parts.push('---');
|
|
227
|
+
parts.push('');
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
parts.push('# 本轮派活:反推' + round.stage + '(第 ' + (o.attempt || 1) + ' 次尝试)');
|
|
231
|
+
parts.push('');
|
|
232
|
+
|
|
233
|
+
parts.push('## 旧稿在哪');
|
|
234
|
+
parts.push('');
|
|
235
|
+
parts.push('- 正文:`' + abs('设定/raw/sources/写作/') + '`(第001章.md ~ 第' +
|
|
236
|
+
String(ctx.chapterCount || 0).padStart(3, '0') + '章.md,共 ' + (ctx.chapterCount || 0) + ' 章)');
|
|
237
|
+
if (ctx.settingCount) {
|
|
238
|
+
parts.push('- 设定资料:`' + abs('设定/raw/sources/导入资料/') + '`(' + ctx.settingCount + ' 个文件)');
|
|
239
|
+
} else {
|
|
240
|
+
parts.push('- 设定资料:没有(旧稿只有正文)');
|
|
241
|
+
}
|
|
242
|
+
parts.push('- **先读这些文件再动笔**,不要凭想象填。');
|
|
243
|
+
parts.push('- **路径一律用上面给的绝对路径**:你的工作目录未必是项目根,');
|
|
244
|
+
parts.push(' 用相对路径会读不到旧稿、把产物写到别的地方去。');
|
|
245
|
+
parts.push('');
|
|
246
|
+
|
|
247
|
+
if (ctx.excerpts && ctx.excerpts.length) {
|
|
248
|
+
parts.push('## 正文开头节选(只做定位用,全文在上面的目录里)');
|
|
249
|
+
parts.push('');
|
|
250
|
+
for (const e of ctx.excerpts) {
|
|
251
|
+
parts.push('### 第 ' + e.ch + ' 章 ' + e.title);
|
|
252
|
+
parts.push('');
|
|
253
|
+
parts.push('```');
|
|
254
|
+
parts.push(e.excerpt);
|
|
255
|
+
parts.push('```');
|
|
256
|
+
parts.push('');
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
if (ctx.quota) {
|
|
261
|
+
parts.push('## 配额(框架.json 由插件生成,照这个写)');
|
|
262
|
+
parts.push('');
|
|
263
|
+
parts.push('| 字段 | 数量 |');
|
|
264
|
+
parts.push('|---|---|');
|
|
265
|
+
for (const line of flattenQuota(ctx.quota)) {
|
|
266
|
+
parts.push('| ' + line[0] + ' | ' + line[1] + ' |');
|
|
267
|
+
}
|
|
268
|
+
parts.push('');
|
|
269
|
+
parts.push('**实体条数必须与配额完全一致**(多一个少一个都会报 `QUOTA_MISMATCH`);' +
|
|
270
|
+
'关系条数按配额里对应的行数写。');
|
|
271
|
+
parts.push('');
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
parts.push('## 本轮要写的文件');
|
|
275
|
+
parts.push('');
|
|
276
|
+
for (const f of round.files) {
|
|
277
|
+
parts.push('- `' + abs('设定/raw/sources/' + round.dir + '/' + f.name) + '`');
|
|
278
|
+
}
|
|
279
|
+
parts.push('');
|
|
280
|
+
parts.push('**只写上面这几个文件。** 其余阶段的文件本轮不要动。');
|
|
281
|
+
parts.push('**用上面给的绝对路径写**(目录不存在就先建目录),不要用相对路径。');
|
|
282
|
+
parts.push('');
|
|
283
|
+
|
|
284
|
+
if (round.id === 'input') {
|
|
285
|
+
parts.push('## 输入快照的字段');
|
|
286
|
+
parts.push('');
|
|
287
|
+
parts.push('```json');
|
|
288
|
+
parts.push('{');
|
|
289
|
+
parts.push(' "book_name": "旧稿的书名(没有就用第一章标题)",');
|
|
290
|
+
parts.push(' "genre": "题材(如:都市复仇 / 玄幻修真)",');
|
|
291
|
+
parts.push(' "core_summary": "这本书讲什么(一两句,说清主角处境与主线)",');
|
|
292
|
+
parts.push(' "core_conflict": "核心冲突是什么(谁和谁,为了什么,对抗什么)",');
|
|
293
|
+
parts.push(' "hard_constraints": ["旧稿里明确写死的设定(没有就写空数组)"],');
|
|
294
|
+
parts.push(' "anti_tropes": ["旧稿刻意避开的写法(没有就写空数组)"]');
|
|
295
|
+
parts.push('}');
|
|
296
|
+
parts.push('```');
|
|
297
|
+
parts.push('');
|
|
298
|
+
parts.push('**这 6 个字段都要有实际内容**:空值会被骨架校验器以 `REQUIRED` / ' +
|
|
299
|
+
'`TEXT_TOO_SHORT` / `TEXT_PLACEHOLDER` 拦下。');
|
|
300
|
+
parts.push('');
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
if (o.errors && o.errors.length) {
|
|
304
|
+
parts.push('## 上一轮的校验错误(必须改掉)');
|
|
305
|
+
parts.push('');
|
|
306
|
+
for (const e of o.errors) {
|
|
307
|
+
parts.push('- [' + e.code + '] ' + (e.path || '(无路径)') + ':' + e.message +
|
|
308
|
+
(e.hint ? '(' + e.hint + ')' : ''));
|
|
309
|
+
}
|
|
310
|
+
parts.push('');
|
|
311
|
+
parts.push('**只改错误指向的字段**,其余部分不要动。');
|
|
312
|
+
parts.push('');
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
parts.push('## 本轮怎么算完成');
|
|
316
|
+
parts.push('');
|
|
317
|
+
parts.push('- 你只管**把上面那几个文件写出来**;内容对不对由插件的校验器判,不用自己跑校验。');
|
|
318
|
+
parts.push('- 写完后插件会跑校验器。**下一轮派活里没有「上一轮的校验错误」这一节 = 这一轮通过了**。');
|
|
319
|
+
parts.push('- 下一轮派活里**有**「上一轮的校验错误」= 没通过:按那节里的错误码改');
|
|
320
|
+
parts.push(' (错误码的含义见上面契约的「四、会被拦下的东西」),**只改错误点名的字段**,');
|
|
321
|
+
parts.push(' 改完**重新写整个文件**(产物是整份覆盖,不是打补丁)。');
|
|
322
|
+
parts.push('- 派活标题里的「第 N 次尝试」是第几次。同一轮最多试 ' + IMPORT_MAX_ATTEMPTS + ' 次;');
|
|
323
|
+
parts.push(' 第 ' + IMPORT_MAX_ATTEMPTS + ' 次仍未通过,插件会停在这一轮,');
|
|
324
|
+
parts.push(' 并在导入报告里把它列成「未通过」——这时候不要继续自己往下写别的阶段。');
|
|
325
|
+
parts.push('');
|
|
326
|
+
|
|
327
|
+
parts.push('## 交活');
|
|
328
|
+
parts.push('');
|
|
329
|
+
parts.push('写完给一段话汇报:本轮写了哪些文件、各写了多少条、哪些内容旧稿里没有(已留空)。');
|
|
330
|
+
parts.push('');
|
|
331
|
+
|
|
332
|
+
return parts.join('\n');
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
/**
|
|
336
|
+
* 判断一个产物文件是不是"至少存在且是合法 JSON"。
|
|
337
|
+
* 台账那轮没有模板可校验,用这条底线兜住。
|
|
338
|
+
*
|
|
339
|
+
* @param {string} p
|
|
340
|
+
* @returns {{ok:boolean, error:string|null}}
|
|
341
|
+
*/
|
|
342
|
+
export function checkJsonFile(p) {
|
|
343
|
+
if (!fs.existsSync(p)) return { ok: false, error: '文件没有生成:' + p };
|
|
344
|
+
try {
|
|
345
|
+
JSON.parse(fs.readFileSync(p, 'utf-8'));
|
|
346
|
+
return { ok: true, error: null };
|
|
347
|
+
} catch (e) {
|
|
348
|
+
return { ok: false, error: '不是合法 JSON:' + p + '(' + e.message + ')' };
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
/**
|
|
353
|
+
* 逐轮驱动导入。
|
|
354
|
+
*
|
|
355
|
+
* @param {Object} opts
|
|
356
|
+
* @param {string} opts.projectRoot
|
|
357
|
+
* @param {Array} [opts.rounds]
|
|
358
|
+
* @param {string} [opts.contract]
|
|
359
|
+
* @param {(prompt:string) => Promise<{ok:boolean, error?:string}>} opts.sendPrompt
|
|
360
|
+
* @param {(files:string[]) => Promise<{ok:boolean, reason?:string, code?:string}>} opts.waitIdle
|
|
361
|
+
* @param {(file:string, round:Object) => Promise<{ok:boolean, errors?:Array}>} opts.validateFile
|
|
362
|
+
* @param {(msg:string) => void} [opts.onProgress]
|
|
363
|
+
* @param {(round:Object) => void} [opts.onRound] 一轮结束时的回调(用于生成配额)
|
|
364
|
+
* @param {number} [opts.maxAttempts]
|
|
365
|
+
* @returns {Promise<Object>} 汇总报告
|
|
366
|
+
*/
|
|
367
|
+
export async function runImportStage(opts) {
|
|
368
|
+
const dirs = opts.dirs || importDirs(opts.projectRoot);
|
|
369
|
+
const rounds = opts.rounds || IMPORT_ROUNDS;
|
|
370
|
+
const maxAttempts = opts.maxAttempts || IMPORT_MAX_ATTEMPTS;
|
|
371
|
+
|
|
372
|
+
const report = {
|
|
373
|
+
ok: true,
|
|
374
|
+
error: null,
|
|
375
|
+
rounds: [],
|
|
376
|
+
failed: [],
|
|
377
|
+
chapters: 0,
|
|
378
|
+
settings: 0,
|
|
379
|
+
totalWords: 0
|
|
380
|
+
};
|
|
381
|
+
|
|
382
|
+
/** 环境类失败(投递失败/超时/被中断/没有 agent)→ 整条轮次链停掉 */
|
|
383
|
+
let stopAll = false;
|
|
384
|
+
|
|
385
|
+
for (const round of rounds) {
|
|
386
|
+
const targetDir = path.join(dirs.sources, round.dir);
|
|
387
|
+
const expected = round.files.map((f) => path.join(targetDir, f.name));
|
|
388
|
+
|
|
389
|
+
let attempt = 1;
|
|
390
|
+
let errors = [];
|
|
391
|
+
let lastError = null;
|
|
392
|
+
|
|
393
|
+
while (attempt <= maxAttempts) {
|
|
394
|
+
const prompt = buildRoundPrompt({
|
|
395
|
+
round,
|
|
396
|
+
attempt,
|
|
397
|
+
errors,
|
|
398
|
+
contract: opts.contract,
|
|
399
|
+
context: opts.context || {}
|
|
400
|
+
});
|
|
401
|
+
|
|
402
|
+
const sent = await opts.sendPrompt(prompt);
|
|
403
|
+
if (!sent || sent.ok !== true) {
|
|
404
|
+
lastError = (sent && sent.error) || '提示词投递失败';
|
|
405
|
+
report.ok = false;
|
|
406
|
+
report.error = lastError;
|
|
407
|
+
// 投递失败是**环境问题**(会话没了/插件不可用),不是这一轮的内容问题,
|
|
408
|
+
// 继续发下一轮只会继续失败,还会往会话里塞更多发不出去的话。
|
|
409
|
+
stopAll = true;
|
|
410
|
+
break;
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
const waited = await opts.waitIdle(expected);
|
|
414
|
+
if (!waited || waited.ok !== true) {
|
|
415
|
+
lastError = (waited && waited.reason) || '等待 LLM 失败';
|
|
416
|
+
errors = [{ code: 'NO_OUTPUT', path: round.dir, message: lastError, hint: null }];
|
|
417
|
+
// 等待失败**也是失败** —— 旧实现这里既不置 report.ok=false,
|
|
418
|
+
// 也不阻止后面的轮次继续跑,于是"一轮都没等回来"也能报"导入完成"。
|
|
419
|
+
report.ok = false;
|
|
420
|
+
if (!report.error) report.error = lastError;
|
|
421
|
+
// 环境类失败重试没有意义
|
|
422
|
+
const code = (waited && waited.code) || '';
|
|
423
|
+
if (code === 'TIMEOUT' || code === 'ABORTED' || code === 'NO_AGENT') {
|
|
424
|
+
// 而且**不能继续发下一轮**:
|
|
425
|
+
// ABORTED = 用户点了停止,继续塞提示词就是在用户喊停后继续消耗额度;
|
|
426
|
+
// TIMEOUT = 上一轮可能还在跑,下一轮会和它叠在一起;
|
|
427
|
+
// NO_AGENT = 根本没有 agent 在听。
|
|
428
|
+
// 旧实现的 break 只跳出内层 while,外层 for 照跑不误。
|
|
429
|
+
stopAll = true;
|
|
430
|
+
break;
|
|
431
|
+
}
|
|
432
|
+
attempt++;
|
|
433
|
+
continue;
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
// 逐文件校验
|
|
437
|
+
errors = [];
|
|
438
|
+
for (let i = 0; i < round.files.length; i++) {
|
|
439
|
+
const f = round.files[i];
|
|
440
|
+
const p = expected[i];
|
|
441
|
+
if (!opts.validateFile) {
|
|
442
|
+
const chk = checkJsonFile(p);
|
|
443
|
+
if (!chk.ok) errors.push({ code: 'READ', path: p, message: chk.error, hint: null });
|
|
444
|
+
continue;
|
|
445
|
+
}
|
|
446
|
+
const r = await opts.validateFile(p, round, f);
|
|
447
|
+
if (!r || !r.ok) {
|
|
448
|
+
for (const e of (r && r.errors) || [{ code: 'VALIDATE', path: p, message: '校验失败', hint: null }]) {
|
|
449
|
+
errors.push(e);
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
if (errors.length === 0) {
|
|
455
|
+
if (opts.onProgress) opts.onProgress(round.stage + ' 反推完成(' + round.files.length + ' 个文件)。');
|
|
456
|
+
break;
|
|
457
|
+
}
|
|
458
|
+
if (attempt >= maxAttempts) {
|
|
459
|
+
lastError = '校验未通过(' + errors.length + ' 个错误)';
|
|
460
|
+
break;
|
|
461
|
+
}
|
|
462
|
+
attempt++;
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
const r = {
|
|
466
|
+
id: round.id,
|
|
467
|
+
stage: round.stage,
|
|
468
|
+
files: round.files.map((f) => f.name),
|
|
469
|
+
ok: errors.length === 0 && !lastError,
|
|
470
|
+
errors,
|
|
471
|
+
error: lastError,
|
|
472
|
+
attempts: attempt
|
|
473
|
+
};
|
|
474
|
+
report.rounds.push(r);
|
|
475
|
+
if (!r.ok) {
|
|
476
|
+
report.failed.push(round.stage);
|
|
477
|
+
// 某一轮没过,整体就不算成功 —— report.ok 的语义是"这次导入成了吗"
|
|
478
|
+
report.ok = false;
|
|
479
|
+
if (opts.onProgress) opts.onProgress(round.stage + ' 反推未通过:' + (lastError || errors.length + ' 个错误'));
|
|
480
|
+
}
|
|
481
|
+
if (opts.onRound) opts.onRound(r);
|
|
482
|
+
if (stopAll) break;
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
return report;
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
/**
|
|
489
|
+
* 写导入报告(含待补清单)。
|
|
490
|
+
*
|
|
491
|
+
* 待补清单 = 校验没过的地方 + 反推不出来的地方。
|
|
492
|
+
* 导入的价值一半在"把旧稿变成结构化产物",另一半在
|
|
493
|
+
* **明确告诉用户还差什么** —— 不说清楚,用户会以为导入完了。
|
|
494
|
+
*
|
|
495
|
+
* @param {Object} o
|
|
496
|
+
* @param {Object} o.scan scanSource() 的结果
|
|
497
|
+
* @param {Object} o.report runImportStage() 的结果
|
|
498
|
+
* @param {Array} [o.quotaDecisions] reverseQuota() 的决定
|
|
499
|
+
* @param {Array} [o.gaps] 额外列出的待补项
|
|
500
|
+
* @returns {string} markdown
|
|
501
|
+
*/
|
|
502
|
+
export function renderImportReport(o) {
|
|
503
|
+
const scan = o.scan || {};
|
|
504
|
+
const report = o.report || {};
|
|
505
|
+
const lines = [];
|
|
506
|
+
|
|
507
|
+
lines.push('# 导入报告');
|
|
508
|
+
lines.push('');
|
|
509
|
+
|
|
510
|
+
lines.push('## 一、导入了什么');
|
|
511
|
+
lines.push('');
|
|
512
|
+
lines.push('- 来源:`' + (scan.root || '') + '`');
|
|
513
|
+
lines.push('- 正文:' + (scan.stats ? scan.stats.chapterCount : 0) + ' 章,' +
|
|
514
|
+
(scan.stats ? scan.stats.totalWords : 0) + ' 字(平均每章 ' +
|
|
515
|
+
(scan.stats ? scan.stats.avgWords : 0) + ' 字)');
|
|
516
|
+
lines.push('- 设定资料:' + (scan.stats ? scan.stats.settingCount : 0) + ' 个文件');
|
|
517
|
+
lines.push('');
|
|
518
|
+
|
|
519
|
+
if (scan.warnings && scan.warnings.length) {
|
|
520
|
+
lines.push('### 扫描阶段的提醒');
|
|
521
|
+
lines.push('');
|
|
522
|
+
for (const w of scan.warnings) lines.push('- ' + w);
|
|
523
|
+
lines.push('');
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
// 被覆盖的正文:导入会用原始旧稿盖掉已有章节(去味成果会丢),
|
|
527
|
+
// 必须列出来并给出备份位置 —— 不列出来用户不会意识到覆盖发生了。
|
|
528
|
+
if (o.overwritten && o.overwritten.length) {
|
|
529
|
+
lines.push('### 覆盖了已有正文');
|
|
530
|
+
lines.push('');
|
|
531
|
+
lines.push('导入用原始旧稿覆盖了 ' + o.overwritten.length + ' 个已有章节:' +
|
|
532
|
+
o.overwritten.join('、') + '。');
|
|
533
|
+
if (o.backupDir) {
|
|
534
|
+
lines.push('');
|
|
535
|
+
lines.push('覆盖前的原文已备份到:`' + o.backupDir + '`。');
|
|
536
|
+
}
|
|
537
|
+
lines.push('');
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
lines.push('## 二、各轮反推结果');
|
|
541
|
+
lines.push('');
|
|
542
|
+
lines.push('| 轮次 | 产物 | 结果 |');
|
|
543
|
+
lines.push('|---|---|---|');
|
|
544
|
+
for (const r of (report.rounds || [])) {
|
|
545
|
+
lines.push('| ' + r.stage + ' | ' + r.files.join('、') + ' | ' + (r.ok ? '通过' : '未通过') + ' |');
|
|
546
|
+
}
|
|
547
|
+
lines.push('');
|
|
548
|
+
|
|
549
|
+
lines.push('## 三、配额是怎么定出来的');
|
|
550
|
+
lines.push('');
|
|
551
|
+
lines.push('| 字段 | 值 | 依据 | 说明 |');
|
|
552
|
+
lines.push('|---|---|---|---|');
|
|
553
|
+
for (const d of (o.quotaDecisions || [])) {
|
|
554
|
+
const sourceText = { actual: '旧稿实际规模', formula: '按公式补齐', fallback: '兜底' }[d.source] || d.source;
|
|
555
|
+
lines.push('| ' + d.field + ' | ' + d.value + ' | ' + sourceText + ' | ' + d.note + ' |');
|
|
556
|
+
}
|
|
557
|
+
lines.push('');
|
|
558
|
+
|
|
559
|
+
lines.push('## 四、待补清单');
|
|
560
|
+
lines.push('');
|
|
561
|
+
const gaps = [];
|
|
562
|
+
for (const r of (report.rounds || [])) {
|
|
563
|
+
if (r.ok) continue;
|
|
564
|
+
gaps.push('### ' + r.stage + '(' + r.errors.length + ' 个错误)');
|
|
565
|
+
for (const e of r.errors) {
|
|
566
|
+
gaps.push('- [' + e.code + '] ' + (e.path || '(无路径)') + ':' + e.message +
|
|
567
|
+
(e.hint ? '(' + e.hint + ')' : ''));
|
|
568
|
+
}
|
|
569
|
+
gaps.push('');
|
|
570
|
+
}
|
|
571
|
+
for (const g of (o.gaps || [])) {
|
|
572
|
+
gaps.push('- ' + g);
|
|
573
|
+
}
|
|
574
|
+
if (!gaps.length) {
|
|
575
|
+
lines.push('- 无。六轮产物都通过了校验。');
|
|
576
|
+
} else {
|
|
577
|
+
lines.push(...gaps);
|
|
578
|
+
}
|
|
579
|
+
lines.push('');
|
|
580
|
+
|
|
581
|
+
lines.push('## 五、下一步');
|
|
582
|
+
lines.push('');
|
|
583
|
+
lines.push('1. 修掉上面的待补项(可以直接重新跑 `/fancy-import` 覆盖)。');
|
|
584
|
+
lines.push('2. 跑 `/fancy-build write` 让台账与章节对齐(导入只登记旧稿已有的事实)。');
|
|
585
|
+
lines.push('3. 跑 `/fancy-build deslop` 去掉旧稿里的 AI 味。');
|
|
586
|
+
lines.push('');
|
|
587
|
+
|
|
588
|
+
return lines.join('\n');
|
|
589
|
+
}
|
|
590
|
+
|
|
591
|
+
/**
|
|
592
|
+
* 写导入报告到磁盘。
|
|
593
|
+
* @returns {string} 路径(失败返回空串)
|
|
594
|
+
*/
|
|
595
|
+
export function writeImportReport(dirs, content) {
|
|
596
|
+
try {
|
|
597
|
+
fs.mkdirSync(dirs.reports, { recursive: true });
|
|
598
|
+
const p = path.join(dirs.reports, '导入报告.md');
|
|
599
|
+
fs.writeFileSync(p, content, 'utf-8');
|
|
600
|
+
return p;
|
|
601
|
+
} catch {
|
|
602
|
+
return '';
|
|
603
|
+
}
|
|
604
|
+
}
|