agent-diva-providers 0.3.0

LLM provider integrations for agent-diva
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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
use crate::registry::ProviderSpec;
use crate::registry::{ApiType, ProviderRegistry, RuntimeBackend};
use agent_diva_core::auth::{ProviderAuthKind, ProviderAuthService};
use agent_diva_core::config::ProviderConfig;
use anyhow::Result;
use reqwest::{Client, StatusCode};
use serde::{Deserialize, Serialize};
use std::collections::BTreeSet;
use std::path::Path;
use std::time::Duration;

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum ModelCatalogSource {
    Runtime,
    StaticFallback,
    Unsupported,
    Error,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct ProviderModelCatalog {
    pub provider: String,
    pub source: ModelCatalogSource,
    pub runtime_supported: bool,
    pub api_base: Option<String>,
    pub models: Vec<String>,
    #[serde(default)]
    pub warnings: Vec<String>,
    pub error: Option<String>,
}

#[derive(Debug, Clone)]
pub struct ProviderAccess {
    pub api_key: Option<String>,
    pub api_base: Option<String>,
    pub extra_headers: Vec<(String, String)>,
}

impl ProviderAccess {
    pub fn from_config(config: Option<&ProviderConfig>) -> Self {
        let api_key = config
            .map(|cfg| cfg.api_key.trim().to_string())
            .filter(|value| !value.is_empty());
        let api_base = config
            .and_then(|cfg| cfg.api_base.as_ref())
            .map(|value| value.trim().to_string())
            .filter(|value| !value.is_empty());
        let extra_headers = config
            .and_then(|cfg| cfg.extra_headers.as_ref())
            .map(|headers| {
                headers
                    .iter()
                    .map(|(key, value)| (key.clone(), value.clone()))
                    .collect()
            })
            .unwrap_or_default();

        Self {
            api_key,
            api_base,
            extra_headers,
        }
    }
}

pub async fn resolve_openai_compatible_oauth_access(
    config_dir: &Path,
    provider: &str,
    fallback: ProviderAccess,
) -> Result<ProviderAccess> {
    let registry = ProviderRegistry::new();
    let Some(spec) = registry.find_by_name(provider) else {
        return Ok(fallback);
    };
    if spec.runtime_backend != RuntimeBackend::OpenaiCompatible || !spec.login_supported {
        return Ok(fallback);
    }

    let auth = ProviderAuthService::new(config_dir);
    let Some(profile) = auth.get_active_profile(provider).await? else {
        return Ok(fallback);
    };

    let bearer = match profile.kind {
        ProviderAuthKind::OAuth => profile
            .token_set
            .as_ref()
            .map(|token_set| token_set.access_token.trim().to_string())
            .filter(|token| !token.is_empty()),
        ProviderAuthKind::Token => profile
            .token
            .as_ref()
            .map(|token| token.trim().to_string())
            .filter(|token| !token.is_empty()),
    };

    let api_base = profile
        .metadata
        .get("api_base")
        .map(|value| value.trim().trim_end_matches('/').to_string())
        .filter(|value| !value.is_empty())
        .or(fallback.api_base.clone());

    Ok(ProviderAccess {
        api_key: bearer.or(fallback.api_key),
        api_base,
        extra_headers: fallback.extra_headers,
    })
}

pub async fn fetch_provider_model_catalog(
    spec: &ProviderSpec,
    access: &ProviderAccess,
    allow_static_fallback: bool,
) -> ProviderModelCatalog {
    let api_base = effective_api_base(spec, access);

    match runtime_strategy(spec, &api_base) {
        Some(RuntimeDiscoveryStrategy::OpenAiCompatible) => {
            match fetch_openai_compatible_models(spec, access, api_base.as_deref()).await {
                Ok(models) => ProviderModelCatalog {
                    provider: spec.name.clone(),
                    source: ModelCatalogSource::Runtime,
                    runtime_supported: true,
                    api_base,
                    models,
                    warnings: vec![],
                    error: None,
                },
                Err(error) => fallback_or_error(spec, api_base, allow_static_fallback, error, true),
            }
        }
        None => {
            let message = format!(
                "Provider '{}' does not support runtime model discovery in this build",
                provider_identity(spec)
            );
            if allow_static_fallback && !spec.models.is_empty() {
                ProviderModelCatalog {
                    provider: spec.name.clone(),
                    source: ModelCatalogSource::StaticFallback,
                    runtime_supported: false,
                    api_base,
                    models: spec.models.clone(),
                    warnings: vec![],
                    error: None,
                }
            } else {
                ProviderModelCatalog {
                    provider: spec.name.clone(),
                    source: ModelCatalogSource::Unsupported,
                    runtime_supported: false,
                    api_base,
                    models: vec![],
                    warnings: vec![],
                    error: Some(message),
                }
            }
        }
    }
}

fn fallback_or_error(
    spec: &ProviderSpec,
    api_base: Option<String>,
    allow_static_fallback: bool,
    error: String,
    runtime_supported: bool,
) -> ProviderModelCatalog {
    if allow_static_fallback && !spec.models.is_empty() {
        ProviderModelCatalog {
            provider: spec.name.clone(),
            source: ModelCatalogSource::StaticFallback,
            runtime_supported,
            api_base,
            models: spec.models.clone(),
            warnings: vec![error],
            error: None,
        }
    } else {
        ProviderModelCatalog {
            provider: spec.name.clone(),
            source: ModelCatalogSource::Error,
            runtime_supported,
            api_base,
            models: vec![],
            warnings: vec![],
            error: Some(error),
        }
    }
}

#[derive(Debug, Clone, Copy)]
enum RuntimeDiscoveryStrategy {
    OpenAiCompatible,
}

fn runtime_strategy(
    spec: &ProviderSpec,
    api_base: &Option<String>,
) -> Option<RuntimeDiscoveryStrategy> {
    let supports_openai_compatible = matches!(spec.api_type, ApiType::Openai);

    if supports_openai_compatible && api_base.is_some() {
        Some(RuntimeDiscoveryStrategy::OpenAiCompatible)
    } else {
        None
    }
}

fn effective_api_base(spec: &ProviderSpec, access: &ProviderAccess) -> Option<String> {
    access
        .api_base
        .as_ref()
        .map(|value| value.trim().trim_end_matches('/').to_string())
        .filter(|value| !value.is_empty())
        .or_else(|| {
            (!spec.default_api_base.trim().is_empty()).then(|| {
                spec.default_api_base
                    .trim()
                    .trim_end_matches('/')
                    .to_string()
            })
        })
}

async fn fetch_openai_compatible_models(
    spec: &ProviderSpec,
    access: &ProviderAccess,
    api_base: Option<&str>,
) -> Result<Vec<String>, String> {
    let api_base = api_base.ok_or_else(|| {
        format!(
            "Provider '{}' has no configured or default api_base for runtime discovery",
            provider_identity(spec)
        )
    })?;
    let client = Client::builder()
        .timeout(Duration::from_secs(15))
        .build()
        .map_err(|error| format!("failed to create HTTP client: {error}"))?;
    let url = format!("{api_base}/models");

    let mut request = client.get(&url);
    if let Some(api_key) = access
        .api_key
        .as_ref()
        .filter(|value| !value.trim().is_empty())
    {
        request = request.bearer_auth(api_key);
    }
    for (key, value) in &access.extra_headers {
        request = request.header(key, value);
    }

    let response = request.send().await.map_err(|error| {
        format!(
            "Runtime model discovery request failed for provider '{}' at '{}': {}",
            provider_identity(spec),
            url,
            error
        )
    })?;

    let status = response.status();
    if !status.is_success() {
        let detail = response.text().await.unwrap_or_default();
        let summary = http_error_summary(status, &detail);
        return Err(format!(
            "Runtime model discovery request returned {} for provider '{}' at '{}': {}",
            status,
            provider_identity(spec),
            url,
            summary
        ));
    }

    let payload: OpenAiModelsResponse = response.json().await.map_err(|error| {
        format!(
            "Runtime model discovery response was invalid JSON for provider '{}' at '{}': {}",
            provider_identity(spec),
            url,
            error
        )
    })?;

    let mut unique_models = BTreeSet::new();
    for model in payload.data {
        let id = model.id.trim();
        if !id.is_empty() {
            unique_models.insert(id.to_string());
        }
    }

    Ok(unique_models.into_iter().collect())
}

fn http_error_summary(status: StatusCode, detail: &str) -> String {
    let trimmed = detail.trim();
    if trimmed.is_empty() {
        return "empty response body".to_string();
    }

    let compact = trimmed.replace('\n', " ");
    let preview: String = compact.chars().take(200).collect();
    if compact.chars().count() > 200 {
        format!("{status}: {preview}...")
    } else {
        format!("{status}: {preview}")
    }
}

fn provider_identity(spec: &ProviderSpec) -> &str {
    if spec.name.trim().is_empty() {
        let display_name = spec.display_name.trim();
        if !display_name.is_empty() {
            display_name
        } else {
            "<unknown>"
        }
    } else {
        spec.name.as_str()
    }
}

#[derive(Debug, Deserialize)]
struct OpenAiModelsResponse {
    #[serde(default)]
    data: Vec<OpenAiModelEntry>,
}

#[derive(Debug, Deserialize)]
struct OpenAiModelEntry {
    id: String,
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{
        registry::{ApiType, AuthMode, CredentialStore, RuntimeBackend},
        ProviderSpec,
    };
    use agent_diva_core::auth::{OAuthProfileState, ProviderAuthService, ProviderTokenSet};
    use mockito::{Matcher, Server};
    use std::collections::HashMap;
    use tempfile::tempdir;

    fn openai_like_spec(name: &str, api_base: &str) -> ProviderSpec {
        ProviderSpec {
            name: name.to_string(),
            api_type: ApiType::Openai,
            keywords: vec![name.to_string()],
            env_key: "TEST_API_KEY".to_string(),
            display_name: name.to_string(),
            default_model: None,
            auth_mode: AuthMode::ApiKey,
            login_supported: false,
            credential_store: CredentialStore::Config,
            runtime_backend: RuntimeBackend::OpenaiCompatible,
            litellm_prefix: String::new(),
            skip_prefixes: vec![],
            env_extras: vec![],
            default_api_base: api_base.to_string(),
            supports_prompt_caching: false,
            models: vec!["fallback-a".to_string(), "fallback-b".to_string()],
            model_overrides: vec![],
        }
    }

    #[tokio::test]
    async fn fetch_provider_model_catalog_returns_runtime_models_for_openai_compatible() {
        let mut server = Server::new_async().await;
        let mock = server
            .mock("GET", "/models")
            .match_header("authorization", "Bearer sk-test")
            .match_header("x-test", "1")
            .with_status(200)
            .with_body(r#"{"data":[{"id":"gpt-4o"},{"id":"gpt-4o-mini"},{"id":"gpt-4o"}]}"#)
            .create_async()
            .await;
        let spec = openai_like_spec("openai", &server.url());
        let access = ProviderAccess {
            api_key: Some("sk-test".to_string()),
            api_base: None,
            extra_headers: vec![("x-test".to_string(), "1".to_string())],
        };

        let catalog = fetch_provider_model_catalog(&spec, &access, false).await;

        mock.assert_async().await;
        assert_eq!(catalog.source, ModelCatalogSource::Runtime);
        assert_eq!(
            catalog.models,
            vec!["gpt-4o".to_string(), "gpt-4o-mini".to_string()]
        );
        assert_eq!(catalog.error, None);
    }

    #[tokio::test]
    async fn fetch_provider_model_catalog_uses_static_fallback_on_http_error() {
        let mut server = Server::new_async().await;
        let mock = server
            .mock("GET", "/models")
            .with_status(401)
            .with_body(r#"{"error":"unauthorized"}"#)
            .create_async()
            .await;
        let spec = openai_like_spec("openai", &server.url());

        let catalog = fetch_provider_model_catalog(
            &spec,
            &ProviderAccess {
                api_key: Some("sk-bad".to_string()),
                api_base: None,
                extra_headers: vec![],
            },
            true,
        )
        .await;

        mock.assert_async().await;
        assert_eq!(catalog.source, ModelCatalogSource::StaticFallback);
        assert_eq!(catalog.models, spec.models);
        assert!(catalog.warnings[0].contains("401 Unauthorized"));
    }

    #[tokio::test]
    async fn fetch_provider_model_catalog_returns_unsupported_without_fallback() {
        let spec = ProviderSpec {
            name: "anthropic".to_string(),
            api_type: ApiType::Anthropic,
            keywords: vec!["claude".to_string()],
            env_key: "ANTHROPIC_API_KEY".to_string(),
            display_name: "Anthropic".to_string(),
            default_model: None,
            auth_mode: AuthMode::ApiKey,
            login_supported: false,
            credential_store: CredentialStore::Config,
            runtime_backend: RuntimeBackend::OpenaiCompatible,
            litellm_prefix: String::new(),
            skip_prefixes: vec![],
            env_extras: vec![],
            default_api_base: String::new(),
            supports_prompt_caching: false,
            models: vec![],
            model_overrides: vec![],
        };

        let catalog =
            fetch_provider_model_catalog(&spec, &ProviderAccess::from_config(None), false).await;

        assert_eq!(catalog.source, ModelCatalogSource::Unsupported);
        assert!(catalog.error.unwrap().contains("does not support"));
    }

    #[tokio::test]
    async fn fetch_provider_model_catalog_supports_generic_openai_type_with_api_base() {
        let mut server = Server::new_async().await;
        let mock = server
            .mock("GET", "/models")
            .with_status(200)
            .with_body(r#"{"data":[{"id":"grok-2-latest"}]}"#)
            .create_async()
            .await;
        let spec = openai_like_spec("xai", &server.url());

        let catalog = fetch_provider_model_catalog(
            &spec,
            &ProviderAccess {
                api_key: None,
                api_base: Some(server.url()),
                extra_headers: vec![],
            },
            false,
        )
        .await;

        mock.assert_async().await;
        assert_eq!(catalog.source, ModelCatalogSource::Runtime);
        assert_eq!(catalog.models, vec!["grok-2-latest".to_string()]);
    }

    #[test]
    fn provider_access_from_config_filters_empty_values() {
        let access = ProviderAccess::from_config(Some(&ProviderConfig {
            api_key: "  ".to_string(),
            api_base: Some("  https://example.com/v1/ ".to_string()),
            extra_headers: Some(HashMap::from([
                ("x-a".to_string(), "1".to_string()),
                ("x-b".to_string(), "2".to_string()),
            ])),
            custom_models: vec![],
        }));

        assert_eq!(access.api_key, None);
        assert_eq!(access.api_base, Some("https://example.com/v1/".to_string()));
        assert_eq!(access.extra_headers.len(), 2);
    }

    #[tokio::test]
    async fn fetch_provider_model_catalog_prefers_configured_api_base() {
        let mut server = Server::new_async().await;
        let mock = server
            .mock("GET", "/models")
            .match_query(Matcher::Missing)
            .with_status(200)
            .with_body(r#"{"data":[{"id":"model-a"}]}"#)
            .create_async()
            .await;
        let spec = openai_like_spec("custom", "http://unused.example/v1");
        let access = ProviderAccess {
            api_key: None,
            api_base: Some(server.url()),
            extra_headers: vec![],
        };

        let catalog = fetch_provider_model_catalog(&spec, &access, false).await;

        mock.assert_async().await;
        assert_eq!(catalog.source, ModelCatalogSource::Runtime);
        assert_eq!(catalog.api_base, Some(server.url()));
    }

    #[tokio::test]
    async fn resolve_openai_compatible_oauth_access_prefers_auth_store_for_qwen_login() {
        let dir = tempdir().unwrap();
        let auth = ProviderAuthService::new(dir.path());
        auth.store_oauth_profile(
            "qwen-login",
            "default",
            OAuthProfileState {
                token_set: ProviderTokenSet {
                    access_token: "oauth-bearer".to_string(),
                    refresh_token: Some("refresh".to_string()),
                    id_token: None,
                    expires_at: None,
                    token_type: Some("Bearer".to_string()),
                    scope: Some("openid".to_string()),
                },
                account_id: None,
                metadata: HashMap::from([(
                    "api_base".to_string(),
                    "https://oauth.example/v1".to_string(),
                )])
                .into_iter()
                .collect(),
            },
            true,
        )
        .await
        .unwrap();

        let access = resolve_openai_compatible_oauth_access(
            dir.path(),
            "qwen-login",
            ProviderAccess {
                api_key: Some("config-api-key".to_string()),
                api_base: Some("https://config.example/v1".to_string()),
                extra_headers: vec![],
            },
        )
        .await
        .unwrap();

        assert_eq!(access.api_key.as_deref(), Some("oauth-bearer"));
        assert_eq!(access.api_base.as_deref(), Some("https://oauth.example/v1"));
    }

    #[tokio::test]
    async fn resolve_openai_compatible_oauth_access_keeps_dashscope_on_config_path() {
        let dir = tempdir().unwrap();
        let access = resolve_openai_compatible_oauth_access(
            dir.path(),
            "dashscope",
            ProviderAccess {
                api_key: Some("dashscope-key".to_string()),
                api_base: Some("https://dashscope.aliyuncs.com/compatible-mode/v1".to_string()),
                extra_headers: vec![],
            },
        )
        .await
        .unwrap();

        assert_eq!(access.api_key.as_deref(), Some("dashscope-key"));
        assert_eq!(
            access.api_base.as_deref(),
            Some("https://dashscope.aliyuncs.com/compatible-mode/v1")
        );
    }
}