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
// Strategic clippy configuration for comprehensive API client
//! X.AI Grok API client for Rust
//!
//! This crate provides a comprehensive HTTP client for interacting with X.AI's Grok API.
//! It handles authentication, request/response serialization, and streaming support.
//!
//! # Governing Principle : "Thin Client, Rich API"
//!
//! This library follows the principle of **"Thin Client, Rich API"** - exposing all
//! server-side functionality transparently while maintaining zero client-side intelligence
//! or **automatic** behaviors.
//!
//! **Key Distinction**: The principle prohibits **automatic/implicit** behaviors but explicitly
//! **allows and encourages** **explicit/configurable** enterprise reliability features.
//!
//! ## Core Principles
//!
//! - **API Transparency**: One-to-one mapping with X.AI Grok API endpoints
//! - **Zero Automatic Behavior**: No implicit decision-making or magic thresholds
//! - **Explicit Control**: Developer decides when, how, and why operations occur
//! - **Information vs Action**: Clear separation between data retrieval and state changes
//! - **Configurable Reliability**: Enterprise features available through explicit configuration
//!
//! ## `OpenAI` Compatibility
//!
//! The X.AI Grok API is `OpenAI`-compatible, using the same REST endpoint patterns and
//! request/response formats. This allows for easy migration from `OpenAI` to X.AI with minimal
//! code changes.
//!
//! ## Enterprise Reliability Features
//!
//! The following enterprise reliability features are **explicitly allowed** when implemented
//! with explicit configuration and transparent operation:
//!
//! - **Configurable Retry Logic**: Exponential backoff with explicit configuration (feature : `retry`)
//! - **Circuit Breaker Pattern**: Failure threshold management with transparent state (feature : `circuit_breaker`)
//! - **Rate Limiting**: Request throttling with explicit rate configuration (feature : `rate_limiting`)
//! - **Failover Support**: Multi-endpoint configuration and automatic switching (feature : `failover`)
//! - **Health Checks**: Periodic endpoint health verification and monitoring (feature : `health_checks`)
//!
//! ## State Management Policy
//!
//! **✅ ALLOWED: Runtime-Stateful, Process-Stateless**
//! - Connection pools, circuit breaker state, rate limiting buckets
//! - Retry logic state, failover state, health check state
//! - Runtime state that dies with the process
//! - No persistent storage or cross-process state
//!
//! **❌ PROHIBITED: Process-Persistent State**
//! - File storage, databases, configuration accumulation
//! - State that survives process restarts
//!
//! **Implementation Requirements**:
//! - Feature gating behind cargo features (`retry`, `circuit_breaker`, `rate_limiting`, `failover`, `health_checks`)
//! - Explicit configuration required (no automatic enabling)
//! - Transparent method naming (e.g., `execute_with_retries()`, `execute_with_circuit_breaker()`)
//! - Zero overhead when features disabled
//!
//! # Secret Management with `workspace_tools`
//!
//! This crate follows wTools ecosystem conventions by prioritizing `workspace_tools`
//! for secret management over environment variables.
//!
//! ## Recommended : Automatic Fallback Chain
//!
//! The `Secret::load_with_fallbacks()` method tries multiple sources in priority order:
//!
//! 1. **Workspace secrets** (`-secrets.sh`) - primary workspace pattern
//! 2. **Alternative files** (`secrets.sh`, `.env`) - workspace alternatives
//! 3. **Environment variable** - fallback for CI/deployment
//!
//! ## Setup Instructions
//!
//! **Option 1: Workspace Secrets (Recommended)**
//!
//! Create `./secret/-secrets.sh` in your workspace root:
//!
//! ```bash
//! #!/bin/bash
//! export XAI_API_KEY="xai-your-key-here"
//! ```
//!
//! The `workspace_tools` fallback chain searches:
//! 1. `./secret/{filename}` (local workspace)
//! 2. `$PRO/secret/{filename}` (PRO workspace)
//! 3. `$HOME/secret/{filename}` (home directory)
//! 4. Environment variable `$XAI_API_KEY`
//!
//! **Option 2: Environment Variable (CI/Deployment)**
//!
//! ```bash
//! export XAI_API_KEY="xai-your-key-here"
//! ```
//!
//! ## Usage
//!
//! ```no_run
//! use api_xai::Secret;
//!
//! // Recommended : tries all sources (workspace-first)
//! let secret = Secret::load_with_fallbacks( "XAI_API_KEY" )?;
//!
//! // Explicit : load from workspace only
//! let secret = Secret::load_from_workspace( "XAI_API_KEY", "-secrets.sh" )?;
//!
//! // Explicit : load from environment only
//! let secret = Secret::load_from_env( "XAI_API_KEY" )?;
//! # Ok::<(), Box< dyn std::error::Error > >(())
//! ```
//!
//! # Examples
//!
//! ```no_run
//! use api_xai::{ Client, Secret, XaiEnvironmentImpl, ChatCompletionRequest, Message, ClientApiAccessors };
//!
//! # async fn example() -> Result< (), Box< dyn std::error::Error > > {
//! // Create a client
//! let secret = Secret::new( "xai-your-key-here".to_string() )?;
//! let env = XaiEnvironmentImpl::new( secret )?;
//! let client = Client::build( env )?;
//!
//! // Create a chat request using the Former builder
//! let request = ChatCompletionRequest::former()
//! .model( "grok-2-1212".to_string() )
//! .messages( vec![ Message::user( "Hello, Grok! How are you?" ) ] )
//! .form();
//!
//! // Send the request
//! let response = client.chat().create( request ).await?;
//! println!( "Grok responded : {:?}", response.choices[ 0 ].message.content );
//! # Ok( () )
//! # }
//! ```
use mod_interface;
pub use ClientApiAccessors;
cratemod_interface!