openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
95 lines (94 loc) • 5.23 kB
JavaScript
import { a as runSqliteImmediateTransactionSync } from "./sqlite-transaction-BHHu6P5-.mjs";
import { l as loadDeliveryQueueEntryInDatabase, t as hasLiveDeliveryQueueClaim } from "./delivery-queue-sqlite.types-B7uW265Q.mjs";
import { a as generateSecureUuid } from "./secure-random-CW5OZnK1.mjs";
import { _ as upsertDeliveryQueueEntryInDatabase } from "./delivery-queue-sqlite.kernel-Cv08QjUU.mjs";
//#region src/infra/delivery-queue-sqlite-claim.kernel.ts
const PLATFORM_SEND_OWNER_LEASE_MS = 6e4;
/** Creates the owner published atomically with an immediate live delivery. */
function createInitialDeliveryProducerClaim(e = Date.now()) {
return {
requiresProducerClaim: true,
availableAt: e + PLATFORM_SEND_OWNER_LEASE_MS,
producerClaimId: generateSecureUuid(),
recoveryState: "producer_claimed"
};
}
/** Runs an existing queue mutation only while its exact platform owner survives. */
function transitionOwnedDeliveryQueueEntryInDatabase(e, t, n) {
return runSqliteImmediateTransactionSync(e.db, () => {
const r = loadDeliveryQueueEntryInDatabase(e, t.queueName, t.id, "pending");
if (!r) return t.allowMissingEntry === true;
if (t.platformSendAttemptId === null ? r.platformSendAttemptId !== void 0 || r.producerClaimId !== void 0 : r.platformSendAttemptId !== t.platformSendAttemptId && r.producerClaimId !== t.platformSendAttemptId) return false;
n(r, e);
return true;
}, {
databaseLabel: e.path,
operationLabel: `mutate owned ${t.queueName} delivery platform send`
});
}
function transitionDeliveryQueueEntryPlatformSendInDatabase(e, t, n, r) {
return runSqliteImmediateTransactionSync(e.db, () => {
const i = loadDeliveryQueueEntryInDatabase(e, t.queueName, t.id, "pending");
if (!i) return false;
if (i.platformSendStartedAt !== void 0 && (n === "promote" || n === "claim" && (i.platformSendStartedAt !== t.reconciledPlatformSendStartedAt || i.platformSendAttemptId !== t.reconciledPlatformSendAttemptId || typeof i.platformSendAttemptId !== "string"))) return false;
const a = r(i, Date.now());
return a ? upsertDeliveryQueueEntryInDatabase({
queueName: t.queueName,
entry: a,
updatePendingOnly: true
}, e) : false;
}, {
databaseLabel: e.path,
operationLabel: `${n} ${t.queueName} delivery platform send`
});
}
/** Claim a recoverable producer lease before any provider invocation. */
function claimDeliveryQueueEntryPlatformSendInDatabase(e, t, n = generateSecureUuid()) {
return transitionDeliveryQueueEntryPlatformSendInDatabase(e, t, "claim", (e, r) => {
const i = (e.recoveryState === "send_attempt_started" || e.recoveryState === "unknown_after_send" && t.allowUnknownSendReplay === true) && typeof t.reconciledPlatformSendStartedAt === "number" && e.platformSendStartedAt === t.reconciledPlatformSendStartedAt && typeof t.reconciledPlatformSendAttemptId === "string" && e.platformSendAttemptId === t.reconciledPlatformSendAttemptId;
if (e.recoveryState && !i && (e.recoveryState !== "producer_claimed" || typeof e.availableAt !== "number" || e.availableAt > r)) return;
return {
...e,
...t.requiresProducerClaim === true ? { requiresProducerClaim: true } : {},
availableAt: r + 6e4,
producerClaimId: n,
platformSendAttemptId: void 0,
platformSendStartedAt: void 0,
recoveryState: "producer_claimed",
...i && t.allowUnknownSendReplay === true ? { maxRetries: Math.min(e.maxRetries ?? Infinity, 2) } : {}
};
}) ? n : void 0;
}
/** Renew only the exact unexpired producer that already owns the row. */
function renewDeliveryQueueEntryPlatformSendLeaseInDatabase(e, t) {
let n;
return transitionDeliveryQueueEntryPlatformSendInDatabase(e, t, "renew", (e, r) => {
if (e.requiresProducerClaim !== true || !hasLiveDeliveryQueueClaim(e, t.claimId, r)) return;
n = r + 6e4;
return {
...e,
availableAt: n
};
}) ? n : void 0;
}
function platformSendTransition(e) {
return (t, n) => {
return transitionDeliveryQueueEntryPlatformSendInDatabase(t, n, e, (t, r) => {
if (e === "promote" && t.recoveryState !== "producer_claimed" || !hasLiveDeliveryQueueClaim(t, n.claimId, r)) return;
return {
...t,
availableAt: t.requiresProducerClaim === true ? t.recoveryState === "producer_claimed" ? r + PLATFORM_SEND_OWNER_LEASE_MS : t.availableAt : void 0,
producerClaimId: void 0,
platformSendAttemptId: n.claimId,
platformSendStartedAt: r,
...n.route && "replyToId" in n.route ? { effectiveReplyToId: n.route.replyToId ?? null } : {},
recoveryState: t.recoveryState === "unknown_after_send" ? "unknown_after_send" : "send_attempt_started"
};
});
};
}
/** Atomically fence the exact unexpired owner at the real provider boundary. */
const promoteDeliveryQueueEntryPlatformSendInDatabase = platformSendTransition("promote");
const dispatchDeliveryQueueEntryPlatformSendInDatabase = platformSendTransition("dispatch");
//#endregion
export { promoteDeliveryQueueEntryPlatformSendInDatabase as a, dispatchDeliveryQueueEntryPlatformSendInDatabase as i, claimDeliveryQueueEntryPlatformSendInDatabase as n, renewDeliveryQueueEntryPlatformSendLeaseInDatabase as o, createInitialDeliveryProducerClaim as r, transitionOwnedDeliveryQueueEntryInDatabase as s, PLATFORM_SEND_OWNER_LEASE_MS as t };