swiftide-integrations 0.33.0

Composable LLM agents, typed task graphs, and streaming RAG pipelines in Rust
Documentation
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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
use std::fmt;

use async_openai::error::OpenAIError;
use reqwest::StatusCode;
use serde::Deserialize;
use serde_json::Number;
use swiftide_core::chat_completion::errors::LanguageModelError;

// OpenAI transports billing, spend, and quota failures as HTTP 429, but explicitly documents
// them as non-retryable:
// https://developers.openai.com/api/docs/guides/error-codes#api-errors
const OPENAI_NON_RETRYABLE_QUOTA_ERRORS: &[&str] = &[
    "insufficient_quota",
    "credit_balance_exhausted",
    "organization_spend_limit_exceeded",
    "project_spend_limit_exceeded",
    "organization_usage_limit_exceeded",
];

// Bifrost deliberately emits this as HTTP 502 to distinguish broken upstream credentials from
// the caller's Bifrost credentials, but retrying cannot succeed until its configuration changes:
// https://docs.getbifrost.ai/features/retries-and-fallbacks
const BIFROST_NON_RETRYABLE_ERROR_TYPES: &[&str] = &["upstream_credentials_exhausted"];

// Canonical retryable OpenRouter error_type values for Chat Completions:
// https://openrouter.ai/docs/api_reference/errors-and-debugging#typed-error-codes
const OPENROUTER_TRANSIENT_ERROR_TYPES: &[&str] = &[
    "rate_limit_exceeded",
    "provider_overloaded",
    "provider_unavailable",
    "server",
    "timeout",
];

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum ErrorClassification {
    ContextLengthExceeded,
    Permanent,
    Transient,
}

#[derive(Debug, Deserialize)]
struct ProviderErrorResponse {
    error: Option<ProviderErrorBody>,
    #[serde(rename = "type")]
    error_type: Option<String>,
    status_code: Option<u16>,
    #[serde(default)]
    choices: Vec<ProviderErrorChoice>,
}

#[derive(Debug, Deserialize)]
struct ProviderErrorChoice {
    error: Option<ProviderErrorBody>,
}

#[derive(Debug, Deserialize)]
struct ProviderErrorBody {
    message: String,
    code: Option<StringOrNumber>,
    #[serde(rename = "type")]
    api_type: Option<String>,
    error_type: Option<String>,
    metadata: Option<ProviderErrorMetadata>,
}

#[derive(Debug, Default, Deserialize)]
struct ProviderErrorMetadata {
    error_type: Option<String>,
    provider_code: Option<StringOrNumber>,
}

#[derive(Debug, Deserialize)]
#[serde(untagged)]
enum StringOrNumber {
    String(String),
    Number(Number),
}

impl fmt::Display for StringOrNumber {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::String(value) => formatter.write_str(value),
            Self::Number(value) => fmt::Display::fmt(value, formatter),
        }
    }
}

#[derive(Debug)]
struct ProviderError {
    message: String,
    code: Option<String>,
    error_type: Option<String>,
    provider_code: Option<String>,
    status: Option<StatusCode>,
}

impl ProviderError {
    fn from_response(content: &str) -> Option<Self> {
        let response: ProviderErrorResponse = serde_json::from_str(content).ok()?;
        let status = response
            .status_code
            .and_then(|code| StatusCode::from_u16(code).ok());
        let response_error_type = response.error_type;
        let error = response
            .error
            .or_else(|| response.choices.into_iter().find_map(|choice| choice.error))?;
        let metadata = error.metadata.unwrap_or_default();
        let error_type = metadata
            .error_type
            .or(error.error_type)
            .or(error.api_type)
            .or(response_error_type);

        Some(Self {
            message: error.message,
            code: error.code.map(|code| code.to_string()),
            error_type,
            provider_code: metadata.provider_code.map(|code| code.to_string()),
            status,
        })
    }

    fn classification(&self) -> ErrorClassification {
        classify_error(
            self.status
                .or_else(|| status_from_code(self.code.as_deref())),
            [
                self.code.as_deref(),
                self.error_type.as_deref(),
                self.provider_code.as_deref(),
            ],
        )
    }
}

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

        let mut separator = " (";
        for (name, value) in [
            ("code", self.code.as_deref()),
            ("type", self.error_type.as_deref()),
            ("provider code", self.provider_code.as_deref()),
        ] {
            if let Some(value) = value {
                write!(formatter, "{separator}{name}: {value}")?;
                separator = ", ";
            }
        }

        if separator == ", " {
            write!(formatter, ")")?;
        }

        Ok(())
    }
}

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

pub(super) fn openai_error_to_language_model_error(e: OpenAIError) -> LanguageModelError {
    match e {
        OpenAIError::ApiError(api_error) => {
            let classification = classify_error(
                Some(api_error.status_code),
                [
                    api_error.api_error.code.as_deref(),
                    api_error.api_error.r#type.as_deref(),
                    None,
                ],
            );

            map_classification(classification, OpenAIError::ApiError(api_error))
        }
        OpenAIError::Reqwest(error) => {
            // async_openai passes network errors through as reqwest errors.
            LanguageModelError::transient(error)
        }
        OpenAIError::JSONDeserialize(error, content) => {
            if let Some(provider_error) = ProviderError::from_response(&content) {
                map_classification(provider_error.classification(), provider_error)
            } else {
                // Retain the existing transient fallback for malformed success responses.
                LanguageModelError::transient(OpenAIError::JSONDeserialize(error, content))
            }
        }
        OpenAIError::StreamError(stream_error) => {
            LanguageModelError::permanent(OpenAIError::StreamError(stream_error))
        }
        OpenAIError::FileSaveError(_)
        | OpenAIError::FileReadError(_)
        | OpenAIError::InvalidArgument(_) => LanguageModelError::permanent(e),
    }
}

fn status_from_code(code: Option<&str>) -> Option<StatusCode> {
    code.and_then(|code| code.parse::<u16>().ok())
        .and_then(|code| StatusCode::from_u16(code).ok())
}

// Classification combines OpenRouter's canonical in-band error taxonomy with OpenAI's HTTP error
// behavior, in precedence order:
//
// 1. `context_length_exceeded` maps to Swiftide's specialized context-length error.
// 2. Explicit permanent identifiers override otherwise-retryable statuses. OpenAI's documented
//    billing/spend/quota identifiers override HTTP 429 because retrying them cannot succeed
//    without account action. `OPENAI_NON_RETRYABLE_QUOTA_ERRORS` contains the full set currently
//    documented at:
//    https://developers.openai.com/api/docs/guides/error-codes#api-errors
//    Bifrost's `upstream_credentials_exhausted` similarly overrides its synthetic HTTP 502 because
//    it means every configured upstream credential failed permanently:
//    https://docs.getbifrost.ai/features/retries-and-fallbacks
// 3. Other Bifrost gateway errors classify correctly from their top-level `status_code`: documented
//    governance limits use 429, blocked/budget/request failures use 400/402/403, transient gateway
//    failures use 5xx, and cancellation uses 499. Reading the status avoids duplicating its types:
//    https://docs.getbifrost.ai/features/governance/virtual-keys#error-responses
//    https://github.com/maximhq/bifrost/blob/dev/core/schemas/bifrost.go
//    https://github.com/maximhq/bifrost/blob/dev/core/bifrost.go
// 4. `OPENROUTER_TRANSIENT_ERROR_TYPES` is built from every canonical `error_type` in OpenRouter's
//    rate-limiting and availability group (`rate_limit_exceeded`, `provider_overloaded`, and
//    `provider_unavailable`) plus its retryable generic failures (`server` and `timeout`):
//    https://openrouter.ai/docs/api_reference/errors-and-debugging#typed-error-codes
// 5. OpenRouter documents an in-band numeric `error.code` as the equivalent HTTP status, so 408,
//    429, and 5xx are transient just like ordinary HTTP errors.
// 6. All remaining documented OpenRouter types (other token/length, authentication/authorization,
//    request validation, content policy, image, and `unmapped`) default to permanent. OpenRouter
//    transforms the other token/length types into successful Chat Completions with finish_reason
//    `length`, so only `context_length_exceeded` needs special error handling here.
fn classify_error<'a>(
    status: Option<StatusCode>,
    identifiers: impl IntoIterator<Item = Option<&'a str>>,
) -> ErrorClassification {
    let identifiers = identifiers.into_iter().flatten().collect::<Vec<_>>();

    if contains_identifier(&identifiers, &["context_length_exceeded"]) {
        return ErrorClassification::ContextLengthExceeded;
    }

    // Explicit non-retryable types take precedence over a transport status.
    if contains_identifier(&identifiers, OPENAI_NON_RETRYABLE_QUOTA_ERRORS)
        || contains_identifier(&identifiers, BIFROST_NON_RETRYABLE_ERROR_TYPES)
    {
        return ErrorClassification::Permanent;
    }

    if contains_identifier(&identifiers, OPENROUTER_TRANSIENT_ERROR_TYPES)
        || status.is_some_and(|status| {
            status == StatusCode::REQUEST_TIMEOUT
                || status == StatusCode::TOO_MANY_REQUESTS
                || status.is_server_error()
        })
    {
        ErrorClassification::Transient
    } else {
        ErrorClassification::Permanent
    }
}

fn contains_identifier(identifiers: &[&str], expected: &[&str]) -> bool {
    identifiers.iter().any(|identifier| {
        expected
            .iter()
            .any(|expected| identifier.eq_ignore_ascii_case(expected))
    })
}

fn map_classification(
    classification: ErrorClassification,
    error: impl Into<Box<dyn std::error::Error + Send + Sync>>,
) -> LanguageModelError {
    match classification {
        ErrorClassification::ContextLengthExceeded => {
            LanguageModelError::context_length_exceeded(error)
        }
        ErrorClassification::Permanent => LanguageModelError::permanent(error),
        ErrorClassification::Transient => LanguageModelError::transient(error),
    }
}

#[cfg(test)]
mod tests {
    use async_openai::error::{ApiError, ApiErrorResponse};

    use super::*;

    fn json_deserialize_error(content: &str) -> OpenAIError {
        let error = serde_json::from_str::<String>("{").unwrap_err();
        OpenAIError::JSONDeserialize(error, content.to_owned())
    }

    fn api_error(status_code: StatusCode, error_type: &str, code: Option<&str>) -> OpenAIError {
        OpenAIError::ApiError(ApiErrorResponse {
            status_code,
            api_error: ApiError {
                message: "provider message".to_owned(),
                r#type: Some(error_type.to_owned()),
                param: None,
                code: code.map(str::to_owned),
            },
        })
    }

    #[test]
    fn numeric_code_provider_error_is_transient_and_preserves_details() {
        let error = json_deserialize_error(
            r#"{"error":{"message":"Service unavailable.","code":504,"metadata":{"error_type":"timeout"}}}"#,
        );

        let result = openai_error_to_language_model_error(error);
        assert!(matches!(result, LanguageModelError::TransientError(_)));
        let message = result.to_string();
        assert!(message.contains("Service unavailable."));
        assert!(message.contains("504"));
        assert!(message.contains("timeout"));
        assert!(!message.contains("missing field"));
    }

    #[test]
    fn string_rate_limit_code_is_transient() {
        let error = json_deserialize_error(
            r#"{"error":{"message":"Slow down","code":"429","metadata":{"error_type":"rate_limit_exceeded"}}}"#,
        );

        assert!(matches!(
            openai_error_to_language_model_error(error),
            LanguageModelError::TransientError(_)
        ));
    }

    #[test]
    fn partial_completion_provider_error_is_recognized() {
        let error = json_deserialize_error(
            r#"{"choices":[{"finish_reason":"error","error":{"message":"Provider disconnected","code":502,"metadata":{"error_type":"provider_unavailable"}}}]}"#,
        );

        let result = openai_error_to_language_model_error(error);
        assert!(matches!(result, LanguageModelError::TransientError(_)));
        assert!(result.to_string().contains("Provider disconnected"));
    }

    #[test]
    fn openai_quota_errors_are_permanent_even_with_rate_limit_status() {
        for error in [
            json_deserialize_error(
                r#"{"error":{"message":"Buy credits","code":429,"type":"insufficient_quota"}}"#,
            ),
            api_error(StatusCode::TOO_MANY_REQUESTS, "insufficient_quota", None),
        ] {
            assert!(matches!(
                openai_error_to_language_model_error(error),
                LanguageModelError::PermanentError(_)
            ));
        }

        for code in [
            "credit_balance_exhausted",
            "organization_spend_limit_exceeded",
            "project_spend_limit_exceeded",
            "organization_usage_limit_exceeded",
        ] {
            let error = api_error(StatusCode::TOO_MANY_REQUESTS, "billing_error", Some(code));
            assert!(matches!(
                openai_error_to_language_model_error(error),
                LanguageModelError::PermanentError(_)
            ));
        }
    }

    #[test]
    fn openrouter_availability_error_types_are_transient_without_status() {
        for error_type in [
            "rate_limit_exceeded",
            "provider_overloaded",
            "provider_unavailable",
            "server",
            "timeout",
        ] {
            let content = format!(
                r#"{{"error":{{"message":"Try again","metadata":{{"error_type":"{error_type}"}}}}}}"#
            );
            assert!(matches!(
                openai_error_to_language_model_error(json_deserialize_error(&content)),
                LanguageModelError::TransientError(_)
            ));
        }
    }

    #[test]
    fn bifrost_gateway_status_classifies_in_band_errors() {
        for (status, error_type) in [
            (429, "rate_limited"),
            (429, "token_limited"),
            (429, "request_limited"),
            (504, "request_timed_out"),
            (503, "request_dropped"),
            (502, "provider_connection_failed"),
        ] {
            let content = format!(
                r#"{{"status_code":{status},"error":{{"type":"{error_type}","message":"Try again"}}}}"#
            );
            assert!(matches!(
                openai_error_to_language_model_error(json_deserialize_error(&content)),
                LanguageModelError::TransientError(_)
            ));
        }

        let outer_type = json_deserialize_error(
            r#"{"type":"no_eligible_keys","status_code":503,"error":{"message":"Keys are temporarily suppressed"}}"#,
        );
        assert!(matches!(
            openai_error_to_language_model_error(outer_type),
            LanguageModelError::TransientError(_)
        ));
    }

    #[test]
    fn bifrost_exhausted_credentials_is_permanent_despite_bad_gateway_status() {
        for error in [
            json_deserialize_error(
                r#"{"status_code":502,"error":{"type":"upstream_credentials_exhausted","message":"All configured credentials failed"}}"#,
            ),
            api_error(
                StatusCode::BAD_GATEWAY,
                "upstream_credentials_exhausted",
                None,
            ),
        ] {
            assert!(matches!(
                openai_error_to_language_model_error(error),
                LanguageModelError::PermanentError(_)
            ));
        }
    }

    #[test]
    fn context_length_error_from_envelope_is_specialized() {
        let error = json_deserialize_error(
            r#"{"error":{"message":"Too long","code":"context_length_exceeded"}}"#,
        );

        assert!(matches!(
            openai_error_to_language_model_error(error),
            LanguageModelError::ContextLengthExceeded(_)
        ));
    }

    #[test]
    fn exhausted_server_api_error_is_transient() {
        let error = api_error(StatusCode::BAD_GATEWAY, "api_error", None);

        assert!(matches!(
            openai_error_to_language_model_error(error),
            LanguageModelError::TransientError(_)
        ));
    }

    #[test]
    fn non_retryable_http_errors_are_permanent() {
        for status in [
            StatusCode::UNAUTHORIZED,
            StatusCode::PAYMENT_REQUIRED,
            StatusCode::FORBIDDEN,
            StatusCode::UNPROCESSABLE_ENTITY,
        ] {
            let error = api_error(status, "request_error", None);
            assert!(matches!(
                openai_error_to_language_model_error(error),
                LanguageModelError::PermanentError(_)
            ));
        }
    }

    #[test]
    fn malformed_response_fallback_is_transient() {
        let content = "not a chat completion";
        let result = openai_error_to_language_model_error(json_deserialize_error(content));

        assert!(matches!(result, LanguageModelError::TransientError(_)));
        assert!(result.to_string().contains(content));
    }
}