UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

3 lines • 4.2 kB
import{r as defaultRuntime}from"./runtime-DIcLIHcp.mjs";import{r as isOperatorAuthorityUnavailable}from"./operator-authority-error-B9EU9j0W.mjs";import{_ as completeSessionControllerSourceLifecycle,b as retireSessionControllerInput}from"./session-controller.lifecycle-M3NLAkoz.mjs";import{t as replyCallbackCustody}from"./reply-callback-custody-HLqN_TWY.mjs";import{b as updateFollowupRetryHeartbeatsWithCustody,u as submitSessionControllerInput,y as startFollowupRunPreAdoptionHeartbeatWithCustody}from"./session-controller.mailbox-9GxioY-x.mjs"; //!src/auto-reply/reply/queue/lifecycle.ts function custody(e){if(e.controllerClaim)return e.controllerClaim.custody;return(`run`in e?e.controllerInput??submitSessionControllerInput(e.run.sessionKey??e.run.sessionId,e,{mode:`followup`}):e.controllerInput).custody}const abandonedDeliveryCorrelations=/* @__PURE__ */ new WeakMap;function retainQueuedDeliveryCorrelations(e,t){if(!e.deliveryCorrelations?.length)return;const n=[];e.deliveryCorrelations=e.deliveryCorrelations.map(e=>{let t=true;n.push(()=>{if(!t)return;t=false;try{const t=e.begin();if(typeof t===`function`)t({visibleReplySent:false,failed:true})}catch(e){defaultRuntime.error?.(`followup queue: delivery correlation cleanup failed: ${String(e)}`)}});return{...e,begin:()=>{t=false;const n=e.begin();if(typeof n!==`function`)return n;return e=>{t=e===void 0;n(e)}}}});abandonedDeliveryCorrelations.set(t,()=>{for(const e of n)e()});const r=t.disposeSource;t.disposeSource=()=>{const e=abandonedDeliveryCorrelations.get(t);abandonedDeliveryCorrelations.delete(t);try{r?.()}finally{if(!t.adopted)e?.()}}}function updateFollowupRetryHeartbeats(e,t){updateFollowupRetryHeartbeatsWithCustody(e,t,custody)}function startFollowupRunPreAdoptionHeartbeat(e,t){return startFollowupRunPreAdoptionHeartbeatWithCustody(e,()=>custody(e),t)}function markFollowupRunEnqueued(e){const t=custody(e);const n=e.operatorAuthority;n?.signal?.throwIfAborted();n?.assertCurrent();if(!t.enqueued){t.enqueued=true;try{t.releaseCallbacks=e[replyCallbackCustody]?.retain();if(e.turnAdoptionLifecycle?.onDeferred?.()===false){t.releaseCallbacks?.();t.releaseCallbacks=void 0;t.enqueued=false;return false}t.releaseAuthority=n?.retain?.()}catch(n){t.enqueued=false;completeFollowupRunLifecycle(e);throw n}retainQueuedDeliveryCorrelations(e,t);startFollowupRunPreAdoptionHeartbeat(e)}return true}function retireFollowupRunCancellation(e){const t=custody(e);if(t.cancellationRetired)return;t.cancellationRetired=true;e.turnAdoptionLifecycle?.onCancellationRetired?.()}async function admitFollowupRunLifecycle(e,t){const n=custody(e);e.operatorAuthority?.assertCurrent();if(n.adopted)return await t?.();if(n.adopting){await n.adopting;return await t?.()}if(n.completed)throw new Error(`Input completed before source adoption`);const r=Promise.resolve().then(async()=>{if(n.enqueued)try{e.operatorAuthority?.reauthorize?.()}catch(t){const r=e.controllerInput;if(r&&!isOperatorAuthorityUnavailable(t)){n.failure=t;const i=e.userTurnTranscriptRecorder;try{i?.markBlocked();i?.finishPendingInput?.(`cancelled`);await i?.waitForPendingInputSettlement?.()}finally{retireSessionControllerInput(r)}}throw t}await t?.();await e.turnAdoptionLifecycle?.onAdopted();n.adopted=true;n.failure=void 0;n.stopHeartbeat?.();e.operatorAuthority?.assertCurrent()});n.adopting=r;try{await r}catch(e){n.failure=e;throw e}finally{n.adopting=void 0}}function completeFollowupRunLifecycle(e,t){const n=custody(e);if(!n.completed&&t===`consumed`)abandonedDeliveryCorrelations.delete(n);completeSessionControllerSourceLifecycle(e,n,t)}async function refuseCollaborativeSteering(e,t,n){if(t)t.admission={status:`skipped`,reason:`collaborative-steering-disabled`};const r=e.userTurnTranscriptRecorder;r?.markBlocked();try{r?.finishPendingInput?.(`cancelled`);await r?.waitForPendingInputSettlement?.();await admitFollowupRunLifecycle(e)}finally{if(n)n();else completeFollowupRunLifecycle(e,`consumed`)}}export{retireFollowupRunCancellation as a,refuseCollaborativeSteering as i,completeFollowupRunLifecycle as n,startFollowupRunPreAdoptionHeartbeat as o,markFollowupRunEnqueued as r,updateFollowupRetryHeartbeats as s,admitFollowupRunLifecycle as t};