MCPcopy Index your code
hub / github.com/CodebuffAI/codebuff / fetchAndUpdateUsage

Function fetchAndUpdateUsage

cli/src/utils/fetch-usage.ts:25–76  ·  view source on GitHub ↗
(
  params: FetchAndUpdateUsageParams = {},
)

Source from the content-addressed store, hash-verified

23 * Returns true if successful, false otherwise.
24 */
25export async function fetchAndUpdateUsage(
26 params: FetchAndUpdateUsageParams = {},
27): Promise<boolean> {
28 const {
29 showBanner = false,
30 getAuthToken: getAuthTokenFn = getAuthToken,
31 getChatStore = () => useChatStore.getState(),
32 logger: loggerInstance = logger,
33 apiClient: providedApiClient,
34 } = params
35
36 const authToken = getAuthTokenFn()
37 const chatStore = getChatStore()
38
39 if (!authToken) {
40 loggerInstance.debug('Cannot fetch usage: not authenticated')
41 return false
42 }
43
44 const apiClient =
45 providedApiClient ?? getApiClient()
46
47 try {
48 const response = await apiClient.usage()
49
50 if (!response.ok) {
51 loggerInstance.error(
52 { status: response.status, errorText: response.error },
53 'Usage request failed',
54 )
55 return false
56 }
57
58 // Note: This function is deprecated. Use useUsageQuery hook instead.
59 // We no longer update the store here since usage data is managed by TanStack Query.
60
61 if (showBanner) {
62 chatStore.setInputMode('usage')
63 }
64
65 return true
66 } catch (error) {
67 loggerInstance.error(
68 {
69 error: error instanceof Error ? error.message : String(error),
70 errorStack: error instanceof Error ? error.stack : undefined,
71 },
72 'Error fetching usage',
73 )
74 return false
75 }
76}

Callers 1

Calls 2

getApiClientFunction · 0.90
usageMethod · 0.80

Tested by

no test coverage detected