UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

3 lines • 5.39 kB
import{t as freezeJsonSnapshot}from"./immutable-data-CyaF4H_G.mjs";import{t as isAbortError}from"./abort-signal-BKkRe6pu.mjs";import{o as getPluginRuntimeGatewayRequestScope}from"./gateway-request-scope-7deHmsb_.mjs";import{n as operatorScopeSatisfied,t as intersectOperatorScopes}from"./operator-scope-compat-TmgAKmD2.mjs";import{s as captureCronJobSourceAuthority}from"./active-jobs-QNXDOaIz.mjs";import{a as isSameOpenClawIdTunnelPrincipal,x as GatewayDispatchUnavailableError}from"./tunnel-principal-CmwEB_uz.mjs";import{r as hasAutomationAdminDelegation,t as decodeGatewayOperatorRecoverySource}from"./operator-run-recovery-source.codec-DkQhl9C9.mjs";import{t as CronOperatorSourceError}from"./operator-source-error-C5EqRJig.mjs";import{n as isCronOperatorSourceParent}from"./session-target-BE2kgjYz.mjs";import{i as sourceRolePolicy}from"./operator-role-source-policy-FkG4nDbU.mjs";import{i as tryResolveCronJobEffectiveAgentId}from"./agent-id-DdcCg4fI.mjs";import{n as restoreGatewayOperatorSource}from"./operator-run-recovery-Cx3DshKV.mjs";import{u as resolveCronOperatorExecutionRevision}from"./jobs-tool-policy-Dbd5Denq.mjs";import{isDeepStrictEqual}from"node:util"; //!src/gateway/cron-operator-source.ts async function restoreCronOperatorSource(e){if(!e.job.operatorSource)return;let t;try{const n=e.job.operatorSource;const r=decodeGatewayOperatorRecoverySource(n.source);const i=e.target??(r.kind===`gateway`?void 0:{agentId:r.agentId,sessionKey:r.sessionKey});const a=e.context??getPluginRuntimeGatewayRequestScope()?.resolveGatewayContext?.();const o=captureCronJobSourceAuthority(e.job.id);if(!a||!o||!e.deliveryAttemptFence)throw new Error(`Scheduled operator continuation requires its live Gateway occurrence`);const s=e.deliveryAttemptFence;let c=false;const assertCurrent=()=>{e.signal?.throwIfAborted();o();s.assertCurrent();if(c)return;const t=a.cron.getJob(e.job.id);const l=t?.scheduledToolPolicy;if(a.resolveGatewayContext?.()!==a||!t||t.operatorSource?.executionRevision!==resolveCronOperatorExecutionRevision(t)||tryResolveCronJobEffectiveAgentId(t,a.cron.getDefaultAgentId())!==r.agentId||i&&i.agentId!==r.agentId||r.kind!==`gateway`&&(t.payload.kind!==`agentTurn`&&!r.snapshot.openclawIdPrincipal||!(l?.mode===`account`&&l.ownerSessionKey===r.sessionKey||r.snapshot.openclawIdPrincipal&&t.schedule.kind===`stream`&&t.payload.kind===`command`)||t.owner?.sessionKey!==r.sessionKey||!isCronOperatorSourceParent(t,r.sessionKey))||!isDeepStrictEqual(t.operatorSource,n))throw new Error(`Scheduled operator continuation definition or conversation changed; recreate it from an authenticated turn`)};assertCurrent();t=await restoreGatewayOperatorSource({snapshot:r.snapshot,startGrant:`operator.cron`,target:i,context:a,assertCurrent});assertCurrent();c=true;return t}catch(n){t?.release();if(n instanceof GatewayDispatchUnavailableError&&n.retryable)throw n;if(e.signal?.aborted||isAbortError(n))throw n;throw new CronOperatorSourceError(n)}}async function restoreCronScriptOperatorSource(e,t,n,r){const i=await n.wait(restoreCronOperatorSource({job:e,context:t,signal:n.signal,deliveryAttemptFence:r}));if(!i)return;const a=i.authority.signal;const onRevoked=()=>n.cleanup();const release=()=>{a?.removeEventListener(`abort`,onRevoked);i.release()};try{a?.addEventListener(`abort`,onRevoked,{once:true});i.authority.assertCurrent();return{...i,release,[Symbol.dispose]:release}}catch(e){release();throw e}}function retainCronControlSource(e){const t=e.job.operatorSource;const n=e.capture.operatorSource;if(!t||!n||t.source.kind!==`gateway`&&!t.source.snapshot.openclawIdPrincipal)return e.capture;const r=!(e.patch.schedule!==void 0&&(e.job.schedule.kind===`stream`||e.patch.schedule.kind===`stream`))&&Object.keys(e.patch).every(e=>[`schedule`,`enabled`,`description`,`displayName`].includes(e));const i=t.source.snapshot.profileId===n.snapshot.profileId;const a=isSameOpenClawIdTunnelPrincipal(t.source.snapshot.openclawIdPrincipal,n.snapshot.openclawIdPrincipal);const o=isDeepStrictEqual(sourceRolePolicy(t.source.snapshot.rolePolicy),sourceRolePolicy(n.snapshot.rolePolicy));if(i&&!r)return e.capture;if(i&&a&&o){const r=intersectOperatorScopes(n.snapshot.scopes,t.source.snapshot.scopes);return{...e.capture,operatorSource:freezeJsonSnapshot({...n,snapshot:{...n.snapshot,scopes:r,...n.snapshot.sourceIngress!==`principal`?{automationAdmin:hasAutomationAdminDelegation(t.source.snapshot)&&hasAutomationAdminDelegation(n.snapshot)&&operatorScopeSatisfied(`operator.admin`,r)}:{},controlUiAdmin:t.source.snapshot.controlUiAdmin&&n.snapshot.controlUiAdmin&&operatorScopeSatisfied(`operator.admin`,r)}}),assertOperatorSourceCurrent:()=>{e.capture.assertOperatorSourceCurrent?.();if(!isDeepStrictEqual(e.context.cron.getJob(e.job.id)?.operatorSource,t))throw new Error(`The automation's standing scope changed during the control edit.`)}}}if(!r)throw new TypeError(`Changing this automation's execution requires its original requester or explicit ownership reauthorization.`);return{...e.capture,operatorSource:t.source,assertOperatorSourceCurrent:()=>{e.capture.assertOperatorSourceCurrent?.();if(!isDeepStrictEqual(e.context.cron.getJob(e.job.id)?.operatorSource,t))throw new Error(`The automation's original source changed during the control edit.`)}}}export{restoreCronScriptOperatorSource as n,retainCronControlSource as r,restoreCronOperatorSource as t};