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_manifest::profile::{SecretsConfig, SecretsSource, SecretsValidationMode};
11use systemprompt_manifest::services::SystemAdminConfig;
12use systemprompt_manifest::{
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_manifest::profile::StorageConfig::default(),
61            observability: systemprompt_manifest::profile::ObservabilityConfig::default(),
62            name: self.name,
63            display_name,
64            target: ProfileType::Local,
65            site: SiteConfig {
66                name: "systemprompt.io".to_owned(),
67                github_link: None,
68            },
69            database: ProfileDatabaseConfig {
70                db_type: consts::DEFAULT_DB_TYPE.to_owned(),
71                external_db_access: false,
72                migrate_on_boot: true,
73                pool: None,
74            },
75            server: local_server_config(local_url),
76            paths: PathsConfig {
77                system: system_path,
78                services: self.services_path,
79                bin: ExtensionLoader::resolve_bin_directory(root, None)
80                    .to_string_lossy()
81                    .to_string(),
82                storage: Some(ctx.storage_dir().to_string_lossy().to_string()),
83                geoip_database: None,
84                web_path: None,
85            },
86            security: security_config(consts::LOCAL_ISSUER, Vec::new()),
87            rate_limits: RateLimitsConfig {
88                disabled: true,
89                ..Default::default()
90            },
91            runtime: local_runtime_config(),
92            cloud: Some(CloudConfig {
93                tenant_id: self.tenant_id,
94                validation: CloudValidationMode::Warn,
95            }),
96            secrets: Some(SecretsConfig {
97                secrets_path: Some(self.secrets_path),
98                validation: SecretsValidationMode::Warn,
99                source: SecretsSource::File,
100                vault: None,
101            }),
102            extensions: ExtensionsConfig::default(),
103            governance: Some(webhook_governance(&internal_url)),
104            services: systemprompt_manifest::profile::ServicesProfileConfig::default(),
105            judge: systemprompt_manifest::profile::JudgeProfile::default(),
106            retention: systemprompt_manifest::profile::RetentionConfig::default(),
107            system_admin: SystemAdminConfig {
108                username: "admin".to_owned(),
109                email: Some(Email::local_admin()),
110            },
111        }
112    }
113}
114
115fn local_server_config(local_url: String) -> ServerConfig {
116    ServerConfig {
117        host: consts::LOCAL_HOST.to_owned(),
118        port: consts::DEFAULT_PORT,
119        api_server_url: local_url.clone(),
120        api_internal_url: local_url.clone(),
121        api_external_url: local_url.clone(),
122        use_https: false,
123        cors_allowed_origins: vec![local_url, "http://localhost:5173".to_owned()],
124        content_negotiation: ContentNegotiationConfig::default(),
125        security_headers: SecurityHeadersConfig::default(),
126        instance_id: None,
127        metrics_port: None,
128        max_concurrent_streams: systemprompt_manifest::config::DEFAULT_MAX_CONCURRENT_STREAMS,
129        role: systemprompt_manifest::profile::NodeRole::All,
130        max_in_flight: None,
131        trusted_proxies: crate::trusted_proxies::default_local_trusted_proxies(),
132    }
133}