AIX: Anthropic: make beta headers reusable

This commit is contained in:
Enrico Ros
2026-02-24 16:46:10 -08:00
parent 6116af42df
commit 237065553e
@@ -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<string, string> {
// 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<string, str
if (options?.enableStrictOutputs)
betaFeatures.push('structured-outputs-2025-11-13');
return {
...DEFAULT_ANTHROPIC_HEADERS,
// CORS: allow browser access to Anthropic API servers
...(options?.clientSideFetch ? { 'anthropic-dangerous-direct-browser-access': 'true' } : {}),
// Beta features
...(betaFeatures.length ? { 'anthropic-beta': betaFeatures.join(',') } : {}),
};
return betaFeatures;
}