UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

210 lines (209 loc) • 11.3 kB
import { l as normalizeOptionalString } from "./string-coerce-CCIiB4H7.mjs"; import { t as isPromiseLike } from "./promise-like-aMoWEh5w.mjs"; import { t as AsyncContext } from "./async-context-BYDAvG9A.mjs"; import { O as validateAgentRunDelegatedAuthority, l as getAgentRunContext } from "./agent-run-registry-D7vkrqFT.mjs"; import { i as requesterCreatorProfileId } from "./requester-origin-DG-fk-U0.mjs"; import { o as getGatewayToolCallerIdentity } from "./gateway-caller-context-BNidAW8G.mjs"; import { f as isCronModelManagementAllowed, g as revokeCronCreatorAuthorityRunScope, i as createCronCreatorAuthorityRunScope, l as hasCronChannelRequester, p as mintCronCreatorAuthorityGrant, t as CRON_MANAGEMENT_METHODS } from "./cron-creator-authority-grant-pGPjtQX_.mjs"; //#region src/agents/cron-creator-authority-context.ts function createCronCreatorAuthorityCapability(e, t = { kind: "unknown" }, n, r, i, a, o) { const s = e.trim(); return s ? createCronCreatorAuthorityRunScope(s, t, n, r, i, a, o) : void 0; } const activeCronCreatorAuthority = new AsyncContext(Symbol("cronCreatorAuthority")); const activeCronCreatorAuthorityResolver = new AsyncContext(Symbol("cronCreatorAuthorityResolver")); /** Retain the Cron-only fence when tools materialize outside their creator scope. */ function bindActiveCronAuthorityCurrentness(e) { const t = activeCronCreatorAuthority.getStore(); return t?.active && t.runId === e?.trim() ? t.isCurrent : void 0; } /** Retain only the live Cron scope; late CLI admission still supplies caller authority. */ function bindCronManagementToolConstruction(e) { const t = activeCronCreatorAuthority.getStore(); if (!t?.managementEntitlement || t.runId !== e?.trim()) return; return (e) => { if (!t.active || t.signal.aborted || t.isCurrent?.() === false || t.managementEntitlement?.source === "channel-owner" && !t.managementEntitlement.isCurrent()) return activeCronCreatorAuthority.exit(e); return activeCronCreatorAuthority.run(t, e); }; } /** Retain the exact scope for callbacks invoked outside their creation context. */ function bindRequesterYieldCronAuthority(e) { const t = activeCronCreatorAuthority.getStore(); const n = getGatewayToolCallerIdentity()?.approvalAuthority; if (!t?.managementEntitlement || t.runId !== e || !n || n.operationalRunInstance.runId !== e) return; return (e) => { const r = getGatewayToolCallerIdentity()?.approvalAuthority; if (!t.active || t.signal.aborted || r?.operationalRunInstance.instanceId !== n.operationalRunInstance.instanceId || !validateAgentRunDelegatedAuthority(n)) return activeCronCreatorAuthority.exit(e); return activeCronCreatorAuthority.run(t, e); }; } /** Capture live management and separately admitted owner identity before the requester yields. */ function captureActiveCronManagementAuthority(e) { const t = activeCronCreatorAuthority.getStore(); const n = getGatewayToolCallerIdentity(); const r = n?.approvalAuthority; const i = getAgentRunContext(e.runId); const a = i?.sessionId; if (!t?.managementEntitlement || t.runId !== e.runId || n?.sessionKey !== e.sessionKey || n.agentId !== e.agentId || i?.sessionKey !== e.sessionKey || i.agentId !== e.agentId || !a || !r || r.operationalRunInstance.runId !== e.runId) return; const isActive = () => { try { return t.active && !t.signal.aborted && t.isCurrent?.() !== false && (t.managementEntitlement?.source !== "channel-owner" || t.managementEntitlement.isCurrent()) && !n.approvalSignals?.some((e) => e.aborted) && n.approvalAuthorityCheck?.() !== false && getAgentRunContext(e.runId) === i && validateAgentRunDelegatedAuthority(r); } catch { return false; } }; return isActive() ? { sessionId: a, lifecycleGeneration: r.lifecycleGeneration, managementEntitlement: t.managementEntitlement, requesterOwner: t.requesterOwner, isActive } : void 0; } /** Bind only the separately captured owner identity of an admitted requester continuation. */ function bindRequesterOwnerIdentity(e) { const t = activeCronCreatorAuthority.getStore(); const n = t?.requesterOwner; const r = getGatewayToolCallerIdentity(); const i = r?.approvalAuthority; const a = e.runId ? getAgentRunContext(e.runId) : void 0; if (!t || !n || t.callerOrigin.kind !== "unknown" || !t.isCurrent || !e.runId || t.runId !== e.runId || !e.sessionKey || !e.sessionId || !e.agentId || r?.sessionKey !== e.sessionKey || r.agentId !== e.agentId || a?.sessionKey !== e.sessionKey || a.sessionId !== e.sessionId || a.agentId !== e.agentId || !i || i.operationalRunInstance.runId !== e.runId) return; const o = e.runId; const isCurrent = () => { try { return t.active && !t.signal.aborted && t.isCurrent?.() === true && n.isCurrent() && !r.approvalSignals?.some((e) => e.aborted) && r.approvalAuthorityCheck?.() !== false && getAgentRunContext(o) === a && validateAgentRunDelegatedAuthority(i); } catch { return false; } }; return { isCurrent, senderId: n.senderId, channel: n.channel, accountId: n.accountId, assertCurrent: () => { if (!isCurrent()) throw new Error("Requester owner identity is no longer active for this continuation"); } }; } /** Bind at tool construction, never rediscover authority from model arguments or routes. */ function bindCronManagementGrant(e, t) { if (!isCronModelManagementAllowed(t)) return; const n = activeCronCreatorAuthority.getStore(); const r = getGatewayToolCallerIdentity()?.approvalAuthority; if (!n?.managementEntitlement || !n.active || n.signal.aborted || n.isCurrent?.() === false || n.managementEntitlement.source === "channel-owner" && !n.managementEntitlement.isCurrent() || n.runId !== e || !r || r.operationalRunInstance.runId !== e || !validateAgentRunDelegatedAuthority(r)) return; const i = n.callerOrigin.kind === "unknown" && !n.callerScopedCreation; return { managementOnly: i, mint: (e, t) => { if (!CRON_MANAGEMENT_METHODS.some((t) => t === e)) { if (i) throw new Error("This management-only turn can only list, get, update, run, remove, or bind automations. Use the Automations page for other actions."); return; } return mintCronCreatorAuthorityGrant(n, t, void 0, { method: e, authority: r }); } }; } /** Retains authenticated provenance before late CLI admission without execution authority. */ function captureCronRequesterGrantIssuer(e) { const t = requesterCreatorProfileId(getGatewayToolCallerIdentity()?.requesterOrigin); const n = activeCronCreatorAuthority.getStore(); if (!n || n.callerOrigin.kind !== "local" && !hasCronChannelRequester(n) && !n.callerScopedCreation || n.runId !== e) return; return (e, r, i) => { const isCurrent = () => e.operationalRunInstance.runId === n.runId && validateAgentRunDelegatedAuthority(e) && i?.() !== false; return mintCronCreatorAuthorityGrant(n, r, void 0, void 0, "requester", isCurrent, t); }; } /** Captures authenticated requester facts independently of full tool-surface materialization. */ function bindCronRequesterGrant(e) { const t = captureCronRequesterGrantIssuer(e); const n = getGatewayToolCallerIdentity()?.approvalAuthority; return t && n && n.operationalRunInstance.runId === e && validateAgentRunDelegatedAuthority(n) ? (e) => t(n, e) : void 0; } function isFreshChannelCronAuthorityTurn(e) { return Boolean(e.messageProvider) && Boolean(normalizeOptionalString(e.senderId)) && !e.scheduledAutomation && !e.isRoomEvent && e.inputProvenance === void 0 && e.spawnedBy === void 0 && e.suppressNextUserMessagePersistence !== true; } /** Owns one explicitly transported creator-authority capability until run settlement. */ function runWithCronCreatorAuthorityCapability(e, t, n) { const revoke = () => revokeCronCreatorAuthorityRunScope(e); n?.addEventListener("abort", revoke, { once: true }); if (n?.aborted) revoke(); try { const r = activeCronCreatorAuthority.run(e, t); if (isPromiseLike(r)) return Promise.resolve(r).finally(() => { n?.removeEventListener("abort", revoke); revoke(); }); n?.removeEventListener("abort", revoke); revoke(); return r; } catch (e) { n?.removeEventListener("abort", revoke); revoke(); throw e; } } /** Installs an explicitly transported capability only for synchronous tool construction. */ function runWithCronCreatorAuthorityCapabilityResolver(e) { const t = e.runId?.trim(); const n = e.capability; if (!t || n?.active !== true || n.runId !== t) return e.run(); return activeCronCreatorAuthority.run(n, () => activeCronCreatorAuthorityResolver.run({ runId: t, resolve: e.resolve }, e.run)); } /** Carries a bundled-Codex resolver through synchronous core tool construction. */ function runWithCronCreatorAuthorityResolver(e) { return activeCronCreatorAuthorityResolver.run({ runId: e.runId.trim(), resolve: e.resolve }, e.run); } /** Binds the resolver to the exact active run and revokes retained callbacks at settlement. */ function bindActiveCronCreatorAuthorityResolver(e) { const t = activeCronCreatorAuthority.getStore(); const n = activeCronCreatorAuthorityResolver.getStore(); const r = e?.trim(); if (!r || n?.runId !== r || t?.active !== true || t.runId !== r || t.managementEntitlement && t.callerOrigin.kind === "unknown") return; const i = n.resolve; const a = requesterCreatorProfileId(getGatewayToolCallerIdentity()?.requesterOrigin); return async (e) => { const n = e?.signal; t.signal.throwIfAborted(); n?.throwIfAborted(); if (t.isCurrent?.() === false) throw new Error("Automation caller authority is no longer active."); const r = n ? AbortSignal.any([t.signal, n]) : t.signal; const o = await i({ signal: r }); t.signal.throwIfAborted(); n?.throwIfAborted(); if (!t.active) t.signal.throwIfAborted(); return Object.freeze({ tools: o.tools, provenance: o.provenance, grant: mintCronCreatorAuthorityGrant(t, n, o.runtimeAuthority, void 0, "runtime", void 0, a), ...o.runtimeAuthority ? { holdsRuntimeAuthority: true } : {} }); }; } /** Retains the exact admitted owner turn only while its run scope remains live. */ function bindActiveOperatorTurnAuthority(e) { const t = activeCronCreatorAuthority.getStore(); const n = e?.trim(); if (!n || t?.active !== true || t.runId !== n || t.callerOrigin.kind === "unknown") return; return { source: t.callerOrigin.kind === "local" ? "local" : "channel-owner", assertActive: () => { t.signal.throwIfAborted(); if (!t.active || t.runId !== n || t.isCurrent?.() === false || t.managementEntitlement?.source === "channel-owner" && !t.managementEntitlement.isCurrent()) { t.signal.throwIfAborted(); throw new Error("operator turn authority is no longer active"); } } }; } //#endregion export { bindCronManagementToolConstruction as a, bindRequesterYieldCronAuthority as c, createCronCreatorAuthorityCapability as d, isFreshChannelCronAuthorityTurn as f, runWithCronCreatorAuthorityResolver as h, bindCronManagementGrant as i, captureActiveCronManagementAuthority as l, runWithCronCreatorAuthorityCapabilityResolver as m, bindActiveCronCreatorAuthorityResolver as n, bindCronRequesterGrant as o, runWithCronCreatorAuthorityCapability as p, bindActiveOperatorTurnAuthority as r, bindRequesterOwnerIdentity as s, bindActiveCronAuthorityCurrentness as t, captureCronRequesterGrantIssuer as u };