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
//! AI provider abstraction
//!
//! Defines the AsyncAiProvider enum, AiError types, and factory for creating provider instances.
//! Uses async/await with tokio for non-blocking streaming and CancellationToken for request cancellation.
use std::sync::mpsc::Sender;
use thiserror::Error;
use tokio_util::sync::CancellationToken;
use crate::ai::ai_state::AiResponse;
use crate::config::ai_types::{AiConfig, AiProviderType};
mod async_anthropic;
mod async_bedrock;
mod async_gemini;
mod async_openai;
mod sse;
pub use async_anthropic::AsyncAnthropicClient;
pub use async_bedrock::AsyncBedrockClient;
pub use async_gemini::AsyncGeminiClient;
pub use async_openai::AsyncOpenAiClient;
/// Errors that can occur during AI operations
#[derive(Debug, Error)]
#[non_exhaustive]
pub enum AiError {
/// AI is not configured (missing API key or disabled)
#[error("[{provider}] AI not configured: {message}")]
NotConfigured { provider: String, message: String },
/// Network error during API request
#[error("[{provider}] Network error: {message}")]
Network { provider: String, message: String },
/// API returned an error response
#[error("[{provider}] API error ({code}): {message}")]
Api {
provider: String,
code: u16,
message: String,
},
/// Failed to parse API response
#[error("[{provider}] Parse error: {message}")]
Parse { provider: String, message: String },
/// AWS SDK error (Bedrock-specific)
#[error("[Bedrock] AWS SDK error: {0}")]
AwsSdk(String),
/// Request was cancelled
#[error("Request cancelled")]
Cancelled,
}
/// Async AI provider implementations with cancellation support
///
/// Uses async/await with tokio for non-blocking streaming and
/// CancellationToken for request cancellation.
#[derive(Debug, Clone)]
pub enum AsyncAiProvider {
/// Anthropic Claude API (async)
Anthropic(AsyncAnthropicClient),
/// AWS Bedrock API (async)
Bedrock(AsyncBedrockClient),
/// OpenAI API (async)
Openai(AsyncOpenAiClient),
/// Google Gemini API (async)
Gemini(AsyncGeminiClient),
}
impl AsyncAiProvider {
/// Returns the display name of the provider
pub fn provider_name(&self) -> &'static str {
match self {
AsyncAiProvider::Anthropic(_) => "Anthropic",
AsyncAiProvider::Bedrock(_) => "Bedrock",
AsyncAiProvider::Openai(client) => {
if client.is_custom_endpoint() {
"OpenAI-compatible"
} else {
"OpenAI"
}
}
AsyncAiProvider::Gemini(_) => "Gemini",
}
}
/// Create an async AI provider from configuration
///
/// Returns an error if the configuration is invalid (e.g., missing API key)
pub fn from_config(config: &AiConfig) -> Result<Self, AiError> {
// Check if provider is configured
let provider_type = config.provider.ok_or_else(|| AiError::NotConfigured {
provider: "None".to_string(),
message:
"No AI provider configured. See https://github.com/bellicose100xp/jiq#configuration"
.to_string(),
})?;
if !config.enabled {
let provider_name = match provider_type {
AiProviderType::Anthropic => "Anthropic",
AiProviderType::Bedrock => "Bedrock",
AiProviderType::Openai => "OpenAI",
AiProviderType::Gemini => "Gemini",
};
return Err(AiError::NotConfigured {
provider: provider_name.to_string(),
message: format!(
"AI is disabled. Set 'enabled = true' in [ai] section with provider = \"{}\". See https://github.com/bellicose100xp/jiq#configuration for setup instructions.",
provider_name.to_lowercase()
),
});
}
match provider_type {
AiProviderType::Anthropic => {
let api_key = config
.anthropic
.api_key
.as_ref()
.filter(|k| !k.trim().is_empty())
.ok_or_else(|| AiError::NotConfigured {
provider: "Anthropic".to_string(),
message: "Missing API key. Add 'api_key' in [ai.anthropic] section. Get your key from https://console.anthropic.com/settings/keys. See https://github.com/bellicose100xp/jiq#configuration for full setup.".to_string(),
})?;
let model = config
.anthropic
.model
.as_ref()
.filter(|m| !m.trim().is_empty())
.ok_or_else(|| AiError::NotConfigured {
provider: "Anthropic".to_string(),
message: "Missing model. Add 'model' in [ai.anthropic] section (e.g., 'claude-haiku-4-5-20251001'). See https://github.com/bellicose100xp/jiq#configuration for examples.".to_string(),
})?;
let provider = AsyncAiProvider::Anthropic(AsyncAnthropicClient::new(
api_key.clone(),
model.clone(),
config.anthropic.max_tokens,
));
// Use provider_name to avoid dead code warning
let _ = provider.provider_name();
Ok(provider)
}
AiProviderType::Bedrock => {
let region = config
.bedrock
.region
.as_ref()
.filter(|r| !r.trim().is_empty())
.ok_or_else(|| AiError::NotConfigured {
provider: "Bedrock".to_string(),
message: "Missing region. Add 'region' in [ai.bedrock] section (e.g., 'us-east-1'). Ensure AWS credentials are configured via environment variables or ~/.aws/credentials. See https://github.com/bellicose100xp/jiq#configuration for setup.".to_string(),
})?;
let model = config
.bedrock
.model
.as_ref()
.filter(|m| !m.trim().is_empty())
.ok_or_else(|| AiError::NotConfigured {
provider: "Bedrock".to_string(),
message: "Missing model. Add 'model' in [ai.bedrock] section (e.g., 'anthropic.claude-3-haiku-20240307-v1:0'). See https://github.com/bellicose100xp/jiq#configuration for examples.".to_string(),
})?;
let provider = AsyncAiProvider::Bedrock(AsyncBedrockClient::new(
region.clone(),
model.clone(),
config.bedrock.profile.clone(),
));
// Use provider_name to avoid dead code warning
let _ = provider.provider_name();
Ok(provider)
}
AiProviderType::Openai => {
// Helper: check if URL is OpenAI's API
let is_openai_url = config
.openai
.base_url
.as_ref()
.map(|u| u.contains("api.openai.com"))
.unwrap_or(true); // default (None) = OpenAI
// API key required if using OpenAI (no base_url OR base_url is api.openai.com)
let api_key = if is_openai_url {
config
.openai
.api_key
.as_ref()
.filter(|k| !k.trim().is_empty())
.ok_or_else(|| AiError::NotConfigured {
provider: "OpenAI".to_string(),
message: "Missing API key. Add 'api_key' in [ai.openai] section."
.to_string(),
})?
.clone()
} else {
config.openai.api_key.clone().unwrap_or_default()
};
let model = config
.openai
.model
.as_ref()
.filter(|m| !m.trim().is_empty())
.ok_or_else(|| AiError::NotConfigured {
provider: "OpenAI".to_string(),
message: "Missing model. Add 'model' in [ai.openai] section.".to_string(),
})?;
let provider = AsyncAiProvider::Openai(AsyncOpenAiClient::new(
api_key,
model.clone(),
config.openai.base_url.clone(),
));
// Use provider_name to avoid dead code warning
let _ = provider.provider_name();
Ok(provider)
}
AiProviderType::Gemini => {
let api_key = config
.gemini
.api_key
.as_ref()
.filter(|k| !k.trim().is_empty())
.ok_or_else(|| AiError::NotConfigured {
provider: "Gemini".to_string(),
message: "Missing API key. Add 'api_key' in [ai.gemini] section."
.to_string(),
})?;
let model = config
.gemini
.model
.as_ref()
.filter(|m| !m.trim().is_empty())
.ok_or_else(|| AiError::NotConfigured {
provider: "Gemini".to_string(),
message: "Missing model. Add 'model' in [ai.gemini] section.".to_string(),
})?;
let provider =
AsyncAiProvider::Gemini(AsyncGeminiClient::new(api_key.clone(), model.clone()));
// Use provider_name to avoid dead code warning
let _ = provider.provider_name();
Ok(provider)
}
}
}
/// Stream a response from the AI provider with cancellation support
///
/// Uses async streaming and sends chunks via the response channel.
/// Can be cancelled via the CancellationToken.
///
/// # Arguments
/// * `prompt` - The prompt to send to the API
/// * `request_id` - Unique ID for this request
/// * `cancel_token` - Token to cancel the request
/// * `response_tx` - Channel to send response chunks
///
/// # Returns
/// * `Ok(())` - Stream completed successfully
/// * `Err(AiError::Cancelled)` - Request was cancelled
/// * `Err(AiError::*)` - Other errors
pub async fn stream_with_cancel(
&self,
prompt: &str,
request_id: u64,
cancel_token: CancellationToken,
response_tx: Sender<AiResponse>,
) -> Result<(), AiError> {
match self {
AsyncAiProvider::Anthropic(client) => {
client
.stream_with_cancel(prompt, request_id, cancel_token, response_tx)
.await
}
AsyncAiProvider::Bedrock(client) => {
client
.stream_with_cancel(prompt, request_id, cancel_token, response_tx)
.await
}
AsyncAiProvider::Openai(client) => {
client
.stream_with_cancel(prompt, request_id, cancel_token, response_tx)
.await
}
AsyncAiProvider::Gemini(client) => {
client
.stream_with_cancel(prompt, request_id, cancel_token, response_tx)
.await
}
}
}
}
#[cfg(test)]
#[path = "provider_tests.rs"]
mod provider_tests;