goose-providers 0.1.0-alpha.13

An AI agent
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
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
#[macro_use]
mod macros;

use std::{collections::HashMap, path::Path, str::FromStr};

use anyhow::Result;
use include_dir::{include_dir, Dir};
use serde::{Deserialize, Deserializer, Serialize};

pub static FIXED_PROVIDERS: Dir = include_dir!("$CARGO_MANIFEST_DIR/src/declarative/definitions");

pub(crate) mod declarative_providers {
    use super::*;

    expose_declarative_providers!(
        aigateway,
        aimlapi,
        alibaba,
        atomic_chat,
        celeris,
        cerebras,
        deepseek,
        empiriolabs,
        eurouter,
        fireworks,
        friendli,
        futurmix,
        groq,
        iflytek,
        iflytek_astron,
        inception,
        ionet,
        llama_swap,
        lmstudio,
        lynkr,
        meta,
        minimax,
        mistral,
        moonshot,
        nearai,
        novita,
        nvidia,
        ollama_cloud,
        omlx,
        opencode_go,
        opencode_zen,
        opper,
        orcarouter,
        ovhcloud,
        perplexity,
        pleumrouter,
        routstr,
        sakana,
        saladcloud,
        saygm,
        scaleway,
        tanzu,
        tensorix,
        together,
        trustedrouter,
        venice,
        vercel_ai_gateway,
        zai,
        zai_coding_plan,
        zhipu,
    );
}

use crate::{
    anthropic,
    api_client::TlsConfig,
    base::{ModelInfo, Provider},
    ollama, openai,
};

pub fn fixed_provider_configs() -> anyhow::Result<Vec<DeclarativeProviderConfig>> {
    declarative_providers::fixed_provider_configs()
}

pub fn fixed_provider_config_entries() -> Vec<(&'static str, &'static str)> {
    declarative_providers::fixed_provider_config_entries()
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EnvVarConfig {
    pub name: String,
    #[serde(default)]
    pub required: bool,
    #[serde(default)]
    pub secret: bool,
    /// Defaults to the value of `required` if not specified.
    /// UIs may use this to feature this config value more prominently.
    pub primary: Option<bool>,
    pub description: Option<String>,
    pub default: Option<String>,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum ProviderEngine {
    #[serde(alias = "openai_compatible")]
    OpenAI,
    #[serde(alias = "ollama_compatible")]
    Ollama,
    #[serde(alias = "anthropic_compatible")]
    Anthropic,
}

impl FromStr for ProviderEngine {
    type Err = anyhow::Error;

    fn from_str(engine: &str) -> Result<Self> {
        match engine.trim().to_lowercase().as_str() {
            "openai" | "openai_compatible" => Ok(Self::OpenAI),
            "anthropic" | "anthropic_compatible" => Ok(Self::Anthropic),
            "ollama" | "ollama_compatible" => Ok(Self::Ollama),
            _ => Err(anyhow::anyhow!("Invalid provider type: {}", engine)),
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AuthConfig {
    /// Executed directly, never through a shell, so `args` is never
    /// shell-interpolated. For shell features, invoke an interpreter
    /// explicitly, e.g. `command: "/bin/bash"`, `args: ["-c", "..."]`.
    /// Bare names (no path separator) are resolved via `PATH`; paths are
    /// resolved against `cwd`.
    pub command: String,
    #[serde(default)]
    pub args: Vec<String>,
    /// How long a fetched credential is cached before the command is re-run,
    /// in seconds. `0` disables proactive refresh entirely — the command
    /// only reruns reactively, after an auth failure (matches Codex's
    /// `refresh_interval_ms: 0` convention).
    #[serde(default = "default_refresh_interval")]
    pub refresh_interval: u64,
    /// Timeout for the command, in seconds. Defaults to 10s if unset.
    #[serde(default)]
    pub timeout_seconds: Option<u64>,
    /// Working directory for the command, and the base a relative `command`
    /// path is resolved against. Defaults to goose's current directory.
    #[serde(default)]
    pub cwd: Option<String>,
}

fn default_refresh_interval() -> u64 {
    3600
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DeclarativeProviderConfig {
    pub name: String,
    pub engine: ProviderEngine,
    pub display_name: String,
    pub description: Option<String>,
    #[serde(default)]
    pub api_key_env: String,
    pub base_url: String,
    pub models: Vec<ModelInfo>,
    pub headers: Option<HashMap<String, String>>,
    /// Overrides the default `agent-session-id` header name for session ID propagation.
    #[serde(default)]
    pub session_id_header_override: Option<String>,
    pub timeout_seconds: Option<u64>,
    pub supports_streaming: Option<bool>,
    #[serde(default = "default_requires_auth")]
    pub requires_auth: bool,
    #[serde(default)]
    pub catalog_provider_id: Option<String>,
    #[serde(default)]
    pub base_path: Option<String>,
    #[serde(default)]
    pub env_vars: Option<Vec<EnvVarConfig>>,
    /// Alternative to `api_key_env`: run a command to fetch/refresh the credential
    /// instead of reading a static secret. Mutually exclusive with `api_key_env`.
    #[serde(default)]
    pub auth: Option<AuthConfig>,
    /// Controls whether `fetch_supported_models` calls the provider's `/v1/models`
    /// endpoint or returns the static `models` list directly.
    ///
    /// - `Some(false)` + non-empty `models`: return the static list; no API call.
    ///   Construction fails if `models` is empty.
    /// - `Some(true)` or `None`: try the API; fall back to `models` on 404.
    #[serde(default)]
    pub dynamic_models: Option<bool>,
    #[serde(default)]
    pub skip_canonical_filtering: bool,
    #[serde(default, deserialize_with = "deserialize_non_empty_string")]
    pub model_doc_link: Option<String>,
    #[serde(default)]
    pub setup_steps: Vec<String>,
    #[serde(default)]
    pub toolshim: bool,
    #[serde(default)]
    pub preserves_thinking: bool,
    /// Enables Z.AI's `clear_thinking` field, which Anthropic does not support.
    #[serde(default)]
    pub emit_clear_thinking: bool,
    #[serde(default)]
    pub setup: Option<goose_provider_types::canonical::catalog::ProviderSetupMetadata>,
}

fn default_requires_auth() -> bool {
    true
}

pub fn should_preserve_thinking_by_default(engine: &ProviderEngine) -> bool {
    matches!(engine, ProviderEngine::OpenAI)
}

/// Deserialize an optional string, treating empty/whitespace-only values as None.
fn deserialize_non_empty_string<'de, D>(deserializer: D) -> Result<Option<String>, D::Error>
where
    D: Deserializer<'de>,
{
    let opt: Option<String> = Option::deserialize(deserializer)?;
    Ok(opt.filter(|s| !s.trim().is_empty()))
}

impl DeclarativeProviderConfig {
    pub fn id(&self) -> &str {
        &self.name
    }

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

    pub fn models(&self) -> &[ModelInfo] {
        &self.models
    }

    /// Errors if both `api_key_env` and `auth.command` are set; they're
    /// alternative ways to authenticate and mutually exclusive.
    pub fn validate_auth(&self) -> anyhow::Result<()> {
        if self.auth.is_some() && !self.api_key_env.is_empty() {
            anyhow::bail!(
                "Provider '{}' sets both `api_key_env` and `auth.command`; these are mutually exclusive.",
                self.name
            );
        }
        Ok(())
    }
}

pub trait KeyResolver {
    type Error: std::error::Error + Send + Sync + 'static;

    fn resolve_key(&self, key: &str) -> std::result::Result<String, Self::Error>;
}

pub struct EnvKeyResolver;

impl EnvKeyResolver {
    pub fn new() -> Self {
        EnvKeyResolver {}
    }
}

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

impl KeyResolver for EnvKeyResolver {
    type Error = std::env::VarError;

    fn resolve_key(&self, key: &str) -> std::result::Result<String, Self::Error> {
        std::env::var(key)
    }
}

fn expand_env_vars(template: &str, env_vars: &[EnvVarConfig]) -> Result<String> {
    let mut result = template.to_string();

    for var in env_vars {
        let placeholder = format!("${{{}}}", var.name);
        if !result.contains(&placeholder) {
            continue;
        }

        let value = match std::env::var(&var.name) {
            Ok(value) => value,
            Err(_) => match &var.default {
                Some(default) => default.clone(),
                None if var.required => {
                    anyhow::bail!("Required environment variable {} is not set", var.name)
                }
                None => continue,
            },
        };

        result = result.replace(&placeholder, &value);
    }

    Ok(result)
}

fn resolve_config(config: &mut DeclarativeProviderConfig) -> Result<()> {
    if let Some(env_vars) = &config.env_vars {
        config.base_url = expand_env_vars(&config.base_url, env_vars)?;

        for var in env_vars {
            if var.name.ends_with("_STREAMING") {
                let value = std::env::var(&var.name)
                    .ok()
                    .or_else(|| var.default.clone())
                    .map(|value| value.eq_ignore_ascii_case("true"));
                if let Some(value) = value {
                    config.supports_streaming = Some(value);
                }
            }
        }
    }

    Ok(())
}

pub fn deserialize_provider_config(json: &str) -> Result<DeclarativeProviderConfig> {
    let raw: serde_json::Value = serde_json::from_str(json)?;
    let preserves_thinking_was_set = raw.get("preserves_thinking").is_some();
    let mut config: DeclarativeProviderConfig = serde_json::from_value(raw)?;

    if !preserves_thinking_was_set {
        config.preserves_thinking = should_preserve_thinking_by_default(&config.engine);
    }

    Ok(config)
}

fn config_from_json(json: &str) -> Result<DeclarativeProviderConfig> {
    let mut config = deserialize_provider_config(json)?;
    resolve_config(&mut config)?;
    Ok(config)
}

pub fn load_custom_providers(dir: &Path) -> Result<Vec<DeclarativeProviderConfig>> {
    if !dir.exists() {
        return Ok(Vec::new());
    }

    std::fs::read_dir(dir)?
        .filter_map(|entry| {
            let path = entry.ok()?.path();
            (path.extension()? == "json").then_some(path)
        })
        .map(|path| {
            let content = std::fs::read_to_string(&path)?;
            deserialize_provider_config(&content)
                .map_err(|e| anyhow::anyhow!("Failed to parse {}: {}", path.display(), e))
        })
        .collect()
}

pub fn from_json(
    json: &str,
    tls_config: Option<TlsConfig>,
    key_resolver: impl KeyResolver,
) -> Result<Box<dyn Provider>> {
    let config = config_from_json(json)?;

    match config.engine {
        ProviderEngine::OpenAI => openai::from_declarative_config(config, tls_config, key_resolver)
            .map(|provider| Box::new(provider.build()) as Box<dyn Provider>),
        ProviderEngine::Ollama => ollama::from_declarative_config(config, tls_config, key_resolver)
            .map(|provider| Box::new(provider.build()) as Box<dyn Provider>),
        ProviderEngine::Anthropic => {
            anthropic::from_declarative_config(config, tls_config, key_resolver)
                .map(|provider| Box::new(provider.build()) as Box<dyn Provider>)
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;
    use std::collections::HashSet;

    fn model_json() -> serde_json::Value {
        json!({
            "name": "test-model",
            "context_limit": 4096,
            "input_token_cost": null,
            "output_token_cost": null,
            "currency": null,
            "supports_cache_control": null,
            "reasoning": false
        })
    }

    #[test]
    fn provider_engine_deserializes_compatible_aliases() {
        let openai: DeclarativeProviderConfig = serde_json::from_value(json!({
            "name": "test-openai",
            "engine": "openai_compatible",
            "display_name": "Test OpenAI",
            "base_url": "http://localhost:1234",
            "models": [model_json()]
        }))
        .unwrap();
        assert_eq!(openai.engine, ProviderEngine::OpenAI);

        let anthropic: DeclarativeProviderConfig = serde_json::from_value(json!({
            "name": "test-anthropic",
            "engine": "anthropic_compatible",
            "display_name": "Test Anthropic",
            "base_url": "http://localhost:1234",
            "models": [model_json()]
        }))
        .unwrap();
        assert_eq!(anthropic.engine, ProviderEngine::Anthropic);

        let ollama: DeclarativeProviderConfig = serde_json::from_value(json!({
            "name": "test-ollama",
            "engine": "ollama_compatible",
            "display_name": "Test Ollama",
            "base_url": "http://localhost:11434",
            "models": [model_json()]
        }))
        .unwrap();
        assert_eq!(ollama.engine, ProviderEngine::Ollama);
    }

    #[test]
    fn groq_json_disables_thinking_preservation() {
        let config =
            deserialize_provider_config(crate::groq::JSON).expect("groq.json should parse");

        assert!(!config.preserves_thinking);
    }

    #[test]
    fn setup_metadata_rejects_unknown_fields() {
        let mut definition: serde_json::Value = serde_json::from_str(crate::groq::JSON).unwrap();
        definition["setup"]["description"] = json!("This field would be ignored");

        let error = deserialize_provider_config(&definition.to_string()).unwrap_err();

        assert!(error.to_string().contains("unknown field `description`"));
    }

    fn placeholder_var_names(template: &str) -> Vec<String> {
        template
            .split("${")
            .skip(1)
            .filter_map(|chunk| chunk.split_once('}'))
            .map(|(name, _)| name.to_string())
            .collect()
    }

    fn validate_provider_id(id: &str) -> Result<()> {
        let mut chars = id.chars();
        let Some(first) = chars.next() else {
            anyhow::bail!("Invalid provider id: provider id cannot be empty");
        };

        if !(first.is_ascii_lowercase() || first.is_ascii_digit() || first == '_') {
            anyhow::bail!("Invalid provider id: {id}");
        }

        if chars.all(|ch| ch.is_ascii_lowercase() || ch.is_ascii_digit() || ch == '_' || ch == '-')
        {
            Ok(())
        } else {
            anyhow::bail!("Invalid provider id: {id}")
        }
    }

    #[test]
    fn expose_declarative_providers_enumerates_all_bundled_json_files() {
        let enumerated: HashSet<_> = fixed_provider_config_entries()
            .into_iter()
            .map(|(path, _)| path.to_string())
            .collect();
        let bundled: HashSet<_> = FIXED_PROVIDERS
            .files()
            .filter(|file| file.path().extension().and_then(|s| s.to_str()) == Some("json"))
            .map(|file| {
                file.path()
                    .file_name()
                    .unwrap()
                    .to_string_lossy()
                    .into_owned()
            })
            .collect();

        assert_eq!(enumerated, bundled);
    }

    #[test]
    fn all_bundled_providers_are_valid() {
        let mut seen_ids = HashSet::new();

        for (path, json) in fixed_provider_config_entries() {
            let config = deserialize_provider_config(json)
                .unwrap_or_else(|e| panic!("{path} failed to parse: {e}"));

            validate_provider_id(config.id())
                .unwrap_or_else(|e| panic!("{path} has an invalid provider id: {e}"));
            assert!(
                seen_ids.insert(config.id().to_string()),
                "{path} has a duplicate provider id: {}",
                config.id()
            );
            assert!(!config.base_url.is_empty(), "{path} has an empty base_url");

            if config.dynamic_models == Some(false) {
                assert!(
                    !config.models.is_empty(),
                    "{path} disables dynamic_models but lists no static models"
                );
            }

            let declared: HashSet<&str> = config
                .env_vars
                .iter()
                .flatten()
                .map(|v| v.name.as_str())
                .collect();
            let templates = std::iter::once(config.base_url.as_str())
                .chain(config.base_path.as_deref())
                .chain(
                    config
                        .headers
                        .iter()
                        .flat_map(|h| h.values())
                        .map(String::as_str),
                );
            for template in templates {
                for var in placeholder_var_names(template) {
                    assert!(
                        declared.contains(var.as_str()),
                        "{path} references ${{{var}}} but declares no matching env_var"
                    );
                }
            }
        }

        assert!(!seen_ids.is_empty(), "no bundled providers were found");
    }

    #[test]
    fn opencode_go_overrides_session_id_header() {
        let config = fixed_provider_configs()
            .expect("bundled providers should load")
            .into_iter()
            .find(|config| config.name == "opencode_go")
            .expect("opencode_go should be bundled");

        assert_eq!(
            config.session_id_header_override.as_deref(),
            Some("x-opencode-session")
        );
    }

    #[test]
    fn fixed_provider_configs_are_unresolved() {
        let configs = fixed_provider_configs().expect("bundled providers should load");
        let config = configs
            .iter()
            .find(|config| config.env_vars.is_some())
            .expect("at least one bundled provider should declare env_vars");

        assert!(
            config.base_url.contains("${"),
            "{} should keep base_url placeholders unresolved",
            config.id()
        );
    }

    #[test]
    fn from_json_defaults_openai_preserves_thinking_to_true() {
        let json = json!({
            "name": "test-provider",
            "engine": "openai",
            "display_name": "Test Provider",
            "base_url": "http://localhost:1234/v1/chat/completions",
            "models": [model_json()],
            "requires_auth": false,
            "dynamic_models": false
        })
        .to_string();

        let config = config_from_json(&json).unwrap();

        assert!(config.preserves_thinking);
    }

    #[test]
    fn from_json_preserves_explicit_openai_preserves_thinking_false() {
        let json = json!({
            "name": "test-provider",
            "engine": "openai",
            "display_name": "Test Provider",
            "base_url": "http://localhost:1234/v1/chat/completions",
            "models": [model_json()],
            "requires_auth": false,
            "dynamic_models": false,
            "preserves_thinking": false
        })
        .to_string();

        let config = config_from_json(&json).unwrap();

        assert!(!config.preserves_thinking);
    }

    #[test]
    fn from_json_expands_base_url_from_env_var_default() {
        let _guard = env_lock::lock_env([("TEST_PROVIDER_HOST", None::<&str>)]);
        let json = json!({
            "name": "test-provider",
            "engine": "openai",
            "display_name": "Test Provider",
            "base_url": "${TEST_PROVIDER_HOST}/v1/chat/completions",
            "models": [model_json()],
            "requires_auth": false,
            "dynamic_models": false,
            "env_vars": [{
                "name": "TEST_PROVIDER_HOST",
                "default": "http://localhost:1234"
            }]
        })
        .to_string();

        let provider = from_json(&json, None, EnvKeyResolver).unwrap();

        assert_eq!(provider.get_name(), "test-provider");
    }

    #[tokio::test]
    async fn from_json_ollama_returns_static_models_when_dynamic_models_false() {
        let json = json!({
            "name": "test-ollama",
            "engine": "ollama",
            "display_name": "Test Ollama",
            "base_url": "http://localhost:11434",
            "models": [model_json()],
            "requires_auth": false,
            "dynamic_models": false
        })
        .to_string();

        let provider = from_json(&json, None, EnvKeyResolver).unwrap();

        assert_eq!(
            provider.fetch_supported_models().await.unwrap(),
            vec!["test-model".to_string()]
        );
    }

    #[test]
    fn from_json_errors_when_required_env_var_is_missing() {
        let _guard = env_lock::lock_env([("TEST_PROVIDER_REQUIRED_HOST", None::<&str>)]);
        let json = json!({
            "name": "test-provider",
            "engine": "openai",
            "display_name": "Test Provider",
            "base_url": "${TEST_PROVIDER_REQUIRED_HOST}/v1/chat/completions",
            "models": [model_json()],
            "requires_auth": false,
            "dynamic_models": false,
            "env_vars": [{
                "name": "TEST_PROVIDER_REQUIRED_HOST",
                "required": true
            }]
        })
        .to_string();

        let err = match from_json(&json, None, EnvKeyResolver) {
            Ok(_) => panic!("expected missing required env var error"),
            Err(err) => err,
        };

        assert!(err
            .to_string()
            .contains("Required environment variable TEST_PROVIDER_REQUIRED_HOST is not set"));
    }
}