UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

170 lines (169 loc) • 7.06 kB
import { l as normalizeOptionalString } from "./string-coerce-CCIiB4H7.mjs"; import { n as normalizeAgentId } from "./agent-id-CQjtp2ft.mjs"; import { j as parseAgentSessionKey } from "./session-key-CqAyZeVk.mjs"; import { i as listAgentIds } from "./agent-roster-t1ooUdXp.mjs"; import { l as resolveSessionStorePathCore } from "./paths-DvQ5AJvD.mjs"; import { A as tryResolveLegacyCompatibilityAgentId, R as resolvePersistedSessionStoreOwner, V as isPerAgentSessionStoreConfig } from "./agent-scope-config-DmXgFGKP.mjs"; import { n as listConfiguredSessionStoreAgentIds } from "./targets-configured-agents-CQxugAIz.mjs"; import { n as resolveSessionStoreCompatibilityAgentId } from "./legacy.default-agent-owner-DpmHgK0A.mjs"; import { n as authorizeOperatorScopesForMethod } from "./method-scopes-Dpyb0Ck-.mjs"; import { f as resolveGatewayOperatorRoleActor, n as authorizeCurrentOperatorRoleScopes, o as onOperatorRolePolicyChanged, x as readGatewayAccessRevision } from "./operator-role-policy-DhcqVpmG.mjs"; import { o as resolveSessionRoutingContract } from "./main-session-BNL3-mRV.mjs"; import { a as resolveSessionStoreIdentity } from "./session-store-key-CEwU3noi.mjs"; import { t as assertionSucceeds } from "./assertion-BVvbcE3a.mjs"; import { r as isGatewayAuthPolicyCurrent, t as captureGatewayAuthPolicy } from "./auth-policy-CR6nKvhI.mjs"; import { a as readGatewayRequestMutationAuthority } from "./session-mutation-guards-Bjnk_O0z.mjs"; import { i as isHostPluginApprovalCall, n as canResolveOperatorApproval, r as canReviewOperatorApproval } from "./operator-approval-authorization-CczTmdXh.mjs"; import { isDeepStrictEqual } from "node:util"; //#region src/gateway/server-methods/approval-request-authority.ts /** Pure policy/locator facts used by approval visibility; no row or registry discovery. */ function captureApprovalConfigPolicy(e) { const t = listAgentIds(e).toSorted(); const n = listConfiguredSessionStoreAgentIds(e).toSorted(); const r = resolveSessionStoreCompatibilityAgentId(e); const i = [.../* @__PURE__ */ new Set([...n, r])].toSorted(); return { routing: resolveSessionRoutingContract(e), storeOwner: resolvePersistedSessionStoreOwner(e), compatibilityAgent: r, legacyAgent: tryResolveLegacyCompatibilityAgentId(e), agents: t, configuredStores: n, perAgentStore: isPerAgentSessionStoreConfig(e.session?.store), stores: i.map((t) => ({ agentId: t, path: resolveSessionStorePathCore(e.session?.store, { agentId: t }) })) }; } /** * Commit facts for one resolved approval source: its route and the stores it resolves through. * Shared fixed stores select an owner among co-tenants, and unconfigured owners discover * through every configured store root, so those keep the matching inventory. */ function captureApprovalSourcePolicy(e, t) { const { agents: n, configuredStores: r, stores: i, ...a } = captureApprovalConfigPolicy(e); const { sessionKey: o, agentId: s } = t; if (!o) return a; let c; try { c = resolveSessionStoreIdentity({ cfg: e, sessionKey: o, ...s ? { agentId: s } : {} }); } catch { c = null; } const l = new Set([ parseAgentSessionKey(o)?.agentId, s, c?.agentId ].flatMap((e) => e ? [normalizeAgentId(e)] : [])); return { ...a, identity: c, owners: [...l].toSorted().map((t) => { const a = resolveSessionStorePathCore(e.session?.store, { agentId: t }); const o = r.includes(t); return { agentId: t, listed: n.includes(t), configured: o, path: a, stores: o ? i.filter((e) => e.path === a) : i }; }) }; } /** Retain the original invocation; copying options loses its request-owner binding. */ function createApprovalRequestAuthority(e) { const t = readGatewayRequestMutationAuthority(e); const { client: n } = e; const r = e.req.method; const i = n?.authenticatedUserProfile?.profileId; const a = n?.authenticatedUserId; const o = n?.connect.role; const s = n?.connect.device?.id; const c = n?.internal?.approvalRuntime; const l = n?.internal?.agentRuntimeIdentity; const u = resolveGatewayOperatorRoleActor(n); const d = u?.kind; const f = u?.kind === "operator" ? u.profileId : void 0; const p = e.context.getRuntimeConfig; const m = e.context.getCommittedRuntimeConfig; const h = e.context.resolveGatewayContext; const g = h?.() ?? e.context; const _ = m ?? p; const v = _(); const y = n?.authPolicy ?? captureGatewayAuthPolicy(v, null); let captureConfigPolicy = captureApprovalConfigPolicy; let b = captureConfigPolicy(v); let x; const S = readGatewayAccessRevision(); let C = false; let w = false; const observeConfig = () => { try { const e = _(); C = !isGatewayAuthPolicyCurrent(y, e) || Boolean(authorizeCurrentOperatorRoleScopes(n, e)) || !isDeepStrictEqual(b, captureConfigPolicy(e)); } catch { C = true; } }; const T = onOperatorRolePolicyChanged((e) => { if (e.kind !== "config" || e.context !== g || C) return; observeConfig(); }); const bindSource = (e) => { const t = { sessionKey: normalizeOptionalString(e.sessionKey) ?? null, agentId: normalizeOptionalString(e.agentId) ?? null }; if (x) { C ||= !isDeepStrictEqual(x, t); return; } if (C) return; observeConfig(); if (C) return; x = t; captureConfigPolicy = (e) => captureApprovalSourcePolicy(e, t); try { b = captureConfigPolicy(_()); } catch { C = true; } }; const assertPolicyCurrent = () => { const u = resolveGatewayOperatorRoleActor(n); const _ = r.startsWith("exec.approval.") || r.startsWith("plugin.approval.") ? t.family === "native-compatibility" || authorizeOperatorScopesForMethod(r, n?.connect.scopes ?? []).allowed || isHostPluginApprovalCall(r, n) : r === "approval.resolve" ? canResolveOperatorApproval(n) : r === "approval.history" ? authorizeOperatorScopesForMethod(r, n?.connect.scopes ?? []).allowed : canReviewOperatorApproval(n); if (w || !_ || n?.invalidated || n?.connect.role !== o || n?.connect.device?.id !== s || n?.internal?.approvalRuntime !== c || n?.internal?.agentRuntimeIdentity !== l || u?.kind !== d || (u?.kind === "operator" ? u.profileId : void 0) !== f || n?.authenticatedUserProfile?.profileId !== i || n?.authenticatedUserId !== a || e.context.getRuntimeConfig !== p || e.context.getCommittedRuntimeConfig !== m || e.context.resolveGatewayContext !== h || (h?.() ?? e.context) !== g || C || readGatewayAccessRevision() !== S) throw new Error("Approval requester authority changed"); }; const assertCurrent = () => { t.assertCurrent(); t.expectedProfileBinding?.assertCurrent(); assertPolicyCurrent(); }; const E = { family: t.family, assertCurrent: t.family === "native-compatibility" ? assertCurrent : () => { t.assertWorkerCurrent(); t.expectedProfileBinding?.assertCurrent(); assertPolicyCurrent(); } }; return { guard: E, bindSource, assertCurrent, assertCommitCurrent: E.assertCurrent, isCurrent: () => assertionSucceeds(assertCurrent), [Symbol.dispose]() { w = true; T(); } }; } //#endregion export { createApprovalRequestAuthority as t };