From 0ba83e4b5220b1a78f2c301f4d0af80848119ead Mon Sep 17 00:00:00 2001 From: Noah Eisen Date: Mon, 8 Jun 2026 12:55:48 -0700 Subject: [PATCH] Make Bedrock Mantle regions configurable --- .../thread_processor_tests.rs | 1 + codex-rs/config/src/config_toml.rs | 8 ++ codex-rs/config/src/thread_config.rs | 1 + .../proto/codex.thread_config.v1.proto | 1 + .../proto/codex.thread_config.v1.rs | 2 + codex-rs/config/src/thread_config/remote.rs | 13 ++- codex-rs/core/config.schema.json | 9 ++- codex-rs/core/src/compact_tests.rs | 1 + codex-rs/core/src/config/config_tests.rs | 79 ++++++++++++++++++- codex-rs/core/tests/responses_headers.rs | 3 + codex-rs/core/tests/suite/client.rs | 4 + .../core/tests/suite/client_websockets.rs | 1 + .../suite/stream_error_allows_next_turn.rs | 1 + .../core/tests/suite/stream_no_completed.rs | 1 + codex-rs/login/src/auth_env_telemetry.rs | 1 + codex-rs/model-provider-info/src/lib.rs | 54 ++++++++++--- .../src/model_provider_info_tests.rs | 72 ++++++++++++++++- .../src/amazon_bedrock/mantle.rs | 28 +++++-- .../model-provider/src/amazon_bedrock/mod.rs | 28 ++++++- codex-rs/model-provider/src/provider.rs | 1 + 20 files changed, 283 insertions(+), 26 deletions(-) diff --git a/codex-rs/app-server/src/request_processors/thread_processor_tests.rs b/codex-rs/app-server/src/request_processors/thread_processor_tests.rs index c0a909e577..e1ea53316c 100644 --- a/codex-rs/app-server/src/request_processors/thread_processor_tests.rs +++ b/codex-rs/app-server/src/request_processors/thread_processor_tests.rs @@ -599,6 +599,7 @@ mod thread_processor_behavior_tests { experimental_bearer_token: None, auth: None, aws: None, + bedrock_mantle_additional_regions: Vec::new(), wire_api: WireApi::Responses, query_params: None, http_headers: None, diff --git a/codex-rs/config/src/config_toml.rs b/codex-rs/config/src/config_toml.rs index 7c060cadb7..d553f601e6 100644 --- a/codex-rs/config/src/config_toml.rs +++ b/codex-rs/config/src/config_toml.rs @@ -899,8 +899,16 @@ pub fn validate_model_providers( validate_reserved_model_provider_ids(model_providers)?; for (key, provider) in model_providers { if key == AMAZON_BEDROCK_PROVIDER_ID { + provider + .validate_bedrock_mantle_additional_regions() + .map_err(|message| format!("model_providers.{key}: {message}"))?; continue; } + if !provider.bedrock_mantle_additional_regions.is_empty() { + return Err(format!( + "model_providers.{key}: bedrock_mantle_additional_regions is only supported for `{AMAZON_BEDROCK_PROVIDER_ID}`" + )); + } if provider.aws.is_some() { return Err(format!( "model_providers.{key}: provider aws is only supported for `{AMAZON_BEDROCK_PROVIDER_ID}`" diff --git a/codex-rs/config/src/thread_config.rs b/codex-rs/config/src/thread_config.rs index 1b3ea8fe87..11e26dd73e 100644 --- a/codex-rs/config/src/thread_config.rs +++ b/codex-rs/config/src/thread_config.rs @@ -303,6 +303,7 @@ mod tests { experimental_bearer_token: None, auth: None, aws: None, + bedrock_mantle_additional_regions: Vec::new(), wire_api: WireApi::Responses, query_params: None, http_headers: None, diff --git a/codex-rs/config/src/thread_config/proto/codex.thread_config.v1.proto b/codex-rs/config/src/thread_config/proto/codex.thread_config.v1.proto index 1efccfd1bf..24bb48f86f 100644 --- a/codex-rs/config/src/thread_config/proto/codex.thread_config.v1.proto +++ b/codex-rs/config/src/thread_config/proto/codex.thread_config.v1.proto @@ -48,6 +48,7 @@ message ModelProvider { optional uint64 websocket_connect_timeout_ms = 15; bool requires_openai_auth = 16; bool supports_websockets = 17; + repeated string bedrock_mantle_additional_regions = 18; } message StringMap { diff --git a/codex-rs/config/src/thread_config/proto/codex.thread_config.v1.rs b/codex-rs/config/src/thread_config/proto/codex.thread_config.v1.rs index 30a76bc6b2..7aeb8b5b3c 100644 --- a/codex-rs/config/src/thread_config/proto/codex.thread_config.v1.rs +++ b/codex-rs/config/src/thread_config/proto/codex.thread_config.v1.rs @@ -75,6 +75,8 @@ pub struct ModelProvider { pub requires_openai_auth: bool, #[prost(bool, tag = "17")] pub supports_websockets: bool, + #[prost(string, repeated, tag = "18")] + pub bedrock_mantle_additional_regions: ::prost::alloc::vec::Vec<::prost::alloc::string::String>, } #[derive(Clone, PartialEq, ::prost::Message)] pub struct StringMap { diff --git a/codex-rs/config/src/thread_config/remote.rs b/codex-rs/config/src/thread_config/remote.rs index 7b7feacec5..6b1102756a 100644 --- a/codex-rs/config/src/thread_config/remote.rs +++ b/codex-rs/config/src/thread_config/remote.rs @@ -174,6 +174,7 @@ fn model_provider_from_proto( .map(model_provider_auth_from_proto) .transpose()?, aws: None, + bedrock_mantle_additional_regions: provider.bedrock_mantle_additional_regions, wire_api, query_params: provider.query_params.map(|map| map.values), http_headers: provider.http_headers.map(|map| map.values), @@ -201,6 +202,7 @@ fn model_provider_to_proto( experimental_bearer_token, auth, aws: _, + bedrock_mantle_additional_regions, wire_api, query_params, http_headers, @@ -221,6 +223,7 @@ fn model_provider_to_proto( env_key_instructions, experimental_bearer_token, auth: auth.map(model_provider_auth_to_proto), + bedrock_mantle_additional_regions, wire_api: proto_wire_api(wire_api).into(), query_params: query_params.map(proto_string_map), http_headers: http_headers.map(proto_string_map), @@ -300,6 +303,7 @@ mod tests { use std::collections::HashMap; use std::num::NonZeroU64; + use codex_model_provider_info::AMAZON_BEDROCK_PROVIDER_ID; use codex_model_provider_info::ModelProviderInfo; use codex_model_provider_info::WireApi; use codex_protocol::config_types::ModelProviderAuthInfo; @@ -394,11 +398,12 @@ mod tests { #[test] fn model_provider_proto_roundtrips_through_domain_type() { - let expected = expected_provider(); - let proto = model_provider_to_proto("local", expected.clone()); + let mut expected = expected_provider(); + expected.bedrock_mantle_additional_regions = vec!["test-region-1".to_string()]; + let proto = model_provider_to_proto(AMAZON_BEDROCK_PROVIDER_ID, expected.clone()); let (id, actual) = model_provider_from_proto(proto).expect("model provider from proto"); - assert_eq!(id, "local"); + assert_eq!(id, AMAZON_BEDROCK_PROVIDER_ID); assert_eq!(actual, expected); } @@ -448,6 +453,7 @@ mod tests { websocket_connect_timeout_ms: Some(10_000), requires_openai_auth: false, supports_websockets: true, + bedrock_mantle_additional_regions: Vec::new(), }], features: HashMap::from([ ("plugins".to_string(), false), @@ -512,6 +518,7 @@ mod tests { requires_openai_auth: false, supports_websockets: true, aws: None, + bedrock_mantle_additional_regions: Vec::new(), } } diff --git a/codex-rs/core/config.schema.json b/codex-rs/core/config.schema.json index b30b868bc7..93567a336f 100644 --- a/codex-rs/core/config.schema.json +++ b/codex-rs/core/config.schema.json @@ -1489,6 +1489,13 @@ "description": "Base URL for the provider's OpenAI-compatible API.", "type": "string" }, + "bedrock_mantle_additional_regions": { + "description": "Additional AWS regions allowed for the built-in Amazon Bedrock Mantle endpoint.", + "items": { + "type": "string" + }, + "type": "array" + }, "env_http_headers": { "additionalProperties": { "type": "string" @@ -5224,4 +5231,4 @@ }, "title": "ConfigToml", "type": "object" -} +} \ No newline at end of file diff --git a/codex-rs/core/src/compact_tests.rs b/codex-rs/core/src/compact_tests.rs index 69bd95f2df..ccfd47a596 100644 --- a/codex-rs/core/src/compact_tests.rs +++ b/codex-rs/core/src/compact_tests.rs @@ -225,6 +225,7 @@ fn should_use_remote_compact_task_for_azure_provider() { experimental_bearer_token: None, auth: None, aws: None, + bedrock_mantle_additional_regions: Vec::new(), wire_api: WireApi::Responses, query_params: None, http_headers: None, diff --git a/codex-rs/core/src/config/config_tests.rs b/codex-rs/core/src/config/config_tests.rs index 3a3b490d69..1e510a71c7 100644 --- a/codex-rs/core/src/config/config_tests.rs +++ b/codex-rs/core/src/config/config_tests.rs @@ -550,6 +550,37 @@ profile = "codex-bedrock" ); } +#[test] +fn rejects_bedrock_mantle_additional_regions_for_custom_provider() { + let err = toml::from_str::( + r#" +[model_providers.custom] +name = "Custom Provider" +bedrock_mantle_additional_regions = ["test-region-1"] +"#, + ) + .unwrap_err(); + + assert!(err.to_string().contains( + "model_providers.custom: bedrock_mantle_additional_regions is only supported for `amazon-bedrock`" + )); +} + +#[test] +fn rejects_invalid_bedrock_mantle_additional_region() { + let err = toml::from_str::( + r#" +[model_providers.amazon-bedrock] +bedrock_mantle_additional_regions = ["test-region-1.api.aws/redirect"] +"#, + ) + .unwrap_err(); + + assert!(err.to_string().contains( + "model_providers.amazon-bedrock: invalid Bedrock Mantle additional region `test-region-1.api.aws/redirect`" + )); +} + #[test] fn accepts_amazon_bedrock_aws_profile_override() { let cfg = toml::from_str::( @@ -577,6 +608,52 @@ region = "us-west-2" ); } +#[tokio::test] +async fn load_config_applies_amazon_bedrock_additional_regions() { + let cfg = toml::from_str::( + r#" +model_provider = "amazon-bedrock" + +[model_providers.amazon-bedrock] +bedrock_mantle_additional_regions = ["test-region-1"] + +[model_providers.amazon-bedrock.aws] +profile = "codex-bedrock" +region = "test-region-1" +"#, + ) + .expect("Amazon Bedrock additional regions should deserialize"); + + let config = Config::load_from_base_config_with_overrides( + cfg, + ConfigOverrides::default(), + tempdir().expect("tempdir").abs(), + ) + .await + .expect("load config"); + + assert_eq!( + config.model_provider.bedrock_mantle_additional_regions, + vec!["test-region-1".to_string()] + ); + assert_eq!( + config + .model_provider + .aws + .as_ref() + .and_then(|aws| aws.profile.as_deref()), + Some("codex-bedrock") + ); + assert_eq!( + config + .model_provider + .aws + .as_ref() + .and_then(|aws| aws.region.as_deref()), + Some("test-region-1") + ); +} + #[tokio::test] async fn load_config_applies_amazon_bedrock_aws_profile_override() { let cfg = toml::from_str::( @@ -646,7 +723,7 @@ region = "us-west-2" assert_eq!(err.kind(), std::io::ErrorKind::InvalidData); assert!(err.to_string().contains( - "model_providers.amazon-bedrock only supports changing `aws.profile` and `aws.region`; other non-default provider fields are not supported" + "model_providers.amazon-bedrock only supports changing `aws.profile`, `aws.region`, and `bedrock_mantle_additional_regions`; other non-default provider fields are not supported" )); } diff --git a/codex-rs/core/tests/responses_headers.rs b/codex-rs/core/tests/responses_headers.rs index 1ba93aec10..ff480f37eb 100644 --- a/codex-rs/core/tests/responses_headers.rs +++ b/codex-rs/core/tests/responses_headers.rs @@ -58,6 +58,7 @@ async fn responses_stream_includes_subagent_header_on_review() { experimental_bearer_token: None, auth: None, aws: None, + bedrock_mantle_additional_regions: Vec::new(), wire_api: WireApi::Responses, query_params: None, http_headers: None, @@ -190,6 +191,7 @@ async fn responses_stream_includes_subagent_header_on_other() { experimental_bearer_token: None, auth: None, aws: None, + bedrock_mantle_additional_regions: Vec::new(), wire_api: WireApi::Responses, query_params: None, http_headers: None, @@ -303,6 +305,7 @@ async fn responses_respects_model_info_overrides_from_config() { experimental_bearer_token: None, auth: None, aws: None, + bedrock_mantle_additional_regions: Vec::new(), wire_api: WireApi::Responses, query_params: None, http_headers: None, diff --git a/codex-rs/core/tests/suite/client.rs b/codex-rs/core/tests/suite/client.rs index 2fcb73e25a..88182469cf 100644 --- a/codex-rs/core/tests/suite/client.rs +++ b/codex-rs/core/tests/suite/client.rs @@ -860,6 +860,7 @@ async fn send_provider_auth_request(server: &MockServer, auth: ModelProviderAuth experimental_bearer_token: None, auth: Some(auth), aws: None, + bedrock_mantle_additional_regions: Vec::new(), wire_api: WireApi::Responses, query_params: None, http_headers: None, @@ -2371,6 +2372,7 @@ async fn azure_responses_request_includes_store_and_reasoning_ids() { experimental_bearer_token: None, auth: None, aws: None, + bedrock_mantle_additional_regions: Vec::new(), wire_api: WireApi::Responses, query_params: None, http_headers: None, @@ -2978,6 +2980,7 @@ async fn azure_overrides_assign_properties_used_for_responses_url() { experimental_bearer_token: None, auth: None, aws: None, + bedrock_mantle_additional_regions: Vec::new(), query_params: Some(std::collections::HashMap::from([( "api-version".to_string(), "2025-04-01-preview".to_string(), @@ -3073,6 +3076,7 @@ async fn env_var_overrides_loaded_auth() { experimental_bearer_token: None, auth: None, aws: None, + bedrock_mantle_additional_regions: Vec::new(), wire_api: WireApi::Responses, http_headers: Some(std::collections::HashMap::from([( "Custom-Header".to_string(), diff --git a/codex-rs/core/tests/suite/client_websockets.rs b/codex-rs/core/tests/suite/client_websockets.rs index 3e1ed23261..9520855323 100755 --- a/codex-rs/core/tests/suite/client_websockets.rs +++ b/codex-rs/core/tests/suite/client_websockets.rs @@ -1989,6 +1989,7 @@ fn websocket_provider_with_connect_timeout( experimental_bearer_token: None, auth: None, aws: None, + bedrock_mantle_additional_regions: Vec::new(), wire_api: WireApi::Responses, query_params: None, http_headers: None, diff --git a/codex-rs/core/tests/suite/stream_error_allows_next_turn.rs b/codex-rs/core/tests/suite/stream_error_allows_next_turn.rs index d82692c26c..39a05092de 100644 --- a/codex-rs/core/tests/suite/stream_error_allows_next_turn.rs +++ b/codex-rs/core/tests/suite/stream_error_allows_next_turn.rs @@ -71,6 +71,7 @@ async fn continue_after_stream_error() { experimental_bearer_token: None, auth: None, aws: None, + bedrock_mantle_additional_regions: Vec::new(), wire_api: WireApi::Responses, query_params: None, http_headers: None, diff --git a/codex-rs/core/tests/suite/stream_no_completed.rs b/codex-rs/core/tests/suite/stream_no_completed.rs index 471c60db5a..8e71058f2e 100644 --- a/codex-rs/core/tests/suite/stream_no_completed.rs +++ b/codex-rs/core/tests/suite/stream_no_completed.rs @@ -53,6 +53,7 @@ async fn retries_on_early_close() { experimental_bearer_token: None, auth: None, aws: None, + bedrock_mantle_additional_regions: Vec::new(), wire_api: WireApi::Responses, query_params: None, http_headers: None, diff --git a/codex-rs/login/src/auth_env_telemetry.rs b/codex-rs/login/src/auth_env_telemetry.rs index 3cec5a4ce3..ec346ea67c 100644 --- a/codex-rs/login/src/auth_env_telemetry.rs +++ b/codex-rs/login/src/auth_env_telemetry.rs @@ -66,6 +66,7 @@ mod tests { experimental_bearer_token: None, auth: None, aws: None, + bedrock_mantle_additional_regions: Vec::new(), wire_api: WireApi::Responses, query_params: None, http_headers: None, diff --git a/codex-rs/model-provider-info/src/lib.rs b/codex-rs/model-provider-info/src/lib.rs index 5f0f840508..85ffd47fdd 100644 --- a/codex-rs/model-provider-info/src/lib.rs +++ b/codex-rs/model-provider-info/src/lib.rs @@ -102,6 +102,9 @@ pub struct ModelProviderInfo { pub auth: Option, /// AWS SigV4 auth configuration for this provider. pub aws: Option, + /// Additional AWS regions allowed for the built-in Amazon Bedrock Mantle endpoint. + #[serde(default, skip_serializing_if = "Vec::is_empty")] + pub bedrock_mantle_additional_regions: Vec, /// Which wire protocol this provider expects. #[serde(default)] pub wire_api: WireApi, @@ -147,6 +150,26 @@ pub struct ModelProviderAwsAuthInfo { } impl ModelProviderInfo { + /// Validates configured additions to the built-in Bedrock Mantle region allowlist. + pub fn validate_bedrock_mantle_additional_regions(&self) -> std::result::Result<(), String> { + for region in &self.bedrock_mantle_additional_regions { + let bytes = region.as_bytes(); + let is_valid = !bytes.is_empty() + && bytes.len() <= 63 + && bytes.first().is_some_and(u8::is_ascii_alphanumeric) + && bytes.last().is_some_and(u8::is_ascii_alphanumeric) + && bytes.iter().all(|byte| { + byte.is_ascii_lowercase() || byte.is_ascii_digit() || *byte == b'-' + }); + if !is_valid { + return Err(format!( + "invalid Bedrock Mantle additional region `{region}`; expected a DNS-safe lowercase region label" + )); + } + } + Ok(()) + } + pub fn validate(&self) -> std::result::Result<(), String> { if self.aws.is_some() { if self.supports_websockets { @@ -330,6 +353,7 @@ impl ModelProviderInfo { experimental_bearer_token: None, auth: None, aws: None, + bedrock_mantle_additional_regions: Vec::new(), wire_api: WireApi::Responses, query_params: None, http_headers: Some( @@ -372,6 +396,7 @@ impl ModelProviderInfo { profile: None, region: None, })), + bedrock_mantle_additional_regions: Vec::new(), wire_api: WireApi::Responses, query_params: None, http_headers: Some(HashMap::from([( @@ -444,7 +469,7 @@ pub fn built_in_model_providers( /// /// Configured providers extend the built-in set. Built-in providers are not /// generally overridable, but the built-in Amazon Bedrock provider allows the -/// user to set `aws.profile` and `aws.region`. +/// user to set `aws.profile`, `aws.region`, and additional Bedrock Mantle regions. pub fn merge_configured_model_providers( mut model_providers: HashMap, configured_model_providers: HashMap, @@ -452,22 +477,28 @@ pub fn merge_configured_model_providers( for (key, mut provider) in configured_model_providers { if key == AMAZON_BEDROCK_PROVIDER_ID { let aws_override = provider.aws.take(); + provider.validate_bedrock_mantle_additional_regions()?; + let bedrock_mantle_additional_regions = + std::mem::take(&mut provider.bedrock_mantle_additional_regions); if provider != ModelProviderInfo::default() { return Err(format!( "model_providers.{AMAZON_BEDROCK_PROVIDER_ID} only supports changing \ -`aws.profile` and `aws.region`; other non-default provider fields are not supported" +`aws.profile`, `aws.region`, and `bedrock_mantle_additional_regions`; other non-default provider fields are not supported" )); } - if let Some(aws_override) = aws_override - && let Some(built_in_provider) = model_providers.get_mut(AMAZON_BEDROCK_PROVIDER_ID) - && let Some(built_in_aws) = built_in_provider.aws.as_mut() - { - if let Some(profile) = aws_override.profile { - built_in_aws.profile = Some(profile); - } - if let Some(region) = aws_override.region { - built_in_aws.region = Some(region); + if let Some(built_in_provider) = model_providers.get_mut(AMAZON_BEDROCK_PROVIDER_ID) { + built_in_provider.bedrock_mantle_additional_regions = + bedrock_mantle_additional_regions; + if let Some(aws_override) = aws_override + && let Some(built_in_aws) = built_in_provider.aws.as_mut() + { + if let Some(profile) = aws_override.profile { + built_in_aws.profile = Some(profile); + } + if let Some(region) = aws_override.region { + built_in_aws.region = Some(region); + } } } } else { @@ -506,6 +537,7 @@ pub fn create_oss_provider_with_base_url(base_url: &str, wire_api: WireApi) -> M experimental_bearer_token: None, auth: None, aws: None, + bedrock_mantle_additional_regions: Vec::new(), wire_api, query_params: None, http_headers: None, diff --git a/codex-rs/model-provider-info/src/model_provider_info_tests.rs b/codex-rs/model-provider-info/src/model_provider_info_tests.rs index a303050dc1..77b64824fc 100644 --- a/codex-rs/model-provider-info/src/model_provider_info_tests.rs +++ b/codex-rs/model-provider-info/src/model_provider_info_tests.rs @@ -19,6 +19,7 @@ base_url = "http://localhost:11434/v1" experimental_bearer_token: None, auth: None, aws: None, + bedrock_mantle_additional_regions: Vec::new(), wire_api: WireApi::Responses, query_params: None, http_headers: None, @@ -51,6 +52,7 @@ query_params = { api-version = "2025-04-01-preview" } experimental_bearer_token: None, auth: None, aws: None, + bedrock_mantle_additional_regions: Vec::new(), wire_api: WireApi::Responses, query_params: Some(maplit::hashmap! { "api-version".to_string() => "2025-04-01-preview".to_string(), @@ -86,6 +88,7 @@ env_http_headers = { "X-Example-Env-Header" = "EXAMPLE_ENV_VAR" } experimental_bearer_token: None, auth: None, aws: None, + bedrock_mantle_additional_regions: Vec::new(), wire_api: WireApi::Responses, query_params: None, http_headers: Some(maplit::hashmap! { @@ -158,6 +161,7 @@ fn test_supports_remote_compaction_for_azure_name() { experimental_bearer_token: None, auth: None, aws: None, + bedrock_mantle_additional_regions: Vec::new(), wire_api: WireApi::Responses, query_params: None, http_headers: None, @@ -183,6 +187,7 @@ fn test_supports_remote_compaction_for_non_openai_non_azure_provider() { experimental_bearer_token: None, auth: None, aws: None, + bedrock_mantle_additional_regions: Vec::new(), wire_api: WireApi::Responses, query_params: None, http_headers: None, @@ -248,6 +253,45 @@ region = "us-west-2" ); } +#[test] +fn test_deserialize_bedrock_mantle_additional_regions() { + let provider_toml = r#" +bedrock_mantle_additional_regions = ["test-region-1"] + "#; + + let provider: ModelProviderInfo = toml::from_str(provider_toml).unwrap(); + + assert_eq!( + provider.bedrock_mantle_additional_regions, + vec!["test-region-1".to_string()] + ); +} + +#[test] +fn test_validate_bedrock_mantle_additional_regions_rejects_invalid_dns_labels() { + for region in [ + "", + "-test-region-1", + "test-region-1-", + "TEST-region-1", + "test_region_1", + "test-region-1.api.aws", + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", + ] { + let provider = ModelProviderInfo { + bedrock_mantle_additional_regions: vec![region.to_string()], + ..ModelProviderInfo::default() + }; + + assert_eq!( + provider.validate_bedrock_mantle_additional_regions(), + Err(format!( + "invalid Bedrock Mantle additional region `{region}`; expected a DNS-safe lowercase region label" + )) + ); + } +} + #[test] fn test_create_amazon_bedrock_provider() { assert_eq!( @@ -263,6 +307,7 @@ fn test_create_amazon_bedrock_provider() { profile: None, region: None, }), + bedrock_mantle_additional_regions: Vec::new(), wire_api: WireApi::Responses, query_params: None, http_headers: Some(maplit::hashmap! { @@ -360,6 +405,31 @@ fn test_merge_configured_model_providers_applies_amazon_bedrock_profile_override ); } +#[test] +fn test_merge_configured_model_providers_applies_amazon_bedrock_additional_regions() { + let configured_model_providers = std::collections::HashMap::from([( + AMAZON_BEDROCK_PROVIDER_ID.to_string(), + ModelProviderInfo { + bedrock_mantle_additional_regions: vec!["test-region-1".to_string()], + ..ModelProviderInfo::default() + }, + )]); + + let mut expected = built_in_model_providers(/*openai_base_url*/ None); + expected + .get_mut(AMAZON_BEDROCK_PROVIDER_ID) + .expect("Amazon Bedrock provider should be built in") + .bedrock_mantle_additional_regions = vec!["test-region-1".to_string()]; + + assert_eq!( + merge_configured_model_providers( + built_in_model_providers(/*openai_base_url*/ None), + configured_model_providers, + ), + Ok(expected) + ); +} + #[test] fn test_merge_configured_model_providers_rejects_amazon_bedrock_non_default_fields() { let configured_model_providers = std::collections::HashMap::from([( @@ -380,7 +450,7 @@ fn test_merge_configured_model_providers_rejects_amazon_bedrock_non_default_fiel configured_model_providers, ), Err( - "model_providers.amazon-bedrock only supports changing `aws.profile` and `aws.region`; other non-default provider fields are not supported" + "model_providers.amazon-bedrock only supports changing `aws.profile`, `aws.region`, and `bedrock_mantle_additional_regions`; other non-default provider fields are not supported" .to_string() ) ); diff --git a/codex-rs/model-provider/src/amazon_bedrock/mantle.rs b/codex-rs/model-provider/src/amazon_bedrock/mantle.rs index 7881845e45..d9b00f9d19 100644 --- a/codex-rs/model-provider/src/amazon_bedrock/mantle.rs +++ b/codex-rs/model-provider/src/amazon_bedrock/mantle.rs @@ -38,8 +38,12 @@ pub(super) fn region_from_config(aws: &ModelProviderAwsAuthInfo) -> Option Result { - if BEDROCK_MANTLE_SUPPORTED_REGIONS.contains(®ion) { +pub(super) fn base_url(region: &str, additional_regions: &[String]) -> Result { + if BEDROCK_MANTLE_SUPPORTED_REGIONS.contains(®ion) + || additional_regions + .iter() + .any(|additional| additional == region) + { Ok(format!("https://bedrock-mantle.{region}.api.aws/openai/v1")) } else { Err(CodexErr::Fatal(format!( @@ -48,9 +52,12 @@ pub(super) fn base_url(region: &str) -> Result { } } -pub(super) async fn runtime_base_url(aws: &ModelProviderAwsAuthInfo) -> Result { +pub(super) async fn runtime_base_url( + aws: &ModelProviderAwsAuthInfo, + additional_regions: &[String], +) -> Result { let region = resolve_region(aws).await?; - base_url(®ion) + base_url(®ion, additional_regions) } async fn resolve_region(aws: &ModelProviderAwsAuthInfo) -> Result { @@ -69,14 +76,23 @@ mod tests { #[test] fn base_url_uses_region_endpoint() { assert_eq!( - base_url("ap-northeast-1").expect("supported region"), + base_url("ap-northeast-1", &[]).expect("supported region"), "https://bedrock-mantle.ap-northeast-1.api.aws/openai/v1" ); } + #[test] + fn base_url_uses_configured_additional_region_endpoint() { + assert_eq!( + base_url("test-region-1", &["test-region-1".to_string()]) + .expect("configured additional region"), + "https://bedrock-mantle.test-region-1.api.aws/openai/v1" + ); + } + #[test] fn base_url_rejects_unsupported_region() { - let err = base_url("us-west-1").expect_err("unsupported region"); + let err = base_url("us-west-1", &[]).expect_err("unsupported region"); assert_eq!( err.to_string(), diff --git a/codex-rs/model-provider/src/amazon_bedrock/mod.rs b/codex-rs/model-provider/src/amazon_bedrock/mod.rs index 8099993973..8d104beda1 100644 --- a/codex-rs/model-provider/src/amazon_bedrock/mod.rs +++ b/codex-rs/model-provider/src/amazon_bedrock/mod.rs @@ -85,12 +85,15 @@ impl ModelProvider for AmazonBedrockModelProvider { async fn api_provider(&self) -> Result { let mut api_provider_info = self.info.clone(); - api_provider_info.base_url = Some(runtime_base_url(&self.aws).await?); + api_provider_info.base_url = + Some(runtime_base_url(&self.aws, &self.info.bedrock_mantle_additional_regions).await?); api_provider_info.to_api_provider(/*auth_mode*/ None) } async fn runtime_base_url(&self) -> Result> { - Ok(Some(runtime_base_url(&self.aws).await?)) + Ok(Some( + runtime_base_url(&self.aws, &self.info.bedrock_mantle_additional_regions).await?, + )) } async fn api_auth(&self) -> Result { @@ -120,7 +123,7 @@ mod tests { let region = "eu-central-1"; let mut api_provider_info = ModelProviderInfo::create_amazon_bedrock_provider(/*aws*/ None); - api_provider_info.base_url = Some(mantle::base_url(region).expect("supported region")); + api_provider_info.base_url = Some(mantle::base_url(region, &[]).expect("supported region")); let api_provider = api_provider_info .to_api_provider(/*auth_mode*/ None) .expect("api provider should build"); @@ -131,6 +134,25 @@ mod tests { ); } + #[tokio::test] + async fn runtime_base_url_uses_configured_additional_region_endpoint() { + let mut provider_info = + ModelProviderInfo::create_amazon_bedrock_provider(Some(ModelProviderAwsAuthInfo { + profile: None, + region: Some("test-region-1".to_string()), + })); + provider_info.bedrock_mantle_additional_regions = vec!["test-region-1".to_string()]; + let provider = AmazonBedrockModelProvider::new(provider_info); + + assert_eq!( + provider + .runtime_base_url() + .await + .expect("configured additional region"), + Some("https://bedrock-mantle.test-region-1.api.aws/openai/v1".to_string()) + ); + } + #[test] fn capabilities_disable_unsupported_hosted_tools() { let provider = AmazonBedrockModelProvider::new( diff --git a/codex-rs/model-provider/src/provider.rs b/codex-rs/model-provider/src/provider.rs index afd07065d4..4bb13224a1 100644 --- a/codex-rs/model-provider/src/provider.rs +++ b/codex-rs/model-provider/src/provider.rs @@ -312,6 +312,7 @@ mod tests { experimental_bearer_token: None, auth: None, aws: None, + bedrock_mantle_additional_regions: Vec::new(), wire_api: WireApi::Responses, query_params: None, http_headers: None,