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
+74
View File
@@ -23,6 +23,18 @@ function mockFetchOnce() {
return fetchMock;
}
function mockFetchJson(payload: unknown, ok = true, status = 200) {
const fetchMock = vi.fn().mockResolvedValue({
ok,
status,
json: async () => payload,
text: async () => JSON.stringify(payload),
headers: new Headers(),
} as unknown as Response);
vi.stubGlobal("fetch", fetchMock);
return fetchMock;
}
function bodyOf(fetchMock: ReturnType<typeof mockFetchOnce>): Record<string, unknown> {
const init = fetchMock.mock.calls[0][1] as RequestInit;
return JSON.parse(String(init.body)) as Record<string, unknown>;
@@ -64,3 +76,65 @@ describe("chat 请求体", () => {
expect(bodyOf(fetchMock)).not.toHaveProperty("reasoning_effort");
});
});
describe("chat 返回值", () => {
it("带回上游上报的 token 用量与模型名", async () => {
mockFetchJson({
model: "deepseek-flash-0715",
choices: [{ message: { content: "ok" }, finish_reason: "stop" }],
usage: {
prompt_tokens: 1000,
completion_tokens: 200,
total_tokens: 1200,
prompt_cache_hit_tokens: 800,
prompt_cache_miss_tokens: 200,
},
});
const result = await chat(llmConfig, [{ role: "user", content: "hi" }]);
expect(result.content).toBe("ok");
expect(result.model).toBe("deepseek-flash-0715");
expect(result.finishReason).toBe("stop");
expect(result.usageSource).toBe("upstream");
expect(result.usage).toMatchObject({
promptTokens: 1000,
completionTokens: 200,
cacheHitTokens: 800,
cacheMissTokens: 200,
});
});
it("上游没带 usage 时标为 missing,字段全为 null", async () => {
mockFetchJson({ choices: [{ message: { content: "ok" } }] });
const result = await chat(llmConfig, [{ role: "user", content: "hi" }]);
expect(result.usageSource).toBe("missing");
expect(result.usage.promptTokens).toBeNull();
});
it("返回 200 但内容为空时,把已产生的用量挂在错误上带回来", async () => {
mockFetchJson({
choices: [{ message: { content: "" } }],
usage: { prompt_tokens: 500, completion_tokens: 0, total_tokens: 500 },
});
await expect(chat(llmConfig, [{ role: "user", content: "hi" }])).rejects.toMatchObject({
statusCode: 502,
usage: { promptTokens: 500 },
});
});
it("上游报错时错误不带用量(上游没产生可计费的输出)", async () => {
mockFetchJson({ error: "rate limited" }, false, 429);
await expect(chat(llmConfig, [{ role: "user", content: "hi" }])).rejects.toMatchObject({
statusCode: 429,
});
await expect(chat(llmConfig, [{ role: "user", content: "hi" }])).rejects.toHaveProperty(
"usage",
undefined,
);
});
});