Skip to main content

systemprompt_models/services/
slack.rs

1//! Declarative configuration for Slack apps.
2//!
3//! Each app describes one Slack workspace: the secret references for its
4//! signing secret and bot token, the agent it routes to, the roles permitted to
5//! drive it, and whether senders are linked to existing accounts by their
6//! workspace email. Secrets are never inlined — only references resolved
7//! through the profile's secret source at boot. This type lives in `models`
8//! (not the `slack` domain crate) so it can be embedded in
9//! [`super::ServicesConfig`] without a dependency cycle, mirroring
10//! `AgentConfig` and `McpServerSummary`.
11//!
12//! Copyright (c) systemprompt.io — Business Source License 1.1.
13//! See <https://systemprompt.io> for licensing details.
14
15use std::collections::BTreeMap;
16
17use schemars::JsonSchema;
18use serde::{Deserialize, Serialize};
19use systemprompt_identifiers::{AgentName, SecretName, SlackWorkspaceId};
20
21use crate::errors::ConfigValidationError;
22
23#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
24#[serde(deny_unknown_fields)]
25pub struct SlackAppConfig {
26    pub workspace_id: SlackWorkspaceId,
27    pub signing_secret_ref: SecretName,
28    pub bot_token_ref: SecretName,
29    #[serde(default = "default_enabled")]
30    pub enabled: bool,
31    #[serde(default, skip_serializing_if = "Option::is_none")]
32    pub default_agent: Option<AgentName>,
33    #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
34    pub routing: BTreeMap<String, AgentName>,
35    #[serde(default)]
36    pub authz: SlackAuthzConfig,
37}
38
39#[derive(Debug, Clone, Default, Serialize, Deserialize, JsonSchema)]
40#[serde(deny_unknown_fields)]
41pub struct SlackAuthzConfig {
42    #[serde(default)]
43    pub allowed_roles: Vec<String>,
44    // Why: Slack email lookup requires the `users:read.email` bot scope.
45    #[serde(default)]
46    pub link_by_workspace_email: bool,
47}
48
49const fn default_enabled() -> bool {
50    true
51}
52
53impl SlackAppConfig {
54    #[must_use]
55    pub fn agent_for(&self, key: &str) -> Option<&AgentName> {
56        self.routing.get(key).or(self.default_agent.as_ref())
57    }
58
59    pub fn validate(&self, name: &str) -> Result<(), ConfigValidationError> {
60        if self.workspace_id.as_str().is_empty() {
61            return Err(ConfigValidationError::invalid_field(format!(
62                "slack app '{name}' has an empty workspace_id"
63            )));
64        }
65        if self.default_agent.is_none() && self.routing.is_empty() {
66            return Err(ConfigValidationError::required(format!(
67                "slack app '{name}' must set default_agent or at least one routing entry"
68            )));
69        }
70        Ok(())
71    }
72}