UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

111 lines (110 loc) • 4.83 kB
import { a as executeSqliteQueryTakeFirstSync, i as executeSqliteQuerySync, s as getNodeSqliteKysely } from "./kysely-sync-YJo2lmGV.mjs"; import { t as createCommittedWorkerMutation } from "./worker-operation-registry-DziQDvVs.mjs"; //#region src/gateway/managed-image-record-store.kernel.ts const MANAGED_IMAGE_RECORD_COLUMNS = [ "attachment_id", "session_key", "agent_id", "message_id", "created_at", "updated_at", "retention_class", "alt", "original_media_root", "original_media_id", "original_media_subdir", "original_content_type", "original_width", "original_height", "original_size_bytes", "original_filename", "cleanup_pending" ]; function managedImageRecordToRow(e) { return { attachment_id: e.attachmentId, session_key: e.sessionKey, agent_id: e.agentId ?? null, message_id: e.messageId, created_at: e.createdAt, updated_at: e.updatedAt ?? null, retention_class: e.retentionClass ?? null, alt: e.alt, original_media_root: e.original.mediaRoot, original_media_id: e.original.mediaId, original_media_subdir: e.original.mediaSubdir, original_content_type: e.original.contentType, original_width: e.original.width, original_height: e.original.height, original_size_bytes: e.original.sizeBytes, original_filename: e.original.filename, record_json: JSON.stringify(e) }; } function managedImageRecordFromRow(e) { return { attachmentId: e.attachment_id, sessionKey: e.session_key, ...e.agent_id ? { agentId: e.agent_id } : {}, messageId: e.message_id, createdAt: e.created_at, ...e.updated_at ? { updatedAt: e.updated_at } : {}, ...e.retention_class === "history" || e.retention_class === "transient" ? { retentionClass: e.retention_class } : {}, alt: e.alt, original: { mediaRoot: e.original_media_root, mediaId: e.original_media_id, mediaSubdir: e.original_media_subdir, contentType: e.original_content_type, width: e.original_width, height: e.original_height, sizeBytes: e.original_size_bytes, filename: e.original_filename } }; } function managedImageRecordsEqual(e, t) { return JSON.stringify(e) === JSON.stringify(t); } function insertManagedImageRecordInDatabase(e, t) { executeSqliteQuerySync(e, getNodeSqliteKysely(e).insertInto("managed_outgoing_image_records").values(managedImageRecordToRow(t))); return true; } /** Promote a transient record atomically so concurrent message commits cannot lose state. */ function attachManagedImageRecordInDatabase(e, t) { const n = getNodeSqliteKysely(e); const r = executeSqliteQueryTakeFirstSync(e, n.selectFrom("managed_outgoing_image_records").select(MANAGED_IMAGE_RECORD_COLUMNS).where("attachment_id", "=", t.attachmentId).where("session_key", "=", t.sessionKey)); if (!r || r.cleanup_pending === 1) return false; const i = managedImageRecordFromRow(r); if (i.messageId === t.messageId && i.retentionClass === "history") return true; const a = managedImageRecordToRow({ ...i, messageId: t.messageId, retentionClass: "history", updatedAt: t.updatedAt }); executeSqliteQuerySync(e, n.updateTable("managed_outgoing_image_records").set({ message_id: a.message_id, retention_class: a.retention_class, updated_at: a.updated_at, record_json: a.record_json }).where("attachment_id", "=", t.attachmentId)); return true; } /** Both cleanup transitions require the exact planned row; concurrent updates win. */ function mutateManagedImageCleanupInDatabase(e, t, n) { const r = getNodeSqliteKysely(e); const i = executeSqliteQueryTakeFirstSync(e, r.selectFrom("managed_outgoing_image_records").select(MANAGED_IMAGE_RECORD_COLUMNS).where("attachment_id", "=", t.attachmentId)); if (!i || i.cleanup_pending === 1 !== (n === "delete") || !managedImageRecordsEqual(managedImageRecordFromRow(i), t)) return false; if (n === "delete") executeSqliteQuerySync(e, r.deleteFrom("managed_outgoing_image_records").where("attachment_id", "=", t.attachmentId)); else executeSqliteQuerySync(e, r.updateTable("managed_outgoing_image_records").set({ cleanup_pending: 1 }).where("attachment_id", "=", t.attachmentId)); return true; } const managedImageRecordOperations = { "managedImages.insert": createCommittedWorkerMutation("managedImages.insert", insertManagedImageRecordInDatabase), "managedImages.attach": createCommittedWorkerMutation("managedImages.attach", attachManagedImageRecordInDatabase), "managedImages.claimCleanup": createCommittedWorkerMutation("managedImages.claimCleanup", (e, t) => mutateManagedImageCleanupInDatabase(e, t, "claim")), "managedImages.deleteClaimed": createCommittedWorkerMutation("managedImages.deleteClaimed", (e, t) => mutateManagedImageCleanupInDatabase(e, t, "delete")) }; //#endregion export { managedImageRecordsEqual as i, managedImageRecordOperations as n, managedImageRecordToRow as r, managedImageRecordFromRow as t };