openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
3 lines • 4.56 kB
JavaScript
import{n as operatorScopeSatisfied}from"./operator-scope-compat-TmgAKmD2.mjs";import{m as readResidentUserProfileId}from"./user-profile-list-DJsakwXy.mjs";import{r as authorizeGatewaySessionCreation}from"./operator-role-policy-g-dxw0h4.mjs";import{c as prepareUserProfileSelectionAuthority}from"./user-channel-identity-operations-BbYvG0PX.mjs";import{r as resolveAgentMainSessionKey}from"./main-session-C6FGqx0x.mjs";import{c as resolveCronSessionTargetSessionKey}from"./session-target-BE2kgjYz.mjs";import{i as authorizePreparedSessionMutation}from"./session-sharing-policy-CQ0Zqotl.mjs";import{t as prepareSessionMutationFacts}from"./session-sharing-preparation-Bx3oY7gx.mjs";import{c as prepareSessionSharingProfiles,o as prepareProjectedSessionSharing}from"./session-sharing-read-Cm-EAfc6.mjs";import{r as resolveCronJobEffectiveAgentId}from"./agent-id-DdcCg4fI.mjs";import{isDeepStrictEqual}from"node:util";
//!src/gateway/server-methods/cron-human-owner.ts
function isHumanCronMutation(e){return e===`cron.update`||e===`cron.remove`||e===`cron.run`||e===`cron.scratch.set`}function isDirectCronOwnerClient(e){return!e?.internal?.agentRuntimeIdentity&&Boolean(e?.authenticatedUserProfile)&&operatorScopeSatisfied(`operator.cron`,e?.connect?.scopes??[])}function isHumanCronJobOwner(e,t){if(e?.internal?.agentRuntimeIdentity||!t.ownerProfileId)return false;const n=e?.authenticatedUserProfile?.profileId;const r=readResidentUserProfileId(t.ownerProfileId);return Boolean(r&&n&&r===readResidentUserProfileId(n))}function canHumanManageCronJob(e,t){if(!e?.connect||e.internal?.agentRuntimeIdentity||e.connect.scopes?.includes(`operator.admin`))return true;return isDirectCronOwnerClient(e)&&isHumanCronJobOwner(e,t)}function assertHumanCronJobAccess(e,t){if(!canHumanManageCronJob(e,t))throw new TypeError(`Only the automation's human owner or an administrator can change or run it.`)}async function prepareCronOwnerChange(e,t,n){if(n.ownerProfileId===void 0)return;if(e?.internal?.agentRuntimeIdentity)throw new TypeError(`Change the human owner from the Automations page.`);assertHumanCronJobAccess(e,t);const r=await prepareUserProfileSelectionAuthority(n.ownerProfileId);if(!r)throw new TypeError(`Select an existing human owner.`);n.ownerProfileId=r.profileId;return()=>{if(!r.isCurrent())throw new TypeError(`The selected human owner changed. Select the owner again.`)}}function assertHumanCronExecutionAccess(e,t,n){if(!isDirectCronOwnerClient(e)||e?.connect?.scopes?.includes(`operator.admin`))return;if(operatorScopeSatisfied(`operator.write`,e?.connect?.scopes??[]))return;const r=t.payload.kind===`command`||t.schedule.kind===`on-exit`||t.schedule.kind===`stream`||n?.payload?.kind===`command`||n?.schedule?.kind===`on-exit`||n?.schedule?.kind===`stream`;const i=new Map(Object.entries(t));const a=n&&Object.entries(n).every(([e,t])=>isDeepStrictEqual(t,i.get(e))||e===`name`||e===`ownerProfileId`||e===`displayName`||e===`description`||e===`enabled`&&n.enabled===false);if(r&&!a)throw new TypeError(`Running or changing host-command automations requires operator.write. You can still pause, transfer, or delete your automation.`)}async function prepareHumanCronTargetAccess(e,t,n,r,i){if(!isDirectCronOwnerClient(e)||e?.connect?.scopes?.includes(`operator.admin`))return;if(n.agentId===r.agentId&&n.sessionKey===r.sessionKey&&n.sessionTarget===r.sessionTarget)return;const a=t();const o=resolveCronJobEffectiveAgentId(r,i);const s=r.sessionTarget===`main`?resolveAgentMainSessionKey({cfg:a,agentId:o}):resolveCronSessionTargetSessionKey(r.sessionTarget)??r.sessionKey;const c=await prepareSessionSharingProfiles(e);const l=s?await prepareSessionMutationFacts({cfg:a,agentId:o,sessionKey:s,allowMissing:true}):void 0;const assertCurrent=()=>{const n=t();const r=authorizeGatewaySessionCreation({cfg:n,client:e,agentId:o});if(r)throw new TypeError(r.message);if(!l||!s)return;const i=l.readCurrent(n);const a=prepareProjectedSessionSharing({cfg:n,client:e,profiles:c,isMember:(e,t)=>i.membership.has(t)});const u=authorizePreparedSessionMutation({cfg:n,client:e,agentId:o,sessionKey:s},i,{policy:a.policy,aliases:c.readCurrent().profile?.aliases??/* @__PURE__ */ new Set});if(u)throw new TypeError(u.message)};try{assertCurrent();return{assertCurrent,release:()=>l?.release()}}catch(e){l?.release();throw e}}export{isHumanCronJobOwner as a,prepareHumanCronTargetAccess as c,isDirectCronOwnerClient as i,assertHumanCronJobAccess as n,isHumanCronMutation as o,canHumanManageCronJob as r,prepareCronOwnerChange as s,assertHumanCronExecutionAccess as t};