@yeaft/webchat-agent 1.0.62 → 1.0.63
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.
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
const DEFAULT_AGENT_HEARTBEAT_TIMEOUT_MS = 180000;
|
|
2
|
+
|
|
3
|
+
function parsePositiveInt(value, fallback) {
|
|
4
|
+
const n = Number(value);
|
|
5
|
+
return Number.isFinite(n) && n > 0 ? n : fallback;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export const AGENT_HEARTBEAT_TIMEOUT_MS = parsePositiveInt(
|
|
9
|
+
process.env.YEAFT_AGENT_HEARTBEAT_TIMEOUT_MS || process.env.AGENT_HEARTBEAT_TIMEOUT_MS,
|
|
10
|
+
DEFAULT_AGENT_HEARTBEAT_TIMEOUT_MS,
|
|
11
|
+
);
|
|
12
|
+
|
|
13
|
+
export function shouldReconnectForHeartbeat(lastPongAt, now = Date.now(), timeoutMs = AGENT_HEARTBEAT_TIMEOUT_MS) {
|
|
14
|
+
return Number.isFinite(lastPongAt) && lastPongAt > 0 && (now - lastPongAt) > timeoutMs;
|
|
15
|
+
}
|
package/connection/heartbeat.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import WebSocket from 'ws';
|
|
2
2
|
import ctx from '../context.js';
|
|
3
|
+
import { shouldReconnectForHeartbeat } from './heartbeat-policy.js';
|
|
3
4
|
|
|
4
5
|
export function startAgentHeartbeat() {
|
|
5
6
|
stopAgentHeartbeat();
|
|
@@ -16,8 +17,9 @@ export function startAgentHeartbeat() {
|
|
|
16
17
|
if (!ctx.ws || ctx.ws.readyState !== WebSocket.OPEN) return;
|
|
17
18
|
|
|
18
19
|
// 检查上次 pong 是否超时
|
|
19
|
-
const
|
|
20
|
-
|
|
20
|
+
const now = Date.now();
|
|
21
|
+
const sincePong = now - ctx.lastPongAt;
|
|
22
|
+
if (shouldReconnectForHeartbeat(ctx.lastPongAt, now)) {
|
|
21
23
|
console.warn(`[Heartbeat] No pong for ${Math.round(sincePong / 1000)}s, reconnecting...`);
|
|
22
24
|
ctx.ws.terminate();
|
|
23
25
|
return;
|
package/package.json
CHANGED