stackless_integrations/providers/chatbase/
agent.rs1use 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-chatbase";
15
16#[derive(Debug, Serialize)]
17pub struct ChatbaseAgentConfig {
18 pub name: String,
19}
20
21impl CatalogService for ChatbaseAgentConfig {
22 const REFERENCE: &'static str = "chatbase/agent";
23}
24
25#[derive(Debug)]
26pub struct ChatbaseAgent;
27
28impl Hostable for ChatbaseAgent {
29 const PROVIDER: &'static str = "chatbase";
30 const HOSTING: IntegrationHosting = IntegrationHosting::Managed;
31 const CONFIG_SCOPE: ConfigScope = ConfigScope::GlobalOnly;
32 const RESOURCE_KIND: &'static str = RESOURCE_KIND;
33 const OUTPUTS: &'static [&'static str] = &["api_key"];
34}
35
36impl FamilyResource for ChatbaseAgent {
37 type Config = ChatbaseAgentConfig;
38 const PROVIDER_PREFIX: &'static str = "CHATBASE";
39 const OUTPUT_FIELDS: &'static [(&'static str, &'static str, bool)] =
41 &[("API_KEY", "api_key", true)];
42
43 fn build_config(ctx: &ProvisionContext<'_>) -> Result<ChatbaseAgentConfig, IntegrationError> {
44 let config = super::integration_config(ctx)?;
45 Ok(ChatbaseAgentConfig {
46 name: super::interp_required(ctx, &config, "name")?,
47 })
48 }
49}
50
51pub fn validate_config(
52 name: &str,
53 config: &BTreeMap<String, toml::Value>,
54) -> Result<(), IntegrationError> {
55 registry::config_string(config, "name").map_err(|err| IntegrationError::ConfigInvalid {
56 location: format!("integrations.{name}.name"),
57 detail: err.to_string(),
58 })?;
59 let _ = (name, config);
60 Ok(())
61}
62
63#[cfg(test)]
64mod tests {
65 use super::*;
66 use crate::ProviderOps;
67 use crate::resource::ResourcePayload;
68 use stackless_core::def::StackDef;
69 use stackless_stripe_projects::stripe::StripeProjects;
70 use stackless_stripe_projects::test_support;
71
72 #[test]
73 fn config_matches_catalog() {
74 const FIXTURE: &str = include_str!(concat!(
75 env!("CARGO_MANIFEST_DIR"),
76 "/../stackless-stripe-projects/tests/fixtures/catalog.json"
77 ));
78 let catalog = stackless_stripe_projects::Catalog::from_json_envelope(FIXTURE).unwrap();
79 let failures = stackless_stripe_projects::verify_service(
80 &catalog,
81 &ChatbaseAgentConfig {
82 name: "test-name".into(),
83 },
84 );
85 assert!(
86 failures.is_empty(),
87 "chatbase/agent catalog gaps:\n{}",
88 failures.join("\n")
89 );
90 }
91
92 const CATALOG_ENVELOPE: &str = r##"{"ok":true,"command":"projects catalog","data":{"last_updated":"2026-07-11T00:00:00Z","services":[{"id":"prvsvc_agent","object":"v2.provisioning.provider_service_detail","provider_id":"prvdr_chatbase","provider_name":"Chatbase","service_id":"agent","categories":["database"],"kind":"deployable","scope":"project","availability":"available","development":false,"livemode":true,"pricing":{"type":"component"},"configuration_schema":{"properties":{"name":{"type":"string"}},"type":"object","required":["name"]}}]}}"##;
93
94 fn test_def() -> StackDef {
95 StackDef::parse(
96 r#"
97[stack]
98name = "atto"
99[stack.projects.stripe]
100project = "project_1"
101[integrations.res]
102provider = "chatbase"
103name = "demo-name"
104[services.api]
105source = { repo = "r", ref = "main" }
106env = { OUT = "${integrations.res.api_key}" }
107health = { path = "/health" }
108[services.api.local]
109run = "true"
110"#,
111 )
112 .unwrap()
113 }
114
115 #[tokio::test]
116 async fn provision_records_outputs() {
117 let runner = test_support::provision_script(
118 CATALOG_ENVELOPE,
119 serde_json::json!({"CHATBASE_API_KEY": "val_api_key"}),
120 0,
121 );
122 let dir = tempfile::tempdir().unwrap();
123 std::fs::write(
124 dir.path().join("stackless.toml"),
125 "[stack]\nname=\"atto\"\n",
126 )
127 .unwrap();
128 let stripe = StripeProjects::new(&runner, dir.path());
129
130 let resource = ChatbaseAgent
131 .provision(
132 &stripe.as_dyn(),
133 &test_def(),
134 dir.path(),
135 "demo",
136 "res",
137 "local",
138 false,
139 )
140 .await
141 .unwrap();
142 assert_eq!(resource.resource_kind, "integration-chatbase");
143 let payload: ResourcePayload = serde_json::from_str(&resource.payload).unwrap();
144 assert_eq!(payload.outputs["api_key"], "val_api_key");
145 }
146}