@toolpack-sdk/agents 2.0.0-alpha.1 → 2.1.0

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.
@@ -1,3 +1,3 @@
1
- var d=class{name;_handler;onMessage(n){this._handler=n}async handleMessage(n){this._handler&&await this._handler(n)}};import{createHmac as P,timingSafeEqual as E}from"crypto";var C=class extends d{isTriggerChannel=!1;config;server;participantCache=new Map;botUserId;allowedChannels;constructor(n){super(),this.config={port:3e3,...n},this.name=n.name;let e=n.channel;this.allowedChannels=e==null?null:Array.isArray(e)?e:[e]}listen(){typeof process<"u"&&import("http").then(n=>{this.server=n.createServer((e,t)=>{this.handleRequest(e,t)}),this.server.listen(this.config.port,()=>{console.log(`[SlackChannel] Listening on port ${this.config.port}`),this.runStartupCheck().catch(()=>{})})}).catch(n=>{console.error("[SlackChannel] Failed to start HTTP server:",n)})}async runStartupCheck(){try{let e=await(await fetch("https://slack.com/api/auth.test",{method:"POST",headers:{Authorization:`Bearer ${this.config.token}`,"Content-Type":"application/json"}})).json();e.ok?(this.botUserId=e.user_id,console.log(`[SlackChannel] Connected as @${e.user} (${e.user_id}) in workspace "${e.team}" \u2014 ${e.url}`)):console.warn(`[SlackChannel] auth.test failed: ${e.error}. Check your bot token.`)}catch(n){console.warn("[SlackChannel] Startup self-check failed (network error):",n)}}verifySignature(n,e){let t=n["x-slack-request-timestamp"],o=n["x-slack-signature"];if(!t||!o||Array.isArray(t)||Array.isArray(o))return!1;let r=parseInt(t,10),i=Math.floor(Date.now()/1e3);if(isNaN(r)||Math.abs(i-r)>300)return!1;let s=`v0:${t}:${e}`,l=`v0=${P("sha256",this.config.signingSecret).update(s).digest("hex")}`;if(l.length!==o.length)return!1;try{return E(Buffer.from(l),Buffer.from(o))}catch{return!1}}async send(n){let e=n.metadata?.threadTs??n.metadata?.thread_ts??n.metadata?.threadId,o=n.metadata?.channelId??(this.allowedChannels&&this.allowedChannels.length>0?this.allowedChannels[0]:void 0);if(!o)throw new Error("[SlackChannel] Cannot send: no channel configured and metadata.channelId is missing. Provide a target via SlackChannelConfig.channel or output.metadata.channelId.");let r={channel:o,text:n.output};e&&(r.thread_ts=e);let i=await fetch("https://slack.com/api/chat.postMessage",{method:"POST",headers:{Authorization:`Bearer ${this.config.token}`,"Content-Type":"application/json"},body:JSON.stringify(r)});if(!i.ok)throw new Error(`Failed to send Slack message: ${i.statusText}`);let s=await i.json();if(!s.ok)throw new Error(`Slack API error: ${s.error}`)}normalize(n){let e=n,t=e.text||"",o=e.ts,r=e.thread_ts,i=r!==void 0&&r!==o,s=e.user,a=s?{kind:"user",id:s}:void 0,l=/<@([A-Z0-9]+)>/g,f=[],u;for(;(u=l.exec(t))!==null;)f.push(u[1]);let p=e.channel;return{message:t,conversationId:i?r:p||o||"",data:e,participant:a,context:{user:s,channel:p,team:e.team,channelType:e.channel_type,threadId:i?r:void 0,mentions:f.length>0?f:void 0,channelId:p,channelName:typeof this.config.channel=="string"?this.config.channel:p}}}async resolveParticipant(n){let e=n.participant?.id??n.context?.user;if(!e)return;let t=this.participantCache.get(e);if(t)return t;try{let o=await fetch(`https://slack.com/api/users.info?user=${encodeURIComponent(e)}`,{method:"GET",headers:{Authorization:`Bearer ${this.config.token}`}});if(!o.ok)return{kind:"user",id:e};let r=await o.json();if(!r.ok||!r.user){let a={kind:"user",id:e};return this.participantCache.set(e,a),a}let i=r.user.profile?.display_name||r.user.profile?.real_name||r.user.real_name||r.user.name||e,s={kind:"user",id:e,displayName:i,metadata:{slackUser:r.user}};return this.participantCache.set(e,s),s}catch{return{kind:"user",id:e}}}invalidateParticipant(n){this.participantCache.delete(n)}shouldProcessEvent(n){let e=n.type;if(e!=="message"&&e!=="app_mention")return!1;if(this.allowedChannels!==null){let i=n.channel_type;if(!(i==="im"||i==="mpim")){let a=n.channel;if(!a||!this.allowedChannels.includes(a))return!1}}let t=n.user;if(this.botUserId&&t===this.botUserId)return!1;let o=n.bot_id;if(!o)return!0;let r=this.config.blockedBotIds??[];if(r.includes(o)||t!==void 0&&r.includes(t))return!1;if(this.config.allowedBotIds!==void 0){let i=this.config.allowedBotIds;return i.includes(o)||t!==void 0&&i.includes(t)}return!0}handleRequest(n,e){if(n.method!=="POST"){e.writeHead(405),e.end("Method not allowed");return}let t="";n.on("data",o=>{t+=o.toString()}),n.on("end",()=>{if(!this.verifySignature(n.headers,t)){e.writeHead(401),e.end("Invalid signature");return}try{let o=JSON.parse(t);if(o.type==="url_verification"){e.writeHead(200,{"Content-Type":"application/json"}),e.end(JSON.stringify({challenge:o.challenge}));return}if(o.type==="event_callback"&&o.event){let r=o.event;if(this.shouldProcessEvent(r)){let i=this.normalize(r);this.handleMessage(i)}else r.type==="user_change"&&r.user&&this.invalidateParticipant(r.user.id);e.writeHead(200),e.end("OK");return}e.writeHead(200),e.end("OK")}catch(o){console.error("[SlackChannel] Error handling request:",o),e.writeHead(400),e.end("Bad request")}})}async stop(){if(this.server)return new Promise(n=>{this.server.close(n)})}};var y=class extends d{isTriggerChannel=!1;config;server;pendingResponses=new Map;constructor(n){super(),this.name=n.name,this.config={port:n.port??3e3,path:n.path??"/webhook"}}listen(){typeof process<"u"&&import("http").then(n=>{this.server=n.createServer((e,t)=>{this.handleRequest(e,t)}),this.server.listen(this.config.port,()=>{console.log(`[WebhookChannel] Listening on port ${this.config.port}${this.config.path}`)})}).catch(n=>{console.error("[WebhookChannel] Failed to start HTTP server:",n)})}async send(n){let e=n.metadata?.conversationId;if(e&&this.pendingResponses.has(e)){let t=this.pendingResponses.get(e);this.pendingResponses.delete(e),t.resolve({output:n.output,metadata:n.metadata})}}normalize(n){let e=n,t=e.headers||{},o=t["x-session-id"]||t["X-Session-Id"]||e.sessionId||e.conversationId||this.generateSessionId();return{message:e.message||e.text||"",intent:e.intent,conversationId:o,data:e,context:{headers:e.headers,method:e.method,sessionId:o}}}handleRequest(n,e){if(n.url!==this.config.path){e.writeHead(404),e.end("Not found");return}if(n.method!=="POST"){e.writeHead(405),e.end("Method not allowed");return}let t="";n.on("data",o=>{t+=o.toString()}),n.on("end",()=>{try{let o=JSON.parse(t),r=this.normalize(o),i=r.conversationId||this.generateSessionId(),s=new Promise((a,l)=>{this.pendingResponses.set(i,{resolve:a,reject:l}),setTimeout(()=>{this.pendingResponses.has(i)&&(this.pendingResponses.delete(i),l(new Error("Agent response timeout")))},3e4)});this.handleMessage({...r,conversationId:i,context:{...r.context,sessionId:i}}),s.then(a=>{e.writeHead(200,{"Content-Type":"application/json"}),e.end(JSON.stringify(a))}).catch(a=>{e.writeHead(500,{"Content-Type":"application/json"}),e.end(JSON.stringify({error:a.message}))})}catch(o){console.error("[WebhookChannel] Error handling request:",o),e.writeHead(400,{"Content-Type":"application/json"}),e.end(JSON.stringify({error:"Bad request"}))}})}generateSessionId(){return`webhook-${Date.now()}-${Math.random().toString(36).substring(2,9)}`}async stop(){if(this.server)return new Promise(n=>{this.server.close(n)})}};import{CronExpressionParser as x}from"cron-parser";var k=class extends d{isTriggerChannel=!0;config;timer;constructor(n){super(),this.config=n,this.name=n.name;try{x.parse(n.cron)}catch(e){throw new Error(`Invalid cron expression '${n.cron}': ${e.message}`)}}listen(){this.scheduleNextRun()}async send(n){let e=this.config.notify.indexOf(":");if(e===-1)throw new Error(`Invalid notify format: ${this.config.notify}. Expected format: 'webhook:https://...'`);let t=this.config.notify.substring(0,e),o=this.config.notify.substring(e+1);if(!t||!o)throw new Error(`Invalid notify format: ${this.config.notify}. Expected format: 'webhook:https://...'`);switch(t.toLowerCase()){case"webhook":await this.sendToWebhook(o,n);break;case"slack":throw new Error("ScheduledChannel no longer supports the 'slack:' notify protocol. Attach a named SlackChannel to the agent and route from inside run() via this.sendTo('<channelName>', output). See ScheduledChannelConfig.notify docs.");default:throw new Error(`Unknown notify protocol: ${t}`)}}normalize(n){let e=new Date,t=`${e.getFullYear()}-${e.getMonth()+1}-${e.getDate()}`;return{intent:this.config.intent,message:`Scheduled task triggered at ${e.toISOString()}`,conversationId:`scheduled:${this.name||"default"}:${t}`,data:{scheduled:!0,cron:this.config.cron,timestamp:e.toISOString()}}}async sendToWebhook(n,e){let t=await fetch(n,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({output:e.output,metadata:e.metadata,timestamp:new Date().toISOString()})});if(!t.ok)throw new Error(`Webhook notification failed: ${t.statusText}`)}getNextRunTime(){return x.parse(this.config.cron,{currentDate:new Date}).next().toDate()}scheduleNextRun(){let n=this.getNextRunTime(),e=n.getTime()-Date.now();if(e<=0){this.scheduleNextRun();return}console.log(`[ScheduledChannel] Next run scheduled for ${n.toISOString()}`),this.timer=setTimeout(()=>{this.trigger(),this.scheduleNextRun()},e)}async trigger(){let n=this.normalize(null);try{await this.handleMessage(n)}catch(e){console.error("[ScheduledChannel] Error triggering scheduled task:",e)}}async stop(){this.timer&&(clearTimeout(this.timer),this.timer=void 0)}};var v=class extends d{isTriggerChannel=!1;config;offset=0;pollingInterval;server;botUserId;botUsername;constructor(n){super(),this.name=n.name,this.config=n}listen(){this.runStartupCheck().catch(()=>{}),this.config.webhookUrl?this.startWebhook():this.startPolling()}async runStartupCheck(){try{let e=await(await fetch(`https://api.telegram.org/bot${this.config.token}/getMe`)).json();if(e.ok&&e.result){let t=e.result;this.botUserId=t.id!=null?String(t.id):void 0,this.botUsername=t.username,console.log(`[TelegramChannel] Connected as @${t.username} (id: ${t.id}, name: ${t.first_name})`)}else console.warn(`[TelegramChannel] getMe failed: ${e.description??"unknown error"}. Check your bot token.`)}catch(n){console.warn("[TelegramChannel] Startup self-check failed (network error):",n)}}async send(n){let e=n.metadata?.chatId;if(!e)throw new Error("Telegram send requires chatId in metadata");let t=await fetch(`https://api.telegram.org/bot${this.config.token}/sendMessage`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({chat_id:e,text:n.output,parse_mode:"Markdown"})});if(!t.ok)throw new Error(`Failed to send Telegram message: ${t.statusText}`);let o=await t.json();if(!o.ok)throw new Error(`Telegram API error: ${o.description}`)}normalize(n){let e=n,t=e.message||e.edited_message||{},o=t.text||"",r=t.chat||{},i=t.from||{},s=i.id!=null?String(i.id):void 0,a=i.first_name||i.username||s,l=s?{kind:"user",id:s,displayName:a??s}:void 0,f=t.entities??[],u=[];for(let w of f)if(w.type==="text_mention"&&w.user){let T=w.user;T.id!=null&&u.push(String(T.id))}let p=r.type,m=r.id!=null?String(r.id):"";return{message:o,conversationId:m,data:e,participant:l,context:{chatId:r.id,userId:i.id,username:i.username,firstName:i.first_name,lastName:i.last_name,messageId:t.message_id,channelType:p,channelId:m,channelName:r.title,mentions:u.length>0?u:void 0}}}startPolling(){console.log("[TelegramChannel] Starting polling mode"),this.pollingInterval=setInterval(async()=>{try{await this.pollUpdates()}catch(n){console.error("[TelegramChannel] Polling error:",n)}},5e3)}async pollUpdates(){let n=`https://api.telegram.org/bot${this.config.token}/getUpdates?offset=${this.offset}&limit=100`,e=await fetch(n);if(!e.ok)throw new Error(`Telegram getUpdates failed: ${e.statusText}`);let t=await e.json();if(!t.ok)throw new Error("Telegram getUpdates returned not ok");for(let o of t.result){let r=o.update_id;r>=this.offset&&(this.offset=r+1);try{let i=this.normalize(o);await this.handleMessage(i)}catch(i){console.error("[TelegramChannel] Error processing update:",i)}}}startWebhook(){typeof process>"u"||(console.log("[TelegramChannel] Starting webhook mode"),import("http").then(n=>{this.server=n.createServer((o,r)=>{this.handleWebhookRequest(o,r)});let e=new URL(this.config.webhookUrl||"http://localhost:3000"),t=parseInt(e.port,10)||3e3;this.server.listen(t,()=>{console.log(`[TelegramChannel] Webhook server listening on port ${t}`)}),this.setWebhook()}).catch(n=>{console.error("[TelegramChannel] Failed to start webhook server:",n)}))}async setWebhook(){if(!this.config.webhookUrl)return;let n=await fetch(`https://api.telegram.org/bot${this.config.token}/setWebhook`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({url:this.config.webhookUrl})});if(!n.ok){console.error("[TelegramChannel] Failed to set webhook");return}let e=await n.json();e.ok?console.log("[TelegramChannel] Webhook set successfully"):console.error("[TelegramChannel] Failed to set webhook:",e.description)}handleWebhookRequest(n,e){if(n.method!=="POST"){e.writeHead(405),e.end("Method not allowed");return}let t="";n.on("data",o=>{t+=o.toString()}),n.on("end",()=>{try{let o=JSON.parse(t);this.handleMessage(this.normalize(o)).catch(r=>{console.error("[TelegramChannel] Error processing webhook:",r)}),e.writeHead(200),e.end("OK")}catch(o){console.error("[TelegramChannel] Error parsing webhook:",o),e.writeHead(400),e.end("Bad request")}})}async stop(){if(this.pollingInterval&&(clearInterval(this.pollingInterval),this.pollingInterval=void 0),this.server)return new Promise(n=>{this.server.close(n)});if(this.config.webhookUrl)try{await fetch(`https://api.telegram.org/bot${this.config.token}/deleteWebhook`,{method:"POST"})}catch(n){console.error("[TelegramChannel] Failed to delete webhook:",n)}}};var S=class extends d{isTriggerChannel=!1;config;client;constructor(n){super(),this.config=n,this.name=n.name}listen(){typeof process<"u"&&import("discord.js").then(n=>{let{Client:e,GatewayIntentBits:t}=n;this.client=new e({intents:[t.Guilds,t.GuildMessages,t.MessageContent,t.DirectMessages]}),this.client.on("ready",()=>{console.log(`[DiscordChannel] Bot logged in as ${this.client.user?.tag}`)}),this.client.on("messageCreate",o=>{this.handleDiscordMessage(o)}),this.client.login(this.config.token).catch(o=>{console.error("[DiscordChannel] Failed to login to Discord:",o)})}).catch(n=>{console.error("[DiscordChannel] Failed to initialize Discord client:",n),console.error("[DiscordChannel] Make sure to install discord.js: npm install discord.js")})}async send(n){if(!this.client)throw new Error("Discord client not initialized. Did you call listen()?");try{let e=n.metadata?.channelId||this.config.channelId,t=await this.client.channels.fetch(e);if(!t||!("send"in t))throw new Error(`Channel ${e} not found or is not a text channel`);let o={content:n.output},r=n.metadata?.threadId;if(r){let i=await this.client.channels.fetch(r);if(i&&"send"in i){await i.send(o);return}}await t.send(o)}catch(e){throw console.error("[DiscordChannel] Failed to send Discord message:",e),new Error(`Failed to send Discord message: ${e instanceof Error?e.message:String(e)}`)}}normalize(n){let e=n,t=e.channelId,o=(t??"")+(e.thread?.id?`:${e.thread.id}`:""),r=e.channel?.type,i=r===1||r===3,s=e.author?.id,a=e.author?.globalName||e.author?.username;return{message:e.content,conversationId:o,data:e,participant:s?{kind:"user",id:s,displayName:a}:void 0,context:{userId:s,username:e.author?.username,channelType:i?"dm":"channel",channelId:t,channelName:e.channel?.name,guildId:e.guildId,threadId:e.thread?.id,messageId:e.id}}}handleDiscordMessage(n){if(n.author?.bot||n.channelId!==this.config.channelId||n.guildId!==this.config.guildId)return;let e=this.normalize(n);this.handleMessage(e)}async stop(){this.client&&(await this.client.destroy(),this.client=void 0)}};var b=class extends d{isTriggerChannel=!0;config;transporter;constructor(n){super(),this.config=n,this.name=n.name}listen(){typeof process<"u"&&import("nodemailer").then(n=>{this.transporter=n.default.createTransport({host:this.config.smtp.host,port:this.config.smtp.port,secure:this.config.smtp.secure??this.config.smtp.port===465,auth:{user:this.config.smtp.auth.user,pass:this.config.smtp.auth.pass}}),console.log(`[EmailChannel] Email transporter initialized for ${this.config.from}`)}).catch(n=>{console.error("[EmailChannel] Failed to initialize nodemailer:",n),console.error("[EmailChannel] Make sure to install nodemailer: npm install nodemailer")})}async send(n){if(!this.transporter)throw new Error("Email transporter not initialized. Did you call listen()?");let e=Array.isArray(this.config.to)?this.config.to:[this.config.to],t=this.config.subject||"Message from Agent",o={from:this.config.from,to:e.join(", "),subject:t,text:n.output,html:this.formatAsHtml(n.output)};try{let r=await this.transporter.sendMail(o);console.log(`[EmailChannel] Email sent: ${r.messageId}`)}catch(r){throw console.error("[EmailChannel] Failed to send email:",r),new Error(`Failed to send email: ${r instanceof Error?r.message:String(r)}`)}}normalize(n){throw new Error("EmailChannel is outbound-only. Use WebhookChannel with email webhook events for inbound email.")}formatAsHtml(n){return n.split(`
1
+ var l=class{name;_handler;onMessage(e){this._handler=e}async handleMessage(e){this._handler&&await this._handler(e)}};import{createHmac as $,timingSafeEqual as _}from"crypto";var C=class extends l{isTriggerChannel=!1;config;server;participantCache=new Map;botUserId;allowedChannels;constructor(e){super(),this.config={port:3e3,...e},this.name=e.name;let n=e.channel;this.allowedChannels=n==null?null:Array.isArray(n)?n:[n]}listen(){typeof process<"u"&&import("http").then(e=>{this.server=e.createServer((n,t)=>{this.handleRequest(n,t)}),this.server.listen(this.config.port,()=>{console.log(`[SlackChannel] Listening on port ${this.config.port}`),this.runStartupCheck().catch(()=>{})})}).catch(e=>{console.error("[SlackChannel] Failed to start HTTP server:",e)})}async runStartupCheck(){try{let n=await(await fetch("https://slack.com/api/auth.test",{method:"POST",headers:{Authorization:`Bearer ${this.config.token}`,"Content-Type":"application/json"}})).json();n.ok?(this.botUserId=n.user_id,console.log(`[SlackChannel] Connected as @${n.user} (${n.user_id}) in workspace "${n.team}" \u2014 ${n.url}`)):console.warn(`[SlackChannel] auth.test failed: ${n.error}. Check your bot token.`)}catch(e){console.warn("[SlackChannel] Startup self-check failed (network error):",e)}}verifySignature(e,n){let t=e["x-slack-request-timestamp"],r=e["x-slack-signature"];if(!t||!r||Array.isArray(t)||Array.isArray(r))return!1;let i=parseInt(t,10),s=Math.floor(Date.now()/1e3);if(isNaN(i)||Math.abs(s-i)>300)return!1;let o=`v0:${t}:${n}`,d=`v0=${$("sha256",this.config.signingSecret).update(o).digest("hex")}`;if(d.length!==r.length)return!1;try{return _(Buffer.from(d),Buffer.from(r))}catch{return!1}}async send(e){let n=e.metadata?.threadTs??e.metadata?.thread_ts??e.metadata?.threadId,r=e.metadata?.channelId??(this.allowedChannels&&this.allowedChannels.length>0?this.allowedChannels[0]:void 0);if(!r)throw new Error("[SlackChannel] Cannot send: no channel configured and metadata.channelId is missing. Provide a target via SlackChannelConfig.channel or output.metadata.channelId.");let i={channel:r,text:e.output};n&&(i.thread_ts=n);let s=await fetch("https://slack.com/api/chat.postMessage",{method:"POST",headers:{Authorization:`Bearer ${this.config.token}`,"Content-Type":"application/json"},body:JSON.stringify(i)});if(!s.ok)throw new Error(`Failed to send Slack message: ${s.statusText}`);let o=await s.json();if(!o.ok)throw new Error(`Slack API error: ${o.error}`)}normalize(e){let n=e,r=(n.text||"").replace(/<(https?:\/\/[^|>]+)\|([^>]+)>/g,"$2 ($1)").replace(/<(https?:\/\/[^>]+)>/g,"$1").replace(/<!here>/g,"@here").replace(/<!channel>/g,"@channel").replace(/<!everyone>/g,"@everyone"),i=n.ts,s=n.thread_ts,o=s!==void 0&&s!==i,a=n.user,d=a?{kind:"user",id:a}:void 0,f=/<@([A-Z0-9]+)>/g,g=[],m;for(;(m=f.exec(r))!==null;)g.push(m[1]);let p=n.channel;return{message:r,conversationId:o?s:p||i||"",data:n,participant:d,context:{user:a,channel:p,team:n.team,channelType:n.channel_type,threadId:o?s:void 0,mentions:g.length>0?g:void 0,channelId:p,channelName:typeof this.config.channel=="string"?this.config.channel:p}}}async resolveParticipant(e){let n=e.participant?.id??e.context?.user;if(!n)return;let t=this.participantCache.get(n);if(t)return t;try{let r=await fetch(`https://slack.com/api/users.info?user=${encodeURIComponent(n)}`,{method:"GET",headers:{Authorization:`Bearer ${this.config.token}`}});if(!r.ok)return{kind:"user",id:n};let i=await r.json();if(!i.ok||!i.user){let a={kind:"user",id:n};return this.participantCache.set(n,a),a}let s=i.user.profile?.display_name||i.user.profile?.real_name||i.user.real_name||i.user.name||n,o={kind:"user",id:n,displayName:s,metadata:{slackUser:i.user}};return this.participantCache.set(n,o),o}catch{return{kind:"user",id:n}}}invalidateParticipant(e){this.participantCache.delete(e)}shouldProcessEvent(e){let n=e.type;if(n!=="message"&&n!=="app_mention")return!1;if(this.allowedChannels!==null){let s=e.channel_type;if(!(s==="im"||s==="mpim")){let a=e.channel;if(!a||!this.allowedChannels.includes(a))return!1}}let t=e.user;if(this.botUserId&&t===this.botUserId)return!1;let r=e.bot_id;if(!r)return!0;let i=this.config.blockedBotIds??[];if(i.includes(r)||t!==void 0&&i.includes(t))return!1;if(this.config.allowedBotIds!==void 0){let s=this.config.allowedBotIds;return s.includes(r)||t!==void 0&&s.includes(t)}return!0}handleRequest(e,n){if(e.method!=="POST"){n.writeHead(405),n.end("Method not allowed");return}let t="";e.on("data",r=>{t+=r.toString()}),e.on("end",()=>{if(!this.verifySignature(e.headers,t)){n.writeHead(401),n.end("Invalid signature");return}try{let r=JSON.parse(t);if(r.type==="url_verification"){n.writeHead(200,{"Content-Type":"application/json"}),n.end(JSON.stringify({challenge:r.challenge}));return}if(r.type==="event_callback"&&r.event){let i=r.event;if(this.shouldProcessEvent(i)){let s=this.normalize(i);this.handleMessage(s)}else i.type==="user_change"&&i.user&&this.invalidateParticipant(i.user.id);n.writeHead(200),n.end("OK");return}n.writeHead(200),n.end("OK")}catch(r){console.error("[SlackChannel] Error handling request:",r),n.writeHead(400),n.end("Bad request")}})}async stop(){if(this.server)return new Promise(e=>{this.server.close(e)})}};var v=class extends l{isTriggerChannel=!1;config;server;pendingResponses=new Map;constructor(e){super(),this.name=e.name,this.config={port:e.port??3e3,path:e.path??"/webhook"}}listen(){typeof process<"u"&&import("http").then(e=>{this.server=e.createServer((n,t)=>{this.handleRequest(n,t)}),this.server.listen(this.config.port,()=>{console.log(`[WebhookChannel] Listening on port ${this.config.port}${this.config.path}`)})}).catch(e=>{console.error("[WebhookChannel] Failed to start HTTP server:",e)})}async send(e){let n=e.metadata?.conversationId;if(n&&this.pendingResponses.has(n)){let t=this.pendingResponses.get(n);this.pendingResponses.delete(n),t.resolve({output:e.output,metadata:e.metadata})}}normalize(e){let n=e,t=n.headers||{},r=t["x-session-id"]||t["X-Session-Id"]||n.sessionId||n.conversationId||this.generateSessionId();return{message:n.message||n.text||"",intent:n.intent,conversationId:r,data:n,context:{headers:n.headers,method:n.method,sessionId:r}}}handleRequest(e,n){if(e.url!==this.config.path){n.writeHead(404),n.end("Not found");return}if(e.method!=="POST"){n.writeHead(405),n.end("Method not allowed");return}let t="";e.on("data",r=>{t+=r.toString()}),e.on("end",()=>{try{let r=JSON.parse(t),i=this.normalize(r),s=i.conversationId||this.generateSessionId(),o=new Promise((a,d)=>{this.pendingResponses.set(s,{resolve:a,reject:d}),setTimeout(()=>{this.pendingResponses.has(s)&&(this.pendingResponses.delete(s),d(new Error("Agent response timeout")))},3e4)});this.handleMessage({...i,conversationId:s,context:{...i.context,sessionId:s}}),o.then(a=>{n.writeHead(200,{"Content-Type":"application/json"}),n.end(JSON.stringify(a))}).catch(a=>{n.writeHead(500,{"Content-Type":"application/json"}),n.end(JSON.stringify({error:a.message}))})}catch(r){console.error("[WebhookChannel] Error handling request:",r),n.writeHead(400,{"Content-Type":"application/json"}),n.end(JSON.stringify({error:"Bad request"}))}})}generateSessionId(){return`webhook-${Date.now()}-${Math.random().toString(36).substring(2,9)}`}async stop(){if(this.server)return new Promise(e=>{this.server.close(e)})}};import{CronExpressionParser as P}from"cron-parser";var k=class extends l{isTriggerChannel=!0;config;timer;_stopped=!1;_generation=0;constructor(e){if(super(),!e.cron&&!e.store)throw new Error("ScheduledChannel: provide at least one of `cron` (static schedule) or `store` (dynamic scheduling).");if(e.cron)try{P.parse(e.cron)}catch(n){throw new Error(`ScheduledChannel: invalid cron expression '${e.cron}': ${n.message}`)}if(e.store&&!e.name&&console.warn("[ScheduledChannel] A `store` was provided without a `name`. All store queries will be unscoped and will pick up jobs from every channel. Set `name` to scope this channel to its own jobs."),e.idlePollMs!==void 0&&e.idlePollMs<1e3)throw new Error(`ScheduledChannel: idlePollMs must be at least 1000ms (got ${e.idlePollMs}). Values below 1 second create a tight polling loop.`);this.config=e,this.name=e.name}listen(){this._generation++,this.timer&&(clearTimeout(this.timer),this.timer=void 0),this._stopped=!1,this.config.store?this._listenWithStore():this._listenStatic()}async stop(){this._stopped=!0,this.timer&&(clearTimeout(this.timer),this.timer=void 0)}async send(e){}normalize(e){let n=e,t=new Date,r=`${t.getFullYear()}-${t.getMonth()+1}-${t.getDate()}`;return{intent:n?.intent??this.config.intent,message:n?.message??this.config.message??`Scheduled task triggered at ${t.toISOString()}`,conversationId:`scheduled:${this.name??"default"}:${r}`,data:{...n?.payload??{},scheduled:!0,jobId:n?.id,cron:n?.cron??this.config.cron,timestamp:t.toISOString()}}}_listenStatic(){this._scheduleNextStatic(this._generation)}_scheduleNextStatic(e){if(this._stopped||e!==this._generation)return;let n=this._nextRunFromCron(this.config.cron),t=n.getTime()-Date.now();if(t<=0){this.timer=setTimeout(()=>this._scheduleNextStatic(e),0);return}console.log(`[ScheduledChannel:${this.name??"default"}] Next run: ${n.toISOString()}`),this.timer=setTimeout(async()=>{this._stopped||e!==this._generation||(await this._triggerStatic(),this._scheduleNextStatic(e))},t)}async _triggerStatic(){if(!this._handler){console.warn(`[ScheduledChannel:${this.name??"default"}] Cron fired but no message handler is registered. Call onMessage() before listen() to avoid silently losing triggers.`);return}let e=this.normalize(null);try{await this.handleMessage(e)}catch(n){console.error(`[ScheduledChannel:${this.name??"default"}] Error on trigger:`,n)}}_listenWithStore(){let e=this.config.store,n=this._generation;if(n===1){let r=e.resetStuck(this.name);r>0&&console.log(`[ScheduledChannel:${this.name??"default"}] Reset ${r} stuck 'running' job(s) to 'pending'.`)}if(this.config.cron){let{duplicate:r}=e.create({channelName:this.name,cron:this.config.cron,intent:this.config.intent,message:this.config.message});r||console.log(`[ScheduledChannel:${this.name??"default"}] Seeded static cron '${this.config.cron}' into store.`)}let t=e.getDue(Date.now(),this.name);t.length>0&&(console.log(`[ScheduledChannel:${this.name??"default"}] Recovering ${t.length} overdue job(s).`),Promise.allSettled(t.map(r=>this._triggerJob(r)))),this._scheduleNextFromStore(n)}_scheduleNextFromStore(e){if(this._stopped||e!==this._generation)return;let n=this.config.store,t=n.getNextPending(this.name);if(!t){let i=this.config.idlePollMs??3e4;this.timer=setTimeout(()=>this._scheduleNextFromStore(e),i);return}let r=Math.max(0,t.nextRunAt-Date.now());console.log(`[ScheduledChannel:${this.name??"default"}] Next store job at ${new Date(t.nextRunAt).toISOString()} (in ${Math.round(r/1e3)}s)`),this.timer=setTimeout(async()=>{if(this._stopped||e!==this._generation)return;let i=n.getDue(Date.now(),this.name);await Promise.allSettled(i.map(s=>this._triggerJob(s))),this._scheduleNextFromStore(e)},r)}async _triggerJob(e){let n=this.config.store;if(!this._handler){console.warn(`[ScheduledChannel:${this.name??"default"}] Job ${e.id} fired but no message handler is registered. Call onMessage() before listen() to avoid silently losing jobs.`),n.markFailed(e.id,"No message handler registered");return}n.markRunning(e.id);let t=this.normalize(e);try{await this.handleMessage(t),n.markCompleted(e.id)}catch(r){let i=r instanceof Error?r.message:String(r);console.error(`[ScheduledChannel:${this.name??"default"}] Job ${e.id} failed:`,r),n.markFailed(e.id,i)}}_nextRunFromCron(e){return P.parse(e,{currentDate:new Date}).next().toDate()}};var y=class extends l{isTriggerChannel=!1;config;offset=0;pollingInterval;server;botUserId;botUsername;constructor(e){super(),this.name=e.name,this.config=e}listen(){this.runStartupCheck().catch(()=>{}),this.config.webhookUrl?this.startWebhook():this.startPolling()}async runStartupCheck(){try{let n=await(await fetch(`https://api.telegram.org/bot${this.config.token}/getMe`)).json();if(n.ok&&n.result){let t=n.result;this.botUserId=t.id!=null?String(t.id):void 0,this.botUsername=t.username,console.log(`[TelegramChannel] Connected as @${t.username} (id: ${t.id}, name: ${t.first_name})`)}else console.warn(`[TelegramChannel] getMe failed: ${n.description??"unknown error"}. Check your bot token.`)}catch(e){console.warn("[TelegramChannel] Startup self-check failed (network error):",e)}}async send(e){let n=e.metadata?.chatId;if(!n)throw new Error("Telegram send requires chatId in metadata");let t=await fetch(`https://api.telegram.org/bot${this.config.token}/sendMessage`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({chat_id:n,text:e.output,parse_mode:"Markdown"})});if(!t.ok)throw new Error(`Failed to send Telegram message: ${t.statusText}`);let r=await t.json();if(!r.ok)throw new Error(`Telegram API error: ${r.description}`)}normalize(e){let n=e,t=n.message||n.edited_message||{},r=t.text||"",i=t.chat||{},s=t.from||{},o=s.id!=null?String(s.id):void 0,a=s.first_name||s.username||o,d=o?{kind:"user",id:o,displayName:a??o}:void 0,f=t.entities??[],g=[];for(let w of f)if(w.type==="text_mention"&&w.user){let T=w.user;T.id!=null&&g.push(String(T.id))}let m=i.type,p=i.id!=null?String(i.id):"";return{message:r,conversationId:p,data:n,participant:d,context:{chatId:i.id,userId:s.id,username:s.username,firstName:s.first_name,lastName:s.last_name,messageId:t.message_id,channelType:m,channelId:p,channelName:i.title,mentions:g.length>0?g:void 0}}}startPolling(){console.log("[TelegramChannel] Starting polling mode"),this.pollingInterval=setInterval(async()=>{try{await this.pollUpdates()}catch(e){console.error("[TelegramChannel] Polling error:",e)}},5e3)}async pollUpdates(){let e=`https://api.telegram.org/bot${this.config.token}/getUpdates?offset=${this.offset}&limit=100`,n=await fetch(e);if(!n.ok)throw new Error(`Telegram getUpdates failed: ${n.statusText}`);let t=await n.json();if(!t.ok)throw new Error("Telegram getUpdates returned not ok");for(let r of t.result){let i=r.update_id;i>=this.offset&&(this.offset=i+1);try{let s=this.normalize(r);await this.handleMessage(s)}catch(s){console.error("[TelegramChannel] Error processing update:",s)}}}startWebhook(){typeof process>"u"||(console.log("[TelegramChannel] Starting webhook mode"),import("http").then(e=>{this.server=e.createServer((r,i)=>{this.handleWebhookRequest(r,i)});let n=new URL(this.config.webhookUrl||"http://localhost:3000"),t=parseInt(n.port,10)||3e3;this.server.listen(t,()=>{console.log(`[TelegramChannel] Webhook server listening on port ${t}`)}),this.setWebhook()}).catch(e=>{console.error("[TelegramChannel] Failed to start webhook server:",e)}))}async setWebhook(){if(!this.config.webhookUrl)return;let e=await fetch(`https://api.telegram.org/bot${this.config.token}/setWebhook`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({url:this.config.webhookUrl})});if(!e.ok){console.error("[TelegramChannel] Failed to set webhook");return}let n=await e.json();n.ok?console.log("[TelegramChannel] Webhook set successfully"):console.error("[TelegramChannel] Failed to set webhook:",n.description)}handleWebhookRequest(e,n){if(e.method!=="POST"){n.writeHead(405),n.end("Method not allowed");return}let t="";e.on("data",r=>{t+=r.toString()}),e.on("end",()=>{try{let r=JSON.parse(t);this.handleMessage(this.normalize(r)).catch(i=>{console.error("[TelegramChannel] Error processing webhook:",i)}),n.writeHead(200),n.end("OK")}catch(r){console.error("[TelegramChannel] Error parsing webhook:",r),n.writeHead(400),n.end("Bad request")}})}async stop(){if(this.pollingInterval&&(clearInterval(this.pollingInterval),this.pollingInterval=void 0),this.server)return new Promise(e=>{this.server.close(e)});if(this.config.webhookUrl)try{await fetch(`https://api.telegram.org/bot${this.config.token}/deleteWebhook`,{method:"POST"})}catch(e){console.error("[TelegramChannel] Failed to delete webhook:",e)}}};var A=new Set([1,3]),x=/<@!?(\d+)>/g,M="https://discord.com/api/v10",S=class extends l{isTriggerChannel=!1;config;allowedChannelIds;botUserId;participantCache=new Map;client;constructor(e){super(),this.config=e,this.name=e.name,e.channelId==null?this.allowedChannelIds=new Set:Array.isArray(e.channelId)?this.allowedChannelIds=new Set(e.channelId):this.allowedChannelIds=new Set([e.channelId])}shouldProcessEvent(e){return!e.author||e.webhookId||this.botUserId&&e.author.id===this.botUserId||this.config.guildId&&e.guildId!==this.config.guildId||this.allowedChannelIds.size>0&&(!e.channelId||!this.allowedChannelIds.has(e.channelId))?!1:e.author.bot||e.author.system?this.config.blockedBotIds?.includes(e.author.id)?!1:this.config.allowedBotIds?this.config.allowedBotIds.includes(e.author.id):!1:!0}normalize(e){let n=e,t=n.channelId,r=(t??"")+(n.thread?.id?`:${n.thread.id}`:""),i=n.channel?.type,s=i!==void 0&&A.has(i),o=n.author?.id,a=n.author?.globalName??n.author?.username,d=[];if(n.content){x.lastIndex=0;let f;for(;(f=x.exec(n.content))!==null;)d.push(f[1])}return{message:n.content,conversationId:r,data:n,participant:o?{kind:"user",id:o,displayName:a??void 0}:void 0,context:{userId:o,username:n.author?.username,channelType:s?"dm":"channel",channelId:t,channelName:n.channel?.name,guildId:n.guildId,threadId:n.thread?.id,messageId:n.id,mentions:d.length>0?d:void 0,isMentioned:this.botUserId?d.includes(this.botUserId):void 0}}}async resolveParticipant(e){let n=e.participant?.id??e.context?.userId;if(!n)return;let t=this.participantCache.get(n);if(t)return t;try{let r=await fetch(`${M}/users/${n}`,{headers:{Authorization:`Bot ${this.config.token}`}});if(!r.ok)return console.warn(`[DiscordChannel] Failed to resolve user ${n}: HTTP ${r.status}`),{kind:"user",id:n};let i=await r.json(),s=i.global_name??i.username,o={kind:"user",id:i.id,displayName:s};return this.participantCache.set(n,o),o}catch(r){return console.warn(`[DiscordChannel] resolveParticipant error for ${n}:`,r),{kind:"user",id:n}}}invalidateParticipant(e){this.participantCache.delete(e)}async send(e){if(!this.client)throw new Error("[DiscordChannel] Client not initialized. Did you call listen()?");let n=e.metadata?.threadId;if(n){let i=await this.client.channels.fetch(n);if(i&&"send"in i)try{await i.send({content:e.output});return}catch(s){throw console.error("[DiscordChannel] Failed to send to thread:",s),new Error(`[DiscordChannel] Failed to send Discord message: ${s instanceof Error?s.message:String(s)}`)}console.warn(`[DiscordChannel] Thread ${n} not found or not sendable; falling back to channel send.`)}let t=e.metadata?.channelId??(this.allowedChannelIds.size>0?[...this.allowedChannelIds][0]:void 0);if(!t)throw new Error("[DiscordChannel] No channel ID available for sending. Configure channelId or pass output.metadata.channelId.");let r=await this.client.channels.fetch(t);if(!r||!("send"in r))throw new Error(`[DiscordChannel] Channel ${t} not found or is not a text channel`);try{await r.send({content:e.output})}catch(i){throw console.error("[DiscordChannel] Failed to send to channel:",i),new Error(`[DiscordChannel] Failed to send Discord message: ${i instanceof Error?i.message:String(i)}`)}}listen(){typeof process>"u"||import("discord.js").then(e=>{let{Client:n,GatewayIntentBits:t}=e;this.client=new n({intents:[t.Guilds,t.GuildMessages,t.MessageContent,t.DirectMessages]}),this.client.on("ready",()=>{let r=this.client.user;this.botUserId=r?.id;let i=this.allowedChannelIds.size===0?"all channels":[...this.allowedChannelIds].join(", "),s=this.config.guildId??"all guilds";console.log(`[DiscordChannel] Logged in as ${r?.tag??"unknown"} (botUserId=${this.botUserId??"unknown"}) | guild=${s} | channels=${i}`)}),this.client.on("messageCreate",r=>{let i=r;if(!this.shouldProcessEvent(i))return;let s=this.normalize(i);this.handleMessage(s)}),this.client.on("userUpdate",(r,i)=>{let s=i;s?.id&&this.invalidateParticipant(s.id)}),this.client.login(this.config.token).catch(r=>{console.error("[DiscordChannel] Failed to login to Discord:",r)})}).catch(e=>{console.error("[DiscordChannel] Failed to initialize Discord client:",e),console.error("[DiscordChannel] Make sure discord.js is installed: npm install discord.js")})}async stop(){this.client&&(await this.client.destroy(),this.client=void 0)}};var b=class extends l{isTriggerChannel=!0;config;transporter;constructor(e){super(),this.config=e,this.name=e.name}listen(){typeof process<"u"&&import("nodemailer").then(e=>{this.transporter=e.default.createTransport({host:this.config.smtp.host,port:this.config.smtp.port,secure:this.config.smtp.secure??this.config.smtp.port===465,auth:{user:this.config.smtp.auth.user,pass:this.config.smtp.auth.pass}}),console.log(`[EmailChannel] Email transporter initialized for ${this.config.from}`)}).catch(e=>{console.error("[EmailChannel] Failed to initialize nodemailer:",e),console.error("[EmailChannel] Make sure to install nodemailer: npm install nodemailer")})}async send(e){if(!this.transporter)throw new Error("Email transporter not initialized. Did you call listen()?");let n=Array.isArray(this.config.to)?this.config.to:[this.config.to],t=this.config.subject||"Message from Agent",r={from:this.config.from,to:n.join(", "),subject:t,text:e.output,html:this.formatAsHtml(e.output)};try{await this.transporter.sendMail(r)}catch(i){throw console.error("[EmailChannel] Failed to send email:",i),new Error(`Failed to send email: ${i instanceof Error?i.message:String(i)}`)}}normalize(e){throw new Error("EmailChannel is outbound-only. Use WebhookChannel with email webhook events for inbound email.")}formatAsHtml(e){return e.split(`
2
2
 
3
- `).map(e=>`<p>${e.replace(/\n/g,"<br>")}</p>`).join("")}async stop(){this.transporter&&(this.transporter.close(),this.transporter=void 0)}};var I=class extends d{config;twilioClient;server;constructor(n){super(),this.config={port:3e3,...n},this.name=n.name}get isTriggerChannel(){return!this.config.webhookPath}listen(){typeof process<"u"&&import("twilio").then(n=>{this.twilioClient=n.default(this.config.accountSid,this.config.authToken),console.log("[SMSChannel] Twilio client initialized"),this.config.webhookPath&&this.startWebhookServer()}).catch(n=>{console.error("[SMSChannel] Failed to initialize Twilio client:",n),console.error("[SMSChannel] Make sure to install twilio: npm install twilio")})}async send(n){if(!this.twilioClient)throw new Error("Twilio client not initialized. Did you call listen()?");let e=n.metadata?.from||this.config.to;if(!e)throw new Error('No recipient phone number specified. Set "to" in config or provide in output.metadata.from');try{let t=await this.twilioClient.messages.create({body:n.output,from:this.config.from,to:e});console.log(`[SMSChannel] SMS sent: ${t.sid}`)}catch(t){throw console.error("[SMSChannel] Failed to send SMS:",t),new Error(`Failed to send SMS: ${t instanceof Error?t.message:String(t)}`)}}normalize(n){let e=n,t=e.From,o=e.Body,r=e.MessageSid;return{message:o,conversationId:t,data:e,context:{from:t,to:e.To,messageSid:r}}}startWebhookServer(){import("http").then(n=>{this.server=n.createServer((e,t)=>{this.handleWebhookRequest(e,t)}),this.server.listen(this.config.port,()=>{console.log(`[SMSChannel] Webhook server listening on port ${this.config.port} at ${this.config.webhookPath}`)})}).catch(n=>{console.error("[SMSChannel] Failed to start webhook server:",n)})}handleWebhookRequest(n,e){if(n.method!=="POST"||n.url!==this.config.webhookPath){e.writeHead(404),e.end("Not found");return}let t="";n.on("data",o=>{t+=o.toString()}),n.on("end",()=>{try{let o=new URLSearchParams(t),r={};o.forEach((s,a)=>{r[a]=s});let i=this.normalize(r);this.handleMessage(i),e.writeHead(200,{"Content-Type":"text/xml"}),e.end('<?xml version="1.0" encoding="UTF-8"?><Response></Response>')}catch(o){console.error("[SMSChannel] Error handling webhook:",o),e.writeHead(400),e.end("Bad request")}})}async stop(){if(this.server)return new Promise(n=>{this.server.close(n)})}};export{d as BaseChannel,S as DiscordChannel,b as EmailChannel,I as SMSChannel,k as ScheduledChannel,C as SlackChannel,v as TelegramChannel,y as WebhookChannel};
3
+ `).map(n=>`<p>${n.replace(/\n/g,"<br>")}</p>`).join("")}async stop(){this.transporter&&(this.transporter.close(),this.transporter=void 0)}};var I=class extends l{config;twilioClient;server;constructor(e){super(),this.config={port:3e3,...e},this.name=e.name}get isTriggerChannel(){return!this.config.webhookPath}listen(){typeof process<"u"&&import("twilio").then(e=>{this.twilioClient=e.default(this.config.accountSid,this.config.authToken),console.log("[SMSChannel] Twilio client initialized"),this.config.webhookPath&&this.startWebhookServer()}).catch(e=>{console.error("[SMSChannel] Failed to initialize Twilio client:",e),console.error("[SMSChannel] Make sure to install twilio: npm install twilio")})}async send(e){if(!this.twilioClient)throw new Error("Twilio client not initialized. Did you call listen()?");let n=e.metadata?.from||this.config.to;if(!n)throw new Error('No recipient phone number specified. Set "to" in config or provide in output.metadata.from');try{await this.twilioClient.messages.create({body:e.output,from:this.config.from,to:n})}catch(t){throw console.error("[SMSChannel] Failed to send SMS:",t),new Error(`Failed to send SMS: ${t instanceof Error?t.message:String(t)}`)}}normalize(e){let n=e,t=n.From,r=n.Body,i=n.MessageSid;return{message:r,conversationId:t,data:n,context:{from:t,to:n.To,messageSid:i}}}startWebhookServer(){import("http").then(e=>{this.server=e.createServer((n,t)=>{this.handleWebhookRequest(n,t)}),this.server.listen(this.config.port,()=>{console.log(`[SMSChannel] Webhook server listening on port ${this.config.port} at ${this.config.webhookPath}`)})}).catch(e=>{console.error("[SMSChannel] Failed to start webhook server:",e)})}handleWebhookRequest(e,n){if(e.method!=="POST"||e.url!==this.config.webhookPath){n.writeHead(404),n.end("Not found");return}let t="";e.on("data",r=>{t+=r.toString()}),e.on("end",()=>{try{let r=new URLSearchParams(t),i={};r.forEach((o,a)=>{i[a]=o});let s=this.normalize(i);this.handleMessage(s),n.writeHead(200,{"Content-Type":"text/xml"}),n.end('<?xml version="1.0" encoding="UTF-8"?><Response></Response>')}catch(r){console.error("[SMSChannel] Error handling webhook:",r),n.writeHead(400),n.end("Bad request")}})}async stop(){if(this.server)return new Promise(e=>{this.server.close(e)})}};export{l as BaseChannel,S as DiscordChannel,b as EmailChannel,I as SMSChannel,k as ScheduledChannel,C as SlackChannel,y as TelegramChannel,v as WebhookChannel};