UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

111 lines (110 loc) 6.07 kB
import { T as validateAgentRunDelegatedAuthority } from "./agent-run-registry-CKYKdfNd.js"; import { r as normalizeCronScheduledToolCallerOrigin } from "./scheduled-tool-policy-WcH3cpLk.js"; import { t as cloneCronRuntimeAuthority } from "./runtime-authority-BpR-jalg.js"; import { AsyncLocalStorage } from "node:async_hooks"; import { randomBytes } from "node:crypto"; //#region src/gateway/cron-creator-authority-grant.ts const CRON_MANAGEMENT_METHODS = [ "cron.list", "cron.get", "cron.update", "cron.run", "cron.remove" ]; const activeManagement = new AsyncLocalStorage(); const grantsByToken = /* @__PURE__ */ new Map(); function expiredAuthorityError() { return Object.assign(/* @__PURE__ */ new TypeError("Configured MCP cron authority is no longer active for this run. Retry the automation mutation from the active local operator turn."), { name: "CronCreatorAuthorityExpiredError", status: 403 }); } function createCronCreatorAuthorityRunScope(runId, callerOrigin = { kind: "unknown" }, controlUiAdmin) { const abortController = new AbortController(); return { runId, callerOrigin: normalizeCronScheduledToolCallerOrigin(callerOrigin), signal: abortController.signal, grantTokens: /* @__PURE__ */ new Set(), ...controlUiAdmin ? { controlUiAdmin } : {}, active: true, abort: () => abortController.abort(expiredAuthorityError()) }; } function mintCronCreatorAuthorityGrant(scope, operationSignal, runtimeAuthority, management) { if (!scope.active || scope.signal.aborted || operationSignal?.aborted) throw management ? expiredManagementError() : expiredAuthorityError(); if (!management && scope.controlUiAdmin && scope.callerOrigin.kind === "unknown") throw new TypeError("Automation creation is not granted to this turn. Use the Automations page to create an automation."); if (management && (!scope.controlUiAdmin || !CRON_MANAGEMENT_METHODS.some((method) => method === management.method) || management.authority.operationalRunInstance.runId !== scope.runId || !validateAgentRunDelegatedAuthority(management.authority))) throw expiredManagementError(); const token = randomBytes(32).toString("base64url"); const normalizedRuntimeAuthority = runtimeAuthority ? cloneCronRuntimeAuthority(runtimeAuthority) : void 0; if (runtimeAuthority && !normalizedRuntimeAuthority) throw new TypeError("cron creator runtime authority is invalid"); const entry = { scope, operationSignal, ...normalizedRuntimeAuthority ? { runtimeAuthority: normalizedRuntimeAuthority } : {}, ...management ? { management: { ...management, expiresAtMs: Date.now() + 6e4 } } : {} }; if (operationSignal) entry.onOperationAbort = () => revokeCronCreatorAuthorityGrant(token); grantsByToken.set(token, entry); scope.grantTokens.add(token); if (operationSignal && entry.onOperationAbort) operationSignal.addEventListener("abort", entry.onOperationAbort, { once: true }); return Object.freeze({ runId: scope.runId, token }); } function revokeCronCreatorAuthorityGrant(token) { const entry = grantsByToken.get(token); if (!entry) return; grantsByToken.delete(token); entry.scope.grantTokens.delete(token); if (entry.operationSignal && entry.onOperationAbort) entry.operationSignal.removeEventListener("abort", entry.onOperationAbort); } function revokeCronCreatorAuthorityRunScope(scope) { if (!scope.active) return; scope.active = false; scope.abort(); for (const token of scope.grantTokens) revokeCronCreatorAuthorityGrant(token); } /** Consumes one live exact-run grant synchronously at the cron commit boundary. */ function consumeCronCreatorAuthorityGrant(grant) { const runId = grant.runId.trim(); const token = grant.token.trim(); const entry = token ? grantsByToken.get(token) : void 0; if (!entry) throw expiredAuthorityError(); const scope = entry.scope; if (entry.management || !scope.active || scope.signal.aborted || entry.operationSignal?.aborted || scope.runId !== runId) { if (!scope.active || scope.signal.aborted || entry.operationSignal?.aborted) revokeCronCreatorAuthorityGrant(token); throw expiredAuthorityError(); } revokeCronCreatorAuthorityGrant(token); return entry.runtimeAuthority ? cloneCronRuntimeAuthority(entry.runtimeAuthority) : void 0; } function expiredManagementError() { return /* @__PURE__ */ new TypeError("Automation admin grant is missing, expired, or already used. Retry from a fresh authenticated Control UI administrator turn, or use the Automations page."); } /** Redeem once, retaining the exact operational owner through every await and commit. */ async function withCronManagementGrant(grant, identity, method, run) { const entry = grantsByToken.get(grant.token); const management = entry?.management; const authority = identity.delegatedAuthority; if (!entry || !management || management.method !== method || grant.runId !== entry.scope.runId || management.authority.operationalRunInstance.instanceId !== identity.operationalRunInstance.instanceId || management.authority.operationalRunInstance.runId !== identity.operationalRunInstance.runId || management.authority.lifecycleGeneration !== authority.lifecycleGeneration || management.authority.claimId !== authority.claimId) throw expiredManagementError(); revokeCronCreatorAuthorityGrant(grant.token); const assertActive = () => { if (!entry.scope.active || entry.scope.signal.aborted || entry.operationSignal?.aborted || Date.now() >= management.expiresAtMs || !validateAgentRunDelegatedAuthority(management.authority)) throw expiredManagementError(); }; assertActive(); return await activeManagement.run({ identity, assertActive }, run); } function getCronManagementAuthority(identity) { const management = activeManagement.getStore(); return management?.identity === identity ? management.assertActive : void 0; } //#endregion export { mintCronCreatorAuthorityGrant as a, getCronManagementAuthority as i, consumeCronCreatorAuthorityGrant as n, revokeCronCreatorAuthorityRunScope as o, createCronCreatorAuthorityRunScope as r, withCronManagementGrant as s, CRON_MANAGEMENT_METHODS as t };