oxi-ai 0.63.0

Unified LLM API — multi-provider streaming interface for AI coding assistants
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
//! GitLab Duo provider — REST API proxy for GitLab AI Gateway.
//!
//! Port of omp `packages/ai/src/providers/gitlab-duo.ts` (399 lines).
//!
//! GitLab Duo is a **REST proxy** that:
//! 1. Authenticates with the GitLab access token (personal/group token)
//! 2. Exchanges it for a "direct access" token via GitLab's REST API
//! 3. Delegates to the Anthropic or OpenAI provider with the proxy base URL
//!
//! This is NOT the WebSocket-based `gitlab-duo-agent` workflow provider.
//! That's a separate, more complex provider (`gitlab-duo-workflow.ts`) that
//! requires WebSocket + protobuf infra and is tracked separately.
//!
//! ## Protocol
//! - REST: POST `/api/v4/ai/third_party_agents/direct_access` → direct access token
//! - LLM: Anthropic Messages API or OpenAI Chat/Responses API via AI Gateway
//! - Auth: GitLab personal access token → direct access JWT
//!
//! ## Model mappings
//! The `MODEL_MAPPINGS` table maps Duo model IDs (e.g. `duo-chat-sonnet-4-5`)
//! to upstream provider + model ID + proxy base URL.

use std::future::Future;
use std::pin::Pin;
use std::sync::Mutex;
use std::time::{Duration, Instant};

use super::anthropic::AnthropicProvider;
use super::openai::OpenAiProvider;
use super::openai_responses::OpenAiResponsesProvider;
use crate::{
    Context, HttpErrorDetail, Model, Provider, StreamOptions, StreamResult, error::ProviderError,
};

// ── Constants ───────────────────────────────────────────────────────

const GITLAB_COM_URL: &str = "https://gitlab.com";
const ANTHROPIC_PROXY_URL: &str = "https://cloud.gitlab.com/ai/v1/proxy/anthropic/";
const OPENAI_PROXY_URL: &str = "https://cloud.gitlab.com/ai/v1/proxy/openai/v1";
const DIRECT_ACCESS_TTL_MS: u64 = 25 * 60 * 1000; // 25 minutes
const DIRECT_ACCESS_PATH: &str = "/api/v4/ai/third_party_agents/direct_access";

// ── Model mappings ──────────────────────────────────────────────────

/// Which upstream provider a GitLab Duo model delegates to.
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum GitLabProviderType {
    Anthropic,
    OpenAi,
}

/// OpenAI API subtype for GitLab Duo models.
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum GitLabOpenAIApiType {
    Chat,
    Responses,
}

/// Mapping from a GitLab Duo model ID to its upstream configuration.
#[derive(Clone, Debug)]
pub struct GitLabModelMapping {
    pub provider: GitLabProviderType,
    pub model: &'static str,
    pub openai_api_type: Option<GitLabOpenAIApiType>,
    #[allow(dead_code)]
    pub name: &'static str,
    #[allow(dead_code)]
    pub reasoning: bool,
}

/// GitLab Duo model ID to upstream model/proxy mapping.
static MODEL_MAPPINGS: &[(&str, GitLabModelMapping)] = &[
    (
        "duo-chat-opus-4-6",
        GitLabModelMapping {
            provider: GitLabProviderType::Anthropic,
            model: "claude-opus-4-6",
            openai_api_type: None,
            name: "Duo Chat Opus 4.6",
            reasoning: true,
        },
    ),
    (
        "duo-chat-sonnet-4-6",
        GitLabModelMapping {
            provider: GitLabProviderType::Anthropic,
            model: "claude-sonnet-4-6",
            openai_api_type: None,
            name: "Duo Chat Sonnet 4.6",
            reasoning: true,
        },
    ),
    (
        "duo-chat-opus-4-5",
        GitLabModelMapping {
            provider: GitLabProviderType::Anthropic,
            model: "claude-opus-4-5-20251101",
            openai_api_type: None,
            name: "Duo Chat Opus 4.5",
            reasoning: true,
        },
    ),
    (
        "duo-chat-sonnet-4-5",
        GitLabModelMapping {
            provider: GitLabProviderType::Anthropic,
            model: "claude-sonnet-4-5-20250929",
            openai_api_type: None,
            name: "Duo Chat Sonnet 4.5",
            reasoning: true,
        },
    ),
    (
        "duo-chat-haiku-4-5",
        GitLabModelMapping {
            provider: GitLabProviderType::Anthropic,
            model: "claude-haiku-4-5-20251001",
            openai_api_type: None,
            name: "Duo Chat Haiku 4.5",
            reasoning: true,
        },
    ),
    (
        "duo-chat-gpt-5-1",
        GitLabModelMapping {
            provider: GitLabProviderType::OpenAi,
            model: "gpt-5.1-2025-11-13",
            openai_api_type: Some(GitLabOpenAIApiType::Chat),
            name: "Duo Chat GPT-5.1",
            reasoning: true,
        },
    ),
    (
        "duo-chat-gpt-5-2",
        GitLabModelMapping {
            provider: GitLabProviderType::OpenAi,
            model: "gpt-5.2-2025-12-11",
            openai_api_type: Some(GitLabOpenAIApiType::Chat),
            name: "Duo Chat GPT-5.2",
            reasoning: true,
        },
    ),
    (
        "duo-chat-gpt-5-mini",
        GitLabModelMapping {
            provider: GitLabProviderType::OpenAi,
            model: "gpt-5-mini-2025-08-07",
            openai_api_type: Some(GitLabOpenAIApiType::Chat),
            name: "Duo Chat GPT-5 Mini",
            reasoning: true,
        },
    ),
    (
        "duo-chat-gpt-5-codex",
        GitLabModelMapping {
            provider: GitLabProviderType::OpenAi,
            model: "gpt-5-codex",
            openai_api_type: Some(GitLabOpenAIApiType::Responses),
            name: "Duo Chat GPT-5 Codex",
            reasoning: true,
        },
    ),
    (
        "duo-chat-gpt-5-2-codex",
        GitLabModelMapping {
            provider: GitLabProviderType::OpenAi,
            model: "gpt-5.2-codex",
            openai_api_type: Some(GitLabOpenAIApiType::Responses),
            name: "Duo Chat GPT-5.2 Codex",
            reasoning: true,
        },
    ),
];

/// Look up a GitLab Duo model mapping by model ID.
pub fn get_model_mapping(model_id: &str) -> Option<&'static GitLabModelMapping> {
    if let Some((_, mapping)) = MODEL_MAPPINGS.iter().find(|(id, _)| *id == model_id) {
        return Some(mapping);
    }
    MODEL_MAPPINGS
        .iter()
        .find(|(_, m)| m.model == model_id)
        .map(|(_, m)| m)
}

// ── Direct access token cache ───────────────────────────────────────

struct CachedDirectAccess {
    token: String,
    headers: Vec<(String, String)>,
    expires_at: Instant,
}

static DIRECT_ACCESS_CACHE: std::sync::LazyLock<Mutex<Option<CachedDirectAccess>>> =
    std::sync::LazyLock::new(|| Mutex::new(None));

fn get_cached_direct_access() -> Option<CachedDirectAccess> {
    let cache = DIRECT_ACCESS_CACHE.lock().ok()?;
    let cached = cache.as_ref()?;
    if cached.expires_at > Instant::now() {
        Some(CachedDirectAccess {
            token: cached.token.clone(),
            headers: cached.headers.clone(),
            expires_at: cached.expires_at,
        })
    } else {
        None
    }
}

fn set_cached_direct_access(token: String, headers: Vec<(String, String)>) {
    if let Ok(mut cache) = DIRECT_ACCESS_CACHE.lock() {
        *cache = Some(CachedDirectAccess {
            token,
            headers,
            expires_at: Instant::now() + Duration::from_millis(DIRECT_ACCESS_TTL_MS),
        });
    }
}

#[allow(dead_code)]
fn clear_direct_access_cache() {
    if let Ok(mut cache) = DIRECT_ACCESS_CACHE.lock() {
        *cache = None;
    }
}

// ── GitLab Duo Provider ─────────────────────────────────────────────

/// GitLab Duo provider — delegates to Anthropic/OpenAI through GitLab's AI Gateway.
///
/// This is a **REST proxy** provider. It handles GitLab Duo auth (direct access
/// token exchange) and delegates the actual LLM call to the appropriate upstream
/// provider (Anthropic Messages API or OpenAI Chat/Responses API) with the proxy
/// base URL set to GitLab's AI Gateway.
///
/// **Not** to be confused with `GitLabDuoAgent` (WebSocket workflow provider),
/// which is a separate provider tracked separately.
#[derive(Clone)]
pub struct GitLabDuoProvider {
    gitlab_token: Option<String>,
}

impl GitLabDuoProvider {
    pub fn new() -> Self {
        Self { gitlab_token: None }
    }

    #[allow(dead_code)]
    pub fn with_gitlab_token(token: impl Into<String>) -> Self {
        Self {
            gitlab_token: Some(token.into()),
        }
    }
}

impl Default for GitLabDuoProvider {
    fn default() -> Self {
        Self::new()
    }
}

impl Provider for GitLabDuoProvider {
    fn stream<'a>(
        &'a self,
        model: &'a Model,
        context: &'a Context,
        options: Option<StreamOptions>,
    ) -> Pin<Box<dyn Future<Output = StreamResult> + Send + 'a>> {
        // Clone the Arc'd fields we need outside the async block.
        let gitlab_token = self.gitlab_token.clone();
        let model_id = model.id.clone();

        Box::pin(async move {
            // 1. Resolve model mapping
            let mapping = get_model_mapping(&model_id).ok_or_else(|| {
                ProviderError::UnknownProvider(format!(
                    "Unknown GitLab Duo model: {}. Supported models: duo-chat-*",
                    model_id
                ))
            })?;

            // 2. Resolve GitLab access token
            let token = gitlab_token
                .or_else(|| std::env::var("GITLAB_TOKEN").ok())
                .or_else(|| std::env::var("GITLAB_ACCESS_TOKEN").ok())
                .ok_or_else(|| {
                    ProviderError::InvalidResponse(
                        "GitLab access token required. Set GITLAB_TOKEN or provide via options."
                            .to_string(),
                    )
                })?;

            // 3. Exchange for direct access token (with cache)
            let creds = get_or_fetch_direct_access(&token).await?;

            // 4. Determine proxy URL and delegate
            match mapping.provider {
                GitLabProviderType::Anthropic => {
                    let inner = AnthropicProvider::with_config(
                        ANTHROPIC_PROXY_URL,
                        Some(creds.token),
                        creds.headers,
                    );
                    let inner_model = Model {
                        id: mapping.model.to_string(),
                        api: crate::Api::AnthropicMessages,
                        provider: "anthropic".to_string(),
                        base_url: ANTHROPIC_PROXY_URL.to_string(),
                        ..model.clone()
                    };
                    inner.stream(&inner_model, context, options).await
                }
                GitLabProviderType::OpenAi => match mapping.openai_api_type {
                    Some(GitLabOpenAIApiType::Responses) => {
                        let inner = OpenAiResponsesProvider::with_base_url_and_key(
                            OPENAI_PROXY_URL,
                            Some(creds.token),
                        );
                        let inner_model = Model {
                            id: mapping.model.to_string(),
                            api: crate::Api::OpenAiResponses,
                            provider: "openai".to_string(),
                            base_url: OPENAI_PROXY_URL.to_string(),
                            ..model.clone()
                        };
                        inner.stream(&inner_model, context, options).await
                    }
                    _ => {
                        let inner = OpenAiProvider::with_base_url_and_key(
                            OPENAI_PROXY_URL,
                            Some(creds.token),
                        );
                        let inner_model = Model {
                            id: mapping.model.to_string(),
                            api: crate::Api::OpenAiCompletions,
                            provider: "openai".to_string(),
                            base_url: OPENAI_PROXY_URL.to_string(),
                            ..model.clone()
                        };
                        inner.stream(&inner_model, context, options).await
                    }
                },
            }
        })
    }
}

// ── Auth ────────────────────────────────────────────────────────────

/// Direct access token response from GitLab's REST API.
#[derive(serde::Deserialize)]
struct DirectAccessResponse {
    token: Option<String>,
    headers: Option<std::collections::HashMap<String, String>>,
}

/// Fetch a direct access token from GitLab's REST API, with caching.
async fn get_or_fetch_direct_access(
    gitlab_token: &str,
) -> Result<DirectAccessCredentials, ProviderError> {
    if let Some(cached) = get_cached_direct_access() {
        return Ok(DirectAccessCredentials {
            token: cached.token,
            headers: cached.headers,
        });
    }

    let client = super::shared_client();

    let response = client
        .post(format!("{}{}", GITLAB_COM_URL, DIRECT_ACCESS_PATH))
        .header("Authorization", format!("Bearer {}", gitlab_token))
        .header("Content-Type", "application/json")
        .json(&serde_json::json!({
            "feature_flags": { "DuoAgentPlatformNext": true }
        }))
        .send()
        .await
        .map_err(|e| ProviderError::NetworkError(format!("Request failed: {}", e)))?;

    if !response.status().is_success() {
        let status = response.status();
        let body = response.text().await.unwrap_or_default();
        return if status == 403 {
            Err(ProviderError::HttpError(HttpErrorDetail {
                status: 403,
                body: format!("GitLab Duo access denied. Ensure Duo is enabled. {}", body),
                provider: Some("gitlab-duo".to_string()),
                request_id: None,
            }))
        } else {
            Err(ProviderError::HttpError(HttpErrorDetail {
                status: status.as_u16(),
                body,
                provider: Some("gitlab-duo".to_string()),
                request_id: None,
            }))
        };
    }

    let payload: DirectAccessResponse = response.json().await.map_err(|e| {
        ProviderError::InvalidResponse(format!(
            "Failed to parse GitLab Duo direct access response: {}",
            e
        ))
    })?;

    let token = payload.token.ok_or_else(|| {
        ProviderError::InvalidResponse(
            "GitLab Duo direct access response missing token".to_string(),
        )
    })?;

    let headers: Vec<(String, String)> = payload.headers.unwrap_or_default().into_iter().collect();

    let creds = DirectAccessCredentials {
        token: token.clone(),
        headers: headers.clone(),
    };

    set_cached_direct_access(token, headers);
    Ok(creds)
}

/// Credentials returned from the direct access token exchange.
#[derive(Clone, Debug)]
struct DirectAccessCredentials {
    token: String,
    headers: Vec<(String, String)>,
}

/// Clear the direct access token cache (for testing).
#[allow(dead_code)]
pub fn clear_token_cache() {
    clear_direct_access_cache();
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_gitlab_duo_provider_creation() {
        let provider = GitLabDuoProvider::new();
        let _ = provider;
    }

    #[test]
    fn test_model_mapping_direct_lookup() {
        let mapping = get_model_mapping("duo-chat-sonnet-4-5").unwrap();
        assert_eq!(mapping.provider, GitLabProviderType::Anthropic);
        assert_eq!(mapping.model, "claude-sonnet-4-5-20250929");
    }

    #[test]
    fn test_model_mapping_canonical_lookup() {
        let mapping = get_model_mapping("claude-sonnet-4-5-20250929").unwrap();
        assert_eq!(mapping.provider, GitLabProviderType::Anthropic);
    }

    #[test]
    fn test_model_mapping_openai_chat() {
        let mapping = get_model_mapping("duo-chat-gpt-5-1").unwrap();
        assert_eq!(mapping.provider, GitLabProviderType::OpenAi);
        assert_eq!(mapping.openai_api_type, Some(GitLabOpenAIApiType::Chat));
    }

    #[test]
    fn test_model_mapping_openai_responses() {
        let mapping = get_model_mapping("duo-chat-gpt-5-codex").unwrap();
        assert_eq!(mapping.provider, GitLabProviderType::OpenAi);
        assert_eq!(
            mapping.openai_api_type,
            Some(GitLabOpenAIApiType::Responses)
        );
    }

    #[test]
    fn test_model_mapping_unknown() {
        assert!(get_model_mapping("unknown-model").is_none());
    }
}