@easbot/gateway 0.2.11 → 0.2.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/chunks/{chunk-Y7UBDKER.cjs → chunk-72MPR4K5.cjs} +1 -1
- package/dist/chunks/chunk-77F43CRD.mjs +2 -0
- package/dist/chunks/{chunk-34MFQLJP.cjs → chunk-ASIAS35P.cjs} +2 -2
- package/dist/chunks/chunk-DJTIL3AB.mjs +2 -0
- package/dist/chunks/chunk-ENCE7NRO.mjs +1 -0
- package/dist/chunks/chunk-RR6UFR5U.cjs +2 -0
- package/dist/chunks/chunk-ULWP4SK3.cjs +1 -0
- package/dist/chunks/{chunk-3UUTBBIX.mjs → chunk-X2DH5PDZ.mjs} +1 -1
- package/dist/chunks/{global-ZNJCD2DC.mjs → global-F6KU7V3U.mjs} +1 -1
- package/dist/chunks/{global-LIE6RHEM.cjs → global-NMLJCTEY.cjs} +1 -1
- package/dist/chunks/package-DXSVCCMB.cjs +1 -0
- package/dist/chunks/package-JSK7ZEYD.mjs +1 -0
- package/dist/chunks/server-CPD2KBY7.cjs +1 -0
- package/dist/chunks/server-TLTUACMN.mjs +1 -0
- package/dist/cli.cjs +2 -2
- package/dist/cli.mjs +1 -1
- package/dist/index.cjs +8 -8
- package/dist/index.d.cts +8 -3
- package/dist/index.d.ts +8 -3
- package/dist/index.mjs +2 -2
- package/package.json +5 -5
- package/dist/chunks/chunk-4PHQN25U.cjs +0 -2
- package/dist/chunks/chunk-AY3AGYW4.mjs +0 -1
- package/dist/chunks/chunk-K2WP2GA3.mjs +0 -2
- package/dist/chunks/chunk-RQJCZJCM.cjs +0 -1
- package/dist/chunks/chunk-YU4JG7OA.mjs +0 -2
- package/dist/chunks/package-DLCORTTP.mjs +0 -1
- package/dist/chunks/package-ICSXKMHH.cjs +0 -1
- package/dist/chunks/server-C5W7D4CU.mjs +0 -1
- package/dist/chunks/server-HQL7QNZY.cjs +0 -1
package/dist/index.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export{a as Gateway}from'./chunks/chunk-AY3AGYW4.mjs';import {j,k as k$1,c as c$1,r}from'./chunks/chunk-K2WP2GA3.mjs';export{q as ChannelPluginLoader,j as DEFAULT_AGENT_REGISTRY_CONFIG,l as DEFAULT_CONNECTION_POOL_CONFIG,k as DEFAULT_SYNC_CONFIG,i as DEFAULT_TOKEN_AUTH_CONFIG,h as DEFAULT_WEBSOCKET_SERVER_CONFIG,s as GatewayServer,p as GatewaySessionManager,a as MessageLockManager,b as MessageRetryManager,m as MessageRouter,n as MessageStore,o as SessionStore,f as createDefaultSessionState,c as createGatewayMessage,d as createTextMessage,e as generateSessionId,g as generateSubscriptionId}from'./chunks/chunk-K2WP2GA3.mjs';export{p as AgentRegistryConfigSchema,k as AgentSyncConfigSchema,j as AuthConfigSchema,h as ChannelConfigSchema,o as CircuitBreakerConfigSchema,m as ConnectionPoolConfigSchema,c as FeishuChannelConfigSchema,l as GatewayClusterConfigSchema,s as GatewayConfigSchema,r as GatewayServerConfigSchema,i as HTTPSConfigSchema,n as MessageQueueConfigSchema,g as NostrChannelConfigSchema,q as SessionConfigSchema,f as SignalChannelConfigSchema,b as SlackChannelConfigSchema,a as TelegramChannelConfigSchema,d as WeChatChannelConfigSchema,e as WebChatChannelConfigSchema,z as clearConfigCache,G as getAgentRegistryConfig,J as getAuthConfig,C as getChannelConfig,F as getCircuitBreakerConfig,K as getClusterConfig,x as getConfig,A as getConfigDirectory,D as getConnectionPoolConfig,L as getDefaultAgent,I as getHTTPSConfig,M as getLogLevel,E as getMessageQueueConfig,B as getServerConfig,H as getSessionConfig,y as isConfigLoaded,w as loadConfig,u as parsePartialConfig,t as validateConfig}from'./chunks/chunk-YU4JG7OA.mjs';import {a}from'./chunks/chunk-6TRL3CVJ.mjs';import {a as a$1}from'./chunks/chunk-HAMGVOQD.mjs';var f=a.create({service:"gateway:agent-registry"}),k=class{constructor(n={}){a$1(this,"config");a$1(this,"agents",new Map);a$1(this,"heartbeatTimer",null);a$1(this,"roundRobinIndex",0);this.config={...j,...n};}async register(n){if(this.agents.has(n.id))throw new Error(`Agent with ID "${n.id}" is already registered`);if(this.agents.size>=this.config.maxAgents)throw new Error(`Maximum number of agents (${this.config.maxAgents}) reached`);let e=Date.now(),t={...n,status:"healthy",lastHeartbeat:e,registeredAt:e,connectionCount:0};this.agents.set(n.id,t),f.info("Agent registered",{agentId:n.id,name:n.name,capabilities:n.capabilities,address:n.address,totalAgents:this.agents.size});}async deregister(n){let e=this.agents.get(n);if(!e){f.warn("Attempted to deregister unknown agent",{agentId:n});return}this.agents.delete(n),f.info("Agent deregistered",{agentId:n,name:e.name,totalAgents:this.agents.size});}async heartbeat(n,e){let t=this.agents.get(n);if(!t)throw f.warn("Heartbeat received from unknown agent",{agentId:n}),new Error(`Agent "${n}" is not registered`);t.lastHeartbeat=Date.now(),t.status="healthy",e&&(t.metadata={...t.metadata,...e}),f.debug("Agent heartbeat received",{agentId:n,lastHeartbeat:t.lastHeartbeat});}getAgent(n){return this.agents.get(n)}getAllAgents(){return Array.from(this.agents.values())}getHealthyAgents(){return this.getAllAgents().filter(n=>n.status==="healthy")}selectAgent(n){let{strategy:e,capabilities:t,healthyOnly:s=true}=n,i=s?this.getHealthyAgents():this.getAllAgents();if(t&&t.length>0&&(i=i.filter(o=>t.every(u=>o.capabilities.includes(u)))),i.length===0){f.warn("No agents available for selection",{strategy:e,capabilities:t,healthyOnly:s,totalAgents:this.agents.size});return}let a;switch(e){case "random":a=this.selectRandom(i);break;case "round-robin":a=this.selectRoundRobin(i);break;case "least-connections":a=this.selectLeastConnections(i);break;default:a=this.selectRandom(i);}return f.debug("Agent selected",{agentId:a.id,strategy:e,connectionCount:a.connectionCount}),a}incrementConnection(n){let e=this.agents.get(n);e&&(e.connectionCount++,f.debug("Agent connection incremented",{agentId:n,connectionCount:e.connectionCount}));}decrementConnection(n){let e=this.agents.get(n);e&&e.connectionCount>0&&(e.connectionCount--,f.debug("Agent connection decremented",{agentId:n,connectionCount:e.connectionCount}));}startHeartbeatCheck(){if(this.heartbeatTimer){f.warn("Heartbeat check is already running");return}f.info("Starting heartbeat check",{interval:this.config.heartbeatCheckInterval,timeout:this.config.heartbeatTimeout}),this.heartbeatTimer=setInterval(()=>this.checkHeartbeats(),this.config.heartbeatCheckInterval);}stopHeartbeatCheck(){this.heartbeatTimer&&(clearInterval(this.heartbeatTimer),this.heartbeatTimer=null,f.info("Heartbeat check stopped"));}checkHeartbeats(){let n=Date.now(),{heartbeatTimeout:e,autoDeregisterTimeout:t}=this.config,s=[],i=0;for(let[a,o]of this.agents.entries()){let u=n-o.lastHeartbeat;if(u>t){s.push(a);continue}u>e&&o.status==="healthy"&&(o.status="unhealthy",i++,f.warn("Agent marked as unhealthy due to heartbeat timeout",{agentId:a,lastHeartbeat:o.lastHeartbeat,timeSinceLastHeartbeat:u,timeout:e}));}for(let a of s){let o=this.agents.get(a);o&&(f.info("Auto-deregistering agent due to timeout",{agentId:a,name:o.name,lastHeartbeat:o.lastHeartbeat,timeout:t}),this.agents.delete(a));}(i>0||s.length>0)&&f.info("Heartbeat check completed",{unhealthyCount:i,deregisteredCount:s.length,totalAgents:this.agents.size});}selectRandom(n){let e=Math.floor(Math.random()*n.length);return n[e]}selectRoundRobin(n){this.roundRobinIndex=this.roundRobinIndex%n.length;let e=n[this.roundRobinIndex];return this.roundRobinIndex++,e}selectLeastConnections(n){return n.reduce((e,t)=>t.connectionCount<e.connectionCount?t:e)}getStats(){let n=this.getAllAgents();return {totalAgents:n.length,healthyAgents:n.filter(e=>e.status==="healthy").length,unhealthyAgents:n.filter(e=>e.status==="unhealthy").length,offlineAgents:n.filter(e=>e.status==="offline").length,totalConnections:n.reduce((e,t)=>e+t.connectionCount,0)}}setAgentStatus(n,e){let t=this.agents.get(n);t&&(t.status=e);}setLastHeartbeat(n,e){let t=this.agents.get(n);t&&(t.lastHeartbeat=e);}};var g=a.create({service:"gateway:agent-sync-manager"}),T=class{constructor(n){a$1(this,"config");a$1(this,"registry");a$1(this,"localNodeId");a$1(this,"localNodeName");a$1(this,"remoteNodes",new Map);a$1(this,"remoteAgents",new Map);a$1(this,"syncTimer",null);a$1(this,"running",false);a$1(this,"httpClient");this.registry=n.registry,this.localNodeId=n.localNodeId,this.localNodeName=n.localNodeName,this.config={...k$1,...n.config},this.httpClient=n.httpClient||{fetch:globalThis.fetch.bind(globalThis)};}async start(){if(this.running){g.warn("Agent sync manager is already running");return}g.info("Starting agent sync manager",{localNodeId:this.localNodeId,mode:this.config.mode,interval:this.config.interval,remoteNodes:this.config.remoteNodes.length});for(let n of this.config.remoteNodes)this.remoteNodes.set(n.id,{info:{id:n.id,name:n.id,address:n.address,port:n.port,lastSyncAt:0,status:"unknown"},lastSyncAt:0,status:"synced"});(this.config.mode==="pull"||this.config.mode==="both")&&(this.syncTimer=setInterval(()=>this.pullFromAllNodes(),this.config.interval)),this.running=true,g.info("Agent sync manager started");}async stop(){if(!this.running){g.warn("Agent sync manager is not running");return}g.info("Stopping agent sync manager"),this.syncTimer&&(clearInterval(this.syncTimer),this.syncTimer=null),this.remoteNodes.clear(),this.remoteAgents.clear(),this.running=false,g.info("Agent sync manager stopped");}async pullFromAllNodes(){for(let[n]of this.remoteNodes.entries())try{await this.pullFromNode(n);}catch(e){g.error("Failed to pull from node",{nodeId:n,error:e.message});}}async pullFromNode(n){let e=this.remoteNodes.get(n);if(!e)throw new Error(`Unknown remote node: ${n}`);e.status="syncing";let t=this.config.remoteNodes.find(s=>s.id===n);if(!t)throw new Error(`Remote node not found in config: ${n}`);g.debug("Pulling agent list from node",{nodeId:n,address:t.address});try{let s=`http://${t.address}:${t.port}/sync/agents`,i=await this.httpClient.fetch(s,{method:"GET",headers:{"Content-Type":"application/json","X-Gateway-Node-Id":this.localNodeId}});if(!i.ok)throw new Error(`HTTP ${i.status}: ${i.statusText}`);let o=(await i.json()).agents||[];for(let u of o)u.sourceGatewayId=n,this.remoteAgents.set(u.id,u);return e.lastSyncAt=Date.now(),e.status="synced",e.info.lastSyncAt=e.lastSyncAt,e.info.status="online",delete e.error,g.info("Pulled agent list from node",{nodeId:n,agentCount:o.length,lastSyncAt:e.lastSyncAt}),o}catch(s){throw e.status="error",e.error=s.message,e.info.status="offline",g.error("Failed to pull from node",{nodeId:n,error:s.message}),s}}async pushEvent(n){if(this.config.mode!=="push"&&this.config.mode!=="both")return;let e=[];for(let[t]of this.remoteNodes.entries())e.push(this.pushToNode(t,n));await Promise.allSettled(e);}async pushToNode(n,e){let t=this.config.remoteNodes.find(s=>s.id===n);if(!t){g.warn("Unknown remote node for push",{nodeId:n});return}try{let s=`http://${t.address}:${t.port}/sync/events`,i=await this.httpClient.fetch(s,{method:"POST",headers:{"Content-Type":"application/json","X-Gateway-Node-Id":this.localNodeId},body:JSON.stringify(e)});if(!i.ok)throw new Error(`HTTP ${i.status}: ${i.statusText}`);g.debug("Pushed event to node",{nodeId:n,eventType:e.type});}catch(s){g.error("Failed to push to node",{nodeId:n,eventType:e.type,error:s.message});}}async handleSyncMessage(n){switch(g.debug("Received sync message",{type:n.type,sourceNodeId:n.sourceNodeId}),n.type){case "agent_list_request":await this.handleAgentListRequest(n);break;case "agent_list_response":await this.handleAgentListResponse(n);break;case "agent_register":await this.handleAgentRegister(n);break;case "agent_deregister":await this.handleAgentDeregister(n);break;case "agent_heartbeat":await this.handleAgentHeartbeat(n);break;case "agent_status_change":await this.handleAgentStatusChange(n);break;default:g.warn("Unknown sync message type",{type:n.type});}}async handleAgentListRequest(n){let e=this.getLocalAgentsForSync(),t={type:"agent_list_response",sourceNodeId:this.localNodeId,timestamp:Date.now(),requestId:n.requestId,agents:e,fullList:true,syncAt:Date.now()};await this.pushToNode(n.sourceNodeId,t);}async handleAgentListResponse(n){for(let e of n.agents)this.remoteAgents.set(e.id,e);g.info("Received agent list response",{sourceNodeId:n.sourceNodeId,agentCount:n.agents.length});}async handleAgentRegister(n){let{agent:e}=n,t=this.remoteAgents.get(e.id);if(t){let s=this.resolveConflict(t,e);this.remoteAgents.set(e.id,s);}else this.remoteAgents.set(e.id,e);g.info("Remote agent registered",{agentId:e.id,name:e.name,sourceNodeId:n.sourceNodeId});}async handleAgentDeregister(n){let{agentId:e}=n;this.remoteAgents.delete(e),g.info("Remote agent deregistered",{agentId:e,sourceNodeId:n.sourceNodeId});}async handleAgentHeartbeat(n){let{agentId:e,heartbeatAt:t,status:s}=n,i=this.remoteAgents.get(e);i&&(i.updatedAt=t,i.status=s),g.debug("Remote agent heartbeat received",{agentId:e,status:s,sourceNodeId:n.sourceNodeId});}async handleAgentStatusChange(n){let{agentId:e,newStatus:t,changedAt:s}=n,i=this.remoteAgents.get(e);i&&(i.status=t,i.updatedAt=s),g.info("Remote agent status changed",{agentId:e,newStatus:t,sourceNodeId:n.sourceNodeId});}resolveConflict(n,e){switch(this.config.conflictResolution){case "latest":return e.updatedAt>n.updatedAt?e:n;case "local":return n;case "remote":return e;default:return e.updatedAt>n.updatedAt?e:n}}getLocalAgentsForSync(){return this.registry.getAllAgents().map(e=>({id:e.id,name:e.name,sourceGatewayId:this.localNodeId,address:e.address,capabilities:e.capabilities,status:e.status,updatedAt:e.lastHeartbeat,metadata:e.metadata}))}getMergedAgentList(){let n=new Map;for(let e of this.getLocalAgentsForSync())n.set(e.id,e);for(let[e,t]of this.remoteAgents.entries())n.has(e)||n.set(e,t);return Array.from(n.values())}getRemoteAgents(){return Array.from(this.remoteAgents.values())}getStats(){let n=this.registry.getAllAgents(),e=0,t=0;for(let i of this.remoteNodes.values())i.info.status==="online"?e++:t++;let s=null;for(let i of this.remoteNodes.values())i.lastSyncAt>(s||0)&&(s=i.lastSyncAt);return {localAgentCount:n.length,remoteAgentCount:this.remoteAgents.size,totalAgentCount:n.length+this.remoteAgents.size,onlineNodeCount:e,offlineNodeCount:t,lastSyncAt:s}}isRunning(){return this.running}addRemoteNode(n){this.remoteNodes.set(n.id,{info:{id:n.id,name:n.id,address:n.address,port:n.port,lastSyncAt:0,status:"unknown"},lastSyncAt:0,status:"synced"}),g.info("Remote node added",{nodeId:n.id,address:n.address});}removeRemoteNode(n){this.remoteNodes.delete(n);for(let[e,t]of this.remoteAgents.entries())t.sourceGatewayId===n&&this.remoteAgents.delete(e);g.info("Remote node removed",{nodeId:n});}};var c=class{constructor(){a$1(this,"version","1.0.0");a$1(this,"log");a$1(this,"config",null);a$1(this,"messageHandler",null);a$1(this,"_status","stopped");a$1(this,"lastActivity",0);}initLog(){this.log=a.create({service:`channel:${this.platform}`});}async start(n,e){if(this._status==="running"){this.log.warn("plugin is already running");return}this.log.info("starting channel plugin",{id:this.id,platform:this.platform}),this._status="starting",this.config=n,this.messageHandler=e;try{await this.doStart(),this._status="running",this.lastActivity=Date.now(),this.log.info("channel plugin started",{id:this.id});}catch(t){throw this._status="error",this.log.error("failed to start channel plugin",{error:String(t)}),t}}async stop(){if(this._status==="stopped"){this.log.warn("plugin is already stopped");return}this.log.info("stopping channel plugin",{id:this.id}),this._status="stopping";try{await this.doStop(),this._status="stopped",this.log.info("channel plugin stopped",{id:this.id});}catch(n){throw this._status="error",this.log.error("failed to stop channel plugin",{error:String(n)}),n}}async healthCheck(){try{return {healthy:await this.doHealthCheck(),lastCheck:Date.now()}}catch(n){return {healthy:false,lastCheck:Date.now(),error:n instanceof Error?n.message:String(n)}}}getStatus(){return {status:this._status,lastActivity:this.lastActivity}}async doHealthCheck(){return this._status==="running"}updateActivity(){this.lastActivity=Date.now();}async handleMessage(n){if(!this.messageHandler){this.log.warn("no message handler set, dropping message",{id:n.id});return}this.updateActivity(),await this.messageHandler.onMessage(n);}async handleEvent(n,e){if(!this.messageHandler){this.log.warn("no message handler set, dropping event",{type:n});return}await this.messageHandler.onEvent({type:n,channelId:this.id,data:e,timestamp:Date.now()});}};var x=class{constructor(n){a$1(this,"log");a$1(this,"config");a$1(this,"ws",null);a$1(this,"state","disconnected");a$1(this,"subscriptions",new Map);a$1(this,"messageCallbacks",new Set);a$1(this,"agentListCallbacks",new Set);a$1(this,"cachedAgentList",[]);a$1(this,"localAgentList",[]);a$1(this,"reconnectAttempts",0);a$1(this,"reconnectTimer",null);a$1(this,"heartbeatTimer",null);a$1(this,"agentListRequestId",null);a$1(this,"agentListRequestResolve",null);this.config={url:n.url,type:n.type,id:n.id??`client_${Date.now()}_${Math.random().toString(36).substring(2,9)}`,reconnect:{enabled:n.reconnect?.enabled??true,maxAttempts:n.reconnect?.maxAttempts??5,delay:n.reconnect?.delay??3e3},heartbeat:{enabled:n.heartbeat?.enabled??true,interval:n.heartbeat?.interval??3e4}},this.log=a.create({service:`gateway:client:${this.config.id}`});}async connect(){if(this.state==="connected"||this.state==="connecting"){this.log.warn("already connected or connecting");return}return this.state="connecting",this.log.info("connecting to gateway",{url:this.config.url}),new Promise((n,e)=>{try{this.ws=new WebSocket(this.config.url),this.ws.onopen=()=>{this.state="connected",this.reconnectAttempts=0,this.log.info("connected to gateway"),this.config.heartbeat.enabled&&this.startHeartbeat(),this.resubscribeAll(),n();},this.ws.onmessage=t=>{this.handleMessage(t.data);},this.ws.onclose=t=>{this.handleClose(t.code,t.reason);},this.ws.onerror=t=>{this.log.error("WebSocket error",{error:String(t)}),this.state==="connecting"&&e(new Error("Connection failed"));};}catch(t){this.state="disconnected",e(t);}})}async disconnect(){this.state!=="disconnected"&&(this.log.info("disconnecting from gateway"),this.stopHeartbeat(),this.stopReconnect(),this.ws&&(this.ws.close(1e3,"Client disconnect"),this.ws=null),this.state="disconnected",this.log.info("disconnected from gateway"));}async subscribe(n){if(this.state!=="connected")throw new Error("Not connected to gateway");this.sendData({type:"subscribe",sessionId:n,clientId:this.config.id}),this.log.info("subscribed to session",{sessionId:n});}async unsubscribe(n){if(this.state!=="connected")throw new Error("Not connected to gateway");this.sendData({type:"unsubscribe",sessionId:n,clientId:this.config.id}),this.subscriptions.delete(n),this.log.info("unsubscribed from session",{sessionId:n});}async send(n){if(this.state!=="connected")throw new Error("Not connected to gateway");this.sendData({type:"message",message:n});}onMessage(n){this.messageCallbacks.add(n);}offMessage(n){this.messageCallbacks.delete(n);}getState(){return this.state}getId(){return this.config.id}getSubscriptions(){return [...this.subscriptions.keys()]}setLocalAgents(n){this.localAgentList=n,this.updateMergedAgentList();}async fetchAgentList(){if(this.state!=="connected")throw new Error("Not connected to gateway");return new Promise((n,e)=>{let t=`req_${Date.now()}_${Math.random().toString(36).substring(2,9)}`;this.agentListRequestId=t,this.agentListRequestResolve=n,this.sendData({type:"agent_list_request",requestId:t}),setTimeout(()=>{this.agentListRequestId===t&&(this.agentListRequestId=null,this.agentListRequestResolve=null,e(new Error("Agent list request timeout")));},1e4);})}getConnectableAgents(n={}){let{localAgents:e,preferLocal:t=true,filterOffline:s=true}=n,i=e||this.localAgentList,a=this.cachedAgentList,o=this.mergeAgentLists(i,a,t);return s?o.filter(u=>u.status!=="offline"):o}onAgentListChange(n){this.agentListCallbacks.add(n);}offAgentListChange(n){this.agentListCallbacks.delete(n);}mergeAgentLists(n,e,t){let s=new Map,i=t?e:n,a=t?n:e;for(let o of i)s.set(o.id,o);for(let o of a)s.set(o.id,o);return Array.from(s.values())}updateMergedAgentList(){let n=this.getConnectableAgents({filterOffline:false});for(let e of this.agentListCallbacks)try{e(n);}catch(t){this.log.error("agent list callback error",{error:String(t)});}}handleAgentListResponse(n,e){e&&this.agentListRequestId===e&&this.agentListRequestResolve&&(this.agentListRequestId=null,this.agentListRequestResolve(n),this.agentListRequestResolve=null),this.cachedAgentList=n,this.updateMergedAgentList(),this.log.debug("received agent list",{count:n.length});}sendData(n){if(!this.ws||this.ws.readyState!==WebSocket.OPEN){this.log.warn("WebSocket not ready, cannot send");return}this.ws.send(JSON.stringify(n));}handleMessage(n){try{let e=JSON.parse(n);switch(e.type){case "message":this.handleGatewayMessage(e.message);break;case "subscribed":this.subscriptions.set(e.sessionId,e.subscription);break;case "unsubscribed":this.subscriptions.delete(e.sessionId);break;case "pong":break;case "agent_list_response":this.handleAgentListResponse(e.agents||[],e.requestId);break;case "agent_update":this.handleAgentUpdate(e.agent,e.action);break;default:this.log.debug("unknown message type",{type:e.type});}}catch(e){this.log.error("failed to parse message",{error:String(e)});}}handleAgentUpdate(n,e){switch(e){case "add":case "update":{let t=this.cachedAgentList.findIndex(s=>s.id===n.id);t>=0?this.cachedAgentList[t]=n:this.cachedAgentList.push(n);break}case "remove":this.cachedAgentList=this.cachedAgentList.filter(t=>t.id!==n.id);break}this.updateMergedAgentList();}handleGatewayMessage(n){for(let e of this.messageCallbacks)try{e(n);}catch(t){this.log.error("message callback error",{error:String(t)});}}handleClose(n,e){this.log.info("connection closed",{code:n,reason:e}),this.state="disconnected",this.ws=null,this.stopHeartbeat(),this.config.reconnect.enabled&&this.scheduleReconnect();}scheduleReconnect(){if(this.reconnectAttempts>=this.config.reconnect.maxAttempts){this.log.error("max reconnect attempts reached");return}this.reconnectAttempts++,this.state="reconnecting";let n=this.config.reconnect.delay*this.reconnectAttempts;this.log.info("scheduling reconnect",{attempt:this.reconnectAttempts,delay:n}),this.reconnectTimer=setTimeout(async()=>{try{await this.connect();}catch(e){this.log.error("reconnect failed",{error:String(e)});}},n);}stopReconnect(){this.reconnectTimer&&(clearTimeout(this.reconnectTimer),this.reconnectTimer=null);}startHeartbeat(){this.heartbeatTimer=setInterval(()=>{this.state==="connected"&&this.sendData({type:"ping"});},this.config.heartbeat.interval);}stopHeartbeat(){this.heartbeatTimer&&(clearInterval(this.heartbeatTimer),this.heartbeatTimer=null);}async resubscribeAll(){for(let n of this.subscriptions.keys())try{await this.subscribe(n);}catch(e){this.log.error("failed to resubscribe",{sessionId:n,error:String(e)});}}};var y=class extends c{constructor(e="telegram-main"){super();a$1(this,"id");a$1(this,"platform","telegram");a$1(this,"name","Telegram Bot");a$1(this,"botConfig",null);a$1(this,"pollingInterval",null);a$1(this,"lastUpdateId",0);a$1(this,"baseUrl","");this.id=e,this.initLog();}async doStart(){if(!this.config)throw new Error("Config not set");if(this.botConfig=this.config.platform,!this.botConfig.botToken)throw new Error("Bot token is required");this.baseUrl=`https://api.telegram.org/bot${this.botConfig.botToken}`;let e=await this.getMe();this.log.info("Telegram bot connected",{id:e.id,username:e.username,first_name:e.first_name}),this.botConfig.polling?.enabled!==false&&this.startPolling();}async doStop(){this.stopPolling(),this.botConfig=null;}async send(e){let t=e.metadata.channel.chatId;if(!t)throw new Error("Chat ID is required");let s=this.extractText(e);return (await this.sendMessage(t,s)).message_id.toString()}startPolling(){if(this.pollingInterval)return;let e=this.botConfig?.polling?.interval??1e3;this.pollingInterval=setInterval(async()=>{try{await this.poll();}catch(t){this.log.error("Polling error",{error:String(t)});}},e),this.log.info("Polling started",{interval:e});}stopPolling(){this.pollingInterval&&(clearInterval(this.pollingInterval),this.pollingInterval=null,this.log.info("Polling stopped"));}async poll(){let e=await this.getUpdates(this.lastUpdateId+1,100,0);for(let t of e)this.lastUpdateId=t.update_id,t.message&&await this.handleTelegramMessage(t.message);}async handleTelegramMessage(e){if(this.updateActivity(),e.from?.is_bot)return;let t=this.toGatewayMessage(e);await this.handleMessage(t);}toGatewayMessage(e){let t=this.getSessionId(e);return c$1(t,"input",this.parseContent(e),{channel:{platform:"telegram",channelId:this.id,userId:e.from?.id.toString(),chatId:e.chat.id.toString(),username:e.from?.username,firstName:e.from?.first_name,lastName:e.from?.last_name,chatType:e.chat.type,chatTitle:e.chat.title},context:{replyTo:e.reply_to_message?.message_id.toString()}})}parseContent(e){let t=[];if(e.text&&t.push({type:"text",text:e.text}),e.photo&&e.photo.length>0){let s=e.photo[e.photo.length-1];s&&t.push({type:"image",image:s.file_id,mime:"image/jpeg"});}if(e.document&&t.push({type:"file",data:e.document.file_id,mime:e.document.mime_type}),e.caption&&t.length>0){let s=t[0];s&&s.type==="text"?t[0]={type:"text",text:`${s.text}
|
|
1
|
+
export{a as Gateway}from'./chunks/chunk-ENCE7NRO.mjs';import {j,k as k$1,c as c$1,r}from'./chunks/chunk-DJTIL3AB.mjs';export{q as ChannelPluginLoader,j as DEFAULT_AGENT_REGISTRY_CONFIG,l as DEFAULT_CONNECTION_POOL_CONFIG,k as DEFAULT_SYNC_CONFIG,i as DEFAULT_TOKEN_AUTH_CONFIG,h as DEFAULT_WEBSOCKET_SERVER_CONFIG,s as GatewayServer,p as GatewaySessionManager,a as MessageLockManager,b as MessageRetryManager,m as MessageRouter,n as MessageStore,o as SessionStore,f as createDefaultSessionState,c as createGatewayMessage,d as createTextMessage,e as generateSessionId,g as generateSubscriptionId}from'./chunks/chunk-DJTIL3AB.mjs';export{p as AgentRegistryConfigSchema,k as AgentSyncConfigSchema,j as AuthConfigSchema,h as ChannelConfigSchema,o as CircuitBreakerConfigSchema,m as ConnectionPoolConfigSchema,c as FeishuChannelConfigSchema,l as GatewayClusterConfigSchema,s as GatewayConfigSchema,r as GatewayServerConfigSchema,i as HTTPSConfigSchema,n as MessageQueueConfigSchema,g as NostrChannelConfigSchema,q as SessionConfigSchema,f as SignalChannelConfigSchema,b as SlackChannelConfigSchema,a as TelegramChannelConfigSchema,d as WeChatChannelConfigSchema,e as WebChatChannelConfigSchema,z as clearConfigCache,O as getAgentAdapter,G as getAgentRegistryConfig,J as getAuthConfig,C as getChannelConfig,F as getCircuitBreakerConfig,K as getClusterConfig,x as getConfig,A as getConfigDirectory,D as getConnectionPoolConfig,L as getDefaultAgent,I as getHTTPSConfig,M as getLogLevel,E as getMessageQueueConfig,B as getServerConfig,H as getSessionConfig,P as hasAgentAdapter,y as isConfigLoaded,w as loadConfig,u as parsePartialConfig,Q as requireAgentAdapter,N as setAgentAdapter,t as validateConfig}from'./chunks/chunk-77F43CRD.mjs';import {a}from'./chunks/chunk-6TRL3CVJ.mjs';import {a as a$1}from'./chunks/chunk-HAMGVOQD.mjs';var f=a.create({service:"gateway:agent-registry"}),k=class{constructor(n={}){a$1(this,"config");a$1(this,"agents",new Map);a$1(this,"heartbeatTimer",null);a$1(this,"roundRobinIndex",0);this.config={...j,...n};}async register(n){if(this.agents.has(n.id))throw new Error(`Agent with ID "${n.id}" is already registered`);if(this.agents.size>=this.config.maxAgents)throw new Error(`Maximum number of agents (${this.config.maxAgents}) reached`);let e=Date.now(),t={...n,status:"healthy",lastHeartbeat:e,registeredAt:e,connectionCount:0};this.agents.set(n.id,t),f.info("Agent registered",{agentId:n.id,name:n.name,capabilities:n.capabilities,address:n.address,totalAgents:this.agents.size});}async deregister(n){let e=this.agents.get(n);if(!e){f.warn("Attempted to deregister unknown agent",{agentId:n});return}this.agents.delete(n),f.info("Agent deregistered",{agentId:n,name:e.name,totalAgents:this.agents.size});}async heartbeat(n,e){let t=this.agents.get(n);if(!t)throw f.warn("Heartbeat received from unknown agent",{agentId:n}),new Error(`Agent "${n}" is not registered`);t.lastHeartbeat=Date.now(),t.status="healthy",e&&(t.metadata={...t.metadata,...e}),f.debug("Agent heartbeat received",{agentId:n,lastHeartbeat:t.lastHeartbeat});}getAgent(n){return this.agents.get(n)}getAllAgents(){return Array.from(this.agents.values())}getHealthyAgents(){return this.getAllAgents().filter(n=>n.status==="healthy")}selectAgent(n){let{strategy:e,capabilities:t,healthyOnly:s=true}=n,i=s?this.getHealthyAgents():this.getAllAgents();if(t&&t.length>0&&(i=i.filter(o=>t.every(u=>o.capabilities.includes(u)))),i.length===0){f.warn("No agents available for selection",{strategy:e,capabilities:t,healthyOnly:s,totalAgents:this.agents.size});return}let a;switch(e){case "random":a=this.selectRandom(i);break;case "round-robin":a=this.selectRoundRobin(i);break;case "least-connections":a=this.selectLeastConnections(i);break;default:a=this.selectRandom(i);}return f.debug("Agent selected",{agentId:a.id,strategy:e,connectionCount:a.connectionCount}),a}incrementConnection(n){let e=this.agents.get(n);e&&(e.connectionCount++,f.debug("Agent connection incremented",{agentId:n,connectionCount:e.connectionCount}));}decrementConnection(n){let e=this.agents.get(n);e&&e.connectionCount>0&&(e.connectionCount--,f.debug("Agent connection decremented",{agentId:n,connectionCount:e.connectionCount}));}startHeartbeatCheck(){if(this.heartbeatTimer){f.warn("Heartbeat check is already running");return}f.info("Starting heartbeat check",{interval:this.config.heartbeatCheckInterval,timeout:this.config.heartbeatTimeout}),this.heartbeatTimer=setInterval(()=>this.checkHeartbeats(),this.config.heartbeatCheckInterval);}stopHeartbeatCheck(){this.heartbeatTimer&&(clearInterval(this.heartbeatTimer),this.heartbeatTimer=null,f.info("Heartbeat check stopped"));}checkHeartbeats(){let n=Date.now(),{heartbeatTimeout:e,autoDeregisterTimeout:t}=this.config,s=[],i=0;for(let[a,o]of this.agents.entries()){let u=n-o.lastHeartbeat;if(u>t){s.push(a);continue}u>e&&o.status==="healthy"&&(o.status="unhealthy",i++,f.warn("Agent marked as unhealthy due to heartbeat timeout",{agentId:a,lastHeartbeat:o.lastHeartbeat,timeSinceLastHeartbeat:u,timeout:e}));}for(let a of s){let o=this.agents.get(a);o&&(f.info("Auto-deregistering agent due to timeout",{agentId:a,name:o.name,lastHeartbeat:o.lastHeartbeat,timeout:t}),this.agents.delete(a));}(i>0||s.length>0)&&f.info("Heartbeat check completed",{unhealthyCount:i,deregisteredCount:s.length,totalAgents:this.agents.size});}selectRandom(n){let e=Math.floor(Math.random()*n.length);return n[e]}selectRoundRobin(n){this.roundRobinIndex=this.roundRobinIndex%n.length;let e=n[this.roundRobinIndex];return this.roundRobinIndex++,e}selectLeastConnections(n){return n.reduce((e,t)=>t.connectionCount<e.connectionCount?t:e)}getStats(){let n=this.getAllAgents();return {totalAgents:n.length,healthyAgents:n.filter(e=>e.status==="healthy").length,unhealthyAgents:n.filter(e=>e.status==="unhealthy").length,offlineAgents:n.filter(e=>e.status==="offline").length,totalConnections:n.reduce((e,t)=>e+t.connectionCount,0)}}setAgentStatus(n,e){let t=this.agents.get(n);t&&(t.status=e);}setLastHeartbeat(n,e){let t=this.agents.get(n);t&&(t.lastHeartbeat=e);}};var g=a.create({service:"gateway:agent-sync-manager"}),T=class{constructor(n){a$1(this,"config");a$1(this,"registry");a$1(this,"localNodeId");a$1(this,"localNodeName");a$1(this,"remoteNodes",new Map);a$1(this,"remoteAgents",new Map);a$1(this,"syncTimer",null);a$1(this,"running",false);a$1(this,"httpClient");this.registry=n.registry,this.localNodeId=n.localNodeId,this.localNodeName=n.localNodeName,this.config={...k$1,...n.config},this.httpClient=n.httpClient||{fetch:globalThis.fetch.bind(globalThis)};}async start(){if(this.running){g.warn("Agent sync manager is already running");return}g.info("Starting agent sync manager",{localNodeId:this.localNodeId,mode:this.config.mode,interval:this.config.interval,remoteNodes:this.config.remoteNodes.length});for(let n of this.config.remoteNodes)this.remoteNodes.set(n.id,{info:{id:n.id,name:n.id,address:n.address,port:n.port,lastSyncAt:0,status:"unknown"},lastSyncAt:0,status:"synced"});(this.config.mode==="pull"||this.config.mode==="both")&&(this.syncTimer=setInterval(()=>this.pullFromAllNodes(),this.config.interval)),this.running=true,g.info("Agent sync manager started");}async stop(){if(!this.running){g.warn("Agent sync manager is not running");return}g.info("Stopping agent sync manager"),this.syncTimer&&(clearInterval(this.syncTimer),this.syncTimer=null),this.remoteNodes.clear(),this.remoteAgents.clear(),this.running=false,g.info("Agent sync manager stopped");}async pullFromAllNodes(){for(let[n]of this.remoteNodes.entries())try{await this.pullFromNode(n);}catch(e){g.error("Failed to pull from node",{nodeId:n,error:e.message});}}async pullFromNode(n){let e=this.remoteNodes.get(n);if(!e)throw new Error(`Unknown remote node: ${n}`);e.status="syncing";let t=this.config.remoteNodes.find(s=>s.id===n);if(!t)throw new Error(`Remote node not found in config: ${n}`);g.debug("Pulling agent list from node",{nodeId:n,address:t.address});try{let s=`http://${t.address}:${t.port}/sync/agents`,i=await this.httpClient.fetch(s,{method:"GET",headers:{"Content-Type":"application/json","X-Gateway-Node-Id":this.localNodeId}});if(!i.ok)throw new Error(`HTTP ${i.status}: ${i.statusText}`);let o=(await i.json()).agents||[];for(let u of o)u.sourceGatewayId=n,this.remoteAgents.set(u.id,u);return e.lastSyncAt=Date.now(),e.status="synced",e.info.lastSyncAt=e.lastSyncAt,e.info.status="online",delete e.error,g.info("Pulled agent list from node",{nodeId:n,agentCount:o.length,lastSyncAt:e.lastSyncAt}),o}catch(s){throw e.status="error",e.error=s.message,e.info.status="offline",g.error("Failed to pull from node",{nodeId:n,error:s.message}),s}}async pushEvent(n){if(this.config.mode!=="push"&&this.config.mode!=="both")return;let e=[];for(let[t]of this.remoteNodes.entries())e.push(this.pushToNode(t,n));await Promise.allSettled(e);}async pushToNode(n,e){let t=this.config.remoteNodes.find(s=>s.id===n);if(!t){g.warn("Unknown remote node for push",{nodeId:n});return}try{let s=`http://${t.address}:${t.port}/sync/events`,i=await this.httpClient.fetch(s,{method:"POST",headers:{"Content-Type":"application/json","X-Gateway-Node-Id":this.localNodeId},body:JSON.stringify(e)});if(!i.ok)throw new Error(`HTTP ${i.status}: ${i.statusText}`);g.debug("Pushed event to node",{nodeId:n,eventType:e.type});}catch(s){g.error("Failed to push to node",{nodeId:n,eventType:e.type,error:s.message});}}async handleSyncMessage(n){switch(g.debug("Received sync message",{type:n.type,sourceNodeId:n.sourceNodeId}),n.type){case "agent_list_request":await this.handleAgentListRequest(n);break;case "agent_list_response":await this.handleAgentListResponse(n);break;case "agent_register":await this.handleAgentRegister(n);break;case "agent_deregister":await this.handleAgentDeregister(n);break;case "agent_heartbeat":await this.handleAgentHeartbeat(n);break;case "agent_status_change":await this.handleAgentStatusChange(n);break;default:g.warn("Unknown sync message type",{type:n.type});}}async handleAgentListRequest(n){let e=this.getLocalAgentsForSync(),t={type:"agent_list_response",sourceNodeId:this.localNodeId,timestamp:Date.now(),requestId:n.requestId,agents:e,fullList:true,syncAt:Date.now()};await this.pushToNode(n.sourceNodeId,t);}async handleAgentListResponse(n){for(let e of n.agents)this.remoteAgents.set(e.id,e);g.info("Received agent list response",{sourceNodeId:n.sourceNodeId,agentCount:n.agents.length});}async handleAgentRegister(n){let{agent:e}=n,t=this.remoteAgents.get(e.id);if(t){let s=this.resolveConflict(t,e);this.remoteAgents.set(e.id,s);}else this.remoteAgents.set(e.id,e);g.info("Remote agent registered",{agentId:e.id,name:e.name,sourceNodeId:n.sourceNodeId});}async handleAgentDeregister(n){let{agentId:e}=n;this.remoteAgents.delete(e),g.info("Remote agent deregistered",{agentId:e,sourceNodeId:n.sourceNodeId});}async handleAgentHeartbeat(n){let{agentId:e,heartbeatAt:t,status:s}=n,i=this.remoteAgents.get(e);i&&(i.updatedAt=t,i.status=s),g.debug("Remote agent heartbeat received",{agentId:e,status:s,sourceNodeId:n.sourceNodeId});}async handleAgentStatusChange(n){let{agentId:e,newStatus:t,changedAt:s}=n,i=this.remoteAgents.get(e);i&&(i.status=t,i.updatedAt=s),g.info("Remote agent status changed",{agentId:e,newStatus:t,sourceNodeId:n.sourceNodeId});}resolveConflict(n,e){switch(this.config.conflictResolution){case "latest":return e.updatedAt>n.updatedAt?e:n;case "local":return n;case "remote":return e;default:return e.updatedAt>n.updatedAt?e:n}}getLocalAgentsForSync(){return this.registry.getAllAgents().map(e=>({id:e.id,name:e.name,sourceGatewayId:this.localNodeId,address:e.address,capabilities:e.capabilities,status:e.status,updatedAt:e.lastHeartbeat,metadata:e.metadata}))}getMergedAgentList(){let n=new Map;for(let e of this.getLocalAgentsForSync())n.set(e.id,e);for(let[e,t]of this.remoteAgents.entries())n.has(e)||n.set(e,t);return Array.from(n.values())}getRemoteAgents(){return Array.from(this.remoteAgents.values())}getStats(){let n=this.registry.getAllAgents(),e=0,t=0;for(let i of this.remoteNodes.values())i.info.status==="online"?e++:t++;let s=null;for(let i of this.remoteNodes.values())i.lastSyncAt>(s||0)&&(s=i.lastSyncAt);return {localAgentCount:n.length,remoteAgentCount:this.remoteAgents.size,totalAgentCount:n.length+this.remoteAgents.size,onlineNodeCount:e,offlineNodeCount:t,lastSyncAt:s}}isRunning(){return this.running}addRemoteNode(n){this.remoteNodes.set(n.id,{info:{id:n.id,name:n.id,address:n.address,port:n.port,lastSyncAt:0,status:"unknown"},lastSyncAt:0,status:"synced"}),g.info("Remote node added",{nodeId:n.id,address:n.address});}removeRemoteNode(n){this.remoteNodes.delete(n);for(let[e,t]of this.remoteAgents.entries())t.sourceGatewayId===n&&this.remoteAgents.delete(e);g.info("Remote node removed",{nodeId:n});}};var c=class{constructor(){a$1(this,"version","1.0.0");a$1(this,"log");a$1(this,"config",null);a$1(this,"messageHandler",null);a$1(this,"_status","stopped");a$1(this,"lastActivity",0);}initLog(){this.log=a.create({service:`channel:${this.platform}`});}async start(n,e){if(this._status==="running"){this.log.warn("plugin is already running");return}this.log.info("starting channel plugin",{id:this.id,platform:this.platform}),this._status="starting",this.config=n,this.messageHandler=e;try{await this.doStart(),this._status="running",this.lastActivity=Date.now(),this.log.info("channel plugin started",{id:this.id});}catch(t){throw this._status="error",this.log.error("failed to start channel plugin",{error:String(t)}),t}}async stop(){if(this._status==="stopped"){this.log.warn("plugin is already stopped");return}this.log.info("stopping channel plugin",{id:this.id}),this._status="stopping";try{await this.doStop(),this._status="stopped",this.log.info("channel plugin stopped",{id:this.id});}catch(n){throw this._status="error",this.log.error("failed to stop channel plugin",{error:String(n)}),n}}async healthCheck(){try{return {healthy:await this.doHealthCheck(),lastCheck:Date.now()}}catch(n){return {healthy:false,lastCheck:Date.now(),error:n instanceof Error?n.message:String(n)}}}getStatus(){return {status:this._status,lastActivity:this.lastActivity}}async doHealthCheck(){return this._status==="running"}updateActivity(){this.lastActivity=Date.now();}async handleMessage(n){if(!this.messageHandler){this.log.warn("no message handler set, dropping message",{id:n.id});return}this.updateActivity(),await this.messageHandler.onMessage(n);}async handleEvent(n,e){if(!this.messageHandler){this.log.warn("no message handler set, dropping event",{type:n});return}await this.messageHandler.onEvent({type:n,channelId:this.id,data:e,timestamp:Date.now()});}};var x=class{constructor(n){a$1(this,"log");a$1(this,"config");a$1(this,"ws",null);a$1(this,"state","disconnected");a$1(this,"subscriptions",new Map);a$1(this,"messageCallbacks",new Set);a$1(this,"agentListCallbacks",new Set);a$1(this,"cachedAgentList",[]);a$1(this,"localAgentList",[]);a$1(this,"reconnectAttempts",0);a$1(this,"reconnectTimer",null);a$1(this,"heartbeatTimer",null);a$1(this,"agentListRequestId",null);a$1(this,"agentListRequestResolve",null);this.config={url:n.url,type:n.type,id:n.id??`client_${Date.now()}_${Math.random().toString(36).substring(2,9)}`,reconnect:{enabled:n.reconnect?.enabled??true,maxAttempts:n.reconnect?.maxAttempts??5,delay:n.reconnect?.delay??3e3},heartbeat:{enabled:n.heartbeat?.enabled??true,interval:n.heartbeat?.interval??3e4}},this.log=a.create({service:`gateway:client:${this.config.id}`});}async connect(){if(this.state==="connected"||this.state==="connecting"){this.log.warn("already connected or connecting");return}return this.state="connecting",this.log.info("connecting to gateway",{url:this.config.url}),new Promise((n,e)=>{try{this.ws=new WebSocket(this.config.url),this.ws.onopen=()=>{this.state="connected",this.reconnectAttempts=0,this.log.info("connected to gateway"),this.config.heartbeat.enabled&&this.startHeartbeat(),this.resubscribeAll(),n();},this.ws.onmessage=t=>{this.handleMessage(t.data);},this.ws.onclose=t=>{this.handleClose(t.code,t.reason);},this.ws.onerror=t=>{this.log.error("WebSocket error",{error:String(t)}),this.state==="connecting"&&e(new Error("Connection failed"));};}catch(t){this.state="disconnected",e(t);}})}async disconnect(){this.state!=="disconnected"&&(this.log.info("disconnecting from gateway"),this.stopHeartbeat(),this.stopReconnect(),this.ws&&(this.ws.close(1e3,"Client disconnect"),this.ws=null),this.state="disconnected",this.log.info("disconnected from gateway"));}async subscribe(n){if(this.state!=="connected")throw new Error("Not connected to gateway");this.sendData({type:"subscribe",sessionId:n,clientId:this.config.id}),this.log.info("subscribed to session",{sessionId:n});}async unsubscribe(n){if(this.state!=="connected")throw new Error("Not connected to gateway");this.sendData({type:"unsubscribe",sessionId:n,clientId:this.config.id}),this.subscriptions.delete(n),this.log.info("unsubscribed from session",{sessionId:n});}async send(n){if(this.state!=="connected")throw new Error("Not connected to gateway");this.sendData({type:"message",message:n});}onMessage(n){this.messageCallbacks.add(n);}offMessage(n){this.messageCallbacks.delete(n);}getState(){return this.state}getId(){return this.config.id}getSubscriptions(){return [...this.subscriptions.keys()]}setLocalAgents(n){this.localAgentList=n,this.updateMergedAgentList();}async fetchAgentList(){if(this.state!=="connected")throw new Error("Not connected to gateway");return new Promise((n,e)=>{let t=`req_${Date.now()}_${Math.random().toString(36).substring(2,9)}`;this.agentListRequestId=t,this.agentListRequestResolve=n,this.sendData({type:"agent_list_request",requestId:t}),setTimeout(()=>{this.agentListRequestId===t&&(this.agentListRequestId=null,this.agentListRequestResolve=null,e(new Error("Agent list request timeout")));},1e4);})}getConnectableAgents(n={}){let{localAgents:e,preferLocal:t=true,filterOffline:s=true}=n,i=e||this.localAgentList,a=this.cachedAgentList,o=this.mergeAgentLists(i,a,t);return s?o.filter(u=>u.status!=="offline"):o}onAgentListChange(n){this.agentListCallbacks.add(n);}offAgentListChange(n){this.agentListCallbacks.delete(n);}mergeAgentLists(n,e,t){let s=new Map,i=t?e:n,a=t?n:e;for(let o of i)s.set(o.id,o);for(let o of a)s.set(o.id,o);return Array.from(s.values())}updateMergedAgentList(){let n=this.getConnectableAgents({filterOffline:false});for(let e of this.agentListCallbacks)try{e(n);}catch(t){this.log.error("agent list callback error",{error:String(t)});}}handleAgentListResponse(n,e){e&&this.agentListRequestId===e&&this.agentListRequestResolve&&(this.agentListRequestId=null,this.agentListRequestResolve(n),this.agentListRequestResolve=null),this.cachedAgentList=n,this.updateMergedAgentList(),this.log.debug("received agent list",{count:n.length});}sendData(n){if(!this.ws||this.ws.readyState!==WebSocket.OPEN){this.log.warn("WebSocket not ready, cannot send");return}this.ws.send(JSON.stringify(n));}handleMessage(n){try{let e=JSON.parse(n);switch(e.type){case "message":this.handleGatewayMessage(e.message);break;case "subscribed":this.subscriptions.set(e.sessionId,e.subscription);break;case "unsubscribed":this.subscriptions.delete(e.sessionId);break;case "pong":break;case "agent_list_response":this.handleAgentListResponse(e.agents||[],e.requestId);break;case "agent_update":this.handleAgentUpdate(e.agent,e.action);break;default:this.log.debug("unknown message type",{type:e.type});}}catch(e){this.log.error("failed to parse message",{error:String(e)});}}handleAgentUpdate(n,e){switch(e){case "add":case "update":{let t=this.cachedAgentList.findIndex(s=>s.id===n.id);t>=0?this.cachedAgentList[t]=n:this.cachedAgentList.push(n);break}case "remove":this.cachedAgentList=this.cachedAgentList.filter(t=>t.id!==n.id);break}this.updateMergedAgentList();}handleGatewayMessage(n){for(let e of this.messageCallbacks)try{e(n);}catch(t){this.log.error("message callback error",{error:String(t)});}}handleClose(n,e){this.log.info("connection closed",{code:n,reason:e}),this.state="disconnected",this.ws=null,this.stopHeartbeat(),this.config.reconnect.enabled&&this.scheduleReconnect();}scheduleReconnect(){if(this.reconnectAttempts>=this.config.reconnect.maxAttempts){this.log.error("max reconnect attempts reached");return}this.reconnectAttempts++,this.state="reconnecting";let n=this.config.reconnect.delay*this.reconnectAttempts;this.log.info("scheduling reconnect",{attempt:this.reconnectAttempts,delay:n}),this.reconnectTimer=setTimeout(async()=>{try{await this.connect();}catch(e){this.log.error("reconnect failed",{error:String(e)});}},n);}stopReconnect(){this.reconnectTimer&&(clearTimeout(this.reconnectTimer),this.reconnectTimer=null);}startHeartbeat(){this.heartbeatTimer=setInterval(()=>{this.state==="connected"&&this.sendData({type:"ping"});},this.config.heartbeat.interval);}stopHeartbeat(){this.heartbeatTimer&&(clearInterval(this.heartbeatTimer),this.heartbeatTimer=null);}async resubscribeAll(){for(let n of this.subscriptions.keys())try{await this.subscribe(n);}catch(e){this.log.error("failed to resubscribe",{sessionId:n,error:String(e)});}}};var y=class extends c{constructor(e="telegram-main"){super();a$1(this,"id");a$1(this,"platform","telegram");a$1(this,"name","Telegram Bot");a$1(this,"botConfig",null);a$1(this,"pollingInterval",null);a$1(this,"lastUpdateId",0);a$1(this,"baseUrl","");this.id=e,this.initLog();}async doStart(){if(!this.config)throw new Error("Config not set");if(this.botConfig=this.config.platform,!this.botConfig.botToken)throw new Error("Bot token is required");this.baseUrl=`https://api.telegram.org/bot${this.botConfig.botToken}`;let e=await this.getMe();this.log.info("Telegram bot connected",{id:e.id,username:e.username,first_name:e.first_name}),this.botConfig.polling?.enabled!==false&&this.startPolling();}async doStop(){this.stopPolling(),this.botConfig=null;}async send(e){let t=e.metadata.channel.chatId;if(!t)throw new Error("Chat ID is required");let s=this.extractText(e);return (await this.sendMessage(t,s)).message_id.toString()}startPolling(){if(this.pollingInterval)return;let e=this.botConfig?.polling?.interval??1e3;this.pollingInterval=setInterval(async()=>{try{await this.poll();}catch(t){this.log.error("Polling error",{error:String(t)});}},e),this.log.info("Polling started",{interval:e});}stopPolling(){this.pollingInterval&&(clearInterval(this.pollingInterval),this.pollingInterval=null,this.log.info("Polling stopped"));}async poll(){let e=await this.getUpdates(this.lastUpdateId+1,100,0);for(let t of e)this.lastUpdateId=t.update_id,t.message&&await this.handleTelegramMessage(t.message);}async handleTelegramMessage(e){if(this.updateActivity(),e.from?.is_bot)return;let t=this.toGatewayMessage(e);await this.handleMessage(t);}toGatewayMessage(e){let t=this.getSessionId(e);return c$1(t,"input",this.parseContent(e),{channel:{platform:"telegram",channelId:this.id,userId:e.from?.id.toString(),chatId:e.chat.id.toString(),username:e.from?.username,firstName:e.from?.first_name,lastName:e.from?.last_name,chatType:e.chat.type,chatTitle:e.chat.title},context:{replyTo:e.reply_to_message?.message_id.toString()}})}parseContent(e){let t=[];if(e.text&&t.push({type:"text",text:e.text}),e.photo&&e.photo.length>0){let s=e.photo[e.photo.length-1];s&&t.push({type:"image",image:s.file_id,mime:"image/jpeg"});}if(e.document&&t.push({type:"file",data:e.document.file_id,mime:e.document.mime_type}),e.caption&&t.length>0){let s=t[0];s&&s.type==="text"?t[0]={type:"text",text:`${s.text}
|
|
2
2
|
|
|
3
3
|
${e.caption}`}:t.unshift({type:"text",text:e.caption});}return t}getSessionId(e){let t=e.chat.id.toString(),s=e.from?.id.toString();return e.chat.type==="private"?`telegram_${this.id}_${t}`:`telegram_${this.id}_${t}_${s}`}extractText(e){let t=[];for(let s of e.content)s.type==="text"&&s.text&&t.push(s.text);return t.join(`
|
|
4
4
|
`)}async getMe(){return (await this.request("getMe")).result}async getUpdates(e,t,s){return (await this.request("getUpdates",{offset:e,limit:t,timeout:s})).result||[]}async sendMessage(e,t){return (await this.request("sendMessage",{chat_id:e,text:t,parse_mode:"Markdown"})).result}async request(e,t={}){let s=`${this.baseUrl}/${e}`,a=await(await fetch(s,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(t)})).json();if(!a.ok)throw new Error(`Telegram API error: ${a.description}`);return a}};var b=class extends c{constructor(e="discord-main"){super();a$1(this,"id");a$1(this,"platform","discord");a$1(this,"name","Discord Bot");a$1(this,"botConfig",null);a$1(this,"baseUrl","https://discord.com/api/v10");a$1(this,"gatewayWs",null);a$1(this,"heartbeatInterval",null);a$1(this,"sessionId",null);a$1(this,"sequenceNumber",null);this.id=e,this.initLog();}async doStart(){if(!this.config)throw new Error("Config not set");if(this.botConfig=this.config.platform,!this.botConfig.botToken)throw new Error("Bot token is required");let e=await this.getCurrentUser();this.log.info("Discord bot connected",{id:e.id,username:e.username}),this.botConfig.gateway?.enabled!==false&&await this.startGateway();}async doStop(){this.heartbeatInterval&&(clearInterval(this.heartbeatInterval),this.heartbeatInterval=null),this.gatewayWs&&(this.gatewayWs.close(),this.gatewayWs=null),this.botConfig=null;}async send(e){let t=e.metadata.channel.chatId;if(!t)throw new Error("Channel ID is required");let s=this.extractText(e);return (await this.createMessage(t,s,e.metadata.context?.replyTo)).id}async startGateway(){let t=`${(await this.getGatewayBot()).url}/?v=10&encoding=json`;this.gatewayWs=new WebSocket(t),this.gatewayWs.onmessage=s=>{this.handleGatewayMessage(JSON.parse(s.data.toString()));},this.gatewayWs.onclose=()=>{this.log.info("Gateway disconnected"),setTimeout(()=>this.startGateway(),5e3);},this.gatewayWs.onerror=s=>{this.log.error("Gateway error",{error:String(s)});};}handleGatewayMessage(e){switch(this.updateActivity(),e.op){case 10:this.handleHello(e.d);break;case 11:this.log.debug("Heartbeat ACK");break;case 0:e.s&&(this.sequenceNumber=e.s),this.handleDispatch(e.t,e.d);break;case 9:this.log.warn("Invalid session, reconnecting"),this.gatewayWs?.close();break}return Promise.resolve()}handleHello(e){this.heartbeatInterval=setInterval(()=>{this.gatewayWs?.send(JSON.stringify({op:1,d:this.sequenceNumber}));},e.heartbeat_interval),this.gatewayWs?.send(JSON.stringify({op:2,d:{token:this.botConfig?.botToken,intents:this.botConfig?.gateway?.intents??513,properties:{os:"linux",browser:"easbot",device:"easbot"}}}));}handleDispatch(e,t){e==="MESSAGE_CREATE"&&this.handleDiscordMessage(t);}async handleDiscordMessage(e){if(e.author.bot)return;let t=this.toGatewayMessage(e);await this.handleMessage(t);}toGatewayMessage(e){let t=this.getSessionId(e);return c$1(t,"input",this.parseContent(e),{channel:{platform:"discord",channelId:this.id,userId:e.author.id,chatId:e.channel_id,messageId:e.id,guildId:e.guild_id,username:e.author.username,discriminator:e.author.discriminator},context:{threadId:e.message_reference?.message_id,conversationId:e.channel_id}})}parseContent(e){let t=[];e.content&&t.push({type:"text",text:e.content});for(let s of e.attachments)s.content_type?.startsWith("image/")?t.push({type:"image",image:s.url,mime:s.content_type}):t.push({type:"file",data:s.url,mime:s.content_type});return t}getSessionId(e){let t=e.channel_id,s=e.author.id;return `discord_${this.id}_${t}_${s}`}extractText(e){let t=[];for(let s of e.content)s.type==="text"&&s.text&&t.push(s.text);return t.join(`
|
|
@@ -8,4 +8,4 @@ ${e.caption}`}:t.unshift({type:"text",text:e.caption});}return t}getSessionId(e)
|
|
|
8
8
|
`)}async refreshTenantAccessToken(){let t=await(await fetch(`${this.baseUrl}/auth/v3/tenant_access_token/internal`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({app_id:this.botConfig?.appId,app_secret:this.botConfig?.appSecret})})).json();if(t.code!==0)throw new Error(`Feishu API error: ${t.msg}`);this.tenantAccessToken=t.tenant_access_token,this.tokenExpireTime=Date.now()+t.expire*1e3-6e4;}async ensureTokenValid(){(!this.tenantAccessToken||Date.now()>=this.tokenExpireTime)&&await this.refreshTenantAccessToken();}async sendMessage(e,t){let i=await(await fetch(`${this.baseUrl}/im/v1/messages?receive_id_type=chat_id`,{method:"POST",headers:{Authorization:`Bearer ${this.tenantAccessToken}`,"Content-Type":"application/json"},body:JSON.stringify({receive_id:e,msg_type:"text",content:JSON.stringify({text:t})})})).json();if(i.code!==0)throw new Error(`Feishu API error: ${i.msg}`);return i.data}};var w=class extends c{constructor(e="wechat-main"){super();a$1(this,"id");a$1(this,"platform","wechat");a$1(this,"name","WeChat Bot");a$1(this,"botConfig",null);a$1(this,"accessToken",null);a$1(this,"tokenExpireTime",0);this.id=e,this.initLog();}async doStart(){if(!this.config)throw new Error("Config not set");if(this.botConfig=this.config.platform,!this.botConfig.appId||!this.botConfig.appSecret)throw new Error("App ID and App Secret are required");await this.refreshAccessToken(),this.log.info("WeChat bot connected",{app_id:this.botConfig.appId,mode:this.botConfig.mode});}async doStop(){this.botConfig=null,this.accessToken=null;}async send(e){let t=e.metadata.channel.userId;if(!t)throw new Error("User ID is required");await this.ensureTokenValid();let s=this.extractText(e);return this.botConfig?.mode==="wecom"?await this.sendWeComMessage(t,s):await this.sendOfficialMessage(t,s)}async handleWebhookMessage(e){this.updateActivity();let t=this.parseXmlMessage(e);if(t){let s=this.toGatewayMessage(t);await this.handleMessage(s);}}parseXmlMessage(e){let t=a=>e.match(new RegExp(`<${a}><!\\[CDATA\\[(.*?)\\]\\]></${a}>`))?.[1],s=a=>{let o=e.match(new RegExp(`<${a}>(\\d+)</${a}>`));return o?.[1]?parseInt(o[1],10):void 0},i=t("MsgType");return i?{ToUserName:t("ToUserName")??"",FromUserName:t("FromUserName")??"",CreateTime:s("CreateTime")??0,MsgType:i,Content:t("Content"),MsgId:s("MsgId"),PicUrl:t("PicUrl"),MediaId:t("MediaId")}:null}toGatewayMessage(e){let t=this.getSessionId(e);return c$1(t,"input",this.parseContent(e),{channel:{platform:"wechat",channelId:this.id,userId:e.FromUserName,chatId:e.FromUserName,toUser:e.ToUserName,msgId:e.MsgId?.toString(),mode:this.botConfig?.mode},context:{conversationId:e.FromUserName}})}parseContent(e){let t=[];switch(e.MsgType){case "text":e.Content&&t.push({type:"text",text:e.Content});break;case "image":(e.PicUrl||e.MediaId)&&t.push({type:"image",image:e.PicUrl??e.MediaId??"",mime:"image/jpeg"});break;case "voice":e.MediaId&&t.push({type:"file",data:e.MediaId,mime:"audio/amr"}),"Recognition"in e&&e.Recognition&&t.push({type:"text",text:e.Recognition});break;case "video":case "shortvideo":e.MediaId&&t.push({type:"file",data:e.MediaId,mime:"video/mp4"});break;default:e.Content&&t.push({type:"text",text:e.Content});}return t}getSessionId(e){let t=e.FromUserName;return `wechat_${this.id}_${t}`}extractText(e){let t=[];for(let s of e.content)s.type==="text"&&s.text&&t.push(s.text);return t.join(`
|
|
9
9
|
`)}async refreshAccessToken(){let e;this.botConfig?.mode==="wecom"?e=`https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=${this.botConfig.appId}&corpsecret=${this.botConfig.appSecret}`:e=`https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=${this.botConfig?.appId}&secret=${this.botConfig?.appSecret}`;let s=await(await fetch(e)).json();if(s.errcode&&s.errcode!==0)throw new Error(`WeChat API error: ${s.errmsg}`);this.accessToken=s.access_token,this.tokenExpireTime=Date.now()+s.expires_in*1e3-6e4;}async ensureTokenValid(){(!this.accessToken||Date.now()>=this.tokenExpireTime)&&await this.refreshAccessToken();}async sendOfficialMessage(e,t){let i=await(await fetch(`https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=${this.accessToken}`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({touser:e,msgtype:"text",text:{content:t}})})).json();if(i.errcode!==0)throw new Error(`WeChat API error: ${i.errmsg}`);return i.msg_id?.toString()??Date.now().toString()}async sendWeComMessage(e,t){let i=await(await fetch(`https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=${this.accessToken}`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({touser:e,msgtype:"text",agentid:this.botConfig?.agentId,text:{content:t}})})).json();if(i.errcode!==0)throw new Error(`WeCom API error: ${i.errmsg}`);return i.msgid??Date.now().toString()}};var S=class extends c{constructor(e="webchat-main"){super();a$1(this,"id");a$1(this,"platform","webchat");a$1(this,"name","WebChat");a$1(this,"webChatConfig",null);a$1(this,"connections",new Map);a$1(this,"heartbeatInterval",null);a$1(this,"wsServer",null);this.id=e,this.initLog();}async doStart(){if(!this.config)throw new Error("Config not set");this.webChatConfig=this.config.platform,await this.startWebSocketServer(),this.startHeartbeat(),this.log.info("WebChat plugin started",{port:this.webChatConfig.port,hostname:this.webChatConfig.hostname,path:this.webChatConfig.path,maxConnections:this.webChatConfig.maxConnections});}async startWebSocketServer(){if(!this.webChatConfig)return;let e={port:this.webChatConfig.port,hostname:this.webChatConfig.hostname,path:this.webChatConfig.path,maxConnections:this.webChatConfig.maxConnections,connectionTimeout:this.webChatConfig.connectionTimeout,heartbeatInterval:this.webChatConfig.heartbeatInterval,sessionExpireMs:this.webChatConfig.sessionExpireMs,https:this.webChatConfig.https};this.wsServer=new r(e),await this.wsServer.start(),this.log.info("WebSocket server started",{url:`${this.webChatConfig.https?.enabled?"wss":"ws"}://${this.webChatConfig.hostname}:${this.webChatConfig.port}${this.webChatConfig.path}`});}async doStop(){this.wsServer&&(await this.wsServer.stop(),this.wsServer=null),this.heartbeatInterval&&(clearInterval(this.heartbeatInterval),this.heartbeatInterval=null);for(let e of this.connections.values())e.ws.close();this.connections.clear(),this.webChatConfig=null;}async send(e){let t=e.sessionId,s=e.metadata.channel.userId;if(this.wsServer&&t){let o={type:"message",sessionId:t,properties:{sessionID:t,content:e.content,timestamp:e.timestamp||Date.now()}};return this.wsServer.broadcastToSession(t,o),e.id}if(!s)throw new Error("User ID is required");let i=this.findConnectionByUser(s);if(!i)throw new Error("User not connected");let a={type:"message",sessionId:t,content:e.content,metadata:e.metadata};return i.ws.send(JSON.stringify(a)),i.lastActivity=Date.now(),e.id}pushEvent(e,t){this.wsServer&&this.wsServer.broadcastToSession(e,t);}getWebSocketServer(){return this.wsServer}async handleConnection(e,t){let s=this.generateConnectionId();if(this.connections.size>=(this.webChatConfig?.maxConnections??1e3)){e.close(1013,"Maximum connections reached");return}let i={id:s,ws:e,userId:t,lastActivity:Date.now()};this.connections.set(s,i),e.onmessage=a=>{this.handleWebSocketMessage(i,a.data.toString());},e.onclose=()=>{this.connections.delete(s),this.log.debug("WebSocket closed",{connId:s,userId:t});},e.onerror=a=>{this.log.error("WebSocket error",{connId:s,userId:t,error:String(a)});},this.log.info("WebSocket connected",{connId:s,userId:t});}async handleWebSocketMessage(e,t){this.updateActivity(),e.lastActivity=Date.now();try{let s=JSON.parse(t);switch(s.type){case "message":await this.handleChatMessage(e,s);break;case "ping":e.ws.send(JSON.stringify({type:"pong"}));break;case "subscribe":s.sessionId&&(e.sessionId=s.sessionId);break;case "unsubscribe":e.sessionId=void 0;break}}catch(s){this.log.error("Failed to handle WebSocket message",{error:String(s)});}}async handleChatMessage(e,t){if(!t.content||!t.sessionId)return;let s=c$1(t.sessionId,"input",t.content,{channel:{platform:"webchat",channelId:this.id,userId:e.userId,chatId:t.sessionId,connectionId:e.id},context:{conversationId:t.sessionId},...t.metadata});await this.handleMessage(s);}startHeartbeat(){let e=this.webChatConfig?.heartbeatInterval??3e4;this.heartbeatInterval=setInterval(()=>{let t=Date.now(),s=this.webChatConfig?.connectionTimeout??6e4;for(let[i,a]of this.connections)t-a.lastActivity>s&&(this.log.info("Closing inactive connection",{connId:i}),a.ws.close(1001,"Connection timeout"),this.connections.delete(i));},e);}findConnectionByUser(e){for(let t of this.connections.values())if(t.userId===e)return t}generateConnectionId(){return `conn_${Date.now()}_${Math.random().toString(36).substring(2,9)}`}getConnectionCount(){return this.connections.size}getWsServerConnectionCount(){return this.wsServer?.getConnectionCount()??0}};var A=class extends c{constructor(e="signal-main"){super();a$1(this,"id");a$1(this,"platform","signal");a$1(this,"name","Signal Bot");a$1(this,"botConfig",null);a$1(this,"pollingInterval",null);a$1(this,"lastTimestamp",0);this.id=e,this.initLog();}async doStart(){if(!this.config)throw new Error("Config not set");if(this.botConfig=this.config.platform,!this.botConfig.phoneNumber)throw new Error("Phone number is required");let e=await this.getAccountInfo();this.log.info("Signal bot connected",{number:this.botConfig.phoneNumber,address:e.address}),this.startPolling();}async doStop(){this.pollingInterval&&(clearInterval(this.pollingInterval),this.pollingInterval=null),this.botConfig=null;}async send(e){let t=e.metadata.channel.userId;if(!t)throw new Error("Recipient is required");let s=this.extractText(e);return (await this.sendMessage(t,s)).timestamp.toString()}startPolling(){this.pollingInterval=setInterval(async()=>{try{await this.poll();}catch(t){this.log.error("Polling error",{error:String(t)});}},5e3),this.log.info("Polling started",{interval:5e3});}async poll(){let e=await this.receiveMessages(this.lastTimestamp);for(let t of e)this.lastTimestamp=t.envelope.timestamp,t.envelope.dataMessage&&await this.handleSignalMessage(t);}async handleSignalMessage(e){this.updateActivity();let t=this.toGatewayMessage(e);await this.handleMessage(t);}toGatewayMessage(e){let t=this.getSessionId(e),s=e.envelope.dataMessage;return c$1(t,"input",this.parseContent(s),{channel:{platform:"signal",channelId:this.id,userId:e.envelope.sourceNumber,chatId:e.envelope.sourceNumber,sourceUuid:e.envelope.sourceUuid,timestamp:e.envelope.timestamp},context:{conversationId:e.envelope.sourceNumber}})}parseContent(e){let t=[];if(!e)return t;if(e.message&&t.push({type:"text",text:e.message}),e.attachments)for(let s of e.attachments)s.contentType.startsWith("image/")?t.push({type:"image",image:s.id,mime:s.contentType}):t.push({type:"file",data:s.id,mime:s.contentType});return t}getSessionId(e){let t=e.envelope.sourceNumber;return `signal_${this.id}_${t}`}extractText(e){let t=[];for(let s of e.content)s.type==="text"&&s.text&&t.push(s.text);return t.join(`
|
|
10
10
|
`)}async getAccountInfo(){return await this.request("GET",`/v1/accounts/${this.botConfig?.phoneNumber}`)}async receiveMessages(e){let t=e>0?`?since=${e}`:"";return await this.request("GET",`/v1/receive/${this.botConfig?.phoneNumber}${t}`)||[]}async sendMessage(e,t){return await this.request("POST","/v2/send",{number:this.botConfig?.phoneNumber,recipients:[e],message:t})}async request(e,t,s){let i=`${this.botConfig?.serviceUrl}${t}`,a={"Content-Type":"application/json"};this.botConfig?.password&&(a.Authorization=`Basic ${Buffer.from(`${this.botConfig.phoneNumber}:${this.botConfig.password}`).toString("base64")}`);let o=await fetch(i,{method:e,headers:a,body:s?JSON.stringify(s):void 0});if(!o.ok){let u=await o.text();throw new Error(`Signal API error: ${u}`)}return o.json()}};var M=class extends c{constructor(e="nostr-main"){super();a$1(this,"id");a$1(this,"platform","nostr");a$1(this,"name","Nostr Bot");a$1(this,"botConfig",null);a$1(this,"relayConnections",new Map);a$1(this,"subscriptions",new Map);a$1(this,"eventHandlers",new Map);this.id=e,this.initLog();}async doStart(){if(!this.config)throw new Error("Config not set");if(this.botConfig=this.config.platform,!this.botConfig.relays||this.botConfig.relays.length===0)throw new Error("At least one relay is required");for(let e of this.botConfig.relays)await this.connectToRelay(e);this.log.info("Nostr bot connected",{relays:this.botConfig.relays,pubkey:this.botConfig.publicKey?.substring(0,16)+"..."});}async doStop(){for(let[e,t]of this.relayConnections)t.close();this.relayConnections.clear(),this.subscriptions.clear(),this.eventHandlers.clear(),this.botConfig=null;}async send(e){if(!this.botConfig?.privateKey)throw new Error("Private key is required for sending messages");let t=this.extractText(e),s=await this.createEvent(t,e.metadata.context?.replyTo);return await this.publishEvent(s),s.id}async connectToRelay(e){let t=new WebSocket(e);t.onopen=()=>{this.log.info("Connected to relay",{relay:e});for(let[s,i]of this.subscriptions)this.subscribeToRelay(t,s,i);},t.onmessage=s=>{this.handleRelayMessage(e,s.data.toString());},t.onclose=()=>{this.log.info("Disconnected from relay",{relay:e}),this.relayConnections.delete(e),setTimeout(()=>this.connectToRelay(e),5e3);},t.onerror=s=>{this.log.error("Relay error",{relay:e,error:String(s)});},this.relayConnections.set(e,t);}handleRelayMessage(e,t){this.updateActivity();try{let s=JSON.parse(t);if(s[0]==="EVENT"&&s[2]){let i=s[2];this.handleNostrEvent(i);}if(s[0]==="OK"){let i=s[1],a=s[2];this.log.debug("Event published",{eventId:i,success:a});}}catch(s){this.log.error("Failed to handle relay message",{error:String(s)});}return Promise.resolve()}async handleNostrEvent(e){if(e.kind!==1||e.pubkey===this.botConfig?.publicKey)return;let t=this.toGatewayMessage(e);await this.handleMessage(t);}toGatewayMessage(e){let t=this.getSessionId(e);return c$1(t,"input",this.parseContent(e),{channel:{platform:"nostr",channelId:this.id,userId:e.pubkey,chatId:e.pubkey,eventId:e.id,createdAt:e.created_at,tags:e.tags},context:{replyTo:this.findReplyTo(e.tags),conversationId:e.pubkey}})}parseContent(e){let t=[];e.content&&t.push({type:"text",text:e.content});for(let s of e.tags)if(s[0]==="url"&&s[1]){let i=s[1];i.match(/\.(jpg|jpeg|png|gif|webp)$/i)&&t.push({type:"image",image:i});}return t}findReplyTo(e){for(let t of e)if(t[0]==="e"&&t[1])return t[1]}getSessionId(e){return `nostr_${this.id}_${e.pubkey}`}extractText(e){let t=[];for(let s of e.content)s.type==="text"&&s.text&&t.push(s.text);return t.join(`
|
|
11
|
-
`)}subscribeToRelay(e,t,s){let i=["REQ",t,s];e.send(JSON.stringify(i));}async createEvent(e,t){let s=[];t&&s.push(["e",t]);let i={id:"",pubkey:this.botConfig?.publicKey??"",created_at:Math.floor(Date.now()/1e3),kind:1,tags:s,content:e,sig:""};return i.id=await this.computeEventId(i),i.sig=await this.signEvent(i),i}async computeEventId(e){let t=JSON.stringify([0,e.pubkey,e.created_at,e.kind,e.tags,e.content]),i=new TextEncoder().encode(t),a=await crypto.subtle.digest("SHA-256",i);return Array.from(new Uint8Array(a)).map(u=>u.toString(16).padStart(2,"0")).join("")}async signEvent(e){if(!this.botConfig?.privateKey)throw new Error("Private key is required");return "signature_placeholder"}async publishEvent(e){let t=["EVENT",e];for(let s of this.relayConnections.values())s.readyState===WebSocket.OPEN&&s.send(JSON.stringify(t));}};var _=class extends c{constructor(e){super();a$1(this,"id");a$1(this,"platform");a$1(this,"name");a$1(this,"version");a$1(this,"options");this.id=e.id,this.platform=e.platform||"custom",this.name=e.name,this.version=e.version||"1.0.0",this.options=e,this.initLog();}async doStart(){this.options.setup?.initialize&&await this.options.setup.initialize(),this.log.info("Channel plugin started",{id:this.id,platform:this.platform});}async doStop(){this.options.setup?.destroy&&await this.options.setup.destroy(),this.log.info("Channel plugin stopped",{id:this.id});}async send(e){let t=e;this.options.actions?.beforeSend&&(t=await this.options.actions.beforeSend(e));let s=await this.options.messaging.send(t);return this.options.actions?.afterSend&&await this.options.actions.afterSend(t,s),s}async healthCheck(){return this.options.status?.healthCheck?{healthy:await this.options.status.healthCheck(),lastCheck:Date.now()}:{healthy:true,lastCheck:Date.now()}}};function
|
|
11
|
+
`)}subscribeToRelay(e,t,s){let i=["REQ",t,s];e.send(JSON.stringify(i));}async createEvent(e,t){let s=[];t&&s.push(["e",t]);let i={id:"",pubkey:this.botConfig?.publicKey??"",created_at:Math.floor(Date.now()/1e3),kind:1,tags:s,content:e,sig:""};return i.id=await this.computeEventId(i),i.sig=await this.signEvent(i),i}async computeEventId(e){let t=JSON.stringify([0,e.pubkey,e.created_at,e.kind,e.tags,e.content]),i=new TextEncoder().encode(t),a=await crypto.subtle.digest("SHA-256",i);return Array.from(new Uint8Array(a)).map(u=>u.toString(16).padStart(2,"0")).join("")}async signEvent(e){if(!this.botConfig?.privateKey)throw new Error("Private key is required");return "signature_placeholder"}async publishEvent(e){let t=["EVENT",e];for(let s of this.relayConnections.values())s.readyState===WebSocket.OPEN&&s.send(JSON.stringify(t));}};var _=class extends c{constructor(e){super();a$1(this,"id");a$1(this,"platform");a$1(this,"name");a$1(this,"version");a$1(this,"options");this.id=e.id,this.platform=e.platform||"custom",this.name=e.name,this.version=e.version||"1.0.0",this.options=e,this.initLog();}async doStart(){this.options.setup?.initialize&&await this.options.setup.initialize(),this.log.info("Channel plugin started",{id:this.id,platform:this.platform});}async doStop(){this.options.setup?.destroy&&await this.options.setup.destroy(),this.log.info("Channel plugin stopped",{id:this.id});}async send(e){let t=e;this.options.actions?.beforeSend&&(t=await this.options.actions.beforeSend(e));let s=await this.options.messaging.send(t);return this.options.actions?.afterSend&&await this.options.actions.afterSend(t,s),s}async healthCheck(){return this.options.status?.healthCheck?{healthy:await this.options.status.healthCheck(),lastCheck:Date.now()}:{healthy:true,lastCheck:Date.now()}}};function N(l){return new _(l)}var E={telegram:y,discord:b,slack:C,feishu:v,wechat:w,webchat:S,signal:A,nostr:M};function $e(){return Object.keys(E)}async function kn(l){let{Log:n}=await import('./chunks/log-K67H67P2.mjs'),e=false;await n.init({logDir:l.logDir??process.env.EASBOT_LOG_PATH??process.cwd(),print:l.print??false,dev:l.dev??e,level:l.level??("INFO")});}export{k as AgentRegistry,T as AgentSyncManager,c as BaseChannelPlugin,E as ChannelPluginRegistry,b as DiscordPlugin,v as FeishuPlugin,x as GatewayClient,M as NostrPlugin,A as SignalPlugin,C as SlackPlugin,y as TelegramPlugin,w as WeChatPlugin,S as WebChatPlugin,N as createChannelPlugin,$e as getSupportedPlatforms,kn as initLog};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@easbot/gateway",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.14",
|
|
4
4
|
"description": "EASBot Gateway - AI Agent Server and Multi-channel Integration Platform - 支持 WebSocket、HTTP、Discord、Telegram、Slack 等多渠道集成",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.cjs",
|
|
@@ -66,10 +66,10 @@
|
|
|
66
66
|
"jieba-wasm": "^2.4.0",
|
|
67
67
|
"xdg-basedir": "^5.1.0",
|
|
68
68
|
"minimatch": "^10.2.5",
|
|
69
|
-
"@easbot/plugin": "0.2.
|
|
70
|
-
"@easbot/sdk": "0.2.
|
|
71
|
-
"@easbot/types": "0.2.
|
|
72
|
-
"@easbot/utils": "0.2.
|
|
69
|
+
"@easbot/plugin": "0.2.14",
|
|
70
|
+
"@easbot/sdk": "0.2.14",
|
|
71
|
+
"@easbot/types": "0.2.14",
|
|
72
|
+
"@easbot/utils": "0.2.14"
|
|
73
73
|
},
|
|
74
74
|
"devDependencies": {
|
|
75
75
|
"@biomejs/biome": "^2.4.14",
|
|
@@ -1,2 +0,0 @@
|
|
|
1
|
-
'use strict';var chunk5ETPYYQR_cjs=require('./chunk-5ETPYYQR.cjs'),e=require('zod'),L=require('path'),fs=require('fs'),be=require('fs/promises');function _interopDefault(e){return e&&e.__esModule?e:{default:e}}var e__default=/*#__PURE__*/_interopDefault(e);var L__default=/*#__PURE__*/_interopDefault(L);var be__default=/*#__PURE__*/_interopDefault(be);var $=chunk5ETPYYQR_cjs.a.create({service:"gateway:config"});e__default.default.enum(["telegram","slack","feishu","wechat","webchat","signal","nostr"]);e__default.default.enum(["low","medium","high","critical"]);var m=e__default.default.object({enabled:e__default.default.boolean().default(true),botToken:e__default.default.string().describe("Telegram Bot Token"),webhookUrl:e__default.default.string().optional().describe("Webhook URL for receiving updates"),maxConnections:e__default.default.number().int().positive().optional().default(100),timeout:e__default.default.number().int().positive().optional().default(3e4)}),y=e__default.default.object({enabled:e__default.default.boolean().default(true),botToken:e__default.default.string().describe("Slack Bot Token (xoxb-*)"),appToken:e__default.default.string().optional().describe("Slack App Token (xapp-*) for Socket Mode"),signingSecret:e__default.default.string().optional().describe("Slack Signing Secret for webhook verification"),maxConnections:e__default.default.number().int().positive().optional().default(100),timeout:e__default.default.number().int().positive().optional().default(3e4)}),b=e__default.default.object({enabled:e__default.default.boolean().default(true),appId:e__default.default.string().describe("Feishu App ID"),appSecret:e__default.default.string().describe("Feishu App Secret"),encryptKey:e__default.default.string().optional().describe("Feishu Encrypt Key for message encryption"),verificationToken:e__default.default.string().optional().describe("Feishu Verification Token"),maxConnections:e__default.default.number().int().positive().optional().default(100),timeout:e__default.default.number().int().positive().optional().default(3e4)}),S=e__default.default.object({enabled:e__default.default.boolean().default(true),mode:e__default.default.enum(["official","wecom"]).default("official"),appId:e__default.default.string().describe("WeChat App ID"),appSecret:e__default.default.string().describe("WeChat App Secret"),token:e__default.default.string().optional().describe("WeChat Token for message verification"),encodingAESKey:e__default.default.string().optional().describe("WeChat Encoding AES Key"),agentId:e__default.default.string().optional().describe("WeCom Agent ID"),maxConnections:e__default.default.number().int().positive().optional().default(100),timeout:e__default.default.number().int().positive().optional().default(3e4)}),w=e__default.default.object({enabled:e__default.default.boolean().default(true),path:e__default.default.string().optional().default("/"),heartbeatInterval:e__default.default.number().int().positive().optional().default(3e4),connectionTimeout:e__default.default.number().int().positive().optional().default(6e4),maxConnections:e__default.default.number().int().positive().optional().default(1e3),cors:e__default.default.boolean().optional().default(true)}),A=e__default.default.object({enabled:e__default.default.boolean().default(true),serviceUrl:e__default.default.string().optional().default("https://chat.signal.org"),phoneNumber:e__default.default.string().describe("Signal phone number"),password:e__default.default.string().optional().describe("Signal account password"),maxConnections:e__default.default.number().int().positive().optional().default(100),timeout:e__default.default.number().int().positive().optional().default(3e4)}),x=e__default.default.object({enabled:e__default.default.boolean().default(true),relays:e__default.default.array(e__default.default.string()).min(1).describe("Nostr relay URLs"),privateKey:e__default.default.string().optional().describe("Nostr private key (hex)"),publicKey:e__default.default.string().optional().describe("Nostr public key (hex)"),maxConnections:e__default.default.number().int().positive().optional().default(100),timeout:e__default.default.number().int().positive().optional().default(3e4)}),D=e__default.default.object({telegram:m.optional(),slack:y.optional(),feishu:b.optional(),wechat:S.optional(),webchat:w.optional(),signal:A.optional(),nostr:x.optional()}),j=e__default.default.object({enabled:e__default.default.boolean().default(false),cert:e__default.default.string().describe("Path to SSL certificate file (\u6216 certPath)"),key:e__default.default.string().describe("Path to SSL key file (\u6216 keyPath)"),certPath:e__default.default.string().optional().describe("Path to SSL certificate file (alias of cert)"),keyPath:e__default.default.string().optional().describe("Path to SSL key file (alias of key)"),ca:e__default.default.string().optional().describe("Path to CA certificate file"),forceRedirect:e__default.default.boolean().optional().default(false).describe("Force redirect HTTP to HTTPS"),httpPort:e__default.default.number().int().positive().optional().default(80).describe("HTTP redirect port")}),X=e__default.default.object({name:e__default.default.string().describe("Token name/description"),value:e__default.default.string().describe("Token value"),permissions:e__default.default.array(e__default.default.string()).default(["*"]).describe("Associated permissions"),expiresAt:e__default.default.number().int().positive().optional().describe("Expiration timestamp (Unix timestamp)"),description:e__default.default.string().optional().describe("Token description")}),Z=e__default.default.object({enabled:e__default.default.boolean().default(true),tokens:e__default.default.array(X).default([]).describe("List of valid tokens"),onInvalid:e__default.default.enum(["reject","anonymous"]).default("reject").describe("Behavior when token validation fails"),allowAnonymous:e__default.default.boolean().default(false).describe("Allow anonymous access"),defaultPermissions:e__default.default.array(e__default.default.string()).default([]).describe("Default permissions for anonymous users")}),ee=e__default.default.object({enabled:e__default.default.boolean().default(false),authorizationServer:e__default.default.string().describe("OAuth2 authorization server URL"),clientId:e__default.default.string().describe("OAuth2 client ID"),clientSecret:e__default.default.string().describe("OAuth2 client secret"),tokenValidationEndpoint:e__default.default.string().describe("OAuth2 token validation endpoint"),scope:e__default.default.array(e__default.default.string()).default([]).describe("OAuth2 scopes")}),z=e__default.default.object({type:e__default.default.enum(["token","oauth2"]).default("token"),token:Z.optional(),oauth2:ee.optional()}),U=e__default.default.object({mode:e__default.default.enum(["pull","push","both"]).default("both"),interval:e__default.default.number().int().positive().default(3e4),pushEvents:e__default.default.array(e__default.default.enum(["register","deregister","heartbeat","status_change"])).default(["register","deregister","status_change"]),conflictResolution:e__default.default.enum(["latest","local","remote"]).default("latest")}),te=e__default.default.object({id:e__default.default.string().describe("Gateway node ID"),address:e__default.default.string().describe("Gateway node address (e.g., http://localhost:3001)"),enabled:e__default.default.boolean().default(true),metadata:e__default.default.record(e__default.default.string(),e__default.default.any()).optional()}),W=e__default.default.object({nodeId:e__default.default.string().describe("Current gateway node ID"),nodes:e__default.default.array(te).default([]).describe("Other gateway nodes in the cluster"),sync:U.optional()}),v=e__default.default.object({maxConnectionsPerChannel:e__default.default.number().int().positive().default(100),idleTimeout:e__default.default.number().int().positive().default(3e5),reuseStrategy:e__default.default.enum(["lru","fifo","random"]).default("lru"),healthCheckInterval:e__default.default.number().int().positive().default(6e4),acquireTimeout:e__default.default.number().int().positive().default(5e3),warmupCount:e__default.default.number().int().nonnegative().default(0).describe("Number of connections to warm up")}),T=e__default.default.object({maxLength:e__default.default.number().int().positive().default(1e4),concurrency:e__default.default.number().int().positive().default(100),batchSize:e__default.default.number().int().positive().default(10),batchTimeout:e__default.default.number().int().positive().default(100),priorityLevels:e__default.default.number().int().positive().default(4)}),G=e__default.default.object({failureThreshold:e__default.default.number().int().positive().default(5),successThreshold:e__default.default.number().int().positive().default(3),timeout:e__default.default.number().int().positive().default(3e4),halfOpenDuration:e__default.default.number().int().positive().default(15e3)}),P=e__default.default.object({heartbeatInterval:e__default.default.number().int().positive().default(3e4),heartbeatTimeout:e__default.default.number().int().positive().default(9e4),heartbeatCheckInterval:e__default.default.number().int().positive().default(1e4),autoDeregisterTimeout:e__default.default.number().int().positive().default(3e5),maxAgents:e__default.default.number().int().positive().default(100),persistenceFile:e__default.default.string().optional().describe("Path to persist agent registrations"),maxRegistrations:e__default.default.number().int().positive().default(1e3)}),k=e__default.default.object({sessionTimeout:e__default.default.number().int().positive().default(18e5),resumeGracePeriod:e__default.default.number().int().positive().default(3e5),maxSessions:e__default.default.number().int().positive().default(1e4),persistenceFile:e__default.default.string().optional().describe("Path to persist sessions")}),I=e__default.default.object({enabled:e__default.default.boolean().default(true),port:e__default.default.number().int().positive().default(8080),hostname:e__default.default.string().default("localhost"),path:e__default.default.string().optional().default("/"),maxConnections:e__default.default.number().int().positive().optional().default(1e3),connectionTimeout:e__default.default.number().int().positive().optional().default(6e4),heartbeatInterval:e__default.default.number().int().positive().optional().default(3e4),sessionExpireMs:e__default.default.number().int().positive().optional().default(3e5),https:j.optional(),auth:z.optional(),cors:e__default.default.boolean().default(true),corsOrigins:e__default.default.array(e__default.default.string()).optional(),mdns:e__default.default.boolean().default(false),mdnsDomain:e__default.default.string().optional().default("gateway.easbot.local")}),d=e__default.default.object({server:I.optional(),channels:D.optional(),cluster:W.optional(),connectionPool:v.optional(),messageQueue:T.optional(),circuitBreaker:G.optional(),agentRegistry:P.optional(),session:k.optional(),defaultAgent:e__default.default.string().optional().describe("Default agent ID for routing"),logLevel:e__default.default.enum(["DEBUG","INFO","WARN","ERROR"]).optional().default("INFO")}).catchall(e__default.default.unknown());function N(t){let n=d.safeParse(t);return n.success||$.warn("Invalid gateway config",{issues:n.error.issues}),n.success?n.data:d.parse({})}function ne(t){return d.parse(t)}chunk5ETPYYQR_cjs.a.create({service:"gateway:interfaces"});var g;function K(){return g!==void 0}function q(){throw new Error("Agent adapter not set. Call setAgentAdapter() before using Gateway.");}var de={Path:{data:".easbot"}},me={directory:process.cwd()},R=class{async load(n){return F(n)}},ye=new R;function $e(){return ye}function V(){return de}function _(){return me}var l=chunk5ETPYYQR_cjs.a.create({service:"gateway:config"}),H={enabled:true,port:8080,hostname:"localhost",path:"/",maxConnections:1e3,connectionTimeout:6e4,heartbeatInterval:3e4,sessionExpireMs:3e5,cors:true,mdns:false,mdnsDomain:"gateway.easbot.local",https:{enabled:false,cert:"",key:"",certPath:void 0,keyPath:void 0,ca:void 0,forceRedirect:false,httpPort:80}},Se={maxConnectionsPerChannel:100,idleTimeout:3e5,reuseStrategy:"lru",healthCheckInterval:6e4,acquireTimeout:5e3,warmupCount:0},we={maxLength:1e4,concurrency:100,batchSize:10,batchTimeout:100,priorityLevels:4},Ae={failureThreshold:5,successThreshold:3,timeout:3e4,halfOpenDuration:15e3},xe={heartbeatInterval:3e4,heartbeatTimeout:9e4,heartbeatCheckInterval:1e4,autoDeregisterTimeout:3e5,maxAgents:100,maxRegistrations:1e3},ve={sessionTimeout:18e5,resumeGracePeriod:3e5,maxSessions:1e4},Te={enabled:true,botToken:"",maxConnections:100,timeout:3e4},Ge={enabled:true,botToken:"",maxConnections:100,timeout:3e4},Pe={enabled:true,appId:"",appSecret:"",maxConnections:100,timeout:3e4},ke={enabled:true,mode:"official",appId:"",appSecret:"",maxConnections:100,timeout:3e4},Ie={enabled:true,path:"/ws",heartbeatInterval:3e4,connectionTimeout:6e4,maxConnections:1e3,cors:true},Ne={enabled:true,serviceUrl:"https://chat.signal.org",phoneNumber:"",maxConnections:100,timeout:3e4},Le={enabled:true,relays:[],maxConnections:100,timeout:3e4},C,p=null;async function J(t){try{if(!fs.existsSync(t))return null;let n=await be__default.default.readFile(t,"utf-8"),o=n,r=!1,a=!1,s=[];for(let i=0;i<n.length;i++){let f=n[i];if(a){s.push(f),a=!1;continue}if(f==="\\"){s.push(f),a=!0;continue}if(f==='"'){r=!r,s.push(f);continue}if(!r&&f==="/"&&n[i+1]==="/"){for(;i<n.length&&n[i]!==`
|
|
2
|
-
`&&n[i]!=="\r";)i++;continue}if(!r&&f==="/"&&n[i+1]==="*"){for(i+=2;i<n.length-1;){if(n[i]==="*"&&n[i+1]==="/"){i+=2;break}i++;}continue}s.push(f);}return o=s.join("").trim(),o?JSON.parse(o):null}catch(n){return l.debug("Failed to load config file",{file:t,error:String(n)}),null}}function h(t,n){let o={...t,...n};return (t.server||n.server)&&(o.server={...t.server,...n.server}),(t.channels||n.channels)&&(o.channels={...t.channels,...n.channels}),o}async function Ee(){let t=V(),n=_(),o=[],r=L__default.default.join(t.Path.data,"config");o.push(r),o.push(n.directory);try{let{Filesystem:s}=await import('@easbot/utils'),i=await Array.fromAsync(s.up({targets:[".easbot"],start:n.directory}));o.push(...i);let f=process.env.HOME||process.env.USERPROFILE;if(f){let O=L__default.default.join(f,".easbot");fs.existsSync(O)&&o.push(O);}}catch{l.debug("Filesystem utils not available, using basic directories");}let a=process.env.EASBOT_CONFIG_DIR;return a&&(o.push(a),l.debug("Using EASBOT_CONFIG_DIR",{path:a})),[...new Set(o)]}async function Fe(t){let n=["gateway.jsonc","gateway.json"],o={};for(let r of n){let a=L__default.default.join(t,r),s=await J(a);if(s)if("gateway"in s&&s.gateway){let i=s.gateway;"server"in i||"channels"in i||"cluster"in i?o=h(o,i):o=h(o,{server:i}),l.debug("Loaded gateway config from file (nested format)",{file:a});}else "gateway"in s||(o=h(o,s),l.debug("Loaded gateway config from file (direct format)",{file:a}));}return o}async function Re(t){let n=["easbot.jsonc","easbot.json"];for(let o of n){let r=L__default.default.join(t,o),a=await J(r);if(a?.gateway)return l.debug("Loaded gateway config from easbot.json",{file:r}),a.gateway}return {}}function _e(){return C||(C=async()=>{let t=await Ee(),n={};for(let r of t){let a=await Fe(r);n=h(n,a);let s=await Re(r);n=h(n,s);}let o=N(n);return p=o,l.info("Gateway config loaded",{directories:t.length,port:o.server?.port,logLevel:o.logLevel}),{config:o,directories:t}}),C}async function c(){return _e()()}async function F(t){return t&&(C=void 0,p=null),(await c()).config}function B(){return p}function M(){return p!==null}function oe(){C=void 0,p=null,l.debug("Config cache cleared");}function ie(){return _().directory}async function ae(){let{config:t}=await c();return I.parse({...H,...t.server})}async function re(t){let{config:n}=await c(),o=n.channels?.[t],a={telegram:m.parse(Te),slack:y.parse(Ge),feishu:b.parse(Pe),wechat:S.parse(ke),webchat:w.parse(Ie),signal:A.parse(Ne),nostr:x.parse(Le)}[t];if(!a)throw new Error(`Unknown channel type: ${t}`);return {...a,...o}}async function se(){let{config:t}=await c();return v.parse({...Se,...t.connectionPool})}async function ce(){let{config:t}=await c();return T.parse({...we,...t.messageQueue})}async function fe(){let{config:t}=await c();return G.parse({...Ae,...t.circuitBreaker})}async function le(){let{config:t}=await c();return P.parse({...xe,...t.agentRegistry})}async function ge(){let{config:t}=await c();return k.parse({...ve,...t.session})}async function ue(){let{config:t}=await c();return t.server?.https}async function pe(){let{config:t}=await c();return t.server?.auth}async function Ce(){let{config:t}=await c();return t.cluster}async function he(){let{config:t}=await c();return t.defaultAgent}async function Q(){let{config:t}=await c();return t.logLevel??"INFO"}exports.A=ie;exports.B=ae;exports.C=re;exports.D=se;exports.E=ce;exports.F=fe;exports.G=le;exports.H=ge;exports.I=ue;exports.J=pe;exports.K=Ce;exports.L=he;exports.M=Q;exports.N=K;exports.O=q;exports.P=$e;exports.Q=V;exports.R=_;exports.a=m;exports.b=y;exports.c=b;exports.d=S;exports.e=w;exports.f=A;exports.g=x;exports.h=D;exports.i=j;exports.j=z;exports.k=U;exports.l=W;exports.m=v;exports.n=T;exports.o=G;exports.p=P;exports.q=k;exports.r=I;exports.s=d;exports.t=N;exports.u=ne;exports.v=H;exports.w=F;exports.x=B;exports.y=M;exports.z=oe;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import {P,R}from'./chunk-YU4JG7OA.mjs';import {a as a$1}from'./chunk-6TRL3CVJ.mjs';import {a as a$2}from'./chunk-HAMGVOQD.mjs';var a=a$1.create({service:"gateway"}),c=class t extends Error{constructor(r,y,m){super(r);a$2(this,"type");a$2(this,"stage");a$2(this,"cause");this.type="GatewayInitializationError",this.stage=y,this.cause=m,Error.captureStackTrace&&Error.captureStackTrace(this,t);}};function E(t){let s=["ECONNRESET","EPIPE","ETIMEDOUT","ECONNREFUSED","ENOTFOUND","EHOSTUNREACH","ENETUNREACH"],r=t.code;return s.includes(r??"")}function C(){process.on("unhandledRejection",t=>{let s=t instanceof Error?t:new Error(String(t));E(s)||a.error("unhandled promise rejection",{error:s.message,stack:s.stack});}),process.on("uncaughtException",t=>{E(t)||(a.error("uncaught exception",{error:t.message,stack:t.stack}),process.exit(1));});}var h;(k=>{let t={initialized:false,initPromise:null,directory:".easbot",config:null},s,r={server:null,config:null,initialized:false,status:"stopped"};async function y(o={}){if(t.initialized)return;if(t.initPromise)return t.initPromise;let i={directory:o.directory??".easbot",printLogs:o.printLogs??false,logLevel:o.logLevel??"INFO"};return t.directory=i.directory,t.initPromise=(async()=>{let{Global:n}=await import('./global-ZNJCD2DC.mjs');try{await n.init();}catch(e){throw new c("Failed to initialize global directories","global",e instanceof Error?e:void 0)}try{await a$1.init({logDir:n.Path.log,print:i.printLogs,level:i.logLevel});}catch(e){throw new c("Failed to initialize log infrastructure","log",e instanceof Error?e:void 0)}try{C();}catch(e){throw new c("Failed to install unhandled exception handlers","server",e instanceof Error?e:void 0)}try{let e=P();t.config=await e.load(i.directory);}catch(e){throw new c("Failed to load gateway configuration","config",e instanceof Error?e:void 0)}t.initialized=true,a.info("gateway runtime initialized",{directory:i.directory,logLevel:i.logLevel});})(),t.initPromise}k.init=y;function m(){return t.initialized}k.isInitialized=m;function S(){return s||(s=async()=>{if(!t.initialized)throw new c("Gateway not initialized. Call init() first.","server");let o=P(),i=R(),n=await o.load(i.directory);return n.server?.enabled?(r={server:null,config:n,initialized:true,status:"stopped"},a.info("gateway initialized",{port:n.server?.port,hostname:n.server?.hostname}),r):(a.debug("gateway server disabled or not configured"),{server:null,config:n,initialized:true,status:"stopped"})}),s}function u(){return S()()}k.state=u;async function z(){return (await u()).server}k.get=z;async function R$1(){return (await u()).config?.server?.enabled??false}k.isEnabled=R$1;async function L(){return (await u()).config}k.config=L;function N(){return r.status}k.getStatus=N;async function b(o){t.initialized||await y();let i=await u();if(!(i.config?.server?.enabled??true))throw a.warn("gateway server is disabled"),new Error("Gateway server is disabled");if(r.status==="running"){a.info("gateway server is already running");return}if(r.status==="starting"){a.info("gateway server is starting");return}r.status="starting";try{let e={...i.config?.server,...o};a.debug("gateway start: creating server with config",{port:e.port,hostname:e.hostname,path:e.path,https:e.https?.enabled});let g=await G(e);a.debug("gateway start: server created, updating state"),r.server=g,r.status="running",r.error=void 0,a.info("gateway server started",{port:e.port,hostname:e.hostname});}catch(e){throw r.status="error",r.error=e instanceof Error?e.message:String(e),a.error("failed to start gateway server",{error:r.error}),e}}k.start=b;async function P$1(){if(r.status==="stopped"){a.info("gateway server is already stopped");return}if(r.status==="stopping"){a.info("gateway server is stopping");return}r.status="stopping";try{r.server&&await r.server.stop(),r.server=null,r.status="stopped",r.error=void 0,a.info("gateway server stopped");}catch(o){throw r.status="error",r.error=o instanceof Error?o.message:String(o),a.error("failed to stop gateway server",{error:r.error}),o}}k.stop=P$1;async function T(o){a.info("restarting gateway server"),(r.status==="running"||r.status==="starting")&&await P$1(),await b(o);}k.restart=T;async function I(){let o=P(),i=R(),n=await o.load(i.directory);return r.config=n,t.config=n,a.info("gateway config reloaded"),n}k.reloadConfig=I;async function G(o,i){let{GatewayServer:n}=await import('./server-C5W7D4CU.mjs'),e=new n(o);a.debug("createGatewayServer: created server instance, calling start()");let g=i?.startupTimeout??3e4;if(await(async()=>{let l=new Promise((d,x)=>{setTimeout(()=>{x(new Error(`Gateway server startup timeout after ${g}ms`));},g);});try{a.debug("createGatewayServer: calling server.start()"),await Promise.race([e.start(),l]),a.debug("createGatewayServer: server.start() completed");}catch(d){a.error("gateway server start failed, attempting cleanup",{error:d instanceof Error?d.message:String(d)});try{await e.stop();}catch{}throw d}})(),i?.onStarted)try{await i.onStarted(e);}catch(l){a.warn("server started but onStarted callback failed",{error:l instanceof Error?l.message:String(l)});}return a.info("gateway server created and started",{port:o.port,hostname:o.hostname}),e}k.createGatewayServer=G;})(h||(h={}));export{h as a};
|
|
@@ -1,2 +0,0 @@
|
|
|
1
|
-
import {v as v$1,Q as Q$1,N as N$1,O}from'./chunk-YU4JG7OA.mjs';import {a}from'./chunk-6TRL3CVJ.mjs';import {a as a$1}from'./chunk-HAMGVOQD.mjs';import*as f from'fs';import f__default from'fs';import*as W from'path';import {createServer}from'@easbot/utils';import {WebSocketServer}from'ws';import Q from'https';import V from'http';import {HookEvent}from'@easbot/plugin';var I=class{constructor(e={}){a$1(this,"log",a.create({service:"gateway:lock"}));a$1(this,"config");a$1(this,"locks",new Map);a$1(this,"cleanupTimer",null);this.config={defaultTimeout:e.defaultTimeout??6e4,checkInterval:e.checkInterval??1e4};}tryAcquire(e,t,s,n){let r=this.locks.get(e);if(r)if(this.isLockExpired(r))this.log.warn("lock expired, releasing",{sessionId:e,oldHolder:r.holderId,newHolder:s}),this.locks.delete(e);else return this.log.debug("lock already held",{sessionId:e,holder:r.holderId,messageId:r.messageId}),false;let a={sessionId:e,messageId:t,holderId:s,acquiredAt:Date.now(),timeout:n??this.config.defaultTimeout};return this.locks.set(e,a),this.log.debug("lock acquired",{sessionId:e,messageId:t,subscriberId:s}),true}release(e,t){let s=this.locks.get(e);return s?s.holderId!==t?(this.log.warn("cannot release lock held by another",{sessionId:e,holder:s.holderId,requester:t}),false):(this.locks.delete(e),this.log.debug("lock released",{sessionId:e,subscriberId:t}),true):(this.log.debug("no lock to release",{sessionId:e}),false)}forceRelease(e){let t=this.locks.get(e);return t?(this.locks.delete(e),this.log.warn("lock force released",{sessionId:e,holder:t.holderId,messageId:t.messageId}),true):false}isLockExpired(e){return Date.now()-e.acquiredAt>e.timeout}getLock(e){return this.locks.get(e)}isLocked(e){let t=this.locks.get(e);return t?this.isLockExpired(t)?(this.locks.delete(e),false):true:false}getHolder(e){let t=this.locks.get(e);if(!(!t||this.isLockExpired(t)))return t.holderId}startCleanup(){this.cleanupTimer||(this.cleanupTimer=setInterval(()=>{this.cleanupExpired();},this.config.checkInterval),this.log.info("lock cleanup started",{interval:this.config.checkInterval}));}stopCleanup(){this.cleanupTimer&&(clearInterval(this.cleanupTimer),this.cleanupTimer=null,this.log.info("lock cleanup stopped"));}cleanupExpired(){let e=0;for(let[t,s]of this.locks)this.isLockExpired(s)&&(this.locks.delete(t),e++,this.log.warn("expired lock cleaned up",{sessionId:t,holder:s.holderId,messageId:s.messageId}));return e>0&&this.log.info("cleaned up expired locks",{count:e}),e}getAllLocks(){return [...this.locks.values()]}getLockCount(){return this.locks.size}};var k=class{constructor(e={}){a$1(this,"log",a.create({service:"gateway:retry"}));a$1(this,"policy");a$1(this,"retryQueue",new Map);a$1(this,"retryTimer",null);a$1(this,"handlers",new Map);this.policy={maxRetries:e.maxRetries??3,initialDelay:e.initialDelay??1e3,maxDelay:e.maxDelay??3e4,backoffMultiplier:e.backoffMultiplier??2};}registerHandler(e,t){this.handlers.set(e,t);}unregisterHandler(e){this.handlers.delete(e);}scheduleRetry(e,t){let s=e.id,n=this.retryQueue.get(s);if(n||(n={messageId:s,retryCount:0,nextRetryAt:0,errors:[]},this.retryQueue.set(s,n)),n.errors.push({error:t.message,timestamp:Date.now()}),n.retryCount>=this.policy.maxRetries)return this.log.error("max retries exceeded",{messageId:s,retryCount:n.retryCount,maxRetries:this.policy.maxRetries}),false;let r=Math.min(this.policy.initialDelay*this.policy.backoffMultiplier**n.retryCount,this.policy.maxDelay);return n.retryCount++,n.nextRetryAt=Date.now()+r,this.log.warn("retry scheduled",{messageId:s,retryCount:n.retryCount,delay:r,error:t.message}),true}getPendingRetries(){let e=Date.now(),t=[];for(let s of this.retryQueue.values())s.nextRetryAt<=e&&s.retryCount<=this.policy.maxRetries&&t.push(s);return t}async executeRetry(e){if(!this.handlers.get(e.messageId))return this.log.warn("no handler for retry",{messageId:e.messageId}),false;this.retryQueue.delete(e.messageId);try{return this.log.info("executing retry",{messageId:e.messageId,retryCount:e.retryCount}),!0}catch(s){return this.log.error("retry execution failed",{messageId:e.messageId,error:String(s)}),false}}clearRetry(e){this.retryQueue.delete(e),this.handlers.delete(e),this.log.debug("retry cleared",{messageId:e});}startProcessing(){this.retryTimer||(this.retryTimer=setInterval(async()=>{let e=this.getPendingRetries();for(let t of e)await this.executeRetry(t);},1e3),this.log.info("retry processing started"));}stopProcessing(){this.retryTimer&&(clearInterval(this.retryTimer),this.retryTimer=null,this.log.info("retry processing stopped"));}getStats(){let e=0;for(let t of this.retryQueue.values())e+=t.errors.length;return {pendingCount:this.retryQueue.size,totalErrors:e}}};function L(c,e,t,s){return {id:`msg_${Date.now()}_${Math.random().toString(36).slice(2,9)}`,sessionId:c,type:e,content:t,metadata:{channel:{platform:s?.channel?.platform??"api",channelId:s?.channel?.channelId??"default",userId:s?.channel?.userId,chatId:s?.channel?.chatId,...Object.fromEntries(Object.entries(s?.channel??{}).filter(([n])=>!["platform","channelId","userId","chatId"].includes(n)))},context:s?.context,agent:s?.agent},timestamp:Date.now()}}function B(c,e,t,s){return L(c,e,[{type:"text",text:t}],s)}function R(c){let e=[c.platform,c.channelId];return c.chatId&&e.push(c.chatId),c.userId&&e.push(c.userId),e.join("_")}function E(){return {status:"active",messageCount:0,lastMessageAt:null}}function x(c,e){return `sub_${c}_${e}`}var D=v$1,S={maxConnections:1e3,connectionTimeout:6e4,sessionExpireMs:3e5};var q={type:"token",enabled:true,tokens:[],onInvalid:"reject",allowAnonymous:false,defaultPermissions:[]};var F={heartbeatInterval:3e4,heartbeatTimeout:9e4,heartbeatCheckInterval:1e4,autoDeregisterTimeout:3e5,maxAgents:100,maxRegistrations:1e3};var U={mode:"both",interval:3e4,pushEvents:["register","deregister","status_change"],conflictResolution:"latest",remoteNodes:[]};var $={maxConnectionsPerChannel:100,idleTimeout:3e5,reuseStrategy:"lru",healthCheckInterval:6e4,acquireTimeout:5e3,warmupCount:0};var P=class{constructor(){a$1(this,"log",a.create({service:"gateway:router"}));a$1(this,"subscribers",new Map);a$1(this,"subscriptions",new Map);a$1(this,"sessionSubscriptions",new Map);a$1(this,"channelPlugins",new Map);a$1(this,"lockManager");a$1(this,"retryManager");a$1(this,"cancelHandlers",new Set);this.lockManager=new I,this.retryManager=new k,this.lockManager.startCleanup();}onCancel(e){this.cancelHandlers.add(e);}offCancel(e){this.cancelHandlers.delete(e);}async emitCancel(e){for(let t of this.cancelHandlers)try{await t(e);}catch(s){this.log.error("cancel handler error",{error:String(s)});}}async route(e){this.log.debug("routing message",{id:e.id,sessionId:e.sessionId,type:e.type});let t=this.sessionSubscriptions.get(e.sessionId);if(!t||t.size===0)return this.log.debug("no subscribers for session",{sessionId:e.sessionId}),{success:true,broadcastCount:0,dispatchedToSubAgent:false};let s=0,n=null,r=[];for(let a of t){let g=this.subscriptions.get(a);if(!g)continue;let l=this.subscribers.get(g.subscriberId);if(l){if(e.type==="input"){if(!this.lockManager.tryAcquire(e.sessionId,e.id,l.id)){this.log.debug("skipping subscriber, lock held by another",{subscriberId:l.id,holder:this.lockManager.getHolder(e.sessionId)});continue}n=l.id;}try{await l.connection.send(e),s++,l.lastActiveAt=Date.now();}catch(p){this.log.warn("failed to send message to subscriber",{subscriberId:l.id,error:String(p)}),r.push(p instanceof Error?p:new Error(String(p))),n===l.id&&(this.lockManager.release(e.sessionId,l.id),n=null);}}}return this.log.debug("message routed",{id:e.id,broadcastCount:s,errorCount:r.length,processedBy:n}),{success:r.length===0,broadcastCount:s,dispatchedToSubAgent:false,error:r.length>0?r.map(a=>a.message).join("; "):void 0}}async completeMessage(e,t,s,n="completed"){this.lockManager.release(e,s),await this.emitCancel({sessionId:e,messageId:t,processedBy:s,reason:n,timestamp:Date.now()}),this.log.info("message completed",{sessionId:e,messageId:t,subscriberId:s,reason:n});}scheduleRetry(e,t){return this.retryManager.scheduleRetry(e,t)}async subscribe(e,t){let s=x(t.id,e);if(this.subscriptions.has(s))return this.log.debug("already subscribed",{subscriberId:t.id,sessionId:e}),this.subscriptions.get(s);let n={id:s,subscriberId:t.id,sessionId:e,createdAt:Date.now()};return this.subscribers.has(t.id)||this.subscribers.set(t.id,t),this.subscriptions.set(s,n),this.sessionSubscriptions.has(e)||this.sessionSubscriptions.set(e,new Set),this.sessionSubscriptions.get(e).add(s),t.sessions.add(e),this.log.info("subscribed to session",{subscriberId:t.id,sessionId:e,subscriptionId:s}),n}async unsubscribe(e){let t=this.subscriptions.get(e);if(!t){this.log.warn("subscription not found",{subscriptionId:e});return}this.subscriptions.delete(e);let s=this.sessionSubscriptions.get(t.sessionId);s&&(s.delete(e),s.size===0&&this.sessionSubscriptions.delete(t.sessionId));let n=this.subscribers.get(t.subscriberId);n&&n.sessions.delete(t.sessionId),this.log.info("unsubscribed from session",{subscriberId:t.subscriberId,sessionId:t.sessionId,subscriptionId:e});}async unsubscribeAll(e){let t=this.subscribers.get(e);if(!t)return;let s=[...t.sessions];for(let n of s){let r=x(e,n);await this.unsubscribe(r);}this.subscribers.delete(e),this.log.info("unsubscribed all sessions",{subscriberId:e});}async registerChannel(e){if(this.channelPlugins.has(e.id)){this.log.warn("channel plugin already registered",{id:e.id});return}this.channelPlugins.set(e.id,e),this.log.info("channel plugin registered",{id:e.id,platform:e.platform});}async unregisterChannel(e){let t=this.channelPlugins.get(e);if(!t){this.log.warn("channel plugin not found",{id:e});return}await t.stop(),this.channelPlugins.delete(e),this.log.info("channel plugin unregistered",{id:e});}getSubscriberCount(e){let t=this.sessionSubscriptions.get(e);return t?t.size:0}getAllSubscribers(){return [...this.subscribers.values()]}getChannelPlugin(e){return this.channelPlugins.get(e)}getAllChannelPlugins(){return [...this.channelPlugins.values()]}getLockManager(){return this.lockManager}getRetryManager(){return this.retryManager}async shutdown(){this.lockManager.stopCleanup(),this.retryManager.stopProcessing(),this.log.info("router shutdown");}};var b=class{constructor(e={}){a$1(this,"log",a.create({service:"gateway:message-store"}));a$1(this,"config");a$1(this,"messages",new Map);a$1(this,"sessionMessages",new Map);this.config={storagePath:e.storagePath??"./data/gateway-messages",maxRetentionDays:e.maxRetentionDays??30,maxRecords:e.maxRecords??1e5};}async store(e){let t={message:e,status:"pending",processedBy:null,processedAt:null,retryCount:0,error:null,createdAt:Date.now(),updatedAt:Date.now()};return this.messages.set(e.id,t),this.sessionMessages.has(e.sessionId)||this.sessionMessages.set(e.sessionId,new Set),this.sessionMessages.get(e.sessionId).add(e.id),this.log.debug("message stored",{id:e.id,sessionId:e.sessionId,type:e.type}),t}get(e){return this.messages.get(e)}async updateStatus(e,t,s={}){let n=this.messages.get(e);if(!n){this.log.warn("message not found for status update",{messageId:e});return}n.status=t,n.updatedAt=Date.now(),s.processedBy&&(n.processedBy=s.processedBy),(t==="completed"||t==="failed")&&(n.processedAt=Date.now()),s.error&&(n.error=s.error),this.log.debug("message status updated",{id:e,status:t,processedBy:s.processedBy});}incrementRetry(e){let t=this.messages.get(e);return t?(t.retryCount++,t.updatedAt=Date.now(),t.retryCount):0}isProcessed(e){let t=this.messages.get(e);return t?t.status==="completed"||t.status==="processing":false}getSessionHistory(e,t=100){let s=this.sessionMessages.get(e);if(!s)return [];let n=[];for(let r of s){let a=this.messages.get(r);a&&n.push(a);}return n.sort((r,a)=>r.createdAt-a.createdAt),n.slice(-t)}getPendingMessages(e){let t=[];for(let s of this.messages.values())s.status==="pending"&&(!e||s.message.sessionId===e)&&t.push(s);return t}getFailedMessages(e=3){let t=[];for(let s of this.messages.values())s.status==="failed"&&s.retryCount<e&&t.push(s);return t}async cleanup(){let e=Date.now(),t=this.config.maxRetentionDays*24*60*60*1e3,s=[];for(let[n,r]of this.messages)e-r.createdAt>t&&s.push(n);for(let n of s){let r=this.messages.get(n);if(r){let a=this.sessionMessages.get(r.message.sessionId);a&&a.delete(n),this.messages.delete(n);}}return s.length>0&&this.log.info("cleaned up expired messages",{count:s.length}),s.length}getStats(){let e={total:this.messages.size,pending:0,processing:0,completed:0,failed:0,cancelled:0};for(let t of this.messages.values())e[t.status]++;return e}};var v=class{constructor(e={}){a$1(this,"log",a.create({service:"gateway:session-store"}));a$1(this,"config");a$1(this,"sessions",new Map);a$1(this,"channelSessions",new Map);a$1(this,"autoSaveTimer",null);a$1(this,"isDirty",false);this.config={storagePath:e.storagePath??"./data/gateway-sessions",autoSaveInterval:e.autoSaveInterval??6e4,maxRetentionDays:e.maxRetentionDays??7,enablePersistence:e.enablePersistence??true};}async initialize(){if(!this.config.enablePersistence){this.log.info("session persistence disabled");return}await this.ensureStorageDir(),await this.load(),this.startAutoSave(),this.log.info("session store initialized",{sessionCount:this.sessions.size,storagePath:this.config.storagePath});}async close(){this.stopAutoSave(),this.isDirty&&await this.save(),this.log.info("session store closed");}async set(e){this.sessions.set(e.id,e),this.channelSessions.has(e.channel.channelId)||this.channelSessions.set(e.channel.channelId,new Set),this.channelSessions.get(e.channel.channelId).add(e.id),this.markDirty();}get(e){return this.sessions.get(e)}async delete(e){let t=this.sessions.get(e);if(!t)return;this.sessions.delete(e);let s=this.channelSessions.get(t.channel.channelId);s&&(s.delete(e),s.size===0&&this.channelSessions.delete(t.channel.channelId)),this.markDirty();}getAll(){return [...this.sessions.values()]}getByChannel(e){let t=this.channelSessions.get(e);return t?[...t].map(s=>this.sessions.get(s)).filter(s=>s!==void 0):[]}size(){return this.sessions.size}markDirty(){this.isDirty=true;}async save(){if(!this.config.enablePersistence)return;let e=this.getSessionFilePath(),t=this.serializeAll();try{await this.ensureStorageDir(),await f.promises.writeFile(e,JSON.stringify(t,null,2),"utf-8"),this.isDirty=!1,this.log.debug("sessions saved",{count:t.length});}catch(s){throw this.log.error("failed to save sessions",{error:s}),s}}async load(){if(!this.config.enablePersistence)return;let e=this.getSessionFilePath();try{if(!f.existsSync(e)){this.log.debug("no saved sessions found");return}let t=await f.promises.readFile(e,"utf-8"),s=JSON.parse(t);this.sessions.clear(),this.channelSessions.clear();let n=Date.now(),r=this.config.maxRetentionDays*24*60*60*1e3;for(let a of s){if(n-a.lastActiveAt>r)continue;let g={id:a.id,channel:a.channel,backendSessionId:a.backendSessionId,subscribers:new Set(a.subscribers),state:a.state,createdAt:a.createdAt,lastActiveAt:a.lastActiveAt};this.sessions.set(g.id,g),this.channelSessions.has(g.channel.channelId)||this.channelSessions.set(g.channel.channelId,new Set),this.channelSessions.get(g.channel.channelId).add(g.id);}this.log.info("sessions loaded",{loaded:this.sessions.size,skipped:s.length-this.sessions.size});}catch(t){this.log.error("failed to load sessions",{error:t});}}serializeAll(){let e=[];for(let t of this.sessions.values())e.push(this.serialize(t));return e}serialize(e){return {id:e.id,channel:e.channel,backendSessionId:e.backendSessionId,subscribers:[...e.subscribers],state:e.state,createdAt:e.createdAt,lastActiveAt:e.lastActiveAt}}getSessionFilePath(){return W.join(this.config.storagePath,"sessions.json")}async ensureStorageDir(){f.existsSync(this.config.storagePath)||await f.promises.mkdir(this.config.storagePath,{recursive:true});}startAutoSave(){this.autoSaveTimer||(this.autoSaveTimer=setInterval(async()=>{this.isDirty&&await this.save();},this.config.autoSaveInterval));}stopAutoSave(){this.autoSaveTimer&&(clearInterval(this.autoSaveTimer),this.autoSaveTimer=null);}async cleanup(){let e=Date.now(),t=this.config.maxRetentionDays*24*60*60*1e3,s=[];for(let n of this.sessions.values())e-n.lastActiveAt>t&&s.push(n.id);for(let n of s)await this.delete(n);return s.length>0&&(this.log.info("cleaned up expired sessions",{count:s.length}),await this.save()),s.length}getStats(){let e={total:this.sessions.size,byStatus:{active:0,idle:0,closed:0},byPlatform:{}};for(let t of this.sessions.values()){e.byStatus[t.state.status]++;let s=t.channel.platform;e.byPlatform[s]=(e.byPlatform[s]||0)+1;}return e}};var C=class{constructor(e={}){a$1(this,"log",a.create({service:"gateway:session"}));a$1(this,"sessionStore");a$1(this,"config");a$1(this,"initialized",false);this.config={enablePersistence:e.enablePersistence??true,storagePath:e.storagePath??"./data/gateway-sessions",autoSaveInterval:e.autoSaveInterval??6e4,maxRetentionDays:e.maxRetentionDays??7},this.sessionStore=new v({storagePath:this.config.storagePath,autoSaveInterval:this.config.autoSaveInterval,maxRetentionDays:this.config.maxRetentionDays,enablePersistence:this.config.enablePersistence});}async initialize(){this.initialized||(await this.sessionStore.initialize(),this.initialized=true,this.log.info("session manager initialized",{sessionCount:this.sessionStore.size(),persistenceEnabled:this.config.enablePersistence}));}async close(){await this.sessionStore.close(),this.log.info("session manager closed");}async getOrCreate(e,t={}){let s=R(e),n=this.sessionStore.get(s);return n?(n.lastActiveAt=Date.now(),this.log.debug("session found",{sessionId:s}),n):(n={id:s,channel:e,backendSessionId:null,subscribers:new Set,state:E(),createdAt:Date.now(),lastActiveAt:Date.now()},await this.sessionStore.set(n),this.log.info("session created",{sessionId:s,platform:e.platform,channelId:e.channelId,chatId:e.chatId,userId:e.userId}),n)}get(e){return this.sessionStore.get(e)}async closeSession(e){let t=this.sessionStore.get(e);if(!t){this.log.warn("session not found",{sessionId:e});return}t.state.status="closed",await this.sessionStore.delete(e),this.log.info("session closed",{sessionId:e});}async closeById(e){return this.closeSession(e)}async bindBackendSession(e,t){let s=this.sessionStore.get(e);if(!s)throw new Error(`Session not found: ${e}`);s.backendSessionId=t,await this.sessionStore.set(s),this.log.info("backend session bound",{gatewaySessionId:e,backendSessionId:t});}async unbindBackendSession(e){let t=this.sessionStore.get(e);if(!t){this.log.warn("session not found",{sessionId:e});return}t.backendSessionId=null,await this.sessionStore.set(t),this.log.info("backend session unbound",{gatewaySessionId:e});}getState(e){return this.sessionStore.get(e)?.state}async incrementMessageCountOrCreate(e,t){let s=this.sessionStore.get(e);if(!s){let n=t??{platform:"api",channelId:"default"};s=await this.getOrCreate(n);}return s.state.messageCount++,s.state.lastMessageAt=Date.now(),s.lastActiveAt=Date.now(),await this.sessionStore.set(s),s}async incrementMessageCount(e){let t=this.sessionStore.get(e);t&&(t.state.messageCount++,t.state.lastMessageAt=Date.now(),t.lastActiveAt=Date.now(),await this.sessionStore.set(t));}async addSubscriber(e,t){let s=this.sessionStore.get(e);if(!s){this.log.warn("session not found",{sessionId:e});return}s.subscribers.add(t),await this.sessionStore.set(s),this.log.debug("subscriber added to session",{sessionId:e,subscriberId:t});}async removeSubscriber(e,t){let s=this.sessionStore.get(e);s&&(s.subscribers.delete(t),await this.sessionStore.set(s),this.log.debug("subscriber removed from session",{sessionId:e,subscriberId:t}));}query(e){let t=this.sessionStore.getAll();return e.platform&&(t=t.filter(s=>s.channel.platform===e.platform)),e.channelId&&(t=t.filter(s=>s.channel.channelId===e.channelId)),e.userId&&(t=t.filter(s=>s.channel.userId===e.userId)),e.status&&(t=t.filter(s=>s.state.status===e.status)),t}getSessionsByChannel(e){return this.sessionStore.getByChannel(e)}getAllSessions(){return this.sessionStore.getAll()}getSessionCount(){return this.sessionStore.size()}async cleanupIdleSessions(e){let t=Date.now(),s=[];for(let n of this.sessionStore.getAll())n.state.status==="idle"&&t-n.lastActiveAt>e&&s.push(n.id);for(let n of s)await this.closeSession(n);return s.length>0&&this.log.info("cleaned up idle sessions",{count:s.length}),s.length}async save(){this.log.debug("session save triggered");}getStats(){return this.sessionStore.getStats()}};var w=class{constructor(e={}){a$1(this,"log",a.create({service:"gateway:plugin-loader"}));a$1(this,"plugins",new Map);a$1(this,"messageHandler",null);a$1(this,"healthCheckTimer",null);a$1(this,"config");this.config={pluginDir:e.pluginDir??"./plugins",autoDiscover:e.autoDiscover??false,healthCheckInterval:e.healthCheckInterval??6e4,healthCheckTimeout:e.healthCheckTimeout??5e3};}setMessageHandler(e){this.messageHandler=e;}async register(e,t){if(this.plugins.has(e.id)){this.log.warn("plugin already registered",{id:e.id});return}this.plugins.set(e.id,{plugin:e,config:t,started:false}),this.log.info("plugin registered",{id:e.id,platform:e.platform,name:e.name});}async unregister(e){let t=this.plugins.get(e);if(!t){this.log.warn("plugin not found",{id:e});return}t.started&&await this.stop(e),this.plugins.delete(e),this.log.info("plugin unregistered",{id:e});}async start(e){let t=this.plugins.get(e);if(!t)throw new Error(`Plugin not found: ${e}`);if(t.started){this.log.warn("plugin already started",{id:e});return}if(!this.messageHandler)throw new Error("Message handler not set");if(!t.config.enabled){this.log.warn("plugin is disabled",{id:e});return}this.log.info("starting plugin",{id:e});try{await t.plugin.start(t.config,this.messageHandler),t.started=!0,this.log.info("plugin started",{id:e});}catch(s){throw this.log.error("failed to start plugin",{id:e,error:String(s)}),s}}async stop(e){let t=this.plugins.get(e);if(!t){this.log.warn("plugin not found",{id:e});return}if(!t.started){this.log.warn("plugin not started",{id:e});return}this.log.info("stopping plugin",{id:e});try{await t.plugin.stop(),t.started=!1,this.log.info("plugin stopped",{id:e});}catch(s){throw this.log.error("failed to stop plugin",{id:e,error:String(s)}),s}}async startAll(){let e=[];for(let[t,s]of this.plugins){if(!s.config.enabled){this.log.debug("skipping disabled plugin",{id:t});continue}try{await this.start(t);}catch(n){e.push({id:t,error:n instanceof Error?n:new Error(String(n))});}}e.length>0&&this.log.warn("some plugins failed to start",{count:e.length,errors:e.map(t=>({id:t.id,error:t.error.message}))});}async stopAll(){let e=[];for(let[t,s]of this.plugins)if(s.started)try{await this.stop(t);}catch(n){e.push({id:t,error:n instanceof Error?n:new Error(String(n))});}e.length>0&&this.log.warn("some plugins failed to stop",{count:e.length,errors:e.map(t=>({id:t.id,error:t.error.message}))});}getPlugin(e){return this.plugins.get(e)?.plugin}getAllPlugins(){return [...this.plugins.values()].map(e=>e.plugin)}getRunningPlugins(){return [...this.plugins.values()].filter(e=>e.started).map(e=>e.plugin)}isRunning(e){return this.plugins.get(e)?.started??false}async healthCheck(e){let t=this.plugins.get(e);if(!t)return {healthy:false,lastCheck:Date.now(),error:"Plugin not found"};try{let s=await Promise.race([t.plugin.healthCheck(),new Promise((n,r)=>setTimeout(()=>r(new Error("Health check timeout")),this.config.healthCheckTimeout))]);return t.lastHealthCheck=s,s}catch(s){let n={healthy:false,lastCheck:Date.now(),error:s instanceof Error?s.message:String(s)};return t.lastHealthCheck=n,n}}async healthCheckAll(){let e=new Map;for(let[t]of this.plugins)e.set(t,await this.healthCheck(t));return e}startHealthCheckTimer(){if(this.healthCheckTimer){this.log.warn("health check timer already running");return}this.healthCheckTimer=setInterval(async()=>{let e=await this.healthCheckAll();for(let[t,s]of e)s.healthy||this.log.warn("plugin health check failed",{id:t,error:s.error});},this.config.healthCheckInterval),this.log.info("health check timer started",{interval:this.config.healthCheckInterval});}stopHealthCheckTimer(){this.healthCheckTimer&&(clearInterval(this.healthCheckTimer),this.healthCheckTimer=null,this.log.info("health check timer stopped"));}async sendToPlugin(e,t){let s=this.plugins.get(e);if(!s)throw new Error(`Plugin not found: ${e}`);if(!s.started)throw new Error(`Plugin not started: ${e}`);return s.plugin.send(t)}};var o=a.create({service:"gateway:websocket-server"});function K(){return `ws_${Date.now()}_${Math.random().toString(36).substring(2,9)}`}function A(c,e){for(let[t,s]of e.entries())if(s===c)return t;return null}var M=class{constructor(e={}){a$1(this,"config");a$1(this,"server",null);a$1(this,"wss",null);a$1(this,"httpsServer",null);a$1(this,"httpRedirectServer",null);a$1(this,"connections",new Map);a$1(this,"subscriptions",new Map);a$1(this,"sessionSubscriptions",new Map);a$1(this,"sessionLastActivity",new Map);a$1(this,"cleanupInterval",null);a$1(this,"heartbeatInterval",null);a$1(this,"startTime",0);a$1(this,"running",false);this.config={...D,...e};}async start(){if(this.running){o.warn("WebSocket server is already running");return}o.debug("WebSocket server start: checking configuration",{port:this.config.port,hostname:this.config.hostname,path:this.config.path,https:this.config.https?.enabled??false}),this.startTime=Date.now();let e={onOpen:(n,r)=>{this.handleConnection(r);},onMessage:async(n,r)=>{try{let a=JSON.parse(n.data.toString());await this.handleMessage(r,a);}catch(a){o.error("Error processing WebSocket message",{error:a.message}),this.sendError(r,a.message);}},onClose:(n,r)=>{this.handleDisconnection(r);},onError:(n,r)=>{o.error("WebSocket error",{error:n.type});}},t=this.config.https?.enabled??false;o.debug("WebSocket server start: choosing server type",{https:t});try{t?(o.debug("WebSocket server start: starting HTTPS server"),await this.startHTTPSServer(e)):(o.debug("WebSocket server start: starting HTTP server"),await this.startHTTPServer(e)),o.debug("WebSocket server start: HTTP/HTTPS server started successfully");}catch(n){throw o.error("WebSocket server start: HTTP/HTTPS server failed",{error:n instanceof Error?n.message:String(n)}),n}o.debug("WebSocket server start: server initialization complete"),this.cleanupInterval=setInterval(()=>this.cleanupExpiredSessions(),3e4),this.heartbeatInterval=setInterval(()=>this.checkConnectionTimeout(),this.config.heartbeatInterval),this.running=true;let s=t?"wss":"ws";o.info("WebSocket server started",{url:`${s}://${this.config.hostname}:${this.config.port}${this.config.path}`});}async startHTTPServer(e){o.debug("HTTP server start: creating server",{port:this.config.port,hostname:this.config.hostname}),this.server=createServer({port:this.config.port,hostname:this.config.hostname,routes:this.createRoutes(),timeout:this.config.connectionTimeout,cors:true}),o.debug("HTTP server start: server created, checking raw server");let t=this.server.raw;t?(o.debug("HTTP server start: raw server available, setting up WebSocket upgrade"),this.setupWebSocketUpgrade(t,e),o.debug("HTTP server start: WebSocket upgrade configured")):o.warn("HTTP server start: raw server is undefined");}async startHTTPSServer(e){let t=this.config.https,s=this.loadSSLCertificates(t);if(!s){o.error("Failed to load SSL certificates, falling back to HTTP"),await this.startHTTPServer(e);return}this.server=createServer({port:this.config.port,hostname:this.config.hostname,routes:this.createRoutes(),timeout:this.config.connectionTimeout,cors:true});let n=this.server.raw;this.httpsServer=Q.createServer(s,n.listeners("request")[0]),this.setupWebSocketUpgrade(this.httpsServer,e),await new Promise(r=>{this.httpsServer.listen(this.config.port,this.config.hostname,()=>{o.info("HTTPS server started",{port:this.config.port,hostname:this.config.hostname}),r();});}),t.forceRedirect&&t.httpPort&&this.startHTTPRedirectServer(t.httpPort);}loadSSLCertificates(e){try{return e.cert&&e.key?{cert:e.cert,key:e.key}:e.certPath&&e.keyPath?{cert:f__default.readFileSync(e.certPath),key:f__default.readFileSync(e.keyPath)}:null}catch(t){return o.error("Failed to load SSL certificates",{error:t.message}),null}}setupWebSocketUpgrade(e,t){this.wss=new WebSocketServer({noServer:true}),e.on("upgrade",(s,n,r)=>{try{let g=new URL(s.url||"/",`http://${s.headers.host}`).pathname;(g===this.config.path||this.config.path==="/"&&(g==="/"||g===""))&&this.wss.handleUpgrade(s,n,r,l=>{t.onOpen?.({type:"open"},l),l.on("message",p=>{t.onMessage?.({data:p},l);}),l.on("close",(p,G)=>{t.onClose?.({code:p,reason:G},l);}),l.on("error",p=>{t.onError?.({type:"error",message:p.message},l);});});}catch{}});}startHTTPRedirectServer(e){this.httpRedirectServer=V.createServer((t,s)=>{let r=`https://${t.headers.host?.split(":")[0]||this.config.hostname}:${this.config.port}${t.url}`;o.debug("Redirecting HTTP to HTTPS",{from:t.url,to:r}),s.writeHead(301,{Location:r,"Content-Type":"text/plain"}),s.end(`Redirecting to ${r}`);}),this.httpRedirectServer.listen(e,this.config.hostname,()=>{o.info("HTTP redirect server started",{port:e,hostname:this.config.hostname});});}async stop(){if(!this.running){o.warn("WebSocket server is not running");return}o.info("Stopping WebSocket server"),this.cleanupInterval&&(clearInterval(this.cleanupInterval),this.cleanupInterval=null),this.heartbeatInterval&&(clearInterval(this.heartbeatInterval),this.heartbeatInterval=null);for(let[e,t]of this.connections.entries())try{t.close(1e3,"Server shutting down");}catch(s){o.debug("Error closing connection",{clientId:e,error:String(s)});}this.connections.clear(),this.subscriptions.clear(),this.sessionSubscriptions.clear(),this.sessionLastActivity.clear(),this.wss&&(this.wss.close(),this.wss=null),this.httpsServer&&(await new Promise(e=>{this.httpsServer.close(()=>e());}),this.httpsServer=null),this.httpRedirectServer&&(await new Promise(e=>{this.httpRedirectServer.close(()=>e());}),this.httpRedirectServer=null),this.server&&(await this.server.stop(),this.server=null),this.running=false,o.info("WebSocket server stopped");}broadcastToSession(e,t){this.touchSession(e);let s=this.sessionSubscriptions.get(e);if(!s||s.size===0){o.debug("No clients subscribed to session",{sessionId:e});return}let n=t?.properties?.sessionID;if(typeof n=="string"&&n&&n!==e)return;let r=JSON.stringify({type:"event",sessionId:e,payload:t});for(let a of s){let g=this.subscriptions.get(a);if(g&&g.ws.readyState===g.ws.OPEN&&g.subscribedSessions.has(e)){if(g.subscribedEventTypes.size>0&&!g.subscribedEventTypes.has(t.type)){o.debug("Event type not subscribed, skipping",{clientId:a,sessionId:e,eventType:t.type,subscribedTypes:Array.from(g.subscribedEventTypes)});continue}g.ws.send(r),o.debug("Event pushed to client",{clientId:a,sessionId:e,eventType:t.type});}}}getConnectionCount(){return this.connections.size}getSubscriptionCount(){return this.sessionSubscriptions.size}getConnectionInfos(){let e=[];for(let[t,s]of this.subscriptions.entries())e.push({clientId:t,state:s.ws.readyState===s.ws.OPEN?"connected":"disconnected",connectedAt:s.connectedAt,lastActivityAt:s.lastActivityAt,authInfo:s.authInfo});return e}isRunning(){return this.running}handleConnection(e){if(this.connections.size>=(this.config.maxConnections??S.maxConnections)){o.warn("Maximum connections reached, rejecting new connection"),e.close(1013,"Maximum connections reached");return}let t=K();this.connections.set(t,e),this.subscriptions.set(t,{clientId:t,ws:e,subscribedSessions:new Set,subscribedEventTypes:new Set,connectedAt:Date.now(),lastActivityAt:Date.now()}),o.info("WebSocket client connected",{clientId:t,totalConnections:this.connections.size}),this.sendMessage(e,{type:"connected",clientId:t,payload:{version:"1.0.0",capabilities:["subscribe","message","interrupt","releaseSubscription"]}});}handleDisconnection(e){let t=A(e,this.connections);if(!t)return;this.connections.delete(t);let s=this.subscriptions.get(t);if(s){for(let n of s.subscribedSessions){let r=this.sessionSubscriptions.get(n);r&&(r.delete(t),r.size===0&&this.sessionSubscriptions.delete(n));}this.subscriptions.delete(t);}o.info("WebSocket client disconnected",{clientId:t,totalConnections:this.connections.size});}async handleMessage(e,t){let s=A(e,this.connections);if(!s){this.sendError(e,"Client not found");return}let n=this.subscriptions.get(s);switch(n&&(n.lastActivityAt=Date.now()),t.type){case "ping":this.sendMessage(e,{type:"pong"});break;case "initialize":await this.handleInitialize(e,t);break;case "subscribe":await this.handleSubscribe(e,t);break;case "unsubscribe":await this.handleUnsubscribe(e,t);break;case "message":await this.handleMessageRequest(e,t);break;case "interrupt":await this.handleInterrupt(e,t);break;case "releaseSubscription":await this.handleReleaseSubscription(e,t);break;default:this.sendError(e,`Unknown message type: ${t.type}`);}}async handleInitialize(e,t){this.sendMessage(e,{type:"initialize_response",success:true,result:{capabilities:["subscribe","message","interrupt","releaseSubscription"]}});}async handleSubscribe(e,t){if(t.type!=="subscribe"||!t.sessionId){this.sendError(e,"Invalid subscribe request");return}let s=A(e,this.connections);if(!s){this.sendError(e,"Client not found");return}let n=this.subscriptions.get(s);if(!n){this.sendError(e,"Subscription not found");return}if(n.subscribedSessions.add(t.sessionId),this.sessionSubscriptions.has(t.sessionId)||this.sessionSubscriptions.set(t.sessionId,new Set),this.sessionSubscriptions.get(t.sessionId).add(s),this.touchSession(t.sessionId),t.payload?.eventTypes)for(let r of t.payload.eventTypes)n.subscribedEventTypes.add(r);o.info("Client subscribed to session",{clientId:s,sessionId:t.sessionId}),this.sendMessage(e,{type:"subscribed",sessionId:t.sessionId});}async handleUnsubscribe(e,t){if(t.type!=="unsubscribe"||!t.sessionId){this.sendError(e,"Invalid unsubscribe request");return}let s=A(e,this.connections);if(!s)return;let n=this.subscriptions.get(s);if(!n)return;n.subscribedSessions.delete(t.sessionId);let r=this.sessionSubscriptions.get(t.sessionId);r&&(r.delete(s),r.size===0&&this.sessionSubscriptions.delete(t.sessionId)),o.info("Client unsubscribed from session",{clientId:s,sessionId:t.sessionId}),this.sendMessage(e,{type:"unsubscribed",sessionId:t.sessionId});}async handleMessageRequest(e,t){if(t.type!=="message"||!t.sessionId||!t.payload){this.sendError(e,"Invalid message request");return}o.debug("Message received",{sessionId:t.sessionId}),this.sendMessage(e,{type:"response",sessionId:t.sessionId,success:true,result:{received:true}});}async handleInterrupt(e,t){if(t.type!=="interrupt"||!t.sessionId){this.sendError(e,"Invalid interrupt request");return}o.info("Interrupt requested via WebSocket",{sessionId:t.sessionId}),this.sendMessage(e,{type:"interrupted",sessionId:t.sessionId,success:true});}async handleReleaseSubscription(e,t){if(t.type!=="releaseSubscription"||!t.sessionId){this.sendError(e,"Invalid releaseSubscription request");return}o.info("Release subscription requested via WebSocket",{sessionId:t.sessionId}),this.releaseSessionResources(t.sessionId),this.sendMessage(e,{type:"subscriptionReleased",sessionId:t.sessionId});}touchSession(e){this.sessionLastActivity.set(e,Date.now());}checkConnectionTimeout(){let e=Date.now(),t=this.config.connectionTimeout??S.connectionTimeout,s=[];for(let[n,r]of this.subscriptions.entries())e-r.lastActivityAt>t&&s.push(n);for(let n of s){let r=this.subscriptions.get(n);if(r){o.info("Closing inactive connection",{clientId:n,lastActivityAt:r.lastActivityAt,timeoutMs:t});try{r.ws.close(1001,"Connection timeout");}catch(a){o.debug("Error closing timed out connection",{clientId:n,error:String(a)});}}}s.length>0&&o.debug("Closed timed out connections",{count:s.length,remainingConnections:this.connections.size-s.length});}cleanupExpiredSessions(){let e=Date.now(),t=[],s=this.config.sessionExpireMs??S.sessionExpireMs;for(let[n,r]of this.sessionLastActivity.entries())e-r>s&&t.push(n);for(let n of t)o.info("Session expired due to inactivity, releasing resources",{sessionId:n,lastActivity:this.sessionLastActivity.get(n),expireMs:s}),this.releaseSessionResources(n),this.sessionLastActivity.delete(n);t.length>0&&o.debug("Cleaned up expired sessions",{count:t.length,remainingActive:this.sessionLastActivity.size});}releaseSessionResources(e){let t=this.sessionSubscriptions.get(e);if(t){for(let s of t){let n=this.subscriptions.get(s);n&&n.ws.readyState===n.ws.OPEN&&this.sendMessage(n.ws,{type:"session_closed",sessionId:e,payload:{reason:"Session resources released due to expiration or explicit release"}});}for(let s of t){let n=this.subscriptions.get(s);n&&n.subscribedSessions.delete(e);}this.sessionSubscriptions.delete(e),o.info("Session resources released",{sessionId:e,clientCount:t.size});}}sendMessage(e,t){e.readyState===e.OPEN&&e.send(JSON.stringify(t));}sendError(e,t,s){this.sendMessage(e,{type:"error",message:t,code:s});}createRoutes(){return [{method:"get",path:"/health",handler:e=>{let t={status:"healthy",timestamp:Date.now(),version:"1.0.0"};return e.json(t)}},{method:"get",path:"/status",handler:e=>{let t={status:this.running?"running":"stopped",connections:this.connections.size,sessions:this.sessionSubscriptions.size,subscriptions:this.subscriptions.size,uptime:this.running?Date.now()-this.startTime:0,version:"1.0.0"};return e.json(t)}},{method:"post",path:"/initialize",handler:async e=>{let t=await e.req.json().catch(()=>({}));return o.debug("ACP initialize request received",{body:t}),e.json({success:true,result:{capabilities:["subscribe","message","interrupt","releaseSubscription"],version:"1.0.0"}})}},{method:"post",path:"/session",handler:async e=>{await e.req.json().catch(()=>({}));let n={id:`session_${Date.now()}_${Math.random().toString(36).substring(2,9)}`,createdAt:Date.now()};return e.json(n)}},{method:"post",path:"/session/:sessionId/prompt",handler:async e=>{let t=e.req.param("sessionId");await e.req.json().catch(()=>({}));if(!t)return e.json({error:"No sessionId provided"},400);let r={messageId:`msg_${Date.now()}_${Math.random().toString(36).substring(2,9)}`,sessionId:t,createdAt:Date.now()};return e.status(202),e.json(r)}},{method:"get",path:"/subscriptions",handler:e=>{let t=Array.from(this.sessionSubscriptions.entries()).map(([n,r])=>({sessionId:n,subscriberCount:r.size,subscribers:Array.from(r)})),s={subscriptions:t,total:t.length};return e.json(s)}}]}};function J(c){let e={platform:c.channel?.platform??"api",channelId:c.channel?.channelId??"unknown",chatId:c.channel?.chatId,userId:c.channel?.userId};return {id:c.messageId??`msg_${Date.now()}_${Math.random().toString(36).slice(2,9)}`,sessionId:c.sessionId,type:c.type,content:Array.isArray(c.content)?c.content:[{type:"text",text:String(c.content??"")}],metadata:{channel:e,replyToMessageId:c.replyToMessageId},timestamp:c.timestamp??Date.now()}}var N=class{constructor(e){a$1(this,"log",a.create({service:"gateway:server"}));a$1(this,"config");a$1(this,"router");a$1(this,"sessionManager");a$1(this,"pluginLoader");a$1(this,"messageStore");a$1(this,"wsServer",null);a$1(this,"running",false);a$1(this,"connections",0);this.config=e,this.router=new P,this.sessionManager=new C,this.pluginLoader=new w({healthCheckInterval:6e4});let t=Q$1(),s=N$1();this.messageStore=new b({storagePath:`${t.Path.data}/gateway-messages`}),s&&O().setMessageStorage({store:async r=>{let a=J(r);await this.messageStore.store(a);},updateStatus:async(r,a,g)=>{await this.messageStore.updateStatus(r,a,g);}}),this.pluginLoader.setMessageHandler({onMessage:n=>this.onMessage(n),onEvent:n=>this.onEvent(n)});}async start(){if(this.running){this.log.warn("gateway server is already running");return}this.log.info("starting gateway server",{port:this.config.port,hostname:this.config.hostname}),await this.sessionManager.initialize(),this.log.debug("starting WebSocket server"),this.wsServer=new M({port:this.config.port,hostname:this.config.hostname,path:String(this.config.path??"/")}),await this.wsServer.start(),this.log.debug("WebSocket server started"),await this.pluginLoader.startAll(),this.pluginLoader.startHealthCheckTimer(),this.running=true,this.log.info("gateway server started");}async stop(){if(!this.running){this.log.warn("gateway server is not running");return}this.log.info("stopping gateway server"),this.wsServer&&(this.log.debug("stopping WebSocket server"),await this.wsServer.stop(),this.wsServer=null,this.log.debug("WebSocket server stopped")),this.pluginLoader.stopHealthCheckTimer(),await this.pluginLoader.stopAll(),await this.router.shutdown(),await this.sessionManager.close(),this.running=false,this.log.info("gateway server stopped");}getStatus(){return {running:this.running,port:this.config.port,hostname:this.config.hostname,connections:this.connections}}async onMessage(e){if(this.log.debug("message received",{id:e.id,sessionId:e.sessionId,type:e.type}),this.messageStore.isProcessed(e.id)){this.log.warn("message already processed, skipping",{id:e.id});return}await this.storeMessage(e),await this.sessionManager.incrementMessageCountOrCreate(e.sessionId,e.metadata.channel),(await this.router.route(e)).broadcastCount===0&&e.type==="input"&&this.log.debug("no subscribers, message pending",{id:e.id});}async storeMessage(e){if(await this.messageStore.store(e),N$1())try{let t=O(),s={name:HookEvent.GatewayMessageReceive,data:{messageId:e.id,sessionId:e.sessionId,type:e.type,content:e.content,channel:e.metadata.channel,replyToMessageId:e.metadata.replyToMessageId,timestamp:e.timestamp||Date.now()}};await t.hookRegistry.emit(s);}catch{this.log.debug("hook trigger skipped (no context)");}}async processMessage(e,t,s,n={}){if(await this.messageStore.updateStatus(t,"processing",{processedBy:s}),N$1())try{await O().hookRegistry.emit({name:HookEvent.GatewayMessageProcess,data:{messageId:t,sessionId:e,processorId:s,processType:n.processType||"direct",agent:n.agent,timestamp:Date.now()}});}catch{this.log.debug("hook trigger skipped (no context)");}}async completeMessage(e,t,s,n={}){let{success:r=true,error:a,responseMessageId:g,tokens:l,duration:p}=n;if(await this.messageStore.updateStatus(t,r?"completed":"failed",{processedBy:s,error:a}),N$1())try{await O().hookRegistry.emit({name:HookEvent.GatewayMessageComplete,data:{messageId:t,sessionId:e,processorId:s,status:r?"completed":"failed",error:a,responseMessageId:g,tokens:l,duration:p,timestamp:Date.now()}});}catch{this.log.debug("hook trigger skipped (no context)");}await this.router.completeMessage(e,t,s,r?"completed":"failed");}async onEvent(e){switch(this.log.debug("channel event",{type:e.type,channelId:e.channelId}),e.type){case "connected":this.connections++;break;case "disconnected":this.connections--;break;case "error":this.log.error("channel error",{channelId:e.channelId,data:e.data});break}}async registerChannel(e,t){let s=t??{enabled:true,platform:{},session:{autoCreate:true,resetPolicy:"onNewConversation"},routing:{broadcast:true,dispatchToSubAgent:false}};await this.pluginLoader.register(e,s),this.running&&await this.pluginLoader.start(e.id);}async unregisterChannel(e){await this.pluginLoader.unregister(e);}async subscribe(e,t){await this.router.subscribe(e,t),await this.sessionManager.addSubscriber(e,t.id);}async unsubscribe(e){await this.router.unsubscribe(e);}async send(e){await this.onMessage(e);}async sendToChannel(e,t,s={}){let{proactive:n=false,replyToMessageId:r}=s;if(N$1())try{await O().hookRegistry.emit({name:HookEvent.GatewayMessageSend,data:{messageId:t.id,sessionId:t.sessionId,type:t.type,channel:t.metadata.channel,proactive:n,replyToMessageId:r,timestamp:Date.now()}});}catch{this.log.debug("hook trigger skipped (no context)");}return this.pluginLoader.sendToPlugin(e,t)}async sendProactiveMessage(e,t,s,n){let r={id:`msg_${Date.now()}_${Math.random().toString(36).slice(2,9)}`,sessionId:t,type:"output",content:s,metadata:{channel:n},timestamp:Date.now()};return this.sendToChannel(e,r,{proactive:true})}getSessionManager(){return this.sessionManager}getRouter(){return this.router}getPluginLoader(){return this.pluginLoader}getMessageStore(){return this.messageStore}async getMessageHistory(e,t){return this.messageStore.getSessionHistory(e,t)}};
|
|
2
|
-
export{I as a,k as b,L as c,B as d,R as e,E as f,x as g,D as h,q as i,F as j,U as k,$ as l,P as m,b as n,v as o,C as p,w as q,M as r,N as s};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
'use strict';var chunk4PHQN25U_cjs=require('./chunk-4PHQN25U.cjs'),chunk5ETPYYQR_cjs=require('./chunk-5ETPYYQR.cjs'),chunkGY3SWWW3_cjs=require('./chunk-GY3SWWW3.cjs');var a=chunk5ETPYYQR_cjs.a.create({service:"gateway"}),c=class t extends Error{constructor(r,y,m){super(r);chunkGY3SWWW3_cjs.a(this,"type");chunkGY3SWWW3_cjs.a(this,"stage");chunkGY3SWWW3_cjs.a(this,"cause");this.type="GatewayInitializationError",this.stage=y,this.cause=m,Error.captureStackTrace&&Error.captureStackTrace(this,t);}};function E(t){let s=["ECONNRESET","EPIPE","ETIMEDOUT","ECONNREFUSED","ENOTFOUND","EHOSTUNREACH","ENETUNREACH"],r=t.code;return s.includes(r??"")}function C(){process.on("unhandledRejection",t=>{let s=t instanceof Error?t:new Error(String(t));E(s)||a.error("unhandled promise rejection",{error:s.message,stack:s.stack});}),process.on("uncaughtException",t=>{E(t)||(a.error("uncaught exception",{error:t.message,stack:t.stack}),process.exit(1));});}exports.a=void 0;(k=>{let t={initialized:false,initPromise:null,directory:".easbot",config:null},s,r={server:null,config:null,initialized:false,status:"stopped"};async function y(o={}){if(t.initialized)return;if(t.initPromise)return t.initPromise;let i={directory:o.directory??".easbot",printLogs:o.printLogs??false,logLevel:o.logLevel??"INFO"};return t.directory=i.directory,t.initPromise=(async()=>{let{Global:n}=await import('./global-LIE6RHEM.cjs');try{await n.init();}catch(e){throw new c("Failed to initialize global directories","global",e instanceof Error?e:void 0)}try{await chunk5ETPYYQR_cjs.a.init({logDir:n.Path.log,print:i.printLogs,level:i.logLevel});}catch(e){throw new c("Failed to initialize log infrastructure","log",e instanceof Error?e:void 0)}try{C();}catch(e){throw new c("Failed to install unhandled exception handlers","server",e instanceof Error?e:void 0)}try{let e=chunk4PHQN25U_cjs.P();t.config=await e.load(i.directory);}catch(e){throw new c("Failed to load gateway configuration","config",e instanceof Error?e:void 0)}t.initialized=true,a.info("gateway runtime initialized",{directory:i.directory,logLevel:i.logLevel});})(),t.initPromise}k.init=y;function m(){return t.initialized}k.isInitialized=m;function S(){return s||(s=async()=>{if(!t.initialized)throw new c("Gateway not initialized. Call init() first.","server");let o=chunk4PHQN25U_cjs.P(),i=chunk4PHQN25U_cjs.R(),n=await o.load(i.directory);return n.server?.enabled?(r={server:null,config:n,initialized:true,status:"stopped"},a.info("gateway initialized",{port:n.server?.port,hostname:n.server?.hostname}),r):(a.debug("gateway server disabled or not configured"),{server:null,config:n,initialized:true,status:"stopped"})}),s}function u(){return S()()}k.state=u;async function z(){return (await u()).server}k.get=z;async function R(){return (await u()).config?.server?.enabled??false}k.isEnabled=R;async function L(){return (await u()).config}k.config=L;function N(){return r.status}k.getStatus=N;async function b(o){t.initialized||await y();let i=await u();if(!(i.config?.server?.enabled??true))throw a.warn("gateway server is disabled"),new Error("Gateway server is disabled");if(r.status==="running"){a.info("gateway server is already running");return}if(r.status==="starting"){a.info("gateway server is starting");return}r.status="starting";try{let e={...i.config?.server,...o};a.debug("gateway start: creating server with config",{port:e.port,hostname:e.hostname,path:e.path,https:e.https?.enabled});let g=await G(e);a.debug("gateway start: server created, updating state"),r.server=g,r.status="running",r.error=void 0,a.info("gateway server started",{port:e.port,hostname:e.hostname});}catch(e){throw r.status="error",r.error=e instanceof Error?e.message:String(e),a.error("failed to start gateway server",{error:r.error}),e}}k.start=b;async function P(){if(r.status==="stopped"){a.info("gateway server is already stopped");return}if(r.status==="stopping"){a.info("gateway server is stopping");return}r.status="stopping";try{r.server&&await r.server.stop(),r.server=null,r.status="stopped",r.error=void 0,a.info("gateway server stopped");}catch(o){throw r.status="error",r.error=o instanceof Error?o.message:String(o),a.error("failed to stop gateway server",{error:r.error}),o}}k.stop=P;async function T(o){a.info("restarting gateway server"),(r.status==="running"||r.status==="starting")&&await P(),await b(o);}k.restart=T;async function I(){let o=chunk4PHQN25U_cjs.P(),i=chunk4PHQN25U_cjs.R(),n=await o.load(i.directory);return r.config=n,t.config=n,a.info("gateway config reloaded"),n}k.reloadConfig=I;async function G(o,i){let{GatewayServer:n}=await import('./server-HQL7QNZY.cjs'),e=new n(o);a.debug("createGatewayServer: created server instance, calling start()");let g=i?.startupTimeout??3e4;if(await(async()=>{let l=new Promise((d,x)=>{setTimeout(()=>{x(new Error(`Gateway server startup timeout after ${g}ms`));},g);});try{a.debug("createGatewayServer: calling server.start()"),await Promise.race([e.start(),l]),a.debug("createGatewayServer: server.start() completed");}catch(d){a.error("gateway server start failed, attempting cleanup",{error:d instanceof Error?d.message:String(d)});try{await e.stop();}catch{}throw d}})(),i?.onStarted)try{await i.onStarted(e);}catch(l){a.warn("server started but onStarted callback failed",{error:l instanceof Error?l.message:String(l)});}return a.info("gateway server created and started",{port:o.port,hostname:o.hostname}),e}k.createGatewayServer=G;})(exports.a||(exports.a={}));
|
|
@@ -1,2 +0,0 @@
|
|
|
1
|
-
import {a}from'./chunk-6TRL3CVJ.mjs';import e from'zod';import L from'path';import {existsSync}from'fs';import Se from'fs/promises';var X=a.create({service:"gateway:config"});e.enum(["telegram","slack","feishu","wechat","webchat","signal","nostr"]);e.enum(["low","medium","high","critical"]);var m=e.object({enabled:e.boolean().default(true),botToken:e.string().describe("Telegram Bot Token"),webhookUrl:e.string().optional().describe("Webhook URL for receiving updates"),maxConnections:e.number().int().positive().optional().default(100),timeout:e.number().int().positive().optional().default(3e4)}),y=e.object({enabled:e.boolean().default(true),botToken:e.string().describe("Slack Bot Token (xoxb-*)"),appToken:e.string().optional().describe("Slack App Token (xapp-*) for Socket Mode"),signingSecret:e.string().optional().describe("Slack Signing Secret for webhook verification"),maxConnections:e.number().int().positive().optional().default(100),timeout:e.number().int().positive().optional().default(3e4)}),b=e.object({enabled:e.boolean().default(true),appId:e.string().describe("Feishu App ID"),appSecret:e.string().describe("Feishu App Secret"),encryptKey:e.string().optional().describe("Feishu Encrypt Key for message encryption"),verificationToken:e.string().optional().describe("Feishu Verification Token"),maxConnections:e.number().int().positive().optional().default(100),timeout:e.number().int().positive().optional().default(3e4)}),S=e.object({enabled:e.boolean().default(true),mode:e.enum(["official","wecom"]).default("official"),appId:e.string().describe("WeChat App ID"),appSecret:e.string().describe("WeChat App Secret"),token:e.string().optional().describe("WeChat Token for message verification"),encodingAESKey:e.string().optional().describe("WeChat Encoding AES Key"),agentId:e.string().optional().describe("WeCom Agent ID"),maxConnections:e.number().int().positive().optional().default(100),timeout:e.number().int().positive().optional().default(3e4)}),w=e.object({enabled:e.boolean().default(true),path:e.string().optional().default("/"),heartbeatInterval:e.number().int().positive().optional().default(3e4),connectionTimeout:e.number().int().positive().optional().default(6e4),maxConnections:e.number().int().positive().optional().default(1e3),cors:e.boolean().optional().default(true)}),A=e.object({enabled:e.boolean().default(true),serviceUrl:e.string().optional().default("https://chat.signal.org"),phoneNumber:e.string().describe("Signal phone number"),password:e.string().optional().describe("Signal account password"),maxConnections:e.number().int().positive().optional().default(100),timeout:e.number().int().positive().optional().default(3e4)}),x=e.object({enabled:e.boolean().default(true),relays:e.array(e.string()).min(1).describe("Nostr relay URLs"),privateKey:e.string().optional().describe("Nostr private key (hex)"),publicKey:e.string().optional().describe("Nostr public key (hex)"),maxConnections:e.number().int().positive().optional().default(100),timeout:e.number().int().positive().optional().default(3e4)}),j=e.object({telegram:m.optional(),slack:y.optional(),feishu:b.optional(),wechat:S.optional(),webchat:w.optional(),signal:A.optional(),nostr:x.optional()}),z=e.object({enabled:e.boolean().default(false),cert:e.string().describe("Path to SSL certificate file (\u6216 certPath)"),key:e.string().describe("Path to SSL key file (\u6216 keyPath)"),certPath:e.string().optional().describe("Path to SSL certificate file (alias of cert)"),keyPath:e.string().optional().describe("Path to SSL key file (alias of key)"),ca:e.string().optional().describe("Path to CA certificate file"),forceRedirect:e.boolean().optional().default(false).describe("Force redirect HTTP to HTTPS"),httpPort:e.number().int().positive().optional().default(80).describe("HTTP redirect port")}),Z=e.object({name:e.string().describe("Token name/description"),value:e.string().describe("Token value"),permissions:e.array(e.string()).default(["*"]).describe("Associated permissions"),expiresAt:e.number().int().positive().optional().describe("Expiration timestamp (Unix timestamp)"),description:e.string().optional().describe("Token description")}),ee=e.object({enabled:e.boolean().default(true),tokens:e.array(Z).default([]).describe("List of valid tokens"),onInvalid:e.enum(["reject","anonymous"]).default("reject").describe("Behavior when token validation fails"),allowAnonymous:e.boolean().default(false).describe("Allow anonymous access"),defaultPermissions:e.array(e.string()).default([]).describe("Default permissions for anonymous users")}),te=e.object({enabled:e.boolean().default(false),authorizationServer:e.string().describe("OAuth2 authorization server URL"),clientId:e.string().describe("OAuth2 client ID"),clientSecret:e.string().describe("OAuth2 client secret"),tokenValidationEndpoint:e.string().describe("OAuth2 token validation endpoint"),scope:e.array(e.string()).default([]).describe("OAuth2 scopes")}),U=e.object({type:e.enum(["token","oauth2"]).default("token"),token:ee.optional(),oauth2:te.optional()}),W=e.object({mode:e.enum(["pull","push","both"]).default("both"),interval:e.number().int().positive().default(3e4),pushEvents:e.array(e.enum(["register","deregister","heartbeat","status_change"])).default(["register","deregister","status_change"]),conflictResolution:e.enum(["latest","local","remote"]).default("latest")}),ne=e.object({id:e.string().describe("Gateway node ID"),address:e.string().describe("Gateway node address (e.g., http://localhost:3001)"),enabled:e.boolean().default(true),metadata:e.record(e.string(),e.any()).optional()}),H=e.object({nodeId:e.string().describe("Current gateway node ID"),nodes:e.array(ne).default([]).describe("Other gateway nodes in the cluster"),sync:W.optional()}),v=e.object({maxConnectionsPerChannel:e.number().int().positive().default(100),idleTimeout:e.number().int().positive().default(3e5),reuseStrategy:e.enum(["lru","fifo","random"]).default("lru"),healthCheckInterval:e.number().int().positive().default(6e4),acquireTimeout:e.number().int().positive().default(5e3),warmupCount:e.number().int().nonnegative().default(0).describe("Number of connections to warm up")}),T=e.object({maxLength:e.number().int().positive().default(1e4),concurrency:e.number().int().positive().default(100),batchSize:e.number().int().positive().default(10),batchTimeout:e.number().int().positive().default(100),priorityLevels:e.number().int().positive().default(4)}),G=e.object({failureThreshold:e.number().int().positive().default(5),successThreshold:e.number().int().positive().default(3),timeout:e.number().int().positive().default(3e4),halfOpenDuration:e.number().int().positive().default(15e3)}),P=e.object({heartbeatInterval:e.number().int().positive().default(3e4),heartbeatTimeout:e.number().int().positive().default(9e4),heartbeatCheckInterval:e.number().int().positive().default(1e4),autoDeregisterTimeout:e.number().int().positive().default(3e5),maxAgents:e.number().int().positive().default(100),persistenceFile:e.string().optional().describe("Path to persist agent registrations"),maxRegistrations:e.number().int().positive().default(1e3)}),k=e.object({sessionTimeout:e.number().int().positive().default(18e5),resumeGracePeriod:e.number().int().positive().default(3e5),maxSessions:e.number().int().positive().default(1e4),persistenceFile:e.string().optional().describe("Path to persist sessions")}),I=e.object({enabled:e.boolean().default(true),port:e.number().int().positive().default(8080),hostname:e.string().default("localhost"),path:e.string().optional().default("/"),maxConnections:e.number().int().positive().optional().default(1e3),connectionTimeout:e.number().int().positive().optional().default(6e4),heartbeatInterval:e.number().int().positive().optional().default(3e4),sessionExpireMs:e.number().int().positive().optional().default(3e5),https:z.optional(),auth:U.optional(),cors:e.boolean().default(true),corsOrigins:e.array(e.string()).optional(),mdns:e.boolean().default(false),mdnsDomain:e.string().optional().default("gateway.easbot.local")}),d=e.object({server:I.optional(),channels:j.optional(),cluster:H.optional(),connectionPool:v.optional(),messageQueue:T.optional(),circuitBreaker:G.optional(),agentRegistry:P.optional(),session:k.optional(),defaultAgent:e.string().optional().describe("Default agent ID for routing"),logLevel:e.enum(["DEBUG","INFO","WARN","ERROR"]).optional().default("INFO")}).catchall(e.unknown());function N(t){let n=d.safeParse(t);return n.success||X.warn("Invalid gateway config",{issues:n.error.issues}),n.success?n.data:d.parse({})}function oe(t){return d.parse(t)}a.create({service:"gateway:interfaces"});var g;function q(){return g!==void 0}function V(){throw new Error("Agent adapter not set. Call setAgentAdapter() before using Gateway.");}var me={Path:{data:".easbot"}},ye={directory:process.cwd()},_=class{async load(n){return R(n)}},be=new _;function Xe(){return be}function Y(){return me}function O(){return ye}var l=a.create({service:"gateway:config"}),B={enabled:true,port:8080,hostname:"localhost",path:"/",maxConnections:1e3,connectionTimeout:6e4,heartbeatInterval:3e4,sessionExpireMs:3e5,cors:true,mdns:false,mdnsDomain:"gateway.easbot.local",https:{enabled:false,cert:"",key:"",certPath:void 0,keyPath:void 0,ca:void 0,forceRedirect:false,httpPort:80}},we={maxConnectionsPerChannel:100,idleTimeout:3e5,reuseStrategy:"lru",healthCheckInterval:6e4,acquireTimeout:5e3,warmupCount:0},Ae={maxLength:1e4,concurrency:100,batchSize:10,batchTimeout:100,priorityLevels:4},xe={failureThreshold:5,successThreshold:3,timeout:3e4,halfOpenDuration:15e3},ve={heartbeatInterval:3e4,heartbeatTimeout:9e4,heartbeatCheckInterval:1e4,autoDeregisterTimeout:3e5,maxAgents:100,maxRegistrations:1e3},Te={sessionTimeout:18e5,resumeGracePeriod:3e5,maxSessions:1e4},Ge={enabled:true,botToken:"",maxConnections:100,timeout:3e4},Pe={enabled:true,botToken:"",maxConnections:100,timeout:3e4},ke={enabled:true,appId:"",appSecret:"",maxConnections:100,timeout:3e4},Ie={enabled:true,mode:"official",appId:"",appSecret:"",maxConnections:100,timeout:3e4},Ne={enabled:true,path:"/ws",heartbeatInterval:3e4,connectionTimeout:6e4,maxConnections:1e3,cors:true},Le={enabled:true,serviceUrl:"https://chat.signal.org",phoneNumber:"",maxConnections:100,timeout:3e4},Ee={enabled:true,relays:[],maxConnections:100,timeout:3e4},C,p=null;async function $(t){try{if(!existsSync(t))return null;let n=await Se.readFile(t,"utf-8"),o=n,r=!1,a=!1,s=[];for(let i=0;i<n.length;i++){let f=n[i];if(a){s.push(f),a=!1;continue}if(f==="\\"){s.push(f),a=!0;continue}if(f==='"'){r=!r,s.push(f);continue}if(!r&&f==="/"&&n[i+1]==="/"){for(;i<n.length&&n[i]!==`
|
|
2
|
-
`&&n[i]!=="\r";)i++;continue}if(!r&&f==="/"&&n[i+1]==="*"){for(i+=2;i<n.length-1;){if(n[i]==="*"&&n[i+1]==="/"){i+=2;break}i++;}continue}s.push(f);}return o=s.join("").trim(),o?JSON.parse(o):null}catch(n){return l.debug("Failed to load config file",{file:t,error:String(n)}),null}}function h(t,n){let o={...t,...n};return (t.server||n.server)&&(o.server={...t.server,...n.server}),(t.channels||n.channels)&&(o.channels={...t.channels,...n.channels}),o}async function Fe(){let t=Y(),n=O(),o=[],r=L.join(t.Path.data,"config");o.push(r),o.push(n.directory);try{let{Filesystem:s}=await import('@easbot/utils'),i=await Array.fromAsync(s.up({targets:[".easbot"],start:n.directory}));o.push(...i);let f=process.env.HOME||process.env.USERPROFILE;if(f){let D=L.join(f,".easbot");existsSync(D)&&o.push(D);}}catch{l.debug("Filesystem utils not available, using basic directories");}let a=process.env.EASBOT_CONFIG_DIR;return a&&(o.push(a),l.debug("Using EASBOT_CONFIG_DIR",{path:a})),[...new Set(o)]}async function Re(t){let n=["gateway.jsonc","gateway.json"],o={};for(let r of n){let a=L.join(t,r),s=await $(a);if(s)if("gateway"in s&&s.gateway){let i=s.gateway;"server"in i||"channels"in i||"cluster"in i?o=h(o,i):o=h(o,{server:i}),l.debug("Loaded gateway config from file (nested format)",{file:a});}else "gateway"in s||(o=h(o,s),l.debug("Loaded gateway config from file (direct format)",{file:a}));}return o}async function _e(t){let n=["easbot.jsonc","easbot.json"];for(let o of n){let r=L.join(t,o),a=await $(r);if(a?.gateway)return l.debug("Loaded gateway config from easbot.json",{file:r}),a.gateway}return {}}function Oe(){return C||(C=async()=>{let t=await Fe(),n={};for(let r of t){let a=await Re(r);n=h(n,a);let s=await _e(r);n=h(n,s);}let o=N(n);return p=o,l.info("Gateway config loaded",{directories:t.length,port:o.server?.port,logLevel:o.logLevel}),{config:o,directories:t}}),C}async function c(){return Oe()()}async function R(t){return t&&(C=void 0,p=null),(await c()).config}function M(){return p}function Q(){return p!==null}function ie(){C=void 0,p=null,l.debug("Config cache cleared");}function ae(){return O().directory}async function re(){let{config:t}=await c();return I.parse({...B,...t.server})}async function se(t){let{config:n}=await c(),o=n.channels?.[t],a={telegram:m.parse(Ge),slack:y.parse(Pe),feishu:b.parse(ke),wechat:S.parse(Ie),webchat:w.parse(Ne),signal:A.parse(Le),nostr:x.parse(Ee)}[t];if(!a)throw new Error(`Unknown channel type: ${t}`);return {...a,...o}}async function ce(){let{config:t}=await c();return v.parse({...we,...t.connectionPool})}async function fe(){let{config:t}=await c();return T.parse({...Ae,...t.messageQueue})}async function le(){let{config:t}=await c();return G.parse({...xe,...t.circuitBreaker})}async function ge(){let{config:t}=await c();return P.parse({...ve,...t.agentRegistry})}async function ue(){let{config:t}=await c();return k.parse({...Te,...t.session})}async function pe(){let{config:t}=await c();return t.server?.https}async function Ce(){let{config:t}=await c();return t.server?.auth}async function he(){let{config:t}=await c();return t.cluster}async function de(){let{config:t}=await c();return t.defaultAgent}async function K(){let{config:t}=await c();return t.logLevel??"INFO"}export{ae as A,re as B,se as C,ce as D,fe as E,le as F,ge as G,ue as H,pe as I,Ce as J,he as K,de as L,K as M,q as N,V as O,Xe as P,Y as Q,O as R,m as a,y as b,b as c,S as d,w as e,A as f,x as g,j as h,z as i,U as j,W as k,H as l,v as m,T as n,G as o,P as p,k as q,I as r,d as s,N as t,oe as u,B as v,R as w,M as x,Q as y,ie as z};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export{a as default}from'./chunk-3UUTBBIX.mjs';import'./chunk-HAMGVOQD.mjs';
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
'use strict';var chunkY7UBDKER_cjs=require('./chunk-Y7UBDKER.cjs');require('./chunk-GY3SWWW3.cjs');module.exports=chunkY7UBDKER_cjs.a;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export{q as ChannelPluginLoader,s as GatewayServer,r as GatewayWebSocketServer}from'./chunk-K2WP2GA3.mjs';import'./chunk-YU4JG7OA.mjs';import'./chunk-6TRL3CVJ.mjs';import'./chunk-HAMGVOQD.mjs';
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
'use strict';var chunk34MFQLJP_cjs=require('./chunk-34MFQLJP.cjs');require('./chunk-4PHQN25U.cjs'),require('./chunk-5ETPYYQR.cjs'),require('./chunk-GY3SWWW3.cjs');Object.defineProperty(exports,"ChannelPluginLoader",{enumerable:true,get:function(){return chunk34MFQLJP_cjs.q}});Object.defineProperty(exports,"GatewayServer",{enumerable:true,get:function(){return chunk34MFQLJP_cjs.s}});Object.defineProperty(exports,"GatewayWebSocketServer",{enumerable:true,get:function(){return chunk34MFQLJP_cjs.r}});
|