feat(experiments): 独立注册实验类型并分离用户分组查询
- 实验类型:自定义标识、数据契约、默认值和真实调用后的接入验收 - 测试方案:按类型校验 A/B 值,保留历史分组与预设快照,未验收禁止发布 - 用户管理:真实账号分页搜索、只读预览与有效账号人工指定校验 - 文档与验证:同步迁移014及消费者接口说明,15项核心回归通过
This commit is contained in:
@@ -1,4 +1,14 @@
|
||||
/** 数字人 API:前台私有会话与后台内容/实验严格分权。 */
|
||||
import {
|
||||
experimentTypes,
|
||||
saveExperimentType,
|
||||
typeInputSchema,
|
||||
confirmTypeIntegration,
|
||||
} from "./digital.experiment-types";
|
||||
import {
|
||||
experimentUsers,
|
||||
assertActiveExperimentAccount,
|
||||
} from "./digital.experiment-users";
|
||||
import { env } from "../../config/env";
|
||||
import { scenes, saveScene, sceneSchema } from "./digital.scenes";
|
||||
import { randomUUID } from "node:crypto";
|
||||
@@ -26,7 +36,6 @@ import {
|
||||
transition,
|
||||
resolveExperiment,
|
||||
decide,
|
||||
accountCreated,
|
||||
} from "./digital.experiments";
|
||||
import {
|
||||
createConversation,
|
||||
@@ -52,6 +61,22 @@ export async function digitalRoutes(app: FastifyInstance) {
|
||||
app.register(
|
||||
async (front) => {
|
||||
front.addHook("preHandler", userGuard);
|
||||
front.get("/decisions/:key", async (r) => {
|
||||
const { key } = z.object({ key: idSchema }).parse(r.params);
|
||||
const d = await decide(r.user.sub, key);
|
||||
// 通用消费者不接收服务端预设正文或账号创建信息。
|
||||
return ok({
|
||||
variant: d.variant,
|
||||
reason: d.reason,
|
||||
experimentId: d.experimentId,
|
||||
decisionId: d.decisionId,
|
||||
value: d.value,
|
||||
typeKey: d.typeKey,
|
||||
typeRevision: d.typeRevision,
|
||||
expiresAt: d.expiresAt,
|
||||
fallbackVariant: d.fallbackVariant,
|
||||
});
|
||||
});
|
||||
front.get("/bootstrap", async (r) => {
|
||||
const [catalog, profile, decision] = await Promise.all([
|
||||
rows<ContentRow>(
|
||||
@@ -355,6 +380,36 @@ export async function digitalRoutes(app: FastifyInstance) {
|
||||
});
|
||||
return ok({ saved: true });
|
||||
});
|
||||
admin.get("/experiment-types", async () => ok(await experimentTypes()));
|
||||
admin.put("/experiment-types/:id", async (r) => {
|
||||
const { id } = params.parse(r.params);
|
||||
await saveExperimentType(r.user.sub, id, typeInputSchema.parse(r.body));
|
||||
return ok({ saved: true });
|
||||
});
|
||||
admin.post("/experiment-types/:id/integration", async (r) => {
|
||||
const { id } = params.parse(r.params);
|
||||
const input = z
|
||||
.object({
|
||||
revision: z.number().int().positive(),
|
||||
contractRevision: z.number().int().positive(),
|
||||
verified: z.boolean(),
|
||||
notes: z.string().max(2000),
|
||||
})
|
||||
.parse(r.body);
|
||||
await confirmTypeIntegration(r.user.sub, id, input);
|
||||
return ok({ saved: true });
|
||||
});
|
||||
admin.get("/experiment-users", async (r) => {
|
||||
const input = z
|
||||
.object({
|
||||
q: z.string().max(100).default(""),
|
||||
page: z.coerce.number().int().min(1).default(1),
|
||||
size: z.coerce.number().int().min(1).max(50).default(20),
|
||||
experiment: idSchema.optional(),
|
||||
})
|
||||
.parse(r.query);
|
||||
return ok(await experimentUsers(input));
|
||||
});
|
||||
admin.get("/scenes", async () => ok(await scenes()));
|
||||
admin.put("/scenes/:id", async (r) => {
|
||||
const { id } = params.parse(r.params);
|
||||
@@ -440,7 +495,7 @@ export async function digitalRoutes(app: FastifyInstance) {
|
||||
Date.parse(b.expiresAt) > Date.now() + 30 * 86400000
|
||||
)
|
||||
throw badRequest("指定用户有效期须在未来30天内");
|
||||
await accountCreated(b.account);
|
||||
await assertActiveExperimentAccount(b.account);
|
||||
await transaction(async (c) => {
|
||||
const [e] = await rows<{ state: string }>(
|
||||
"SELECT state FROM dh_experiments WHERE id=? FOR UPDATE",
|
||||
|
||||
Reference in New Issue
Block a user