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
//! Protocol-based provider implementations
//!
//! This module organizes providers by the API protocol they follow, rather than
//! by individual provider names. This approach recognizes that many providers
//! implement the same underlying protocol (especially OpenAI-compatible APIs).
//!
//! # Architecture Overview
//!
//! ```text
//! ┌─────────────────────────────────────────────────────────────┐
//! │ Application Layer │
//! │ (Client, ProviderRegistry) │
//! └─────────────────────────────────────────────────────────────┘
//! │
//! ▼
//! ┌─────────────────────────────────────────────────────────────┐
//! │ Provider Trait │
//! │ (Public API: chat(), chat_stream()) │
//! └─────────────────────────────────────────────────────────────┘
//! │
//! ▼
//! ┌─────────────────────────────────────────────────────────────┐
//! │ GenericProvider<Adapter> │
//! │ (Universal implementation for all protocols) │
//! └─────────────────────────────────────────────────────────────┘
//! │
//! ┌─────────────┼─────────────┐
//! ▼ ▼ ▼
//! ┌──────────────┐ ┌──────────────┐ ┌──────────────┐
//! │ OpenAI │ │ Anthropic │ │ Aliyun │
//! │ Protocol │ │ Protocol │ │ Protocol │
//! └──────────────┘ └──────────────┘ └──────────────┘
//! │ │ │
//! ┌──────┴────┐ │ │
//! ▼ ▼ ▼ ▼
//! DeepSeek Zhipu Claude Qwen
//! Moonshot Kimi
//! VolcEngine
//! Tencent
//! MiniMax
//! StepFun
//! LongCat
//! ```
//!
//! # Module Structure
//!
//! ## Core Module (`core.rs`)
//!
//! Defines the fundamental abstractions:
//! - **`Provider`** - Public trait for external API
//! - **`ProviderAdapter`** - Internal trait for protocol implementations
//! - **`GenericProvider<A>`** - Universal provider implementation
//! - **`HttpTransport`** - Shared HTTP client and configuration
//! - **`ErrorMapper`** - Protocol-specific error handling
//!
//! ## Protocol Modules
//!
//! ### OpenAI Protocol (`openai.rs`)
//!
//! The most widely adopted protocol, used by 8 providers:
//! - **DeepSeek** - `deepseek()` - DeepSeek-V3, DeepSeek-Chat
//! - **Zhipu (GLM)** - `zhipu()` - GLM-4, GLM-4-Plus, GLM-4-Flash
//! - **Moonshot (Kimi)** - `moonshot()` - Moonshot-v1 series
//! - **VolcEngine (Doubao)** - `volcengine()` - Doubao models
//! - **Tencent (Hunyuan)** - `tencent()` - Hunyuan models
//! - **MiniMax** - `minimax()` - MiniMax models
//! - **StepFun** - `stepfun()` - Step series models
//! - **LongCat** - `longcat()` - Free quota available for testing
//!
//! **Endpoint**: `POST /v1/chat/completions`
//!
//! ### Anthropic Protocol (`anthropic.rs`)
//!
//! Used by Anthropic's Claude models:
//! - **Claude** - `claude()` - Claude 3.5 Sonnet, Claude 3 Opus, Claude 3 Haiku
//!
//! **Endpoint**: `POST /v1/messages`
//!
//! **Key Differences**:
//! - Separate `system` field instead of system message
//! - Required `max_tokens` field
//! - Response content is an array of blocks
//! - Different streaming event types
//!
//! ### Aliyun Protocol (`aliyun.rs`)
//!
//! Custom protocol used by Aliyun DashScope:
//! - **Qwen** - `qwen()` - Qwen-Max, Qwen-Plus, Qwen-Turbo
//!
//! **Endpoint**: `POST /services/aigc/text-generation/generation`
//!
//! **Key Differences**:
//! - Nested `input` and `parameters` structure
//! - Response data in `output.choices`
//! - Different field names for usage statistics
//!
//! ## Factory Module (`factory.rs`)
//!
//! Provides dynamic protocol creation for YAML configuration:
//! - **`ProtocolFactory`** - Trait for creating protocol adapters
//! - **`ProtocolFactoryRegistry`** - Manages all protocol factories
//! - **Built-in factories**: OpenAI, Anthropic, Aliyun
//!
//! # Usage Examples
//!
//! ## Direct Provider Creation
//!
//! ```rust
//! use llm_connector::{
//! config::ProviderConfig,
//! protocols::{core::GenericProvider, openai::deepseek},
//! };
//!
//! # async fn example() -> Result<(), Box<dyn std::error::Error>> {
//! let config = ProviderConfig::new("your-api-key");
//! let provider = GenericProvider::new(config, deepseek())?;
//! # Ok(())
//! # }
//! ```
//!
//! ## Using Provider Registry
//!
//! ```rust,no_run
//! use llm_connector::config::RegistryConfig;
//! use llm_connector::registry::ProviderRegistry;
//!
//! # async fn example() -> Result<(), Box<dyn std::error::Error>> {
//! let config = RegistryConfig::from_yaml_file("config.yaml")?;
//! let registry = ProviderRegistry::from_config(config)?;
//! let provider = registry.get("deepseek").unwrap();
//! # Ok(())
//! # }
//! ```
//!
//! # Adding New Providers
//!
//! ## For OpenAI-Compatible Providers (3 lines)
//!
//! ```rust
//! use llm_connector::protocols::openai::OpenAIProtocol;
//!
//! pub fn my_provider() -> OpenAIProtocol {
//! OpenAIProtocol::new("my-provider", "https://api.example.com/v1", vec!["model-1"])
//! }
//! ```
//!
//! ## For Custom Protocols (~300 lines)
//!
//! Only needed if the provider uses a truly different protocol:
//! 1. Define request/response structures
//! 2. Implement `ProviderAdapter` trait
//! 3. Implement `ErrorMapper` trait
//! 4. Create factory (optional, for YAML support)
// Re-export core components
pub use ;
// Re-export protocols
pub use ;
pub use ;
pub use ;
// Re-export factory
pub use ;
// Re-export configuration from the unified config module
pub use crate;