ai review
This commit is contained in:
+18
-4
@@ -11,15 +11,29 @@ class ChatTask {
|
||||
maxIterations: options.maxIterations || 10,
|
||||
};
|
||||
|
||||
this.agents = {};
|
||||
this.agents = new Map();
|
||||
this.maxAgents = options.maxAgents || 100;
|
||||
}
|
||||
|
||||
async streamChat(userInfo, message, callback) {
|
||||
const userId = userInfo ? userInfo._id : message.appId;
|
||||
if (!this.agents[userId]) {
|
||||
this.agents[userId] = new EscortAgent();
|
||||
|
||||
let agent = this.agents.get(userId);
|
||||
if (agent) {
|
||||
// LRU:重新插入以更新使用顺序
|
||||
this.agents.delete(userId);
|
||||
} else {
|
||||
agent = new EscortAgent();
|
||||
}
|
||||
return this.agents[userId].streamChat(userInfo, [message], callback);
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user