ai review

This commit is contained in:
lik
2026-09-01 21:49:17 +08:00
parent 390c871d35
commit 98c9026485
14 changed files with 288 additions and 262 deletions
+18 -4
View File
@@ -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);
}
}