YuJitang 417416087b
fix: use backend thread token usage for header total (#2800)
* fix: use backend thread token usage for header total

* Refactor thread token usage fetch
2026-05-09 19:40:32 +08:00

25 lines
685 B
TypeScript

import { fetch as fetchWithAuth } from "@/core/api/fetcher";
import { getBackendBaseURL } from "@/core/config";
import type { ThreadTokenUsageResponse } from "./types";
export async function fetchThreadTokenUsage(
threadId: string,
): Promise<ThreadTokenUsageResponse | null> {
const response = await fetchWithAuth(
`${getBackendBaseURL()}/api/threads/${encodeURIComponent(threadId)}/token-usage`,
{
method: "GET",
},
);
if (!response.ok) {
if (response.status === 403 || response.status === 404) {
return null;
}
throw new Error("Failed to load thread token usage.");
}
return (await response.json()) as ThreadTokenUsageResponse;
}