UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

3 lines • 9.78 kB
import{n as materializeErrorStack}from"./error-graph-internal-Bt2pFJBM.mjs";import{a as getRuntimeConfigSnapshot}from"./runtime-snapshot-Bg5K4JNu.mjs";import{O as validateAgentRunDelegatedAuthority}from"./agent-run-registry-ONwZSqWq.mjs";import{t as DEFAULT_EXEC_APPROVAL_TIMEOUT_MS}from"./exec-approvals-core-BZ6-zeZE.mjs";import{r as normalizeCronScheduledToolCallerOrigin}from"./scheduled-tool-policy-uo_Vc05h.mjs";import{t as cloneCronRuntimeAuthority}from"./runtime-authority-Dcu45bo5.mjs";import{t as normalizeCronAuthenticatedChannelRequester}from"./tools-allow-provenance-CddZhkeE.mjs";import{randomBytes}from"node:crypto";import{AsyncLocalStorage}from"node:async_hooks"; //!src/gateway/cron-creator-authority-grant.ts function isCronModelManagementAllowed(e=getRuntimeConfigSnapshot()??void 0){return e?.tools?.gatewayPolicy?.automations?.scope!==`caller`}const CRON_MANAGEMENT_METHODS=[`cron.list`,`cron.get`,`cron.update`,`cron.run`,`cron.remove`,`cron.bind`];const activeManagement=new AsyncLocalStorage;const grantsByToken=/* @__PURE__ */ new Map;const channelRequestersByScope=/* @__PURE__ */ new WeakMap;function expiredAuthorityError(){return Object.assign(/* @__PURE__ */ new TypeError(`Configured MCP cron authority is no longer active for this run. Retry the automation mutation from a fresh authenticated creator turn.`),{name:`CronCreatorAuthorityExpiredError`,status:403})}function createCronCreatorAuthorityRunScope(e,t={kind:`unknown`},n,r,i,a,o){const s=new AbortController;const c=normalizeCronAuthenticatedChannelRequester(i);const l={runId:e,callerOrigin:normalizeCronScheduledToolCallerOrigin(t),signal:s.signal,grantTokens:/* @__PURE__ */ new Set,...n?{managementEntitlement:n}:{},...a?{requesterOwner:a}:{},...o?{callerScopedCreation:o}:{},get controlUiAdmin(){return n?.source===`control-ui-admin`?true:void 0},...r?{isCurrent:r}:{},active:true,abort:()=>{const e=expiredAuthorityError();materializeErrorStack(e);s.abort(e)}};if(c)channelRequestersByScope.set(l,Object.freeze(c));return l}function hasCronChannelRequester(e){return channelRequestersByScope.has(e)}function hasCronAuthenticatedRequester(e){return e.callerOrigin.kind===`local`||e.callerOrigin.kind===`external`&&e.managementEntitlement?.source===`channel-owner`||hasCronChannelRequester(e)||e.callerScopedCreation===true}function mintCronCreatorAuthorityGrant(e,t,n,r,i=`runtime`,a,o){if(!e.active||e.signal.aborted||t?.aborted||e.isCurrent?.()===false||a?.()===false||e.managementEntitlement?.source===`channel-owner`&&!e.managementEntitlement.isCurrent())throw r?expiredManagementError():expiredAuthorityError();if(!r&&e.managementEntitlement&&e.callerOrigin.kind===`unknown`&&!(i===`requester`&&e.callerScopedCreation))throw new TypeError(`Automation creation is not granted to this turn. Use the Automations page to create an automation.`);if(i===`requester`&&(!hasCronAuthenticatedRequester(e)||n||r))throw new TypeError(`requester-only cron authority requires authenticated creator facts`);if(r&&(!e.managementEntitlement||!CRON_MANAGEMENT_METHODS.some(e=>e===r.method)||r.authority.operationalRunInstance.runId!==e.runId||!validateAgentRunDelegatedAuthority(r.authority)))throw expiredManagementError();const s=randomBytes(32).toString(`base64url`);const c=n?cloneCronRuntimeAuthority(n):void 0;if(n&&!c)throw new TypeError(`cron creator runtime authority is invalid`);const l={scope:e,capturesRuntimeAuthority:i===`runtime`,...o?{creatorProfileId:o}:{},...a?{isCurrent:a}:{},operationSignal:t,...c?{runtimeAuthority:c}:{},...r?{management:{...r,expiresAtMs:Date.now()+6e4}}:{}};if(t)l.onOperationAbort=()=>revokeCronCreatorAuthorityGrant(s);grantsByToken.set(s,l);e.grantTokens.add(s);if(t&&l.onOperationAbort)t.addEventListener(`abort`,l.onOperationAbort,{once:true});return Object.freeze({runId:e.runId,token:s})}function resolveCronCreatorAuthorityGrantProvenance(e,t){const n=grantsByToken.get(e.token);const r=n?.scope;if(!n||!r||n.management||!r.active||r.signal.aborted||n.operationSignal?.aborted||n.isCurrent?.()===false||r.isCurrent?.()===false||r.managementEntitlement?.source===`channel-owner`&&!r.managementEntitlement.isCurrent()||r.runId!==e.runId||r.runId!==t)return;const i=channelRequestersByScope.get(r);return{capturesRuntimeAuthority:n.capturesRuntimeAuthority,...r.callerScopedCreation?{callerScopedCreation:true}:{},...n.creatorProfileId?{creatorProfileId:n.creatorProfileId}:{},...r.callerOrigin.kind===`local`||r.callerOrigin.kind===`external`&&r.managementEntitlement?.source===`channel-owner`?{callerOrigin:{...r.callerOrigin}}:{},...i?{channelRequester:{...i}}:{}}}function hasCronCreatorGrantProvenance(e,t){if(!e.cronCreatorAuthorityGrant||e.cronToolsAllowCapture===`final-executable-surface`)return true;const n=resolveCronCreatorAuthorityGrantProvenance(e.cronCreatorAuthorityGrant,t);return Boolean(n?.callerOrigin||n?.channelRequester||n?.callerScopedCreation)}function revokeCronCreatorAuthorityGrant(e){const t=grantsByToken.get(e);if(!t)return;grantsByToken.delete(e);t.scope.grantTokens.delete(e);if(t.operationSignal&&t.onOperationAbort)t.operationSignal.removeEventListener(`abort`,t.onOperationAbort)}function revokeCronCreatorAuthorityRunScope(e){if(!e.active)return;e.active=false;e.abort();channelRequestersByScope.delete(e);for(const t of e.grantTokens)revokeCronCreatorAuthorityGrant(t)}function assertCronGrantCurrent(e,t){const{scope:n,management:r}=e;if(!n.active||n.signal.aborted||e.operationSignal?.aborted||e.isCurrent?.()===false||n.isCurrent?.()===false||n.runId!==t||n.managementEntitlement?.source===`channel-owner`&&!n.managementEntitlement.isCurrent()||r&&(Date.now()>=r.expiresAtMs||!validateAgentRunDelegatedAuthority(r.authority)))throw r?expiredManagementError():expiredAuthorityError()}function attachCronStreamExecPolicy(e,t){const n=grantsByToken.get(e.token);if(!n)throw expiredAuthorityError();assertCronGrantCurrent(n,e.runId);if(n.management?.method===`cron.update`&&!n.execPolicy)n.management.expiresAtMs=Date.now()+DEFAULT_EXEC_APPROVAL_TIMEOUT_MS+1e4;n.execPolicy=t}function resolveCronCreatorStreamExecPolicy(e){const t=grantsByToken.get(e.token);if(!t||t.management)throw expiredAuthorityError();assertCronGrantCurrent(t,e.runId);return bindCronStreamExecPolicyCurrentness(t.execPolicy,()=>assertCronGrantCurrent(t,e.runId))}function bindCronStreamExecPolicyCurrentness(e,t){if(!e)return;return e.kind===`resolved`?{kind:`resolved`,sandboxContext:e.sandboxContext,onApprovalCommitted:e.onApprovalCommitted,assertCurrentSource:()=>{t();e.assertCurrentSource?.()},assertCurrent:n=>{t();e.assertCurrent(n)}}:{kind:`needs-approvals`,sandboxContext:e.sandboxContext,onApprovalCommitted:e.onApprovalCommitted,assertCurrentSource:()=>{t();e.assertCurrentSource?.()},assertCurrent:(n,r)=>{t();e.assertCurrent(n,r)}}}function consumeCronCreatorAuthorityGrant(e){const t=e.runId.trim();const n=e.token.trim();const r=n?grantsByToken.get(n):void 0;if(!r||r.management)throw expiredAuthorityError();const assertCurrent=()=>assertCronGrantCurrent(r,t);assertCurrent();revokeCronCreatorAuthorityGrant(n);return{authority:r.runtimeAuthority?cloneCronRuntimeAuthority(r.runtimeAuthority):void 0,assertCurrent}}function expiredManagementError(){return/* @__PURE__ */ new TypeError(`Automation admin grant is missing, expired, or already used. Retry from a fresh authenticated configured channel owner or Control UI administrator turn, or use the Automations page.`)}function findCronManagementGrant(e,t,n){const r=grantsByToken.get(e.token);const i=r?.management;const a=t.delegatedAuthority;if(!r||!i||i.method!==n||e.runId!==r.scope.runId||i.authority.operationalRunInstance.instanceId!==t.operationalRunInstance.instanceId||i.authority.operationalRunInstance.runId!==t.operationalRunInstance.runId||i.authority.lifecycleGeneration!==a.lifecycleGeneration||i.authority.claimId!==a.claimId)return;return{entry:r,management:i}}function holdsCronManagementGrant(e,t,n){return findCronManagementGrant(e,t,n)!==void 0}async function withCronManagementGrant(e,t,n,r){const i=findCronManagementGrant(e,t,n);if(!i)throw expiredManagementError();const{entry:a,management:o}=i;revokeCronCreatorAuthorityGrant(e.token);const assertActive=()=>{assertCronGrantCurrent(a,o.authority.operationalRunInstance.runId)};assertActive();return await activeManagement.run({identity:t,assertActive,...a.scope.callerOrigin.kind!==`unknown`?{callerOrigin:{...a.scope.callerOrigin}}:{},channelRequester:channelRequestersByScope.get(a.scope),execPolicy:a.execPolicy},r)}function getCronManagementAuthority(e){const t=activeManagement.getStore();return t?.identity===e?t.assertActive:void 0}function getCronManagementStreamExecPolicy(e){const t=activeManagement.getStore();return t?.identity===e?bindCronStreamExecPolicyCurrentness(t.execPolicy,t.assertActive):void 0}function getCronManagementChannelRequester(e){const t=activeManagement.getStore();if(t?.identity!==e)return;t.assertActive();return t.channelRequester?{...t.channelRequester}:void 0}function getCronManagementCallerOrigin(e){const t=activeManagement.getStore();if(t?.identity!==e)return;t.assertActive();return t.callerOrigin?{...t.callerOrigin}:void 0}export{withCronManagementGrant as _,getCronManagementAuthority as a,getCronManagementStreamExecPolicy as c,holdsCronManagementGrant as d,isCronModelManagementAllowed as f,revokeCronCreatorAuthorityRunScope as g,resolveCronCreatorStreamExecPolicy as h,createCronCreatorAuthorityRunScope as i,hasCronAuthenticatedRequester as l,resolveCronCreatorAuthorityGrantProvenance as m,attachCronStreamExecPolicy as n,getCronManagementCallerOrigin as o,mintCronCreatorAuthorityGrant as p,consumeCronCreatorAuthorityGrant as r,getCronManagementChannelRequester as s,CRON_MANAGEMENT_METHODS as t,hasCronCreatorGrantProvenance as u};