basant307/AI_Governance_Project
045
1/**2 * @license3 * Copyright 2025 Qwen Team4 * SPDX-License-Identifier: Apache-2.05 */6 7export function getDefaultApiKeyEnvVar(authType: string | undefined): string {8 switch (authType) {9 case 'openai':10 return 'OPENAI_API_KEY';11 case 'anthropic':12 return 'ANTHROPIC_API_KEY';13 case 'gemini':14 return 'GEMINI_API_KEY';15 case 'vertex-ai':16 return 'GOOGLE_API_KEY';17 default:18 return 'API_KEY';19 }20}21 22export function getDefaultModelEnvVar(authType: string | undefined): string {23 switch (authType) {24 case 'openai':25 return 'OPENAI_MODEL';26 case 'anthropic':27 return 'ANTHROPIC_MODEL';28 case 'gemini':29 return 'GEMINI_MODEL';30 case 'vertex-ai':31 return 'GOOGLE_MODEL';32 default:33 return 'MODEL';34 }35}36 37export abstract class ModelConfigError extends Error {38 abstract readonly code: string;39 40 protected constructor(message: string) {41 super(message);42 this.name = new.target.name;43 Object.setPrototypeOf(this, new.target.prototype);44 }45}46 47export class StrictMissingCredentialsError extends ModelConfigError {48 readonly code = 'STRICT_MISSING_CREDENTIALS';49 50 constructor(51 authType: string | undefined,52 model: string | undefined,53 envKey?: string,54 ) {55 const providerKey = authType || '(unknown)';56 const modelName = model || '(unknown)';57 super(58 `Missing credentials for modelProviders model '${modelName}'. ` +59 (envKey60 ? `Current configured envKey: '${envKey}'. Set that environment variable, or update modelProviders.${providerKey}[].envKey.`61 : `Configure modelProviders.${providerKey}[].envKey and set that environment variable.`),62 );63 }64}65 66export class StrictMissingModelIdError extends ModelConfigError {67 readonly code = 'STRICT_MISSING_MODEL_ID';68 69 constructor(authType: string | undefined) {70 super(71 `Missing model id for strict modelProviders resolution (authType: ${authType}).`,72 );73 }74}75 76export class MissingApiKeyError extends ModelConfigError {77 readonly code = 'MISSING_API_KEY';78 79 constructor(params: {80 authType: string | undefined;81 model: string | undefined;82 baseUrl: string | undefined;83 envKey: string;84 }) {85 super(86 `Missing API key for ${params.authType} auth. ` +87 `Current model: '${params.model || '(unknown)'}', baseUrl: '${params.baseUrl || '(default)'}'. ` +88 `Provide an API key via settings (security.auth.apiKey), ` +89 `or set the environment variable '${params.envKey}'.`,90 );91 }92}93 94export class MissingModelError extends ModelConfigError {95 readonly code = 'MISSING_MODEL';96 97 constructor(params: { authType: string | undefined; envKey: string }) {98 super(99 `Missing model for ${params.authType} auth. ` +100 `Set the environment variable '${params.envKey}'.`,101 );102 }103}104 105export class MissingBaseUrlError extends ModelConfigError {106 readonly code = 'MISSING_BASE_URL';107 108 constructor(params: {109 authType: string | undefined;110 model: string | undefined;111 }) {112 super(113 `Missing baseUrl for modelProviders model '${params.model || '(unknown)'}'. ` +114 `Configure modelProviders.${params.authType || '(unknown)'}[].baseUrl.`,115 );116 }117}118 119export class MissingAnthropicBaseUrlEnvError extends ModelConfigError {120 readonly code = 'MISSING_ANTHROPIC_BASE_URL_ENV';121 122 constructor() {123 super('ANTHROPIC_BASE_URL environment variable not found.');124 }125}126 