openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
7 lines • 8.16 kB
JavaScript
import{R as resolveTimerTimeoutMs}from"./number-coercion-B3CfI447.mjs";import{t as sleep}from"./sleep-B4CgCyZu.mjs";import{t as formatErrorMessage}from"./errors-D37BJTfI.mjs";import{n as awaitWithinDeadline,t as ABSOLUTE_DEADLINE_EXPIRED}from"./absolute-deadline-CE14TWdo.mjs";import{i as ErrorCodes}from"./missing-scope-error-CkgBgh6d.mjs";import{p as errorShape}from"./error-codes-CQNGhXT-.mjs";import{c as runNodeWakeAttempt,l as runNodeWakeNudgeAttempt,n as NODE_WAKE_RECONNECT_WAIT_MS,o as isNodeWakeLifecycleCurrent,r as captureNodeWakeLifecycle,s as releaseNodeWakeLifecycle,t as NODE_WAKE_RECONNECT_RETRY_WAIT_MS}from"./node-wake-state-CFYzfbEQ.mjs";import{t as nodeInvokePolicy}from"./nodes-policy-JmvffcOg.mjs";import{n as clearApnsRegistrationIfCurrent,o as loadApnsRegistration}from"./push-apns-store-DWo_qlu5.mjs";import{c as shouldClearStoredApnsRegistration,n as sendApnsAlert,r as sendApnsBackgroundWake}from"./push-apns-BIQUd9Be.mjs";import{r as isNodePairingGenerationCurrent}from"./device-pairing-node-state-USLLjQBY.mjs";import{t as resolveNodePushTransport}from"./node-push-transport-JQ_qGMu9.mjs";
//!src/gateway/server-methods/nodes.shared.ts
function respondPairingChanged(e,t=`invocation`){e(false,void 0,errorShape(ErrorCodes.UNAVAILABLE,`node pairing changed while ${t} was active`,{retryable:true,details:{code:`PAIRING_CHANGED`}}))}async function isNodePairingWorkCurrent(e){if(!isNodeWakeLifecycleCurrent(e.nodeId,e.lifecycle,e.generation.key))return false;if(!await isNodePairingGenerationCurrent(e.generation))return false;return isNodeWakeLifecycleCurrent(e.nodeId,e.lifecycle,e.generation.key)}async function isNodePushAttemptCurrent(e){return e.generation?isNodePairingWorkCurrent({nodeId:e.nodeId,generation:e.generation,lifecycle:e.lifecycle}):isNodeWakeLifecycleCurrent(e.nodeId,e.lifecycle)}function resolveDispatchableNodeSession(e){return e?.client?.invalidated===true?void 0:e}
//!src/gateway/server-methods/nodes.wake.ts
async function clearStaleApnsRegistrationIfNeeded(e,t,n){if(shouldClearStoredApnsRegistration({registration:e,result:n}))await clearApnsRegistrationIfCurrent({nodeId:t,registration:e})}async function maybeWakeNodeWithApns(e,t){const n=t?.lifecycle!==void 0;const r=t?.generation?.key;const i=t?.lifecycle??captureNodeWakeLifecycle(e,r);const isAttemptCurrent=()=>isNodePushAttemptCurrent({nodeId:e,lifecycle:i,generation:t?.generation});try{if(!await isAttemptCurrent())return{available:false,throttled:false,path:`invalidated`,durationMs:0};const n=await runNodeWakeAttempt({nodeId:e,pairingGeneration:r,force:t?.force===true,throttleMs:nodeInvokePolicy.wakeThrottleMs,attempt:async n=>{const r=performance.now();let a=false;const withDuration=e=>({...e,durationMs:Math.round(performance.now()-r)});const markWakeAttempted=()=>{a=true;n()};try{if(!await isAttemptCurrent())return withDuration({available:false,throttled:false,path:`invalidated`});const n=await loadApnsRegistration(e);if(!await isAttemptCurrent())return withDuration({available:false,throttled:false,path:`invalidated`});if(!n)return withDuration({available:false,throttled:false,path:`no-registration`});const r=await resolveNodePushTransport(n,t?.cfg);if(!r.ok)return withDuration({available:false,throttled:false,path:`no-auth`,apnsReason:r.error});if(!await isAttemptCurrent())return withDuration({available:false,throttled:false,path:`invalidated`});markWakeAttempted();const a=await sendApnsBackgroundWake({...r.transport,nodeId:e,wakeReason:t?.wakeReason??`node.invoke`,signal:i,isCurrent:isAttemptCurrent});if(!await isAttemptCurrent())return withDuration({available:false,throttled:false,path:`invalidated`});await clearStaleApnsRegistrationIfNeeded(n,e,a);return withDuration({available:true,throttled:false,path:a.ok?`sent`:`send-error`,apnsStatus:a.status,apnsReason:a.reason})}catch(e){if(!await isAttemptCurrent())return withDuration({available:false,throttled:false,path:`invalidated`});return withDuration({available:a,throttled:false,path:`send-error`,apnsReason:formatErrorMessage(e)})}}});return await isAttemptCurrent()?n:{available:false,throttled:false,path:`invalidated`,durationMs:0}}finally{if(!n)releaseNodeWakeLifecycle(e,i)}}async function maybeSendNodeWakeNudge(e,t){const n=performance.now();const withDuration=e=>({...e,durationMs:Math.round(performance.now()-n)});const r=t?.lifecycle!==void 0;const i=t?.generation?.key;const a=t?.lifecycle??captureNodeWakeLifecycle(e,i);const isAttemptCurrent=()=>isNodePushAttemptCurrent({nodeId:e,lifecycle:a,generation:t?.generation});try{if(!await isAttemptCurrent())return withDuration({sent:false,throttled:false,reason:`invalidated`});return await runNodeWakeNudgeAttempt({nodeId:e,pairingGeneration:i,throttleMs:nodeInvokePolicy.wakeNudgeThrottleMs,throttled:()=>withDuration({sent:false,throttled:true,reason:`throttled`}),attempt:async()=>{const n=await loadApnsRegistration(e);if(!await isAttemptCurrent())return withDuration({sent:false,throttled:false,reason:`invalidated`});if(!n)return withDuration({sent:false,throttled:false,reason:`no-registration`});try{const r=await resolveNodePushTransport(n,t?.cfg);if(!r.ok)return withDuration({sent:false,throttled:false,reason:`no-auth`,apnsReason:r.error});if(!await isAttemptCurrent())return withDuration({sent:false,throttled:false,reason:`invalidated`});const i=await sendApnsAlert({...r.transport,nodeId:e,title:`OpenClaw needs a quick reopen`,body:`Tap to reopen OpenClaw and restore the node connection.`,signal:a,isCurrent:isAttemptCurrent});if(!await isAttemptCurrent())return withDuration({sent:i.ok,throttled:false,reason:`invalidated`});await clearStaleApnsRegistrationIfNeeded(n,e,i);if(!await isAttemptCurrent())return withDuration({sent:i.ok,throttled:false,reason:`invalidated`});return withDuration({sent:i.ok,throttled:false,reason:i.ok?`sent`:`apns-not-ok`,apnsStatus:i.status,apnsReason:i.reason})}catch(e){if(!await isAttemptCurrent())return withDuration({sent:false,throttled:false,reason:`invalidated`});return withDuration({sent:false,throttled:false,reason:`send-error`,apnsReason:formatErrorMessage(e)})}}})}finally{if(!r)releaseNodeWakeLifecycle(e,a)}}async function waitForNodeReconnect(e){const t=resolveTimerTimeoutMs(e.timeoutMs,NODE_WAKE_RECONNECT_WAIT_MS,1);const n=performance.now()+t;for(;;){const t=performance.now()<n;if(e.lifecycle&&!isNodeWakeLifecycleCurrent(e.nodeId,e.lifecycle,e.pairingGeneration))return false;const r=e.pairingGeneration?e.context.nodeRegistry.getForPairingGeneration(e.nodeId,e.pairingGeneration):e.context.nodeRegistry.get(e.nodeId);if(resolveDispatchableNodeSession(r))return true;if(!t)return false;await sleep(Math.min(150,Math.max(0,n-performance.now())))}}
//!src/gateway/server-methods/nodes.wake-reconnect.ts
async function wakeNodeForReconnect(e){const{nodeId:t,context:n,cfg:r,generation:i,lifecycle:a,force:o,deadlineAtMs:s}=e;const c=await awaitWithinDeadline(()=>maybeWakeNodeWithApns(t,{...o?{force:true}:{},...e.source===`pending`?{wakeReason:`node.pending`}:{},cfg:r,lifecycle:a,generation:i}),s,()=>performance.now());if(c===ABSOLUTE_DEADLINE_EXPIRED)return c;const l=e.source===`invoke`?`node wake`:`node pending wake`;const u=o?2:1;n.logGateway.info(`${l} stage=wake${u} node=${t} req=${e.requestId}${o?` force=true`:``} available=${c.available} throttled=${c.throttled} path=${c.path} durationMs=${c.durationMs} apnsStatus=${c.apnsStatus??-1} apnsReason=${c.apnsReason??`-`}`);if(s!==void 0&&performance.now()>=s)return ABSOLUTE_DEADLINE_EXPIRED;if(c.available){const r=Date.now();const c=o?NODE_WAKE_RECONNECT_RETRY_WAIT_MS:NODE_WAKE_RECONNECT_WAIT_MS;const d=s===void 0?c:Math.min(c,Math.max(0,s-performance.now()));const f=await waitForNodeReconnect({nodeId:t,context:n,timeoutMs:d,lifecycle:a,pairingGeneration:i.key});const p=e.source===`invoke`?` durationMs=${Math.max(0,Date.now()-r)}`:``;n.logGateway.info(`${l} stage=wait${u} node=${t} req=${e.requestId} reconnected=${f} timeoutMs=${d}${p}`)}return c}export{isNodePairingWorkCurrent as a,waitForNodeReconnect as i,maybeSendNodeWakeNudge as n,resolveDispatchableNodeSession as o,maybeWakeNodeWithApns as r,respondPairingChanged as s,wakeNodeForReconnect as t};