UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

222 lines (221 loc) • 8.92 kB
import { t as isPromiseLike } from "./promise-like-aMoWEh5w.mjs"; import { t as createHostChannelInboundEventContextBuilder } from "./host-context-builder-BXWPIn7b.mjs"; import { r as shouldIncludeSupplementalContext } from "./context-visibility-DKa41bxb.mjs"; import { n as createCommandTurnContext, t as commandTurnKindToSource } from "./command-turn-context-BaN4kAo-.mjs"; import { t as normalizeInboundTextNewlines } from "./inbound-text-Bmx57Z44.mjs"; import { t as finalizeInboundContext } from "./inbound-context-0zDtB0Zn.mjs"; import { t as copyConversationBindingRouteFacts } from "./conversation-binding-route-facts-DfCm6YNl.mjs"; import { t as buildChannelInboundMediaPayload } from "./media-C-9Rs9OV.mjs"; //#region src/channels/inbound-event/context.ts /** * Channel inbound event context builder. * * Converts route, sender, command, media, and supplemental facts into finalized message context. */ function filterSupplementalContext(e, t, n) { if (!e || e === "all") return n; if (n?.senderAllowed === void 0) return; return shouldIncludeSupplementalContext({ mode: e, kind: t, senderAllowed: n.senderAllowed }) ? n : void 0; } function filterChannelInboundSupplementalContext(e) { const t = e.supplemental; if (!t) return; return { ...t, quote: filterSupplementalContext(e.contextVisibility, "quote", t.quote), forwarded: filterSupplementalContext(e.contextVisibility, "forwarded", t.forwarded), thread: filterSupplementalContext(e.contextVisibility, "thread", t.thread) }; } /** Resolves whether a supplemental-context sender passes the active group policy. */ function resolveInboundSupplementalSenderAllowed(e) { if (!e.isGroup || e.groupPolicy !== "allowlist") return true; return e.isSenderAllowed(e.allowFrom); } function filterChannelInboundQuoteContext(e, t) { return filterSupplementalContext(e, "quote", t); } function finalizeChannelInboundContext(e) { const t = e.context.SupplementalContext; const prepareSupplemental = () => { const n = e.supplemental ?? t; const { contextVisibility: r, media: i, resolveSupplementalMedia: a, suppressSelfQuoteBody: o, suppressSelfQuoteMedia: s } = e; const c = filterChannelInboundSupplementalContext({ supplemental: n, contextVisibility: r }); const l = [...i ?? []]; if (!n?.quote || !c?.quote) return { rawSupplemental: n, supplemental: c, media: l }; const u = c.quote; const d = u.isSelf === true; const f = o ?? false; const p = s ?? true; const finalizeQuote = (e) => { if (!(d && p)) l.push(...e ?? []); const { media: t, isSelf: r, ...i } = u; const a = d && f ? (({ body: e, ...t }) => t)(i) : i; return { rawSupplemental: n, supplemental: { ...c, quote: a }, media: l }; }; if (d && p) return finalizeQuote(void 0); if (!a) return finalizeQuote(Array.isArray(u.media) ? u.media : void 0); if (typeof u.media !== "function") return finalizeQuote(u.media); const m = u.media(); return isPromiseLike(m) ? m.then(finalizeQuote) : finalizeQuote(m); }; const n = prepareSupplemental(); const finish = (t) => { const { context: n, finalize: r, finalizeOptions: i } = e; const { rawSupplemental: a, supplemental: o, media: s } = t; const c = Object.fromEntries(Object.entries(buildChannelInboundMediaPayload(s)).filter(([, e]) => e !== void 0)); const l = { ...n, SupplementalContext: o, media: [...s], ...c }; const u = []; const d = l.ChannelStructuredContext ?? l.UntrustedStructuredContext; if (Array.isArray(d)) u.push(...d); const f = o?.channelStructuredContext ?? o?.untrustedContext; if (f !== void 0) u.push(...f); const p = normalizeUntrustedGroupPrompt(o?.untrustedGroupSystemPrompt); if (p) u.push({ label: "Group prompt context", type: "group_prompt_context", payload: { text: p } }); const m = d !== void 0 || f !== void 0 || p !== void 0 ? u : void 0; return { context: (r ?? finalizeInboundContext)({ ...l, ...m !== void 0 ? { ChannelStructuredContext: m } : {} }, i), supplemental: o, quoteHidden: Boolean(a?.quote && !o?.quote), forwardedHidden: Boolean(a?.forwarded && !o?.forwarded), threadHidden: Boolean(a?.thread && !o?.thread) }; }; if (e.resolveSupplementalMedia) return Promise.resolve(n).then(finish); return isPromiseLike(n) ? n.then(finish) : finish(n); } function normalizeUntrustedGroupPrompt(e) { if (typeof e !== "string") return; const t = normalizeInboundTextNewlines(e); return t.trim().length > 0 ? t : void 0; } function resolveChannelCommandContext(e) { if (e.commandTurn) return e.commandTurn; const t = e.command; if (!t) return; const n = t.body ?? e.message.commandBody ?? e.message.rawBody; return createCommandTurnContext(commandTurnKindToSource(t.kind), { authorized: t.kind === "normal" ? false : t.authorized ?? e.access?.commands?.authorized === true, commandName: t.name, body: n }); } function buildChannelInboundEventContext(e) { return buildChannelInboundEventContextValue(e); } const buildHostChannelInboundEventContextValue = createHostChannelInboundEventContextBuilder(buildChannelInboundEventContextValue); function buildHostChannelInboundEventContext(e) { return buildHostChannelInboundEventContextValue(e); } function buildChannelInboundEventContextValue(e) { const t = e.message.body ?? e.message.rawBody; const n = resolveChannelCommandContext(e); const r = { Body: t, InboundEventKind: e.message.inboundEventKind ?? "user_request", BodyForAgent: e.message.bodyForAgent ?? e.message.rawBody, InboundHistory: e.message.inboundHistory, SessionTranscriptContext: e.sessionTranscript && e.sessionTranscript.historyLimit > 0 ? e.sessionTranscript : void 0, SourceModality: e.message.sourceModality, RawBody: e.message.rawBody, CommandBody: e.message.commandBody ?? e.message.rawBody, BodyForCommands: e.message.commandBody ?? e.message.rawBody, From: e.from, To: e.reply.to, SessionKey: e.route.dispatchSessionKey ?? e.route.routeSessionKey, AgentId: e.route.agentId, DmScope: e.route.dmScope, AccountId: e.route.accountId ?? e.accountId, ParentSessionKey: e.route.parentSessionKey, ModelParentSessionKey: e.route.modelParentSessionKey, MessageSid: e.messageId, MessageSidFull: e.messageIdFull, ReplyToId: e.reply.replyToId, ReplyToIdFull: e.reply.replyToIdFull, ChatType: e.conversation.kind, ChatId: e.conversation.id, ConversationRoutePeerId: e.conversation.routePeer?.id, ConversationLabel: e.conversation.label, GroupSubject: e.conversation.kind !== "direct" ? e.conversation.label : void 0, GroupSpace: e.conversation.spaceId, SenderName: e.sender.name ?? e.sender.displayLabel, SenderId: e.sender.id, SenderUsername: e.sender.username, SenderTag: e.sender.tag, SenderIsBot: e.sender.isBot, SenderIsSelf: e.sender.isSelf === true ? true : void 0, MemberRoleIds: e.sender.roles, Timestamp: e.timestamp, Provider: e.provider ?? e.channel, Surface: e.surface ?? e.provider ?? e.channel, WasMentioned: e.access?.mentions?.wasMentioned, GroupRequireMention: e.access?.mentions?.requireMention, ExplicitlyMentionedBot: e.access?.mentions?.explicitlyMentionedBot, MentionedUserIds: e.access?.mentions?.mentionedUserIds, MentionedSubteamIds: e.access?.mentions?.mentionedSubteamIds, ImplicitMentionKinds: e.access?.mentions?.implicitMentionKinds, MentionSource: e.access?.mentions?.mentionSource, CommandAuthorized: e.access?.commands?.authorized === true, ConversationToolPolicy: e.access?.toolPolicy, CommandTurn: n, MessageThreadId: e.reply.messageThreadId ?? e.conversation.threadId, NativeChannelId: e.reply.nativeChannelId ?? e.conversation.nativeChannelId, ConversationAvatar: e.conversation.avatar, ChannelContext: e.channelContext, OriginatingChannel: e.channel, OriginatingTo: e.reply.originatingTo ?? e.reply.to, ThreadParentId: e.reply.threadParentId ?? e.conversation.parentId, InboundAccessAuthorized: true, ConversationRouteContextObserved: e.conversation.routePeer ? true : void 0, ...e.extra }; copyConversationBindingRouteFacts(e.route, r); const i = { finalize: e.finalize, finalizeOptions: e.finalizeOptions, supplemental: e.supplemental, contextVisibility: e.contextVisibility, media: e.media, context: r }; const a = e.resolveSupplementalMedia ? finalizeChannelInboundContext({ ...i, resolveSupplementalMedia: true, suppressSelfQuoteBody: e.suppressSelfQuoteBody, suppressSelfQuoteMedia: e.suppressSelfQuoteMedia }) : finalizeChannelInboundContext(i); const unwrap = (e) => e.context; return isPromiseLike(a) ? a.then(unwrap) : unwrap(a); } //#endregion export { finalizeChannelInboundContext as a, filterChannelInboundSupplementalContext as i, buildHostChannelInboundEventContext as n, resolveInboundSupplementalSenderAllowed as o, filterChannelInboundQuoteContext as r, buildChannelInboundEventContext as t };