| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744 |
- import { readFile, writeFile, mkdir } from "node:fs/promises";
- import { join } from "node:path";
- import { BaseAgent } from "./base.js";
- import type { BookConfig } from "../models/book.js";
- import { parseBookRules } from "../models/book-rules.js";
- import { ChapterIntentSchema, type ChapterConflict, type ChapterIntent } from "../models/input-governance.js";
- import {
- parseChapterSummariesMarkdown,
- renderHookSnapshot,
- renderSummarySnapshot,
- retrieveMemorySelection,
- } from "../utils/memory-retrieval.js";
- import { analyzeChapterCadence } from "../utils/chapter-cadence.js";
- import { buildPlannerHookAgenda } from "../utils/hook-agenda.js";
- export interface PlanChapterInput {
- readonly book: BookConfig;
- readonly bookDir: string;
- readonly chapterNumber: number;
- readonly externalContext?: string;
- }
- export interface PlanChapterOutput {
- readonly intent: ChapterIntent;
- readonly intentMarkdown: string;
- readonly plannerInputs: ReadonlyArray<string>;
- readonly runtimePath: string;
- }
- export class PlannerAgent extends BaseAgent {
- get name(): string {
- return "planner";
- }
- async planChapter(input: PlanChapterInput): Promise<PlanChapterOutput> {
- const storyDir = join(input.bookDir, "story");
- const runtimeDir = join(storyDir, "runtime");
- await mkdir(runtimeDir, { recursive: true });
- const sourcePaths = {
- authorIntent: join(storyDir, "author_intent.md"),
- currentFocus: join(storyDir, "current_focus.md"),
- storyBible: join(storyDir, "story_bible.md"),
- volumeOutline: join(storyDir, "volume_outline.md"),
- chapterSummaries: join(storyDir, "chapter_summaries.md"),
- bookRules: join(storyDir, "book_rules.md"),
- currentState: join(storyDir, "current_state.md"),
- } as const;
- const [
- authorIntent,
- currentFocus,
- storyBible,
- volumeOutline,
- chapterSummaries,
- bookRulesRaw,
- currentState,
- ] = await Promise.all([
- this.readFileOrDefault(sourcePaths.authorIntent),
- this.readFileOrDefault(sourcePaths.currentFocus),
- this.readFileOrDefault(sourcePaths.storyBible),
- this.readFileOrDefault(sourcePaths.volumeOutline),
- this.readFileOrDefault(sourcePaths.chapterSummaries),
- this.readFileOrDefault(sourcePaths.bookRules),
- this.readFileOrDefault(sourcePaths.currentState),
- ]);
- const outlineNode = this.findOutlineNode(volumeOutline, input.chapterNumber);
- const matchedOutlineAnchor = this.hasMatchedOutlineAnchor(volumeOutline, input.chapterNumber);
- const goal = this.deriveGoal(input.externalContext, currentFocus, authorIntent, outlineNode, input.chapterNumber);
- const parsedRules = parseBookRules(bookRulesRaw);
- const mustKeep = this.collectMustKeep(currentState, storyBible);
- const mustAvoid = this.collectMustAvoid(currentFocus, parsedRules.rules.prohibitions);
- const styleEmphasis = this.collectStyleEmphasis(authorIntent, currentFocus);
- const conflicts = this.collectConflicts(input.externalContext, currentFocus, outlineNode, volumeOutline);
- const planningAnchor = conflicts.length > 0 ? undefined : outlineNode;
- const memorySelection = await retrieveMemorySelection({
- bookDir: input.bookDir,
- chapterNumber: input.chapterNumber,
- goal,
- outlineNode: planningAnchor,
- mustKeep,
- });
- const activeHookCount = memorySelection.activeHooks.filter(
- (hook) => hook.status !== "resolved" && hook.status !== "deferred",
- ).length;
- const hookAgenda = buildPlannerHookAgenda({
- hooks: memorySelection.activeHooks,
- chapterNumber: input.chapterNumber,
- targetChapters: input.book.targetChapters,
- language: input.book.language ?? "zh",
- });
- const directives = this.buildStructuredDirectives({
- chapterNumber: input.chapterNumber,
- language: input.book.language,
- volumeOutline,
- outlineNode,
- matchedOutlineAnchor,
- chapterSummaries,
- });
- const intent = ChapterIntentSchema.parse({
- chapter: input.chapterNumber,
- goal,
- outlineNode,
- ...directives,
- mustKeep,
- mustAvoid,
- styleEmphasis,
- conflicts,
- hookAgenda,
- });
- const runtimePath = join(runtimeDir, `chapter-${String(input.chapterNumber).padStart(4, "0")}.intent.md`);
- const intentMarkdown = this.renderIntentMarkdown(
- intent,
- input.book.language ?? "zh",
- renderHookSnapshot(memorySelection.hooks, input.book.language ?? "zh"),
- renderSummarySnapshot(memorySelection.summaries, input.book.language ?? "zh"),
- activeHookCount,
- );
- await writeFile(runtimePath, intentMarkdown, "utf-8");
- return {
- intent,
- intentMarkdown,
- plannerInputs: [
- ...Object.values(sourcePaths),
- join(storyDir, "pending_hooks.md"),
- ...(memorySelection.dbPath ? [memorySelection.dbPath] : []),
- ],
- runtimePath,
- };
- }
- private buildStructuredDirectives(input: {
- readonly chapterNumber: number;
- readonly language?: string;
- readonly volumeOutline: string;
- readonly outlineNode: string | undefined;
- readonly matchedOutlineAnchor: boolean;
- readonly chapterSummaries: string;
- }): Pick<ChapterIntent, "sceneDirective" | "arcDirective" | "moodDirective" | "titleDirective"> {
- const recentSummaries = parseChapterSummariesMarkdown(input.chapterSummaries)
- .filter((summary) => summary.chapter < input.chapterNumber)
- .sort((left, right) => left.chapter - right.chapter)
- .slice(-4);
- const cadence = analyzeChapterCadence({
- language: this.isChineseLanguage(input.language) ? "zh" : "en",
- rows: recentSummaries.map((summary) => ({
- chapter: summary.chapter,
- title: summary.title,
- mood: summary.mood,
- chapterType: summary.chapterType,
- })),
- });
- return {
- arcDirective: this.buildArcDirective(
- input.language,
- input.volumeOutline,
- input.outlineNode,
- input.matchedOutlineAnchor,
- ),
- sceneDirective: this.buildSceneDirective(input.language, cadence),
- moodDirective: this.buildMoodDirective(input.language, cadence),
- titleDirective: this.buildTitleDirective(input.language, cadence),
- };
- }
- private deriveGoal(
- externalContext: string | undefined,
- currentFocus: string,
- authorIntent: string,
- outlineNode: string | undefined,
- chapterNumber: number,
- ): string {
- const first = this.extractFirstDirective(externalContext);
- if (first) return first;
- const localOverride = this.extractLocalOverrideGoal(currentFocus);
- if (localOverride) return localOverride;
- const outline = this.extractFirstDirective(outlineNode);
- if (outline) return outline;
- const focus = this.extractFocusGoal(currentFocus);
- if (focus) return focus;
- const author = this.extractFirstDirective(authorIntent);
- if (author) return author;
- return `Advance chapter ${chapterNumber} with clear narrative focus.`;
- }
- private collectMustKeep(currentState: string, storyBible: string): string[] {
- return this.unique([
- ...this.extractListItems(currentState, 2),
- ...this.extractListItems(storyBible, 2),
- ]).slice(0, 4);
- }
- private collectMustAvoid(currentFocus: string, prohibitions: ReadonlyArray<string>): string[] {
- const avoidSection = this.extractSection(currentFocus, [
- "avoid",
- "must avoid",
- "禁止",
- "避免",
- "避雷",
- ]);
- const focusAvoids = avoidSection
- ? this.extractListItems(avoidSection, 10)
- : currentFocus
- .split("\n")
- .map((line) => line.trim())
- .filter((line) =>
- line.startsWith("-") &&
- /avoid|don't|do not|不要|别|禁止/i.test(line),
- )
- .map((line) => this.cleanListItem(line))
- .filter((line): line is string => Boolean(line));
- return this.unique([...focusAvoids, ...prohibitions]).slice(0, 6);
- }
- private collectStyleEmphasis(authorIntent: string, currentFocus: string): string[] {
- return this.unique([
- ...this.extractFocusStyleItems(currentFocus),
- ...this.extractListItems(authorIntent, 2),
- ]).slice(0, 4);
- }
- private collectConflicts(
- externalContext: string | undefined,
- currentFocus: string,
- outlineNode: string | undefined,
- volumeOutline: string,
- ): ChapterConflict[] {
- const outlineText = outlineNode ?? volumeOutline;
- if (!outlineText || outlineText === "(文件尚未创建)") return [];
- if (externalContext) {
- const indicatesOverride = /ignore|skip|defer|instead|不要|别|先别|暂停/i.test(externalContext);
- if (!indicatesOverride && this.hasKeywordOverlap(externalContext, outlineText)) return [];
- return [
- {
- type: "outline_vs_request",
- resolution: "allow local outline deferral",
- },
- ];
- }
- const localOverride = this.extractLocalOverrideGoal(currentFocus);
- if (!localOverride || !outlineNode) {
- return [];
- }
- return [
- {
- type: "outline_vs_current_focus",
- resolution: "allow explicit current focus override",
- detail: localOverride,
- },
- ];
- }
- private extractFirstDirective(content?: string): string | undefined {
- if (!content) return undefined;
- return content
- .split("\n")
- .map((line) => line.trim())
- .find((line) =>
- line.length > 0
- && !line.startsWith("#")
- && !line.startsWith("-")
- && !this.isTemplatePlaceholder(line),
- );
- }
- private extractListItems(content: string, limit: number): string[] {
- return content
- .split("\n")
- .map((line) => line.trim())
- .filter((line) => line.startsWith("-"))
- .map((line) => this.cleanListItem(line))
- .filter((line): line is string => Boolean(line))
- .slice(0, limit);
- }
- private extractFocusGoal(currentFocus: string): string | undefined {
- const focusSection = this.extractSection(currentFocus, [
- "active focus",
- "focus",
- "当前聚焦",
- "当前焦点",
- "近期聚焦",
- ]) ?? currentFocus;
- const directives = this.extractFocusStyleItems(focusSection, 3);
- if (directives.length === 0) {
- return this.extractFirstDirective(focusSection);
- }
- return directives.join(this.containsChinese(focusSection) ? ";" : "; ");
- }
- private extractLocalOverrideGoal(currentFocus: string): string | undefined {
- const overrideSection = this.extractSection(currentFocus, [
- "local override",
- "explicit override",
- "chapter override",
- "local task override",
- "局部覆盖",
- "本章覆盖",
- "临时覆盖",
- "当前覆盖",
- ]);
- if (!overrideSection) {
- return undefined;
- }
- const directives = this.extractListItems(overrideSection, 3);
- if (directives.length > 0) {
- return directives.join(this.containsChinese(overrideSection) ? ";" : "; ");
- }
- return this.extractFirstDirective(overrideSection);
- }
- private extractFocusStyleItems(currentFocus: string, limit = 3): string[] {
- const focusSection = this.extractSection(currentFocus, [
- "active focus",
- "focus",
- "当前聚焦",
- "当前焦点",
- "近期聚焦",
- ]) ?? currentFocus;
- return this.extractListItems(focusSection, limit);
- }
- private buildArcDirective(
- language: string | undefined,
- volumeOutline: string,
- outlineNode: string | undefined,
- matchedOutlineAnchor: boolean,
- ): string | undefined {
- if (matchedOutlineAnchor || !outlineNode || volumeOutline === "(文件尚未创建)") {
- return undefined;
- }
- return this.isChineseLanguage(language)
- ? "不要继续依赖卷纲的 fallback 指令,必须把本章推进到新的弧线节点或地点变化。"
- : "Do not keep leaning on the outline fallback. Force this chapter toward a fresh arc beat or location change.";
- }
- private buildSceneDirective(
- language: string | undefined,
- cadence: ReturnType<typeof analyzeChapterCadence>,
- ): string | undefined {
- if (cadence.scenePressure?.pressure !== "high") {
- return undefined;
- }
- const repeatedType = cadence.scenePressure.repeatedType;
- return this.isChineseLanguage(language)
- ? `最近章节连续停留在“${repeatedType}”,本章必须更换场景容器、地点或行动方式。`
- : `Recent chapters are stuck in repeated ${repeatedType} beats. Change the scene container, location, or action pattern this chapter.`;
- }
- private buildMoodDirective(
- language: string | undefined,
- cadence: ReturnType<typeof analyzeChapterCadence>,
- ): string | undefined {
- if (cadence.moodPressure?.pressure !== "high") {
- return undefined;
- }
- const moods = cadence.moodPressure.recentMoods;
- return this.isChineseLanguage(language)
- ? `最近${moods.length}章情绪持续高压(${moods.slice(0, 3).join("、")}),本章必须降调——安排日常/喘息/温情/幽默场景,让读者呼吸。`
- : `The last ${moods.length} chapters have been relentlessly tense (${moods.slice(0, 3).join(", ")}). This chapter must downshift — write a quieter scene with warmth, humor, or breathing room.`;
- }
- private buildTitleDirective(
- language: string | undefined,
- cadence: ReturnType<typeof analyzeChapterCadence>,
- ): string | undefined {
- if (cadence.titlePressure?.pressure !== "high") {
- return undefined;
- }
- const repeatedToken = cadence.titlePressure.repeatedToken;
- return this.isChineseLanguage(language)
- ? `标题不要再围绕“${repeatedToken}”重复命名,换一个新的意象或动作焦点。`
- : `Avoid another ${repeatedToken}-centric title. Pick a new image or action focus for this chapter title.`;
- }
- private renderHookBudget(activeCount: number, language: "zh" | "en"): string {
- const cap = 12;
- if (activeCount < 10) {
- return language === "en"
- ? `### Hook Budget\n- ${activeCount} active hooks (capacity: ${cap})`
- : `### 伏笔预算\n- 当前 ${activeCount} 条活跃伏笔(容量:${cap})`;
- }
- const remaining = Math.max(0, cap - activeCount);
- return language === "en"
- ? `### Hook Budget\n- ${activeCount} active hooks — approaching capacity (${cap}). Only ${remaining} new hook(s) allowed. Prioritize resolving existing debt over opening new threads.`
- : `### 伏笔预算\n- 当前 ${activeCount} 条活跃伏笔——接近容量上限(${cap})。仅剩 ${remaining} 个新坑位。优先回收旧债,不要轻易开新线。`;
- }
- private extractSection(content: string, headings: ReadonlyArray<string>): string | undefined {
- const targets = headings.map((heading) => this.normalizeHeading(heading));
- const lines = content.split("\n");
- let buffer: string[] | null = null;
- let sectionLevel = 0;
- for (const line of lines) {
- const headingMatch = line.match(/^(#+)\s*(.+?)\s*$/);
- if (headingMatch) {
- const level = headingMatch[1]!.length;
- const heading = this.normalizeHeading(headingMatch[2]!);
- if (buffer && level <= sectionLevel) {
- break;
- }
- if (targets.includes(heading)) {
- buffer = [];
- sectionLevel = level;
- continue;
- }
- }
- if (buffer) {
- buffer.push(line);
- }
- }
- const section = buffer?.join("\n").trim();
- return section && section.length > 0 ? section : undefined;
- }
- private normalizeHeading(heading: string): string {
- return heading
- .toLowerCase()
- .replace(/[*_`:#]/g, "")
- .replace(/\s+/g, " ")
- .trim();
- }
- private cleanListItem(line: string): string | undefined {
- const cleaned = line.replace(/^-\s*/, "").trim();
- if (cleaned.length === 0) return undefined;
- if (/^[-|]+$/.test(cleaned)) return undefined;
- if (this.isTemplatePlaceholder(cleaned)) return undefined;
- return cleaned;
- }
- private isTemplatePlaceholder(line: string): boolean {
- const normalized = line.trim();
- if (!normalized) return false;
- return (
- /^\((describe|briefly describe|write)\b[\s\S]*\)$/i.test(normalized)
- || /^((?:在这里描述|描述|填写|写下)[\s\S]*)$/u.test(normalized)
- );
- }
- private containsChinese(content: string): boolean {
- return /[\u4e00-\u9fff]/.test(content);
- }
- private findOutlineNode(volumeOutline: string, chapterNumber: number): string | undefined {
- const lines = volumeOutline.split("\n").map((line) => line.trim()).filter(Boolean);
- for (let index = 0; index < lines.length; index += 1) {
- const line = lines[index]!;
- const match = this.matchExactOutlineLine(line, chapterNumber);
- if (!match) continue;
- const inlineContent = this.cleanOutlineContent(match[1]);
- if (inlineContent) {
- return inlineContent;
- }
- const nextContent = this.findNextOutlineContent(lines, index + 1);
- if (nextContent) {
- return nextContent;
- }
- }
- for (let index = 0; index < lines.length; index += 1) {
- const line = lines[index]!;
- const match = this.matchRangeOutlineLine(line, chapterNumber);
- if (!match) continue;
- const inlineContent = this.cleanOutlineContent(match[3]);
- if (inlineContent) {
- return inlineContent;
- }
- const nextContent = this.findNextOutlineContent(lines, index + 1);
- if (nextContent) {
- return nextContent;
- }
- }
- for (let index = 0; index < lines.length; index += 1) {
- const line = lines[index]!;
- if (!this.isOutlineAnchorLine(line)) continue;
- const exactMatch = this.matchAnyExactOutlineLine(line);
- if (exactMatch) {
- const inlineContent = this.cleanOutlineContent(exactMatch[1]);
- if (inlineContent) {
- return inlineContent;
- }
- }
- const rangeMatch = this.matchAnyRangeOutlineLine(line);
- if (rangeMatch) {
- const inlineContent = this.cleanOutlineContent(rangeMatch[3]);
- if (inlineContent) {
- return inlineContent;
- }
- }
- const nextContent = this.findNextOutlineContent(lines, index + 1);
- if (nextContent) {
- return nextContent;
- }
- break;
- }
- return this.extractFirstDirective(volumeOutline);
- }
- private cleanOutlineContent(content?: string): string | undefined {
- const cleaned = content?.trim();
- if (!cleaned) return undefined;
- if (/^[*_`~::-]+$/.test(cleaned)) return undefined;
- return cleaned;
- }
- private findNextOutlineContent(lines: ReadonlyArray<string>, startIndex: number): string | undefined {
- for (let index = startIndex; index < lines.length; index += 1) {
- const line = lines[index]!;
- if (!line) {
- continue;
- }
- if (this.isOutlineAnchorLine(line)) {
- return undefined;
- }
- if (line.startsWith("#")) {
- continue;
- }
- const cleaned = this.cleanOutlineContent(line);
- if (cleaned) {
- return cleaned;
- }
- }
- return undefined;
- }
- private hasMatchedOutlineAnchor(volumeOutline: string, chapterNumber: number): boolean {
- const lines = volumeOutline.split("\n").map((line) => line.trim()).filter(Boolean);
- return lines.some((line) =>
- this.matchExactOutlineLine(line, chapterNumber) !== undefined
- || this.matchRangeOutlineLine(line, chapterNumber) !== undefined,
- );
- }
- private matchExactOutlineLine(line: string, chapterNumber: number): RegExpMatchArray | undefined {
- const patterns = [
- new RegExp(`^(?:#+\\s*)?(?:[-*]\\s+)?(?:\\*\\*)?Chapter\\s*${chapterNumber}(?!\\d|\\s*[-~–—]\\s*\\d)(?:[::-])?(?:\\*\\*)?\\s*(.*)$`, "i"),
- new RegExp(`^(?:#+\\s*)?(?:[-*]\\s+)?(?:\\*\\*)?第\\s*${chapterNumber}\\s*章(?!\\d|\\s*[-~–—]\\s*\\d)(?:[::-])?(?:\\*\\*)?\\s*(.*)$`),
- ];
- return patterns
- .map((pattern) => line.match(pattern))
- .find((result): result is RegExpMatchArray => Boolean(result));
- }
- private matchAnyExactOutlineLine(line: string): RegExpMatchArray | undefined {
- const patterns = [
- /^(?:#+\s*)?(?:[-*]\s+)?(?:\*\*)?Chapter\s*\d+(?!\s*[-~–—]\s*\d)(?:[::-])?(?:\*\*)?\s*(.*)$/i,
- /^(?:#+\s*)?(?:[-*]\s+)?(?:\*\*)?第\s*\d+\s*章(?!\s*[-~–—]\s*\d)(?:[::-])?(?:\*\*)?\s*(.*)$/i,
- ];
- return patterns
- .map((pattern) => line.match(pattern))
- .find((result): result is RegExpMatchArray => Boolean(result));
- }
- private matchRangeOutlineLine(line: string, chapterNumber: number): RegExpMatchArray | undefined {
- const match = this.matchAnyRangeOutlineLine(line);
- if (!match) return undefined;
- if (this.isChapterWithinRange(match[1], match[2], chapterNumber)) {
- return match;
- }
- return undefined;
- }
- private matchAnyRangeOutlineLine(line: string): RegExpMatchArray | undefined {
- const patterns = [
- /^(?:#+\s*)?(?:[-*]\s+)?(?:\*\*)?Chapter\s*(\d+)\s*[-~–—]\s*(\d+)\b(?:[::-])?(?:\*\*)?\s*(.*)$/i,
- /^(?:#+\s*)?(?:[-*]\s+)?(?:\*\*)?第\s*(\d+)\s*[-~–—]\s*(\d+)\s*章(?:[::-])?(?:\*\*)?\s*(.*)$/i,
- ];
- return patterns
- .map((pattern) => line.match(pattern))
- .find((result): result is RegExpMatchArray => Boolean(result));
- }
- private isOutlineAnchorLine(line: string): boolean {
- return this.matchAnyExactOutlineLine(line) !== undefined
- || this.matchAnyRangeOutlineLine(line) !== undefined;
- }
- private isChapterWithinRange(startText: string | undefined, endText: string | undefined, chapterNumber: number): boolean {
- const start = Number.parseInt(startText ?? "", 10);
- const end = Number.parseInt(endText ?? "", 10);
- if (!Number.isFinite(start) || !Number.isFinite(end)) return false;
- const lower = Math.min(start, end);
- const upper = Math.max(start, end);
- return chapterNumber >= lower && chapterNumber <= upper;
- }
- private hasKeywordOverlap(left: string, right: string): boolean {
- const keywords = this.extractKeywords(left);
- if (keywords.length === 0) return false;
- const normalizedRight = right.toLowerCase();
- return keywords.some((keyword) => normalizedRight.includes(keyword.toLowerCase()));
- }
- private extractKeywords(content: string): string[] {
- const english = content.match(/[a-z]{4,}/gi) ?? [];
- const chinese = content.match(/[\u4e00-\u9fff]{2,4}/g) ?? [];
- return this.unique([...english, ...chinese]);
- }
- private renderIntentMarkdown(
- intent: ChapterIntent,
- language: "zh" | "en",
- pendingHooks: string,
- chapterSummaries: string,
- activeHookCount: number,
- ): string {
- const conflictLines = intent.conflicts.length > 0
- ? intent.conflicts.map((conflict) => `- ${conflict.type}: ${conflict.resolution}`).join("\n")
- : "- none";
- const mustKeep = intent.mustKeep.length > 0
- ? intent.mustKeep.map((item) => `- ${item}`).join("\n")
- : "- none";
- const mustAvoid = intent.mustAvoid.length > 0
- ? intent.mustAvoid.map((item) => `- ${item}`).join("\n")
- : "- none";
- const styleEmphasis = intent.styleEmphasis.length > 0
- ? intent.styleEmphasis.map((item) => `- ${item}`).join("\n")
- : "- none";
- const directives = [
- intent.arcDirective ? `- arc: ${intent.arcDirective}` : undefined,
- intent.sceneDirective ? `- scene: ${intent.sceneDirective}` : undefined,
- intent.moodDirective ? `- mood: ${intent.moodDirective}` : undefined,
- intent.titleDirective ? `- title: ${intent.titleDirective}` : undefined,
- ].filter(Boolean).join("\n") || "- none";
- const hookAgenda = [
- "### Must Advance",
- intent.hookAgenda.mustAdvance.length > 0
- ? intent.hookAgenda.mustAdvance.map((item) => `- ${item}`).join("\n")
- : "- none",
- "",
- "### Eligible Resolve",
- intent.hookAgenda.eligibleResolve.length > 0
- ? intent.hookAgenda.eligibleResolve.map((item) => `- ${item}`).join("\n")
- : "- none",
- "",
- "### Stale Debt",
- intent.hookAgenda.staleDebt.length > 0
- ? intent.hookAgenda.staleDebt.map((item) => `- ${item}`).join("\n")
- : "- none",
- "",
- "### Avoid New Hook Families",
- intent.hookAgenda.avoidNewHookFamilies.length > 0
- ? intent.hookAgenda.avoidNewHookFamilies.map((item) => `- ${item}`).join("\n")
- : "- none",
- "",
- this.renderHookBudget(activeHookCount, language),
- ].join("\n");
- return [
- "# Chapter Intent",
- "",
- "## Goal",
- intent.goal,
- "",
- "## Outline Node",
- intent.outlineNode ?? "(not found)",
- "",
- "## Must Keep",
- mustKeep,
- "",
- "## Must Avoid",
- mustAvoid,
- "",
- "## Style Emphasis",
- styleEmphasis,
- "",
- "## Structured Directives",
- directives,
- "",
- "## Hook Agenda",
- hookAgenda,
- "",
- "## Conflicts",
- conflictLines,
- "",
- "## Pending Hooks Snapshot",
- pendingHooks,
- "",
- "## Chapter Summaries Snapshot",
- chapterSummaries,
- "",
- ].join("\n");
- }
- private unique(values: ReadonlyArray<string>): string[] {
- return [...new Set(values.map((value) => value.trim()).filter(Boolean))];
- }
- private isChineseLanguage(language: string | undefined): boolean {
- return (language ?? "zh").toLowerCase().startsWith("zh");
- }
- private async readFileOrDefault(path: string): Promise<string> {
- try {
- return await readFile(path, "utf-8");
- } catch {
- return "(文件尚未创建)";
- }
- }
- }
|