chalk-client 0.1.0

Rust client SDK for the Chalk feature store
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
//! Configuration resolution for the Chalk client.
//!
//! Chalk credentials and endpoints can come from three places (checked in
//! this order):
//!
//! 1. **Explicit values** passed to the builder (highest priority).
//! 2. **Environment variables** like `CHALK_CLIENT_ID` and `_CHALK_CLIENT_ID`.
//! 3. **`~/.chalk.yml`** — a YAML file written by `chalk login`.
//! 4. **Defaults** — e.g. the API server defaults to `https://api.chalk.ai`.
//!
//! This module implements the builder pattern — you create a
//! [`ChalkClientConfigBuilder`](crate::config::ChalkClientConfigBuilder), set the fields you know, call `.build()`,
//! and the builder fills in the rest from env/file/defaults.

use std::collections::HashMap;
use std::path::PathBuf;

use serde::Deserialize;

use crate::error::{ChalkClientError, Result};

/// The default Chalk API server URL.
const DEFAULT_API_SERVER: &str = "https://api.chalk.ai";

/// Holds all resolved configuration needed to connect to Chalk.
///
/// You don't construct this directly — use [`ChalkClientConfigBuilder`].
#[derive(Debug, Clone)]
pub struct ChalkClientConfig {
    /// OAuth2 client ID (required).
    pub client_id: String,

    /// OAuth2 client secret (required).
    pub client_secret: String,

    /// The API server URL (e.g. `https://api.chalk.ai`).
    pub api_server: String,

    /// The target environment (e.g. `"production"` or an environment ID).
    pub environment: Option<String>,

    /// A branch ID for branch deployments.
    pub branch_id: Option<String>,

    /// A deployment tag for routing to specific deployments.
    pub deployment_tag: Option<String>,

    /// Override for the query server URL.
    pub query_server: Option<String>,
}

/// A builder for [`ChalkClientConfig`].
///
/// ## Example
///
/// ```rust,no_run
/// use chalk_client::config::ChalkClientConfigBuilder;
///
/// let config = ChalkClientConfigBuilder::new()
///     .client_id("my-client-id")
///     .client_secret("my-secret")
///     .environment("production")
///     .build()
///     .expect("failed to build config");
/// ```
#[derive(Debug, Default)]
pub struct ChalkClientConfigBuilder {
    client_id: Option<String>,
    client_secret: Option<String>,
    api_server: Option<String>,
    environment: Option<String>,
    branch_id: Option<String>,
    deployment_tag: Option<String>,
    query_server: Option<String>,
}

impl ChalkClientConfigBuilder {
    /// Create a new, empty builder.
    pub fn new() -> Self {
        Self::default()
    }

    /// Set the OAuth2 client ID.
    pub fn client_id(mut self, id: impl Into<String>) -> Self {
        self.client_id = Some(id.into());
        self
    }

    /// Set the OAuth2 client secret.
    pub fn client_secret(mut self, secret: impl Into<String>) -> Self {
        self.client_secret = Some(secret.into());
        self
    }

    /// Set the API server URL.
    pub fn api_server(mut self, url: impl Into<String>) -> Self {
        self.api_server = Some(url.into());
        self
    }

    /// Set the target environment.
    pub fn environment(mut self, env: impl Into<String>) -> Self {
        self.environment = Some(env.into());
        self
    }

    /// Set the branch ID.
    pub fn branch_id(mut self, id: impl Into<String>) -> Self {
        self.branch_id = Some(id.into());
        self
    }

    /// Set the deployment tag.
    pub fn deployment_tag(mut self, tag: impl Into<String>) -> Self {
        self.deployment_tag = Some(tag.into());
        self
    }

    /// Set the query server URL directly (skips engine-map resolution).
    pub fn query_server(mut self, url: impl Into<String>) -> Self {
        self.query_server = Some(url.into());
        self
    }

    /// Resolve all configuration and produce a [`ChalkClientConfig`].
    ///
    /// Returns an error if `client_id` or `client_secret` cannot be found
    /// from any source.
    pub fn build(self) -> Result<ChalkClientConfig> {
        let yaml_config = load_yaml_config();

        let client_id = self
            .client_id
            .or_else(|| get_env("CHALK_CLIENT_ID"))
            .or_else(|| get_env("_CHALK_CLIENT_ID"))
            .or_else(|| yaml_config.as_ref().map(|c| c.client_id.clone()))
            .ok_or_else(|| {
                ChalkClientError::Config(
                    "client_id is required — set it explicitly, via CHALK_CLIENT_ID env var, \
                     or by running `chalk login`"
                        .into(),
                )
            })?;

        let client_secret = self
            .client_secret
            .or_else(|| get_env("CHALK_CLIENT_SECRET"))
            .or_else(|| get_env("_CHALK_CLIENT_SECRET"))
            .or_else(|| yaml_config.as_ref().map(|c| c.client_secret.clone()))
            .ok_or_else(|| {
                ChalkClientError::Config(
                    "client_secret is required — set it explicitly, via CHALK_CLIENT_SECRET \
                     env var, or by running `chalk login`"
                        .into(),
                )
            })?;

        let api_server = self
            .api_server
            .or_else(|| get_env("CHALK_API_SERVER"))
            .or_else(|| get_env("_CHALK_API_SERVER"))
            .or_else(|| yaml_config.as_ref().and_then(|c| c.api_server.clone()))
            .unwrap_or_else(|| DEFAULT_API_SERVER.to_string());

        let environment = self
            .environment
            .or_else(|| get_env("CHALK_ACTIVE_ENVIRONMENT"))
            .or_else(|| get_env("_CHALK_ACTIVE_ENVIRONMENT"))
            .or_else(|| {
                yaml_config
                    .as_ref()
                    .and_then(|c| c.active_environment.clone())
            });

        let branch_id = self
            .branch_id
            .or_else(|| get_env("CHALK_BRANCH_ID"))
            .or_else(|| get_env("_CHALK_BRANCH_ID"));

        let deployment_tag = self
            .deployment_tag
            .or_else(|| get_env("CHALK_DEPLOYMENT_TAG"))
            .or_else(|| get_env("_CHALK_DEPLOYMENT_TAG"));

        let query_server = self
            .query_server
            .or_else(|| get_env("CHALK_QUERY_SERVER"))
            .or_else(|| get_env("_CHALK_QUERY_SERVER"));

        Ok(ChalkClientConfig {
            client_id,
            client_secret,
            api_server,
            environment,
            branch_id,
            deployment_tag,
            query_server,
        })
    }
}

/// Read an environment variable, returning `None` if it's unset or empty.
fn get_env(key: &str) -> Option<String> {
    std::env::var(key).ok().filter(|v| !v.is_empty())
}

/// The top-level structure of `~/.chalk.yml`.
#[derive(Debug, Deserialize)]
struct YamlConfig {
    #[serde(default)]
    tokens: HashMap<String, YamlProjectToken>,
}

/// Credentials for a single project in the YAML config.
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
struct YamlProjectToken {
    client_id: String,
    client_secret: String,
    #[serde(default)]
    api_server: Option<String>,
    #[serde(default)]
    active_environment: Option<String>,
}

/// Try to find and parse `~/.chalk.yml` (or `~/.chalk.yaml`).
fn load_yaml_config() -> Option<YamlProjectToken> {
    let home = dirs::home_dir()?;
    let path = find_config_file(&home)?;
    let contents = std::fs::read_to_string(&path).ok()?;
    let config: YamlConfig = serde_yaml::from_str(&contents).ok()?;

    // 1. Try a directory containing chalk.yml/chalk.yaml (project marker).
    if let Some(root) = find_project_root() {
        if let Some(token) = config.tokens.get(&root) {
            return Some(token.clone());
        }
    }

    // 2. Walk up from CWD looking for a matching key in ~/.chalk.yml.
    //    This handles `chalk login` / `chalkadmin customer login`, which
    //    store credentials keyed by the working directory.
    if let Ok(mut dir) = std::env::current_dir() {
        loop {
            let key = dir.to_string_lossy().into_owned();
            if let Some(token) = config.tokens.get(&key) {
                return Some(token.clone());
            }
            if !dir.pop() {
                break;
            }
        }
    }

    // 3. Fall back to the "default" key.
    config.tokens.get("default").cloned()
}

/// Look for `.chalk.yml` or `.chalk.yaml` in the home directory.
fn find_config_file(home: &std::path::Path) -> Option<PathBuf> {
    let yml = home.join(".chalk.yml");
    if yml.exists() {
        return Some(yml);
    }

    let yaml = home.join(".chalk.yaml");
    if yaml.exists() {
        return Some(yaml);
    }

    if let Some(xdg) = get_env("XDG_CONFIG_HOME") {
        let xdg_path = PathBuf::from(xdg).join(".chalk.yml");
        if xdg_path.exists() {
            return Some(xdg_path);
        }
    }

    None
}

/// Walk up from the current directory looking for `chalk.yml` or `chalk.yaml`.
fn find_project_root() -> Option<String> {
    let mut dir = std::env::current_dir().ok()?;
    loop {
        if dir.join("chalk.yml").exists() || dir.join("chalk.yaml").exists() {
            return Some(dir.to_string_lossy().into_owned());
        }
        if !dir.pop() {
            break;
        }
    }
    None
}

impl Clone for YamlProjectToken {
    fn clone(&self) -> Self {
        Self {
            client_id: self.client_id.clone(),
            client_secret: self.client_secret.clone(),
            api_server: self.api_server.clone(),
            active_environment: self.active_environment.clone(),
        }
    }
}

/// Prepend `https://` if the URL has no scheme. The token response may
/// return bare hostnames for engine URLs (e.g. `host.chalk.ai:443`).
pub(crate) fn ensure_scheme(url: String) -> String {
    if url.starts_with("http://") || url.starts_with("https://") || url.is_empty() {
        url
    } else {
        format!("https://{}", url)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::sync::Mutex;

    /// Tests in this module mutate process-global env vars (`CHALK_*`, `HOME`).
    /// Acquiring this lock before touching env state prevents races when cargo
    /// runs tests in parallel.
    static ENV_LOCK: Mutex<()> = Mutex::new(());

    const CHALK_ENV_VARS: &[&str] = &[
        "CHALK_CLIENT_ID",
        "_CHALK_CLIENT_ID",
        "CHALK_CLIENT_SECRET",
        "_CHALK_CLIENT_SECRET",
        "CHALK_API_SERVER",
        "_CHALK_API_SERVER",
        "CHALK_ACTIVE_ENVIRONMENT",
        "_CHALK_ACTIVE_ENVIRONMENT",
        "CHALK_BRANCH_ID",
        "_CHALK_BRANCH_ID",
        "CHALK_DEPLOYMENT_TAG",
        "_CHALK_DEPLOYMENT_TAG",
        "CHALK_QUERY_SERVER",
        "_CHALK_QUERY_SERVER",
    ];

    fn clear_chalk_env() {
        for var in CHALK_ENV_VARS {
            std::env::remove_var(var);
        }
    }

    #[test]
    fn test_builder_explicit_values() {
        let config = ChalkClientConfigBuilder::new()
            .client_id("test-id")
            .client_secret("test-secret")
            .api_server("https://custom.chalk.ai")
            .environment("staging")
            .branch_id("branch-1")
            .deployment_tag("canary")
            .query_server("https://query.chalk.ai")
            .build()
            .unwrap();

        assert_eq!(config.client_id, "test-id");
        assert_eq!(config.client_secret, "test-secret");
        assert_eq!(config.api_server, "https://custom.chalk.ai");
        assert_eq!(config.environment.as_deref(), Some("staging"));
        assert_eq!(config.branch_id.as_deref(), Some("branch-1"));
        assert_eq!(config.deployment_tag.as_deref(), Some("canary"));
        assert_eq!(
            config.query_server.as_deref(),
            Some("https://query.chalk.ai")
        );
    }

    #[test]
    fn test_builder_default_api_server() {
        let _lock = ENV_LOCK.lock().unwrap();
        clear_chalk_env();

        let config = ChalkClientConfigBuilder::new()
            .client_id("id")
            .client_secret("secret")
            .api_server(DEFAULT_API_SERVER)
            .build()
            .unwrap();

        assert_eq!(config.api_server, DEFAULT_API_SERVER);
    }

    #[test]
    fn test_builder_missing_credentials() {
        let _lock = ENV_LOCK.lock().unwrap();
        clear_chalk_env();
        // Use a real but empty temp dir so dirs::home_dir() resolves it
        // but no .chalk.yml exists inside it.
        let tmp = std::env::temp_dir().join("chalk_test_no_yaml_creds");
        let _ = std::fs::create_dir_all(&tmp);
        let original_home = std::env::var("HOME").ok();
        std::env::set_var("HOME", &tmp);

        // Missing client_id
        let result = ChalkClientConfigBuilder::new()
            .client_secret("secret")
            .build();
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("client_id"));

        // Missing client_secret
        let result = ChalkClientConfigBuilder::new()
            .client_id("id")
            .build();
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("client_secret"));

        if let Some(h) = original_home {
            std::env::set_var("HOME", h);
        }
        let _ = std::fs::remove_dir_all(&tmp);
    }

    #[test]
    fn test_get_env_helper() {
        let var = "_CHALK_TEST_GET_ENV_HELPER";

        std::env::remove_var(var);
        assert_eq!(get_env(var), None);

        std::env::set_var(var, "");
        assert_eq!(get_env(var), None);

        std::env::set_var(var, "hello");
        assert_eq!(get_env(var), Some("hello".to_string()));

        std::env::remove_var(var);
    }

    #[test]
    fn test_builder_explicit_overrides_env() {
        let _lock = ENV_LOCK.lock().unwrap();
        clear_chalk_env();

        std::env::set_var("CHALK_CLIENT_ID", "env-id");
        std::env::set_var("CHALK_CLIENT_SECRET", "env-secret");

        let config = ChalkClientConfigBuilder::new()
            .client_id("explicit-id")
            .client_secret("explicit-secret")
            .build()
            .unwrap();

        assert_eq!(config.client_id, "explicit-id");
        assert_eq!(config.client_secret, "explicit-secret");

        clear_chalk_env();
    }

    #[test]
    fn test_yaml_config_parsing() {
        let yaml = r#"
tokens:
  default:
    clientId: "yaml-id"
    clientSecret: "yaml-secret"
    apiServer: "https://yaml.chalk.ai"
    activeEnvironment: "yaml-env"
"#;

        let config: YamlConfig = serde_yaml::from_str(yaml).unwrap();
        let token = config.tokens.get("default").unwrap();

        assert_eq!(token.client_id, "yaml-id");
        assert_eq!(token.client_secret, "yaml-secret");
        assert_eq!(token.api_server.as_deref(), Some("https://yaml.chalk.ai"));
        assert_eq!(token.active_environment.as_deref(), Some("yaml-env"));
    }
}