UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

77 lines (76 loc) • 2.53 kB
import { E as parseStrictNonNegativeInteger } from "./number-coercion-D6u-4YD8.mjs"; import "./model-ref-shared-DqE2RqZv.mjs"; import { r as modelKey } from "./provider-registry-index-DhPtwFh_.mjs"; //#region src/agents/fallback-skip-cache.ts const DEFAULT_FALLBACK_SKIP_TTL_MS = 0; const FALLBACK_SKIP_TTL_ENV = "OPENCLAW_FALLBACK_SKIP_TTL_MS"; const FALLBACK_SKIP_TTL_MIN_MS = 1e3; const FALLBACK_SKIP_TTL_MAX_MS = 6e5; function resolveConfiguredSkipTtlMs(e = process.env) { const t = parseStrictNonNegativeInteger(e[FALLBACK_SKIP_TTL_ENV]); if (t === void 0) return DEFAULT_FALLBACK_SKIP_TTL_MS; if (t === 0) return 0; return Math.min(FALLBACK_SKIP_TTL_MAX_MS, Math.max(FALLBACK_SKIP_TTL_MIN_MS, t)); } const GLOBAL_PRUNE_INTERVAL_MS = 5e3; function getState() { const e = globalThis; return e.openclawFallbackSkipCacheState ??= { buckets: /* @__PURE__ */ new Map(), lastGlobalPruneAtMs: 0 }; } function candidateKey(e, t, n) { return JSON.stringify([modelKey(e, t), n?.trim() || null]); } function pruneExpired(e, t) { for (const [n, r] of e.entries()) if (r.expiresAtMs <= t) e.delete(n); } function pruneAllExpired(e) { const t = getState(); if (e - t.lastGlobalPruneAtMs < GLOBAL_PRUNE_INTERVAL_MS) return; t.lastGlobalPruneAtMs = e; for (const [n, r] of t.buckets.entries()) { pruneExpired(r, e); if (r.size === 0) t.buckets.delete(n); } } function markFallbackCandidateSkipped(e) { if (!e.sessionId || !e.provider || !e.model) return; const t = e.now ?? Date.now(); const n = e.ttlMs ?? resolveConfiguredSkipTtlMs(); if (n <= 0) return; pruneAllExpired(t); const r = getState().buckets; let i = r.get(e.sessionId); if (!i) { i = /* @__PURE__ */ new Map(); r.set(e.sessionId, i); } i.set(candidateKey(e.provider, e.model, e.authScope), { expiresAtMs: t + n, reason: e.reason }); } function getFallbackCandidateSkipEntry(e) { if (!e.sessionId || !e.provider || !e.model) return; const t = e.now ?? Date.now(); pruneAllExpired(t); const n = getState().buckets; const r = n.get(e.sessionId); if (!r) return; pruneExpired(r, t); if (r.size === 0) { n.delete(e.sessionId); return; } return r.get(candidateKey(e.provider, e.model, e.authScope)); } function isFallbackCandidateSkipped(e) { return getFallbackCandidateSkipEntry(e) !== void 0; } function getFallbackCandidateSkipReason(e) { return getFallbackCandidateSkipEntry(e)?.reason; } //#endregion export { isFallbackCandidateSkipped as n, markFallbackCandidateSkipped as r, getFallbackCandidateSkipReason as t };