diff --git a/backend/crates/providers/openai/src/provider/mod.rs b/backend/crates/providers/openai/src/provider/mod.rs index ac2394fdb..2a189b061 100644 --- a/backend/crates/providers/openai/src/provider/mod.rs +++ b/backend/crates/providers/openai/src/provider/mod.rs @@ -87,7 +87,7 @@ use crate::transport::protocol::responses::{ }; use crate::transport::request::{ CodexRequestEncodeError, RequestAccountScope, align_structured_location_fields, - encode_generate_payload, scope_request_to_account, + apply_codex_request_country, encode_generate_payload, scope_request_to_account, }; use crate::transport::session::CodexSessionIdentity; use crate::transport::usage::normalize_service_tier; @@ -1090,6 +1090,10 @@ impl CodexProvider { "after": crate::transport::diagnostics::evidence::request_state(&upstream_request), })); } + let location = lease + .account() + .request_location() + .or(context.request_location()); if codex_oauth { if matches!( guardian_role, @@ -1104,16 +1108,13 @@ impl CodexProvider { &middleware_headers, context.request_id().as_str(), ); + apply_codex_request_country( + upstream_request.body_mut(), + location, + context.openai_request_country(), + ); } - let location = lease - .account() - .request_location() - .or(context.request_location()); - align_structured_location_fields( - upstream_request.body_mut(), - location, - context.openai_request_country(), - ); + align_structured_location_fields(upstream_request.body_mut(), location); let requirement = transport_requirement(&upstream_request); let http_only = lease.transport() == crate::credential::ResponsesTransport::Http; if http_only && requirement.requires_websocket() { diff --git a/backend/crates/providers/openai/src/transport/request.rs b/backend/crates/providers/openai/src/transport/request.rs index 049a96aff..2be2d0416 100644 --- a/backend/crates/providers/openai/src/transport/request.rs +++ b/backend/crates/providers/openai/src/transport/request.rs @@ -108,11 +108,12 @@ pub fn encode_generate_request( location: Option<&CodexRequestLocation>, ) -> Result { let mut encoded = encode_generate_payload(request, upstream_model)?; - align_structured_location_fields( + apply_codex_request_country( encoded.body_mut(), location, &OpenAiRequestCountry::default(), ); + align_structured_location_fields(encoded.body_mut(), location); Ok(encoded) } @@ -135,7 +136,7 @@ pub(crate) fn encode_generate_payload( Ok(encoded) } -pub(crate) fn align_structured_location_fields( +pub(crate) fn apply_codex_request_country( body: &mut Map, location: Option<&CodexRequestLocation>, country: &OpenAiRequestCountry, @@ -155,6 +156,12 @@ pub(crate) fn align_structured_location_fields( body.insert("request_country".into(), Value::from(country.as_str())); } } +} + +pub(crate) fn align_structured_location_fields( + body: &mut Map, + location: Option<&CodexRequestLocation>, +) { let Some(location) = location else { return }; if let Some(input) = body.get_mut("input").and_then(Value::as_array_mut) { for item in input { diff --git a/backend/crates/providers/openai/tests/provider/contract/mod.rs b/backend/crates/providers/openai/tests/provider/contract/mod.rs index ef595a45a..fd90cd33a 100644 --- a/backend/crates/providers/openai/tests/provider/contract/mod.rs +++ b/backend/crates/providers/openai/tests/provider/contract/mod.rs @@ -13463,10 +13463,12 @@ async fn downstream_client_markers_preserve_instruction_content() { )]); let sent = capture_client_request(body.clone(), context, api_key).await; let mut expected = body; - expected.insert( - "request_country".to_owned(), - json!(global_request_location().country), - ); + if !api_key { + expected.insert( + "request_country".to_owned(), + json!(global_request_location().country), + ); + } assert_eq!(sent, expected); } } diff --git a/backend/crates/providers/openai/tests/provider/contract/passthrough.rs b/backend/crates/providers/openai/tests/provider/contract/passthrough.rs index 1668458b3..87f905c42 100644 --- a/backend/crates/providers/openai/tests/provider/contract/passthrough.rs +++ b/backend/crates/providers/openai/tests/provider/contract/passthrough.rs @@ -53,8 +53,7 @@ async fn responses_options_follow_the_selected_upstream_on_both_transports() { "model": "gpt-5.4", "input": "hello", "max_output_tokens": 1, "max_completion_tokens": 32, "max_tokens": 64, "stream_options": {"include_usage": true, "reasoning_summary_delivery": "sequential_cutoff"}, - "request_country": "US", - "client_metadata": {"user": "keep", "request_country": "US"}, + "client_metadata": {"user": "keep"}, "tools": [{"type": "function", "name": "sample", "parameters": { "type": "object", "properties": {"top_p": {"type": "number"}} }}] @@ -86,102 +85,86 @@ async fn responses_options_follow_the_selected_upstream_on_both_transports() { ); } assert_eq!(sent["client_metadata"]["user"], "keep"); - assert_eq!(sent.get("request_country"), Some(&Value::Null)); - assert_eq!(sent["client_metadata"]["request_country"], "US"); assert_eq!(sent["tools"], body["tools"]); } } } #[tokio::test] -async fn request_country_modes_only_change_the_top_level_across_transports() { +async fn request_country_follows_the_selected_account_across_transports() { use OpenAiRequestCountry::{Custom, Default, FollowLocation, Passthrough}; - for (api_key, websocket, policy, input, expected) in [ + let location = global_request_location(); + for (policy, location, input, oauth_country) in [ + (FollowLocation {}, None, None, Some(Value::Null)), + (FollowLocation {}, Some(&location), None, Some(json!("NZ"))), ( - false, - false, Default {}, + Some(&location), Some(json!("CA")), Some(Value::Null), ), - (true, true, Default {}, None, Some(Value::Null)), - (false, true, Passthrough {}, None, None), + (Passthrough {}, Some(&location), None, None), ( - true, - false, - Passthrough {}, - Some(Value::Null), - Some(Value::Null), - ), - ( - false, - false, Passthrough {}, + Some(&location), Some(json!("CA")), Some(json!("CA")), ), ( - true, - true, - FollowLocation {}, - Some(json!("CA")), - Some(json!("NZ")), - ), - ( - true, - false, - Custom { - country: "JP".into(), - }, - None, - Some(json!("JP")), - ), - ( - false, - true, Custom { country: "JP".into(), }, + Some(&location), Some(Value::Null), Some(json!("JP")), ), ] { - let location = global_request_location(); let mut body = json!({ - "model": "gpt-5.4", - "input": "hello", - "client_metadata": {"request_country":"business"}, - "tools": [{"type":"web_search"}], + "model": "gpt-5.4", "input": "hello", + "client_metadata": {"request_country": "business"}, + "tools": [{"type": "web_search"}], }); - if let Some(input) = input { - body["request_country"] = input; + if let Some(country) = &input { + body["request_country"] = country.clone(); + } + for api_key in [false, true] { + let expected = if api_key { &input } else { &oauth_country }; + for websocket in [false, true] { + let sent = capture_downstream_with_country( + body.clone(), + api_key, + websocket, + false, + location.cloned(), + policy.clone(), + ) + .await; + assert_eq!( + sent.get("request_country"), + expected.as_ref(), + "{policy:?}, api_key={api_key}, websocket={websocket}", + ); + assert_eq!( + sent.pointer("/client_metadata/request_country"), + body.pointer("/client_metadata/request_country"), + ); + assert_eq!( + sent.pointer("/tools/0/user_location/country") + .and_then(Value::as_str), + location.map(|location| location.country.as_str()), + ); + assert_eq!( + sent.pointer("/tools/0/user_location/city") + .and_then(Value::as_str), + location.map(|location| location.city.as_str()), + ); + assert_eq!( + sent.pointer("/tools/0/user_location/timezone") + .and_then(Value::as_str), + location.map(|location| location.timezone.name()), + ); + } } - let sent = capture_downstream_with_country( - body, - api_key, - websocket, - false, - Some(location.clone()), - policy, - ) - .await; - assert_eq!(sent.get("request_country"), expected.as_ref()); - assert_eq!( - sent.pointer("/client_metadata/request_country"), - Some(&json!("business")) - ); - assert_eq!( - sent.pointer("/tools/0/user_location/country"), - Some(&json!(location.country)) - ); - assert_eq!( - sent.pointer("/tools/0/user_location/city"), - Some(&json!(location.city)) - ); - assert_eq!( - sent.pointer("/tools/0/user_location/timezone"), - Some(&json!(location.timezone.name())) - ); } } diff --git a/deploy/README.md b/deploy/README.md index 4fedc3499..d3afa6dc8 100644 --- a/deploy/README.md +++ b/deploy/README.md @@ -418,7 +418,7 @@ PostgreSQL/Redis 启动密码。日常校验使用 `config --quiet` | 并发与排队 | “系统设置 → 运行设置”;Key 与账号分别设置排队容量和超时,可选择账号排队失败后换号,见[排队规则](../docs/api.md#并发与排队) | | 账号错误保护 | “系统设置 → 运行设置 → 错误保护”;默认关闭,按[自定义规则](../docs/api.md#账号错误保护)暂停账号调度 | | 全局请求位置 | “系统设置 → 上游配置 → 请求位置覆盖”,默认关闭 | -| OpenAI 请求国家 | “系统设置 → 上游配置 → 请求国家”,默认跟随位置,也可选择“空值”“透传”或“自定义” | +| Codex OAuth 请求国家 | “系统设置 → 上游配置 → 请求国家”,默认跟随位置,也可选择“空值”“透传”或“自定义” | | OpenAI OAuth 同步审核模型 | “系统设置 → 上游配置 → 模型映射”中的“专用审核模型”默认开启,关闭后沿用客户端模型。[适用条件](../docs/api.md#正文兼容) | | 代理位置 | “代理管理”;优先于全局位置,全局关闭时仍可独立生效 | @@ -438,7 +438,7 @@ PostgreSQL/Redis 启动密码。日常校验使用 `config --quiet` 在下游导入或编辑 OpenAI API Key 账号,填写上游 API 地址和该 Key,后台自动发现 CPR 互联能力。 地址包含自定义前缀时保留完整前缀,后台在该地址下请求 `cpr/link`。 -列表与详情显示已保存的观测。展开账号可查看上游密钥限制,普通账号并发留空时跟随上游有效的正数上限。启用的 OpenAI API Key 账号可点击「刷新额度」重新识别。上游明确关闭共享或不支持互联时,账号显示为 API,重新识别成功后显示 CPR +可在账号行的「更多操作」中选择「检测 CPR 互联」手动识别。识别成功后账号显示为 CPR,展开可查看上游密钥限制和刷新额度。普通账号并发留空时跟随上游有效的正数上限。上游明确关闭共享或不支持互联时,账号显示为 API 第三方网关通过插件实现相同协议也可被识别,不按软件名称判定兼容。 上游明确返回 Key 预算耗尽时,下游暂停该账号;后续同步确认允许使用后自动恢复,普通限流冷却独立保留。 diff --git a/docs/api.md b/docs/api.md index 17f6ae387..fab7d15be 100644 --- a/docs/api.md +++ b/docs/api.md @@ -1386,7 +1386,7 @@ Key 消费窗口的 `localUsage` 按各自 `[resetAt - windowSeconds, resetAt)` - 收到有效快照时显示 CPR 互联。明确返回不支持协议(HTTP 404、405、501),或返回 `403 peer_sharing_disabled` 时,清除互联身份和共享数据,显示普通 API Key - 普通 401/403 保留原识别结果并清除已撤销的共享内容。超时、限流、服务异常或无效响应保留原识别结果和已有观测。无观测时 `observedAt` 与 `pool` 为 `null`,额度窗口为空 -- 展开可探测的 API Key 后,点击「刷新额度」可重新识别互联。卡片显示已有更新时间,手动刷新结果通过通知反馈。明确不支持协议的连接停止周期探测,共享关闭的连接按授权退避周期复查 +- 可探测但尚未识别为互联的 API Key,可在账号行的「更多操作」中选择「检测 CPR 互联」。识别成功后可在额度卡点击「刷新额度」。明确不支持协议的连接停止周期探测,共享关闭的连接按授权退避周期复查 账号地址、密钥或出口变更使旧观测与识别身份失效,重新识别当前连接。 账号停用后停止同步,重新启用后按当前连接的识别结果恢复后台任务 @@ -1508,7 +1508,7 @@ API 的 `autoLocation` 默认为 `false`;开启时使用已检测位置,测 只改位置不清空连通性测试结果,也不更改账号凭据版本 关联账号的 OpenAI/Codex Responses 请求(HTTP/SSE、WebSocket)优先使用代理位置,否则使用全局 -运行设置中已开启的 `requestLocation`。出站正文的顶层 `request_country` 默认跟随位置,也可在[请求位置](#请求位置)选择“空值”“透传”或“自定义”。 +运行设置中已开启的 `requestLocation`。Codex OAuth 请求的顶层国家字段由[请求国家策略](#请求位置)控制。 位置覆盖同时更新客户端时间上下文的时区和 Web Search 的结构化位置,未启用覆盖时这两项保留客户端原值。 全局覆盖按请求冻结,保存后对新请求生效。代理覆盖按每次执行选定的账号解析,包括换号和换出口。 位置覆盖不改变 epoch 时间戳、真实出口 IP、服务或管理端时区、数据驻留约束及 xAI 请求 @@ -2272,7 +2272,7 @@ HTTP 返回 `429`,`error.code` 为 `key_daily_budget_exceeded` 或 `key_weekly 关联代理配置了自定义位置时优先使用代理值。保存后通过现有配置发布机制对新请求生效, 已开始请求及其重试保持同一份全局值;普通文本、绝对时间戳和数据驻留要求不受影响 -`openaiRequestCountry` 单独控制 OpenAI Responses 出站正文顶层的 `request_country`。保存后对新请求生效。 +`openaiRequestCountry` 仅控制发往 Codex OAuth 上游的 Responses 请求顶层字段 `request_country`。API Key 上游保留客户端原值,包括显式 `null`,客户端省略时也省略。保存后对新请求生效 | 管理端选项 | 设置值 | 发往上游的字段 | | --- | --- | --- | diff --git a/frontend/src/components/HelpPopover.vue b/frontend/src/components/HelpPopover.vue index fa7b75271..0590b86fd 100644 --- a/frontend/src/components/HelpPopover.vue +++ b/frontend/src/components/HelpPopover.vue @@ -9,7 +9,7 @@ const canHover = useMediaQuery('(hover: hover)')