UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

3 lines • 6.95 kB
import{l as normalizeOptionalString}from"./string-coerce-CpTFcSwY.mjs";import{n as normalizeAgentId}from"./agent-id-cOfqmfln.mjs";import{n as buildAgentMainSessionKey}from"./session-key-DYtwLLn1.mjs";import"./session-key-BPgKfU2m.mjs";import{r as normalizeOptionalAccountId}from"./account-id-3yqrEsRn.mjs";import{r as waitForAgentDatabaseWriteAdmission}from"./agent-database-preparation-context-CPut921o.mjs";import{t as normalizeRouteBindingChannelId}from"./binding-scope-D9i37lvD.mjs";import{t as INTERNAL_MESSAGE_CHANNEL}from"./message-channel-constants-BbP_hczw.mjs";import{n as assertAgentDatabaseAdmitted}from"./agent-database-admission-DiJx9iIl.mjs";import{n as normalizeMessageChannel}from"./message-channel-core-D_hPOadj.mjs";import{n as isGatewayMessageChannel,t as isDeliverableMessageChannel}from"./message-channel-normalize-hDAO6_JQ.mjs";import"./message-channel-514k57tV.mjs";import{r as listConfiguredBindings,t as isRouteBinding}from"./bindings-ChJyFxlC.mjs";import{i as resolveChannelDefaultAccountId}from"./helpers-1A2j6LGz.mjs";import{t as resolveSessionDeliveryTarget}from"./targets-session-CAONApI3.mjs";import{r as resolveOutboundChannelPlugin}from"./channel-resolution-DC4uO43j.mjs";import{i as resolveChannelTarget}from"./target-resolver-DE3emqE4.mjs";import{a as resolveOutboundSessionRoute}from"./outbound-session-CHdHqfxF.mjs";import{n as resolveOutboundTarget}from"./targets-DMMK26tO.mjs"; //!src/infra/outbound/agent-delivery.ts function resolveAgentDeliveryPlan(e){const t=normalizeOptionalString(e.requestedChannel)??``;const n=(t?normalizeMessageChannel(t):void 0)||`last`;const r=normalizeOptionalString(e.explicitTo);const i=e.turnSourceChannel?normalizeMessageChannel(e.turnSourceChannel):void 0;const a=i&&isDeliverableMessageChannel(i)?i:void 0;const o=normalizeOptionalString(e.turnSourceTo);const s=normalizeOptionalAccountId(e.turnSourceAccountId);const c=e.turnSourceThreadId!=null&&e.turnSourceThreadId!==``?e.turnSourceThreadId:void 0;const l=resolveSessionDeliveryTarget({entry:e.sessionEntry,requestedChannel:n===`webchat`?`last`:n,explicitTo:r,explicitThreadId:e.explicitThreadId,turnSourceChannel:a,turnSourceTo:o,turnSourceAccountId:s,turnSourceThreadId:c});const u=n===`webchat`?INTERNAL_MESSAGE_CHANNEL:n!==`last`&&isGatewayMessageChannel(n)?n:l.channel||`webchat`;const d=r?`explicit`:isDeliverableMessageChannel(u)?`implicit`:void 0;const f=normalizeOptionalAccountId(e.accountId)??(d===`implicit`?l.accountId:void 0);let p=r;if(!p&&isDeliverableMessageChannel(u)&&u===l.lastChannel)p=l.lastTo;return{baseDelivery:l,resolvedChannel:u,resolvedTo:p,resolvedAccountId:f,resolvedThreadId:l.threadId,deliveryTargetMode:d}}async function resolveAgentDeliveryPlanWithSessionRoute(e){await waitForAgentDatabaseWriteAdmission(e.agentId);assertAgentDatabaseAdmitted(e.agentId);const t=resolveAgentDeliveryPlan(e);const{resolvedChannel:n}=t;if(!e.wantsDelivery||!isDeliverableMessageChannel(n))return t;const r=e.preparedPlugin??resolveOutboundChannelPlugin({channel:n,cfg:e.cfg,agentId:e.agentId,allowBootstrap:true});if(!r)return t;t.plugin=r;const i=Boolean(r.messaging?.resolveOutboundSessionRoute);const a=Boolean(r.messaging?.targetResolver);const o=Boolean(r.messaging?.targetResolver?.resolveTarget);if(!i&&!a&&e.sessionRouteMode!==`allow-fallback`)return t;t.resolvedAccountId??=e.sessionRouteMode===`allow-fallback`?resolveChannelDefaultAccountId({plugin:r,cfg:e.cfg}):void 0;const s=await resolveOutboundTarget({channel:n,plugin:r,to:t.resolvedTo,cfg:e.cfg,accountId:t.resolvedAccountId,mode:t.deliveryTargetMode??`explicit`});const c=s.ok?s.to:t.resolvedTo;if(!c)return s.ok?t:{...t,targetResolutionError:s.error};const l=await resolveChannelTarget({cfg:e.cfg,channel:n,input:c,accountId:t.resolvedAccountId,unknownTargetMode:o?`error`:`normalized`,plugin:r});if(!l.ok)return{...t,targetResolutionError:l.error};if(!s.ok&&l.target.resolutionSource===`normalized`)return{...t,targetResolutionError:s.error};const u=l.target.to;const d=!s.ok||s.to!==l.target.to||l.target.resolutionSource===`directory`?l.target:void 0;t.resolvedTo=u;if(!i&&e.sessionRouteMode!==`allow-fallback`)return t;const f=e.explicitThreadId!=null&&e.explicitThreadId!==``?e.explicitThreadId:void 0;const p=t.deliveryTargetMode===`explicit`?f:t.resolvedThreadId;const m=await resolveOutboundSessionRoute({cfg:e.cfg,channel:n,plugin:r,agentId:e.agentId,accountId:t.resolvedAccountId,target:u,...d?{resolvedTarget:d}:{},currentSessionKey:e.currentSessionKey,threadId:p}).catch(()=>null);const h=e.cfg.session?.dmScope??`main`;const g=e.sessionRouteMode===`allow-fallback`&&(m?.recipientSessionExact===false||m?.recipientSessionExact===`direct-alias`);const _=buildAgentMainSessionKey({agentId:e.agentId,mainKey:e.cfg.session?.mainKey});const v=m?.recipientSessionExact===`direct-alias`&&m.chatType===`direct`&&m.sessionKey===m.baseSessionKey&&m.sessionKey===_&&h===`main`&&!listConfiguredBindings(e.cfg).some(e=>isRouteBinding(e)&&e.session?.dmScope!==void 0&&e.session.dmScope!==`main`&&normalizeRouteBindingChannelId(e.match.channel)===n);const y=m?.recipientSessionExact===`delivery-identity`&&m.baseSessionKey!==_&&m.baseSessionKey.startsWith(`agent:${normalizeAgentId(e.agentId)}:${n}:`)&&(m.sessionKey===m.baseSessionKey||m.sessionKey.startsWith(`${m.baseSessionKey}:`));const b=m&&(m.recipientSessionExact===`delivery-identity`?y:!g||v)?m:null;if(!b)return d?{...t,resolvedTo:d.to,resolvedThreadId:p}:t;return{...t,resolvedSessionKey:b.sessionKey,resolvedTo:i?b.to:d?.to??u,resolvedThreadId:b.threadId??p}}async function resolveAgentExplicitRecipientSession(e){const t=await resolveAgentDeliveryPlanWithSessionRoute({cfg:e.cfg,agentId:e.agentId,requestedChannel:e.channel,explicitTo:e.to,explicitThreadId:e.threadId,accountId:e.accountId,wantsDelivery:true,sessionRouteMode:`allow-fallback`});if(!t.resolvedSessionKey&&!t.targetResolutionError)return{error:/* @__PURE__ */ new Error(`Unable to resolve a session route for channel "${e.channel}"`)};return{sessionKey:t.resolvedSessionKey,channel:t.resolvedChannel,to:t.resolvedTo,accountId:t.resolvedAccountId,threadId:t.resolvedThreadId,error:t.targetResolutionError}}async function resolveAgentOutboundTarget(e){const t=e.targetMode??e.plan.deliveryTargetMode??(e.plan.resolvedTo?`explicit`:`implicit`);if(e.plan.targetResolutionError)return{resolvedTarget:{ok:false,error:e.plan.targetResolutionError},resolvedTo:void 0};if(!isDeliverableMessageChannel(e.plan.resolvedChannel)||e.validateExplicitTarget!==true&&e.plan.resolvedTo)return{resolvedTarget:null,resolvedTo:e.plan.resolvedTo};const n=await resolveOutboundTarget({channel:e.plan.resolvedChannel,...e.plan.plugin?{plugin:e.plan.plugin}:{},to:e.plan.resolvedTo,cfg:e.cfg,accountId:e.plan.resolvedAccountId,mode:t});return{resolvedTarget:n,resolvedTo:n.ok?n.to:e.plan.resolvedTo}}export{resolveAgentExplicitRecipientSession as n,resolveAgentOutboundTarget as r,resolveAgentDeliveryPlanWithSessionRoute as t};