pub mod error;
pub mod openai;
pub mod anthropic_compatible;
pub mod registry;
pub mod streaming;
use async_trait::async_trait;
use crate::models::{AnthropicRequest, CountTokensRequest, CountTokensResponse, ContentBlock};
use error::ProviderError;
use serde::{Deserialize, Serialize};
use bytes::Bytes;
use futures::stream::Stream;
use std::pin::Pin;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProviderResponse {
pub id: String,
pub r#type: String,
pub role: String,
pub content: Vec<ContentBlock>,
pub model: String,
pub stop_reason: Option<String>,
pub stop_sequence: Option<String>,
pub usage: Usage,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Usage {
pub input_tokens: u32,
pub output_tokens: u32,
}
#[async_trait]
pub trait AnthropicProvider: Send + Sync {
async fn send_message(&self, request: AnthropicRequest) -> Result<ProviderResponse, ProviderError>;
async fn send_message_stream(
&self,
request: AnthropicRequest
) -> Result<Pin<Box<dyn Stream<Item = Result<Bytes, ProviderError>> + Send>>, ProviderError>;
async fn count_tokens(&self, request: CountTokensRequest) -> Result<CountTokensResponse, ProviderError>;
fn supports_model(&self, model: &str) -> bool;
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "lowercase")]
pub enum AuthType {
ApiKey,
OAuth,
}
impl Default for AuthType {
fn default() -> Self {
AuthType::ApiKey
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProviderConfig {
pub name: String,
pub provider_type: String,
#[serde(default)]
pub auth_type: AuthType,
#[serde(skip_serializing_if = "Option::is_none")]
pub api_key: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub oauth_provider: Option<String>,
pub base_url: Option<String>,
pub models: Vec<String>,
pub enabled: Option<bool>,
}
impl ProviderConfig {
pub fn is_enabled(&self) -> bool {
self.enabled.unwrap_or(true)
}
pub fn get_auth_credential(&self) -> Option<String> {
match self.auth_type {
AuthType::ApiKey => self.api_key.clone(),
AuthType::OAuth => self.oauth_provider.clone(),
}
}
}
pub use openai::OpenAIProvider;
pub use anthropic_compatible::AnthropicCompatibleProvider;
pub use registry::ProviderRegistry;