feat: 采集 LLM token 用量并上报 lpt-be

- 客户端解析上游 usage(含 DeepSeek 缓存命中/未命中),跨重试累加
- 新增计价表(三档单价 + 峰谷时段),费用在结算时冻结
- 新增用量上报与本地 outbox 重试;4xx 判定为确定性拒绝,不无限重试
- 面板补充 Token/费用/调用次数与计价快照
- 顺带修复面板内联脚本转义错误(模板字面量吃掉 \n 与 \',面板一直空白)
This commit is contained in:
2026-10-10 18:46:01 +08:00
parent fd1193a66f
commit 935f4781a4
24 changed files with 2632 additions and 65 deletions
+84 -1
View File
@@ -13,10 +13,12 @@
import { randomUUID, createHash } from "node:crypto";
import { chat, isAvailable, loadConfig, LlmError, type ChatMessage, type ChatOptions } from "./llm/client.js";
import { accumulateUsage, combineSource, emptyUsage, type TokenUsage, type UsageSource } from "./llm/usage.js";
import { withRetry, isRetryableLlmError, type RetryOptions } from "./llm/retry.js";
import { aggregateReportPrompt, compareRecallPrompt, generateMindMapPrompt, guidancePrompt } from "./llm/prompts.js";
import { loadThinkingConfig, promptLength, reasoningEffortFor, type ThinkingConfig } from "./llm/thinking.js";
import { logStore } from "./admin/store.js";
import { loadPricingConfig, priceUsage, type PricingSnapshot } from "./admin/pricing.js";
// ---- 类型 ----
@@ -35,6 +37,16 @@ export interface TaskRecord {
error?: string;
/** 已尝试次数(含首次),便于排查重试情况 */
attempts: number;
/**
* 跨所有尝试累加的 token 用量。
*
* 重试会重复计费,所以这里是「这个任务一共花了多少」,不是「最后一次花了多少」;
* 某个字段为 null 表示所有尝试都没拿到该维度(不要当成 0)。
*/
usage?: TokenUsage;
usageSource?: UsageSource;
/** 失败分类(timeout / rate_limited / ...),便于按错误类型聚合用量 */
errorCode?: string;
createdAt: string; // ISO 8601
startedAt?: string;
completedAt?: string;
@@ -289,6 +301,8 @@ export class TaskQueue {
task.status = "failed";
task.error = "LLM API Key 未配置";
task.completedAt = new Date(this.now()).toISOString();
task.usage = emptyUsage();
task.usageSource = "missing";
this.settle(task);
return;
}
@@ -308,9 +322,31 @@ export class TaskQueue {
},
};
// 每次尝试的用量都要留下:重试 N 次就是 N 次计费,失败尝试也可能已经产生了 token
const attemptUsages: TokenUsage[] = [];
const attemptSources: UsageSource[] = [];
try {
const runOnce =
this.deps.runOnce ?? (async (t, ctx) => parseResult(t.type, await chat(ctx.config, ctx.messages, callOptions)));
this.deps.runOnce ??
(async (t, ctx) => {
try {
const result = await chat(ctx.config, ctx.messages, callOptions);
attemptUsages.push(result.usage);
attemptSources.push(result.usageSource);
return parseResult(t.type, result.content, result.model);
} catch (err) {
// 上游返回 200 但内容为空这类失败已经计费,用量挂在 LlmError 上带回来
if (err instanceof LlmError && err.usage) {
attemptUsages.push(err.usage);
attemptSources.push("upstream");
} else {
attemptUsages.push(emptyUsage());
attemptSources.push("missing");
}
throw err;
}
});
const result = await withRetry(
() => runOnce(task, { attempt: task.attempts + 1, messages, config }),
retryOptions,
@@ -323,8 +359,11 @@ export class TaskQueue {
task.attempts += 1;
task.status = "failed";
task.error = err instanceof Error ? err.message : String(err);
task.errorCode = classifyLlmError(err);
task.completedAt = new Date(this.now()).toISOString();
} finally {
task.usage = accumulateUsage(attemptUsages);
task.usageSource = combineSource(attemptSources);
this.settle(task, config);
}
}
@@ -338,15 +377,28 @@ export class TaskQueue {
task.startedAt && task.completedAt
? Date.parse(task.completedAt) - Date.parse(task.startedAt)
: 0;
const usage = task.usage ?? emptyUsage();
// 计价在结算时冻结:价格表会变,历史账不能被新价格改写。
// 重试跨越峰谷分界时整单按结算时刻计价,偏差上限是一个任务的执行时长。
const priced = priceUsage(usage, new Date(this.now()), loadPricingConfig());
logStore.add({
timestamp: new Date(this.now()).toISOString(),
endpoint: `/ai/tasks#${task.type}`,
taskType: task.type,
taskId: task.id,
model: config.model,
attempts: task.attempts,
isLlmCall: true,
usage,
usageSource: task.usageSource ?? "missing",
costUsd: priced.costUsd,
pricing: priced.snapshot,
durationMs,
status: task.status === "done" ? 200 : 500,
requestBody: task.params,
responseBody: task.status === "done" ? (task.result ?? {}) : {},
...(task.error ? { error: task.error } : {}),
...(task.errorCode ? { errorCode: task.errorCode } : {}),
});
}
@@ -377,6 +429,37 @@ export class TaskQueue {
}
}
// ---- 失败分类 ----
/**
* 把失败原因归成可聚合的短分类。
*
* 面板要能回答「这个月有多少次是被限流打掉的」;把整句错误文案当分类是聚合不起来的。
* 上游原文仍然留在 `error` 字段里,分类只用于分组统计。
*/
export function classifyLlmError(error: unknown): string {
if (!(error instanceof LlmError)) return "unknown";
if (error.message.includes("API Key 未配置")) return "not_configured";
switch (error.statusCode) {
case 408:
case 504:
return "timeout";
case 429:
return "rate_limited";
case 402:
return "insufficient_balance";
case 401:
case 403:
return "auth_failed";
case 400:
case 404:
case 422:
return "bad_request";
default:
return error.statusCode >= 500 ? "upstream_error" : "unknown";
}
}
// ---- 结果解析与 Prompt ----
function baseCallOptions(type: TaskType): ChatOptions {