feat(digital): 接入角色世界书与账号实验服务

- digital: 新增私有会话、DeepSeek流式生成、停止与分支、世界书和预设编排
- experiments: 新增账号稳定分组、人工指定、生命周期、预设快照及实验漏斗审计
- migrations/data: 初始化20位成年角色、4本世界书与3种叙事预设
- tests/docs: 补充11项核心回归、市场参考和部署说明
This commit is contained in:
湛兮
2026-10-27 10:20:49 +08:00
parent 8a28ca4d83
commit 00dd5095bb
20 changed files with 2769 additions and 10 deletions
+66
View File
@@ -0,0 +1,66 @@
/** 数字人 MySQL 访问与事务审计,所有账号数据查询显式包含所属账号。数字人表与现有账号表按服务器 UTC 解码。 */
import type {
RowDataPacket,
ResultSetHeader,
PoolConnection,
} from "mysql2/promise";
import { pool } from "../../db/pool";
/** JSON 列兼容 mysql2 返回对象或字符串的情况。 */
export function json<T>(value: unknown): T {
return (typeof value === "string" ? JSON.parse(value) : value) as T;
}
/** 参数化读取;调用方仅可传入固定 SQL。 */
export async function rows<T>(
sql: string,
args: unknown[] = [],
connection?: PoolConnection,
): Promise<T[]> {
const [result] = await (connection ?? pool).query<RowDataPacket[]>(
{ sql, timezone: "Z" },
args,
);
return result as T[];
}
/** 参数化写入并返回影响行数,供乐观锁和租约判断使用。 */
export async function write(
sql: string,
args: unknown[] = [],
connection?: PoolConnection,
) {
const [result] = await (connection ?? pool).query<ResultSetHeader>(
{ sql, timezone: "Z" },
args,
);
return result;
}
/** 审计与状态变更共用同一连接,避免有变更却无审计。 */
export async function transaction<T>(
fn: (c: PoolConnection) => Promise<T>,
): Promise<T> {
const c = await pool.getConnection();
try {
await c.beginTransaction();
const v = await fn(c);
await c.commit();
return v;
} catch (e) {
await c.rollback();
throw e;
} finally {
c.release();
}
}
/** 审计不保存用户聊天、密钥或上游响应。 */
export async function audit(
actor: string,
action: string,
target: string,
detail: unknown,
c?: PoolConnection,
) {
await write(
"INSERT INTO dh_audits(actor,action,target,detail) VALUES (?,?,?,?)",
[actor, action, target, JSON.stringify(detail)],
c,
);
}