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:
62
apps/web/lib/templates/templates.test.ts
Normal file
62
apps/web/lib/templates/templates.test.ts
Normal 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();
|
||||
});
|
||||
});
|
||||
39
apps/web/lib/templates/templates.ts
Normal file
39
apps/web/lib/templates/templates.ts
Normal 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,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user