mirror of
https://github.com/openai/codex.git
synced 2026-05-16 01:02:48 +00:00
## Why Once the repo-local lint exists, `codex-rs` needs to follow the checked-in convention and CI needs to keep it from drifting. This commit applies the fallback `/*param*/` style consistently across existing positional literal call sites without changing those APIs. The longer-term preference is still to avoid APIs that require comments by choosing clearer parameter types and call shapes. This PR is intentionally the mechanical follow-through for the places where the existing signatures stay in place. After rebasing onto newer `main`, the rollout also had to cover newly introduced `tui_app_server` call sites. That made it clear the first cut of the CI job was too expensive for the common path: it was spending almost as much time installing `cargo-dylint` and re-testing the lint crate as a representative test job spends running product tests. The CI update keeps the full workspace enforcement but trims that extra overhead from ordinary `codex-rs` PRs. ## What changed - keep a dedicated `argument_comment_lint` job in `rust-ci` - mechanically annotate remaining opaque positional literals across `codex-rs` with exact `/*param*/` comments, including the rebased `tui_app_server` call sites that now fall under the lint - keep the checked-in style aligned with the lint policy by using `/*param*/` and leaving string and char literals uncommented - cache `cargo-dylint`, `dylint-link`, and the relevant Cargo registry/git metadata in the lint job - split changed-path detection so the lint crate's own `cargo test` step runs only when `tools/argument-comment-lint/*` or `rust-ci.yml` changes - continue to run the repo wrapper over the `codex-rs` workspace, so product-code enforcement is unchanged Most of the code changes in this commit are intentionally mechanical comment rewrites or insertions driven by the lint itself. ## Verification - `./tools/argument-comment-lint/run.sh --workspace` - `cargo test -p codex-tui-app-server -p codex-tui` - parsed `.github/workflows/rust-ci.yml` locally with PyYAML --- * -> #14652 * #14651
273 lines
8.3 KiB
Rust
273 lines
8.3 KiB
Rust
use crate::auth::AuthProvider;
|
|
use crate::endpoint::session::EndpointSession;
|
|
use crate::error::ApiError;
|
|
use crate::provider::Provider;
|
|
use codex_client::HttpTransport;
|
|
use codex_client::RequestTelemetry;
|
|
use codex_protocol::openai_models::ModelInfo;
|
|
use codex_protocol::openai_models::ModelsResponse;
|
|
use http::HeaderMap;
|
|
use http::Method;
|
|
use http::header::ETAG;
|
|
use std::sync::Arc;
|
|
|
|
pub struct ModelsClient<T: HttpTransport, A: AuthProvider> {
|
|
session: EndpointSession<T, A>,
|
|
}
|
|
|
|
impl<T: HttpTransport, A: AuthProvider> ModelsClient<T, A> {
|
|
pub fn new(transport: T, provider: Provider, auth: A) -> Self {
|
|
Self {
|
|
session: EndpointSession::new(transport, provider, auth),
|
|
}
|
|
}
|
|
|
|
pub fn with_telemetry(self, request: Option<Arc<dyn RequestTelemetry>>) -> Self {
|
|
Self {
|
|
session: self.session.with_request_telemetry(request),
|
|
}
|
|
}
|
|
|
|
fn path() -> &'static str {
|
|
"models"
|
|
}
|
|
|
|
fn append_client_version_query(req: &mut codex_client::Request, client_version: &str) {
|
|
let separator = if req.url.contains('?') { '&' } else { '?' };
|
|
req.url = format!("{}{}client_version={client_version}", req.url, separator);
|
|
}
|
|
|
|
pub async fn list_models(
|
|
&self,
|
|
client_version: &str,
|
|
extra_headers: HeaderMap,
|
|
) -> Result<(Vec<ModelInfo>, Option<String>), ApiError> {
|
|
let resp = self
|
|
.session
|
|
.execute_with(
|
|
Method::GET,
|
|
Self::path(),
|
|
extra_headers,
|
|
/*body*/ None,
|
|
|req| {
|
|
Self::append_client_version_query(req, client_version);
|
|
},
|
|
)
|
|
.await?;
|
|
|
|
let header_etag = resp
|
|
.headers
|
|
.get(ETAG)
|
|
.and_then(|value| value.to_str().ok())
|
|
.map(ToString::to_string);
|
|
|
|
let ModelsResponse { models } = serde_json::from_slice::<ModelsResponse>(&resp.body)
|
|
.map_err(|e| {
|
|
ApiError::Stream(format!(
|
|
"failed to decode models response: {e}; body: {}",
|
|
String::from_utf8_lossy(&resp.body)
|
|
))
|
|
})?;
|
|
|
|
Ok((models, header_etag))
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
use crate::provider::RetryConfig;
|
|
use async_trait::async_trait;
|
|
use codex_client::Request;
|
|
use codex_client::Response;
|
|
use codex_client::StreamResponse;
|
|
use codex_client::TransportError;
|
|
use http::HeaderMap;
|
|
use http::StatusCode;
|
|
use pretty_assertions::assert_eq;
|
|
use serde_json::json;
|
|
use std::sync::Arc;
|
|
use std::sync::Mutex;
|
|
use std::time::Duration;
|
|
|
|
#[derive(Clone)]
|
|
struct CapturingTransport {
|
|
last_request: Arc<Mutex<Option<Request>>>,
|
|
body: Arc<ModelsResponse>,
|
|
etag: Option<String>,
|
|
}
|
|
|
|
impl Default for CapturingTransport {
|
|
fn default() -> Self {
|
|
Self {
|
|
last_request: Arc::new(Mutex::new(None)),
|
|
body: Arc::new(ModelsResponse { models: Vec::new() }),
|
|
etag: None,
|
|
}
|
|
}
|
|
}
|
|
|
|
#[async_trait]
|
|
impl HttpTransport for CapturingTransport {
|
|
async fn execute(&self, req: Request) -> Result<Response, TransportError> {
|
|
*self.last_request.lock().unwrap() = Some(req);
|
|
let body = serde_json::to_vec(&*self.body).unwrap();
|
|
let mut headers = HeaderMap::new();
|
|
if let Some(etag) = &self.etag {
|
|
headers.insert(ETAG, etag.parse().unwrap());
|
|
}
|
|
Ok(Response {
|
|
status: StatusCode::OK,
|
|
headers,
|
|
body: body.into(),
|
|
})
|
|
}
|
|
|
|
async fn stream(&self, _req: Request) -> Result<StreamResponse, TransportError> {
|
|
Err(TransportError::Build("stream should not run".to_string()))
|
|
}
|
|
}
|
|
|
|
#[derive(Clone, Default)]
|
|
struct DummyAuth;
|
|
|
|
impl AuthProvider for DummyAuth {
|
|
fn bearer_token(&self) -> Option<String> {
|
|
None
|
|
}
|
|
}
|
|
|
|
fn provider(base_url: &str) -> Provider {
|
|
Provider {
|
|
name: "test".to_string(),
|
|
base_url: base_url.to_string(),
|
|
query_params: None,
|
|
headers: HeaderMap::new(),
|
|
retry: RetryConfig {
|
|
max_attempts: 1,
|
|
base_delay: Duration::from_millis(1),
|
|
retry_429: false,
|
|
retry_5xx: true,
|
|
retry_transport: true,
|
|
},
|
|
stream_idle_timeout: Duration::from_secs(1),
|
|
}
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn appends_client_version_query() {
|
|
let response = ModelsResponse { models: Vec::new() };
|
|
|
|
let transport = CapturingTransport {
|
|
last_request: Arc::new(Mutex::new(None)),
|
|
body: Arc::new(response),
|
|
etag: None,
|
|
};
|
|
|
|
let client = ModelsClient::new(
|
|
transport.clone(),
|
|
provider("https://example.com/api/codex"),
|
|
DummyAuth,
|
|
);
|
|
|
|
let (models, _) = client
|
|
.list_models("0.99.0", HeaderMap::new())
|
|
.await
|
|
.expect("request should succeed");
|
|
|
|
assert_eq!(models.len(), 0);
|
|
|
|
let url = transport
|
|
.last_request
|
|
.lock()
|
|
.unwrap()
|
|
.as_ref()
|
|
.unwrap()
|
|
.url
|
|
.clone();
|
|
assert_eq!(
|
|
url,
|
|
"https://example.com/api/codex/models?client_version=0.99.0"
|
|
);
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn parses_models_response() {
|
|
let response = ModelsResponse {
|
|
models: vec![
|
|
serde_json::from_value(json!({
|
|
"slug": "gpt-test",
|
|
"display_name": "gpt-test",
|
|
"description": "desc",
|
|
"default_reasoning_level": "medium",
|
|
"supported_reasoning_levels": [{"effort": "low", "description": "low"}, {"effort": "medium", "description": "medium"}, {"effort": "high", "description": "high"}],
|
|
"shell_type": "shell_command",
|
|
"visibility": "list",
|
|
"minimal_client_version": [0, 99, 0],
|
|
"supported_in_api": true,
|
|
"priority": 1,
|
|
"upgrade": null,
|
|
"base_instructions": "base instructions",
|
|
"supports_reasoning_summaries": false,
|
|
"support_verbosity": false,
|
|
"default_verbosity": null,
|
|
"apply_patch_tool_type": null,
|
|
"truncation_policy": {"mode": "bytes", "limit": 10_000},
|
|
"supports_parallel_tool_calls": false,
|
|
"supports_image_detail_original": false,
|
|
"context_window": 272_000,
|
|
"experimental_supported_tools": [],
|
|
}))
|
|
.unwrap(),
|
|
],
|
|
};
|
|
|
|
let transport = CapturingTransport {
|
|
last_request: Arc::new(Mutex::new(None)),
|
|
body: Arc::new(response),
|
|
etag: None,
|
|
};
|
|
|
|
let client = ModelsClient::new(
|
|
transport,
|
|
provider("https://example.com/api/codex"),
|
|
DummyAuth,
|
|
);
|
|
|
|
let (models, _) = client
|
|
.list_models("0.99.0", HeaderMap::new())
|
|
.await
|
|
.expect("request should succeed");
|
|
|
|
assert_eq!(models.len(), 1);
|
|
assert_eq!(models[0].slug, "gpt-test");
|
|
assert_eq!(models[0].supported_in_api, true);
|
|
assert_eq!(models[0].priority, 1);
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn list_models_includes_etag() {
|
|
let response = ModelsResponse { models: Vec::new() };
|
|
|
|
let transport = CapturingTransport {
|
|
last_request: Arc::new(Mutex::new(None)),
|
|
body: Arc::new(response),
|
|
etag: Some("\"abc\"".to_string()),
|
|
};
|
|
|
|
let client = ModelsClient::new(
|
|
transport,
|
|
provider("https://example.com/api/codex"),
|
|
DummyAuth,
|
|
);
|
|
|
|
let (models, etag) = client
|
|
.list_models("0.1.0", HeaderMap::new())
|
|
.await
|
|
.expect("request should succeed");
|
|
|
|
assert_eq!(models.len(), 0);
|
|
assert_eq!(etag, Some("\"abc\"".to_string()));
|
|
}
|
|
}
|