UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

5 lines • 9.55 kB
import{l as normalizeOptionalString}from"./string-coerce-CpTFcSwY.mjs";import{N as parseAgentSessionKey}from"./session-key-BPgKfU2m.mjs";import{r as normalizeOptionalAccountId}from"./account-id-3yqrEsRn.mjs";import{l as resolveSessionStorePathCore}from"./paths-CcxgiC3g.mjs";import{t as warnPluginSdkDeprecation}from"./sdk-deprecation-BDUCyBrU.mjs";import{n as normalizeMessageChannel}from"./message-channel-core-D_hPOadj.mjs";import"./message-channel-514k57tV.mjs";import{i as readSessionEntryReadOnlyInWorker}from"./session-entry-read-runtime-kAgwDw0B.mjs";import{r as sessionDeliveryOrigin,t as deliveryContextFromSession}from"./delivery-context.read-DWpauVKG.mjs";import{t as captureSessionActorStorageOwner}from"./session-actor-storage-binding-Bq_IHhu0.mjs";import"./session-accessor-CY8UYP4m.mjs";import{a as loadSessionEntryReadOnly}from"./session-accessor.sqlite-exact-read-CzR5cjvF.mjs";import{t as matchesApprovalRequestFilters}from"./approval-request-filters-Dj8ITkL-.mjs"; //!src/infra/approval-types.ts function resolveApprovalRequestKind(e){if(`proposalHash`in e.request&&`sessionId`in e.request)return`system-agent`;const t=`command`in e.request;if(t===(`title`in e.request&&`description`in e.request))throw new Error(`approval request payload does not identify exactly one owner`);return t?`exec`:`plugin`}function isExecApprovalRequest(e){return resolveApprovalRequestKind(e)===`exec`}function isPluginApprovalRequest(e){return resolveApprovalRequestKind(e)===`plugin`}function hasExecApprovalKind(e){return e.approvalKind===`exec`}function hasPluginApprovalKind(e){return e.approvalKind===`plugin`}function normalizeApprovalRequest(e){if(isExecApprovalRequest(e)){if(hasExecApprovalKind(e))return e;return{...e,approvalKind:`exec`}}if(isPluginApprovalRequest(e))return hasPluginApprovalKind(e)?e:{...e,approvalKind:`plugin`};return{...e,approvalKind:`system-agent`}} //!src/infra/approval-request-account-binding.ts function resolveApprovalForwardTargets(e){const t=resolveApprovalRequestKind(e.request)===`exec`?e.cfg.approvals?.exec:e.cfg.approvals?.plugin;const n=normalizeMessageChannel(e.channel);if(!t?.enabled||t.mode!==`targets`&&t.mode!==`both`)return[];if(!matchesApprovalRequestFilters({request:e.request.request,agentFilter:t.agentFilter,sessionFilter:t.sessionFilter}))return[];return(t.targets??[]).filter(e=>normalizeMessageChannel(e.channel)===n)}function resolveApprovalForwardAccountIds(e){return resolveApprovalForwardTargets(e).flatMap(t=>{const n=normalizeOptionalAccountId(t.accountId??e.defaultAccountId);return n?[n]:[]})}function approvalRequestSessionDelivery(e){return e?{sessionId:e.sessionId,updatedAt:e.updatedAt,delivery:e.delivery}:null}async function classifyApprovalRequestChannelRouteAsync(e){if(!normalizeMessageChannel(e.channel))return`unbound`;if(await resolveApprovalRequestChannelAccountIdAsync(e))return`bound-or-explicit`;if(resolveApprovalForwardTargets(e).length>0)return`bound-or-explicit`;return`unbound`}function resolveLegacyApprovalRequestSessionDelivery(e){const t=normalizeOptionalString(e.request.request.sessionKey);if(!t)return null;const n=parseAgentSessionKey(t)?.agentId??e.request.request.agentId??`main`;const r=resolveSessionStorePathCore(e.cfg.session?.store,{agentId:n});const i=captureSessionActorStorageOwner({sessionKey:t,agentId:n,storePath:r},{assertCurrent(){},authorize(){}});if(i)return approvalRequestSessionDelivery(i.binding?.actor.target.sessionKey===t?i.binding.actor.snapshot(i.authority)?.entry:i.owner?.readSession(t,i.authority)?.entry);return approvalRequestSessionDelivery(loadSessionEntryReadOnly({storePath:r,sessionKey:t,clone:false}))}function resolvePersistedApprovalRequestSessionBinding(e){if(!e)return null;const t=sessionDeliveryOrigin(e);const n=deliveryContextFromSession(e);const r=normalizeMessageChannel(n?.channel??t?.provider);const i=normalizeOptionalAccountId(n?.accountId??t?.accountId);return r||i?{channel:r,accountId:i}:null}function resolveApprovalRequestAccountBinding(e,t){const n=normalizeMessageChannel(e.channel);const r=normalizeMessageChannel(e.request.request.turnSourceChannel);if(n&&r&&r!==n)return null;const i=normalizeOptionalAccountId(e.request.request.turnSourceAccountId);if(i)return{accountId:i};const a=resolvePersistedApprovalRequestSessionBinding(t);const o=a?.channel;if(n&&o&&o!==n)return null;return{accountId:a?.accountId??null}}function resolveApprovalRequestAccountIdFromDelivery(e,t){return resolveApprovalRequestAccountBinding(e,t)?.accountId??null}function resolveApprovalRequestChannelAccountIdFromDelivery(e,t){const n=normalizeMessageChannel(e.channel);if(!n)return null;const r=normalizeMessageChannel(e.request.request.turnSourceChannel);if(!r||r===n)return resolveApprovalRequestAccountIdFromDelivery(e,t);const i=resolvePersistedApprovalRequestSessionBinding(t);return i?.channel===n?i.accountId??null:null}function doesApprovalRequestMatchChannelAccountFromDelivery(e,t){if(!normalizeMessageChannel(e.channel))return false;const n=resolveApprovalRequestAccountBinding(e,t);if(!n)return false;const r=normalizeOptionalAccountId(e.accountId);return!r||!n.accountId||r===n.accountId}function doesApprovalRequestSelectChannelAccountFromDelivery(e,t){const n=normalizeOptionalAccountId(e.accountId)??normalizeOptionalAccountId(e.defaultAccountId);if(!n)return false;const r=resolveApprovalRequestChannelAccountIdFromDelivery(e,t);if(n===normalizeOptionalAccountId(r))return true;const i=resolveApprovalForwardAccountIds(e);if(i.includes(n))return true;if(r||i.length>0)return false;const a=normalizeMessageChannel(e.request.request.turnSourceChannel);if(a&&a!==normalizeMessageChannel(e.channel))return false;const o=e.eligibleAccountIds.map(normalizeOptionalAccountId).filter(e=>Boolean(e));return o.length===1&&o[0]===n}async function resolveApprovalRequestSessionDeliveryAsync(e){if(e.sessionDelivery!==void 0)return e.sessionDelivery;const t=normalizeOptionalString(e.request.request.sessionKey);if(!t)return null;const n=parseAgentSessionKey(t)?.agentId??e.request.request.agentId??`main`;const r=resolveSessionStorePathCore(e.cfg.session?.store,{agentId:n});const i=captureSessionActorStorageOwner({sessionKey:t,agentId:n,storePath:r},{assertCurrent(){},authorize(){}});if(i)return approvalRequestSessionDelivery(i.binding?.actor.target.sessionKey===t?i.binding.actor.snapshot(i.authority)?.entry:i.owner?.readSession(t,i.authority)?.entry);return approvalRequestSessionDelivery(await readSessionEntryReadOnlyInWorker({agentId:n,storePath:r,sessionKey:t,projection:`list`,hydrateSkillPromptRefs:false}))}function warnLegacyApprovalRouting(e){warnPluginSdkDeprecation({family:`approval-session-routing`,method:e,replacement:`${e}Async`})}function resolveApprovalRequestAccountId(e){warnLegacyApprovalRouting(`resolveApprovalRequestAccountId`);return resolveApprovalRequestAccountIdFromDelivery(e,resolveLegacyApprovalRequestSessionDelivery(e))}async function resolveApprovalRequestAccountIdAsync(e){return resolveApprovalRequestAccountIdFromDelivery(e,normalizeOptionalAccountId(e.request.request.turnSourceAccountId)?null:await resolveApprovalRequestSessionDeliveryAsync(e))}function resolveApprovalRequestChannelAccountId(e){warnLegacyApprovalRouting(`resolveApprovalRequestChannelAccountId`);return resolveApprovalRequestChannelAccountIdFromDelivery(e,resolveLegacyApprovalRequestSessionDelivery(e))}async function resolveApprovalRequestChannelAccountIdAsync(e){const t=normalizeMessageChannel(e.channel);const n=normalizeMessageChannel(e.request.request.turnSourceChannel);const r=normalizeOptionalAccountId(e.request.request.turnSourceAccountId)&&(!n||n===t);return resolveApprovalRequestChannelAccountIdFromDelivery(e,!t||r?null:await resolveApprovalRequestSessionDeliveryAsync(e))}function doesApprovalRequestMatchChannelAccount(e){warnLegacyApprovalRouting(`doesApprovalRequestMatchChannelAccount`);return doesApprovalRequestMatchChannelAccountFromDelivery(e,resolveLegacyApprovalRequestSessionDelivery(e))}async function doesApprovalRequestMatchChannelAccountAsync(e){return doesApprovalRequestMatchChannelAccountFromDelivery(e,normalizeOptionalAccountId(e.request.request.turnSourceAccountId)?null:await resolveApprovalRequestSessionDeliveryAsync(e))}function doesApprovalRequestSelectChannelAccount(e){warnLegacyApprovalRouting(`doesApprovalRequestSelectChannelAccount`);return doesApprovalRequestSelectChannelAccountFromDelivery(e,resolveLegacyApprovalRequestSessionDelivery(e))}async function doesApprovalRequestSelectChannelAccountAsync(e){const t=normalizeMessageChannel(e.channel);const n=normalizeMessageChannel(e.request.request.turnSourceChannel);const r=normalizeOptionalAccountId(e.request.request.turnSourceAccountId)&&(!n||n===t);return doesApprovalRequestSelectChannelAccountFromDelivery(e,!t||r?null:await resolveApprovalRequestSessionDeliveryAsync(e))}export{doesApprovalRequestSelectChannelAccount as a,resolveApprovalRequestAccountId as c,resolveApprovalRequestChannelAccountIdAsync as d,resolveApprovalRequestSessionDeliveryAsync as f,resolveApprovalRequestKind as g,normalizeApprovalRequest as h,doesApprovalRequestMatchChannelAccountFromDelivery as i,resolveApprovalRequestAccountIdAsync as l,isPluginApprovalRequest as m,doesApprovalRequestMatchChannelAccount as n,doesApprovalRequestSelectChannelAccountAsync as o,resolveLegacyApprovalRequestSessionDelivery as p,doesApprovalRequestMatchChannelAccountAsync as r,doesApprovalRequestSelectChannelAccountFromDelivery as s,classifyApprovalRequestChannelRouteAsync as t,resolveApprovalRequestChannelAccountId as u};