Skip to main content

systemprompt_cloud/profile_authoring/
local_builder.rs

1//! Builder for the local-development [`Profile`].
2//!
3//! Copyright (c) systemprompt.io — Business Source License 1.1.
4//! See <https://systemprompt.io> for licensing details.
5
6use std::path::Path;
7
8use systemprompt_identifiers::{Email, TenantId};
9use systemprompt_loader::ExtensionLoader;
10use systemprompt_models::profile::{SecretsConfig, SecretsSource, SecretsValidationMode};
11use systemprompt_models::services::SystemAdminConfig;
12use systemprompt_models::{
13    CloudConfig, CloudValidationMode, ContentNegotiationConfig, ExtensionsConfig, PathsConfig,
14    Profile, ProfileDatabaseConfig, ProfileType, RateLimitsConfig, SecurityHeadersConfig,
15    ServerConfig, SiteConfig,
16};
17
18use super::{generate_display_name, local_runtime_config, security_config, webhook_governance};
19use crate::constants::profile as consts;
20use crate::paths::ProjectContext;
21
22#[derive(Debug)]
23pub struct LocalProfileBuilder {
24    name: String,
25    tenant_id: Option<TenantId>,
26    secrets_path: String,
27    services_path: String,
28}
29
30impl LocalProfileBuilder {
31    pub fn new(
32        name: impl Into<String>,
33        secrets_path: impl AsRef<Path>,
34        services_path: impl AsRef<Path>,
35    ) -> Self {
36        Self {
37            name: name.into(),
38            tenant_id: None,
39            secrets_path: secrets_path.as_ref().to_string_lossy().to_string(),
40            services_path: services_path.as_ref().to_string_lossy().to_string(),
41        }
42    }
43
44    #[must_use]
45    pub fn with_tenant_id(mut self, tenant_id: TenantId) -> Self {
46        self.tenant_id = Some(tenant_id);
47        self
48    }
49
50    #[must_use]
51    pub fn build(self) -> Profile {
52        let ctx = ProjectContext::discover();
53        let root = ctx.root();
54        let system_path = root.to_string_lossy().to_string();
55        let display_name = generate_display_name(&self.name);
56        let local_url = format!("http://localhost:{}", consts::DEFAULT_PORT);
57        let internal_url = local_url.clone();
58
59        Profile {
60            storage: systemprompt_models::profile::StorageConfig::default(),
61            name: self.name,
62            display_name,
63            target: ProfileType::Local,
64            site: SiteConfig {
65                name: "systemprompt.io".to_owned(),
66                github_link: None,
67            },
68            database: ProfileDatabaseConfig {
69                db_type: consts::DEFAULT_DB_TYPE.to_owned(),
70                external_db_access: false,
71                pool: None,
72            },
73            server: ServerConfig {
74                host: consts::LOCAL_HOST.to_owned(),
75                port: consts::DEFAULT_PORT,
76                api_server_url: local_url.clone(),
77                api_internal_url: local_url.clone(),
78                api_external_url: local_url.clone(),
79                use_https: false,
80                cors_allowed_origins: vec![local_url, "http://localhost:5173".to_owned()],
81                content_negotiation: ContentNegotiationConfig::default(),
82                security_headers: SecurityHeadersConfig::default(),
83                instance_id: None,
84                metrics_port: None,
85                max_concurrent_streams: systemprompt_models::config::DEFAULT_MAX_CONCURRENT_STREAMS,
86                trusted_proxies: crate::trusted_proxies::default_local_trusted_proxies(),
87            },
88            paths: PathsConfig {
89                system: system_path,
90                services: self.services_path,
91                bin: ExtensionLoader::resolve_bin_directory(root, None)
92                    .to_string_lossy()
93                    .to_string(),
94                storage: Some(ctx.storage_dir().to_string_lossy().to_string()),
95                geoip_database: None,
96                web_path: None,
97            },
98            security: security_config(consts::LOCAL_ISSUER, Vec::new()),
99            rate_limits: RateLimitsConfig {
100                disabled: true,
101                ..Default::default()
102            },
103            runtime: local_runtime_config(),
104            cloud: Some(CloudConfig {
105                tenant_id: self.tenant_id,
106                validation: CloudValidationMode::Warn,
107            }),
108            secrets: Some(SecretsConfig {
109                secrets_path: self.secrets_path,
110                validation: SecretsValidationMode::Warn,
111                source: SecretsSource::File,
112            }),
113            extensions: ExtensionsConfig::default(),
114            governance: Some(webhook_governance(&internal_url)),
115            services: systemprompt_models::profile::ServicesProfileConfig::default(),
116            system_admin: SystemAdminConfig {
117                username: "admin".to_owned(),
118                email: Some(Email::new("admin@localhost.localdomain")),
119            },
120        }
121    }
122}