openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
5 lines • 8.38 kB
JavaScript
import{t as stableStringify}from"./stable-stringify-Cy0VZ0ci.mjs";import{r as STATE_DIR,t as CONFIG_PATH}from"./paths-D_FOvGon.mjs";import{i as normalizeMainKey,r as isIncognitoSessionKey}from"./session-key-DYtwLLn1.mjs";import{N as parseAgentSessionKey}from"./session-key-BPgKfU2m.mjs";import{h as readRuntimeConfig,i as getRuntimeConfigAppliedHash}from"./runtime-snapshot-Bg5K4JNu.mjs";import{t as GATEWAY_CLIENT_CAPS}from"./client-info-Dr3-kuD1.mjs";import{o as resolveAgentEffectiveModelPrimary}from"./agent-scope-Dw5XRSEh.mjs";import{o as READ_SCOPE,s as SESSION_READ_SCOPE}from"./operator-scopes-bEvB7E5X.mjs";import"./io-Di_yuX3t.mjs";import{l as getGatewaySuspendAdmissionPhase}from"./gateway-work-admission-CnMW3_qr.mjs";import{n as operatorScopeSatisfied}from"./operator-scope-compat-TmgAKmD2.mjs";import{r as resolveAgentMainSessionKey}from"./main-session-C6FGqx0x.mjs";import{n as getSessionActorStorageBinding,t as captureSessionActorStorageOwner}from"./session-actor-storage-binding-Bq_IHhu0.mjs";import{o as resolveSessionEventAgentScope}from"./session-request-agent-DmeXmT1P.mjs";import"./sessions-DQD7zQ95.mjs";import{r as listSystemPresence}from"./system-presence-BtA5jBNR.mjs";import{n as getUpdateSchedule,t as getUpdateAvailable}from"./update-status-state-BIsSkC8u.mjs";import{n as resolveGatewayAgentSelectionState}from"./agent-list-D13wtniS.mjs";import{c as projectUpdateAvailable}from"./events-Cmrg6oec.mjs";import{a as isGatewayClientProfilePending}from"./gateway-client-identity-H5JO7081.mjs";import{d as isGatewayAdmin}from"./session-sharing-policy-CQ0Zqotl.mjs";import{o as prepareProjectedSessionSharing}from"./session-sharing-read-Cm-EAfc6.mjs";import"./session-sharing-BmRcWi9h.mjs";import{r as HEALTH_REFRESH_INTERVAL_MS}from"./server-constants-BmIepgnr.mjs";
//!src/gateway/presence-projection.ts
function projectPersonPresence(e,t){const n=e.user?.identity?{id:e.user.id,identity:e.user.identity,...e.user.name?{name:e.user.name}:{},...e.user.avatarUrl?{avatarUrl:e.user.avatarUrl}:{}}:void 0;const r=n?e.watchedSessions?.filter(e=>t.has(e)):void 0;return{ts:e.ts,text:``,...e.reason===`disconnect`?{reason:e.reason}:{},...n&&e.lastActivityAt!==void 0?{lastActivityAt:e.lastActivityAt}:{},...n?{user:n}:{},...r?.length?{watchedSessions:r}:{}}}function createPresenceRecipientProjection(e){const t=[...new Set(e.presence.flatMap(e=>e.watchedSessions??[]))];const n=getSessionActorStorageBinding({});const r=/* @__PURE__ */ new Map;for(const i of t){if(!isIncognitoSessionKey(i))continue;const t=resolveSessionEventAgentScope(e.cfg,i,parseAgentSessionKey(i)?.agentId)?.[1];if(t&&!r.has(t))r.set(t,captureSessionActorStorageOwner({agentId:t,sessionKey:i,sessionActor:n},{assertCurrent(){},authorize(){}}))}const i=/* @__PURE__ */ new Map;const a=/* @__PURE__ */ new Map;const o=t.some(isIncognitoSessionKey);let s;let c=[];let l;const u=/* @__PURE__ */ new Map;const resolveTarget=(t,i,a)=>{if(isIncognitoSessionKey(i)){n?.actor.assertReadable();const e=r.get(a);const t=e?.owner?.readSession(i,e.authority)?.entry;return t?{canonicalKey:i,entry:t}:void 0}if(!u.has(t)){const n=e.projection?.sharingTargetState({key:i,agentId:a});u.set(t,n?.status===`ready`?n.target:null)}return u.get(t)};return n=>{if(!n?.connect||(n.connect.role??`operator`)!==`operator`)return[];const r=n.connect.scopes??[];const d=operatorScopeSatisfied(READ_SCOPE,r);if(!d&&!operatorScopeSatisfied(`operator.sessions.read`,r))return[];const f=d?`full`:`person`;const p=e.projection?.getPolicyConfig()??e.cfg;if(s!==p){s=p;c=t.map(e=>{const t=parseAgentSessionKey(e);const n=t?.rest===`global`||t?.rest===`unknown`?t.rest:e;return{sessionKey:e,key:n,agentId:resolveSessionEventAgentScope(p,n,t?.agentId)?.[1]}});u.clear();a.clear()}const m=e.projection?.sharingRevision;if(l!==m||m===void 0){l=m;u.clear();a.clear()}const h=isGatewayAdmin(n)||!isGatewayClientProfilePending(n);const g=prepareProjectedSessionSharing({cfg:p,client:n,isMember:(t,n)=>e.projection?.hasMembership(t.storePath,t.storeKey,n)??false});const _=m&&!o?`${f}:${g.getCacheKey()}`:void 0;const v=_===void 0?void 0:a.get(_);if(v)return v;const y=/* @__PURE__ */ new Set;const b=[];if(h)for(const[e,{sessionKey:t,key:n,agentId:r}]of c.entries()){if(!r)continue;const i=resolveTarget(t,n,r);if(i&&(`storePath`in i?g.canReadTarget(i):g.entryFilter?.(i.canonicalKey,i.entry)??true)){y.add(t);b.push(e)}}const x=`${f}:${b.join(`,`)}`;let S=i.get(x);if(!S){S=e.presence.map(e=>{if(!d)return projectPersonPresence(e,y);if(!e.watchedSessions)return e;const t=e.watchedSessions.filter(e=>y.has(e));const{watchedSessions:n,...r}=e;return t.length?{...r,watchedSessions:t}:r});i.set(x,S)}if(_!==void 0)a.set(_,S);return S}}
//!src/gateway/server/health-state.ts
let presenceVersion=1;let healthVersion=1;let healthCache=null;let healthDigest;let broadcastHealthUpdate=null;const healthRefreshStates={public:{passive:null,probe:null},admin:{passive:null,probe:null}};function buildGatewaySnapshot(e){const t=readRuntimeConfig();const n=resolveGatewayAgentSelectionState(t);const r=n.defaultId;const i=normalizeMainKey(t.session?.mainKey);const a=t.session?.scope??`per-sender`;const o=a===`global`?`global`:resolveAgentMainSessionKey({cfg:t,agentId:r});const s=listSystemPresence({includeConnectionId:e.client?.connId});const c=createPresenceRecipientProjection({cfg:t,presence:e.client?.connect.caps?.includes(GATEWAY_CLIENT_CAPS.HELLO_PRESENCE_SELF)?s.filter(t=>t.connectionId===e.client?.connId):s,projection:e.sessionRowProjection})(e.client);const l=Math.round(process.uptime()*1e3);const u=e.includeUpdateDetails===true;const d=projectUpdateAvailable(getUpdateAvailable(),u)??void 0;const f=u?getUpdateSchedule()??void 0:void 0;const p=getRuntimeConfigAppliedHash();const m={suspension:{phase:getGatewaySuspendAdmissionPhase()},presence:c,health:{},stateVersion:{presence:presenceVersion,health:healthVersion},uptimeMs:l,appliedConfigHash:p?e.revisionProjector.projectResolvedHash(p):null,sessionDefaults:{defaultAgentId:r,modelConfigured:Boolean(resolveAgentEffectiveModelPrimary(t,r)),ownership:n.ownership,selectionRequired:n.selectionRequired,mainKey:i,mainSessionKey:o,scope:a},updateAvailable:d,updateSchedule:f};if(e.includeSensitive===true){m.configPath=CONFIG_PATH;m.stateDir=STATE_DIR}return m}function getHealthCache(){return healthCache}function getHealthVersion(){return healthVersion}function incrementPresenceVersion(){presenceVersion+=1;return presenceVersion}function getPresenceVersion(){return presenceVersion}function setBroadcastHealthUpdate(e){broadcastHealthUpdate=e}function healthEventDigest(e){const channel=({authAgeMs:e,lastProbeAt:t,lastInboundAt:n,lastOutboundAt:r,lastTransportActivityAt:i,lastMessageAt:a,lastEventAt:o,lastRunActivityAt:s,busy:c,activeRuns:l,activeRunStartedAt:u,accounts:d,...f})=>({...f,...d?{accounts:Object.fromEntries(Object.entries(d).map(([e,t])=>[e,channel(t)]))}:{}});const t=e.eventLoop?.degraded===true&&(e.eventLoop.degradedSinceMs??0)>=6e4;return stableStringify({...e,ts:void 0,durationMs:void 0,eventLoop:e.eventLoop?{degraded:t,reasons:t?e.eventLoop.reasons:[]}:void 0,channels:Object.fromEntries(Object.entries(e.channels).map(([e,t])=>[e,channel(t)])),agents:e.agents.map(({sessions:e,...t})=>t),sessions:void 0})}async function refreshGatewayHealthSnapshot(e){const t=e?.includeSensitive===true;const n=t?`admin`:`public`;const r=healthRefreshStates[n];const i=e?.probe===false?`passive`:`probe`;const a=i===`passive`?r.probe??r.passive:r.probe;if(a)return a;const o=(async()=>{const{collectGatewayHealthSnapshot:r}=await import(`./collector-DuJbkjfJ.mjs`);let a;try{a=e?.getRuntimeSnapshot?.()}catch{a=void 0}const o=e?.getConfigReloaderHotReloadStatus?.();const s=await r({audience:n,probe:i===`probe`,runtimeSnapshot:a,cronState:e?.getCronState?.(),...o?{configReloadHotReloadStatus:o}:{},...e?.getSessionRowProjection?{sessionRowProjection:e.getSessionRowProjection()}:{}});const c=e?.getEventLoopHealth?.();if(c)s.eventLoop=c;if(!t){const e=healthEventDigest(s);healthCache=s;if(e!==healthDigest){healthDigest=e;healthVersion+=1;broadcastHealthUpdate?.(s)}}return s})().finally(()=>{r[i]=null});r[i]=o;return o}export{incrementPresenceVersion as a,createPresenceRecipientProjection as c,getPresenceVersion as i,getHealthCache as n,refreshGatewayHealthSnapshot as o,getHealthVersion as r,setBroadcastHealthUpdate as s,buildGatewaySnapshot as t};