UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

5 lines • 4.12 kB
import{c as normalizeOptionalLowercaseString,l as normalizeOptionalString,o as normalizeLowercaseStringOrEmpty}from"./string-coerce-CpTFcSwY.mjs";import{b as uniqueStrings,f as normalizeStringEntries}from"./string-normalization-D_oBUpO3.mjs";import{t as dedupeByKey}from"./dedupe-by-key-Bdyq9B5M.mjs";import{t as summarizeStringEntries}from"./string-sample-Bbl8b2yp.mjs";import"./chat-target-prefixes-D9B8D83t.mjs"; //!src/channels/allowlists/resolve-utils.ts function dedupeAllowlistEntries(e,t=normalizeLowercaseStringOrEmpty){return dedupeByKey(e.map(e=>e.trim()).filter(Boolean),t)}function mergeAllowlist(e){return dedupeAllowlistEntries([...(e.existing??[]).map(String),...e.additions])}function buildAllowlistResolutionSummary(e,t){const n=new Map(e.map(e=>[e.input,e]));const resolvedOk=e=>Boolean(e.resolved&&e.id);const r=t?.formatResolved??(e=>e.id===e.input?null:`${e.input}→${e.id}`);const i=t?.formatUnresolved??(e=>e.input);const a=e.filter(resolvedOk).map(r).filter(e=>e!==null);const o=e.filter(resolvedOk).map(e=>e.id).filter(e=>Boolean(e));return{resolvedMap:n,mapping:a,unresolved:e.filter(e=>!resolvedOk(e)).map(i),additions:o}}function canonicalizeAllowlistWithResolvedIds(e){const t=[];for(const n of e.existing??[]){const r=normalizeOptionalString(n)??``;if(!r)continue;if(r===`*`){t.push(r);continue}const i=e.resolvedMap.get(r);t.push(i?.resolved&&i.id?i.id:r)}return dedupeAllowlistEntries(t,e.entryKey)}function patchAllowlistUsersInConfigEntries(e){const t={...e.entries};for(const[n,r]of Object.entries(e.entries)){if(!r||typeof r!==`object`)continue;const i=r.users;if(!Array.isArray(i)||i.length===0)continue;let a;if(e.strategy===`canonicalize`)a=canonicalizeAllowlistWithResolvedIds({existing:i,resolvedMap:e.resolvedMap,entryKey:e.entryKey});else{const t=e.resolvedMap;const n=[];for(const e of i){const r=normalizeOptionalString(e)??``;const i=t.get(r);if(i?.resolved&&i.id)n.push(i.id)}a=mergeAllowlist({existing:i,additions:n})}t[n]={...r,users:a}}return t}function addAllowlistUserEntriesFromConfigEntry(e,t){if(!t||typeof t!==`object`)return;const n=t.users;if(!Array.isArray(n))return;for(const t of n){const n=normalizeOptionalString(t)??``;if(n&&n!==`*`)e.add(n)}}function summarizeMapping(e,t,n,r){if(t.length>0)r.log?.(`${e} resolved: ${summarizeStringEntries({entries:t,limit:6})}`);if(n.length>0)r.log?.(`${e} unresolved: ${summarizeStringEntries({entries:n,limit:6})}`)} //!src/plugin-sdk/allow-from.ts function formatAllowFromLowercase(e){return normalizeStringEntries(e.allowFrom).map(t=>e.stripPrefixRe?t.replace(e.stripPrefixRe,``):t).map(e=>normalizeOptionalLowercaseString(e)).filter(e=>Boolean(e))}function formatNormalizedAllowFromEntries(e){return normalizeStringEntries(e.allowFrom).map(t=>e.normalizeEntry(t)).filter(e=>Boolean(e))}function parseAllowFromEntries(e,t){const n=[];for(const r of normalizeStringEntries(e.split(/[\n,;]+/g))){if(r===`*`){n.push(r);continue}const e=t(r);if(`error`in e)return{entries:[],error:e.error};n.push(e.value)}return{entries:uniqueStrings(normalizeStringEntries(n))}}async function resolveBasicAllowFromEntries(e){const t=e.token?.trim();if(!t)return e.entries.map(e=>({input:e,resolved:false,id:null}));return(await e.resolveEntries({token:t,entries:e.entries})).map(e=>({input:e.input,resolved:e.resolved,id:e.id??null}))}function isNormalizedSenderAllowed(e){const t=formatAllowFromLowercase({allowFrom:e.allowFrom,stripPrefixRe:e.stripPrefixRe});if(t.length===0)return false;if(t.includes(`*`))return true;const n=normalizeOptionalLowercaseString(String(e.senderId));return n?t.includes(n):false}async function mapAllowlistResolutionInputs(e){const t=[];for(const n of e.inputs)t.push(await e.mapInput(n));return t}export{parseAllowFromEntries as a,buildAllowlistResolutionSummary as c,patchAllowlistUsersInConfigEntries as d,summarizeMapping as f,mapAllowlistResolutionInputs as i,canonicalizeAllowlistWithResolvedIds as l,formatNormalizedAllowFromEntries as n,resolveBasicAllowFromEntries as o,isNormalizedSenderAllowed as r,addAllowlistUserEntriesFromConfigEntry as s,formatAllowFromLowercase as t,mergeAllowlist as u};