({
apiKey,
maxRetries,
model,
fetchOverride,
source,
}: {
apiKey?: string
maxRetries: number
model?: string
fetchOverride?: ClientOptions['fetch']
source?: string
})
| 86 | } |
| 87 | |
| 88 | export async function getAnthropicClient({ |
| 89 | apiKey, |
| 90 | maxRetries, |
| 91 | model, |
| 92 | fetchOverride, |
| 93 | source, |
| 94 | }: { |
| 95 | apiKey?: string |
| 96 | maxRetries: number |
| 97 | model?: string |
| 98 | fetchOverride?: ClientOptions['fetch'] |
| 99 | source?: string |
| 100 | }): Promise<Anthropic> { |
| 101 | const defaultHeaders = await getFirstPartyRequestHeaders() |
| 102 | const resolvedFetch = getWrappedClientFetch(fetchOverride, source) |
| 103 | |
| 104 | const ARGS = { |
| 105 | defaultHeaders, |
| 106 | maxRetries, |
| 107 | timeout: parseInt(process.env.API_TIMEOUT_MS || String(600 * 1000), 10), |
| 108 | dangerouslyAllowBrowser: true, |
| 109 | fetchOptions: getProxyFetchOptions({ |
| 110 | forAnthropicAPI: true, |
| 111 | }) as ClientOptions['fetchOptions'], |
| 112 | ...(resolvedFetch && { |
| 113 | fetch: resolvedFetch, |
| 114 | }), |
| 115 | } |
| 116 | if (isEnvTruthy(process.env.CLAUDE_CODE_USE_BEDROCK)) { |
| 117 | const { AnthropicBedrock } = await import('@anthropic-ai/bedrock-sdk') |
| 118 | // Use region override for small fast model if specified |
| 119 | const awsRegion = |
| 120 | model === getSmallFastModel() && |
| 121 | process.env.ANTHROPIC_SMALL_FAST_MODEL_AWS_REGION |
| 122 | ? process.env.ANTHROPIC_SMALL_FAST_MODEL_AWS_REGION |
| 123 | : getAWSRegion() |
| 124 | |
| 125 | const bedrockArgs: ConstructorParameters<typeof AnthropicBedrock>[0] = { |
| 126 | ...ARGS, |
| 127 | awsRegion, |
| 128 | ...(isEnvTruthy(process.env.CLAUDE_CODE_SKIP_BEDROCK_AUTH) && { |
| 129 | skipAuth: true, |
| 130 | }), |
| 131 | ...(isDebugToStdErr() && { logger: createStderrLogger() }), |
| 132 | } |
| 133 | |
| 134 | // Add API key authentication if available |
| 135 | if (process.env.AWS_BEARER_TOKEN_BEDROCK) { |
| 136 | bedrockArgs.skipAuth = true |
| 137 | // Add the Bearer token for Bedrock API key authentication |
| 138 | bedrockArgs.defaultHeaders = { |
| 139 | ...bedrockArgs.defaultHeaders, |
| 140 | Authorization: `Bearer ${process.env.AWS_BEARER_TOKEN_BEDROCK}`, |
| 141 | } |
| 142 | } else if (!isEnvTruthy(process.env.CLAUDE_CODE_SKIP_BEDROCK_AUTH)) { |
| 143 | // Refresh auth and get credentials with cache clearing |
| 144 | const cachedCredentials = await refreshCurrentAwsCredentials() |
| 145 | if (cachedCredentials) { |
no test coverage detected