@myassis/gateway 1.0.37 → 1.0.38
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.
|
@@ -474,9 +474,23 @@ class Session {
|
|
|
474
474
|
const sendSSE = (res, data) => {
|
|
475
475
|
if (!res)
|
|
476
476
|
return;
|
|
477
|
-
|
|
478
|
-
|
|
477
|
+
try {
|
|
478
|
+
res.write(`data: ${JSON.stringify(data)}\n\n`);
|
|
479
|
+
res.flush?.();
|
|
480
|
+
}
|
|
481
|
+
catch (error) {
|
|
482
|
+
logger.error('SSE write error:', error);
|
|
483
|
+
}
|
|
479
484
|
};
|
|
485
|
+
// 心跳机制:每 15 秒发送一次心跳,防止连接超时
|
|
486
|
+
const heartbeatInterval = setInterval(() => {
|
|
487
|
+
if (res && !res.destroyed) {
|
|
488
|
+
sendSSE(res, { type: 'heartbeat' });
|
|
489
|
+
}
|
|
490
|
+
else {
|
|
491
|
+
clearInterval(heartbeatInterval);
|
|
492
|
+
}
|
|
493
|
+
}, 15000);
|
|
480
494
|
// Send message start event
|
|
481
495
|
sendSSE(res, { type: 'message_start' });
|
|
482
496
|
// Build messages for API call (保留 tool_call_id 等必要字段)
|
|
@@ -918,17 +932,29 @@ class Session {
|
|
|
918
932
|
this.addAssistantMessage('', toolCalls, [...new Set(modelNames)].join(','), this.currentMessageId);
|
|
919
933
|
}
|
|
920
934
|
if (error.message !== 'aborted') {
|
|
921
|
-
|
|
935
|
+
try {
|
|
936
|
+
sendSSE(res, { type: 'error', error: error.message });
|
|
937
|
+
}
|
|
938
|
+
catch (e) {
|
|
939
|
+
logger.error('Failed to send error event:', e);
|
|
940
|
+
}
|
|
922
941
|
}
|
|
923
942
|
}
|
|
924
943
|
return { success: false, error: error.message };
|
|
925
944
|
}
|
|
926
945
|
finally {
|
|
946
|
+
// 清理心跳定时器
|
|
947
|
+
clearInterval(heartbeatInterval);
|
|
927
948
|
if (!childAgent) {
|
|
928
949
|
this.isGenerating = false;
|
|
929
950
|
this.abortController = null;
|
|
930
|
-
if (res) {
|
|
931
|
-
|
|
951
|
+
if (res && !res.destroyed) {
|
|
952
|
+
try {
|
|
953
|
+
res.end();
|
|
954
|
+
}
|
|
955
|
+
catch (e) {
|
|
956
|
+
logger.error('Error closing SSE connection:', e);
|
|
957
|
+
}
|
|
932
958
|
}
|
|
933
959
|
// 非当前查看的 session,助手回复完成后增加未读计数
|
|
934
960
|
(0, SessionManager_js_1.getSessionManager)(this.userId).onMessageComplete(this.id);
|