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
+11
View File
@@ -4,6 +4,7 @@ import { dirname, resolve } from "node:path";
import Fastify from "fastify";
import { aiRoutes } from "./routes/ai.js";
import { adminRoutes } from "./admin/index.js";
import { usageReporter } from "./admin/reporter.js";
// ===== 加载 .env(零依赖,兼容 dev/start 两种启动方式)=====
const __dirname = dirname(fileURLToPath(import.meta.url));
@@ -36,6 +37,16 @@ const app = Fastify({
await app.register(aiRoutes);
await app.register(adminRoutes);
// 用量上报(lpt-ai → lpt-be):未配置 LPT_AI_REPORT_URL 时为空操作。
// 必须放在 .env 加载之后:ESM 的 import 先于本文件顶层代码执行,
// 在模块顶层读环境变量永远读不到 .env 里的值。
if (usageReporter.isConfigured()) {
usageReporter.start();
app.log.info("用量上报已启用,待上报条数 " + usageReporter.pendingCount());
} else {
app.log.info("未配置 LPT_AI_REPORT_URL,用量只保留在本地面板(无长期账本)");
}
const port = Number(process.env.PORT ?? 5199);
try {