mirror of
https://github.com/zyycn/codex-proxy-rs.git
synced 2026-11-17 00:06:34 +00:00
fix(openai): normalize unsupported Codex OAuth request options (#630)
Co-authored-by: Zyy <zyycn@users.noreply.github.com> Co-authored-by: Codex <noreply@openai.com>
This commit is contained in:
@@ -5,6 +5,7 @@ use serde_json::{Map, Value, json};
|
||||
/// 非官方客户端及跨客户端历史回填的兼容入口;调用方必须已选定 Codex/OAuth 上游
|
||||
/// 只处理已验证被拒绝的字段,不根据客户端品牌推断整份历史是否合法
|
||||
pub(super) fn normalize_non_codex_request_body(body: &mut Map<String, Value>) {
|
||||
normalize_codex_oauth_options(body);
|
||||
let Some(input) = body.get_mut("input").and_then(Value::as_array_mut) else {
|
||||
return;
|
||||
};
|
||||
@@ -33,6 +34,55 @@ pub(super) fn normalize_non_codex_request_body(body: &mut Map<String, Value>) {
|
||||
}
|
||||
}
|
||||
|
||||
fn normalize_codex_oauth_options(body: &mut Map<String, Value>) {
|
||||
// Codex OAuth 拒绝这些客户端选项,即使采样参数取默认值
|
||||
for field in [
|
||||
"top_p",
|
||||
"frequency_penalty",
|
||||
"presence_penalty",
|
||||
"chat_template_kwargs",
|
||||
"user",
|
||||
"metadata",
|
||||
"safety_identifier",
|
||||
] {
|
||||
body.shift_remove(field);
|
||||
}
|
||||
// Responses 自带用量;保留官方流式推理摘要等其他选项
|
||||
if let Some(options) = body
|
||||
.get_mut("stream_options")
|
||||
.and_then(Value::as_object_mut)
|
||||
{
|
||||
options.shift_remove("include_usage");
|
||||
}
|
||||
// 只省略没有实际效果的配置,不静默取消客户端要求的截断、停止条件或缓存策略
|
||||
if body.get("truncation").and_then(Value::as_str) == Some("disabled") {
|
||||
body.shift_remove("truncation");
|
||||
}
|
||||
if body
|
||||
.get("stop_sequences")
|
||||
.and_then(Value::as_array)
|
||||
.is_some_and(Vec::is_empty)
|
||||
{
|
||||
body.shift_remove("stop_sequences");
|
||||
}
|
||||
if body
|
||||
.get("prompt_cache_options")
|
||||
.and_then(Value::as_object)
|
||||
.is_some_and(Map::is_empty)
|
||||
{
|
||||
body.shift_remove("prompt_cache_options");
|
||||
}
|
||||
// 原生上限优先,Chat 别名只补缺,非法值仍交给上游校验
|
||||
for alias in ["max_completion_tokens", "max_tokens"] {
|
||||
if let Some(limit) = body.shift_remove(alias)
|
||||
&& !limit.is_null()
|
||||
&& body.get("max_output_tokens").is_none_or(Value::is_null)
|
||||
{
|
||||
body.insert("max_output_tokens".to_owned(), limit);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// 补齐 Codex 请求缺省字段并适配已确认不兼容的请求形状,不递归清洗业务正文
|
||||
///
|
||||
/// 兼容基准是 Codex Core/Desktop 的模型请求,不是公开 OpenAI Responses API
|
||||
|
||||
@@ -2,6 +2,75 @@
|
||||
|
||||
use super::*;
|
||||
|
||||
#[tokio::test]
|
||||
async fn responses_options_follow_the_selected_upstream_on_both_transports() {
|
||||
let oauth_omissions = json!({
|
||||
"frequency_penalty": 0, "presence_penalty": 0, "top_p": 1,
|
||||
"chat_template_kwargs": {"enable_thinking": false}, "user": "example",
|
||||
"metadata": {"source": "example"}, "safety_identifier": "example",
|
||||
"truncation": "disabled", "stop_sequences": [], "prompt_cache_options": {}
|
||||
});
|
||||
let mut body = json!({
|
||||
"model": "gpt-5.4", "input": "hello", "max_tokens": 16,
|
||||
"stream_options": {"include_usage": true, "reasoning_summary_delivery": "sequential_cutoff"},
|
||||
"client_metadata": {"user": "keep"},
|
||||
"tools": [{"type": "function", "name": "sample", "parameters": {
|
||||
"type": "object", "properties": {"top_p": {"type": "number"}}
|
||||
}}]
|
||||
});
|
||||
let omissions = oauth_omissions.as_object().unwrap();
|
||||
body.as_object_mut().unwrap().extend(omissions.clone());
|
||||
for api_key in [false, true] {
|
||||
for websocket in [false, true] {
|
||||
let sent = capture_downstream_request(body.clone(), api_key, websocket, false).await;
|
||||
for (field, value) in omissions {
|
||||
assert_eq!(
|
||||
sent.get(field),
|
||||
api_key.then_some(value),
|
||||
"{field}, api_key={api_key}, websocket={websocket}"
|
||||
);
|
||||
}
|
||||
if api_key {
|
||||
assert_eq!(sent["stream_options"], body["stream_options"]);
|
||||
assert_eq!(sent["max_tokens"], 16);
|
||||
assert!(sent.get("max_output_tokens").is_none());
|
||||
} else {
|
||||
assert_eq!(
|
||||
sent["stream_options"],
|
||||
json!({"reasoning_summary_delivery": "sequential_cutoff"})
|
||||
);
|
||||
assert_eq!(sent["max_output_tokens"], 16);
|
||||
assert!(sent.get("max_tokens").is_none());
|
||||
}
|
||||
assert_eq!(sent["client_metadata"]["user"], "keep");
|
||||
assert_eq!(sent["tools"], body["tools"]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn codex_oauth_keeps_explicit_limits_and_context_options() {
|
||||
for (native, completion, expected) in [
|
||||
(json!(16), json!(32), json!(16)),
|
||||
(Value::Null, json!(32), json!(32)),
|
||||
(Value::Null, Value::Null, json!(64)),
|
||||
] {
|
||||
let body = json!({
|
||||
"model": "gpt-5.4", "input": "hello", "max_output_tokens": native,
|
||||
"max_completion_tokens": completion, "max_tokens": 64,
|
||||
"truncation": "auto", "stop_sequences": ["end"],
|
||||
"prompt_cache_options": {"future_option": true}
|
||||
});
|
||||
let sent = capture_downstream_request(body.clone(), false, false, false).await;
|
||||
assert_eq!(sent["max_output_tokens"], expected);
|
||||
assert!(sent.get("max_completion_tokens").is_none());
|
||||
assert!(sent.get("max_tokens").is_none());
|
||||
for key in ["truncation", "stop_sequences", "prompt_cache_options"] {
|
||||
assert_eq!(sent[key], body[key]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn effort_values_reach_upstream_before_and_after_middleware() {
|
||||
let store = Arc::new(MemoryAccountStore::default());
|
||||
|
||||
+11
-2
@@ -325,8 +325,8 @@ Chat 沿用 [Client Key 鉴权](#openai-数据面客户端接口)、[请求期
|
||||
[请求头与身份](#请求头与身份)及[隐私策略](#隐私策略)。隐私策略的 `request_body` 面向转换后的 Responses 正文,
|
||||
例如 `$.input`,不需要另外维护一套针对 `messages` 的规则
|
||||
|
||||
转换后的请求按选中 Provider 的正文规则处理。OpenAI 的[正文兼容](#正文兼容)会移除顶层 `temperature` 和
|
||||
`prompt_cache_retention`。输出上限原值交给上游校验,低于模型下限时会收到上游错误;`top_p` 是否可用也由上游判断
|
||||
转换后的请求按选中的 Provider 执行。OpenAI 的字段处理见[正文兼容](#正文兼容);
|
||||
输出上限原值交给上游校验,低于模型下限时会收到上游错误
|
||||
|
||||
### Images 请求
|
||||
|
||||
@@ -454,6 +454,15 @@ OpenAI Provider 的 Responses 上游编码会移除 Codex 不接受的顶层 `te
|
||||
不删除工具参数 schema、输入内容或 `client_metadata` 内的同名业务字段;其他未知字段继续透传。
|
||||
`prompt_cache_key` 不用于补造会话或线程请求头;客户端未提供缓存键、会话或线程身份时保持缺省
|
||||
|
||||
发往 Codex/OAuth 的 Responses 请求另按下表处理;API Key 上游保留这些字段。规则只匹配表中列出的位置
|
||||
|
||||
| 请求字段 | Codex/OAuth 出站行为 |
|
||||
| --- | --- |
|
||||
| `top_p`、`frequency_penalty`、`presence_penalty`、`chat_template_kwargs`、`user`、`metadata`、`safety_identifier` | 省略;采样参数取默认值时也省略 |
|
||||
| `stream_options.include_usage` | 省略该成员,保留其他流式选项 |
|
||||
| `truncation`、`stop_sequences`、`prompt_cache_options` | 分别仅在值为 `"disabled"`、`[]`、`{}` 时省略;显式策略交给上游校验 |
|
||||
| `max_completion_tokens`、`max_tokens` | 转为 `max_output_tokens`。原生字段优先,其次按左列顺序取首个非 `null` 值 |
|
||||
|
||||
Codex/OAuth 上游的历史回填按字段形状兼容,不以 User-Agent 品牌区分:显式 `type: "reasoning"`
|
||||
的 `input` 项移除顶层 `status`;该项具有非空字符串 `encrypted_content` 时,还会移除非空数组
|
||||
`content`。其他字段及顺序保持不变,缺少非空加密载荷的明文历史由上游判定。
|
||||
|
||||
Reference in New Issue
Block a user