openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
5 lines • 5.26 kB
JavaScript
import{c as normalizeOptionalLowercaseString,l as normalizeOptionalString,o as normalizeLowercaseStringOrEmpty}from"./string-coerce-CpTFcSwY.mjs";import{T as parseStrictInteger}from"./number-coercion-B3CfI447.mjs";import{f as normalizeStringEntries}from"./string-normalization-D_oBUpO3.mjs";
//!src/channels/allowlist-match.ts
function formatAllowlistMatchMeta(e){return`matchKey=${e?.matchKey??`none`} matchSource=${e?.matchSource??`none`}`}function compileAllowlist(e){const t=new Set(e.filter(Boolean));return{set:t,wildcard:t.has(`*`)}}function resolveAllowlistCandidates(e){for(const t of e.candidates){if(!t.value)continue;if(e.compiledAllowlist.set.has(t.value))return{allowed:true,matchKey:t.value,matchSource:t.source}}return{allowed:false}}function resolveCompiledAllowlistMatch(e){if(e.compiledAllowlist.set.size===0)return{allowed:false};if(e.compiledAllowlist.wildcard)return{allowed:true,matchKey:`*`,matchSource:`wildcard`};return resolveAllowlistCandidates(e)}function resolveAllowlistMatchByCandidates(e){return resolveCompiledAllowlistMatch({compiledAllowlist:compileAllowlist(e.allowList),candidates:e.candidates})}function resolveAllowlistMatchSimple(e){const t=normalizeLowercaseStringOrEmpty(e.senderId);const n=normalizeOptionalLowercaseString(e.senderName);return resolveCompiledAllowlistMatch({compiledAllowlist:compileAllowlist(e.allowFrom.map(e=>normalizeLowercaseStringOrEmpty(String(e)))),candidates:[{value:t,source:`id`},{value:e.allowNameMatching===true?n:void 0,source:`name`}]})}
//!src/channels/plugins/chat-target-prefixes.ts
function isAllowedParsedChatSender(e){const t=normalizeStringEntries(e.allowFrom);const n=e.normalizeSender(e.sender);const r=e.allowConversationTargets===true;const i=r?e.chatId??void 0:void 0;const a=r?normalizeOptionalString(e.chatGuid):void 0;const o=r?normalizeOptionalString(e.chatIdentifier):void 0;const s=t.map(t=>{if(t===`*`)return t;const n=e.parseAllowTarget(t);if(n.kind===`chat_id`)return`chat_id:${n.chatId}`;if(n.kind===`chat_guid`)return`chat_guid:${n.chatGuid}`;if(n.kind===`chat_identifier`)return`chat_identifier:${n.chatIdentifier}`;return`handle:${n.handle}`});return resolveAllowlistMatchByCandidates({allowList:s,candidates:[{value:n?`handle:${n}`:void 0,source:`handle`},{value:i!==void 0?`chat_id:${i}`:void 0,source:`chat_id`},{value:a?`chat_guid:${a}`:void 0,source:`chat_guid`},{value:o?`chat_identifier:${o}`:void 0,source:`chat_identifier`}]}).allowed}function stripPrefix(e,t){return e.slice(t.length).trim()}function resolveServicePrefixedTarget(e){for(const{prefix:t,service:n}of e.servicePrefixes){if(!e.lower.startsWith(t))continue;const r=stripPrefix(e.trimmed,t);if(!r)throw new Error(`${t} target is required`);const i=normalizeLowercaseStringOrEmpty(r);if(e.isChatTarget(i))return e.parseTarget(r);return{kind:`handle`,to:r,service:n}}return null}function resolveServicePrefixedChatTarget(e){const t=[...e.chatIdPrefixes,...e.chatGuidPrefixes,...e.chatIdentifierPrefixes,...e.extraChatPrefixes??[]];return resolveServicePrefixedTarget({...e,isChatTarget:e=>t.some(t=>e.startsWith(t))})}function parseChatTargetPrefixesOrThrow(e){return parseChatTargetPrefixes(e,true)}function parseChatTargetPrefixes(e,t){for(const[n,r]of[[`chat_id`,e.chatIdPrefixes],[`chat_guid`,e.chatGuidPrefixes],[`chat_identifier`,e.chatIdentifierPrefixes]])for(const i of r){if(!e.lower.startsWith(i))continue;const r=stripPrefix(e.trimmed,i);if(n===`chat_id`){const e=parseStrictInteger(r);if(e!==void 0)return{kind:n,chatId:e}}else if(r)return n===`chat_guid`?{kind:n,chatGuid:r}:{kind:n,chatIdentifier:r};if(t)throw new Error(n===`chat_id`?`Invalid chat_id: ${r}`:`${n} is required`)}return null}function resolveServicePrefixedAllowTarget(e){for(const{prefix:t}of e.servicePrefixes){if(!e.lower.startsWith(t))continue;const n=stripPrefix(e.trimmed,t);if(!n)return{kind:`handle`,handle:``};return e.parseAllowTarget(n)}return null}function resolveServicePrefixedOrChatAllowTarget(e){const t=resolveServicePrefixedAllowTarget(e);if(t)return t;return parseChatAllowTargetPrefixes(e)}function createAllowedChatSenderMatcher(e){return t=>isAllowedParsedChatSender({...t,allowConversationTargets:t.allowConversationTargets??e.allowConversationTargets??false,normalizeSender:e.normalizeSender,parseAllowTarget:e.parseAllowTarget})}function parseChatAllowTargetPrefixes(e){return parseChatTargetPrefixes(e,false)}function stripChannelTargetPrefix(e,...t){const n=e.trim();for(const e of t){const t=`${normalizeLowercaseStringOrEmpty(e)}:`;if(normalizeLowercaseStringOrEmpty(n).startsWith(t))return n.slice(t.length).trim()}return n}function stripTargetKindPrefix(e){return e.replace(/^(user|channel|group|conversation|room|dm):/i,``).trim()}export{resolveServicePrefixedAllowTarget as a,resolveServicePrefixedTarget as c,compileAllowlist as d,formatAllowlistMatchMeta as f,resolveCompiledAllowlistMatch as g,resolveAllowlistMatchSimple as h,parseChatTargetPrefixesOrThrow as i,stripChannelTargetPrefix as l,resolveAllowlistMatchByCandidates as m,isAllowedParsedChatSender as n,resolveServicePrefixedChatTarget as o,resolveAllowlistCandidates as p,parseChatAllowTargetPrefixes as r,resolveServicePrefixedOrChatAllowTarget as s,createAllowedChatSenderMatcher as t,stripTargetKindPrefix as u};