@myassis/gateway 1.0.92 → 1.0.93
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -43,6 +43,7 @@ class RelayClient {
|
|
|
43
43
|
stopped = false;
|
|
44
44
|
/** Server 在 HELLO_ACK 中下发的参数,未收到前用协议默认值 */
|
|
45
45
|
initialWindowSize = protocol_js_1.INITIAL_WINDOW_SIZE;
|
|
46
|
+
wsInitialWindowSize = protocol_js_1.WS_INITIAL_WINDOW_SIZE;
|
|
46
47
|
maxConcurrentStreams = protocol_js_1.MAX_CONCURRENT_STREAMS;
|
|
47
48
|
/**
|
|
48
49
|
* 启动中继。
|
|
@@ -350,6 +351,11 @@ class RelayClient {
|
|
|
350
351
|
this.initialWindowSize = ack.initialWindowSize;
|
|
351
352
|
if (ack.maxConcurrentStreams > 0)
|
|
352
353
|
this.maxConcurrentStreams = ack.maxConcurrentStreams;
|
|
354
|
+
// 旧 Server 不下发该字段:回退到 HTTP 窗口,与改造前行为一致。
|
|
355
|
+
// 两侧必须用同一个值,否则接收方会把合法数据当成「对端超发」。
|
|
356
|
+
this.wsInitialWindowSize = ack.wsInitialWindowSize > 0
|
|
357
|
+
? ack.wsInitialWindowSize
|
|
358
|
+
: this.initialWindowSize;
|
|
353
359
|
this.failureCount = 0;
|
|
354
360
|
this.lastError = undefined;
|
|
355
361
|
this.connectedAt = Date.now();
|
|
@@ -461,8 +467,9 @@ class RelayClient {
|
|
|
461
467
|
return;
|
|
462
468
|
}
|
|
463
469
|
const open = (0, protocol_js_1.decodeJsonPayload)(payload);
|
|
464
|
-
|
|
465
|
-
const
|
|
470
|
+
// WS 没有逗条背压,窗口耗尽只能中止整条流,因此用更宽的专用窗口
|
|
471
|
+
const sendWindow = new protocol_js_1.SendWindow(this.wsInitialWindowSize);
|
|
472
|
+
const recvWindow = new protocol_js_1.ReceiveWindow(this.wsInitialWindowSize);
|
|
466
473
|
const socket = new LoopbackForwarder_js_1.LoopbackWebSocket(this.localPort, open.path, open.headers, {
|
|
467
474
|
onOpen: () => {
|
|
468
475
|
// 回环连上后无需额外通知:远端 WS 在 Server 侧已经 accept,
|
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
* CI 会校验本文件与源文件的一致性,不一致将导致构建失败。
|
|
10
10
|
*/
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.stripHopByHopHeaders = exports.checkGatewayFeatures = exports.checkProtocolVersion = exports.ReceiveWindow = exports.SendWindow = exports.StreamIdAllocator = exports.decodeWsData = exports.encodeWsDataFrames = exports.MAX_WS_DATA_CHUNK = exports.WS_DATA_PREFIX_SIZE = exports.WsDataKind = exports.splitIntoFrames = exports.decodeJsonPayload = exports.decodeFrame = exports.encodeJsonFrame = exports.encodeFrame = exports.RelayProtocolError = exports.AbortReason = exports.isControlFrame = exports.isKnownFrameType = exports.frameTypeName = exports.FrameType = exports.CONTROL_STREAM_ID = exports.PING_TIMEOUT_MS = exports.PING_INTERVAL_MS = exports.MAX_CONCURRENT_STREAMS = exports.INITIAL_WINDOW_SIZE = exports.MAX_FRAME_PAYLOAD = exports.FRAME_HEADER_SIZE = exports.RELAY_REQUIRED_FEATURE = exports.RELAY_MIN_PROTOCOL_VERSION = exports.RELAY_PROTOCOL_VERSION = void 0;
|
|
12
|
+
exports.stripHopByHopHeaders = exports.checkGatewayFeatures = exports.checkProtocolVersion = exports.ReceiveWindow = exports.SendWindow = exports.StreamIdAllocator = exports.decodeWsData = exports.encodeWsDataFrames = exports.MAX_WS_DATA_CHUNK = exports.WS_DATA_PREFIX_SIZE = exports.WsDataKind = exports.splitIntoFrames = exports.decodeJsonPayload = exports.decodeFrame = exports.encodeJsonFrame = exports.encodeFrame = exports.RelayProtocolError = exports.AbortReason = exports.isControlFrame = exports.isKnownFrameType = exports.frameTypeName = exports.FrameType = exports.CONTROL_STREAM_ID = exports.PING_TIMEOUT_MS = exports.PING_INTERVAL_MS = exports.MAX_CONCURRENT_STREAMS = exports.WS_INITIAL_WINDOW_SIZE = exports.INITIAL_WINDOW_SIZE = exports.MAX_FRAME_PAYLOAD = exports.FRAME_HEADER_SIZE = exports.RELAY_REQUIRED_FEATURE = exports.RELAY_MIN_PROTOCOL_VERSION = exports.RELAY_PROTOCOL_VERSION = void 0;
|
|
13
13
|
/**
|
|
14
14
|
* 中继帧协议(单源实现)
|
|
15
15
|
* ============================================================
|
|
@@ -55,8 +55,29 @@ exports.FRAME_HEADER_SIZE = 5;
|
|
|
55
55
|
* 偏小是有意为之:帧越小,SSE/流式响应的转发粒度越细,首字延迟越低。
|
|
56
56
|
*/
|
|
57
57
|
exports.MAX_FRAME_PAYLOAD = 64 * 1024;
|
|
58
|
-
/**
|
|
58
|
+
/**
|
|
59
|
+
* HTTP 流的初始接收窗口(信用式流控)。
|
|
60
|
+
*
|
|
61
|
+
* 保持 256KB:HTTP 流有真正的背压——信用耗尽会 req.pause() 暂停
|
|
62
|
+
* 上游,等 WINDOW_UPDATE 回来再继续,窗口小只影响吞吐不会丢数据,
|
|
63
|
+
* 而帧细粒度对 SSE 首字延迟有利。
|
|
64
|
+
*/
|
|
59
65
|
exports.INITIAL_WINDOW_SIZE = 256 * 1024;
|
|
66
|
+
/**
|
|
67
|
+
* WebSocket 流的初始接收窗口。
|
|
68
|
+
*
|
|
69
|
+
* 必须远大于 HTTP 窗口,因为 WS 没有背压可用:ws 库不支持逗条
|
|
70
|
+
* 暂停,一条消息发不下去就只能 abortStream(见
|
|
71
|
+
* RelayHub.forwardWebSocket 与 RelayClient.onWsOpen 里的 QUOTA_EXCEEDED 分支)。
|
|
72
|
+
* 一旦中止,终端的通知通道就断了,那一段的会话镜像事件全丢。
|
|
73
|
+
*
|
|
74
|
+
* 现实触发条件:会话镜像会把含 base64 附件的用户消息整条下发,
|
|
75
|
+
* 一张 5MB 的图经 base64 膨胀约 4/3 后接近 7MB,瞬时打穿 256KB。
|
|
76
|
+
*
|
|
77
|
+
* 16MB 不是预分配:窗口只是「允许对端在未收到确认前先发多少」。
|
|
78
|
+
* 而且 WS 流数量等于在线终端数(个位数),不是 MAX_CONCURRENT_STREAMS。
|
|
79
|
+
*/
|
|
80
|
+
exports.WS_INITIAL_WINDOW_SIZE = 16 * 1024 * 1024;
|
|
60
81
|
/** 单条隧道允许的最大并发流数 */
|
|
61
82
|
exports.MAX_CONCURRENT_STREAMS = 64;
|
|
62
83
|
/** 心跳间隔与超时(毫秒) */
|
|
@@ -120,6 +120,17 @@ class Session {
|
|
|
120
120
|
isGenerating = false;
|
|
121
121
|
unreadCount = 0;
|
|
122
122
|
currentMessageId = null;
|
|
123
|
+
/**
|
|
124
|
+
* 镜像事件序号(会话级单调递增)。
|
|
125
|
+
*
|
|
126
|
+
* WebSocket 镜像是 fire-and-forget:连接不在 OPEN(重连窗口)或中继流
|
|
127
|
+
* 被中止时,sendToUser 会静默丢弃,接收端无从得知。带上序号后,
|
|
128
|
+
* 终端一旦发现跳号就能立即触发补拉,而不是靠「连接是否断过」猜。
|
|
129
|
+
*
|
|
130
|
+
* 不持久化:它只用于检测单次进程内的事件连续性,网关重启后
|
|
131
|
+
* 终端会因 WebSocket 重连而重置基准。
|
|
132
|
+
*/
|
|
133
|
+
mirrorSeq = 0;
|
|
123
134
|
abortController = null;
|
|
124
135
|
/** 后台预压缩的延迟定时器 */
|
|
125
136
|
precompressionTimer = null;
|
|
@@ -741,6 +752,9 @@ class Session {
|
|
|
741
752
|
if (NON_MIRRORED_SSE_EVENTS.has(data?.type))
|
|
742
753
|
return;
|
|
743
754
|
try {
|
|
755
|
+
// 序号在「决定要发」后才抬:被过滤掉的事件不占号,
|
|
756
|
+
// 否则接收端会把正常的过滤看成丢包。
|
|
757
|
+
this.mirrorSeq += 1;
|
|
744
758
|
wsService.sendToUser(String(this.userId), {
|
|
745
759
|
type: 'session_stream',
|
|
746
760
|
payload: {
|
|
@@ -748,6 +762,7 @@ class Session {
|
|
|
748
762
|
agentId: this.agentId,
|
|
749
763
|
userMessageId,
|
|
750
764
|
assistantMessageId,
|
|
765
|
+
streamSeq: this.mirrorSeq,
|
|
751
766
|
event: data,
|
|
752
767
|
},
|
|
753
768
|
}, { excludeClientId: sseAlive ? clientId : undefined });
|