llm-stack 0.7.0

Core traits, types, and tools for the llm-stack SDK
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
//! Dynamic provider registry for configuration-driven provider instantiation.
//!
//! The registry allows providers to be registered by name and instantiated from
//! configuration at runtime. This enables:
//!
//! - Config-file driven provider selection
//! - Third-party provider registration
//! - Dynamic provider switching without code changes
//!
//! # Example
//!
//! ```rust,no_run
//! use llm_stack::registry::{ProviderRegistry, ProviderConfig};
//!
//! // Get the global registry (providers register themselves on startup)
//! let registry = ProviderRegistry::global();
//!
//! // Build a provider from config
//! let config = ProviderConfig {
//!     provider: "anthropic".into(),
//!     api_key: Some("sk-...".into()),
//!     model: "claude-sonnet-4-20250514".into(),
//!     ..Default::default()
//! };
//!
//! let provider = registry.build(&config).expect("provider registered");
//! ```
//!
//! # Registering providers
//!
//! Provider crates register their factory on initialization:
//!
//! ```rust,ignore
//! use llm_stack::registry::{ProviderRegistry, ProviderFactory, ProviderConfig};
//!
//! struct MyProviderFactory;
//!
//! impl ProviderFactory for MyProviderFactory {
//!     fn name(&self) -> &str { "my-provider" }
//!
//!     fn build(&self, config: &ProviderConfig) -> Result<Box<dyn DynProvider>, LlmError> {
//!         // Build and return provider
//!     }
//! }
//!
//! // Register on crate initialization
//! ProviderRegistry::global().register(Box::new(MyProviderFactory));
//! ```

use std::collections::HashMap;
use std::sync::{Arc, OnceLock, RwLock};
use std::time::Duration;

use crate::error::LlmError;
use crate::provider::DynProvider;

/// Configuration for building a provider from the registry.
///
/// This struct contains common configuration fields that work across
/// all providers. Provider-specific options go in the `extra` map.
#[derive(Debug, Clone, Default)]
pub struct ProviderConfig {
    /// Provider name (e.g., "anthropic", "openai", "ollama").
    pub provider: String,

    /// API key for authenticated providers.
    pub api_key: Option<String>,

    /// Model identifier (e.g., "claude-sonnet-4-20250514", "gpt-4o").
    pub model: String,

    /// Custom base URL for the API endpoint.
    pub base_url: Option<String>,

    /// Request timeout.
    pub timeout: Option<Duration>,

    /// Shared HTTP client for connection pooling.
    ///
    /// When set, the provider will use this client instead of creating
    /// its own. Useful when multiple providers should share a connection
    /// pool (e.g., in multi-agent systems).
    pub client: Option<reqwest::Client>,

    /// Provider-specific configuration options.
    ///
    /// Use this for options that don't fit the common fields above.
    /// Each provider documents which keys it recognizes.
    pub extra: HashMap<String, serde_json::Value>,
}

impl ProviderConfig {
    /// Creates a new config with the given provider and model.
    pub fn new(provider: impl Into<String>, model: impl Into<String>) -> Self {
        Self {
            provider: provider.into(),
            model: model.into(),
            ..Default::default()
        }
    }

    /// Sets the API key.
    #[must_use]
    pub fn api_key(mut self, key: impl Into<String>) -> Self {
        self.api_key = Some(key.into());
        self
    }

    /// Sets the base URL.
    #[must_use]
    pub fn base_url(mut self, url: impl Into<String>) -> Self {
        self.base_url = Some(url.into());
        self
    }

    /// Sets the timeout.
    #[must_use]
    pub fn timeout(mut self, timeout: Duration) -> Self {
        self.timeout = Some(timeout);
        self
    }

    /// Sets a shared HTTP client for connection pooling.
    #[must_use]
    pub fn client(mut self, client: reqwest::Client) -> Self {
        self.client = Some(client);
        self
    }

    /// Adds a provider-specific extra option.
    #[must_use]
    pub fn extra(mut self, key: impl Into<String>, value: impl Into<serde_json::Value>) -> Self {
        self.extra.insert(key.into(), value.into());
        self
    }

    /// Gets a string value from extra options.
    pub fn get_extra_str(&self, key: &str) -> Option<&str> {
        self.extra.get(key).and_then(|v| v.as_str())
    }

    /// Gets a bool value from extra options.
    pub fn get_extra_bool(&self, key: &str) -> Option<bool> {
        self.extra.get(key).and_then(serde_json::Value::as_bool)
    }

    /// Gets an integer value from extra options.
    pub fn get_extra_i64(&self, key: &str) -> Option<i64> {
        self.extra.get(key).and_then(serde_json::Value::as_i64)
    }
}

/// Factory trait for creating providers from configuration.
///
/// Implement this trait to register a provider with the registry.
pub trait ProviderFactory: Send + Sync {
    /// Returns the provider name used for registration and lookup.
    ///
    /// This should be a lowercase identifier (e.g., "anthropic", "openai").
    fn name(&self) -> &str;

    /// Creates a provider instance from the given configuration.
    ///
    /// # Errors
    ///
    /// Returns an error if the configuration is invalid or missing
    /// required fields for this provider.
    fn build(&self, config: &ProviderConfig) -> Result<Box<dyn DynProvider>, LlmError>;
}

/// A registry of provider factories for dynamic provider instantiation.
///
/// The registry maintains a map of provider names to their factories,
/// allowing providers to be created from configuration at runtime.
///
/// # Thread Safety
///
/// The registry is thread-safe and can be accessed concurrently.
/// Registration and lookup use interior mutability via `RwLock`.
///
/// # Global vs Local Registries
///
/// Use [`ProviderRegistry::global()`] for the shared global registry,
/// or create local registries with [`ProviderRegistry::new()`] for
/// testing or isolated contexts.
pub struct ProviderRegistry {
    factories: RwLock<HashMap<String, Arc<dyn ProviderFactory>>>,
}

impl std::fmt::Debug for ProviderRegistry {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let factories = self
            .factories
            .read()
            .expect("provider registry lock poisoned");
        let names: Vec<_> = factories.keys().collect();
        f.debug_struct("ProviderRegistry")
            .field("providers", &names)
            .finish()
    }
}

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

impl ProviderRegistry {
    /// Creates a new empty registry.
    pub fn new() -> Self {
        Self {
            factories: RwLock::new(HashMap::new()),
        }
    }

    /// Returns the global shared registry.
    ///
    /// Provider crates should register their factories here on initialization.
    /// Application code can then build providers from configuration without
    /// knowing which providers are available at compile time.
    pub fn global() -> &'static Self {
        static GLOBAL: OnceLock<ProviderRegistry> = OnceLock::new();
        GLOBAL.get_or_init(ProviderRegistry::new)
    }

    /// Registers a provider factory.
    ///
    /// If a factory with the same name already exists, it is replaced.
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// use llm_stack::registry::{ProviderRegistry, ProviderFactory};
    ///
    /// ProviderRegistry::global().register(Box::new(MyProviderFactory));
    /// ```
    pub fn register(&self, factory: Box<dyn ProviderFactory>) -> &Self {
        let name = factory.name().to_lowercase();
        let mut factories = self
            .factories
            .write()
            .expect("provider registry lock poisoned");
        factories.insert(name, Arc::from(factory));
        self
    }

    /// Registers a provider factory (chainable Arc version).
    ///
    /// Use this when you want to share the factory instance.
    pub fn register_shared(&self, factory: Arc<dyn ProviderFactory>) -> &Self {
        let name = factory.name().to_lowercase();
        let mut factories = self
            .factories
            .write()
            .expect("provider registry lock poisoned");
        factories.insert(name, factory);
        self
    }

    /// Unregisters a provider by name.
    ///
    /// Returns `true` if the provider was registered and removed.
    pub fn unregister(&self, name: &str) -> bool {
        let mut factories = self
            .factories
            .write()
            .expect("provider registry lock poisoned");
        factories.remove(&name.to_lowercase()).is_some()
    }

    /// Checks if a provider is registered.
    pub fn contains(&self, name: &str) -> bool {
        let factories = self
            .factories
            .read()
            .expect("provider registry lock poisoned");
        factories.contains_key(&name.to_lowercase())
    }

    /// Returns the names of all registered providers.
    pub fn providers(&self) -> Vec<String> {
        let factories = self
            .factories
            .read()
            .expect("provider registry lock poisoned");
        factories.keys().cloned().collect()
    }

    /// Builds a provider from configuration.
    ///
    /// Looks up the factory by `config.provider` and delegates to it.
    ///
    /// # Errors
    ///
    /// Returns [`LlmError::InvalidRequest`] if no factory is registered
    /// for the requested provider name.
    pub fn build(&self, config: &ProviderConfig) -> Result<Box<dyn DynProvider>, LlmError> {
        let name = config.provider.to_lowercase();
        let factories = self
            .factories
            .read()
            .expect("provider registry lock poisoned");

        let factory = factories.get(&name).ok_or_else(|| {
            let available: Vec<_> = factories.keys().cloned().collect();
            LlmError::InvalidRequest(format!(
                "unknown provider '{}'. Available: {:?}",
                config.provider, available
            ))
        })?;

        factory.build(config)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::chat::{ChatResponse, ContentBlock, StopReason};
    use crate::provider::{ChatParams, Provider, ProviderMetadata};
    use crate::stream::ChatStream;
    use crate::usage::Usage;
    use std::collections::{HashMap, HashSet};

    struct TestProvider {
        model: String,
    }

    impl Provider for TestProvider {
        async fn generate(&self, _params: &ChatParams) -> Result<ChatResponse, LlmError> {
            Ok(ChatResponse {
                content: vec![ContentBlock::Text("test".into())],
                usage: Usage::default(),
                stop_reason: StopReason::EndTurn,
                model: self.model.clone(),
                metadata: HashMap::default(),
            })
        }

        async fn stream(&self, _params: &ChatParams) -> Result<ChatStream, LlmError> {
            Err(LlmError::InvalidRequest("not implemented".into()))
        }

        fn metadata(&self) -> ProviderMetadata {
            ProviderMetadata {
                name: "test".into(),
                model: self.model.clone(),
                context_window: 4096,
                capabilities: HashSet::new(),
            }
        }
    }

    struct TestFactory;

    impl ProviderFactory for TestFactory {
        fn name(&self) -> &'static str {
            "test"
        }

        fn build(&self, config: &ProviderConfig) -> Result<Box<dyn DynProvider>, LlmError> {
            Ok(Box::new(TestProvider {
                model: config.model.clone(),
            }))
        }
    }

    #[test]
    fn test_registry_register_and_build() {
        let registry = ProviderRegistry::new();
        registry.register(Box::new(TestFactory));

        assert!(registry.contains("test"));
        assert!(registry.contains("TEST")); // case insensitive

        let config = ProviderConfig::new("test", "test-model");
        let provider = registry.build(&config).unwrap();

        assert_eq!(provider.metadata().model, "test-model");
    }

    #[test]
    fn test_registry_unknown_provider() {
        let registry = ProviderRegistry::new();

        let config = ProviderConfig::new("unknown", "model");
        let result = registry.build(&config);

        assert!(result.is_err());
        let err = result.err().unwrap();
        assert!(matches!(err, LlmError::InvalidRequest(_)));
    }

    #[test]
    fn test_registry_unregister() {
        let registry = ProviderRegistry::new();
        registry.register(Box::new(TestFactory));

        assert!(registry.contains("test"));
        assert!(registry.unregister("test"));
        assert!(!registry.contains("test"));
        assert!(!registry.unregister("test")); // already removed
    }

    #[test]
    fn test_registry_providers_list() {
        let registry = ProviderRegistry::new();
        registry.register(Box::new(TestFactory));

        let providers = registry.providers();
        assert_eq!(providers, vec!["test"]);
    }

    #[test]
    fn test_provider_config_builder() {
        let config = ProviderConfig::new("anthropic", "claude-3")
            .api_key("sk-123")
            .base_url("https://custom.api")
            .timeout(Duration::from_secs(60))
            .extra("organization", "org-123");

        assert_eq!(config.provider, "anthropic");
        assert_eq!(config.model, "claude-3");
        assert_eq!(config.api_key, Some("sk-123".into()));
        assert_eq!(config.base_url, Some("https://custom.api".into()));
        assert_eq!(config.timeout, Some(Duration::from_secs(60)));
        assert_eq!(config.get_extra_str("organization"), Some("org-123"));
    }

    #[test]
    fn test_provider_config_extra_types() {
        let config = ProviderConfig::new("test", "model")
            .extra("flag", true)
            .extra("count", 42i64)
            .extra("name", "value");

        assert_eq!(config.get_extra_bool("flag"), Some(true));
        assert_eq!(config.get_extra_i64("count"), Some(42));
        assert_eq!(config.get_extra_str("name"), Some("value"));
        assert_eq!(config.get_extra_str("missing"), None);
    }

    #[tokio::test]
    async fn test_built_provider_works() {
        let registry = ProviderRegistry::new();
        registry.register(Box::new(TestFactory));

        let config = ProviderConfig::new("test", "my-model");
        let provider = registry.build(&config).unwrap();

        let response = provider
            .generate_boxed(&ChatParams::default())
            .await
            .unwrap();
        assert_eq!(response.model, "my-model");
    }

    #[test]
    fn test_registry_replace_factory() {
        struct AltFactory;
        impl ProviderFactory for AltFactory {
            fn name(&self) -> &'static str {
                "test"
            }
            fn build(&self, config: &ProviderConfig) -> Result<Box<dyn DynProvider>, LlmError> {
                Ok(Box::new(TestProvider {
                    model: format!("alt-{}", config.model),
                }))
            }
        }

        let registry = ProviderRegistry::new();
        registry.register(Box::new(TestFactory));
        registry.register(Box::new(AltFactory)); // replaces

        let config = ProviderConfig::new("test", "model");
        let provider = registry.build(&config).unwrap();

        assert_eq!(provider.metadata().model, "alt-model");
    }
}