-
+
);
}
-export function ChapterListContent({ currentChapterNo }: ChapterListProps) {
+export function ChapterListContent({
+ projectId,
+ chapters,
+ currentChapterNo,
+}: ChapterListProps) {
+ const entries = buildChapterEntries(chapters, currentChapterNo);
+ const hasOutline = chapters.length > 0;
return (
<>
目录
- -
-
- ●第 {currentChapterNo} 章
-
-
+ {entries.map((entry) => {
+ const active = entry.no === currentChapterNo;
+ return (
+ -
+
+
+ {active ? ● : null}第{" "}
+ {entry.no} 章
+
+ {entry.title ? (
+
+ {entry.title}
+
+ ) : null}
+
+
+ );
+ })}
-
- 多卷多章目录将在大纲模块开放(后续里程碑)。
-
+ {!hasOutline ? (
+
+ 还没有章节目录。
+
+ 去「大纲」生成
+
+ 。
+
+ ) : null}
>
);
}
diff --git a/apps/web/components/workbench/Workbench.tsx b/apps/web/components/workbench/Workbench.tsx
index de854ae..8f745d6 100644
--- a/apps/web/components/workbench/Workbench.tsx
+++ b/apps/web/components/workbench/Workbench.tsx
@@ -9,7 +9,10 @@ import type { ProjectResponse } from "@/lib/api/types";
import { friendlyError } from "@/lib/errors/messages";
import { useAutosave } from "@/lib/autosave/useAutosave";
import { useDraftStream } from "@/lib/stream/useDraftStream";
-import { WORKBENCH_CHAPTER_NO } from "@/lib/workbench/chapter";
+import {
+ WORKBENCH_CHAPTER_NO,
+ type ChapterEntry,
+} from "@/lib/workbench/chapter";
import { composeDirective, STYLE_PRESETS } from "@/lib/workbench/directive";
import { ChapterList, ChapterListContent } from "./ChapterList";
import { ChapterAssistant, AssistantContent } from "./ChapterAssistant";
@@ -17,8 +20,12 @@ import { Editor } from "./Editor";
interface WorkbenchProps {
project: ProjectResponse;
+ // 当前章号(来自写作路由 `?chapter=N`);缺省回退第 1 章。
+ chapterNo?: number;
// RSC 种入的已存草稿正文(GET .../draft);无草稿(404)→ ""(空编辑器)。
initialText?: string;
+ // 大纲章节目录(GET .../outline);无大纲 → 空数组(目录回退到仅当前章 + 去大纲提示)。
+ chapters?: ChapterEntry[];
}
// 移动端右栏(目录/助手)经抽屉触达;桌面用三栏栅格。
@@ -27,8 +34,12 @@ type MobilePanel = "chapters" | "assistant" | null;
// M1:单章工作台。章节列表为占位(无章节列表端点);默认第 1 章。
// WORKBENCH_CHAPTER_NO 移到 lib/workbench/chapter.ts(非客户端模块),供 Server Component 安全导入。
-export function Workbench({ project, initialText = "" }: WorkbenchProps) {
- const chapterNo = WORKBENCH_CHAPTER_NO;
+export function Workbench({
+ project,
+ chapterNo = WORKBENCH_CHAPTER_NO,
+ initialText = "",
+ chapters = [],
+}: WorkbenchProps) {
// 用已存草稿作初值;流式开始后由下方 effect 覆盖(流不会被初值反扑——
// stream.state.text 起始为空,仅当 streaming/done/aborted 且变化才写回)。
const [text, setText] = useState(initialText);
@@ -85,7 +96,11 @@ export function Workbench({ project, initialText = "" }: WorkbenchProps) {
activeNav="write"
>
-
+
-
+
{
+ it("prefers the suggestion's own code when present", () => {
+ expect(suggestForeshadowCode("F-007", 3)).toBe("F-007");
+ expect(suggestForeshadowCode(" F-008 ", 3)).toBe("F-008");
+ });
+
+ it("falls back to FS-<2-digit index> when code is empty/null", () => {
+ expect(suggestForeshadowCode(null, 0)).toBe("FS-01");
+ expect(suggestForeshadowCode("", 4)).toBe("FS-05");
+ expect(suggestForeshadowCode(" ", 11)).toBe("FS-12");
+ });
+});
+
const view = (over: Partial): ForeshadowView => ({
code: "F-001",
title: "线索",
diff --git a/apps/web/lib/foreshadow/board.ts b/apps/web/lib/foreshadow/board.ts
index 9bf3a5d..9c9f579 100644
--- a/apps/web/lib/foreshadow/board.ts
+++ b/apps/web/lib/foreshadow/board.ts
@@ -59,6 +59,17 @@ export function isWindowApproaching(
return currentChapter >= from && currentChapter <= to;
}
+// 为「新埋待确认」建议预填一个建议代号:优先用建议自带 code,否则 FS-<两位序号>。
+// 纯函数(可单测);作者可在登记表单里改。
+export function suggestForeshadowCode(
+ code: string | null | undefined,
+ index: number,
+): string {
+ const trimmed = code?.trim();
+ if (trimmed) return trimmed;
+ return `FS-${String(index + 1).padStart(2, "0")}`;
+}
+
export interface RegisterInput {
projectId: string;
code: string;
diff --git a/apps/web/lib/generation/cards.test.ts b/apps/web/lib/generation/cards.test.ts
index c39544a..c570dd5 100644
--- a/apps/web/lib/generation/cards.test.ts
+++ b/apps/web/lib/generation/cards.test.ts
@@ -105,8 +105,22 @@ describe("extractIngestConflicts", () => {
expect(out).toEqual({
conflictCount: 2,
conflicts: [
- { type: "性格漂移", where: "第3章", refs: ["甲"], suggestion: "改" },
- { type: "未分类", where: "", refs: [], suggestion: "" },
+ {
+ type: "性格漂移",
+ where: "第3章",
+ refs: ["甲"],
+ suggestion: "改",
+ original: null,
+ replacement: null,
+ },
+ {
+ type: "未分类",
+ where: "",
+ refs: [],
+ suggestion: "",
+ original: null,
+ replacement: null,
+ },
],
});
});
diff --git a/apps/web/lib/generation/cards.ts b/apps/web/lib/generation/cards.ts
index 547fdbc..cec3b2e 100644
Binary files a/apps/web/lib/generation/cards.ts and b/apps/web/lib/generation/cards.ts differ
diff --git a/apps/web/lib/review/applyFix.test.ts b/apps/web/lib/review/applyFix.test.ts
new file mode 100644
index 0000000..4c70f1e
--- /dev/null
+++ b/apps/web/lib/review/applyFix.test.ts
@@ -0,0 +1,44 @@
+import { describe, expect, it } from "vitest";
+
+import { applyConflictFix, hasApplicableFix } from "./applyFix";
+
+describe("hasApplicableFix", () => {
+ it("requires non-empty original and string replacement", () => {
+ expect(hasApplicableFix("迈巴赫", "奔驰")).toBe(true);
+ expect(hasApplicableFix("删我", "")).toBe(true); // 空串=删除,仍可应用
+ expect(hasApplicableFix(null, "奔驰")).toBe(false);
+ expect(hasApplicableFix("", "奔驰")).toBe(false);
+ expect(hasApplicableFix("迈巴赫", null)).toBe(false);
+ expect(hasApplicableFix(undefined, undefined)).toBe(false);
+ });
+});
+
+describe("applyConflictFix", () => {
+ it("replaces the first occurrence of original with replacement", () => {
+ const out = applyConflictFix(
+ "他乘坐迈巴赫扬长而去,迈巴赫的尾灯。",
+ "迈巴赫",
+ "奔驰",
+ );
+ expect(out.status).toBe("applied");
+ expect(out.text).toBe("他乘坐奔驰扬长而去,迈巴赫的尾灯。");
+ });
+
+ it("returns not-found and leaves text untouched when original is absent (drift)", () => {
+ const out = applyConflictFix("作者已经手改过的终稿。", "迈巴赫", "奔驰");
+ expect(out.status).toBe("not-found");
+ expect(out.text).toBe("作者已经手改过的终稿。");
+ });
+
+ it("returns no-patch when conflict carries no applicable patch", () => {
+ const out = applyConflictFix("正文", null, null);
+ expect(out.status).toBe("no-patch");
+ expect(out.text).toBe("正文");
+ });
+
+ it("does not mutate the input string", () => {
+ const original = "迈巴赫尾灯";
+ applyConflictFix(original, "迈巴赫", "奔驰");
+ expect(original).toBe("迈巴赫尾灯");
+ });
+});
diff --git a/apps/web/lib/review/applyFix.ts b/apps/web/lib/review/applyFix.ts
new file mode 100644
index 0000000..dc8a2a3
--- /dev/null
+++ b/apps/web/lib/review/applyFix.ts
@@ -0,0 +1,42 @@
+// 一键采纳改法:把审稿提议的补丁(original→replacement)应用到终稿。
+// 纯逻辑(可单测,不依赖 DOM)。粒度=最小句段;找不到原文则不动正文(作者手改)。
+
+export type FixOutcome =
+ | { status: "applied"; text: string } // 命中原文并替换:text=改后终稿
+ | { status: "not-found"; text: string } // 有补丁但终稿里找不到原文(作者已改稿/漂移):text 原样
+ | { status: "no-patch"; text: string }; // 该冲突无可自动应用的补丁:text 原样
+
+// 是否带可应用补丁:original 非空 + replacement 为字符串(容许空串=删除)。
+export function hasApplicableFix(
+ original: string | null | undefined,
+ replacement: string | null | undefined,
+): boolean {
+ return (
+ typeof original === "string" &&
+ original.length > 0 &&
+ typeof replacement === "string"
+ );
+}
+
+// 把首个匹配的 original 替换为 replacement(不可变:返回新串,不改入参)。
+// 无补丁 → no-patch;有补丁但未命中 → not-found(终稿不变);命中 → applied。
+export function applyConflictFix(
+ text: string,
+ original: string | null | undefined,
+ replacement: string | null | undefined,
+): FixOutcome {
+ if (!hasApplicableFix(original, replacement)) {
+ return { status: "no-patch", text };
+ }
+ // hasApplicableFix 已收窄为 string。
+ const from = original as string;
+ const to = replacement as string;
+ const idx = text.indexOf(from);
+ if (idx === -1) {
+ return { status: "not-found", text };
+ }
+ return {
+ status: "applied",
+ text: text.slice(0, idx) + to + text.slice(idx + from.length),
+ };
+}
diff --git a/apps/web/lib/review/grouping.test.ts b/apps/web/lib/review/grouping.test.ts
index 8575d4f..6c3135e 100644
--- a/apps/web/lib/review/grouping.test.ts
+++ b/apps/web/lib/review/grouping.test.ts
@@ -9,7 +9,14 @@ import {
import type { ReviewConflict } from "./sse";
function conflict(type: string, suggestion = "x"): ReviewConflict {
- return { type, where: "", refs: [], suggestion };
+ return {
+ type,
+ where: "",
+ refs: [],
+ suggestion,
+ original: null,
+ replacement: null,
+ };
}
describe("groupConflicts", () => {
diff --git a/apps/web/lib/review/history.test.ts b/apps/web/lib/review/history.test.ts
index c96f24b..6e36760 100644
--- a/apps/web/lib/review/history.test.ts
+++ b/apps/web/lib/review/history.test.ts
@@ -31,8 +31,22 @@ describe("normalizeConflicts", () => {
]),
);
expect(out).toEqual([
- { type: "设定违例", where: "第4段", refs: ["第30章"], suggestion: "统一血脉" },
- { type: "时间线倒错", where: "第8段", refs: [], suggestion: "调整" },
+ {
+ type: "设定违例",
+ where: "第4段",
+ refs: ["第30章"],
+ suggestion: "统一血脉",
+ original: null,
+ replacement: null,
+ },
+ {
+ type: "时间线倒错",
+ where: "第8段",
+ refs: [],
+ suggestion: "调整",
+ original: null,
+ replacement: null,
+ },
]);
});
@@ -41,7 +55,14 @@ describe("normalizeConflicts", () => {
item([{ type: "", where: "", suggestion: "" }]),
);
expect(out).toEqual([
- { type: "未分类", where: "", refs: [], suggestion: "" },
+ {
+ type: "未分类",
+ where: "",
+ refs: [],
+ suggestion: "",
+ original: null,
+ replacement: null,
+ },
]);
});
diff --git a/apps/web/lib/review/history.ts b/apps/web/lib/review/history.ts
index 099dac1..dbe86c5 100644
--- a/apps/web/lib/review/history.ts
+++ b/apps/web/lib/review/history.ts
@@ -34,6 +34,9 @@ export function normalizeConflicts(
where: c.where,
refs: c.refs ?? [],
suggestion: c.suggestion,
+ // 一键采纳补丁对:留痕里带则透传,供「采纳改法」find-replace 进终稿(缺省 null)。
+ original: c.original ?? null,
+ replacement: c.replacement ?? null,
}));
}
diff --git a/apps/web/lib/review/locate.test.ts b/apps/web/lib/review/locate.test.ts
new file mode 100644
index 0000000..9370e3e
--- /dev/null
+++ b/apps/web/lib/review/locate.test.ts
@@ -0,0 +1,54 @@
+import { describe, expect, it } from "vitest";
+
+import { extractDraftQuotes, locateInDraft } from "./locate";
+
+const TEXT = "第一段没问题。\n\n他乘坐迈巴赫扬长而去,留下一地尘土。\n\n结尾。";
+
+describe("locateInDraft", () => {
+ it("locates the exact original fragment", () => {
+ const loc = locateInDraft(TEXT, { original: "迈巴赫", where: "第2段" });
+ expect(loc).not.toBeNull();
+ expect(TEXT.slice(loc!.start, loc!.end)).toBe("迈巴赫");
+ });
+
+ it("falls back to the paragraph snippet when no original", () => {
+ const loc = locateInDraft(TEXT, { original: null, where: "第2段" });
+ expect(loc).not.toBeNull();
+ expect(TEXT.slice(loc!.start, loc!.end)).toBe(
+ "他乘坐迈巴赫扬长而去,留下一地尘土。",
+ );
+ });
+
+ it("returns null when original is absent from text and no paragraph hint", () => {
+ expect(
+ locateInDraft(TEXT, { original: "保时捷", where: "无段号" }),
+ ).toBeNull();
+ });
+
+ it("returns null when the snippet paragraph is out of range", () => {
+ expect(locateInDraft(TEXT, { original: null, where: "第9段" })).toBeNull();
+ });
+
+ it("locates via quoted draft text inside where (no original, no 段号)", () => {
+ // 真实形态:where = 草稿中写"<草稿原文>",引号内即正文精确子串。
+ const loc = locateInDraft(TEXT, {
+ original: null,
+ where: '草稿中写"迈巴赫扬长而去"',
+ });
+ expect(loc).not.toBeNull();
+ expect(TEXT.slice(loc!.start, loc!.end)).toBe("迈巴赫扬长而去");
+ });
+});
+
+describe("extractDraftQuotes", () => {
+ it("extracts quoted spans (multiple quote styles) longest-first, deduped", () => {
+ expect(extractDraftQuotes('草稿中写"长一些的原文",又如「短的」')).toEqual([
+ "长一些的原文",
+ "短的",
+ ]);
+ });
+
+ it("returns empty when there are no quotes", () => {
+ expect(extractDraftQuotes("第3段,主角忽然示弱")).toEqual([]);
+ });
+});
diff --git a/apps/web/lib/review/locate.ts b/apps/web/lib/review/locate.ts
new file mode 100644
index 0000000..06ac3d6
--- /dev/null
+++ b/apps/web/lib/review/locate.ts
@@ -0,0 +1,66 @@
+// 在终稿里定位某条冲突对应的正文区间(供编辑器 setSelectionRange 高亮)。
+// 纯逻辑(可单测,不依赖 DOM)。定位优先级:精确 original → where 引号内原文 → 段级 snippet。
+
+import type { ReviewConflict } from "./sse";
+import { conflictSnippet } from "./snippet";
+
+export interface DraftRange {
+ start: number;
+ end: number;
+}
+
+// 抽取一段文本里所有成对引号内的内容(「」/“”/‘’/ 直双引号 / 直单引号)。
+// where 多形如 草稿中写"<草稿原文>" —— 引号里就是正文精确子串,是最可靠的定位锚。
+// 返回去重后按长度降序(长串更具体、误命中概率低)。
+const QUOTE_PATTERNS: RegExp[] = [
+ /「([^」]{2,})」/g,
+ /“([^”]{2,})”/g,
+ /"([^"]{2,})"/g,
+ /‘([^’]{2,})’/g,
+ /'([^']{2,})'/g,
+];
+
+export function extractDraftQuotes(where: string): string[] {
+ const found = new Set();
+ for (const re of QUOTE_PATTERNS) {
+ for (const m of where.matchAll(re)) {
+ const inner = m[1]?.trim();
+ if (inner && inner.length >= 2) found.add(inner);
+ }
+ }
+ return [...found].sort((a, b) => b.length - a.length);
+}
+
+// 返回冲突在 text 中的字符区间;定位不到(无 original/引号/段号,或已被改动)→ null。
+export function locateInDraft(
+ text: string,
+ conflict: Pick,
+): DraftRange | null {
+ // 1) 精确原文片段(审稿提议的 original)逐字匹配——最可靠。
+ if (conflict.original) {
+ const idx = text.indexOf(conflict.original);
+ if (idx !== -1) {
+ return { start: idx, end: idx + conflict.original.length };
+ }
+ }
+ // 2) 从 where 的引号里抽出草稿原文逐字匹配(覆盖 `草稿中写"…"` 这类定位)。
+ for (const quote of extractDraftQuotes(conflict.where)) {
+ const idx = text.indexOf(quote);
+ if (idx !== -1) {
+ return { start: idx, end: idx + quote.length };
+ }
+ }
+ // 3) 回退:按 where 段号取段级预览,定位该段。snippet 超长会带省略号,
+ // 去掉末尾「…」后用其前缀(仍是该段真实前缀)做 indexOf。
+ const snippet = conflictSnippet(text, conflict.where);
+ if (snippet) {
+ const needle = snippet.endsWith("…") ? snippet.slice(0, -1) : snippet;
+ if (needle.length > 0) {
+ const idx = text.indexOf(needle);
+ if (idx !== -1) {
+ return { start: idx, end: idx + needle.length };
+ }
+ }
+ }
+ return null;
+}
diff --git a/apps/web/lib/review/sse.test.ts b/apps/web/lib/review/sse.test.ts
index 64fd444..b148b59 100644
--- a/apps/web/lib/review/sse.test.ts
+++ b/apps/web/lib/review/sse.test.ts
@@ -30,6 +30,8 @@ describe("parseReviewBlock", () => {
where: "第4段",
refs: ["第30章"],
suggestion: "统一血脉设定",
+ original: null,
+ replacement: null,
},
});
});
@@ -108,6 +110,8 @@ describe("parseReviewBlock", () => {
where: "第1段",
refs: ["第3章"],
suggestion: "s",
+ original: null,
+ replacement: null,
},
});
expect(
@@ -142,7 +146,14 @@ describe("ReviewFrameBuffer", () => {
expect(second).toEqual([
{
event: "conflict",
- data: { type: "性格漂移", where: "第2段", refs: [], suggestion: "x" },
+ data: {
+ type: "性格漂移",
+ where: "第2段",
+ refs: [],
+ suggestion: "x",
+ original: null,
+ replacement: null,
+ },
},
]);
});
@@ -165,11 +176,25 @@ describe("reduceReview", () => {
const events: ReviewSseEvent[] = [
{
event: "conflict",
- data: { type: "设定违例", where: "a", refs: [], suggestion: "s1" },
+ data: {
+ type: "设定违例",
+ where: "a",
+ refs: [],
+ suggestion: "s1",
+ original: null,
+ replacement: null,
+ },
},
{
event: "conflict",
- data: { type: "时间线倒错", where: "b", refs: ["第3章"], suggestion: "s2" },
+ data: {
+ type: "时间线倒错",
+ where: "b",
+ refs: ["第3章"],
+ suggestion: "s2",
+ original: null,
+ replacement: null,
+ },
},
];
const state = events.reduce(reduceReview, initialReviewState);
@@ -222,7 +247,14 @@ describe("reduceReview", () => {
let e = reduceReview(initialReviewState, {
event: "conflict",
- data: { type: "能力不符", where: "x", refs: [], suggestion: "y" },
+ data: {
+ type: "能力不符",
+ where: "x",
+ refs: [],
+ suggestion: "y",
+ original: null,
+ replacement: null,
+ },
});
e = reduceReview(e, {
event: "error",
diff --git a/apps/web/lib/review/sse.ts b/apps/web/lib/review/sse.ts
index a083861..e85d4ae 100644
--- a/apps/web/lib/review/sse.ts
+++ b/apps/web/lib/review/sse.ts
@@ -15,6 +15,9 @@ export interface ReviewConflict {
where: string;
refs: string[];
suggestion: string;
+ // 一键采纳补丁对(可缺):可局部修复时审稿提议,前端「采纳改法」据此 find-replace 进终稿。
+ original: string | null;
+ replacement: string | null;
}
export type SectionStatus = "started" | "done" | "incomplete";
@@ -197,6 +200,8 @@ function narrowReviewEvent(event: string, data: unknown): ReviewSseEvent | null
where: data.where,
refs: asStringArray(data.refs),
suggestion: data.suggestion,
+ original: nullableString(data.original),
+ replacement: nullableString(data.replacement),
},
}
: null;
diff --git a/apps/web/lib/workbench/chapter.test.ts b/apps/web/lib/workbench/chapter.test.ts
new file mode 100644
index 0000000..ac4faba
--- /dev/null
+++ b/apps/web/lib/workbench/chapter.test.ts
@@ -0,0 +1,55 @@
+import { describe, expect, it } from "vitest";
+
+import {
+ WORKBENCH_CHAPTER_NO,
+ buildChapterEntries,
+ parseChapterParam,
+} from "./chapter";
+
+describe("parseChapterParam", () => {
+ it("parses a valid positive chapter number", () => {
+ expect(parseChapterParam("2")).toBe(2);
+ expect(parseChapterParam("17")).toBe(17);
+ });
+
+ it("takes the first value when given an array", () => {
+ expect(parseChapterParam(["3", "9"])).toBe(3);
+ });
+
+ it("falls back to chapter 1 for missing/invalid/<1 values", () => {
+ expect(parseChapterParam(undefined)).toBe(WORKBENCH_CHAPTER_NO);
+ expect(parseChapterParam("")).toBe(WORKBENCH_CHAPTER_NO);
+ expect(parseChapterParam("abc")).toBe(WORKBENCH_CHAPTER_NO);
+ expect(parseChapterParam("0")).toBe(WORKBENCH_CHAPTER_NO);
+ expect(parseChapterParam("-4")).toBe(WORKBENCH_CHAPTER_NO);
+ });
+});
+
+describe("buildChapterEntries", () => {
+ it("dedupes by no and sorts ascending", () => {
+ const out = buildChapterEntries(
+ [
+ { no: 3, title: "c3" },
+ { no: 1, title: "c1" },
+ { no: 3, title: "dup" },
+ ],
+ 1,
+ );
+ expect(out.map((e) => e.no)).toEqual([1, 3]);
+ expect(out[1]?.title).toBe("c3"); // first wins on duplicate
+ });
+
+ it("always includes the current chapter even if beyond the outline", () => {
+ const out = buildChapterEntries([{ no: 1 }, { no: 2 }], 5);
+ expect(out.map((e) => e.no)).toEqual([1, 2, 5]);
+ });
+
+ it("returns just the current chapter when the outline is empty", () => {
+ expect(buildChapterEntries([], 1)).toEqual([{ no: 1 }]);
+ });
+
+ it("drops invalid chapter numbers from the outline", () => {
+ const out = buildChapterEntries([{ no: 0 }, { no: -2 }, { no: 2 }], 1);
+ expect(out.map((e) => e.no)).toEqual([1, 2]);
+ });
+});
diff --git a/apps/web/lib/workbench/chapter.ts b/apps/web/lib/workbench/chapter.ts
index dac1260..da3355e 100644
--- a/apps/web/lib/workbench/chapter.ts
+++ b/apps/web/lib/workbench/chapter.ts
@@ -3,3 +3,36 @@
// 若从客户端组件(Workbench.tsx,"use client")导入,Next 会把它替换成客户端引用代理,
// 服务端使用时变成抛错的函数 → draft URL 变成 `/chapters/function(){…}/draft`(422)。
export const WORKBENCH_CHAPTER_NO = 1;
+
+// 从写作路由的 `?chapter=N` 解析章号:取正整数,非法/缺省/<1 → 默认第 1 章。
+// 纯函数(可单测),供 write/page.tsx(Server Component)解析 searchParams。
+export function parseChapterParam(raw: string | string[] | undefined): number {
+ const value = Array.isArray(raw) ? raw[0] : raw;
+ if (value === undefined) return WORKBENCH_CHAPTER_NO;
+ const n = Number.parseInt(value, 10);
+ return Number.isInteger(n) && n >= 1 ? n : WORKBENCH_CHAPTER_NO;
+}
+
+// 目录条目:章号 + 可选短标题(大纲无 title,用首个节拍当摘要)。
+export interface ChapterEntry {
+ no: number;
+ title?: string;
+}
+
+// 合并大纲章节与「当前章」成目录:按章号去重 + 升序;当前章即使超出大纲也必含
+// (否则在写大纲外的新章时会从目录里消失,作者迷路)。纯函数(可单测)。
+export function buildChapterEntries(
+ chapters: readonly ChapterEntry[],
+ currentChapterNo: number,
+): ChapterEntry[] {
+ const byNo = new Map();
+ for (const c of chapters) {
+ if (Number.isInteger(c.no) && c.no >= 1 && !byNo.has(c.no)) {
+ byNo.set(c.no, { no: c.no, title: c.title });
+ }
+ }
+ if (!byNo.has(currentChapterNo)) {
+ byNo.set(currentChapterNo, { no: currentChapterNo });
+ }
+ return [...byNo.values()].sort((a, b) => a.no - b.no);
+}