feat(chat): 整合预设回退与只读风格状态查询

- Grok业务改动:纳入叙事预设、种子迁移、系统前言和下架资源回退

- 风格状态:校验会话归属并只读预览方案,打开设定不产生分组和决策

- 验证与文档:迁移和种子内容一致,21项规则及鉴权回归通过
This commit is contained in:
湛兮
2026-10-27 12:54:50 +08:00
parent 669d1eb4cd
commit cdae952c6a
8 changed files with 172 additions and 19 deletions
+57 -12
View File
@@ -1,4 +1,4 @@
/** 流式对话编排,数据库租约防止并发双发,断线后保存已生成部分。 */
/** 流式对话编排。租约防止并发双发;下架的预设或世界书跳过,不中断本轮。 */
import { env } from "../../config/env";
import { randomUUID } from "node:crypto";
import type { FastifyReply } from "fastify";
@@ -10,7 +10,13 @@ import {
settingsSchema,
type ContentInput,
} from "./digital.schema";
import { decide, trackUiEvent } from "./digital.experiments";
import {
activeExperiment,
experiments,
resolveExperiment,
decide,
trackUiEvent,
} from "./digital.experiments";
import {
badRequest,
conflict,
@@ -36,15 +42,32 @@ export type Conversation = {
lease_id: string | null;
lease_until: Date | null;
};
/** 只允许读取已发布的同类型内容;后台编辑使用独立接口。 */
export async function published(id: string, kind: ContentInput["kind"]) {
/** 已发布内容;缺失时返回空,供预设和世界书降级。角色卡仍必须存在。 */
export async function publishedOptional(
id: string,
kind: ContentInput["kind"],
) {
const [r] = await rows<ContentRow>(
"SELECT * FROM dh_content WHERE id=? AND kind=? AND status='published'",
[id, kind],
);
if (!r) throw notFound("内容尚未发布或已下架");
if (!r) return null;
return { ...r, body: contentSchema.shape.body.parse(json(r.body)) };
}
/** 只允许读取已发布的同类型内容;后台编辑使用独立接口。 */
export async function published(id: string, kind: ContentInput["kind"]) {
const found = await publishedOptional(id, kind);
if (!found) throw notFound("内容尚未发布或已下架");
return found;
}
/** 会话指定预设优先,其次角色默认预设,都不可用时用内置写法。 */
export function choosePresetBody(
selected: ContentInput["body"] | null,
characterDefault: ContentInput["body"] | null,
base: ContentInput["body"],
) {
return selected ?? characterDefault ?? base;
}
/** 用户会话隔离边界,禁止仅凭 conversation id 操作。 */
export async function ownConversation(
id: string,
@@ -59,6 +82,15 @@ export async function ownConversation(
if (!v) throw notFound("会话不存在");
return { ...v, settings: settingsSchema.parse(json(v.settings)) };
}
/** 只读说明当前风格是否由统一方案控制,不产生分组、决策、回执或曝光。 */
export async function conversationStylePolicy(account: string, id: string) {
await ownConversation(id, account);
const plan = activeExperiment(await experiments(), "chat");
const decision = plan ? await resolveExperiment(plan, account, true) : null;
return {
managed: !!(decision && (decision.presetSnapshot || decision.presetId)),
};
}
/** 公开卡片剔除提示词、示例和内部来源编排。 */
export function publicContent(r: ContentRow) {
const b = contentSchema.shape.body.parse(json(r.body));
@@ -180,15 +212,24 @@ export async function streamChat(
const decision = await decide(account, "chat");
const presetId =
decision.presetId ?? conversation.settings.presetId ?? card.body.presetId;
const preset =
decision.presetSnapshot ??
(presetId ? (await published(presetId, "preset")).body : basePreset);
const selectedPreset = decision.presetSnapshot
? decision.presetSnapshot
: presetId
? ((await publishedOptional(presetId, "preset"))?.body ?? null)
: null;
const characterPreset =
!selectedPreset && card.body.presetId && card.body.presetId !== presetId
? ((await publishedOptional(card.body.presetId, "preset"))?.body ?? null)
: null;
const preset = choosePresetBody(selectedPreset, characterPreset, basePreset);
const bookIds = [
...new Set([...card.body.bookIds, ...conversation.settings.bookIds]),
];
const books = await Promise.all(
bookIds.map((book) => published(book, "worldbook")),
);
const books = (
await Promise.all(
bookIds.map((book) => publishedOptional(book, "worldbook")),
)
).filter((book) => book !== null);
if (inFlight >= 4)
throw new HttpError(429, "MODEL_BUSY", "当前对话较多,请稍后再试");
const lease = randomUUID();
@@ -205,7 +246,11 @@ export async function streamChat(
);
if (!result.affectedRows)
throw conflict("上一条回复仍在生成,请先停止或稍后重试");
await write("UPDATE dh_messages SET status='stopped' WHERE conversation_id=? AND status='generating'",[id],c);
await write(
"UPDATE dh_messages SET status='stopped' WHERE conversation_id=? AND status='generating'",
[id],
c,
);
const duplicate = await rows(
"SELECT id FROM dh_messages WHERE conversation_id=? AND request_id=? LIMIT 1",
[id, input.requestId],