UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

71 lines (70 loc) 4.57 kB
import { l as normalizeOptionalString } from "./string-coerce-CIXf7egm.js"; import { r as getLoadedChannelPluginForRead } from "./registry-loaded-Bh7xuMJh.js"; import "./message-channel-constants-2zSoJXQC.js"; import { n as normalizeMessageChannel } from "./message-channel-core-AaEWic-A.js"; import "./message-channel-BQrhwUEA.js"; import { t as normalizeChatType } from "./chat-type-CG0X_HJM.js"; import { f as stringifyRouteThreadId } from "./channel-route--2in3Ckg.js"; import { f as sessionDeliveryOrigin, n as deliveryContextFromSession, p as sessionDeliveryRoute } from "./delivery-context.shared-CXmRgetN.js"; import { n as stripOutboundTargetKindPrefix, r as stripTargetProviderPrefix } from "./channel-target-prefix-D7Ut6oKe.js"; //#region src/auto-reply/reply/effective-reply-route.ts /** Resolves the effective reply route from current context and persisted session route. */ /** Normalizes an external target without collapsing provider-owned topic suffixes. */ function normalizeEffectiveReplyTarget(raw, channel, threadId) { let target = normalizeOptionalString(raw); if (target && channel && threadId != null) target = getLoadedChannelPluginForRead(channel)?.threading?.resolveCurrentChannelId?.({ to: target, threadId }) ?? target; return target ? normalizeOptionalString(stripOutboundTargetKindPrefix(stripTargetProviderPrefix(target, channel ?? ""))) : void 0; } function isSessionsSendInterSessionHandoff(inputProvenance) { return inputProvenance?.kind === "inter_session" && inputProvenance.sourceTool?.toLowerCase() === "sessions_send"; } function resolveTrustedInheritedThreadId(entry) { const deliveryThreadId = deliveryContextFromSession(entry)?.threadId; if (deliveryThreadId == null) return; const routeThread = sessionDeliveryRoute(entry)?.thread; if (routeThread?.id != null && (routeThread.source === "explicit" || routeThread.source === "target" || routeThread.source === "turn") && stringifyRouteThreadId(routeThread.id) === stringifyRouteThreadId(deliveryThreadId)) return deliveryThreadId; } /** Resolves current, inherited, or persisted reply route for a session turn. */ function resolveEffectiveReplyRoute(params) { const currentSurface = normalizeMessageChannel(params.ctx.Provider) ?? normalizeMessageChannel(params.ctx.Surface) ?? normalizeMessageChannel(params.ctx.OriginatingChannel); const persistedDeliveryContext = deliveryContextFromSession(params.entry); const persistedRoute = sessionDeliveryRoute(params.entry); const persistedOrigin = sessionDeliveryOrigin(params.entry); const persistedDeliveryChannel = normalizeMessageChannel(persistedDeliveryContext?.channel); const liveChatType = normalizeChatType(params.ctx.ChatType); const persistedChatType = persistedRoute?.target?.chatType ?? params.entry?.chatType ?? normalizeChatType(persistedOrigin?.chatType); if (isSessionsSendInterSessionHandoff(params.ctx.InputProvenance) && currentSurface === "webchat" && persistedDeliveryChannel && persistedDeliveryChannel !== "webchat" && persistedDeliveryContext?.to) { const inheritedThreadId = resolveTrustedInheritedThreadId(params.entry); return { channel: persistedDeliveryChannel, to: persistedDeliveryContext.to, accountId: persistedDeliveryContext.accountId, ...inheritedThreadId !== void 0 ? { threadId: inheritedThreadId } : {}, ...persistedChatType ? { chatType: persistedChatType } : {}, inheritedExternalRoute: true }; } if (params.ctx.InternalTurnSource === void 0) return { channel: params.ctx.OriginatingChannel, to: params.ctx.OriginatingTo, accountId: params.ctx.AccountId, ...params.ctx.MessageThreadId !== void 0 ? { threadId: params.ctx.MessageThreadId } : {}, ...liveChatType ? { chatType: liveChatType } : {} }; const persistedChannel = persistedDeliveryContext?.channel; const liveChannel = params.ctx.OriginatingChannel; const canInheritPersistedTuple = !liveChannel || normalizeMessageChannel(liveChannel) === normalizeMessageChannel(persistedChannel); const chatType = liveChatType ?? (canInheritPersistedTuple ? persistedChatType : void 0); return { channel: liveChannel ?? persistedChannel, to: params.ctx.OriginatingTo ?? (canInheritPersistedTuple ? persistedDeliveryContext?.to : void 0), accountId: params.ctx.AccountId ?? (canInheritPersistedTuple ? persistedDeliveryContext?.accountId : void 0), ...params.ctx.MessageThreadId !== void 0 ? { threadId: params.ctx.MessageThreadId } : {}, ...chatType ? { chatType } : {} }; } //#endregion export { resolveEffectiveReplyRoute as n, normalizeEffectiveReplyTarget as t };