feat: M4 文风 + M5 生成/多provider/Skill + Kimi Code 订阅接入 + 本地联调修复
M4(文风): style-auditor 双轨(提取指纹/漂移第四审)+ jobs 长任务框架(zombie reaper) + 回炉 refine + GET /style read-back。 M5(生成+扩展): worldbuilder/character-gen(入库 continuity 409 gate + partition_writes 白名单 + schema→JSONB 形变); 网关多 provider 回退链/熔断/能力降级(Anthropic/Gemini 适配器);Skill registry + 表权限沙箱 + 规则; 前端 角色生成器/世界观/Codex/规则页/技能库/⌘K 命令面板。 K1(Kimi Code 订阅接入): OAuth device-flow(kimi-code)+ 静态 Console key(kimi-code-key)两路径; coding 端点 KimiCLI 伪造头(实测 UA allow-list 门禁,缺则 403)+ JSON 模式结构化(thinking ⊥ tool_choice)。 本地联调修复: CORS 中间件;assemble 注入 premise+「写第N章」指令(修空 prompt 400); GET /outline·/draft read-back + 大纲/工作台/审稿页重载;写页 client/server 常量边界 + notFound 健壮化; 字数 toLocaleString locale 水合;审稿页终稿从已存草稿 seed(修 accept 422)。 门禁: backend ruff/mypy(157)/alembic 无漂移/pytest 451 · frontend lint/tsc/vitest/build。 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
199
apps/web/lib/generation/cards.test.ts
Normal file
199
apps/web/lib/generation/cards.test.ts
Normal file
@@ -0,0 +1,199 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import type {
|
||||
CharacterCardView,
|
||||
WorldEntityCardView,
|
||||
} from "@/lib/api/types";
|
||||
import {
|
||||
buildCharacterGenerateRequest,
|
||||
buildCharacterIngestRequest,
|
||||
buildWorldGenerateRequest,
|
||||
clampCharacterCount,
|
||||
errorCode,
|
||||
extractIngestConflicts,
|
||||
generationErrorMessage,
|
||||
MAX_CHARACTER_COUNT,
|
||||
mergeCharacterCards,
|
||||
mergeWorldEntities,
|
||||
MIN_CHARACTER_COUNT,
|
||||
updateCard,
|
||||
worldEntityRules,
|
||||
} from "./cards";
|
||||
|
||||
const card = (over: Partial<CharacterCardView> = {}): CharacterCardView => ({
|
||||
name: "甲",
|
||||
role: "主角",
|
||||
backstory: "出身寒门",
|
||||
arc: "由怯转勇",
|
||||
...over,
|
||||
});
|
||||
|
||||
describe("buildWorldGenerateRequest", () => {
|
||||
it("trims brief", () => {
|
||||
expect(buildWorldGenerateRequest(" 修真世界 ")).toEqual({
|
||||
brief: "修真世界",
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("clampCharacterCount", () => {
|
||||
it("clamps to [MIN, MAX] and rounds", () => {
|
||||
expect(clampCharacterCount(0)).toBe(MIN_CHARACTER_COUNT);
|
||||
expect(clampCharacterCount(99)).toBe(MAX_CHARACTER_COUNT);
|
||||
expect(clampCharacterCount(3.6)).toBe(4);
|
||||
expect(clampCharacterCount(Number.NaN)).toBe(MIN_CHARACTER_COUNT);
|
||||
});
|
||||
});
|
||||
|
||||
describe("buildCharacterGenerateRequest", () => {
|
||||
it("trims brief, clamps count, omits empty role", () => {
|
||||
expect(buildCharacterGenerateRequest(" 剑修 ", 99)).toEqual({
|
||||
brief: "剑修",
|
||||
count: MAX_CHARACTER_COUNT,
|
||||
});
|
||||
});
|
||||
|
||||
it("includes trimmed role when present", () => {
|
||||
expect(buildCharacterGenerateRequest("剑修", 2, " 对手 ")).toEqual({
|
||||
brief: "剑修",
|
||||
count: 2,
|
||||
role: "对手",
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("buildCharacterIngestRequest", () => {
|
||||
it("copies cards and carries acknowledge flag", () => {
|
||||
const cards = [card()];
|
||||
const req = buildCharacterIngestRequest(cards, true);
|
||||
expect(req.acknowledge_conflicts).toBe(true);
|
||||
expect(req.cards).toEqual(cards);
|
||||
expect(req.cards[0]).not.toBe(cards[0]); // immutable copy
|
||||
});
|
||||
|
||||
it("defaults acknowledge to false", () => {
|
||||
expect(buildCharacterIngestRequest([card()]).acknowledge_conflicts).toBe(
|
||||
false,
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("updateCard", () => {
|
||||
it("returns a new card with patch applied (no mutation)", () => {
|
||||
const original = card();
|
||||
const next = updateCard(original, { name: "乙" });
|
||||
expect(next.name).toBe("乙");
|
||||
expect(original.name).toBe("甲");
|
||||
expect(next).not.toBe(original);
|
||||
});
|
||||
});
|
||||
|
||||
describe("extractIngestConflicts", () => {
|
||||
it("narrows CONFLICT_UNRESOLVED envelope details", () => {
|
||||
const out = extractIngestConflicts({
|
||||
error: {
|
||||
code: "CONFLICT_UNRESOLVED",
|
||||
details: {
|
||||
conflicts: [
|
||||
{ type: "性格漂移", where: "第3章", refs: ["甲"], suggestion: "改" },
|
||||
"junk",
|
||||
],
|
||||
conflict_count: 2,
|
||||
},
|
||||
},
|
||||
});
|
||||
expect(out).toEqual({
|
||||
conflictCount: 2,
|
||||
conflicts: [
|
||||
{ type: "性格漂移", where: "第3章", refs: ["甲"], suggestion: "改" },
|
||||
{ type: "未分类", where: "", refs: [], suggestion: "" },
|
||||
],
|
||||
});
|
||||
});
|
||||
|
||||
it("returns null for non-conflict errors", () => {
|
||||
expect(extractIngestConflicts({ error: { code: "LLM_UNAVAILABLE" } })).toBeNull();
|
||||
expect(extractIngestConflicts(null)).toBeNull();
|
||||
});
|
||||
|
||||
it("falls back conflict_count to conflicts length", () => {
|
||||
const out = extractIngestConflicts({
|
||||
error: {
|
||||
code: "CONFLICT_UNRESOLVED",
|
||||
details: { conflicts: [{ type: "设定违例" }] },
|
||||
},
|
||||
});
|
||||
expect(out?.conflictCount).toBe(1);
|
||||
});
|
||||
});
|
||||
|
||||
describe("errorCode / generationErrorMessage", () => {
|
||||
it("extracts code and maps 503 to settings prompt", () => {
|
||||
expect(errorCode({ error: { code: "LLM_UNAVAILABLE" } })).toBe(
|
||||
"LLM_UNAVAILABLE",
|
||||
);
|
||||
expect(generationErrorMessage({ error: { code: "LLM_UNAVAILABLE" } })).toContain(
|
||||
"设置",
|
||||
);
|
||||
expect(generationErrorMessage({ error: { code: "INTERNAL" } })).toContain(
|
||||
"重试",
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("worldEntityRules", () => {
|
||||
it("returns rules or empty array", () => {
|
||||
expect(worldEntityRules({ type: "势力", name: "宗门", rules: ["a"] })).toEqual([
|
||||
"a",
|
||||
]);
|
||||
expect(worldEntityRules({ type: "势力", name: "宗门" })).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("mergeCharacterCards", () => {
|
||||
it("appends session cards not already in the persisted list", () => {
|
||||
const persisted = [card({ name: "甲" })];
|
||||
const session = [card({ name: "乙" })];
|
||||
const out = mergeCharacterCards(persisted, session);
|
||||
expect(out.map((c) => c.name)).toEqual(["甲", "乙"]);
|
||||
});
|
||||
|
||||
it("dedups by name (persisted wins, no duplicate on reload echo)", () => {
|
||||
const persisted = [card({ name: "甲", role: "主角" })];
|
||||
const session = [card({ name: "甲", role: "对手" })];
|
||||
const out = mergeCharacterCards(persisted, session);
|
||||
expect(out).toHaveLength(1);
|
||||
expect(out[0].role).toBe("主角"); // persisted version kept
|
||||
});
|
||||
|
||||
it("returns persisted list when no session cards", () => {
|
||||
const persisted = [card({ name: "甲" })];
|
||||
expect(mergeCharacterCards(persisted, [])).toEqual(persisted);
|
||||
});
|
||||
});
|
||||
|
||||
describe("mergeWorldEntities", () => {
|
||||
const entity = (
|
||||
over: Partial<WorldEntityCardView> = {},
|
||||
): WorldEntityCardView => ({ type: "势力", name: "宗门", ...over });
|
||||
|
||||
it("appends entities not already present (keyed by type+name)", () => {
|
||||
const persisted = [entity({ name: "天剑宗" })];
|
||||
const session = [entity({ name: "魔渊" })];
|
||||
expect(mergeWorldEntities(persisted, session).map((e) => e.name)).toEqual([
|
||||
"天剑宗",
|
||||
"魔渊",
|
||||
]);
|
||||
});
|
||||
|
||||
it("dedups by type+name; same name different type kept separate", () => {
|
||||
const persisted = [entity({ type: "势力", name: "玄" })];
|
||||
const session = [
|
||||
entity({ type: "势力", name: "玄" }),
|
||||
entity({ type: "地点", name: "玄" }),
|
||||
];
|
||||
const out = mergeWorldEntities(persisted, session);
|
||||
expect(out).toHaveLength(2);
|
||||
expect(out.map((e) => e.type)).toEqual(["势力", "地点"]);
|
||||
});
|
||||
});
|
||||
BIN
apps/web/lib/generation/cards.ts
Normal file
BIN
apps/web/lib/generation/cards.ts
Normal file
Binary file not shown.
150
apps/web/lib/generation/useCharacterGen.ts
Normal file
150
apps/web/lib/generation/useCharacterGen.ts
Normal file
@@ -0,0 +1,150 @@
|
||||
"use client";
|
||||
|
||||
import { useCallback, useState } from "react";
|
||||
|
||||
import { api } from "@/lib/api/client";
|
||||
import { useToast } from "@/components/Toast";
|
||||
import type {
|
||||
CharacterCardView,
|
||||
CharacterIngestResponse,
|
||||
} from "@/lib/api/types";
|
||||
import {
|
||||
buildCharacterGenerateRequest,
|
||||
buildCharacterIngestRequest,
|
||||
extractIngestConflicts,
|
||||
generationErrorMessage,
|
||||
type IngestConflicts,
|
||||
} from "./cards";
|
||||
|
||||
export type CharacterGenStatus = "idle" | "generating" | "preview" | "error";
|
||||
export type IngestStatus = "idle" | "ingesting" | "conflict" | "done" | "error";
|
||||
|
||||
export interface CharacterGenInput {
|
||||
projectId: string;
|
||||
brief: string;
|
||||
count: number;
|
||||
role?: string | null;
|
||||
}
|
||||
|
||||
export interface UseCharacterGen {
|
||||
genStatus: CharacterGenStatus;
|
||||
cards: CharacterCardView[];
|
||||
ingestStatus: IngestStatus;
|
||||
// 409 冲突详情(待作者裁决);裁决后带 acknowledge 重发。
|
||||
conflicts: IngestConflicts | null;
|
||||
created: string[];
|
||||
generate: (input: CharacterGenInput) => Promise<void>;
|
||||
// 入库选中卡;acknowledge 用于过 409(已查看冲突后重发)。
|
||||
ingest: (
|
||||
projectId: string,
|
||||
cards: readonly CharacterCardView[],
|
||||
acknowledge?: boolean,
|
||||
) => Promise<boolean>;
|
||||
reset: () => void;
|
||||
}
|
||||
|
||||
// 角色生成器(UX §6.6):生成预览 → 作者选/改 → 入库(处理 409 裁决流)。
|
||||
export function useCharacterGen(): UseCharacterGen {
|
||||
const [genStatus, setGenStatus] = useState<CharacterGenStatus>("idle");
|
||||
const [cards, setCards] = useState<CharacterCardView[]>([]);
|
||||
const [ingestStatus, setIngestStatus] = useState<IngestStatus>("idle");
|
||||
const [conflicts, setConflicts] = useState<IngestConflicts | null>(null);
|
||||
const [created, setCreated] = useState<string[]>([]);
|
||||
const toast = useToast();
|
||||
|
||||
const generate = useCallback<UseCharacterGen["generate"]>(
|
||||
async ({ projectId, brief, count, role }) => {
|
||||
setGenStatus("generating");
|
||||
setCards([]);
|
||||
setConflicts(null);
|
||||
setIngestStatus("idle");
|
||||
try {
|
||||
const { data, error } = await api.POST(
|
||||
"/projects/{project_id}/characters/generate",
|
||||
{
|
||||
params: { path: { project_id: projectId } },
|
||||
body: buildCharacterGenerateRequest(brief, count, role),
|
||||
},
|
||||
);
|
||||
if (error || !data) {
|
||||
setGenStatus("error");
|
||||
toast(generationErrorMessage(error), "error");
|
||||
return;
|
||||
}
|
||||
setCards(data.cards ?? []);
|
||||
setGenStatus("preview");
|
||||
} catch {
|
||||
setGenStatus("error");
|
||||
toast("生成请求异常,请检查网络。", "error");
|
||||
}
|
||||
},
|
||||
[toast],
|
||||
);
|
||||
|
||||
const ingest = useCallback<UseCharacterGen["ingest"]>(
|
||||
async (projectId, ingestCards, acknowledge = false) => {
|
||||
if (ingestCards.length === 0) {
|
||||
toast("请至少选择一张角色卡。", "error");
|
||||
return false;
|
||||
}
|
||||
setIngestStatus("ingesting");
|
||||
try {
|
||||
const { data, error } = await api.POST(
|
||||
"/projects/{project_id}/characters",
|
||||
{
|
||||
params: { path: { project_id: projectId } },
|
||||
body: buildCharacterIngestRequest(ingestCards, acknowledge),
|
||||
},
|
||||
);
|
||||
if (error || !data) {
|
||||
const conflict = extractIngestConflicts(error);
|
||||
if (conflict) {
|
||||
// 409 CONFLICT_UNRESOLVED:展示冲突让作者裁决,再带 acknowledge 重发。
|
||||
setConflicts(conflict);
|
||||
setIngestStatus("conflict");
|
||||
return false;
|
||||
}
|
||||
setIngestStatus("error");
|
||||
toast(generationErrorMessage(error), "error");
|
||||
return false;
|
||||
}
|
||||
const result = data as CharacterIngestResponse;
|
||||
setCreated(result.created ?? []);
|
||||
setConflicts(null);
|
||||
setIngestStatus("done");
|
||||
toast(`已入库 ${result.created?.length ?? 0} 个角色`, "success");
|
||||
if (result.rejected_tables && result.rejected_tables.length > 0) {
|
||||
toast(
|
||||
`越权写表被丢弃:${result.rejected_tables.join("、")}`,
|
||||
"info",
|
||||
);
|
||||
}
|
||||
return true;
|
||||
} catch {
|
||||
setIngestStatus("error");
|
||||
toast("入库请求异常,请检查网络。", "error");
|
||||
return false;
|
||||
}
|
||||
},
|
||||
[toast],
|
||||
);
|
||||
|
||||
const reset = useCallback((): void => {
|
||||
setGenStatus("idle");
|
||||
setCards([]);
|
||||
setIngestStatus("idle");
|
||||
setConflicts(null);
|
||||
setCreated([]);
|
||||
}, []);
|
||||
|
||||
return {
|
||||
genStatus,
|
||||
cards,
|
||||
ingestStatus,
|
||||
conflicts,
|
||||
created,
|
||||
generate,
|
||||
ingest,
|
||||
reset,
|
||||
};
|
||||
}
|
||||
58
apps/web/lib/generation/useWorldGen.ts
Normal file
58
apps/web/lib/generation/useWorldGen.ts
Normal file
@@ -0,0 +1,58 @@
|
||||
"use client";
|
||||
|
||||
import { useCallback, useState } from "react";
|
||||
|
||||
import { api } from "@/lib/api/client";
|
||||
import { useToast } from "@/components/Toast";
|
||||
import type { WorldEntityCardView } from "@/lib/api/types";
|
||||
import { buildWorldGenerateRequest, generationErrorMessage } from "./cards";
|
||||
|
||||
export type WorldGenStatus = "idle" | "generating" | "done" | "error";
|
||||
|
||||
export interface UseWorldGen {
|
||||
status: WorldGenStatus;
|
||||
entities: WorldEntityCardView[];
|
||||
generate: (projectId: string, brief: string) => Promise<void>;
|
||||
reset: () => void;
|
||||
}
|
||||
|
||||
// 世界观设计器:POST /world/generate → 预览实体(不入库)。
|
||||
export function useWorldGen(): UseWorldGen {
|
||||
const [status, setStatus] = useState<WorldGenStatus>("idle");
|
||||
const [entities, setEntities] = useState<WorldEntityCardView[]>([]);
|
||||
const toast = useToast();
|
||||
|
||||
const generate = useCallback<UseWorldGen["generate"]>(
|
||||
async (projectId, brief) => {
|
||||
setStatus("generating");
|
||||
setEntities([]);
|
||||
try {
|
||||
const { data, error } = await api.POST(
|
||||
"/projects/{project_id}/world/generate",
|
||||
{
|
||||
params: { path: { project_id: projectId } },
|
||||
body: buildWorldGenerateRequest(brief),
|
||||
},
|
||||
);
|
||||
if (error || !data) {
|
||||
setStatus("error");
|
||||
toast(generationErrorMessage(error), "error");
|
||||
return;
|
||||
}
|
||||
setEntities(data.entities ?? []);
|
||||
setStatus("done");
|
||||
} catch {
|
||||
setStatus("error");
|
||||
toast("生成请求异常,请检查网络。", "error");
|
||||
}
|
||||
},
|
||||
[toast],
|
||||
);
|
||||
|
||||
const reset = useCallback((): void => {
|
||||
setStatus("idle");
|
||||
setEntities([]);
|
||||
}, []);
|
||||
|
||||
return { status, entities, generate, reset };
|
||||
}
|
||||
Reference in New Issue
Block a user