feat: 用量归属支持 callerRef 透传

- POST /ai/tasks 接受顶层可选 callerRef(限 64 字符),不进入 params、不参与幂等键、不进 prompt
- 结算时写进用量记录,lpt-be 据此做用户维度报表
- 复用已有任务时沿用原归属:那次 LLM 调用只发生一次,账算在原调用方头上
This commit is contained in:
2026-10-10 20:08:51 +08:00
parent 935f4781a4
commit 4aff5c03fe
9 changed files with 240 additions and 2 deletions
+5
View File
@@ -120,6 +120,11 @@ describe("toUsageRecord", () => {
expect(record.model).toBe("");
});
it("归属标记原样带上;没传时显式写 null", () => {
expect(toUsageRecord(entry({ callerRef: "U-123" })).callerRef).toBe("U-123");
expect(toUsageRecord(entry()).callerRef).toBeNull();
});
it("必填字段永远非空,否则 lpt-be 会把整批上报判 400", () => {
// 契约 1 的必填项:lpt-be 侧 @NotBlank 的字段不能是空串,@NotNull 的字段不能缺。
// 尤其是 /fetch-title 的 model——它合法地是空串,所以 lpt-be 那边用的是 @NotNull。
+3
View File
@@ -23,6 +23,8 @@ export interface UsageRecord {
occurredAt: string;
taskType: string;
taskId: string | null;
/** 归属标记(lpt-be 传的用户 id);lpt-be 把它落到 ai_usage_records.user_id */
callerRef: string | null;
model: string;
attempts: number;
isLlmCall: boolean;
@@ -91,6 +93,7 @@ export function toUsageRecord(entry: LogEntry): UsageRecord {
occurredAt: entry.timestamp,
taskType: entry.taskType ?? entry.endpoint,
taskId: entry.taskId ?? null,
callerRef: entry.callerRef ?? null,
model: entry.model,
attempts: entry.attempts ?? 1,
isLlmCall: entry.isLlmCall,
+1
View File
@@ -185,6 +185,7 @@ async function toggleDetail(id, el) {
let txt = '=== lpt-ai 请求日志 ===\\n';
txt += '端点: '+(log.endpoint||'')+'\\n';
txt += '模型: '+(log.model||'')+'\\n';
txt += '归属: '+(log.callerRef||'未归属')+'\\n';
txt += '耗时: '+log.durationMs+'ms\\n';
txt += '状态: '+log.status+'\\n';
if (log.attempts != null) txt += '调用次数: '+log.attempts+'\\n';
+4
View File
@@ -31,6 +31,8 @@ export interface LogEntry {
taskType?: string;
/** 任务标识,便于把面板上的一条日志对回具体任务 */
taskId?: string;
/** 归属标记(lpt-be 传的用户 id);为空表示调用方没传或为独立调用 */
callerRef?: string;
model: string;
/** 本次请求实际发起的 LLM 调用次数(含失败重试);重试会重复计费 */
attempts?: number;
@@ -57,6 +59,7 @@ export interface LogSummary {
timestamp: string;
endpoint: string;
taskType?: string;
callerRef?: string;
model: string;
attempts?: number;
isLlmCall: boolean;
@@ -140,6 +143,7 @@ export class LogStore {
timestamp: entry.timestamp,
endpoint: entry.endpoint,
...(entry.taskType !== undefined ? { taskType: entry.taskType } : {}),
...(entry.callerRef !== undefined ? { callerRef: entry.callerRef } : {}),
model: entry.model,
...(entry.attempts !== undefined ? { attempts: entry.attempts } : {}),
isLlmCall: entry.isLlmCall,