fix(openai): omit unreferenced item IDs from replayed text messages

This commit is contained in:
Zyy
2026-11-20 15:42:02 +08:00
parent 4b1cae89b2
commit d90b9d129e
3 changed files with 106 additions and 25 deletions
@@ -1,5 +1,7 @@
//! 下游客户端模型请求正文的 Codex 协议兼容
use std::collections::HashSet;
use serde_json::{Map, Value, json};
/// 非官方客户端及跨客户端历史回填的兼容入口;调用方必须已选定 Codex/OAuth 上游
@@ -9,31 +11,83 @@ pub(super) fn normalize_non_codex_request_body(body: &mut Map<String, Value>) {
let Some(input) = body.get_mut("input").and_then(Value::as_array_mut) else {
return;
};
let mut messages = Vec::new();
let mut referenced_ids = HashSet::new();
for item in input {
let Some(item) = item.as_object_mut() else {
continue;
};
if item.get("type").and_then(Value::as_str) != Some("reasoning") {
continue;
let kind = item.get("type").and_then(Value::as_str);
match kind {
Some("reasoning") => normalize_replayed_reasoning(item),
Some("message") => {
if generic_item_id(item).is_some() && has_inline_text_content(item) {
messages.push(item);
}
}
_ => {
let is_reference = kind == Some("item_reference")
|| (item.get("type").is_none_or(Value::is_null)
&& !item.contains_key("role")
&& !item.contains_key("content"));
if is_reference && let Some(id) = generic_item_id(item) {
referenced_ids.insert(id);
}
}
}
// SDK 输出项的 status 不属于 Codex reasoning 输入合同;其他项的 status 可能合法
item.shift_remove("status");
// Codex 只接受空 content 数组;仅在加密历史仍可回填时去掉冗余明文,
// 没有加密内容的历史不自动丢弃,保留给上游明确拒绝
if item
.get("encrypted_content")
.and_then(Value::as_str)
.is_some_and(|content| !content.trim().is_empty())
&& item
.get("content")
.and_then(Value::as_array)
.is_some_and(|content| !content.is_empty())
{
item.shift_remove("content");
}
// Codex 要求消息 ID 以 msg 开头;引用可以出现在消息前后,收齐后才能省略可选 ID
for message in messages {
if generic_item_id(message).is_some_and(|id| !referenced_ids.contains(id)) {
message.shift_remove("id");
}
}
}
fn generic_item_id(item: &Map<String, Value>) -> Option<&str> {
item.get("id")
.and_then(Value::as_str)
.filter(|id| id.starts_with("item_"))
}
fn has_inline_text_content(item: &Map<String, Value>) -> bool {
matches!(
item.get("role").and_then(Value::as_str),
Some("user" | "assistant" | "developer" | "system")
) && !item.contains_key("encrypted_content")
&& match item.get("content") {
Some(Value::String(text)) => !text.is_empty(),
Some(Value::Array(parts)) => {
!parts.is_empty()
&& parts.iter().all(|part| {
matches!(
part.get("type").and_then(Value::as_str),
Some("input_text" | "output_text")
) && part.get("text").is_some_and(Value::is_string)
})
}
_ => false,
}
}
fn normalize_replayed_reasoning(item: &mut Map<String, Value>) {
// SDK 输出项的 status 不属于 Codex reasoning 输入合同;其他项的 status 可能合法
item.shift_remove("status");
// Codex 只接受空 content 数组;仅在加密历史仍可回填时去掉冗余明文,
// 没有加密内容的历史不自动丢弃,保留给上游明确拒绝
if item
.get("encrypted_content")
.and_then(Value::as_str)
.is_some_and(|content| !content.trim().is_empty())
&& item
.get("content")
.and_then(Value::as_array)
.is_some_and(|content| !content.is_empty())
{
item.shift_remove("content");
}
}
fn normalize_codex_oauth_options(body: &mut Map<String, Value>) {
// Codex OAuth 拒绝这些客户端选项,即使采样参数取默认值
for field in [
@@ -896,13 +896,40 @@ async fn replay_compatibility_should_leave_normal_official_history_unchanged() {
}
}
#[tokio::test]
async fn replay_compatibility_should_omit_only_unreferenced_inline_message_ids() {
let input = json!([
{"type":"item_reference","id":"item_before"},
{"type":"message","id":"item_standalone","role":"assistant","phase":"final_answer",
"content":[{"type":"output_text","text":"done"}]},
{"type":"message","id":"item_before","role":"assistant","content":"first"},
{"type":"message","id":"item_after","role":"user","content":"second"},
{"type":"message","id":"item_implicit","role":"developer","content":"instructions"},
{"type":"item_reference","id":"item_after"},
{"id":"item_implicit"},
{"type":"message","id":"msg_valid","role":"assistant","content":"answer"},
{"type":"message","id":"item_no_content","role":"assistant","content":[]},
{"type":"message","id":"item_encrypted","role":"assistant","content":"summary","encrypted_content":"opaque"},
{"type":"message","id":"item_opaque_part","role":"assistant","content":[{"type":"encrypted_content","encrypted_content":"opaque"}]},
{"type":"message","id":"item_image","role":"user","content":[{"type":"input_image","file_id":"file_example"}]},
{"type":"agent_message","id":"item_agent","content":[{"type":"encrypted_content","encrypted_content":"opaque"}]},
{"type":"function_call","id":"item_call","call_id":"call_example","name":"echo","arguments":"{}"}
]);
let mut expected = input.clone();
expected[1].as_object_mut().unwrap().shift_remove("id");
for websocket in [false, true] {
let actual = capture_replay_compatibility_request(input.clone(), false, websocket).await;
assert_eq!(actual["input"].to_string(), expected.to_string());
}
}
#[tokio::test]
async fn replay_compatibility_should_not_apply_codex_rules_to_api_key_accounts() {
let input = json!([{
"type":"reasoning","id":"rs_api","status":"completed","summary":[],
"content":[{"type":"reasoning_text","text":"API-specific history"}],
"encrypted_content":"test-cipher"
}]);
let input = json!([
{"type":"reasoning","id":"rs_api","status":"completed","summary":[],
"content":[{"type":"reasoning_text","text":"API-specific history"}],"encrypted_content":"test-cipher"},
{"type":"message","id":"item_standalone","role":"assistant","content":"answer"}
]);
for websocket in [false, true] {
let actual = capture_replay_compatibility_request(input.clone(), true, websocket).await;
assert_eq!(actual["input"].to_string(), input.to_string());
+4 -4
View File
@@ -571,10 +571,10 @@ WS 握手的轮次元数据头只随首次请求转发,后续帧在正文中
| `stream_options.include_usage` | 省略该成员,保留其他流式选项 |
| `truncation`、`stop_sequences`、`prompt_cache_options` | 分别仅在值为 `"disabled"`、`[]`、`{}` 时省略;显式策略交给上游校验 |
Codex/OAuth 上游的历史回填按字段形状兼容,不以 User-Agent 品牌区分:显式 `type: "reasoning"`
的 `input` 项移除顶层 `status`;该项具有非空字符串 `encrypted_content` 时,还会移除非空数组
`content`。其他字段及顺序保持不变,缺少非空加密载荷的明文历史由上游判定。
普通消息、工具项及未知类型不受此规则影响,API Key 上游不应用此规则
选定 Codex/OAuth 上游后,HTTP 和 WebSocket 按相同规则处理 `input` 中的回放条目,适用于所有 User-Agent。API Key 上游保留这些历史字段
- 显式 `message` 带有普通消息角色和完整文本正文(非空字符串或仅含 `input_text`、`output_text` 的非空数组),且未携带顶层 `encrypted_content` 时,省略未被本次请求的 `item_reference` 引用的 `item_…` ID。消息正文和顺序保持不变
- 显式 `reasoning` 移除顶层 `status`。该项具有非空字符串 `encrypted_content` 时,还会移除非空数组 `content`。缺少非空加密载荷的明文历史由上游判定
请求头过滤不提供客户端匿名化;系统提示词、工具定义、工具结果、工作目录及其他业务 metadata
保持原有语义,可能包含客户端环境信息。