openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
88 lines (87 loc) • 5.17 kB
JavaScript
import { a as executeSqliteQueryTakeFirstSync, i as executeSqliteQuerySync, s as getNodeSqliteKysely } from "./kysely-sync-YJo2lmGV.mjs";
import { a as runSqliteImmediateTransactionSync } from "./sqlite-transaction-BHHu6P5-.mjs";
import { _ as upsertDeliveryQueueEntryInDatabase, a as deleteDeliveryQueueEntryInDatabase, l as getDeliveryQueueEntryOwnersInDatabase, t as completeDeliveryQueueEntryInDatabase } from "./delivery-queue-sqlite.kernel-Cv08QjUU.mjs";
//#region src/infra/delivery-queue-sqlite-namespace.kernel.ts
/** Atomically publishes one staged owner only when retired namespaces do not own its id. */
function commitStagedDeliveryQueueEntryOnceAcrossNamespacesInDatabase(e, t) {
return runSqliteImmediateTransactionSync(e.db, () => {
const n = getNodeSqliteKysely(e.db);
if (!loadPendingDeliveryQueueRow(e, t.stagingQueueName, t.stagingId)) return "missing";
if (!insertDeliveryQueueOwner(e, t)) return "existing";
if (executeSqliteQuerySync(e.db, n.deleteFrom("delivery_queue_entries").where("queue_name", "=", t.stagingQueueName).where("id", "=", t.stagingId).where("status", "=", "pending")).numAffectedRows !== 1n) throw new Error(`Delivery queue staging row changed during commit: ${t.stagingQueueName}/${t.stagingId}`);
return "created";
}, {
databaseLabel: e.path,
operationLabel: "commit staged stable delivery queue owner"
});
}
/** The caller holds the write transaction across admission and publication. */
function insertDeliveryQueueOwner(e, t) {
return getDeliveryQueueEntryOwnersInDatabase(e, [t.queueName, ...t.conflictQueueNames], t.entry.id).size === 0 && upsertDeliveryQueueEntryInDatabase({
queueName: t.queueName,
entry: t.entry,
insertOnly: true
}, e);
}
/** Inserts one stable owner only when no current or retired namespace owns its id. */
function upsertDeliveryQueueEntryOnceAcrossNamespacesInDatabase(e, t) {
return runSqliteImmediateTransactionSync(e.db, () => insertDeliveryQueueOwner(e, t), {
databaseLabel: e.path,
operationLabel: "insert stable delivery queue owner"
});
}
function loadPendingDeliveryQueueRow(e, t, n) {
return executeSqliteQueryTakeFirstSync(e.db, getNodeSqliteKysely(e.db).selectFrom("delivery_queue_entries").select("entry_json").where("queue_name", "=", t).where("id", "=", n).where("status", "=", "pending"));
}
function matchesPendingDeliveryQueueEntry(e, t, n) {
return loadPendingDeliveryQueueRow(e, t, n.id)?.entry_json === JSON.stringify(n);
}
function pendingEntryMutation(e, t, n) {
return (r, i) => {
n?.(i);
return runSqliteImmediateTransactionSync(r.db, () => matchesPendingDeliveryQueueEntry(r, i.queueName, i.expectedEntry) && t(r, i), {
databaseLabel: r.path,
operationLabel: e
});
};
}
function assertDeliveryQueueReplacement(e) {
if (e.expectedEntry.id !== e.replacementEntry.id) throw new Error(`Delivery queue replacement id mismatch: ${e.expectedEntry.id} != ${e.replacementEntry.id}`);
}
/** Replaces a pending entry only while its authoritative serialized value is unchanged. */
const replacePendingDeliveryQueueEntryInDatabase = pendingEntryMutation("replace pending delivery queue entry", (e, t) => upsertDeliveryQueueEntryInDatabase({
queueName: t.queueName,
entry: t.replacementEntry,
updatePendingOnly: true
}, e), assertDeliveryQueueReplacement);
/** Completes a pending entry only while its authoritative serialized value is unchanged. */
const completePendingDeliveryQueueEntryInDatabase = pendingEntryMutation("complete pending delivery queue entry", (e, t) => {
completeDeliveryQueueEntryInDatabase(e, t.queueName, t.expectedEntry.id);
return true;
});
/**
* Commits an asynchronously prepared replacement only if the authoritative
* source row is unchanged, then removes or terminally fences the old owner.
*/
function movePendingDeliveryQueueEntryNamespaceInDatabase(e, t) {
return runSqliteImmediateTransactionSync(e.db, () => {
if (!matchesPendingDeliveryQueueEntry(e, t.sourceQueueName, t.expectedSourceEntry)) return "source-changed";
if (getDeliveryQueueEntryOwnersInDatabase(e, [t.destinationQueueName, ...t.conflictQueueNames ?? []], t.destinationEntry.id).size > 0) return "destination-exists";
if (t.stagingId && t.stagingQueueName) {
if (!loadPendingDeliveryQueueRow(e, t.stagingQueueName, t.stagingId)) return "staging-missing";
}
if (!upsertDeliveryQueueEntryInDatabase({
queueName: t.destinationQueueName,
entry: t.destinationEntry,
insertOnly: true
}, e)) return "destination-exists";
(t.retainSourceCompletionFence ? completeDeliveryQueueEntryInDatabase : deleteDeliveryQueueEntryInDatabase)(e, t.sourceQueueName, t.expectedSourceEntry.id);
if (t.stagingId && t.stagingQueueName) deleteDeliveryQueueEntryInDatabase(e, t.stagingQueueName, t.stagingId);
return "moved";
}, {
databaseLabel: e.path,
operationLabel: "migrate delivery queue namespace"
});
}
//#endregion
export { upsertDeliveryQueueEntryOnceAcrossNamespacesInDatabase as a, replacePendingDeliveryQueueEntryInDatabase as i, completePendingDeliveryQueueEntryInDatabase as n, movePendingDeliveryQueueEntryNamespaceInDatabase as r, commitStagedDeliveryQueueEntryOnceAcrossNamespacesInDatabase as t };