oxibonsai-serve 0.1.4

Standalone OpenAI-compatible inference server for OxiBonsai
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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
//! Layered server configuration.
//!
//! `ServerConfig` is the production-ready configuration object for
//! `oxibonsai-serve`.  It is built from up to four layers, each overriding the
//! previous one on a per-field basis:
//!
//! 1. **Defaults** — [`ServerConfig::default`] (baked in)
//! 2. **TOML file** — optional `--config <PATH>`
//! 3. **Environment variables** — `OXIBONSAI_*` prefix (see [`crate::env`])
//! 4. **CLI arguments** — [`crate::args::ServerArgs`] (highest precedence)
//!
//! Each layer is represented as a [`PartialServerConfig`] where every field is
//! an `Option`.  Merging is then a trivial `if Some(x) { self.x = Some(x) }`
//! pattern.  The final conversion to a concrete `ServerConfig` happens once at
//! the top level via [`ServerConfig::from_partial`].
//!
//! This scheme makes it easy to test each layer in isolation and to prove the
//! layering identity (see `tests/config_tests.rs` and
//! `tests/property_tests.rs`).

use serde::{Deserialize, Serialize};
use std::path::PathBuf;
use thiserror::Error;

// ─── Errors ──────────────────────────────────────────────────────────────

/// Errors arising while loading, parsing or validating a configuration.
#[derive(Debug, Error)]
pub enum ConfigError {
    /// The supplied TOML string could not be parsed.
    #[error("failed to parse TOML config: {0}")]
    TomlParse(String),

    /// An environment variable could not be interpreted.
    #[error("failed to parse environment variable {name}: {reason}")]
    EnvParse {
        /// The name of the offending variable.
        name: String,
        /// Human-readable explanation of the parse failure.
        reason: String,
    },

    /// A validation rule was violated.
    #[error("configuration validation failed: {0}")]
    Validation(String),

    /// The config file could not be read from disk.
    #[error("failed to read config file {path}: {source}")]
    Io {
        /// Path of the file that could not be read.
        path: String,
        /// Underlying I/O error.
        #[source]
        source: std::io::Error,
    },
}

// ─── Sub-sections ────────────────────────────────────────────────────────

/// Bind-address section of the config.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct BindConfig {
    /// Host or interface to bind to.
    pub host: String,
    /// TCP port to listen on.
    pub port: u16,
}

impl Default for BindConfig {
    fn default() -> Self {
        Self {
            host: "0.0.0.0".to_string(),
            port: 8080,
        }
    }
}

/// Model-file section of the config.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct ModelConfig {
    /// Optional path to a GGUF file.
    pub path: Option<PathBuf>,
    /// Optional quantization hint (e.g. "TQ2" or "Q8_0").
    pub quantization_hint: Option<String>,
}

/// Tokenizer section of the config.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct TokenizerConfigSection {
    /// Optional path to a tokenizer.json file.
    pub path: Option<PathBuf>,
    /// Tokenizer kind — e.g. "huggingface" or "oxitok".
    pub kind: Option<String>,
}

/// Sampling-defaults section.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct SamplingConfig {
    /// Default maximum tokens to generate.
    pub default_max_tokens: usize,
    /// Default temperature.
    pub default_temperature: f32,
    /// Default top-p.
    pub default_top_p: f32,
}

impl Default for SamplingConfig {
    fn default() -> Self {
        Self {
            default_max_tokens: 256,
            default_temperature: 0.7,
            default_top_p: 1.0,
        }
    }
}

/// Resource-limit section.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct LimitsConfig {
    /// Maximum prompt length (in tokens).
    pub max_input_tokens: usize,
    /// Maximum number of concurrent requests.
    pub max_concurrent_requests: usize,
    /// Per-request timeout, in milliseconds.
    pub per_request_timeout_ms: u64,
}

impl Default for LimitsConfig {
    fn default() -> Self {
        Self {
            max_input_tokens: 8192,
            max_concurrent_requests: 32,
            per_request_timeout_ms: 60_000,
        }
    }
}

/// Authentication section.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct AuthConfig {
    /// Optional bearer token required on auth-protected endpoints.
    pub bearer_token: Option<String>,
}

/// Observability section.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct ObservabilityConfig {
    /// Log level (one of: error/warn/info/debug/trace/off).
    pub log_level: String,
    /// Whether Prometheus metrics are enabled.
    pub metrics_enabled: bool,
    /// Path to serve Prometheus metrics at.
    pub metrics_path: String,
}

impl Default for ObservabilityConfig {
    fn default() -> Self {
        Self {
            log_level: "info".to_string(),
            metrics_enabled: true,
            metrics_path: "/metrics".to_string(),
        }
    }
}

// ─── Top-level config ────────────────────────────────────────────────────

/// Production-ready server configuration.
///
/// Obtain an instance with [`ServerConfig::load`] or via the explicit
/// layering helpers ([`ServerConfig::from_partial`],
/// [`PartialServerConfig::merge`]).
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct ServerConfig {
    /// Bind-address section.
    #[serde(default)]
    pub bind: BindConfig,
    /// Model section.
    #[serde(default)]
    pub model: ModelConfig,
    /// Tokenizer section.
    #[serde(default)]
    pub tokenizer: TokenizerConfigSection,
    /// Sampling defaults.
    #[serde(default)]
    pub sampling: SamplingConfig,
    /// Resource limits.
    #[serde(default)]
    pub limits: LimitsConfig,
    /// Authentication.
    #[serde(default)]
    pub auth: AuthConfig,
    /// Observability.
    #[serde(default)]
    pub observability: ObservabilityConfig,
    /// RNG seed (for deterministic sampling).
    #[serde(default = "default_seed")]
    pub seed: u64,
}

fn default_seed() -> u64 {
    42
}

impl Default for ServerConfig {
    fn default() -> Self {
        Self {
            bind: BindConfig::default(),
            model: ModelConfig::default(),
            tokenizer: TokenizerConfigSection::default(),
            sampling: SamplingConfig::default(),
            limits: LimitsConfig::default(),
            auth: AuthConfig::default(),
            observability: ObservabilityConfig::default(),
            seed: default_seed(),
        }
    }
}

// ─── Partial config (for layering) ───────────────────────────────────────

/// Partial counterpart to [`ServerConfig`] where every field is optional.
///
/// This is the shape used by the TOML/env/CLI loading layers and by
/// [`PartialServerConfig::merge`].  Unset fields leave the previously layered
/// value untouched.
///
/// Deliberately *not* `#[non_exhaustive]` so downstream crates and integration
/// tests can construct partials with struct-expression syntax.
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct PartialServerConfig {
    /// Bind host.
    pub host: Option<String>,
    /// Bind port.
    pub port: Option<u16>,
    /// Model path.
    pub model_path: Option<PathBuf>,
    /// Quantization hint.
    pub quantization_hint: Option<String>,
    /// Tokenizer path.
    pub tokenizer_path: Option<PathBuf>,
    /// Tokenizer kind.
    pub tokenizer_kind: Option<String>,
    /// Default max tokens.
    pub default_max_tokens: Option<usize>,
    /// Default temperature.
    pub default_temperature: Option<f32>,
    /// Default top-p.
    pub default_top_p: Option<f32>,
    /// Maximum input tokens.
    pub max_input_tokens: Option<usize>,
    /// Maximum concurrent requests.
    pub max_concurrent_requests: Option<usize>,
    /// Per-request timeout, milliseconds.
    pub per_request_timeout_ms: Option<u64>,
    /// Bearer token.
    pub bearer_token: Option<String>,
    /// Log level.
    pub log_level: Option<String>,
    /// Metrics enabled.
    pub metrics_enabled: Option<bool>,
    /// Metrics path.
    pub metrics_path: Option<String>,
    /// RNG seed.
    pub seed: Option<u64>,
}

impl PartialServerConfig {
    /// Merge `other` into `self`: every field set in `other` overrides the
    /// corresponding field in `self`.  Returns the merged result.
    pub fn merge(mut self, other: PartialServerConfig) -> Self {
        macro_rules! merge_field {
            ($name:ident) => {
                if other.$name.is_some() {
                    self.$name = other.$name;
                }
            };
        }
        merge_field!(host);
        merge_field!(port);
        merge_field!(model_path);
        merge_field!(quantization_hint);
        merge_field!(tokenizer_path);
        merge_field!(tokenizer_kind);
        merge_field!(default_max_tokens);
        merge_field!(default_temperature);
        merge_field!(default_top_p);
        merge_field!(max_input_tokens);
        merge_field!(max_concurrent_requests);
        merge_field!(per_request_timeout_ms);
        merge_field!(bearer_token);
        merge_field!(log_level);
        merge_field!(metrics_enabled);
        merge_field!(metrics_path);
        merge_field!(seed);
        self
    }

    /// Parse a TOML string into a partial config.
    ///
    /// Unlike [`ServerConfig::from_toml`], the result is a partial config so
    /// missing fields remain `None` and do not override downstream layers.
    pub fn from_toml_str(s: &str) -> Result<Self, ConfigError> {
        // We round-trip through a fully-populated helper struct so that any
        // extra fields are rejected and section-based layout is preserved.
        let helper: TomlHelper =
            toml::from_str(s).map_err(|e| ConfigError::TomlParse(e.to_string()))?;
        Ok(helper.into_partial())
    }
}

// ─── TOML helper shape ────────────────────────────────────────────────────

/// Mirror of the TOML schema, used only during parsing.
#[derive(Debug, Default, Deserialize)]
struct TomlHelper {
    #[serde(default)]
    bind: Option<BindPartial>,
    #[serde(default)]
    model: Option<ModelPartial>,
    #[serde(default)]
    tokenizer: Option<TokenizerPartial>,
    #[serde(default)]
    sampling: Option<SamplingPartial>,
    #[serde(default)]
    limits: Option<LimitsPartial>,
    #[serde(default)]
    auth: Option<AuthPartial>,
    #[serde(default)]
    observability: Option<ObservabilityPartial>,
    #[serde(default)]
    seed: Option<u64>,
}

#[derive(Debug, Default, Deserialize)]
struct BindPartial {
    host: Option<String>,
    port: Option<u16>,
}
#[derive(Debug, Default, Deserialize)]
struct ModelPartial {
    path: Option<PathBuf>,
    quantization_hint: Option<String>,
}
#[derive(Debug, Default, Deserialize)]
struct TokenizerPartial {
    path: Option<PathBuf>,
    kind: Option<String>,
}
#[derive(Debug, Default, Deserialize)]
struct SamplingPartial {
    default_max_tokens: Option<usize>,
    default_temperature: Option<f32>,
    default_top_p: Option<f32>,
}
#[derive(Debug, Default, Deserialize)]
struct LimitsPartial {
    max_input_tokens: Option<usize>,
    max_concurrent_requests: Option<usize>,
    per_request_timeout_ms: Option<u64>,
}
#[derive(Debug, Default, Deserialize)]
struct AuthPartial {
    bearer_token: Option<String>,
}
#[derive(Debug, Default, Deserialize)]
struct ObservabilityPartial {
    log_level: Option<String>,
    metrics_enabled: Option<bool>,
    metrics_path: Option<String>,
}

impl TomlHelper {
    fn into_partial(self) -> PartialServerConfig {
        let bind = self.bind.unwrap_or_default();
        let model = self.model.unwrap_or_default();
        let tok = self.tokenizer.unwrap_or_default();
        let samp = self.sampling.unwrap_or_default();
        let lim = self.limits.unwrap_or_default();
        let auth = self.auth.unwrap_or_default();
        let obs = self.observability.unwrap_or_default();
        PartialServerConfig {
            host: bind.host,
            port: bind.port,
            model_path: model.path,
            quantization_hint: model.quantization_hint,
            tokenizer_path: tok.path,
            tokenizer_kind: tok.kind,
            default_max_tokens: samp.default_max_tokens,
            default_temperature: samp.default_temperature,
            default_top_p: samp.default_top_p,
            max_input_tokens: lim.max_input_tokens,
            max_concurrent_requests: lim.max_concurrent_requests,
            per_request_timeout_ms: lim.per_request_timeout_ms,
            bearer_token: auth.bearer_token,
            log_level: obs.log_level,
            metrics_enabled: obs.metrics_enabled,
            metrics_path: obs.metrics_path,
            seed: self.seed,
        }
    }
}

// ─── ServerConfig construction helpers ────────────────────────────────────

impl ServerConfig {
    /// Parse a TOML string directly into a full `ServerConfig`, using defaults
    /// for any missing fields.
    pub fn from_toml(s: &str) -> Result<Self, ConfigError> {
        let partial = PartialServerConfig::from_toml_str(s)?;
        Ok(Self::from_partial(partial))
    }

    /// Build a full `ServerConfig` from a partial, filling unset fields with
    /// the [`Default`] values.
    pub fn from_partial(p: PartialServerConfig) -> Self {
        let mut out = Self::default();
        if let Some(v) = p.host {
            out.bind.host = v;
        }
        if let Some(v) = p.port {
            out.bind.port = v;
        }
        if let Some(v) = p.model_path {
            out.model.path = Some(v);
        }
        if let Some(v) = p.quantization_hint {
            out.model.quantization_hint = Some(v);
        }
        if let Some(v) = p.tokenizer_path {
            out.tokenizer.path = Some(v);
        }
        if let Some(v) = p.tokenizer_kind {
            out.tokenizer.kind = Some(v);
        }
        if let Some(v) = p.default_max_tokens {
            out.sampling.default_max_tokens = v;
        }
        if let Some(v) = p.default_temperature {
            out.sampling.default_temperature = v;
        }
        if let Some(v) = p.default_top_p {
            out.sampling.default_top_p = v;
        }
        if let Some(v) = p.max_input_tokens {
            out.limits.max_input_tokens = v;
        }
        if let Some(v) = p.max_concurrent_requests {
            out.limits.max_concurrent_requests = v;
        }
        if let Some(v) = p.per_request_timeout_ms {
            out.limits.per_request_timeout_ms = v;
        }
        if let Some(v) = p.bearer_token {
            out.auth.bearer_token = Some(v);
        }
        if let Some(v) = p.log_level {
            out.observability.log_level = v;
        }
        if let Some(v) = p.metrics_enabled {
            out.observability.metrics_enabled = v;
        }
        if let Some(v) = p.metrics_path {
            out.observability.metrics_path = v;
        }
        if let Some(v) = p.seed {
            out.seed = v;
        }
        out
    }

    /// Serialize the current config to a TOML string.
    pub fn to_toml_string(&self) -> Result<String, ConfigError> {
        toml::to_string_pretty(self).map_err(|e| ConfigError::TomlParse(e.to_string()))
    }

    /// Load a configuration from a file on disk.
    pub fn from_toml_file<P: AsRef<std::path::Path>>(path: P) -> Result<Self, ConfigError> {
        let p = path.as_ref();
        let body = std::fs::read_to_string(p).map_err(|e| ConfigError::Io {
            path: p.display().to_string(),
            source: e,
        })?;
        Self::from_toml(&body)
    }

    /// Load a partial configuration from a file on disk.
    pub fn partial_from_file<P: AsRef<std::path::Path>>(
        path: P,
    ) -> Result<PartialServerConfig, ConfigError> {
        let p = path.as_ref();
        let body = std::fs::read_to_string(p).map_err(|e| ConfigError::Io {
            path: p.display().to_string(),
            source: e,
        })?;
        PartialServerConfig::from_toml_str(&body)
    }

    /// Layered loader.
    ///
    /// 1. Start from [`ServerConfig::default`].
    /// 2. If `toml_path` is `Some`, merge the TOML file on top.
    /// 3. If `env` is `Some`, merge the env-derived partial on top.
    /// 4. If `cli` is `Some`, merge the CLI-derived partial on top (highest
    ///    precedence).
    ///
    /// The final result is validated via [`ServerConfig::validate`] before
    /// being returned.
    pub fn load(
        toml_path: Option<&std::path::Path>,
        env_partial: Option<PartialServerConfig>,
        cli_partial: Option<PartialServerConfig>,
    ) -> Result<Self, ConfigError> {
        let mut merged = PartialServerConfig::default();

        if let Some(p) = toml_path {
            let from_file = Self::partial_from_file(p)?;
            merged = merged.merge(from_file);
        }
        if let Some(env) = env_partial {
            merged = merged.merge(env);
        }
        if let Some(cli) = cli_partial {
            merged = merged.merge(cli);
        }

        let cfg = Self::from_partial(merged);
        cfg.validate()?;
        Ok(cfg)
    }
}

// ─── Tests ───────────────────────────────────────────────────────────────

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

    #[test]
    fn defaults_roundtrip() {
        let cfg = ServerConfig::default();
        let toml = cfg.to_toml_string().expect("to_toml");
        let parsed = ServerConfig::from_toml(&toml).expect("from_toml");
        assert_eq!(cfg, parsed);
    }

    #[test]
    fn partial_default_is_empty() {
        let p = PartialServerConfig::default();
        assert!(p.host.is_none());
        assert!(p.port.is_none());
    }

    #[test]
    fn partial_merge_overrides() {
        let a = PartialServerConfig {
            port: Some(1),
            log_level: Some("info".to_string()),
            ..Default::default()
        };
        let b = PartialServerConfig {
            port: Some(2),
            ..Default::default()
        };
        let merged = a.merge(b);
        assert_eq!(merged.port, Some(2));
        assert_eq!(merged.log_level.as_deref(), Some("info"));
    }

    #[test]
    fn from_partial_applies_fields() {
        let p = PartialServerConfig {
            host: Some("1.2.3.4".to_string()),
            port: Some(9999),
            default_top_p: Some(0.9),
            ..Default::default()
        };
        let cfg = ServerConfig::from_partial(p);
        assert_eq!(cfg.bind.host, "1.2.3.4");
        assert_eq!(cfg.bind.port, 9999);
        assert!((cfg.sampling.default_top_p - 0.9).abs() < f32::EPSILON);
    }
}