fix(usage): guard malformed DeepSeek payloads (#110785)

Co-authored-by: Leon-SK668 <17695126+Leon-SK668@users.noreply.github.com>
This commit is contained in:
Leon-SK668
2026-07-29 19:02:44 +08:00
committed by GitHub
parent e92da4c8ba
commit 7c024cad48
2 changed files with 16 additions and 3 deletions
@@ -85,6 +85,18 @@ describe("fetchDeepSeekUsage", () => {
expect(result.windows).toHaveLength(0);
});
it.each([
["null response", null],
["array response", []],
])("treats %s as absent balance data", async (_name, payload) => {
const mockFetch = createProviderUsageFetch(async () => makeResponse(200, payload));
const result = await fetchDeepSeekUsage("deepseek-key", 5000, mockFetch);
expect(result.error).toBe("No balance data");
expect(result.windows).toHaveLength(0);
});
it("marks unavailable accounts while keeping the balance summary", async () => {
const mockFetch = createProviderUsageFetch(async () =>
makeResponse(200, {
+4 -3
View File
@@ -1,4 +1,5 @@
// Fetches and normalizes DeepSeek provider usage records.
import { isRecord } from "@openclaw/normalization-core/record-coerce";
import {
buildUsageHttpErrorSnapshot,
discardUsageResponseBody,
@@ -86,8 +87,8 @@ export async function fetchDeepSeekUsage(
return parsed.snapshot;
}
const data = parsed.data as DeepSeekBalanceResponse;
const balances = Array.isArray(data.balance_infos) ? data.balance_infos : [];
const data = isRecord(parsed.data) ? (parsed.data as DeepSeekBalanceResponse) : undefined;
const balances = data && Array.isArray(data.balance_infos) ? data.balance_infos : [];
const summary = balances
.map((info) => buildBalanceSummary(info))
.filter((entry): entry is string => Boolean(entry))
@@ -120,6 +121,6 @@ export async function fetchDeepSeekUsage(
windows: [],
billing,
summary,
...(data.is_available === false ? { plan: "Unavailable" } : {}),
...(data?.is_available === false ? { plan: "Unavailable" } : {}),
};
}