Skip to content

Commit 2c60276

Browse files
committed
fix(usage): guard malformed DeepSeek payloads
1 parent 0951d0d commit 2c60276

2 files changed

Lines changed: 16 additions & 3 deletions

File tree

‎src/infra/provider-usage.fetch.deepseek.test.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,18 @@ describe("fetchDeepSeekUsage", () => {
8585
expect(result.windows).toHaveLength(0);
8686
});
8787

88+
it.each([
89+
["null response", null],
90+
["array response", []],
91+
])("treats %s as absent balance data", async (_name, payload) => {
92+
const mockFetch = createProviderUsageFetch(async () => makeResponse(200, payload));
93+
94+
const result = await fetchDeepSeekUsage("deepseek-key", 5000, mockFetch);
95+
96+
expect(result.error).toBe("No balance data");
97+
expect(result.windows).toHaveLength(0);
98+
});
99+
88100
it("marks unavailable accounts while keeping the balance summary", async () => {
89101
const mockFetch = createProviderUsageFetch(async () =>
90102
makeResponse(200, {

‎src/infra/provider-usage.fetch.deepseek.ts‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// Fetches and normalizes DeepSeek provider usage records.
2+
import { isRecord } from "@openclaw/normalization-core/record-coerce";
23
import {
34
buildUsageHttpErrorSnapshot,
45
discardUsageResponseBody,
@@ -86,8 +87,8 @@ export async function fetchDeepSeekUsage(
8687
return parsed.snapshot;
8788
}
8889

89-
const data = parsed.data as DeepSeekBalanceResponse;
90-
const balances = Array.isArray(data.balance_infos) ? data.balance_infos : [];
90+
const data = isRecord(parsed.data) ? (parsed.data as DeepSeekBalanceResponse) : undefined;
91+
const balances = data && Array.isArray(data.balance_infos) ? data.balance_infos : [];
9192
const summary = balances
9293
.map((info) => buildBalanceSummary(info))
9394
.filter((entry): entry is string => Boolean(entry))
@@ -120,6 +121,6 @@ export async function fetchDeepSeekUsage(
120121
windows: [],
121122
billing,
122123
summary,
123-
...(data.is_available === false ? { plan: "Unavailable" } : {}),
124+
...(data?.is_available === false ? { plan: "Unavailable" } : {}),
124125
};
125126
}

0 commit comments

Comments
 (0)