1use std::env;
2
3use crate::client::ClawDB;
4use crate::error::{SdkError, SdkResult};
5
6#[derive(Debug, Default)]
8pub struct ClawDBBuilder {
9 endpoint: Option<String>,
10 api_key: Option<String>,
11 agent_id: Option<String>,
12 workspace: Option<String>,
13 role: Option<String>,
14 timeout_ms: Option<u64>,
15 tls: Option<bool>,
16}
17
18impl ClawDBBuilder {
19 pub fn new() -> Self {
20 Self::default()
21 }
22
23 pub fn endpoint(mut self, endpoint: impl Into<String>) -> Self {
24 self.endpoint = Some(endpoint.into());
25 self
26 }
27
28 pub fn api_key(mut self, api_key: impl Into<String>) -> Self {
29 self.api_key = Some(api_key.into());
30 self
31 }
32
33 pub fn agent_id(mut self, agent_id: impl Into<String>) -> Self {
34 self.agent_id = Some(agent_id.into());
35 self
36 }
37
38 pub fn workspace(mut self, workspace: impl Into<String>) -> Self {
39 self.workspace = Some(workspace.into());
40 self
41 }
42
43 pub fn role(mut self, role: impl Into<String>) -> Self {
44 self.role = Some(role.into());
45 self
46 }
47
48 pub fn timeout_ms(mut self, ms: u64) -> Self {
49 self.timeout_ms = Some(ms);
50 self
51 }
52
53 pub fn tls(mut self, enabled: bool) -> Self {
54 self.tls = Some(enabled);
55 self
56 }
57
58 pub fn from_env() -> Self {
60 Self {
61 endpoint: env::var("CLAWDB_ENDPOINT").ok(),
62 api_key: env::var("CLAWDB_API_KEY").ok(),
63 agent_id: env::var("CLAWDB_AGENT_ID").ok(),
64 workspace: env::var("CLAWDB_WORKSPACE").ok(),
65 role: env::var("CLAWDB_ROLE").ok(),
66 timeout_ms: env::var("CLAWDB_TIMEOUT_MS").ok().and_then(|v| v.parse().ok()),
67 tls: env::var("CLAWDB_TLS").ok().map(|v| v == "true" || v == "1"),
68 }
69 }
70
71 pub async fn build(self) -> SdkResult<ClawDB> {
73 let endpoint = self.endpoint.unwrap_or_else(|| "http://localhost:50050".into());
74 let api_key = self.api_key.unwrap_or_default();
75 let agent_id = self.agent_id.unwrap_or_else(|| "default-agent".into());
76 let workspace = self.workspace.unwrap_or_else(|| "default".into());
77 let role = self.role.unwrap_or_else(|| "assistant".into());
78 let timeout_ms = self.timeout_ms.unwrap_or(30_000);
79
80 let _ = url::Url::parse(&endpoint).map_err(|e| SdkError::InvalidUrl(e))?;
82
83 ClawDB::new_internal(endpoint, api_key, agent_id, workspace, role, timeout_ms).await
84 }
85}