2025-08-13 11:31:21 +08:00
|
|
|
/**
|
|
|
|
|
* API配置管理
|
|
|
|
|
* 统一管理所有API相关的配置信息
|
|
|
|
|
*/
|
|
|
|
|
|
2025-09-16 16:55:39 +08:00
|
|
|
import { DataId, DataManager } from "../data/DataManager";
|
|
|
|
|
import { GlobalData } from "../data/GlobalData";
|
2025-09-01 16:26:37 +08:00
|
|
|
import { ConfigManager } from "../manager/ConfigManager";
|
2025-08-13 11:31:21 +08:00
|
|
|
|
|
|
|
|
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 {
|
2025-09-16 16:55:39 +08:00
|
|
|
//const config = DataManager.I.getDataById<GlobalData>(DataId.Global).getApiKey();
|
2025-08-13 11:31:21 +08:00
|
|
|
this.config = {
|
|
|
|
|
// 警告: API密钥不应该硬编码在代码中
|
|
|
|
|
// 生产环境中应该从环境变量或安全配置文件中读取
|
2025-09-16 16:55:39 +08:00
|
|
|
apiKey: DataManager.I.getDataById<GlobalData>(DataId.Global).getApiKey(),
|
|
|
|
|
model: DataManager.I.getDataById<GlobalData>(DataId.Global).getModel(),
|
|
|
|
|
temperature: DataManager.I.getDataById<GlobalData>(
|
|
|
|
|
DataId.Global
|
|
|
|
|
).getTemperature(),
|
|
|
|
|
maxTokens: DataManager.I.getDataById<GlobalData>(
|
|
|
|
|
DataId.Global
|
|
|
|
|
).getMaxTokens(),
|
|
|
|
|
timeout: DataManager.I.getDataById<GlobalData>(
|
|
|
|
|
DataId.Global
|
|
|
|
|
).getTimeout(), // 30秒
|
2025-08-13 11:31:21 +08:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取AI配置
|
|
|
|
|
*/
|
|
|
|
|
public getAIConfig(): AIConfig {
|
|
|
|
|
return { ...this.config };
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-26 19:41:13 +08:00
|
|
|
/**
|
|
|
|
|
* 获取情绪AI配置
|
|
|
|
|
*/
|
|
|
|
|
public getEmotionAIConfig(): AIConfig {
|
|
|
|
|
return {
|
2025-09-16 16:55:39 +08:00
|
|
|
apiKey: DataManager.I.getDataById<GlobalData>(
|
|
|
|
|
DataId.Global
|
|
|
|
|
).getEmotionApiKey(),
|
|
|
|
|
model: DataManager.I.getDataById<GlobalData>(DataId.Global).getModel(),
|
|
|
|
|
temperature: 0.2,
|
|
|
|
|
maxTokens: DataManager.I.getDataById<GlobalData>(
|
|
|
|
|
DataId.Global
|
|
|
|
|
).getMaxTokens(),
|
|
|
|
|
timeout: DataManager.I.getDataById<GlobalData>(
|
|
|
|
|
DataId.Global
|
|
|
|
|
).getTimeout(),
|
2025-08-26 19:41:13 +08:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-13 11:31:21 +08:00
|
|
|
/**
|
|
|
|
|
* 更新生成参数
|
|
|
|
|
* @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;
|
|
|
|
|
}
|
|
|
|
|
}
|