UNPKG

@ui18n/angular

Version:

🅰️ Modern Angular internationalization with standalone components, signals, and dependency injection support for Angular 15+

218 lines 6.32 kB
/** * UI18n双API配置系统 * 支持大模型API(开发者自定义)和账户API(UI18n云端服务)的独立配置 * 集成安全配置管理 */ import { SupportedLanguage } from './types'; export type AIProviderType = 'openai' | 'anthropic' | 'google' | 'deepl' | 'azure' | 'aws' | 'baidu' | 'tencent' | 'youdao' | 'alibaba' | 'custom'; export interface AIProviderConfig { /** 提供商类型 */ type: AIProviderType; /** API密钥 */ apiKey: string; /** 自定义端点(用于自建服务) */ endpoint?: string; /** 请求超时时间(毫秒) */ timeout?: number; /** 区域设置(用于AWS、Azure等) */ region?: string; /** 模型/引擎选择 */ model?: string; /** 额外的认证参数 */ auth?: { secretKey?: string; appId?: string; appSecret?: string; tenantId?: string; subscriptionKey?: string; }; /** 服务质量设置 */ quality?: { priority: 'speed' | 'quality' | 'balanced'; maxRetries: number; fallbackEnabled: boolean; }; } export interface CloudServiceConfig { /** UI18n账户API密钥 */ accountApiKey: string; /** 云端服务端点(可选,默认为官方服务) */ endpoint?: string; /** 请求超时时间(毫秒) */ timeout?: number; /** 是否启用云端回退(当大模型API失败时) */ fallbackEnabled?: boolean; /** 速率限制配置 */ rateLimiting?: { maxRequestsPerMinute?: number; maxRequestsPerDay?: number; maxCharsPerRequest?: number; }; /** 成本控制 */ costControl?: { dailyBudget?: number; monthlyBudget?: number; warningThreshold?: number; }; } export interface DualAPIConfig { /** 大模型API配置(开发者自定义) */ aiProvider?: AIProviderConfig; /** 云端服务配置(UI18n官方服务) */ cloudService?: CloudServiceConfig; /** API密钥配置(用于安全配置管理) */ apiKeys?: Record<string, string>; /** 回退策略 */ fallbackStrategy?: { /** 回退顺序 */ order: Array<'aiProvider' | 'cloudService' | 'cache' | 'builtin'>; /** 错误重试次数 */ retryCount?: number; /** 重试间隔(毫秒) */ retryDelay?: number; }; /** 错误处理配置 */ errorHandling?: { /** 是否在用户端显示错误 */ showUserErrors?: boolean; /** 是否记录详细日志 */ enableDetailedLogging?: boolean; /** 错误上报端点 */ errorReportingEndpoint?: string; }; /** 安全配置选项 */ security?: { /** 是否启用配置加密 */ enableEncryption?: boolean; /** 自定义加密密钥 */ encryptionKey?: string; /** 配置文件路径 */ configPath?: string; /** 是否优先使用环境变量 */ preferEnvironmentVars?: boolean; }; } export declare enum APIStatus { IDLE = "idle", LOADING = "loading", SUCCESS = "success", ERROR = "error", TIMEOUT = "timeout", RATE_LIMITED = "rate_limited" } export interface APICallResult<T = any> { status: APIStatus; data?: T; error?: { code: string; message: string; details?: any; }; source: 'aiProvider' | 'cloudService' | 'cache' | 'builtin'; duration: number; retryCount?: number; } export interface TranslationAPIRequest { /** 要翻译的文本 */ text: string; /** 源语言 */ from: SupportedLanguage; /** 目标语言 */ to: SupportedLanguage; /** 上下文信息 */ context?: string; /** 强制使用特定API */ forceAPI?: 'aiProvider' | 'cloudService'; /** 跳过缓存 */ skipCache?: boolean; /** 请求优先级 */ priority?: 'low' | 'normal' | 'high'; /** 用户ID(用于云端服务计费) */ userId?: string; } export interface TranslationAPIResponse { /** 翻译后的文本 */ text: string; /** 源语言 */ sourceLanguage: SupportedLanguage; /** 目标语言 */ targetLanguage: SupportedLanguage; /** 翻译来源 */ source: 'aiProvider' | 'cloudService' | 'cache' | 'builtin'; /** 置信度(0-1) */ confidence: number; /** 翻译耗时(毫秒) */ duration: number; /** 使用的API信息 */ apiInfo?: { provider: string; model?: string; tokensUsed?: number; cost?: number; }; } export declare class APIConfigValidator { /** * 验证大模型API配置 */ static validateAIProviderConfig(config: AIProviderConfig): { valid: boolean; errors: string[]; }; /** * 验证云端服务配置 */ static validateCloudServiceConfig(config: CloudServiceConfig): { valid: boolean; errors: string[]; }; /** * 验证双API配置 */ static validateDualAPIConfig(config: DualAPIConfig): { valid: boolean; errors: string[]; }; } export declare class DefaultAPIConfig { /** * 创建默认的大模型API配置 */ static createDefaultAIProviderConfig(type: AIProviderType, apiKey: string): AIProviderConfig; /** * 创建默认的云端服务配置 */ static createDefaultCloudServiceConfig(accountApiKey: string): CloudServiceConfig; /** * 创建默认的双API配置 */ static createDefaultDualAPIConfig(options: { aiProvider?: { type: AIProviderType; apiKey: string; }; cloudService?: { accountApiKey: string; }; security?: { enableEncryption?: boolean; encryptionKey?: string; preferEnvironmentVars?: boolean; }; }): DualAPIConfig; /** * 创建安全的双API配置(推荐用于生产环境) */ static createSecureDualAPIConfig(options: { aiProvider?: { type: AIProviderType; apiKey: string; }; cloudService?: { accountApiKey: string; }; encryptionKey?: string; configPath?: string; }): DualAPIConfig; } //# sourceMappingURL=api-config.d.ts.map