UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

69 lines (68 loc) • 2.54 kB
import { o as normalizeLowercaseStringOrEmpty } from "./string-coerce-1VuiLaaR.mjs"; import { D as resolveExpiresAtMsFromDurationMs, g as isFutureDateTimestampMs } from "./number-coercion-CkhMiRw8.mjs"; import { t as pruneMapToMaxSize } from "./map-size-BlrH_CnE.mjs"; import { r as resolveGlobalMap } from "./global-singleton-0m-JziJW.mjs"; import { n as normalizeAccountId } from "./account-id-ifCOO7_9.mjs"; //#region src/channels/message/outbound-echo-state.ts const outboundMessageIdentities = resolveGlobalMap(Symbol.for("openclaw.outboundMessageIdentities")); //#endregion //#region src/channels/message/outbound-echo.ts const OUTBOUND_ECHO_WINDOW_MS = 3e4; function resolveIdentityKeys(identity) { const channel = normalizeLowercaseStringOrEmpty(identity.channel); const conversationId = identity.conversationId.trim(); if (!channel || !conversationId) return []; const scope = [ channel, normalizeAccountId(identity.accountId), conversationId ]; const keys = []; for (const kind of ["message", "source"]) { const id = identity[`${kind}Id`]?.trim(); if (id) keys.push(JSON.stringify([ ...scope, kind, id ])); } return keys; } function pruneExpiredEntries(nowMs) { for (const [key, expiresAt] of outboundMessageIdentities) { if (isFutureDateTimestampMs(expiresAt, { nowMs })) return; outboundMessageIdentities.delete(key); } } /** Records a platform message id emitted by a channel's own outbound send path. */ function recordOutboundMessageIdentity(identity) { const keys = resolveIdentityKeys(identity); if (keys.length === 0) return; const nowMs = Date.now(); const expiresAt = resolveExpiresAtMsFromDurationMs(OUTBOUND_ECHO_WINDOW_MS, { nowMs }); if (expiresAt === void 0) { for (const key of keys) outboundMessageIdentities.delete(key); return; } pruneExpiredEntries(nowMs); for (const key of keys) { outboundMessageIdentities.delete(key); pruneMapToMaxSize(outboundMessageIdentities, 9999); outboundMessageIdentities.set(key, expiresAt); } } /** Returns whether an inbound platform message matches a recently emitted outbound id. */ function isRecentOutboundMessageIdentity(identity) { for (const key of resolveIdentityKeys(identity)) { const expiresAt = outboundMessageIdentities.get(key); if (expiresAt === void 0) continue; if (!isFutureDateTimestampMs(expiresAt)) { outboundMessageIdentities.delete(key); continue; } return true; } return false; } //#endregion export { recordOutboundMessageIdentity as n, isRecentOutboundMessageIdentity as t };