cognis-llm 0.3.0

LLM client and provider abstractions for Cognis: Client, LLMProvider trait, chat options, tool definitions, and streaming. Provider implementations (OpenAI, Anthropic, Google, Ollama, Azure) are feature-gated.
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
//! Load balancer — distribute calls across a fleet of inner providers.
//!
//! Three strategies ship in the box; users plug in their own by
//! implementing [`LoadBalancingStrategy`]:
//!
//! - [`RoundRobinStrategy`] — cycle through endpoints in order.
//! - [`WeightedRoundRobinStrategy`] — RR with per-endpoint weights.
//! - [`RandomStrategy`] — uniform random pick.
//!
//! Failover: when the chosen endpoint errors, the load balancer can
//! optionally retry on the next endpoint. Toggle via
//! [`LoadBalancerProvider::with_failover`].

use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc;

use async_trait::async_trait;

use cognis_core::{CognisError, Result, RunnableStream};

use crate::chat::{ChatOptions, ChatResponse, HealthStatus, StreamChunk};
use crate::provider::{LLMProvider, Provider};
use crate::tools::ToolDefinition;
use crate::Message;

/// Pluggable load-balancing policy.
pub trait LoadBalancingStrategy: Send + Sync {
    /// Pick an index into the endpoints array. `attempt` is 0 for the
    /// first try, 1 for the failover retry, etc. Implementations must
    /// return a value in `[0, n_endpoints)`.
    fn pick(&self, n_endpoints: usize, attempt: usize) -> usize;
}

/// Round-robin: every call increments a shared counter.
#[derive(Default)]
pub struct RoundRobinStrategy {
    cursor: AtomicUsize,
}

impl RoundRobinStrategy {
    /// New strategy.
    pub fn new() -> Self {
        Self::default()
    }
}

impl LoadBalancingStrategy for RoundRobinStrategy {
    fn pick(&self, n: usize, attempt: usize) -> usize {
        if n == 0 {
            return 0;
        }
        let base = self.cursor.fetch_add(1, Ordering::Relaxed);
        (base + attempt) % n
    }
}

/// Weighted round-robin: endpoints with higher weights get proportionally
/// more traffic.
pub struct WeightedRoundRobinStrategy {
    weights: Vec<u32>,
    /// Pre-expanded schedule (e.g. weights [3, 1] → schedule [0,0,0,1]).
    schedule: Vec<usize>,
    cursor: AtomicUsize,
}

impl WeightedRoundRobinStrategy {
    /// Build with `weights[i]` = weight of endpoint `i`. Empty weights
    /// degrade to round-robin.
    pub fn new(weights: Vec<u32>) -> Self {
        let mut schedule = Vec::new();
        for (idx, &w) in weights.iter().enumerate() {
            for _ in 0..w {
                schedule.push(idx);
            }
        }
        Self {
            weights,
            schedule,
            cursor: AtomicUsize::new(0),
        }
    }

    /// Borrow the configured weights.
    pub fn weights(&self) -> &[u32] {
        &self.weights
    }
}

impl LoadBalancingStrategy for WeightedRoundRobinStrategy {
    fn pick(&self, n: usize, attempt: usize) -> usize {
        if n == 0 {
            return 0;
        }
        if self.schedule.is_empty() {
            // No weights configured — fall back to plain RR.
            let base = self.cursor.fetch_add(1, Ordering::Relaxed);
            return (base + attempt) % n;
        }
        let base = self.cursor.fetch_add(1, Ordering::Relaxed);
        let idx = (base + attempt) % self.schedule.len();
        // schedule entries are guaranteed < weights.len(); the caller's
        // n_endpoints should match; if it's smaller, modulo-clamp.
        self.schedule[idx] % n
    }
}

/// Uniform-random pick.
pub struct RandomStrategy {
    counter: AtomicUsize,
}

impl Default for RandomStrategy {
    fn default() -> Self {
        Self::new()
    }
}

impl RandomStrategy {
    /// New random strategy with a deterministic-but-shuffled progression
    /// (uses a multiplicative hash; no `rand` dep needed).
    pub fn new() -> Self {
        Self {
            counter: AtomicUsize::new(0),
        }
    }
}

impl LoadBalancingStrategy for RandomStrategy {
    fn pick(&self, n: usize, attempt: usize) -> usize {
        if n == 0 {
            return 0;
        }
        let c = self.counter.fetch_add(1, Ordering::Relaxed);
        // Cheap LCG-style mix to avoid clustering.
        let mixed = (c.wrapping_mul(6364136223846793005).wrapping_add(1)) ^ attempt;
        mixed % n
    }
}

/// Closure-based custom strategy.
impl<F> LoadBalancingStrategy for F
where
    F: Fn(usize, usize) -> usize + Send + Sync,
{
    fn pick(&self, n: usize, attempt: usize) -> usize {
        (self)(n, attempt)
    }
}

/// Load-balancing wrapper across multiple provider instances.
pub struct LoadBalancerProvider {
    endpoints: Vec<Arc<dyn LLMProvider>>,
    strategy: Box<dyn LoadBalancingStrategy>,
    failover_attempts: usize,
    name: String,
}

impl LoadBalancerProvider {
    /// Build with a fleet of endpoints and a strategy. `name` is the
    /// reported provider name (e.g. `"openai-pool"`).
    pub fn new(
        name: impl Into<String>,
        endpoints: Vec<Arc<dyn LLMProvider>>,
        strategy: Box<dyn LoadBalancingStrategy>,
    ) -> Result<Self> {
        if endpoints.is_empty() {
            return Err(CognisError::Configuration(
                "LoadBalancerProvider requires at least one endpoint".into(),
            ));
        }
        Ok(Self {
            endpoints,
            strategy,
            failover_attempts: 0,
            name: name.into(),
        })
    }

    /// Failover behavior on error: if the chosen endpoint errors, retry
    /// up to `n` other endpoints before propagating the failure.
    /// Default: 0 (no failover).
    pub fn with_failover(mut self, n: usize) -> Self {
        self.failover_attempts = n;
        self
    }

    /// Borrow the wrapped endpoints.
    pub fn endpoints(&self) -> &[Arc<dyn LLMProvider>] {
        &self.endpoints
    }

    fn pick(&self, attempt: usize) -> &Arc<dyn LLMProvider> {
        let idx = self.strategy.pick(self.endpoints.len(), attempt);
        // Defensive: clamp, in case a custom strategy returns out-of-range.
        let idx = idx.min(self.endpoints.len() - 1);
        &self.endpoints[idx]
    }
}

#[async_trait]
impl LLMProvider for LoadBalancerProvider {
    fn name(&self) -> &str {
        &self.name
    }

    fn provider_type(&self) -> Provider {
        // Load balancer's "type" is whatever its first endpoint reports.
        // (All endpoints are expected to be the same provider; this is
        // a soft assumption.)
        self.endpoints[0].provider_type()
    }

    async fn chat_completion(
        &self,
        messages: Vec<Message>,
        opts: ChatOptions,
    ) -> Result<ChatResponse> {
        let mut last_err: Option<CognisError> = None;
        let n = self.endpoints.len();
        let first_idx = self.strategy.pick(n, 0).min(n - 1);
        for attempt in 0..=self.failover_attempts {
            let idx = (first_idx + attempt) % n;
            let ep = &self.endpoints[idx];
            match ep.chat_completion(messages.clone(), opts.clone()).await {
                Ok(r) => return Ok(r),
                Err(e) => last_err = Some(e),
            }
        }
        Err(last_err
            .unwrap_or_else(|| CognisError::Internal("load balancer reached no endpoints".into())))
    }

    async fn chat_completion_stream(
        &self,
        messages: Vec<Message>,
        opts: ChatOptions,
    ) -> Result<RunnableStream<StreamChunk>> {
        // Streaming doesn't have a clean failover path (chunks already
        // emitted couldn't be undone), so we only attempt the first pick.
        let ep = self.pick(0);
        ep.chat_completion_stream(messages, opts).await
    }

    async fn chat_completion_with_tools(
        &self,
        messages: Vec<Message>,
        tools: Vec<ToolDefinition>,
        opts: ChatOptions,
    ) -> Result<ChatResponse> {
        let mut last_err: Option<CognisError> = None;
        let n = self.endpoints.len();
        let first_idx = self.strategy.pick(n, 0).min(n - 1);
        for attempt in 0..=self.failover_attempts {
            let idx = (first_idx + attempt) % n;
            let ep = &self.endpoints[idx];
            match ep
                .chat_completion_with_tools(messages.clone(), tools.clone(), opts.clone())
                .await
            {
                Ok(r) => return Ok(r),
                Err(e) => last_err = Some(e),
            }
        }
        Err(last_err
            .unwrap_or_else(|| CognisError::Internal("load balancer reached no endpoints".into())))
    }

    async fn health_check(&self) -> Result<HealthStatus> {
        // Healthy if any endpoint is healthy. Returns the first Healthy
        // result verbatim (with its latency).
        let mut last: Result<HealthStatus> = Err(CognisError::Internal("no endpoints".into()));
        for ep in &self.endpoints {
            match ep.health_check().await {
                Ok(s @ HealthStatus::Healthy { .. }) => return Ok(s),
                Ok(s) => last = Ok(s),
                Err(e) => last = Err(e),
            }
        }
        last
    }
}

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

    struct Tagged {
        tag: &'static str,
        ok: bool,
        seen: Arc<Mutex<Vec<&'static str>>>,
    }

    #[async_trait]
    impl LLMProvider for Tagged {
        fn name(&self) -> &str {
            self.tag
        }
        fn provider_type(&self) -> Provider {
            Provider::OpenAI
        }
        async fn chat_completion(&self, _: Vec<Message>, _: ChatOptions) -> Result<ChatResponse> {
            self.seen.lock().unwrap().push(self.tag);
            if self.ok {
                Ok(ChatResponse {
                    message: Message::ai(self.tag),
                    usage: None,
                    finish_reason: "stop".into(),
                    model: self.tag.into(),
                })
            } else {
                Err(CognisError::Internal("nope".into()))
            }
        }
        async fn chat_completion_stream(
            &self,
            _: Vec<Message>,
            _: ChatOptions,
        ) -> Result<RunnableStream<StreamChunk>> {
            unimplemented!()
        }
        async fn health_check(&self) -> Result<HealthStatus> {
            Ok(if self.ok {
                HealthStatus::Healthy { latency_ms: 0 }
            } else {
                HealthStatus::Unhealthy {
                    reason: "scripted".into(),
                }
            })
        }
    }

    fn ep(
        tag: &'static str,
        ok: bool,
        seen: Arc<Mutex<Vec<&'static str>>>,
    ) -> Arc<dyn LLMProvider> {
        Arc::new(Tagged { tag, ok, seen })
    }

    #[tokio::test]
    async fn round_robin_cycles_endpoints() {
        let seen = Arc::new(Mutex::new(Vec::new()));
        let lb = LoadBalancerProvider::new(
            "pool",
            vec![ep("a", true, seen.clone()), ep("b", true, seen.clone())],
            Box::new(RoundRobinStrategy::new()),
        )
        .unwrap();
        for _ in 0..4 {
            let _ = lb.chat_completion(vec![], ChatOptions::default()).await;
        }
        let s = seen.lock().unwrap().clone();
        assert_eq!(s.iter().filter(|t| **t == "a").count(), 2);
        assert_eq!(s.iter().filter(|t| **t == "b").count(), 2);
    }

    #[tokio::test]
    async fn weighted_rr_respects_weights() {
        let seen = Arc::new(Mutex::new(Vec::new()));
        let lb = LoadBalancerProvider::new(
            "pool",
            vec![ep("a", true, seen.clone()), ep("b", true, seen.clone())],
            Box::new(WeightedRoundRobinStrategy::new(vec![3, 1])),
        )
        .unwrap();
        for _ in 0..8 {
            let _ = lb.chat_completion(vec![], ChatOptions::default()).await;
        }
        let s = seen.lock().unwrap().clone();
        assert_eq!(s.iter().filter(|t| **t == "a").count(), 6);
        assert_eq!(s.iter().filter(|t| **t == "b").count(), 2);
    }

    #[tokio::test]
    async fn failover_retries_on_error() {
        let seen = Arc::new(Mutex::new(Vec::new()));
        let lb = LoadBalancerProvider::new(
            "pool",
            vec![
                ep("bad", false, seen.clone()),
                ep("good", true, seen.clone()),
            ],
            Box::new(RoundRobinStrategy::new()),
        )
        .unwrap()
        .with_failover(1);
        let res = lb.chat_completion(vec![], ChatOptions::default()).await;
        assert!(res.is_ok());
        // Both endpoints saw the call (first failed, second succeeded).
        let s = seen.lock().unwrap().clone();
        assert!(s.contains(&"bad"));
        assert!(s.contains(&"good"));
    }

    #[tokio::test]
    async fn rejects_empty_endpoints() {
        let res = LoadBalancerProvider::new("x", Vec::new(), Box::new(RoundRobinStrategy::new()));
        assert!(res.is_err());
    }

    #[tokio::test]
    async fn closure_strategy_works() {
        let seen = Arc::new(Mutex::new(Vec::new()));
        // Always pick endpoint 1.
        let lb = LoadBalancerProvider::new(
            "pool",
            vec![ep("a", true, seen.clone()), ep("b", true, seen.clone())],
            Box::new(|_n: usize, _a: usize| 1usize),
        )
        .unwrap();
        for _ in 0..3 {
            let _ = lb.chat_completion(vec![], ChatOptions::default()).await;
        }
        let s = seen.lock().unwrap().clone();
        assert!(s.iter().all(|t| *t == "b"));
    }
}