UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

3 lines • 2.43 kB
import{l as normalizeOptionalString}from"./string-coerce-CpTFcSwY.mjs";import{E as parseRawSessionConversationRef}from"./session-key-BPgKfU2m.mjs"; //!src/plugins/hook-agent-context.ts 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}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}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}:{}}}export{buildAgentHookContextIdentityFields as n,buildAgentHookContextChannelFields as t};