import type { ComplianceProvenance, HostingProvenance, JurisdictionCountry, ModelIdentity, ProvenanceProvider, WeightsOriginCountry, } from '../receipts/schema'; export interface ProviderRouteInfo { provider: ProvenanceProvider; weights_origin_country: WeightsOriginCountry; available_jurisdictions: string[]; baa_path: string | null; hipaa_eligible: boolean; zdr_available: boolean; default_retention_days: number | null; foreign_origin?: boolean; } export interface ProviderRegistryRequest { url?: string; provider?: string; providerRoute?: string; model?: string; } export interface ProviderRegistryConfig { providerRoute?: string; jurisdictionCountry?: JurisdictionCountry; jurisdictionRegion?: string; baaInForce?: boolean; baaVendor?: string | null; dataRetentionDays?: number | null; dataResidencyAttested?: boolean; foreignOriginConsentReceiptId?: string | null; inferenceBilling?: 'agentguard_managed' | 'customer_managed'; inferenceBillingDetail?: ComplianceProvenance['inference_billing_detail']; } export const PROVIDER_REGISTRY: Record = { 'anthropic-direct': { provider: 'anthropic', weights_origin_country: 'US', available_jurisdictions: ['US'], baa_path: 'Anthropic Enterprise Messages API with BAA eligible routing', hipaa_eligible: true, zdr_available: true, default_retention_days: 0, }, 'aws-bedrock': { provider: 'anthropic', weights_origin_country: 'US', available_jurisdictions: ['us-east-1', 'us-east-2', 'us-west-2', 'eu-west-1', 'eu-central-1', 'ap-northeast-1'], baa_path: 'AWS BAA for Bedrock HIPAA eligible services', hipaa_eligible: true, zdr_available: true, default_retention_days: 0, }, 'azure-openai': { provider: 'openai', weights_origin_country: 'US', available_jurisdictions: ['eastus', 'westus', 'westeurope', 'francecentral', 'switzerlandnorth'], baa_path: 'Microsoft Product Terms DPA', hipaa_eligible: true, zdr_available: true, default_retention_days: 0, }, 'openai-direct': { provider: 'openai', weights_origin_country: 'US', available_jurisdictions: ['US'], baa_path: 'OpenAI Enterprise BAA with eligible API configuration', hipaa_eligible: true, zdr_available: true, default_retention_days: 30, }, 'vertex-ai': { provider: 'google', weights_origin_country: 'US', available_jurisdictions: ['us-central1', 'us-east4', 'europe-west4', 'europe-west1', 'asia-northeast1'], baa_path: 'Google Cloud BAA', hipaa_eligible: true, zdr_available: true, default_retention_days: 0, }, fireworks: { provider: 'fireworks', weights_origin_country: 'unknown', available_jurisdictions: ['US'], baa_path: null, hipaa_eligible: false, zdr_available: false, default_retention_days: 30, }, baseten: { provider: 'baseten', weights_origin_country: 'unknown', available_jurisdictions: ['US'], baa_path: null, hipaa_eligible: false, zdr_available: false, default_retention_days: 30, }, together: { provider: 'together', weights_origin_country: 'unknown', available_jurisdictions: ['US'], baa_path: null, hipaa_eligible: false, zdr_available: false, default_retention_days: 30, }, 'moonshot-direct': { provider: 'moonshot', weights_origin_country: 'CN', available_jurisdictions: ['CN'], baa_path: null, hipaa_eligible: false, zdr_available: false, default_retention_days: 30, foreign_origin: true, }, 'deepseek-direct': { provider: 'deepseek', weights_origin_country: 'CN', available_jurisdictions: ['CN'], baa_path: null, hipaa_eligible: false, zdr_available: false, default_retention_days: 30, foreign_origin: true, }, 'alibaba-direct': { provider: 'alibaba', weights_origin_country: 'CN', available_jurisdictions: ['CN'], baa_path: null, hipaa_eligible: false, zdr_available: false, default_retention_days: 30, foreign_origin: true, }, 'kimi-k2-on-baseten': { provider: 'baseten', weights_origin_country: 'CN', available_jurisdictions: ['US'], baa_path: null, hipaa_eligible: false, zdr_available: false, default_retention_days: 30, foreign_origin: true, }, 'deepseek-on-together': { provider: 'together', weights_origin_country: 'CN', available_jurisdictions: ['US'], baa_path: null, hipaa_eligible: false, zdr_available: false, default_retention_days: 30, foreign_origin: true, }, 'qwen-on-together': { provider: 'together', weights_origin_country: 'CN', available_jurisdictions: ['US'], baa_path: null, hipaa_eligible: false, zdr_available: false, default_retention_days: 30, foreign_origin: true, }, 'self-hosted': { provider: 'self_hosted', weights_origin_country: 'unknown', available_jurisdictions: ['self-hosted'], baa_path: null, hipaa_eligible: false, zdr_available: true, default_retention_days: 0, }, }; const FOREIGN_ORIGIN_PATTERNS = [/kimi/i, /deepseek/i, /qwen/i, /yi[-_ ]/i, /baichuan/i, /glm/i, /moonshot/i]; export function isForeignOriginModel(model: string): boolean { return FOREIGN_ORIGIN_PATTERNS.some((pattern) => pattern.test(model)); } export function inferProviderRoute(req: ProviderRegistryRequest): string { const model = String(req.model || '').toLowerCase(); const url = String(req.url || '').toLowerCase(); if ((req.providerRoute === 'baseten' || url.includes('baseten')) && /kimi/.test(model)) return 'kimi-k2-on-baseten'; if ((req.providerRoute === 'together' || url.includes('together')) && /deepseek/.test(model)) return 'deepseek-on-together'; if ((req.providerRoute === 'together' || url.includes('together')) && /qwen/.test(model)) return 'qwen-on-together'; if (req.providerRoute && PROVIDER_REGISTRY[req.providerRoute]) return req.providerRoute; if (url.includes('bedrock') || req.provider === 'bedrock') return 'aws-bedrock'; if (url.includes('anthropic') || req.provider === 'anthropic') return 'anthropic-direct'; if (url.includes('azure') || url.includes('openai.azure')) return 'azure-openai'; if (url.includes('aiplatform.googleapis') || url.includes('vertex')) return 'vertex-ai'; if (url.includes('fireworks') || req.providerRoute === 'fireworks') return 'fireworks'; if (url.includes('baseten') || req.providerRoute === 'baseten') return 'baseten'; if (url.includes('together') || req.providerRoute === 'together') return 'together'; if (url.includes('moonshot') || /kimi/.test(model)) return 'moonshot-direct'; if (url.includes('deepseek') || /deepseek/.test(model)) return 'deepseek-direct'; if (url.includes('dashscope') || url.includes('alibaba') || /qwen/.test(model)) return 'alibaba-direct'; if (req.provider === 'openai') return 'openai-direct'; if (req.provider === 'gemini') return 'vertex-ai'; return 'self-hosted'; } export function inferModelIdentity(model: string, route: string): ModelIdentity { const info = PROVIDER_REGISTRY[route] ?? PROVIDER_REGISTRY['self-hosted']; const modelId = model || 'unknown'; return { provider: modelProvider(modelId, info.provider), model_id: modelId, model_version: modelVersion(modelId), model_family: modelFamily(modelId), weights_origin_country: isForeignOriginModel(modelId) || info.foreign_origin ? 'CN' : info.weights_origin_country, }; } export function inferHosting(route: string, config: ProviderRegistryConfig = {}): HostingProvenance { const info = PROVIDER_REGISTRY[route] ?? PROVIDER_REGISTRY['self-hosted']; const region = config.jurisdictionRegion || info.available_jurisdictions[0] || 'unknown'; return { provider_route: route, jurisdiction_country: config.jurisdictionCountry || countryFromRegion(region), jurisdiction_region: region, }; } export function inferCompliance(route: string, model: ModelIdentity, config: ProviderRegistryConfig = {}): ComplianceProvenance { const info = PROVIDER_REGISTRY[route] ?? PROVIDER_REGISTRY['self-hosted']; const retention = config.dataRetentionDays !== undefined ? config.dataRetentionDays : info.default_retention_days; return { baa_in_force: config.baaInForce ?? false, baa_vendor: config.baaVendor ?? info.baa_path, hipaa_eligible: info.hipaa_eligible, data_retention_days: retention, data_residency_attested: config.dataResidencyAttested ?? false, foreign_origin_weight_flag: model.weights_origin_country === 'CN' || info.foreign_origin === true, foreign_origin_consent_receipt_id: config.foreignOriginConsentReceiptId ?? null, inference_billing: config.inferenceBilling ?? 'customer_managed', inference_billing_detail: config.inferenceBilling === 'agentguard_managed' ? config.inferenceBillingDetail ?? null : null, }; } function modelProvider(model: string, fallback: ProvenanceProvider): ProvenanceProvider { const lower = model.toLowerCase(); if (lower.includes('claude')) return 'anthropic'; if (lower.includes('gpt') || lower.includes('openai')) return 'openai'; if (lower.includes('gemini')) return 'google'; if (lower.includes('mistral')) return 'mistral'; if (lower.includes('llama')) return 'meta'; if (lower.includes('command')) return 'cohere'; if (lower.includes('deepseek')) return 'deepseek'; if (lower.includes('kimi') || lower.includes('moonshot')) return 'moonshot'; if (lower.includes('qwen') || lower.includes('yi') || lower.includes('baichuan') || lower.includes('glm')) return 'alibaba'; return fallback; } function modelVersion(model: string): string { const match = model.match(/(?:^|[-_])(20\d{6}|\d{8})(?:$|[-_])/); return match?.[1] ?? 'unknown'; } function modelFamily(model: string): string { const lower = model.toLowerCase(); if (lower.includes('claude')) return 'claude'; if (lower.includes('gpt')) return 'gpt'; if (lower.includes('gemini')) return 'gemini'; if (lower.includes('llama-4') || lower.includes('llama4')) return 'llama-4'; if (lower.includes('mistral')) return 'mistral'; if (lower.includes('kimi')) return 'kimi'; if (lower.includes('deepseek')) return 'deepseek'; if (lower.includes('qwen')) return 'qwen'; return lower.split(/[/:_]/).pop()?.split('-').slice(0, 2).join('-') || 'unknown'; } function countryFromRegion(region: string): JurisdictionCountry { const lower = region.toLowerCase(); if (lower.startsWith('us') || lower.includes('eastus') || lower.includes('westus')) return 'US'; if (lower.startsWith('eu') || lower.includes('europe') || lower.includes('france') || lower.includes('switzerland')) return 'EU'; if (lower.startsWith('uk')) return 'UK'; if (lower.startsWith('ca')) return 'CA'; if (lower.startsWith('cn')) return 'CN'; if (lower.startsWith('ap-northeast') || lower.includes('tokyo') || lower.includes('japan')) return 'JP'; if (lower.startsWith('ap-southeast-2') || lower.includes('australia')) return 'AU'; return 'unknown'; }