diff --git a/src/modules/llms/server/anthropic/anthropic.access.ts b/src/modules/llms/server/anthropic/anthropic.access.ts index f5ef5a958..bf01aa61f 100644 --- a/src/modules/llms/server/anthropic/anthropic.access.ts +++ b/src/modules/llms/server/anthropic/anthropic.access.ts @@ -39,17 +39,19 @@ export const ANTHROPIC_API_PATHS = { } as const; -const DEFAULT_ANTHROPIC_HEADERS = { +const ANTHROPIC_HEADERS_VERSION = { // Latest version hasn't changed (as of Feb 2025) 'anthropic-version': '2023-06-01', - // Enable CORS for browsers - we don't use this on server - // 'anthropic-dangerous-direct-browser-access': 'true', - - // Used for instance by Claude Code - shall we set it + // Used for instance by Claude Code - shall we set it? // 'x-app': 'big-agi', } as const; +const ANTHROPIC_HEADERS_CORS = { + // CORS header to allow browser access to Anthropic API servers + 'anthropic-dangerous-direct-browser-access': 'true', +} as const; + const DEFAULT_ANTHROPIC_BETA_FEATURES: string[] = [ // Known SDK beta headers (for reference, not all used): @@ -121,15 +123,19 @@ export function anthropicAccess(access: AnthropicAccessSchema, apiPath: string, anthropicHost = `https://${DEFAULT_HELICONE_ANTHROPIC_HOST}`; } - // [CSF] add CORS-allow header if client-side fetch - if (access.clientSideFetch) - options = { ...options, clientSideFetch: true }; + // Beta features + const betaFeatures = anthropicBetaFeatures(options); return { headers: { 'Accept': 'application/json', 'Content-Type': 'application/json', - ..._anthropicHeaders(options), + // anthropic-version + ...ANTHROPIC_HEADERS_VERSION, + // [CSF] add CORS-allow header to allow browser access to Anthropic API servers + ...(access.clientSideFetch && ANTHROPIC_HEADERS_CORS), + // Beta features + ...(betaFeatures.length && { 'anthropic-beta': betaFeatures.join(',') }), 'X-API-Key': anthropicKey, ...(heliKey && { 'Helicone-Auth': `Bearer ${heliKey}` }), }, @@ -138,9 +144,11 @@ export function anthropicAccess(access: AnthropicAccessSchema, apiPath: string, } -function _anthropicHeaders(options?: AnthropicHeaderOptions): Record { - - // accumulate the beta features +/** + * Build the list of Anthropic beta feature strings from options. + * Used by both the direct Anthropic path (as header) and Bedrock path (as body field). + */ +export function anthropicBetaFeatures(options?: AnthropicHeaderOptions): string[] { const betaFeatures = [...DEFAULT_ANTHROPIC_BETA_FEATURES]; if (options?.modelIdForBetaFeatures) { // string search (.includes) within the keys, to be more resilient to modelId changes/prefixing @@ -181,11 +189,5 @@ function _anthropicHeaders(options?: AnthropicHeaderOptions): Record