fix(websocket): 同一账号的聊天轮次跨连接串行
单条连接的队列只能保证连接内串行,同一账号多设备/多标签页共享 Agent 历史,AI(tool_calls) 与工具结果会跨请求交错。新增按用户维度的轮次锁, 完整聊天轮次再串行一层。
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import { test } from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
|
||||
import WebSocketServerManager from "../websocket.js";
|
||||
import WebSocketServerManager, { withChatTurnLock } from "../websocket.js";
|
||||
|
||||
function createWs() {
|
||||
const sent = [];
|
||||
@@ -125,3 +125,24 @@ test("null / 数组等非对象帧同样按消息格式错误处理", () => {
|
||||
assert.equal(item.content, "消息格式错误");
|
||||
}
|
||||
});
|
||||
|
||||
test("同一用户的跨连接聊天轮次串行执行", async () => {
|
||||
const order = [];
|
||||
let releaseFirst;
|
||||
const first = withChatTurnLock("user-1", async () => {
|
||||
order.push("first-start");
|
||||
await new Promise((resolve) => { releaseFirst = resolve; });
|
||||
order.push("first-end");
|
||||
});
|
||||
const second = withChatTurnLock("user-1", async () => {
|
||||
order.push("second-start");
|
||||
order.push("second-end");
|
||||
});
|
||||
|
||||
await new Promise((resolve) => setImmediate(resolve));
|
||||
assert.deepEqual(order, ["first-start"]);
|
||||
|
||||
releaseFirst();
|
||||
await Promise.all([first, second]);
|
||||
assert.deepEqual(order, ["first-start", "first-end", "second-start", "second-end"]);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user