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
+16
View File
@@ -37,6 +37,13 @@ export interface TaskRecord {
error?: string;
/** 已尝试次数(含首次),便于排查重试情况 */
attempts: number;
/**
* 调用方打的归属标记(lpt-be 传的是用户 id)。
*
* opaque 字符串,lpt-ai 不解释它的含义,只在结算时原样写进用量记录。
* 刻意不放进 `params`:那会污染 prompt 模板的输入,也会把幂等键算错。
*/
callerRef?: string;
/**
* 跨所有尝试累加的 token 用量。
*
@@ -57,6 +64,13 @@ export interface CreateTaskOptions {
idempotencyKey?: string;
/** 跳过复用,强制新建任务(用户主动「重新生成」时用) */
force?: boolean;
/**
* 归属标记(lpt-be 传用户 id),结算时原样写进用量记录。
*
* 复用已有任务时**不会**覆盖原任务的标记:那次 LLM 调用只发生了一次,
* 账算在原调用方头上;复用方没有产生新的 token 消耗。
*/
callerRef?: string;
}
export interface CreateTaskResult {
@@ -210,6 +224,7 @@ export class TaskQueue {
status: "pending",
attempts: 0,
createdAt: new Date(this.now()).toISOString(),
...(options.callerRef ? { callerRef: options.callerRef } : {}),
};
this.records.set(id, task);
this.keyIndex.set(key, id);
@@ -386,6 +401,7 @@ export class TaskQueue {
endpoint: `/ai/tasks#${task.type}`,
taskType: task.type,
taskId: task.id,
...(task.callerRef ? { callerRef: task.callerRef } : {}),
model: config.model,
attempts: task.attempts,
isLlmCall: true,