UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

267 lines (266 loc) • 8.17 kB
import { a as logVerbose } from "./globals-DBUMOBZ8.js"; import { p as resolveSendableOutboundReplyParts } from "./reply-payload-CiQ7TPgp.js"; //#region src/auto-reply/reply/block-reply-coalescer.ts function createBlockReplyCoalescer(params) { const { config, shouldAbort, onFlush } = params; const minChars = Math.max(1, Math.floor(config.minChars)); const maxChars = Math.max(minChars, Math.floor(config.maxChars)); const idleMs = Math.max(0, Math.floor(config.idleMs)); const joiner = config.joiner ?? ""; const flushOnEnqueue = config.flushOnEnqueue === true; let bufferText = ""; let bufferReplyToId; let bufferAudioAsVoice; let idleTimer; const clearIdleTimer = () => { if (!idleTimer) return; clearTimeout(idleTimer); idleTimer = void 0; }; const resetBuffer = () => { bufferText = ""; bufferReplyToId = void 0; bufferAudioAsVoice = void 0; }; const scheduleIdleFlush = () => { if (idleMs <= 0) return; clearIdleTimer(); idleTimer = setTimeout(() => { flush({ force: false }); }, idleMs); }; const flush = async (options) => { clearIdleTimer(); if (shouldAbort()) { resetBuffer(); return; } if (!bufferText) return; if (!options?.force && !flushOnEnqueue && bufferText.length < minChars) { scheduleIdleFlush(); return; } const payload = { text: bufferText, replyToId: bufferReplyToId, audioAsVoice: bufferAudioAsVoice }; resetBuffer(); await onFlush(payload); }; const enqueue = (payload) => { if (shouldAbort()) return; const reply = resolveSendableOutboundReplyParts(payload); const hasMedia = reply.hasMedia; const text = reply.text; const hasText = reply.hasText; if (hasMedia) { flush({ force: true }); onFlush(payload); return; } if (!hasText) return; if (flushOnEnqueue) { if (bufferText) flush({ force: true }); bufferReplyToId = payload.replyToId; bufferAudioAsVoice = payload.audioAsVoice; bufferText = text; flush({ force: true }); return; } const replyToConflict = Boolean(bufferText && payload.replyToId && (!bufferReplyToId || bufferReplyToId !== payload.replyToId)); if (bufferText && (replyToConflict || bufferAudioAsVoice !== payload.audioAsVoice)) flush({ force: true }); if (!bufferText) { bufferReplyToId = payload.replyToId; bufferAudioAsVoice = payload.audioAsVoice; } const nextText = bufferText ? `${bufferText}${joiner}${text}` : text; if (nextText.length > maxChars) { if (bufferText) { flush({ force: true }); bufferReplyToId = payload.replyToId; bufferAudioAsVoice = payload.audioAsVoice; if (text.length >= maxChars) { onFlush(payload); return; } bufferText = text; scheduleIdleFlush(); return; } onFlush(payload); return; } bufferText = nextText; if (bufferText.length >= maxChars) { flush({ force: true }); return; } scheduleIdleFlush(); }; return { enqueue, flush, hasBuffered: () => Boolean(bufferText), stop: () => clearIdleTimer() }; } //#endregion //#region src/auto-reply/reply/block-reply-pipeline.ts function createAudioAsVoiceBuffer(params) { let seenAudioAsVoice = false; return { onEnqueue: (payload) => { if (payload.audioAsVoice) seenAudioAsVoice = true; }, shouldBuffer: (payload) => params.isAudioPayload(payload), finalize: (payload) => seenAudioAsVoice ? { ...payload, audioAsVoice: true } : payload }; } function createBlockReplyPayloadKey(payload) { const reply = resolveSendableOutboundReplyParts(payload); return JSON.stringify({ text: reply.trimmedText, mediaList: reply.mediaUrls, replyToId: payload.replyToId ?? null }); } function createBlockReplyContentKey(payload) { const reply = resolveSendableOutboundReplyParts(payload); return JSON.stringify({ text: reply.trimmedText, mediaList: reply.mediaUrls }); } const withTimeout = async (promise, timeoutMs, timeoutError) => { if (!timeoutMs || timeoutMs <= 0) return promise; let timer; const timeoutPromise = new Promise((_, reject) => { timer = setTimeout(() => reject(timeoutError), timeoutMs); }); try { return await Promise.race([promise, timeoutPromise]); } finally { if (timer) clearTimeout(timer); } }; function createBlockReplyPipeline(params) { const { onBlockReply, timeoutMs, coalescing, buffer } = params; const sentKeys = /* @__PURE__ */ new Set(); const sentContentKeys = /* @__PURE__ */ new Set(); const pendingKeys = /* @__PURE__ */ new Set(); const seenKeys = /* @__PURE__ */ new Set(); const bufferedKeys = /* @__PURE__ */ new Set(); const bufferedPayloadKeys = /* @__PURE__ */ new Set(); const bufferedPayloads = []; let sendChain = Promise.resolve(); let aborted = false; let didStream = false; let didLogTimeout = false; const sendPayload = (payload, bypassSeenCheck = false) => { if (aborted) return; const payloadKey = createBlockReplyPayloadKey(payload); const contentKey = createBlockReplyContentKey(payload); if (!bypassSeenCheck) { if (seenKeys.has(payloadKey)) return; seenKeys.add(payloadKey); } if (sentKeys.has(payloadKey) || pendingKeys.has(payloadKey)) return; pendingKeys.add(payloadKey); const timeoutError = /* @__PURE__ */ new Error(`block reply delivery timed out after ${timeoutMs}ms`); const abortController = new AbortController(); sendChain = sendChain.then(async () => { if (aborted) return false; await withTimeout(Promise.resolve(onBlockReply(payload, { abortSignal: abortController.signal, timeoutMs })), timeoutMs, timeoutError); return true; }).then((didSend) => { if (!didSend) return; sentKeys.add(payloadKey); sentContentKeys.add(contentKey); didStream = true; }).catch((err) => { if (err === timeoutError) { abortController.abort(); aborted = true; if (!didLogTimeout) { didLogTimeout = true; logVerbose(`block reply delivery timed out after ${timeoutMs}ms; skipping remaining block replies to preserve ordering`); } return; } logVerbose(`block reply delivery failed: ${String(err)}`); }).finally(() => { pendingKeys.delete(payloadKey); }); }; const coalescer = coalescing ? createBlockReplyCoalescer({ config: coalescing, shouldAbort: () => aborted, onFlush: (payload) => { bufferedKeys.clear(); sendPayload(payload, true); } }) : null; const bufferPayload = (payload) => { buffer?.onEnqueue?.(payload); if (!buffer?.shouldBuffer(payload)) return false; const payloadKey = createBlockReplyPayloadKey(payload); if (seenKeys.has(payloadKey) || sentKeys.has(payloadKey) || pendingKeys.has(payloadKey) || bufferedPayloadKeys.has(payloadKey)) return true; seenKeys.add(payloadKey); bufferedPayloadKeys.add(payloadKey); bufferedPayloads.push(payload); return true; }; const flushBuffered = () => { if (!bufferedPayloads.length) return; for (const payload of bufferedPayloads) sendPayload(buffer?.finalize?.(payload) ?? payload, true); bufferedPayloads.length = 0; bufferedPayloadKeys.clear(); }; const enqueue = (payload) => { if (aborted) return; if (bufferPayload(payload)) return; if (resolveSendableOutboundReplyParts(payload).hasMedia) { coalescer?.flush({ force: true }); sendPayload(payload, false); return; } if (coalescer) { const payloadKey = createBlockReplyPayloadKey(payload); if (seenKeys.has(payloadKey) || pendingKeys.has(payloadKey) || bufferedKeys.has(payloadKey)) return; seenKeys.add(payloadKey); bufferedKeys.add(payloadKey); coalescer.enqueue(payload); return; } sendPayload(payload, false); }; const flush = async (options) => { await coalescer?.flush(options); flushBuffered(); await sendChain; }; const stop = () => { coalescer?.stop(); }; return { enqueue, flush, stop, hasBuffered: () => Boolean(coalescer?.hasBuffered() || bufferedPayloads.length > 0), didStream: () => didStream, isAborted: () => aborted, hasSentPayload: (payload) => { const payloadKey = createBlockReplyContentKey(payload); return sentContentKeys.has(payloadKey); } }; } //#endregion export { createBlockReplyContentKey as n, createBlockReplyPipeline as r, createAudioAsVoiceBuffer as t };