78109942d2
- 分组支持 A 至 Z,按整数比例验证合计100%,支持0%组人工验收 - 未入组使用类型默认值,分组持久化、指定与最终结果按实际组校验 - 保留历史双组哈希区间,新增多组与生命周期回归并同步文档
100 lines
4.1 KiB
TypeScript
100 lines
4.1 KiB
TypeScript
/** 数字人业务输入边界,限制内容体积并分离公开内容和服务端提示词。 */
|
|
import { z } from "zod";
|
|
/** URL 中的稳定业务标识。 */
|
|
export const idSchema = z.string().regex(/^[a-zA-Z0-9_-]{1,64}$/);
|
|
const text = z.string().max(12000);
|
|
/** 世界书条目:采用确定性关键词匹配,禁止运行用户正则。 */
|
|
export const entrySchema = z.object({
|
|
id: idSchema,
|
|
name: z.string().max(120),
|
|
keys: z.array(z.string().min(1).max(100)).max(40).default([]),
|
|
secondaryKeys: z.array(z.string().min(1).max(100)).max(40).default([]),
|
|
content: text,
|
|
constant: z.boolean().default(false),
|
|
enabled: z.boolean().default(true),
|
|
priority: z.number().int().min(0).max(1000).default(100),
|
|
position: z.enum(["before", "after"]).default("after"),
|
|
caseSensitive: z.boolean().default(false),
|
|
scanDepth: z.number().int().min(1).max(30).default(6),
|
|
});
|
|
/** 可发布的角色、世界书和生成预设;未知字段不进入数据库。 */
|
|
export const contentSchema = z.object({
|
|
kind: z.enum(["character", "worldbook", "preset"]),
|
|
name: z.string().min(1).max(120),
|
|
status: z.enum(["draft", "published", "archived"]).default("draft"),
|
|
body: z.object({
|
|
description: text.default(""),
|
|
personality: text.default(""),
|
|
scenario: text.default(""),
|
|
greeting: text.default(""),
|
|
alternateGreetings: z.array(text).max(8).default([]),
|
|
examples: text.default(""),
|
|
systemPrompt: text.default(""),
|
|
gender: z.enum(["female", "male", "other"]).default("female"),
|
|
age: z.number().int().min(18).max(1000).default(25),
|
|
cover: z
|
|
.string()
|
|
.max(1000)
|
|
.refine(
|
|
(v) => !v || /^\/(?!\/)/.test(v) || /^https:\/\//.test(v),
|
|
"图片须为站内路径或 HTTPS",
|
|
),
|
|
tags: z.array(z.string().max(40)).max(12).default([]),
|
|
bookIds: z.array(idSchema).max(12).default([]),
|
|
presetId: idSchema.optional(),
|
|
entries: z.array(entrySchema).max(150).default([]),
|
|
temperature: z.number().min(0).max(1.5).default(0.9),
|
|
maxTokens: z.number().int().min(100).max(1500).default(700),
|
|
contextBudget: z.number().int().min(6000).max(24000).default(12000),
|
|
worldBudget: z.number().int().min(0).max(8000).default(3500),
|
|
source: z.string().max(1000).default("项目原创"),
|
|
}),
|
|
});
|
|
/** 内容记录只保存经过 schema 解析的数据。 */
|
|
export type ContentInput = z.infer<typeof contentSchema>;
|
|
/** 组标识仅限单个大写字母;允许0%保留用于人工验收。 */
|
|
export const groupKeySchema = z.string().regex(/^[A-Z]$/, "组别须为 A 到 Z");
|
|
/** 比例按万分比整数保存,避免小数累计误差。 */
|
|
export const experimentGroupsSchema = z
|
|
.array(
|
|
z.object({
|
|
key: groupKeySchema,
|
|
weight: z.number().int().min(0).max(10000),
|
|
value: z.json(),
|
|
}),
|
|
)
|
|
.min(1)
|
|
.max(26)
|
|
.superRefine((groups, ctx) => {
|
|
if (new Set(groups.map((g) => g.key)).size !== groups.length)
|
|
ctx.addIssue({ code: "custom", message: "组别不能重复" });
|
|
if (groups.reduce((sum, g) => sum + g.weight, 0) !== 10000)
|
|
ctx.addIssue({ code: "custom", message: "分组比例合计必须为100%" });
|
|
});
|
|
/** 实验配置仅通过后台写入,分组语义在同一实验生命周期中固定。 */
|
|
export const experimentSchema = z.object({
|
|
name: z.string().min(1).max(120),
|
|
scope: idSchema,
|
|
config: z.object({
|
|
audience: z.enum(["all", "new", "existing", "specified"]).default("all"),
|
|
cutoff: z.iso.datetime().optional(),
|
|
accounts: z
|
|
.array(z.string().regex(/^(employee|super_admin):[0-9]+$/))
|
|
.max(500)
|
|
.default([]),
|
|
traffic: z.number().int().min(0).max(10000).default(10000),
|
|
groups: experimentGroupsSchema,
|
|
winner: groupKeySchema.optional(),
|
|
description: z.string().max(2000).default(""),
|
|
typeRevision: z.number().int().positive().optional(),
|
|
}),
|
|
});
|
|
/** 用户自己的会话配置,不包含实验或服务端模型选择。 */
|
|
export const settingsSchema = z.object({
|
|
persona: z.string().max(2000).default(""),
|
|
userName: z.string().max(60).default("旅人"),
|
|
memory: z.string().max(3000).default(""),
|
|
presetId: idSchema.optional(),
|
|
bookIds: z.array(idSchema).max(8).default([]),
|
|
});
|