@easbot/gateway 0.2.24 → 0.2.26
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-OBW6CNOG.mjs → chunk-7OFQ65NG.mjs} +6 -6
- package/dist/chunks/{chunk-RIWYHEQ6.cjs → chunk-AUPHFE34.cjs} +6 -6
- package/dist/chunks/chunk-BJV43DSB.mjs +14 -0
- package/dist/chunks/chunk-SOCQU5UV.cjs +14 -0
- package/dist/chunks/{global-DVGSJ3V3.mjs → global-5GMGC2RN.mjs} +1 -1
- package/dist/chunks/{global-WNXPJXAS.cjs → global-TEPBDNQM.cjs} +1 -1
- package/dist/chunks/log-JFEBRH2W.cjs +1 -0
- package/dist/chunks/log-MIT4H7S5.mjs +1 -0
- package/dist/chunks/server-UXFF63KL.cjs +1 -0
- package/dist/chunks/server-Y336TWLM.mjs +1 -0
- package/dist/index.cjs +2 -2
- package/dist/index.d.cts +30 -14
- package/dist/index.d.ts +30 -14
- 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/log-7WPRHZWH.cjs +0 -1
- package/dist/chunks/log-MKRLBH4R.mjs +0 -1
- package/dist/chunks/server-HGRZI7JT.mjs +0 -1
- package/dist/chunks/server-ZWJPPFTK.cjs +0 -1
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
'use strict';var chunkAUPHFE34_cjs=require('./chunk-AUPHFE34.cjs'),chunkGY3SWWW3_cjs=require('./chunk-GY3SWWW3.cjs'),a=require('zod'),_=require('path'),R=require('fs'),w=require('fs/promises'),utils=require('@easbot/utils'),ws=require('ws'),is=require('https'),rs=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 ___namespace=/*#__PURE__*/_interopNamespace(_);var R__namespace=/*#__PURE__*/_interopNamespace(R);var w__namespace=/*#__PURE__*/_interopNamespace(w);var is__default=/*#__PURE__*/_interopDefault(is);var rs__default=/*#__PURE__*/_interopDefault(rs);var se=class{constructor(t={}){chunkGY3SWWW3_cjs.a(this,"log",chunkAUPHFE34_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 ne=class{constructor(t={}){chunkGY3SWWW3_cjs.a(this,"log",chunkAUPHFE34_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 S(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 ot(c,t,e,s){return S(c,t,[{type:"text",text:e}],s)}function Ae(c){let t=[c.platform,c.channelId];return c.chatId&&t.push(c.chatId),c.userId&&t.push(c.userId),t.join("_")}function ke(){return {status:"active",messageCount:0,lastMessageAt:null}}function ie(c,t){if(!c||!t)throw new Error("platform and stableId are required");return `${c}:${t}`}function re(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 Te(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:ie(t,e),platform:t,stableId:e,aliases:{...n},profile:{...s},createdAt:r,lastSeenAt:r,metadata:i}}function Ee(c,t){return `sub_${c}_${t}`}var at=chunkAUPHFE34_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 ae=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)}),ce=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)}),le=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)}),ge=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)}),de=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)}),ue=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)}),he=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)}),ze=a__default.default.object({telegram:ae.optional(),slack:ce.optional(),feishu:le.optional(),wechat:ge.optional(),webchat:de.optional(),signal:ue.optional(),nostr:he.optional()}),$e=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")}),ct=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")}),lt=a__default.default.object({enabled:a__default.default.boolean().default(true),tokens:a__default.default.array(ct).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")}),gt=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")}),je=a__default.default.object({type:a__default.default.enum(["token","oauth2"]).default("token"),token:lt.optional(),oauth2:gt.optional()}),qe=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")}),dt=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()}),Ke=a__default.default.object({nodeId:a__default.default.string().describe("Current gateway node ID"),nodes:a__default.default.array(dt).default([]).describe("Other gateway nodes in the cluster"),sync:qe.optional()}),pe=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")}),fe=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)}),me=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)}),ye=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)}),be=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")}),Ce=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:$e.optional(),auth:je.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")}),ut=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")}),oe=a__default.default.object({server:Ce.optional(),channels:ze.optional(),cluster:Ke.optional(),connectionPool:pe.optional(),messageQueue:fe.optional(),circuitBreaker:me.optional(),agentRegistry:ye.optional(),session:be.optional(),defaults:ut.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 Re(c){let t=oe.safeParse(c);return t.success||at.warn("Invalid gateway config",{issues:t.error.issues}),t.success?t.data:oe.parse({})}function ht(c){return oe.parse(c)}var pt=chunkAUPHFE34_cjs.a.create({service:"gateway:interfaces"}),I;function Fs(c){I=c,pt.debug("Agent adapter registered",{hasSubAgentRunner:!!c.subAgentRunner});}function Ws(){return I}function k(){return I!==void 0}function T(){if(!I)throw new Error("Agent adapter not set. Call setAgentAdapter() before using Gateway.");return I}var ft={Path:{home:process.env.HOME||process.env.USERPROFILE||"",data:".easbot",cache:".easbot/cache",config:".easbot",state:".easbot/state",log:".easbot/log",bin:".easbot/bin"}},mt={directory:process.cwd(),worktree:process.cwd()};function L(){return I?I.global:ft}function _e(){return I?I.instance:mt}var P=chunkAUPHFE34_cjs.a.create({service:"gateway:config"}),Se={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}},Ct={maxConnectionsPerChannel:100,idleTimeout:3e5,reuseStrategy:"lru",healthCheckInterval:6e4,acquireTimeout:5e3,warmupCount:0},St={maxLength:1e4,concurrency:100,batchSize:10,batchTimeout:100,priorityLevels:4},vt={failureThreshold:5,successThreshold:3,timeout:3e4,halfOpenDuration:15e3},wt={heartbeatInterval:3e4,heartbeatTimeout:9e4,heartbeatCheckInterval:1e4,autoDeregisterTimeout:3e5,maxAgents:100,maxRegistrations:1e3},It={sessionTimeout:18e5,resumeGracePeriod:3e5,maxSessions:1e4},Pt={enabled:true,botToken:"",maxConnections:100,timeout:3e4},xt={enabled:true,botToken:"",maxConnections:100,timeout:3e4},Mt={enabled:true,appId:"",appSecret:"",maxConnections:100,timeout:3e4},At={enabled:true,mode:"official",appId:"",appSecret:"",maxConnections:100,timeout:3e4},kt={enabled:true,path:"/ws",heartbeatInterval:3e4,connectionTimeout:6e4,maxConnections:1e3,cors:true},Tt={enabled:true,serviceUrl:"https://chat.signal.org",phoneNumber:"",maxConnections:100,timeout:3e4},Et={enabled:true,relays:[],maxConnections:100,timeout:3e4},O,F=null;async function Ye(c){try{if(!R.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 U(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 Rt(){let c=L(),t=_e(),e=[],s=___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 _t(c){let t=["gateway.json"],e={};for(let s of t){let n=___namespace.default.join(c,s),i=await Ye(n);if(i)if("gateway"in i&&i.gateway){let r=i.gateway;"server"in r||"channels"in r||"cluster"in r?e=U(e,r):e=U(e,{server:r}),P.debug("Loaded gateway config from file (nested format)",{file:n});}else "gateway"in i||(e=U(e,i),P.debug("Loaded gateway config from file (direct format)",{file:n}));}return e}async function Nt(c){let t=["easbot.json"];for(let e of t){let s=___namespace.default.join(c,e),n=await Ye(s);if(n?.gateway)return P.debug("Loaded gateway config from easbot.json",{file:s}),n.gateway}return {}}function Gt(){return O||(O=async()=>{let c=await Rt(),t={};for(let s of c){let n=await _t(s);t=U(t,n);let i=await Nt(s);t=U(t,i);}let e=Re(t);return F=e,P.info("Gateway config loaded",{directories:c.length,port:e.server?.port,logLevel:e.logLevel}),{config:e,directories:c}}),O}async function v(){return Gt()()}async function Ve(c){return c&&(O=void 0,F=null),(await v()).config}function Je(){return F}function Qe(){return F!==null}function Dt(){O=void 0,F=null,P.debug("Config cache cleared");}function Lt(){return _e().directory}async function Ot(){let{config:c}=await v();return Ce.parse({...Se,...c.server})}async function Ut(c){let{config:t}=await v(),e=t.channels?.[c],n={telegram:ae.parse(Pt),slack:ce.parse(xt),feishu:le.parse(Mt),wechat:ge.parse(At),webchat:de.parse(kt),signal:ue.parse(Tt),nostr:he.parse(Et)}[c];if(!n)throw new Error(`Unknown channel type: ${c}`);return {...n,...e}}async function Ft(){let{config:c}=await v();return pe.parse({...Ct,...c.connectionPool})}async function Wt(){let{config:c}=await v();return fe.parse({...St,...c.messageQueue})}async function Bt(){let{config:c}=await v();return me.parse({...vt,...c.circuitBreaker})}async function Ht(){let{config:c}=await v();return ye.parse({...wt,...c.agentRegistry})}async function zt(){let{config:c}=await v();return be.parse({...It,...c.session})}async function $t(){let{config:c}=await v();return c.server?.https}async function jt(){let{config:c}=await v();return c.server?.auth}async function qt(){let{config:c}=await v();return c.cluster}async function Kt(){let{config:c}=await v();return c.defaultAgent}async function Xe(){let{config:c}=await v();return c.logLevel??"INFO"}var Ge=Se,W={maxConnections:1e3,connectionTimeout:6e4,sessionExpireMs:3e5};var Vt={type:"token",enabled:true,tokens:[],onInvalid:"reject",allowAnonymous:false,defaultPermissions:[]};var Jt={heartbeatInterval:3e4,heartbeatTimeout:9e4,heartbeatCheckInterval:1e4,autoDeregisterTimeout:3e5,maxAgents:100,maxRegistrations:1e3};var Qt={mode:"both",interval:3e4,pushEvents:["register","deregister","status_change"],conflictResolution:"latest",remoteNodes:[]};var Xt={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 E(c,t,e,s=true){return new x(e,{code:t,recoverable:s,details:{channelId:c}})}var ve=class{constructor(){chunkGY3SWWW3_cjs.a(this,"log",chunkAUPHFE34_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 se,this.retryManager=new ne,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(d){this.log.warn("failed to send message to subscriber",{subscriberId:g.id,error:String(d)}),i.push(d instanceof Error?d:new Error(String(d))),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=Ee(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 B=class{constructor(t={}){chunkGY3SWWW3_cjs.a(this,"log",chunkAUPHFE34_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",[]);chunkGY3SWWW3_cjs.a(this,"storagePath");chunkGY3SWWW3_cjs.a(this,"dirty",false);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.storagePath=this.config.storagePath,this.ensureDir(this.storagePath),this.startCleanupTimer();}async ensureDir(t){try{await w__namespace.mkdir(t,{recursive:!0});}catch(e){e.code!=="EEXIST"&&this.log.warn("failed to create storage directory",{dirPath:t,error:String(e)});}}getMessageFilePath(t){return ___namespace.join(this.storagePath,`${this.sanitizeFileName(t)}.json`)}getSessionDir(t){return ___namespace.join(this.storagePath,"sessions",this.sanitizeFileName(t))}sanitizeFileName(t){return t.replace(/[^a-zA-Z0-9_-]/g,"_")}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.dirty=true,await this.saveMessageToFile(e),this.log.debug("message stored",{id:t.id,sessionId:t.sessionId,type:t.type,total:this.messages.size}),e}async saveMessageToFile(t){try{let e=this.getSessionDir(t.message.sessionId);await this.ensureDir(e);let s=___namespace.join(e,`${this.sanitizeFileName(t.message.id)}.json`);await w__namespace.writeFile(s,JSON.stringify(t,null,2),"utf-8");}catch(e){this.log.warn("failed to save message to file",{messageId:t.message.id,error:String(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.dirty=true,await this.saveMessageToFile(n),this.log.debug("message status updated",{id:t,status:e,processedBy:s.processedBy});}async incrementRetry(t){let e=this.messages.get(t);return e?(e.retryCount++,e.updatedAt=Date.now(),this.dirty=true,await this.saveMessageToFile(e),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 H=class{constructor(t={}){chunkGY3SWWW3_cjs.a(this,"log",chunkAUPHFE34_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(),await this.save();}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(),await this.save();}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 R__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(!R__namespace.existsSync(t)){this.log.debug("no saved sessions found");return}let e=await R__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 ___namespace.join(this.config.storagePath,"sessions.json")}async ensureStorageDir(){R__namespace.existsSync(this.config.storagePath)||await R__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 Yt="./data/gateway",Zt="contacts",es="index.json",z=class{constructor(t={}){chunkGY3SWWW3_cjs.a(this,"log",chunkAUPHFE34_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??Yt,this.contactDir=___namespace.join(this.baseDir,Zt),this.indexPath=___namespace.join(this.contactDir,es),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=re(t);if(e){let s=this.getFilePath(e.platform,e.stableId);this.indexFileMap[t]=s;}}}getFilePath(t,e){let s=this.sanitizeFilename(e);return ___namespace.join(this.contactDir,t,`${s}.json`)}sanitizeFilename(t){return t.replace(/[/\\:*?"<>|]/g,"_")}getPlatformDir(t){return ___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(___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=Te(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=re(t);return e?this.findByPlatformId(e.platform,e.stableId):null}async findByPlatformId(t,e){let s=ie(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 d=await this.findByUid(g);d&&l.push(d);}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),d=g.contacts;if(t.username){let m=t.username.toLowerCase();d=d.filter(y=>y.profile.username?.toLowerCase().includes(m));}if(t.email){let m=t.email.toLowerCase();d=d.filter(y=>y.profile.email?.toLowerCase().includes(m));}if(t.phone){let m=t.phone.replace(/\s/g,"");d=d.filter(y=>y.profile.phone?.replace(/\s/g,"").includes(m));}return {contacts:d,total:g.total,hasMore:g.hasMore}}let e=[];for(let g of Object.keys(this.index.byPlatform)){let d=await this.listByPlatform(g,1e3,0);e.push(...d.contacts);}let s=e;if(t.username){let g=t.username.toLowerCase();s=s.filter(d=>d.profile.username?.toLowerCase().includes(g));}if(t.email){let g=t.email.toLowerCase();s=s.filter(d=>d.profile.email?.toLowerCase().includes(g));}if(t.phone){let g=t.phone.replace(/\s/g,"");s=s.filter(d=>d.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 De=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}},$=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 De(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 j=class{constructor(t={}){chunkGY3SWWW3_cjs.a(this,"log",chunkAUPHFE34_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 H({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=Ae(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:ke(),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",chunkAUPHFE34_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";}},K=chunkAUPHFE34_cjs.a.create({service:"gateway:channel-selection"});function G(c){if(!(!c||typeof c!="string"))return c.toLowerCase().trim()}async function Oe(c){let{channel:t,sessionId:e,userId:s,defaultChannel:n,fallbackChannels:i,sessionManager:r,pluginLoader:l,contactStore:g}=c,d=G(t);if(d){let p=Le(d,l);if(p){if(s&&g){let C=await ts(d,s,g,l);if(C)return C}return p}if(e){let C=tt(e,r);if(C)return K.debug("explicit channel unavailable, using session-bound channel as fallback",{explicitChannel:d,sessionChannel:C.channel}),C}throw new M(`Channel \u4E0D\u53EF\u7528\u6216\u672A\u914D\u7F6E: ${d}`,"CHANNEL_UNAVAILABLE",[])}if(e){let p=tt(e,r);if(p)return p}if(s&&g){let p=await ss(s,g,l);if(p)return p}let m=Ue(l);if(m.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 y=G(n);if(y){let p=Le(y,l);if(p)return K.debug("using default channel",{channel:y}),{...p,source:"default"};K.warn("default channel unavailable",{channel:y});}if(m.length===1){let p=m[0],C=l?.getPlugin(p);return {channel:p,source:"single-configured",channelInfo:{platform:C?.platform??"api",channelId:p}}}let A=(i??[]).map(G).filter(p=>p!==void 0);for(let p of A){let C=Le(p,l);if(C)return K.debug("using fallback channel",{channel:p}),{...C,source:"fallback"};K.debug("fallback channel unavailable, trying next",{channel:p});}throw y?new M(`\u914D\u7F6E\u7684\u9ED8\u8BA4 Channel \u4E0D\u53EF\u7528: ${y}`,"CHANNEL_UNAVAILABLE",m):new M(`\u9700\u8981\u660E\u786E\u6307\u5B9A Channel\uFF08\u591A\u4E2A Channel \u5DF2\u914D\u7F6E\uFF09: ${m.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",m)}function Le(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=G(c);if(!s)return null;let n=t.getAllPlugins();for(let i of n)if(t.isRunning(i.id)&&G(i.platform)===s)return {channel:i.id,source:"explicit",channelInfo:{platform:i.platform,channelId:i.id}};return null}function tt(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 ts(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 ss(c,t,e){let s=Ue(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 Ue(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 Fe(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 u=chunkAUPHFE34_cjs.a.create({service:"gateway:websocket-server"});function os(){return `ws_${Date.now()}_${Math.random().toString(36).substring(2,9)}`}function Pe(c,t){for(let[e,s]of t.entries())if(s===c)return e;return null}var N=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={...Ge,...t};}setChannelResolveDeps(t){this.channelResolveParams=t,u.debug("channel resolve deps set",{hasSessionManager:!!t.sessionManager,hasPluginLoader:!!t.pluginLoader,hasContactStore:!!t.contactStore});}setMessageSender(t){this.messageSender=t,u.debug("message sender set",{hasSender:!!t});}setPluginLoader(t){this.pluginLoaderRef=t;}getChannelResolveParams(){return this.channelResolveParams?this.channelResolveParams:{}}async start(){if(this.running){u.warn("WebSocket server is already running");return}u.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){u.error("Error processing WebSocket message",{error:r.message}),this.sendError(i,r.message);}},onClose:(n,i)=>{this.handleDisconnection(i);},onError:(n,i)=>{u.error("WebSocket error",{error:n.type});}},e=this.config.https?.enabled??false;u.debug("WebSocket server start: choosing server type",{https:e});try{e?(u.debug("WebSocket server start: starting HTTPS server"),await this.startHTTPSServer(t)):(u.debug("WebSocket server start: starting HTTP server"),await this.startHTTPServer(t)),u.debug("WebSocket server start: HTTP/HTTPS server started successfully");}catch(n){throw u.error("WebSocket server start: HTTP/HTTPS server failed",{error:n instanceof Error?n.message:String(n)}),n}u.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";u.info("WebSocket server started",{url:`${s}://${this.config.hostname}:${this.config.port}${this.config.path}`});}async startHTTPServer(t){u.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}),u.debug("HTTP server start: server created, checking raw server");let e=this.server.raw;e?(u.debug("HTTP server start: raw server available, setting up WebSocket upgrade"),this.setupWebSocketUpgrade(e,t),u.debug("HTTP server start: WebSocket upgrade configured")):u.warn("HTTP server start: raw server is undefined");}async startHTTPSServer(t){let e=this.config.https,s=this.loadSSLCertificates(e);if(!s){u.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=is__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,()=>{u.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:R__namespace.default.readFileSync(t.certPath),key:R__namespace.default.readFileSync(t.keyPath)}:null}catch(e){return u.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,g=this.config.path||"";(l===g||g===""&&(l==="/"||l===""))&&this.wss.handleUpgrade(s,n,i,d=>{e.onOpen?.({type:"open"},d),d.on("message",m=>{e.onMessage?.({data:m},d);}),d.on("close",(m,y)=>{e.onClose?.({code:m,reason:y},d);}),d.on("error",m=>{e.onError?.({type:"error",message:m.message},d);});});}catch{}});}startHTTPRedirectServer(t){this.httpRedirectServer=rs__default.default.createServer((e,s)=>{let i=`https://${e.headers.host?.split(":")[0]||this.config.hostname}:${this.config.port}${e.url}`;u.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,()=>{u.info("HTTP redirect server started",{port:t,hostname:this.config.hostname});});}async stop(){if(!this.running){u.warn("WebSocket server is not running");return}u.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){u.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,u.info("WebSocket server stopped");}broadcastToSession(t,e){this.touchSession(t);let s=this.sessionSubscriptions.get(t);if(!s||s.size===0){u.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)){u.debug("Event type not subscribed, skipping",{clientId:r,sessionId:t,eventType:e.type,subscribedTypes:Array.from(l.subscribedEventTypes)});continue}l.ws.send(i),u.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??W.maxConnections)){u.warn("Maximum connections reached, rejecting new connection"),t.close(1013,"Maximum connections reached");return}let e=os();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()}),u.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=Pe(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);}u.info("WebSocket client disconnected",{clientId:e,totalConnections:this.connections.size});}async handleMessage(t,e){let s=Pe(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=Pe(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),u.info("backend session bound on subscribe",{gatewaySessionId:i.sessionId,backendSessionId:i.backendSessionId});}catch(l){u.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);u.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=Pe(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)),u.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}u.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}u.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}u.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??W.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){u.info("Closing inactive connection",{clientId:n,lastActivityAt:i.lastActivityAt,timeoutMs:e});try{i.ws.close(1001,"Connection timeout");}catch(r){u.debug("Error closing timed out connection",{clientId:n,error:String(r)});}}}s.length>0&&u.debug("Closed timed out connections",{count:s.length,remainingConnections:this.connections.size-s.length});}cleanupExpiredSessions(){let t=Date.now(),e=[],s=this.config.sessionExpireMs??W.sessionExpireMs;for(let[n,i]of this.sessionLastActivity.entries())t-i>s&&e.push(n);for(let n of e)u.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&&u.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),u.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:"/channels",handler:async t=>{let e=this.channelResolveParams?.pluginLoader;if(!e)return t.json({error:"Plugin loader not available"},503);let n=e.getAllPlugins().map(i=>({id:i.id,platform:i.platform,name:i.name,version:i.version}));return t.json({channels:n,total:n.length})}},{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 u.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 u.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),n=t.req.query("platform"),i=t.req.query("channelId"),r=t.req.query("userId"),l=[];for(let[d,m]of this.sessionSubscriptions){let y=d.split("_"),A=y[0],p=y[1],C=y[y.length-1];(!n||A===n)&&(!i||p===i)&&(!r||C===r)&&l.push({id:d,platform:A,channelId:p,userId:C,state:"active",createdAt:this.sessionLastActivity.get(d)||Date.now(),lastActiveAt:this.sessionLastActivity.get(d)||Date.now()});}let g=l.slice(s,s+e);return t.json({sessions:g,total:l.length,hasMore:s+e<l.length})}},{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 u.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 Oe({...n,sessionId:e,channel:s.channel,userId:s.userId});u.debug("channel resolved for message",{sessionId:e,channel:i.channel,source:i.source,channelInfo:i.channelInfo});let r=`msg_${Date.now()}_${Math.random().toString(36).substring(2,9)}`,l={...i.channelInfo};s.userId&&!l.chatId&&(l.chatId=s.userId,l.userId=s.userId);let g={id:r,sessionId:e,type:"output",content:typeof s.content=="string"?[{type:"text",text:s.content}]:s.content||[],metadata:{channel:l,replyToMessageId:s.replyToMessageId},timestamp:Date.now()};if(this.server)await this.server.sendToChannel(i.channel,g,{replyToMessageId:s.replyToMessageId}),u.debug("message sent via server.sendToChannel",{messageId:r,channel:i.channel});else if(this.pluginLoaderRef)try{await this.pluginLoaderRef.send(i.channel,g),u.debug("message sent via pluginLoader",{messageId:r,channel:i.channel});}catch(m){return u.error("failed to send message",{sessionId:e,error:String(m)}),t.json({error:String(m)},500)}let d={messageId:r,sessionId:e,createdAt:Date.now(),metadata:{channel:i.channel,channelSource:i.source,channelInfo:i.channelInfo}};return t.status(202),t.json(d)}catch(n){return n instanceof M?(u.warn("channel selection failed",{sessionId:e,error:n.message}),t.json({error:Fe(n),code:n.code},400)):(u.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 b=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=chunkAUPHFE34_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 V=class extends b{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 E(this.id,"CHANNEL_INIT_FAILED","Config not set");if(this.botConfig=this.config.platform,!this.botConfig.botToken)throw E(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 E(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,d)=>this.sendApiMessage(e.chat.id.toString(),g,d)};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 S(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 d=parseInt(l.headers.get("Retry-After")??"1",10)*1e3;await this.sleep(d);continue}throw E(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 E(this.id,"CHANNEL_CONNECTION_FAILED",`Failed after ${this.retryState.maxAttempts+1} attempts: ${i?.message}`,true)}};var J=class extends b{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 S(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 Q=class extends b{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 S(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 X=class extends b{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 S(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 Y=class extends b{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 S(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 Z=class extends b{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 N(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=S(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 ee=class extends b{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 S(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 te=class extends b{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 S(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 Be=class extends b{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 ls(c){return new Be(c)}var He={telegram:V,discord:J,slack:Q,feishu:X,wechat:Y,webchat:Z,signal:ee,nostr:te};function ao(){return Object.keys(He)}function gs(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 rt=class{constructor(t){chunkGY3SWWW3_cjs.a(this,"log",chunkAUPHFE34_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 ve,this.log.debug("GatewayServer.constructor: creating GatewaySessionManager");let e=L();this.sessionManager=new j({storagePath:`${e.Path.data}/gateway/sessions`,enablePersistence:true}),this.log.debug("GatewayServer.constructor: creating ChannelPluginLoader"),this.pluginLoader=new q({healthCheckInterval:6e4}),this.log.debug("GatewayServer.constructor: pluginLoader created");let s=k();this.log.debug("GatewayServer.constructor: hasAgentAdapter",{hasAdapter:s}),this.messageStore=new B({storagePath:`${e.Path.data}/gateway/messages`}),s&&(this.log.debug("GatewayServer.constructor: adapter exists, setting message storage"),T().setMessageStorage({store:async i=>{let r=gs(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 v(),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=He[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(d){this.log.error("failed to register channel plugin",{platform:s,error:String(d)});}}}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=`${L().Path.data}/gateway`,s=new z({baseDir:e});this.contactStore=new $(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 N({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 v(),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(),await this.messageStore.close(),this.contactStore?.close&&await this.contactStore.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(!k()){this.log.debug("no agent adapter, message remains pending",{id:t.id});return}try{let s=T().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=t.metadata.channel,l=r?.platform||"unknown",g=r?.userId||r?.chatId||"unknown user",d=`${l}:${g}`,m=`## External Channel Message
|
|
5
|
+
- Platform: ${l}
|
|
6
|
+
- From: ${g} (contactUid: ${d})
|
|
7
|
+
- Channel: ${r?.channelId||"default"}
|
|
8
|
+
- Session: ${t.sessionId}
|
|
9
|
+
|
|
10
|
+
## Instructions
|
|
11
|
+
- Reply the message directly if it's a simple response.
|
|
12
|
+
- Use gateway_channel tool to proactively send messages: \`\`\`json
|
|
13
|
+
{"operation": "send", "contactUid": "${d}", "content": "Your message here"}
|
|
14
|
+
\`\`\``,y={source:t.sessionId,title:`[${l}] Message from ${g}`,agent:"gateway",sessionId:i,parts:t.content,system:m,channel:{platform:l,contactUid:d,channelId:r?.channelId,userId:g}},A=await s.run(y),p=A.metadata?.sessionId;if(p&&n&&n.backendSessionId!==p&&(await this.sessionManager.bindBackendSession(t.sessionId,p),this.log.info("backend session bound",{gatewaySessionId:t.sessionId,backendSessionId:p})),await this.triggerHook(plugin.HookEvent.GatewayMessageComplete,{messageId:t.id,sessionId:t.sessionId,backendSessionId:p||i,processorId:"subagent",status:"completed",timestamp:Date.now()}),A.output){let C=t.metadata.channel;C?.channelId&&await this.sendToChannel(C.channelId,{id:`msg_${Date.now()}_${Math.random().toString(36).slice(2,9)}`,sessionId:t.sessionId,type:"output",content:[{type:"text",text:A.output}],metadata:{channel:C},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(k())try{await T().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(k())try{await T().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}),k())try{await T().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:d}=n;if(await this.messageStore.updateStatus(e,i?"completed":"failed",{processedBy:s,error:r}),k())try{await T().hookRegistry.emit({name:plugin.HookEvent.GatewayMessageComplete,data:{messageId:e,sessionId:t,processorId:s,status:i?"completed":"failed",error:r,responseMessageId:l,tokens:g,duration:d,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(await this.messageStore.store(e),this.log.debug("message stored",{messageId:e.id,proactive:n}),i&&(await this.messageStore.updateStatus(i,"completed",{processedBy:"gateway"}),this.log.debug("original message status updated to completed",{replyToMessageId:i})),k())try{await T().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)}};exports.$=Qt;exports.A=Ot;exports.B=Ut;exports.C=Ft;exports.D=Wt;exports.E=Bt;exports.F=Ht;exports.G=zt;exports.H=$t;exports.I=jt;exports.J=qt;exports.K=Kt;exports.L=Xe;exports.M=Fs;exports.N=Ws;exports.O=k;exports.P=T;exports.Q=_e;exports.R=se;exports.S=ne;exports.T=S;exports.U=ot;exports.V=Ae;exports.W=ke;exports.X=Ee;exports.Y=Ge;exports.Z=Vt;exports._=Jt;exports.a=ae;exports.aa=Xt;exports.b=ce;exports.ba=ve;exports.c=le;exports.ca=B;exports.d=ge;exports.da=H;exports.e=de;exports.ea=j;exports.f=ue;exports.fa=q;exports.g=he;exports.ga=N;exports.h=ze;exports.ha=b;exports.i=$e;exports.ia=V;exports.j=je;exports.ja=J;exports.k=qe;exports.ka=Q;exports.l=Ke;exports.la=X;exports.m=pe;exports.ma=Y;exports.n=fe;exports.na=Z;exports.o=me;exports.oa=ee;exports.p=ye;exports.pa=te;exports.q=be;exports.qa=ls;exports.r=Ce;exports.ra=He;exports.s=oe;exports.sa=ao;exports.t=Re;exports.ta=rt;exports.u=ht;exports.v=Ve;exports.w=Je;exports.x=Qe;exports.y=Dt;exports.z=Lt;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import'./chunk-HAMGVOQD.mjs';import {mkdir,readFile,readdir,rm,writeFile}from'fs/promises';import i from'path';import {xdgData,xdgCache,xdgConfig,xdgState}from'xdg-basedir';import O from'os';var c={name:"@easbot/gateway",version:"0.2.
|
|
1
|
+
import'./chunk-HAMGVOQD.mjs';import {mkdir,readFile,readdir,rm,writeFile}from'fs/promises';import i from'path';import {xdgData,xdgCache,xdgConfig,xdgState}from'xdg-basedir';import O from'os';var c={name:"@easbot/gateway",version:"0.2.26",description:"EASBot Gateway - AI Agent Server and Multi-channel Integration Platform - \u652F\u6301 WebSocket\u3001HTTP\u3001Discord\u3001Telegram\u3001Slack \u7B49\u591A\u6E20\u9053\u96C6\u6210",type:"module",main:"dist/index.cjs",module:"dist/index.mjs",types:"dist/index.d.ts",exports:{".":{types:"./dist/index.d.ts",import:"./dist/index.mjs",require:"./dist/index.cjs"},"./package.json":"./package.json"},scripts:{dev:"cross-env NODE_ENV=development tsx src/cli.ts -- start",start:"cross-env NODE_ENV=production node dist/cli.mjs start",build:"tsup --env.NODE_ENV production",test:"vitest","test:run":"vitest run",lint:"biome check .","lint:fix":"biome check --write .","lint:report":"biome check --reporter=summary .",format:"biome format .","format:fix":"biome format --write .","type-check":"tsc --noEmit",clean:"npx rimraf dist node_modules",prepare:"echo norun",prepublishOnly:"pnpm build","publish:npm":"bash scripts/publish.sh","publish:npm:win":"powershell -ExecutionPolicy Bypass -File scripts/publish.ps1"},keywords:["easbot","gateway","server","websocket","http","agent","multi-channel","discord","telegram","slack","feishu","wechat","chat","integration"],author:"houjallen",license:"MIT",repository:{type:"git",url:"https://github.com/houjallen/easbot.git",directory:"packages/gateway"},homepage:"https://github.com/houjallen/easbot/tree/main/packages/gateway#readme",bugs:{url:"https://github.com/houjallen/easbot/issues"},files:["dist","README.md","README.en.md","LICENSE"],dependencies:{"@ai-sdk/anthropic":"^3.0.76","@ai-sdk/openai-compatible":"^2.0.47","@ai-sdk/provider":"^3.0.10","@ai-sdk/provider-utils":"^4.0.27","@easbot/plugin":"workspace:*","@easbot/sdk":"workspace:*","@easbot/types":"workspace:*","@easbot/utils":"workspace:*","@hono/node-server":"^2.0.2","@hono/node-ws":"^1.3.0","@hono/standard-validator":"^0.2.2","@hono/zod-validator":"^0.7.6",ai:"^6.0.176","better-sqlite3":"^12.9.0",commander:"^14.0.3",hono:"^4.12.18","hono-openapi":"^1.3.0","jieba-wasm":"^2.4.0",minimatch:"^10.2.5",undici:"^8.3.0",ws:"^8.20.0","xdg-basedir":"^5.1.0",zod:"^4.4.3"},devDependencies:{"@biomejs/biome":"^2.4.14","@types/better-sqlite3":"^7.6.13","@types/node":"^25.6.2","@types/ws":"^8.18.1","@vitest/coverage-v8":"^4.1.5",dotenv:"^17.4.2",tsup:"^8.5.1",typescript:"^6.0.3",vitest:"^4.1.5"},engines:{node:">=22.22.3"},publishConfig:{access:"public"}};var a="easbot",l=i.join(xdgData,a),k=i.join(xdgCache,a),x=i.join(xdgConfig,a),j=i.join(xdgState,a),m;(e=>{e.Path={get home(){return process.env.EASBOT_TEST_HOME||O.homedir()},data:l,bin:i.join(l,"bin"),log:i.join(l,"log"),cache:k,config:x,state:j};let t=null,s=null;async function v(){if(t)return t;if(process.env.EASBOT_VERSION)return t=process.env.EASBOT_VERSION,t;try{t=c.version||"local";}catch{t="local";}return t||"local"}function g(){if(t)return t;if(process.env.EASBOT_VERSION)return t=process.env.EASBOT_VERSION,t;try{t=c.version||"local";}catch{t="local";}return t||"local"}e.getVersion=g;function p(){if(s)return s;if(process.env.EASBOT_CHANNEL)return s=process.env.EASBOT_CHANNEL,s;let o=g();return o==="local"||o.includes("dev")?s="local":o.includes("beta")||o.includes("alpha")?s="preview":s="latest",s||"local"}e.getChannel=p;function B(){return p()==="local"}e.isLocal=B;function C(){return p()==="preview"}e.isPreview=C;let u=false,n=null;async function H(){if(!u)return n||(n=(async()=>{await v(),await Promise.all([mkdir(e.Path.data,{recursive:true}),mkdir(e.Path.config,{recursive:true}),mkdir(e.Path.state,{recursive:true}),mkdir(e.Path.log,{recursive:true}),mkdir(e.Path.bin,{recursive:true}),mkdir(e.Path.cache,{recursive:true})]).then(()=>{process.env.EASBOT_DATA_PATH=e.Path.data,process.env.EASBOT_CONFIG_PATH=e.Path.config,process.env.EASBOT_STATE_PATH=e.Path.state,process.env.EASBOT_LOG_PATH=e.Path.log,process.env.EASBOT_BIN_PATH=e.Path.bin,process.env.EASBOT_CACHE_PATH=e.Path.cache;});let o="1",d=i.join(e.Path.cache,"version");if(await readFile(d,"utf-8").catch(()=>"0")!==o){try{let h=await readdir(e.Path.cache);await Promise.all(h.map(b=>rm(i.join(e.Path.cache,b),{recursive:!0,force:!0})));}catch{}await writeFile(d,o);}u=true;})(),n)}e.init=H;})(m||(m={}));
|
|
2
2
|
export{m as Global};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
'use strict';require('./chunk-GY3SWWW3.cjs');var promises=require('fs/promises'),i=require('path'),xdgBasedir=require('xdg-basedir'),O=require('os');function _interopDefault(e){return e&&e.__esModule?e:{default:e}}var i__default=/*#__PURE__*/_interopDefault(i);var O__default=/*#__PURE__*/_interopDefault(O);var c={name:"@easbot/gateway",version:"0.2.
|
|
1
|
+
'use strict';require('./chunk-GY3SWWW3.cjs');var promises=require('fs/promises'),i=require('path'),xdgBasedir=require('xdg-basedir'),O=require('os');function _interopDefault(e){return e&&e.__esModule?e:{default:e}}var i__default=/*#__PURE__*/_interopDefault(i);var O__default=/*#__PURE__*/_interopDefault(O);var c={name:"@easbot/gateway",version:"0.2.26",description:"EASBot Gateway - AI Agent Server and Multi-channel Integration Platform - \u652F\u6301 WebSocket\u3001HTTP\u3001Discord\u3001Telegram\u3001Slack \u7B49\u591A\u6E20\u9053\u96C6\u6210",type:"module",main:"dist/index.cjs",module:"dist/index.mjs",types:"dist/index.d.ts",exports:{".":{types:"./dist/index.d.ts",import:"./dist/index.mjs",require:"./dist/index.cjs"},"./package.json":"./package.json"},scripts:{dev:"cross-env NODE_ENV=development tsx src/cli.ts -- start",start:"cross-env NODE_ENV=production node dist/cli.mjs start",build:"tsup --env.NODE_ENV production",test:"vitest","test:run":"vitest run",lint:"biome check .","lint:fix":"biome check --write .","lint:report":"biome check --reporter=summary .",format:"biome format .","format:fix":"biome format --write .","type-check":"tsc --noEmit",clean:"npx rimraf dist node_modules",prepare:"echo norun",prepublishOnly:"pnpm build","publish:npm":"bash scripts/publish.sh","publish:npm:win":"powershell -ExecutionPolicy Bypass -File scripts/publish.ps1"},keywords:["easbot","gateway","server","websocket","http","agent","multi-channel","discord","telegram","slack","feishu","wechat","chat","integration"],author:"houjallen",license:"MIT",repository:{type:"git",url:"https://github.com/houjallen/easbot.git",directory:"packages/gateway"},homepage:"https://github.com/houjallen/easbot/tree/main/packages/gateway#readme",bugs:{url:"https://github.com/houjallen/easbot/issues"},files:["dist","README.md","README.en.md","LICENSE"],dependencies:{"@ai-sdk/anthropic":"^3.0.76","@ai-sdk/openai-compatible":"^2.0.47","@ai-sdk/provider":"^3.0.10","@ai-sdk/provider-utils":"^4.0.27","@easbot/plugin":"workspace:*","@easbot/sdk":"workspace:*","@easbot/types":"workspace:*","@easbot/utils":"workspace:*","@hono/node-server":"^2.0.2","@hono/node-ws":"^1.3.0","@hono/standard-validator":"^0.2.2","@hono/zod-validator":"^0.7.6",ai:"^6.0.176","better-sqlite3":"^12.9.0",commander:"^14.0.3",hono:"^4.12.18","hono-openapi":"^1.3.0","jieba-wasm":"^2.4.0",minimatch:"^10.2.5",undici:"^8.3.0",ws:"^8.20.0","xdg-basedir":"^5.1.0",zod:"^4.4.3"},devDependencies:{"@biomejs/biome":"^2.4.14","@types/better-sqlite3":"^7.6.13","@types/node":"^25.6.2","@types/ws":"^8.18.1","@vitest/coverage-v8":"^4.1.5",dotenv:"^17.4.2",tsup:"^8.5.1",typescript:"^6.0.3",vitest:"^4.1.5"},engines:{node:">=22.22.3"},publishConfig:{access:"public"}};var a="easbot",l=i__default.default.join(xdgBasedir.xdgData,a),k=i__default.default.join(xdgBasedir.xdgCache,a),x=i__default.default.join(xdgBasedir.xdgConfig,a),j=i__default.default.join(xdgBasedir.xdgState,a);exports.Global=void 0;(e=>{e.Path={get home(){return process.env.EASBOT_TEST_HOME||O__default.default.homedir()},data:l,bin:i__default.default.join(l,"bin"),log:i__default.default.join(l,"log"),cache:k,config:x,state:j};let t=null,s=null;async function v(){if(t)return t;if(process.env.EASBOT_VERSION)return t=process.env.EASBOT_VERSION,t;try{t=c.version||"local";}catch{t="local";}return t||"local"}function g(){if(t)return t;if(process.env.EASBOT_VERSION)return t=process.env.EASBOT_VERSION,t;try{t=c.version||"local";}catch{t="local";}return t||"local"}e.getVersion=g;function p(){if(s)return s;if(process.env.EASBOT_CHANNEL)return s=process.env.EASBOT_CHANNEL,s;let o=g();return o==="local"||o.includes("dev")?s="local":o.includes("beta")||o.includes("alpha")?s="preview":s="latest",s||"local"}e.getChannel=p;function B(){return p()==="local"}e.isLocal=B;function C(){return p()==="preview"}e.isPreview=C;let u=false,n=null;async function H(){if(!u)return n||(n=(async()=>{await v(),await Promise.all([promises.mkdir(e.Path.data,{recursive:true}),promises.mkdir(e.Path.config,{recursive:true}),promises.mkdir(e.Path.state,{recursive:true}),promises.mkdir(e.Path.log,{recursive:true}),promises.mkdir(e.Path.bin,{recursive:true}),promises.mkdir(e.Path.cache,{recursive:true})]).then(()=>{process.env.EASBOT_DATA_PATH=e.Path.data,process.env.EASBOT_CONFIG_PATH=e.Path.config,process.env.EASBOT_STATE_PATH=e.Path.state,process.env.EASBOT_LOG_PATH=e.Path.log,process.env.EASBOT_BIN_PATH=e.Path.bin,process.env.EASBOT_CACHE_PATH=e.Path.cache;});let o="1",d=i__default.default.join(e.Path.cache,"version");if(await promises.readFile(d,"utf-8").catch(()=>"0")!==o){try{let h=await promises.readdir(e.Path.cache);await Promise.all(h.map(b=>promises.rm(i__default.default.join(e.Path.cache,b),{recursive:!0,force:!0})));}catch{}await promises.writeFile(d,o);}u=true;})(),n)}e.init=H;})(exports.Global||(exports.Global={}));
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
'use strict';var chunkAUPHFE34_cjs=require('./chunk-AUPHFE34.cjs');require('./chunk-GY3SWWW3.cjs');Object.defineProperty(exports,"Log",{enumerable:true,get:function(){return chunkAUPHFE34_cjs.a}});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export{a as Log}from'./chunk-7OFQ65NG.mjs';import'./chunk-HAMGVOQD.mjs';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
'use strict';var chunkSOCQU5UV_cjs=require('./chunk-SOCQU5UV.cjs');require('./chunk-AUPHFE34.cjs'),require('./chunk-GY3SWWW3.cjs');Object.defineProperty(exports,"ChannelPluginLoader",{enumerable:true,get:function(){return chunkSOCQU5UV_cjs.fa}});Object.defineProperty(exports,"GatewayServer",{enumerable:true,get:function(){return chunkSOCQU5UV_cjs.ta}});Object.defineProperty(exports,"GatewayWebSocketServer",{enumerable:true,get:function(){return chunkSOCQU5UV_cjs.ga}});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export{fa as ChannelPluginLoader,ta as GatewayServer,ga as GatewayWebSocketServer}from'./chunk-BJV43DSB.mjs';import'./chunk-7OFQ65NG.mjs';import'./chunk-HAMGVOQD.mjs';
|