pub mod access;
pub mod capture;
pub mod discord;
pub mod discord_ws;
pub mod github;
pub mod leader;
pub mod notification;
pub mod routing;
pub mod slack;
pub mod stream;
pub mod telegram;
pub mod tmux;
pub mod transport;
use std::fmt;
use std::fmt::Debug;
use std::fmt::Formatter;
use std::sync::{Mutex, PoisonError};
use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use tokio::sync::mpsc::Receiver;
use tokio::task::{AbortHandle, JoinHandle};
pub(crate) fn html_escape(s: &str) -> String {
s.replace('&', "&")
.replace('<', "<")
.replace('>', ">")
}
pub(crate) fn split_utf8(content: &str, max_bytes: usize) -> Vec<String> {
if content.len() <= max_bytes {
return vec![content.to_string()];
}
let mut chunks = Vec::new();
let mut start = 0;
let total = content.len();
while start < total {
let mut end = (start + max_bytes).min(total);
while end > start && !content.is_char_boundary(end) {
end -= 1;
}
if end == start {
if let Some(ch) = content[start..].chars().next() {
end = start + ch.len_utf8();
} else {
break;
}
}
chunks.push(content[start..end].to_string());
start = end;
}
chunks
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct IncomingMessage {
pub channel: String,
pub id: String,
pub thread_id: String,
pub author: String,
pub body: String,
pub timestamp: chrono::DateTime<chrono::Utc>,
#[serde(default)]
pub metadata: serde_json::Value,
#[serde(default)]
pub topic_id: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OutgoingMessage {
pub thread_id: String,
pub body: String,
pub reply_to: Option<String>,
#[serde(default)]
pub metadata: serde_json::Value,
#[serde(default)]
pub topic_id: Option<String>,
}
#[derive(Debug, Clone)]
pub struct OutputChunk {
pub content: String,
pub is_final: bool,
}
#[async_trait]
pub trait Channel: Send + Sync {
fn name(&self) -> &str;
async fn start(&self) -> anyhow::Result<Receiver<IncomingMessage>>;
async fn send(&self, msg: &OutgoingMessage) -> anyhow::Result<()>;
async fn ack_interaction(&self, _callback_query_id: &str) -> anyhow::Result<()> {
Ok(())
}
async fn health_check(&self) -> anyhow::Result<()>;
async fn stop(&self) {}
}
#[derive(Debug, Default)]
pub(crate) struct Poller(Mutex<Option<AbortHandle>>);
impl Poller {
pub(crate) fn set(&self, task: &JoinHandle<()>) {
let old = self
.0
.lock()
.unwrap_or_else(PoisonError::into_inner)
.replace(task.abort_handle());
if let Some(old) = old {
old.abort();
}
}
pub(crate) fn stop(&self) {
let task = self.0.lock().unwrap_or_else(PoisonError::into_inner).take();
if let Some(task) = task {
task.abort();
}
}
}
pub struct ChannelRegistry {
channels: Vec<Box<dyn Channel>>,
}
impl Debug for ChannelRegistry {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.debug_list()
.entries(self.channels.iter().map(|c| c.name()))
.finish()
}
}
impl ChannelRegistry {
pub fn new() -> Self {
Self {
channels: Vec::new(),
}
}
pub fn register(&mut self, channel: Box<dyn Channel>) {
tracing::info!(channel = channel.name(), "registered channel");
self.channels.push(channel);
}
pub fn iter(&self) -> impl Iterator<Item = &dyn Channel> {
self.channels.iter().map(AsRef::as_ref)
}
}