feat(experiments): 新增后台场景注册与应用接入映射
保留已有ui/chat场景与分组,支持命名、创建和停用;同一应用接入点实验互斥,保护历史场景类型与运行实验;增加自定义场景决策回归。
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
/** 后台实验生命周期与账号稳定分配;前台只消费短时决策并回报曝光。 */
|
||||
import { scene } from "./digital.scenes";
|
||||
import { createHash, randomUUID } from "node:crypto";
|
||||
import type { PoolConnection } from "mysql2/promise";
|
||||
import type { z } from "zod";
|
||||
@@ -16,7 +17,9 @@ type Config = z.infer<typeof experimentSchema>["config"] & {
|
||||
export type Experiment = {
|
||||
id: string;
|
||||
name: string;
|
||||
scope: "ui" | "chat";
|
||||
scope: string;
|
||||
runtime: "ui" | "chat";
|
||||
sceneEnabled: number;
|
||||
state: "draft" | "running" | "paused" | "finished";
|
||||
config: Config;
|
||||
revision: number;
|
||||
@@ -45,7 +48,7 @@ export function eligible(config: Config, account: string, createdAt: string) {
|
||||
export async function experiments() {
|
||||
return (
|
||||
await rows<Experiment>(
|
||||
"SELECT * FROM dh_experiments ORDER BY created_at DESC LIMIT 200",
|
||||
"SELECT e.*,s.runtime,s.name AS sceneName,s.enabled AS sceneEnabled FROM dh_experiments e JOIN dh_scenes s ON s.id=e.scope ORDER BY e.created_at DESC LIMIT 200",
|
||||
)
|
||||
).map((e) => ({ ...e, config: json<Config>(e.config) }));
|
||||
}
|
||||
@@ -73,6 +76,13 @@ export async function saveExperiment(
|
||||
)
|
||||
throw badRequest("新老用户实验必须设置注册时间分界");
|
||||
return transaction(async (c) => {
|
||||
await rows(
|
||||
"SELECT version FROM schema_migrations ORDER BY version LIMIT 1 FOR UPDATE",
|
||||
[],
|
||||
c,
|
||||
);
|
||||
const registered = await scene(input.scope, c);
|
||||
if (!registered.enabled) throw badRequest("场景已停用,不能创建或编辑实验");
|
||||
const [old] = await rows<Experiment>(
|
||||
"SELECT * FROM dh_experiments WHERE id=? FOR UPDATE",
|
||||
[id],
|
||||
@@ -136,15 +146,20 @@ export async function transition(
|
||||
...json<Config>(e.config),
|
||||
...(winner ? { winner } : {}),
|
||||
};
|
||||
const registered = await scene(e.scope, c);
|
||||
if (state === "running") {
|
||||
if (!registered.enabled) throw badRequest("场景已停用,不能启动实验");
|
||||
const others = await rows(
|
||||
"SELECT id FROM dh_experiments WHERE scope=? AND id<>? AND state IN ('running','paused')",
|
||||
[e.scope, id],
|
||||
"SELECT e.id FROM dh_experiments e JOIN dh_scenes s ON s.id=e.scope WHERE s.runtime=? AND e.id<>? AND e.state IN ('running','paused')",
|
||||
[registered.runtime, id],
|
||||
c,
|
||||
);
|
||||
if (others.length) throw conflict("此场景已有未结束的实验");
|
||||
if (others.length)
|
||||
throw conflict(
|
||||
"该应用接入点已有未结束的实验,请先结束,避免多个场景同时控制同一功能",
|
||||
);
|
||||
const cfg = config;
|
||||
if (e.scope === "chat" && !cfg.presetSnapshots) {
|
||||
if (registered.runtime === "chat" && !cfg.presetSnapshots) {
|
||||
if (!cfg.presetA || !cfg.presetB)
|
||||
throw badRequest("对话实验须分别选择两个已发布预设");
|
||||
const available = await rows<{ id: string; body: unknown }>(
|
||||
@@ -246,13 +261,19 @@ export async function resolveExperiment(
|
||||
presetSnapshot: cfg.presetSnapshots?.[variant],
|
||||
};
|
||||
}
|
||||
/** 从已按创建时间排序的场景实验中选择当前接入点的有效配置。 */
|
||||
export function activeExperiment(list: Experiment[], runtime: "ui" | "chat") {
|
||||
const candidates = list.filter(
|
||||
(e) => e.runtime === runtime && !!e.sceneEnabled && e.state !== "draft",
|
||||
);
|
||||
return (
|
||||
candidates.find((e) => e.state === "running" || e.state === "paused") ??
|
||||
candidates[0]
|
||||
);
|
||||
}
|
||||
/** 返回前台所需最小决策,规则、指定名单和提示词均不下发。 */
|
||||
export async function decide(account: string, scope: "ui" | "chat") {
|
||||
const list = (await experiments()).filter(
|
||||
(e) => e.scope === scope && e.state !== "draft",
|
||||
);
|
||||
const e =
|
||||
list.find((e) => e.state === "running" || e.state === "paused") ?? list[0];
|
||||
const e = activeExperiment(await experiments(), scope);
|
||||
if (!e)
|
||||
return {
|
||||
variant: "A" as const,
|
||||
@@ -307,8 +328,8 @@ export async function trackUiEvent(
|
||||
await write(
|
||||
`INSERT IGNORE INTO dh_events(experiment_id,decision_id,account,variant,cohort,event_name,conversation_id,request_id)
|
||||
SELECT d.experiment_id,d.id,d.account,d.variant,IF(d.reason='override','manual','random'),?,?,?
|
||||
FROM dh_decisions d JOIN dh_experiments e ON e.id=d.experiment_id
|
||||
WHERE d.id=? AND d.account=? AND e.scope='ui' AND d.reason IN ('allocated','sticky','override') AND ${validity}`,
|
||||
FROM dh_decisions d JOIN dh_experiments e ON e.id=d.experiment_id JOIN dh_scenes s ON s.id=e.scope
|
||||
WHERE d.id=? AND d.account=? AND s.runtime='ui' AND d.reason IN ('allocated','sticky','override') AND ${validity}`,
|
||||
[
|
||||
event,
|
||||
conversationId,
|
||||
|
||||
Reference in New Issue
Block a user