Skip to main content

adk_tool/slack/
toolset.rs

1//! Slack toolset structure and `Toolset` trait implementation.
2
3use crate::slack::tools::{SlackAddReaction, SlackListThreads, SlackReadChannel, SlackSendMessage};
4use adk_core::{ReadonlyContext, Result, Tool, Toolset};
5use async_trait::async_trait;
6use std::sync::Arc;
7
8/// How the Slack Bot Token is resolved at runtime.
9#[derive(Debug, Clone)]
10pub enum TokenSource {
11    /// A token provided directly at construction time.
12    Direct(String),
13    /// A secret name resolved via `ToolContext::get_secret()` at execution time.
14    SecretRef(String),
15}
16
17/// Native Slack toolset providing channel messaging, reactions, and thread tools.
18///
19/// Authenticates using a Slack Bot Token supplied either directly or via the
20/// configured [`SecretProvider`](adk_core::ToolContext::get_secret).
21///
22/// # Example
23///
24/// ```rust,ignore
25/// use adk_tool::slack::SlackToolset;
26///
27/// // Direct token
28/// let toolset = SlackToolset::new("xoxb-your-bot-token");
29///
30/// // Via secret provider (resolved at tool execution time)
31/// let toolset = SlackToolset::from_secret("slack-bot-token");
32/// ```
33pub struct SlackToolset {
34    pub(crate) client: reqwest::Client,
35    pub(crate) token_source: TokenSource,
36}
37
38impl SlackToolset {
39    /// Create a new `SlackToolset` with a direct Slack Bot Token.
40    pub fn new(token: impl Into<String>) -> Self {
41        Self { client: reqwest::Client::new(), token_source: TokenSource::Direct(token.into()) }
42    }
43
44    /// Create a new `SlackToolset` that resolves the token from the secret
45    /// provider at execution time via `ctx.get_secret(secret_name)`.
46    pub fn from_secret(secret_name: impl Into<String>) -> Self {
47        Self {
48            client: reqwest::Client::new(),
49            token_source: TokenSource::SecretRef(secret_name.into()),
50        }
51    }
52}
53
54#[async_trait]
55impl Toolset for SlackToolset {
56    fn name(&self) -> &str {
57        "slack"
58    }
59
60    async fn tools(&self, _ctx: Arc<dyn ReadonlyContext>) -> Result<Vec<Arc<dyn Tool>>> {
61        let client = self.client.clone();
62        let token_source = self.token_source.clone();
63
64        Ok(vec![
65            Arc::new(SlackSendMessage::new(client.clone(), token_source.clone())),
66            Arc::new(SlackReadChannel::new(client.clone(), token_source.clone())),
67            Arc::new(SlackAddReaction::new(client.clone(), token_source.clone())),
68            Arc::new(SlackListThreads::new(client, token_source)),
69        ])
70    }
71}