UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

146 lines (145 loc) • 6.57 kB
import { _ as sql, i as executeSqliteQuerySync, n as createSqliteQueryCache, u as prepareSqliteQuerySync } from "./kysely-sync-YJo2lmGV.mjs"; import { n as sha256Hex } from "./node-crypto-B6yuTjmm.mjs"; import { t as getSessionKysely } from "./session-accessor.sqlite-scope-helpers-5_tKUULP.mjs"; import { p as readSessionEntryRow } from "./session-accessor.sqlite-entry-read-csOyyi3K.mjs"; import { t as assertConversationAuthority } from "./conversation-authority-B_Nl9_uN.mjs"; import { i as ConversationDeliveryMissingError, n as normalizeConversationDeliveryOperationId, t as assertConversationDeliveryInput } from "./conversation-delivery-policy-XpNA31Ek.mjs"; import { t as resolveConversationInDatabase } from "./session-accessor.sqlite-conversation-read-kr7lcwOX.mjs"; //#region src/config/sessions/conversation-delivery-store.kernel.ts function normalizeStatus(e) { switch (e) { case "created": case "queued": case "sent": case "suppressed": case "rejected": case "unknown": case "replied": return e; default: throw new Error(`Invalid conversation delivery status: ${e}`); } } function normalizeOperationKind(e) { if (e === "send" || e === "turn") return e; throw new Error(`Invalid conversation delivery operation kind: ${e}`); } function mapRow(e) { const t = e.reply_message_id && e.reply_text !== null && e.reply_timestamp !== null ? { messageId: e.reply_message_id, ...e.reply_to_id ? { replyToId: e.reply_to_id } : {}, ...e.reply_thread_id ? { threadId: e.reply_thread_id } : {}, text: e.reply_text, timestamp: e.reply_timestamp } : void 0; return { operationId: e.operation_id, operationKind: normalizeOperationKind(e.operation_kind), conversationRef: e.conversation_id, channel: e.channel, ...e.source_session_key ? { sourceSessionKey: e.source_session_key } : {}, messageHash: e.message_hash, status: normalizeStatus(e.status), ...e.prepared_message_id ? { preparedMessageId: e.prepared_message_id } : {}, ...e.platform_message_id ? { platformMessageId: e.platform_message_id } : {}, ...e.queue_id ? { queueId: e.queue_id } : {}, ...e.rejection_error ? { rejectionError: e.rejection_error } : {}, ...t ? { reply: t } : {}, createdAt: e.created_at, updatedAt: e.updated_at }; } const operationQuery = createSqliteQueryCache((e) => { const t = getSessionKysely(e); return prepareSqliteQuerySync(e, (e) => t.selectFrom("conversation_deliveries as delivery").innerJoin("conversations as conversation", "conversation.conversation_id", "delivery.conversation_id").selectAll("delivery").select("conversation.channel as channel").where("delivery.operation_id", "=", e((e) => e))); }); function selectOperation(e, t) { const n = operationQuery(e.db)(t).rows[0]; return n ? mapRow(n) : void 0; } function readConversationDeliveryInDatabase(e, t) { if (!("operationId" in t)) return findConversationTurnDeliveryInDatabase(e, t); const { operationId: n, expectedInput: r } = t; const i = selectOperation(e, normalizeConversationDeliveryOperationId(n)); if (i && r) assertConversationDeliveryInput(i, r); return i; } function beginConversationDeliveryInDatabase(e, t) { if (t.authority) assertConversationAuthority(resolveConversationInDatabase(e, t.authority.conversationRef), t.authority); const n = normalizeConversationDeliveryOperationId(t.operationId); const r = t.sourceSessionKey?.trim() || void 0; const i = sha256Hex(t.message); const a = selectOperation(e, n); if (a) { assertConversationDeliveryInput(a, t, i); return { created: false, record: a }; } const o = Date.now(); const s = getSessionKysely(e.db); executeSqliteQuerySync(e.db, s.insertInto("conversation_deliveries").values({ operation_id: n, operation_kind: t.operationKind, conversation_id: t.conversationRef, source_session_key: r ?? null, message_hash: i, status: "created", prepared_message_id: t.preparedMessageId ?? null, platform_message_id: null, queue_id: null, rejection_error: null, reply_message_id: null, reply_to_id: null, reply_thread_id: null, reply_text: null, reply_timestamp: null, created_at: o, updated_at: o })); const c = selectOperation(e, n); if (!c) throw new Error(`Conversation delivery operation was not persisted: ${n}`); return { created: true, record: c }; } function transitionConversationDeliveryInDatabase(e, t) { if (t.session) { const { sessionKey: n, sessionId: r, lifecycleRevision: i } = t.session; const a = readSessionEntryRow(e, n)?.entry; if (a?.sessionId !== r || a.lifecycleRevision !== i) throw new Error(`session changed before captured reply persistence: ${n}`); } const n = normalizeConversationDeliveryOperationId(t.operationId); const r = selectOperation(e, n); if (!r) throw new ConversationDeliveryMissingError(`Conversation delivery operation not found: ${n}`); if (!t.allowedFrom.includes(r.status)) return r; const i = getSessionKysely(e.db); executeSqliteQuerySync(e.db, i.updateTable("conversation_deliveries").set({ status: t.status, ...t.queueId !== void 0 ? { queue_id: t.queueId } : {}, ...t.platformMessageId !== void 0 ? { platform_message_id: t.platformMessageId } : {}, ...t.rejectionError !== void 0 ? { rejection_error: t.rejectionError } : {}, ...t.reply ? { reply_message_id: t.reply.messageId, reply_to_id: t.reply.replyToId ?? null, reply_thread_id: t.reply.threadId ?? null, reply_text: t.reply.text, reply_timestamp: t.reply.timestamp } : {}, updated_at: Date.now() }).where("operation_id", "=", n)); const a = selectOperation(e, n); if (!a) throw new Error(`Conversation delivery operation disappeared: ${n}`); return a; } function findConversationTurnDeliveryInDatabase(e, t) { const n = getSessionKysely(e.db); const r = executeSqliteQuerySync(e.db, n.selectFrom("conversation_deliveries as delivery").innerJoin("conversations as conversation", "conversation.conversation_id", "delivery.conversation_id").selectAll("delivery").select("conversation.channel as channel").where("delivery.conversation_id", "=", t.conversationRef).where("delivery.operation_kind", "=", "turn").where((e) => e.or([e("delivery.platform_message_id", "=", t.replyToId), e("delivery.prepared_message_id", "=", t.replyToId)])).where("delivery.status", "in", [ "queued", "sent", "replied" ].map((e) => sql.lit(e))).orderBy("delivery.updated_at", "desc").limit(1)).rows[0]; return r ? mapRow(r) : void 0; } //#endregion export { beginConversationDeliveryInDatabase, readConversationDeliveryInDatabase, transitionConversationDeliveryInDatabase };