@cloud411716/fancy-webnovel 2.2.0 → 3.0.3
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/README.md +65 -0
- package/client.js +201 -15
- package/cordis.patch.yml +5 -1
- 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 +12 -7
- package/lib/atomic-write.js +109 -0
- package/lib/chapter-order.js +100 -0
- package/lib/confirmation.js +80 -0
- package/lib/db-init.js +123 -0
- package/lib/deployed-state.js +11 -11
- package/lib/direction-labels.js +265 -0
- package/lib/error-codes.js +31 -31
- package/lib/infra.js +29 -4
- 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 +140 -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 +31 -7
- package/plugins/fancy-analyze/analyze-definitions.md +519 -0
- package/plugins/fancy-analyze/index.js +1128 -77
- package/plugins/fancy-analyze/ingest-coverage.js +107 -0
- package/plugins/fancy-analyze/schema-analyze.md +355 -107
- package/plugins/fancy-analyze/scripts/append-summary-chapter.js +612 -0
- package/plugins/fancy-analyze/summary-progress.js +71 -7
- package/plugins/fancy-analyze/templates.js +437 -139
- package/plugins/fancy-bootstrap/index.js +276 -131
- package/plugins/fancy-bootstrap/scripts/init.js +59 -25
- package/plugins/fancy-bootstrap/templates.js +35 -18
- 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-driver.js +29 -6
- package/plugins/fancy-import/index.js +13 -5
- package/plugins/fancy-import/prompts/01-import-prompt.md +39 -14
- package/plugins/fancy-import/templates.js +17 -8
- 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 +145 -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 +410 -21
- 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 +169 -22
- package/plugins/fancy-scan/scripts/scrapers/fanqie-rank-scraper.js +145 -16
- package/plugins/fancy-scan/scripts/scrapers/jjwxc-rank-scraper.js +179 -49
- package/plugins/fancy-scan/scripts/scrapers/qimao-rank-scraper.js +38 -20
- 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 +545 -146
- 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/flesh-output.js +19 -6
- package/plugins/fancy-story-builder/index.js +4105 -396
- package/plugins/fancy-story-builder/patch-driver.js +390 -0
- package/plugins/fancy-story-builder/prompts/01-skeleton-prompt.md +29 -19
- package/plugins/fancy-story-builder/prompts/02-flesh-prompt.md +72 -10
- package/plugins/fancy-story-builder/prompts/03-rhythm-prompt.md +40 -5
- package/plugins/fancy-story-builder/prompts/04-writing-prompt.md +37 -0
- package/plugins/fancy-story-builder/purpose-template.md +42 -66
- package/plugins/fancy-story-builder/references/formulas-skeleton.json +229 -56
- package/plugins/fancy-story-builder/references/genre-profiles.json +3514 -0
- package/plugins/fancy-story-builder/references/glossary.md +15 -6
- package/plugins/fancy-story-builder/references/index.md +97 -6
- package/plugins/fancy-story-builder/references//350/212/202/345/245/217/346/214/207/345/257/274/00-index.md +1 -1
- package/plugins/fancy-story-builder/references//350/212/202/345/245/217/346/214/207/345/257/274/07-audit.md +25 -7
- package/plugins/fancy-story-builder/references//350/241/200/350/202/211/346/214/207/345/257/274/01-character.md +5 -1
- package/plugins/fancy-story-builder/references//350/241/200/350/202/211/346/214/207/345/257/274/03-relations.md +6 -2
- package/plugins/fancy-story-builder/references//350/241/200/350/202/211/346/214/207/345/257/274/05-audit.md +15 -5
- package/plugins/fancy-story-builder/references//351/252/250/346/236/266/346/214/207/345/257/274/00-index.md +1 -1
- package/plugins/fancy-story-builder/references//351/252/250/346/236/266/346/214/207/345/257/274/01-positioning.md +2 -1
- package/plugins/fancy-story-builder/references//351/252/250/346/236/266/346/214/207/345/257/274/03-entities.md +1 -1
- package/plugins/fancy-story-builder/references//351/252/250/346/236/266/346/214/207/345/257/274/06-audit.md +19 -1
- package/plugins/fancy-story-builder/schema-setting.json +1100 -0
- package/plugins/fancy-story-builder/schema-setting.md +201 -293
- package/plugins/fancy-story-builder/scripts/brief-validate.js +164 -0
- package/plugins/fancy-story-builder/scripts/content-quality.js +4 -1
- package/plugins/fancy-story-builder/scripts/flesh-validate.js +135 -12
- package/plugins/fancy-story-builder/scripts/graph-utils.js +146 -47
- package/plugins/fancy-story-builder/scripts/reference-log.js +110 -0
- package/plugins/fancy-story-builder/scripts/rhythm-validate.js +100 -4
- package/plugins/fancy-story-builder/scripts/schema-pages.js +2668 -0
- package/plugins/fancy-story-builder/scripts/semantic-lint.js +5 -1
- package/plugins/fancy-story-builder/scripts/skeleton-validate.js +158 -17
- package/plugins/fancy-story-builder/scripts/stage-batches.js +36 -2
- package/plugins/fancy-story-builder/scripts/stage-contract.js +12 -0
- package/plugins/fancy-story-builder/scripts/story-artifacts.js +933 -0
- package/plugins/fancy-story-builder/scripts/upstream-query.js +1 -1
- 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/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 +267 -44
- 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 +121 -19
- 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 +129 -21
- 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 +76 -17
- 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 +56 -8
- 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 +63 -13
- 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 +34 -6
- 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 +81 -18
- 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 +202 -42
- 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 +160 -27
- 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 +217 -41
- 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 +215 -41
- package/plugins/fancy-story-builder/templates.js +272 -74
- package/plugins/fancy-story-builder/wiki-references.js +429 -93
- package/plugins/fancy-story-builder/writing-driver.js +336 -9
- package/plugins/fancy-topic/index.js +414 -61
- 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 +106 -63
- package/plugins/fancy-topic/templates.js +225 -122
- package/plugins/fancy-topic/tool-deploy.js +200 -0
- package/lib/safe-rm.js +0 -94
|
@@ -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); });
|