Skip to content

Use Claude Code with your ChatGPT, Kimi, Cursor or Grok subscription via a local Anthropic-compatible proxy

Rust

add codex transcription endpoint

Raine Virta4e2d7956a2e9
diff --git a/Cargo.lock b/Cargo.lock
index d5bf447..63ae366 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -1240,6 +1240,16 @@ version = "0.3.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6877bb514081ee2a7ff5ef9de3281f14a4dd4bceac4c09388074a6b5df8a139a"
+[[package]]
+name = "mime_guess"
+version = "2.0.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f7c44f8e672c00fe5308fa235f821cb4198414e1c77935c1ab6948d3fd78550e"
+dependencies = [
+ "mime",
+ "unicase",
+]
+
[[package]]
name = "miniz_oxide"
version = "0.8.9"
@@ -1689,6 +1699,7 @@ dependencies = [
"hyper-util",
"js-sys",
"log",
+ "mime_guess",
"percent-encoding",
"pin-project-lite",
"quinn",
@@ -2457,6 +2468,12 @@ version = "1.20.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b6f5e870be6c3b371b77fe0ee0bafb859fa4964b4404c27de1d380043c4dda20"
+[[package]]
+name = "unicase"
+version = "2.9.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "dbc4bc3a9f746d862c45cb89d705aa10f187bb96c76001afab07a0d35ce60142"
+
[[package]]
name = "unicode-ident"
version = "1.0.24"
diff --git a/Cargo.toml b/Cargo.toml
index 87a51db..d945cc5 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -26,7 +26,7 @@ tracing = "0.1"
tracing-subscriber = { version = "0.3", features = ["env-filter", "fmt", "json"] }
uuid = { version = "1", features = ["v4", "serde"] }
time = { version = "0.3", features = ["formatting", "parsing"] }
-reqwest = { version = "0.12", default-features = false, features = ["json", "stream", "rustls-tls", "blocking", "http2", "socks"] }
+reqwest = { version = "0.12", default-features = false, features = ["json", "stream", "multipart", "rustls-tls", "blocking", "http2", "socks"] }
base64 = "0.22"
rand = "0.8"
sha2 = "0.10"
diff --git a/src/config.rs b/src/config.rs
index f11a1f9..4ed20ab 100644
--- a/src/config.rs
+++ b/src/config.rs
@@ -64,6 +64,8 @@ struct CodexConfig {
pub images_api: Option<bool>,
#[serde(rename = "imagesBaseUrl")]
pub images_base_url: Option<String>,
+ #[serde(rename = "transcriptionsApi")]
+ pub transcriptions_api: Option<bool>,
#[serde(rename = "serviceTier")]
pub service_tier: Option<String>,
#[serde(rename = "reasoningSummary")]
@@ -255,6 +257,9 @@ pub fn config_override_summary_lines(cfg: &LoadedConfig) -> Vec<String> {
if env.contains_key("CCP_CODEX_IMAGES_BASE_URL") {
out.push("codex.imagesBaseUrl (env)".to_string());
}
+ if env.contains_key("CCP_CODEX_TRANSCRIPTIONS_API") {
+ out.push("codex.transcriptionsApi (env)".to_string());
+ }
if env.contains_key("CCP_KIMI_OAUTH_HOST") {
out.push("kimi.oauthHost (env)".to_string());
}
@@ -334,6 +339,9 @@ pub fn config_override_summary_lines(cfg: &LoadedConfig) -> Vec<String> {
if codex.images_base_url.is_some() {
out.push("codex.imagesBaseUrl (config)".to_string());
}
+ if codex.transcriptions_api == Some(true) {
+ out.push("codex.transcriptionsApi: true".to_string());
+ }
}
}
out
@@ -592,6 +600,21 @@ pub fn codex_images_api() -> bool {
false
}
+pub fn codex_transcriptions_api() -> bool {
+ let env: HashMap<_, _> = std::env::vars().collect();
+ if let Some(raw) = env.get("CCP_CODEX_TRANSCRIPTIONS_API") {
+ return matches!(raw.to_ascii_lowercase().as_str(), "1" | "true" | "yes");
+ }
+ let config_dir = paths::config_dir();
+ if let Some(file) = read_file_config(&config_dir)
+ && let Some(codex) = file.codex
+ && let Some(enabled) = codex.transcriptions_api
+ {
+ return enabled;
+ }
+ false
+}
+
pub fn codex_images_base_url() -> String {
let env: HashMap<_, _> = std::env::vars().collect();
if let Some(raw) = env.get("CCP_CODEX_IMAGES_BASE_URL") {
@@ -816,6 +839,7 @@ mod tests {
std::env::remove_var("CCP_CODEX_RESPONSES_API");
std::env::remove_var("CCP_CODEX_IMAGES_API");
std::env::remove_var("CCP_CODEX_IMAGES_BASE_URL");
+ std::env::remove_var("CCP_CODEX_TRANSCRIPTIONS_API");
std::env::remove_var("CCP_AUTO_REVIEW_MODEL");
}
}
@@ -1040,6 +1064,23 @@ mod tests {
);
}
+ #[test]
+ fn codex_transcriptions_api_defaults_to_disabled_and_env_overrides_config() {
+ let _guard = ENV_LOCK.lock().unwrap();
+ clear_env();
+ let config = tempfile::TempDir::new().unwrap();
+ std::fs::write(
+ config.path().join("config.json"),
+ r#"{"codex":{"transcriptionsApi":true}}"#,
+ )
+ .unwrap();
+ let _config_env = EnvGuard::set("CCP_CONFIG_DIR", config.path());
+
+ assert!(codex_transcriptions_api());
+ let _enabled_env = EnvGuard::set("CCP_CODEX_TRANSCRIPTIONS_API", "false");
+ assert!(!codex_transcriptions_api());
+ }
+
#[test]
fn codex_reasoning_summary_reads_config() {
let _guard = ENV_LOCK.lock().unwrap();
diff --git a/src/monitor.rs b/src/monitor.rs
index a433ac2..03aa642 100644
--- a/src/monitor.rs
+++ b/src/monitor.rs
@@ -19,6 +19,7 @@ pub enum EndpointKind {
Responses,
ChatCompletions,
Images,
+ Transcriptions,
}
impl EndpointKind {
@@ -29,6 +30,7 @@ impl EndpointKind {
Self::Responses => "responses",
Self::ChatCompletions => "chat_completions",
Self::Images => "images",
+ Self::Transcriptions => "transcriptions",
}
}
}
diff --git a/src/providers/codex/client.rs b/src/providers/codex/client.rs
index 1e16fd9..fcbfe33 100644
--- a/src/providers/codex/client.rs
+++ b/src/providers/codex/client.rs
@@ -245,6 +245,42 @@ pub fn build_codex_image_headers(
Ok(headers)
}
+pub fn build_codex_transcription_headers(
+ auth: &StoredAuth,
+ ctx: &RequestContext,
+) -> Result<http::HeaderMap, CodexError> {
+ let mut headers = http::HeaderMap::new();
+ headers.insert(
+ http::header::ACCEPT,
+ header_value("accept", "application/json")?,
+ );
+ headers.insert(
+ http::header::AUTHORIZATION,
+ header_value("authorization", &format!("Bearer {}", auth.access))?,
+ );
+ headers.insert("originator", header_value("originator", "Codex Desktop")?);
+ if let Some(account_id) = auth.account_id.as_deref() {
+ headers.insert(
+ "ChatGPT-Account-Id",
+ header_value("ChatGPT-Account-Id", account_id)?,
+ );
+ }
+ if let Some(session_id) = ctx.session_id.as_deref() {
+ headers.insert(
+ "x-client-request-id",
+ header_value("x-client-request-id", session_id)?,
+ );
+ }
+ let user_agent = config::codex_user_agent(&default_user_agent(false));
+ if !user_agent.is_empty() {
+ headers.insert(
+ http::header::USER_AGENT,
+ header_value("user-agent", &user_agent)?,
+ );
+ }
+ Ok(headers)
+}
+
fn header_value(name: &str, value: &str) -> Result<http::HeaderValue, CodexError> {
http::HeaderValue::from_str(value).map_err(|e| CodexError {
status: 500,
@@ -579,6 +615,91 @@ impl CodexHttpClient {
self.body_idle_timeout_ms
}
+ pub(crate) async fn post_transcription(
+ &self,
+ base_url: &str,
+ input: &super::transcription::PreparedTranscription,
+ ctx: &RequestContext,
+ ) -> Result<reqwest::Response, CodexError> {
+ let url = format!("{}/transcribe", base_url.trim_end_matches('/'));
+ let mut auth = self
+ .auth_manager
+ .get_auth()
+ .await
+ .map_err(|error| CodexError {
+ status: 401,
+ message: "Auth error".to_string(),
+ detail: Some(error.to_string()),
+ retry_after: None,
+ origin: CodexErrorOrigin::Auth,
+ })?;
+ let mut refresh_attempted = false;
+
+ loop {
+ let headers = build_codex_transcription_headers(&auth, ctx)?;
+ let response = self.attempt_transcription(&url, &headers, input).await?;
+ if response.status() == reqwest::StatusCode::UNAUTHORIZED && !refresh_attempted {
+ refresh_attempted = true;
+ drop(response);
+ auth = self
+ .auth_manager
+ .force_refresh(&auth.access)
+ .await
+ .map_err(auth_refresh_error)?;
+ continue;
+ }
+ return Ok(response);
+ }
+ }
+
+ async fn attempt_transcription(
+ &self,
+ url: &str,
+ headers: &http::HeaderMap,
+ input: &super::transcription::PreparedTranscription,
+ ) -> Result<reqwest::Response, CodexError> {
+ let part = reqwest::multipart::Part::bytes(input.audio.to_vec())
+ .file_name(input.filename.clone())
+ .mime_str(&input.content_type)
+ .map_err(|error| CodexError {
+ status: 400,
+ message: "Invalid audio content type".to_string(),
+ detail: Some(error.to_string()),
+ retry_after: None,
+ origin: CodexErrorOrigin::Http,
+ })?;
+ let mut form = reqwest::multipart::Form::new().part("file", part);
+ if let Some(language) = input.language.as_deref() {
+ form = form.text("language", language.to_string());
+ }
+ let mut request = self.native_client.post(url).multipart(form);
+ for (key, value) in headers {
+ request = request.header(key.as_str(), value.as_bytes());
+ }
+ tokio::time::timeout(
+ Duration::from_millis(IMAGE_HEADER_TIMEOUT_MS),
+ request.send(),
+ )
+ .await
+ .map_err(|_| CodexError {
+ status: 0,
+ message: format!(
+ "Timed out waiting {}ms for Codex transcription response headers",
+ IMAGE_HEADER_TIMEOUT_MS
+ ),
+ detail: None,
+ retry_after: None,
+ origin: CodexErrorOrigin::Http,
+ })?
+ .map_err(|error| CodexError {
+ status: 0,
+ message: format!("Codex transcription transport error: {error}"),
+ detail: None,
+ retry_after: None,
+ origin: CodexErrorOrigin::Http,
+ })
+ }
+
pub(crate) async fn post_image_json(
&self,
base_url: &str,
diff --git a/src/providers/codex/mod.rs b/src/providers/codex/mod.rs
index c3b1aba..0425761 100644
--- a/src/providers/codex/mod.rs
+++ b/src/providers/codex/mod.rs
@@ -9,6 +9,7 @@ pub mod images;
pub mod native;
pub mod request_summary;
pub mod search;
+pub mod transcription;
pub mod translate;
pub mod websocket;
diff --git a/src/providers/codex/transcription.rs b/src/providers/codex/transcription.rs
new file mode 100644
index 0000000..a2843a8
--- /dev/null
+++ b/src/providers/codex/transcription.rs
@@ -0,0 +1,502 @@
+use axum::response::{IntoResponse, Response};
+use bytes::Bytes;
+use http::StatusCode;
+use serde::Deserialize;
+
+pub const MAX_AUDIO_BYTES: usize = 25 * 1024 * 1024;
+pub const MAX_TRANSCRIPTION_REQUEST_BYTES: usize = MAX_AUDIO_BYTES + 1024 * 1024;
+const MAX_TRANSCRIPTION_RESPONSE_BYTES: usize = 1024 * 1024;
+const TRANSCRIPTION_BASE_URL: &str = "https://chatgpt.com/backend-api";
+
+#[derive(Debug)]
+pub struct TranscriptionRequestError {
+ pub status: StatusCode,
+ pub message: String,
+ pub param: Option<&'static str>,
+ pub code: &'static str,
+}
+
+impl TranscriptionRequestError {
+ pub fn invalid(message: impl Into<String>, param: Option<&'static str>) -> Self {
+ Self {
+ status: StatusCode::BAD_REQUEST,
+ message: message.into(),
+ param,
+ code: "invalid_request",
+ }
+ }
+
+ fn upstream(message: impl Into<String>) -> Self {
+ Self {
+ status: StatusCode::BAD_GATEWAY,
+ message: message.into(),
+ param: None,
+ code: "invalid_upstream_response",
+ }
+ }
+}
+
+#[derive(Debug, Clone)]
+pub struct PreparedTranscription {
+ pub audio: Bytes,
+ pub filename: String,
+ pub content_type: String,
+ pub language: Option<String>,
+}
+
+#[derive(Debug, Deserialize)]
+struct TranscriptionResponse<'a> {
+ #[serde(borrow)]
+ text: &'a str,
+}
+
+pub struct CodexTranscriptionBackend {
+ client: std::sync::Arc<super::client::CodexHttpClient>,
+ base_url: String,
+ limiter: std::sync::Arc<tokio::sync::Semaphore>,
+}
+
+impl CodexTranscriptionBackend {
+ pub fn new() -> Self {
+ Self {
+ client: std::sync::Arc::new(super::client::CodexHttpClient::new()),
+ base_url: TRANSCRIPTION_BASE_URL.to_string(),
+ limiter: std::sync::Arc::new(tokio::sync::Semaphore::new(4)),
+ }
+ }
+
+ #[cfg(test)]
+ fn new_for_test(
+ client: std::sync::Arc<super::client::CodexHttpClient>,
+ base_url: String,
+ ) -> Self {
+ Self {
+ client,
+ base_url: base_url.trim_end_matches('/').to_string(),
+ limiter: std::sync::Arc::new(tokio::sync::Semaphore::new(4)),
+ }
+ }
+
+ pub async fn handle(
+ &self,
+ input: PreparedTranscription,
+ ctx: crate::provider::RequestContext,
+ ) -> Response {
+ let _permit = match self.limiter.clone().try_acquire_owned() {
+ Ok(permit) => permit,
+ Err(_) => {
+ return transcription_error_response(TranscriptionRequestError {
+ status: StatusCode::TOO_MANY_REQUESTS,
+ message: "Too many concurrent transcription requests".to_string(),
+ param: None,
+ code: "local_capacity_exceeded",
+ });
+ }
+ };
+ if let Some(monitor) = ctx.monitor.as_ref() {
+ monitor.upstream_started(&ctx.req_id);
+ }
+ let upstream = match self
+ .client
+ .post_transcription(&self.base_url, &input, &ctx)
+ .await
+ {
+ Ok(response) => response,
+ Err(error) => return transcription_transport_error_response(error),
+ };
+ let status = upstream.status();
+ let headers = upstream.headers().clone();
+ if status.is_redirection() {
+ return transcription_error_response(TranscriptionRequestError::upstream(
+ "Codex transcription service returned an unexpected redirect",
+ ));
+ }
+ if !status.is_success() {
+ let mut response = transcription_error_response(TranscriptionRequestError {
+ status,
+ message: format!(
+ "Codex transcription service returned HTTP {}",
+ status.as_u16()
+ ),
+ param: None,
+ code: "upstream_error",
+ });
+ copy_safe_headers(&headers, response.headers_mut());
+ return response;
+ }
+ let body = match collect_response_body(upstream, self.client.body_idle_timeout_ms()).await {
+ Ok(body) => body,
+ Err(error) => return transcription_error_response(error),
+ };
+ if let Err(error) = validate_success_response(&body) {
+ return transcription_error_response(error);
+ }
+ if let Some(monitor) = ctx.monitor.as_ref() {
+ monitor.generation_started(&ctx.req_id);
+ }
+ let mut response = (
+ StatusCode::OK,
+ [(http::header::CONTENT_TYPE, "application/json")],
+ body,
+ )
+ .into_response();
+ response.headers_mut().insert(
+ http::header::CACHE_CONTROL,
+ http::HeaderValue::from_static("no-store"),
+ );
+ response.headers_mut().insert(
+ http::header::X_CONTENT_TYPE_OPTIONS,
+ http::HeaderValue::from_static("nosniff"),
+ );
+ copy_safe_headers(&headers, response.headers_mut());
+ response
+ }
+}
+
+impl Default for CodexTranscriptionBackend {
+ fn default() -> Self {
+ Self::new()
+ }
+}
+
+pub fn prepare_transcription(
+ audio: Option<Bytes>,
+ filename: Option<String>,
+ content_type: Option<String>,
+ language: Option<String>,
+) -> Result<PreparedTranscription, TranscriptionRequestError> {
+ let audio = audio.ok_or_else(|| {
+ TranscriptionRequestError::invalid("Missing required 'file' field", Some("file"))
+ })?;
+ if audio.is_empty() {
+ return Err(TranscriptionRequestError::invalid(
+ "Uploaded audio file is empty",
+ Some("file"),
+ ));
+ }
+ if audio.len() > MAX_AUDIO_BYTES {
+ return Err(TranscriptionRequestError {
+ status: StatusCode::PAYLOAD_TOO_LARGE,
+ message: format!("Audio file must be at most {MAX_AUDIO_BYTES} bytes"),
+ param: Some("file"),
+ code: "request_too_large",
+ });
+ }
+ let content_type = content_type.unwrap_or_else(|| "application/octet-stream".to_string());
+ if !supported_audio_content_type(&content_type) {
+ return Err(TranscriptionRequestError::invalid(
+ format!("Unsupported audio content type '{content_type}'"),
+ Some("file"),
+ ));
+ }
+ let language = language
+ .map(|value| value.trim().to_string())
+ .filter(|value| !value.is_empty());
+ if language.as_ref().is_some_and(|value| value.len() > 64) {
+ return Err(TranscriptionRequestError::invalid(
+ "'language' must be at most 64 characters",
+ Some("language"),
+ ));
+ }
+ Ok(PreparedTranscription {
+ audio,
+ filename: safe_filename(filename.as_deref().unwrap_or("audio.webm")),
+ content_type,
+ language,
+ })
+}
+
+fn safe_filename(filename: &str) -> String {
+ std::path::Path::new(filename)
+ .file_name()
+ .and_then(|name| name.to_str())
+ .map(|name| name.replace(['\r', '\n', '"'], ""))
+ .filter(|name| !name.is_empty())
+ .unwrap_or_else(|| "audio.webm".to_string())
+}
+
+fn supported_audio_content_type(content_type: &str) -> bool {
+ matches!(
+ content_type
+ .split(';')
+ .next()
+ .unwrap_or_default()
+ .trim()
+ .to_ascii_lowercase()
+ .as_str(),
+ "audio/flac"
+ | "audio/m4a"
+ | "audio/mp3"
+ | "audio/mp4"
+ | "audio/mpeg"
+ | "audio/ogg"
+ | "audio/wav"
+ | "audio/wave"
+ | "audio/webm"
+ | "audio/x-m4a"
+ | "audio/x-wav"
+ )
+}
+
+async fn collect_response_body(
+ mut response: reqwest::Response,
+ idle_timeout_ms: u64,
+) -> Result<Vec<u8>, TranscriptionRequestError> {
+ if response
+ .content_length()
+ .is_some_and(|length| length > MAX_TRANSCRIPTION_RESPONSE_BYTES as u64)
+ {
+ return Err(TranscriptionRequestError::upstream(
+ "Codex transcription response exceeded the size limit",
+ ));
+ }
+ let mut body = Vec::new();
+ loop {
+ let chunk = tokio::time::timeout(
+ std::time::Duration::from_millis(idle_timeout_ms),
+ response.chunk(),
+ )
+ .await
+ .map_err(|_| {
+ TranscriptionRequestError::upstream("Timed out reading Codex transcription response")
+ })?
+ .map_err(|_| {
+ TranscriptionRequestError::upstream("Failed to read Codex transcription response")
+ })?;
+ let Some(chunk) = chunk else {
+ break;
+ };
+ if body.len().saturating_add(chunk.len()) > MAX_TRANSCRIPTION_RESPONSE_BYTES {
+ return Err(TranscriptionRequestError::upstream(
+ "Codex transcription response exceeded the size limit",
+ ));
+ }
+ body.extend_from_slice(&chunk);
+ }
+ Ok(body)
+}
+
+fn validate_success_response(body: &[u8]) -> Result<(), TranscriptionRequestError> {
+ let response: TranscriptionResponse<'_> = serde_json::from_slice(body).map_err(|_| {
+ TranscriptionRequestError::upstream("Codex transcription service returned invalid JSON")
+ })?;
+ if response.text.trim().is_empty() {
+ return Err(TranscriptionRequestError::upstream(
+ "Codex transcription service returned no text",
+ ));
+ }
+ Ok(())
+}
+
+fn copy_safe_headers(source: &http::HeaderMap, target: &mut http::HeaderMap) {
+ for name in ["retry-after", "x-request-id", "openai-processing-ms"] {
+ if let Some(value) = source.get(name) {
+ target.insert(http::HeaderName::from_static(name), value.clone());
+ }
+ }
+}
+
+fn transcription_transport_error_response(error: super::client::CodexError) -> Response {
+ let status = match error.status {
+ 401 => StatusCode::UNAUTHORIZED,
+ 403 => StatusCode::FORBIDDEN,
+ 429 => StatusCode::TOO_MANY_REQUESTS,
+ value if (400..=599).contains(&value) => {
+ StatusCode::from_u16(value).unwrap_or(StatusCode::BAD_GATEWAY)
+ }
+ _ => StatusCode::BAD_GATEWAY,
+ };
+ let mut response = transcription_error_response(TranscriptionRequestError {
+ status,
+ message: if error.status == 0 {
+ "Codex transcription service is unavailable".to_string()
+ } else {
+ format!("Codex transcription service returned HTTP {}", error.status)
+ },
+ param: None,
+ code: if status == StatusCode::UNAUTHORIZED {
+ "authentication_error"
+ } else if status == StatusCode::FORBIDDEN {
+ "permission_error"
+ } else if status == StatusCode::TOO_MANY_REQUESTS {
+ "rate_limit_error"
+ } else {
+ "upstream_error"
+ },
+ });
+ if let Some(retry_after) = error.retry_after
+ && let Ok(value) = http::HeaderValue::from_str(&retry_after)
+ {
+ response
+ .headers_mut()
+ .insert(http::header::RETRY_AFTER, value);
+ }
+ response
+}
+
+pub fn transcription_error_response(error: TranscriptionRequestError) -> Response {
+ let error_type = match error.status {
+ StatusCode::UNAUTHORIZED => "authentication_error",
+ StatusCode::FORBIDDEN => "permission_error",
+ StatusCode::TOO_MANY_REQUESTS => "rate_limit_error",
+ status if status.is_client_error() => "invalid_request_error",
+ _ => "api_error",
+ };
+ (
+ error.status,
+ [
+ (http::header::CONTENT_TYPE, "application/json"),
+ (http::header::CACHE_CONTROL, "no-store"),
+ ],
+ axum::Json(serde_json::json!({
+ "error": {
+ "message": error.message,
+ "type": error_type,
+ "param": error.param,
+ "code": error.code,
+ }
+ })),
+ )
+ .into_response()
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ fn context() -> crate::provider::RequestContext {
+ crate::provider::RequestContext {
+ req_id: "transcription-test".to_string(),
+ session_id: None,
+ session_seq: None,
+ provider: "codex".to_string(),
+ traffic: None,
+ monitor: None,
+ }
+ }
+
+ #[test]
+ fn validates_audio_and_normalizes_filename() {
+ let prepared = prepare_transcription(
+ Some(Bytes::from_static(b"audio")),
+ Some("../recording.webm".to_string()),
+ Some("audio/webm;codecs=opus".to_string()),
+ Some(" en ".to_string()),
+ )
+ .unwrap();
+ assert_eq!(prepared.filename, "recording.webm");
+ assert_eq!(prepared.language.as_deref(), Some("en"));
+
+ assert!(prepare_transcription(None, None, None, None).is_err());
+ assert!(
+ prepare_transcription(
+ Some(Bytes::from_static(b"audio")),
+ None,
+ Some("text/plain".to_string()),
+ None,
+ )
+ .is_err()
+ );
+ }
+
+ #[tokio::test]
+ async fn backend_forwards_multipart_with_codex_auth() {
+ use tokio::io::{AsyncReadExt, AsyncWriteExt};
+ use tokio::net::TcpListener;
+
+ let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
+ let addr = listener.local_addr().unwrap();
+ let server = tokio::spawn(async move {
+ let (mut stream, _) = listener.accept().await.unwrap();
+ let mut request = Vec::new();
+ let mut chunk = [0_u8; 4096];
+ loop {
+ let read = stream.read(&mut chunk).await.unwrap();
+ assert!(read > 0);
+ request.extend_from_slice(&chunk[..read]);
+ let Some(header_end) = request.windows(4).position(|part| part == b"\r\n\r\n")
+ else {
+ continue;
+ };
+ let headers = String::from_utf8_lossy(&request[..header_end]);
+ let content_length = headers
+ .lines()
+ .find_map(|line| {
+ let (name, value) = line.split_once(':')?;
+ name.eq_ignore_ascii_case("content-length")
+ .then(|| value.trim().parse::<usize>().ok())
+ .flatten()
+ })
+ .unwrap();
+ if request.len() >= header_end + 4 + content_length {
+ break;
+ }
+ }
+ let request_text = String::from_utf8_lossy(&request);
+ assert!(request_text.starts_with("POST /root/transcribe HTTP/1.1"));
+ assert!(request_text.contains("authorization: Bearer test"));
+ assert!(request_text.contains("chatgpt-account-id: acct"));
+ assert!(request_text.contains("originator: Codex Desktop"));
+ assert!(request_text.contains("name=\"file\""));
+ assert!(request_text.contains("filename=\"recording.webm\""));
+ assert!(request_text.contains("name=\"language\""));
+ assert!(request_text.contains("\r\nen\r\n"));
+ assert!(request_text.contains("audio-bytes"));
+
+ let body = br#"{"text":"hello world"}"#;
+ let head = format!(
+ "HTTP/1.1 200 OK\r\ncontent-type: application/json\r\nx-request-id: upstream-transcribe\r\ncontent-length: {}\r\nconnection: close\r\n\r\n",
+ body.len()
+ );
+ stream.write_all(head.as_bytes()).await.unwrap();
+ stream.write_all(body).await.unwrap();
+ });
+
+ let client = super::super::client::CodexHttpClient::new_for_test(
+ reqwest::Client::builder().no_proxy().build().unwrap(),
+ format!("http://{addr}/responses"),
+ 1_000,
+ 1_000,
+ 0,
+ );
+ client
+ .auth_manager()
+ .set_test_auth(super::super::auth::token_store::StoredAuth {
+ access: "test".into(),
+ refresh: String::new(),
+ account_id: Some("acct".into()),
+ expires: u64::MAX,
+ });
+ let backend = CodexTranscriptionBackend::new_for_test(
+ std::sync::Arc::new(client),
+ format!("http://{addr}/root"),
+ );
+ let response = backend
+ .handle(
+ prepare_transcription(
+ Some(Bytes::from_static(b"audio-bytes")),
+ Some("recording.webm".to_string()),
+ Some("audio/webm".to_string()),
+ Some("en".to_string()),
+ )
+ .unwrap(),
+ context(),
+ )
+ .await;
+ server.await.unwrap();
+
+ assert_eq!(response.status(), StatusCode::OK);
+ assert_eq!(response.headers()["x-request-id"], "upstream-transcribe");
+ let body = axum::body::to_bytes(response.into_body(), 1024)
+ .await
+ .unwrap();
+ assert_eq!(body, br#"{"text":"hello world"}"#.as_slice());
+ }
+
+ #[test]
+ fn validates_success_payload() {
+ assert!(validate_success_response(br#"{"text":"hello"}"#).is_ok());
+ assert!(validate_success_response(br#"{"text":""}"#).is_err());
+ assert!(validate_success_response(br#"{}"#).is_err());
+ }
+}
diff --git a/src/server.rs b/src/server.rs
index 545ca0a..c31894b 100644
--- a/src/server.rs
+++ b/src/server.rs
@@ -19,6 +19,10 @@ use crate::{
native::{
CodexNativeBackend, NativeResponseOutcome, openai_error, validate_native_request_model,
},
+ transcription::{
+ CodexTranscriptionBackend, MAX_TRANSCRIPTION_REQUEST_BYTES, TranscriptionRequestError,
+ prepare_transcription, transcription_error_response,
+ },
},
registry::{Registry, normalize_incoming_model},
session::{self, SessionState},
@@ -176,6 +180,7 @@ pub fn app(registry: Arc<Registry>) -> Router {
AppFeatures {
responses_api: crate::config::codex_responses_api(),
images_api: crate::config::codex_images_api(),
+ transcriptions_api: crate::config::codex_transcriptions_api(),
},
)
}
@@ -187,6 +192,7 @@ pub fn app_with_monitor(registry: Arc<Registry>, monitor: Option<MonitorHandle>)
AppFeatures {
responses_api: crate::config::codex_responses_api(),
images_api: crate::config::codex_images_api(),
+ transcriptions_api: crate::config::codex_transcriptions_api(),
},
)
}
@@ -195,6 +201,7 @@ pub fn app_with_monitor(registry: Arc<Registry>, monitor: Option<MonitorHandle>)
pub struct AppFeatures {
pub responses_api: bool,
pub images_api: bool,
+ pub transcriptions_api: bool,
}
pub fn app_with_options(
@@ -208,6 +215,7 @@ pub fn app_with_options(
AppFeatures {
responses_api,
images_api: false,
+ transcriptions_api: false,
},
)
}
@@ -237,12 +245,16 @@ pub fn app_with_features(
} else {
None
};
+ let transcriptions = features
+ .transcriptions_api
+ .then(|| Arc::new(CodexTranscriptionBackend::new()));
let state = Arc::new(AppState {
registry,
monitor,
native_responses,
chat_completions,
images,
+ transcriptions,
});
let router = Router::new()
.route("/healthz", get(healthz))
@@ -266,6 +278,15 @@ pub fn app_with_features(
} else {
router
};
+ let router = if features.transcriptions_api {
+ router.route(
+ "/v1/audio/transcriptions",
+ post(handler_transcription)
+ .layer(DefaultBodyLimit::max(MAX_TRANSCRIPTION_REQUEST_BYTES)),
+ )
+ } else {
+ router
+ };
router.fallback(fallback_handler).with_state(state)
}
@@ -276,6 +297,7 @@ struct AppState {
native_responses: Option<Arc<CodexNativeBackend>>,
chat_completions: Option<Arc<ChatCompletionsBackend>>,
images: Option<Arc<CodexImagesBackend>>,
+ transcriptions: Option<Arc<CodexTranscriptionBackend>>,
}
async fn healthz() -> Json<serde_json::Value> {
@@ -330,6 +352,218 @@ async fn handler_count_tokens(State(state): State<Arc<AppState>>, req: Request<B
dispatch_request(state, req, true).await
}
+async fn handler_transcription(State(state): State<Arc<AppState>>, req: Request<Body>) -> Response {
+ let started_at = Instant::now();
+ let log = create_logger("server");
+ let req_id = Uuid::new_v4().to_string();
+ let headers = req.headers().clone();
+ log.info(
+ "request",
+ Some(Map::from_iter([
+ ("reqId".to_string(), json!(&req_id)),
+ ("method".to_string(), json!("POST")),
+ ("path".to_string(), json!("/v1/audio/transcriptions")),
+ ("query".to_string(), json!({})),
+ ])),
+ );
+ let session_id = native_session_id(&headers);
+ if let Some(monitor) = state.monitor.as_ref() {
+ monitor.request_started(
+ &req_id,
+ session_id.clone(),
+ None,
+ EndpointKind::Transcriptions,
+ );
+ monitor.provider_selected(&req_id, "codex", "codex-transcribe", None);
+ }
+ let request_guard = RequestMonitorGuard::new(state.monitor.clone(), req_id.clone());
+ if req.uri().query().is_some() {
+ let response = transcription_error_response(TranscriptionRequestError::invalid(
+ "Transcription endpoint does not accept query parameters",
+ None,
+ ));
+ log_native_request_completed(
+ &log,
+ &req_id,
+ "transcriptions",
+ Some("codex-transcribe"),
+ response.status(),
+ started_at,
+ );
+ return monitor_response_body(response, request_guard);
+ }
+ let content_type = headers
+ .get(http::header::CONTENT_TYPE)
+ .and_then(|value| value.to_str().ok())
+ .unwrap_or_default()
+ .to_ascii_lowercase();
+ if !content_type.starts_with("multipart/form-data") {
+ let response = transcription_error_response(TranscriptionRequestError {
+ status: StatusCode::UNSUPPORTED_MEDIA_TYPE,
+ message: "Transcription request must use multipart/form-data".to_string(),
+ param: None,
+ code: "unsupported_media_type",
+ });
+ log_native_request_completed(
+ &log,
+ &req_id,
+ "transcriptions",
+ Some("codex-transcribe"),
+ response.status(),
+ started_at,
+ );
+ return monitor_response_body(response, request_guard);
+ }
+ let mut multipart = match Multipart::from_request(req, &()).await {
+ Ok(multipart) => multipart,
+ Err(error) => {
+ let status = axum::response::IntoResponse::into_response(error).status();
+ let response = transcription_error_response(TranscriptionRequestError {
+ status: if status == StatusCode::PAYLOAD_TOO_LARGE {
+ StatusCode::PAYLOAD_TOO_LARGE
+ } else {
+ StatusCode::BAD_REQUEST
+ },
+ message: if status == StatusCode::PAYLOAD_TOO_LARGE {
+ "Transcription request exceeded the size limit".to_string()
+ } else {
+ "Invalid multipart transcription request".to_string()
+ },
+ param: None,
+ code: if status == StatusCode::PAYLOAD_TOO_LARGE {
+ "request_too_large"
+ } else {
+ "invalid_multipart"
+ },
+ });
+ log_native_request_completed(
+ &log,
+ &req_id,
+ "transcriptions",
+ Some("codex-transcribe"),
+ response.status(),
+ started_at,
+ );
+ return monitor_response_body(response, request_guard);
+ }
+ };
+ let mut audio = None;
+ let mut filename = None;
+ let mut audio_content_type = None;
+ let mut language = None;
+ while let Some(field) = match multipart.next_field().await {
+ Ok(field) => field,
+ Err(_) => {
+ let response = transcription_error_response(TranscriptionRequestError::invalid(
+ "Invalid multipart transcription request",
+ None,
+ ));
+ return monitor_response_body(response, request_guard);
+ }
+ } {
+ let name = field.name().unwrap_or_default().to_string();
+ match name.as_str() {
+ "file" => {
+ if audio.is_some() {
+ let response =
+ transcription_error_response(TranscriptionRequestError::invalid(
+ "Transcription request must contain one 'file' field",
+ Some("file"),
+ ));
+ return monitor_response_body(response, request_guard);
+ }
+ filename = field.file_name().map(str::to_string);
+ audio_content_type = field.content_type().map(str::to_string);
+ audio = match field.bytes().await {
+ Ok(bytes) => Some(bytes),
+ Err(_) => {
+ let response =
+ transcription_error_response(TranscriptionRequestError::invalid(
+ "Failed to read uploaded audio",
+ Some("file"),
+ ));
+ return monitor_response_body(response, request_guard);
+ }
+ };
+ }
+ "language" => {
+ language = match multipart_text(field, "language").await {
+ Ok(value) => Some(value),
+ Err(error) => {
+ return monitor_response_body(
+ transcription_error_response(TranscriptionRequestError {
+ status: error.status,
+ message: error.message,
+ param: error.param,
+ code: error.code.unwrap_or("invalid_multipart"),
+ }),
+ request_guard,
+ );
+ }
+ };
+ }
+ "model" => {
+ if multipart_text(field, "model").await.is_err() {
+ let response =
+ transcription_error_response(TranscriptionRequestError::invalid(
+ "Invalid multipart 'model' field",
+ Some("model"),
+ ));
+ return monitor_response_body(response, request_guard);
+ }
+ }
+ _ => {
+ let response = transcription_error_response(TranscriptionRequestError::invalid(
+ format!("Unsupported multipart field '{name}'"),
+ None,
+ ));
+ return monitor_response_body(response, request_guard);
+ }
+ }
+ }
+ let prepared = match prepare_transcription(audio, filename, audio_content_type, language) {
+ Ok(prepared) => prepared,
+ Err(error) => {
+ let response = transcription_error_response(error);
+ log_native_request_completed(
+ &log,
+ &req_id,
+ "transcriptions",
+ Some("codex-transcribe"),
+ response.status(),
+ started_at,
+ );
+ return monitor_response_body(response, request_guard);
+ }
+ };
+ let context = RequestContext {
+ req_id: req_id.clone(),
+ session_id,
+ session_seq: None,
+ provider: "codex".to_string(),
+ traffic: None,
+ monitor: state.monitor.clone(),
+ };
+ let response = match state.transcriptions.as_ref() {
+ Some(backend) => backend.handle(prepared, context).await,
+ None => transcription_error_response(TranscriptionRequestError {
+ status: StatusCode::SERVICE_UNAVAILABLE,
+ message: "Codex transcription API is unavailable".to_string(),
+ param: None,
+ code: "transcriptions_api_unavailable",
+ }),
+ };
+ log_native_request_completed(
+ &log,
+ &req_id,
+ "transcriptions",
+ Some("codex-transcribe"),
+ response.status(),
+ started_at,
+ );
+ monitor_response_body(response, request_guard)
+}
+
async fn handler_image_generation(
State(state): State<Arc<AppState>>,
req: Request<Body>,
diff --git a/tests/server.rs b/tests/server.rs
index 0bdad3f..9087aff 100644
--- a/tests/server.rs
+++ b/tests/server.rs
@@ -358,6 +358,7 @@ async fn image_routes_reject_variations_wrong_media_and_oversized_generation() {
let features = AppFeatures {
responses_api: false,
images_api: true,
+ transcriptions_api: false,
};
let variation = app_with_features(Arc::new(Registry::with_default_alias()), None, features)
.oneshot(
@@ -407,6 +408,7 @@ async fn monitor_tracks_image_endpoint_without_session_affinity() {
AppFeatures {
responses_api: false,
images_api: true,
+ transcriptions_api: false,
},
);
let response = app
@@ -441,6 +443,7 @@ async fn image_edit_accepts_multipart_and_validates_fields() {
AppFeatures {
responses_api: false,
images_api: true,
+ transcriptions_api: false,
},
);
let boundary = "ccp-image-test";
@@ -486,6 +489,7 @@ async fn image_routes_are_independently_opt_in() {
AppFeatures {
responses_api: false,
images_api: false,
+ transcriptions_api: false,
},
);
let response = disabled
@@ -507,6 +511,7 @@ async fn image_routes_are_independently_opt_in() {
AppFeatures {
responses_api: false,
images_api: true,
+ transcriptions_api: false,
},
);
let response = enabled
@@ -523,6 +528,98 @@ async fn image_routes_are_independently_opt_in() {
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
}
+#[tokio::test]
+async fn transcription_route_is_independently_opt_in_and_validates_multipart() {
+ let disabled = app_with_features(
+ Arc::new(Registry::with_default_alias()),
+ None,
+ AppFeatures {
+ responses_api: false,
+ images_api: false,
+ transcriptions_api: false,
+ },
+ );
+ let response = disabled
+ .oneshot(
+ Request::builder()
+ .method(Method::POST)
+ .uri("/v1/audio/transcriptions")
+ .body(Body::empty())
+ .unwrap(),
+ )
+ .await
+ .unwrap();
+ assert_eq!(response.status(), StatusCode::NOT_FOUND);
+
+ let enabled = app_with_features(
+ Arc::new(Registry::with_default_alias()),
+ None,
+ AppFeatures {
+ responses_api: false,
+ images_api: false,
+ transcriptions_api: true,
+ },
+ );
+ let boundary = "ccp-transcription-test";
+ let body = format!(
+ "--{boundary}\r\nContent-Disposition: form-data; name=\"model\"\r\n\r\ngpt-4o-mini-transcribe\r\n--{boundary}--\r\n"
+ );
+ let response = enabled
+ .oneshot(
+ Request::builder()
+ .method(Method::POST)
+ .uri("/v1/audio/transcriptions")
+ .header(
+ "content-type",
+ format!("multipart/form-data; boundary={boundary}"),
+ )
+ .body(Body::from(body))
+ .unwrap(),
+ )
+ .await
+ .unwrap();
+ assert_eq!(response.status(), StatusCode::BAD_REQUEST);
+ let body: Value = serde_json::from_slice(
+ &axum::body::to_bytes(response.into_body(), usize::MAX)
+ .await
+ .unwrap(),
+ )
+ .unwrap();
+ assert_eq!(body["error"]["param"], "file");
+}
+
+#[tokio::test]
+async fn transcription_route_rejects_non_audio_uploads() {
+ let app = app_with_features(
+ Arc::new(Registry::with_default_alias()),
+ None,
+ AppFeatures {
+ responses_api: false,
+ images_api: false,
+ transcriptions_api: true,
+ },
+ );
+ let boundary = "ccp-transcription-type-test";
+ let body = format!(
+ "--{boundary}\r\nContent-Disposition: form-data; name=\"file\"; filename=\"notes.txt\"\r\nContent-Type: text/plain\r\n\r\nnot audio\r\n--{boundary}--\r\n"
+ );
+ let response = app
+ .oneshot(
+ Request::builder()
+ .method(Method::POST)
+ .uri("/v1/audio/transcriptions")
+ .header(
+ "content-type",
+ format!("multipart/form-data; boundary={boundary}"),
+ )
+ .body(Body::from(body))
+ .unwrap(),
+ )
+ .await
+ .unwrap();
+ assert_eq!(response.status(), StatusCode::BAD_REQUEST);
+}
+
#[tokio::test]
async fn native_responses_route_is_disabled_by_default_option() {
let app = app_with_options(Arc::new(Registry::with_default_alias()), None, false);