feat: 采集 LLM token 用量并上报 lpt-be
- 客户端解析上游 usage(含 DeepSeek 缓存命中/未命中),跨重试累加 - 新增计价表(三档单价 + 峰谷时段),费用在结算时冻结 - 新增用量上报与本地 outbox 重试;4xx 判定为确定性拒绝,不无限重试 - 面板补充 Token/费用/调用次数与计价快照 - 顺带修复面板内联脚本转义错误(模板字面量吃掉 \n 与 \',面板一直空白)
This commit is contained in:
+123
-22
@@ -1,19 +1,46 @@
|
||||
/**
|
||||
* 请求日志存储 —— 环形缓冲区 + 可选文件持久化。
|
||||
*
|
||||
* 每条日志记录 AI 路由的一次完整请求/响应,供 /admin 面板查看。
|
||||
* 每条日志记录 AI 路由的一次完整请求/响应,供 /admin 面板查看,
|
||||
* 并携带该次请求的 token 用量与计价快照。
|
||||
*
|
||||
* 设计要点:
|
||||
* - 这里只是**最近的调试视图**:真正的长期账本在 lpt-be 的 MySQL(见 reporter.ts)。
|
||||
* 因此保留条数有限,用量统计的权威口径不在这个文件里。
|
||||
* - **原子落盘**:直写会在进程被杀时留下截断的 JSON,把连同历史用量在内的
|
||||
* 整个文件一起毁掉,所以先写临时文件再 rename 替换。
|
||||
* - `usage` 里的字段可以为 null(上游未上报),**不要用 0 代替**。
|
||||
*/
|
||||
|
||||
import { appendFileSync, existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
||||
import { existsSync, mkdirSync, readFileSync, renameSync, writeFileSync } from "node:fs";
|
||||
import { resolve } from "node:path";
|
||||
import { randomUUID } from "node:crypto";
|
||||
import type { TokenUsage, UsageSource } from "../llm/usage.js";
|
||||
import type { PricingSnapshot } from "./pricing.js";
|
||||
import { usageReporter } from "./reporter.js";
|
||||
|
||||
// ---- 类型 ----
|
||||
|
||||
export interface LogEntry {
|
||||
id: number;
|
||||
/** 上报幂等键:lpt-be 按它去重,重复推送不会重复入账 */
|
||||
recordId: string;
|
||||
timestamp: string; // ISO 8601
|
||||
endpoint: string; // 如 "/ai/aggregate-report"
|
||||
endpoint: string; // 如 "/ai/tasks#aggregate-report"
|
||||
/** 任务类型;`/fetch-title` 这类非任务端点为空 */
|
||||
taskType?: string;
|
||||
/** 任务标识,便于把面板上的一条日志对回具体任务 */
|
||||
taskId?: string;
|
||||
model: string;
|
||||
/** 本次请求实际发起的 LLM 调用次数(含失败重试);重试会重复计费 */
|
||||
attempts?: number;
|
||||
/** false 表示这次请求没有消耗 LLM(如抓取网页标题) */
|
||||
isLlmCall: boolean;
|
||||
usage?: TokenUsage;
|
||||
usageSource?: UsageSource;
|
||||
/** 结算时算好并冻结的费用;null 表示用量未上报、无法计价 */
|
||||
costUsd?: number | null;
|
||||
pricing?: PricingSnapshot;
|
||||
durationMs: number;
|
||||
status: number; // HTTP status code
|
||||
requestBody: unknown; // Java 端发来的原始请求体
|
||||
@@ -21,65 +48,120 @@ export interface LogEntry {
|
||||
params?: Record<string, unknown>; // 模型调用参数(temperature、max_tokens 等)
|
||||
responseBody: unknown;
|
||||
error?: string;
|
||||
/** 失败分类,便于按错误类型聚合(timeout / rate_limited / ...) */
|
||||
errorCode?: string;
|
||||
}
|
||||
|
||||
export interface LogSummary {
|
||||
id: number;
|
||||
timestamp: string;
|
||||
endpoint: string;
|
||||
taskType?: string;
|
||||
model: string;
|
||||
attempts?: number;
|
||||
isLlmCall: boolean;
|
||||
usage?: TokenUsage;
|
||||
usageSource?: UsageSource;
|
||||
costUsd?: number | null;
|
||||
durationMs: number;
|
||||
status: number;
|
||||
error?: string;
|
||||
errorCode?: string;
|
||||
}
|
||||
|
||||
// ---- 存储实现 ----
|
||||
|
||||
const MAX_ENTRIES = 200;
|
||||
const DATA_DIR = resolve(process.cwd(), "data");
|
||||
const LOG_FILE = resolve(DATA_DIR, "logs.json");
|
||||
/** 面板只展示最近若干条;长期账本在 lpt-be,这里不需要留太多 */
|
||||
const MAX_ENTRIES = 500;
|
||||
|
||||
class LogStore {
|
||||
/**
|
||||
* 数据目录。
|
||||
*
|
||||
* 可配置是为了两件事:部署时能挂卷,以及**测试不污染开发者的真实面板数据**——
|
||||
* 单例默认写在 `process.cwd()/data`,测试里一旦触发落盘就会把真实的 logs.json 清掉。
|
||||
* 路径惰性解析:ESM 的 import 先于测试里设置环境变量执行,模块顶层解析会读不到。
|
||||
*/
|
||||
function dataDir(): string {
|
||||
const configured = (process.env.LPT_AI_DATA_DIR ?? "").trim();
|
||||
return configured.length > 0 ? configured : resolve(process.cwd(), "data");
|
||||
}
|
||||
|
||||
function logFile(): string {
|
||||
return resolve(dataDir(), "logs.json");
|
||||
}
|
||||
|
||||
/**
|
||||
* 把旧格式的历史日志补齐到当前结构。
|
||||
*
|
||||
* 用量字段是这次才加的,旧条目没有 `isLlmCall` 也没有 `recordId`;
|
||||
* 不补的话它们会被当成「非 LLM 请求」而从用量统计里静默消失(比报错更难发现)。
|
||||
* 旧条目无法回溯 token,所以用量保持未上报,但 `isLlmCall` 可以从端点推出。
|
||||
*/
|
||||
function migrateEntry(entry: LogEntry): LogEntry {
|
||||
return {
|
||||
...entry,
|
||||
recordId: entry.recordId ?? randomUUID(),
|
||||
isLlmCall: entry.isLlmCall ?? entry.endpoint.startsWith("/ai/tasks"),
|
||||
};
|
||||
}
|
||||
|
||||
export class LogStore {
|
||||
private entries: LogEntry[] = [];
|
||||
private nextId = 1;
|
||||
private dirty = false;
|
||||
private loaded = false;
|
||||
|
||||
constructor() {
|
||||
this.load();
|
||||
}
|
||||
|
||||
/** 追加一条日志,超出上限时移除最旧的 */
|
||||
add(entry: Omit<LogEntry, "id">): LogEntry {
|
||||
const full: LogEntry = { id: this.nextId++, ...entry };
|
||||
/** 追加一条日志,超出上限时移除最旧的;同时投递到用量上报队列 */
|
||||
add(entry: Omit<LogEntry, "id" | "recordId"> & { recordId?: string }): LogEntry {
|
||||
this.ensureLoaded();
|
||||
const full: LogEntry = { id: this.nextId++, recordId: entry.recordId ?? randomUUID(), ...entry };
|
||||
this.entries.push(full);
|
||||
while (this.entries.length > MAX_ENTRIES) {
|
||||
this.entries.shift();
|
||||
}
|
||||
this.dirty = true;
|
||||
this.flush(); // fire-and-forget
|
||||
// 上报失败由 reporter 自己持久化重试,这里不阻塞请求
|
||||
usageReporter.enqueue(full);
|
||||
return full;
|
||||
}
|
||||
|
||||
/** 列出摘要(不含请求/响应体,减少面板传输量) */
|
||||
list(endpoint?: string): LogSummary[] {
|
||||
this.ensureLoaded();
|
||||
let result = [...this.entries];
|
||||
if (endpoint) {
|
||||
result = result.filter((e) => e.endpoint === endpoint);
|
||||
}
|
||||
// 倒序:最新在前
|
||||
result.reverse();
|
||||
return result.map(({ id, timestamp, endpoint, model, durationMs, status, error }) => ({
|
||||
id, timestamp, endpoint, model, durationMs, status, error,
|
||||
return result.map((entry) => ({
|
||||
id: entry.id,
|
||||
timestamp: entry.timestamp,
|
||||
endpoint: entry.endpoint,
|
||||
...(entry.taskType !== undefined ? { taskType: entry.taskType } : {}),
|
||||
model: entry.model,
|
||||
...(entry.attempts !== undefined ? { attempts: entry.attempts } : {}),
|
||||
isLlmCall: entry.isLlmCall,
|
||||
...(entry.usage !== undefined ? { usage: entry.usage } : {}),
|
||||
...(entry.usageSource !== undefined ? { usageSource: entry.usageSource } : {}),
|
||||
...(entry.costUsd !== undefined ? { costUsd: entry.costUsd } : {}),
|
||||
durationMs: entry.durationMs,
|
||||
status: entry.status,
|
||||
...(entry.error !== undefined ? { error: entry.error } : {}),
|
||||
...(entry.errorCode !== undefined ? { errorCode: entry.errorCode } : {}),
|
||||
}));
|
||||
}
|
||||
|
||||
/** 获取单条完整日志 */
|
||||
get(id: number): LogEntry | undefined {
|
||||
this.ensureLoaded();
|
||||
return this.entries.find((e) => e.id === id);
|
||||
}
|
||||
|
||||
/** 清空 */
|
||||
clear(): void {
|
||||
this.ensureLoaded();
|
||||
this.entries = [];
|
||||
this.dirty = true;
|
||||
this.flush();
|
||||
@@ -87,18 +169,26 @@ class LogStore {
|
||||
|
||||
/** 端点列表(用于筛选下拉) */
|
||||
endpoints(): string[] {
|
||||
this.ensureLoaded();
|
||||
return [...new Set(this.entries.map((e) => e.endpoint))].sort();
|
||||
}
|
||||
|
||||
// ---- 持久化 ----
|
||||
|
||||
private ensureLoaded(): void {
|
||||
if (this.loaded) return;
|
||||
this.loaded = true;
|
||||
this.load();
|
||||
}
|
||||
|
||||
private load(): void {
|
||||
try {
|
||||
if (existsSync(LOG_FILE)) {
|
||||
const raw = readFileSync(LOG_FILE, "utf-8");
|
||||
const file = logFile();
|
||||
if (existsSync(file)) {
|
||||
const raw = readFileSync(file, "utf-8");
|
||||
const data = JSON.parse(raw) as { nextId: number; entries: LogEntry[] };
|
||||
this.nextId = data.nextId ?? 1;
|
||||
this.entries = data.entries ?? [];
|
||||
this.entries = (data.entries ?? []).map((entry) => migrateEntry(entry));
|
||||
// 裁剪
|
||||
while (this.entries.length > MAX_ENTRIES) {
|
||||
this.entries.shift();
|
||||
@@ -113,10 +203,16 @@ class LogStore {
|
||||
if (!this.dirty) return;
|
||||
this.dirty = false;
|
||||
try {
|
||||
if (!existsSync(DATA_DIR)) {
|
||||
mkdirSync(DATA_DIR, { recursive: true });
|
||||
const dir = dataDir();
|
||||
const file = logFile();
|
||||
if (!existsSync(dir)) {
|
||||
mkdirSync(dir, { recursive: true });
|
||||
}
|
||||
writeFileSync(LOG_FILE, JSON.stringify({ nextId: this.nextId, entries: this.entries }), "utf-8");
|
||||
// 先写临时文件再原子替换:直写被中断会留下截断的 JSON,
|
||||
// 下次启动读不出来就把历史用量一起丢了
|
||||
const tmpFile = `${file}.tmp`;
|
||||
writeFileSync(tmpFile, JSON.stringify({ nextId: this.nextId, entries: this.entries }), "utf-8");
|
||||
renameSync(tmpFile, file);
|
||||
} catch {
|
||||
// 写入失败不阻塞服务
|
||||
}
|
||||
@@ -124,3 +220,8 @@ class LogStore {
|
||||
}
|
||||
|
||||
export const logStore = new LogStore();
|
||||
|
||||
/** 仅用于测试:重置内部状态,避免用例之间互相污染 */
|
||||
export function resetLogStoreForTest(): void {
|
||||
logStore.clear();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user