@easbot/gateway 0.2.24 → 0.2.25
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-OHUELRXK.mjs +14 -0
- package/dist/chunks/chunk-U32NADBK.cjs +14 -0
- package/dist/chunks/{global-DVGSJ3V3.mjs → global-JYEIXTJ2.mjs} +1 -1
- package/dist/chunks/{global-WNXPJXAS.cjs → global-Y53VDBKD.cjs} +1 -1
- package/dist/chunks/server-MTS5PR2P.mjs +1 -0
- package/dist/chunks/{server-ZWJPPFTK.cjs → server-ZPM3AQHL.cjs} +1 -1
- package/dist/index.cjs +2 -2
- package/dist/index.d.cts +4 -9
- package/dist/index.d.ts +4 -9
- package/dist/index.mjs +2 -2
- package/package.json +5 -5
- package/dist/chunks/chunk-GQFBXITJ.mjs +0 -5
- package/dist/chunks/chunk-LR37LRDG.cjs +0 -5
- package/dist/chunks/server-HGRZI7JT.mjs +0 -1
|
@@ -1,5 +0,0 @@
|
|
|
1
|
-
'use strict';var chunkRIWYHEQ6_cjs=require('./chunk-RIWYHEQ6.cjs'),chunkGY3SWWW3_cjs=require('./chunk-GY3SWWW3.cjs'),a=require('zod'),R=require('path'),E=require('fs'),w=require('fs/promises'),utils=require('@easbot/utils'),ws=require('ws'),ss=require('https'),ns=require('http'),plugin=require('@easbot/plugin');function _interopDefault(e){return e&&e.__esModule?e:{default:e}}function _interopNamespace(e){if(e&&e.__esModule)return e;var n=Object.create(null);if(e){Object.keys(e).forEach(function(k){if(k!=='default'){var d=Object.getOwnPropertyDescriptor(e,k);Object.defineProperty(n,k,d.get?d:{enumerable:true,get:function(){return e[k]}});}})}n.default=e;return Object.freeze(n)}var a__default=/*#__PURE__*/_interopDefault(a);var R__namespace=/*#__PURE__*/_interopNamespace(R);var E__namespace=/*#__PURE__*/_interopNamespace(E);var w__namespace=/*#__PURE__*/_interopNamespace(w);var ss__default=/*#__PURE__*/_interopDefault(ss);var ns__default=/*#__PURE__*/_interopDefault(ns);var te=class{constructor(t={}){chunkGY3SWWW3_cjs.a(this,"log",chunkRIWYHEQ6_cjs.a.create({service:"gateway:lock"}));chunkGY3SWWW3_cjs.a(this,"config");chunkGY3SWWW3_cjs.a(this,"locks",new Map);chunkGY3SWWW3_cjs.a(this,"cleanupTimer",null);this.config={defaultTimeout:t.defaultTimeout??6e4,checkInterval:t.checkInterval??1e4};}tryAcquire(t,e,s,n){let i=this.locks.get(t);if(i)if(this.isLockExpired(i))this.log.warn("lock expired, releasing",{sessionId:t,oldHolder:i.holderId,newHolder:s}),this.locks.delete(t);else return this.log.debug("lock already held",{sessionId:t,holder:i.holderId,messageId:i.messageId}),false;let r={sessionId:t,messageId:e,holderId:s,acquiredAt:Date.now(),timeout:n??this.config.defaultTimeout};return this.locks.set(t,r),this.log.debug("lock acquired",{sessionId:t,messageId:e,subscriberId:s}),true}release(t,e){let s=this.locks.get(t);return s?s.holderId!==e?(this.log.warn("cannot release lock held by another",{sessionId:t,holder:s.holderId,requester:e}),false):(this.locks.delete(t),this.log.debug("lock released",{sessionId:t,subscriberId:e}),true):(this.log.debug("no lock to release",{sessionId:t}),false)}forceRelease(t){let e=this.locks.get(t);return e?(this.locks.delete(t),this.log.warn("lock force released",{sessionId:t,holder:e.holderId,messageId:e.messageId}),true):false}isLockExpired(t){return Date.now()-t.acquiredAt>t.timeout}getLock(t){return this.locks.get(t)}isLocked(t){let e=this.locks.get(t);return e?this.isLockExpired(e)?(this.locks.delete(t),false):true:false}getHolder(t){let e=this.locks.get(t);if(!(!e||this.isLockExpired(e)))return e.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 t=0;for(let[e,s]of this.locks)this.isLockExpired(s)&&(this.locks.delete(e),t++,this.log.warn("expired lock cleaned up",{sessionId:e,holder:s.holderId,messageId:s.messageId}));return t>0&&this.log.info("cleaned up expired locks",{count:t}),t}getAllLocks(){return [...this.locks.values()]}getLockCount(){return this.locks.size}};var se=class{constructor(t={}){chunkGY3SWWW3_cjs.a(this,"log",chunkRIWYHEQ6_cjs.a.create({service:"gateway:retry"}));chunkGY3SWWW3_cjs.a(this,"policy");chunkGY3SWWW3_cjs.a(this,"retryQueue",new Map);chunkGY3SWWW3_cjs.a(this,"retryTimer",null);chunkGY3SWWW3_cjs.a(this,"handlers",new Map);this.policy={maxRetries:t.maxRetries??3,initialDelay:t.initialDelay??1e3,maxDelay:t.maxDelay??3e4,backoffMultiplier:t.backoffMultiplier??2};}registerHandler(t,e){this.handlers.set(t,e);}unregisterHandler(t){this.handlers.delete(t);}scheduleRetry(t,e){let s=t.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:e.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 i=Math.min(this.policy.initialDelay*this.policy.backoffMultiplier**n.retryCount,this.policy.maxDelay);return n.retryCount++,n.nextRetryAt=Date.now()+i,this.log.warn("retry scheduled",{messageId:s,retryCount:n.retryCount,delay:i,error:e.message}),true}getPendingRetries(){let t=Date.now(),e=[];for(let s of this.retryQueue.values())s.nextRetryAt<=t&&s.retryCount<=this.policy.maxRetries&&e.push(s);return e}async executeRetry(t){if(!this.handlers.get(t.messageId))return this.log.warn("no handler for retry",{messageId:t.messageId}),false;this.retryQueue.delete(t.messageId);try{return this.log.info("executing retry",{messageId:t.messageId,retryCount:t.retryCount}),!0}catch(s){return this.log.error("retry execution failed",{messageId:t.messageId,error:String(s)}),false}}clearRetry(t){this.retryQueue.delete(t),this.handlers.delete(t),this.log.debug("retry cleared",{messageId:t});}startProcessing(){this.retryTimer||(this.retryTimer=setInterval(async()=>{let t=this.getPendingRetries();for(let e of t)await this.executeRetry(e);},1e3),this.log.info("retry processing started"));}stopProcessing(){this.retryTimer&&(clearInterval(this.retryTimer),this.retryTimer=null,this.log.info("retry processing stopped"));}getStats(){let t=0;for(let e of this.retryQueue.values())t+=e.errors.length;return {pendingCount:this.retryQueue.size,totalErrors:t}}};function y(c,t,e,s){return {id:`msg_${Date.now()}_${Math.random().toString(36).slice(2,9)}`,sessionId:c,type:t,content:e,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 it(c,t,e,s){return y(c,t,[{type:"text",text:e}],s)}function Pe(c){let t=[c.platform,c.channelId];return c.chatId&&t.push(c.chatId),c.userId&&t.push(c.userId),t.join("_")}function xe(){return {status:"active",messageCount:0,lastMessageAt:null}}function ne(c,t){if(!c||!t)throw new Error("platform and stableId are required");return `${c}:${t}`}function ie(c){if(!c||typeof c!="string")return null;let t=c.indexOf(":");if(t<=0||t>=c.length-1)return null;let e=c.slice(0,t),s=c.slice(t+1);return !e||!s?null:{platform:e,stableId:s}}function Me(c){let{platform:t,stableId:e,profile:s={},aliases:n={},metadata:i}=c;if(!t||!e)throw new Error("platform and stableId are required");let r=Date.now();return {uid:ne(t,e),platform:t,stableId:e,aliases:{...n},profile:{...s},createdAt:r,lastSeenAt:r,metadata:i}}function Ae(c,t){return `sub_${c}_${t}`}var rt=chunkRIWYHEQ6_cjs.a.create({service:"gateway:config"});a__default.default.enum(["telegram","slack","feishu","wechat","webchat","signal","nostr"]);a__default.default.enum(["low","medium","high","critical"]);var oe=a__default.default.object({enabled:a__default.default.boolean().default(true),botToken:a__default.default.string().describe("Telegram Bot Token"),webhookUrl:a__default.default.string().optional().describe("Webhook URL for receiving updates"),maxConnections:a__default.default.number().int().positive().optional().default(100),timeout:a__default.default.number().int().positive().optional().default(3e4)}),ae=a__default.default.object({enabled:a__default.default.boolean().default(true),botToken:a__default.default.string().describe("Slack Bot Token (xoxb-*)"),appToken:a__default.default.string().optional().describe("Slack App Token (xapp-*) for Socket Mode"),signingSecret:a__default.default.string().optional().describe("Slack Signing Secret for webhook verification"),maxConnections:a__default.default.number().int().positive().optional().default(100),timeout:a__default.default.number().int().positive().optional().default(3e4)}),ce=a__default.default.object({enabled:a__default.default.boolean().default(true),appId:a__default.default.string().describe("Feishu App ID"),appSecret:a__default.default.string().describe("Feishu App Secret"),encryptKey:a__default.default.string().optional().describe("Feishu Encrypt Key for message encryption"),verificationToken:a__default.default.string().optional().describe("Feishu Verification Token"),maxConnections:a__default.default.number().int().positive().optional().default(100),timeout:a__default.default.number().int().positive().optional().default(3e4)}),le=a__default.default.object({enabled:a__default.default.boolean().default(true),mode:a__default.default.enum(["official","wecom"]).default("official"),appId:a__default.default.string().describe("WeChat App ID"),appSecret:a__default.default.string().describe("WeChat App Secret"),token:a__default.default.string().optional().describe("WeChat Token for message verification"),encodingAESKey:a__default.default.string().optional().describe("WeChat Encoding AES Key"),agentId:a__default.default.string().optional().describe("WeCom Agent ID"),maxConnections:a__default.default.number().int().positive().optional().default(100),timeout:a__default.default.number().int().positive().optional().default(3e4)}),ge=a__default.default.object({enabled:a__default.default.boolean().default(true),path:a__default.default.string().optional().default("/"),heartbeatInterval:a__default.default.number().int().positive().optional().default(3e4),connectionTimeout:a__default.default.number().int().positive().optional().default(6e4),maxConnections:a__default.default.number().int().positive().optional().default(1e3),cors:a__default.default.boolean().optional().default(true)}),de=a__default.default.object({enabled:a__default.default.boolean().default(true),serviceUrl:a__default.default.string().optional().default("https://chat.signal.org"),phoneNumber:a__default.default.string().describe("Signal phone number"),password:a__default.default.string().optional().describe("Signal account password"),maxConnections:a__default.default.number().int().positive().optional().default(100),timeout:a__default.default.number().int().positive().optional().default(3e4)}),ue=a__default.default.object({enabled:a__default.default.boolean().default(true),relays:a__default.default.array(a__default.default.string()).min(1).describe("Nostr relay URLs"),privateKey:a__default.default.string().optional().describe("Nostr private key (hex)"),publicKey:a__default.default.string().optional().describe("Nostr public key (hex)"),maxConnections:a__default.default.number().int().positive().optional().default(100),timeout:a__default.default.number().int().positive().optional().default(3e4)}),We=a__default.default.object({telegram:oe.optional(),slack:ae.optional(),feishu:ce.optional(),wechat:le.optional(),webchat:ge.optional(),signal:de.optional(),nostr:ue.optional()}),Be=a__default.default.object({enabled:a__default.default.boolean().default(false),cert:a__default.default.string().describe("Path to SSL certificate file (\u6216 certPath)"),key:a__default.default.string().describe("Path to SSL key file (\u6216 keyPath)"),certPath:a__default.default.string().optional().describe("Path to SSL certificate file (alias of cert)"),keyPath:a__default.default.string().optional().describe("Path to SSL key file (alias of key)"),ca:a__default.default.string().optional().describe("Path to CA certificate file"),forceRedirect:a__default.default.boolean().optional().default(false).describe("Force redirect HTTP to HTTPS"),httpPort:a__default.default.number().int().positive().optional().default(80).describe("HTTP redirect port")}),ot=a__default.default.object({name:a__default.default.string().describe("Token name/description"),value:a__default.default.string().describe("Token value"),permissions:a__default.default.array(a__default.default.string()).default(["*"]).describe("Associated permissions"),expiresAt:a__default.default.number().int().positive().optional().describe("Expiration timestamp (Unix timestamp)"),description:a__default.default.string().optional().describe("Token description")}),at=a__default.default.object({enabled:a__default.default.boolean().default(true),tokens:a__default.default.array(ot).default([]).describe("List of valid tokens"),onInvalid:a__default.default.enum(["reject","anonymous"]).default("reject").describe("Behavior when token validation fails"),allowAnonymous:a__default.default.boolean().default(false).describe("Allow anonymous access"),defaultPermissions:a__default.default.array(a__default.default.string()).default([]).describe("Default permissions for anonymous users")}),ct=a__default.default.object({enabled:a__default.default.boolean().default(false),authorizationServer:a__default.default.string().describe("OAuth2 authorization server URL"),clientId:a__default.default.string().describe("OAuth2 client ID"),clientSecret:a__default.default.string().describe("OAuth2 client secret"),tokenValidationEndpoint:a__default.default.string().describe("OAuth2 token validation endpoint"),scope:a__default.default.array(a__default.default.string()).default([]).describe("OAuth2 scopes")}),He=a__default.default.object({type:a__default.default.enum(["token","oauth2"]).default("token"),token:at.optional(),oauth2:ct.optional()}),ze=a__default.default.object({mode:a__default.default.enum(["pull","push","both"]).default("both"),interval:a__default.default.number().int().positive().default(3e4),pushEvents:a__default.default.array(a__default.default.enum(["register","deregister","heartbeat","status_change"])).default(["register","deregister","status_change"]),conflictResolution:a__default.default.enum(["latest","local","remote"]).default("latest")}),lt=a__default.default.object({id:a__default.default.string().describe("Gateway node ID"),address:a__default.default.string().describe("Gateway node address (e.g., http://localhost:3001)"),enabled:a__default.default.boolean().default(true),metadata:a__default.default.record(a__default.default.string(),a__default.default.any()).optional()}),$e=a__default.default.object({nodeId:a__default.default.string().describe("Current gateway node ID"),nodes:a__default.default.array(lt).default([]).describe("Other gateway nodes in the cluster"),sync:ze.optional()}),he=a__default.default.object({maxConnectionsPerChannel:a__default.default.number().int().positive().default(100),idleTimeout:a__default.default.number().int().positive().default(3e5),reuseStrategy:a__default.default.enum(["lru","fifo","random"]).default("lru"),healthCheckInterval:a__default.default.number().int().positive().default(6e4),acquireTimeout:a__default.default.number().int().positive().default(5e3),warmupCount:a__default.default.number().int().nonnegative().default(0).describe("Number of connections to warm up")}),pe=a__default.default.object({maxLength:a__default.default.number().int().positive().default(1e4),concurrency:a__default.default.number().int().positive().default(100),batchSize:a__default.default.number().int().positive().default(10),batchTimeout:a__default.default.number().int().positive().default(100),priorityLevels:a__default.default.number().int().positive().default(4)}),fe=a__default.default.object({failureThreshold:a__default.default.number().int().positive().default(5),successThreshold:a__default.default.number().int().positive().default(3),timeout:a__default.default.number().int().positive().default(3e4),halfOpenDuration:a__default.default.number().int().positive().default(15e3)}),me=a__default.default.object({heartbeatInterval:a__default.default.number().int().positive().default(3e4),heartbeatTimeout:a__default.default.number().int().positive().default(9e4),heartbeatCheckInterval:a__default.default.number().int().positive().default(1e4),autoDeregisterTimeout:a__default.default.number().int().positive().default(3e5),maxAgents:a__default.default.number().int().positive().default(100),persistenceFile:a__default.default.string().optional().describe("Path to persist agent registrations"),maxRegistrations:a__default.default.number().int().positive().default(1e3)}),ye=a__default.default.object({sessionTimeout:a__default.default.number().int().positive().default(18e5),resumeGracePeriod:a__default.default.number().int().positive().default(3e5),maxSessions:a__default.default.number().int().positive().default(1e4),persistenceFile:a__default.default.string().optional().describe("Path to persist sessions")}),be=a__default.default.object({enabled:a__default.default.boolean().default(true),port:a__default.default.number().int().positive().default(8080),hostname:a__default.default.string().default("localhost"),path:a__default.default.string().optional().default("/"),maxConnections:a__default.default.number().int().positive().optional().default(1e3),connectionTimeout:a__default.default.number().int().positive().optional().default(6e4),heartbeatInterval:a__default.default.number().int().positive().optional().default(3e4),sessionExpireMs:a__default.default.number().int().positive().optional().default(3e5),https:Be.optional(),auth:He.optional(),cors:a__default.default.boolean().default(true),corsOrigins:a__default.default.array(a__default.default.string()).optional(),mdns:a__default.default.boolean().default(false),mdnsDomain:a__default.default.string().optional().default("gateway.easbot.local")}),gt=a__default.default.object({channel:a__default.default.string().optional().describe("Default channel ID, used when multiple channels are configured"),fallbackChannels:a__default.default.array(a__default.default.string()).optional().describe("Fallback channel list, sorted by priority, used for degraded channel selection")}),re=a__default.default.object({server:be.optional(),channels:We.optional(),cluster:$e.optional(),connectionPool:he.optional(),messageQueue:pe.optional(),circuitBreaker:fe.optional(),agentRegistry:me.optional(),session:ye.optional(),defaults:gt.optional(),defaultAgent:a__default.default.string().optional().describe("Default agent ID for routing"),logLevel:a__default.default.enum(["DEBUG","INFO","WARN","ERROR"]).optional().default("INFO")}).catchall(a__default.default.unknown());function ke(c){let t=re.safeParse(c);return t.success||rt.warn("Invalid gateway config",{issues:t.error.issues}),t.success?t.data:re.parse({})}function dt(c){return re.parse(c)}var ut=chunkRIWYHEQ6_cjs.a.create({service:"gateway:interfaces"}),I;function Gs(c){I=c,ut.debug("Agent adapter registered",{hasSubAgentRunner:!!c.subAgentRunner});}function Ds(){return I}function A(){return I!==void 0}function k(){if(!I)throw new Error("Agent adapter not set. Call setAgentAdapter() before using Gateway.");return I}var ht={Path:{home:process.env.HOME||process.env.USERPROFILE||"",data:".easbot",cache:".easbot/cache",config:".easbot",state:".easbot/state",log:".easbot/log",bin:".easbot/bin"}},pt={directory:process.cwd(),worktree:process.cwd()};function D(){return I?I.global:ht}function Te(){return I?I.instance:pt}var P=chunkRIWYHEQ6_cjs.a.create({service:"gateway:config"}),Ce={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}},yt={maxConnectionsPerChannel:100,idleTimeout:3e5,reuseStrategy:"lru",healthCheckInterval:6e4,acquireTimeout:5e3,warmupCount:0},bt={maxLength:1e4,concurrency:100,batchSize:10,batchTimeout:100,priorityLevels:4},Ct={failureThreshold:5,successThreshold:3,timeout:3e4,halfOpenDuration:15e3},St={heartbeatInterval:3e4,heartbeatTimeout:9e4,heartbeatCheckInterval:1e4,autoDeregisterTimeout:3e5,maxAgents:100,maxRegistrations:1e3},vt={sessionTimeout:18e5,resumeGracePeriod:3e5,maxSessions:1e4},wt={enabled:true,botToken:"",maxConnections:100,timeout:3e4},It={enabled:true,botToken:"",maxConnections:100,timeout:3e4},Pt={enabled:true,appId:"",appSecret:"",maxConnections:100,timeout:3e4},xt={enabled:true,mode:"official",appId:"",appSecret:"",maxConnections:100,timeout:3e4},Mt={enabled:true,path:"/ws",heartbeatInterval:3e4,connectionTimeout:6e4,maxConnections:1e3,cors:true},At={enabled:true,serviceUrl:"https://chat.signal.org",phoneNumber:"",maxConnections:100,timeout:3e4},kt={enabled:true,relays:[],maxConnections:100,timeout:3e4},L,U=null;async function Je(c){try{if(!E.existsSync(c))return null;let t=await w__namespace.default.readFile(c,"utf-8");t=t.replace(/\{env:([^}]+)\}/g,(r,l)=>{let g=process.env[l];return g===void 0?(P.debug("Environment variable not found",{varName:l,file:c}),""):g});let e=t,s=!1,n=!1,i=[];for(let r=0;r<t.length;r++){let l=t[r];if(n){i.push(l),n=!1;continue}if(l==="\\"){i.push(l),n=!0;continue}if(l==='"'){s=!s,i.push(l);continue}if(!s&&l==="/"&&t[r+1]==="/"){for(;r<t.length&&t[r]!==`
|
|
2
|
-
`&&t[r]!=="\r";)r++;continue}if(!s&&l==="/"&&t[r+1]==="*"){for(r+=2;r<t.length-1;){if(t[r]==="*"&&t[r+1]==="/"){r+=2;break}r++;}continue}i.push(l);}return e=i.join("").trim(),e?JSON.parse(e):null}catch(t){return P.debug("Failed to load config file",{file:c,error:String(t)}),null}}function O(c,t){let e={...c,...t};return (c.server||t.server)&&(e.server={...c.server,...t.server}),(c.channels||t.channels)&&(e.channels={...c.channels,...t.channels}),e}async function Tt(){let c=D(),t=Te(),e=[],s=R__namespace.default.join(c.Path.config);e.push(s),e.push(t.directory);try{let{Filesystem:i}=await import('@easbot/utils'),r=await Array.fromAsync(i.up({targets:[".easbot"],start:t.directory,stop:t.worktree}));e.push(...r);}catch{P.debug("Filesystem utils not available, using basic directories");}let n=process.env.EASBOT_CONFIG_DIR;return n&&(e.push(n),P.debug("Using EASBOT_CONFIG_DIR",{path:n})),[...new Set(e)]}async function Et(c){let t=["gateway.json"],e={};for(let s of t){let n=R__namespace.default.join(c,s),i=await Je(n);if(i)if("gateway"in i&&i.gateway){let r=i.gateway;"server"in r||"channels"in r||"cluster"in r?e=O(e,r):e=O(e,{server:r}),P.debug("Loaded gateway config from file (nested format)",{file:n});}else "gateway"in i||(e=O(e,i),P.debug("Loaded gateway config from file (direct format)",{file:n}));}return e}async function Rt(c){let t=["easbot.json"];for(let e of t){let s=R__namespace.default.join(c,e),n=await Je(s);if(n?.gateway)return P.debug("Loaded gateway config from easbot.json",{file:s}),n.gateway}return {}}function _t(){return L||(L=async()=>{let c=await Tt(),t={};for(let s of c){let n=await Et(s);t=O(t,n);let i=await Rt(s);t=O(t,i);}let e=ke(t);return U=e,P.info("Gateway config loaded",{directories:c.length,port:e.server?.port,logLevel:e.logLevel}),{config:e,directories:c}}),L}async function b(){return _t()()}async function qe(c){return c&&(L=void 0,U=null),(await b()).config}function je(){return U}function Ke(){return U!==null}function Nt(){L=void 0,U=null,P.debug("Config cache cleared");}function Gt(){return Te().directory}async function Dt(){let{config:c}=await b();return be.parse({...Ce,...c.server})}async function Lt(c){let{config:t}=await b(),e=t.channels?.[c],n={telegram:oe.parse(wt),slack:ae.parse(It),feishu:ce.parse(Pt),wechat:le.parse(xt),webchat:ge.parse(Mt),signal:de.parse(At),nostr:ue.parse(kt)}[c];if(!n)throw new Error(`Unknown channel type: ${c}`);return {...n,...e}}async function Ot(){let{config:c}=await b();return he.parse({...yt,...c.connectionPool})}async function Ut(){let{config:c}=await b();return pe.parse({...bt,...c.messageQueue})}async function Ft(){let{config:c}=await b();return fe.parse({...Ct,...c.circuitBreaker})}async function Wt(){let{config:c}=await b();return me.parse({...St,...c.agentRegistry})}async function Bt(){let{config:c}=await b();return ye.parse({...vt,...c.session})}async function Ht(){let{config:c}=await b();return c.server?.https}async function zt(){let{config:c}=await b();return c.server?.auth}async function $t(){let{config:c}=await b();return c.cluster}async function qt(){let{config:c}=await b();return c.defaultAgent}async function Ve(){let{config:c}=await b();return c.logLevel??"INFO"}var Re=Ce,F={maxConnections:1e3,connectionTimeout:6e4,sessionExpireMs:3e5};var jt={type:"token",enabled:true,tokens:[],onInvalid:"reject",allowAnonymous:false,defaultPermissions:[]};var Kt={heartbeatInterval:3e4,heartbeatTimeout:9e4,heartbeatCheckInterval:1e4,autoDeregisterTimeout:3e5,maxAgents:100,maxRegistrations:1e3};var Vt={mode:"both",interval:3e4,pushEvents:["register","deregister","status_change"],conflictResolution:"latest",remoteNodes:[]};var Jt={maxConnectionsPerChannel:100,idleTimeout:3e5,reuseStrategy:"lru",healthCheckInterval:6e4,acquireTimeout:5e3,warmupCount:0};var x=class c extends Error{constructor(e,s){super(e);chunkGY3SWWW3_cjs.a(this,"code");chunkGY3SWWW3_cjs.a(this,"recoverable");chunkGY3SWWW3_cjs.a(this,"details");chunkGY3SWWW3_cjs.a(this,"cause");this.name="GatewayError",this.code=s.code,this.recoverable=s.recoverable,this.details=s.details,this.cause=s.cause,Error.captureStackTrace&&Error.captureStackTrace(this,c);}toJSON(){return {name:this.name,message:this.message,code:this.code,recoverable:this.recoverable,details:this.details,cause:this.cause?.message,stack:this.stack}}static isRecoverable(e){if(e instanceof c)return e.recoverable;let s=e.code;return ["ECONNRESET","ETIMEDOUT","ECONNREFUSED","ENOTFOUND","EHOSTUNREACH"].includes(s??"")}};function T(c,t,e,s=true){return new x(e,{code:t,recoverable:s,details:{channelId:c}})}var Se=class{constructor(){chunkGY3SWWW3_cjs.a(this,"log",chunkRIWYHEQ6_cjs.a.create({service:"gateway:router"}));chunkGY3SWWW3_cjs.a(this,"subscribers",new Map);chunkGY3SWWW3_cjs.a(this,"subscriptions",new Map);chunkGY3SWWW3_cjs.a(this,"sessionSubscriptions",new Map);chunkGY3SWWW3_cjs.a(this,"channelPlugins",new Map);chunkGY3SWWW3_cjs.a(this,"lockManager");chunkGY3SWWW3_cjs.a(this,"retryManager");chunkGY3SWWW3_cjs.a(this,"cancelHandlers",new Set);this.lockManager=new te,this.retryManager=new se,this.lockManager.startCleanup();}onCancel(t){this.cancelHandlers.add(t);}offCancel(t){this.cancelHandlers.delete(t);}async emitCancel(t){for(let e of this.cancelHandlers)try{await e(t);}catch(s){this.log.error("cancel handler error",{error:String(s)});}}async route(t){this.log.debug("routing message",{id:t.id,sessionId:t.sessionId,type:t.type});let e=this.sessionSubscriptions.get(t.sessionId);if(!e||e.size===0)return this.log.debug("no subscribers for session",{sessionId:t.sessionId}),{success:true,broadcastCount:0,dispatchedToSubAgent:false};let s=0,n=null,i=[];for(let r of e){let l=this.subscriptions.get(r);if(!l)continue;let g=this.subscribers.get(l.subscriberId);if(g){if(t.type==="input"){if(!this.lockManager.tryAcquire(t.sessionId,t.id,g.id)){this.log.debug("skipping subscriber, lock held by another",{subscriberId:g.id,holder:this.lockManager.getHolder(t.sessionId)});continue}n=g.id;}try{await g.connection.send(t),s++,g.lastActiveAt=Date.now();}catch(u){this.log.warn("failed to send message to subscriber",{subscriberId:g.id,error:String(u)}),i.push(u instanceof Error?u:new Error(String(u))),n===g.id&&(this.lockManager.release(t.sessionId,g.id),n=null);}}}return this.log.debug("message routed",{id:t.id,broadcastCount:s,errorCount:i.length,processedBy:n}),{success:i.length===0,broadcastCount:s,dispatchedToSubAgent:false,error:i.length>0?i.map(r=>r.message).join("; "):void 0}}async completeMessage(t,e,s,n="completed"){this.lockManager.release(t,s),await this.emitCancel({sessionId:t,messageId:e,processedBy:s,reason:n,timestamp:Date.now()}),this.log.info("message completed",{sessionId:t,messageId:e,subscriberId:s,reason:n});}scheduleRetry(t,e){return this.retryManager.scheduleRetry(t,e)}async subscribe(t,e){let s=Ae(e.id,t);if(this.subscriptions.has(s))return this.log.debug("already subscribed",{subscriberId:e.id,sessionId:t}),this.subscriptions.get(s);let n={id:s,subscriberId:e.id,sessionId:t,createdAt:Date.now()};return this.subscribers.has(e.id)||this.subscribers.set(e.id,e),this.subscriptions.set(s,n),this.sessionSubscriptions.has(t)||this.sessionSubscriptions.set(t,new Set),this.sessionSubscriptions.get(t).add(s),e.sessions.add(t),this.log.info("subscribed to session",{subscriberId:e.id,sessionId:t,subscriptionId:s}),n}async unsubscribe(t){let e=this.subscriptions.get(t);if(!e){this.log.warn("subscription not found",{subscriptionId:t});return}this.subscriptions.delete(t);let s=this.sessionSubscriptions.get(e.sessionId);s&&(s.delete(t),s.size===0&&this.sessionSubscriptions.delete(e.sessionId));let n=this.subscribers.get(e.subscriberId);n&&n.sessions.delete(e.sessionId),this.log.info("unsubscribed from session",{subscriberId:e.subscriberId,sessionId:e.sessionId,subscriptionId:t});}async unsubscribeAll(t){let e=this.subscribers.get(t);if(!e)return;let s=[...e.sessions];for(let n of s){let i=this.sessionSubscriptions.get(n);if(i)for(let r of i)this.subscriptions.get(r)?.subscriberId===t&&await this.unsubscribe(r);}this.subscribers.delete(t),this.log.info("all subscriptions removed for subscriber",{subscriberId:t});}registerChannel(t){this.channelPlugins.set(t.id,t),this.log.info("channel plugin registered",{id:t.id,platform:t.platform});}unregisterChannel(t){this.channelPlugins.delete(t),this.log.info("channel plugin unregistered",{id:t});}getSubscriber(t){return this.subscribers.get(t)}getSubscription(t){return this.subscriptions.get(t)}getSubscriberCount(t){return this.sessionSubscriptions.get(t)?.size??0}getStats(){return {subscribers:this.subscribers.size,subscriptions:this.subscriptions.size,sessions:this.sessionSubscriptions.size}}async shutdown(){this.lockManager.stopCleanup(),this.subscribers.clear(),this.subscriptions.clear(),this.sessionSubscriptions.clear(),this.channelPlugins.clear(),this.cancelHandlers.clear(),this.log.info("router shutdown");}};var W=class{constructor(t={}){chunkGY3SWWW3_cjs.a(this,"log",chunkRIWYHEQ6_cjs.a.create({service:"gateway:message-store"}));chunkGY3SWWW3_cjs.a(this,"config");chunkGY3SWWW3_cjs.a(this,"messages",new Map);chunkGY3SWWW3_cjs.a(this,"sessionMessages",new Map);chunkGY3SWWW3_cjs.a(this,"cleanupTimer",null);chunkGY3SWWW3_cjs.a(this,"accessOrder",[]);this.config={storagePath:t.storagePath??"./data/gateway-messages",maxRetentionDays:t.maxRetentionDays??7,maxRecords:t.maxRecords??5e4,cleanupIntervalMs:t.cleanupIntervalMs??36e5,cleanupBatchSize:t.cleanupBatchSize??1e3,lruEnabled:t.lruEnabled??true,lruMaxMemory:t.lruMaxMemory??0},this.startCleanupTimer();}startCleanupTimer(){this.cleanupTimer||(this.cleanupTimer=setInterval(()=>{this.performCleanup().catch(t=>{this.log.error("Cleanup error",{error:String(t)});});},this.config.cleanupIntervalMs),this.cleanupTimer.unref?.(),this.log.info("Cleanup timer started",{intervalMs:this.config.cleanupIntervalMs}));}performCleanup(){return new Promise(t=>{let e=this.cleanup();this.log.info("Periodic cleanup completed",{deleted:e}),t();})}async store(t){this.messages.size>=this.config.maxRecords&&this.evictLRU(1e3),this.config.lruEnabled&&this.updateAccessOrder(t.id);let e={message:t,status:"pending",processedBy:null,processedAt:null,retryCount:0,error:null,createdAt:Date.now(),updatedAt:Date.now()};return this.messages.set(t.id,e),this.sessionMessages.has(t.sessionId)||this.sessionMessages.set(t.sessionId,new Set),this.sessionMessages.get(t.sessionId).add(t.id),this.log.debug("message stored",{id:t.id,sessionId:t.sessionId,type:t.type,total:this.messages.size}),e}get(t){let e=this.messages.get(t);return e&&this.config.lruEnabled&&this.updateAccessOrder(t),e}async updateStatus(t,e,s={}){let n=this.messages.get(t);if(!n){this.log.warn("message not found for status update",{messageId:t});return}n.status=e,n.updatedAt=Date.now(),s.processedBy&&(n.processedBy=s.processedBy),(e==="completed"||e==="failed")&&(n.processedAt=Date.now()),s.error&&(n.error=s.error),this.log.debug("message status updated",{id:t,status:e,processedBy:s.processedBy});}incrementRetry(t){let e=this.messages.get(t);return e?(e.retryCount++,e.updatedAt=Date.now(),e.retryCount):0}isProcessed(t){let e=this.messages.get(t);return e?e.status==="completed"||e.status==="processing":false}getSessionHistory(t,e=100){let s=this.sessionMessages.get(t);if(!s)return [];let n=[];for(let i of s){let r=this.messages.get(i);r&&n.push(r);}return n.sort((i,r)=>i.createdAt-r.createdAt),n.slice(-e)}getPendingMessages(t){let e=[];for(let s of this.messages.values())s.status==="pending"&&(!t||s.message.sessionId===t)&&e.push(s);return e}getFailedMessages(t=3){let e=[];for(let s of this.messages.values())s.status==="failed"&&s.retryCount<t&&e.push(s);return e}cleanup(){let t=Date.now(),e=this.config.maxRetentionDays*24*60*60*1e3,s=[];for(let[n,i]of this.messages)if((t-i.createdAt>e||i.status==="completed")&&(s.push(n),s.length>=this.config.cleanupBatchSize))break;for(let n of s)this.deleteMessage(n);return s.length>0&&this.log.info("cleanup completed",{deleted:s.length,remaining:this.messages.size}),s.length}evictLRU(t){if(!this.config.lruEnabled||this.accessOrder.length===0)return;let e=0;for(;e<t&&this.accessOrder.length>0;){let s=this.accessOrder.shift();s&&(this.deleteMessage(s),e++);}this.log.info("LRU eviction completed",{evicted:e,remaining:this.messages.size});}updateAccessOrder(t){let e=this.accessOrder.indexOf(t);e>-1&&this.accessOrder.splice(e,1),this.accessOrder.push(t);}deleteMessage(t){let e=this.messages.get(t);if(e){let s=this.sessionMessages.get(e.message.sessionId);s&&(s.delete(t),s.size===0&&this.sessionMessages.delete(e.message.sessionId)),this.messages.delete(t);let n=this.accessOrder.indexOf(t);n>-1&&this.accessOrder.splice(n,1);}}getStats(){let t={total:this.messages.size,pending:0,processing:0,completed:0,failed:0,cancelled:0,sessions:this.sessionMessages.size};for(let e of this.messages.values())t[e.status]++;return t}async close(){this.cleanupTimer&&(clearInterval(this.cleanupTimer),this.cleanupTimer=null),this.messages.clear(),this.sessionMessages.clear(),this.accessOrder=[],this.log.info("MessageStore closed");}};var B=class{constructor(t={}){chunkGY3SWWW3_cjs.a(this,"log",chunkRIWYHEQ6_cjs.a.create({service:"gateway:session-store"}));chunkGY3SWWW3_cjs.a(this,"config");chunkGY3SWWW3_cjs.a(this,"sessions",new Map);chunkGY3SWWW3_cjs.a(this,"channelSessions",new Map);chunkGY3SWWW3_cjs.a(this,"autoSaveTimer",null);chunkGY3SWWW3_cjs.a(this,"isDirty",false);this.config={storagePath:t.storagePath??"./data/gateway-sessions",autoSaveInterval:t.autoSaveInterval??6e4,maxRetentionDays:t.maxRetentionDays??7,enablePersistence:t.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(t){this.sessions.set(t.id,t),this.channelSessions.has(t.channel.channelId)||this.channelSessions.set(t.channel.channelId,new Set),this.channelSessions.get(t.channel.channelId).add(t.id),this.markDirty();}get(t){return this.sessions.get(t)}async delete(t){let e=this.sessions.get(t);if(!e)return;this.sessions.delete(t);let s=this.channelSessions.get(e.channel.channelId);s&&(s.delete(t),s.size===0&&this.channelSessions.delete(e.channel.channelId)),this.markDirty();}getAll(){return [...this.sessions.values()]}getByChannel(t){let e=this.channelSessions.get(t);return e?[...e].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 t=this.getSessionFilePath(),e=this.serializeAll();try{await this.ensureStorageDir(),await E__namespace.promises.writeFile(t,JSON.stringify(e,null,2),"utf-8"),this.isDirty=!1,this.log.debug("sessions saved",{count:e.length});}catch(s){throw this.log.error("failed to save sessions",{error:s}),s}}async load(){if(!this.config.enablePersistence)return;let t=this.getSessionFilePath();try{if(!E__namespace.existsSync(t)){this.log.debug("no saved sessions found");return}let e=await E__namespace.promises.readFile(t,"utf-8"),s=JSON.parse(e);this.sessions.clear(),this.channelSessions.clear();let n=Date.now(),i=this.config.maxRetentionDays*24*60*60*1e3;for(let r of s){if(n-r.lastActiveAt>i)continue;let l={id:r.id,channel:r.channel,backendSessionId:r.backendSessionId,subscribers:new Set(r.subscribers),state:r.state,createdAt:r.createdAt,lastActiveAt:r.lastActiveAt};this.sessions.set(l.id,l),this.channelSessions.has(l.channel.channelId)||this.channelSessions.set(l.channel.channelId,new Set),this.channelSessions.get(l.channel.channelId).add(l.id);}this.log.info("sessions loaded",{loaded:this.sessions.size,skipped:s.length-this.sessions.size});}catch(e){this.log.error("failed to load sessions",{error:e});}}serializeAll(){let t=[];for(let e of this.sessions.values())t.push(this.serialize(e));return t}serialize(t){return {id:t.id,channel:t.channel,backendSessionId:t.backendSessionId,subscribers:[...t.subscribers],state:t.state,createdAt:t.createdAt,lastActiveAt:t.lastActiveAt}}getSessionFilePath(){return R__namespace.join(this.config.storagePath,"sessions.json")}async ensureStorageDir(){E__namespace.existsSync(this.config.storagePath)||await E__namespace.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 t=Date.now(),e=this.config.maxRetentionDays*24*60*60*1e3,s=[];for(let n of this.sessions.values())t-n.lastActiveAt>e&&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 t={total:this.sessions.size,byStatus:{active:0,idle:0,closed:0},byPlatform:{}};for(let e of this.sessions.values()){t.byStatus[e.state.status]++;let s=e.channel.platform;t.byPlatform[s]=(t.byPlatform[s]||0)+1;}return t}};var Qt="./data/gateway",Xt="contacts",Yt="index.json",H=class{constructor(t={}){chunkGY3SWWW3_cjs.a(this,"log",chunkRIWYHEQ6_cjs.a.create({service:"gateway:contact-store"}));chunkGY3SWWW3_cjs.a(this,"baseDir");chunkGY3SWWW3_cjs.a(this,"contactDir");chunkGY3SWWW3_cjs.a(this,"indexPath");chunkGY3SWWW3_cjs.a(this,"index");chunkGY3SWWW3_cjs.a(this,"indexFileMap");chunkGY3SWWW3_cjs.a(this,"dirty",false);chunkGY3SWWW3_cjs.a(this,"saveTimer",null);chunkGY3SWWW3_cjs.a(this,"SAVE_DEBOUNCE_MS",1e3);this.baseDir=t.baseDir??Qt,this.contactDir=R__namespace.join(this.baseDir,Xt),this.indexPath=R__namespace.join(this.contactDir,Yt),this.index={byUid:{},byUsername:{},byEmail:{},byPhone:{},byPlatform:{},updatedAt:0},this.indexFileMap={};}async initialize(){await this.ensureDir(this.contactDir),await this.loadIndex(),this.log.info("contact store initialized",{baseDir:this.baseDir,contactCount:Object.keys(this.index.byUid).length});}async ensureDir(t){try{await w__namespace.mkdir(t,{recursive:!0});}catch(e){if(e.code!=="EEXIST")throw e}}async loadIndex(){try{let t=await w__namespace.readFile(this.indexPath,"utf-8");this.index=JSON.parse(t),await this.rebuildFileMap();}catch(t){t.code!=="ENOENT"&&this.log.warn("failed to load contact index, starting fresh",{error:String(t)}),this.index={byUid:{},byUsername:{},byEmail:{},byPhone:{},byPlatform:{},updatedAt:0};}}async saveIndex(){this.saveTimer&&clearTimeout(this.saveTimer),this.saveTimer=setTimeout(async()=>{await this.doSaveIndex();},this.SAVE_DEBOUNCE_MS);}async doSaveIndex(){try{this.index.updatedAt=Date.now(),await w__namespace.writeFile(this.indexPath,JSON.stringify(this.index,null,2),"utf-8"),this.dirty=!1;}catch(t){this.log.error("failed to save contact index",{error:String(t)});}}async rebuildFileMap(){this.indexFileMap={};for(let t of Object.keys(this.index.byUid)){let e=ie(t);if(e){let s=this.getFilePath(e.platform,e.stableId);this.indexFileMap[t]=s;}}}getFilePath(t,e){let s=this.sanitizeFilename(e);return R__namespace.join(this.contactDir,t,`${s}.json`)}sanitizeFilename(t){return t.replace(/[/\\:*?"<>|]/g,"_")}getPlatformDir(t){return R__namespace.join(this.contactDir,t)}async save(t){let{platform:e,stableId:s,uid:n}=t,i=this.getFilePath(e,s);return await this.ensureDir(R__namespace.dirname(i)),await w__namespace.writeFile(i,JSON.stringify(t,null,2),"utf-8"),this.index.byUid[n]=i,this.indexFileMap[n]=i,this.index.byPlatform[e]||(this.index.byPlatform[e]=[]),this.index.byPlatform[e].includes(n)||this.index.byPlatform[e].push(n),this.updateSearchIndices(t),this.dirty=true,await this.saveIndex(),this.log.debug("contact saved",{uid:n,platform:e,stableId:s}),t}async create(t){let e=Me(t);return this.save(e)}async getOrCreate(t,e,s){let n=await this.findByPlatformId(t,e);return n||this.create({platform:t,stableId:e,profile:s})}async findByUid(t){let e=ie(t);return e?this.findByPlatformId(e.platform,e.stableId):null}async findByPlatformId(t,e){let s=ne(t,e),n=this.index.byUid[s];if(n)try{let r=await w__namespace.readFile(n,"utf-8");return JSON.parse(r)}catch(r){r.code!=="ENOENT"&&this.log.warn("failed to read contact file",{uid:s,filePath:n,error:String(r)}),delete this.index.byUid[s],this.dirty=true;}let i=this.getFilePath(t,e);try{let r=await w__namespace.readFile(i,"utf-8"),l=JSON.parse(r);return this.index.byUid[s]=i,this.indexFileMap[s]=i,this.dirty=!0,await this.saveIndex(),l}catch{return null}}async update(t,e){let s=await this.findByUid(t);if(!s)return null;let n={...s,profile:e.profile?{...s.profile,...e.profile}:s.profile,aliases:e.aliases?{...s.aliases,...e.aliases}:s.aliases,metadata:e.metadata?{...s.metadata,...e.metadata}:s.metadata,lastSeenAt:Date.now()};return this.save(n)}async touch(t){let e=await this.findByUid(t);e&&(e.lastSeenAt=Date.now(),await this.save(e));}async delete(t){let e=await this.findByUid(t);if(!e)return false;let{platform:s,stableId:n}=e,i=this.index.byUid[t]||this.getFilePath(s,n);try{await w__namespace.unlink(i);}catch(r){r.code!=="ENOENT"&&this.log.warn("failed to delete contact file",{uid:t,filePath:i,error:String(r)});}return delete this.index.byUid[t],delete this.indexFileMap[t],this.index.byPlatform[s]&&(this.index.byPlatform[s]=this.index.byPlatform[s].filter(r=>r!==t)),this.removeFromSearchIndices(e),this.dirty=true,await this.saveIndex(),this.log.debug("contact deleted",{uid:t}),true}async listByPlatform(t,e=100,s=0){let n=this.index.byPlatform[t]||[],i=n.length,r=n.slice(s,s+e),l=[];for(let g of r){let u=await this.findByUid(g);u&&l.push(u);}return {contacts:l,total:i,hasMore:s+e<i}}async findByUsername(t){let e=t.toLowerCase(),s=this.index.byUsername[e];return s?this.findByUid(s):null}async findByEmail(t){let e=t.toLowerCase(),s=this.index.byEmail[e];return s?this.findByUid(s):null}async findByPhone(t){let e=t.replace(/\s/g,""),s=this.index.byPhone[e];return s?this.findByUid(s):null}async search(t){if(t.platform){let g=await this.listByPlatform(t.platform,t.limit,t.offset),u=g.contacts;if(t.username){let C=t.username.toLowerCase();u=u.filter(S=>S.profile.username?.toLowerCase().includes(C));}if(t.email){let C=t.email.toLowerCase();u=u.filter(S=>S.profile.email?.toLowerCase().includes(C));}if(t.phone){let C=t.phone.replace(/\s/g,"");u=u.filter(S=>S.profile.phone?.replace(/\s/g,"").includes(C));}return {contacts:u,total:g.total,hasMore:g.hasMore}}let e=[];for(let g of Object.keys(this.index.byPlatform)){let u=await this.listByPlatform(g,1e3,0);e.push(...u.contacts);}let s=e;if(t.username){let g=t.username.toLowerCase();s=s.filter(u=>u.profile.username?.toLowerCase().includes(g));}if(t.email){let g=t.email.toLowerCase();s=s.filter(u=>u.profile.email?.toLowerCase().includes(g));}if(t.phone){let g=t.phone.replace(/\s/g,"");s=s.filter(u=>u.profile.phone?.replace(/\s/g,"").includes(g));}let n=s.length,i=t.limit??100,r=t.offset??0;return {contacts:s.slice(r,r+i),total:n,hasMore:r+i<n}}async count(t){return t?(this.index.byPlatform[t]||[]).length:Object.keys(this.index.byUid).length}async exists(t){return await this.findByUid(t)!==null}async addAlias(t,e,s){let n=await this.findByUid(t);if(!n)return null;let i={...n,aliases:{...n.aliases,[e]:{...n.aliases[e],...s}},lastSeenAt:Date.now()};return this.save(i)}async close(){this.saveTimer&&clearTimeout(this.saveTimer),this.dirty&&await this.doSaveIndex(),this.log.info("contact store closed");}updateSearchIndices(t){let{uid:e,profile:s}=t;s.username&&(this.index.byUsername[s.username.toLowerCase()]=e),s.email&&(this.index.byEmail[s.email.toLowerCase()]=e),s.phone&&(this.index.byPhone[s.phone.replace(/\s/g,"")]=e);}removeFromSearchIndices(t){let{uid:e,profile:s}=t;if(s.username){let n=s.username.toLowerCase();this.index.byUsername[n]===e&&delete this.index.byUsername[n];}if(s.email){let n=s.email.toLowerCase();this.index.byEmail[n]===e&&delete this.index.byEmail[n];}if(s.phone){let n=s.phone.replace(/\s/g,"");this.index.byPhone[n]===e&&delete this.index.byPhone[n];}}};var _e=class{constructor(t){chunkGY3SWWW3_cjs.a(this,"cache",new Map);chunkGY3SWWW3_cjs.a(this,"maxSize");this.maxSize=t;}get(t){let e=this.cache.get(t);return e!==void 0&&(this.cache.delete(t),this.cache.set(t,e)),e}set(t,e){if(this.cache.has(t))this.cache.delete(t);else if(this.cache.size>=this.maxSize){let s=this.cache.keys().next().value;s!==void 0&&this.cache.delete(s);}this.cache.set(t,e);}has(t){return this.cache.has(t)}delete(t){return this.cache.delete(t)}clear(){this.cache.clear();}get size(){return this.cache.size}},z=class{constructor(t,e={}){chunkGY3SWWW3_cjs.a(this,"store",t);chunkGY3SWWW3_cjs.a(this,"cache");chunkGY3SWWW3_cjs.a(this,"usernameIndex");chunkGY3SWWW3_cjs.a(this,"emailIndex");chunkGY3SWWW3_cjs.a(this,"phoneIndex");chunkGY3SWWW3_cjs.a(this,"platformIndex");chunkGY3SWWW3_cjs.a(this,"initialized",false);this.cache=new _e(e.cacheSize??1e3),this.usernameIndex=new Map,this.emailIndex=new Map,this.phoneIndex=new Map,this.platformIndex=new Map;}async initialize(){this.initialized||("initialize"in this.store&&typeof this.store.initialize=="function"&&await this.store.initialize(),await this.warmCache(),this.initialized=true);}async warmCache(){try{let t=["telegram","discord","slack","feishu","wechat","webchat","signal","nostr"];for(let e of t)try{let s=await this.store.listByPlatform(e,1e3,0);for(let n of s.contacts)this.cacheContact(n);}catch{}}catch{}}cacheContact(t){let{uid:e,profile:s}=t;this.cache.set(e,t),s.username&&this.usernameIndex.set(s.username.toLowerCase(),e),s.email&&this.emailIndex.set(s.email.toLowerCase(),e),s.phone&&this.phoneIndex.set(s.phone.replace(/\s/g,""),e),this.platformIndex.has(t.platform)||this.platformIndex.set(t.platform,new Set),this.platformIndex.get(t.platform).add(e);}invalidateContact(t){let{uid:e,profile:s}=t;if(this.cache.delete(e),s.username){let i=s.username.toLowerCase();this.usernameIndex.get(i)===e&&this.usernameIndex.delete(i);}if(s.email){let i=s.email.toLowerCase();this.emailIndex.get(i)===e&&this.emailIndex.delete(i);}if(s.phone){let i=s.phone.replace(/\s/g,"");this.phoneIndex.get(i)===e&&this.phoneIndex.delete(i);}let n=this.platformIndex.get(t.platform);n&&n.delete(e);}async save(t){this.invalidateContact(t);let e=await this.store.save(t);return this.cacheContact(e),e}async create(t){let e=await this.store.create(t);return this.cacheContact(e),e}async getOrCreate(t,e,s){let n=this.cache.get(`${t}:${e}`);if(n)return n;let i=await this.store.getOrCreate(t,e,s);return this.cacheContact(i),i}async findByUid(t){let e=this.cache.get(t);if(e)return e;let s=await this.store.findByUid(t);return s&&this.cacheContact(s),s}async findByPlatformId(t,e){return this.findByUid(`${t}:${e}`)}async update(t,e){let s=await this.cache.get(t);s&&this.invalidateContact(s);let n=await this.store.update(t,e);return n&&this.cacheContact(n),n}async touch(t){await this.store.touch(t);}async delete(t){let e=await this.cache.get(t);return e&&this.invalidateContact(e),this.store.delete(t)}async listByPlatform(t,e=100,s=0){return this.store.listByPlatform(t,e,s)}async findByUsername(t){let e=t.toLowerCase(),s=this.usernameIndex.get(e);return s?this.findByUid(s):this.store.findByUsername(t)}async findByEmail(t){let e=t.toLowerCase(),s=this.emailIndex.get(e);return s?this.findByUid(s):this.store.findByEmail(t)}async findByPhone(t){let e=t.replace(/\s/g,""),s=this.phoneIndex.get(e);return s?this.findByUid(s):this.store.findByPhone(t)}async search(t){return this.store.search(t)}async count(t){return t?this.platformIndex.get(t)?.size??0:this.cache.size}async exists(t){return this.cache.has(t)?true:this.store.exists(t)}async addAlias(t,e,s){let n=await this.store.addAlias(t,e,s);return n&&(this.invalidateContact(n),this.cacheContact(n)),n}async close(){this.cache.clear(),this.usernameIndex.clear(),this.emailIndex.clear(),this.phoneIndex.clear(),this.platformIndex.clear(),"close"in this.store&&typeof this.store.close=="function"&&await this.store.close();}};var $=class{constructor(t={}){chunkGY3SWWW3_cjs.a(this,"log",chunkRIWYHEQ6_cjs.a.create({service:"gateway:session"}));chunkGY3SWWW3_cjs.a(this,"sessionStore");chunkGY3SWWW3_cjs.a(this,"config");chunkGY3SWWW3_cjs.a(this,"initialized",false);this.config={enablePersistence:t.enablePersistence??true,storagePath:t.storagePath??"./data/gateway-sessions",autoSaveInterval:t.autoSaveInterval??6e4,maxRetentionDays:t.maxRetentionDays??7},this.sessionStore=new B({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(t,e={}){let s=Pe(t),n=this.sessionStore.get(s);return n?(n.lastActiveAt=Date.now(),this.log.debug("session found",{sessionId:s}),n):(n={id:s,channel:t,backendSessionId:null,subscribers:new Set,state:xe(),createdAt:Date.now(),lastActiveAt:Date.now()},await this.sessionStore.set(n),this.log.info("session created",{sessionId:s,platform:t.platform,channelId:t.channelId,chatId:t.chatId,userId:t.userId}),n)}get(t){return this.sessionStore.get(t)}async closeSession(t){let e=this.sessionStore.get(t);if(!e){this.log.warn("session not found",{sessionId:t});return}e.state.status="closed",await this.sessionStore.delete(t),this.log.info("session closed",{sessionId:t});}async closeById(t){return this.closeSession(t)}async bindBackendSession(t,e){let s=this.sessionStore.get(t);if(!s)throw new Error(`Session not found: ${t}`);s.backendSessionId=e,await this.sessionStore.set(s),this.log.info("backend session bound",{gatewaySessionId:t,backendSessionId:e});}async unbindBackendSession(t){let e=this.sessionStore.get(t);if(!e){this.log.warn("session not found",{sessionId:t});return}e.backendSessionId=null,await this.sessionStore.set(e),this.log.info("backend session unbound",{gatewaySessionId:t});}getState(t){return this.sessionStore.get(t)?.state}async incrementMessageCountOrCreate(t,e){let s=this.sessionStore.get(t);if(!s){let n=e??{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(t){let e=this.sessionStore.get(t);e&&(e.state.messageCount++,e.state.lastMessageAt=Date.now(),e.lastActiveAt=Date.now(),await this.sessionStore.set(e));}async addSubscriber(t,e){let s=this.sessionStore.get(t);if(!s){this.log.warn("session not found",{sessionId:t});return}s.subscribers.add(e),await this.sessionStore.set(s),this.log.debug("subscriber added to session",{sessionId:t,subscriberId:e});}async removeSubscriber(t,e){let s=this.sessionStore.get(t);s&&(s.subscribers.delete(e),await this.sessionStore.set(s),this.log.debug("subscriber removed from session",{sessionId:t,subscriberId:e}));}query(t){let e=this.sessionStore.getAll();return t.platform&&(e=e.filter(s=>s.channel.platform===t.platform)),t.channelId&&(e=e.filter(s=>s.channel.channelId===t.channelId)),t.userId&&(e=e.filter(s=>s.channel.userId===t.userId)),t.status&&(e=e.filter(s=>s.state.status===t.status)),e}getSessionsByChannel(t){return this.sessionStore.getByChannel(t)}getAllSessions(){return this.sessionStore.getAll()}getSessionCount(){return this.sessionStore.size()}async cleanupIdleSessions(t){let e=Date.now(),s=[];for(let n of this.sessionStore.getAll())n.state.status==="idle"&&e-n.lastActiveAt>t&&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 q=class{constructor(t={}){chunkGY3SWWW3_cjs.a(this,"log",chunkRIWYHEQ6_cjs.a.create({service:"gateway:plugin-loader"}));chunkGY3SWWW3_cjs.a(this,"plugins",new Map);chunkGY3SWWW3_cjs.a(this,"messageHandler",null);chunkGY3SWWW3_cjs.a(this,"healthCheckTimer",null);chunkGY3SWWW3_cjs.a(this,"config");chunkGY3SWWW3_cjs.a(this,"backoffPolicy");this.config={pluginDir:t.pluginDir??"./plugins",autoDiscover:t.autoDiscover??false,healthCheckInterval:t.healthCheckInterval??6e4,healthCheckTimeout:t.healthCheckTimeout??5e3,autoReconnect:t.autoReconnect??true,maxReconnectAttempts:t.maxReconnectAttempts??10,reconnectInitialDelay:t.reconnectInitialDelay??5e3,reconnectMaxDelay:t.reconnectMaxDelay??3e5},this.backoffPolicy={initialMs:this.config.reconnectInitialDelay,maxMs:this.config.reconnectMaxDelay,factor:2,jitter:.1};}setMessageHandler(t){this.messageHandler=t;}async register(t,e){if(this.plugins.has(t.id)){this.log.warn("plugin already registered",{id:t.id});return}this.plugins.set(t.id,{plugin:t,config:e,started:false,reconnectAttempts:0}),this.log.info("plugin registered",{id:t.id,platform:t.platform,name:t.name});}async unregister(t){let e=this.plugins.get(t);if(!e){this.log.warn("plugin not found",{id:t});return}this.cancelReconnect(t),e.started&&await this.stop(t),this.plugins.delete(t),this.log.info("plugin unregistered",{id:t});}async start(t){let e=this.plugins.get(t);if(!e)throw new x(`Plugin not found: ${t}`,{code:"PLUGIN_NOT_FOUND",recoverable:false});if(e.started){this.log.warn("plugin already started",{id:t});return}if(!this.messageHandler)throw new x("Message handler not set",{code:"PLUGIN_ERROR",recoverable:false});if(!e.config.enabled){this.log.warn("plugin is disabled",{id:t});return}this.log.info("starting plugin",{id:t});try{await e.plugin.start(e.config,this.messageHandler),e.started=!0,e.reconnectAttempts=0,this.log.info("plugin started",{id:t});}catch(s){throw this.log.error("failed to start plugin",{id:t,error:String(s)}),this.scheduleReconnect(t),s}}async stop(t){let e=this.plugins.get(t);if(!e){this.log.warn("plugin not found",{id:t});return}if(!e.started){this.log.warn("plugin not started",{id:t});return}this.cancelReconnect(t),this.log.info("stopping plugin",{id:t});try{await e.plugin.stop(),e.started=!1,this.log.info("plugin stopped",{id:t});}catch(s){e.started=false,this.log.error("failed to stop plugin",{id:t,error:String(s)});}}async startAll(){let t=[];for(let[e,s]of this.plugins){if(!s.config.enabled){this.log.debug("skipping disabled plugin",{id:e});continue}try{await this.start(e);}catch(n){t.push({id:e,error:n instanceof Error?n:new Error(String(n))});}}t.length>0&&this.log.warn("some plugins failed to start",{count:t.length,errors:t.map(e=>({id:e.id,error:e.error.message}))});}async stopAll(){this.stopHealthCheckTimer();for(let[t]of this.plugins)await this.stop(t);}getPlugin(t){return this.plugins.get(t)?.plugin}getAllPlugins(){return [...this.plugins.values()].map(t=>t.plugin)}getRunningPlugins(){return [...this.plugins.values()].filter(t=>t.started).map(t=>t.plugin)}isRunning(t){return this.plugins.get(t)?.started??false}async healthCheck(t){let e=this.plugins.get(t);if(!e)return {healthy:false,lastCheck:Date.now(),error:"Plugin not found"};if(!e.started)return {healthy:false,lastCheck:Date.now(),error:"Plugin not running"};try{let s=await Promise.race([e.plugin.healthCheck(),new Promise((n,i)=>setTimeout(()=>i(new Error("Health check timeout")),this.config.healthCheckTimeout))]);return e.lastHealthCheck=s,s}catch(s){let n={healthy:false,lastCheck:Date.now(),error:s instanceof Error?s.message:String(s)};return e.lastHealthCheck=n,n}}async healthCheckAll(){let t=new Map;for(let[e]of this.plugins)t.set(e,await this.healthCheck(e));return t}startHealthCheckTimer(){this.healthCheckTimer||(this.healthCheckTimer=setInterval(async()=>{for(let[t,e]of this.plugins){if(!e.started)continue;let s=await this.healthCheck(t);s.healthy||(this.log.warn("plugin health check failed",{id:t,error:s.error}),this.config.autoReconnect&&(e.started=false,this.scheduleReconnect(t)));}},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"));}scheduleReconnect(t){if(!this.config.autoReconnect)return;let e=this.plugins.get(t);if(!e)return;if(e.reconnectAttempts>=this.config.maxReconnectAttempts){this.log.error("max reconnect attempts reached",{id:t,attempts:e.reconnectAttempts});return}let s=this.calculateBackoffDelay(e.reconnectAttempts);e.reconnectAttempts++,this.log.info("scheduling reconnect",{id:t,attempt:e.reconnectAttempts,delay:s}),e.reconnectTimer=setTimeout(async()=>{try{await this.start(t),this.log.info("reconnect successful",{id:t});}catch(n){this.log.warn("reconnect failed",{id:t,error:String(n)}),this.scheduleReconnect(t);}},s),e.reconnectTimer.unref?.();}cancelReconnect(t){let e=this.plugins.get(t);e&&e.reconnectTimer&&(clearTimeout(e.reconnectTimer),e.reconnectTimer=void 0);}calculateBackoffDelay(t){let e=Math.min(this.backoffPolicy.initialMs*this.backoffPolicy.factor**t,this.backoffPolicy.maxMs),s=e*this.backoffPolicy.jitter*(Math.random()*2-1);return Math.floor(e+s)}};var M=class extends Error{constructor(e,s,n){super(e);chunkGY3SWWW3_cjs.a(this,"code",s);chunkGY3SWWW3_cjs.a(this,"configuredChannels",n);this.name="ChannelSelectionError";}},j=chunkRIWYHEQ6_cjs.a.create({service:"gateway:channel-selection"});function N(c){if(!(!c||typeof c!="string"))return c.toLowerCase().trim()}async function Ge(c){let{channel:t,sessionId:e,userId:s,defaultChannel:n,fallbackChannels:i,sessionManager:r,pluginLoader:l,contactStore:g}=c,u=N(t);if(u){let f=Ne(u,l);if(f){if(s&&g){let v=await Zt(u,s,g,l);if(v)return v}return f}if(e){let v=Ye(e,r);if(v)return j.debug("explicit channel unavailable, using session-bound channel as fallback",{explicitChannel:u,sessionChannel:v.channel}),v}throw new M(`Channel \u4E0D\u53EF\u7528\u6216\u672A\u914D\u7F6E: ${u}`,"CHANNEL_UNAVAILABLE",[])}if(e){let f=Ye(e,r);if(f)return f}if(s&&g){let f=await es(s,g,l);if(f)return f}let C=De(l);if(C.length===0)throw new M("\u6CA1\u6709\u5DF2\u914D\u7F6E\u7684 Channel\uFF0C\u65E0\u6CD5\u53D1\u9001\u6D88\u606F\u3002\u8BF7\u81F3\u5C11\u914D\u7F6E\u4E00\u4E2A Channel\u3002","NO_CHANNEL",[]);let S=N(n);if(S){let f=Ne(S,l);if(f)return j.debug("using default channel",{channel:S}),{...f,source:"default"};j.warn("default channel unavailable",{channel:S});}if(C.length===1){let f=C[0],v=l?.getPlugin(f);return {channel:f,source:"single-configured",channelInfo:{platform:v?.platform??"api",channelId:f}}}let nt=(i??[]).map(N).filter(f=>f!==void 0);for(let f of nt){let v=Ne(f,l);if(v)return j.debug("using fallback channel",{channel:f}),{...v,source:"fallback"};j.debug("fallback channel unavailable, trying next",{channel:f});}throw S?new M(`\u914D\u7F6E\u7684\u9ED8\u8BA4 Channel \u4E0D\u53EF\u7528: ${S}`,"CHANNEL_UNAVAILABLE",C):new M(`\u9700\u8981\u660E\u786E\u6307\u5B9A Channel\uFF08\u591A\u4E2A Channel \u5DF2\u914D\u7F6E\uFF09: ${C.join(", ")}\u3002\u8BF7\u901A\u8FC7 channel \u53C2\u6570\u6307\u5B9A\u8981\u4F7F\u7528\u7684 Channel\uFF0C\u6216\u5728 gateway.json \u4E2D\u914D\u7F6E defaults.channel\u3002`,"MULTIPLE_CHANNELS",C)}function Ne(c,t){if(!t)return null;let e=t.getPlugin(c);if(e&&t.isRunning(c))return {channel:c,source:"explicit",channelInfo:{platform:e.platform,channelId:c}};let s=N(c);if(!s)return null;let n=t.getAllPlugins();for(let i of n)if(t.isRunning(i.id)&&N(i.platform)===s)return {channel:i.id,source:"explicit",channelInfo:{platform:i.platform,channelId:i.id}};return null}function Ye(c,t){if(!t)return null;let e=t.get(c);return e?{channel:e.channel.channelId,source:"session-bound",channelInfo:e.channel}:null}async function Zt(c,t,e,s){let n=s?.getPlugin(c);if(!n)return null;let i=await e.findByPlatformId(n.platform,t);return i?{channel:c,source:"explicit",channelInfo:{platform:n.platform,channelId:c,userId:i.stableId,chatId:i.stableId}}:null}async function es(c,t,e){let s=De(e);if(s.length===0)return null;for(let n of s){let i=e?.getPlugin(n);if(!i)continue;let r=await t.findByPlatformId(i.platform,c);if(r)return {channel:n,source:"single-configured",channelInfo:{platform:i.platform,channelId:n,userId:r.stableId,chatId:r.stableId}}}return null}function De(c){if(!c)return [];let t=[],e=c.getAllPlugins();for(let s of e)c.isRunning(s.id)&&t.push(s.id);return t}function Le(c){switch(c.code){case "NO_CHANNEL":return "\u6CA1\u6709\u5DF2\u914D\u7F6E\u7684 Channel\uFF0C\u65E0\u6CD5\u53D1\u9001\u6D88\u606F\u3002\u8BF7\u5728 gateway.json \u4E2D\u914D\u7F6E\u81F3\u5C11\u4E00\u4E2A Channel\u3002";case "MULTIPLE_CHANNELS":return `\u591A\u4E2A Channel \u5DF2\u914D\u7F6E\uFF0C\u9700\u8981\u660E\u786E\u6307\u5B9A: ${c.configuredChannels?.join(", ")||""}\u3002\u8BF7\u901A\u8FC7 channel \u53C2\u6570\u6307\u5B9A\u8981\u4F7F\u7528\u7684 Channel\uFF0C\u6216\u5728 gateway.json \u4E2D\u914D\u7F6E defaults.channel \u4F5C\u4E3A\u9ED8\u8BA4\u503C\u3002`;case "CHANNEL_UNAVAILABLE":return c.message;case "UNKNOWN_CHANNEL":return `\u672A\u77E5\u7684 Channel: ${c.message}\u3002\u8BF7\u68C0\u67E5 Channel ID \u662F\u5426\u6B63\u786E\u3002`;default:return c.message}}var d=chunkRIWYHEQ6_cjs.a.create({service:"gateway:websocket-server"});function is(){return `ws_${Date.now()}_${Math.random().toString(36).substring(2,9)}`}function ve(c,t){for(let[e,s]of t.entries())if(s===c)return e;return null}var _=class{constructor(t={}){chunkGY3SWWW3_cjs.a(this,"config");chunkGY3SWWW3_cjs.a(this,"server",null);chunkGY3SWWW3_cjs.a(this,"wss",null);chunkGY3SWWW3_cjs.a(this,"httpsServer",null);chunkGY3SWWW3_cjs.a(this,"httpRedirectServer",null);chunkGY3SWWW3_cjs.a(this,"connections",new Map);chunkGY3SWWW3_cjs.a(this,"subscriptions",new Map);chunkGY3SWWW3_cjs.a(this,"sessionSubscriptions",new Map);chunkGY3SWWW3_cjs.a(this,"sessionLastActivity",new Map);chunkGY3SWWW3_cjs.a(this,"cleanupInterval",null);chunkGY3SWWW3_cjs.a(this,"heartbeatInterval",null);chunkGY3SWWW3_cjs.a(this,"startTime",0);chunkGY3SWWW3_cjs.a(this,"running",false);chunkGY3SWWW3_cjs.a(this,"channelResolveParams",null);chunkGY3SWWW3_cjs.a(this,"messageSender",null);chunkGY3SWWW3_cjs.a(this,"pluginLoaderRef",null);this.config={...Re,...t};}setChannelResolveDeps(t){this.channelResolveParams=t,d.debug("channel resolve deps set",{hasSessionManager:!!t.sessionManager,hasPluginLoader:!!t.pluginLoader,hasContactStore:!!t.contactStore});}setMessageSender(t){this.messageSender=t,d.debug("message sender set",{hasSender:!!t});}setPluginLoader(t){this.pluginLoaderRef=t;}getChannelResolveParams(){return this.channelResolveParams?this.channelResolveParams:{}}async start(){if(this.running){d.warn("WebSocket server is already running");return}d.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 t={onOpen:(n,i)=>{this.handleConnection(i);},onMessage:async(n,i)=>{try{let r=JSON.parse(n.data.toString());await this.handleMessage(i,r);}catch(r){d.error("Error processing WebSocket message",{error:r.message}),this.sendError(i,r.message);}},onClose:(n,i)=>{this.handleDisconnection(i);},onError:(n,i)=>{d.error("WebSocket error",{error:n.type});}},e=this.config.https?.enabled??false;d.debug("WebSocket server start: choosing server type",{https:e});try{e?(d.debug("WebSocket server start: starting HTTPS server"),await this.startHTTPSServer(t)):(d.debug("WebSocket server start: starting HTTP server"),await this.startHTTPServer(t)),d.debug("WebSocket server start: HTTP/HTTPS server started successfully");}catch(n){throw d.error("WebSocket server start: HTTP/HTTPS server failed",{error:n instanceof Error?n.message:String(n)}),n}d.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=e?"wss":"ws";d.info("WebSocket server started",{url:`${s}://${this.config.hostname}:${this.config.port}${this.config.path}`});}async startHTTPServer(t){d.debug("HTTP server start: creating server",{port:this.config.port,hostname:this.config.hostname}),this.server=utils.createServer({port:this.config.port,hostname:this.config.hostname,routes:this.createRoutes(),timeout:this.config.connectionTimeout,cors:true}),d.debug("HTTP server start: server created, checking raw server");let e=this.server.raw;e?(d.debug("HTTP server start: raw server available, setting up WebSocket upgrade"),this.setupWebSocketUpgrade(e,t),d.debug("HTTP server start: WebSocket upgrade configured")):d.warn("HTTP server start: raw server is undefined");}async startHTTPSServer(t){let e=this.config.https,s=this.loadSSLCertificates(e);if(!s){d.error("Failed to load SSL certificates, falling back to HTTP"),await this.startHTTPServer(t);return}this.server=utils.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=ss__default.default.createServer(s,n.listeners("request")[0]),this.setupWebSocketUpgrade(this.httpsServer,t),await new Promise(i=>{this.httpsServer.listen(this.config.port,this.config.hostname,()=>{d.info("HTTPS server started",{port:this.config.port,hostname:this.config.hostname}),i();});}),e.forceRedirect&&e.httpPort&&this.startHTTPRedirectServer(e.httpPort);}loadSSLCertificates(t){try{return t.cert&&t.key?{cert:t.cert,key:t.key}:t.certPath&&t.keyPath?{cert:E__namespace.default.readFileSync(t.certPath),key:E__namespace.default.readFileSync(t.keyPath)}:null}catch(e){return d.error("Failed to load SSL certificates",{error:e.message}),null}}setupWebSocketUpgrade(t,e){this.wss=new ws.WebSocketServer({noServer:true}),t.on("upgrade",(s,n,i)=>{try{let l=new URL(s.url||"/",`http://${s.headers.host}`).pathname;(l===this.config.path||this.config.path==="/"&&(l==="/"||l===""))&&this.wss.handleUpgrade(s,n,i,g=>{e.onOpen?.({type:"open"},g),g.on("message",u=>{e.onMessage?.({data:u},g);}),g.on("close",(u,C)=>{e.onClose?.({code:u,reason:C},g);}),g.on("error",u=>{e.onError?.({type:"error",message:u.message},g);});});}catch{}});}startHTTPRedirectServer(t){this.httpRedirectServer=ns__default.default.createServer((e,s)=>{let i=`https://${e.headers.host?.split(":")[0]||this.config.hostname}:${this.config.port}${e.url}`;d.debug("Redirecting HTTP to HTTPS",{from:e.url,to:i}),s.writeHead(301,{Location:i,"Content-Type":"text/plain"}),s.end(`Redirecting to ${i}`);}),this.httpRedirectServer.listen(t,this.config.hostname,()=>{d.info("HTTP redirect server started",{port:t,hostname:this.config.hostname});});}async stop(){if(!this.running){d.warn("WebSocket server is not running");return}d.info("Stopping WebSocket server"),this.cleanupInterval&&(clearInterval(this.cleanupInterval),this.cleanupInterval=null),this.heartbeatInterval&&(clearInterval(this.heartbeatInterval),this.heartbeatInterval=null);for(let[t,e]of this.connections.entries())try{e.close(1e3,"Server shutting down");}catch(s){d.debug("Error closing connection",{clientId:t,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(t=>{this.httpsServer.close(()=>t());}),this.httpsServer=null),this.httpRedirectServer&&(await new Promise(t=>{this.httpRedirectServer.close(()=>t());}),this.httpRedirectServer=null),this.server&&(await this.server.stop(),this.server=null),this.running=false,d.info("WebSocket server stopped");}broadcastToSession(t,e){this.touchSession(t);let s=this.sessionSubscriptions.get(t);if(!s||s.size===0){d.debug("No clients subscribed to session",{sessionId:t});return}let n=e?.properties?.sessionID;if(typeof n=="string"&&n&&n!==t)return;let i=JSON.stringify({type:"event",sessionId:t,payload:e});for(let r of s){let l=this.subscriptions.get(r);if(l&&l.ws.readyState===l.ws.OPEN&&l.subscribedSessions.has(t)){if(l.subscribedEventTypes.size>0&&!l.subscribedEventTypes.has(e.type)){d.debug("Event type not subscribed, skipping",{clientId:r,sessionId:t,eventType:e.type,subscribedTypes:Array.from(l.subscribedEventTypes)});continue}l.ws.send(i),d.debug("Event pushed to client",{clientId:r,sessionId:t,eventType:e.type});}}}getConnectionCount(){return this.connections.size}getSubscriptionCount(){return this.sessionSubscriptions.size}getConnectionInfos(){let t=[];for(let[e,s]of this.subscriptions.entries())t.push({clientId:e,state:s.ws.readyState===s.ws.OPEN?"connected":"disconnected",connectedAt:s.connectedAt,lastActivityAt:s.lastActivityAt,authInfo:s.authInfo});return t}isRunning(){return this.running}handleConnection(t){if(this.connections.size>=(this.config.maxConnections??F.maxConnections)){d.warn("Maximum connections reached, rejecting new connection"),t.close(1013,"Maximum connections reached");return}let e=is();this.connections.set(e,t),this.subscriptions.set(e,{clientId:e,ws:t,subscribedSessions:new Set,subscribedEventTypes:new Set,connectedAt:Date.now(),lastActivityAt:Date.now()}),d.info("WebSocket client connected",{clientId:e,totalConnections:this.connections.size}),this.sendMessage(t,{type:"connected",clientId:e,payload:{version:"1.0.0",capabilities:["subscribe","message","interrupt","releaseSubscription"]}});}handleDisconnection(t){let e=ve(t,this.connections);if(!e)return;this.connections.delete(e);let s=this.subscriptions.get(e);if(s){for(let n of s.subscribedSessions){let i=this.sessionSubscriptions.get(n);i&&(i.delete(e),i.size===0&&this.sessionSubscriptions.delete(n));}this.subscriptions.delete(e);}d.info("WebSocket client disconnected",{clientId:e,totalConnections:this.connections.size});}async handleMessage(t,e){let s=ve(t,this.connections);if(!s){this.sendError(t,"Client not found");return}let n=this.subscriptions.get(s);switch(n&&(n.lastActivityAt=Date.now()),e.type){case "ping":this.sendMessage(t,{type:"pong"});break;case "initialize":await this.handleInitialize(t,e);break;case "subscribe":await this.handleSubscribe(t,e);break;case "unsubscribe":await this.handleUnsubscribe(t,e);break;case "message":await this.handleMessageRequest(t,e);break;case "interrupt":await this.handleInterrupt(t,e);break;case "releaseSubscription":await this.handleReleaseSubscription(t,e);break;default:this.sendError(t,`Unknown message type: ${e.type}`);}}async handleInitialize(t,e){this.sendMessage(t,{type:"initialize_response",success:true,result:{capabilities:["subscribe","message","interrupt","releaseSubscription"]}});}async handleSubscribe(t,e){if(e.type!=="subscribe"||!e.sessionId){this.sendError(t,"Invalid subscribe request");return}let s=ve(t,this.connections);if(!s){this.sendError(t,"Client not found");return}let n=this.subscriptions.get(s);if(!n){this.sendError(t,"Subscription not found");return}n.subscribedSessions.add(e.sessionId),this.sessionSubscriptions.has(e.sessionId)||this.sessionSubscriptions.set(e.sessionId,new Set),this.sessionSubscriptions.get(e.sessionId).add(s);let i=e;if(i.backendSessionId){let r=this.getChannelResolveParams();if(r.sessionManager)try{await r.sessionManager.bindBackendSession(i.sessionId,i.backendSessionId),d.info("backend session bound on subscribe",{gatewaySessionId:i.sessionId,backendSessionId:i.backendSessionId});}catch(l){d.warn("failed to bind backend session on subscribe",{sessionId:i.sessionId,backendSessionId:i.backendSessionId,error:String(l)});}}if(this.touchSession(e.sessionId),e.payload?.eventTypes)for(let r of e.payload.eventTypes)n.subscribedEventTypes.add(r);d.info("Client subscribed to session",{clientId:s,sessionId:e.sessionId,backendSessionId:i.backendSessionId}),this.sendMessage(t,{type:"subscribed",sessionId:e.sessionId});}async handleUnsubscribe(t,e){if(e.type!=="unsubscribe"||!e.sessionId){this.sendError(t,"Invalid unsubscribe request");return}let s=ve(t,this.connections);if(!s)return;let n=this.subscriptions.get(s);if(!n)return;n.subscribedSessions.delete(e.sessionId);let i=this.sessionSubscriptions.get(e.sessionId);i&&(i.delete(s),i.size===0&&this.sessionSubscriptions.delete(e.sessionId)),d.info("Client unsubscribed from session",{clientId:s,sessionId:e.sessionId}),this.sendMessage(t,{type:"unsubscribed",sessionId:e.sessionId});}async handleMessageRequest(t,e){if(e.type!=="message"||!e.sessionId||!e.payload){this.sendError(t,"Invalid message request");return}d.debug("Message received",{sessionId:e.sessionId}),this.sendMessage(t,{type:"response",sessionId:e.sessionId,success:true,result:{received:true}});}async handleInterrupt(t,e){if(e.type!=="interrupt"||!e.sessionId){this.sendError(t,"Invalid interrupt request");return}d.info("Interrupt requested via WebSocket",{sessionId:e.sessionId}),this.sendMessage(t,{type:"interrupted",sessionId:e.sessionId,success:true});}async handleReleaseSubscription(t,e){if(e.type!=="releaseSubscription"||!e.sessionId){this.sendError(t,"Invalid releaseSubscription request");return}d.info("Release subscription requested via WebSocket",{sessionId:e.sessionId}),this.releaseSessionResources(e.sessionId),this.sendMessage(t,{type:"subscriptionReleased",sessionId:e.sessionId});}touchSession(t){this.sessionLastActivity.set(t,Date.now());}checkConnectionTimeout(){let t=Date.now(),e=this.config.connectionTimeout??F.connectionTimeout,s=[];for(let[n,i]of this.subscriptions.entries())t-i.lastActivityAt>e&&s.push(n);for(let n of s){let i=this.subscriptions.get(n);if(i){d.info("Closing inactive connection",{clientId:n,lastActivityAt:i.lastActivityAt,timeoutMs:e});try{i.ws.close(1001,"Connection timeout");}catch(r){d.debug("Error closing timed out connection",{clientId:n,error:String(r)});}}}s.length>0&&d.debug("Closed timed out connections",{count:s.length,remainingConnections:this.connections.size-s.length});}cleanupExpiredSessions(){let t=Date.now(),e=[],s=this.config.sessionExpireMs??F.sessionExpireMs;for(let[n,i]of this.sessionLastActivity.entries())t-i>s&&e.push(n);for(let n of e)d.info("Session expired due to inactivity, releasing resources",{sessionId:n,lastActivity:this.sessionLastActivity.get(n),expireMs:s}),this.releaseSessionResources(n),this.sessionLastActivity.delete(n);e.length>0&&d.debug("Cleaned up expired sessions",{count:e.length,remainingActive:this.sessionLastActivity.size});}releaseSessionResources(t){let e=this.sessionSubscriptions.get(t);if(e){for(let s of e){let n=this.subscriptions.get(s);n&&n.ws.readyState===n.ws.OPEN&&this.sendMessage(n.ws,{type:"session_closed",sessionId:t,payload:{reason:"Session resources released due to expiration or explicit release"}});}for(let s of e){let n=this.subscriptions.get(s);n&&n.subscribedSessions.delete(t);}this.sessionSubscriptions.delete(t),d.info("Session resources released",{sessionId:t,clientCount:e.size});}}sendMessage(t,e){t.readyState===t.OPEN&&t.send(JSON.stringify(e));}sendError(t,e,s){this.sendMessage(t,{type:"error",message:e,code:s});}createRoutes(){return [{method:"get",path:"/health",handler:t=>{let e={status:"healthy",timestamp:Date.now(),version:"1.0.0"};return t.json(e)}},{method:"get",path:"/status",handler:t=>{let e={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 t.json(e)}},{method:"get",path:"/contacts",handler:async t=>{let e=t.req.query("platform"),s=parseInt(t.req.query("limit")||"100",10),n=parseInt(t.req.query("offset")||"0",10),i=this.channelResolveParams?.contactStore;if(!i)return t.json({error:"Contact store not available"},503);try{if(e){let r=await i.listByPlatform(e,s,n);return t.json({contacts:r.contacts.map(l=>({uid:l.uid,platform:l.platform,stableId:l.stableId,profile:l.profile,lastSeenAt:l.lastSeenAt,createdAt:l.createdAt})),total:r.total,hasMore:r.hasMore})}else {let r=await i.count();return t.json({total:r,message:"Specify platform query parameter to list contacts"})}}catch(r){return d.error("failed to list contacts",{error:String(r)}),t.json({error:String(r)},500)}}},{method:"get",path:"/contacts/:uid",handler:async t=>{let e=t.req.param("uid"),s=this.channelResolveParams?.contactStore;if(!s)return t.json({error:"Contact store not available"},503);try{let n=await s.findByUid(e);return n?t.json({uid:n.uid,platform:n.platform,stableId:n.stableId,profile:n.profile,aliases:n.aliases,lastSeenAt:n.lastSeenAt,createdAt:n.createdAt}):t.json({error:"Contact not found"},404)}catch(n){return d.error("failed to get contact",{uid:e,error:String(n)}),t.json({error:String(n)},500)}}},{method:"get",path:"/sessions",handler:async t=>{let e=parseInt(t.req.query("limit")||"100",10),s=parseInt(t.req.query("offset")||"0",10);t.req.query("platform");let i=[];for(let[r,l]of this.sessionSubscriptions)i.length>=s&&i.length<s+e&&i.push({id:r,state:"active",createdAt:this.sessionLastActivity.get(r)||Date.now(),lastActiveAt:this.sessionLastActivity.get(r)||Date.now()});return t.json({sessions:i,total:this.sessionSubscriptions.size,hasMore:s+e<this.sessionSubscriptions.size})}},{method:"get",path:"/sessions/:sessionId",handler:async t=>{let e=t.req.param("sessionId"),s=this.sessionSubscriptions.get(e);return s?t.json({id:e,state:"active",subscriberCount:s.size,lastActiveAt:this.sessionLastActivity.get(e)||Date.now()}):t.json({error:"Session not found"},404)}},{method:"post",path:"/initialize",handler:async t=>{let e=await t.req.json().catch(()=>({}));return d.debug("ACP initialize request received",{body:e}),t.json({success:true,result:{capabilities:["subscribe","message","interrupt","releaseSubscription"],version:"1.0.0"}})}},{method:"post",path:"/session",handler:async t=>{await t.req.json().catch(()=>({}));let n={id:`session_${Date.now()}_${Math.random().toString(36).substring(2,9)}`,createdAt:Date.now()};return t.json(n)}},{method:"post",path:"/session/:sessionId/prompt",handler:async t=>{let e=t.req.param("sessionId"),s=await t.req.json().catch(()=>({}));if(!e)return t.json({error:"No sessionId provided"},400);try{let n=this.getChannelResolveParams(),i=await Ge({...n,sessionId:e,channel:s.channel,userId:s.userId});d.debug("channel resolved for message",{sessionId:e,channel:i.channel,source:i.source});let r=`msg_${Date.now()}_${Math.random().toString(36).substring(2,9)}`,l={id:r,sessionId:e,type:"output",content:typeof s.content=="string"?[{type:"text",text:s.content}]:s.content||[],metadata:{channel:i.channelInfo},timestamp:Date.now()};if(this.pluginLoaderRef)try{await this.pluginLoaderRef.send(i.channel,l),d.debug("message sent via pluginLoader",{messageId:r,channel:i.channel});}catch(u){return d.error("failed to send message",{sessionId:e,error:String(u)}),t.json({error:String(u)},500)}let g={messageId:r,sessionId:e,createdAt:Date.now(),metadata:{channel:i.channel,channelSource:i.source,channelInfo:i.channelInfo}};return t.status(202),t.json(g)}catch(n){return n instanceof M?(d.warn("channel selection failed",{sessionId:e,error:n.message}),t.json({error:Le(n),code:n.code},400)):(d.error("send message error",{sessionId:e,error:String(n)}),t.json({error:String(n)},500))}}},{method:"get",path:"/subscriptions",handler:t=>{let e=Array.from(this.sessionSubscriptions.entries()).map(([n,i])=>({sessionId:n,subscriberCount:i.size,subscribers:Array.from(i)})),s={subscriptions:e,total:e.length};return t.json(s)}}]}};var m=class{constructor(){chunkGY3SWWW3_cjs.a(this,"version","1.0.0");chunkGY3SWWW3_cjs.a(this,"log");chunkGY3SWWW3_cjs.a(this,"config",null);chunkGY3SWWW3_cjs.a(this,"messageHandler",null);chunkGY3SWWW3_cjs.a(this,"_status","stopped");chunkGY3SWWW3_cjs.a(this,"lastActivity",0);chunkGY3SWWW3_cjs.a(this,"startPromise",null);chunkGY3SWWW3_cjs.a(this,"stopPromise",null);}initLog(){this.log=chunkRIWYHEQ6_cjs.a.create({service:`channel:${this.platform}`});}async start(t,e){if(this.startPromise)return this.log.warn("plugin start already in progress"),this.startPromise;if(this._status==="running"){this.log.warn("plugin is already running");return}this.startPromise=this.doStartInternal(t,e);try{await this.startPromise;}finally{this.startPromise=null;}}async doStartInternal(t,e){this.log.info("starting channel plugin",{id:this.id,platform:this.platform}),this._status="starting",this.config=t,this.messageHandler=e;try{await this.doStart(),this._status="running",this.lastActivity=Date.now(),this.log.info("channel plugin started",{id:this.id});}catch(s){throw this._status="error",this.log.error("failed to start channel plugin",{error:String(s)}),s}}async stop(){if(this.stopPromise)return this.log.warn("plugin stop already in progress"),this.stopPromise;if(this._status==="stopped"){this.log.warn("plugin is already stopped");return}this.stopPromise=this.doStopInternal();try{await this.stopPromise;}finally{this.stopPromise=null;}}async doStopInternal(){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(t){throw this._status="error",this.log.error("failed to stop channel plugin",{error:String(t)}),t}}async healthCheck(){try{return {healthy:await this.doHealthCheck(),lastCheck:Date.now()}}catch(t){return {healthy:false,lastCheck:Date.now(),error:t instanceof Error?t.message:String(t)}}}getStatus(){return {status:this._status,lastActivity:this.lastActivity}}async doHealthCheck(){return this._status==="running"}updateActivity(){this.lastActivity=Date.now();}extractText(t){return t.content.filter(e=>e.type==="text").map(e=>e.text??"").join(`
|
|
3
|
-
`)}buildSessionId(t,e,s){let n=[this.platform,t,e];return s&&n.push(s),n.join("_")}async handleMessage(t){if(!this.messageHandler){this.log.warn("no message handler set, dropping message",{id:t.id});return}this.updateActivity(),await this.messageHandler.onMessage(t);}async handleEvent(t,e){if(!this.messageHandler){this.log.warn("no message handler set, dropping event",{type:t});return}await this.messageHandler.onEvent({type:t,channelId:this.id,data:e,timestamp:Date.now()});}};var K=class extends m{constructor(e="telegram-main"){super();chunkGY3SWWW3_cjs.a(this,"id");chunkGY3SWWW3_cjs.a(this,"platform","telegram");chunkGY3SWWW3_cjs.a(this,"name","Telegram Bot");chunkGY3SWWW3_cjs.a(this,"botConfig",null);chunkGY3SWWW3_cjs.a(this,"baseUrl","");chunkGY3SWWW3_cjs.a(this,"abortController",null);chunkGY3SWWW3_cjs.a(this,"lastUpdateId",0);chunkGY3SWWW3_cjs.a(this,"typingTimers",new Map);chunkGY3SWWW3_cjs.a(this,"commandRouter",new Map);chunkGY3SWWW3_cjs.a(this,"retryState",{attempts:0,maxAttempts:3,initialDelay:1e3,maxDelay:3e4});this.id=e,this.initLog();}async doStart(){if(!this.config)throw T(this.id,"CHANNEL_INIT_FAILED","Config not set");if(this.botConfig=this.config.platform,!this.botConfig.botToken)throw T(this.id,"CONFIG_MISSING_REQUIRED","Bot token is required");if(this.baseUrl=`https://api.telegram.org/bot${this.botConfig.botToken}`,this.botConfig.retry&&(this.retryState={attempts:0,maxAttempts:this.botConfig.retry.maxAttempts??3,initialDelay:this.botConfig.retry.initialDelay??1e3,maxDelay:this.botConfig.retry.maxDelay??3e4}),this.botConfig.commands)for(let s of this.botConfig.commands)this.commandRouter.set(`/${s.command}`,s);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.abortController?.abort(),this.abortController=null,this.clearAllTyping(),this.commandRouter.clear(),this.botConfig=null,this.log.info("Telegram plugin stopped");}async send(e){let s=e.metadata.channel.chatId;if(!s)throw T(this.id,"MESSAGE_SEND_FAILED","Chat ID is required");let n=this.detectParseMode(e),i=e.metadata.context?.replyTo;return this.sendApiMessage(s,this.extractText(e),{parseMode:n,replyTo:i?parseInt(i,10):void 0})}async sendTyping(e){await this.sendChatAction(e,"typing");}async sendChatAction(e,s){await this.request("sendChatAction",{chat_id:e,action:s}),this.scheduleTypingStop(e,5e3);}scheduleTypingStop(e,s){let n=this.typingTimers.get(e);n&&clearTimeout(n);let i=setTimeout(()=>{this.typingTimers.delete(e);},s);this.typingTimers.set(e,i);}clearAllTyping(){for(let e of this.typingTimers.values())clearTimeout(e);this.typingTimers.clear();}detectParseMode(e){let s=this.extractText(e);return s.includes("<")&&s.includes("</")?"HTML":s.includes("*")||s.includes("_")||s.includes("`")?"Markdown":"None"}startPolling(){this.abortController=new AbortController,this.pollLoop(),this.log.info("Polling started");}pollLoop(){(async()=>{try{await this.poll(),this.abortController?.signal.aborted||this.pollLoop();}catch(s){this.handlePollingError(s);}})();}async poll(){let e=this.botConfig?.polling?.timeout??0,s=await this.getUpdates(this.lastUpdateId+1,100,e);for(let n of s)this.lastUpdateId=Math.max(this.lastUpdateId,n.update_id),n.message&&await this.handleTelegramMessage(n.message),n.edited_message&&await this.handleTelegramMessage(n.edited_message);this.retryState.attempts=0;}handlePollingError(e){if(!this.abortController?.signal.aborted)if(this.log.error("Polling error",{error:String(e)}),this.retryState.attempts<this.retryState.maxAttempts){let s=Math.min(this.retryState.initialDelay*2**this.retryState.attempts,this.retryState.maxDelay);this.retryState.attempts++,this.log.info("Retrying polling",{attempt:this.retryState.attempts,delay:s}),this.sleep(s).then(()=>this.pollLoop());}else this.log.error("Polling failed after max attempts",{attempts:this.retryState.maxAttempts});}sleep(e){return new Promise(s=>setTimeout(s,e))}async handleTelegramMessage(e){if(this.updateActivity(),e.from?.is_bot||e.text?.startsWith("/")&&await this.handleCommand(e))return;await this.sendTyping(e.chat.id.toString());let s=this.toGatewayMessage(e);await this.handleMessage(s);}async handleCommand(e){let s=(e.text??"").split(" "),n=s[0]?.toLowerCase()??"",i=s.slice(1),r=this.commandRouter.get(n);if(!r)return false;let l={chatId:e.chat.id.toString(),userId:e.from?.id.toString(),args:i,messageId:e.message_id,reply:async(g,u)=>this.sendApiMessage(e.chat.id.toString(),g,u)};try{return await r.handler(l),!0}catch(g){return this.log.error("Command handler error",{command:n,error:String(g)}),true}}toGatewayMessage(e){let s=this.processMessageAndGetSessionId(e);return y(s,"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(),messageId:e.message_id.toString()}})}parseContent(e){let s=[],n=e.text??e.caption??"";if(e.photo&&e.photo.length>0){let i=e.photo[e.photo.length-1];i&&s.push({type:"image",image:i.file_id,mime:"image/jpeg"});}return e.document&&s.push({type:"file",data:e.document.file_id,mime:e.document.mime_type}),n&&s.push({type:"text",text:n}),s}buildSessionId(e,s,n){return n?`telegram_${e}_${s}_${n}`:`telegram_${e}_${s}`}processMessageAndGetSessionId(e){let s=e.chat.id.toString(),n=e.from?.id.toString();return e.chat.type==="private"?this.buildSessionId(this.id,s):this.buildSessionId(this.id,s,n)}async getMe(){return (await this.request("getMe")).result}async getUpdates(e,s,n){let i={offset:e,limit:s};return n>0&&(i.timeout=n),(await this.request("getUpdates",i)).result||[]}async sendApiMessage(e,s,n){let i={chat_id:e,text:s};return n?.parseMode&&n.parseMode!=="None"&&(i.parse_mode=n.parseMode),n?.replyTo&&(i.reply_to_message_id=n.replyTo),n?.replyMarkup&&(i.reply_markup=n.replyMarkup),(await this.request("sendMessage",i)).result.message_id.toString()}async request(e,s={}){let n=`${this.baseUrl}/${e}`,i;for(let r=0;r<=this.retryState.maxAttempts;r++)try{let l=await utils.Fetch.post(n,s,{timeout:this.retryState.maxDelay,retries:0,headers:{"Content-Type":"application/json"},signal:this.abortController?.signal}),g=l.data;if(!g.ok){if(g.error_code===429){this.log.warn("Rate limited, retrying after",{retryAfter:l.headers.get("Retry-After")});let u=parseInt(l.headers.get("Retry-After")??"1",10)*1e3;await this.sleep(u);continue}throw T(this.id,"CHANNEL_CONNECTION_FAILED",`Telegram API error: ${g.description}`,g.error_code!==400&&g.error_code!==401)}return g}catch(l){if(l instanceof x||this.abortController?.signal.aborted)throw l;if(i=l instanceof Error?l:new Error(String(l)),this.log.warn("Request failed, retrying",{method:e,attempt:r+1,error:i.message}),r<this.retryState.maxAttempts){let g=Math.min(this.retryState.initialDelay*2**r,this.retryState.maxDelay);await this.sleep(g);}}throw T(this.id,"CHANNEL_CONNECTION_FAILED",`Failed after ${this.retryState.maxAttempts+1} attempts: ${i?.message}`,true)}};var V=class extends m{constructor(e="discord-main"){super();chunkGY3SWWW3_cjs.a(this,"id");chunkGY3SWWW3_cjs.a(this,"platform","discord");chunkGY3SWWW3_cjs.a(this,"name","Discord Bot");chunkGY3SWWW3_cjs.a(this,"botConfig",null);chunkGY3SWWW3_cjs.a(this,"baseUrl","https://discord.com/api/v10");chunkGY3SWWW3_cjs.a(this,"gatewayWs",null);chunkGY3SWWW3_cjs.a(this,"heartbeatInterval",null);chunkGY3SWWW3_cjs.a(this,"sessionId",null);chunkGY3SWWW3_cjs.a(this,"sequenceNumber",null);chunkGY3SWWW3_cjs.a(this,"abortController",null);chunkGY3SWWW3_cjs.a(this,"reconnectAttempts",0);chunkGY3SWWW3_cjs.a(this,"maxReconnectAttempts",5);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.abortController?.abort(),this.abortController=null,this.heartbeatInterval&&(clearInterval(this.heartbeatInterval),this.heartbeatInterval=null),this.gatewayWs&&(this.gatewayWs.close(),this.gatewayWs=null),this.botConfig=null,this.reconnectAttempts=0,this.log.info("Discord plugin stopped");}async send(e){let s=e.metadata.channel.chatId;if(!s)throw new Error("Channel ID is required");let n=this.extractText(e);return (await this.createMessage(s,n,e.metadata.context?.replyTo)).id}async startGateway(){if(this.abortController?.signal.aborted)return;let s=`${(await this.getGatewayBot()).url}/?v=10&encoding=json`;this.gatewayWs=new WebSocket(s),this.gatewayWs.onmessage=n=>{this.handleGatewayMessage(JSON.parse(n.data.toString()));},this.gatewayWs.onclose=()=>{if(!this.abortController?.signal.aborted)if(this.log.info("Gateway disconnected"),this.reconnectAttempts<this.maxReconnectAttempts){let n=Math.min(1e3*2**this.reconnectAttempts,3e4);this.reconnectAttempts++,this.log.info("Attempting to reconnect",{attempt:this.reconnectAttempts,delay:n}),setTimeout(()=>this.startGateway(),n);}else this.log.error("Max reconnect attempts reached");},this.gatewayWs.onerror=n=>{this.log.error("Gateway error",{error:String(n)});};}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,s){e==="MESSAGE_CREATE"&&this.handleDiscordMessage(s);}async handleDiscordMessage(e){if(e.author.bot)return;let s=this.toGatewayMessage(e);await this.handleMessage(s);}toGatewayMessage(e){let s=this.processMessageAndGetSessionId(e);return y(s,"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 s=[];e.content&&s.push({type:"text",text:e.content});for(let n of e.attachments)n.content_type?.startsWith("image/")?s.push({type:"image",image:n.url,mime:n.content_type}):s.push({type:"file",data:n.url,mime:n.content_type});return s}buildSessionId(e,s,n){return n?`discord_${e}_${s}_${n}`:`discord_${e}_${s}`}processMessageAndGetSessionId(e){let s=e.channel_id,n=e.author.id;return this.buildSessionId(this.id,s,n)}async getCurrentUser(){return this.request("GET","/users/@me")}async getGatewayBot(){return this.request("GET","/gateway/bot")}async createMessage(e,s,n){let i={content:s};return n&&(i.message_reference={message_id:n}),this.request("POST",`/channels/${e}/messages`,i)}async request(e,s,n){let i=`${this.baseUrl}${s}`,r;if(e==="GET"?r=await utils.Fetch.get(i,{headers:{Authorization:`Bot ${this.botConfig?.botToken}`,"Content-Type":"application/json"}}):e==="POST"?r=await utils.Fetch.post(i,n,{headers:{Authorization:`Bot ${this.botConfig?.botToken}`,"Content-Type":"application/json"}}):e==="DELETE"?r=await utils.Fetch.del(i,{headers:{Authorization:`Bot ${this.botConfig?.botToken}`,"Content-Type":"application/json"}}):r=await utils.Fetch.request(i,{method:e,body:n,headers:{Authorization:`Bot ${this.botConfig?.botToken}`,"Content-Type":"application/json"}}),!r.ok)throw new Error(`Discord API error: ${r.data}`);return r.data}};var J=class extends m{constructor(e="slack-main"){super();chunkGY3SWWW3_cjs.a(this,"id");chunkGY3SWWW3_cjs.a(this,"platform","slack");chunkGY3SWWW3_cjs.a(this,"name","Slack Bot");chunkGY3SWWW3_cjs.a(this,"botConfig",null);chunkGY3SWWW3_cjs.a(this,"baseUrl","");chunkGY3SWWW3_cjs.a(this,"socketModeClient",null);chunkGY3SWWW3_cjs.a(this,"abortController",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");this.abortController=new AbortController,this.baseUrl="https://slack.com/api";let e=await this.authTest();this.log.info("Slack bot connected",{user:e.user,team:e.team,bot_id:e.bot_id}),this.botConfig.socketMode&&this.botConfig.appToken&&await this.startSocketMode();}async doStop(){this.abortController?.abort(),this.abortController=null,this.socketModeClient&&(this.socketModeClient.close(),this.socketModeClient=null),this.botConfig=null,this.log.info("Slack plugin stopped");}async send(e){let s=e.metadata.channel.chatId;if(!s)throw new Error("Channel ID is required");let n=this.extractText(e);return (await this.postMessage(s,n,e.metadata.context?.threadId)).ts}async startSocketMode(){if(!this.botConfig?.appToken)return;let e="wss://wss-primary.slack.com/socket";this.socketModeClient=new WebSocket(e),this.socketModeClient.onmessage=async s=>{try{let n=JSON.parse(s.data.toString());await this.handleSocketModeEvent(n);}catch(n){this.log.error("Socket mode event error",{error:String(n)});}},this.log.info("Socket mode started");}async handleSocketModeEvent(e){if(e.envelope_id&&this.socketModeClient?.send(JSON.stringify({envelope_id:e.envelope_id})),e.type==="events_api"&&e.payload?.event){let s=e.payload.event;s.type==="message"&&!s.subtype&&await this.handleSlackMessage(s);}}async handleSlackMessage(e){this.updateActivity();let s=this.toGatewayMessage(e);await this.handleMessage(s);}toGatewayMessage(e){let s=this.processMessageAndGetSessionId(e);return y(s,"input",this.parseContent(e),{channel:{platform:"slack",channelId:this.id,userId:e.user,chatId:e.channel,ts:e.ts,threadTs:e.thread_ts},context:{threadId:e.thread_ts,conversationId:e.channel}})}parseContent(e){let s=[];if(e.text&&s.push({type:"text",text:e.text}),e.files)for(let n of e.files)s.push({type:"file",data:n.url_private,mime:n.mimetype});return s}buildSessionId(e,s,n){return n?`slack_${e}_${s}_${n}`:`slack_${e}_${s}`}processMessageAndGetSessionId(e){let s=e.channel,n=e.user;return this.buildSessionId(this.id,s,n)}async authTest(){return await this.request("auth.test")}async postMessage(e,s,n){let i={channel:e,text:s};return n&&(i.thread_ts=n),await this.request("chat.postMessage",i)}async request(e,s={}){let n=`${this.baseUrl}/${e}`,i=await utils.Fetch.post(n,s,{headers:{Authorization:`Bearer ${this.botConfig?.botToken}`,"Content-Type":"application/json; charset=utf-8"}});if(!i.data.ok)throw new Error(`Slack API error: ${i.data.error}`);return i.data}};var Q=class extends m{constructor(e="feishu-main"){super();chunkGY3SWWW3_cjs.a(this,"id");chunkGY3SWWW3_cjs.a(this,"platform","feishu");chunkGY3SWWW3_cjs.a(this,"name","Feishu Bot");chunkGY3SWWW3_cjs.a(this,"botConfig",null);chunkGY3SWWW3_cjs.a(this,"baseUrl","https://open.feishu.cn/open-apis");chunkGY3SWWW3_cjs.a(this,"tenantAccessToken",null);chunkGY3SWWW3_cjs.a(this,"tokenExpireTime",0);chunkGY3SWWW3_cjs.a(this,"abortController",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.appId||!this.botConfig.appSecret)throw new Error("App ID and App Secret are required");this.abortController=new AbortController,await this.refreshTenantAccessToken(),this.log.info("Feishu bot connected",{app_id:this.botConfig.appId});}async doStop(){this.abortController?.abort(),this.abortController=null,this.botConfig=null,this.tenantAccessToken=null,this.log.info("Feishu plugin stopped");}async send(e){let s=e.metadata.channel.chatId;if(!s)throw new Error("Chat ID is required");await this.ensureTokenValid();let n=this.extractText(e);return (await this.sendMessage(s,n)).message_id}async handleWebhookEvent(e){if(this.updateActivity(),this.botConfig?.verificationToken,e.event?.message){let s=this.toGatewayMessage(e);await this.handleMessage(s);}}toGatewayMessage(e){let s=e.event.message,n=this.processMessageAndGetSessionId(e);return y(n,"input",this.parseContent(s),{channel:{platform:"feishu",channelId:this.id,userId:s.sender.sender_id.open_id,chatId:s.chat_id,messageId:s.message_id,rootId:s.root_id,parentId:s.parent_id,tenantKey:e.tenant_key},context:{threadId:s.root_id,conversationId:s.chat_id}})}parseContent(e){let s=[];try{let n=JSON.parse(e.content);switch(e.message_type){case "text":n.text&&s.push({type:"text",text:n.text});break;case "post":if(n.content){let i=this.extractPostContent(n.content);s.push({type:"text",text:i});}break;case "image":n.image_key&&s.push({type:"image",image:n.image_key,mime:"image/jpeg"});break;case "file":n.file_key&&s.push({type:"file",data:n.file_key,mime:n.file_type});break;default:s.push({type:"text",text:JSON.stringify(n)});}}catch{s.push({type:"text",text:e.content});}return s}extractPostContent(e){let s=[];for(let n of e)if(typeof n=="object"&&n!==null){let i=n;i.text&&s.push(String(i.text)),Array.isArray(i.children)&&s.push(this.extractPostContent(i.children));}return s.join(`
|
|
4
|
-
`)}buildSessionId(e,s,n){return n?`feishu_${e}_${s}_${n}`:`feishu_${e}_${s}`}processMessageAndGetSessionId(e){let s=e.event.message,n=s.chat_id,i=s.sender.sender_id.open_id;return this.buildSessionId(this.id,n,i)}async refreshTenantAccessToken(){let s=(await utils.Fetch.post(`${this.baseUrl}/auth/v3/tenant_access_token/internal`,{app_id:this.botConfig?.appId,app_secret:this.botConfig?.appSecret},{headers:{"Content-Type":"application/json"}})).data;if(s.code!==0)throw new Error(`Feishu API error: ${s.msg}`);this.tenantAccessToken=s.tenant_access_token,this.tokenExpireTime=Date.now()+s.expire*1e3-6e4;}async ensureTokenValid(){(!this.tenantAccessToken||Date.now()>=this.tokenExpireTime)&&await this.refreshTenantAccessToken();}async sendMessage(e,s){let i=(await utils.Fetch.post(`${this.baseUrl}/im/v1/messages?receive_id_type=chat_id`,{receive_id:e,msg_type:"text",content:JSON.stringify({text:s})},{headers:{Authorization:`Bearer ${this.tenantAccessToken}`,"Content-Type":"application/json"}})).data;if(i.code!==0)throw new Error(`Feishu API error: ${i.msg}`);return i.data}};var X=class extends m{constructor(e="wechat-main"){super();chunkGY3SWWW3_cjs.a(this,"id");chunkGY3SWWW3_cjs.a(this,"platform","wechat");chunkGY3SWWW3_cjs.a(this,"name","WeChat Bot");chunkGY3SWWW3_cjs.a(this,"botConfig",null);chunkGY3SWWW3_cjs.a(this,"accessToken",null);chunkGY3SWWW3_cjs.a(this,"tokenExpireTime",0);chunkGY3SWWW3_cjs.a(this,"abortController",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.appId||!this.botConfig.appSecret)throw new Error("App ID and App Secret are required");this.abortController=new AbortController,await this.refreshAccessToken(),this.log.info("WeChat bot connected",{app_id:this.botConfig.appId,mode:this.botConfig.mode});}async doStop(){this.abortController?.abort(),this.abortController=null,this.botConfig=null,this.accessToken=null,this.log.info("WeChat plugin stopped");}async send(e){let s=e.metadata.channel.userId;if(!s)throw new Error("User ID is required");await this.ensureTokenValid();let n=this.extractText(e);return this.botConfig?.mode==="wecom"?await this.sendWeComMessage(s,n):await this.sendOfficialMessage(s,n)}async handleWebhookMessage(e){this.updateActivity();let s=this.parseXmlMessage(e);if(s){let n=this.toGatewayMessage(s);await this.handleMessage(n);}}parseXmlMessage(e){let s=r=>e.match(new RegExp(`<${r}><!\\[CDATA\\[(.*?)\\]\\]></${r}>`))?.[1],n=r=>{let l=e.match(new RegExp(`<${r}>(\\d+)</${r}>`));return l?.[1]?parseInt(l[1],10):void 0},i=s("MsgType");return i?{ToUserName:s("ToUserName")??"",FromUserName:s("FromUserName")??"",CreateTime:n("CreateTime")??0,MsgType:i,Content:s("Content"),MsgId:n("MsgId"),PicUrl:s("PicUrl"),MediaId:s("MediaId")}:null}toGatewayMessage(e){let s=this.processMessageAndGetSessionId(e);return y(s,"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 s=[];switch(e.MsgType){case "text":e.Content&&s.push({type:"text",text:e.Content});break;case "image":(e.PicUrl||e.MediaId)&&s.push({type:"image",image:e.PicUrl??e.MediaId??"",mime:"image/jpeg"});break;case "voice":e.MediaId&&s.push({type:"file",data:e.MediaId,mime:"audio/amr"}),"Recognition"in e&&e.Recognition&&s.push({type:"text",text:e.Recognition});break;case "video":case "shortvideo":e.MediaId&&s.push({type:"file",data:e.MediaId,mime:"video/mp4"});break;default:e.Content&&s.push({type:"text",text:e.Content});}return s}buildSessionId(e,s,n){return n?`wechat_${e}_${s}_${n}`:`wechat_${e}_${s}`}processMessageAndGetSessionId(e){let s=e.FromUserName;return this.buildSessionId(this.id,s)}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 n=(await utils.Fetch.get(e)).data;if(n.errcode&&n.errcode!==0)throw new Error(`WeChat API error: ${n.errmsg}`);this.accessToken=n.access_token,this.tokenExpireTime=Date.now()+n.expires_in*1e3-6e4;}async ensureTokenValid(){(!this.accessToken||Date.now()>=this.tokenExpireTime)&&await this.refreshAccessToken();}async sendOfficialMessage(e,s){let i=(await utils.Fetch.post(`https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=${this.accessToken}`,{touser:e,msgtype:"text",text:{content:s}},{headers:{"Content-Type":"application/json"}})).data;if(i.errcode!==0)throw new Error(`WeChat API error: ${i.errmsg}`);return i.msg_id?.toString()??Date.now().toString()}async sendWeComMessage(e,s){let i=(await utils.Fetch.post(`https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=${this.accessToken}`,{touser:e,msgtype:"text",agentid:this.botConfig?.agentId,text:{content:s}},{headers:{"Content-Type":"application/json"}})).data;if(i.errcode!==0)throw new Error(`WeCom API error: ${i.errmsg}`);return i.msgid??Date.now().toString()}};var Y=class extends m{constructor(e="webchat-main"){super();chunkGY3SWWW3_cjs.a(this,"id");chunkGY3SWWW3_cjs.a(this,"platform","webchat");chunkGY3SWWW3_cjs.a(this,"name","WebChat");chunkGY3SWWW3_cjs.a(this,"webChatConfig",null);chunkGY3SWWW3_cjs.a(this,"connections",new Map);chunkGY3SWWW3_cjs.a(this,"heartbeatInterval",null);chunkGY3SWWW3_cjs.a(this,"abortController",null);chunkGY3SWWW3_cjs.a(this,"wsServer",null);this.id=e,this.initLog();}async doStart(){if(!this.config)throw new Error("Config not set");this.webChatConfig=this.config.platform,this.abortController=new AbortController,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 _(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.abortController?.abort(),this.abortController=null,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,this.log.info("WebChat plugin stopped");}async send(e){let s=e.sessionId,n=e.metadata.channel.userId;if(this.wsServer&&s){let l={type:"message",sessionId:s,properties:{sessionID:s,content:e.content,timestamp:e.timestamp||Date.now()}};return this.wsServer.broadcastToSession(s,l),e.id}if(!n)throw new Error("User ID is required");let i=this.findConnectionByUser(n);if(!i)throw new Error("User not connected");let r={type:"message",sessionId:s,content:e.content,metadata:e.metadata};return i.ws.send(JSON.stringify(r)),i.lastActivity=Date.now(),e.id}pushEvent(e,s){this.wsServer&&this.wsServer.broadcastToSession(e,s);}getWebSocketServer(){return this.wsServer}async handleConnection(e,s){let n=this.generateConnectionId();if(this.connections.size>=(this.webChatConfig?.maxConnections??1e3)){e.close(1013,"Maximum connections reached");return}let i={id:n,ws:e,userId:s,lastActivity:Date.now()};this.connections.set(n,i),e.onmessage=r=>{this.handleWebSocketMessage(i,r.data.toString());},e.onclose=()=>{this.connections.delete(n),this.log.debug("WebSocket closed",{connId:n,userId:s});},e.onerror=r=>{this.log.error("WebSocket error",{connId:n,userId:s,error:String(r)});},this.log.info("WebSocket connected",{connId:n,userId:s});}async handleWebSocketMessage(e,s){this.updateActivity(),e.lastActivity=Date.now();try{let n=JSON.parse(s);switch(n.type){case "message":await this.handleChatMessage(e,n);break;case "ping":e.ws.send(JSON.stringify({type:"pong"}));break;case "subscribe":n.sessionId&&(e.sessionId=n.sessionId);break;case "unsubscribe":e.sessionId=void 0;break}}catch(n){this.log.error("Failed to handle WebSocket message",{error:String(n)});}}async handleChatMessage(e,s){if(!s.content||!s.sessionId)return;let n=y(s.sessionId,"input",s.content,{channel:{platform:"webchat",channelId:this.id,userId:e.userId,chatId:s.sessionId,connectionId:e.id},context:{conversationId:s.sessionId},...s.metadata});await this.handleMessage(n);}startHeartbeat(){let e=this.webChatConfig?.heartbeatInterval??3e4;this.heartbeatInterval=setInterval(()=>{let s=Date.now(),n=this.webChatConfig?.connectionTimeout??6e4;for(let[i,r]of this.connections)s-r.lastActivity>n&&(this.log.info("Closing inactive connection",{connId:i}),r.ws.close(1001,"Connection timeout"),this.connections.delete(i));},e);}findConnectionByUser(e){for(let s of this.connections.values())if(s.userId===e)return s}generateConnectionId(){return `conn_${Date.now()}_${Math.random().toString(36).substring(2,9)}`}getConnectionCount(){return this.connections.size}getWsServerConnectionCount(){return this.wsServer?.getConnectionCount()??0}};var Z=class extends m{constructor(e="signal-main"){super();chunkGY3SWWW3_cjs.a(this,"id");chunkGY3SWWW3_cjs.a(this,"platform","signal");chunkGY3SWWW3_cjs.a(this,"name","Signal Bot");chunkGY3SWWW3_cjs.a(this,"botConfig",null);chunkGY3SWWW3_cjs.a(this,"abortController",null);chunkGY3SWWW3_cjs.a(this,"lastTimestamp",0);chunkGY3SWWW3_cjs.a(this,"pollingIntervalMs",5e3);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");this.abortController=new AbortController;let e=await this.getAccountInfo();this.log.info("Signal bot connected",{number:this.botConfig.phoneNumber,address:e.address}),this.startPolling();}async doStop(){this.abortController?.abort(),this.abortController=null,this.botConfig=null,this.lastTimestamp=0,this.log.info("Signal plugin stopped");}async send(e){let s=e.metadata.channel.userId;if(!s)throw new Error("Recipient is required");let n=this.extractText(e);return (await this.sendMessage(s,n)).timestamp.toString()}startPolling(){this.pollLoop(),this.log.info("Polling started",{interval:this.pollingIntervalMs});}pollLoop(){if(this.abortController?.signal.aborted)return;(async()=>{try{await this.poll();}catch(s){this.log.error("Polling error",{error:String(s)});}})().then(()=>{this.abortController?.signal.aborted||setTimeout(()=>this.pollLoop(),this.pollingIntervalMs);});}async poll(){let e=await this.receiveMessages(this.lastTimestamp);for(let s of e)this.lastTimestamp=s.envelope.timestamp,s.envelope.dataMessage&&await this.handleSignalMessage(s);}async handleSignalMessage(e){this.updateActivity();let s=this.toGatewayMessage(e);await this.handleMessage(s);}toGatewayMessage(e){let s=this.processMessageAndGetSessionId(e),n=e.envelope.dataMessage;return y(s,"input",this.parseContent(n),{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 s=[];if(!e)return s;if(e.message&&s.push({type:"text",text:e.message}),e.attachments)for(let n of e.attachments)n.contentType.startsWith("image/")?s.push({type:"image",image:n.id,mime:n.contentType}):s.push({type:"file",data:n.id,mime:n.contentType});return s}buildSessionId(e,s,n){return n?`signal_${e}_${s}_${n}`:`signal_${e}_${s}`}processMessageAndGetSessionId(e){let s=e.envelope.sourceNumber;return this.buildSessionId(this.id,s)}async getAccountInfo(){return await this.request("GET",`/v1/accounts/${this.botConfig?.phoneNumber}`)}async receiveMessages(e){let s=e>0?`?since=${e}`:"";return await this.request("GET",`/v1/receive/${this.botConfig?.phoneNumber}${s}`)||[]}async sendMessage(e,s){return await this.request("POST","/v2/send",{number:this.botConfig?.phoneNumber,recipients:[e],message:s})}async request(e,s,n){let i=`${this.botConfig?.serviceUrl}${s}`,r={"Content-Type":"application/json"};this.botConfig?.password&&(r.Authorization=`Basic ${Buffer.from(`${this.botConfig.phoneNumber}:${this.botConfig.password}`).toString("base64")}`);let l;if(e==="GET"?l=await utils.Fetch.get(i,{headers:r}):e==="POST"?l=await utils.Fetch.post(i,n,{headers:r}):e==="DELETE"?l=await utils.Fetch.del(i,{headers:r}):l=await utils.Fetch.request(i,{method:e,body:n,headers:r}),!l.ok)throw new Error(`Signal API error: ${l.data}`);return l.data}};var ee=class extends m{constructor(e="nostr-main"){super();chunkGY3SWWW3_cjs.a(this,"id");chunkGY3SWWW3_cjs.a(this,"platform","nostr");chunkGY3SWWW3_cjs.a(this,"name","Nostr Bot");chunkGY3SWWW3_cjs.a(this,"botConfig",null);chunkGY3SWWW3_cjs.a(this,"relayConnections",new Map);chunkGY3SWWW3_cjs.a(this,"subscriptions",new Map);chunkGY3SWWW3_cjs.a(this,"eventHandlers",new Map);chunkGY3SWWW3_cjs.a(this,"abortController",null);chunkGY3SWWW3_cjs.a(this,"relayReconnectAttempts",new Map);chunkGY3SWWW3_cjs.a(this,"maxReconnectAttempts",5);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");this.abortController=new AbortController;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(){this.abortController?.abort(),this.abortController=null;for(let[e,s]of this.relayConnections)s.close();this.relayConnections.clear(),this.subscriptions.clear(),this.eventHandlers.clear(),this.relayReconnectAttempts.clear(),this.botConfig=null,this.log.info("Nostr plugin stopped");}async send(e){if(!this.botConfig?.privateKey)throw new Error("Private key is required for sending messages");let s=this.extractText(e),n=await this.createEvent(s,e.metadata.context?.replyTo);return await this.publishEvent(n),n.id}async connectToRelay(e){if(this.abortController?.signal.aborted)return;let s=new WebSocket(e);s.onopen=()=>{this.log.info("Connected to relay",{relay:e}),this.relayReconnectAttempts.set(e,0);for(let[n,i]of this.subscriptions)this.subscribeToRelay(s,n,i);},s.onmessage=n=>{this.handleRelayMessage(e,n.data.toString());},s.onclose=()=>{if(this.abortController?.signal.aborted)return;this.log.info("Disconnected from relay",{relay:e}),this.relayConnections.delete(e);let n=this.relayReconnectAttempts.get(e)??0;if(n<this.maxReconnectAttempts){let i=Math.min(1e3*2**n,3e4);this.relayReconnectAttempts.set(e,n+1),this.log.info("Attempting to reconnect to relay",{relay:e,attempt:n+1,delay:i}),setTimeout(()=>this.connectToRelay(e),i);}else this.log.error("Max reconnect attempts reached for relay",{relay:e});},s.onerror=n=>{this.log.error("Relay error",{relay:e,error:String(n)});},this.relayConnections.set(e,s);}handleRelayMessage(e,s){this.updateActivity();try{let n=JSON.parse(s);if(n[0]==="EVENT"&&n[2]){let i=n[2];this.handleNostrEvent(i);}if(n[0]==="OK"){let i=n[1],r=n[2];this.log.debug("Event published",{eventId:i,success:r});}}catch(n){this.log.error("Failed to handle relay message",{error:String(n)});}return Promise.resolve()}async handleNostrEvent(e){if(e.kind!==1||e.pubkey===this.botConfig?.publicKey)return;let s=this.toGatewayMessage(e);await this.handleMessage(s);}toGatewayMessage(e){let s=this.processMessageAndGetSessionId(e);return y(s,"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 s=[];e.content&&s.push({type:"text",text:e.content});for(let n of e.tags)if(n[0]==="url"&&n[1]){let i=n[1];i.match(/\.(jpg|jpeg|png|gif|webp)$/i)&&s.push({type:"image",image:i});}return s}findReplyTo(e){for(let s of e)if(s[0]==="e"&&s[1])return s[1]}buildSessionId(e,s,n){return n?`nostr_${e}_${s}_${n}`:`nostr_${e}_${s}`}processMessageAndGetSessionId(e){return this.buildSessionId(this.id,e.pubkey)}subscribeToRelay(e,s,n){let i=["REQ",s,n];e.send(JSON.stringify(i));}async createEvent(e,s){let n=[];s&&n.push(["e",s]);let i={id:"",pubkey:this.botConfig?.publicKey??"",created_at:Math.floor(Date.now()/1e3),kind:1,tags:n,content:e,sig:""};return i.id=await this.computeEventId(i),i.sig=await this.signEvent(i),i}async computeEventId(e){let s=JSON.stringify([0,e.pubkey,e.created_at,e.kind,e.tags,e.content]),i=new TextEncoder().encode(s),r=await crypto.subtle.digest("SHA-256",i);return Array.from(new Uint8Array(r)).map(g=>g.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 s=["EVENT",e];for(let n of this.relayConnections.values())n.readyState===WebSocket.OPEN&&n.send(JSON.stringify(s));}};var Ue=class extends m{constructor(e){super();chunkGY3SWWW3_cjs.a(this,"id");chunkGY3SWWW3_cjs.a(this,"platform");chunkGY3SWWW3_cjs.a(this,"name");chunkGY3SWWW3_cjs.a(this,"version");chunkGY3SWWW3_cjs.a(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 s=e;this.options.actions?.beforeSend&&(s=await this.options.actions.beforeSend(e));let n=await this.options.messaging.send(s);return this.options.actions?.afterSend&&await this.options.actions.afterSend(s,n),n}async healthCheck(){return this.options.status?.healthCheck?{healthy:await this.options.status.healthCheck(),lastCheck:Date.now()}:{healthy:true,lastCheck:Date.now()}}};function as(c){return new Ue(c)}var Fe={telegram:K,discord:V,slack:J,feishu:Q,wechat:X,webchat:Y,signal:Z,nostr:ee};function so(){return Object.keys(Fe)}function cs(c){let t={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:t,replyToMessageId:c.replyToMessageId},timestamp:c.timestamp??Date.now()}}var st=class{constructor(t){chunkGY3SWWW3_cjs.a(this,"log",chunkRIWYHEQ6_cjs.a.create({service:"gateway:server"}));chunkGY3SWWW3_cjs.a(this,"config");chunkGY3SWWW3_cjs.a(this,"router");chunkGY3SWWW3_cjs.a(this,"sessionManager");chunkGY3SWWW3_cjs.a(this,"pluginLoader");chunkGY3SWWW3_cjs.a(this,"messageStore");chunkGY3SWWW3_cjs.a(this,"contactStore",null);chunkGY3SWWW3_cjs.a(this,"wsServer",null);chunkGY3SWWW3_cjs.a(this,"running",false);chunkGY3SWWW3_cjs.a(this,"connections",0);this.log.debug("GatewayServer.constructor: starting"),this.config=t,this.log.debug("GatewayServer.constructor: creating MessageRouter"),this.router=new Se,this.log.debug("GatewayServer.constructor: creating GatewaySessionManager"),this.sessionManager=new $,this.log.debug("GatewayServer.constructor: creating ChannelPluginLoader"),this.pluginLoader=new q({healthCheckInterval:6e4}),this.log.debug("GatewayServer.constructor: pluginLoader created");let e=D(),s=A();this.log.debug("GatewayServer.constructor: hasAgentAdapter",{hasAdapter:s}),this.messageStore=new W({storagePath:`${e.Path.data}/gateway-messages`}),s&&(this.log.debug("GatewayServer.constructor: adapter exists, setting message storage"),k().setMessageStorage({store:async i=>{let r=cs(i);await this.messageStore.store(r);},updateStatus:async(i,r,l)=>{await this.messageStore.updateStatus(i,r,l);}})),this.pluginLoader.setMessageHandler({onMessage:n=>this.onMessage(n),onEvent:n=>this.onEvent(n)}),this.log.debug("GatewayServer.constructor: completed");}async loadChannelPlugins(){try{let{config:t}=await b(),e=t.channels;if(!e){this.log.debug("no channel config found, skipping plugin loading");return}for(let[s,n]of Object.entries(e)){if(!n?.enabled){this.log.debug("channel disabled or not configured",{platform:s});continue}let i=Fe[s];if(!i){this.log.warn("no plugin class found for platform",{platform:s});continue}let r=`${s}-main`,l=new i(r),g={enabled:!0,platform:n,session:{autoCreate:!0,resetPolicy:"onNewConversation"},routing:{broadcast:!0,dispatchToSubAgent:!0}};try{await this.pluginLoader.register(l,g),this.log.info("channel plugin registered",{platform:s,id:r});}catch(u){this.log.error("failed to register channel plugin",{platform:s,error:String(u)});}}}catch(t){this.log.error("failed to load channel plugins",{error:String(t)});}}async start(){if(this.log.debug("GatewayServer.start: called"),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}),this.log.debug("GatewayServer.start: initializing sessionManager"),await this.sessionManager.initialize(),this.log.debug("GatewayServer.start: sessionManager initialized"),this.log.debug("GatewayServer.start: initializing contactStore");let e=`${D().Path.data}/gateway`,s=new H({baseDir:e});this.contactStore=new z(s,{cacheSize:1e3}),this.contactStore.initialize&&await this.contactStore.initialize(),this.log.debug("GatewayServer.start: contactStore initialized"),this.log.debug("GatewayServer.start: creating WebSocket server"),this.wsServer=new _({port:this.config.port,hostname:this.config.hostname,path:String(this.config.path??"/")}),this.log.debug("GatewayServer.start: calling wsServer.start()"),await this.wsServer.start(),this.log.debug("GatewayServer.start: wsServer started"),await this.loadChannelPlugins(),this.log.debug("GatewayServer.start: starting plugins in background"),this.pluginLoader.startAll().catch(r=>{this.log.error("plugin startup error",{error:String(r)});}),this.log.debug("GatewayServer.start: plugins startup initiated"),this.log.debug("GatewayServer.start: starting health check timer"),this.pluginLoader.startHealthCheckTimer();let{config:n}=await b(),i=n.defaults;this.wsServer.setChannelResolveDeps({sessionManager:{get:r=>this.sessionManager.get(r),bindBackendSession:(r,l)=>this.sessionManager.bindBackendSession(r,l)},pluginLoader:{getPlugin:r=>this.pluginLoader.getPlugin(r),getAllPlugins:()=>this.pluginLoader.getAllPlugins(),isRunning:r=>this.pluginLoader.isRunning(r)},defaultChannel:i?.channel,fallbackChannels:i?.fallbackChannels,contactStore:this.contactStore?{findByPlatformId:(r,l)=>this.contactStore.findByPlatformId(r,l),findByUid:r=>this.contactStore.findByUid(r),listByPlatform:(r,l,g)=>this.contactStore.listByPlatform(r,l,g),count:r=>this.contactStore.count(r)}:void 0}),this.wsServer.setPluginLoader({send:(r,l)=>this.sendToChannel(r,l)}),this.running=true,this.log.debug("GatewayServer.start: completed"),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(t){if(this.log.debug("message received",{id:t.id,sessionId:t.sessionId,type:t.type}),this.messageStore.isProcessed(t.id)){this.log.warn("message already processed, skipping",{id:t.id});return}await this.storeMessage(t),t.type==="input"&&await this.saveContactFromMessage(t),await this.sessionManager.incrementMessageCountOrCreate(t.sessionId,t.metadata.channel),(await this.router.route(t)).broadcastCount===0&&t.type==="input"&&(this.log.debug("no subscribers, dispatching to local SubAgent",{id:t.id}),await this.dispatchToSubAgent(t));}async saveContactFromMessage(t){if(!this.contactStore)return;let{platform:e,channelId:s,userId:n,chatId:i}=t.metadata.channel;if(!e||e==="api")return;let r=n||i;if(r)try{let l=await this.contactStore.getOrCreate(e,r,{username:t.metadata.channel?.username,displayName:t.metadata.channel?.firstName});this.log.debug("contact saved",{uid:l.uid});}catch(l){this.log.warn("failed to save contact",{platform:e,stableId:r,error:String(l)});}}async dispatchToSubAgent(t){if(!A()){this.log.debug("no agent adapter, message remains pending",{id:t.id});return}try{let s=k().subAgentRunner;if(!s){this.log.debug("no SubAgentRunner available, message remains pending",{id:t.id});return}this.log.info("dispatching message to local SubAgent",{id:t.id,sessionId:t.sessionId});let n=this.sessionManager.get(t.sessionId),i=n?.backendSessionId||void 0;await this.triggerHook(plugin.HookEvent.GatewayMessageProcess,{messageId:t.id,sessionId:t.sessionId,backendSessionId:i,processorId:"subagent",processType:"subagent",agent:"default",timestamp:Date.now()});let r={source:"gateway",title:`Message from ${t.sessionId}`,agent:"default",sessionId:i,parts:t.content},l=await s.run(r),g=l.metadata?.sessionId;if(g&&n&&n.backendSessionId!==g&&(await this.sessionManager.bindBackendSession(t.sessionId,g),this.log.info("backend session bound",{gatewaySessionId:t.sessionId,backendSessionId:g})),await this.triggerHook(plugin.HookEvent.GatewayMessageComplete,{messageId:t.id,sessionId:t.sessionId,backendSessionId:g||i,processorId:"subagent",status:"completed",timestamp:Date.now()}),l.output){let u=t.metadata.channel;u?.channelId&&await this.sendToChannel(u.channelId,{id:`msg_${Date.now()}_${Math.random().toString(36).slice(2,9)}`,sessionId:t.sessionId,type:"output",content:[{type:"text",text:l.output}],metadata:{channel:u},timestamp:Date.now()});}this.log.info("SubAgent response sent",{id:t.id});}catch(e){this.log.error("SubAgent dispatch failed",{id:t.id,error:String(e)});}}async triggerHook(t,e){if(A())try{await k().hookRegistry.emit({name:t,data:e});}catch{this.log.debug("hook trigger skipped",{event:t});}}async storeMessage(t){let e={messageId:t.id,sessionId:t.sessionId,type:t.type,content:t.content,channel:t.metadata.channel,replyToMessageId:t.metadata.replyToMessageId,timestamp:t.timestamp||Date.now()};if(A())try{await k().hookRegistry.emit({name:plugin.HookEvent.GatewayMessageReceive,data:e}),this.log.debug("message stored via hook",{messageId:t.id});return}catch{this.log.debug("hook trigger skipped, using local storage");}await this.messageStore.store(t),this.log.debug("message stored locally",{messageId:t.id});}async processMessage(t,e,s,n={}){if(await this.messageStore.updateStatus(e,"processing",{processedBy:s}),A())try{await k().hookRegistry.emit({name:plugin.HookEvent.GatewayMessageProcess,data:{messageId:e,sessionId:t,processorId:s,processType:n.processType||"direct",agent:n.agent,timestamp:Date.now()}});}catch{this.log.debug("hook trigger skipped (no context)");}}async completeMessage(t,e,s,n={}){let{success:i=true,error:r,responseMessageId:l,tokens:g,duration:u}=n;if(await this.messageStore.updateStatus(e,i?"completed":"failed",{processedBy:s,error:r}),A())try{await k().hookRegistry.emit({name:plugin.HookEvent.GatewayMessageComplete,data:{messageId:e,sessionId:t,processorId:s,status:i?"completed":"failed",error:r,responseMessageId:l,tokens:g,duration:u,timestamp:Date.now()}});}catch{this.log.debug("hook trigger skipped (no context)");}await this.router.completeMessage(t,e,s,i?"completed":"failed");}async onEvent(t){switch(this.log.debug("channel event",{type:t.type,channelId:t.channelId}),t.type){case "connected":this.connections++;break;case "disconnected":this.connections--;break;case "error":this.log.error("channel error",{channelId:t.channelId,data:t.data});break}}async registerChannel(t,e){let s=e??{enabled:true,platform:{},session:{autoCreate:true,resetPolicy:"onNewConversation"},routing:{broadcast:true,dispatchToSubAgent:false}};await this.pluginLoader.register(t,s),this.running&&await this.pluginLoader.start(t.id);}async unregisterChannel(t){await this.pluginLoader.unregister(t);}async subscribe(t,e){await this.router.subscribe(t,e),await this.sessionManager.addSubscriber(t,e.id);}async unsubscribe(t){await this.router.unsubscribe(t);}async send(t){await this.onMessage(t);}async sendToChannel(t,e,s={}){let{proactive:n=false,replyToMessageId:i}=s;if(A())try{await k().hookRegistry.emit({name:plugin.HookEvent.GatewayMessageSend,data:{messageId:e.id,sessionId:e.sessionId,type:e.type,channel:e.metadata.channel,proactive:n,replyToMessageId:i,timestamp:Date.now()}});}catch{this.log.debug("hook trigger skipped (no context)");}let r=this.pluginLoader.getPlugin(t);if(!r)throw new Error(`Plugin not found: ${t}`);return r.send(e)}async sendProactiveMessage(t,e,s,n){let i={id:`msg_${Date.now()}_${Math.random().toString(36).slice(2,9)}`,sessionId:e,type:"output",content:s,metadata:{channel:n},timestamp:Date.now()};return this.sendToChannel(t,i,{proactive:true})}getSessionManager(){return this.sessionManager}getRouter(){return this.router}getPluginLoader(){return this.pluginLoader}getMessageStore(){return this.messageStore}getContactStore(){return this.contactStore}async getMessageHistory(t,e){return this.messageStore.getSessionHistory(t,e)}};
|
|
5
|
-
exports.$=Vt;exports.A=Dt;exports.B=Lt;exports.C=Ot;exports.D=Ut;exports.E=Ft;exports.F=Wt;exports.G=Bt;exports.H=Ht;exports.I=zt;exports.J=$t;exports.K=qt;exports.L=Ve;exports.M=Gs;exports.N=Ds;exports.O=A;exports.P=k;exports.Q=Te;exports.R=te;exports.S=se;exports.T=y;exports.U=it;exports.V=Pe;exports.W=xe;exports.X=Ae;exports.Y=Re;exports.Z=jt;exports._=Kt;exports.a=oe;exports.aa=Jt;exports.b=ae;exports.ba=Se;exports.c=ce;exports.ca=W;exports.d=le;exports.da=B;exports.e=ge;exports.ea=$;exports.f=de;exports.fa=q;exports.g=ue;exports.ga=_;exports.h=We;exports.ha=m;exports.i=Be;exports.ia=K;exports.j=He;exports.ja=V;exports.k=ze;exports.ka=J;exports.l=$e;exports.la=Q;exports.m=he;exports.ma=X;exports.n=pe;exports.na=Y;exports.o=fe;exports.oa=Z;exports.p=me;exports.pa=ee;exports.q=ye;exports.qa=as;exports.r=be;exports.ra=Fe;exports.s=re;exports.sa=so;exports.t=ke;exports.ta=st;exports.u=dt;exports.v=qe;exports.w=je;exports.x=Ke;exports.y=Nt;exports.z=Gt;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export{fa as ChannelPluginLoader,ta as GatewayServer,ga as GatewayWebSocketServer}from'./chunk-GQFBXITJ.mjs';import'./chunk-OBW6CNOG.mjs';import'./chunk-HAMGVOQD.mjs';
|