UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

151 lines (150 loc) • 5.23 kB
import { l as normalizeOptionalString } from "./string-coerce-CCIiB4H7.mjs"; import { a as resolveGlobalSingleton } from "./global-singleton-D7yf05nR.mjs"; import { t as createDeferredCore } from "./deferred-D0La5CRk.mjs"; import crypto from "node:crypto"; //#region src/sessions/conversation-turns.ts const pendingTurns = resolveGlobalSingleton(Symbol.for("openclaw.pendingConversationTurns"), () => /* @__PURE__ */ new Map(), (e) => { for (const t of e.values()) t.settle(void 0); }); const pendingTurnsByOutboundId = resolveGlobalSingleton(Symbol.for("openclaw.pendingConversationTurnsByOutboundId"), () => /* @__PURE__ */ new Map(), (e) => e.clear()); function pendingTurnKey(e, t) { return JSON.stringify([e, t]); } function removePendingOutboundMembership(e) { if (!e.outboundMessageId) return; const t = pendingTurnKey(e.agentId, e.outboundMessageId); const n = pendingTurnsByOutboundId.get(t); n?.delete(e); if (n?.size === 0) pendingTurnsByOutboundId.delete(t); } /** Registers one process-local waiter; transcript correlation remains durable after completion. */ function registerPendingConversationTurn(e) { const t = normalizeOptionalString(e.agentId); if (!t) throw new Error("conversation turn requires an agent id"); const n = normalizeOptionalString(e.id) ?? crypto.randomUUID(); const r = pendingTurnKey(t, n); if (pendingTurns.has(r)) throw new Error(`conversation turn already pending for ${t}: ${n}`); const i = Date.now(); const a = Math.max(0, e.timeoutMs); let o = false; const { promise: s, resolve: c } = createDeferredCore(); const { promise: l, resolve: u } = createDeferredCore(); let d; const stopTimeout = () => { clearTimeout(d); d = void 0; }; const settle = (t) => { if (o) return; o = true; u(); removePendingOutboundMembership(f); if (pendingTurns.get(r) === f) pendingTurns.delete(r); stopTimeout(); e.signal?.removeEventListener("abort", cancel); c(t); }; const cancel = () => settle(void 0); const f = { key: r, agentId: t, id: n, conversationRef: e.conversationRef, sessionId: e.sessionId, threadId: normalizeOptionalString(e.threadId), createdAt: i, correlationReady: l, markCorrelationReady: u, claimed: false, settle }; pendingTurns.set(r, f); d = setTimeout(cancel, a); d.unref?.(); e.signal?.addEventListener("abort", cancel, { once: true }); if (e.signal?.aborted) cancel(); return { id: n, setOutboundMessageId: (e) => { if (pendingTurns.get(r) !== f) return; removePendingOutboundMembership(f); f.outboundMessageId = normalizeOptionalString(e); if (!f.outboundMessageId) { f.settle(void 0); return; } const t = pendingTurnKey(f.agentId, f.outboundMessageId); const n = pendingTurnsByOutboundId.get(t) ?? /* @__PURE__ */ new Set(); n.add(f); pendingTurnsByOutboundId.set(t, n); }, markReady: () => { if (pendingTurns.get(r) !== f) return; if (f.outboundMessageId) f.markCorrelationReady(); else f.settle(void 0); }, wait: async () => await s, cancel }; } /** Cancels one Gateway-owned turn so a late reply follows ordinary inbound dispatch. */ function cancelPendingConversationTurn(e) { const t = normalizeOptionalString(e.agentId); const n = normalizeOptionalString(e.id); const r = t && n ? pendingTurns.get(pendingTurnKey(t, n)) : void 0; if (!r) return false; r.settle(void 0); return true; } /** Claims a correlated inbound reply so the waiting turn can consume it without a second agent run. */ async function claimPendingConversationTurnReply(e) { const t = normalizeOptionalString(e.replyToId); if (!t) return; const n = normalizeOptionalString(e.threadId); const r = normalizeOptionalString(e.parentConversationRef); const i = normalizeOptionalString(e.agentId); if (!i) return; let a; const o = pendingTurnsByOutboundId.get(pendingTurnKey(i, t)); for (const s of o ?? []) { if (s.claimed || s.agentId !== i || s.outboundMessageId !== t || s.conversationRef !== e.conversationRef && (s.threadId || n !== t || r !== s.conversationRef) || s.sessionId !== e.sessionId || s.threadId && n && s.threadId !== n) continue; if (!a || s.createdAt < a.createdAt) a = s; } if (!a) return; a.claimed = true; await a.correlationReady; if (pendingTurns.get(a.key) !== a) return; let s = true; const isCurrent = () => s && pendingTurns.get(a.key) === a && a.claimed; const c = { conversationRef: e.conversationRef, messageId: e.messageId, replyToId: t, ...n ? { threadId: n } : {}, text: e.text, timestamp: e.timestamp ?? Date.now() }; return { turnId: a.id, sessionId: a.sessionId, assertCurrent: () => { if (!isCurrent()) throw new Error("conversation turn reply claim is no longer active"); }, complete: (e = {}) => { if (!isCurrent()) return; s = false; a.settle({ ...c, ...e.transcriptArtifactId ? { transcriptArtifactId: e.transcriptArtifactId } : {}, ...e.transcriptMessageId ? { transcriptMessageId: e.transcriptMessageId } : {} }); }, release: () => { if (!isCurrent()) return; s = false; a.claimed = false; } }; } //#endregion export { claimPendingConversationTurnReply as n, registerPendingConversationTurn as r, cancelPendingConversationTurn as t };