-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Expand file tree
/
Copy pathconfig_management.rs
More file actions
865 lines (777 loc) · 27 KB
/
config_management.rs
File metadata and controls
865 lines (777 loc) · 27 KB
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
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
use crate::routes::errors::ErrorResponse;
use crate::routes::utils::check_provider_configured;
use crate::state::AppState;
use axum::routing::put;
use axum::{
extract::Path,
routing::{delete, get, post},
Json, Router,
};
use goose::config::declarative_providers::LoadedProvider;
use goose::config::paths::Paths;
use goose::config::ExtensionEntry;
use goose::config::{Config, ConfigError};
use goose::model::ModelConfig;
use goose::providers::base::{ProviderMetadata, ProviderType};
use goose::providers::canonical::maybe_get_canonical_model;
use goose::providers::catalog::{
get_provider_template, get_providers_by_format, ProviderCatalogEntry, ProviderFormat,
ProviderTemplate,
};
use goose::providers::create_with_default_model;
use goose::providers::providers as get_providers;
use goose::{
agents::execute_commands, agents::ExtensionConfig, config::permission::PermissionLevel,
slash_commands,
};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use serde_yaml;
use std::{collections::HashMap, sync::Arc};
use utoipa::ToSchema;
#[derive(Serialize, ToSchema)]
pub struct ExtensionResponse {
pub extensions: Vec<ExtensionEntry>,
#[serde(default)]
pub warnings: Vec<String>,
}
#[derive(Deserialize, ToSchema)]
pub struct ExtensionQuery {
pub name: String,
pub config: ExtensionConfig,
pub enabled: bool,
}
#[derive(Deserialize, ToSchema)]
pub struct UpsertConfigQuery {
pub key: String,
pub value: Value,
pub is_secret: bool,
}
#[derive(Deserialize, Serialize, ToSchema)]
pub struct ConfigKeyQuery {
pub key: String,
pub is_secret: bool,
}
#[derive(Serialize, ToSchema)]
pub struct ConfigResponse {
pub config: HashMap<String, Value>,
}
#[derive(Debug, Serialize, Deserialize, ToSchema)]
pub struct ProviderDetails {
pub name: String,
pub metadata: ProviderMetadata,
pub is_configured: bool,
pub provider_type: ProviderType,
}
#[derive(Serialize, ToSchema)]
pub struct ProvidersResponse {
pub providers: Vec<ProviderDetails>,
}
#[derive(Debug, Serialize, Deserialize, ToSchema)]
pub struct ToolPermission {
pub tool_name: String,
pub permission: PermissionLevel,
}
#[derive(Deserialize, ToSchema)]
pub struct UpsertPermissionsQuery {
pub tool_permissions: Vec<ToolPermission>,
}
#[derive(Deserialize, ToSchema)]
pub struct UpdateCustomProviderRequest {
pub engine: String,
pub display_name: String,
pub api_url: String,
pub api_key: String,
pub models: Vec<String>,
pub supports_streaming: Option<bool>,
pub headers: Option<std::collections::HashMap<String, String>>,
#[serde(default = "default_requires_auth")]
pub requires_auth: bool,
#[serde(default)]
pub catalog_provider_id: Option<String>,
#[serde(default)]
pub base_path: Option<String>,
}
fn default_requires_auth() -> bool {
true
}
#[derive(Deserialize, ToSchema)]
pub struct CheckProviderRequest {
pub provider: String,
}
#[derive(Deserialize, ToSchema)]
pub struct SetProviderRequest {
pub provider: String,
pub model: String,
}
#[derive(Serialize, ToSchema)]
#[serde(rename_all = "camelCase")]
pub struct MaskedSecret {
pub masked_value: String,
}
#[derive(Serialize, ToSchema)]
#[serde(untagged)]
pub enum ConfigValueResponse {
Value(Value),
MaskedValue(MaskedSecret),
}
#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
pub enum CommandType {
Builtin,
Recipe,
Skill,
}
#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
pub struct SlashCommand {
pub command: String,
pub help: String,
pub command_type: CommandType,
}
#[derive(Serialize, ToSchema)]
pub struct SlashCommandsResponse {
pub commands: Vec<SlashCommand>,
}
#[utoipa::path(
post,
path = "/config/upsert",
request_body = UpsertConfigQuery,
responses(
(status = 200, description = "Configuration value upserted successfully", body = String),
(status = 500, description = "Internal server error")
)
)]
pub async fn upsert_config(
Json(query): Json<UpsertConfigQuery>,
) -> Result<Json<Value>, ErrorResponse> {
let config = Config::global();
config.set(&query.key, &query.value, query.is_secret)?;
Ok(Json(Value::String(format!("Upserted key {}", query.key))))
}
#[utoipa::path(
post,
path = "/config/remove",
request_body = ConfigKeyQuery,
responses(
(status = 200, description = "Configuration value removed successfully", body = String),
(status = 404, description = "Configuration key not found"),
(status = 500, description = "Internal server error")
)
)]
pub async fn remove_config(
Json(query): Json<ConfigKeyQuery>,
) -> Result<Json<String>, ErrorResponse> {
let config = Config::global();
if query.is_secret {
config.delete_secret(&query.key)?;
} else {
config.delete(&query.key)?;
}
Ok(Json(format!("Removed key {}", query.key)))
}
const SECRET_MASK_SHOW_LEN: usize = 8;
fn mask_secret(secret: Value) -> String {
let as_string = match secret {
Value::String(s) => s,
_ => serde_json::to_string(&secret).unwrap_or_else(|_| secret.to_string()),
};
let chars: Vec<_> = as_string.chars().collect();
let show_len = std::cmp::min(chars.len() / 2, SECRET_MASK_SHOW_LEN);
let visible: String = chars.iter().take(show_len).collect();
let mask = "*".repeat(chars.len() - show_len);
format!("{}{}", visible, mask)
}
fn is_valid_provider_name(provider_name: &str) -> bool {
!provider_name.is_empty()
&& provider_name
.chars()
.all(|c| c.is_ascii_alphanumeric() || c == '-' || c == '_')
}
#[utoipa::path(
post,
path = "/config/read",
request_body = ConfigKeyQuery,
responses(
(status = 200, description = "Configuration value retrieved successfully", body = Value),
(status = 500, description = "Unable to get the configuration value"),
)
)]
pub async fn read_config(
Json(query): Json<ConfigKeyQuery>,
) -> Result<Json<ConfigValueResponse>, ErrorResponse> {
let config = Config::global();
let response_value = match config.get(&query.key, query.is_secret) {
Ok(value) => {
if query.is_secret {
ConfigValueResponse::MaskedValue(MaskedSecret {
masked_value: mask_secret(value),
})
} else {
ConfigValueResponse::Value(value)
}
}
Err(ConfigError::NotFound(_)) => ConfigValueResponse::Value(Value::Null),
Err(e) => return Err(e.into()),
};
Ok(Json(response_value))
}
#[utoipa::path(
get,
path = "/config/extensions",
responses(
(status = 200, description = "All extensions retrieved successfully", body = ExtensionResponse),
(status = 500, description = "Internal server error")
)
)]
pub async fn get_extensions() -> Result<Json<ExtensionResponse>, ErrorResponse> {
let extensions = goose::config::get_all_extensions()
.into_iter()
.filter(|ext| !goose::agents::extension_manager::is_hidden_extension(&ext.config.name()))
.collect();
let warnings = goose::config::get_warnings();
Ok(Json(ExtensionResponse {
extensions,
warnings,
}))
}
#[utoipa::path(
post,
path = "/config/extensions",
request_body = ExtensionQuery,
responses(
(status = 200, description = "Extension added or updated successfully", body = String),
(status = 400, description = "Invalid request"),
(status = 422, description = "Could not serialize config.yaml"),
(status = 500, description = "Internal server error")
)
)]
pub async fn add_extension(
Json(extension_query): Json<ExtensionQuery>,
) -> Result<Json<String>, ErrorResponse> {
let extensions = goose::config::get_all_extensions();
let key = goose::config::extensions::name_to_key(&extension_query.name);
let is_update = extensions.iter().any(|e| e.config.key() == key);
goose::config::set_extension(ExtensionEntry {
enabled: extension_query.enabled,
config: extension_query.config,
});
if is_update {
Ok(Json(format!("Updated extension {}", extension_query.name)))
} else {
Ok(Json(format!("Added extension {}", extension_query.name)))
}
}
#[utoipa::path(
delete,
path = "/config/extensions/{name}",
responses(
(status = 200, description = "Extension removed successfully", body = String),
(status = 404, description = "Extension not found"),
(status = 500, description = "Internal server error")
)
)]
pub async fn remove_extension(Path(name): Path<String>) -> Result<Json<String>, ErrorResponse> {
let key = goose::config::extensions::name_to_key(&name);
goose::config::remove_extension(&key);
Ok(Json(format!("Removed extension {}", name)))
}
#[utoipa::path(
get,
path = "/config",
responses(
(status = 200, description = "All configuration values retrieved successfully", body = ConfigResponse)
)
)]
pub async fn read_all_config() -> Result<Json<ConfigResponse>, ErrorResponse> {
let config = Config::global();
let values = config
.all_values()
.map_err(|e| ErrorResponse::unprocessable(e.to_string()))?;
Ok(Json(ConfigResponse { config: values }))
}
#[utoipa::path(
get,
path = "/config/providers",
responses(
(status = 200, description = "All configuration values retrieved successfully", body = [ProviderDetails])
)
)]
pub async fn providers() -> Result<Json<Vec<ProviderDetails>>, ErrorResponse> {
let providers = get_providers().await;
let providers_response: Vec<ProviderDetails> = providers
.into_iter()
.map(|(metadata, provider_type)| {
let is_configured = check_provider_configured(&metadata, provider_type);
ProviderDetails {
name: metadata.name.clone(),
metadata,
is_configured,
provider_type,
}
})
.collect();
Ok(Json(providers_response))
}
#[utoipa::path(
get,
path = "/config/providers/{name}/models",
params(
("name" = String, Path, description = "Provider name (e.g., openai)")
),
responses(
(status = 200, description = "Models fetched successfully", body = [String]),
(status = 400, description = "Unknown provider, provider not configured, or authentication error"),
(status = 429, description = "Rate limit exceeded"),
(status = 500, description = "Internal server error")
)
)]
pub async fn get_provider_models(
Path(name): Path<String>,
) -> Result<Json<Vec<String>>, ErrorResponse> {
let all = get_providers().await.into_iter().collect::<Vec<_>>();
let Some((metadata, provider_type)) = all.into_iter().find(|(m, _)| m.name == name) else {
return Err(ErrorResponse::bad_request(format!(
"Unknown provider: {}",
name
)));
};
if !check_provider_configured(&metadata, provider_type) {
return Err(ErrorResponse::bad_request(format!(
"Provider '{}' is not configured",
name
)));
}
let model_config = ModelConfig::new(&metadata.default_model)?.with_canonical_limits(&name);
let provider = goose::providers::create(&name, model_config, Vec::new()).await?;
let models_result = provider.fetch_recommended_models().await;
match models_result {
Ok(models) => Ok(Json(models)),
Err(provider_error) => Err(provider_error.into()),
}
}
#[derive(Deserialize, utoipa::IntoParams)]
pub struct SlashCommandsQuery {
/// Optional working directory to discover local skills from
pub working_dir: Option<String>,
}
#[utoipa::path(
get,
path = "/config/slash_commands",
params(SlashCommandsQuery),
responses(
(status = 200, description = "Slash commands retrieved successfully", body = SlashCommandsResponse)
)
)]
pub async fn get_slash_commands(
axum::extract::Query(query): axum::extract::Query<SlashCommandsQuery>,
) -> Result<Json<SlashCommandsResponse>, ErrorResponse> {
let mut commands: Vec<_> = slash_commands::list_commands()
.iter()
.map(|command| SlashCommand {
command: command.command.clone(),
help: command.recipe_path.clone(),
command_type: CommandType::Recipe,
})
.collect();
for cmd_def in execute_commands::list_commands() {
commands.push(SlashCommand {
command: cmd_def.name.to_string(),
help: cmd_def.description.to_string(),
command_type: CommandType::Builtin,
});
}
let working_dir = query.working_dir.map(std::path::PathBuf::from);
for source in
goose::agents::platform_extensions::skills::list_installed_skills(working_dir.as_deref())
{
commands.push(SlashCommand {
command: source.name,
help: source.description,
command_type: CommandType::Skill,
});
}
Ok(Json(SlashCommandsResponse { commands }))
}
#[derive(Serialize, ToSchema)]
pub struct ModelInfoData {
pub provider: String,
pub model: String,
pub context_limit: usize,
pub max_output_tokens: Option<usize>,
pub input_token_cost: Option<f64>,
pub output_token_cost: Option<f64>,
pub cache_read_token_cost: Option<f64>,
pub cache_write_token_cost: Option<f64>,
pub currency: String,
}
#[derive(Serialize, ToSchema)]
pub struct ModelInfoResponse {
pub model_info: Option<ModelInfoData>,
pub source: String,
}
#[derive(Deserialize, ToSchema)]
pub struct ModelInfoQuery {
pub provider: String,
pub model: String,
}
#[utoipa::path(
post,
path = "/config/canonical-model-info",
request_body = ModelInfoQuery,
responses(
(status = 200, description = "Model information retrieved successfully", body = ModelInfoResponse)
)
)]
pub async fn get_canonical_model_info(
Json(query): Json<ModelInfoQuery>,
) -> Json<ModelInfoResponse> {
let canonical_model = maybe_get_canonical_model(&query.provider, &query.model);
let model_info = canonical_model.map(|canonical_model| ModelInfoData {
provider: query.provider.clone(),
model: query.model.clone(),
context_limit: canonical_model.limit.context,
max_output_tokens: canonical_model.limit.output,
// Costs are per million tokens - client handles division for display
input_token_cost: canonical_model.cost.input,
output_token_cost: canonical_model.cost.output,
cache_read_token_cost: canonical_model.cost.cache_read,
cache_write_token_cost: canonical_model.cost.cache_write,
currency: "$".to_string(),
});
Json(ModelInfoResponse {
model_info,
source: "canonical".to_string(),
})
}
#[utoipa::path(
post,
path = "/config/permissions",
request_body = UpsertPermissionsQuery,
responses(
(status = 200, description = "Permission update completed", body = String),
(status = 400, description = "Invalid request"),
)
)]
pub async fn upsert_permissions(
Json(query): Json<UpsertPermissionsQuery>,
) -> Result<Json<String>, ErrorResponse> {
let permission_manager = goose::config::PermissionManager::instance();
for tool_permission in &query.tool_permissions {
permission_manager.update_user_permission(
&tool_permission.tool_name,
tool_permission.permission.clone(),
);
}
Ok(Json("Permissions updated successfully".to_string()))
}
#[utoipa::path(
get,
path = "/config/validate",
responses(
(status = 200, description = "Config validation result", body = String),
(status = 422, description = "Config file is corrupted")
)
)]
pub async fn validate_config() -> Result<Json<String>, ErrorResponse> {
let config_path = Paths::config_dir().join("config.yaml");
if !config_path.exists() {
return Ok(Json("Config file does not exist".to_string()));
}
let content = std::fs::read_to_string(&config_path)?;
serde_yaml::from_str::<serde_yaml::Value>(&content)
.map_err(|e| ErrorResponse::unprocessable(format!("Config file is corrupted: {}", e)))?;
Ok(Json("Config file is valid".to_string()))
}
#[derive(Serialize, ToSchema)]
pub struct CreateCustomProviderResponse {
pub provider_name: String,
}
#[utoipa::path(
post,
path = "/config/custom-providers",
request_body = UpdateCustomProviderRequest,
responses(
(status = 200, description = "Custom provider created successfully", body = CreateCustomProviderResponse),
(status = 400, description = "Invalid request"),
(status = 500, description = "Internal server error")
)
)]
pub async fn create_custom_provider(
Json(request): Json<UpdateCustomProviderRequest>,
) -> Result<Json<CreateCustomProviderResponse>, ErrorResponse> {
let config = goose::config::declarative_providers::create_custom_provider(
goose::config::declarative_providers::CreateCustomProviderParams {
engine: request.engine,
display_name: request.display_name,
api_url: request.api_url,
api_key: request.api_key,
models: request.models,
supports_streaming: request.supports_streaming,
headers: request.headers,
requires_auth: request.requires_auth,
catalog_provider_id: request.catalog_provider_id,
base_path: request.base_path,
},
)?;
goose::providers::refresh_custom_providers().await?;
Ok(Json(CreateCustomProviderResponse {
provider_name: config.id().to_string(),
}))
}
#[utoipa::path(
get,
path = "/config/custom-providers/{id}",
responses(
(status = 200, description = "Custom provider retrieved successfully", body = LoadedProvider),
(status = 404, description = "Provider not found"),
(status = 500, description = "Internal server error")
)
)]
pub async fn get_custom_provider(
Path(id): Path<String>,
) -> Result<Json<LoadedProvider>, ErrorResponse> {
let loaded_provider = goose::config::declarative_providers::load_provider(id.as_str())
.map_err(|e| {
ErrorResponse::not_found(format!("Custom provider '{}' not found: {}", id, e))
})?;
Ok(Json(loaded_provider))
}
#[utoipa::path(
delete,
path = "/config/custom-providers/{id}",
responses(
(status = 200, description = "Custom provider removed successfully", body = String),
(status = 404, description = "Provider not found"),
(status = 500, description = "Internal server error")
)
)]
pub async fn remove_custom_provider(Path(id): Path<String>) -> Result<Json<String>, ErrorResponse> {
goose::config::declarative_providers::remove_custom_provider(&id)?;
goose::providers::refresh_custom_providers().await?;
Ok(Json(format!("Removed custom provider: {}", id)))
}
#[utoipa::path(
post,
path = "/config/providers/{name}/cleanup",
params(
("name" = String, Path, description = "Provider name (e.g., githubcopilot)")
),
responses(
(status = 200, description = "Provider cache cleaned up successfully", body = String),
(status = 500, description = "Internal server error")
)
)]
pub async fn cleanup_provider_cache(
Path(name): Path<String>,
) -> Result<Json<String>, ErrorResponse> {
goose::providers::cleanup_provider(&name).await?;
Ok(Json(format!("Cleaned up provider cache: {}", name)))
}
#[utoipa::path(
put,
path = "/config/custom-providers/{id}",
request_body = UpdateCustomProviderRequest,
responses(
(status = 200, description = "Custom provider updated successfully", body = String),
(status = 404, description = "Provider not found"),
(status = 500, description = "Internal server error")
)
)]
pub async fn update_custom_provider(
Path(id): Path<String>,
Json(request): Json<UpdateCustomProviderRequest>,
) -> Result<Json<String>, ErrorResponse> {
goose::config::declarative_providers::update_custom_provider(
goose::config::declarative_providers::UpdateCustomProviderParams {
id: id.clone(),
engine: request.engine,
display_name: request.display_name,
api_url: request.api_url,
api_key: request.api_key,
models: request.models,
supports_streaming: request.supports_streaming,
headers: request.headers,
requires_auth: request.requires_auth,
catalog_provider_id: request.catalog_provider_id,
base_path: request.base_path,
},
)?;
goose::providers::refresh_custom_providers().await?;
Ok(Json(format!("Updated custom provider: {}", id)))
}
#[utoipa::path(
post,
path = "/config/check_provider",
request_body = CheckProviderRequest,
)]
pub async fn check_provider(
Json(CheckProviderRequest { provider }): Json<CheckProviderRequest>,
) -> Result<(), ErrorResponse> {
// Provider check does not use extensions.
create_with_default_model(&provider, Vec::new())
.await
.map_err(|err| {
ErrorResponse::bad_request(format!("Provider '{}' check failed: {}", provider, err))
})?;
Ok(())
}
#[utoipa::path(
post,
path = "/config/set_provider",
request_body = SetProviderRequest,
)]
pub async fn set_config_provider(
Json(SetProviderRequest { provider, model }): Json<SetProviderRequest>,
) -> Result<(), ErrorResponse> {
// Provider validation does not use extensions.
create_with_default_model(&provider, Vec::new())
.await
.and_then(|_| {
let config = Config::global();
config
.set_goose_provider(provider.clone())
.and_then(|_| config.set_goose_model(model.clone()))
.map_err(|e| anyhow::anyhow!(e))
})
.map_err(|err| {
ErrorResponse::bad_request(format!(
"Failed to set provider to '{}' with model '{}': {}",
provider, model, err
))
})?;
Ok(())
}
#[utoipa::path(
get,
path = "/config/provider-catalog",
params(
("format" = Option<String>, Query, description = "Filter by provider format (openai, anthropic, ollama)")
),
responses(
(status = 200, description = "Provider catalog retrieved successfully", body = [ProviderCatalogEntry]),
(status = 400, description = "Invalid format parameter")
)
)]
pub async fn get_provider_catalog(
axum::extract::Query(params): axum::extract::Query<HashMap<String, String>>,
) -> Result<Json<Vec<ProviderCatalogEntry>>, ErrorResponse> {
let format_str = params.get("format").map(|s| s.as_str()).unwrap_or("openai");
let format = format_str.parse::<ProviderFormat>().map_err(|_| {
ErrorResponse::bad_request(format!(
"Invalid format '{}'. Must be one of: openai, anthropic, ollama",
format_str
))
})?;
let providers = get_providers_by_format(format).await;
Ok(Json(providers))
}
#[utoipa::path(
get,
path = "/config/provider-catalog/{id}",
params(
("id" = String, Path, description = "Provider ID from models.dev")
),
responses(
(status = 200, description = "Provider template retrieved successfully", body = ProviderTemplate),
(status = 404, description = "Provider not found in catalog")
)
)]
pub async fn get_provider_catalog_template(
Path(id): Path<String>,
) -> Result<Json<ProviderTemplate>, ErrorResponse> {
let template = get_provider_template(&id).ok_or_else(|| {
ErrorResponse::not_found(format!("Provider '{}' not found in catalog", id))
})?;
Ok(Json(template))
}
#[utoipa::path(
post,
path = "/config/providers/{name}/oauth",
params(
("name" = String, Path, description = "Provider name")
),
responses(
(status = 200, description = "OAuth configuration completed"),
(status = 400, description = "OAuth configuration failed")
)
)]
pub async fn configure_provider_oauth(
Path(provider_name): Path<String>,
) -> Result<Json<String>, ErrorResponse> {
use goose::model::ModelConfig;
use goose::providers::create;
if !is_valid_provider_name(&provider_name) {
return Err(ErrorResponse::bad_request(format!(
"Invalid provider name: '{}'",
provider_name
)));
}
let temp_model = ModelConfig::new("temp")
.map_err(|e| {
ErrorResponse::bad_request(format!("Failed to create temporary model config: {}", e))
})?
.with_canonical_limits(&provider_name);
// OAuth configuration does not use extensions.
let provider = create(&provider_name, temp_model, Vec::new())
.await
.map_err(|e| {
ErrorResponse::bad_request(format!(
"Failed to create provider '{}': {}",
provider_name, e
))
})?;
provider.configure_oauth().await.map_err(|e| {
ErrorResponse::bad_request(format!(
"OAuth configuration failed for provider '{}': {}",
provider_name, e
))
})?;
// Mark the provider as configured after successful OAuth
let configured_marker = format!("{}_configured", provider_name);
let config = goose::config::Config::global();
config.set_param(&configured_marker, true)?;
Ok(Json("OAuth configuration completed".to_string()))
}
pub fn routes(state: Arc<AppState>) -> Router {
Router::new()
.route("/config", get(read_all_config))
.route("/config/upsert", post(upsert_config))
.route("/config/remove", post(remove_config))
.route("/config/read", post(read_config))
.route("/config/extensions", get(get_extensions))
.route("/config/extensions", post(add_extension))
.route("/config/extensions/{name}", delete(remove_extension))
.route("/config/providers", get(providers))
.route("/config/providers/{name}/models", get(get_provider_models))
.route("/config/provider-catalog", get(get_provider_catalog))
.route(
"/config/provider-catalog/{id}",
get(get_provider_catalog_template),
)
.route(
"/config/providers/{name}/cleanup",
post(cleanup_provider_cache),
)
.route("/config/slash_commands", get(get_slash_commands))
.route(
"/config/canonical-model-info",
post(get_canonical_model_info),
)
.route("/config/validate", get(validate_config))
.route("/config/permissions", post(upsert_permissions))
.route("/config/custom-providers", post(create_custom_provider))
.route(
"/config/custom-providers/{id}",
delete(remove_custom_provider),
)
.route("/config/custom-providers/{id}", put(update_custom_provider))
.route("/config/custom-providers/{id}", get(get_custom_provider))
.route("/config/check_provider", post(check_provider))
.route("/config/set_provider", post(set_config_provider))
.route(
"/config/providers/{name}/oauth",
post(configure_provider_oauth),
)
.with_state(state)
}
#[cfg(test)]
mod tests {}