openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
75 lines (74 loc) • 3.92 kB
JavaScript
import { A as parseStrictPositiveInteger } from "./path-guards-quCEQx1i.mjs";
//#region packages/gateway-client/src/timeouts.ts
function isTestRuntimeEnv(e) {
return e.VITEST === "true" || e.VITEST === "1" || e.VITEST_POOL_ID !== void 0 || e.VITEST_WORKER_ID !== void 0 || e.NODE_ENV === "test" || e !== process.env && isTestRuntimeEnv(process.env);
}
/** Maximum delay Node timers can represent without overflow warnings. */
const MAX_SAFE_TIMEOUT_DELAY_MS = 2147483647;
/** Default server-side window for gateway preauth handshakes. */
const DEFAULT_PREAUTH_HANDSHAKE_TIMEOUT_MS = 3e4;
/** Starts the browser-safe deadline that covers Gateway connect preparation and hello. */
function startGatewayConnectTimeout(e) {
const t = setTimeout(e, DEFAULT_PREAUTH_HANDSHAKE_TIMEOUT_MS);
t.unref?.();
return t;
}
/** Clears either pending Gateway handshake phase without retaining its timer. */
function clearGatewayConnectTimeout(e) {
if (e !== null) clearTimeout(e);
return null;
}
/** Default deadline for a single non-streaming Gateway request. */
const DEFAULT_GATEWAY_REQUEST_TIMEOUT_MS = 3e4;
/** Minimum client watchdog delay for connect challenge setup. */
const MIN_CONNECT_CHALLENGE_TIMEOUT_MS = 250;
/** Default maximum client watchdog delay, aligned with the preauth server timeout. */
const MAX_CONNECT_CHALLENGE_TIMEOUT_MS = DEFAULT_PREAUTH_HANDSHAKE_TIMEOUT_MS;
/** Clamps arbitrary timer delays to Node's safe range and an optional floor. */
function resolveSafeTimeoutDelayMs(e, t) {
const n = t?.minMs ?? 1;
const r = Math.min(MAX_SAFE_TIMEOUT_DELAY_MS, Math.max(0, Number.isFinite(n) ? Math.floor(n) : 1));
return Math.min(MAX_SAFE_TIMEOUT_DELAY_MS, Math.max(r, Number.isFinite(e) ? Math.floor(e) : r));
}
/** Resolves optional timeout values through a fallback and safe timer clamp. */
function resolveFiniteTimeoutDelayMs(e, t, n) {
return resolveSafeTimeoutDelayMs(typeof e === "number" && Number.isFinite(e) ? e : t, n);
}
/** Clamps connect challenge watchdog timeouts to the gateway-supported range. */
function clampConnectChallengeTimeoutMs(e, t = MAX_CONNECT_CHALLENGE_TIMEOUT_MS) {
return Math.max(250, Math.min(Math.max(250, t), e));
}
/** Reads the connect challenge watchdog override from the process environment. */
function getConnectChallengeTimeoutMsFromEnv(e = process.env) {
const t = e.OPENCLAW_CONNECT_CHALLENGE_TIMEOUT_MS;
if (t) {
const e = parseStrictPositiveInteger(t);
if (e !== void 0) return resolveSafeTimeoutDelayMs(e);
}
}
/** Resolves the client watchdog timeout using explicit, env, then preauth defaults. */
function resolveConnectChallengeTimeoutMs(e, t) {
const n = resolvePreauthHandshakeTimeoutMs({
env: t?.env,
configuredTimeoutMs: t?.configuredTimeoutMs
});
const r = Math.max(DEFAULT_PREAUTH_HANDSHAKE_TIMEOUT_MS, n);
if (typeof e === "number" && Number.isFinite(e)) return clampConnectChallengeTimeoutMs(e, r);
const i = getConnectChallengeTimeoutMsFromEnv(t?.env);
if (i !== void 0) return clampConnectChallengeTimeoutMs(i, Math.max(r, i));
return clampConnectChallengeTimeoutMs(n, r);
}
/** Resolves the server preauth timeout from env, explicit config, or default. */
function resolvePreauthHandshakeTimeoutMs(e) {
const t = e?.env ?? process.env;
const n = t.OPENCLAW_HANDSHAKE_TIMEOUT_MS || (isTestRuntimeEnv(t) ? t.OPENCLAW_TEST_HANDSHAKE_TIMEOUT_MS : void 0);
if (n) {
const e = parseStrictPositiveInteger(n);
if (e !== void 0) return resolveSafeTimeoutDelayMs(e);
}
const r = e?.configuredTimeoutMs;
if (typeof r === "number" && Number.isFinite(r) && r > 0) return resolveSafeTimeoutDelayMs(r);
return DEFAULT_PREAUTH_HANDSHAKE_TIMEOUT_MS;
}
//#endregion
export { resolvePreauthHandshakeTimeoutMs as a, resolveFiniteTimeoutDelayMs as i, clearGatewayConnectTimeout as n, resolveSafeTimeoutDelayMs as o, resolveConnectChallengeTimeoutMs as r, startGatewayConnectTimeout as s, DEFAULT_GATEWAY_REQUEST_TIMEOUT_MS as t };