()
| 67 | * Returns either OAuth headers for Max/Pro users or API key headers for regular users |
| 68 | */ |
| 69 | export function getAuthHeaders(): AuthHeaders { |
| 70 | if (isClaudeAISubscriber()) { |
| 71 | const oauthTokens = getClaudeAIOAuthTokens() |
| 72 | if (!oauthTokens?.accessToken) { |
| 73 | return { |
| 74 | headers: {}, |
| 75 | error: 'No OAuth token available', |
| 76 | } |
| 77 | } |
| 78 | return { |
| 79 | headers: { |
| 80 | Authorization: `Bearer ${oauthTokens.accessToken}`, |
| 81 | 'anthropic-beta': OAUTH_BETA_HEADER, |
| 82 | }, |
| 83 | } |
| 84 | } |
| 85 | // TODO: this will fail if the API key is being set to an LLM Gateway key |
| 86 | // should we try to query keychain / credentials for a valid Anthropic key? |
| 87 | const apiKey = getAnthropicApiKey() |
| 88 | if (!apiKey) { |
| 89 | return { |
| 90 | headers: {}, |
| 91 | error: 'No API key available', |
| 92 | } |
| 93 | } |
| 94 | return { |
| 95 | headers: { |
| 96 | 'x-api-key': apiKey, |
| 97 | }, |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Wrapper that handles OAuth 401 errors by force-refreshing the token and |
no test coverage detected