(
params: FetchAndUpdateUsageParams = {},
)
| 23 | * Returns true if successful, false otherwise. |
| 24 | */ |
| 25 | export 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 | } |
no test coverage detected