mod agent;
pub(crate) mod follow_up_question;
pub(crate) mod handler;
pub use agent::SlackAgent;
use slack_morphism::prelude::SlackHyperClient;
use std::collections::HashMap;
use std::sync::Arc;
use tokio::sync::{Mutex, oneshot};
use tokio_util::sync::CancellationToken;
use uuid::Uuid;
type PendingSlackQuestion = (oneshot::Sender<String>, Vec<String>);
pub struct SlackState {
client: Mutex<Option<Arc<SlackHyperClient>>>,
bot_token: Mutex<Option<String>>,
owner_channel_id: Mutex<Option<String>>,
session_channels: Mutex<HashMap<Uuid, String>>,
pending_approvals: Mutex<HashMap<String, oneshot::Sender<(bool, bool)>>>,
pending_questions: Mutex<HashMap<String, PendingSlackQuestion>>,
cancel_tokens: Mutex<HashMap<Uuid, CancellationToken>>,
}
impl Default for SlackState {
fn default() -> Self {
Self::new()
}
}
impl SlackState {
pub fn new() -> Self {
Self {
client: Mutex::new(None),
bot_token: Mutex::new(None),
owner_channel_id: Mutex::new(None),
session_channels: Mutex::new(HashMap::new()),
pending_approvals: Mutex::new(HashMap::new()),
pending_questions: Mutex::new(HashMap::new()),
cancel_tokens: Mutex::new(HashMap::new()),
}
}
pub async fn register_pending_question(
&self,
id: String,
tx: oneshot::Sender<String>,
options: Vec<String>,
) {
self.pending_questions
.lock()
.await
.insert(id, (tx, options));
}
pub async fn resolve_pending_question(&self, id: &str, idx: usize) -> Option<String> {
let (tx, options) = self.pending_questions.lock().await.remove(id)?;
let answer = options.get(idx)?.clone();
let _ = tx.send(answer.clone());
Some(answer)
}
pub async fn set_connected(
&self,
client: Arc<SlackHyperClient>,
bot_token: String,
channel_id: Option<String>,
) {
*self.client.lock().await = Some(client);
*self.bot_token.lock().await = Some(bot_token);
if let Some(id) = channel_id {
*self.owner_channel_id.lock().await = Some(id);
}
}
pub async fn set_owner_channel(&self, channel_id: String) {
*self.owner_channel_id.lock().await = Some(channel_id);
}
pub async fn client(&self) -> Option<Arc<SlackHyperClient>> {
self.client.lock().await.clone()
}
pub async fn bot_token(&self) -> Option<String> {
self.bot_token.lock().await.clone()
}
pub async fn owner_channel_id(&self) -> Option<String> {
self.owner_channel_id.lock().await.clone()
}
pub async fn is_connected(&self) -> bool {
self.client.lock().await.is_some()
}
pub async fn register_session_channel(&self, session_id: Uuid, channel_id: String) {
self.session_channels
.lock()
.await
.insert(session_id, channel_id);
}
pub async fn session_channel(&self, session_id: Uuid) -> Option<String> {
self.session_channels.lock().await.get(&session_id).cloned()
}
pub async fn register_pending_approval(&self, id: String, tx: oneshot::Sender<(bool, bool)>) {
self.pending_approvals.lock().await.insert(id, tx);
}
pub async fn resolve_pending_approval(&self, id: &str, approved: bool, always: bool) -> bool {
if let Some(tx) = self.pending_approvals.lock().await.remove(id) {
let _ = tx.send((approved, always));
true
} else {
false
}
}
pub async fn store_cancel_token(&self, session_id: Uuid, token: CancellationToken) {
let mut tokens = self.cancel_tokens.lock().await;
if let Some(old) = tokens.remove(&session_id) {
tracing::warn!(
"Slack: cancelling previous in-flight agent call for session {}",
session_id
);
old.cancel();
}
tokens.insert(session_id, token);
}
pub async fn cancel_session(&self, session_id: Uuid) -> bool {
if let Some(token) = self.cancel_tokens.lock().await.remove(&session_id) {
token.cancel();
true
} else {
false
}
}
pub async fn remove_cancel_token(&self, session_id: Uuid) {
let mut tokens = self.cancel_tokens.lock().await;
if let Some(token) = tokens.get(&session_id)
&& token.is_cancelled()
{
tokens.remove(&session_id);
}
}
}