Skip to main content

stackless_integrations/providers/agentphone/
number.rs

1//! `agentphone/number` integration.
2
3use std::collections::BTreeMap;
4
5use serde::Serialize;
6use stackless_stripe_projects::catalog::verify::CatalogService;
7use stackless_stripe_projects::provision::ProvisionContext;
8
9use super::FamilyResource;
10use crate::error::IntegrationError;
11use crate::hostable::{ConfigScope, Hostable, IntegrationHosting};
12use crate::registry;
13
14pub const RESOURCE_KIND: &str = "integration-agentphone";
15
16#[derive(Debug, Serialize)]
17pub struct AgentPhoneNumberConfig {
18    pub agent_name: String,
19    #[serde(skip_serializing_if = "Option::is_none")]
20    pub area_code: Option<String>,
21    #[serde(skip_serializing_if = "Option::is_none")]
22    pub country: Option<String>,
23}
24
25impl CatalogService for AgentPhoneNumberConfig {
26    const REFERENCE: &'static str = "agentphone/number";
27}
28
29#[derive(Debug)]
30pub struct AgentPhoneNumber;
31
32impl Hostable for AgentPhoneNumber {
33    const PROVIDER: &'static str = "agentphone";
34    const HOSTING: IntegrationHosting = IntegrationHosting::Managed;
35    const CONFIG_SCOPE: ConfigScope = ConfigScope::GlobalOnly;
36    const RESOURCE_KIND: &'static str = RESOURCE_KIND;
37    const OUTPUTS: &'static [&'static str] = &[
38        "agentphone_agent_id",
39        "agentphone_api_key",
40        "agentphone_base_url",
41        "agentphone_phone_number",
42    ];
43}
44
45impl FamilyResource for AgentPhoneNumber {
46    type Config = AgentPhoneNumberConfig;
47    const PROVIDER_PREFIX: &'static str = "AGENTPHONE";
48    const OUTPUT_FIELDS: &'static [(&'static str, &'static str, bool)] = &[
49        ("AGENTPHONE_AGENT_ID", "agentphone_agent_id", true),
50        ("AGENTPHONE_API_KEY", "agentphone_api_key", true),
51        ("AGENTPHONE_BASE_URL", "agentphone_base_url", true),
52        ("AGENTPHONE_PHONE_NUMBER", "agentphone_phone_number", true),
53    ];
54
55    fn build_config(
56        ctx: &ProvisionContext<'_>,
57    ) -> Result<AgentPhoneNumberConfig, IntegrationError> {
58        let config = super::integration_config(ctx)?;
59        Ok(AgentPhoneNumberConfig {
60            agent_name: super::interp_required(ctx, &config, "agent_name")?,
61            area_code: super::interp_optional(ctx, &config, "area_code")?,
62            country: super::interp_optional(ctx, &config, "country")?,
63        })
64    }
65}
66
67pub fn validate_config(
68    name: &str,
69    config: &BTreeMap<String, toml::Value>,
70) -> Result<(), IntegrationError> {
71    registry::config_string(config, "agent_name").map_err(|err| {
72        IntegrationError::ConfigInvalid {
73            location: format!("integrations.{name}.agent_name"),
74            detail: err.to_string(),
75        }
76    })?;
77    let _ = (name, config);
78    Ok(())
79}
80
81#[cfg(test)]
82mod tests {
83    use super::*;
84    use crate::ProviderOps;
85    use crate::resource::ResourcePayload;
86    use stackless_core::def::StackDef;
87    use stackless_stripe_projects::stripe::StripeProjects;
88    use stackless_stripe_projects::test_support;
89
90    #[test]
91    fn config_matches_catalog() {
92        const FIXTURE: &str = include_str!(concat!(
93            env!("CARGO_MANIFEST_DIR"),
94            "/../stackless-stripe-projects/tests/fixtures/catalog.json"
95        ));
96        let catalog = stackless_stripe_projects::Catalog::from_json_envelope(FIXTURE).unwrap();
97        let failures = stackless_stripe_projects::verify_service(
98            &catalog,
99            &AgentPhoneNumberConfig {
100                agent_name: "test-agent_name".into(),
101                area_code: None,
102                country: None,
103            },
104        );
105        assert!(
106            failures.is_empty(),
107            "agentphone/number catalog gaps:\n{}",
108            failures.join("\n")
109        );
110    }
111
112    const CATALOG_ENVELOPE: &str = r##"{"ok":true,"command":"projects catalog","data":{"last_updated":"2026-07-11T00:00:00Z","services":[{"id":"prvsvc_number","object":"v2.provisioning.provider_service_detail","provider_id":"prvdr_agentphone","provider_name":"AgentPhone","service_id":"number","categories":["database"],"kind":"deployable","scope":"project","availability":"available","development":false,"livemode":true,"pricing":{"type":"paid"},"configuration_schema":{"properties":{"agent_name":{"description":"Name for the AI agent","type":"string"},"area_code":{"description":"Preferred 3-digit area code (optional)","type":"string"},"country":{"description":"Country for the phone number","enum":["US","CA"],"type":"string"}},"required":["agent_name"],"type":"object"}}]}}"##;
113
114    fn test_def() -> StackDef {
115        StackDef::parse(
116            r#"
117[stack]
118name = "atto"
119[stack.projects.stripe]
120project = "project_1"
121[integrations.res]
122provider = "agentphone"
123agent_name = "test-agent_name"
124[services.api]
125source = { repo = "r", ref = "main" }
126env = { OUT = "${integrations.res.agentphone_agent_id}" }
127health = { path = "/health" }
128[services.api.local]
129run = "true"
130"#,
131        )
132        .unwrap()
133    }
134
135    #[tokio::test]
136    async fn provision_records_outputs() {
137        let runner = test_support::provision_script(
138            CATALOG_ENVELOPE,
139            serde_json::json!({"AGENTPHONE_AGENTPHONE_AGENT_ID": "val_agentphone_agent_id", "AGENTPHONE_AGENTPHONE_API_KEY": "val_agentphone_api_key", "AGENTPHONE_AGENTPHONE_BASE_URL": "val_agentphone_base_url", "AGENTPHONE_AGENTPHONE_PHONE_NUMBER": "val_agentphone_phone_number"}),
140            0,
141        );
142        let dir = tempfile::tempdir().unwrap();
143        std::fs::write(
144            dir.path().join("stackless.toml"),
145            "[stack]\nname=\"atto\"\n",
146        )
147        .unwrap();
148        let stripe = StripeProjects::new(&runner, dir.path());
149
150        let resource = AgentPhoneNumber
151            .provision(
152                &stripe.as_dyn(),
153                &test_def(),
154                dir.path(),
155                "demo",
156                "res",
157                "local",
158                false,
159            )
160            .await
161            .unwrap();
162        assert_eq!(resource.resource_kind, "integration-agentphone");
163        let payload: ResourcePayload = serde_json::from_str(&resource.payload).unwrap();
164        assert_eq!(
165            payload.outputs["agentphone_agent_id"],
166            "val_agentphone_agent_id"
167        );
168        assert_eq!(
169            payload.outputs["agentphone_api_key"],
170            "val_agentphone_api_key"
171        );
172        assert_eq!(
173            payload.outputs["agentphone_base_url"],
174            "val_agentphone_base_url"
175        );
176        assert_eq!(
177            payload.outputs["agentphone_phone_number"],
178            "val_agentphone_phone_number"
179        );
180    }
181}