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            evaluator: None,
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                pool: None,
73            },
74            server: ServerConfig {
75                host: consts::LOCAL_HOST.to_owned(),
76                port: consts::DEFAULT_PORT,
77                api_server_url: local_url.clone(),
78                api_internal_url: local_url.clone(),
79                api_external_url: local_url.clone(),
80                use_https: false,
81                cors_allowed_origins: vec![local_url, "http://localhost:5173".to_owned()],
82                content_negotiation: ContentNegotiationConfig::default(),
83                security_headers: SecurityHeadersConfig::default(),
84                instance_id: None,
85                metrics_port: None,
86                max_concurrent_streams: systemprompt_models::config::DEFAULT_MAX_CONCURRENT_STREAMS,
87                trusted_proxies: crate::trusted_proxies::default_local_trusted_proxies(),
88            },
89            paths: PathsConfig {
90                system: system_path,
91                services: self.services_path,
92                bin: ExtensionLoader::resolve_bin_directory(root, None)
93                    .to_string_lossy()
94                    .to_string(),
95                storage: Some(ctx.storage_dir().to_string_lossy().to_string()),
96                geoip_database: None,
97                web_path: None,
98            },
99            security: security_config(consts::LOCAL_ISSUER, Vec::new()),
100            rate_limits: RateLimitsConfig {
101                disabled: true,
102                ..Default::default()
103            },
104            runtime: local_runtime_config(),
105            cloud: Some(CloudConfig {
106                tenant_id: self.tenant_id,
107                validation: CloudValidationMode::Warn,
108            }),
109            secrets: Some(SecretsConfig {
110                secrets_path: Some(self.secrets_path),
111                validation: SecretsValidationMode::Warn,
112                source: SecretsSource::File,
113                vault: None,
114            }),
115            extensions: ExtensionsConfig::default(),
116            governance: Some(webhook_governance(&internal_url)),
117            services: systemprompt_models::profile::ServicesProfileConfig::default(),
118            system_admin: SystemAdminConfig {
119                username: "admin".to_owned(),
120                email: Some(Email::local_admin()),
121            },
122        }
123    }
124}