cargo-ai 0.3.0

Build lightweight AI agents with Cargo. Powered by Rust. Declared in JSON.
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
//! Shared provider error taxonomy and user-facing diagnostics policy.

use super::runtime::ContentPart;
use reqwest::StatusCode;
use std::fmt;

#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub(crate) enum ProviderKind {
    Ollama,
    OpenAi,
}

impl ProviderKind {
    pub(crate) fn from_server_value(server: &str) -> Option<Self> {
        match server.trim().to_ascii_lowercase().as_str() {
            "ollama" => Some(Self::Ollama),
            "openai" => Some(Self::OpenAi),
            _ => None,
        }
    }

    pub(crate) fn display_name(self) -> &'static str {
        match self {
            Self::Ollama => "Ollama",
            Self::OpenAi => "OpenAI",
        }
    }

    pub(crate) fn default_url(self) -> &'static str {
        match self {
            Self::Ollama => "http://localhost:11434/v1/chat/completions",
            Self::OpenAi => "https://api.openai.com/v1/chat/completions",
        }
    }
}

#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub(crate) enum ProviderErrorKind {
    ModelNotFound,
    Unauthorized,
    RateLimited,
    Timeout,
    Connectivity,
    InvalidRequest,
    InvalidResponse,
    Unknown,
}

#[derive(Debug, Clone)]
pub(crate) struct ProviderError {
    provider: ProviderKind,
    kind: ProviderErrorKind,
    message: String,
}

impl ProviderError {
    pub(crate) fn from_reqwest(provider: ProviderKind, error: reqwest::Error) -> Self {
        let kind = if error.is_timeout() {
            ProviderErrorKind::Timeout
        } else if error.is_connect() {
            ProviderErrorKind::Connectivity
        } else if error.is_request() {
            ProviderErrorKind::InvalidRequest
        } else if error.is_decode() {
            ProviderErrorKind::InvalidResponse
        } else {
            ProviderErrorKind::Unknown
        };

        Self {
            provider,
            kind,
            message: format!("Request failed: {error}"),
        }
    }

    pub(crate) fn from_http_status(provider: ProviderKind, status: StatusCode, body: &str) -> Self {
        Self {
            provider,
            kind: classify_http_status(status, body),
            message: format!("HTTP error {status}: {body}"),
        }
    }

    pub(crate) fn invalid_response(provider: ProviderKind, message: impl Into<String>) -> Self {
        Self {
            provider,
            kind: ProviderErrorKind::InvalidResponse,
            message: message.into(),
        }
    }

    pub(crate) fn provider(&self) -> ProviderKind {
        self.provider
    }

    pub(crate) fn kind(&self) -> ProviderErrorKind {
        self.kind
    }

    pub(crate) fn message(&self) -> &str {
        &self.message
    }
}

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

impl std::error::Error for ProviderError {}

fn classify_http_status(status: StatusCode, body: &str) -> ProviderErrorKind {
    let normalized_body = body.to_ascii_lowercase();
    let is_model_not_found = normalized_body.contains("model")
        && (normalized_body.contains("not found") || normalized_body.contains("does not exist"));

    if status == StatusCode::NOT_FOUND && is_model_not_found {
        return ProviderErrorKind::ModelNotFound;
    }

    match status {
        StatusCode::UNAUTHORIZED | StatusCode::FORBIDDEN => ProviderErrorKind::Unauthorized,
        StatusCode::TOO_MANY_REQUESTS => ProviderErrorKind::RateLimited,
        StatusCode::BAD_REQUEST => ProviderErrorKind::InvalidRequest,
        _ => ProviderErrorKind::Unknown,
    }
}

fn provider_hint(
    kind: ProviderErrorKind,
    provider: ProviderKind,
    message: &str,
) -> Option<&'static str> {
    match kind {
        ProviderErrorKind::ModelNotFound => match provider {
            ProviderKind::Ollama => Some(
                "Run `ollama list` to inspect installed models, then `ollama pull <model>` for missing models.",
            ),
            ProviderKind::OpenAi => {
                Some("Verify the model name and confirm your account has access to it.")
            }
        },
        ProviderErrorKind::Unauthorized => match provider {
            ProviderKind::OpenAi => {
                Some("Verify your OpenAI token (`--token` or profile token), or re-run `cargo ai auth login openai`, and confirm model access.")
            }
            ProviderKind::Ollama => Some(
                "Verify your Ollama endpoint and credentials (if your deployment requires auth).",
            ),
        },
        ProviderErrorKind::RateLimited => match provider {
            ProviderKind::OpenAi => {
                Some("OpenAI rate limit reached; retry later or adjust your account/model limits.")
            }
            ProviderKind::Ollama => Some(
                "Ollama appears rate-limited; retry shortly or reduce concurrent local requests.",
            ),
        },
        ProviderErrorKind::Connectivity => match provider {
            ProviderKind::Ollama => {
                Some("Ensure Ollama is running (`ollama serve`) and the configured URL is reachable.")
            }
            ProviderKind::OpenAi => Some(
                "Check network connectivity and ensure the configured OpenAI URL is reachable.",
            ),
        },
        ProviderErrorKind::Timeout => match provider {
            ProviderKind::Ollama => {
                Some("Request timed out; ensure Ollama/model is responsive or increase `--inference-timeout-in-sec`.")
            }
            ProviderKind::OpenAi => {
                Some("Request timed out; retry later or increase `--inference-timeout-in-sec`.")
            }
        },
        ProviderErrorKind::InvalidRequest => {
            let normalized_message = message.to_ascii_lowercase();
            if normalized_message.contains("file")
                || normalized_message.contains("pdf")
                || normalized_message.contains("docx")
                || normalized_message.contains("csv")
            {
                Some(
                    "The selected provider/model rejected the supplied file input. Verify that the model and endpoint support the current file type, or retry without `file` / `--input-file`.",
                )
            } else {
                Some("Check `--model`, `--url`, and request parameters for invalid values.")
            }
        }
        ProviderErrorKind::InvalidResponse => {
            Some("The provider returned an unexpected response shape; verify model and endpoint compatibility.")
        }
        ProviderErrorKind::Unknown => None,
    }
}

pub(crate) fn provider_error_messages(error: &ProviderError) -> Vec<String> {
    let mut messages = vec![
        format!(
            "❌ Issue communicating with the AI server ({}).",
            error.provider().display_name()
        ),
        format!("Reason: {}", error.message()),
    ];

    if let Some(hint) = provider_hint(error.kind(), error.provider(), error.message()) {
        messages.push(format!("Hint: {hint}"));
    }

    messages
}

pub(crate) fn validate_provider_request(
    provider: ProviderKind,
    model: &str,
    url: &str,
    token: &str,
) -> Result<(), Vec<String>> {
    let mut issues = Vec::new();

    if model.trim().is_empty() {
        issues.push("❌ Missing model. Provide `--model <name>` or configure a default profile with a model.".to_string());
    }

    if url.trim().is_empty() {
        issues.push(format!(
            "❌ Missing URL for {} server.",
            provider.display_name()
        ));
    } else if !(url.starts_with("http://") || url.starts_with("https://")) {
        issues.push(format!(
            "❌ Invalid URL '{}'. Use an absolute URL beginning with `http://` or `https://`.",
            url
        ));
    }

    if provider == ProviderKind::OpenAi && token.trim().is_empty() {
        issues.push(
            "❌ Missing OpenAI token. Provide `--token <TOKEN>`, run `cargo ai auth login openai`, or configure `cargo ai profile set <name> --token <TOKEN> --auth api_key`."
                .to_string(),
        );
    }

    if issues.is_empty() {
        Ok(())
    } else {
        Err(issues)
    }
}

pub(crate) fn validate_provider_content_parts(
    provider: ProviderKind,
    url: &str,
    content_parts: &[ContentPart],
) -> Result<(), Vec<String>> {
    let includes_images = content_parts
        .iter()
        .any(|part| matches!(part, ContentPart::Image { .. }));
    let includes_files = content_parts
        .iter()
        .any(|part| matches!(part, ContentPart::File { .. }));

    if !includes_images && !includes_files {
        return Ok(());
    }

    let normalized_url = url.trim().to_ascii_lowercase();
    let mut issues = Vec::new();

    if provider == ProviderKind::Ollama
        && (normalized_url.contains("/api/generate") || normalized_url.contains("/api/chat"))
    {
        if includes_images {
            issues.push(
                "❌ Image inputs require Ollama's OpenAI-compatible `/v1/chat/completions` transport. Update `--url` or your profile URL before retrying."
                    .to_string(),
            );
        }
        if includes_files {
            issues.push(
                "❌ File inputs require a transport that accepts OpenAI-style file content parts. Ollama `/api/generate` and `/api/chat` are not compatible with `file` / `--input-file`."
                    .to_string(),
            );
        }
    }

    if issues.is_empty() {
        Ok(())
    } else {
        Err(issues)
    }
}

#[cfg(test)]
mod tests {
    use super::{
        provider_error_messages, validate_provider_content_parts, validate_provider_request,
        ProviderError, ProviderKind,
    };
    use crate::providers::runtime::ContentPart;
    use reqwest::StatusCode;
    use tokio::net::TcpListener;

    #[test]
    fn parses_provider_kind_from_server_value() {
        assert_eq!(
            ProviderKind::from_server_value("ollama"),
            Some(ProviderKind::Ollama)
        );
        assert_eq!(
            ProviderKind::from_server_value("OPENAI"),
            Some(ProviderKind::OpenAi)
        );
        assert_eq!(ProviderKind::from_server_value("wat"), None);
    }

    #[test]
    fn classifies_model_not_found_from_http_status() {
        let error = ProviderError::from_http_status(
            ProviderKind::Ollama,
            StatusCode::NOT_FOUND,
            "{\"error\":\"model 'mixtral' not found\"}",
        );
        let messages = provider_error_messages(&error);
        assert!(messages
            .iter()
            .any(|line| line.contains("Issue communicating with the AI server (Ollama)")));
        assert!(messages
            .iter()
            .any(|line| line.contains("ollama pull <model>")));
    }

    #[test]
    fn classifies_unauthorized_with_openai_hint() {
        let error = ProviderError::from_http_status(
            ProviderKind::OpenAi,
            StatusCode::UNAUTHORIZED,
            "{\"error\":\"invalid api key\"}",
        );
        let messages = provider_error_messages(&error);
        assert!(messages
            .iter()
            .any(|line| line.contains("Issue communicating with the AI server (OpenAI)")));
        assert!(messages
            .iter()
            .any(|line| line.contains("Verify your OpenAI token")));
    }

    #[test]
    fn validates_openai_token_requirement() {
        let issues = validate_provider_request(
            ProviderKind::OpenAi,
            "gpt-4o-mini",
            "https://api.openai.com/v1/chat/completions",
            "",
        )
        .expect_err("expected token validation failure");
        assert!(issues
            .iter()
            .any(|line| line.contains("Missing OpenAI token")));
    }

    #[test]
    fn invalid_response_uses_actionable_hint() {
        let error = ProviderError::invalid_response(
            ProviderKind::OpenAi,
            "Failed to parse JSON from provider",
        );
        let messages = provider_error_messages(&error);
        assert!(messages
            .iter()
            .any(|line| line.contains("unexpected response shape")));
    }

    #[test]
    fn invalid_request_with_file_input_uses_file_specific_hint() {
        let error = ProviderError::from_http_status(
            ProviderKind::OpenAi,
            StatusCode::BAD_REQUEST,
            "{\"error\":\"file inputs are not supported for this model\"}",
        );
        let messages = provider_error_messages(&error);
        assert!(messages
            .iter()
            .any(|line| line.contains("rejected the supplied file input")));
    }

    #[test]
    fn rejects_file_inputs_on_non_openai_ollama_transport() {
        let issues = validate_provider_content_parts(
            ProviderKind::Ollama,
            "http://localhost:11434/api/chat",
            &[ContentPart::File {
                filename: "report.pdf".to_string(),
                file_data: "data:application/pdf;base64,JVBERi0xLjQK".to_string(),
            }],
        )
        .expect_err("expected transport validation failure");

        assert!(issues
            .iter()
            .any(|line| line.contains("File inputs require a transport")));
    }

    #[tokio::test]
    async fn classifies_connectivity_reqwest_errors() {
        let listener = TcpListener::bind("127.0.0.1:0")
            .await
            .expect("bind ephemeral port");
        let addr = listener.local_addr().expect("capture local address");
        drop(listener); // no server listening now -> connection refused

        let request_error = reqwest::Client::new()
            .get(format!("http://{addr}/"))
            .send()
            .await
            .expect_err("request should fail with connectivity error");

        let provider_error = ProviderError::from_reqwest(ProviderKind::Ollama, request_error);
        assert_eq!(
            provider_error.kind(),
            super::ProviderErrorKind::Connectivity
        );
    }
}