UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

92 lines (91 loc) • 3.08 kB
import { l as normalizeOptionalString } from "./string-coerce-CCIiB4H7.mjs"; import { T as parseRawSessionConversationRef } from "./session-key-CqAyZeVk.mjs"; //#region src/plugins/hook-agent-context.ts /** Builds plugin hook agent context snapshots from active session and model state. */ const TARGET_PREFIXES = /* @__PURE__ */ new Set([ "channel", "chat", "direct", "dm", "group", "thread", "user" ]); function normalizeKey(e) { return (e ?? "").trim().toLowerCase(); } function stripConversationPrefix(e, ...t) { const n = normalizeOptionalString(e); if (!n) return; const r = n.indexOf(":"); if (r === -1) return n; const i = normalizeKey(n.slice(0, r)); const a = normalizeOptionalString(n.slice(r + 1)); if (!a) return n; if (TARGET_PREFIXES.has(i) || t.some((e) => i === normalizeKey(e))) return a; return n; } /** Builds channel/provider fields for plugin agent hook context. */ function buildAgentHookContextChannelFields(e) { function resolveAgentHookChannel() { const t = normalizeOptionalString(e.messageChannel); const n = normalizeOptionalString(e.messageProvider); if (!t) return n; const r = t.indexOf(":"); if (r === -1) return t; const i = normalizeOptionalString(t.slice(0, r)); if (!i) return n; const a = normalizeKey(i); if (TARGET_PREFIXES.has(a) || a === normalizeKey(n)) return n; return i; } /** Resolves the channel id exposed to plugin agent hooks. */ function resolveAgentHookChannelId() { const t = normalizeOptionalString(e.messageProvider); const n = normalizeOptionalString(e.messageChannel); const r = parseRawSessionConversationRef(e.sessionKey); if (r?.rawId) return r.rawId; const i = stripConversationPrefix(e.currentChannelId ?? void 0, t, n) ?? stripConversationPrefix(e.messageTo ?? void 0, t, n); if (i && normalizeKey(i) !== normalizeKey(t)) return i; const a = stripConversationPrefix(e.messageChannel ?? void 0, t, n); if (a && normalizeKey(a) !== normalizeKey(t)) return a; return n ?? t; } const t = resolveAgentHookChannel(); const n = resolveAgentHookChannelId(); const r = normalizeOptionalString(e.agentAccountId); return { channel: t, ...r ? { accountId: r } : {}, messageProvider: normalizeOptionalString(e.messageProvider), channelId: n, chatId: n, senderId: normalizeOptionalString(e.senderId) }; } function buildAgentHookContextIdentityFields(e) { const t = normalizeOptionalString(e.trigger); if (t && t !== "user") return {}; const n = normalizeOptionalString(e.senderId); const r = normalizeOptionalString(e.chatId); const i = n ? { ...e.channelContext?.sender, id: n } : e.channelContext?.sender; const a = r ? { ...e.channelContext?.chat, id: r } : e.channelContext?.chat; const o = i || a || e.channelContext ? { ...e.channelContext, ...i ? { sender: i } : {}, ...a ? { chat: a } : {} } : void 0; return { ...n ? { senderId: n } : {}, ...r ? { chatId: r } : {}, ...o ? { channelContext: o } : {} }; } //#endregion export { buildAgentHookContextIdentityFields as n, buildAgentHookContextChannelFields as t };