UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

3 lines • 4.25 kB
import{a as runSqliteImmediateTransactionSync}from"./sqlite-transaction-CON-dGDv.mjs";import{l as loadDeliveryQueueEntryInDatabase,t as hasLiveDeliveryQueueClaim}from"./delivery-queue-sqlite.types-glc5FW3-.mjs";import{a as generateSecureUuid}from"./secure-random-CDFMDkJP.mjs";import{_ as upsertDeliveryQueueEntryInDatabase}from"./delivery-queue-sqlite.kernel-DjujS9GY.mjs"; //!src/infra/delivery-queue-sqlite-claim.kernel.ts const PLATFORM_SEND_OWNER_LEASE_MS=6e4;function createInitialDeliveryProducerClaim(e=Date.now()){return{requiresProducerClaim:true,availableAt:e+PLATFORM_SEND_OWNER_LEASE_MS,producerClaimId:generateSecureUuid(),recoveryState:`producer_claimed`}}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`})}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}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`}})}}const promoteDeliveryQueueEntryPlatformSendInDatabase=platformSendTransition(`promote`);const dispatchDeliveryQueueEntryPlatformSendInDatabase=platformSendTransition(`dispatch`);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};