Skip to main content

systemprompt_agent/services/shared/
config.rs

1use crate::services::shared::error::{AgentServiceError, Result};
2use serde::{Deserialize, Serialize};
3use std::time::Duration;
4use systemprompt_identifiers::AgentId;
5
6#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
7pub struct ServiceConfiguration {
8    pub enabled: bool,
9    pub timeout_seconds: u64,
10    pub retry_attempts: u32,
11    pub retry_delay_milliseconds: u64,
12    pub max_connections: usize,
13}
14
15impl ServiceConfiguration {
16    pub const fn timeout(&self) -> Duration {
17        Duration::from_secs(self.timeout_seconds)
18    }
19
20    pub const fn retry_delay(&self) -> Duration {
21        Duration::from_millis(self.retry_delay_milliseconds)
22    }
23
24    pub fn validate(&self) -> Result<()> {
25        if self.retry_attempts == 0 {
26            return Err(AgentServiceError::Configuration(
27                "ServiceConfiguration".to_string(),
28                "retry_attempts must be at least 1".to_string(),
29            ));
30        }
31        if self.max_connections == 0 {
32            return Err(AgentServiceError::Configuration(
33                "ServiceConfiguration".to_string(),
34                "max_connections must be at least 1".to_string(),
35            ));
36        }
37        Ok(())
38    }
39}
40
41impl Default for ServiceConfiguration {
42    fn default() -> Self {
43        Self {
44            enabled: true,
45            timeout_seconds: 30,
46            retry_attempts: 3,
47            retry_delay_milliseconds: 500,
48            max_connections: 10,
49        }
50    }
51}
52
53#[derive(Debug, Clone, Serialize, Deserialize)]
54pub struct RuntimeConfiguration {
55    pub agent_id: AgentId,
56    pub name: String,
57    pub port: u16,
58    pub host: String,
59    pub ssl_enabled: bool,
60    pub auth_required: bool,
61    pub system_prompt: Option<String>,
62}
63
64#[derive(Debug, Clone)]
65pub struct RuntimeConfigurationBuilder {
66    agent_id: AgentId,
67    name: String,
68    port: u16,
69    host: String,
70    ssl_enabled: bool,
71    auth_required: bool,
72    system_prompt: Option<String>,
73}
74
75impl RuntimeConfigurationBuilder {
76    pub fn new(agent_id: AgentId, name: String) -> Self {
77        Self {
78            agent_id,
79            name,
80            port: 8080,
81            host: "localhost".to_string(),
82            ssl_enabled: false,
83            auth_required: false,
84            system_prompt: None,
85        }
86    }
87
88    pub const fn port(mut self, port: u16) -> Self {
89        self.port = port;
90        self
91    }
92
93    pub fn host(mut self, host: String) -> Self {
94        self.host = host;
95        self
96    }
97
98    pub const fn enable_ssl(mut self) -> Self {
99        self.ssl_enabled = true;
100        self
101    }
102
103    pub const fn require_auth(mut self) -> Self {
104        self.auth_required = true;
105        self
106    }
107
108    pub fn system_prompt(mut self, prompt: String) -> Self {
109        self.system_prompt = Some(prompt);
110        self
111    }
112
113    pub fn build(self) -> RuntimeConfiguration {
114        RuntimeConfiguration {
115            agent_id: self.agent_id,
116            name: self.name,
117            port: self.port,
118            host: self.host,
119            ssl_enabled: self.ssl_enabled,
120            auth_required: self.auth_required,
121            system_prompt: self.system_prompt,
122        }
123    }
124}
125
126#[derive(Debug, Clone, Serialize, Deserialize)]
127pub struct ConnectionConfiguration {
128    pub url: String,
129    pub timeout_seconds: u64,
130    pub keepalive_enabled: bool,
131    pub pool_size: usize,
132}
133
134impl ConnectionConfiguration {
135    pub const fn timeout(&self) -> Duration {
136        Duration::from_secs(self.timeout_seconds)
137    }
138}
139
140pub trait ConfigValidation {
141    fn validate(&self) -> Result<()>;
142}
143
144#[derive(Debug, Clone, Serialize, Deserialize)]
145pub struct AgentServiceConfig {
146    pub agent_id: AgentId,
147    pub name: String,
148    pub description: String,
149    pub version: String,
150    pub endpoint: String,
151    pub port: u16,
152    pub is_active: bool,
153}
154
155impl ConfigValidation for AgentServiceConfig {
156    fn validate(&self) -> Result<()> {
157        if self.agent_id.as_str().is_empty() {
158            return Err(AgentServiceError::Validation(
159                "agent_id".to_string(),
160                "cannot be empty".to_string(),
161            ));
162        }
163        if self.port == 0 {
164            return Err(AgentServiceError::Validation(
165                "port".to_string(),
166                "must be greater than 0".to_string(),
167            ));
168        }
169        if self.name.is_empty() {
170            return Err(AgentServiceError::Validation(
171                "name".to_string(),
172                "cannot be empty".to_string(),
173            ));
174        }
175        Ok(())
176    }
177}
178
179impl Default for AgentServiceConfig {
180    fn default() -> Self {
181        Self {
182            agent_id: AgentId::generate(),
183            name: "Default Agent".to_string(),
184            description: "Default agent instance".to_string(),
185            version: "0.1.0".to_string(),
186            endpoint: "http://localhost:8080".to_string(),
187            port: 8080,
188            is_active: true,
189        }
190    }
191}