feat(web): F1/F2/F3 前端——拆书入库为规则 / 续写式链选择 / 模板库

- gen:api 纳入 /templates、chain continue_volume、teardown rules ingest 端点
- F1:GeneratorRunner 识别拆书为单对象入库(整 preview 作一条 teardown),
  ingestTable(BookTeardownResult)=rules,buildIngestRequest 组装 teardown 体,
  按 isSingleObjectIngest 隐藏勾选框、改文案「入库为规则」
- F2:ChainStarter 加链类型单选(draft_volume 从头写 / continue_volume 续写),
  chain_key 作 path 参传给 run(CHAIN_KINDS 对齐后端 SUPPORTED_CHAINS)
- F3:模板库页 app/templates + 全局 nav 入口 + TemplatesManager(列/建/删,乐观更新回滚);
  GeneratorRunner 加 TemplateFiller「从模板填入」(body 填进 brief/text,纯前端)
- vitest 覆盖 templates/ingest teardown/templateFillTarget/CHAIN_KINDS 纯逻辑
This commit is contained in:
Yaojia Wang
2026-06-23 20:29:48 +02:00
parent 5d8e619408
commit 1a402f5ccc
18 changed files with 869 additions and 19 deletions

View File

@@ -0,0 +1,62 @@
import { describe, expect, it } from "vitest";
import {
buildTemplateCreateRequest,
EMPTY_TEMPLATE_DRAFT,
isTemplateDraftValid,
type TemplateDraft,
} from "./templates";
const draft = (over: Partial<TemplateDraft>): TemplateDraft => ({
...EMPTY_TEMPLATE_DRAFT,
...over,
});
describe("isTemplateDraftValid", () => {
it("returns true when title and body are both non-empty", () => {
expect(isTemplateDraftValid(draft({ title: "标题", body: "正文" }))).toBe(
true,
);
});
it("returns false when title is blank", () => {
expect(isTemplateDraftValid(draft({ title: " ", body: "正文" }))).toBe(
false,
);
});
it("returns false when body is blank", () => {
expect(isTemplateDraftValid(draft({ title: "标题", body: "" }))).toBe(false);
});
it("returns false for the empty draft", () => {
expect(isTemplateDraftValid(EMPTY_TEMPLATE_DRAFT)).toBe(false);
});
});
describe("buildTemplateCreateRequest", () => {
it("trims title and body and includes optional fields", () => {
const body = buildTemplateCreateRequest(
draft({
title: " 脑洞模板 ",
body: " 写一个穿越故事 ",
category: " 脑洞 ",
toolKey: " idea ",
}),
);
expect(body).toEqual({
title: "脑洞模板",
body: "写一个穿越故事",
category: "脑洞",
tool_key: "idea",
});
});
it("nulls out blank category and tool_key", () => {
const body = buildTemplateCreateRequest(
draft({ title: "t", body: "b", category: " ", toolKey: "" }),
);
expect(body.category).toBeNull();
expect(body.tool_key).toBeNull();
});
});

View File

@@ -0,0 +1,39 @@
// 提示词/模板库前端纯逻辑F3新建表单值校验 + 请求体组装。
// 纯逻辑(无 React / 无 fetch便于 node 环境单测。后端 title/body 非空校验为权威422 兜底),
// 前端先拦一道给即时反馈(守边界:不信输入,提交前 trim 判空)。
import type { TemplateCreateRequest } from "@/lib/api/types";
// 新建模板的草稿表单值(受控输入原始值)。
export interface TemplateDraft {
title: string;
body: string;
category: string;
toolKey: string;
}
export const EMPTY_TEMPLATE_DRAFT: TemplateDraft = {
title: "",
body: "",
category: "",
toolKey: "",
};
// 草稿是否可提交title/body 去空白后非空)。
export function isTemplateDraftValid(draft: TemplateDraft): boolean {
return draft.title.trim().length > 0 && draft.body.trim().length > 0;
}
// 草稿 → 创建请求体trim title/body空 category/toolKey 省略为 null后端可空
export function buildTemplateCreateRequest(
draft: TemplateDraft,
): TemplateCreateRequest {
const category = draft.category.trim();
const toolKey = draft.toolKey.trim();
return {
title: draft.title.trim(),
body: draft.body.trim(),
category: category.length > 0 ? category : null,
tool_key: toolKey.length > 0 ? toolKey : null,
};
}