brainwires-provider 0.11.0

LLM provider implementations (Anthropic, OpenAI Chat + Responses, Google Gemini, Ollama, Bedrock, Vertex AI, local llama.cpp) for the Brainwires Agent Framework. Speech (TTS/STT) providers live in `brainwires-provider-speech`.
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
#![deny(missing_docs)]
//! Provider layer for the Brainwires Agent Framework.
//!
//! Contains both low-level API client structs (HTTP transport, auth, rate
//! limiting, serde) and high-level chat provider implementations that wrap
//! them with the `brainwires_core::Provider` trait.

// Re-export core traits for convenience
pub use brainwires_core::provider::{ChatOptions, Provider};

// Rate limiting and HTTP client
#[cfg(feature = "native")]
pub mod http_client;
#[cfg(feature = "native")]
pub mod rate_limiter;

#[cfg(feature = "native")]
pub use http_client::RateLimitedClient;
#[cfg(feature = "native")]
pub use rate_limiter::RateLimiter;

// ── Protocol directories ──────────────────────────────────────────────

/// OpenAI Chat Completions protocol (also used by Groq, Together, Fireworks, Anyscale).
#[cfg(feature = "native")]
pub mod openai_chat;

/// OpenAI Responses API protocol (`/v1/responses`).
#[cfg(feature = "native")]
pub mod openai_responses;

/// Anthropic Messages protocol (also used by Bedrock, Vertex AI).
#[cfg(feature = "native")]
pub mod anthropic;

/// Google Gemini generateContent protocol.
#[cfg(feature = "native")]
pub mod gemini;

/// Ollama native chat protocol.
#[cfg(feature = "native")]
pub mod ollama;

/// Brainwires HTTP relay protocol.
#[cfg(feature = "native")]
pub mod brainwires_http;
#[cfg(feature = "native")]
pub use brainwires_http::{DEFAULT_BACKEND_URL, DEV_BACKEND_URL, get_backend_from_api_key};

// Speech (TTS / STT) provider clients live in `brainwires-provider-speech`.

// ── Registry ──────────────────────────────────────────────────────────

/// Provider registry — protocol, auth, and endpoint metadata for all known providers.
pub mod registry;

// ── Model listing ─────────────────────────────────────────────────────

/// Model listing — query available models from provider APIs.
#[cfg(feature = "native")]
pub mod model_listing;

/// Chat provider factory — registry-driven protocol dispatch.
#[cfg(feature = "native")]
pub mod chat_factory;

// ── Local LLM ─────────────────────────────────────────────────────────

/// Local LLM inference (always compiled, llama.cpp behind feature flag).
pub mod local_llm;

// Browser-native `web_speech` lives in `brainwires-provider-speech`.

// ── Re-exports ────────────────────────────────────────────────────────

// Chat-capable API clients
#[cfg(feature = "native")]
pub use anthropic::AnthropicClient;
#[cfg(feature = "native")]
pub use brainwires_http::BrainwiresHttpProvider;
#[cfg(feature = "native")]
pub use gemini::GoogleClient;
#[cfg(feature = "native")]
pub use ollama::OllamaProvider;
#[cfg(feature = "native")]
pub use openai_chat::OpenAiClient;

// Chat providers
#[cfg(feature = "native")]
pub use anthropic::chat::AnthropicChatProvider;
#[cfg(feature = "native")]
pub use gemini::chat::GoogleChatProvider;
#[cfg(feature = "native")]
pub use ollama::chat::OllamaChatProvider;
#[cfg(feature = "native")]
pub use openai_chat::chat::OpenAiChatProvider;
#[cfg(feature = "native")]
pub use openai_responses::OpenAiResponsesProvider;

// Audio/speech client re-exports live on `brainwires-provider-speech`.

// Model listing
#[cfg(feature = "native")]
pub use model_listing::{AvailableModel, ModelCapability, ModelLister, create_model_lister};

// Factory
#[cfg(feature = "native")]
pub use chat_factory::ChatProviderFactory;

// Local LLM
pub use local_llm::*;

use serde::{Deserialize, Serialize};
use std::fmt;
use std::str::FromStr;

/// AI provider types
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, Hash)]
#[serde(rename_all = "lowercase")]
pub enum ProviderType {
    /// Anthropic (Claude).
    Anthropic,
    /// OpenAI (GPT).
    OpenAI,
    /// Google (Gemini).
    Google,
    /// Groq inference.
    Groq,
    /// Ollama local models.
    Ollama,
    /// Brainwires HTTP relay.
    Brainwires,
    /// Together AI.
    Together,
    /// Fireworks AI.
    Fireworks,
    /// Anyscale.
    Anyscale,
    /// Amazon Bedrock (Anthropic Messages via AWS SigV4).
    Bedrock,
    /// Google Vertex AI (Anthropic Messages via OAuth2).
    VertexAI,
    /// ElevenLabs.
    ElevenLabs,
    /// Deepgram.
    Deepgram,
    /// Azure Speech.
    Azure,
    /// Fish Audio.
    Fish,
    /// Cartesia.
    Cartesia,
    /// Murf AI.
    Murf,
    /// OpenAI Responses API.
    OpenAiResponses,
    /// MiniMax AI.
    MiniMax,
    /// Custom / user-defined provider.
    Custom,
}

impl ProviderType {
    /// Get the default model for this provider
    pub fn default_model(&self) -> &'static str {
        match self {
            Self::Anthropic => "claude-sonnet-4-6",
            Self::OpenAI => "gpt-5-mini",
            Self::Google => "gemini-2.5-flash",
            Self::Groq => "llama-3.3-70b-versatile",
            Self::Ollama => "llama3.3",
            Self::Brainwires => "gpt-5-mini",
            Self::Together => "meta-llama/Llama-3.1-8B-Instruct",
            Self::Fireworks => "accounts/fireworks/models/llama-v3p1-8b-instruct",
            Self::Anyscale => "meta-llama/Meta-Llama-3.1-8B-Instruct",
            Self::Bedrock => "anthropic.claude-sonnet-4-6-v1:0",
            Self::VertexAI => "claude-sonnet-4-6",
            Self::ElevenLabs => "eleven_multilingual_v2",
            Self::Deepgram => "nova-2",
            Self::Azure => "en-US-JennyNeural",
            Self::Fish => "default",
            Self::Cartesia => "sonic-english",
            Self::Murf => "en-US-natalie",
            Self::OpenAiResponses => "gpt-5-mini",
            Self::MiniMax => "MiniMax-M2.7",
            Self::Custom => "claude-sonnet-4-6",
        }
    }

    /// Parse from string
    pub fn from_str_opt(s: &str) -> Option<Self> {
        match s.to_lowercase().as_str() {
            "anthropic" => Some(Self::Anthropic),
            "openai" => Some(Self::OpenAI),
            "google" | "gemini" => Some(Self::Google),
            "groq" => Some(Self::Groq),
            "ollama" => Some(Self::Ollama),
            "brainwires" => Some(Self::Brainwires),
            "together" => Some(Self::Together),
            "fireworks" => Some(Self::Fireworks),
            "anyscale" => Some(Self::Anyscale),
            "bedrock" => Some(Self::Bedrock),
            "vertex-ai" | "vertexai" | "vertex_ai" => Some(Self::VertexAI),
            "elevenlabs" => Some(Self::ElevenLabs),
            "deepgram" => Some(Self::Deepgram),
            "azure" => Some(Self::Azure),
            "fish" => Some(Self::Fish),
            "cartesia" => Some(Self::Cartesia),
            "murf" => Some(Self::Murf),
            "openai-responses" | "openai_responses" => Some(Self::OpenAiResponses),
            "minimax" => Some(Self::MiniMax),
            "custom" => Some(Self::Custom),
            _ => None,
        }
    }

    /// Convert to string
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::Anthropic => "anthropic",
            Self::OpenAI => "openai",
            Self::Google => "google",
            Self::Groq => "groq",
            Self::Ollama => "ollama",
            Self::Brainwires => "brainwires",
            Self::Together => "together",
            Self::Fireworks => "fireworks",
            Self::Anyscale => "anyscale",
            Self::Bedrock => "bedrock",
            Self::VertexAI => "vertex-ai",
            Self::ElevenLabs => "elevenlabs",
            Self::Deepgram => "deepgram",
            Self::Azure => "azure",
            Self::Fish => "fish",
            Self::Cartesia => "cartesia",
            Self::Murf => "murf",
            Self::OpenAiResponses => "openai-responses",
            Self::MiniMax => "minimax",
            Self::Custom => "custom",
        }
    }

    /// Whether this provider requires an API key
    pub fn requires_api_key(&self) -> bool {
        !matches!(self, Self::Ollama | Self::Bedrock | Self::VertexAI)
    }
}

impl fmt::Display for ProviderType {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.as_str())
    }
}

impl FromStr for ProviderType {
    type Err = anyhow::Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Self::from_str_opt(s).ok_or_else(|| anyhow::anyhow!("Unknown provider: {}", s))
    }
}

/// Provider configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProviderConfig {
    /// Provider type
    pub provider: ProviderType,
    /// Model name
    pub model: String,
    /// API key (if required)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub api_key: Option<String>,
    /// Base URL (for custom endpoints)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub base_url: Option<String>,
    /// Additional provider-specific options
    #[serde(flatten)]
    pub options: std::collections::HashMap<String, serde_json::Value>,
    /// Analytics collector — not serialized, threaded through at runtime.
    #[cfg(feature = "telemetry")]
    #[serde(skip)]
    pub analytics_collector: Option<std::sync::Arc<brainwires_telemetry::AnalyticsCollector>>,
}

impl ProviderConfig {
    /// Create a new provider config
    pub fn new(provider: ProviderType, model: String) -> Self {
        Self {
            provider,
            model,
            api_key: None,
            base_url: None,
            options: std::collections::HashMap::new(),
            #[cfg(feature = "telemetry")]
            analytics_collector: None,
        }
    }

    /// Set API key
    pub fn with_api_key<S: Into<String>>(mut self, api_key: S) -> Self {
        self.api_key = Some(api_key.into());
        self
    }

    /// Set base URL
    pub fn with_base_url<S: Into<String>>(mut self, base_url: S) -> Self {
        self.base_url = Some(base_url.into());
        self
    }

    /// Set a provider-specific option.
    pub fn with_option(mut self, key: impl Into<String>, value: serde_json::Value) -> Self {
        self.options.insert(key.into(), value);
        self
    }

    /// Set the AWS region (for Bedrock) or GCP region (for Vertex AI).
    pub fn with_region(self, region: impl Into<String>) -> Self {
        self.with_option("region", serde_json::Value::String(region.into()))
    }

    /// Set the GCP project ID (for Vertex AI).
    pub fn with_project_id(self, project_id: impl Into<String>) -> Self {
        self.with_option("project_id", serde_json::Value::String(project_id.into()))
    }

    /// Attach an analytics collector — called by the factory layer before provider construction.
    #[cfg(feature = "telemetry")]
    pub fn with_analytics(
        mut self,
        collector: std::sync::Arc<brainwires_telemetry::AnalyticsCollector>,
    ) -> Self {
        self.analytics_collector = Some(collector);
        self
    }
}

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

    #[test]
    fn test_provider_type_default_model() {
        assert_eq!(ProviderType::Anthropic.default_model(), "claude-sonnet-4-6");
        assert_eq!(ProviderType::OpenAI.default_model(), "gpt-5-mini");
        assert_eq!(ProviderType::Google.default_model(), "gemini-2.5-flash");
        assert_eq!(
            ProviderType::Groq.default_model(),
            "llama-3.3-70b-versatile"
        );
        assert_eq!(ProviderType::Ollama.default_model(), "llama3.3");
        assert_eq!(ProviderType::Brainwires.default_model(), "gpt-5-mini");
        assert_eq!(ProviderType::MiniMax.default_model(), "MiniMax-M2.7");
    }

    #[test]
    fn test_provider_type_from_str() {
        assert_eq!(
            ProviderType::from_str_opt("anthropic"),
            Some(ProviderType::Anthropic)
        );
        assert_eq!(
            ProviderType::from_str_opt("openai"),
            Some(ProviderType::OpenAI)
        );
        assert_eq!(
            ProviderType::from_str_opt("google"),
            Some(ProviderType::Google)
        );
        assert_eq!(
            ProviderType::from_str_opt("gemini"),
            Some(ProviderType::Google)
        );
        assert_eq!(ProviderType::from_str_opt("groq"), Some(ProviderType::Groq));
        assert_eq!(
            ProviderType::from_str_opt("ollama"),
            Some(ProviderType::Ollama)
        );
        assert_eq!(
            ProviderType::from_str_opt("brainwires"),
            Some(ProviderType::Brainwires)
        );
        assert_eq!(
            ProviderType::from_str_opt("together"),
            Some(ProviderType::Together)
        );
        assert_eq!(
            ProviderType::from_str_opt("fireworks"),
            Some(ProviderType::Fireworks)
        );
        assert_eq!(
            ProviderType::from_str_opt("anyscale"),
            Some(ProviderType::Anyscale)
        );
        assert_eq!(
            ProviderType::from_str_opt("elevenlabs"),
            Some(ProviderType::ElevenLabs)
        );
        assert_eq!(
            ProviderType::from_str_opt("deepgram"),
            Some(ProviderType::Deepgram)
        );
        assert_eq!(
            ProviderType::from_str_opt("custom"),
            Some(ProviderType::Custom)
        );
        assert_eq!(
            ProviderType::from_str_opt("minimax"),
            Some(ProviderType::MiniMax)
        );
        assert_eq!(ProviderType::from_str_opt("unknown"), None);
    }

    #[test]
    fn test_provider_type_requires_api_key() {
        assert!(ProviderType::Anthropic.requires_api_key());
        assert!(ProviderType::OpenAI.requires_api_key());
        assert!(!ProviderType::Ollama.requires_api_key());
        assert!(ProviderType::ElevenLabs.requires_api_key());
        assert!(ProviderType::MiniMax.requires_api_key());
    }

    #[test]
    fn test_provider_config() {
        let config = ProviderConfig::new(ProviderType::Anthropic, "claude-3".to_string())
            .with_api_key("sk-test")
            .with_base_url("https://api.example.com");
        assert_eq!(config.provider, ProviderType::Anthropic);
        assert_eq!(config.api_key, Some("sk-test".to_string()));
        assert_eq!(config.base_url, Some("https://api.example.com".to_string()));
    }
}