112 lines
2.7 KiB
TypeScript
112 lines
2.7 KiB
TypeScript
/**
|
|
* API配置管理
|
|
* 统一管理所有API相关的配置信息
|
|
*/
|
|
|
|
export interface AIConfig {
|
|
/** API密钥 */
|
|
apiKey: string;
|
|
/** 模型名称 */
|
|
model: string;
|
|
/** 生成温度参数 */
|
|
temperature: number;
|
|
/** 最大令牌数 */
|
|
maxTokens?: number;
|
|
/** 请求超时时间(毫秒) */
|
|
timeout?: number;
|
|
}
|
|
|
|
/**
|
|
* API配置管理器
|
|
*/
|
|
export class ApiConfig {
|
|
private static _instance: ApiConfig;
|
|
private config: AIConfig;
|
|
|
|
private constructor() {
|
|
this.initConfig();
|
|
}
|
|
|
|
public static get Instance(): ApiConfig {
|
|
if (!this._instance) {
|
|
this._instance = new ApiConfig();
|
|
}
|
|
return this._instance;
|
|
}
|
|
|
|
/**
|
|
* 初始化配置
|
|
* TODO: 应该从环境变量或安全配置文件中读取
|
|
*/
|
|
private initConfig(): void {
|
|
this.config = {
|
|
// 警告: API密钥不应该硬编码在代码中
|
|
// 生产环境中应该从环境变量或安全配置文件中读取
|
|
apiKey: "AIzaSyBJT_68Fc-sKPp_lYSbQmDck0otsd3uKn8",
|
|
model: "gemini-2.5-flash",
|
|
temperature: 0.7,
|
|
maxTokens: 2048,
|
|
timeout: 30000 // 30秒
|
|
};
|
|
}
|
|
|
|
/**
|
|
* 获取AI配置
|
|
*/
|
|
public getAIConfig(): AIConfig {
|
|
return { ...this.config };
|
|
}
|
|
|
|
/**
|
|
* 更新API密钥
|
|
* @param apiKey 新的API密钥
|
|
*/
|
|
public updateApiKey(apiKey: string): void {
|
|
this.config.apiKey = apiKey;
|
|
}
|
|
|
|
/**
|
|
* 更新模型配置
|
|
* @param model 模型名称
|
|
*/
|
|
public updateModel(model: string): void {
|
|
this.config.model = model;
|
|
}
|
|
|
|
/**
|
|
* 更新生成参数
|
|
* @param temperature 温度参数
|
|
*/
|
|
public updateTemperature(temperature: number): void {
|
|
if (temperature >= 0 && temperature <= 2) {
|
|
this.config.temperature = temperature;
|
|
} else {
|
|
console.warn("Temperature should be between 0 and 2");
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 验证配置是否有效
|
|
*/
|
|
public validateConfig(): boolean {
|
|
if (!this.config.apiKey || this.config.apiKey.trim() === "") {
|
|
console.error("API key is missing");
|
|
return false;
|
|
}
|
|
if (!this.config.model || this.config.model.trim() === "") {
|
|
console.error("Model name is missing");
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* 从环境变量加载配置
|
|
* TODO: 实现环境变量读取逻辑
|
|
*/
|
|
public loadFromEnvironment(): void {
|
|
// 这里应该实现从环境变量读取配置的逻辑
|
|
// 例如: process.env.GEMINI_API_KEY
|
|
console.log("Loading configuration from environment variables...");
|
|
}
|
|
} |