UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

3 lines • 6.32 kB
import{l as normalizeOptionalString}from"./string-coerce-CpTFcSwY.mjs";import{o as getPluginRuntimeGatewayRequestScope}from"./gateway-request-scope-7deHmsb_.mjs";import{v as isReplyPayloadTargetSuppressed}from"./reply-payload-C7eQHHoS.mjs";import{T as getGroupThreadDispatchContext}from"./hooks-Bu2gwS_g.mjs";import{s as isOutboundDeliveryError}from"./deliver-types-QUMIGH6t.mjs";import{l as isProvenDeliveryNotSentError}from"./delivery-recovery.shared-CMcli399.mjs";import{t as normalizeDeliverableOutboundChannel}from"./channel-resolution-DC4uO43j.mjs";import{s as resolveOutboundDurableFinalDeliverySupport}from"./deliver-prepare-BZF1WFGi.mjs";import{t as deriveDurableFinalDeliveryRequirements}from"./capabilities-CZeZVSwr.mjs";import"./deliver-36L8iK0I.mjs";import{t as buildOutboundSessionContext}from"./session-context-7JncscTR.mjs";import{d as createChannelPartialDeliveryError,l as createChannelDeliveryResultFromReceipt}from"./live-CZp14Lp3.mjs";import{n as sendDurableMessageBatchCore,r as sendStructuredDurableMessageBatchCore,t as durableMessageBatchMayHaveReachedRecipient}from"./send-BGs4mM8f.mjs";import{t as runAdmittingGatewayDelivery}from"./admitting-gateway-delivery-BC7BdAYF.mjs"; //!src/channels/turn/durable-delivery.ts function resolveDurableInboundReplyToId(e){if(isReplyPayloadTargetSuppressed(e.payload))return null;if(e.replyToId===null||e.payload.replyToId===null)return null;return normalizeOptionalString(e.replyToId)??normalizeOptionalString(e.payload.replyToId)??normalizeOptionalString(e.ctxPayload.ReplyToIdFull)??normalizeOptionalString(e.ctxPayload.ReplyToId)}function resolveDurableSuppression(e){const t=e.payloadOutcomes?.find(e=>e.status===`suppressed`)?.hookEffect;return{reason:e.reason,...t?.cancelReason?{cancelReason:t.cancelReason}:{},...t?.metadata?{metadata:t.metadata}:{}}}function isDurableInboundReplyDeliveryHandled(e){return e.status===`handled_visible`||e.status===`handled_no_send`}function throwIfDurableInboundReplyDeliveryFailed(e){if(e.status===`failed`)throw e.error}async function deliverInboundReplyWithMessageSendContextCore(e){return await deliverInboundReplyWithMessageSendContext(e,sendDurableMessageBatchCore)}async function deliverStructuredInboundReplyWithMessageSendContextCore(e){const{plan:t,...n}=e;return await deliverInboundReplyWithMessageSendContext({...n,payload:t.payload},({payloads:e,...n})=>sendStructuredDurableMessageBatchCore({...n,plan:[t]}))}async function deliverInboundReplyWithMessageSendContext(e,t){if(e.info.kind!==`final`)return{status:`not_applicable`,reason:`non_final`};try{const deliver=(n,r)=>deliverAdmittedInboundReply({...e,cfg:n},t,r);const n=getPluginRuntimeGatewayRequestScope()?.pluginRegistry;if(!n)return await deliver(e.cfg);return await runAdmittingGatewayDelivery({registry:n,cfg:e.cfg,channel:e.channel,prepareRuntimeHandoff:e.prepareRuntimeHandoff},deliver)}catch(e){return{status:`failed`,error:e}}}async function deliverAdmittedInboundReply(e,t,n){const r=getGroupThreadDispatchContext();const i=r?{...e,agentId:r.ctx.AgentId??e.agentId,ctxPayload:r.ctx,runId:r.runState.runId,executionIdentityToken:r.runState.executionIdentityToken}:e;const a=normalizeDeliverableOutboundChannel(i.channel);const o=normalizeOptionalString(i.to)??normalizeOptionalString(i.ctxPayload.OriginatingTo)??normalizeOptionalString(i.ctxPayload.To);if(!a)return{status:`unsupported`,reason:`missing_channel`};if(!o)return{status:`unsupported`,reason:`missing_target`};const s=resolveDurableInboundReplyToId(i);const c=`threadId`in i?i.threadId:i.ctxPayload.MessageThreadId;const l=i.requiredCapabilities??deriveDurableFinalDeliveryRequirements({payload:i.payload,replyToId:s,threadId:c,silent:i.silent});const u=l.reconcileUnknownSend===true?`required`:`best_effort`;const d=await resolveOutboundDurableFinalDeliverySupport({cfg:i.cfg,agentId:i.agentId,channel:a,requirements:l});if(!d.ok)return{status:`unsupported`,reason:d.reason,...d.capability?{capability:d.capability}:{}};const f=buildOutboundSessionContext({cfg:i.cfg,sessionKey:i.ctxPayload.SessionKey,policySessionKey:i.ctxPayload.RuntimePolicySessionKey,conversationType:i.ctxPayload.ChatType,agentId:i.agentId,requesterAccountId:i.accountId??i.ctxPayload.AccountId,requesterSenderId:i.ctxPayload.SenderId??i.ctxPayload.From,requesterSenderName:i.ctxPayload.SenderName,requesterSenderUsername:i.ctxPayload.SenderUsername,requesterSenderE164:i.ctxPayload.SenderE164});n?.();const p=await t({assertDirectAdapterHandoff:n,cfg:i.cfg,channel:a,to:o,accountId:i.accountId,payloads:[i.payload],retryAmbiguousFinalText:true,...i.runId??i.executionIdentityToken?.runId?{runId:i.runId??i.executionIdentityToken?.runId}:{},...i.executionIdentityToken?{executionIdentityToken:i.executionIdentityToken}:{},threadId:c,replyToId:s,replyToMode:i.replyToMode,formatting:i.formatting,identity:i.identity,deps:i.deps,mediaAccess:i.mediaAccess,silent:i.silent,durability:u,...l.reconcileUnknownSend===true?{requireUnknownSendReconciliation:true}:{},session:f,gatewayClientScopes:i.ctxPayload.GatewayClientScopes??[]});if(p.status===`failed`){const e=!durableMessageBatchMayHaveReachedRecipient(p)&&isProvenDeliveryNotSentError(p.error);return{status:`failed`,error:p.error,...e?{sentBeforeError:false}:{},...e&&isOutboundDeliveryError(p.error)&&p.error.queueCustody===`held`?{retainedForRecovery:true}:{}}}const m=p.status===`partial_failed`?p.results.map(e=>e.meta?.visibleText).filter(e=>typeof e===`string`).join(``):void 0;const h=createChannelDeliveryResultFromReceipt({receipt:p.receipt,threadId:c==null?void 0:String(c),...s?{replyToId:s}:{},visibleReplySent:p.status!==`suppressed`,...m?{content:m}:{},...p.deliveryIntent?{deliveryIntent:{id:p.deliveryIntent.id,kind:`outbound_queue`,queuePolicy:p.deliveryIntent.queuePolicy}}:{}});if(p.status===`partial_failed`)return{status:`failed`,error:createChannelPartialDeliveryError(p.error,{...h,visibleReplySent:true}),sentBeforeError:true};if(p.status===`suppressed`)return{status:`handled_no_send`,reason:`no_visible_result`,delivery:{...h,suppression:resolveDurableSuppression(p)}};return{status:`handled_visible`,delivery:h}}export{throwIfDurableInboundReplyDeliveryFailed as i,deliverStructuredInboundReplyWithMessageSendContextCore as n,isDurableInboundReplyDeliveryHandled as r,deliverInboundReplyWithMessageSendContextCore as t};