mirror of
https://github.com/openai/codex.git
synced 2026-11-15 21:50:46 +00:00
Support explicit provider model catalog URLs (#46561)
## Why Providers need to serve Codex model metadata independently of their inference endpoint. A custom inference `base_url` alone should not opt API-key sessions into remote catalog discovery. ## What changed - Add `model_catalog_url` to provider configuration and thread-config serialization. Fetch the full catalog URL with provider authentication, headers, query parameters, and `client_version`, while preserving inference routing. - Require an explicit catalog URL for API-key discovery with a custom base URL. Keep default OpenAI discovery on the Codex backend, and apply `api_key_model_discovery` gating to provider API keys and cached catalogs. - Include the catalog URL in cache identity so different catalogs do not share cached metadata. - Limit explicitly configured catalog responses to 1 MiB, reject redirects, and suppress URL and response diagnostics that could expose credentials. ## Testing Add coverage for catalog configuration round-trips, query encoding, authentication and header reuse, cache identity and discovery gating, oversized responses, and redirect rejection without credential forwarding. Add an integration test verifying that catalog model metadata and instructions reach conversation requests. GitOrigin-RevId: 7f6ac5eec257278685e1867a6bcebae6514a365a
This commit is contained in:
@@ -11,6 +11,7 @@ use http::HeaderMap;
|
||||
use http::Method;
|
||||
use http::header::ETAG;
|
||||
use std::sync::Arc;
|
||||
use url::Url;
|
||||
|
||||
pub struct ModelsClient<T: HttpTransport> {
|
||||
session: EndpointSession<T>,
|
||||
@@ -44,17 +45,36 @@ impl<T: HttpTransport> ModelsClient<T> {
|
||||
request.url
|
||||
}
|
||||
|
||||
/// Builds a full catalog URL, preserving provider routing parameters and client version.
|
||||
pub fn catalog_request_url(
|
||||
provider: &Provider,
|
||||
catalog_url: &str,
|
||||
client_version: &str,
|
||||
) -> Result<String, ApiError> {
|
||||
let invalid = |message: &str| ApiError::InvalidRequest {
|
||||
message: message.to_string(),
|
||||
};
|
||||
let mut url = Url::parse(catalog_url)
|
||||
.map_err(|_| invalid("model_catalog_url must be an absolute URL"))?;
|
||||
{
|
||||
let mut query = url.query_pairs_mut();
|
||||
if let Some(params) = &provider.query_params {
|
||||
query.extend_pairs(params);
|
||||
}
|
||||
query.append_pair("client_version", client_version);
|
||||
}
|
||||
Ok(url.into())
|
||||
}
|
||||
|
||||
/// Fetches and decodes a catalog, optionally bounding response bytes before decoding.
|
||||
pub async fn list_models(
|
||||
&self,
|
||||
request_url: String,
|
||||
extra_headers: HeaderMap,
|
||||
response_body_limit_bytes: Option<usize>,
|
||||
) -> Result<(Vec<ModelInfo>, Option<String>), ApiError> {
|
||||
let (body, header_etag) = self
|
||||
.list_models_raw(
|
||||
request_url,
|
||||
extra_headers,
|
||||
/*response_body_limit_bytes*/ None,
|
||||
)
|
||||
.list_models_raw(request_url, extra_headers, response_body_limit_bytes)
|
||||
.await?;
|
||||
let ModelsResponse { models } =
|
||||
serde_json::from_slice::<ModelsResponse>(&body).map_err(|e| {
|
||||
@@ -207,6 +227,39 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn catalog_request_url_preserves_routing_and_encodes_queries() {
|
||||
let mut provider = provider("https://gateway.example/v1");
|
||||
provider.query_params = Some(std::collections::HashMap::from([(
|
||||
"api-version".to_string(),
|
||||
"2026 09".to_string(),
|
||||
)]));
|
||||
let url = ModelsClient::<CapturingTransport>::catalog_request_url(
|
||||
&provider,
|
||||
"https://catalog.example/codex/models?deployment=one",
|
||||
"1.2.3",
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
url,
|
||||
"https://catalog.example/codex/models?deployment=one&api-version=2026+09&client_version=1.2.3"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn catalog_request_url_rejects_relative_urls() {
|
||||
let provider = provider("https://gateway.example/v1");
|
||||
for catalog_url in ["/codex/models", "codex/models"] {
|
||||
let error = ModelsClient::<CapturingTransport>::catalog_request_url(
|
||||
&provider,
|
||||
catalog_url,
|
||||
"1.2.3",
|
||||
)
|
||||
.unwrap_err();
|
||||
assert!(matches!(error, ApiError::InvalidRequest { .. }));
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn response_body_limit_survives_auth_retry_without_limiting_other_models_requests() {
|
||||
let transport = CapturingTransport::default();
|
||||
@@ -217,7 +270,7 @@ mod tests {
|
||||
let client = ModelsClient::new(transport.clone(), provider, auth.clone());
|
||||
|
||||
client
|
||||
.list_models_raw(
|
||||
.list_models(
|
||||
request_url.clone(),
|
||||
HeaderMap::new(),
|
||||
/*response_body_limit_bytes*/ Some(64),
|
||||
@@ -233,7 +286,11 @@ mod tests {
|
||||
}
|
||||
|
||||
let (models, _) = client
|
||||
.list_models(request_url, HeaderMap::new())
|
||||
.list_models(
|
||||
request_url,
|
||||
HeaderMap::new(),
|
||||
/*response_body_limit_bytes*/ None,
|
||||
)
|
||||
.await
|
||||
.expect("ordinary request on the same client should remain unbounded");
|
||||
assert!(models.is_empty());
|
||||
@@ -268,7 +325,11 @@ mod tests {
|
||||
let client = ModelsClient::new(transport.clone(), provider, Arc::new(DummyAuth));
|
||||
|
||||
let (models, _) = client
|
||||
.list_models(request_url, HeaderMap::new())
|
||||
.list_models(
|
||||
request_url,
|
||||
HeaderMap::new(),
|
||||
/*response_body_limit_bytes*/ None,
|
||||
)
|
||||
.await
|
||||
.expect("request should succeed");
|
||||
|
||||
@@ -327,7 +388,11 @@ mod tests {
|
||||
let client = ModelsClient::new(transport, provider, Arc::new(DummyAuth));
|
||||
|
||||
let (models, _) = client
|
||||
.list_models(request_url, HeaderMap::new())
|
||||
.list_models(
|
||||
request_url,
|
||||
HeaderMap::new(),
|
||||
/*response_body_limit_bytes*/ None,
|
||||
)
|
||||
.await
|
||||
.expect("request should succeed");
|
||||
|
||||
@@ -352,7 +417,11 @@ mod tests {
|
||||
let client = ModelsClient::new(transport, provider, Arc::new(DummyAuth));
|
||||
|
||||
let (models, etag) = client
|
||||
.list_models(request_url, HeaderMap::new())
|
||||
.list_models(
|
||||
request_url,
|
||||
HeaderMap::new(),
|
||||
/*response_body_limit_bytes*/ None,
|
||||
)
|
||||
.await
|
||||
.expect("request should succeed");
|
||||
|
||||
|
||||
Reference in New Issue
Block a user