import EscortAgent from "./agent.js"; class ChatTask { constructor(options = {}) { this.options = { modelProvider: options.modelProvider || "deepseek", apiKey: options.apiKey, baseURL: options.baseURL, modelName: options.modelName, temperature: options.temperature ?? 0.7, maxIterations: options.maxIterations || 10, }; this.agents = new Map(); this.maxAgents = options.maxAgents || 100; } async streamChat(userInfo, message, callback) { const userId = userInfo ? userInfo._id : message.appId; let agent = this.agents.get(userId); if (agent) { // LRU:重新插入以更新使用顺序 this.agents.delete(userId); } else { agent = new EscortAgent(); } this.agents.set(userId, agent); // 超出上限时淘汰最久未使用的 Agent,避免内存泄漏 if (this.agents.size > this.maxAgents) { const oldest = this.agents.keys().next().value; this.agents.delete(oldest); } return agent.streamChat(userInfo, [message], callback); } } const chatTask = new ChatTask(); export { ChatTask, chatTask }; export default chatTask;