1pub mod access;
13pub mod capture;
14pub mod discord;
15pub mod discord_ws;
16pub mod github;
17pub mod notification;
18pub mod routing;
19pub mod slack;
20pub mod stream;
21pub mod telegram;
22pub mod tmux;
23pub mod transport;
24
25use std::fmt;
26use std::fmt::Debug;
27use std::fmt::Formatter;
28
29use async_trait::async_trait;
30use serde::{Deserialize, Serialize};
31use tokio::sync::mpsc::Receiver;
32
33pub(crate) fn html_escape(s: &str) -> String {
35 s.replace('&', "&")
36 .replace('<', "<")
37 .replace('>', ">")
38}
39
40pub(crate) fn split_utf8(content: &str, max_bytes: usize) -> Vec<String> {
42 if content.len() <= max_bytes {
43 return vec![content.to_string()];
44 }
45
46 let mut chunks = Vec::new();
47 let mut start = 0;
48 let total = content.len();
49
50 while start < total {
51 let mut end = (start + max_bytes).min(total);
52 while end > start && !content.is_char_boundary(end) {
53 end -= 1;
54 }
55 if end == start {
56 if let Some(ch) = content[start..].chars().next() {
58 end = start + ch.len_utf8();
59 } else {
60 break;
61 }
62 }
63 chunks.push(content[start..end].to_string());
64 start = end;
65 }
66
67 chunks
68}
69
70#[derive(Debug, Clone, Serialize, Deserialize)]
72pub struct IncomingMessage {
73 pub channel: String,
75 pub id: String,
77 pub thread_id: String,
79 pub author: String,
81 pub body: String,
83 pub timestamp: chrono::DateTime<chrono::Utc>,
85 #[serde(default)]
87 pub metadata: serde_json::Value,
88 #[serde(default)]
90 pub topic_id: Option<String>,
91}
92
93#[derive(Debug, Clone, Serialize, Deserialize)]
95pub struct OutgoingMessage {
96 pub thread_id: String,
98 pub body: String,
100 pub reply_to: Option<String>,
102 #[serde(default)]
104 pub metadata: serde_json::Value,
105 #[serde(default)]
107 pub topic_id: Option<String>,
108}
109
110#[derive(Debug, Clone)]
112pub struct OutputChunk {
113 pub content: String,
114 pub is_final: bool,
115}
116
117#[async_trait]
119pub trait Channel: Send + Sync {
120 fn name(&self) -> &str;
122
123 async fn start(&self) -> anyhow::Result<Receiver<IncomingMessage>>;
126
127 async fn send(&self, msg: &OutgoingMessage) -> anyhow::Result<()>;
129
130 async fn ack_interaction(&self, _callback_query_id: &str) -> anyhow::Result<()> {
136 Ok(())
137 }
138
139 async fn health_check(&self) -> anyhow::Result<()>;
141}
142
143pub struct ChannelRegistry {
145 channels: Vec<Box<dyn Channel>>,
146}
147
148impl Debug for ChannelRegistry {
149 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
150 f.debug_list()
151 .entries(self.channels.iter().map(|c| c.name()))
152 .finish()
153 }
154}
155
156impl ChannelRegistry {
157 pub fn new() -> Self {
158 Self {
159 channels: Vec::new(),
160 }
161 }
162
163 pub fn register(&mut self, channel: Box<dyn Channel>) {
164 tracing::info!(channel = channel.name(), "registered channel");
165 self.channels.push(channel);
166 }
167
168 pub fn iter(&self) -> impl Iterator<Item = &dyn Channel> {
169 self.channels.iter().map(AsRef::as_ref)
170 }
171}