| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324 |
- import { Command } from "commander";
- import { PipelineRunner, StateManager } from "@actalk/inkos-core";
- import { readdir, stat, unlink } from "node:fs/promises";
- import { join } from "node:path";
- import { createInterface } from "node:readline";
- import { loadConfig, buildPipelineConfig, findProjectRoot, getLegacyMigrationHint, resolveContext, resolveBookId, log, logError } from "../utils.js";
- import { formatWriteNextComplete, formatWriteNextProgress, formatWriteNextResultLines, resolveCliLanguage } from "../localization.js";
- export const writeCommand = new Command("write")
- .description("Write chapters");
- writeCommand
- .command("next")
- .description("Write the next chapter for a book")
- .argument("[book-id]", "Book ID (auto-detected if only one book)")
- .option("--count <n>", "Number of chapters to write", "1")
- .option("--words <n>", "Words per chapter (overrides book config)")
- .option("--context <text>", "Creative guidance (natural language)")
- .option("--context-file <path>", "Read guidance from file")
- .option("--json", "Output JSON")
- .option("-q, --quiet", "Suppress console output")
- .action(async (bookIdArg: string | undefined, opts) => {
- try {
- const root = findProjectRoot();
- const bookId = await resolveBookId(bookIdArg, root);
- const context = await resolveContext(opts);
- const state = new StateManager(root);
- const book = await state.loadBookConfig(bookId);
- const language = resolveCliLanguage(book.language);
- const migrationHint = await getLegacyMigrationHint(root, bookId);
- if (migrationHint && !opts.json) {
- log(`[migration] ${migrationHint}`);
- }
- const config = await loadConfig();
- const pipeline = new PipelineRunner(buildPipelineConfig(config, root, { externalContext: context, quiet: opts.quiet }));
- const count = parseInt(opts.count, 10);
- const wordCount = opts.words ? parseInt(opts.words, 10) : undefined;
- const results = [];
- for (let i = 0; i < count; i++) {
- if (!opts.json) log(formatWriteNextProgress(language, i + 1, count, bookId));
- const result = await pipeline.writeNextChapter(bookId, wordCount);
- results.push(result);
- if (!opts.json) {
- for (const line of formatWriteNextResultLines(language, {
- chapterNumber: result.chapterNumber,
- title: result.title,
- wordCount: result.wordCount,
- auditPassed: result.auditResult.passed,
- revised: result.revised,
- status: result.status,
- issues: result.auditResult.issues,
- })) {
- log(line);
- }
- log("");
- }
- if (result.status === "state-degraded") {
- if (!opts.json) {
- log(language === "en"
- ? "State repair required before continuing. Stopping batch."
- : "需要先修复 state,已停止后续连写。");
- }
- break;
- }
- }
- if (opts.json) {
- log(JSON.stringify(results, null, 2));
- } else {
- log(formatWriteNextComplete(language));
- }
- } catch (e) {
- if (opts.json) {
- log(JSON.stringify({ error: String(e) }));
- } else {
- logError(`Failed to write chapter: ${e}`);
- }
- process.exit(1);
- }
- });
- writeCommand
- .command("rewrite")
- .description("Re-generate a specific chapter: rewrite [book-id] <chapter>")
- .argument("<args...>", "Book ID (optional) and chapter number")
- .option("--force", "Skip confirmation prompt")
- .option("--words <n>", "Words per chapter (overrides book config)")
- .option("--brief <text>", "One-off creative guidance for this rewrite only")
- .option("--json", "Output JSON")
- .action(async (args: ReadonlyArray<string>, opts) => {
- try {
- const root = findProjectRoot();
- let bookId: string;
- let chapter: number;
- if (args.length === 1) {
- chapter = parseInt(args[0]!, 10);
- if (isNaN(chapter)) throw new Error(`Expected chapter number, got "${args[0]}"`);
- bookId = await resolveBookId(undefined, root);
- } else if (args.length === 2) {
- chapter = parseInt(args[1]!, 10);
- if (isNaN(chapter)) throw new Error(`Expected chapter number, got "${args[1]}"`);
- bookId = await resolveBookId(args[0], root);
- } else {
- throw new Error("Usage: inkos write rewrite [book-id] <chapter>");
- }
- if (!opts.force) {
- const rl = createInterface({ input: process.stdin, output: process.stdout });
- const answer = await new Promise<string>((resolve) => {
- rl.question(`Rewrite chapter ${chapter} of "${bookId}"? This will delete chapter ${chapter} and all later chapters. (y/N) `, resolve);
- });
- rl.close();
- if (answer.toLowerCase() !== "y") {
- log("Cancelled.");
- return;
- }
- }
- const state = new StateManager(root);
- const bookDir = state.bookDir(bookId);
- const chaptersDir = join(bookDir, "chapters");
- const restoreFrom = chapter - 1;
- const restoreSnapshotDir = join(bookDir, "story", "snapshots", String(restoreFrom));
- await stat(restoreSnapshotDir).catch(() => {
- throw new Error(`Cannot rewrite chapter ${chapter}: missing snapshot for chapter ${restoreFrom}`);
- });
- const migrationHint = await getLegacyMigrationHint(root, bookId);
- if (migrationHint && !opts.json) {
- log(`[migration] ${migrationHint}`);
- }
- // Remove existing chapter file
- const files = await readdir(chaptersDir);
- const paddedNum = String(chapter).padStart(4, "0");
- const existing = files.filter((f) => f.startsWith(paddedNum) && f.endsWith(".md"));
- for (const f of existing) {
- await unlink(join(chaptersDir, f));
- if (!opts.json) log(`Removed: ${f}`);
- }
- // Remove from index (and all chapters after it)
- const index = await state.loadChapterIndex(bookId);
- const trimmed = index.filter((ch) => ch.number < chapter);
- await state.saveChapterIndex(bookId, trimmed);
- // Also remove later chapter files since state will be rolled back
- const laterFiles = files.filter((f) => {
- const num = parseInt(f.slice(0, 4), 10);
- return num > chapter && f.endsWith(".md");
- });
- for (const f of laterFiles) {
- await unlink(join(chaptersDir, f));
- if (!opts.json) log(`Removed later chapter: ${f}`);
- }
- // Restore state to previous chapter's end-state (chapter 1 uses snapshot-0 from initBook)
- const restored = await state.restoreState(bookId, restoreFrom);
- if (!restored) {
- throw new Error(`Cannot rewrite chapter ${chapter}: failed to restore snapshot for chapter ${restoreFrom}`);
- }
- if (!opts.json) log(`State restored from chapter ${restoreFrom} snapshot.`);
- const nextChapter = await state.getNextChapterNumber(bookId);
- if (nextChapter !== chapter) {
- throw new Error(`Cannot rewrite chapter ${chapter}: expected next chapter to be ${chapter}, but resolved to ${nextChapter}`);
- }
- if (!opts.json) log(`Regenerating chapter ${chapter}...`);
- const wordCount = opts.words ? parseInt(opts.words, 10) : undefined;
- const config = await loadConfig();
- const pipeline = new PipelineRunner(buildPipelineConfig(config, root, {
- externalContext: opts.brief,
- }));
- const result = await pipeline.writeNextChapter(bookId, wordCount);
- const book = await state.loadBookConfig(bookId);
- const language = resolveCliLanguage(book.language);
- if (opts.json) {
- log(JSON.stringify(result, null, 2));
- } else {
- for (const line of formatWriteNextResultLines(language, {
- chapterNumber: result.chapterNumber,
- title: result.title,
- wordCount: result.wordCount,
- auditPassed: result.auditResult.passed,
- revised: result.revised,
- status: result.status,
- issues: result.auditResult.issues,
- })) {
- log(line);
- }
- }
- } catch (e) {
- if (opts.json) {
- log(JSON.stringify({ error: String(e) }));
- } else {
- logError(`Failed to rewrite chapter: ${e}`);
- }
- process.exit(1);
- }
- });
- writeCommand
- .command("sync")
- .description("Rebuild truth files and SQLite indexes from the latest edited chapter body")
- .argument("<args...>", "Book ID (optional) and chapter number")
- .option("--brief <text>", "One-off guidance for how to interpret the edited chapter while syncing")
- .option("--json", "Output JSON")
- .action(async (args: ReadonlyArray<string>, opts) => {
- try {
- const root = findProjectRoot();
- let bookId: string;
- let chapter: number;
- if (args.length === 1) {
- chapter = parseInt(args[0]!, 10);
- if (isNaN(chapter)) throw new Error(`Expected chapter number, got "${args[0]}"`);
- bookId = await resolveBookId(undefined, root);
- } else if (args.length === 2) {
- chapter = parseInt(args[1]!, 10);
- if (isNaN(chapter)) throw new Error(`Expected chapter number, got "${args[1]}"`);
- bookId = await resolveBookId(args[0], root);
- } else {
- throw new Error("Usage: inkos write sync [book-id] <chapter>");
- }
- const state = new StateManager(root);
- const book = await state.loadBookConfig(bookId);
- const language = resolveCliLanguage(book.language);
- const config = await loadConfig();
- const pipeline = new PipelineRunner(buildPipelineConfig(config, root, {
- externalContext: opts.brief,
- }));
- const result = await pipeline.resyncChapterArtifacts(bookId, chapter);
- if (opts.json) {
- log(JSON.stringify(result, null, 2));
- } else {
- for (const line of formatWriteNextResultLines(language, {
- chapterNumber: result.chapterNumber,
- title: result.title,
- wordCount: result.wordCount,
- auditPassed: result.auditResult.passed,
- revised: result.revised,
- status: result.status,
- issues: result.auditResult.issues,
- })) {
- log(line);
- }
- }
- } catch (e) {
- if (opts.json) {
- log(JSON.stringify({ error: String(e) }));
- } else {
- logError(`Failed to sync chapter artifacts: ${e}`);
- }
- process.exit(1);
- }
- });
- writeCommand
- .command("repair-state")
- .description("Rebuild truth files for a persisted state-degraded chapter without rewriting body text")
- .argument("<args...>", "Book ID (optional) and chapter number")
- .option("--json", "Output JSON")
- .action(async (args: ReadonlyArray<string>, opts) => {
- try {
- const root = findProjectRoot();
- let bookId: string;
- let chapter: number;
- if (args.length === 1) {
- chapter = parseInt(args[0]!, 10);
- if (isNaN(chapter)) throw new Error(`Expected chapter number, got "${args[0]}"`);
- bookId = await resolveBookId(undefined, root);
- } else if (args.length === 2) {
- chapter = parseInt(args[1]!, 10);
- if (isNaN(chapter)) throw new Error(`Expected chapter number, got "${args[1]}"`);
- bookId = await resolveBookId(args[0], root);
- } else {
- throw new Error("Usage: inkos write repair-state [book-id] <chapter>");
- }
- const state = new StateManager(root);
- const book = await state.loadBookConfig(bookId);
- const language = resolveCliLanguage(book.language);
- const config = await loadConfig();
- const pipeline = new PipelineRunner(buildPipelineConfig(config, root));
- const result = await pipeline.repairChapterState(bookId, chapter);
- if (opts.json) {
- log(JSON.stringify(result, null, 2));
- } else {
- for (const line of formatWriteNextResultLines(language, {
- chapterNumber: result.chapterNumber,
- title: result.title,
- wordCount: result.wordCount,
- auditPassed: result.auditResult.passed,
- revised: result.revised,
- status: result.status,
- issues: result.auditResult.issues,
- })) {
- log(line);
- }
- }
- } catch (e) {
- if (opts.json) {
- log(JSON.stringify({ error: String(e) }));
- } else {
- logError(`Failed to repair chapter state: ${e}`);
- }
- process.exit(1);
- }
- });
|