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
+38 -5
View File
@@ -8,12 +8,28 @@
*/
import type { ReasoningEffort } from "./thinking.js";
import { normalizeUsage, type TokenUsage, type UsageSource } from "./usage.js";
export interface ChatMessage {
role: "system" | "user" | "assistant";
content: string;
}
/**
* 一次成功调用的返回值。
*
* 除了正文,还带回上游上报的 token 用量:调用方必须把它记下来,
* 否则重试与失败尝试产生的消耗就再也拿不到了(上游不会补发)。
*/
export interface ChatResult {
content: string;
usage: TokenUsage;
usageSource: UsageSource;
/** 上游回显的模型名,可能与本项目配置的模型名不同 */
model: string;
finishReason: string | null;
}
/** 单次调用的采样参数:温度与输出上限按任务类型定,思考强度按 prompt 长度定 */
export interface ChatOptions {
temperature?: number;
@@ -53,6 +69,13 @@ export class LlmError extends Error {
* 而不是用自己的指数退避——服务商限流时自己的退避往往偏短。
*/
public readonly retryAfterMs?: number,
/**
* 这次失败的调用是否已经产生了 token 消耗。
*
* 典型场景是上游返回 200 但内容为空:请求已经计费,只是没法用。
* 有值时调用方必须把它计入用量,否则这部分消耗会凭空消失。
*/
public readonly usage?: TokenUsage,
) {
super(message);
this.name = "LlmError";
@@ -81,13 +104,13 @@ export function parseRetryAfter(value: string | null, now: number = Date.now()):
}
/**
* 非流式调用,返回完整文本
* 非流式调用,返回正文与上游上报的 token 用量。
*/
export async function chat(
config: LlmConfig,
messages: ChatMessage[],
options?: ChatOptions,
): Promise<string> {
): Promise<ChatResult> {
if (!isAvailable(config)) {
throw new LlmError("LLM API Key 未配置", 503);
}
@@ -126,13 +149,23 @@ export async function chat(
}
const data = (await response.json()) as {
choices?: { message?: { content?: string } }[];
model?: string;
choices?: { message?: { content?: string }; finish_reason?: string }[];
usage?: unknown;
};
// 先把用量取出来:即使下面因为内容为空而抛错,这次请求也已经计费了
const snapshot = normalizeUsage(data.usage);
const content = data.choices?.[0]?.message?.content;
if (!content) {
throw new LlmError("LLM 返回内容为空", 502);
throw new LlmError("LLM 返回内容为空", 502, undefined, snapshot.usage);
}
return content;
return {
content,
usage: snapshot.usage,
usageSource: snapshot.source,
model: data.model ?? config.model,
finishReason: data.choices?.[0]?.finish_reason ?? null,
};
} catch (err) {
if (err instanceof LlmError) throw err;
if (err instanceof Error && err.name === "AbortError") {