feat: convert Codex Chat error responses to Responses envelope

The Codex Chat-to-Responses bridge already rewrites successful upstream
responses to the Responses shape, but the error branch in
handle_codex_chat_to_responses_transform passed Chat-shaped error bodies
through untouched (MiniMax base_resp, raw OpenAI Chat error, text/plain
"Unauthorized" pages, etc.), leaving Codex clients unable to recognize the
error.

Add chat_error_to_response_error in transform_codex_chat to regularize all
upstream error shapes into the standard {error: {message, type, code, param}}
envelope, then wire it through a new handle_codex_chat_error_response that
preserves the original HTTP status code. Non-JSON error bodies (HTML, plain
text) are wrapped as Value::String and truncated to 1KB at a UTF-8 char
boundary to keep diagnostic context without flooding the response.

Also fix a pre-existing append-vs-insert pitfall in three rebuilt-body
branches (Claude transform, Codex Chat normal, Codex Chat error): http
Builder::header is append, so leaking the upstream Content-Type produced
two Content-Type headers when the rewritten body was JSON. Remove the
upstream value before writing application/json.
This commit is contained in:
Jason
2026-05-21 09:15:05 +08:00
parent 72bc912e0d
commit ead9e22b21
2 changed files with 234 additions and 4 deletions
+84 -4
View File
@@ -441,12 +441,17 @@ async fn handle_claude_transform(
let mut builder = axum::response::Response::builder().status(status);
strip_entity_headers_for_rebuilt_body(&mut response_headers);
strip_hop_by_hop_response_headers(&mut response_headers);
// Builder::header 是 append 语义;不先 remove 会和上游 Content-Type 双发。
response_headers.remove(axum::http::header::CONTENT_TYPE);
for (key, value) in response_headers.iter() {
builder = builder.header(key, value);
}
builder = builder.header("content-type", "application/json");
builder = builder.header(
axum::http::header::CONTENT_TYPE,
axum::http::HeaderValue::from_static("application/json"),
);
let response_body = serde_json::to_vec(&anthropic_response).map_err(|e| {
log::error!("[Claude] 序列化响应失败: {e}");
@@ -695,8 +700,10 @@ async fn handle_codex_chat_to_responses_transform(
let status = response.status();
if !status.is_success() {
return process_response(response, ctx, state, &CODEX_PARSER_CONFIG, connection_guard)
.await;
// 上游 Chat 错误体形状与 Responses 不一致(如 MiniMax 的 base_resp、自定义 detail 字段);
// 直接透传会让 Codex 客户端无法识别错误码。这里统一转换为 Responses 风格
// `{"error": {message, type, code, param}}`,保留原始 HTTP 状态码。
return handle_codex_chat_error_response(response, ctx, status).await;
}
if is_stream || response.is_sse() {
@@ -826,12 +833,17 @@ async fn handle_codex_chat_to_responses_transform(
strip_entity_headers_for_rebuilt_body(&mut response_headers);
strip_hop_by_hop_response_headers(&mut response_headers);
// Builder::header 是 append 语义;不先 remove 会和上游 Content-Type 双发。
response_headers.remove(axum::http::header::CONTENT_TYPE);
let mut builder = axum::response::Response::builder().status(status);
for (key, value) in response_headers.iter() {
builder = builder.header(key, value);
}
builder = builder.header("content-type", "application/json");
builder = builder.header(
axum::http::header::CONTENT_TYPE,
axum::http::HeaderValue::from_static("application/json"),
);
let response_body = serde_json::to_vec(&responses_response).map_err(|e| {
log::error!("[Codex] 序列化 Responses 响应失败: {e}");
@@ -846,6 +858,74 @@ async fn handle_codex_chat_to_responses_transform(
})
}
/// 把上游 Chat Completions 的错误响应转换为 Responses API 错误形状。
///
/// 与正常响应分支配套:正常响应已经被改写成 Responses 形式,错误响应若仍保留
/// Chat 错误体(如 MiniMax 的 `{"base_resp": {"status_code": 2013}}`),Codex
/// 客户端的错误处理就无法对齐字段。这里读取上游 body、规整成
/// `{"error": {message, type, code, param}}` 并保留原始 HTTP 状态码。
async fn handle_codex_chat_error_response(
response: super::hyper_client::ProxyResponse,
ctx: &RequestContext,
status: axum::http::StatusCode,
) -> Result<axum::response::Response, ProxyError> {
let body_timeout =
if ctx.app_config.auto_failover_enabled && ctx.app_config.non_streaming_timeout > 0 {
std::time::Duration::from_secs(ctx.app_config.non_streaming_timeout as u64)
} else {
std::time::Duration::ZERO
};
let (mut response_headers, _status, body_bytes) =
read_decoded_body(response, ctx.tag, body_timeout).await?;
// 非 JSON 上游错误体(Cloudflare HTML、纯文本 "Unauthorized" 等)若丢成 None
// 客户端就看不到原始诊断信息;包成 Value::String 走转换函数的字符串分支。
let parsed_value: Value = match serde_json::from_slice::<Value>(&body_bytes) {
Ok(value) => value,
Err(_) => {
const MAX_RAW_ERROR_BYTES: usize = 1024;
let lossy = String::from_utf8_lossy(&body_bytes);
let truncated = if lossy.len() > MAX_RAW_ERROR_BYTES {
let mut end = MAX_RAW_ERROR_BYTES;
while end > 0 && !lossy.is_char_boundary(end) {
end -= 1;
}
format!("{}…(truncated)", &lossy[..end])
} else {
lossy.into_owned()
};
log::warn!("[Codex] Chat 错误响应不是合法 JSON,按文本透传: {truncated}");
Value::String(truncated)
}
};
let responses_error = transform_codex_chat::chat_error_to_response_error(Some(&parsed_value));
strip_entity_headers_for_rebuilt_body(&mut response_headers);
strip_hop_by_hop_response_headers(&mut response_headers);
// Builder::header 是 append 语义;不先 remove 会和上游 Content-Type 双发。
response_headers.remove(axum::http::header::CONTENT_TYPE);
let mut builder = axum::response::Response::builder().status(status);
for (key, value) in response_headers.iter() {
builder = builder.header(key, value);
}
builder = builder.header(
axum::http::header::CONTENT_TYPE,
axum::http::HeaderValue::from_static("application/json"),
);
let body = serde_json::to_vec(&responses_error).map_err(|e| {
log::error!("[Codex] 序列化 Responses 错误体失败: {e}");
ProxyError::TransformError(format!("Failed to serialize responses error: {e}"))
})?;
builder.body(axum::body::Body::from(body)).map_err(|e| {
log::error!("[Codex] 构建 Responses 错误响应失败: {e}");
ProxyError::Internal(format!("Failed to build response: {e}"))
})
}
// ============================================================================
// Gemini API 处理器
// ============================================================================