UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

1 lines • 3.32 kB
import{gn as e,hn as t,pn as n}from"./openclaw-state-read-subsystem-Dwoo5bcz.mjs";import{n as r}from"./openclaw-state-read-agent-id-DmscQLD6.mjs";import{a as i,n as a}from"./openclaw-state-read-src-4Au2Uzuv.mjs";const o=/(^|:)signal:group:([^:]+)/gi,s=/^(?:(?:agent:[^:]*:)+:*)?matrix:(?:channel|group):/i;function casePreservingPeerSpan(e,t){let r=n(e),i=n(t);if(r===`signal`&&i===`group`)return`segment`;if(r===`matrix`&&(i===`channel`||i===`group`))return`tail`}function matrixEventThreadIdStart(e){let t=e.toLowerCase().lastIndexOf(`:thread:`),n=t+8;return t>=0&&e[n]===`$`?n:-1}function requiresFoldedSessionKeyAliasProof(n){let r=e(n);if(!r)return!1;if(matrixEventThreadIdStart(r)>=0)return!0;let i=r.split(`:`),a=0,o=!1;for(;i.length-a>=3&&t(i[a])===`agent`;)o=!0,a+=2;if(o)for(;a<i.length&&!e(i[a]);)a+=1;return casePreservingPeerSpan(i[a],i[a+1])===`tail`}const c=/* @__PURE__ */ new Map;function writeNormalizedSessionKeyCache(e,t){if(!(e.length>4096)&&(c.set(e,t),c.size>2048)){let e=c.keys().next();e.done||c.delete(e.value)}}function collectCasePreservedSpans(e){let t=[],r=matrixEventThreadIdStart(e);r>=0&&t.push({start:r,end:e.length,trim:!1});for(let n of e.matchAll(o)){let e=n[0],r=n[2]??``,i=n.index+e.length-r.length;t.push({start:i,end:i+r.length,trim:!0})}let i=s.exec(e);if(!i||i[0].length>=e.length)return t;let a=i[0].length,c=e.slice(a),l=n(c).lastIndexOf(`:thread:`);if(l===-1)return t.push({start:a,end:e.length,trim:!1}),t;t.push({start:a,end:a+l,trim:!1});let u=a+l+8;return u<e.length&&t.push({start:u,end:e.length,trim:!1}),t}function normalizeSessionKeyPreservingOpaquePeerIds(t){let r=e(t);if(!r)return``;let i=c.get(r);if(i!==void 0)return i;let a=r.toLowerCase();if(!a.includes(`signal:`)&&!a.includes(`matrix:`)&&!a.includes(`:thread:$`))return a;let o=collectCasePreservedSpans(r).filter(e=>e.end>e.start).toSorted((e,t)=>e.start-t.start),s=``,l=0;for(let e of o){if(e.start<l)continue;s+=n(r.slice(l,e.start));let t=r.slice(e.start,e.end);s+=e.trim?t.trim():t,l=e.end}return s+=n(r.slice(l)),writeNormalizedSessionKeyCache(r,s),s}function parseAgentSessionKey(e){return i(normalizeSessionKeyPreservingOpaquePeerIds(e))}const l=`main`;function toAgentStoreSessionKey(e){let t=(e.requestKey??``).trim(),i=n(t);if(!t||i===`main`)return a({agentId:e.agentId,mainKey:e.mainKey});let o=parseAgentSessionKey(t);if(o)return`agent:${o.agentId}:${o.rest}`;let s=normalizeSessionKeyPreservingOpaquePeerIds(t);return i.startsWith(`agent:`)?s:`agent:${r(e.agentId)}:${s}`}function resolveAgentIdFromSessionKey(e,t){let n=parseAgentSessionKey(e);if(n?.agentId)return r(n.agentId);if(classifySessionKeyShape(e)===`malformed_agent`)throw Error(`Malformed agent session key; refusing default-agent resolution.`);let i=t?.trim();if(i)return r(i);throw Error(`Session key does not contain an agent id; resolve it with the configured default agent.`)}function classifySessionKeyShape(e){let t=(e??``).trim();return t?parseAgentSessionKey(t)?`agent`:n(t).startsWith(`agent:`)?`malformed_agent`:`legacy_or_alias`:`missing`}function sanitizeAgentId(e){return r(e)}export{toAgentStoreSessionKey as a,requiresFoldedSessionKeyAliasProof as c,sanitizeAgentId as i,classifySessionKeyShape as n,normalizeSessionKeyPreservingOpaquePeerIds as o,resolveAgentIdFromSessionKey as r,parseAgentSessionKey as s,l as t};