novelforge-agent 0.1.1 → 0.3.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/README.md +45 -18
- package/dist/src/cli/index.js +81 -4
- package/dist/src/core/bibleStore.js +36 -0
- package/dist/src/core/characterStore.js +74 -0
- package/dist/src/core/contextBuilder.js +76 -1
- package/dist/src/core/fileNames.js +4 -0
- package/dist/src/core/index.js +4 -0
- package/dist/src/core/projectDiscovery.js +1 -0
- package/dist/src/core/projectOps.js +193 -0
- package/dist/src/core/projectStore.js +15 -1
- package/dist/src/core/prompts/en-US.js +247 -16
- package/dist/src/core/prompts/zh-CN.js +246 -15
- package/dist/src/core/retrieval/index.js +8 -0
- package/dist/src/core/schemas.js +121 -1
- package/dist/src/core/steps/architecture.js +7 -1
- package/dist/src/core/steps/architectureExtension.js +72 -0
- package/dist/src/core/steps/chapter.js +11 -1
- package/dist/src/core/steps/chapterReview.js +26 -1
- package/dist/src/core/steps/chapterRevision.js +17 -0
- package/dist/src/core/steps/index.js +4 -0
- package/dist/src/core/steps/memoryCard.js +26 -1
- package/dist/src/core/steps/novelMetadata.js +4 -2
- package/dist/src/core/steps/storyBible.js +1 -1
- package/dist/src/core/steps/styleGuide.js +12 -0
- package/dist/src/core/threadStore.js +150 -0
- package/dist/src/core/workflow.js +5 -3
- package/dist/src/mcp/tools.js +228 -20
- package/package.json +5 -1
- package/src/cli/index.ts +84 -3
- package/src/core/bibleStore.ts +57 -0
- package/src/core/characterStore.ts +93 -0
- package/src/core/contextBuilder.ts +74 -4
- package/src/core/fileNames.ts +5 -0
- package/src/core/index.ts +4 -0
- package/src/core/projectDiscovery.ts +2 -0
- package/src/core/projectOps.ts +251 -0
- package/src/core/projectStore.ts +19 -1
- package/src/core/prompts/en-US.ts +258 -25
- package/src/core/prompts/types.ts +4 -1
- package/src/core/prompts/zh-CN.ts +250 -17
- package/src/core/retrieval/index.ts +10 -0
- package/src/core/schemas.ts +133 -1
- package/src/core/steps/architecture.ts +7 -1
- package/src/core/steps/architectureExtension.ts +88 -0
- package/src/core/steps/chapter.ts +11 -1
- package/src/core/steps/chapterReview.ts +28 -1
- package/src/core/steps/chapterRevision.ts +18 -0
- package/src/core/steps/index.ts +4 -0
- package/src/core/steps/memoryCard.ts +27 -1
- package/src/core/steps/novelMetadata.ts +4 -2
- package/src/core/steps/storyBible.ts +1 -1
- package/src/core/steps/styleGuide.ts +13 -0
- package/src/core/threadStore.ts +173 -0
- package/src/core/types.ts +134 -1
- package/src/core/workflow.ts +5 -3
- package/src/mcp/tools.ts +351 -21
package/dist/src/mcp/tools.js
CHANGED
|
@@ -1,6 +1,28 @@
|
|
|
1
1
|
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
|
|
2
2
|
import { z } from 'zod';
|
|
3
|
-
import {
|
|
3
|
+
import { readFileSync } from 'node:fs';
|
|
4
|
+
import { dirname, join, resolve } from 'node:path';
|
|
5
|
+
import { fileURLToPath } from 'node:url';
|
|
6
|
+
import { amendStoryBible, assertProjectPath, buildContext, createProject, deleteChapter, forkProject, getNextStep, getProjectStatus, listProjects, listStoryBibleVersions, loadState, loadThreads, redoStep, requestSideTrack, retrieve, submitStepResult, updateThread, } from '../core/index.js';
|
|
7
|
+
function packageVersion() {
|
|
8
|
+
let dir = dirname(fileURLToPath(import.meta.url));
|
|
9
|
+
while (true) {
|
|
10
|
+
try {
|
|
11
|
+
const raw = readFileSync(join(dir, 'package.json'), 'utf8');
|
|
12
|
+
const parsed = JSON.parse(raw);
|
|
13
|
+
if (typeof parsed.version === 'string' && parsed.version)
|
|
14
|
+
return parsed.version;
|
|
15
|
+
}
|
|
16
|
+
catch {
|
|
17
|
+
// keep walking upward until the package root is found
|
|
18
|
+
}
|
|
19
|
+
const parent = dirname(dir);
|
|
20
|
+
if (parent === dir)
|
|
21
|
+
return '0.0.0';
|
|
22
|
+
dir = parent;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
const MCP_SERVER_VERSION = packageVersion();
|
|
4
26
|
function textResult(value) {
|
|
5
27
|
return {
|
|
6
28
|
content: [{
|
|
@@ -10,36 +32,48 @@ function textResult(value) {
|
|
|
10
32
|
};
|
|
11
33
|
}
|
|
12
34
|
export function createNovelAgentServer(options) {
|
|
35
|
+
function checkedProjectPath(projectPath) {
|
|
36
|
+
assertProjectPath(options.workspaceRoot, projectPath);
|
|
37
|
+
return projectPath;
|
|
38
|
+
}
|
|
39
|
+
function checkedOutputDir(outputDir) {
|
|
40
|
+
assertProjectPath(options.workspaceRoot, resolve(options.workspaceRoot, outputDir));
|
|
41
|
+
return outputDir;
|
|
42
|
+
}
|
|
13
43
|
const server = new McpServer({
|
|
14
44
|
name: 'novelforge-agent',
|
|
15
|
-
version:
|
|
45
|
+
version: MCP_SERVER_VERSION,
|
|
16
46
|
});
|
|
17
47
|
server.tool('start_novel_project', 'Create a local novel project and return the first generation instruction.', {
|
|
18
48
|
prompt: z.string().min(1),
|
|
19
49
|
language: z.enum(['zh-CN', 'en-US']).default('zh-CN'),
|
|
20
50
|
outputDir: z.string().default('novels'),
|
|
21
|
-
targetChapters: z.number().int().positive().default(
|
|
22
|
-
|
|
51
|
+
targetChapters: z.number().int().positive().default(5),
|
|
52
|
+
plannedTotalChapters: z.number().int().positive().default(12),
|
|
53
|
+
}, async ({ prompt, language, outputDir, targetChapters, plannedTotalChapters }) => {
|
|
23
54
|
const result = await createProject({
|
|
24
55
|
workspaceRoot: options.workspaceRoot,
|
|
25
56
|
prompt,
|
|
26
57
|
language,
|
|
27
58
|
outputDir,
|
|
28
59
|
targetChapters,
|
|
60
|
+
plannedTotalChapters,
|
|
29
61
|
});
|
|
30
62
|
return textResult({ state: result.state, next: await getNextStep(result.state.projectPath) });
|
|
31
63
|
});
|
|
32
64
|
server.tool('list_projects', 'List all NovelForge projects under the workspace, sorted by most recently updated. Use this to find an existing projectPath instead of asking the user.', {
|
|
33
65
|
outputDir: z.string().default('novels'),
|
|
34
|
-
}, async ({ outputDir }) => textResult(await listProjects({ workspaceRoot: options.workspaceRoot, outputDir })));
|
|
35
|
-
server.tool('get_project_status', 'Return a compact, one-screen summary of a project: current step, chapters written, open threads, latest review verdict, completion state.', { projectPath: z.string().min(1) }, async ({ projectPath }) => textResult(await getProjectStatus(projectPath)));
|
|
36
|
-
server.tool('get_next_step', 'Return the next required generation step for a novel project.', { projectPath: z.string().min(1) }, async ({ projectPath }) => textResult(await getNextStep(projectPath)));
|
|
66
|
+
}, async ({ outputDir }) => textResult(await listProjects({ workspaceRoot: options.workspaceRoot, outputDir: checkedOutputDir(outputDir) })));
|
|
67
|
+
server.tool('get_project_status', 'Return a compact, one-screen summary of a project: current step, chapters written, open threads, latest review verdict, completion state.', { projectPath: z.string().min(1) }, async ({ projectPath }) => textResult(await getProjectStatus(checkedProjectPath(projectPath))));
|
|
68
|
+
server.tool('get_next_step', 'Return the next required generation step for a novel project.', { projectPath: z.string().min(1) }, async ({ projectPath }) => textResult(await getNextStep(checkedProjectPath(projectPath))));
|
|
37
69
|
server.tool('submit_step_result', 'Submit host-generated content for validation, saving, and workflow advancement.', {
|
|
38
70
|
projectPath: z.string().min(1),
|
|
39
71
|
step: z.enum([
|
|
40
72
|
'novel_metadata',
|
|
41
73
|
'story_bible',
|
|
74
|
+
'style_guide',
|
|
42
75
|
'architecture',
|
|
76
|
+
'architecture_extension',
|
|
43
77
|
'chapter',
|
|
44
78
|
'memory_card',
|
|
45
79
|
'continuity_review',
|
|
@@ -49,11 +83,13 @@ export function createNovelAgentServer(options) {
|
|
|
49
83
|
'complete',
|
|
50
84
|
]),
|
|
51
85
|
content: z.string(),
|
|
52
|
-
}, async ({ projectPath, step, content }) => textResult(await submitStepResult({ projectPath, step, content })));
|
|
86
|
+
}, async ({ projectPath, step, content }) => textResult(await submitStepResult({ projectPath: checkedProjectPath(projectPath), step, content })));
|
|
53
87
|
server.tool('get_context', 'Build purpose-specific context for generation, memory extraction, review, or revision.', {
|
|
54
88
|
projectPath: z.string().min(1),
|
|
55
89
|
purpose: z.enum([
|
|
56
90
|
'chapter_generation',
|
|
91
|
+
'style_guide',
|
|
92
|
+
'architecture_extension',
|
|
57
93
|
'memory_extraction',
|
|
58
94
|
'continuity_review',
|
|
59
95
|
'revision',
|
|
@@ -64,44 +100,51 @@ export function createNovelAgentServer(options) {
|
|
|
64
100
|
start: z.number().int().positive().optional(),
|
|
65
101
|
end: z.number().int().positive().optional(),
|
|
66
102
|
}, async ({ projectPath, purpose, chapterNumber, start, end }) => textResult(await buildContext({
|
|
67
|
-
projectPath,
|
|
103
|
+
projectPath: checkedProjectPath(projectPath),
|
|
68
104
|
purpose,
|
|
69
105
|
chapterNumber,
|
|
70
106
|
range: start && end ? { start, end } : undefined,
|
|
71
107
|
})));
|
|
72
|
-
server.tool('save_chapter', '
|
|
108
|
+
server.tool('save_chapter', 'Submit a generated chapter Markdown draft through the workflow state machine. This requires currentStep="chapter" and advances to chapter_review.', {
|
|
73
109
|
projectPath: z.string().min(1),
|
|
74
110
|
chapterNumber: z.number().int().positive(),
|
|
75
111
|
title: z.string().min(1),
|
|
76
112
|
content: z.string().min(1),
|
|
77
113
|
}, async ({ projectPath, chapterNumber, title, content }) => {
|
|
78
|
-
const
|
|
79
|
-
const
|
|
80
|
-
|
|
114
|
+
const checked = checkedProjectPath(projectPath);
|
|
115
|
+
const state = await loadState(checked);
|
|
116
|
+
if (state.currentStep !== 'chapter' || state.currentChapter !== chapterNumber) {
|
|
117
|
+
throw new Error(`save_chapter requires currentStep="chapter" and currentChapter=${chapterNumber}; got currentStep="${state.currentStep}", currentChapter=${state.currentChapter}`);
|
|
118
|
+
}
|
|
119
|
+
return textResult(await submitStepResult({
|
|
120
|
+
projectPath: checked,
|
|
121
|
+
step: 'chapter',
|
|
122
|
+
content: `# ${title}\n\n${content}`,
|
|
123
|
+
}));
|
|
81
124
|
});
|
|
82
125
|
server.tool('generate_chapter', 'Build the chapter-generation context and instruction for a specific chapter without changing workflow state.', {
|
|
83
126
|
projectPath: z.string().min(1),
|
|
84
127
|
chapterNumber: z.number().int().positive(),
|
|
85
128
|
}, async ({ projectPath, chapterNumber }) => textResult({
|
|
86
|
-
context: await buildContext({ projectPath, purpose: 'chapter_generation', chapterNumber }),
|
|
87
|
-
hint: 'Persist the result via
|
|
129
|
+
context: await buildContext({ projectPath: checkedProjectPath(projectPath), purpose: 'chapter_generation', chapterNumber }),
|
|
130
|
+
hint: 'Persist the result via submit_step_result(step="chapter") when the workflow currentStep is "chapter"; the workflow then requires chapter_review before memory_card.',
|
|
88
131
|
}));
|
|
89
132
|
server.tool('extract_memory_card', 'Build the memory-extraction context for a specific chapter without changing workflow state.', {
|
|
90
133
|
projectPath: z.string().min(1),
|
|
91
134
|
chapterNumber: z.number().int().positive(),
|
|
92
135
|
}, async ({ projectPath, chapterNumber }) => textResult({
|
|
93
|
-
context: await buildContext({ projectPath, purpose: 'memory_extraction', chapterNumber }),
|
|
136
|
+
context: await buildContext({ projectPath: checkedProjectPath(projectPath), purpose: 'memory_extraction', chapterNumber }),
|
|
94
137
|
hint: 'Submit the extracted memory card via submit_step_result with step="memory_card" when the workflow currentStep matches.',
|
|
95
138
|
}));
|
|
96
139
|
server.tool('review_chapter', 'Ask the host to review a specific chapter. Switches the workflow into chapter_review side-track and returns the review prompt + packed context. Resume original step after submit_step_result(step="chapter_review").', {
|
|
97
140
|
projectPath: z.string().min(1),
|
|
98
141
|
chapterNumber: z.number().int().positive(),
|
|
99
|
-
}, async ({ projectPath, chapterNumber }) => textResult(await requestSideTrack({ projectPath, step: 'chapter_review', chapterNumber })));
|
|
142
|
+
}, async ({ projectPath, chapterNumber }) => textResult(await requestSideTrack({ projectPath: checkedProjectPath(projectPath), step: 'chapter_review', chapterNumber })));
|
|
100
143
|
server.tool('revise_chapter', 'Ask the host to rewrite a specific chapter based on prior review feedback and optional extra instructions. Previous version is archived under chapters/.versions/.', {
|
|
101
144
|
projectPath: z.string().min(1),
|
|
102
145
|
chapterNumber: z.number().int().positive(),
|
|
103
146
|
feedback: z.string().optional(),
|
|
104
|
-
}, async ({ projectPath, chapterNumber, feedback }) => textResult(await requestSideTrack({ projectPath, step: 'chapter_revision', chapterNumber, feedback })));
|
|
147
|
+
}, async ({ projectPath, chapterNumber, feedback }) => textResult(await requestSideTrack({ projectPath: checkedProjectPath(projectPath), step: 'chapter_revision', chapterNumber, feedback })));
|
|
105
148
|
server.tool('retrieve', 'Lexical BM25-style retrieval over indexed chapter paragraphs, story-bible sections, and memory cards. Returns ranked snippets with chapter attribution.', {
|
|
106
149
|
projectPath: z.string().min(1),
|
|
107
150
|
query: z.string().min(1),
|
|
@@ -111,7 +154,7 @@ export function createNovelAgentServer(options) {
|
|
|
111
154
|
chapterEnd: z.number().int().positive().optional(),
|
|
112
155
|
}, async ({ projectPath, query, topK, types, chapterStart, chapterEnd }) => {
|
|
113
156
|
const chapterRange = chapterStart && chapterEnd ? { start: chapterStart, end: chapterEnd } : undefined;
|
|
114
|
-
const hits = await retrieve(projectPath, query, { topK, types, chapterRange });
|
|
157
|
+
const hits = await retrieve(checkedProjectPath(projectPath), query, { topK, types, chapterRange });
|
|
115
158
|
return textResult({ query, hits });
|
|
116
159
|
});
|
|
117
160
|
server.tool('cross_chapter_review', 'Ask the host to review a chapter range for cross-chapter continuity conflicts. Defaults to all generated chapters.', {
|
|
@@ -120,7 +163,172 @@ export function createNovelAgentServer(options) {
|
|
|
120
163
|
end: z.number().int().positive().optional(),
|
|
121
164
|
}, async ({ projectPath, start, end }) => {
|
|
122
165
|
const range = start && end ? { start, end } : undefined;
|
|
123
|
-
return textResult(await requestSideTrack({ projectPath, step: 'cross_chapter_review', range }));
|
|
166
|
+
return textResult(await requestSideTrack({ projectPath: checkedProjectPath(projectPath), step: 'cross_chapter_review', range }));
|
|
167
|
+
});
|
|
168
|
+
// ----- v0.2 tools -----
|
|
169
|
+
server.tool('amend_story_bible', 'Replace the story bible with a revised version. Old version is auto-archived under story-bible-versions/ and the lexical index is rebuilt for the new content.', {
|
|
170
|
+
projectPath: z.string().min(1),
|
|
171
|
+
content: z.string().min(1),
|
|
172
|
+
reason: z.string().optional(),
|
|
173
|
+
}, async ({ projectPath, content, reason }) => textResult(await amendStoryBible({ projectPath: checkedProjectPath(projectPath), content, reason })));
|
|
174
|
+
server.tool('list_bible_versions', 'List archived story-bible versions for a project (filenames sorted oldest first).', { projectPath: z.string().min(1) }, async ({ projectPath }) => textResult({ versions: await listStoryBibleVersions(checkedProjectPath(projectPath)) }));
|
|
175
|
+
server.tool('list_threads', 'List foreshadow threads for a project, optionally filtered by status. Threads are aggregated from memory_card.threadActions.', {
|
|
176
|
+
projectPath: z.string().min(1),
|
|
177
|
+
status: z.enum(['planted', 'building', 'paid', 'dropped']).optional(),
|
|
178
|
+
}, async ({ projectPath, status }) => {
|
|
179
|
+
const all = await loadThreads(checkedProjectPath(projectPath));
|
|
180
|
+
const filtered = status ? all.filter((t) => t.status === status) : all;
|
|
181
|
+
return textResult({ threads: filtered });
|
|
124
182
|
});
|
|
183
|
+
server.tool('update_thread', 'Update a single foreshadow thread (override status, plannedPayoffAt, paidOffAt, droppedAt, description, notes).', {
|
|
184
|
+
projectPath: z.string().min(1),
|
|
185
|
+
id: z.string().min(1),
|
|
186
|
+
status: z.enum(['planted', 'building', 'paid', 'dropped']).optional(),
|
|
187
|
+
plannedPayoffAt: z.number().int().positive().nullable().optional(),
|
|
188
|
+
paidOffAt: z.number().int().positive().nullable().optional(),
|
|
189
|
+
droppedAt: z.number().int().positive().nullable().optional(),
|
|
190
|
+
description: z.string().min(1).optional(),
|
|
191
|
+
notes: z.string().nullable().optional(),
|
|
192
|
+
}, async ({ projectPath, id, ...patch }) => textResult(await updateThread(checkedProjectPath(projectPath), id, patch)));
|
|
193
|
+
server.tool('fork_project', 'Copy an existing project to a new sibling directory with a new projectId. Use to try alternate plot branches without losing the original.', {
|
|
194
|
+
sourceProjectPath: z.string().min(1),
|
|
195
|
+
label: z.string().optional(),
|
|
196
|
+
}, async ({ sourceProjectPath, label }) => textResult(await forkProject({ sourceProjectPath: checkedProjectPath(sourceProjectPath), label })));
|
|
197
|
+
server.tool('delete_chapter', 'Delete a chapter, its memory card, its single-chapter review, and all archived versions. Removes the chapter from the lexical index and rewinds the workflow if needed.', {
|
|
198
|
+
projectPath: z.string().min(1),
|
|
199
|
+
chapterNumber: z.number().int().positive(),
|
|
200
|
+
}, async ({ projectPath, chapterNumber }) => textResult(await deleteChapter({ projectPath: checkedProjectPath(projectPath), chapterNumber })));
|
|
201
|
+
server.tool('redo_step', 'Roll the workflow back to a specific step. Files produced by that step (and dependent chapter content for chapter/memory_card steps) are removed; the host must regenerate.', {
|
|
202
|
+
projectPath: z.string().min(1),
|
|
203
|
+
step: z.enum([
|
|
204
|
+
'novel_metadata',
|
|
205
|
+
'story_bible',
|
|
206
|
+
'style_guide',
|
|
207
|
+
'architecture',
|
|
208
|
+
'chapter',
|
|
209
|
+
'memory_card',
|
|
210
|
+
'continuity_review',
|
|
211
|
+
]),
|
|
212
|
+
chapterNumber: z.number().int().positive().optional(),
|
|
213
|
+
}, async ({ projectPath, step, chapterNumber }) => textResult(await redoStep({ projectPath: checkedProjectPath(projectPath), step, chapterNumber })));
|
|
214
|
+
// ===== MCP Prompts (slash commands) =====
|
|
215
|
+
server.prompt('nf-start', 'Start a brand new novel project under the configured workspace.', {
|
|
216
|
+
prompt: z.string().describe('User idea / premise / genre, in any language.'),
|
|
217
|
+
chapters: z.string().optional().describe('Planning batch size as a string. Defaults to 5.'),
|
|
218
|
+
totalChapters: z.string().optional().describe('Whole-book target chapter count as a string. Defaults to 12.'),
|
|
219
|
+
}, ({ prompt, chapters, totalChapters }) => ({
|
|
220
|
+
messages: [{
|
|
221
|
+
role: 'user',
|
|
222
|
+
content: {
|
|
223
|
+
type: 'text',
|
|
224
|
+
text: `Use the novelforge MCP server. Call start_novel_project with prompt="${prompt}", targetChapters=${chapters ?? '5'}, plannedTotalChapters=${totalChapters ?? '12'}, then enter the autonomous loop: read next.instruction, generate the requested content, call submit_step_result, repeat until currentStep is "complete". Show me the projectPath after start_novel_project returns.`,
|
|
225
|
+
},
|
|
226
|
+
}],
|
|
227
|
+
}));
|
|
228
|
+
server.prompt('nf-next', 'Continue the current novelforge workflow by one step.', {
|
|
229
|
+
projectPath: z.string().describe('Absolute path to the project.'),
|
|
230
|
+
}, ({ projectPath }) => ({
|
|
231
|
+
messages: [{
|
|
232
|
+
role: 'user',
|
|
233
|
+
content: {
|
|
234
|
+
type: 'text',
|
|
235
|
+
text: `Use the novelforge MCP server. Call get_next_step with projectPath="${projectPath}". Read the returned instruction + context and produce the requested artifact, then call submit_step_result. Show me what step was advanced.`,
|
|
236
|
+
},
|
|
237
|
+
}],
|
|
238
|
+
}));
|
|
239
|
+
server.prompt('nf-list', 'List all novelforge projects in the workspace.', {}, () => ({
|
|
240
|
+
messages: [{
|
|
241
|
+
role: 'user',
|
|
242
|
+
content: {
|
|
243
|
+
type: 'text',
|
|
244
|
+
text: 'Use the novelforge MCP server: call list_projects with no arguments. Show me the result in a compact table (title, currentStep, chaptersWritten/plannedTotalChapters, updatedAt, projectPath).',
|
|
245
|
+
},
|
|
246
|
+
}],
|
|
247
|
+
}));
|
|
248
|
+
server.prompt('nf-status', 'Show a one-screen status for a novelforge project.', {
|
|
249
|
+
projectPath: z.string(),
|
|
250
|
+
}, ({ projectPath }) => ({
|
|
251
|
+
messages: [{
|
|
252
|
+
role: 'user',
|
|
253
|
+
content: {
|
|
254
|
+
type: 'text',
|
|
255
|
+
text: `Use the novelforge MCP server: call get_project_status with projectPath="${projectPath}". Summarize: title, current step, chapters written, open threads count, latest review verdict.`,
|
|
256
|
+
},
|
|
257
|
+
}],
|
|
258
|
+
}));
|
|
259
|
+
server.prompt('nf-review-chapter', 'Run a single-chapter editorial review.', {
|
|
260
|
+
projectPath: z.string(),
|
|
261
|
+
chapterNumber: z.string(),
|
|
262
|
+
}, ({ projectPath, chapterNumber }) => ({
|
|
263
|
+
messages: [{
|
|
264
|
+
role: 'user',
|
|
265
|
+
content: {
|
|
266
|
+
type: 'text',
|
|
267
|
+
text: `Use the novelforge MCP server: call review_chapter with projectPath="${projectPath}", chapterNumber=${chapterNumber}. Read the returned instruction + context, produce the JSON chapter review, then call submit_step_result with step="chapter_review". Summarize the findings for me.`,
|
|
268
|
+
},
|
|
269
|
+
}],
|
|
270
|
+
}));
|
|
271
|
+
server.prompt('nf-revise-chapter', 'Revise a chapter based on review feedback or new instructions.', {
|
|
272
|
+
projectPath: z.string(),
|
|
273
|
+
chapterNumber: z.string(),
|
|
274
|
+
feedback: z.string().optional(),
|
|
275
|
+
}, ({ projectPath, chapterNumber, feedback }) => ({
|
|
276
|
+
messages: [{
|
|
277
|
+
role: 'user',
|
|
278
|
+
content: {
|
|
279
|
+
type: 'text',
|
|
280
|
+
text: `Use the novelforge MCP server: call revise_chapter with projectPath="${projectPath}", chapterNumber=${chapterNumber}${feedback ? `, feedback=${JSON.stringify(feedback)}` : ''}. Read the returned instruction + context, produce the revised Markdown chapter, then call submit_step_result with step="chapter_revision". Confirm the previous version was archived.`,
|
|
281
|
+
},
|
|
282
|
+
}],
|
|
283
|
+
}));
|
|
284
|
+
server.prompt('nf-cross-review', 'Cross-chapter continuity review over a range.', {
|
|
285
|
+
projectPath: z.string(),
|
|
286
|
+
start: z.string().optional(),
|
|
287
|
+
end: z.string().optional(),
|
|
288
|
+
}, ({ projectPath, start, end }) => ({
|
|
289
|
+
messages: [{
|
|
290
|
+
role: 'user',
|
|
291
|
+
content: {
|
|
292
|
+
type: 'text',
|
|
293
|
+
text: `Use the novelforge MCP server: call cross_chapter_review with projectPath="${projectPath}"${start && end ? `, start=${start}, end=${end}` : ''}. Read the returned instruction + context, produce the JSON cross-chapter review, then call submit_step_result with step="cross_chapter_review". Summarize verdict and any issues.`,
|
|
294
|
+
},
|
|
295
|
+
}],
|
|
296
|
+
}));
|
|
297
|
+
server.prompt('nf-retrieve', 'Lexical retrieval over a project (BM25-style).', {
|
|
298
|
+
projectPath: z.string(),
|
|
299
|
+
query: z.string(),
|
|
300
|
+
}, ({ projectPath, query }) => ({
|
|
301
|
+
messages: [{
|
|
302
|
+
role: 'user',
|
|
303
|
+
content: {
|
|
304
|
+
type: 'text',
|
|
305
|
+
text: `Use the novelforge MCP server: call retrieve with projectPath="${projectPath}", query=${JSON.stringify(query)}, topK=8. List the hits with chapter attribution and short excerpts.`,
|
|
306
|
+
},
|
|
307
|
+
}],
|
|
308
|
+
}));
|
|
309
|
+
server.prompt('nf-amend-bible', 'Amend the story bible with new content (previous version auto-archived).', {
|
|
310
|
+
projectPath: z.string(),
|
|
311
|
+
reason: z.string().optional(),
|
|
312
|
+
}, ({ projectPath, reason }) => ({
|
|
313
|
+
messages: [{
|
|
314
|
+
role: 'user',
|
|
315
|
+
content: {
|
|
316
|
+
type: 'text',
|
|
317
|
+
text: `Use the novelforge MCP server. First call get_project_status with projectPath="${projectPath}" to confirm the project exists, then read the current story-bible.md (you may use the host's filesystem tools). Apply the following amendment intent and produce a complete revised story bible Markdown:\n\n${reason ?? '(no specific reason supplied — ask the user what to change)'}\n\nThen call amend_story_bible with projectPath="${projectPath}" and the new content. Confirm the archived version path.`,
|
|
318
|
+
},
|
|
319
|
+
}],
|
|
320
|
+
}));
|
|
321
|
+
server.prompt('nf-threads', 'Show active foreshadow threads for a project.', {
|
|
322
|
+
projectPath: z.string(),
|
|
323
|
+
status: z.enum(['planted', 'building', 'paid', 'dropped']).optional(),
|
|
324
|
+
}, ({ projectPath, status }) => ({
|
|
325
|
+
messages: [{
|
|
326
|
+
role: 'user',
|
|
327
|
+
content: {
|
|
328
|
+
type: 'text',
|
|
329
|
+
text: `Use the novelforge MCP server: call list_threads with projectPath="${projectPath}"${status ? `, status="${status}"` : ''}. Show me the threads as a compact list: id, status, plantedAt, plannedPayoffAt (if set), description.`,
|
|
330
|
+
},
|
|
331
|
+
}],
|
|
332
|
+
}));
|
|
125
333
|
return server;
|
|
126
334
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "novelforge-agent",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "Local-first long-form novel workflow engine for any MCP host (Claude Code, Codex CLI, …) or CLI. State machine + zod schemas + BM25 retrieval + persistent project state. No LLM dependency.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"mcp",
|
|
@@ -43,6 +43,10 @@
|
|
|
43
43
|
"scripts": {
|
|
44
44
|
"build": "tsc -p tsconfig.json",
|
|
45
45
|
"test": "npm run build && node --test dist/test/*.test.js",
|
|
46
|
+
"test:e2e": "npm run build && bash scripts/e2e.sh",
|
|
47
|
+
"inspect": "npm run build && npx -y @modelcontextprotocol/inspector node dist/src/mcp/server.js",
|
|
48
|
+
"inspect:tools": "npm run build && npx -y @modelcontextprotocol/inspector --cli node dist/src/mcp/server.js --method tools/list",
|
|
49
|
+
"inspect:prompts": "npm run build && npx -y @modelcontextprotocol/inspector --cli node dist/src/mcp/server.js --method prompts/list",
|
|
46
50
|
"dev:mcp": "tsx src/mcp/server.ts",
|
|
47
51
|
"dev:cli": "tsx src/cli/index.ts",
|
|
48
52
|
"prepublishOnly": "npm run build && npm test",
|
package/src/cli/index.ts
CHANGED
|
@@ -1,14 +1,20 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import { readFile } from 'node:fs/promises';
|
|
3
3
|
import {
|
|
4
|
+
amendStoryBible,
|
|
4
5
|
buildContext,
|
|
5
6
|
createProject,
|
|
7
|
+
deleteChapter,
|
|
8
|
+
forkProject,
|
|
6
9
|
getNextStep,
|
|
7
10
|
getProjectStatus,
|
|
8
11
|
listProjects,
|
|
12
|
+
loadThreads,
|
|
13
|
+
redoStep,
|
|
9
14
|
requestSideTrack,
|
|
10
15
|
retrieve,
|
|
11
16
|
submitStepResult,
|
|
17
|
+
updateThread,
|
|
12
18
|
} from '../core/index.js';
|
|
13
19
|
import { formatInstallResult, runInstall, InstallHost } from './install.js';
|
|
14
20
|
|
|
@@ -48,9 +54,17 @@ export async function runCli(argv = process.argv.slice(2), cwd = process.cwd()):
|
|
|
48
54
|
const prompt = valueAfter(argv, '--prompt') || '';
|
|
49
55
|
if (!prompt.trim()) throw new Error('Missing --prompt');
|
|
50
56
|
const language = parseLanguage(valueAfter(argv, '--language') || 'zh-CN');
|
|
51
|
-
const chapters = Number(valueAfter(argv, '--chapters') ||
|
|
57
|
+
const chapters = Number(valueAfter(argv, '--chapters') || 5);
|
|
58
|
+
const totalChapters = Number(valueAfter(argv, '--total-chapters') || 12);
|
|
52
59
|
const outputDir = valueAfter(argv, '--output') || 'novels';
|
|
53
|
-
const result = await createProject({
|
|
60
|
+
const result = await createProject({
|
|
61
|
+
workspaceRoot: cwd,
|
|
62
|
+
prompt,
|
|
63
|
+
language,
|
|
64
|
+
outputDir,
|
|
65
|
+
targetChapters: chapters,
|
|
66
|
+
plannedTotalChapters: totalChapters,
|
|
67
|
+
});
|
|
54
68
|
const next = await getNextStep(result.state.projectPath);
|
|
55
69
|
console.log(JSON.stringify({ state: result.state, next }, null, 2));
|
|
56
70
|
return;
|
|
@@ -156,7 +170,74 @@ export async function runCli(argv = process.argv.slice(2), cwd = process.cwd()):
|
|
|
156
170
|
return;
|
|
157
171
|
}
|
|
158
172
|
|
|
159
|
-
|
|
173
|
+
if (command === 'amend-bible') {
|
|
174
|
+
if (!projectPath) throw new Error('Missing projectPath');
|
|
175
|
+
const file = valueAfter(argv, '--file');
|
|
176
|
+
const reason = valueAfter(argv, '--reason');
|
|
177
|
+
if (!file) throw new Error('Missing --file with new bible Markdown');
|
|
178
|
+
const content = await readFile(file, 'utf8');
|
|
179
|
+
console.log(JSON.stringify(await amendStoryBible({ projectPath, content, reason }), null, 2));
|
|
180
|
+
return;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
if (command === 'threads') {
|
|
184
|
+
if (!projectPath) throw new Error('Missing projectPath');
|
|
185
|
+
const status = valueAfter(argv, '--status') as 'planted' | 'building' | 'paid' | 'dropped' | undefined;
|
|
186
|
+
const all = await loadThreads(projectPath);
|
|
187
|
+
const filtered = status ? all.filter((t) => t.status === status) : all;
|
|
188
|
+
console.log(JSON.stringify(filtered, null, 2));
|
|
189
|
+
return;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
if (command === 'update-thread') {
|
|
193
|
+
if (!projectPath) throw new Error('Missing projectPath');
|
|
194
|
+
const id = valueAfter(argv, '--id');
|
|
195
|
+
if (!id) throw new Error('Missing --id');
|
|
196
|
+
const status = valueAfter(argv, '--status') as 'planted' | 'building' | 'paid' | 'dropped' | undefined;
|
|
197
|
+
const plannedPayoffAt = valueAfter(argv, '--planned-payoff');
|
|
198
|
+
const description = valueAfter(argv, '--description');
|
|
199
|
+
const notes = valueAfter(argv, '--notes');
|
|
200
|
+
const updated = await updateThread(projectPath, id, {
|
|
201
|
+
status,
|
|
202
|
+
plannedPayoffAt: plannedPayoffAt ? Number(plannedPayoffAt) : undefined,
|
|
203
|
+
description,
|
|
204
|
+
notes,
|
|
205
|
+
});
|
|
206
|
+
console.log(JSON.stringify(updated, null, 2));
|
|
207
|
+
return;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
if (command === 'fork') {
|
|
211
|
+
if (!projectPath) throw new Error('Missing projectPath');
|
|
212
|
+
const label = valueAfter(argv, '--label');
|
|
213
|
+
console.log(JSON.stringify(await forkProject({ sourceProjectPath: projectPath, label }), null, 2));
|
|
214
|
+
return;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
if (command === 'delete-chapter') {
|
|
218
|
+
if (!projectPath) throw new Error('Missing projectPath');
|
|
219
|
+
const chapter = valueAfter(argv, '--chapter');
|
|
220
|
+
if (!chapter) throw new Error('Missing --chapter');
|
|
221
|
+
console.log(JSON.stringify(await deleteChapter({ projectPath, chapterNumber: Number(chapter) }), null, 2));
|
|
222
|
+
return;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
if (command === 'redo') {
|
|
226
|
+
if (!projectPath) throw new Error('Missing projectPath');
|
|
227
|
+
const step = valueAfter(argv, '--step') as
|
|
228
|
+
| 'novel_metadata' | 'story_bible' | 'style_guide' | 'architecture' | 'chapter' | 'memory_card' | 'continuity_review'
|
|
229
|
+
| undefined;
|
|
230
|
+
if (!step) throw new Error('Missing --step');
|
|
231
|
+
const chapter = valueAfter(argv, '--chapter');
|
|
232
|
+
console.log(JSON.stringify(await redoStep({
|
|
233
|
+
projectPath,
|
|
234
|
+
step,
|
|
235
|
+
chapterNumber: chapter ? Number(chapter) : undefined,
|
|
236
|
+
}), null, 2));
|
|
237
|
+
return;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
throw new Error('Usage: novelforge-agent install|start|list|status|next|submit|context|review|revise|cross-review|retrieve|amend-bible|threads|update-thread|fork|delete-chapter|redo');
|
|
160
241
|
}
|
|
161
242
|
|
|
162
243
|
if (import.meta.url === `file://${process.argv[1]}`) {
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { readdir } from 'node:fs/promises';
|
|
2
|
+
import { join } from 'node:path';
|
|
3
|
+
import {
|
|
4
|
+
archiveStoryBible,
|
|
5
|
+
loadState,
|
|
6
|
+
saveMarkdownFile,
|
|
7
|
+
saveState,
|
|
8
|
+
} from './projectStore.js';
|
|
9
|
+
import { storyBibleVersionFileName } from './fileNames.js';
|
|
10
|
+
import { indexStoryBible } from './retrieval/index.js';
|
|
11
|
+
|
|
12
|
+
export interface AmendStoryBibleInput {
|
|
13
|
+
projectPath: string;
|
|
14
|
+
content: string;
|
|
15
|
+
reason?: string;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface AmendStoryBibleResult {
|
|
19
|
+
archivedPath?: string;
|
|
20
|
+
bibleVersion: number;
|
|
21
|
+
savedPath: string;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function isEmpty(content: string): boolean {
|
|
25
|
+
return !content || !content.trim();
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export async function amendStoryBible(input: AmendStoryBibleInput): Promise<AmendStoryBibleResult> {
|
|
29
|
+
if (isEmpty(input.content)) throw new Error('Amended story bible content is empty');
|
|
30
|
+
const state = await loadState(input.projectPath);
|
|
31
|
+
// Archive current
|
|
32
|
+
const archived = await archiveStoryBible(
|
|
33
|
+
state.projectPath,
|
|
34
|
+
join('story-bible-versions', storyBibleVersionFileName(new Date().toISOString()))
|
|
35
|
+
);
|
|
36
|
+
// Save new
|
|
37
|
+
const savedPath = await saveMarkdownFile(state.projectPath, 'story-bible.md', input.content);
|
|
38
|
+
// Re-index
|
|
39
|
+
await indexStoryBible(state.projectPath, input.content);
|
|
40
|
+
// Track in state
|
|
41
|
+
const bibleVersion = (state.completedSteps.filter((s) => s === 'story_bible_amend').length ?? 0) + 1;
|
|
42
|
+
await saveState({
|
|
43
|
+
...state,
|
|
44
|
+
completedSteps: [...state.completedSteps, 'story_bible_amend' as const],
|
|
45
|
+
files: { ...state.files, storyBible: 'story-bible.md' },
|
|
46
|
+
});
|
|
47
|
+
return { archivedPath: archived, bibleVersion, savedPath };
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export async function listStoryBibleVersions(projectPath: string): Promise<string[]> {
|
|
51
|
+
try {
|
|
52
|
+
const items = await readdir(join(projectPath, 'story-bible-versions'));
|
|
53
|
+
return items.filter((f) => f.endsWith('.md')).sort();
|
|
54
|
+
} catch {
|
|
55
|
+
return [];
|
|
56
|
+
}
|
|
57
|
+
}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { readFile, writeFile } from 'node:fs/promises';
|
|
2
|
+
import { join } from 'node:path';
|
|
3
|
+
import { CharacterState, CharacterStateUpdate, CoreCastMember } from './types.js';
|
|
4
|
+
|
|
5
|
+
const CHARACTERS_FILE = 'characters.json';
|
|
6
|
+
|
|
7
|
+
export interface CharactersBundle {
|
|
8
|
+
characters: CharacterState[];
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
function emptyState(member: CoreCastMember, chapterNumber = 0): CharacterState {
|
|
12
|
+
return {
|
|
13
|
+
name: member.name,
|
|
14
|
+
role: member.role,
|
|
15
|
+
goal: '未确认',
|
|
16
|
+
belief: '未确认',
|
|
17
|
+
relationships: [],
|
|
18
|
+
abilities: [],
|
|
19
|
+
secrets: [],
|
|
20
|
+
emotionalState: member.description,
|
|
21
|
+
lastUpdatedAt: chapterNumber,
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export async function loadCharacterStates(projectPath: string): Promise<CharacterState[]> {
|
|
26
|
+
try {
|
|
27
|
+
const raw = await readFile(join(projectPath, CHARACTERS_FILE), 'utf8');
|
|
28
|
+
const parsed = JSON.parse(raw) as CharactersBundle;
|
|
29
|
+
return Array.isArray(parsed.characters) ? parsed.characters : [];
|
|
30
|
+
} catch {
|
|
31
|
+
return [];
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export async function saveCharacterStates(projectPath: string, characters: CharacterState[]): Promise<string> {
|
|
36
|
+
const fullPath = join(projectPath, CHARACTERS_FILE);
|
|
37
|
+
await writeFile(fullPath, `${JSON.stringify({ characters }, null, 2)}\n`, 'utf8');
|
|
38
|
+
return fullPath;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export async function initializeCharacterStates(
|
|
42
|
+
projectPath: string,
|
|
43
|
+
coreCast: CoreCastMember[]
|
|
44
|
+
): Promise<string> {
|
|
45
|
+
const existing = await loadCharacterStates(projectPath);
|
|
46
|
+
const byName = new Map(existing.map((c) => [c.name, c]));
|
|
47
|
+
for (const member of coreCast) {
|
|
48
|
+
if (!byName.has(member.name)) {
|
|
49
|
+
byName.set(member.name, emptyState(member));
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
return saveCharacterStates(projectPath, Array.from(byName.values()));
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export async function applyCharacterUpdates(
|
|
56
|
+
projectPath: string,
|
|
57
|
+
chapterNumber: number,
|
|
58
|
+
updates: CharacterStateUpdate[] | undefined
|
|
59
|
+
): Promise<CharacterState[]> {
|
|
60
|
+
const existing = await loadCharacterStates(projectPath);
|
|
61
|
+
if (!updates || !updates.length) return existing;
|
|
62
|
+
|
|
63
|
+
const byName = new Map(existing.map((c) => [c.name, { ...c }]));
|
|
64
|
+
for (const update of updates) {
|
|
65
|
+
const current = byName.get(update.name) ?? {
|
|
66
|
+
name: update.name,
|
|
67
|
+
role: update.role,
|
|
68
|
+
goal: '未确认',
|
|
69
|
+
belief: '未确认',
|
|
70
|
+
relationships: [],
|
|
71
|
+
abilities: [],
|
|
72
|
+
secrets: [],
|
|
73
|
+
emotionalState: '未确认',
|
|
74
|
+
lastUpdatedAt: chapterNumber,
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
byName.set(update.name, {
|
|
78
|
+
...current,
|
|
79
|
+
role: update.role ?? current.role,
|
|
80
|
+
goal: update.goal ?? current.goal,
|
|
81
|
+
belief: update.belief ?? current.belief,
|
|
82
|
+
relationships: update.relationships ?? current.relationships,
|
|
83
|
+
abilities: update.abilities ?? current.abilities,
|
|
84
|
+
secrets: update.secrets ?? current.secrets,
|
|
85
|
+
emotionalState: update.emotionalState ?? current.emotionalState,
|
|
86
|
+
lastUpdatedAt: chapterNumber,
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const next = Array.from(byName.values()).sort((a, b) => a.name.localeCompare(b.name));
|
|
91
|
+
await saveCharacterStates(projectPath, next);
|
|
92
|
+
return next;
|
|
93
|
+
}
|