openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
3 lines • 8.45 kB
JavaScript
import{t as createSubsystemLogger}from"./subsystem-DLCDFvCH.mjs";import{t as warnPluginSdkDeprecation}from"./sdk-deprecation-BDUCyBrU.mjs";import{t as canChannelEnforcePluginReviewerPolicy}from"./approval-channel-policy-support-C1UTUR8o.mjs";import{t as getGatewayNativeApprovalRuntime}from"./approval-gateway-runtime-context-BrssdAQd.mjs";import{h as normalizeApprovalRequest}from"./approval-request-account-binding-DFLDpifZ.mjs";import"./approval-handler-adapter-runtime-okvizeSM.mjs";import{n as createChannelNativeApprovalRuntimeAsync}from"./approval-native-runtime-C4YDPD_t.mjs";import{n as buildPendingApprovalView,r as buildResolvedApprovalView,t as buildExpiredApprovalView}from"./approval-view-model-BpkC98SL.mjs";
//!src/infra/approval-handler-runtime.ts
function createChannelApprovalNativeRuntimeAdapter(e){warnPluginSdkDeprecation({family:`approval-session-routing`,method:`createChannelApprovalNativeRuntimeAdapter`,replacement:`createChannelApprovalNativeRuntimeAdapterAsync`});return{...createChannelApprovalNativeRuntimeAdapterAsync(e),availability:e.availability}}function createChannelApprovalNativeRuntimeAdapterAsync(e){const t={...e.eventKinds?{eventKinds:e.eventKinds}:{},...e.resolveApprovalKind?{resolveApprovalKind:e.resolveApprovalKind}:{},availability:{isConfigured:e.availability.isConfigured,shouldHandle:e.availability.shouldHandle},presentation:{buildPendingPayload:async t=>await e.presentation.buildPendingPayload(t),buildResolvedResult:async t=>await e.presentation.buildResolvedResult(t),buildExpiredResult:async t=>await e.presentation.buildExpiredResult(t)},transport:{prepareTarget:async t=>await e.transport.prepareTarget(t),deliverPending:async t=>await e.transport.deliverPending(t)}};if(e.transport.updateEntry)t.transport.updateEntry=async t=>await e.transport.updateEntry?.(t);if(e.transport.deleteEntry)t.transport.deleteEntry=async t=>await e.transport.deleteEntry?.(t);if(e.interactions){const n={};if(e.interactions.bindPending)n.bindPending=async t=>await e.interactions.bindPending(t)??null;if(e.interactions.unbindPending)n.unbindPending=async t=>await e.interactions?.unbindPending?.(t);if(e.interactions.clearPendingActions)n.clearPendingActions=async t=>await e.interactions?.clearPendingActions?.(t);if(e.interactions.cancelDelivered)n.cancelDelivered=async t=>await e.interactions?.cancelDelivered?.(t);t.interactions=n}if(e.observe){const n={};if(e.observe.onDeliveryError)n.onDeliveryError=t=>e.observe?.onDeliveryError?.(t);if(e.observe.onDuplicateSkipped)n.onDuplicateSkipped=t=>e.observe?.onDuplicateSkipped?.(t);if(e.observe.onDelivered)n.onDelivered=t=>e.observe?.onDelivered?.(t);if(e.observe.onFinalized)n.onFinalized=t=>e.observe?.onFinalized?.(t);t.observe=n}return t}function createChannelApprovalHandler(e){return createChannelApprovalHandlerAsync(e)}function createChannelApprovalHandlerAsync(e){return createChannelNativeApprovalRuntimeAsync({label:e.runtime.label,clientDisplayName:e.runtime.clientDisplayName,cfg:e.runtime.cfg,getRequestConfig:e.runtime.getRequestConfig,gatewayUrl:e.runtime.gatewayUrl,eventKinds:e.runtime.eventKinds,channel:e.runtime.channel,channelLabel:e.runtime.channelLabel,accountId:e.runtime.accountId,nativeAdapter:e.runtime.nativeAdapter,...e.runtime.resolveApprovalKind?{resolveApprovalKind:e.runtime.resolveApprovalKind}:{},isConfigured:e.runtime.isConfigured,shouldHandle:e.runtime.shouldHandle,nowMs:e.runtime.nowMs,buildPendingContent:e.content.buildPendingContent,prepareTarget:e.transport.prepareTarget,deliverTarget:e.transport.deliverTarget,onDeliveryError:e.lifecycle.onDeliveryError,onDuplicateSkipped:e.lifecycle.onDuplicateSkipped,onDelivered:e.lifecycle.onDelivered,finalizeResolved:e.lifecycle.finalizeResolved,finalizeExpired:e.lifecycle.finalizeExpired,onStopped:e.lifecycle.onStopped})}async function createChannelApprovalHandlerFromCapability(e){const t=e.capability?.nativeRuntimeAsync??e.capability?.nativeRuntime;if(!t)return null;const n=createSubsystemLogger(e.label);const r=/* @__PURE__ */ new Map;let i=false;const resolveApprovalKind=e=>{const n=normalizeApprovalRequest(e);return t.resolveApprovalKind?.(n)??n.approvalKind};const a=getGatewayNativeApprovalRuntime();const createContext=()=>({cfg:a?a.getRuntimeConfig():e.cfg,accountId:e.accountId,gatewayUrl:e.gatewayUrl,context:e.context});const o=/* @__PURE__ */ new WeakMap;const getRequestContext=e=>{let t=o.get(e.request);if(!t){t=createContext();o.set(e.request,t)}return t};const pendingContext=({pendingContent:e,...t})=>({...getRequestContext(t.request),...t,view:e.view,pendingPayload:e.payload});const finalize=async(e,i,a)=>{const o=getRequestContext(e);const s=r.get(e.id)?.entries??i;r.delete(e.id);const c=resolveApprovalKind(e);let buildResult;if(a.phase===`resolved`){const n=buildResolvedApprovalView(e,a.resolved);buildResult=r=>t.presentation.buildResolvedResult({...o,request:e,resolved:a.resolved,view:n,entry:r})}else{const n=buildExpiredApprovalView(e);buildResult=r=>t.presentation.buildExpiredResult({...o,request:e,view:n,entry:r})}for(const[r,i]of s.entries())try{const n={...o,entry:i.entry,phase:a.phase};if(i.binding!==void 0)await t.interactions?.unbindPending?.({...o,entry:i.entry,binding:i.binding,request:e,approvalKind:c});const l=await buildResult(i.entry);switch(l.kind){case`update`:await t.transport.updateEntry?.({...n,request:e,approvalKind:c,payload:l.payload,remainingEntries:s.slice(r+1).map(e=>e.entry)});break;case`delete`:await t.transport.deleteEntry?.(n);break;case`clear-actions`:await t.interactions?.clearPendingActions?.(n)}}catch(t){n.error(`failed to finalize ${a.phase} native approval entry approval=${e.id}: ${String(t)}`)}t.observe?.onFinalized?.({...o,request:e,approvalKind:c,phase:a.phase})};return createChannelApprovalHandlerAsync({runtime:{label:e.label,clientDisplayName:e.clientDisplayName,channel:e.channel,channelLabel:e.channelLabel,cfg:e.cfg,getRequestConfig:e=>getRequestContext(e).cfg,accountId:e.accountId,gatewayUrl:e.gatewayUrl,eventKinds:t.eventKinds,nativeAdapter:e.capability?.nativeAsync??e.capability?.native,...t.resolveApprovalKind?{resolveApprovalKind:t.resolveApprovalKind}:{},isConfigured:()=>t.availability.isConfigured(createContext()),shouldHandle:n=>{const r=resolveApprovalKind(n);const i=getRequestContext(n);if(r===`plugin`&&!canChannelEnforcePluginReviewerPolicy(i.cfg,e.channel,e.capability))return false;return t.availability.shouldHandle({...i,request:n,approvalKind:r})},nowMs:e.nowMs},content:{buildPendingContent:async({request:e,approvalKind:n,nowMs:r})=>{const i=buildPendingApprovalView(e);return{view:i,payload:await t.presentation.buildPendingPayload({...getRequestContext(e),request:e,approvalKind:n,nowMs:r,view:i})}}},transport:{prepareTarget:async e=>await t.transport.prepareTarget(pendingContext(e)),deliverTarget:async e=>{const{request:n,approvalKind:a,pendingContent:o}=e;const s=await t.transport.deliverPending(pendingContext(e));if(!s)return null;const c={...getRequestContext(n),entry:s,request:n,approvalKind:a};const l=i?void 0:await t.interactions?.bindPending?.({...c,view:o.view,pendingPayload:o.payload});if(i){if(l!==void 0&&l!==null)await t.interactions?.unbindPending?.({...c,binding:l});else await t.interactions?.cancelDelivered?.(c);return null}const u={entry:s,...l===void 0||l===null?{}:{binding:l}};const d=r.get(n.id)??{request:n,approvalKind:a,entries:[]};d.entries.push(u);r.set(n.id,d);return u}},lifecycle:{onDeliveryError:e=>{t.observe?.onDeliveryError?.(pendingContext(e))},onDuplicateSkipped:e=>{t.observe?.onDuplicateSkipped?.(pendingContext(e))},onDelivered:e=>{t.observe?.onDelivered?.({...pendingContext(e),entry:e.entry.entry})},finalizeResolved:({request:e,resolved:t,entries:n})=>finalize(e,n,{phase:`resolved`,resolved:t}),finalizeExpired:({request:e,entries:t})=>finalize(e,t,{phase:`expired`}),onStopped:async()=>{i=true;for(const e of r.values()){if(!t.interactions?.unbindPending)continue;for(const r of e.entries){if(r.binding===void 0)continue;try{await t.interactions.unbindPending({...getRequestContext(e.request),entry:r.entry,binding:r.binding,request:e.request,approvalKind:e.approvalKind})}catch(t){n.error(`failed to unbind stopped native approval entry approval=${e.request.id}: ${String(t)}`)}}}r.clear()}}})}export{createChannelApprovalNativeRuntimeAdapterAsync as i,createChannelApprovalHandlerFromCapability as n,createChannelApprovalNativeRuntimeAdapter as r,createChannelApprovalHandler as t};