UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

3 lines • 2.24 kB
import{a as resolveGlobalSet}from"./global-singleton-DolL9xGt.mjs";import{t as createSubsystemLogger}from"./subsystem-DLCDFvCH.mjs";import{t as isPluginJsonValue}from"./host-hook-json-C9BOL2Yx.mjs"; //!src/plugins/gateway-events.ts const log=createSubsystemLogger(`plugins`);const sessionsChangedHandlers=resolveGlobalSet(Symbol.for(`openclaw.pluginSessionsChangedHandlers`),`plugin-registry`);function createPluginServiceGatewayEvents({pluginId:e,broadcast:t,lease:n}){if(!t)return;return{emit:(r,i,a)=>{n.assertActive(`gateway event emitter`);if(!/^[a-z][a-z0-9_-]*$/u.test(r))throw new Error(`invalid plugin gateway event name: ${r}`);if(!isPluginJsonValue(i))throw new Error(`plugin gateway event payload must be bounded JSON`);if(a?.scope!==`operator.read`&&a?.scope!==`operator.write`&&a?.scope!==`operator.admin`)throw new Error(`plugin gateway event scope must be an operator scope`);t(`plugin.${e}.${r}`,i,a.scope)},onSessionsChanged:e=>{n.assertActive(`gateway event subscriber`);return n.retain(subscribePluginSessionsChanged(e))}}}function subscribePluginSessionsChanged(e){const subscription=t=>e(t);sessionsChangedHandlers.add(subscription);return()=>{sessionsChangedHandlers.delete(subscription)}}function hasPluginSessionsChangedSubscribers(){return sessionsChangedHandlers.size>0}function queuePluginSessionsChanged(e){if(!e||typeof e!==`object`||Array.isArray(e))return;const t=e;if(typeof t.sessionKey!==`string`||t.sessionKey.length===0)return;if(sessionsChangedHandlers.size===0)return;const n=[...sessionsChangedHandlers];const r={sessionKey:t.sessionKey,...typeof t.agentId===`string`?{agentId:t.agentId}:{},...typeof t.label===`string`?{label:t.label}:{},...typeof t.displayName===`string`?{displayName:t.displayName}:{},...typeof t.reason===`string`?{reason:t.reason}:{},...typeof t.phase===`string`?{phase:t.phase}:{}};queueMicrotask(()=>{for(const e of n){if(!sessionsChangedHandlers.has(e))continue;try{const t=e(r);Promise.resolve(t).catch(e=>{log.warn(`plugin sessions.changed handler failed: ${String(e)}`)})}catch(e){log.warn(`plugin sessions.changed handler failed: ${String(e)}`)}}})}export{hasPluginSessionsChangedSubscribers as n,queuePluginSessionsChanged as r,createPluginServiceGatewayEvents as t};