committed by
trae-solo-agent
co-authored by
Kimi-K3.1-Pro noreply@anthropic.com
parent
3f3dd07a2a
commit
83ef5c2960
+24
-11
@@ -5,6 +5,19 @@ import { chatTask } from "./agent/bots/bot-escort/task.js";
|
|||||||
import { adminAgent } from "./agent/bots/bot-escort-admin/agent.js"
|
import { adminAgent } from "./agent/bots/bot-escort-admin/agent.js"
|
||||||
import { getUserByToken } from "./utils/user_service.js";
|
import { getUserByToken } from "./utils/user_service.js";
|
||||||
|
|
||||||
|
let systemMessageSequence = 0;
|
||||||
|
|
||||||
|
// 所有下行帧保持同一结构,调用方无需按消息类型猜测字段。
|
||||||
|
const sendFrame = (ws, source, type, content = '', id = '') => {
|
||||||
|
if (ws.readyState !== WebSocket.OPEN) return;
|
||||||
|
ws.send(JSON.stringify({
|
||||||
|
source,
|
||||||
|
type,
|
||||||
|
content,
|
||||||
|
id: id || `system_${Date.now()}_${++systemMessageSequence}`
|
||||||
|
}));
|
||||||
|
};
|
||||||
|
|
||||||
export default class WebSocketServerManager {
|
export default class WebSocketServerManager {
|
||||||
constructor(port = 8080) {
|
constructor(port = 8080) {
|
||||||
this.port = port;
|
this.port = port;
|
||||||
@@ -44,7 +57,7 @@ export default class WebSocketServerManager {
|
|||||||
if (path === '/chat') {
|
if (path === '/chat') {
|
||||||
this._handleChatConnection(ws);
|
this._handleChatConnection(ws);
|
||||||
} else {
|
} else {
|
||||||
ws.send(JSON.stringify({ type: 'system', content: 'Unknown path' }));
|
sendFrame(ws, 'system', 'error', 'Unknown path');
|
||||||
ws.close(1000, 'Unknown path');
|
ws.close(1000, 'Unknown path');
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -55,7 +68,7 @@ export default class WebSocketServerManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
_handleChatConnection(ws) {
|
_handleChatConnection(ws) {
|
||||||
ws.send(JSON.stringify({ type: 'system', content: 'Connected to chat server' }));
|
sendFrame(ws, 'system', 'connected', 'Connected to chat server');
|
||||||
|
|
||||||
ws.on('message', (data) => this._handleMessage(ws, data));
|
ws.on('message', (data) => this._handleMessage(ws, data));
|
||||||
|
|
||||||
@@ -77,7 +90,7 @@ export default class WebSocketServerManager {
|
|||||||
console.log('Received message type:', msg.type);
|
console.log('Received message type:', msg.type);
|
||||||
|
|
||||||
if (msg.type === 'ping') {
|
if (msg.type === 'ping') {
|
||||||
ws.send(JSON.stringify({ type: 'pong' }));
|
sendFrame(ws, 'system', 'pong', '', msg.id);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -86,26 +99,24 @@ export default class WebSocketServerManager {
|
|||||||
if (ws.readyState !== WebSocket.OPEN) return;
|
if (ws.readyState !== WebSocket.OPEN) return;
|
||||||
|
|
||||||
const send = (source, type, content, id) => {
|
const send = (source, type, content, id) => {
|
||||||
if (ws.readyState === WebSocket.OPEN) {
|
sendFrame(ws, source, type, content, id);
|
||||||
ws.send(JSON.stringify({ source, type, content, id }));
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// 身份只认 token:不再接受凭 userId 明文接入,防止冒充他人聊天
|
// 身份只认 token:不再接受凭 userId 明文接入,防止冒充他人聊天
|
||||||
if (!msg.token) {
|
if (!msg.token) {
|
||||||
ws.send(JSON.stringify({ type: 'error', content: '缺少登录凭证,请重新登录' }));
|
send('system', 'error', '请先登录', msg.id);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const userInfo = await getUserByToken(msg.token);
|
const userInfo = await getUserByToken(msg.token);
|
||||||
if (!userInfo) {
|
if (!userInfo) {
|
||||||
ws.send(JSON.stringify({ type: 'error', content: '登录已失效' }));
|
send('system', 'error', '请先登录', msg.id);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (msg.agent === 'escort-admin') {
|
if (msg.agent === 'escort-admin') {
|
||||||
// 管理端 agent 含写库/环境变量等高危工具,必须校验管理员角色,防止普通用户越权
|
// 管理端 agent 含写库/环境变量等高危工具,必须校验管理员角色,防止普通用户越权
|
||||||
if (!userInfo.app || !("wxapp-escort-admin" in userInfo.app)) {
|
if (!userInfo.app || !("wxapp-escort-admin" in userInfo.app)) {
|
||||||
ws.send(JSON.stringify({ type: 'error', content: '无管理员权限' }));
|
send('system', 'error', '无管理员权限', msg.id);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
await adminAgent.streamChat(userInfo, [msg], (source, type, content, id) => {
|
await adminAgent.streamChat(userInfo, [msg], (source, type, content, id) => {
|
||||||
@@ -116,9 +127,11 @@ export default class WebSocketServerManager {
|
|||||||
send(source, type, content, id);
|
send(source, type, content, id);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
// 流式 chunk 可能携带不同的模型消息 ID,结束帧统一关联本次客户端请求。
|
||||||
|
send('system', 'end', '', msg.id);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error('streamChat error:', err);
|
console.error('streamChat error:', err);
|
||||||
send('system', 'error', '处理消息失败');
|
send('system', 'error', '处理消息失败,请稍后重试', msg.id);
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -131,7 +144,7 @@ export default class WebSocketServerManager {
|
|||||||
console.log('[Unknown message type]', msg.type);
|
console.log('[Unknown message type]', msg.type);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error('Message parse error:', e);
|
console.error('Message parse error:', e);
|
||||||
ws.send(JSON.stringify({ type: 'error', content: 'Invalid JSON' }));
|
sendFrame(ws, 'system', 'error', '消息格式错误');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user