This commit is contained in:
lik
2026-06-01 22:10:41 +08:00
parent cebe7876f7
commit 43aa9ac844
5 changed files with 40 additions and 18 deletions

View File

@@ -1,5 +1,6 @@
import WebSocket, { WebSocketServer } from 'ws';
import http from 'http';
import fetch from 'node-fetch';
import { DBModel } from "./models/index.js";
import { chatTask } from "./agent/escort/task.js";
import { adminAgent } from "./agent/escort-admin/agent.js"
@@ -81,12 +82,12 @@ export default class WebSocketServerManager {
if (msg.type === 'chat' || msg.type === 'clear') {
if (msg.agent === 'escort-admin') {
const userInfo = await this.getUserInfo(msg.userId);
const userInfo = await this.getUserInfo(msg.token, msg.userId);
adminAgent.streamChat(userInfo, [msg], (source, type, content, id) => {
ws.send(JSON.stringify({ source, type, content, id }));
});
} else {
const userInfo = await this.getUserInfo(msg.userId);
const userInfo = await this.getUserInfo(msg.token, msg.userId);
chatTask.streamChat(userInfo, msg, (source, type, content, id) => {
ws.send(JSON.stringify({ source, type, content, id }));
});
@@ -132,8 +133,19 @@ export default class WebSocketServerManager {
return this.wss.clients.size;
}
async getUserInfo(userId) {
if (!DBModel.User || userId.length === 0) return null;
return await DBModel.User.findById(userId).lean().exec();
async getUserInfo(token, userId) {
if (!token || !userId) return null;
const url = "http://127.0.0.1:9010/user/userInfo";
const res = await fetch(url, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
token,
userId
})
});
const data = await res.json();
return data.data.user;
}
}