mirror of
https://github.com/zyycn/codex-proxy-rs.git
synced 2026-11-20 16:01:55 +00:00
feat(openai): align Codex Guardian routing and request country (#754)
This commit is contained in:
@@ -5,6 +5,7 @@ use std::{collections::BTreeMap, fmt};
|
||||
use chrono::{DateTime, Utc};
|
||||
|
||||
use gateway_core::routing::{ProviderKind, PublicModelId, UpstreamModelId};
|
||||
use gateway_core::settings::default_oauth_guardian_review_model;
|
||||
|
||||
use super::Revision;
|
||||
|
||||
@@ -83,6 +84,10 @@ pub struct RuntimeSettingsValues {
|
||||
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
pub struct RuntimeSettings {
|
||||
#[serde(default = "default_oauth_guardian_review_model")]
|
||||
pub openai_oauth_guardian_review_model: bool,
|
||||
#[serde(default)]
|
||||
pub openai_request_country: gateway_core::account::OpenAiRequestCountry,
|
||||
#[serde(flatten)]
|
||||
pub values: RuntimeSettingsValues,
|
||||
pub request_profiles: ProviderRequestProfiles,
|
||||
@@ -96,6 +101,11 @@ pub struct RuntimeSettings {
|
||||
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
pub struct ReplaceRuntimeSettings {
|
||||
/// 旧客户端缺少此字段时保留已保存的审核模型策略
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub openai_oauth_guardian_review_model: Option<bool>,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub openai_request_country: Option<gateway_core::account::OpenAiRequestCountry>,
|
||||
#[serde(flatten)]
|
||||
pub values: RuntimeSettingsValues,
|
||||
/// 读取设置时的版本;与写入在同一事务内比较,防止覆盖并发更新
|
||||
@@ -176,6 +186,8 @@ impl From<RuntimeSettings> for ReplaceRuntimeSettings {
|
||||
fn from(settings: RuntimeSettings) -> Self {
|
||||
Self {
|
||||
expected_revision: settings.config_revision,
|
||||
openai_oauth_guardian_review_model: Some(settings.openai_oauth_guardian_review_model),
|
||||
openai_request_country: Some(settings.openai_request_country),
|
||||
request_profile_updates: settings
|
||||
.request_profiles
|
||||
.into_iter()
|
||||
|
||||
@@ -109,6 +109,11 @@ impl DefaultSettingsService {
|
||||
}
|
||||
|
||||
fn validate_settings(command: &ReplaceRuntimeSettings) -> Result<(), AdminError> {
|
||||
if let Some(policy) = &command.openai_request_country {
|
||||
policy
|
||||
.validate()
|
||||
.map_err(|_| AdminError::invalid("请求国家必须是两位大写 ASCII 字母"))?;
|
||||
}
|
||||
crate::model::settings::validate_model_mappings(
|
||||
command
|
||||
.model_mappings
|
||||
|
||||
@@ -1168,6 +1168,8 @@ impl SettingsStore for StaticSettingsStore {
|
||||
}
|
||||
async fn load_runtime_settings(&self) -> AdminStoreResult<RuntimeSettings> {
|
||||
Ok(RuntimeSettings {
|
||||
openai_oauth_guardian_review_model: false,
|
||||
openai_request_country: Default::default(),
|
||||
request_profiles: Default::default(),
|
||||
config_revision: revision(1),
|
||||
model_mappings: Default::default(),
|
||||
|
||||
@@ -27,6 +27,8 @@ use super::accounts::{FakeAccountStore, FakeProviderAdmin, account_record, event
|
||||
|
||||
fn runtime_settings(enabled: bool, probe_enabled: bool) -> RuntimeSettings {
|
||||
RuntimeSettings {
|
||||
openai_oauth_guardian_review_model: false,
|
||||
openai_request_country: Default::default(),
|
||||
request_profiles: Default::default(),
|
||||
config_revision: revision(1),
|
||||
model_mappings: Default::default(),
|
||||
|
||||
@@ -1425,6 +1425,8 @@ impl SettingsStore for FixtureSettingsStore {
|
||||
}
|
||||
async fn load_runtime_settings(&self) -> AdminStoreResult<RuntimeSettings> {
|
||||
Ok(RuntimeSettings {
|
||||
openai_oauth_guardian_review_model: false,
|
||||
openai_request_country: Default::default(),
|
||||
request_profiles: Default::default(),
|
||||
config_revision: Revision::new(1).expect("revision"),
|
||||
model_mappings: Default::default(),
|
||||
|
||||
@@ -254,7 +254,7 @@ fn sdk_settings_contract_matches_host_declarations() {
|
||||
),
|
||||
(
|
||||
"../gateway-core/src/account/location.rs",
|
||||
&["RequestLocation"][..],
|
||||
&["RequestLocation", "OpenAiRequestCountry"][..],
|
||||
),
|
||||
(
|
||||
"../gateway-core/src/account/scheduling.rs",
|
||||
@@ -344,9 +344,11 @@ fn sdk_settings_contract_matches_host_declarations() {
|
||||
assert_eq!(operations.len(), method_count, "公开服务方法必须完整声明");
|
||||
let assignments = replacement_fields.iter().map(|field| match field.to_string().as_str() {
|
||||
"expected_revision" => quote!(expected_revision: settings.config_revision),
|
||||
"openai_oauth_guardian_review_model" | "openai_request_country" => quote!(#field: Some(settings.#field)),
|
||||
"request_profile_updates" => quote!(request_profile_updates: settings.request_profiles.into_iter().map(|(provider, value)| (provider, Some(value))).collect()),
|
||||
_ => quote!(#field: settings.#field),
|
||||
});
|
||||
let default_review_model = gateway_core::settings::default_oauth_guardian_review_model();
|
||||
let generated = quote! {
|
||||
//! 宿主设置服务的操作标识与请求、响应数据合同
|
||||
//!
|
||||
@@ -362,7 +364,11 @@ fn sdk_settings_contract_matches_host_declarations() {
|
||||
pub type RotationStrategy = String;
|
||||
pub type AccountAffinity = String;
|
||||
pub type PricingOverrides = BTreeMap<String, BTreeMap<String, ModelPriceOverride>>;
|
||||
fn default_oauth_guardian_review_model() -> bool { #default_review_model }
|
||||
#(#types)*
|
||||
impl Default for OpenAiRequestCountry {
|
||||
fn default() -> Self { Self::FollowLocation {} }
|
||||
}
|
||||
#(#operations)*
|
||||
impl From<RuntimeSettings> for ReplaceRuntimeSettings {
|
||||
fn from(settings: RuntimeSettings) -> Self {
|
||||
|
||||
@@ -320,6 +320,8 @@ async fn settings_should_reject_zero_refresh_margin_before_store_call() {
|
||||
request_id: "request-settings".to_owned(),
|
||||
},
|
||||
ReplaceRuntimeSettings {
|
||||
openai_oauth_guardian_review_model: None,
|
||||
openai_request_country: None,
|
||||
expected_revision: gateway_admin::model::Revision::new(1).unwrap(),
|
||||
request_profile_updates: Default::default(),
|
||||
model_mappings: Default::default(),
|
||||
|
||||
@@ -74,6 +74,8 @@ pub struct RuntimeSettingsFields {
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct RuntimeSettingsView {
|
||||
pub openai_oauth_guardian_review_model: bool,
|
||||
pub openai_request_country: gateway_core::account::OpenAiRequestCountry,
|
||||
#[serde(flatten)]
|
||||
pub values: RuntimeSettingsFields,
|
||||
pub config_revision: u64,
|
||||
@@ -94,16 +96,20 @@ pub struct RuntimeSettingsView {
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
|
||||
#[serde(rename_all = "camelCase", deny_unknown_fields)]
|
||||
pub struct UpdateRuntimeSettingsRequest {
|
||||
#[serde(default, deserialize_with = "deserialize_present_update")]
|
||||
pub openai_oauth_guardian_review_model: Option<bool>,
|
||||
#[serde(default, deserialize_with = "deserialize_present_update")]
|
||||
pub openai_request_country: Option<gateway_core::account::OpenAiRequestCountry>,
|
||||
#[serde(flatten)]
|
||||
pub values: RuntimeSettingsFields,
|
||||
pub config_revision: u64,
|
||||
#[serde(default)]
|
||||
pub provider_request_profiles: ProviderRequestProfileUpdates,
|
||||
/// 兼容既有 wire;与泛化字段冲突时拒绝整个请求
|
||||
#[serde(default, deserialize_with = "deserialize_profile_update")]
|
||||
#[serde(default, deserialize_with = "deserialize_present_update")]
|
||||
pub openai_client_profile: Option<serde_json::Map<String, serde_json::Value>>,
|
||||
/// 兼容既有 wire;与泛化字段冲突时拒绝整个请求
|
||||
#[serde(default, deserialize_with = "deserialize_profile_update")]
|
||||
#[serde(default, deserialize_with = "deserialize_present_update")]
|
||||
pub xai_client_profile: Option<serde_json::Map<String, serde_json::Value>>,
|
||||
pub model_mappings: ModelMappings,
|
||||
pub rotation_strategy: String,
|
||||
@@ -112,6 +118,7 @@ pub struct UpdateRuntimeSettingsRequest {
|
||||
impl UpdateRuntimeSettingsRequest {
|
||||
/// 校验公共运行参数
|
||||
pub fn validate(&self) -> Result<(), WireValidationError> {
|
||||
self.validate_request_country()?;
|
||||
validate_model_mappings(&self.model_mappings)?;
|
||||
if RotationStrategy::parse(&self.rotation_strategy).is_none() {
|
||||
return Err(WireValidationError::new("rotationStrategy"));
|
||||
@@ -120,6 +127,7 @@ impl UpdateRuntimeSettingsRequest {
|
||||
}
|
||||
|
||||
fn into_command(self) -> Result<ReplaceRuntimeSettings, WireValidationError> {
|
||||
self.validate_request_country()?;
|
||||
validate_model_mappings(&self.model_mappings)?;
|
||||
let request_profile_updates = normalize_request_profile_updates(
|
||||
self.provider_request_profiles,
|
||||
@@ -127,6 +135,8 @@ impl UpdateRuntimeSettingsRequest {
|
||||
self.xai_client_profile,
|
||||
)?;
|
||||
Ok(ReplaceRuntimeSettings {
|
||||
openai_oauth_guardian_review_model: self.openai_oauth_guardian_review_model,
|
||||
openai_request_country: self.openai_request_country,
|
||||
expected_revision: gateway_admin::model::Revision::new(self.config_revision)
|
||||
.map_err(|_| WireValidationError::new("configRevision"))?,
|
||||
request_profile_updates,
|
||||
@@ -136,6 +146,15 @@ impl UpdateRuntimeSettingsRequest {
|
||||
values: self.values.into_values()?,
|
||||
})
|
||||
}
|
||||
|
||||
fn validate_request_country(&self) -> Result<(), WireValidationError> {
|
||||
if let Some(policy) = &self.openai_request_country {
|
||||
policy
|
||||
.validate()
|
||||
.map_err(|_| WireValidationError::new("openaiRequestCountry"))?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl From<(RuntimeSettings, crate::time::TimePresenter)> for RuntimeSettingsView {
|
||||
@@ -147,6 +166,8 @@ impl From<(RuntimeSettings, crate::time::TimePresenter)> for RuntimeSettingsView
|
||||
.collect::<ProviderRequestProfiles>();
|
||||
Self {
|
||||
config_revision: settings.config_revision.get(),
|
||||
openai_oauth_guardian_review_model: settings.openai_oauth_guardian_review_model,
|
||||
openai_request_country: settings.openai_request_country,
|
||||
openai_client_profile: provider_request_profiles.get("openai").cloned(),
|
||||
xai_client_profile: provider_request_profiles.get("xai").cloned(),
|
||||
provider_request_profiles,
|
||||
@@ -595,6 +616,7 @@ fn map_wire_error(error: WireValidationError) -> AdminError {
|
||||
"settingsUsageRetentionOverflow" => "用量记录保留天数不合法".to_owned(),
|
||||
"settingsOpsRetentionOverflow" => "运维事件保留天数不合法".to_owned(),
|
||||
"settingsAuditRetentionOverflow" => "审计记录保留天数不合法".to_owned(),
|
||||
"openaiRequestCountry" => "自定义请求国家必须是两位大写 ASCII 字母".to_owned(),
|
||||
"requestLocation" => "请求位置的国家代码、地区或城市不合法".to_owned(),
|
||||
"providerRequestProfiles" => "服务商客户端身份配置无效或字段冲突".to_owned(),
|
||||
"minCodexDesktopVersion" => "Codex Desktop 最低版本格式不合法".to_owned(),
|
||||
@@ -645,11 +667,11 @@ fn normalize_request_profile_updates(
|
||||
Ok(normalized)
|
||||
}
|
||||
|
||||
// 字段省略时保留已有配置;显式 null 不能清空唯一的通用默认
|
||||
fn deserialize_profile_update<'de, D: serde::Deserializer<'de>>(
|
||||
// 字段省略时保留已有配置;显式 null 不能清空设置
|
||||
fn deserialize_present_update<'de, D: serde::Deserializer<'de>, T: Deserialize<'de>>(
|
||||
deserializer: D,
|
||||
) -> Result<Option<serde_json::Map<String, serde_json::Value>>, D::Error> {
|
||||
serde_json::Map::<String, serde_json::Value>::deserialize(deserializer).map(Some)
|
||||
) -> Result<Option<T>, D::Error> {
|
||||
T::deserialize(deserializer).map(Some)
|
||||
}
|
||||
|
||||
async fn detect_location<S>(
|
||||
|
||||
@@ -33,6 +33,8 @@ pub(super) enum RequestDecodeSource {
|
||||
|
||||
#[derive(Clone, Default)]
|
||||
pub struct OpenAiRequestHeaders {
|
||||
guardian: Option<String>,
|
||||
ambiguous_guardian_headers: bool,
|
||||
turn_state: Option<String>,
|
||||
turn_metadata: Option<String>,
|
||||
version: Option<String>,
|
||||
@@ -57,6 +59,17 @@ impl OpenAiRequestHeaders {
|
||||
#[must_use]
|
||||
pub fn from_headers(headers: &HeaderMap) -> Self {
|
||||
Self {
|
||||
guardian: header_string(headers, "x-codex-guardian"),
|
||||
ambiguous_guardian_headers: [
|
||||
"x-codex-guardian",
|
||||
OPENAI_SUBAGENT_KEY,
|
||||
"x-codex-turn-metadata",
|
||||
]
|
||||
.into_iter()
|
||||
.any(|name| {
|
||||
let mut values = headers.get_all(name).iter();
|
||||
values.next().is_some_and(|v| v.to_str().is_err()) || values.next().is_some()
|
||||
}),
|
||||
turn_state: header_string(headers, "x-codex-turn-state"),
|
||||
turn_metadata: header_string(headers, "x-codex-turn-metadata"),
|
||||
version: header_string(headers, "version"),
|
||||
@@ -87,7 +100,46 @@ impl OpenAiRequestHeaders {
|
||||
self
|
||||
}
|
||||
|
||||
fn apply_subagent(&self, body: &mut Map<String, Value>) {
|
||||
fn apply_subagent(
|
||||
&self,
|
||||
body: &mut Map<String, Value>,
|
||||
context: &mut Map<String, Value>,
|
||||
source: RequestDecodeSource,
|
||||
) {
|
||||
if matches!(source, RequestDecodeSource::WebSocketFrame)
|
||||
&& let Some(frame_agent) = body
|
||||
.get("client_metadata")
|
||||
.and_then(|m| m.get(OPENAI_SUBAGENT_KEY))
|
||||
.and_then(Value::as_str)
|
||||
&& (frame_agent == "guardian"
|
||||
|| self.subagent.as_deref() == Some("guardian")
|
||||
|| self.guardian.is_some())
|
||||
{
|
||||
// 帧内声明优先;旧 opening 的不透明副本也不能在 Provider 再度覆盖
|
||||
let stale_subagent = self
|
||||
.subagent
|
||||
.as_deref()
|
||||
.is_some_and(|opening| opening != frame_agent);
|
||||
let stale_guardian = self.guardian.as_deref().is_some_and(|opening| {
|
||||
frame_agent != "guardian"
|
||||
|| gateway_protocol::openai::codex_guardian_role(body, context)
|
||||
.is_some_and(|role| role.header() != Some(opening))
|
||||
});
|
||||
if stale_subagent {
|
||||
context.insert("subagent".into(), Value::from(frame_agent));
|
||||
}
|
||||
if (stale_subagent || stale_guardian)
|
||||
&& let Some(Value::Array(headers)) =
|
||||
context.get_mut(PASSTHROUGH_HEADERS_CONTEXT_KEY)
|
||||
{
|
||||
headers.retain(|pair| match pair.get(0).and_then(Value::as_str) {
|
||||
Some(OPENAI_SUBAGENT_KEY) => !stale_subagent,
|
||||
Some("x-codex-guardian") => !stale_guardian,
|
||||
_ => true,
|
||||
});
|
||||
}
|
||||
return;
|
||||
}
|
||||
if let Some(subagent) = self.subagent.as_deref() {
|
||||
inject_subagent_metadata(body, subagent);
|
||||
}
|
||||
@@ -329,8 +381,14 @@ pub(super) fn decode_request_object(
|
||||
.map(|response_id| ContinuationIntent::PreviousResponseId(response_id.to_owned()))
|
||||
.unwrap_or(ContinuationIntent::None);
|
||||
|
||||
request_headers.apply_subagent(&mut object);
|
||||
let protocol_context = request_headers.protocol_context(use_websocket, source);
|
||||
let mut protocol_context = request_headers.protocol_context(use_websocket, source);
|
||||
gateway_protocol::openai::capture_codex_guardian_identity(
|
||||
&object,
|
||||
&mut protocol_context,
|
||||
request_headers.guardian.as_deref(),
|
||||
request_headers.ambiguous_guardian_headers,
|
||||
);
|
||||
request_headers.apply_subagent(&mut object, &mut protocol_context, source);
|
||||
|
||||
let semantics =
|
||||
gateway_protocol::openai::codex_responses_request_semantics(&object, &protocol_context);
|
||||
|
||||
@@ -332,6 +332,13 @@ async fn serve_responses_websocket(socket: Socket, session: ResponsesWebSocketSe
|
||||
// 握手的轮次头只属于首次执行,复用连接时由各 response.create 携带本轮数据
|
||||
raw_headers.remove("x-codex-turn-state");
|
||||
raw_headers.remove("x-codex-turn-metadata");
|
||||
raw_headers.remove("x-codex-guardian");
|
||||
if raw_headers
|
||||
.get("x-openai-subagent")
|
||||
.is_some_and(|value| value == "guardian")
|
||||
{
|
||||
raw_headers.remove("x-openai-subagent");
|
||||
}
|
||||
let service_for_terminal = service.clone();
|
||||
let replay_for_terminal = replay.clone();
|
||||
let connection_id_for_terminal = connection.id().to_owned();
|
||||
|
||||
@@ -636,6 +636,12 @@ impl SettingsStore for MemorySettingsStore {
|
||||
}
|
||||
}
|
||||
let updated = RuntimeSettings {
|
||||
openai_oauth_guardian_review_model: command
|
||||
.openai_oauth_guardian_review_model
|
||||
.unwrap_or(settings.openai_oauth_guardian_review_model),
|
||||
openai_request_country: command
|
||||
.openai_request_country
|
||||
.unwrap_or_else(|| settings.openai_request_country.clone()),
|
||||
request_profiles,
|
||||
config_revision: next_revision(settings.config_revision),
|
||||
model_mappings: command.model_mappings,
|
||||
@@ -1990,6 +1996,9 @@ fn test_runtime_settings() -> RuntimeSettings {
|
||||
),
|
||||
]);
|
||||
RuntimeSettings {
|
||||
openai_oauth_guardian_review_model:
|
||||
gateway_core::settings::default_oauth_guardian_review_model(),
|
||||
openai_request_country: Default::default(),
|
||||
request_profiles: Default::default(),
|
||||
config_revision: Revision::new(7).expect("revision"),
|
||||
model_mappings: mappings,
|
||||
|
||||
@@ -523,6 +523,13 @@ fn settings_service_contract_flattens_values_and_rejects_unknown_fields() {
|
||||
serde_json::from_value::<RuntimeSettings>(record.clone()).unwrap(),
|
||||
current
|
||||
);
|
||||
let mut legacy = record.as_object().unwrap().clone();
|
||||
legacy.remove("openai_oauth_guardian_review_model");
|
||||
legacy.remove("openai_request_country");
|
||||
assert_eq!(
|
||||
serde_json::from_value::<RuntimeSettings>(Value::Object(legacy)).unwrap(),
|
||||
current
|
||||
);
|
||||
record["unexpected_setting"] = json!(true);
|
||||
assert!(serde_json::from_value::<RuntimeSettings>(record).is_err());
|
||||
|
||||
@@ -536,6 +543,12 @@ fn settings_service_contract_flattens_values_and_rejects_unknown_fields() {
|
||||
serde_json::from_value::<ReplaceRuntimeSettings>(wire.clone()).unwrap(),
|
||||
command
|
||||
);
|
||||
let mut legacy = wire.as_object().unwrap().clone();
|
||||
legacy.remove("openai_oauth_guardian_review_model");
|
||||
legacy.remove("openai_request_country");
|
||||
let legacy = serde_json::from_value::<ReplaceRuntimeSettings>(Value::Object(legacy)).unwrap();
|
||||
assert_eq!(legacy.openai_oauth_guardian_review_model, None);
|
||||
assert_eq!(legacy.openai_request_country, None);
|
||||
wire["unexpected_setting"] = json!(true);
|
||||
assert!(serde_json::from_value::<ReplaceRuntimeSettings>(wire).is_err());
|
||||
}
|
||||
@@ -1416,6 +1429,86 @@ async fn settings_update_rejects_a_stale_version_without_replacing_the_saved_val
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn openai_request_settings_preserve_omitted_fields_and_reject_stale_writes() {
|
||||
let fixture = AdminTestFixture::new().await;
|
||||
fixture.auth.insert_session("valid-session");
|
||||
let read = || app(fixture.state()).oneshot(request(Method::GET, "/api/admin/settings", None));
|
||||
let save = |body| {
|
||||
app(fixture.state()).oneshot(request(
|
||||
Method::POST,
|
||||
"/api/admin/settings/update",
|
||||
Some(body),
|
||||
))
|
||||
};
|
||||
let initial = response_json(read().await.unwrap()).await["data"].clone();
|
||||
assert_eq!(initial["openaiOauthGuardianReviewModel"], true);
|
||||
assert_eq!(
|
||||
initial["openaiRequestCountry"],
|
||||
json!({"mode":"follow_location"})
|
||||
);
|
||||
let mut body = update_body();
|
||||
body["configRevision"] = initial["configRevision"].clone();
|
||||
body["openaiOauthGuardianReviewModel"] = json!(false);
|
||||
body["openaiRequestCountry"] = json!({"mode":"custom","country":"CA"});
|
||||
let response = save(body.clone()).await.unwrap();
|
||||
assert_eq!(response.status(), StatusCode::OK);
|
||||
let saved = response_json(response).await["data"].clone();
|
||||
assert_eq!(saved["openaiOauthGuardianReviewModel"], false);
|
||||
assert_eq!(saved["openaiRequestCountry"], body["openaiRequestCountry"]);
|
||||
|
||||
let mut legacy = update_body();
|
||||
legacy["configRevision"] = saved["configRevision"].clone();
|
||||
assert_eq!(save(legacy).await.unwrap().status(), StatusCode::OK);
|
||||
body["openaiOauthGuardianReviewModel"] = json!(true);
|
||||
body["openaiRequestCountry"] = json!({"mode":"passthrough"});
|
||||
assert_eq!(
|
||||
save(body.clone()).await.unwrap().status(),
|
||||
StatusCode::CONFLICT
|
||||
);
|
||||
let current = response_json(read().await.unwrap()).await["data"].clone();
|
||||
assert_eq!(current["openaiOauthGuardianReviewModel"], false);
|
||||
assert_eq!(
|
||||
current["openaiRequestCountry"],
|
||||
saved["openaiRequestCountry"]
|
||||
);
|
||||
body["configRevision"] = current["configRevision"].clone();
|
||||
let response = save(body).await.unwrap();
|
||||
assert_eq!(response.status(), StatusCode::OK);
|
||||
let changed = response_json(response).await["data"].clone();
|
||||
assert_eq!(changed["openaiOauthGuardianReviewModel"], true);
|
||||
assert_eq!(
|
||||
changed["openaiRequestCountry"],
|
||||
json!({"mode":"passthrough"})
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn openai_request_settings_validate_modes_and_country_codes() {
|
||||
for invalid in [
|
||||
json!(null),
|
||||
json!({"mode":"custom"}),
|
||||
json!({"mode":"custom","country":"ca"}),
|
||||
json!({"mode":"custom","country":"USA"}),
|
||||
json!({"mode":"custom","country":"国"}),
|
||||
json!({"mode":"follow_location","country":"CA"}),
|
||||
json!({"mode":"passthrough","country":"CA"}),
|
||||
json!({"mode":"default","country":"CA"}),
|
||||
json!({"mode":"unknown"}),
|
||||
] {
|
||||
let mut body = update_body();
|
||||
body["openaiRequestCountry"] = invalid.clone();
|
||||
if let Ok(request) = serde_json::from_value::<UpdateRuntimeSettingsRequest>(body) {
|
||||
assert!(request.validate().is_err(), "{invalid}");
|
||||
}
|
||||
}
|
||||
for invalid in [json!(null), json!("true"), json!(1)] {
|
||||
let mut body = update_body();
|
||||
body["openaiOauthGuardianReviewModel"] = invalid;
|
||||
assert!(serde_json::from_value::<UpdateRuntimeSettingsRequest>(body).is_err());
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn guardian_reservation_round_trips_and_rejects_invalid_values() {
|
||||
let fixture = AdminTestFixture::new().await;
|
||||
|
||||
@@ -34,6 +34,215 @@ fn generate_request(body: Value) -> DecodedGenerationRequest {
|
||||
decode_request(body.to_string().as_bytes()).expect("test request should decode")
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn guardian_roles_should_use_complete_current_request_identity_before_header_projection() {
|
||||
use gateway_protocol::openai::{CodexGuardianRole::*, codex_guardian_role};
|
||||
|
||||
let review = r#"{"thread_source":"guardian_review"}"#;
|
||||
let classification =
|
||||
r#"{"thread_source":"guardian_classifier","turn_trigger":"guardian_classifier"}"#;
|
||||
let housekeeping = r#"{"turn_trigger":"guardian_housekeeper"}"#;
|
||||
for (websocket, body_agent, body_turn, header_agent, header_turn, explicit, expected) in [
|
||||
(
|
||||
false,
|
||||
Some("guardian"),
|
||||
Some(review),
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
Some(Reviewer),
|
||||
),
|
||||
(
|
||||
false,
|
||||
None,
|
||||
None,
|
||||
Some("guardian"),
|
||||
Some(review),
|
||||
None,
|
||||
Some(Reviewer),
|
||||
),
|
||||
(
|
||||
false,
|
||||
None,
|
||||
Some(review),
|
||||
Some("guardian"),
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
),
|
||||
(
|
||||
false,
|
||||
Some("other"),
|
||||
Some(review),
|
||||
Some("guardian"),
|
||||
Some(review),
|
||||
None,
|
||||
None,
|
||||
),
|
||||
(
|
||||
false,
|
||||
Some("guardian"),
|
||||
Some(classification),
|
||||
Some("guardian"),
|
||||
Some(review),
|
||||
None,
|
||||
None,
|
||||
),
|
||||
(
|
||||
false,
|
||||
Some("guardian"),
|
||||
Some("invalid"),
|
||||
Some("guardian"),
|
||||
Some(review),
|
||||
None,
|
||||
None,
|
||||
),
|
||||
(
|
||||
false,
|
||||
Some("guardian"),
|
||||
Some(review),
|
||||
None,
|
||||
None,
|
||||
Some("classifier"),
|
||||
None,
|
||||
),
|
||||
(false, None, None, None, None, Some("reviewer"), None),
|
||||
(
|
||||
false,
|
||||
Some("guardian"),
|
||||
Some(classification),
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
Some(Classifier),
|
||||
),
|
||||
(
|
||||
false,
|
||||
Some("guardian"),
|
||||
Some(housekeeping),
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
Some(Housekeeper),
|
||||
),
|
||||
(
|
||||
true,
|
||||
None,
|
||||
None,
|
||||
Some("guardian"),
|
||||
Some(review),
|
||||
Some("reviewer"),
|
||||
None,
|
||||
),
|
||||
(
|
||||
true,
|
||||
None,
|
||||
Some(review),
|
||||
Some("guardian"),
|
||||
Some(review),
|
||||
None,
|
||||
None,
|
||||
),
|
||||
(
|
||||
true,
|
||||
Some("guardian"),
|
||||
Some(classification),
|
||||
Some("stale_agent"),
|
||||
Some(review),
|
||||
Some("reviewer"),
|
||||
Some(Classifier),
|
||||
),
|
||||
(
|
||||
true,
|
||||
Some("guardian"),
|
||||
Some(review),
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
Some(Reviewer),
|
||||
),
|
||||
] {
|
||||
let mut body =
|
||||
json!({"model":"gpt-5.6-luna","input":[],"generate":false,"client_metadata":{}});
|
||||
if let Some(agent) = body_agent {
|
||||
body["client_metadata"]["x-openai-subagent"] = json!(agent);
|
||||
}
|
||||
if let Some(turn) = body_turn {
|
||||
body["client_metadata"]["x-codex-turn-metadata"] = json!(turn);
|
||||
}
|
||||
let mut headers = HeaderMap::new();
|
||||
for (name, value) in [
|
||||
("x-openai-subagent", header_agent),
|
||||
("x-codex-turn-metadata", header_turn),
|
||||
("x-codex-guardian", explicit),
|
||||
] {
|
||||
if let Some(value) = value {
|
||||
headers.insert(name, HeaderValue::from_str(value).unwrap());
|
||||
}
|
||||
}
|
||||
let decoded = if websocket {
|
||||
body["type"] = json!("response.create");
|
||||
decode_response_create_with_context(
|
||||
&body.to_string(),
|
||||
&OpenAiRequestHeaders::from_headers(&headers),
|
||||
)
|
||||
.unwrap()
|
||||
} else {
|
||||
decode_request_with_headers(body.to_string().as_bytes(), &headers, 64 * 1024).unwrap()
|
||||
};
|
||||
let payload = generate_operation(&decoded).protocol_payload();
|
||||
if !websocket && matches!(expected, Some(Classifier | Housekeeper)) {
|
||||
assert!(!payload.context().contains_key("use_websocket"));
|
||||
assert!(
|
||||
!payload
|
||||
.context()
|
||||
.contains_key(gateway_protocol::openai::WEBSOCKET_FRAME_CONTEXT_KEY)
|
||||
);
|
||||
}
|
||||
if websocket && header_agent == Some("stale_agent") {
|
||||
assert_eq!(
|
||||
payload.body()["client_metadata"]["x-openai-subagent"],
|
||||
"guardian"
|
||||
);
|
||||
let opaque = payload.context()["opaque_request_headers"]
|
||||
.as_array()
|
||||
.unwrap();
|
||||
assert!(!opaque.iter().any(|pair| matches!(
|
||||
pair[0].as_str(),
|
||||
Some("x-codex-guardian" | "x-openai-subagent")
|
||||
)));
|
||||
}
|
||||
assert_eq!(
|
||||
codex_guardian_role(payload.body(), payload.context()),
|
||||
expected,
|
||||
"body={body}, headers={headers:?}"
|
||||
);
|
||||
}
|
||||
let mut repeated = HeaderMap::new();
|
||||
repeated.append("x-openai-subagent", HeaderValue::from_static("guardian"));
|
||||
repeated.append("x-openai-subagent", HeaderValue::from_static("other"));
|
||||
repeated.insert("x-codex-turn-metadata", HeaderValue::from_static(review));
|
||||
let decoded = decode_request_with_headers(
|
||||
br#"{"model":"gpt-5.6-luna","input":[]}"#,
|
||||
&repeated,
|
||||
64 * 1024,
|
||||
)
|
||||
.unwrap();
|
||||
let payload = generate_operation(&decoded).protocol_payload();
|
||||
assert_eq!(codex_guardian_role(payload.body(), payload.context()), None);
|
||||
for malformed in [
|
||||
r#"{"thread_source":"guardian_classifier","thread_source":"guardian_review"}"#,
|
||||
r#"{"thread_source":null}"#,
|
||||
r#"["guardian_review"]"#,
|
||||
] {
|
||||
let mut body = json!({"model":"gpt-5.6-luna","input":[],"client_metadata":{"x-openai-subagent":"guardian"}});
|
||||
body["client_metadata"]["x-codex-turn-metadata"] = json!(malformed);
|
||||
let decoded = generate_request(body);
|
||||
let payload = generate_operation(&decoded).protocol_payload();
|
||||
assert_eq!(codex_guardian_role(payload.body(), payload.context()), None);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn decoder_should_attach_only_identified_codex_inference_turns() {
|
||||
let body = json!({"model":"smart-code", "input":"hello", "client_metadata": {
|
||||
|
||||
@@ -536,6 +536,14 @@ async fn errors_on_a_reused_connection_keep_their_own_upstream_ids() {
|
||||
"x-codex-turn-metadata",
|
||||
axum::http::HeaderValue::from_static(r#"{"request_kind":"turn"}"#),
|
||||
);
|
||||
opening.insert(
|
||||
"x-openai-subagent",
|
||||
axum::http::HeaderValue::from_static("guardian"),
|
||||
);
|
||||
opening.insert(
|
||||
"x-codex-guardian",
|
||||
axum::http::HeaderValue::from_static("reviewer"),
|
||||
);
|
||||
let (mut socket, _server) = connect_with_headers(Arc::clone(&trace), Vec::new(), opening).await;
|
||||
for request_id in ["req_first", "req_second"] {
|
||||
send_request(&mut socket).await;
|
||||
@@ -548,9 +556,11 @@ async fn errors_on_a_reused_connection_keep_their_own_upstream_ids() {
|
||||
[
|
||||
[
|
||||
Some("opening".to_owned()),
|
||||
Some(r#"{"request_kind":"turn"}"#.to_owned())
|
||||
Some(r#"{"request_kind":"turn"}"#.to_owned()),
|
||||
Some("guardian".to_owned()),
|
||||
Some("incomplete".to_owned()),
|
||||
],
|
||||
[None, None]
|
||||
[None, None, None, None]
|
||||
]
|
||||
);
|
||||
assert_no_duplicate_failure(&mut socket).await;
|
||||
|
||||
@@ -318,7 +318,7 @@ struct AtomicFailureTrace {
|
||||
committed: AtomicBool,
|
||||
finalized: AtomicBool,
|
||||
downstream_websocket_connection_ids: Mutex<Vec<String>>,
|
||||
request_turn_headers: Mutex<Vec<[Option<String>; 2]>>,
|
||||
request_turn_headers: Mutex<Vec<[Option<String>; 4]>>,
|
||||
initial_errors: Mutex<std::collections::VecDeque<EngineError>>,
|
||||
first_batch: Mutex<Option<CoordinatedEvent>>,
|
||||
session_drops: Arc<AtomicUsize>,
|
||||
@@ -450,8 +450,13 @@ impl ExecutionService for AtomicFailureExecution {
|
||||
{
|
||||
let context = generate.protocol_payload().context();
|
||||
self.trace.request_turn_headers.lock().unwrap().push(
|
||||
["turn_state", "turn_metadata"]
|
||||
.map(|key| context.get(key).and_then(Value::as_str).map(str::to_owned)),
|
||||
[
|
||||
"turn_state",
|
||||
"turn_metadata",
|
||||
"subagent",
|
||||
"codex_guardian_identity",
|
||||
]
|
||||
.map(|key| context.get(key).and_then(Value::as_str).map(str::to_owned)),
|
||||
);
|
||||
self.trace
|
||||
.downstream_websocket_connection_ids
|
||||
|
||||
@@ -11,6 +11,31 @@ pub struct RequestLocation {
|
||||
pub timezone: chrono_tz::Tz,
|
||||
}
|
||||
|
||||
/// 独立选择 OpenAI 请求的国家字段,仅自定义模式需要国家代码
|
||||
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
|
||||
#[serde(tag = "mode", rename_all = "snake_case", deny_unknown_fields)]
|
||||
pub enum OpenAiRequestCountry {
|
||||
Default {},
|
||||
Passthrough {},
|
||||
FollowLocation {},
|
||||
Custom { country: String },
|
||||
}
|
||||
|
||||
impl Default for OpenAiRequestCountry {
|
||||
fn default() -> Self {
|
||||
Self::FollowLocation {}
|
||||
}
|
||||
}
|
||||
|
||||
impl OpenAiRequestCountry {
|
||||
pub fn validate(&self) -> Result<(), InvalidRequestLocation> {
|
||||
if let Self::Custom { country } = self {
|
||||
validate_country(country)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
|
||||
pub enum InvalidRequestLocation {
|
||||
#[error("country must contain two uppercase ASCII letters")]
|
||||
@@ -34,9 +59,7 @@ impl Default for RequestLocation {
|
||||
|
||||
impl RequestLocation {
|
||||
pub fn validate(&self) -> Result<(), InvalidRequestLocation> {
|
||||
if self.country.len() != 2 || !self.country.bytes().all(|byte| byte.is_ascii_uppercase()) {
|
||||
return Err(InvalidRequestLocation::Country);
|
||||
}
|
||||
validate_country(&self.country)?;
|
||||
for (value, error) in [
|
||||
(&self.region, InvalidRequestLocation::Region),
|
||||
(&self.city, InvalidRequestLocation::City),
|
||||
@@ -59,3 +82,11 @@ impl RequestLocation {
|
||||
Ok(self)
|
||||
}
|
||||
}
|
||||
|
||||
fn validate_country(country: &str) -> Result<(), InvalidRequestLocation> {
|
||||
if country.len() == 2 && country.bytes().all(|byte| byte.is_ascii_uppercase()) {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(InvalidRequestLocation::Country)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ pub use error_protection::*;
|
||||
mod fast_mode;
|
||||
pub use fast_mode::FastMode;
|
||||
mod location;
|
||||
pub use location::{InvalidRequestLocation, RequestLocation};
|
||||
pub use location::{InvalidRequestLocation, OpenAiRequestCountry, RequestLocation};
|
||||
mod model;
|
||||
mod model_access;
|
||||
pub use model_access::{
|
||||
|
||||
@@ -1171,6 +1171,7 @@ where
|
||||
.with_fast_mode(self.plan.fast_mode())
|
||||
.with_pricing(self.plan.pricing())
|
||||
.with_request_location(self.plan.request_location().cloned())
|
||||
.with_openai_request_country(self.plan.openai_request_country().clone())
|
||||
.with_privacy(self.plan.privacy())
|
||||
.with_request_body_limit(self.plan.request_body_limit())
|
||||
.with_concurrency_wait_budget(self.concurrency_wait_budget.clone())
|
||||
|
||||
@@ -21,7 +21,7 @@ use crate::{
|
||||
concurrency::{CapacityWait, ConcurrencyWaitBudget, ConcurrencyWaitQueue},
|
||||
engine::{
|
||||
AttemptCoordinator, EngineError, ExecutionStore, GatewayEngine, ModelRequestId,
|
||||
NewModelRequest, ProbeFailure, ProviderAccountId, UpstreamSendState,
|
||||
ModelRouteScope, NewModelRequest, ProbeFailure, ProviderAccountId, UpstreamSendState,
|
||||
admission::{
|
||||
ClientAdmissionDecision, ClientAdmissionPort, ClientAdmissionRejection,
|
||||
ClientAdmissionRequest,
|
||||
@@ -62,7 +62,7 @@ use crate::{
|
||||
policy::{ClientApiKeyId, ClientPolicy},
|
||||
provider_ports::{ProviderSessionAffinityPort, ProviderStoreErrorKind},
|
||||
routing::{
|
||||
FrozenAccountScope, ProviderCatalogUnavailable, ProviderModelContent,
|
||||
DirectAccountModel, FrozenAccountScope, ProviderCatalogUnavailable, ProviderModelContent,
|
||||
PublicModelDescriptor, PublicModelId, RoutingContext, RuntimeSnapshot, UpstreamModelId,
|
||||
request_settings::RequestSettings,
|
||||
},
|
||||
@@ -508,6 +508,48 @@ impl DefaultExecutionService {
|
||||
}
|
||||
routing_context.required_provider = Some(provider);
|
||||
}
|
||||
let continuation = match request.metadata.previous_response_id.as_ref() {
|
||||
Some(previous) => {
|
||||
let resolve = self
|
||||
.continuation
|
||||
.resolve(request.client.policy.key_id(), previous)
|
||||
.fuse();
|
||||
let timeout = Delay::new(COORDINATION_TIMEOUT).fuse();
|
||||
pin_mut!(resolve, timeout);
|
||||
let pin = select_biased! {
|
||||
result = resolve => match result {
|
||||
Ok(pin) => pin,
|
||||
Err(error)
|
||||
if error.kind() == NativeContinuationStoreErrorKind::Unavailable =>
|
||||
{
|
||||
tracing::debug!(request_id = request_id.as_str(), %error, "Continuation affinity 查询失败,退化为外部续接");
|
||||
None
|
||||
}
|
||||
Err(error)
|
||||
if error.kind() == NativeContinuationStoreErrorKind::OwnershipMismatch =>
|
||||
{
|
||||
return Err(GatewayError::new(GatewayErrorKind::PolicyDenied, "Continuation does not belong to this client API key."));
|
||||
}
|
||||
Err(error) => {
|
||||
tracing::warn!(request_id = request_id.as_str(), %error, "Continuation affinity 记录无效,已拒绝续接");
|
||||
return Err(GatewayError::new(GatewayErrorKind::Internal, "Continuation state is invalid."));
|
||||
}
|
||||
},
|
||||
_ = timeout => None,
|
||||
};
|
||||
Some(match pin {
|
||||
Some(pin) if !pin.matches_client(request.client.policy.key_id()) => {
|
||||
return Err(GatewayError::new(GatewayErrorKind::PolicyDenied, "Continuation does not belong to this client API key."));
|
||||
}
|
||||
Some(pin) if !authorization.account_scope.allows(pin.account()) => {
|
||||
return Err(GatewayError::new(GatewayErrorKind::PolicyDenied, "Continuation account is outside the client account scope."));
|
||||
}
|
||||
Some(pin) => ContinuationBinding::Pinned(pin),
|
||||
None => ContinuationBinding::External(previous.clone()),
|
||||
})
|
||||
}
|
||||
None => None,
|
||||
};
|
||||
let account_scope = Arc::clone(&authorization.account_scope);
|
||||
let plan = match &request.target {
|
||||
ExecutionTarget::ProviderEndpoint {
|
||||
@@ -574,65 +616,66 @@ impl DefaultExecutionService {
|
||||
}
|
||||
}
|
||||
}
|
||||
request.client.snapshot.plan(
|
||||
&target_model,
|
||||
&request.operation,
|
||||
account_scope,
|
||||
&target_context,
|
||||
)
|
||||
let pin = continuation.as_ref().and_then(ContinuationBinding::pinned);
|
||||
let extension_route = ModelRouteScope {
|
||||
client_api_key: request.client.policy.key_id(),
|
||||
account_groups: &account_group_ids,
|
||||
extensions: &authorization.extension_scope,
|
||||
model: public_model.as_str(),
|
||||
};
|
||||
let direct = request.client.snapshot
|
||||
.available_provider_kinds(&account_scope, &target_context)
|
||||
.find_map(|kind| {
|
||||
let provider = self.providers.get(kind)?;
|
||||
let previous = pin.filter(|pin| pin.provider() == kind)
|
||||
.and_then(NativeContinuationPin::session_state);
|
||||
let model = provider.direct_account_model(
|
||||
&target_model,
|
||||
&request.operation,
|
||||
previous,
|
||||
request.client.snapshot.settings(),
|
||||
)?;
|
||||
middleware.as_ref()
|
||||
.is_none_or(|plan| plan.preserves_model_bindings(extension_route, kind, &model))
|
||||
.then(|| (kind.clone(), model))
|
||||
});
|
||||
if let Some((kind, model)) = direct {
|
||||
let account_scope = match authorization.required_account.as_ref() {
|
||||
Some(id) => Arc::new(account_scope.restricted_to(&Default::default(), &[id.clone()].into())),
|
||||
None => account_scope,
|
||||
};
|
||||
request.client.snapshot.plan_with_direct_account_model(
|
||||
&target_model,
|
||||
&request.operation,
|
||||
account_scope,
|
||||
&target_context,
|
||||
DirectAccountModel {
|
||||
provider: &kind,
|
||||
model: &model,
|
||||
continuation_owner: pin.map(NativeContinuationPin::account),
|
||||
},
|
||||
|account| upstream_adapters.as_ref().is_none_or(|plan| {
|
||||
plan.uses_native_upstream(extension_route, &kind, account)
|
||||
}),
|
||||
)
|
||||
} else {
|
||||
request.client.snapshot.plan(
|
||||
&target_model,
|
||||
&request.operation,
|
||||
account_scope,
|
||||
&target_context,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
.map_err(map_routing_error)?;
|
||||
let continuation = match request.metadata.previous_response_id.as_ref() {
|
||||
Some(previous) => {
|
||||
let resolve = self
|
||||
.continuation
|
||||
.resolve(request.client.policy.key_id(), previous)
|
||||
.fuse();
|
||||
let timeout = Delay::new(COORDINATION_TIMEOUT).fuse();
|
||||
pin_mut!(resolve, timeout);
|
||||
let pin = select_biased! {
|
||||
result = resolve => match result {
|
||||
Ok(pin) => pin,
|
||||
Err(error)
|
||||
if error.kind() == NativeContinuationStoreErrorKind::Unavailable =>
|
||||
{
|
||||
tracing::debug!(request_id = request_id.as_str(), %error, "Continuation affinity 查询失败,退化为外部续接");
|
||||
None
|
||||
}
|
||||
Err(error)
|
||||
if error.kind() == NativeContinuationStoreErrorKind::OwnershipMismatch =>
|
||||
{
|
||||
return Err(GatewayError::new(GatewayErrorKind::PolicyDenied, "Continuation does not belong to this client API key."));
|
||||
}
|
||||
Err(error) => {
|
||||
tracing::warn!(request_id = request_id.as_str(), %error, "Continuation affinity 记录无效,已拒绝续接");
|
||||
return Err(GatewayError::new(GatewayErrorKind::Internal, "Continuation state is invalid."));
|
||||
}
|
||||
},
|
||||
_ = timeout => None,
|
||||
};
|
||||
Some(match pin {
|
||||
Some(pin) if !pin.matches_client(request.client.policy.key_id()) => {
|
||||
return Err(GatewayError::new(GatewayErrorKind::PolicyDenied, "Continuation does not belong to this client API key."));
|
||||
}
|
||||
Some(pin) if !authorization.account_scope.allows(pin.account()) => {
|
||||
return Err(GatewayError::new(GatewayErrorKind::PolicyDenied, "Continuation account is outside the client account scope."));
|
||||
}
|
||||
Some(pin)
|
||||
if !plan.candidates().iter().any(|candidate| candidate.provider() == pin.provider()) =>
|
||||
{
|
||||
return Err(GatewayError::new(GatewayErrorKind::NoAvailableProvider, "Continuation provider is not available."));
|
||||
}
|
||||
Some(pin) => {
|
||||
attach_continuation_session_state(&mut request.operation, &pin);
|
||||
ContinuationBinding::Pinned(pin)
|
||||
}
|
||||
None => ContinuationBinding::External(previous.clone()),
|
||||
})
|
||||
if let Some(pin) = continuation.as_ref().and_then(ContinuationBinding::pinned) {
|
||||
if !plan.candidates().iter().any(|candidate| candidate.provider() == pin.provider()
|
||||
&& candidate.account_scope().allows(pin.account())) {
|
||||
return Err(GatewayError::new(GatewayErrorKind::NoAvailableProvider, "Continuation provider is not available."));
|
||||
}
|
||||
None => None,
|
||||
};
|
||||
attach_continuation_session_state(&mut request.operation, pin);
|
||||
}
|
||||
if start_guard.is_none() {
|
||||
start_guard = Some(
|
||||
self.prepare_execution_start(&request, &request_id, &mut authorization)
|
||||
|
||||
@@ -9,6 +9,16 @@ use std::{fmt, sync::Arc};
|
||||
|
||||
/// 一个发布代次冻结的统一数据面中间件计划
|
||||
pub trait MiddlewarePlan: Send + Sync + fmt::Debug {
|
||||
/// 只在能证明模型变更保留所有已绑定处理器时允许原生模型替换。
|
||||
fn preserves_model_bindings(
|
||||
&self,
|
||||
_route: crate::engine::ModelRouteScope<'_>,
|
||||
_provider: &crate::routing::ProviderKind,
|
||||
_target: &crate::routing::UpstreamModelId,
|
||||
) -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
fn has_outbound(&self, _context: &MiddlewareContext) -> bool {
|
||||
false
|
||||
}
|
||||
@@ -85,6 +95,16 @@ pub struct FrozenMiddlewarePlan {
|
||||
}
|
||||
|
||||
impl FrozenMiddlewarePlan {
|
||||
#[must_use]
|
||||
pub fn preserves_model_bindings(
|
||||
&self,
|
||||
route: crate::engine::ModelRouteScope<'_>,
|
||||
provider: &crate::routing::ProviderKind,
|
||||
target: &crate::routing::UpstreamModelId,
|
||||
) -> bool {
|
||||
self.plan.preserves_model_bindings(route, provider, target)
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn has_outbound(&self, context: &MiddlewareContext) -> bool {
|
||||
self.plan.has_outbound(context)
|
||||
|
||||
@@ -285,6 +285,31 @@ impl ContinuationAttempt {
|
||||
}
|
||||
}
|
||||
|
||||
/// 插件模型绑定使用同一组 Key、分组、调用链与模型事实。
|
||||
#[derive(Clone, Copy)]
|
||||
pub struct ModelRouteScope<'a> {
|
||||
pub client_api_key: &'a crate::policy::ClientApiKeyId,
|
||||
pub account_groups: &'a [crate::routing::AccountGroupId],
|
||||
pub extensions: &'a extensions::ExtensionCallScope,
|
||||
pub model: &'a str,
|
||||
}
|
||||
|
||||
impl<'a> ModelRouteScope<'a> {
|
||||
fn for_attempt(
|
||||
context: &'a AttemptContext,
|
||||
upstream: &'a crate::routing::UpstreamModelId,
|
||||
) -> Self {
|
||||
Self {
|
||||
client_api_key: context.client_api_key_ref(),
|
||||
account_groups: context.account_group_ids(),
|
||||
extensions: context.extension_scope(),
|
||||
model: context
|
||||
.requested_model()
|
||||
.map_or(upstream.as_str(), |model| model.as_str()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Provider 每次执行可见的 request-local context
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct RequestAttemptContext {
|
||||
@@ -295,6 +320,7 @@ pub struct RequestAttemptContext {
|
||||
pricing: Arc<crate::metering::PricingOverrides>,
|
||||
request_profile: Option<crate::account::OpaqueProviderData>,
|
||||
fast_mode: FastMode,
|
||||
openai_request_country: crate::account::OpenAiRequestCountry,
|
||||
request_location: Option<crate::account::RequestLocation>,
|
||||
privacy: Option<Arc<dyn crate::settings::privacy::CompiledPrivacyPolicy>>,
|
||||
request_id: ModelRequestId,
|
||||
@@ -392,6 +418,15 @@ impl RequestAttemptContext {
|
||||
self
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn with_openai_request_country(
|
||||
mut self,
|
||||
country: crate::account::OpenAiRequestCountry,
|
||||
) -> Self {
|
||||
self.openai_request_country = country;
|
||||
self
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn with_request_location(
|
||||
mut self,
|
||||
@@ -413,6 +448,7 @@ impl RequestAttemptContext {
|
||||
request_profile: None,
|
||||
pricing: Arc::default(),
|
||||
fast_mode: FastMode::Default,
|
||||
openai_request_country: Default::default(),
|
||||
request_location: None,
|
||||
privacy: None,
|
||||
timing_started_at: Instant::now(),
|
||||
@@ -605,7 +641,7 @@ impl AttemptContext {
|
||||
});
|
||||
self.request.upstream_adapters.as_ref().map_or(
|
||||
Ok(upstream_adapter::UpstreamAdapterSelection::Native),
|
||||
|plan| plan.select(self, provider, model, account),
|
||||
|plan| plan.select(ModelRouteScope::for_attempt(self, model), provider, account),
|
||||
)
|
||||
}
|
||||
|
||||
@@ -620,7 +656,11 @@ impl AttemptContext {
|
||||
.upstream_adapters
|
||||
.as_ref()
|
||||
.map_or(Ok(None), |plan| {
|
||||
match plan.select(self, provider, model, Some(account))? {
|
||||
match plan.select(
|
||||
ModelRouteScope::for_attempt(self, model),
|
||||
provider,
|
||||
Some(account),
|
||||
)? {
|
||||
upstream_adapter::UpstreamAdapterSelection::Native => Ok(None),
|
||||
upstream_adapter::UpstreamAdapterSelection::Adapter(adapter) => {
|
||||
Ok(Some(adapter))
|
||||
@@ -685,6 +725,11 @@ impl AttemptContext {
|
||||
self.request.request_location.as_ref()
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub const fn openai_request_country(&self) -> &crate::account::OpenAiRequestCountry {
|
||||
&self.request.openai_request_country
|
||||
}
|
||||
|
||||
/// 当前 attempt 的诊断关联;克隆后可传给后台 transport 任务
|
||||
#[must_use]
|
||||
pub fn trace(&self) -> crate::diagnostics::TraceContext {
|
||||
|
||||
@@ -1174,6 +1174,17 @@ pub trait Provider: Send + Sync {
|
||||
false
|
||||
}
|
||||
|
||||
/// 请求身份明确时,Provider 可以为自身直连账号声明实际模型。Core 按该模型鉴权并冻结重试范围。
|
||||
fn direct_account_model(
|
||||
&self,
|
||||
_model: &PublicModelId,
|
||||
_operation: &Operation,
|
||||
_previous: Option<&crate::operation::ProviderSessionState>,
|
||||
_settings: &crate::settings::SettingsValues,
|
||||
) -> Option<UpstreamModelId> {
|
||||
None
|
||||
}
|
||||
|
||||
/// Provider 解释自己的隐私规则,Core 仅冻结编译结果
|
||||
fn compile_privacy_policy(
|
||||
&self,
|
||||
|
||||
@@ -14,12 +14,11 @@ use crate::{
|
||||
identity::ProviderKind,
|
||||
metering::{CalculatedCost, Usage},
|
||||
operation::Operation,
|
||||
routing::UpstreamModelId,
|
||||
routing::extensions::ExtensionSetReference,
|
||||
};
|
||||
|
||||
use super::{
|
||||
AttemptContext,
|
||||
AttemptContext, ModelRouteScope,
|
||||
middleware::MiddlewareHeader,
|
||||
provider::{EventStream, ProviderCallMetadata},
|
||||
};
|
||||
@@ -28,9 +27,8 @@ use super::{
|
||||
pub trait UpstreamAdapterPlan: fmt::Debug + Send + Sync {
|
||||
fn select(
|
||||
&self,
|
||||
context: &AttemptContext,
|
||||
context: ModelRouteScope<'_>,
|
||||
provider: &ProviderKind,
|
||||
model: &UpstreamModelId,
|
||||
account: Option<&ProviderAccountId>,
|
||||
) -> Result<UpstreamAdapterSelection, ProviderError>;
|
||||
}
|
||||
@@ -62,14 +60,27 @@ impl FrozenUpstreamAdapterPlan {
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn select(
|
||||
/// 只有已确认为原生执行的账号可用于原生模型替换;命中但不可用的适配器保留其拒绝语义。
|
||||
#[must_use]
|
||||
pub fn uses_native_upstream(
|
||||
&self,
|
||||
context: &AttemptContext,
|
||||
route: ModelRouteScope<'_>,
|
||||
provider: &ProviderKind,
|
||||
account: &ProviderAccountId,
|
||||
) -> bool {
|
||||
matches!(
|
||||
self.select(route, provider, Some(account)),
|
||||
Ok(UpstreamAdapterSelection::Native)
|
||||
)
|
||||
}
|
||||
|
||||
pub(crate) fn select(
|
||||
&self,
|
||||
context: ModelRouteScope<'_>,
|
||||
provider: &ProviderKind,
|
||||
model: &UpstreamModelId,
|
||||
account: Option<&ProviderAccountId>,
|
||||
) -> Result<UpstreamAdapterSelection, ProviderError> {
|
||||
self.plan.select(context, provider, model, account)
|
||||
self.plan.select(context, provider, account)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -532,16 +532,30 @@ pub struct RoutingContext {
|
||||
pub blocked_providers: BTreeSet<ProviderKind>,
|
||||
}
|
||||
|
||||
/// 直连模型及首次原生续接必须使用的账号
|
||||
pub struct DirectAccountModel<'a> {
|
||||
pub provider: &'a ProviderKind,
|
||||
pub model: &'a UpstreamModelId,
|
||||
pub continuation_owner: Option<&'a crate::account::ProviderAccountId>,
|
||||
}
|
||||
|
||||
/// 已绑定 Provider 的请求候选;模型端点携带真实上游模型,原生端点不虚构模型
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct ProviderCandidate {
|
||||
provider: ProviderKind,
|
||||
requires_direct_authentication: bool,
|
||||
upstream_model: Option<UpstreamModelId>,
|
||||
emulated_features: BTreeSet<Feature>,
|
||||
account_scope: Arc<FrozenAccountScope>,
|
||||
}
|
||||
|
||||
impl ProviderCandidate {
|
||||
/// 模型按直连账号身份选择,Provider 必须在发送前核对实际凭据。
|
||||
#[must_use]
|
||||
pub const fn requires_direct_authentication(&self) -> bool {
|
||||
self.requires_direct_authentication
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub const fn provider(&self) -> &ProviderKind {
|
||||
&self.provider
|
||||
@@ -571,6 +585,7 @@ pub struct RoutingPlan {
|
||||
request_body_limit: usize,
|
||||
max_retry_after: std::time::Duration,
|
||||
pricing: Arc<crate::metering::PricingOverrides>,
|
||||
openai_request_country: crate::account::OpenAiRequestCountry,
|
||||
request_location: Option<crate::account::RequestLocation>,
|
||||
privacy: Option<Arc<dyn crate::settings::privacy::CompiledPrivacyPolicy>>,
|
||||
config_revision: ConfigRevision,
|
||||
@@ -625,6 +640,11 @@ impl RoutingPlan {
|
||||
self.request_location.as_ref()
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub const fn openai_request_country(&self) -> &crate::account::OpenAiRequestCountry {
|
||||
&self.openai_request_country
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub const fn config_revision(&self) -> ConfigRevision {
|
||||
self.config_revision
|
||||
|
||||
@@ -17,10 +17,10 @@ use crate::settings::{SettingsValues, compiled::CompiledSettings};
|
||||
use crate::validation::RoutingError;
|
||||
|
||||
use super::{
|
||||
AccountGroupId, ClientRoutingScope, ConfigRevision, FrozenAccountScope, ModelCapabilities,
|
||||
ProviderCandidate, ProviderCatalogGeneration, ProviderCatalogPort, ProviderKind, ProviderModel,
|
||||
PublicModelId, RoutingContext, RoutingGroupSnapshot, RoutingPlan, RuntimeAccount,
|
||||
RuntimeAccountDirectory, UpstreamModelId,
|
||||
AccountGroupId, ClientRoutingScope, ConfigRevision, DirectAccountModel, FrozenAccountScope,
|
||||
ModelCapabilities, ProviderCandidate, ProviderCatalogGeneration, ProviderCatalogPort,
|
||||
ProviderKind, ProviderModel, PublicModelId, RoutingContext, RoutingGroupSnapshot, RoutingPlan,
|
||||
RuntimeAccount, RuntimeAccountDirectory, UpstreamModelId,
|
||||
};
|
||||
|
||||
const MAXIMUM_SNAPSHOT_STABILITY_ATTEMPTS: usize = 4;
|
||||
@@ -1288,21 +1288,26 @@ impl RuntimeSnapshot {
|
||||
account_scope: &FrozenAccountScope,
|
||||
context: &RoutingContext,
|
||||
) -> BTreeSet<ProviderKind> {
|
||||
account_scope
|
||||
.provider_kinds()
|
||||
.iter()
|
||||
.filter(|provider| {
|
||||
self.providers.contains(*provider)
|
||||
&& context
|
||||
.required_provider
|
||||
.as_ref()
|
||||
.is_none_or(|required| required == *provider)
|
||||
&& !context.blocked_providers.contains(*provider)
|
||||
})
|
||||
self.available_provider_kinds(account_scope, context)
|
||||
.cloned()
|
||||
.collect()
|
||||
}
|
||||
|
||||
pub(crate) fn available_provider_kinds<'a>(
|
||||
&'a self,
|
||||
account_scope: &'a FrozenAccountScope,
|
||||
context: &'a RoutingContext,
|
||||
) -> impl Iterator<Item = &'a ProviderKind> {
|
||||
account_scope.provider_kinds().iter().filter(|provider| {
|
||||
self.providers.contains(*provider)
|
||||
&& context
|
||||
.required_provider
|
||||
.as_ref()
|
||||
.is_none_or(|required| required == *provider)
|
||||
&& !context.blocked_providers.contains(*provider)
|
||||
})
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn min_codex_client_versions(&self) -> &CodexClientMinVersions {
|
||||
&self.settings.min_codex_client_versions
|
||||
@@ -1315,7 +1320,67 @@ impl RuntimeSnapshot {
|
||||
account_scope: Arc<FrozenAccountScope>,
|
||||
context: &RoutingContext,
|
||||
) -> Result<RoutingPlan, RoutingError> {
|
||||
self.plan_inner(public_model, operation, account_scope, context, true)
|
||||
self.plan_inner(public_model, operation, account_scope, context, true, None)
|
||||
}
|
||||
|
||||
/// 按实际目标分别校验目录、分组和账号。选中直连或常规路径后,重试沿用同一份计划。
|
||||
pub fn plan_with_direct_account_model(
|
||||
&self,
|
||||
public_model: &PublicModelId,
|
||||
operation: &Operation,
|
||||
scope: Arc<FrozenAccountScope>,
|
||||
context: &RoutingContext,
|
||||
direct: DirectAccountModel<'_>,
|
||||
uses_native_upstream: impl Fn(&ProviderAccountId) -> bool,
|
||||
) -> Result<RoutingPlan, RoutingError> {
|
||||
let DirectAccountModel {
|
||||
provider,
|
||||
model: target,
|
||||
continuation_owner,
|
||||
} = direct;
|
||||
if self.model_alias(public_model.as_str()).is_some()
|
||||
|| self
|
||||
.settings
|
||||
.values
|
||||
.model_mappings
|
||||
.contains_key(public_model.as_str())
|
||||
{
|
||||
return self.plan(public_model, operation, scope, context);
|
||||
}
|
||||
// 自动替换必须有原生目录依据;未替换的账号仍使用普通请求的路由合同。
|
||||
let sources = self
|
||||
.model_catalog_accounts
|
||||
.get(provider)
|
||||
.and_then(|models| models.get(target.as_str()));
|
||||
let direct_accounts = scope
|
||||
.allowed_account_ids()
|
||||
.filter(|id| {
|
||||
scope.directory().account(id).is_some_and(|account| {
|
||||
account.provider_kind() == provider && account.is_direct_account()
|
||||
}) && scope.allows_model(id, target.as_str())
|
||||
&& sources.is_none_or(|accounts| accounts.contains(*id))
|
||||
&& uses_native_upstream(id)
|
||||
})
|
||||
.cloned()
|
||||
.collect::<BTreeSet<_>>();
|
||||
// 原生续接先用原账号,容量恢复仍可在同模型的授权账号间换号
|
||||
if !direct_accounts.is_empty()
|
||||
&& continuation_owner.is_none_or(|owner| direct_accounts.contains(owner))
|
||||
&& let Ok(plan) = self.plan_inner(
|
||||
public_model,
|
||||
operation,
|
||||
Arc::new(
|
||||
scope.restricted_to(&BTreeSet::from([provider.clone()]), &direct_accounts),
|
||||
),
|
||||
context,
|
||||
true,
|
||||
Some(target),
|
||||
)
|
||||
{
|
||||
return Ok(plan);
|
||||
}
|
||||
// 只有尚未开始任何 attempt 的请求才可保留原模型;两个模型从不进入同一重试计划。
|
||||
self.plan(public_model, operation, scope, context)
|
||||
}
|
||||
|
||||
/// 管理探测已固定目标账号,不使用数据面 Key 的账号模型政策裁决探测资格
|
||||
@@ -1331,7 +1396,14 @@ impl RuntimeSnapshot {
|
||||
.as_ref()
|
||||
.clone()
|
||||
.with_request_profiles(self.settings().request_profiles().clone());
|
||||
self.plan_inner(public_model, operation, Arc::new(scope), context, false)
|
||||
self.plan_inner(
|
||||
public_model,
|
||||
operation,
|
||||
Arc::new(scope),
|
||||
context,
|
||||
false,
|
||||
None,
|
||||
)
|
||||
}
|
||||
|
||||
fn plan_inner(
|
||||
@@ -1341,6 +1413,7 @@ impl RuntimeSnapshot {
|
||||
account_scope: Arc<FrozenAccountScope>,
|
||||
context: &RoutingContext,
|
||||
enforce_account_model_policy: bool,
|
||||
direct_model: Option<&UpstreamModelId>,
|
||||
) -> Result<RoutingPlan, RoutingError> {
|
||||
let requirements = operation.capability_requirements();
|
||||
let mut candidates = Vec::new();
|
||||
@@ -1350,19 +1423,23 @@ impl RuntimeSnapshot {
|
||||
}
|
||||
|
||||
let requested_model = public_model.as_str();
|
||||
let mapped_model = self.mapped_model(requested_model);
|
||||
let upstream_model = if self
|
||||
.settings
|
||||
.values
|
||||
.model_mappings
|
||||
.contains_key(requested_model)
|
||||
|| self.model_alias(requested_model).is_some()
|
||||
{
|
||||
UpstreamModelId::new(mapped_model)
|
||||
let upstream_model = if let Some(direct) = direct_model {
|
||||
direct.clone()
|
||||
} else {
|
||||
UpstreamModelId::from_client_wire(mapped_model)
|
||||
}
|
||||
.map_err(|_| RoutingError::InvalidIdentifier)?;
|
||||
let mapped_model = self.mapped_model(requested_model);
|
||||
if self
|
||||
.settings
|
||||
.values
|
||||
.model_mappings
|
||||
.contains_key(requested_model)
|
||||
|| self.model_alias(requested_model).is_some()
|
||||
{
|
||||
UpstreamModelId::new(mapped_model)
|
||||
} else {
|
||||
UpstreamModelId::from_client_wire(mapped_model)
|
||||
}
|
||||
.map_err(|_| RoutingError::InvalidIdentifier)?
|
||||
};
|
||||
if enforce_account_model_policy
|
||||
&& account_scope.groups_deny_model(Some(upstream_model.as_str()))
|
||||
{
|
||||
@@ -1383,9 +1460,10 @@ impl RuntimeSnapshot {
|
||||
if !self.providers.contains(provider) {
|
||||
continue;
|
||||
}
|
||||
if self
|
||||
.model_alias(public_model.as_str())
|
||||
.is_some_and(|alias| &alias.provider != provider)
|
||||
if direct_model.is_none()
|
||||
&& self
|
||||
.model_alias(public_model.as_str())
|
||||
.is_some_and(|alias| &alias.provider != provider)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
@@ -1407,10 +1485,18 @@ impl RuntimeSnapshot {
|
||||
.provider_models
|
||||
.get(provider)
|
||||
.and_then(|models| models.get(&upstream_model));
|
||||
// 直连模型需要原生能力,不能借用插件为同名模型声明的账号与能力。
|
||||
if direct_model.is_some() && capabilities.is_none() {
|
||||
continue;
|
||||
}
|
||||
let contributions: Vec<_> = self
|
||||
.contributed_models()
|
||||
.iter()
|
||||
.filter(|model| &model.provider == provider && &model.id == public_model)
|
||||
.filter(|model| {
|
||||
direct_model.is_none()
|
||||
&& &model.provider == provider
|
||||
&& &model.id == public_model
|
||||
})
|
||||
.collect();
|
||||
let mut candidate_scope = Arc::clone(&account_scope);
|
||||
let emulated_features = if contributions.is_empty() {
|
||||
@@ -1467,6 +1553,7 @@ impl RuntimeSnapshot {
|
||||
native.unwrap_or_default()
|
||||
};
|
||||
candidates.push(ProviderCandidate {
|
||||
requires_direct_authentication: direct_model.is_some(),
|
||||
provider: provider.clone(),
|
||||
upstream_model: Some(upstream_model.clone()),
|
||||
emulated_features,
|
||||
@@ -1507,6 +1594,7 @@ impl RuntimeSnapshot {
|
||||
)),
|
||||
config_revision: self.revision,
|
||||
pricing: Arc::clone(&self.settings.values.pricing),
|
||||
openai_request_country: self.settings.values.openai_request_country().clone(),
|
||||
request_location: self.settings.request_location.clone(),
|
||||
privacy: self.settings.privacy.clone(),
|
||||
account_error_protection: Arc::clone(&self.settings.account_error_protection),
|
||||
@@ -1584,6 +1672,7 @@ impl RuntimeSnapshot {
|
||||
});
|
||||
}
|
||||
let candidate = ProviderCandidate {
|
||||
requires_direct_authentication: false,
|
||||
provider: provider.clone(),
|
||||
upstream_model: upstream_model.cloned(),
|
||||
emulated_features: BTreeSet::new(),
|
||||
@@ -1596,6 +1685,7 @@ impl RuntimeSnapshot {
|
||||
)),
|
||||
config_revision: self.revision,
|
||||
pricing: Arc::clone(&self.settings.values.pricing),
|
||||
openai_request_country: self.settings.values.openai_request_country().clone(),
|
||||
request_location: self.settings.request_location.clone(),
|
||||
privacy: self.settings.privacy.clone(),
|
||||
account_error_protection: Arc::clone(&self.settings.account_error_protection),
|
||||
|
||||
@@ -111,6 +111,10 @@ impl CompiledSettings {
|
||||
max_waiting: settings.max_waiting_per_key,
|
||||
timeout: Duration::from_secs(u64::from(settings.key_queue_timeout_seconds)),
|
||||
};
|
||||
settings
|
||||
.openai_request_country
|
||||
.validate()
|
||||
.map_err(|_| InvalidSettings)?;
|
||||
let request_location = settings
|
||||
.request_location_enabled
|
||||
.then(|| settings.request_location.clone().normalized())
|
||||
|
||||
@@ -5,7 +5,8 @@ pub mod privacy;
|
||||
pub mod request_profiles;
|
||||
mod values;
|
||||
pub use values::{
|
||||
SettingsValues, client_min_versions, response_body_limit, validate_request_limits,
|
||||
SettingsValues, client_min_versions, default_oauth_guardian_review_model, response_body_limit,
|
||||
validate_request_limits,
|
||||
};
|
||||
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
|
||||
@@ -25,6 +25,10 @@ pub struct SettingsValues {
|
||||
pub(crate) switch_account_on_queue_failure: bool,
|
||||
pub(crate) max_retry_after_seconds: u32,
|
||||
pub(crate) openai_guardian_reserved_concurrency: u32,
|
||||
#[serde(default = "default_oauth_guardian_review_model")]
|
||||
pub(crate) openai_oauth_guardian_review_model: bool,
|
||||
#[serde(default)]
|
||||
pub(crate) openai_request_country: crate::account::OpenAiRequestCountry,
|
||||
pub(crate) openai_account_affinity: crate::account::AccountAffinity,
|
||||
pub(crate) max_account_rotations: u32,
|
||||
pub(crate) openai_session_affinity_ttl_hours: u32,
|
||||
@@ -42,6 +46,10 @@ fn initial_protection_revision() -> u64 {
|
||||
1
|
||||
}
|
||||
|
||||
pub const fn default_oauth_guardian_review_model() -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
impl SettingsValues {
|
||||
#[must_use]
|
||||
pub const fn with_account_error_protection_revision(mut self, revision: u64) -> Self {
|
||||
@@ -108,6 +116,31 @@ impl SettingsValues {
|
||||
self
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub const fn with_openai_oauth_guardian_review_model(mut self, enabled: bool) -> Self {
|
||||
self.openai_oauth_guardian_review_model = enabled;
|
||||
self
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub const fn openai_oauth_guardian_review_model(&self) -> bool {
|
||||
self.openai_oauth_guardian_review_model
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn with_openai_request_country(
|
||||
mut self,
|
||||
policy: crate::account::OpenAiRequestCountry,
|
||||
) -> Self {
|
||||
self.openai_request_country = policy;
|
||||
self
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn openai_request_country(&self) -> &crate::account::OpenAiRequestCountry {
|
||||
&self.openai_request_country
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn request_profiles(&self) -> &BTreeMap<ProviderKind, crate::account::OpaqueProviderData> {
|
||||
&self.request_profiles
|
||||
@@ -207,6 +240,8 @@ impl SettingsValues {
|
||||
switch_account_on_queue_failure: false,
|
||||
max_retry_after_seconds: 240,
|
||||
openai_guardian_reserved_concurrency: 0,
|
||||
openai_oauth_guardian_review_model: default_oauth_guardian_review_model(),
|
||||
openai_request_country: Default::default(),
|
||||
openai_account_affinity: crate::account::AccountAffinity::default(),
|
||||
max_account_rotations: 3,
|
||||
openai_session_affinity_ttl_hours: 24,
|
||||
|
||||
@@ -28,9 +28,8 @@ struct EmptyAdapterPlan;
|
||||
impl UpstreamAdapterPlan for EmptyAdapterPlan {
|
||||
fn select(
|
||||
&self,
|
||||
_: &AttemptContext,
|
||||
_: gateway_core::engine::ModelRouteScope<'_>,
|
||||
_: &ProviderKind,
|
||||
_: &UpstreamModelId,
|
||||
_: Option<&gateway_core::account::ProviderAccountId>,
|
||||
) -> Result<UpstreamAdapterSelection, ProviderError> {
|
||||
Ok(UpstreamAdapterSelection::Native)
|
||||
@@ -97,9 +96,8 @@ struct AccountSelectionProbe(std::sync::Mutex<Vec<Option<String>>>);
|
||||
impl UpstreamAdapterPlan for AccountSelectionProbe {
|
||||
fn select(
|
||||
&self,
|
||||
_: &AttemptContext,
|
||||
_: gateway_core::engine::ModelRouteScope<'_>,
|
||||
_: &ProviderKind,
|
||||
_: &UpstreamModelId,
|
||||
account: Option<&gateway_core::account::ProviderAccountId>,
|
||||
) -> Result<UpstreamAdapterSelection, ProviderError> {
|
||||
self.0
|
||||
|
||||
@@ -1772,9 +1772,9 @@ fn revision(value: u64) -> ConfigRevision {
|
||||
|
||||
#[test]
|
||||
fn global_request_location_should_be_frozen_when_snapshot_is_published() {
|
||||
use gateway_core::account::{ProviderAccountId, RequestLocation};
|
||||
use gateway_core::account::{OpenAiRequestCountry, ProviderAccountId, RequestLocation};
|
||||
use gateway_core::runtime::RuntimeSnapshotHandle;
|
||||
let make_snapshot = |version, timezone: &str, enabled| {
|
||||
let make_snapshot = |version, timezone: &str, enabled, country| {
|
||||
let location = RequestLocation {
|
||||
timezone: timezone.parse().unwrap(),
|
||||
..RequestLocation::default()
|
||||
@@ -1783,7 +1783,8 @@ fn global_request_location_should_be_frozen_when_snapshot_is_published() {
|
||||
revision(version),
|
||||
revision(version),
|
||||
SettingsValues::new(3, "smart", BTreeMap::new(), None, None)
|
||||
.with_request_location(location, enabled),
|
||||
.with_request_location(location, enabled)
|
||||
.with_openai_request_country(country),
|
||||
Vec::new(),
|
||||
Vec::new(),
|
||||
vec![SnapshotProviderAccountFacts::new(
|
||||
@@ -1801,7 +1802,12 @@ fn global_request_location_should_be_frozen_when_snapshot_is_published() {
|
||||
)
|
||||
.unwrap()
|
||||
};
|
||||
let handle = RuntimeSnapshotHandle::new(make_snapshot(1, "Asia/Tokyo", true));
|
||||
let handle = RuntimeSnapshotHandle::new(make_snapshot(
|
||||
1,
|
||||
"Asia/Tokyo",
|
||||
true,
|
||||
OpenAiRequestCountry::Passthrough {},
|
||||
));
|
||||
let frozen = handle.acquire().unwrap();
|
||||
let plan = |snapshot: &gateway_core::routing::RuntimeSnapshot| {
|
||||
snapshot
|
||||
@@ -1814,7 +1820,20 @@ fn global_request_location_should_be_frozen_when_snapshot_is_published() {
|
||||
.unwrap()
|
||||
};
|
||||
let old_plan = plan(&frozen);
|
||||
handle.publish(make_snapshot(2, "America/New_York", true));
|
||||
handle.publish(make_snapshot(
|
||||
2,
|
||||
"America/New_York",
|
||||
true,
|
||||
OpenAiRequestCountry::FollowLocation {},
|
||||
));
|
||||
assert_eq!(
|
||||
old_plan.openai_request_country(),
|
||||
&OpenAiRequestCountry::Passthrough {}
|
||||
);
|
||||
assert_eq!(
|
||||
plan(&handle.acquire().unwrap()).openai_request_country(),
|
||||
&OpenAiRequestCountry::FollowLocation {}
|
||||
);
|
||||
assert_eq!(
|
||||
old_plan.request_location().unwrap().timezone.name(),
|
||||
"Asia/Tokyo"
|
||||
@@ -1831,7 +1850,7 @@ fn global_request_location_should_be_frozen_when_snapshot_is_published() {
|
||||
.name(),
|
||||
"America/New_York"
|
||||
);
|
||||
handle.publish(make_snapshot(3, "Asia/Tokyo", false));
|
||||
handle.publish(make_snapshot(3, "Asia/Tokyo", false, Default::default()));
|
||||
assert!(
|
||||
plan(&handle.acquire().unwrap())
|
||||
.request_location()
|
||||
@@ -1841,7 +1860,7 @@ fn global_request_location_should_be_frozen_when_snapshot_is_published() {
|
||||
old_plan.request_location().unwrap().timezone.name(),
|
||||
"Asia/Tokyo"
|
||||
);
|
||||
handle.publish(make_snapshot(4, "Asia/Tokyo", true));
|
||||
handle.publish(make_snapshot(4, "Asia/Tokyo", true, Default::default()));
|
||||
assert_eq!(
|
||||
plan(&handle.acquire().unwrap())
|
||||
.request_location()
|
||||
@@ -2167,3 +2186,223 @@ fn key_profiles_replace_whole_global_choice_and_previous_snapshot_stays_frozen()
|
||||
assert_eq!(values(&previous), ["global-a", "override"]);
|
||||
assert_eq!(values(&build("global-b", false)), ["global-b", "global-b"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn direct_reviewer_model_freezes_only_authorized_native_catalog_accounts() {
|
||||
use gateway_core::account::{AccountModelAccess, AccountModelAccessMode, ProviderAccountId};
|
||||
const TARGET: &str = "codex-auto-review";
|
||||
const LUNA: &str = "gpt-5.6-luna";
|
||||
struct Catalog;
|
||||
impl ProviderCatalogPort for Catalog {
|
||||
fn is_direct_account(&self, _: &ProviderKind, kind: &str) -> bool {
|
||||
kind == "oauth"
|
||||
}
|
||||
fn catalog_generations(&self) -> BTreeMap<ProviderKind, ProviderCatalogGeneration> {
|
||||
[(
|
||||
ProviderKind::new("openai").unwrap(),
|
||||
ProviderCatalogGeneration::new(1),
|
||||
)]
|
||||
.into()
|
||||
}
|
||||
fn query_model_capabilities(
|
||||
&self,
|
||||
_: &ProviderKind,
|
||||
) -> BoxFuture<'_, Result<Vec<ProviderModelCapabilities>, ProviderCatalogUnavailable>>
|
||||
{
|
||||
Box::pin(async {
|
||||
Ok(vec![
|
||||
ProviderModelCapabilities::new(
|
||||
UpstreamModelId::new(TARGET).unwrap(),
|
||||
super::capabilities(),
|
||||
)
|
||||
.with_catalog_accounts(
|
||||
[
|
||||
"acct_oauth",
|
||||
"acct_oauth_backup",
|
||||
"acct_denied",
|
||||
"acct_blocked",
|
||||
"acct_key",
|
||||
]
|
||||
.map(|id| ProviderAccountId::new(id).unwrap())
|
||||
.into(),
|
||||
),
|
||||
ProviderModelCapabilities::new(
|
||||
UpstreamModelId::new(LUNA).unwrap(),
|
||||
super::capabilities(),
|
||||
),
|
||||
])
|
||||
})
|
||||
}
|
||||
}
|
||||
let facts = SnapshotFacts::new(
|
||||
revision(1),
|
||||
revision(1),
|
||||
super::settings(),
|
||||
vec![],
|
||||
vec![],
|
||||
[
|
||||
"acct_oauth",
|
||||
"acct_oauth_backup",
|
||||
"acct_denied",
|
||||
"acct_blocked",
|
||||
"acct_unpublished",
|
||||
"acct_key",
|
||||
]
|
||||
.map(|id| {
|
||||
let account =
|
||||
SnapshotProviderAccountFacts::new(ProviderAccountId::new(id).unwrap(), "openai")
|
||||
.with_authentication_kind(
|
||||
if id == "acct_key" { "api_key" } else { "oauth" }.into(),
|
||||
);
|
||||
if matches!(id, "acct_denied" | "acct_blocked") {
|
||||
let allowed = if id == "acct_denied" {
|
||||
LUNA
|
||||
} else {
|
||||
"unrelated-model"
|
||||
};
|
||||
account.with_model_access(
|
||||
AccountModelAccess::new(
|
||||
AccountModelAccessMode::Allowlist,
|
||||
vec![allowed.into()],
|
||||
)
|
||||
.unwrap(),
|
||||
)
|
||||
} else {
|
||||
account
|
||||
}
|
||||
})
|
||||
.into(),
|
||||
vec![],
|
||||
);
|
||||
let snapshot = block_on(
|
||||
RuntimeSnapshotCompiler::new(
|
||||
Arc::new(TestSnapshotStore::new(Ok(facts))),
|
||||
Arc::new(Catalog),
|
||||
)
|
||||
.compile(),
|
||||
)
|
||||
.unwrap();
|
||||
let snapshot = snapshot.with_extensions(Some(ExtensionSetReference::new(
|
||||
ExtensionSetId::new("review-catalog".into()).unwrap(),
|
||||
Arc::new(ModelLease(vec![gateway_core::routing::ContributedModel {
|
||||
owner: "review-plugin".into(),
|
||||
id: PublicModelId::new(TARGET).unwrap(),
|
||||
provider: ProviderKind::new("openai").unwrap(),
|
||||
accounts: ["acct_oauth", "acct_unpublished"]
|
||||
.map(|id| ProviderAccountId::new(id).unwrap())
|
||||
.into(),
|
||||
capabilities: super::capabilities(),
|
||||
presentation: ModelPresentation::new(Some("Review".into()), None),
|
||||
}])),
|
||||
)));
|
||||
let plan = |fixed: Option<&str>, owner: Option<&str>, adapter_account: Option<&str>| {
|
||||
let scope = snapshot.all_account_scope();
|
||||
let provider = ProviderKind::new("openai").unwrap();
|
||||
let target = UpstreamModelId::new(TARGET).unwrap();
|
||||
let fixed = fixed.map(|id| ProviderAccountId::new(id).unwrap());
|
||||
let owner = owner.map(|id| ProviderAccountId::new(id).unwrap());
|
||||
let scope = match fixed {
|
||||
Some(id) => Arc::new(scope.restricted_to(&Default::default(), &[id].into())),
|
||||
None => scope,
|
||||
};
|
||||
snapshot.plan_with_direct_account_model(
|
||||
&PublicModelId::new(LUNA).unwrap(),
|
||||
&super::operation(),
|
||||
scope,
|
||||
&Default::default(),
|
||||
gateway_core::routing::DirectAccountModel {
|
||||
provider: &provider,
|
||||
model: &target,
|
||||
continuation_owner: owner.as_ref(),
|
||||
},
|
||||
|id| Some(id.as_str()) != adapter_account,
|
||||
)
|
||||
};
|
||||
let oauth_accounts = &["acct_oauth", "acct_oauth_backup"][..];
|
||||
for (fixed, owner, expected_model, expected_accounts) in [
|
||||
(None, None, TARGET, oauth_accounts),
|
||||
(None, Some("acct_oauth"), TARGET, oauth_accounts),
|
||||
(Some("acct_oauth"), None, TARGET, &["acct_oauth"][..]),
|
||||
(Some("acct_key"), None, LUNA, &["acct_key"][..]),
|
||||
(
|
||||
None,
|
||||
Some("acct_key"),
|
||||
LUNA,
|
||||
&[
|
||||
"acct_oauth",
|
||||
"acct_oauth_backup",
|
||||
"acct_denied",
|
||||
"acct_unpublished",
|
||||
"acct_key",
|
||||
][..],
|
||||
),
|
||||
] {
|
||||
let frozen = plan(fixed, owner, None).unwrap();
|
||||
assert_eq!(
|
||||
frozen.candidates().len(),
|
||||
1,
|
||||
"retry must retain the selected model"
|
||||
);
|
||||
let candidate = &frozen.candidates()[0];
|
||||
assert_eq!(
|
||||
candidate.requires_direct_authentication(),
|
||||
expected_model == TARGET
|
||||
);
|
||||
assert_eq!(candidate.upstream_model().unwrap().as_str(), expected_model);
|
||||
for id in [
|
||||
"acct_key",
|
||||
"acct_oauth",
|
||||
"acct_oauth_backup",
|
||||
"acct_denied",
|
||||
"acct_unpublished",
|
||||
"acct_blocked",
|
||||
] {
|
||||
assert_eq!(
|
||||
candidate
|
||||
.account_scope()
|
||||
.allows_model(&ProviderAccountId::new(id).unwrap(), expected_model),
|
||||
expected_accounts.contains(&id)
|
||||
);
|
||||
}
|
||||
}
|
||||
for id in ["acct_denied", "acct_unpublished"] {
|
||||
let preserved = plan(Some(id), None, None).unwrap();
|
||||
let candidate = &preserved.candidates()[0];
|
||||
assert_eq!(candidate.upstream_model().unwrap().as_str(), LUNA);
|
||||
assert!(
|
||||
candidate
|
||||
.account_scope()
|
||||
.allows_model(&ProviderAccountId::new(id).unwrap(), LUNA)
|
||||
);
|
||||
}
|
||||
assert!(plan(Some("acct_blocked"), None, None).is_err());
|
||||
let adapter_plan = plan(None, Some("acct_oauth"), Some("acct_oauth")).unwrap();
|
||||
assert_eq!(
|
||||
adapter_plan.candidates()[0]
|
||||
.upstream_model()
|
||||
.unwrap()
|
||||
.as_str(),
|
||||
LUNA
|
||||
);
|
||||
let settings = snapshot
|
||||
.settings()
|
||||
.clone()
|
||||
.with_model_mappings([(LUNA.into(), "custom".into())].into());
|
||||
let configured = snapshot.with_settings(&settings).unwrap();
|
||||
let configured_plan = configured.plan_with_direct_account_model(
|
||||
&PublicModelId::new(LUNA).unwrap(),
|
||||
&super::operation(),
|
||||
configured.all_account_scope(),
|
||||
&Default::default(),
|
||||
gateway_core::routing::DirectAccountModel {
|
||||
provider: &ProviderKind::new("openai").unwrap(),
|
||||
model: &UpstreamModelId::new(TARGET).unwrap(),
|
||||
continuation_owner: None,
|
||||
},
|
||||
|_| true,
|
||||
);
|
||||
assert!(
|
||||
configured_plan.is_err(),
|
||||
"an explicit mapping to an unavailable model must not be bypassed"
|
||||
);
|
||||
}
|
||||
|
||||
@@ -49,6 +49,31 @@ impl PluginMiddlewarePlan {
|
||||
}
|
||||
|
||||
impl MiddlewarePlan for PluginMiddlewarePlan {
|
||||
fn preserves_model_bindings(
|
||||
&self,
|
||||
route: gateway_core::engine::ModelRouteScope<'_>,
|
||||
provider: &gateway_core::routing::ProviderKind,
|
||||
target: &gateway_core::routing::UpstreamModelId,
|
||||
) -> bool {
|
||||
[MiddlewareMount::Attempt, MiddlewareMount::Outbound]
|
||||
.iter()
|
||||
.filter_map(|mount| self.middleware.get(mount))
|
||||
.flatten()
|
||||
.all(|entry| {
|
||||
let matches = |model| {
|
||||
entry.scope.matches_processing(
|
||||
route.client_api_key,
|
||||
route.account_groups,
|
||||
Some(provider),
|
||||
Some(model),
|
||||
)
|
||||
};
|
||||
route.extensions.contains(&entry.instance_id)
|
||||
|| !matches(route.model)
|
||||
|| matches(target.as_str())
|
||||
})
|
||||
}
|
||||
|
||||
fn has_outbound(&self, context: &MiddlewareContext) -> bool {
|
||||
self.middleware
|
||||
.get(&MiddlewareMount::Outbound)
|
||||
|
||||
@@ -11,16 +11,12 @@ use std::{fmt, sync::Arc};
|
||||
|
||||
use gateway_core::{
|
||||
account::ProviderAccountId,
|
||||
engine::{
|
||||
AttemptContext,
|
||||
upstream_adapter::{
|
||||
UpstreamAdapter, UpstreamAdapterInvocation, UpstreamAdapterPlan,
|
||||
UpstreamAdapterSelection,
|
||||
},
|
||||
engine::ModelRouteScope,
|
||||
engine::upstream_adapter::{
|
||||
UpstreamAdapter, UpstreamAdapterInvocation, UpstreamAdapterPlan, UpstreamAdapterSelection,
|
||||
},
|
||||
error::{ProviderError, ProviderErrorKind},
|
||||
identity::ProviderKind,
|
||||
routing::UpstreamModelId,
|
||||
upstream::UpstreamSendState,
|
||||
};
|
||||
use gateway_plugin_sdk::call::upstream_adapter::UpstreamAdapterDeclaration;
|
||||
@@ -117,19 +113,16 @@ impl fmt::Debug for PluginUpstreamAdapterPlan {
|
||||
impl UpstreamAdapterPlan for PluginUpstreamAdapterPlan {
|
||||
fn select(
|
||||
&self,
|
||||
context: &AttemptContext,
|
||||
context: ModelRouteScope<'_>,
|
||||
provider: &ProviderKind,
|
||||
model: &UpstreamModelId,
|
||||
account: Option<&ProviderAccountId>,
|
||||
) -> Result<UpstreamAdapterSelection, ProviderError> {
|
||||
let model = context
|
||||
.requested_model()
|
||||
.map_or(model.as_str(), |model| model.as_str());
|
||||
let model = context.model;
|
||||
for entry in &self.entries {
|
||||
if context.extension_scope().contains(&entry.instance_id)
|
||||
if context.extensions.contains(&entry.instance_id)
|
||||
|| !entry.scope.matches_processing(
|
||||
context.client_api_key_ref(),
|
||||
context.account_group_ids(),
|
||||
context.client_api_key,
|
||||
context.account_groups,
|
||||
Some(provider),
|
||||
Some(model),
|
||||
)
|
||||
|
||||
@@ -342,3 +342,64 @@ impl Drop for NotifyOnDrop {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn automatic_model_substitution_keeps_attempt_and_outbound_middleware_bindings() {
|
||||
use gateway_core::engine::ModelRouteScope;
|
||||
for stage in ["attempt", "outbound"] {
|
||||
let instances = [
|
||||
("instance-exact", "key-exact", vec!["gpt-5.6-luna".into()]),
|
||||
("instance-wildcard", "key-wildcard", vec![]),
|
||||
(
|
||||
"instance-both",
|
||||
"key-both",
|
||||
vec!["gpt-5.6-luna".into(), "codex-auto-review".into()],
|
||||
),
|
||||
]
|
||||
.map(|(id, key, models)| {
|
||||
let mut entry = instance(id, 0, serde_json::json!({}));
|
||||
entry.bindings[0].stage = stage.into();
|
||||
entry.bindings[0].models = models;
|
||||
entry.bindings[0].client_key_ids = vec![key.into()];
|
||||
entry
|
||||
})
|
||||
.into();
|
||||
let (cache, runtime) = if stage == "outbound" {
|
||||
runtime(instances, RpcLimits::default()).await
|
||||
} else {
|
||||
super::setup(instances).await
|
||||
};
|
||||
let generation = prepare(&runtime).await;
|
||||
let plan = runtime
|
||||
.execution_registry()
|
||||
.middleware(&generation)
|
||||
.unwrap();
|
||||
for (key, preserved) in [
|
||||
("key-exact", false),
|
||||
("key-wildcard", true),
|
||||
("key-both", true),
|
||||
("key-other", true),
|
||||
] {
|
||||
let key = ClientApiKeyId::new(key).unwrap();
|
||||
let route = ModelRouteScope {
|
||||
client_api_key: &key,
|
||||
account_groups: &[],
|
||||
extensions: &Default::default(),
|
||||
model: "gpt-5.6-luna",
|
||||
};
|
||||
assert_eq!(
|
||||
plan.preserves_model_bindings(
|
||||
route,
|
||||
&ProviderKind::new("openai").unwrap(),
|
||||
&gateway_core::routing::UpstreamModelId::new("codex-auto-review").unwrap()
|
||||
),
|
||||
preserved,
|
||||
"{stage}"
|
||||
);
|
||||
}
|
||||
drop(plan);
|
||||
drop(generation);
|
||||
runtime.shutdown().await;
|
||||
super::super::wait_until_empty(cache.path()).await;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -243,6 +243,56 @@ fn execute_with_context(
|
||||
})
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn native_model_eligibility_respects_explicit_upstream_adapter_bindings() {
|
||||
use gateway_core::engine::ModelRouteScope;
|
||||
for models in [json!(["gpt-5.6-luna"]), json!([])] {
|
||||
let wildcard = models.as_array().unwrap().is_empty();
|
||||
let mut config = configuration("https://example.invalid", false);
|
||||
let entry = &mut config["upstream_registration"]["adapters"][0];
|
||||
entry["models"] = models;
|
||||
entry["account_ids"] = json!(["acct_one"]);
|
||||
let (cache, store, runtime) = setup(config).await;
|
||||
store.snapshot.lock().unwrap().instances[0].bindings[0].client_key_ids =
|
||||
vec!["key-one".into()];
|
||||
let generation =
|
||||
ExtensionPreparationPort::prepare(&runtime, ConfigRevision::new(1).unwrap())
|
||||
.await
|
||||
.unwrap();
|
||||
let plan = runtime
|
||||
.execution_registry()
|
||||
.upstream_adapters(&generation)
|
||||
.unwrap();
|
||||
let provider = ProviderKind::new("openai").unwrap();
|
||||
for (key, account, model, native) in [
|
||||
("key-one", "acct_one", "gpt-5.6-luna", false),
|
||||
("key-one", "acct_one", "codex-auto-review", !wildcard),
|
||||
("key-two", "acct_one", "gpt-5.6-luna", true),
|
||||
("key-one", "acct_two", "gpt-5.6-luna", true),
|
||||
] {
|
||||
let key = ClientApiKeyId::new(key).unwrap();
|
||||
let route = ModelRouteScope {
|
||||
client_api_key: &key,
|
||||
account_groups: &[],
|
||||
extensions: &Default::default(),
|
||||
model,
|
||||
};
|
||||
assert_eq!(
|
||||
plan.uses_native_upstream(
|
||||
route,
|
||||
&provider,
|
||||
&ProviderAccountId::new(account).unwrap()
|
||||
),
|
||||
native
|
||||
);
|
||||
}
|
||||
drop(plan);
|
||||
drop(generation);
|
||||
runtime.shutdown().await;
|
||||
super::wait_until_empty(cache.path()).await;
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn account_scoped_preselection_keeps_fallback_failures_within_matching_accounts() {
|
||||
for fallback_first in [false, true] {
|
||||
|
||||
@@ -15,6 +15,9 @@ pub type Revision = NonZeroU64;
|
||||
pub type RotationStrategy = String;
|
||||
pub type AccountAffinity = String;
|
||||
pub type PricingOverrides = BTreeMap<String, BTreeMap<String, ModelPriceOverride>>;
|
||||
fn default_oauth_guardian_review_model() -> bool {
|
||||
true
|
||||
}
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase", deny_unknown_fields)]
|
||||
pub struct AccountErrorProtection {
|
||||
@@ -136,6 +139,10 @@ pub struct PrivacyPreviewResult {
|
||||
#[derive(Clone, Serialize, Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
pub struct RuntimeSettings {
|
||||
#[serde(default = "default_oauth_guardian_review_model")]
|
||||
pub openai_oauth_guardian_review_model: bool,
|
||||
#[serde(default)]
|
||||
pub openai_request_country: OpenAiRequestCountry,
|
||||
pub codex_privacy_policy: CodexPrivacyPolicy,
|
||||
pub request_location_enabled: bool,
|
||||
pub request_location: RequestLocation,
|
||||
@@ -177,6 +184,11 @@ pub struct RuntimeSettings {
|
||||
#[derive(Clone, Serialize, Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
pub struct ReplaceRuntimeSettings {
|
||||
/// 旧客户端缺少此字段时保留已保存的审核模型策略
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub openai_oauth_guardian_review_model: Option<bool>,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub openai_request_country: Option<OpenAiRequestCountry>,
|
||||
pub codex_privacy_policy: CodexPrivacyPolicy,
|
||||
pub request_location_enabled: bool,
|
||||
pub request_location: RequestLocation,
|
||||
@@ -249,6 +261,14 @@ pub struct RequestLocation {
|
||||
pub city: String,
|
||||
pub timezone: String,
|
||||
}
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(tag = "mode", rename_all = "snake_case", deny_unknown_fields)]
|
||||
pub enum OpenAiRequestCountry {
|
||||
Default {},
|
||||
Passthrough {},
|
||||
FollowLocation {},
|
||||
Custom { country: String },
|
||||
}
|
||||
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase", deny_unknown_fields)]
|
||||
pub struct SmartSchedulingConfig {
|
||||
@@ -314,6 +334,11 @@ pub struct UpdatePricing {
|
||||
pub models: Vec<String>,
|
||||
pub change: PricingChange,
|
||||
}
|
||||
impl Default for OpenAiRequestCountry {
|
||||
fn default() -> Self {
|
||||
Self::FollowLocation {}
|
||||
}
|
||||
}
|
||||
pub struct Load;
|
||||
impl Operation for Load {
|
||||
const NAME: &'static str = "settings.load";
|
||||
@@ -389,6 +414,8 @@ impl Operation for PreviewClientProfile {
|
||||
impl From<RuntimeSettings> for ReplaceRuntimeSettings {
|
||||
fn from(settings: RuntimeSettings) -> Self {
|
||||
Self {
|
||||
openai_oauth_guardian_review_model: Some(settings.openai_oauth_guardian_review_model),
|
||||
openai_request_country: Some(settings.openai_request_country),
|
||||
codex_privacy_policy: settings.codex_privacy_policy,
|
||||
request_location_enabled: settings.request_location_enabled,
|
||||
request_location: settings.request_location,
|
||||
|
||||
@@ -0,0 +1,230 @@
|
||||
//! 在 Responses 入站身份字段合并前识别 Guardian 的三种调用
|
||||
|
||||
use serde::{Deserialize, Deserializer};
|
||||
use serde_json::{Map, Value};
|
||||
|
||||
const CONTEXT_KEY: &str = "codex_guardian_identity";
|
||||
/// Codex 原生 OAuth 同步审核使用的专用模型
|
||||
pub const CODEX_AUTO_REVIEW_MODEL: &str = "codex-auto-review";
|
||||
|
||||
/// 当前 Responses 请求的 Codex Guardian 调用类型
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum CodexGuardianRole {
|
||||
Reviewer,
|
||||
Classifier,
|
||||
Housekeeper,
|
||||
}
|
||||
|
||||
impl CodexGuardianRole {
|
||||
/// 该调用在原生 OAuth 上游使用的角色头值
|
||||
#[must_use]
|
||||
pub fn header(self) -> Option<&'static str> {
|
||||
match self {
|
||||
Self::Reviewer => Some("reviewer"),
|
||||
Self::Classifier => Some("classifier"),
|
||||
Self::Housekeeper => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// 分类器和后台整理器用各自的 sampler 选择 WebSocket 或 HTTP
|
||||
#[must_use]
|
||||
pub fn uses_sampler_transport(self) -> bool {
|
||||
matches!(self, Self::Classifier | Self::Housekeeper)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, PartialEq, Eq)]
|
||||
enum Identity {
|
||||
Role(CodexGuardianRole),
|
||||
Incomplete,
|
||||
Other,
|
||||
Ambiguous,
|
||||
}
|
||||
|
||||
impl Identity {
|
||||
fn label(self) -> &'static str {
|
||||
match self {
|
||||
Self::Role(CodexGuardianRole::Reviewer) => "reviewer",
|
||||
Self::Role(CodexGuardianRole::Classifier) => "classifier",
|
||||
Self::Role(CodexGuardianRole::Housekeeper) => "housekeeper",
|
||||
Self::Incomplete => "incomplete",
|
||||
Self::Other => "other",
|
||||
Self::Ambiguous => "ambiguous",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct Source<'a> {
|
||||
subagent: Option<&'a str>,
|
||||
turn: Turn,
|
||||
}
|
||||
|
||||
#[derive(Default, Deserialize)]
|
||||
struct Turn {
|
||||
#[serde(default, deserialize_with = "present_string")]
|
||||
thread_source: Option<String>,
|
||||
#[serde(default, deserialize_with = "present_string")]
|
||||
turn_trigger: Option<String>,
|
||||
}
|
||||
|
||||
// 区分缺失与错误类型;serde 对重复出现的身份字段也会拒绝
|
||||
fn present_string<'de, D: Deserializer<'de>>(d: D) -> Result<Option<String>, D::Error> {
|
||||
String::deserialize(d).map(Some)
|
||||
}
|
||||
|
||||
impl<'a> Source<'a> {
|
||||
fn new(subagent: Option<&'a str>, turn: Option<&str>) -> Option<Self> {
|
||||
let turn = match turn {
|
||||
Some(raw) if raw.trim_start().starts_with('{') => serde_json::from_str(raw).ok()?,
|
||||
Some(_) => return None,
|
||||
None => Turn::default(),
|
||||
};
|
||||
Some(Self { subagent, turn })
|
||||
}
|
||||
|
||||
fn conflicts_with(&self, other: &Self) -> bool {
|
||||
[
|
||||
self.subagent.zip(other.subagent),
|
||||
self.turn
|
||||
.thread_source
|
||||
.as_deref()
|
||||
.zip(other.turn.thread_source.as_deref()),
|
||||
self.turn
|
||||
.turn_trigger
|
||||
.as_deref()
|
||||
.zip(other.turn.turn_trigger.as_deref()),
|
||||
]
|
||||
.into_iter()
|
||||
.flatten()
|
||||
.any(|(left, right)| left != right)
|
||||
}
|
||||
|
||||
fn identity(&self) -> Identity {
|
||||
let source = self.turn.thread_source.as_deref();
|
||||
let trigger = self.turn.turn_trigger.as_deref();
|
||||
if self.subagent.is_some_and(|v| v != "guardian") {
|
||||
return Identity::Other;
|
||||
}
|
||||
let role = match (source, trigger) {
|
||||
(Some("guardian_review"), Some("guardian_classifier" | "guardian_housekeeper"))
|
||||
| (Some("guardian_classifier"), Some("guardian_review" | "guardian_housekeeper")) => {
|
||||
return Identity::Ambiguous;
|
||||
}
|
||||
(Some("guardian_review"), _) => CodexGuardianRole::Reviewer,
|
||||
(Some("guardian_classifier"), _) => CodexGuardianRole::Classifier,
|
||||
(None, Some("guardian_housekeeper")) => CodexGuardianRole::Housekeeper,
|
||||
(Some(_), _) => return Identity::Other,
|
||||
_ => return Identity::Incomplete,
|
||||
};
|
||||
if self.subagent.is_some() {
|
||||
Identity::Role(role)
|
||||
} else {
|
||||
Identity::Incomplete
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn identity(body: &Map<String, Value>, context: &Map<String, Value>) -> Identity {
|
||||
let metadata = body.get("client_metadata");
|
||||
let body_subagent = metadata.and_then(|m| m.get("x-openai-subagent"));
|
||||
let websocket = context
|
||||
.get(super::WEBSOCKET_FRAME_CONTEXT_KEY)
|
||||
.and_then(Value::as_bool)
|
||||
== Some(true);
|
||||
let header_subagent = (!websocket)
|
||||
.then(|| context.get("subagent"))
|
||||
.flatten()
|
||||
.and_then(Value::as_str);
|
||||
if metadata.is_some_and(|m| !m.is_object()) || body_subagent.is_some_and(|v| !v.is_string()) {
|
||||
return Identity::Ambiguous;
|
||||
}
|
||||
if body_subagent.and_then(Value::as_str) != Some("guardian")
|
||||
&& header_subagent != Some("guardian")
|
||||
{
|
||||
return if body_subagent.is_some() || header_subagent.is_some() {
|
||||
Identity::Other
|
||||
} else {
|
||||
Identity::Incomplete
|
||||
};
|
||||
}
|
||||
let turn = metadata.and_then(|m| m.get("x-codex-turn-metadata"));
|
||||
if turn.is_some_and(|v| !v.is_string()) {
|
||||
return Identity::Ambiguous;
|
||||
}
|
||||
let Some(body) = Source::new(
|
||||
body_subagent.and_then(Value::as_str),
|
||||
turn.and_then(Value::as_str),
|
||||
) else {
|
||||
return Identity::Ambiguous;
|
||||
};
|
||||
if websocket {
|
||||
return body.identity();
|
||||
}
|
||||
let Some(header) = Source::new(
|
||||
header_subagent,
|
||||
context.get("turn_metadata").and_then(Value::as_str),
|
||||
) else {
|
||||
return Identity::Ambiguous;
|
||||
};
|
||||
if body.conflicts_with(&header) {
|
||||
return Identity::Ambiguous;
|
||||
}
|
||||
match (body.identity(), header.identity()) {
|
||||
(Identity::Ambiguous, _) | (_, Identity::Ambiguous) => Identity::Ambiguous,
|
||||
(Identity::Incomplete, header) => header,
|
||||
(body, _) => body,
|
||||
}
|
||||
}
|
||||
|
||||
/// WS 只使用本帧正文;HTTP 两侧须各自完整,不能拼接彼此欠缺的身份字段
|
||||
pub fn capture_codex_guardian_identity(
|
||||
body: &Map<String, Value>,
|
||||
context: &mut Map<String, Value>,
|
||||
guardian_header: Option<&str>,
|
||||
ambiguous_headers: bool,
|
||||
) {
|
||||
if guardian_header.is_none()
|
||||
&& !ambiguous_headers
|
||||
&& context.get("subagent").and_then(Value::as_str) != Some("guardian")
|
||||
&& body
|
||||
.get("client_metadata")
|
||||
.and_then(|m| m.get("x-openai-subagent"))
|
||||
.and_then(Value::as_str)
|
||||
!= Some("guardian")
|
||||
{
|
||||
return;
|
||||
}
|
||||
let websocket = context
|
||||
.get(super::WEBSOCKET_FRAME_CONTEXT_KEY)
|
||||
.and_then(Value::as_bool)
|
||||
== Some(true);
|
||||
let mut found = identity(body, context);
|
||||
if !websocket
|
||||
&& (ambiguous_headers
|
||||
|| matches!((guardian_header, found),
|
||||
(Some(header), Identity::Role(role)) if role.header() != Some(header)))
|
||||
{
|
||||
found = Identity::Ambiguous;
|
||||
}
|
||||
context.insert(CONTEXT_KEY.into(), Value::from(found.label()));
|
||||
}
|
||||
|
||||
/// 使用解码阶段保存的原始身份;程序内构造的请求可以直接使用正文和当前请求上下文
|
||||
#[must_use]
|
||||
pub fn codex_guardian_role(
|
||||
body: &Map<String, Value>,
|
||||
context: &Map<String, Value>,
|
||||
) -> Option<CodexGuardianRole> {
|
||||
if let Some(prepared) = context.get(CONTEXT_KEY) {
|
||||
return match prepared.as_str()? {
|
||||
"reviewer" => Some(CodexGuardianRole::Reviewer),
|
||||
"classifier" => Some(CodexGuardianRole::Classifier),
|
||||
"housekeeper" => Some(CodexGuardianRole::Housekeeper),
|
||||
_ => None,
|
||||
};
|
||||
}
|
||||
match identity(body, context) {
|
||||
Identity::Role(role) => Some(role),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
@@ -19,6 +19,7 @@ pub const X_OPENAI_MEMGEN_REQUEST_HEADER: &str = "x-openai-memgen-request";
|
||||
pub mod codex;
|
||||
/// OpenAI/Codex 事件语义、用量与限流字段编解码
|
||||
pub mod events;
|
||||
mod guardian;
|
||||
mod headers;
|
||||
/// Server-Sent Events 帧的解析与编码
|
||||
pub mod sse;
|
||||
@@ -30,6 +31,10 @@ pub use codex::{
|
||||
codex_turn_id, context_turn_metadata, observed_turn_metadata, turn_metadata_session_id,
|
||||
turn_metadata_thread_id, turn_metadata_turn_id,
|
||||
};
|
||||
pub use guardian::{
|
||||
CODEX_AUTO_REVIEW_MODEL, CodexGuardianRole, capture_codex_guardian_identity,
|
||||
codex_guardian_role,
|
||||
};
|
||||
pub use headers::{
|
||||
is_transport_managed_request_header, parse_retry_after_seconds, response_header_is_forwardable,
|
||||
};
|
||||
|
||||
@@ -83,6 +83,8 @@ impl SettingsStore for AdminSettingsStoreAdapter {
|
||||
let replacement = postgres::ControlPlaneReplacement {
|
||||
expected_revision: store_revision(command.expected_revision)?,
|
||||
settings: postgres::RuntimeSettingsUpdate {
|
||||
openai_oauth_guardian_review_model: command.openai_oauth_guardian_review_model,
|
||||
openai_request_country: command.openai_request_country,
|
||||
request_profile_updates: command.request_profile_updates,
|
||||
rotation_strategy: command.rotation_strategy.as_str().to_owned(),
|
||||
model_mappings: store_model_mappings(command.model_mappings),
|
||||
@@ -108,6 +110,8 @@ impl SettingsStore for AdminSettingsStoreAdapter {
|
||||
"switch_account_on_queue_failure".to_owned(),
|
||||
"max_retry_after_seconds".to_owned(),
|
||||
"openai_guardian_reserved_concurrency".to_owned(),
|
||||
"openai_oauth_guardian_review_model".to_owned(),
|
||||
"openai_request_country".to_owned(),
|
||||
"openai_account_affinity".to_owned(),
|
||||
"max_account_rotations".to_owned(),
|
||||
"openai_session_affinity_ttl_hours".to_owned(),
|
||||
@@ -207,6 +211,8 @@ pub(crate) fn admin_runtime_settings(
|
||||
})
|
||||
.collect::<AdminStoreResult<ModelMappings>>()?;
|
||||
Ok(AdminRuntimeSettings {
|
||||
openai_oauth_guardian_review_model: settings.openai_oauth_guardian_review_model,
|
||||
openai_request_country: settings.openai_request_country,
|
||||
request_profiles: settings.request_profiles,
|
||||
config_revision: admin_revision(settings.config_revision)?,
|
||||
model_mappings,
|
||||
|
||||
@@ -20,6 +20,8 @@ use crate::{Revision, StoreError, StoreResult, postgres_unavailable};
|
||||
|
||||
#[derive(Clone, PartialEq, Eq)]
|
||||
pub struct RuntimeSettings {
|
||||
pub openai_oauth_guardian_review_model: bool,
|
||||
pub openai_request_country: gateway_core::account::OpenAiRequestCountry,
|
||||
pub values: RuntimeSettingsValues,
|
||||
pub request_profiles:
|
||||
BTreeMap<gateway_core::routing::ProviderKind, gateway_core::account::OpaqueProviderData>,
|
||||
@@ -51,6 +53,8 @@ impl fmt::Debug for RuntimeSettings {
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct RuntimeSettingsUpdate {
|
||||
pub openai_oauth_guardian_review_model: Option<bool>,
|
||||
pub openai_request_country: Option<gateway_core::account::OpenAiRequestCountry>,
|
||||
pub values: RuntimeSettingsValues,
|
||||
pub request_profile_updates: BTreeMap<
|
||||
gateway_core::routing::ProviderKind,
|
||||
@@ -78,6 +82,10 @@ impl fmt::Debug for RuntimeSettingsUpdate {
|
||||
impl RuntimeSettingsUpdate {
|
||||
pub fn validate(&self) -> StoreResult<()> {
|
||||
if self.values.validate().is_err()
|
||||
|| self
|
||||
.openai_request_country
|
||||
.as_ref()
|
||||
.is_some_and(|policy| policy.validate().is_err())
|
||||
|| gateway_admin::model::settings::validate_model_mappings(
|
||||
self.model_mappings
|
||||
.iter()
|
||||
@@ -155,7 +163,7 @@ pub(crate) async fn load_runtime_settings_from_pool(pool: &PgPool) -> StoreResul
|
||||
refresh_concurrency, max_concurrent_per_account,
|
||||
rotation_strategy, smart_scheduling_json, quota_reset_window_weights_json, model_mappings_json, usage_retention_days, ops_event_retention_days,
|
||||
audit_retention_days, min_codex_desktop_version,
|
||||
min_codex_cli_version, updated_at, responses_max_decompressed_body_bytes, max_waiting_per_key, max_waiting_per_account, key_queue_timeout_seconds, account_queue_timeout_seconds, switch_account_on_queue_failure, max_retry_after_seconds, openai_guardian_reserved_concurrency, openai_account_affinity, max_account_rotations, openai_session_affinity_ttl_hours,
|
||||
min_codex_cli_version, updated_at, responses_max_decompressed_body_bytes, max_waiting_per_key, max_waiting_per_account, key_queue_timeout_seconds, account_queue_timeout_seconds, switch_account_on_queue_failure, max_retry_after_seconds, openai_guardian_reserved_concurrency, openai_oauth_guardian_review_model, openai_request_country_json, openai_account_affinity, max_account_rotations, openai_session_affinity_ttl_hours,
|
||||
account_error_protection_json, account_error_protection_revision,
|
||||
account_warmup_enabled, account_warmup_cron, account_warmup_model, reset_credit_lead_minutes, reset_credit_max_weekly_remaining_percent, reset_credit_on_low_weekly_quota
|
||||
from runtime_settings where id = 1",
|
||||
@@ -335,7 +343,7 @@ pub(crate) async fn load_runtime_settings_in_transaction(
|
||||
refresh_concurrency, max_concurrent_per_account,
|
||||
rotation_strategy, smart_scheduling_json, quota_reset_window_weights_json, model_mappings_json, usage_retention_days, ops_event_retention_days,
|
||||
audit_retention_days, min_codex_desktop_version,
|
||||
min_codex_cli_version, updated_at, responses_max_decompressed_body_bytes, max_waiting_per_key, max_waiting_per_account, key_queue_timeout_seconds, account_queue_timeout_seconds, switch_account_on_queue_failure, max_retry_after_seconds, openai_guardian_reserved_concurrency, openai_account_affinity, max_account_rotations, openai_session_affinity_ttl_hours,
|
||||
min_codex_cli_version, updated_at, responses_max_decompressed_body_bytes, max_waiting_per_key, max_waiting_per_account, key_queue_timeout_seconds, account_queue_timeout_seconds, switch_account_on_queue_failure, max_retry_after_seconds, openai_guardian_reserved_concurrency, openai_oauth_guardian_review_model, openai_request_country_json, openai_account_affinity, max_account_rotations, openai_session_affinity_ttl_hours,
|
||||
account_error_protection_json, account_error_protection_revision,
|
||||
account_warmup_enabled, account_warmup_cron, account_warmup_model, reset_credit_lead_minutes, reset_credit_max_weekly_remaining_percent, reset_credit_on_low_weekly_quota
|
||||
from runtime_settings where id = 1",
|
||||
@@ -420,6 +428,8 @@ pub(crate) async fn update_runtime_settings_in_transaction(
|
||||
switch_account_on_queue_failure = $33,
|
||||
reset_credit_on_low_weekly_quota = $34,
|
||||
quota_reset_window_weights_json = $35,
|
||||
openai_oauth_guardian_review_model = coalesce($36, openai_oauth_guardian_review_model),
|
||||
openai_request_country_json = coalesce($37, openai_request_country_json),
|
||||
updated_at = now()
|
||||
where id = 1
|
||||
returning config_revision",
|
||||
@@ -467,6 +477,8 @@ pub(crate) async fn update_runtime_settings_in_transaction(
|
||||
.bind(update.values.switch_account_on_queue_failure)
|
||||
.bind(update.values.reset_credit_on_low_weekly_quota)
|
||||
.bind(sqlx::types::Json(update.values.quota_reset_window_weights))
|
||||
.bind(update.openai_oauth_guardian_review_model)
|
||||
.bind(update.openai_request_country.as_ref().map(sqlx::types::Json))
|
||||
.fetch_optional(&mut **transaction)
|
||||
.await
|
||||
.map_err(|source| postgres_unavailable("update runtime settings in transaction", source))?
|
||||
@@ -560,6 +572,8 @@ struct RuntimeSettingsRow {
|
||||
switch_account_on_queue_failure: bool,
|
||||
max_retry_after_seconds: i64,
|
||||
openai_guardian_reserved_concurrency: i64,
|
||||
openai_oauth_guardian_review_model: bool,
|
||||
openai_request_country_json: sqlx::types::Json<gateway_core::account::OpenAiRequestCountry>,
|
||||
openai_account_affinity: String,
|
||||
max_account_rotations: i64,
|
||||
openai_session_affinity_ttl_hours: i64,
|
||||
@@ -589,6 +603,8 @@ fn runtime_settings_from_row(row: RuntimeSettingsRow) -> StoreResult<RuntimeSett
|
||||
})
|
||||
.collect::<StoreResult<BTreeMap<_, _>>>()?;
|
||||
Ok(RuntimeSettings {
|
||||
openai_oauth_guardian_review_model: row.openai_oauth_guardian_review_model,
|
||||
openai_request_country: row.openai_request_country_json.0,
|
||||
request_profiles,
|
||||
config_revision: Revision::new(to_u64(row.config_revision)?)?,
|
||||
account_error_protection_revision: Revision::new(to_u64(
|
||||
|
||||
@@ -237,6 +237,8 @@ struct SnapshotSettingsRow {
|
||||
switch_account_on_queue_failure: bool,
|
||||
max_retry_after_seconds: i64,
|
||||
openai_guardian_reserved_concurrency: i64,
|
||||
openai_oauth_guardian_review_model: bool,
|
||||
openai_request_country_json: sqlx::types::Json<gateway_core::account::OpenAiRequestCountry>,
|
||||
openai_account_affinity: String,
|
||||
max_account_rotations: i64,
|
||||
openai_session_affinity_ttl_hours: i64,
|
||||
@@ -253,7 +255,7 @@ async fn load_settings(
|
||||
transaction: &mut Transaction<'_, Postgres>,
|
||||
) -> StoreResult<(Revision, SettingsValues)> {
|
||||
let row = sqlx::query_as::<_, SnapshotSettingsRow>(
|
||||
"select config_revision, account_error_protection_revision, account_error_protection_json, max_concurrent_per_account, rotation_strategy, smart_scheduling_json, quota_reset_window_weights_json, model_mappings_json, min_codex_desktop_version, min_codex_cli_version, max_waiting_per_key, max_waiting_per_account, key_queue_timeout_seconds, account_queue_timeout_seconds, switch_account_on_queue_failure, max_retry_after_seconds, openai_guardian_reserved_concurrency, openai_account_affinity, max_account_rotations, openai_session_affinity_ttl_hours, request_location_json, request_location_enabled, codex_privacy_policy_json, responses_max_decompressed_body_bytes, provider_request_profiles_json, pricing_overrides_json, pricing_synced_json from runtime_settings where id = 1",
|
||||
"select config_revision, account_error_protection_revision, account_error_protection_json, max_concurrent_per_account, rotation_strategy, smart_scheduling_json, quota_reset_window_weights_json, model_mappings_json, min_codex_desktop_version, min_codex_cli_version, max_waiting_per_key, max_waiting_per_account, key_queue_timeout_seconds, account_queue_timeout_seconds, switch_account_on_queue_failure, max_retry_after_seconds, openai_guardian_reserved_concurrency, openai_oauth_guardian_review_model, openai_request_country_json, openai_account_affinity, max_account_rotations, openai_session_affinity_ttl_hours, request_location_json, request_location_enabled, codex_privacy_policy_json, responses_max_decompressed_body_bytes, provider_request_profiles_json, pricing_overrides_json, pricing_synced_json from runtime_settings where id = 1",
|
||||
)
|
||||
.fetch_optional(&mut **transaction)
|
||||
.await
|
||||
@@ -278,6 +280,8 @@ async fn load_settings(
|
||||
.with_openai_guardian_reserved_concurrency(to_u32(
|
||||
row.openai_guardian_reserved_concurrency,
|
||||
)?)
|
||||
.with_openai_oauth_guardian_review_model(row.openai_oauth_guardian_review_model)
|
||||
.with_openai_request_country(row.openai_request_country_json.0)
|
||||
.with_openai_account_affinity(
|
||||
gateway_core::account::AccountAffinity::parse(&row.openai_account_affinity)
|
||||
.ok_or_else(|| invalid("invalid account affinity"))?,
|
||||
|
||||
@@ -38,8 +38,14 @@ async fn control_plane_replacement_commits_one_writer_per_revision_and_preserves
|
||||
created_at: Utc::now(),
|
||||
},
|
||||
};
|
||||
let mut customized = replacement("first", 1800);
|
||||
customized.settings.openai_oauth_guardian_review_model = Some(false);
|
||||
customized.settings.openai_request_country =
|
||||
Some(gateway_core::account::OpenAiRequestCountry::Custom {
|
||||
country: "CA".into(),
|
||||
});
|
||||
let (first, second) = tokio::join!(
|
||||
repository.replace_control_plane(replacement("first", 1800)),
|
||||
repository.replace_control_plane(customized),
|
||||
repository.replace_control_plane(replacement("second", 7200)),
|
||||
);
|
||||
let (saved, conflict) = match (first, second) {
|
||||
@@ -59,6 +65,11 @@ async fn control_plane_replacement_commits_one_writer_per_revision_and_preserves
|
||||
current.values.refresh_margin_seconds,
|
||||
saved.values.refresh_margin_seconds
|
||||
);
|
||||
assert_eq!(
|
||||
current.openai_oauth_guardian_review_model,
|
||||
saved.openai_oauth_guardian_review_model
|
||||
);
|
||||
assert_eq!(current.openai_request_country, saved.openai_request_country);
|
||||
let audit_count: i64 = sqlx::query_scalar(
|
||||
"select count(*) from admin_audit_events where action = 'settings.replace'",
|
||||
)
|
||||
@@ -89,7 +100,19 @@ async fn control_plane_replacement_commits_one_writer_per_revision_and_preserves
|
||||
.unwrap()
|
||||
.settings
|
||||
.config_revision;
|
||||
repository.replace_control_plane(fresh).await.unwrap();
|
||||
let after_legacy_write = repository
|
||||
.replace_control_plane(fresh)
|
||||
.await
|
||||
.unwrap()
|
||||
.settings;
|
||||
assert_eq!(
|
||||
after_legacy_write.openai_oauth_guardian_review_model,
|
||||
saved.openai_oauth_guardian_review_model
|
||||
);
|
||||
assert_eq!(
|
||||
after_legacy_write.openai_request_country,
|
||||
saved.openai_request_country
|
||||
);
|
||||
assert_eq!(
|
||||
repository
|
||||
.load_control_plane()
|
||||
|
||||
@@ -15,6 +15,8 @@ fn enabled_protection() -> gateway_core::account::AccountErrorProtection {
|
||||
|
||||
pub(super) fn settings_with_margin(refresh_margin_seconds: u64) -> RuntimeSettingsUpdate {
|
||||
RuntimeSettingsUpdate {
|
||||
openai_oauth_guardian_review_model: None,
|
||||
openai_request_country: None,
|
||||
request_profile_updates: BTreeMap::new(),
|
||||
rotation_strategy: "smart".to_owned(),
|
||||
model_mappings: BTreeMap::from([
|
||||
@@ -571,6 +573,73 @@ async fn retry_after_limit_defaults_and_updates_reach_the_runtime_snapshot() {
|
||||
database.close().await;
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn openai_request_settings_round_trip_into_snapshot_across_legacy_writes() {
|
||||
use gateway_core::account::OpenAiRequestCountry;
|
||||
use gateway_store::postgres::{PgRuntimeSnapshotRepository, RuntimeSnapshotRepository};
|
||||
let Some(database) = TestDatabase::create("openai_request_settings").await else {
|
||||
return;
|
||||
};
|
||||
let repository = PgRuntimeSettingsRepository::new(database.pool.clone());
|
||||
let snapshots = PgRuntimeSnapshotRepository::new(database.pool.clone());
|
||||
let initial = snapshots.load_runtime_snapshot().await.unwrap();
|
||||
assert!(initial.settings.openai_oauth_guardian_review_model());
|
||||
assert_eq!(
|
||||
initial.settings.openai_request_country(),
|
||||
&OpenAiRequestCountry::FollowLocation {}
|
||||
);
|
||||
|
||||
for (enabled, country) in [
|
||||
(
|
||||
true,
|
||||
OpenAiRequestCountry::Custom {
|
||||
country: "CA".into(),
|
||||
},
|
||||
),
|
||||
(true, OpenAiRequestCountry::FollowLocation {}),
|
||||
(false, OpenAiRequestCountry::Passthrough {}),
|
||||
(false, OpenAiRequestCountry::Default {}),
|
||||
] {
|
||||
let update = RuntimeSettingsUpdate {
|
||||
openai_oauth_guardian_review_model: Some(enabled),
|
||||
openai_request_country: Some(country.clone()),
|
||||
..settings_with_margin(600)
|
||||
};
|
||||
repository.update_runtime_settings(update).await.unwrap();
|
||||
let saved = repository.load_runtime_settings().await.unwrap();
|
||||
assert_eq!(saved.openai_oauth_guardian_review_model, enabled);
|
||||
assert_eq!(saved.openai_request_country, country);
|
||||
repository
|
||||
.update_runtime_settings(settings_with_margin(900))
|
||||
.await
|
||||
.unwrap();
|
||||
let snapshot = snapshots.load_runtime_snapshot().await.unwrap();
|
||||
assert_eq!(
|
||||
snapshot.settings.openai_oauth_guardian_review_model(),
|
||||
enabled
|
||||
);
|
||||
assert_eq!(snapshot.settings.openai_request_country(), &country);
|
||||
assert!(snapshot.config_revision > saved.config_revision);
|
||||
}
|
||||
for invalid in [
|
||||
serde_json::json!({"mode":"custom","country":"ca"}),
|
||||
serde_json::json!({"mode":"custom","country":null}),
|
||||
serde_json::json!({"mode":"custom","country":"CA","extra":1}),
|
||||
serde_json::json!({"mode":"follow_location","country":"CA"}),
|
||||
] {
|
||||
assert!(
|
||||
sqlx::query(
|
||||
"update runtime_settings set openai_request_country_json = $1 where id = 1"
|
||||
)
|
||||
.bind(sqlx::types::Json(invalid))
|
||||
.execute(&database.pool)
|
||||
.await
|
||||
.is_err()
|
||||
);
|
||||
}
|
||||
database.close().await;
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn concurrency_queue_settings_round_trip_into_the_runtime_snapshot() {
|
||||
use gateway_store::postgres::{PgRuntimeSnapshotRepository, RuntimeSnapshotRepository};
|
||||
|
||||
@@ -701,12 +701,12 @@ impl CodexCredentialCatalogService {
|
||||
.authentication
|
||||
.authorization_header()
|
||||
.map_err(CredentialRepositoryError::from)?;
|
||||
let mut client = client
|
||||
.for_account(account)
|
||||
let mut client = credential
|
||||
.authentication
|
||||
.model_client_for_account(client, account)
|
||||
.map_err(|error| CodexCredentialCatalogError::Upstream {
|
||||
detail: error.to_string(),
|
||||
})?
|
||||
.with_responses_api_base_url(credential.authentication.responses_api_base_url());
|
||||
})?;
|
||||
if let super::CodexRuntimeAuthentication::ApiKey(auth) = &credential.authentication
|
||||
&& auth.configuration.protocol == super::ApiKeyProtocol::Messages
|
||||
{
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
//! Codex 明文 credential JSON 的 schema 校验与日志脱敏边界
|
||||
|
||||
use super::api_key::ApiKeyAuthentication;
|
||||
use gateway_core::account::PlaintextCredential;
|
||||
use crate::transport::{CodexBackendClient, CodexClientResult};
|
||||
use gateway_core::account::{PlaintextCredential, ProviderAccount};
|
||||
use secrecy::{ExposeSecret, SecretString};
|
||||
use serde_json::Value;
|
||||
use thiserror::Error;
|
||||
@@ -53,12 +54,17 @@ impl CodexRuntimeAuthentication {
|
||||
}
|
||||
}
|
||||
|
||||
/// 传输只消费公开连接设置,不读取凭据枚举或秘密
|
||||
pub(crate) fn responses_api_base_url(&self) -> Option<&str> {
|
||||
match self {
|
||||
Self::ApiKey(auth) => Some(&auth.configuration.base_url),
|
||||
Self::OAuth(_) => None,
|
||||
}
|
||||
/// 出口、API 地址与 OAuth 路由声明必须来自同一个被选账号
|
||||
pub(crate) fn model_client_for_account(
|
||||
&self,
|
||||
client: &CodexBackendClient,
|
||||
account: &ProviderAccount,
|
||||
) -> CodexClientResult<CodexBackendClient> {
|
||||
let client = client.for_account(account)?;
|
||||
Ok(match self {
|
||||
Self::ApiKey(auth) => client.with_responses_api_base_url(&auth.configuration.base_url),
|
||||
Self::OAuth(secret) => client.with_fedramp_routing(secret.has_fedramp_routing_claim()),
|
||||
})
|
||||
}
|
||||
|
||||
pub fn authorization_header(&self) -> Result<SecretString, CodexCredentialDataError> {
|
||||
|
||||
@@ -249,7 +249,10 @@ impl CodexCredentialQuotaService {
|
||||
|
||||
let upstream_request = CodexResponsesRequest::from_body(body);
|
||||
let request_id = format!("warmup_{}", Uuid::now_v7().simple());
|
||||
let client_for_account = match client.for_account(account) {
|
||||
let client_for_account = match credential
|
||||
.authentication
|
||||
.model_client_for_account(client, account)
|
||||
{
|
||||
Ok(c) => c,
|
||||
Err(error) => {
|
||||
tracing::warn!(account_id = %account.id(), error = %error, "warmup client for account failed");
|
||||
|
||||
@@ -32,6 +32,17 @@ pub struct CodexOAuthSecret {
|
||||
pub id_token: Option<SecretString>,
|
||||
}
|
||||
|
||||
impl CodexOAuthSecret {
|
||||
/// 托管 OAuth 使用所选账号的 ID token;外部 ChatGPT token 可以只有 access token
|
||||
pub(crate) fn has_fedramp_routing_claim(&self) -> bool {
|
||||
let token = self.id_token.as_ref().unwrap_or(&self.access_token);
|
||||
decode_jwt_payload::<IdClaims>(token.expose_secret())
|
||||
.ok()
|
||||
.and_then(|claims| claims.auth)
|
||||
.is_some_and(|auth| auth.chatgpt_account_is_fedramp)
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Debug for CodexOAuthSecret {
|
||||
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
formatter
|
||||
@@ -168,8 +179,8 @@ struct AuthClaims {
|
||||
user_id: Option<String>,
|
||||
#[serde(default)]
|
||||
chatgpt_account_id: Option<String>,
|
||||
#[serde(rename = "chatgpt_account_is_fedramp", default)]
|
||||
_chatgpt_account_is_fedramp: bool,
|
||||
#[serde(default)]
|
||||
chatgpt_account_is_fedramp: bool,
|
||||
}
|
||||
|
||||
/// 与官方 `codex_protocol::auth::PlanType` 同构的 claims 反序列化类型
|
||||
|
||||
@@ -367,10 +367,10 @@ impl CodexProvider {
|
||||
});
|
||||
let events = cold_json_response_stream(ColdJsonResponse {
|
||||
installation_id,
|
||||
client: client
|
||||
.for_account(lease.account())
|
||||
client: lease
|
||||
.authentication()
|
||||
.model_client_for_account(&client, lease.account())
|
||||
.map_err(|error| map_client_error(error, UpstreamSendState::NotSent, None).error)?
|
||||
.with_responses_api_base_url(lease.authentication().responses_api_base_url())
|
||||
.with_middleware_headers(middleware_headers)
|
||||
.with_outbound_middleware(outbound_middleware)
|
||||
.with_privacy(context.privacy(), context.cancellation().clone()),
|
||||
@@ -446,6 +446,8 @@ pub(super) struct ColdJsonResponse {
|
||||
pub(super) struct OpenAiSessionState {
|
||||
pub(super) account_id: String,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub(super) reviewer_model: Option<String>,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub(super) credential_revision: Option<u64>,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub(super) oauth_identity: Option<String>,
|
||||
@@ -465,6 +467,7 @@ pub(super) enum OpenAiContinuationScope {
|
||||
|
||||
pub(super) struct OpenAiSessionCapture {
|
||||
pub(super) account_id: String,
|
||||
pub(super) reviewer_model: Option<String>,
|
||||
pub(super) credential_revision: Option<u64>,
|
||||
pub(super) oauth_identity: Option<String>,
|
||||
pub(super) conversation_id: Option<String>,
|
||||
@@ -506,6 +509,7 @@ fn encode_openai_session_capture(
|
||||
};
|
||||
encode_openai_session_state(OpenAiSessionState {
|
||||
account_id: capture.account_id.clone(),
|
||||
reviewer_model: capture.reviewer_model.clone(),
|
||||
credential_revision: capture.credential_revision,
|
||||
oauth_identity: capture.oauth_identity.clone(),
|
||||
conversation_id: capture.conversation_id.clone(),
|
||||
@@ -997,6 +1001,7 @@ pub(super) fn cold_response_stream(response: ColdResponse) -> EventStream {
|
||||
}
|
||||
let response_transport = response.transport;
|
||||
let routing_affinity = response.routing_affinity;
|
||||
let codex_oauth = matches!(lease.authentication(), crate::credential::CodexRuntimeAuthentication::OAuth(_));
|
||||
let websocket_connection_id = response.websocket_connection_id;
|
||||
let mut body = response.body;
|
||||
let mut failure_diagnostics = response.diagnostics.clone();
|
||||
@@ -1178,10 +1183,10 @@ pub(super) fn cold_response_stream(response: ColdResponse) -> EventStream {
|
||||
)
|
||||
});
|
||||
let timing_signals = decoder.take_timing_signals();
|
||||
if let Some(affinity) = &routing_affinity {
|
||||
if codex_oauth {
|
||||
for fact in events.iter().flat_map(ProviderEvent::canonical_facts) {
|
||||
if let GatewayEvent::Started(meta) = fact {
|
||||
guardian_routing.remember(context.client_api_key_ref(), &active_account, meta.response_id(), affinity);
|
||||
guardian_routing.remember(context.client_api_key_ref(), &active_account, meta.response_id(), routing_affinity.as_ref());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,28 +1,85 @@
|
||||
//! 按 Client Key 和账号关联父响应的连接路由快照,不持久化 Cookie 或改变选号
|
||||
|
||||
use std::{
|
||||
collections::HashMap,
|
||||
num::NonZeroUsize,
|
||||
sync::{Arc, Mutex},
|
||||
time::{Duration, Instant},
|
||||
};
|
||||
|
||||
use gateway_core::{account::ProviderAccount, policy::ClientApiKeyId};
|
||||
use lru::LruCache;
|
||||
use sha2::{Digest, Sha256};
|
||||
|
||||
use crate::transport::routing_affinity::ChatGptRoutingAffinity;
|
||||
|
||||
use gateway_protocol::openai::CODEX_AUTO_REVIEW_MODEL;
|
||||
|
||||
pub(super) fn review_model(
|
||||
enabled: bool,
|
||||
model: &gateway_core::routing::PublicModelId,
|
||||
operation: &gateway_core::operation::Operation,
|
||||
previous: Option<&gateway_core::operation::ProviderSessionState>,
|
||||
) -> Option<gateway_core::routing::UpstreamModelId> {
|
||||
let gateway_core::operation::Operation::Generate(request) = operation else {
|
||||
return None;
|
||||
};
|
||||
let raw_model = request.protocol_payload().body().get("model")?.as_str()?;
|
||||
if model.as_str() != raw_model || raw_model == CODEX_AUTO_REVIEW_MODEL || !is_reviewer(request)
|
||||
{
|
||||
return None;
|
||||
}
|
||||
// 已确认的续接沿用上一跳的实际模型,只有新审核使用本次请求冻结的设置
|
||||
let previous_model = previous
|
||||
.and_then(|state| state.payload().get("reviewer_model"))
|
||||
.and_then(serde_json::Value::as_str);
|
||||
previous_model
|
||||
.map_or(enabled, |previous| previous == CODEX_AUTO_REVIEW_MODEL)
|
||||
.then(|| {
|
||||
gateway_core::routing::UpstreamModelId::new(CODEX_AUTO_REVIEW_MODEL)
|
||||
.expect("valid reviewer model")
|
||||
})
|
||||
}
|
||||
|
||||
pub(super) fn is_reviewer(request: &gateway_core::operation::GenerateRequest) -> bool {
|
||||
use gateway_protocol::openai::{CodexGuardianRole, OPENAI_PROTOCOL, codex_guardian_role};
|
||||
let payload = request.protocol_payload();
|
||||
payload.protocol() == OPENAI_PROTOCOL
|
||||
&& codex_guardian_role(payload.body(), payload.context())
|
||||
== Some(CodexGuardianRole::Reviewer)
|
||||
}
|
||||
|
||||
pub(super) fn continuation_model_matches(
|
||||
previous: Option<&str>,
|
||||
next: &str,
|
||||
direct_model: bool,
|
||||
) -> bool {
|
||||
// 外部及旧版记录没有已确认的审核模型,不能将其原生 handle 交给自动选择的目标
|
||||
previous.map_or(!direct_model, |previous| previous == next)
|
||||
}
|
||||
|
||||
// 与连接续接 tombstone 使用相同保留窗口;最多 4096 份、每份两个 8 KiB Cookie
|
||||
const RETENTION: Duration = Duration::from_mins(30);
|
||||
const MAX_PARENT_RESPONSES: usize = 4096;
|
||||
|
||||
#[derive(Clone, Default)]
|
||||
#[derive(Clone)]
|
||||
pub(super) struct GuardianRouting {
|
||||
parents: Arc<Mutex<HashMap<[u8; 32], ParentRouting>>>,
|
||||
parents: Arc<Mutex<LruCache<[u8; 32], ParentRouting>>>,
|
||||
}
|
||||
|
||||
struct ParentRouting {
|
||||
#[derive(Clone)]
|
||||
pub(super) struct ParentRouting {
|
||||
captured_at: Instant,
|
||||
affinity: ChatGptRoutingAffinity,
|
||||
pub(super) affinity: Option<ChatGptRoutingAffinity>,
|
||||
}
|
||||
|
||||
impl Default for GuardianRouting {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
parents: Arc::new(Mutex::new(LruCache::new(
|
||||
NonZeroUsize::new(MAX_PARENT_RESPONSES).expect("positive parent limit"),
|
||||
))),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl GuardianRouting {
|
||||
@@ -31,18 +88,18 @@ impl GuardianRouting {
|
||||
client_key: &ClientApiKeyId,
|
||||
account: &ProviderAccount,
|
||||
response_id: &str,
|
||||
) -> Option<ChatGptRoutingAffinity> {
|
||||
) -> Option<ParentRouting> {
|
||||
let key = parent_key(client_key, account, response_id);
|
||||
let mut parents = self
|
||||
.parents
|
||||
.lock()
|
||||
.unwrap_or_else(std::sync::PoisonError::into_inner);
|
||||
let parent = parents.get(&key)?;
|
||||
let parent = parents.peek(&key)?;
|
||||
if parent.captured_at.elapsed() >= RETENTION {
|
||||
parents.remove(&key);
|
||||
parents.pop(&key);
|
||||
return None;
|
||||
}
|
||||
Some(parent.affinity.clone())
|
||||
Some(parent.clone())
|
||||
}
|
||||
|
||||
pub(super) fn remember(
|
||||
@@ -50,32 +107,21 @@ impl GuardianRouting {
|
||||
client_key: &ClientApiKeyId,
|
||||
account: &ProviderAccount,
|
||||
response_id: &str,
|
||||
affinity: &ChatGptRoutingAffinity,
|
||||
affinity: Option<&ChatGptRoutingAffinity>,
|
||||
) {
|
||||
let key = parent_key(client_key, account, response_id);
|
||||
let mut parents = self
|
||||
.parents
|
||||
.lock()
|
||||
.unwrap_or_else(std::sync::PoisonError::into_inner);
|
||||
if parents.contains_key(&key) {
|
||||
if parents.contains(&key) {
|
||||
return;
|
||||
}
|
||||
if parents.len() >= MAX_PARENT_RESPONSES {
|
||||
parents.retain(|_, parent| parent.captured_at.elapsed() < RETENTION);
|
||||
if parents.len() >= MAX_PARENT_RESPONSES
|
||||
&& let Some(oldest) = parents
|
||||
.iter()
|
||||
.min_by_key(|(_, parent)| parent.captured_at)
|
||||
.map(|(key, _)| *key)
|
||||
{
|
||||
parents.remove(&oldest);
|
||||
}
|
||||
}
|
||||
parents.insert(
|
||||
parents.put(
|
||||
key,
|
||||
ParentRouting {
|
||||
captured_at: Instant::now(),
|
||||
affinity: affinity.clone(),
|
||||
affinity: affinity.cloned(),
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
@@ -364,11 +364,13 @@ impl CodexProvider {
|
||||
.cloned()
|
||||
.collect::<Vec<_>>();
|
||||
let events = cold_live_call_stream(ColdLiveCall {
|
||||
client: self
|
||||
.client_for_request(&context, lease.account())?
|
||||
.for_account(lease.account())
|
||||
client: lease
|
||||
.authentication()
|
||||
.model_client_for_account(
|
||||
&self.client_for_request(&context, lease.account())?,
|
||||
lease.account(),
|
||||
)
|
||||
.map_err(|error| map_client_error(error, UpstreamSendState::NotSent, None).error)?
|
||||
.with_responses_api_base_url(lease.authentication().responses_api_base_url())
|
||||
.with_middleware_headers(middleware_headers)
|
||||
.with_outbound_middleware(outbound_middleware),
|
||||
registry: Arc::clone(&self.live_registry),
|
||||
|
||||
@@ -44,10 +44,10 @@ use gateway_core::task::{
|
||||
WorkerRunnable, WorkerSchedule, WorkerTaskError,
|
||||
};
|
||||
use gateway_core::upstream::{UpstreamSendState, UpstreamTransport};
|
||||
use gateway_protocol::openai::OPENAI_PROTOCOL;
|
||||
use gateway_protocol::openai::events::{
|
||||
ParsedRateLimits, parse_rate_limit_headers, rate_limits_to_header_pairs,
|
||||
};
|
||||
use gateway_protocol::openai::{CodexGuardianRole, OPENAI_PROTOCOL, codex_guardian_role};
|
||||
use reqwest::{Client, header::HeaderValue};
|
||||
use secrecy::{ExposeSecret, SecretString};
|
||||
use serde::{Deserialize, Serialize};
|
||||
@@ -87,7 +87,7 @@ use crate::transport::protocol::responses::{
|
||||
};
|
||||
use crate::transport::request::{
|
||||
CodexRequestEncodeError, RequestAccountScope, align_structured_location_fields,
|
||||
encode_generate_request, scope_request_to_account,
|
||||
encode_generate_payload, scope_request_to_account,
|
||||
};
|
||||
use crate::transport::session::CodexSessionIdentity;
|
||||
use crate::transport::usage::normalize_service_tier;
|
||||
@@ -173,7 +173,19 @@ impl PreparedGenerateRequest {
|
||||
fn checked_native_continuation_scope(
|
||||
&self,
|
||||
context: &AttemptContext,
|
||||
direct_model: bool,
|
||||
) -> Result<PreviousResponseScope, ProviderError> {
|
||||
if self.continuation_requested
|
||||
&& !guardian::continuation_model_matches(
|
||||
self.previous_session
|
||||
.as_ref()
|
||||
.and_then(|state| state.reviewer_model.as_deref()),
|
||||
self.upstream.model(),
|
||||
direct_model,
|
||||
)
|
||||
{
|
||||
return Err(continuation_replay_required_error("review_model_changed"));
|
||||
}
|
||||
let pin = context.continuation().and_then(ContinuationBinding::pinned);
|
||||
let scope = match self
|
||||
.previous_session
|
||||
@@ -221,6 +233,7 @@ struct SelectedGenerate {
|
||||
lease: CodexCredentialLease,
|
||||
account_selection_wait_ms: u64,
|
||||
frozen_requirements: CapabilityRequirements,
|
||||
direct_model: bool,
|
||||
}
|
||||
|
||||
impl CodexProvider {
|
||||
@@ -404,6 +417,21 @@ impl Provider for CodexProvider {
|
||||
crate::credential::is_direct_account_kind(authentication_kind)
|
||||
}
|
||||
|
||||
fn direct_account_model(
|
||||
&self,
|
||||
model: &gateway_core::routing::PublicModelId,
|
||||
operation: &Operation,
|
||||
previous: Option<&ProviderSessionState>,
|
||||
settings: &gateway_core::settings::SettingsValues,
|
||||
) -> Option<UpstreamModelId> {
|
||||
guardian::review_model(
|
||||
settings.openai_oauth_guardian_review_model(),
|
||||
model,
|
||||
operation,
|
||||
previous,
|
||||
)
|
||||
}
|
||||
|
||||
fn compile_privacy_policy(
|
||||
&self,
|
||||
policy: &gateway_core::settings::privacy::CodexPrivacyPolicy,
|
||||
@@ -492,7 +520,7 @@ impl Provider for CodexProvider {
|
||||
..Default::default()
|
||||
};
|
||||
};
|
||||
let Ok(encoded) = encode_generate_request(request, "observability", None) else {
|
||||
let Ok(encoded) = encode_generate_payload(request, "observability") else {
|
||||
return ProviderRequestObservation::default();
|
||||
};
|
||||
let previous_reasoning_effort_override = request
|
||||
@@ -629,7 +657,7 @@ impl Provider for CodexProvider {
|
||||
}
|
||||
// 跨协议转换必须在选号后执行;选号前只按各自协议读取明确会话身份
|
||||
let upstream = (generate.protocol_payload().protocol() == OPENAI_PROTOCOL)
|
||||
.then(|| encode_generate_request(generate, upstream_model.as_str(), None))
|
||||
.then(|| encode_generate_payload(generate, upstream_model.as_str()))
|
||||
.transpose()
|
||||
.map_err(map_request_error)?;
|
||||
// 固定调度约束:Guardian 分类先于选号,不能随原生/适配器发送路径改变
|
||||
@@ -644,11 +672,12 @@ impl Provider for CodexProvider {
|
||||
false,
|
||||
)
|
||||
});
|
||||
let direct_model = candidate.requires_direct_authentication();
|
||||
let preselection = upstream
|
||||
.filter(|_| !adapted)
|
||||
.map(|upstream| self.prepare_generate_request(generate, upstream, &context));
|
||||
if let Some(prepared) = preselection.as_ref() {
|
||||
prepared.checked_native_continuation_scope(&context)?;
|
||||
prepared.checked_native_continuation_scope(&context, direct_model)?;
|
||||
}
|
||||
let cache_preference = preselection
|
||||
.as_ref()
|
||||
@@ -730,6 +759,14 @@ impl Provider for CodexProvider {
|
||||
};
|
||||
let account_selection_wait_ms =
|
||||
u64::try_from(selection_started_at.elapsed().as_millis()).unwrap_or(u64::MAX);
|
||||
// 账号可能在快照编译后轮换认证方式;专用审核目标不能发给新换入的 API Key。
|
||||
if direct_model
|
||||
&& !crate::credential::is_direct_account_kind(lease.account().authentication_kind())
|
||||
{
|
||||
return Err(map_selection_error(
|
||||
CredentialSelectionError::NoEligibleCredential,
|
||||
));
|
||||
}
|
||||
if context.continuation_attempt() == ContinuationAttempt::RotateForClientReplay {
|
||||
// Core 已确认允许换号;选号器迁移共享绑定后,完整历史仍须由客户端提供
|
||||
// 不将旧账号的 previous_response_id 或增量输入发送给替代账号
|
||||
@@ -757,6 +794,13 @@ impl Provider for CodexProvider {
|
||||
let payload = generate.protocol_payload();
|
||||
let mut operation = Operation::Generate(generate.clone());
|
||||
if payload.protocol() == OPENAI_PROTOCOL
|
||||
&& !(adapter.is_none()
|
||||
&& matches!(
|
||||
lease.authentication(),
|
||||
crate::credential::CodexRuntimeAuthentication::OAuth(_)
|
||||
)
|
||||
&& codex_guardian_role(payload.body(), payload.context())
|
||||
== Some(CodexGuardianRole::Housekeeper))
|
||||
&& let Some(tier) = fast_policy_tier(
|
||||
context.fast_mode(),
|
||||
payload.body().get("service_tier").and_then(Value::as_str),
|
||||
@@ -813,6 +857,7 @@ impl Provider for CodexProvider {
|
||||
lease,
|
||||
account_selection_wait_ms,
|
||||
frozen_requirements,
|
||||
direct_model,
|
||||
},
|
||||
)
|
||||
.await
|
||||
@@ -836,6 +881,7 @@ impl CodexProvider {
|
||||
mut lease,
|
||||
account_selection_wait_ms,
|
||||
frozen_requirements,
|
||||
direct_model,
|
||||
} = selected;
|
||||
let Operation::Generate(generate) = operation else {
|
||||
return Err(provider_error(
|
||||
@@ -858,7 +904,7 @@ impl CodexProvider {
|
||||
"provider_input",
|
||||
None,
|
||||
);
|
||||
let mut upstream = encode_generate_request(&generate, upstream_model.as_str(), None)
|
||||
let mut upstream = encode_generate_payload(&generate, upstream_model.as_str())
|
||||
.map_err(map_request_error)?;
|
||||
gateway_protocol::anthropic::strip_foreign_reasoning_for_responses(upstream.body_mut());
|
||||
upstream.client_account_follow_only = crate::request_identity::follows_session_with_headers(
|
||||
@@ -872,11 +918,17 @@ impl CodexProvider {
|
||||
&middleware_headers,
|
||||
);
|
||||
let processed = self.prepare_generate_request(&generate, upstream, &context);
|
||||
let native_scope = processed.checked_native_continuation_scope(&context)?;
|
||||
let native_scope = processed.checked_native_continuation_scope(&context, direct_model)?;
|
||||
let mut upstream_request = processed.upstream;
|
||||
let session_transport_key =
|
||||
derive_codex_transport_key(&upstream_request, context.client_api_key_ref());
|
||||
let previous_session = processed.previous_session;
|
||||
let reviewer_model = (direct_model
|
||||
|| guardian::is_reviewer(&generate)
|
||||
|| previous_session
|
||||
.as_ref()
|
||||
.is_some_and(|state| state.reviewer_model.is_some()))
|
||||
.then(|| upstream_model.as_str().to_owned());
|
||||
let continuation_requested = processed.continuation_requested;
|
||||
if !context.is_diagnostic_required_account() {
|
||||
self.selector
|
||||
@@ -984,11 +1036,14 @@ impl CodexProvider {
|
||||
let state_before = trace
|
||||
.is_enabled()
|
||||
.then(|| crate::transport::diagnostics::evidence::request_state(&upstream_request));
|
||||
let codex_oauth = oauth_identity.is_some();
|
||||
let guardian_role = codex_oauth
|
||||
.then(|| upstream_request.oauth_guardian_role(&middleware_headers))
|
||||
.flatten();
|
||||
let guardian_parent = (!state_owner_cross_account
|
||||
&& upstream_request.is_guardian()
|
||||
&& matches!(
|
||||
lease.authentication(),
|
||||
crate::credential::CodexRuntimeAuthentication::OAuth(_)
|
||||
guardian_role,
|
||||
Some(CodexGuardianRole::Reviewer | CodexGuardianRole::Classifier)
|
||||
))
|
||||
.then(|| {
|
||||
upstream_request
|
||||
@@ -1010,14 +1065,19 @@ impl CodexProvider {
|
||||
);
|
||||
scope_request_to_account(&mut upstream_request, &installation_id, account_scope);
|
||||
// 父响应的可信关联只恢复此字段,不把首次 Guardian 请求的其他账号状态视为可信
|
||||
if let Some((response_id, _)) = &guardian_parent
|
||||
if guardian_role.is_some()
|
||||
&& let Some(Value::Object(metadata)) =
|
||||
upstream_request.body_mut().get_mut("client_metadata")
|
||||
{
|
||||
metadata.insert(
|
||||
"parent_response_id".to_owned(),
|
||||
Value::String(response_id.clone()),
|
||||
);
|
||||
metadata.remove("guardian_credits_requested");
|
||||
if let Some((response_id, _)) = &guardian_parent {
|
||||
metadata.insert(
|
||||
"parent_response_id".to_owned(),
|
||||
Value::String(response_id.clone()),
|
||||
);
|
||||
} else {
|
||||
metadata.remove("parent_response_id");
|
||||
}
|
||||
}
|
||||
if let Some(before) = state_before {
|
||||
trace.record_evidence("provider.request.account_scope", serde_json::json!({
|
||||
@@ -1034,11 +1094,13 @@ impl CodexProvider {
|
||||
"after": crate::transport::diagnostics::evidence::request_state(&upstream_request),
|
||||
}));
|
||||
}
|
||||
let codex_oauth = matches!(
|
||||
lease.authentication(),
|
||||
crate::credential::CodexRuntimeAuthentication::OAuth(_)
|
||||
);
|
||||
if codex_oauth {
|
||||
if matches!(
|
||||
guardian_role,
|
||||
Some(CodexGuardianRole::Reviewer | CodexGuardianRole::Classifier)
|
||||
) {
|
||||
upstream_request.body_mut().shift_remove("service_tier");
|
||||
}
|
||||
let responses_lite = upstream_request.responses_lite.as_deref() == Some("true");
|
||||
crate::compat::normalize_selected_codex_downstream_body(
|
||||
upstream_request.body_mut(),
|
||||
@@ -1051,7 +1113,11 @@ impl CodexProvider {
|
||||
.account()
|
||||
.request_location()
|
||||
.or(context.request_location());
|
||||
align_structured_location_fields(upstream_request.body_mut(), location);
|
||||
align_structured_location_fields(
|
||||
upstream_request.body_mut(),
|
||||
location,
|
||||
context.openai_request_country(),
|
||||
);
|
||||
let requirement = transport_requirement(&upstream_request);
|
||||
let http_only = lease.transport() == crate::credential::ResponsesTransport::Http;
|
||||
if http_only && requirement.requires_websocket() {
|
||||
@@ -1063,7 +1129,7 @@ impl CodexProvider {
|
||||
let requested_transport = if http_only {
|
||||
CodexProviderTransport::HttpOnly
|
||||
} else {
|
||||
selected_transport(&upstream_request)
|
||||
selected_transport(&upstream_request, guardian_role)
|
||||
};
|
||||
let session_http_fallback = requirement.allows_pre_send_http_fallback()
|
||||
&& session_transport_key
|
||||
@@ -1115,6 +1181,7 @@ impl CodexProvider {
|
||||
let session_capture =
|
||||
(!continuation_requested || previous_session.is_some()).then(|| OpenAiSessionCapture {
|
||||
account_id: lease.account_id().as_str().to_owned(),
|
||||
reviewer_model,
|
||||
oauth_identity,
|
||||
credential_revision: matches!(
|
||||
lease.authentication(),
|
||||
@@ -1143,11 +1210,15 @@ impl CodexProvider {
|
||||
};
|
||||
let events = cold_response_stream(ColdResponse {
|
||||
installation_id,
|
||||
client: client
|
||||
.for_account(lease.account())
|
||||
client: lease
|
||||
.authentication()
|
||||
.model_client_for_account(&client, lease.account())
|
||||
.map_err(|error| map_client_error(error, UpstreamSendState::NotSent, None).error)?
|
||||
.with_responses_api_base_url(lease.authentication().responses_api_base_url())
|
||||
.with_routing_affinity(guardian_parent.map(|(_, affinity)| affinity))
|
||||
.with_routing_affinity(
|
||||
guardian_parent
|
||||
.filter(|_| guardian_role == Some(CodexGuardianRole::Reviewer))
|
||||
.and_then(|(_, parent)| parent.affinity),
|
||||
)
|
||||
.with_connection_budget(context.connection_budget().clone())
|
||||
.with_middleware_headers(middleware_headers)
|
||||
.with_outbound_middleware(outbound_middleware)
|
||||
|
||||
@@ -643,8 +643,16 @@ pub(super) fn codex_model_presentation(model: &CodexCatalogModel) -> ModelPresen
|
||||
.with_hidden(hidden)
|
||||
}
|
||||
|
||||
pub(super) fn selected_transport(request: &CodexResponsesRequest) -> CodexProviderTransport {
|
||||
if request.force_http_sse {
|
||||
pub(super) fn selected_transport(
|
||||
request: &CodexResponsesRequest,
|
||||
guardian_role: Option<CodexGuardianRole>,
|
||||
) -> CodexProviderTransport {
|
||||
// 专用 sampler 的 HTTP 请求可能没有传输标记。显式 WS 和物理入站连接仍沿用原合同
|
||||
let dedicated_http = guardian_role.is_some_and(CodexGuardianRole::uses_sampler_transport)
|
||||
&& !request.use_websocket
|
||||
&& !request.websocket_frame
|
||||
&& request.downstream_websocket_connection_id.is_none();
|
||||
if request.force_http_sse || dedicated_http {
|
||||
CodexProviderTransport::HttpOnly
|
||||
} else {
|
||||
CodexProviderTransport::PreferWebSocket
|
||||
|
||||
@@ -43,7 +43,7 @@ use super::websocket::{
|
||||
|
||||
const UPSTREAM_CONNECT_TIMEOUT: Duration = Duration::from_secs(15);
|
||||
pub(super) const UPSTREAM_STREAM_IDLE_TIMEOUT: Duration = Duration::from_secs(5 * 60);
|
||||
const X_CODEX_WS_STREAM_REQUEST_START_MS_CLIENT_METADATA_KEY: &str =
|
||||
pub(super) const X_CODEX_WS_STREAM_REQUEST_START_MS_CLIENT_METADATA_KEY: &str =
|
||||
"x-codex-ws-stream-request-start-ms";
|
||||
type ReqwestClientCacheKey = (Option<String>, String, Duration);
|
||||
type ReqwestClientCache = Mutex<HashMap<ReqwestClientCacheKey, Client>>;
|
||||
@@ -699,6 +699,7 @@ pub struct CodexBackendClient {
|
||||
pub(super) base_url: String,
|
||||
pub(super) official_base_url: String,
|
||||
pub(super) protocol: OpenAiUpstreamProtocol,
|
||||
pub(super) fedramp_routing: bool,
|
||||
pub(super) profile: CodexWireProfileState,
|
||||
pub(super) websocket_pool: Option<Arc<CodexWebSocketPool>>,
|
||||
pub(super) websocket_origin_breaker: WebSocketOriginBreaker,
|
||||
@@ -743,6 +744,7 @@ impl CodexBackendClient {
|
||||
base_url,
|
||||
official_base_url: super::endpoints::OFFICIAL_CODEX_BASE_URL.to_owned(),
|
||||
protocol: OpenAiUpstreamProtocol::Codex,
|
||||
fedramp_routing: false,
|
||||
profile,
|
||||
websocket_pool: None,
|
||||
websocket_origin_breaker: WebSocketOriginBreaker::default(),
|
||||
@@ -852,21 +854,24 @@ impl CodexBackendClient {
|
||||
fallback.send().await.map_err(CodexClientError::HttpJson)
|
||||
}
|
||||
|
||||
pub(crate) fn with_responses_api_base_url(mut self, base_url: Option<&str>) -> Self {
|
||||
if let Some(base_url) = base_url {
|
||||
self.base_url = base_url.trim_end_matches('/').to_owned();
|
||||
self.protocol = OpenAiUpstreamProtocol::ResponsesApi;
|
||||
self.websocket_origin_key = format!(
|
||||
"{}:{}",
|
||||
websocket_origin_key(&self.base_url),
|
||||
self.egress_key
|
||||
);
|
||||
}
|
||||
pub(crate) fn with_fedramp_routing(mut self, enabled: bool) -> Self {
|
||||
self.fedramp_routing = enabled;
|
||||
self
|
||||
}
|
||||
|
||||
pub(crate) fn with_responses_api_base_url(mut self, base_url: &str) -> Self {
|
||||
self.base_url = base_url.trim_end_matches('/').to_owned();
|
||||
self.protocol = OpenAiUpstreamProtocol::ResponsesApi;
|
||||
self.websocket_origin_key = format!(
|
||||
"{}:{}",
|
||||
websocket_origin_key(&self.base_url),
|
||||
self.egress_key
|
||||
);
|
||||
self
|
||||
}
|
||||
|
||||
pub(crate) fn with_messages_api_base_url(self, base_url: &str) -> Self {
|
||||
let mut client = self.with_responses_api_base_url(Some(base_url));
|
||||
let mut client = self.with_responses_api_base_url(base_url);
|
||||
client.protocol = OpenAiUpstreamProtocol::MessagesApi;
|
||||
client
|
||||
}
|
||||
@@ -876,6 +881,7 @@ impl CodexBackendClient {
|
||||
account: &gateway_core::account::ProviderAccount,
|
||||
) -> Result<Self, CodexClientError> {
|
||||
let mut client = self.clone();
|
||||
client.fedramp_routing = false;
|
||||
client.outbound_proxy = account.outbound_proxy().cloned();
|
||||
client.egress_key = egress_key(account.id().as_str(), account.outbound_proxy());
|
||||
if account.authentication_kind() == crate::CODEX_AUTHENTICATION_KIND_API_KEY {
|
||||
@@ -1085,7 +1091,10 @@ pub(super) async fn http_json_upstream_error(
|
||||
// 请求辅助函数
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
pub(super) fn websocket_upstream_request(request: &CodexResponsesRequest) -> CodexResponsesRequest {
|
||||
pub(super) fn websocket_upstream_request(
|
||||
request: &CodexResponsesRequest,
|
||||
start_ms: &str,
|
||||
) -> CodexResponsesRequest {
|
||||
let mut request = request.clone();
|
||||
// 上游通过流式事件执行,下游仍按客户端原来的偏好返回响应
|
||||
if !request.stream() {
|
||||
@@ -1093,11 +1102,11 @@ pub(super) fn websocket_upstream_request(request: &CodexResponsesRequest) -> Cod
|
||||
.body_mut()
|
||||
.insert("stream".to_owned(), Value::Bool(true));
|
||||
}
|
||||
project_websocket_client_metadata(&mut request);
|
||||
project_websocket_client_metadata(&mut request, start_ms);
|
||||
request
|
||||
}
|
||||
|
||||
fn project_websocket_client_metadata(request: &mut CodexResponsesRequest) {
|
||||
fn project_websocket_client_metadata(request: &mut CodexResponsesRequest, start_ms: &str) {
|
||||
let mut metadata = match request.client_metadata() {
|
||||
Some(Value::Object(metadata)) => metadata.clone(),
|
||||
None => Map::new(),
|
||||
@@ -1122,12 +1131,12 @@ fn project_websocket_client_metadata(request: &mut CodexResponsesRequest) {
|
||||
}
|
||||
metadata.insert(
|
||||
X_CODEX_WS_STREAM_REQUEST_START_MS_CLIENT_METADATA_KEY.to_string(),
|
||||
Value::String(now_unix_timestamp_millis().to_string()),
|
||||
Value::String(start_ms.to_owned()),
|
||||
);
|
||||
request.set_client_metadata(Some(Value::Object(metadata)));
|
||||
}
|
||||
|
||||
fn now_unix_timestamp_millis() -> u128 {
|
||||
pub(super) fn now_unix_timestamp_millis() -> u128 {
|
||||
SystemTime::now()
|
||||
.duration_since(UNIX_EPOCH)
|
||||
.map(|duration| duration.as_millis())
|
||||
|
||||
@@ -9,7 +9,7 @@ use std::{
|
||||
|
||||
use futures::{StreamExt, TryStreamExt};
|
||||
use gateway_protocol::openai::{
|
||||
X_OPENAI_MEMGEN_REQUEST_HEADER,
|
||||
CodexGuardianRole, X_OPENAI_MEMGEN_REQUEST_HEADER,
|
||||
events::{self, retry_after_seconds_from_body},
|
||||
sse::{SseEventDecoder, SseFrame},
|
||||
};
|
||||
@@ -30,7 +30,7 @@ use crate::transport::{
|
||||
},
|
||||
websocket::{
|
||||
websocket_audit_artifact_from_attempt, websocket_connection_limit_failure,
|
||||
websocket_payload_audit_snapshot,
|
||||
websocket_payload_audit_snapshot, websocket_response_create_payload_text,
|
||||
},
|
||||
},
|
||||
response_meta,
|
||||
@@ -68,7 +68,6 @@ impl CodexBackendClient {
|
||||
if let Some(affinity) = &self.routing_affinity {
|
||||
affinity.apply(&endpoint, &mut headers);
|
||||
}
|
||||
// OAuth 请求遵循 Codex 压缩合同;API Key 上游使用普通 JSON
|
||||
// Codex 上游只交付 SSE;即使下游请求 `stream: false`,也要上游流式执行,
|
||||
// 再由 API 层收集 canonical events 并返回完整 JSON
|
||||
// 不能把下游的传输偏好
|
||||
@@ -76,7 +75,12 @@ impl CodexBackendClient {
|
||||
let mut upstream_body = upstream_request.body().clone();
|
||||
upstream_body.insert("stream".to_owned(), serde_json::Value::Bool(true));
|
||||
let mut upstream_body = serde_json::Value::Object(upstream_body);
|
||||
if self.protocol == OpenAiUpstreamProtocol::Codex {
|
||||
// Guardian 专用 HTTP 客户端使用默认 ResponsesOptions,普通 OAuth 及主审流使用 zstd
|
||||
let compress = self.protocol == OpenAiUpstreamProtocol::Codex
|
||||
&& !upstream_request
|
||||
.oauth_guardian_role(&self.middleware_headers)
|
||||
.is_some_and(CodexGuardianRole::uses_sampler_transport);
|
||||
if compress {
|
||||
headers.insert(CONTENT_ENCODING, HeaderValue::from_static("zstd"));
|
||||
}
|
||||
profile.apply_context_versions(&mut upstream_body);
|
||||
@@ -84,6 +88,9 @@ impl CodexBackendClient {
|
||||
&mut upstream_body,
|
||||
&mut headers,
|
||||
context.installation_id,
|
||||
(self.protocol == OpenAiUpstreamProtocol::Codex)
|
||||
.then_some(upstream_request.guardian_role)
|
||||
.flatten(),
|
||||
);
|
||||
self.apply_privacy(&mut upstream_body, &mut headers)?;
|
||||
let body =
|
||||
@@ -106,7 +113,7 @@ impl CodexBackendClient {
|
||||
Some(&headers),
|
||||
);
|
||||
}
|
||||
let body = if self.protocol == OpenAiUpstreamProtocol::Codex {
|
||||
let body = if compress {
|
||||
zstd::stream::encode_all(std::io::Cursor::new(body), 3)
|
||||
.map_err(CodexClientError::RequestCompression)?
|
||||
} else {
|
||||
@@ -263,7 +270,8 @@ impl CodexBackendClient {
|
||||
});
|
||||
}
|
||||
|
||||
let mut websocket_request = websocket_upstream_request(request);
|
||||
let initial_start_ms = now_unix_timestamp_millis().to_string();
|
||||
let mut websocket_request = websocket_upstream_request(request, &initial_start_ms);
|
||||
let profile = self.profile.snapshot();
|
||||
let mut headers =
|
||||
self.request_headers_for_websocket_response(&profile, &websocket_request, context)?;
|
||||
@@ -275,22 +283,16 @@ impl CodexBackendClient {
|
||||
}
|
||||
let mut body = serde_json::Value::Object(std::mem::take(websocket_request.body_mut()));
|
||||
profile.apply_context_versions(&mut body);
|
||||
super::correlation::scope_request(&mut body, &mut headers, context.installation_id);
|
||||
super::correlation::scope_request(
|
||||
&mut body,
|
||||
&mut headers,
|
||||
context.installation_id,
|
||||
(self.protocol == OpenAiUpstreamProtocol::Codex)
|
||||
.then_some(request.guardian_role)
|
||||
.flatten(),
|
||||
);
|
||||
let original_headers = self.privacy.as_ref().map(|_| headers.clone());
|
||||
self.apply_privacy(&mut body, &mut headers)?;
|
||||
if let Some(body) = body.as_object() {
|
||||
super::diagnostics::evidence::record_request(
|
||||
&context.trace.cloned().unwrap_or_default(),
|
||||
if self.outbound_middleware.is_some() {
|
||||
"provider.request.native"
|
||||
} else {
|
||||
"provider.request.outbound"
|
||||
},
|
||||
body,
|
||||
"websocket",
|
||||
Some(&headers),
|
||||
);
|
||||
}
|
||||
// 正文已归当前请求所有,移回对象避免再次复制完整对话
|
||||
let serde_json::Value::Object(body) = body else {
|
||||
return Err(CodexClientError::Privacy(
|
||||
@@ -301,23 +303,36 @@ impl CodexBackendClient {
|
||||
));
|
||||
};
|
||||
*websocket_request.body_mut() = body;
|
||||
let mut websocket_create = CodexWebSocketConnection::responses_create_request_for_path(
|
||||
let trace = context.trace.cloned().unwrap_or_default();
|
||||
let record_body = |stage, request: &CodexResponsesRequest| {
|
||||
super::diagnostics::evidence::record_request(
|
||||
&trace,
|
||||
stage,
|
||||
request.body(),
|
||||
"websocket",
|
||||
Some(&headers),
|
||||
);
|
||||
};
|
||||
if self.outbound_middleware.is_some() {
|
||||
record_body("provider.request.native", &websocket_request);
|
||||
}
|
||||
let mut opening = CodexWebSocketConnection::response_create_opening_for_path(
|
||||
&self.base_url,
|
||||
self.protocol.responses_path(),
|
||||
&generate_key(),
|
||||
websocket_header_pairs(&headers),
|
||||
&websocket_request,
|
||||
)
|
||||
.map_err(CodexClientError::WebSocketEncode)?;
|
||||
websocket_create.connection.outbound_proxy = self.outbound_proxy.clone();
|
||||
websocket_create.connection.connection_budget = self.connection_budget.clone();
|
||||
let (websocket_create, outbound_profile) =
|
||||
self.prepare_websocket_request(websocket_create).await?;
|
||||
);
|
||||
opening.connection.outbound_proxy = self.outbound_proxy.clone();
|
||||
opening.connection.connection_budget = self.connection_budget.clone();
|
||||
let (plugin_payload, outbound_profile) = self
|
||||
.prepare_websocket_request(&mut opening.connection, &websocket_request)
|
||||
.await?;
|
||||
context.trace.cloned().unwrap_or_default().headers(
|
||||
"upstream.request.headers",
|
||||
serde_json::json!({"transport": "websocket", "phase": "prepared_opening"}),
|
||||
websocket_create
|
||||
.connection()
|
||||
opening
|
||||
.connection
|
||||
.headers()
|
||||
.iter()
|
||||
.map(|(name, value)| (name.as_str(), value.as_bytes())),
|
||||
@@ -327,7 +342,7 @@ impl CodexBackendClient {
|
||||
if websocket_audit_dir().is_some() {
|
||||
let artifact = websocket_audit_artifact_from_attempt(
|
||||
&websocket_request,
|
||||
websocket_create.connection().opening_audit_snapshot(),
|
||||
opening.connection.opening_audit_snapshot(),
|
||||
// 插件准备后只由受控 wire capture 记录实际正文,不把原生 JSON 冒充最终帧
|
||||
self.outbound_middleware
|
||||
.is_none()
|
||||
@@ -349,8 +364,8 @@ impl CodexBackendClient {
|
||||
connection_profile.push_str(&profile);
|
||||
}
|
||||
// 上游按握手继承整条路径,不能把本轮请求交给其他路径建立的连接
|
||||
if let Some((_, route)) = websocket_create
|
||||
.connection()
|
||||
if let Some((_, route)) = opening
|
||||
.connection
|
||||
.headers()
|
||||
.iter()
|
||||
.find(|(name, _)| name == gateway_protocol::peer::ROUTE_HEADER)
|
||||
@@ -395,8 +410,8 @@ impl CodexBackendClient {
|
||||
format!(
|
||||
"{}\0{}",
|
||||
websocket_origin_key(
|
||||
&websocket_create
|
||||
.connection()
|
||||
&opening
|
||||
.connection
|
||||
.endpoint()
|
||||
.replacen("wss://", "https://", 1)
|
||||
.replacen("ws://", "http://", 1)
|
||||
@@ -405,7 +420,7 @@ impl CodexBackendClient {
|
||||
)
|
||||
});
|
||||
let prepared = prepare_response_create_request_with_pool(
|
||||
&websocket_create,
|
||||
&opening,
|
||||
pool,
|
||||
&self.websocket_origin_breaker,
|
||||
opening_origin_key
|
||||
@@ -466,6 +481,31 @@ impl CodexBackendClient {
|
||||
}
|
||||
Err(error) => return Err(websocket_exchange_error_to_client_error(error)),
|
||||
};
|
||||
let payload = match plugin_payload {
|
||||
Some(payload) => payload,
|
||||
None => {
|
||||
// 握手或租用完成后才标记发送时间。隐私规则移除或改写过的字段保持其最终值
|
||||
if let Some(stamp) = websocket_request
|
||||
.body_mut()
|
||||
.get_mut("client_metadata")
|
||||
.and_then(|metadata| {
|
||||
metadata.get_mut(X_CODEX_WS_STREAM_REQUEST_START_MS_CLIENT_METADATA_KEY)
|
||||
})
|
||||
&& stamp.as_str() == Some(initial_start_ms.as_str())
|
||||
{
|
||||
*stamp = serde_json::Value::String(now_unix_timestamp_millis().to_string());
|
||||
}
|
||||
record_body("provider.request.outbound", &websocket_request);
|
||||
match websocket_response_create_payload_text(&websocket_request) {
|
||||
Ok(payload) => payload,
|
||||
Err(error) => {
|
||||
prepared.discard().await;
|
||||
return Err(CodexClientError::WebSocketEncode(error));
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
let websocket_create = opening.with_payload(payload);
|
||||
// 按最终出站帧的 UTF-8 字节数判断,握手上限只属于当前物理连接
|
||||
if let Some(limit) = prepared.max_message_bytes()
|
||||
&& websocket_create.payload_text().len() > limit
|
||||
@@ -655,6 +695,7 @@ impl CodexBackendClient {
|
||||
.with_egress_key(&self.egress_key)
|
||||
.with_outbound_proxy(self.outbound_proxy.as_ref())
|
||||
.with_connection_profile(connection_profile)
|
||||
.with_fedramp_routing(self.fedramp_routing)
|
||||
.with_installation_id(context.installation_id);
|
||||
key = key.with_routing_affinity(self.routing_affinity.as_ref().map(|pin| pin.id()));
|
||||
if let Some(connection_id) = request.downstream_websocket_connection_id.as_deref() {
|
||||
@@ -687,6 +728,7 @@ fn websocket_connection_profile(
|
||||
X_OPENAI_MEMGEN_REQUEST_HEADER,
|
||||
"x-codex-guardian",
|
||||
"x-openai-internal-codex-residency",
|
||||
"x-openai-fedramp",
|
||||
"x-openai-account-routing-override",
|
||||
]
|
||||
.map(|name| {
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
//! 在出站副本中隔离账号的追踪标识,不改变本地亲和、续接引用或业务正文
|
||||
|
||||
use gateway_protocol::openai::CodexGuardianRole;
|
||||
use reqwest::header::{HeaderMap, HeaderValue};
|
||||
use serde_json::{Map, Value};
|
||||
use sha2::{Digest, Sha256};
|
||||
@@ -14,6 +15,7 @@ const ID_FIELDS: &[&str] = &[
|
||||
"window_id",
|
||||
"context_window_id",
|
||||
"parent_thread_id",
|
||||
"guardian_classifier_source_thread_id",
|
||||
"forked_from_thread_id",
|
||||
"conversation_id",
|
||||
"x-codex-window-id",
|
||||
@@ -177,10 +179,33 @@ pub(super) fn scope_headers(headers: &mut HeaderMap, installation_id: Option<&st
|
||||
}
|
||||
}
|
||||
|
||||
fn guardian_cache_key(
|
||||
metadata: &Value,
|
||||
role: CodexGuardianRole,
|
||||
cache: &str,
|
||||
installation_id: &str,
|
||||
) -> Option<String> {
|
||||
let (prefix, original) = cache.split_once(':')?;
|
||||
uuid::Uuid::parse_str(original).ok()?;
|
||||
let source = match (role, prefix) {
|
||||
(CodexGuardianRole::Reviewer, "guardian") => {
|
||||
metadata.get("x-codex-parent-thread-id")?.as_str()? == original
|
||||
}
|
||||
(CodexGuardianRole::Classifier, "guardian-v2") => {
|
||||
let turn: Value =
|
||||
serde_json::from_str(metadata.get("x-codex-turn-metadata")?.as_str()?).ok()?;
|
||||
turn.get("guardian_classifier_source_thread_id")?.as_str()? == original
|
||||
}
|
||||
_ => return None,
|
||||
};
|
||||
source.then(|| format!("{prefix}:{}", scoped_id(installation_id, original)))
|
||||
}
|
||||
|
||||
pub(super) fn scope_request(
|
||||
body: &mut Value,
|
||||
headers: &mut HeaderMap,
|
||||
installation_id: Option<&str>,
|
||||
guardian: Option<CodexGuardianRole>,
|
||||
) {
|
||||
scope_headers(headers, installation_id);
|
||||
let Some(installation_id) = installation_id else {
|
||||
@@ -188,7 +213,19 @@ pub(super) fn scope_request(
|
||||
};
|
||||
if let Some(object) = body.as_object_mut() {
|
||||
map_fields(object, installation_id, ID_FIELDS);
|
||||
map_fields(object, installation_id, &["prompt_cache_key"]);
|
||||
// 官方 Guardian 的复合缓存键引用父线程;自定义键仍作为一个整体隔离
|
||||
if let Some(cache) = object
|
||||
.get("prompt_cache_key")
|
||||
.and_then(Value::as_str)
|
||||
.filter(|v| !v.is_empty())
|
||||
{
|
||||
let mapped = guardian
|
||||
.and_then(|role| {
|
||||
guardian_cache_key(object.get("client_metadata")?, role, cache, installation_id)
|
||||
})
|
||||
.unwrap_or_else(|| scoped_id(installation_id, cache));
|
||||
object.insert("prompt_cache_key".into(), Value::from(mapped));
|
||||
}
|
||||
for key in METADATA_FIELDS {
|
||||
if let Some(value) = object.get_mut(*key) {
|
||||
scope_metadata(value, installation_id);
|
||||
|
||||
@@ -137,6 +137,9 @@ impl CodexBackendClient {
|
||||
}
|
||||
};
|
||||
let mut headers = build_codex_model_headers(profile, context.authorization, account_id)?;
|
||||
if self.fedramp_routing {
|
||||
headers.insert("x-openai-fedramp", HeaderValue::from_static("true"));
|
||||
}
|
||||
insert_optional_header(&mut headers, "cookie", cookie_header)?;
|
||||
Ok(headers)
|
||||
}
|
||||
@@ -161,6 +164,7 @@ impl CodexBackendClient {
|
||||
context: CodexRequestContext<'_>,
|
||||
) -> CodexClientResult<HeaderMap> {
|
||||
let mut headers = self.response_headers(profile, request, context)?;
|
||||
self.append_middleware_headers(&mut headers)?;
|
||||
headers.insert(ACCEPT, HeaderValue::from_static("text/event-stream"));
|
||||
headers.insert(CONTENT_TYPE, HeaderValue::from_static("application/json"));
|
||||
insert_optional_protocol_header(
|
||||
@@ -178,6 +182,9 @@ impl CodexBackendClient {
|
||||
context: CodexRequestContext<'_>,
|
||||
) -> CodexClientResult<HeaderMap> {
|
||||
let mut headers = self.response_headers(profile, request, context)?;
|
||||
// WebSocket 逐帧回传轮次状态,握手不能把某一轮的 token 绑定到整条连接
|
||||
headers.remove("x-codex-turn-state");
|
||||
self.append_middleware_headers(&mut headers)?;
|
||||
headers.insert(
|
||||
HeaderName::from_static("openai-beta"),
|
||||
HeaderValue::from_static("responses_websockets=2026-02-06"),
|
||||
@@ -222,13 +229,23 @@ impl CodexBackendClient {
|
||||
if let Some(subagent) = openai_subagent_from_metadata(request.client_metadata()) {
|
||||
insert_optional_protocol_header(&mut headers, "x-openai-subagent", Some(&subagent));
|
||||
}
|
||||
let routing_hint = match request.service_tier() {
|
||||
Some(tier) => format!("model={};tier={tier}", request.model()),
|
||||
None => format!("model={}", request.model()),
|
||||
};
|
||||
insert_optional_protocol_header(&mut headers, "x-codex-routing-hint", Some(&routing_hint));
|
||||
let guardian = (self.protocol == OpenAiUpstreamProtocol::Codex)
|
||||
.then(|| request.oauth_guardian_role(&self.middleware_headers))
|
||||
.flatten();
|
||||
if let Some(role) = guardian {
|
||||
insert_optional_protocol_header(&mut headers, "x-codex-guardian", role.header());
|
||||
} else {
|
||||
let routing_hint = match request.service_tier() {
|
||||
Some(tier) => format!("model={};tier={tier}", request.model()),
|
||||
None => format!("model={}", request.model()),
|
||||
};
|
||||
insert_optional_protocol_header(
|
||||
&mut headers,
|
||||
"x-codex-routing-hint",
|
||||
Some(&routing_hint),
|
||||
);
|
||||
}
|
||||
append_passthrough_headers(&mut headers, &request.passthrough_headers);
|
||||
self.append_middleware_headers(&mut headers)?;
|
||||
Ok(headers)
|
||||
}
|
||||
|
||||
@@ -252,7 +269,8 @@ impl CodexBackendClient {
|
||||
pub(super) fn append_passthrough_headers(headers: &mut HeaderMap, passthrough: &HeaderMap) {
|
||||
for name in passthrough.keys() {
|
||||
// 身份与传输字段只由画像/正文生成;其余协议头保留原始多值字节
|
||||
if is_managed_identity_header(name.as_str())
|
||||
if (name == "x-openai-fedramp" && headers.contains_key(name))
|
||||
|| is_managed_identity_header(name.as_str())
|
||||
|| matches!(
|
||||
name.as_str(),
|
||||
"openai-beta"
|
||||
|
||||
@@ -17,7 +17,10 @@ use tungstenite::http;
|
||||
|
||||
use super::{
|
||||
client::{CodexBackendClient, CodexClientError, CodexClientResult},
|
||||
websocket::CodexWebSocketRequest,
|
||||
protocol::{
|
||||
responses::CodexResponsesRequest, websocket::websocket_response_create_payload_text,
|
||||
},
|
||||
websocket::CodexWebSocketConnection,
|
||||
};
|
||||
|
||||
impl CodexBackendClient {
|
||||
@@ -104,13 +107,13 @@ impl CodexBackendClient {
|
||||
|
||||
pub(super) async fn prepare_websocket_request(
|
||||
&self,
|
||||
mut request: CodexWebSocketRequest,
|
||||
) -> CodexClientResult<(CodexWebSocketRequest, Option<String>)> {
|
||||
connection: &mut CodexWebSocketConnection,
|
||||
frame: &CodexResponsesRequest,
|
||||
) -> CodexClientResult<(Option<String>, Option<String>)> {
|
||||
let path = if self.peer_route.is_some()
|
||||
&& self.protocol != super::client::OpenAiUpstreamProtocol::Codex
|
||||
{
|
||||
let target =
|
||||
Url::parse(request.connection().endpoint()).map_err(|_| invalid_request())?;
|
||||
let target = Url::parse(connection.endpoint()).map_err(|_| invalid_request())?;
|
||||
self.route_to(&target)
|
||||
.map(http::HeaderValue::to_str)
|
||||
.transpose()
|
||||
@@ -118,33 +121,34 @@ impl CodexBackendClient {
|
||||
} else {
|
||||
None
|
||||
};
|
||||
request.connection.set_peer_route(path);
|
||||
self.prepare_websocket_outbound(request).await
|
||||
connection.set_peer_route(path);
|
||||
self.prepare_websocket_outbound(connection, frame).await
|
||||
}
|
||||
|
||||
async fn prepare_websocket_outbound(
|
||||
&self,
|
||||
mut request: CodexWebSocketRequest,
|
||||
) -> CodexClientResult<(CodexWebSocketRequest, Option<String>)> {
|
||||
connection: &mut CodexWebSocketConnection,
|
||||
frame: &CodexResponsesRequest,
|
||||
) -> CodexClientResult<(Option<String>, Option<String>)> {
|
||||
let Some(pipeline) = &self.outbound_middleware else {
|
||||
return Ok((request, None));
|
||||
return Ok((None, None));
|
||||
};
|
||||
let uri: http::Uri = request
|
||||
.connection()
|
||||
let uri: http::Uri = connection
|
||||
.endpoint()
|
||||
.parse()
|
||||
.map_err(|_| invalid_request())?;
|
||||
let outbound = Request {
|
||||
target: Target::WebSocket { uri: uri.clone() },
|
||||
headers: request
|
||||
.connection()
|
||||
headers: connection
|
||||
.headers()
|
||||
.iter()
|
||||
.map(|(name, value)| {
|
||||
MiddlewareHeader::new(name.clone(), Bytes::copy_from_slice(value.as_bytes()))
|
||||
})
|
||||
.collect(),
|
||||
body: request.take_payload_text().into(),
|
||||
body: websocket_response_create_payload_text(frame)
|
||||
.map_err(CodexClientError::WebSocketEncode)?
|
||||
.into(),
|
||||
};
|
||||
let original_headers = outbound
|
||||
.header_map()
|
||||
@@ -180,12 +184,11 @@ impl CodexBackendClient {
|
||||
.collect::<CodexClientResult<Vec<_>>>()?;
|
||||
let payload = String::from_utf8(prepared.body.into()).map_err(|_| invalid_request())?;
|
||||
// 只替换候选 opening 和正文;原请求的 continuation 归属不从插件正文重建
|
||||
request.replace_outbound(endpoint, headers, payload);
|
||||
request
|
||||
.connection
|
||||
connection.replace_outbound(endpoint, headers);
|
||||
connection
|
||||
.validate_prepared_opening()
|
||||
.map_err(|_| invalid_request())?;
|
||||
Ok((request, profile))
|
||||
Ok((Some(payload), profile))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -3,7 +3,9 @@
|
||||
use std::{fmt, time::Duration};
|
||||
|
||||
use gateway_core::account::FastMode;
|
||||
use gateway_core::engine::middleware::MiddlewareHeader;
|
||||
use gateway_protocol::openai::{
|
||||
CODEX_AUTO_REVIEW_MODEL, CodexGuardianRole,
|
||||
CodexResponsesRequestSemantics as CodexRequestSemantics,
|
||||
codex_responses_request_semantics_with_turn_metadata, events,
|
||||
};
|
||||
@@ -59,6 +61,8 @@ pub struct CodexResponsesRequest {
|
||||
body: Map<String, Value>,
|
||||
/// API 边界保存、Provider 逐条恢复的普通客户端请求头
|
||||
pub(crate) passthrough_headers: HeaderMap,
|
||||
/// 原始请求的 Guardian 身份,保留 HTTP 两侧冲突和 WS 帧边界的判定结果
|
||||
pub(crate) guardian_role: Option<CodexGuardianRole>,
|
||||
/// 是否由客户端显式提供了 prompt cache key
|
||||
pub explicit_prompt_cache_key: bool,
|
||||
/// 客户端会话 ID
|
||||
@@ -617,6 +621,7 @@ impl CodexResponsesRequest {
|
||||
Self {
|
||||
body,
|
||||
passthrough_headers: HeaderMap::new(),
|
||||
guardian_role: None,
|
||||
explicit_prompt_cache_key: false,
|
||||
client_conversation_id: None,
|
||||
client_session_id: None,
|
||||
@@ -801,6 +806,30 @@ impl CodexResponsesRequest {
|
||||
self.subagent_kind().as_deref() == Some(GUARDIAN_SUBAGENT_KIND)
|
||||
}
|
||||
|
||||
/// Codex 仅为实际选中的专用审核模型补 reviewer 标记,显式头及中间件仍有各自的覆盖权
|
||||
pub(crate) fn oauth_guardian_role(
|
||||
&self,
|
||||
middleware_headers: &[MiddlewareHeader],
|
||||
) -> Option<CodexGuardianRole> {
|
||||
const HEADER: &str = "x-codex-guardian";
|
||||
let header = middleware_headers
|
||||
.iter()
|
||||
.find(|header| header.name().eq_ignore_ascii_case(HEADER))
|
||||
.map(|header| header.value().as_ref())
|
||||
.or_else(|| self.passthrough_headers.get(HEADER).map(|v| v.as_bytes()));
|
||||
match header {
|
||||
Some(b"reviewer") => Some(CodexGuardianRole::Reviewer),
|
||||
Some(b"classifier") => Some(CodexGuardianRole::Classifier),
|
||||
Some(_) => None,
|
||||
None if self.guardian_role == Some(CodexGuardianRole::Reviewer)
|
||||
&& self.model() != CODEX_AUTO_REVIEW_MODEL =>
|
||||
{
|
||||
None
|
||||
}
|
||||
None => self.guardian_role,
|
||||
}
|
||||
}
|
||||
|
||||
/// 设置 / 合并 client metadata
|
||||
pub fn set_client_metadata(&mut self, client_metadata: Option<Value>) {
|
||||
match client_metadata {
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
use std::io;
|
||||
|
||||
use base64::{Engine as _, engine::general_purpose::STANDARD};
|
||||
use gateway_core::operation::GenerateRequest;
|
||||
use gateway_core::{account::OpenAiRequestCountry, operation::GenerateRequest};
|
||||
use gateway_protocol::openai::{
|
||||
OPENAI_PROTOCOL, WS_REQUEST_HEADER_RESPONSES_LITE_CLIENT_METADATA_KEY,
|
||||
is_transport_managed_request_header,
|
||||
@@ -106,14 +106,28 @@ pub fn encode_generate_request(
|
||||
request: &GenerateRequest,
|
||||
upstream_model: &str,
|
||||
location: Option<&CodexRequestLocation>,
|
||||
) -> Result<CodexResponsesRequest, CodexRequestEncodeError> {
|
||||
let mut encoded = encode_generate_payload(request, upstream_model)?;
|
||||
align_structured_location_fields(
|
||||
encoded.body_mut(),
|
||||
location,
|
||||
&OpenAiRequestCountry::default(),
|
||||
);
|
||||
Ok(encoded)
|
||||
}
|
||||
|
||||
// 选号前保留下游国家字段的缺省、null 与显式值,位置和国家策略在原生出站时统一应用
|
||||
pub(crate) fn encode_generate_payload(
|
||||
request: &GenerateRequest,
|
||||
upstream_model: &str,
|
||||
) -> Result<CodexResponsesRequest, CodexRequestEncodeError> {
|
||||
let payload = request.protocol_payload();
|
||||
if payload.protocol() != OPENAI_PROTOCOL {
|
||||
return Err(CodexRequestEncodeError::InvalidProtocolPayload);
|
||||
}
|
||||
let mut body = payload.body().clone();
|
||||
adapt_codex_responses_body(&mut body, upstream_model, location);
|
||||
|
||||
body.insert("model".to_owned(), Value::String(upstream_model.to_owned()));
|
||||
normalize_codex_request_body(&mut body);
|
||||
let mut encoded = CodexResponsesRequest::from_body(body);
|
||||
encoded.explicit_prompt_cache_key = encoded.prompt_cache_key().is_some();
|
||||
extract_request_context(&mut encoded);
|
||||
@@ -121,23 +135,26 @@ pub fn encode_generate_request(
|
||||
Ok(encoded)
|
||||
}
|
||||
|
||||
fn adapt_codex_responses_body(
|
||||
body: &mut Map<String, Value>,
|
||||
upstream_model: &str,
|
||||
location: Option<&CodexRequestLocation>,
|
||||
) {
|
||||
body.insert("model".to_owned(), Value::String(upstream_model.to_owned()));
|
||||
normalize_codex_request_body(body);
|
||||
align_structured_location_fields(body, location);
|
||||
}
|
||||
|
||||
pub(crate) fn align_structured_location_fields(
|
||||
body: &mut Map<String, Value>,
|
||||
location: Option<&CodexRequestLocation>,
|
||||
country: &OpenAiRequestCountry,
|
||||
) {
|
||||
// 请求国家由生效的位置覆盖决定,未配置时显式发送 null 表示未知
|
||||
let country = location.map(|location| location.country.as_str());
|
||||
body.insert("request_country".to_owned(), Value::from(country));
|
||||
match country {
|
||||
OpenAiRequestCountry::Default {} => {
|
||||
body.insert("request_country".into(), Value::Null);
|
||||
}
|
||||
OpenAiRequestCountry::Passthrough {} => {}
|
||||
OpenAiRequestCountry::FollowLocation {} => {
|
||||
body.insert(
|
||||
"request_country".into(),
|
||||
Value::from(location.map(|location| location.country.as_str())),
|
||||
);
|
||||
}
|
||||
OpenAiRequestCountry::Custom { country } => {
|
||||
body.insert("request_country".into(), Value::from(country.as_str()));
|
||||
}
|
||||
}
|
||||
let Some(location) = location else { return };
|
||||
if let Some(input) = body.get_mut("input").and_then(Value::as_array_mut) {
|
||||
for item in input {
|
||||
@@ -731,6 +748,7 @@ fn replace_metadata_field(metadata: &mut Map<String, Value>, key: &str, value: O
|
||||
// 优先;body 顶层别名只在 header 缺失时兜底(例如 WebSocket 帧无法逐轮携带
|
||||
// 请求头的场景)
|
||||
fn apply_protocol_context(request: &mut CodexResponsesRequest, context: &Map<String, Value>) {
|
||||
request.guardian_role = gateway_protocol::openai::codex_guardian_role(request.body(), context);
|
||||
request.passthrough_headers = decode_passthrough_headers(context);
|
||||
request.websocket_frame = context
|
||||
.get(gateway_protocol::openai::WEBSOCKET_FRAME_CONTEXT_KEY)
|
||||
|
||||
@@ -21,7 +21,7 @@ use super::{
|
||||
handshake::{connect_pumped_websocket, send_websocket_request, websocket_connection_metadata},
|
||||
model::{
|
||||
CodexWebSocketConnection, CodexWebSocketRequest, PreviousResponseUnavailableReason,
|
||||
WebSocketContinuationRequirement,
|
||||
ResponseCreateOpening, WebSocketContinuationRequirement,
|
||||
},
|
||||
pool::{
|
||||
self, CodexWebSocketPool, CodexWebSocketPoolKey, PooledWebSocketConnection,
|
||||
@@ -121,7 +121,7 @@ impl PreparedWebSocket {
|
||||
|
||||
/// 只建立或租用 WebSocket,不发送 payload
|
||||
pub(crate) async fn prepare_response_create_request_with_pool(
|
||||
request: &CodexWebSocketRequest,
|
||||
request: &ResponseCreateOpening,
|
||||
pool: Option<(
|
||||
&CodexWebSocketPool,
|
||||
Option<CodexWebSocketPoolKey>,
|
||||
@@ -135,7 +135,7 @@ pub(crate) async fn prepare_response_create_request_with_pool(
|
||||
) -> Result<WebSocketFastPath<PreparedWebSocket>, CodexWebSocketExchangeError> {
|
||||
let decision_started_at = Instant::now();
|
||||
let Some((pool, Some(key), downstream_cancellation)) = pool else {
|
||||
if require_pool || !request.continuation().permits_fresh_connection() {
|
||||
if require_pool || !request.continuation.permits_fresh_connection() {
|
||||
return Err(continuation_unavailable(
|
||||
PreviousResponseUnavailableReason::PoolUnavailable,
|
||||
));
|
||||
@@ -152,7 +152,7 @@ pub(crate) async fn prepare_response_create_request_with_pool(
|
||||
.await;
|
||||
};
|
||||
|
||||
let required_response_id = match request.continuation() {
|
||||
let required_response_id = match &request.continuation {
|
||||
WebSocketContinuationRequirement::ConnectionLocal { response_id } => {
|
||||
Some(response_id.as_str())
|
||||
}
|
||||
@@ -165,14 +165,14 @@ pub(crate) async fn prepare_response_create_request_with_pool(
|
||||
&key,
|
||||
required_response_id,
|
||||
request.codex_search_declared == Some(true),
|
||||
request.connection().outbound_proxy.as_ref(),
|
||||
request.connection.outbound_proxy.as_ref(),
|
||||
downstream_cancellation,
|
||||
)
|
||||
.await
|
||||
{
|
||||
WebSocketPoolAcquire::Reused { connection, lease } => {
|
||||
if let WebSocketContinuationRequirement::ConnectionLocal { response_id } =
|
||||
request.continuation()
|
||||
&request.continuation
|
||||
&& connection.continuation.latest_response_id() != Some(response_id.as_str())
|
||||
{
|
||||
lease.put(*connection).await;
|
||||
@@ -193,7 +193,7 @@ pub(crate) async fn prepare_response_create_request_with_pool(
|
||||
}))
|
||||
}
|
||||
WebSocketPoolAcquire::Connect(connect_lease) => {
|
||||
if !request.continuation().permits_fresh_connection() {
|
||||
if !request.continuation.permits_fresh_connection() {
|
||||
connect_lease.failed().await;
|
||||
return Err(continuation_unavailable(
|
||||
PreviousResponseUnavailableReason::FreshConnectionRequired,
|
||||
@@ -211,7 +211,7 @@ pub(crate) async fn prepare_response_create_request_with_pool(
|
||||
.await
|
||||
}
|
||||
WebSocketPoolAcquire::Wait(waiter) => {
|
||||
if require_pool || !request.continuation().permits_fresh_connection() {
|
||||
if require_pool || !request.continuation.permits_fresh_connection() {
|
||||
return Err(continuation_unavailable(
|
||||
PreviousResponseUnavailableReason::ConnectionBusy,
|
||||
));
|
||||
@@ -235,7 +235,7 @@ pub(crate) async fn prepare_response_create_request_with_pool(
|
||||
}
|
||||
|
||||
async fn prepare_unpooled_websocket(
|
||||
request: &CodexWebSocketRequest,
|
||||
request: &ResponseCreateOpening,
|
||||
breaker: &WebSocketOriginBreaker,
|
||||
origin_key: &str,
|
||||
fast_path_budget: Option<Duration>,
|
||||
@@ -244,20 +244,15 @@ async fn prepare_unpooled_websocket(
|
||||
capacity_pool: Option<&CodexWebSocketPool>,
|
||||
) -> Result<WebSocketFastPath<PreparedWebSocket>, CodexWebSocketExchangeError> {
|
||||
let proxy_permit = if let Some(pool) = capacity_pool {
|
||||
pool.acquire_proxy_capacity(request.connection().outbound_proxy.as_ref(), None)
|
||||
pool.acquire_proxy_capacity(request.connection.outbound_proxy.as_ref(), None)
|
||||
.await?
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let permit = acquire_breaker_permit(breaker, origin_key, fast_path_budget.is_some())?;
|
||||
let context = pump_log_context_from_connection(request.connection());
|
||||
let connected = connect_with_budget(
|
||||
request.connection(),
|
||||
fast_path_budget,
|
||||
context,
|
||||
proxy_permit,
|
||||
)
|
||||
.await;
|
||||
let context = pump_log_context_from_connection(&request.connection);
|
||||
let connected =
|
||||
connect_with_budget(&request.connection, fast_path_budget, context, proxy_permit).await;
|
||||
let connected = match connected {
|
||||
Ok(WebSocketFastPath::Ready(connection)) => Ok(connection),
|
||||
Ok(WebSocketFastPath::Missed) => {
|
||||
@@ -279,7 +274,7 @@ async fn prepare_unpooled_websocket(
|
||||
}
|
||||
|
||||
async fn prepare_pooled_websocket(
|
||||
request: &CodexWebSocketRequest,
|
||||
request: &ResponseCreateOpening,
|
||||
pool: &CodexWebSocketPool,
|
||||
connect_lease: WebSocketPoolConnectLease,
|
||||
breaker: &WebSocketOriginBreaker,
|
||||
@@ -295,7 +290,7 @@ async fn prepare_pooled_websocket(
|
||||
}
|
||||
};
|
||||
let mut waiter = start_pooled_websocket_connect(
|
||||
request.connection().clone(),
|
||||
request.connection.clone(),
|
||||
pool.clone(),
|
||||
connect_lease,
|
||||
permit,
|
||||
@@ -674,7 +669,7 @@ pub(crate) async fn execute_prepared_response_create_request_stream(
|
||||
created_at,
|
||||
continuation,
|
||||
initial_search_declared: initial_search_declared
|
||||
.unwrap_or(request.codex_search_declared.unwrap_or(false)),
|
||||
.unwrap_or(request.opening.codex_search_declared.unwrap_or(false)),
|
||||
});
|
||||
let mut exchange = stream_websocket_response(
|
||||
websocket,
|
||||
|
||||
@@ -27,7 +27,10 @@ use crate::{
|
||||
|
||||
use super::{
|
||||
error::CodexWebSocketExchangeError,
|
||||
model::{CodexWebSocketConnection, CodexWebSocketRequest, WebSocketContinuationRequirement},
|
||||
model::{
|
||||
CodexWebSocketConnection, CodexWebSocketRequest, ResponseCreateOpening,
|
||||
WebSocketContinuationRequirement,
|
||||
},
|
||||
pool::CodexWebSocketConnectionMetadata,
|
||||
pump::{PumpLogContext, PumpedWebSocket, RawWsStream},
|
||||
};
|
||||
@@ -102,34 +105,34 @@ impl CodexWebSocketConnection {
|
||||
business_headers: Vec<(String, String)>,
|
||||
request: &CodexResponsesRequest,
|
||||
) -> Result<CodexWebSocketRequest, serde_json::Error> {
|
||||
Self::responses_create_request_for_path(
|
||||
Ok(Self::response_create_opening_for_path(
|
||||
base_url,
|
||||
CODEX_RESPONSES_PATH,
|
||||
websocket_key,
|
||||
business_headers,
|
||||
request,
|
||||
)
|
||||
.with_payload(websocket_response_create_payload_text(request)?))
|
||||
}
|
||||
|
||||
pub(crate) fn responses_create_request_for_path(
|
||||
pub(crate) fn response_create_opening_for_path(
|
||||
base_url: &str,
|
||||
path: &str,
|
||||
websocket_key: &str,
|
||||
business_headers: Vec<(String, String)>,
|
||||
request: &CodexResponsesRequest,
|
||||
) -> Result<CodexWebSocketRequest, serde_json::Error> {
|
||||
) -> ResponseCreateOpening {
|
||||
let mut connection = Self::responses(base_url, websocket_key, business_headers);
|
||||
let endpoint = crate::transport::endpoint_url(base_url, path);
|
||||
connection.endpoint = endpoint
|
||||
.replacen("https://", "wss://", 1)
|
||||
.replacen("http://", "ws://", 1);
|
||||
Ok(CodexWebSocketRequest {
|
||||
ResponseCreateOpening {
|
||||
connection,
|
||||
payload_text: websocket_response_create_payload_text(request)?,
|
||||
continuation: WebSocketContinuationRequirement::from_request(request),
|
||||
codex_search_declared: (path == CODEX_RESPONSES_PATH)
|
||||
.then(|| crate::compat::declares_hosted_search(request.body())),
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -48,8 +48,14 @@ impl Eq for CodexWebSocketConnection {}
|
||||
/// 已构造完成的 Responses WebSocket 请求描述
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct CodexWebSocketRequest {
|
||||
pub(crate) connection: CodexWebSocketConnection,
|
||||
pub(crate) opening: ResponseCreateOpening,
|
||||
pub(super) payload_text: String,
|
||||
}
|
||||
|
||||
/// 建连与选择连接只读取路由和续接要求,原生推理帧在连接就绪后编码
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub(crate) struct ResponseCreateOpening {
|
||||
pub(crate) connection: CodexWebSocketConnection,
|
||||
pub(super) continuation: WebSocketContinuationRequirement,
|
||||
// 仅 Codex backend 约束首包搜索初始化,API Key 的 Responses API 不采用此规则
|
||||
pub(super) codex_search_declared: Option<bool>,
|
||||
@@ -127,24 +133,9 @@ impl fmt::Display for PreviousResponseUnavailableReason {
|
||||
}
|
||||
|
||||
impl CodexWebSocketRequest {
|
||||
pub(crate) fn take_payload_text(&mut self) -> String {
|
||||
std::mem::take(&mut self.payload_text)
|
||||
}
|
||||
|
||||
pub(crate) fn replace_outbound(
|
||||
&mut self,
|
||||
endpoint: String,
|
||||
headers: Vec<(String, String)>,
|
||||
payload: String,
|
||||
) {
|
||||
self.connection.endpoint = endpoint;
|
||||
self.connection.headers = headers;
|
||||
self.payload_text = payload;
|
||||
}
|
||||
|
||||
/// 返回连接描述
|
||||
pub fn connection(&self) -> &CodexWebSocketConnection {
|
||||
&self.connection
|
||||
&self.opening.connection
|
||||
}
|
||||
|
||||
/// 返回将要发送的完整 `response.create` 文本
|
||||
@@ -154,11 +145,25 @@ impl CodexWebSocketRequest {
|
||||
|
||||
/// 返回连接续接要求
|
||||
pub fn continuation(&self) -> &WebSocketContinuationRequirement {
|
||||
&self.continuation
|
||||
&self.opening.continuation
|
||||
}
|
||||
}
|
||||
|
||||
impl ResponseCreateOpening {
|
||||
pub(crate) fn with_payload(self, payload_text: String) -> CodexWebSocketRequest {
|
||||
CodexWebSocketRequest {
|
||||
opening: self,
|
||||
payload_text,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl CodexWebSocketConnection {
|
||||
pub(crate) fn replace_outbound(&mut self, endpoint: String, headers: Vec<(String, String)>) {
|
||||
self.endpoint = endpoint;
|
||||
self.headers = headers;
|
||||
}
|
||||
|
||||
pub(crate) fn set_peer_route(&mut self, path: Option<&str>) {
|
||||
self.headers
|
||||
.retain(|(name, _)| !name.eq_ignore_ascii_case(gateway_protocol::peer::ROUTE_HEADER));
|
||||
|
||||
@@ -26,6 +26,7 @@ pub struct CodexWebSocketPoolKey {
|
||||
connection_profile: String,
|
||||
installation_id: Option<String>,
|
||||
routing_affinity_id: Option<Uuid>,
|
||||
fedramp_routing: bool,
|
||||
pub(super) downstream_connection_id: String,
|
||||
egress_key: String,
|
||||
pub(super) proxy_capacity_key: String,
|
||||
@@ -45,6 +46,7 @@ impl CodexWebSocketPoolKey {
|
||||
connection_profile: String::new(),
|
||||
installation_id: None,
|
||||
routing_affinity_id: None,
|
||||
fedramp_routing: false,
|
||||
downstream_connection_id: String::new(),
|
||||
egress_key: String::new(),
|
||||
proxy_capacity_key: String::new(),
|
||||
@@ -81,6 +83,12 @@ impl CodexWebSocketPoolKey {
|
||||
self
|
||||
}
|
||||
|
||||
/// 凭据声明的 FedRAMP 路由也是精确续接的边界,不能把旧响应交给另一条路由
|
||||
pub(crate) fn with_fedramp_routing(mut self, enabled: bool) -> Self {
|
||||
self.fedramp_routing = enabled;
|
||||
self
|
||||
}
|
||||
|
||||
pub(crate) fn with_egress_key(mut self, key: &str) -> Self {
|
||||
self.egress_key = key.to_owned();
|
||||
self
|
||||
@@ -108,6 +116,7 @@ impl CodexWebSocketPoolKey {
|
||||
self.account_id.as_str(),
|
||||
self.conversation_id.as_str(),
|
||||
self.connection_profile.as_str(),
|
||||
if self.fedramp_routing { "fedramp" } else { "" },
|
||||
self.installation_id.as_deref().unwrap_or_default(),
|
||||
self.downstream_connection_id.as_str(),
|
||||
self.egress_key.as_str(),
|
||||
@@ -122,6 +131,7 @@ impl CodexWebSocketPoolKey {
|
||||
&& self.conversation_id == other.conversation_id
|
||||
&& self.egress_key == other.egress_key
|
||||
&& self.routing_affinity_id == other.routing_affinity_id
|
||||
&& self.fedramp_routing == other.fedramp_routing
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -486,5 +486,5 @@ async fn backend_websocket_should_preserve_business_headers_without_downstream_t
|
||||
assert!(values("x-codex-installation-id").is_empty());
|
||||
let session = values("session-id");
|
||||
assert!(uuid::Uuid::parse_str(std::str::from_utf8(&session[0]).unwrap()).is_ok());
|
||||
assert_eq!(values("x-codex-turn-state"), vec![b"turn-ascii".to_vec()]);
|
||||
assert!(values("x-codex-turn-state").is_empty());
|
||||
}
|
||||
|
||||
@@ -190,6 +190,171 @@ fn assert_isolated_request(
|
||||
assert_eq!(body["input"], isolation_history());
|
||||
}
|
||||
|
||||
fn fedramp_secret(access_token: &str) -> provider_openai::credential::CodexOAuthSecret {
|
||||
let mut credential = secret(access_token);
|
||||
let claims = base64::engine::general_purpose::URL_SAFE_NO_PAD
|
||||
.encode(br#"{"https://api.openai.com/auth":{"chatgpt_account_is_fedramp":true}}"#);
|
||||
credential.id_token = Some(format!("header.{claims}.signature").into());
|
||||
credential
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn rotated_fedramp_claim_opens_a_new_socket_for_the_same_account_and_session() {
|
||||
let store = Arc::new(MemoryAccountStore::default());
|
||||
create_account(&store, SELECTED_ACCOUNT).await;
|
||||
let server = RoutingUpstream::start(Some(Vec::new())).await;
|
||||
let provider = provider_with_base_url(&store, server.url.clone());
|
||||
let mut session = ProviderSessionState::new(
|
||||
"openai",
|
||||
Map::from_iter([
|
||||
("account_id".into(), json!(SELECTED_ACCOUNT)),
|
||||
("conversation_id".into(), json!("fedramp-session")),
|
||||
("continuation_scope".into(), json!("connection_local")),
|
||||
]),
|
||||
)
|
||||
.unwrap();
|
||||
for (id, exact) in [
|
||||
("resp_fedramp_seed", false),
|
||||
("resp_fedramp_reuse", false),
|
||||
("resp_old_handle", true),
|
||||
("resp_fedramp_rotated", false),
|
||||
] {
|
||||
if exact {
|
||||
let account = store
|
||||
.get_account(&ProviderAccountId::new(SELECTED_ACCOUNT).unwrap())
|
||||
.await
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
let rotated = fedramp_secret(&format!("at-{SELECTED_ACCOUNT}"));
|
||||
store
|
||||
.repository()
|
||||
.rotate_refreshed_oauth_secret(&account, rotated, None, None)
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
let mut body = Map::from_iter([
|
||||
("model".into(), json!("gpt-5.4")),
|
||||
("input".into(), json!([])),
|
||||
("store".into(), json!(false)),
|
||||
("client_metadata".into(), json!({"fixture_response_id": id})),
|
||||
]);
|
||||
let context = if exact {
|
||||
body.insert("previous_response_id".into(), json!("resp_fedramp_reuse"));
|
||||
pinned_continuation_context(
|
||||
"req_fedramp_exact",
|
||||
SELECTED_ACCOUNT,
|
||||
"resp_fedramp_reuse",
|
||||
"resp_fedramp_reuse",
|
||||
1,
|
||||
ContinuationAttempt::Native,
|
||||
)
|
||||
} else {
|
||||
isolation_context(None)
|
||||
};
|
||||
let payload = ProtocolPayload::json_object("openai", body)
|
||||
.unwrap()
|
||||
.with_context(Map::from_iter([
|
||||
("use_websocket".into(), json!(true)),
|
||||
(
|
||||
"downstream_websocket_connection_id".into(),
|
||||
json!("fedramp-connection"),
|
||||
),
|
||||
]));
|
||||
let operation = Operation::Generate(
|
||||
GenerateRequest::from_protocol_payload(payload)
|
||||
.with_provider_session_state(session.clone()),
|
||||
);
|
||||
let mut stream = Arc::clone(&provider)
|
||||
.execute(planned_request("openai", operation), context)
|
||||
.await
|
||||
.unwrap();
|
||||
if exact {
|
||||
let error = loop {
|
||||
match stream.next().await {
|
||||
Some(Ok(event)) => assert!(!event.has_client_event()),
|
||||
Some(Err(error)) => break error,
|
||||
None => panic!("old route must reject the exact continuation before sending"),
|
||||
}
|
||||
};
|
||||
assert_eq!(
|
||||
error.continuation_recovery_disposition(),
|
||||
Some(ContinuationRecoveryDisposition::ClientReplayRequired)
|
||||
);
|
||||
assert_eq!(error.send_state(), UpstreamSendState::NotSent);
|
||||
assert_eq!(
|
||||
error.continuation_unavailable_reason(),
|
||||
Some("fresh_connection_required")
|
||||
);
|
||||
} else {
|
||||
while let Some(event) = stream.next().await {
|
||||
let event = event.unwrap();
|
||||
if let Some(update) = event.session_update() {
|
||||
session = update.clone();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
let captured = server.requests.lock().unwrap();
|
||||
let [seed, reuse, fresh] = captured.as_slice() else {
|
||||
panic!("only the three compatible frames should be sent")
|
||||
};
|
||||
assert_eq!(seed.0, reuse.0);
|
||||
assert_ne!(reuse.0, fresh.0);
|
||||
assert!(!seed.1.contains_key("x-openai-fedramp"));
|
||||
assert_eq!(fresh.1["x-openai-fedramp"], "true");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn selected_oauth_fedramp_claim_sets_a_single_header_even_after_account_switch() {
|
||||
let store = Arc::new(MemoryAccountStore::default());
|
||||
store
|
||||
.seed_oauth_credential(ImportCodexOAuthCredential {
|
||||
account_id: SELECTED_ACCOUNT.to_owned(),
|
||||
name: SELECTED_ACCOUNT.to_owned(),
|
||||
secret: fedramp_secret("at-fedramp"),
|
||||
verified_account: profile("chatgpt-fedramp"),
|
||||
next_refresh_at: None,
|
||||
enabled: true,
|
||||
})
|
||||
.await;
|
||||
let server = MockServer::start().await;
|
||||
Mock::given(method("POST"))
|
||||
.and(path("/codex/responses"))
|
||||
.respond_with(
|
||||
ResponseTemplate::new(200)
|
||||
.insert_header("content-type", "text/event-stream")
|
||||
.set_body_string(CAPTURE_COMPLETED_SSE),
|
||||
)
|
||||
.expect(2)
|
||||
.mount(&server)
|
||||
.await;
|
||||
let provider = provider_with_base_url(&store, server.uri());
|
||||
for owner in [SELECTED_ACCOUNT, "acct_scope_old"] {
|
||||
let payload = isolation_payload();
|
||||
let mut metadata = payload.context().clone();
|
||||
metadata.insert("use_websocket".into(), json!(false));
|
||||
let operation = Operation::Generate(GenerateRequest::from_protocol_payload(
|
||||
payload.with_context(metadata),
|
||||
));
|
||||
let mut stream = provider
|
||||
.clone()
|
||||
.execute(
|
||||
planned_request("openai", operation),
|
||||
isolation_context(Some(owner)),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
while let Some(event) = stream.next().await {
|
||||
event.unwrap();
|
||||
}
|
||||
}
|
||||
for request in server.received_requests().await.unwrap() {
|
||||
let values: Vec<_> = request.headers.get_all("x-openai-fedramp").iter().collect();
|
||||
assert_eq!(values.len(), 1);
|
||||
assert_eq!(values[0], "true");
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn http_scopes_client_headers_and_guardian_parent_reference_to_the_account() {
|
||||
for owner in [Some(SELECTED_ACCOUNT), Some("acct_scope_old"), None] {
|
||||
@@ -515,8 +680,25 @@ async fn guardian_pins_follow_parent_connection_and_separate_reviewer_pool_reuse
|
||||
)
|
||||
.await;
|
||||
}
|
||||
let payload = ProtocolPayload::json_object("openai", json!({
|
||||
"model":"gpt-5.4","input":"hello","client_metadata":{
|
||||
"fixture_response_id":"classifier","parent_response_id":"parent_a","x-openai-subagent":"guardian",
|
||||
"x-codex-turn-metadata":r#"{"thread_source":"guardian_classifier","turn_trigger":"guardian_classifier"}"#}
|
||||
}).as_object().unwrap().clone()).unwrap().with_context(Map::from_iter([("use_websocket".to_owned(),json!(true))]));
|
||||
execute_routing(
|
||||
&provider,
|
||||
Operation::Generate(GenerateRequest::from_protocol_payload(payload)),
|
||||
routing_context("classifier", "key_routing", SELECTED_ACCOUNT),
|
||||
)
|
||||
.await;
|
||||
let requests = server.requests.lock().unwrap();
|
||||
assert_eq!(requests.len(), 5);
|
||||
assert_eq!(requests.len(), 6);
|
||||
assert_eq!(requests[5].1["x-codex-guardian"], "classifier");
|
||||
assert_eq!(
|
||||
requests[5].2["client_metadata"]["parent_response_id"],
|
||||
"parent_a"
|
||||
);
|
||||
assert!(!requests[5].1.contains_key("cookie"));
|
||||
assert_eq!(
|
||||
requests[2].0, requests[3].0,
|
||||
"same parent keeps the reviewer socket"
|
||||
@@ -536,6 +718,107 @@ async fn guardian_pins_follow_parent_connection_and_separate_reviewer_pool_reuse
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn guardian_structured_roles_use_selected_model_and_observed_http_parent_without_cookies() {
|
||||
let store = Arc::new(MemoryAccountStore::default());
|
||||
create_account(&store, "acct_provider_contract").await;
|
||||
let server = MockServer::start().await;
|
||||
Mock::given(method("POST"))
|
||||
.and(path("/codex/responses"))
|
||||
.respond_with(
|
||||
ResponseTemplate::new(200).set_body_raw(
|
||||
format!("data: {}\n\n{}", json!({"type":"response.created","response":{"id":"resp_scope_capture","model":"gpt-5.4"}}), CAPTURE_COMPLETED_SSE),
|
||||
"text/event-stream",
|
||||
),
|
||||
)
|
||||
.expect(5)
|
||||
.mount(&server)
|
||||
.await;
|
||||
let provider = provider_with_base_url(&store, server.uri());
|
||||
for (id, model, role, parent) in [
|
||||
("root", "gpt-5.4", None, None),
|
||||
(
|
||||
"review",
|
||||
"codex-auto-review",
|
||||
Some("guardian_review"),
|
||||
Some("resp_scope_capture"),
|
||||
),
|
||||
(
|
||||
"unobserved",
|
||||
"codex-auto-review",
|
||||
Some("guardian_review"),
|
||||
Some("unknown"),
|
||||
),
|
||||
(
|
||||
"classifier",
|
||||
"gpt-5.4",
|
||||
Some("guardian_classifier"),
|
||||
Some("resp_scope_capture"),
|
||||
),
|
||||
("custom", "gpt-5.4", Some("guardian_review"), None),
|
||||
] {
|
||||
let mut body = json!({"model":"gpt-5.6-luna","input":[]});
|
||||
if let Some(role) = role {
|
||||
body["client_metadata"] = json!({
|
||||
"x-openai-subagent":"guardian",
|
||||
"x-codex-turn-metadata": json!({"thread_source":role}).to_string(),
|
||||
"guardian_credits_requested":"true"
|
||||
});
|
||||
}
|
||||
if let Some(parent) = parent {
|
||||
body["client_metadata"]["parent_response_id"] = json!(parent);
|
||||
}
|
||||
let payload = ProtocolPayload::json_object("openai", body.as_object().unwrap().clone())
|
||||
.unwrap()
|
||||
.with_context(Map::from_iter([("use_websocket".to_owned(), json!(false))]));
|
||||
let op = Operation::Generate(GenerateRequest::from_protocol_payload(payload));
|
||||
let mut stream = Arc::clone(&provider)
|
||||
.execute(
|
||||
planned_request_with_presentation("openai", op, model, None),
|
||||
context(&format!("req_{id}"), CancellationToken::new()),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
while let Some(event) = stream.next().await {
|
||||
event.unwrap();
|
||||
}
|
||||
}
|
||||
let requests = server.received_requests().await.unwrap();
|
||||
for (index, expected_role, parent) in [
|
||||
(0, None, None),
|
||||
(1, Some("reviewer"), Some("resp_scope_capture")),
|
||||
(2, Some("reviewer"), None),
|
||||
(3, Some("classifier"), Some("resp_scope_capture")),
|
||||
(4, None, None),
|
||||
] {
|
||||
let request = &requests[index];
|
||||
let body = captured_request_body(request);
|
||||
assert_eq!(
|
||||
request
|
||||
.headers
|
||||
.get("x-codex-guardian")
|
||||
.map(|h| h.to_str().unwrap()),
|
||||
expected_role
|
||||
);
|
||||
assert_eq!(
|
||||
request.headers.contains_key("x-codex-routing-hint"),
|
||||
expected_role.is_none()
|
||||
);
|
||||
assert_eq!(
|
||||
body.pointer("/client_metadata/parent_response_id")
|
||||
.and_then(Value::as_str),
|
||||
parent
|
||||
);
|
||||
if expected_role.is_some() {
|
||||
assert!(
|
||||
body.pointer("/client_metadata/guardian_credits_requested")
|
||||
.is_none()
|
||||
);
|
||||
}
|
||||
assert!(!request.headers.contains_key("cookie"));
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn guardian_parent_pins_require_matching_client_key_and_selected_account() {
|
||||
let store = Arc::new(MemoryAccountStore::default());
|
||||
@@ -642,11 +925,9 @@ async fn guardian_ignores_expired_ambiguous_and_wrong_path_routing_cookies() {
|
||||
}
|
||||
let requests = server.requests.lock().unwrap();
|
||||
assert!(!requests[1].1.contains_key("cookie"));
|
||||
assert!(
|
||||
requests[1]
|
||||
.2
|
||||
.pointer("/client_metadata/parent_response_id")
|
||||
.is_none()
|
||||
assert_eq!(
|
||||
requests[1].2["client_metadata"]["parent_response_id"],
|
||||
"parent"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ const CONTEXT_WINDOW_ID: &str = "3b11bc82-c666-417d-b9ff-2a34fd040385";
|
||||
fn metadata(window_number: u64) -> Value {
|
||||
json!({"session_id":SESSION_ID, "thread_id":THREAD_ID, "turn_id":"source-turn",
|
||||
"parent_thread_id":SESSION_ID, "forked_from_thread_id":SESSION_ID,
|
||||
"guardian_classifier_source_thread_id":SESSION_ID,
|
||||
"parent_turn_id":"source-turn", "root_turn_id":"source-turn",
|
||||
"window_id":format!("{THREAD_ID}:{window_number}"), "window_number":window_number,
|
||||
"context_window_id":CONTEXT_WINDOW_ID, "future":{"keep":true},
|
||||
@@ -197,6 +198,10 @@ async fn account_correlation_preserves_turns_across_transports_and_account_switc
|
||||
}
|
||||
assert_eq!(full["parent_thread_id"], flat["session_id"]);
|
||||
assert_eq!(full["forked_from_thread_id"], flat["session_id"]);
|
||||
assert_eq!(
|
||||
full["guardian_classifier_source_thread_id"],
|
||||
flat["session_id"]
|
||||
);
|
||||
let mapped_thread = flat["thread_id"].as_str().unwrap();
|
||||
let expected_window = format!("{mapped_thread}:{window_number}");
|
||||
assert_eq!(headers["x-codex-window-id"], expected_window);
|
||||
@@ -390,6 +395,42 @@ async fn image_generation_and_edit_preserve_the_client_turn_identity() {
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn guardian_cache_keys_reference_the_scoped_source_thread_on_http_and_websocket() {
|
||||
for websocket in [false, true] {
|
||||
for (source, prefix, cache_id, linked) in [
|
||||
("guardian_review", "guardian", THREAD_ID, true),
|
||||
("guardian_classifier", "guardian-v2", THREAD_ID, true),
|
||||
("guardian_classifier", "guardian-v2", SESSION_ID, false),
|
||||
("guardian_classifier", "custom", THREAD_ID, false),
|
||||
("ordinary", "guardian", THREAD_ID, false),
|
||||
] {
|
||||
let cache = format!("{prefix}:{cache_id}");
|
||||
let body = capture_downstream_request(json!({
|
||||
"model":"gpt-5.4","input":[],"prompt_cache_key":cache,"client_metadata":{
|
||||
"x-openai-subagent":"guardian", "x-codex-parent-thread-id":THREAD_ID,
|
||||
"x-codex-turn-metadata":json!({"thread_source":source, "guardian_classifier_source_thread_id":THREAD_ID}).to_string()
|
||||
}
|
||||
}), false, websocket, false).await;
|
||||
let parent = body["client_metadata"]["x-codex-parent-thread-id"]
|
||||
.as_str()
|
||||
.unwrap();
|
||||
let turn: Value = serde_json::from_str(
|
||||
body["client_metadata"]["x-codex-turn-metadata"]
|
||||
.as_str()
|
||||
.unwrap(),
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(turn["guardian_classifier_source_thread_id"], parent);
|
||||
if linked {
|
||||
assert_eq!(body["prompt_cache_key"], format!("{prefix}:{parent}"));
|
||||
} else {
|
||||
assert_scoped_id(body["prompt_cache_key"].as_str().unwrap(), &cache);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn cache_key_preserves_explicit_sharing_and_omission_within_an_account() {
|
||||
let store = Arc::new(MemoryAccountStore::default());
|
||||
|
||||
@@ -0,0 +1,309 @@
|
||||
//! 同步审核的结构化身份、热更新路由与上游续接合同
|
||||
|
||||
use super::*;
|
||||
|
||||
const LUNA: &str = "gpt-5.6-luna";
|
||||
const REVIEW: &str = gateway_protocol::openai::CODEX_AUTO_REVIEW_MODEL;
|
||||
const FUTURE: &str = "custom-review-model";
|
||||
|
||||
fn reviewer_operation(model: &str, source: &str, subagent: &str, input: Value) -> Operation {
|
||||
let body = json!({
|
||||
"model": model, "input": input,
|
||||
"client_metadata": {
|
||||
"x-openai-subagent": subagent,
|
||||
"x-codex-turn-metadata": json!({"thread_source": source}).to_string()
|
||||
}
|
||||
});
|
||||
Operation::Generate(GenerateRequest::from_protocol_payload(
|
||||
ProtocolPayload::json_object("openai", body.as_object().unwrap().clone())
|
||||
.unwrap()
|
||||
.with_context(Map::from_iter([("use_websocket".into(), json!(false))])),
|
||||
))
|
||||
}
|
||||
|
||||
fn settings(enabled: bool) -> SettingsValues {
|
||||
SettingsValues::new(2, "smart", Default::default(), None, None)
|
||||
.with_openai_oauth_guardian_review_model(enabled)
|
||||
}
|
||||
|
||||
fn selected_session(model: &str) -> ProviderSessionState {
|
||||
ProviderSessionState::new(
|
||||
"openai",
|
||||
json!({"account_id":"acct_provider_contract","continuation_scope":"persisted","reviewer_model":model})
|
||||
.as_object().unwrap().clone(),
|
||||
).unwrap()
|
||||
}
|
||||
|
||||
fn reviewer_snapshot() -> RuntimeSnapshot {
|
||||
let provider = ProviderKind::new("openai").unwrap();
|
||||
RuntimeSnapshot::new(
|
||||
ConfigRevision::new(1).unwrap(),
|
||||
settings(false),
|
||||
vec![provider.clone()],
|
||||
[FUTURE, REVIEW]
|
||||
.map(|model| {
|
||||
ProviderModel::new(
|
||||
provider.clone(),
|
||||
UpstreamModelId::new(model).unwrap(),
|
||||
ModelCapabilities::new([OperationKind::Generate].into(), Some(32_000))
|
||||
.with_upstream_feature_validation(),
|
||||
)
|
||||
})
|
||||
.into(),
|
||||
vec![],
|
||||
)
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
fn plan_reviewer(
|
||||
snapshot: &RuntimeSnapshot,
|
||||
provider: &CodexProvider,
|
||||
operation: Operation,
|
||||
previous: Option<&ProviderSessionState>,
|
||||
) -> ProviderRequest {
|
||||
let requested = PublicModelId::new(FUTURE).unwrap();
|
||||
let openai = ProviderKind::new("openai").unwrap();
|
||||
let scope = Arc::new(FrozenAccountScope::new(
|
||||
Arc::new(RuntimeAccountDirectory::new(
|
||||
[(
|
||||
ProviderAccountId::new("acct_provider_contract").unwrap(),
|
||||
RuntimeAccount::new(openai.clone(), BTreeSet::new())
|
||||
.with_direct_authentication_kind(Some("oauth".into())),
|
||||
)]
|
||||
.into(),
|
||||
)),
|
||||
ClientRoutingScope::all_accounts(),
|
||||
));
|
||||
let context = RoutingContext::default();
|
||||
let plan = match provider.direct_account_model(
|
||||
&requested,
|
||||
&operation,
|
||||
previous,
|
||||
snapshot.settings(),
|
||||
) {
|
||||
Some(target) => snapshot.plan_with_direct_account_model(
|
||||
&requested,
|
||||
&operation,
|
||||
scope,
|
||||
&context,
|
||||
gateway_core::routing::DirectAccountModel {
|
||||
provider: &openai,
|
||||
model: &target,
|
||||
continuation_owner: None,
|
||||
},
|
||||
|_| true,
|
||||
),
|
||||
None => snapshot.plan(&requested, &operation, scope, &context),
|
||||
}
|
||||
.unwrap();
|
||||
ProviderRequest::new(operation, plan.candidates()[0].clone())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn guardian_model_matches_structured_reviewer_and_any_original_model() {
|
||||
let store = Arc::new(MemoryAccountStore::default());
|
||||
let provider = provider(&store);
|
||||
for requested in [LUNA, FUTURE] {
|
||||
let model = PublicModelId::new(requested).unwrap();
|
||||
for input in [json!([]), json!([{"role": "user", "content": requested}])] {
|
||||
let op = reviewer_operation(requested, "guardian_review", "guardian", input);
|
||||
assert!(
|
||||
provider
|
||||
.direct_account_model(&model, &op, None, &settings(false))
|
||||
.is_none()
|
||||
);
|
||||
assert_eq!(
|
||||
provider
|
||||
.direct_account_model(&model, &op, None, &settings(true))
|
||||
.unwrap()
|
||||
.as_str(),
|
||||
REVIEW
|
||||
);
|
||||
}
|
||||
}
|
||||
for (raw_model, routed_model, source, role) in [
|
||||
(FUTURE, FUTURE, "guardian_classifier", "guardian"),
|
||||
(FUTURE, FUTURE, "guardian_housekeeper", "guardian"),
|
||||
(FUTURE, FUTURE, "guardian_review", "review"),
|
||||
(FUTURE, FUTURE, "cli", "guardian"),
|
||||
(REVIEW, REVIEW, "guardian_review", "guardian"),
|
||||
(FUTURE, "admin-routed", "guardian_review", "guardian"),
|
||||
] {
|
||||
let op = reviewer_operation(raw_model, source, role, json!([]));
|
||||
assert!(
|
||||
provider
|
||||
.direct_account_model(
|
||||
&PublicModelId::new(routed_model).unwrap(),
|
||||
&op,
|
||||
None,
|
||||
&settings(true),
|
||||
)
|
||||
.is_none()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn guardian_model_rejects_unknown_or_changed_continuations_before_account_selection() {
|
||||
let store = Arc::new(MemoryAccountStore::default());
|
||||
let provider = provider(&store);
|
||||
let snapshot = reviewer_snapshot();
|
||||
for (enabled, previous) in [(true, None), (true, Some(FUTURE)), (false, Some(REVIEW))] {
|
||||
let op = continuing_reviewer(previous);
|
||||
let request = plan_reviewer(
|
||||
&snapshot.with_settings(&settings(enabled)).unwrap(),
|
||||
&provider,
|
||||
op,
|
||||
None,
|
||||
);
|
||||
let result = Arc::clone(&provider)
|
||||
.execute(request, external_continuation_context("req_guardian_model"))
|
||||
.await;
|
||||
let error = match result {
|
||||
Err(err) => err,
|
||||
Ok(_) => panic!("unexpected call"),
|
||||
};
|
||||
assert_eq!(
|
||||
error.continuation_unavailable_reason(),
|
||||
Some("review_model_changed")
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
fn continuing_reviewer(previous: Option<&str>) -> Operation {
|
||||
let Operation::Generate(base) =
|
||||
reviewer_operation(FUTURE, "guardian_review", "guardian", json!([]))
|
||||
else {
|
||||
unreachable!()
|
||||
};
|
||||
let payload = base.protocol_payload();
|
||||
let mut body = payload.body().clone();
|
||||
body.insert(
|
||||
"previous_response_id".into(),
|
||||
json!("external-previous-response"),
|
||||
);
|
||||
let mut request = GenerateRequest::from_protocol_payload(
|
||||
ProtocolPayload::json_object("openai", body)
|
||||
.unwrap()
|
||||
.with_context(payload.context().clone()),
|
||||
);
|
||||
if let Some(previous) = previous {
|
||||
request = request.with_provider_session_state(selected_session(previous));
|
||||
}
|
||||
Operation::Generate(request)
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn guardian_samplers_keep_the_http_transport_chosen_by_the_client() {
|
||||
let store = Arc::new(MemoryAccountStore::default());
|
||||
create_account(&store, "acct_provider_contract").await;
|
||||
let server = MockServer::start().await;
|
||||
Mock::given(method("POST"))
|
||||
.and(path("/codex/responses"))
|
||||
.respond_with(
|
||||
ResponseTemplate::new(200).set_body_raw(CAPTURE_COMPLETED_SSE, "text/event-stream"),
|
||||
)
|
||||
.expect(2)
|
||||
.mount(&server)
|
||||
.await;
|
||||
let provider = provider_with_base_url(&store, server.uri());
|
||||
for turn in [
|
||||
json!({"thread_source":"guardian_classifier", "turn_trigger":"guardian_classifier"}),
|
||||
json!({"turn_trigger":"guardian_housekeeper"}),
|
||||
] {
|
||||
let body = json!({"model":LUNA, "input":[], "stream":true, "store":false,
|
||||
"client_metadata":{"x-openai-subagent":"guardian", "x-codex-turn-metadata":turn.to_string()}});
|
||||
let payload = ProtocolPayload::json_object("openai", body.as_object().unwrap().clone())
|
||||
.unwrap()
|
||||
.with_context(Map::from_iter([
|
||||
("subagent".into(), json!("guardian")),
|
||||
("turn_metadata".into(), json!(turn.to_string())),
|
||||
]));
|
||||
let operation = Operation::Generate(GenerateRequest::from_protocol_payload(payload));
|
||||
let mut stream = Arc::clone(&provider)
|
||||
.execute(
|
||||
planned_request("openai", operation),
|
||||
context("req_sampler_http", CancellationToken::new()),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
while let Some(event) = stream.next().await {
|
||||
event.unwrap();
|
||||
}
|
||||
}
|
||||
let requests = server.received_requests().await.unwrap();
|
||||
assert_eq!(
|
||||
requests.len(),
|
||||
2,
|
||||
"both sampler POSTs should bypass a fresh WS opening"
|
||||
);
|
||||
assert!(
|
||||
requests
|
||||
.iter()
|
||||
.all(|r| !r.headers.contains_key("content-encoding"))
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn guardian_model_freezes_the_selected_model_across_settings_changes_and_continuations() {
|
||||
let store = Arc::new(MemoryAccountStore::default());
|
||||
create_account(&store, "acct_provider_contract").await;
|
||||
let server = MockServer::start().await;
|
||||
Mock::given(method("POST"))
|
||||
.and(path("/codex/responses"))
|
||||
.respond_with(
|
||||
ResponseTemplate::new(200).set_body_raw(CAPTURE_COMPLETED_SSE, "text/event-stream"),
|
||||
)
|
||||
.expect(4)
|
||||
.mount(&server)
|
||||
.await;
|
||||
let provider = provider_with_base_url(&store, server.uri());
|
||||
let off = reviewer_snapshot();
|
||||
let on = off.with_settings(&settings(true)).unwrap();
|
||||
for (snapshot, previous, expected) in [
|
||||
(&off, None, FUTURE),
|
||||
(&on, None, REVIEW),
|
||||
(&off, Some(REVIEW), REVIEW),
|
||||
(&on, Some(FUTURE), FUTURE),
|
||||
] {
|
||||
let operation = match previous {
|
||||
Some(model) => continuing_reviewer(Some(model)),
|
||||
None => reviewer_operation(
|
||||
FUTURE,
|
||||
"guardian_review",
|
||||
"guardian",
|
||||
json!([{"role":"user","content":FUTURE}]),
|
||||
),
|
||||
};
|
||||
let request = plan_reviewer(
|
||||
snapshot,
|
||||
&provider,
|
||||
operation,
|
||||
previous.map(selected_session).as_ref(),
|
||||
);
|
||||
let ctx = if previous.is_some() {
|
||||
external_continuation_context("req_guardian_wire")
|
||||
} else {
|
||||
context("req_guardian_wire", CancellationToken::new())
|
||||
};
|
||||
let mut stream = Arc::clone(&provider).execute(request, ctx).await.unwrap();
|
||||
assert_eq!(
|
||||
stream.metadata().upstream_model().unwrap().as_str(),
|
||||
expected
|
||||
);
|
||||
let mut session = None;
|
||||
while let Some(event) = stream.next().await {
|
||||
if let Some(state) = event.unwrap().session_update() {
|
||||
session = state.payload().get("reviewer_model").cloned();
|
||||
}
|
||||
}
|
||||
assert_eq!(session, Some(json!(expected)));
|
||||
let sent = captured_request_body(server.received_requests().await.unwrap().last().unwrap());
|
||||
assert_eq!(sent["model"], expected);
|
||||
if previous.is_some() {
|
||||
assert_eq!(sent["previous_response_id"], "external-previous-response");
|
||||
} else {
|
||||
assert_eq!(sent["input"][0]["content"], FUTURE);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -8,6 +8,7 @@ mod catalog_startup;
|
||||
mod cookies;
|
||||
mod correlation;
|
||||
mod error_details;
|
||||
mod guardian_model;
|
||||
mod image_model_access;
|
||||
mod messages;
|
||||
mod model_account_eligibility;
|
||||
@@ -39,8 +40,8 @@ use chrono::Utc;
|
||||
use futures::{SinkExt, StreamExt, future::BoxFuture};
|
||||
use gateway_core::account::{
|
||||
AccountFeedbackStats, AccountWeight, CredentialState, FastMode, OpaqueProviderData,
|
||||
ProviderAccountId, ProviderAccountStore as _, QuotaAccessChange, QuotaAccessState,
|
||||
QuotaEvidence, QuotaObservation, QuotaState,
|
||||
OpenAiRequestCountry, ProviderAccountId, ProviderAccountStore as _, QuotaAccessChange,
|
||||
QuotaAccessState, QuotaEvidence, QuotaObservation, QuotaState,
|
||||
};
|
||||
use gateway_core::engine::continuation::{
|
||||
ContinuationBinding, NativeContinuationPin, PreviousResponseId,
|
||||
@@ -796,7 +797,7 @@ async fn selected_proxy_location_overrides_global_and_reloads_without_mutating_c
|
||||
].into_iter().enumerate() {
|
||||
let location = location.map(|value| serde_json::from_value::<RequestLocation>(value).unwrap());
|
||||
store.set_egress(account_id, Some(OutboundProxy::parse(&proxy.uri()).unwrap()), location);
|
||||
let mut stream = Arc::clone(&provider).execute(planned_request("openai", operation.clone()), context_with_state_owner_and_location(&format!("req_proxy_location_{index}"), account_id, global_enabled.then(global_request_location))).await.expect("prepare");
|
||||
let mut stream = Arc::clone(&provider).execute(planned_request("openai", operation.clone()), context_with_request_settings(&format!("req_proxy_location_{index}"), account_id, global_enabled.then(global_request_location), None, OpenAiRequestCountry::FollowLocation {})).await.expect("prepare");
|
||||
while let Some(event) = stream.next().await { event.expect("proxy response"); }
|
||||
let requests = proxy.received_requests().await.unwrap();
|
||||
let body = captured_request_body(requests.last().expect("request reached selected proxy"));
|
||||
@@ -980,6 +981,18 @@ async fn capture_downstream_request(
|
||||
api_key: bool,
|
||||
websocket: bool,
|
||||
lite: bool,
|
||||
) -> Value {
|
||||
capture_downstream_with_country(original, api_key, websocket, lite, None, Default::default())
|
||||
.await
|
||||
}
|
||||
|
||||
async fn capture_downstream_with_country(
|
||||
original: Value,
|
||||
api_key: bool,
|
||||
websocket: bool,
|
||||
lite: bool,
|
||||
location: Option<gateway_core::account::RequestLocation>,
|
||||
country: OpenAiRequestCountry,
|
||||
) -> Value {
|
||||
let http = MockServer::start().await;
|
||||
let (base_url, websocket_server) = if websocket {
|
||||
@@ -1046,10 +1059,12 @@ async fn capture_downstream_request(
|
||||
let mut stream = provider
|
||||
.execute(
|
||||
planned_request("openai", operation),
|
||||
context_with_state_owner_and_location(
|
||||
context_with_request_settings(
|
||||
"req_replay_compatibility",
|
||||
"acct_provider_contract",
|
||||
location,
|
||||
None,
|
||||
country,
|
||||
),
|
||||
)
|
||||
.await
|
||||
@@ -1817,14 +1832,21 @@ fn context_with_state_owner_and_location(
|
||||
owner_account_id: &str,
|
||||
location: Option<gateway_core::account::RequestLocation>,
|
||||
) -> AttemptContext {
|
||||
context_with_state_owner_and_profile(request_id, owner_account_id, location, None)
|
||||
context_with_request_settings(
|
||||
request_id,
|
||||
owner_account_id,
|
||||
location,
|
||||
None,
|
||||
Default::default(),
|
||||
)
|
||||
}
|
||||
|
||||
fn context_with_state_owner_and_profile(
|
||||
fn context_with_request_settings(
|
||||
request_id: &str,
|
||||
owner_account_id: &str,
|
||||
location: Option<gateway_core::account::RequestLocation>,
|
||||
profile: Option<OpaqueProviderData>,
|
||||
country: OpenAiRequestCountry,
|
||||
) -> AttemptContext {
|
||||
let owner = ProviderAccountStateOwner::new(
|
||||
ProviderKind::new("openai").expect("provider"),
|
||||
@@ -1836,6 +1858,7 @@ fn context_with_state_owner_and_profile(
|
||||
ClientApiKeyId::new("key_openai_contract").expect("client key id"),
|
||||
)
|
||||
.with_request_location(location)
|
||||
.with_openai_request_country(country)
|
||||
.with_request_profile(profile),
|
||||
NonZeroU32::new(1).expect("attempt"),
|
||||
SystemTime::now() + Duration::from_secs(30),
|
||||
@@ -6544,11 +6567,12 @@ async fn websocket_account_scoping_keeps_installation_in_metadata() {
|
||||
"openai",
|
||||
Operation::Generate(GenerateRequest::from_protocol_payload(payload)),
|
||||
),
|
||||
context_with_state_owner_and_profile(
|
||||
context_with_request_settings(
|
||||
"req_installation_ws",
|
||||
owner,
|
||||
Some(global_request_location()),
|
||||
Some(profile),
|
||||
Default::default(),
|
||||
),
|
||||
)
|
||||
.await
|
||||
@@ -8213,7 +8237,7 @@ async fn seed_service_tiers(store: &MemoryAccountStore, id: &str, model: &str, t
|
||||
#[tokio::test]
|
||||
async fn responses_should_observe_and_bill_the_outbound_service_tier_on_both_transports() {
|
||||
for use_websocket in [false, true] {
|
||||
for (fast_mode, advertised_tier, requested, reported, expected_tier, expected_cost) in [
|
||||
let regular_cases = [
|
||||
(
|
||||
FastMode::Default,
|
||||
None,
|
||||
@@ -8393,7 +8417,40 @@ async fn responses_should_observe_and_bill_the_outbound_service_tier_on_both_tra
|
||||
Some(6_875_000),
|
||||
),
|
||||
(FastMode::Ultrafast, None, None, None, None, Some(3_437_500)),
|
||||
] {
|
||||
];
|
||||
let guardian_cases = [
|
||||
("reviewer", None, 3_437_500),
|
||||
("Reviewer", Some("priority"), 6_875_000),
|
||||
("classifier", None, 3_437_500),
|
||||
("guardian_classifier", None, 3_437_500),
|
||||
("guardian_housekeeper", Some("default"), 3_437_500),
|
||||
("guardian_housekeeper", None, 3_437_500),
|
||||
]
|
||||
.map(|(role, expected_tier, cost)| {
|
||||
(
|
||||
Some(role),
|
||||
(
|
||||
FastMode::Enabled,
|
||||
Some("priority"),
|
||||
if role == "guardian_housekeeper" {
|
||||
expected_tier.map(|tier| json!(tier))
|
||||
} else {
|
||||
Some(json!("flex"))
|
||||
},
|
||||
None,
|
||||
expected_tier,
|
||||
Some(cost),
|
||||
),
|
||||
)
|
||||
});
|
||||
for (
|
||||
guardian,
|
||||
(fast_mode, advertised_tier, requested, reported, expected_tier, expected_cost),
|
||||
) in regular_cases
|
||||
.into_iter()
|
||||
.map(|case| (None, case))
|
||||
.chain(guardian_cases)
|
||||
{
|
||||
let store = Arc::new(MemoryAccountStore::default());
|
||||
create_account(&store, "acct_provider_contract").await;
|
||||
// 同名模型的聚合画像来自另一个分组,故意与本次能选的账号声明相反
|
||||
@@ -8439,14 +8496,11 @@ async fn responses_should_observe_and_bill_the_outbound_service_tier_on_both_tra
|
||||
let base_url = format!("http://{}", listener.local_addr().expect("address"));
|
||||
let server = tokio::spawn(async move {
|
||||
let (socket, _) = listener.accept().await.expect("accept WebSocket");
|
||||
let mut headers = None;
|
||||
let mut websocket = crate::transport::accept_codex_test_websocket_with(
|
||||
socket,
|
||||
|request, response| {
|
||||
let expected_hint = expected_tier.map_or_else(
|
||||
|| "model=gpt-5.4".to_owned(),
|
||||
|tier| format!("model=gpt-5.4;tier={tier}"),
|
||||
);
|
||||
assert_eq!(request.headers()["x-codex-routing-hint"], expected_hint);
|
||||
headers = Some(request.headers().clone());
|
||||
response.headers_mut().insert(
|
||||
"sec-websocket-extensions",
|
||||
"permessage-deflate".parse().unwrap(),
|
||||
@@ -8463,7 +8517,7 @@ async fn responses_should_observe_and_bill_the_outbound_service_tier_on_both_tra
|
||||
.await
|
||||
.expect("response");
|
||||
}
|
||||
request
|
||||
(headers.unwrap(), request)
|
||||
});
|
||||
(base_url, None, Some(server))
|
||||
} else {
|
||||
@@ -8491,12 +8545,31 @@ async fn responses_should_observe_and_bill_the_outbound_service_tier_on_both_tra
|
||||
if let Some(requested) = &requested {
|
||||
body.insert("service_tier".to_owned(), json!(requested));
|
||||
}
|
||||
let mut protocol_context =
|
||||
Map::from_iter([("use_websocket".to_owned(), json!(use_websocket))]);
|
||||
if let Some(role) = guardian {
|
||||
let mut headers = vec![json!([
|
||||
"x-codex-routing-hint",
|
||||
STANDARD.encode("client-hint")
|
||||
])];
|
||||
if matches!(role, "guardian_classifier" | "guardian_housekeeper") {
|
||||
let turn = if role == "guardian_classifier" {
|
||||
json!({"thread_source":role, "turn_trigger":role})
|
||||
} else {
|
||||
json!({"turn_trigger":role})
|
||||
};
|
||||
body.get_mut("client_metadata").unwrap()["x-openai-subagent"] =
|
||||
json!("guardian");
|
||||
body.get_mut("client_metadata").unwrap()["x-codex-turn-metadata"] =
|
||||
json!(turn.to_string());
|
||||
} else {
|
||||
headers.push(json!(["x-codex-guardian", STANDARD.encode(role)]));
|
||||
}
|
||||
protocol_context.insert("opaque_request_headers".to_owned(), json!(headers));
|
||||
}
|
||||
let payload = ProtocolPayload::json_object("openai", body.clone())
|
||||
.expect("payload")
|
||||
.with_context(Map::from_iter([(
|
||||
"use_websocket".to_owned(),
|
||||
json!(use_websocket),
|
||||
)]));
|
||||
.with_context(protocol_context);
|
||||
let operation = Operation::Generate(GenerateRequest::from_protocol_payload(payload));
|
||||
let original = operation.clone();
|
||||
let provider = provider_with_catalog_and_base_url(&store, base_url).await;
|
||||
@@ -8541,31 +8614,70 @@ async fn responses_should_observe_and_bill_the_outbound_service_tier_on_both_tra
|
||||
raw_messages.push(message.to_owned());
|
||||
}
|
||||
}
|
||||
let outbound = if let Some(server) = http_server {
|
||||
let (headers, outbound) = if let Some(server) = http_server {
|
||||
server.verify().await;
|
||||
let requests = server.received_requests().await.expect("upstream requests");
|
||||
let expected_hint = expected_tier.map_or_else(
|
||||
|| "model=gpt-5.4".to_owned(),
|
||||
|tier| format!("model=gpt-5.4;tier={tier}"),
|
||||
);
|
||||
assert_eq!(requests[0].headers["x-codex-routing-hint"], expected_hint);
|
||||
captured_request_body(&requests[0])
|
||||
(
|
||||
requests[0].headers.clone(),
|
||||
captured_request_body(&requests[0]),
|
||||
)
|
||||
} else {
|
||||
websocket_server
|
||||
.expect("WebSocket server")
|
||||
.await
|
||||
.expect("server task")
|
||||
};
|
||||
let expected_hint = matches!(guardian, None | Some("Reviewer")).then(|| {
|
||||
expected_tier.map_or_else(
|
||||
|| "model=gpt-5.4".to_owned(),
|
||||
|tier| format!("model=gpt-5.4;tier={tier}"),
|
||||
)
|
||||
});
|
||||
assert_eq!(
|
||||
outbound.get("service_tier").and_then(Value::as_str),
|
||||
expected_tier
|
||||
headers
|
||||
.get("x-codex-guardian")
|
||||
.map(|value| value.to_str().unwrap()),
|
||||
match guardian {
|
||||
Some("guardian_classifier") => Some("classifier"),
|
||||
Some("guardian_housekeeper") => None,
|
||||
other => other,
|
||||
}
|
||||
);
|
||||
if !use_websocket
|
||||
&& matches!(
|
||||
guardian,
|
||||
Some("classifier" | "guardian_classifier" | "guardian_housekeeper")
|
||||
)
|
||||
{
|
||||
assert!(headers.get("content-encoding").is_none());
|
||||
}
|
||||
assert_eq!(
|
||||
headers
|
||||
.get("x-codex-routing-hint")
|
||||
.map(|value| value.to_str().unwrap()),
|
||||
expected_hint.as_deref()
|
||||
);
|
||||
assert_eq!(
|
||||
outbound.get("service_tier"),
|
||||
expected_tier.map(|tier| json!(tier)).as_ref()
|
||||
);
|
||||
assert_eq!(operation, original);
|
||||
for (field, value) in body["client_metadata"]
|
||||
.as_object()
|
||||
.expect("client metadata")
|
||||
{
|
||||
assert_eq!(&outbound["client_metadata"][field], value);
|
||||
if field == "x-codex-turn-metadata" {
|
||||
let sent: Value =
|
||||
serde_json::from_str(outbound["client_metadata"][field].as_str().unwrap())
|
||||
.unwrap();
|
||||
let source: Map<String, Value> =
|
||||
serde_json::from_str(value.as_str().unwrap()).unwrap();
|
||||
for (key, value) in source {
|
||||
assert_eq!(sent[&key], value);
|
||||
}
|
||||
} else {
|
||||
assert_eq!(&outbound["client_metadata"][field], value);
|
||||
}
|
||||
}
|
||||
assert!(!observations.is_empty());
|
||||
for observation in &observations {
|
||||
|
||||
@@ -93,6 +93,98 @@ async fn responses_options_follow_the_selected_upstream_on_both_transports() {
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn request_country_modes_only_change_the_top_level_across_transports() {
|
||||
use OpenAiRequestCountry::{Custom, Default, FollowLocation, Passthrough};
|
||||
for (api_key, websocket, policy, input, expected) in [
|
||||
(
|
||||
false,
|
||||
false,
|
||||
Default {},
|
||||
Some(json!("CA")),
|
||||
Some(Value::Null),
|
||||
),
|
||||
(true, true, Default {}, None, Some(Value::Null)),
|
||||
(false, true, Passthrough {}, None, None),
|
||||
(
|
||||
true,
|
||||
false,
|
||||
Passthrough {},
|
||||
Some(Value::Null),
|
||||
Some(Value::Null),
|
||||
),
|
||||
(
|
||||
false,
|
||||
false,
|
||||
Passthrough {},
|
||||
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(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"}],
|
||||
});
|
||||
if let Some(input) = input {
|
||||
body["request_country"] = input;
|
||||
}
|
||||
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()))
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn codex_oauth_keeps_explicit_context_options() {
|
||||
let body = json!({
|
||||
|
||||
@@ -16,13 +16,12 @@ struct UnmatchedAdapterPlan {
|
||||
impl UpstreamAdapterPlan for UnmatchedAdapterPlan {
|
||||
fn select(
|
||||
&self,
|
||||
_: &AttemptContext,
|
||||
route: gateway_core::engine::ModelRouteScope<'_>,
|
||||
provider: &ProviderKind,
|
||||
model: &UpstreamModelId,
|
||||
_: Option<&ProviderAccountId>,
|
||||
) -> Result<UpstreamAdapterSelection, gateway_core::error::ProviderError> {
|
||||
assert_eq!(provider.as_str(), "openai");
|
||||
assert_eq!(model.as_str(), "gpt-5.4");
|
||||
assert_eq!(route.model, "gpt-5.4");
|
||||
self.selections
|
||||
.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
|
||||
Ok(UpstreamAdapterSelection::Native)
|
||||
@@ -185,9 +184,8 @@ struct AdapterProbe {
|
||||
impl gateway_core::engine::upstream_adapter::UpstreamAdapterPlan for AdapterProbe {
|
||||
fn select(
|
||||
&self,
|
||||
_: &AttemptContext,
|
||||
_: gateway_core::engine::ModelRouteScope<'_>,
|
||||
provider: &ProviderKind,
|
||||
_: &UpstreamModelId,
|
||||
_: Option<&ProviderAccountId>,
|
||||
) -> Result<UpstreamAdapterSelection, gateway_core::error::ProviderError> {
|
||||
assert_eq!(provider.as_str(), "openai");
|
||||
@@ -359,9 +357,8 @@ impl std::fmt::Debug for AffinityAdapterProbe {
|
||||
impl UpstreamAdapterPlan for AffinityAdapterProbe {
|
||||
fn select(
|
||||
&self,
|
||||
_: &AttemptContext,
|
||||
_: gateway_core::engine::ModelRouteScope<'_>,
|
||||
_: &ProviderKind,
|
||||
_: &UpstreamModelId,
|
||||
account: Option<&ProviderAccountId>,
|
||||
) -> Result<UpstreamAdapterSelection, gateway_core::error::ProviderError> {
|
||||
if self.account_scoped {
|
||||
@@ -756,9 +753,8 @@ struct AccountScopedProbe {
|
||||
impl UpstreamAdapterPlan for AccountScopedProbe {
|
||||
fn select(
|
||||
&self,
|
||||
_: &AttemptContext,
|
||||
_: gateway_core::engine::ModelRouteScope<'_>,
|
||||
_: &ProviderKind,
|
||||
_: &UpstreamModelId,
|
||||
account: Option<&ProviderAccountId>,
|
||||
) -> Result<UpstreamAdapterSelection, gateway_core::error::ProviderError> {
|
||||
let Some(account) = account else {
|
||||
@@ -994,9 +990,8 @@ impl std::fmt::Debug for ConnectionProbe {
|
||||
impl UpstreamAdapterPlan for ConnectionProbe {
|
||||
fn select(
|
||||
&self,
|
||||
_: &AttemptContext,
|
||||
_: gateway_core::engine::ModelRouteScope<'_>,
|
||||
_: &ProviderKind,
|
||||
_: &UpstreamModelId,
|
||||
_: Option<&ProviderAccountId>,
|
||||
) -> Result<UpstreamAdapterSelection, gateway_core::error::ProviderError> {
|
||||
Ok(UpstreamAdapterSelection::Adapter(Arc::new(Self {
|
||||
|
||||
@@ -158,6 +158,7 @@ async fn backend_websocket_should_forward_context_headers_and_preserve_payload_f
|
||||
],
|
||||
);
|
||||
request.use_websocket = true;
|
||||
request.turn_state = Some("turn-state".to_owned());
|
||||
request.responses_lite = Some("true".to_owned());
|
||||
request.memgen_request = Some("true".to_owned());
|
||||
request.set_client_metadata(Some(json!({
|
||||
@@ -217,11 +218,11 @@ async fn backend_websocket_should_forward_context_headers_and_preserve_payload_f
|
||||
.and_then(Value::as_str)
|
||||
.is_some_and(|value| value.parse::<u128>().is_ok_and(|value| value > 0))
|
||||
);
|
||||
assert_eq!(metadata["x-codex-turn-state"], "turn-state");
|
||||
let headers = received_headers.lock().expect("headers lock");
|
||||
for (name, expected) in [
|
||||
("x-client-request-id", "cp_derived"),
|
||||
("openai-beta", "responses_websockets=2026-02-06"),
|
||||
("x-codex-turn-state", "turn-state"),
|
||||
("x-codex-turn-metadata", "{\"thread_source\":\"subagent\"}"),
|
||||
("x-codex-beta-features", "feature-a"),
|
||||
("x-responsesapi-include-timing-metrics", "true"),
|
||||
@@ -244,6 +245,7 @@ async fn backend_websocket_should_forward_context_headers_and_preserve_payload_f
|
||||
}
|
||||
}
|
||||
for forbidden in [
|
||||
"x-codex-turn-state",
|
||||
"x-codex-installation-id",
|
||||
"x-codex-turn-id",
|
||||
"x-openai-internal-codex-residency",
|
||||
|
||||
@@ -234,8 +234,18 @@ async fn cold_websocket_should_fall_back_without_recording_a_successful_connect(
|
||||
assert!(opening.starts_with("GET /codex/responses HTTP/1.1"));
|
||||
|
||||
let (mut http, _) = listener.accept().await.unwrap();
|
||||
let request = read_http_request(&mut http).await;
|
||||
assert!(request.starts_with("POST /codex/responses HTTP/1.1"));
|
||||
let request = read_http_request_with_body(&mut http).await;
|
||||
assert!(request.starts_with(b"POST /codex/responses HTTP/1.1"));
|
||||
let separator = request
|
||||
.windows(4)
|
||||
.position(|bytes| bytes == b"\r\n\r\n")
|
||||
.unwrap();
|
||||
let body = zstd::stream::decode_all(&request[separator + 4..]).unwrap();
|
||||
let body: Value = serde_json::from_slice(&body).unwrap();
|
||||
assert!(
|
||||
body.pointer("/client_metadata/x-codex-ws-stream-request-start-ms")
|
||||
.is_none()
|
||||
);
|
||||
write_completed_sse_response(&mut http).await;
|
||||
});
|
||||
let pool = Arc::new(CodexWebSocketPool::default());
|
||||
@@ -266,6 +276,21 @@ async fn cold_websocket_should_fall_back_without_recording_a_successful_connect(
|
||||
pool.shutdown().await;
|
||||
}
|
||||
|
||||
fn assert_ws_start_after(payload: &Value, boundary: std::time::SystemTime) {
|
||||
let start_ms = payload["client_metadata"]["x-codex-ws-stream-request-start-ms"]
|
||||
.as_str()
|
||||
.unwrap()
|
||||
.parse::<u128>()
|
||||
.unwrap();
|
||||
assert!(
|
||||
start_ms
|
||||
>= boundary
|
||||
.duration_since(std::time::UNIX_EPOCH)
|
||||
.unwrap()
|
||||
.as_millis()
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn downstream_websocket_new_chain_should_preserve_continuation_after_slow_opening() {
|
||||
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
|
||||
@@ -282,8 +307,12 @@ async fn downstream_websocket_new_chain_should_preserve_continuation_after_slow_
|
||||
return;
|
||||
}
|
||||
}
|
||||
let opening_start = std::time::SystemTime::now();
|
||||
let mut websocket = accept_codex_test_websocket(stream).await;
|
||||
let _first = websocket.next().await.unwrap().unwrap();
|
||||
let first = websocket.next().await.unwrap().unwrap();
|
||||
let first: Value = serde_json::from_str(first.to_text().unwrap()).unwrap();
|
||||
assert_ws_start_after(&first, opening_start);
|
||||
let next_turn_start = std::time::SystemTime::now();
|
||||
websocket
|
||||
.send(Message::Text(
|
||||
completed_websocket_response("resp_slow_seed", 2, 1).into(),
|
||||
@@ -294,6 +323,7 @@ async fn downstream_websocket_new_chain_should_preserve_continuation_after_slow_
|
||||
let second = websocket.next().await.unwrap().unwrap();
|
||||
let second: Value = serde_json::from_str(second.to_text().unwrap()).unwrap();
|
||||
assert_eq!(second["previous_response_id"], "resp_slow_seed");
|
||||
assert_ws_start_after(&second, next_turn_start);
|
||||
websocket
|
||||
.send(Message::Text(
|
||||
completed_websocket_response("resp_slow_continuation", 2, 1).into(),
|
||||
|
||||
@@ -4368,9 +4368,8 @@ struct AdapterProbe {
|
||||
impl gateway_core::engine::upstream_adapter::UpstreamAdapterPlan for AdapterProbe {
|
||||
fn select(
|
||||
&self,
|
||||
_: &AttemptContext,
|
||||
_: gateway_core::engine::ModelRouteScope<'_>,
|
||||
provider: &ProviderKind,
|
||||
_: &UpstreamModelId,
|
||||
account: Option<&ProviderAccountId>,
|
||||
) -> Result<UpstreamAdapterSelection, gateway_core::error::ProviderError> {
|
||||
assert_eq!(provider.as_str(), "xai");
|
||||
|
||||
@@ -83,9 +83,8 @@ struct AccountEligibilityPlan {
|
||||
impl gateway_core::engine::upstream_adapter::UpstreamAdapterPlan for AccountEligibilityPlan {
|
||||
fn select(
|
||||
&self,
|
||||
_: &AttemptContext,
|
||||
_: gateway_core::engine::ModelRouteScope<'_>,
|
||||
_: &ProviderKind,
|
||||
_: &UpstreamModelId,
|
||||
account: Option<&ProviderAccountId>,
|
||||
) -> Result<UpstreamAdapterSelection, ProviderError> {
|
||||
let Some(account) = account else {
|
||||
|
||||
@@ -43,3 +43,4 @@ aeff99fee7cef5a553ae49ffe27a48f49e31ccfb3fbc61a2482c9b109478202d 0041_account_m
|
||||
223589308df852154004385b13c91a03714114fb9472f00472099bcf2c131cb3 0043_quota_reset_window_weights.sql
|
||||
23eeb6ef99de2315cf4a198638e9c73b364fb1cb40b7e571a3f15d3e9d15f67c 0044_account_quota_settings.sql
|
||||
4e4415d21e951f4ce6cddebfd485032dd33d241dd7d9e9ed9b45b3a10f8e3878 0045_account_group_policies.sql
|
||||
d7f87d6f16c40d62b852cd39e59454d0a509c328508494f66cfd98d38b797c21 0046_openai_request_settings.sql
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
-- 默认启用 OAuth 同步审核专用模型,请求国家跟随位置
|
||||
alter table runtime_settings
|
||||
add column openai_oauth_guardian_review_model boolean not null default true,
|
||||
add column openai_request_country_json jsonb not null default '{"mode":"follow_location"}'::jsonb,
|
||||
add constraint runtime_settings_openai_request_country_ck check (
|
||||
openai_request_country_json in ('{"mode":"default"}', '{"mode":"passthrough"}', '{"mode":"follow_location"}')
|
||||
or (
|
||||
openai_request_country_json ?& array['mode', 'country']
|
||||
and openai_request_country_json = jsonb_build_object('mode', 'custom', 'country', openai_request_country_json->>'country')
|
||||
and (openai_request_country_json->>'country') collate "C" ~ '^[A-Z]{2}$'
|
||||
and jsonb_typeof(openai_request_country_json->'country') = 'string'
|
||||
)
|
||||
);
|
||||
@@ -418,6 +418,8 @@ PostgreSQL/Redis 启动密码。日常校验使用 `config --quiet`
|
||||
| 并发与排队 | “系统设置 → 运行设置”;Key 与账号分别设置排队容量和超时,可选择账号排队失败后换号,见[排队规则](../docs/api.md#并发与排队) |
|
||||
| 账号错误保护 | “系统设置 → 运行设置 → 错误保护”;默认关闭,按[自定义规则](../docs/api.md#账号错误保护)暂停账号调度 |
|
||||
| 全局请求位置 | “系统设置 → 上游配置 → 请求位置覆盖”,默认关闭 |
|
||||
| OpenAI 请求国家 | “系统设置 → 上游配置 → 请求位置覆盖”中默认选中“跟随位置”,可选择“空值”“透传”或“自定义” |
|
||||
| OpenAI OAuth 同步审核模型 | “系统设置 → 上游配置 → 模型映射”中的“专用审核模型”默认开启,关闭后沿用客户端模型。[适用条件](../docs/api.md#正文兼容) |
|
||||
| 代理位置 | “代理管理”;优先于全局位置,全局关闭时仍可独立生效 |
|
||||
|
||||
位置覆盖只影响 OpenAI 请求中受支持的位置与时区信息,不改变上下文日期、真实出口 IP、epoch 时间戳或系统时区。
|
||||
|
||||
+42
-10
@@ -510,7 +510,9 @@ Responses 的安装身份通过正文 `client_metadata` 及 turn metadata 发送
|
||||
`x-openai-internal-codex-residency` 和 `x-openai-account-routing-override` 分别由部署配置
|
||||
`openai.residency`、`openai.account_routing_override` 控制,配置值原样发送并覆盖客户端同名头,
|
||||
省略或 `null` 时删除对应请求头。配置说明见 [部署与运维](../deploy/README.md#启动后的运行设置)
|
||||
`x-openai-fedramp` 默认按业务头透传并保留原始多值,Responses 明确换号时清理。
|
||||
`x-openai-fedramp` 默认按业务头透传并保留原始多值,Responses 明确换号时清理客户端值。
|
||||
所选 OpenAI OAuth 令牌声明 FedRAMP 时,网关为模型请求设置唯一的 `x-openai-fedramp: true`。
|
||||
同账号轮换令牌导致该声明变化时,新请求使用新的上游 WS 连接,旧连接上的精确续接需要客户端重放。
|
||||
上述过滤描述原生转发;已信任插件的显式 header 改写可以覆盖账号与画像生成的默认头,
|
||||
不按认证或会话字段名称拦截,边界见 [SDK 请求中间件](../backend/crates/gateway-plugin/sdk/docs/capabilities.md#模型请求与-attempt)
|
||||
|
||||
@@ -536,6 +538,8 @@ HTTP 请求头、正文 metadata、WS 副本及已识别的 Desktop 自动 Git
|
||||
Images 的 `x-codex-image-turn-id` 和 Desktop 自动 Git 上下文的 `clientUserMessageId`。
|
||||
输入项元数据保持不变。Search 的会话 `id` 按同一安装身份域映射,只替换该值的原始字节,其余正文保持不变。
|
||||
`prompt_cache_key` 同样按安装身份映射,保留同一身份内的缓存共享关系;未提供时不补造。
|
||||
官方 Guardian 缓存键中的 UUID 与本次父线程或分类来源线程匹配时,保留 `guardian:` 或
|
||||
`guardian-v2:` 前缀并映射同一线程 UUID。其他缓存键按整个值映射。
|
||||
受信任插件可读取原始请求,自行生成身份并在 `outbound` 改写完整请求与关联字段;宿主不重新派生或补回业务字段,
|
||||
实际选号与计费归属保持不变,详见 [SDK 出站请求准备](../backend/crates/gateway-plugin/sdk/docs/capabilities.md#上游出站准备)。
|
||||
续接 response ID、历史 item/call ID、业务正文和未知扩展不参与映射,
|
||||
@@ -546,7 +550,8 @@ WS 握手的轮次元数据头只随首次请求转发,后续帧在正文中
|
||||
`x-codex-beta-features` 和 `x-responsesapi-include-timing-metrics` 按原始请求头透传
|
||||
|
||||
`x-codex-turn-state` 是上游签发的轮次路由 token。客户端在同一轮原样回传,新轮次省略。
|
||||
网关转发当前请求提供的值,明确换账号时清除。WS 握手携带的值只用于首次请求,当前帧提供的值优先。
|
||||
网关转发当前请求提供的值,明确换账号时清除。客户端 WS 握手携带的值只用于首次请求,当前帧提供的值优先。
|
||||
连接 Codex OAuth 上游时,该值写入当前 `response.create` 帧的 `client_metadata`,新上游 WS 握手省略此头。
|
||||
续接 response ID 及其他账号状态的归属独立校验
|
||||
|
||||
#### 正文兼容
|
||||
@@ -582,13 +587,25 @@ WS 握手的轮次元数据头只随首次请求转发,后续帧在正文中
|
||||
turn metadata 的 `workspaces` 也会保留仓库绝对路径、Git 远端地址、提交及工作区变更状态;
|
||||
安装身份和账号绑定字段的处理不提供这些信息的脱敏
|
||||
|
||||
`client_metadata.parent_response_id` 是 Guardian 的账号内响应引用,只有归属可信且仍为同一账号时保留;
|
||||
切号或归属未知时移除。`x-codex-guardian`、`guardian_credits_requested` 和序列化
|
||||
`x-codex-turn-metadata` 内普通扩展的同名 `parent_response_id` 保持原样
|
||||
Codex 客户端按本地登录方式和模型卡选择同步 Guardian 审核模型。使用默认模型卡时,
|
||||
API Key 登录通常选择 `gpt-5.6-luna`,ChatGPT 登录选择 `codex-auto-review`。
|
||||
“系统设置 → 上游配置”中的“专用审核模型”默认开启。关闭后沿用客户端模型。开启时,网关按当前请求的
|
||||
Guardian 身份识别同步审核和审核预热,优先选择允许 `codex-auto-review` 的原生 OAuth 账号。
|
||||
入站审核模型可以是任意名称。普通请求、后台分类和会话整理沿用原模型。
|
||||
缺少合格账号时,网关按客户端模型正常选路。管理员显式模型映射、别名与已绑定的适配器和中间件优先。
|
||||
已有续接沿用绑定账号及已记录的审核模型;无法确认模型兼容性时要求客户端重放。执行开始后的重试保持已选模型
|
||||
|
||||
同一 Client Key、同一 OAuth 账号的 Guardian 请求可利用网关记录的父响应 WS 握手信息,
|
||||
尽力复用父请求的上游路由。路由 Cookie 仅在网关与上游之间使用,不传给客户端;
|
||||
网关重启、记录淘汰或 Cookie 到期后仍可按普通路由请求,不保证上游路由亲和
|
||||
发往 Codex OAuth 的同步 `codex-auto-review` 审核带 `x-codex-guardian: reviewer`,
|
||||
后台分类带 `x-codex-guardian: classifier`。这两类请求都省略顶层 `service_tier` 和
|
||||
`x-codex-routing-hint`,分组强制加速同样适用。后台会话整理保留会话层级并省略上述两个请求头。
|
||||
后台分类和会话整理的 HTTP 请求使用未压缩 JSON。普通 OAuth 生成和同步审核的 HTTP 正文使用 zstd
|
||||
|
||||
同步审核和后台分类的 `client_metadata.parent_response_id` 只有在网关记录能确认同一 Client Key、
|
||||
同一 OAuth 账号的父响应时才发送,HTTP 和 WS 收到的父响应都可验证。预热请求可以没有父响应。
|
||||
同步审核可复用该父响应有效的上游 WS 路由 Cookie。缺少可用 Cookie 时仍可发送已验证的父响应 ID;
|
||||
父响应记录淘汰或网关重启后,网关省略该 ID 并继续审核。
|
||||
网关保留父会话发来的 `guardian_credits_requested`,不会根据账号类型补造该字段,已识别的
|
||||
Guardian 子请求会移除它。序列化 turn metadata 内普通扩展的同名字段保持原样
|
||||
|
||||
#### WebSocket 与上游传输
|
||||
|
||||
@@ -600,6 +617,9 @@ Responses WebSocket 接受文本消息,`response.create` 在同一连接串行
|
||||
留在有界接收队列中,待当前响应完成终结和写出后再逐条校验、准入与执行,不因请求提前到达而断开。
|
||||
接收队列容量为 32 个事件,超载仍关闭连接;Ping/Pong、客户端关闭和服务关闭不等待队列中的请求执行
|
||||
|
||||
原生上游 WS 帧里的 `x-codex-ws-stream-request-start-ms` 在连接可用后、发送前写入。
|
||||
如果出站插件接管了帧正文,网关按插件返回的字节发送,保留其签名和时间戳
|
||||
|
||||
网关分别维护两段链路的心跳:上游 WS 定时发送 Ping 保活并回复 Pong,下游 WS 独立探活;Ping/Pong 不跨段转发,也不转换为 SSE 事件。
|
||||
下游探活包含消息接收中的字节进度,完整消息收齐后才进入中间件与请求处理。
|
||||
客户端断开时取消当前执行,并释放仍归该客户端所有的上游连接。`response.completed` 结束本轮响应。通常保留客户端 WS 等待下一轮请求,大包回退后的重连规则见下文
|
||||
@@ -1487,7 +1507,7 @@ API 的 `autoLocation` 默认为 `false`;开启时使用已检测位置,测
|
||||
只改位置不清空连通性测试结果,也不更改账号凭据版本
|
||||
|
||||
关联账号的 OpenAI/Codex Responses 请求(HTTP/SSE、WebSocket)优先使用代理位置,否则使用全局
|
||||
运行设置中已开启的 `requestLocation`。出站正文的顶层 `request_country` 始终写入生效位置的两位国家码,无位置覆盖时写入 JSON `null`。
|
||||
运行设置中已开启的 `requestLocation`。出站正文的顶层 `request_country` 默认跟随位置,也可在[请求位置](#请求位置)选择“空值”“透传”或“自定义”。
|
||||
位置覆盖同时更新客户端时间上下文的时区和 Web Search 的结构化位置,未启用覆盖时这两项保留客户端原值。
|
||||
全局覆盖按请求冻结,保存后对新请求生效。代理覆盖按每次执行选定的账号解析,包括换号和换出口。
|
||||
位置覆盖不改变 epoch 时间戳、真实出口 IP、服务或管理端时区、数据驻留约束及 xAI 请求
|
||||
@@ -2203,7 +2223,8 @@ HTTP 返回 `429`,`error.code` 为 `key_daily_budget_exceeded` 或 `key_weekly
|
||||
| --- | --- |
|
||||
| 并发控制版本 | `configRevision` |
|
||||
| [客户端身份](#provider-客户端身份) | `providerRequestProfiles`、`openaiClientProfile`、`xaiClientProfile` |
|
||||
| [请求位置](#请求位置) | `requestLocationEnabled`、`requestLocation` |
|
||||
| [请求位置](#请求位置) | `requestLocationEnabled`、`requestLocation`、`openaiRequestCountry` |
|
||||
| [OpenAI OAuth 同步审核模型](#正文兼容) | `openaiOauthGuardianReviewModel`,默认 `true` |
|
||||
| [隐私策略](#隐私策略) | `codexPrivacyPolicy` |
|
||||
| 模型映射 | `modelMappings` |
|
||||
| 凭据刷新 | `refreshMarginSeconds`、`refreshConcurrency` |
|
||||
@@ -2253,6 +2274,17 @@ HTTP 返回 `429`,`error.code` 为 `key_daily_budget_exceeded` 或 `key_weekly
|
||||
关联代理配置了自定义位置时优先使用代理值。保存后通过现有配置发布机制对新请求生效,
|
||||
已开始请求及其重试保持同一份全局值;普通文本、绝对时间戳和数据驻留要求不受影响
|
||||
|
||||
`openaiRequestCountry` 单独控制 OpenAI Responses 出站正文顶层的 `request_country`。保存后对新请求生效。
|
||||
|
||||
| 管理端选项 | 设置值 | 发往上游的字段 |
|
||||
| --- | --- | --- |
|
||||
| 跟随位置 | `{"mode":"follow_location"}` | 默认选项。先取代理位置,再取已开启的全局位置。两者都没有时发送 `null` |
|
||||
| 空值 | `{"mode":"default"}` | 始终发送 `"request_country": null`,包括客户端传入国家、代理或全局位置已配置的情况 |
|
||||
| 透传 | `{"mode":"passthrough"}` | 保留客户端原值,包括显式 `null`;客户端省略时也省略 |
|
||||
| 自定义 | `{"mode":"custom","country":"CA"}` | 始终发送指定的两位大写 ASCII 国家码,独立于城市、时区和全局位置开关 |
|
||||
|
||||
更新设置时省略 `openaiRequestCountry` 或 `openaiOauthGuardianReviewModel`,对应值保持上次保存的设置。
|
||||
|
||||
### 隐私策略
|
||||
|
||||
`codexPrivacyPolicy` 为必填对象,默认 `{ enabled: false, onError: "skip_rule", rules: [] }`。
|
||||
|
||||
@@ -39,6 +39,10 @@ export interface SmartSchedulingConfig {
|
||||
resetWindowWeights: ResetWindowWeights
|
||||
}
|
||||
|
||||
export type OpenAiRequestCountry
|
||||
= | { mode: 'default' | 'passthrough' | 'follow_location' }
|
||||
| { mode: 'custom', country: string }
|
||||
|
||||
export interface RuntimeSettings {
|
||||
configRevision: number
|
||||
smartScheduling: SmartSchedulingConfig
|
||||
@@ -50,8 +54,10 @@ export interface RuntimeSettings {
|
||||
xaiClientProfile: XaiClientProfileSelection | null
|
||||
|
||||
codexPrivacyPolicy: CodexPrivacyPolicy
|
||||
openaiRequestCountry: OpenAiRequestCountry
|
||||
requestLocationEnabled: boolean
|
||||
requestLocation: RequestLocation
|
||||
openaiOauthGuardianReviewModel: boolean
|
||||
modelMappings: Record<string, string>
|
||||
refreshMarginSeconds: number
|
||||
refreshConcurrency: number
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
<script setup lang="ts">
|
||||
import type { FormRule } from '@codex-proxy/ui'
|
||||
import { ZFormItem, ZInput } from '@codex-proxy/ui'
|
||||
import { computed } from 'vue'
|
||||
import { useI18n } from 'vue-i18n'
|
||||
|
||||
defineProps<{ disabled?: boolean, formProp?: string, label?: string }>()
|
||||
|
||||
const country = defineModel<string>({ required: true })
|
||||
const { t } = useI18n({ useScope: 'global' })
|
||||
const countryRule = computed<FormRule>(() => ({
|
||||
required: true,
|
||||
pattern: /^[A-Za-z]{2}$/u,
|
||||
transform: value => value.trim(),
|
||||
trigger: ['blur', 'change'],
|
||||
message: t('common.location.countryCodePlaceholder'),
|
||||
}))
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<ZFormItem :prop="formProp" :rules="countryRule" :label="label ?? t('common.location.countryCode')" required>
|
||||
<ZInput v-model="country" :disabled="disabled" :aria-label="label ?? t('common.location.countryCode')" :placeholder="t('common.location.countryCodePlaceholder')" maxlength="2" />
|
||||
</ZFormItem>
|
||||
</template>
|
||||
@@ -2,7 +2,7 @@
|
||||
import { ZIconButton, ZPopover } from '@codex-proxy/ui'
|
||||
import { CircleAlert } from '@lucide/vue'
|
||||
|
||||
defineProps<{ label: string, content: string }>()
|
||||
defineProps<{ label: string, content?: string }>()
|
||||
</script>
|
||||
|
||||
<template>
|
||||
@@ -13,7 +13,7 @@ defineProps<{ label: string, content: string }>()
|
||||
</ZIconButton>
|
||||
</template>
|
||||
<section role="dialog" :aria-label="label" class="max-w-72 p-3 text-z-xs leading-relaxed text-z-text-secondary">
|
||||
{{ content }}
|
||||
<slot>{{ content }}</slot>
|
||||
</section>
|
||||
</ZPopover>
|
||||
</template>
|
||||
|
||||
@@ -5,6 +5,7 @@ import type { RequestLocation } from '@/api'
|
||||
import { ZFormItem, ZInput } from '@codex-proxy/ui'
|
||||
import { computed } from 'vue'
|
||||
import { useI18n } from 'vue-i18n'
|
||||
import CountryCodeField from '@/components/CountryCodeField.vue'
|
||||
|
||||
defineProps<{ disabled?: boolean, formProp?: string }>()
|
||||
|
||||
@@ -17,15 +18,6 @@ const requiredRule = computed<FormRule>(() => ({
|
||||
trigger: ['blur', 'change'],
|
||||
message: t('common.location.required'),
|
||||
}))
|
||||
const countryRules = computed<FormRule[]>(() => [
|
||||
requiredRule.value,
|
||||
{
|
||||
pattern: /^[A-Z]{2}$/,
|
||||
transform: value => value.trim().toUpperCase(),
|
||||
trigger: ['blur', 'change'],
|
||||
message: t('common.location.required'),
|
||||
},
|
||||
])
|
||||
|
||||
function update(field: keyof RequestLocation, value: string) {
|
||||
location.value = { ...location.value, [field]: value }
|
||||
@@ -34,9 +26,13 @@ function update(field: keyof RequestLocation, value: string) {
|
||||
|
||||
<template>
|
||||
<div class="grid gap-4.5 sm:grid-cols-2">
|
||||
<ZFormItem :prop="formProp ? `${formProp}.country` : undefined" :rules="countryRules" :show-message="Boolean(location.country.trim())" :label="t('common.location.countryCode')" required>
|
||||
<ZInput :model-value="location.country" :disabled="disabled" :aria-label="t('common.location.countryCode')" :placeholder="t('common.location.countryCodePlaceholder')" maxlength="2" @update:model-value="update('country', $event)" />
|
||||
</ZFormItem>
|
||||
<CountryCodeField
|
||||
:model-value="location.country"
|
||||
:form-prop="formProp ? `${formProp}.country` : undefined"
|
||||
:show-message="Boolean(location.country.trim())"
|
||||
:disabled="disabled"
|
||||
@update:model-value="update('country', $event)"
|
||||
/>
|
||||
<ZFormItem :prop="formProp ? `${formProp}.region` : undefined" :rules="requiredRule" :show-message="false" :label="t('common.location.region')" required>
|
||||
<ZInput :model-value="location.region" :disabled="disabled" :aria-label="t('common.location.region')" :placeholder="t('common.location.regionPlaceholder')" maxlength="128" @update:model-value="update('region', $event)" />
|
||||
</ZFormItem>
|
||||
|
||||
@@ -293,6 +293,8 @@
|
||||
},
|
||||
"modelMapping": "Model mapping",
|
||||
"routing": {
|
||||
"oauthGuardianReviewModel": "Dedicated review model",
|
||||
"oauthGuardianReviewModelHelp": "OAuth synchronous reviews use codex-auto-review by default. Turn off to use the client model.",
|
||||
"modelMapHelp": "Map client model names to the upstream models used"
|
||||
},
|
||||
"addMapping": "Add mapping",
|
||||
@@ -311,6 +313,21 @@
|
||||
"enterDays": "Enter retention days",
|
||||
"day": "d",
|
||||
"location": {
|
||||
"requestCountry": "Request country",
|
||||
"countryHelp": "Request country mode help",
|
||||
"countryModes": {
|
||||
"default": "Null",
|
||||
"passthrough": "Pass through",
|
||||
"follow_location": "Follow location",
|
||||
"custom": "Custom"
|
||||
},
|
||||
"countryHints": {
|
||||
"default": "Always send JSON null.",
|
||||
"passthrough": "Keep the client’s value. Omit the field when the client omits it.",
|
||||
"follow_location": "Use the proxy location first, then the enabled global location. Send null when neither applies.",
|
||||
"custom": "Always use the specified two-letter country code."
|
||||
},
|
||||
"customCountryCode": "Custom country code",
|
||||
"requestLocationOverride": "Request location override",
|
||||
"description": "Override time zone and location fields in Codex requests, custom locations on account proxies take precedence",
|
||||
"enableLocationOverlay": "Enable location overlay"
|
||||
|
||||
@@ -293,6 +293,8 @@
|
||||
},
|
||||
"modelMapping": "模型映射",
|
||||
"routing": {
|
||||
"oauthGuardianReviewModel": "专用审核模型",
|
||||
"oauthGuardianReviewModelHelp": "OAuth 同步审核默认使用 codex-auto-review,关闭后沿用客户端模型",
|
||||
"modelMapHelp": "将客户端请求的模型名称映射为实际使用的上游模型"
|
||||
},
|
||||
"addMapping": "添加映射",
|
||||
@@ -311,6 +313,21 @@
|
||||
"enterDays": "请输入保留天数",
|
||||
"day": "天",
|
||||
"location": {
|
||||
"requestCountry": "请求国家",
|
||||
"countryHelp": "请求国家模式说明",
|
||||
"countryModes": {
|
||||
"default": "空值",
|
||||
"passthrough": "透传",
|
||||
"follow_location": "跟随位置",
|
||||
"custom": "自定义"
|
||||
},
|
||||
"countryHints": {
|
||||
"default": "始终发送 JSON null",
|
||||
"passthrough": "保留客户端原值,未传时省略该字段",
|
||||
"follow_location": "优先使用代理位置,其次使用已开启的全局位置。两者都未设置时发送 null",
|
||||
"custom": "固定使用指定的两位国家代码"
|
||||
},
|
||||
"customCountryCode": "自定义国家代码",
|
||||
"requestLocationOverride": "请求位置覆盖",
|
||||
"description": "覆盖 Codex 请求中的时区与位置字段,账号出站代理的自定义位置优先",
|
||||
"enableLocationOverlay": "启用位置覆盖"
|
||||
|
||||
@@ -293,6 +293,8 @@
|
||||
},
|
||||
"modelMapping": "模型映射",
|
||||
"routing": {
|
||||
"oauthGuardianReviewModel": "專用審核模型",
|
||||
"oauthGuardianReviewModelHelp": "OAuth 同步審核預設使用 codex-auto-review,關閉後沿用用戶端模型",
|
||||
"modelMapHelp": "將客戶端請求的模型名稱映射爲實際使用的上游模型"
|
||||
},
|
||||
"addMapping": "添加映射",
|
||||
@@ -311,6 +313,21 @@
|
||||
"enterDays": "請輸入保留天數",
|
||||
"day": "天",
|
||||
"location": {
|
||||
"requestCountry": "請求國家",
|
||||
"countryHelp": "請求國家模式說明",
|
||||
"countryModes": {
|
||||
"default": "空值",
|
||||
"passthrough": "透傳",
|
||||
"follow_location": "跟隨位置",
|
||||
"custom": "自訂"
|
||||
},
|
||||
"countryHints": {
|
||||
"default": "一律傳送 JSON null",
|
||||
"passthrough": "保留用戶端原值,未傳送時省略此欄位",
|
||||
"follow_location": "優先使用代理位置,其次使用已啟用的全域位置。兩者皆未設定時傳送 null",
|
||||
"custom": "固定使用指定的兩位國家代碼"
|
||||
},
|
||||
"customCountryCode": "自訂國家代碼",
|
||||
"requestLocationOverride": "請求位置覆蓋",
|
||||
"description": "覆蓋 Codex 請求中的時區與位置字段,賬號出站代理的自定義位置優先",
|
||||
"enableLocationOverlay": "啓用位置覆蓋"
|
||||
|
||||
@@ -1,16 +1,20 @@
|
||||
<script setup lang="ts">
|
||||
import type { FormRule } from '@codex-proxy/ui'
|
||||
import { ZButton, ZCard, ZFormItem, ZIconButton, ZInput } from '@codex-proxy/ui'
|
||||
import { ZButton, ZCard, ZFormItem, ZIconButton, ZInput, ZSwitch } from '@codex-proxy/ui'
|
||||
|
||||
import { Plus, Trash2 } from '@lucide/vue'
|
||||
|
||||
import { computed } from 'vue'
|
||||
import { useI18n } from 'vue-i18n'
|
||||
|
||||
import HelpPopover from '@/components/HelpPopover.vue'
|
||||
|
||||
const props = withDefaults(defineProps<{
|
||||
disabled?: boolean
|
||||
mappings: Array<{ requestedModel: string, upstreamModel: string }>
|
||||
loading?: boolean
|
||||
}>(), {
|
||||
disabled: false,
|
||||
loading: false,
|
||||
})
|
||||
|
||||
@@ -20,6 +24,8 @@ const emit = defineEmits<{
|
||||
removeMapping: [index: number]
|
||||
}>()
|
||||
|
||||
const oauthGuardianReviewModel = defineModel<boolean>('oauthGuardianReviewModel', { required: true })
|
||||
|
||||
const { t } = useI18n({ useScope: 'global' })
|
||||
const requiredRule = computed<FormRule>(() => ({
|
||||
required: true,
|
||||
@@ -43,6 +49,18 @@ const uniqueModelRule = computed<FormRule>(() => ({
|
||||
:description="t('settings.routing.modelMapHelp')"
|
||||
>
|
||||
<div class="grid gap-4">
|
||||
<div id="oauth-guardian-review-model-help" class="flex items-center gap-2">
|
||||
<ZSwitch
|
||||
v-model="oauthGuardianReviewModel"
|
||||
:label="t('settings.routing.oauthGuardianReviewModel')"
|
||||
:aria-description="t('settings.routing.oauthGuardianReviewModelHelp')"
|
||||
:disabled="disabled"
|
||||
/>
|
||||
<HelpPopover
|
||||
:label="t('common.form.fieldHelp', { label: t('settings.routing.oauthGuardianReviewModel') })"
|
||||
:content="t('settings.routing.oauthGuardianReviewModelHelp')"
|
||||
/>
|
||||
</div>
|
||||
<div class="flex flex-wrap items-center gap-3">
|
||||
<ZButton :disabled="loading" @click="emit('addMapping')">
|
||||
<template #icon>
|
||||
|
||||
@@ -1,8 +1,12 @@
|
||||
<script setup lang="ts">
|
||||
import type { RequestLocation } from '@/api'
|
||||
import type { SelectOption } from '@codex-proxy/ui/select'
|
||||
import type { OpenAiRequestCountry, RequestLocation } from '@/api'
|
||||
|
||||
import { ZCard, ZSwitch } from '@codex-proxy/ui'
|
||||
import { ZCard, ZFormItem, ZSelect, ZSwitch } from '@codex-proxy/ui'
|
||||
import { computed } from 'vue'
|
||||
import { useI18n } from 'vue-i18n'
|
||||
import CountryCodeField from '@/components/CountryCodeField.vue'
|
||||
import HelpPopover from '@/components/HelpPopover.vue'
|
||||
import RequestLocationDetectButton from '@/components/RequestLocationDetectButton.vue'
|
||||
import RequestLocationFields from '@/components/RequestLocationFields.vue'
|
||||
|
||||
@@ -14,6 +18,37 @@ const { t } = useI18n({ useScope: 'global' })
|
||||
|
||||
const location = defineModel<RequestLocation>({ required: true })
|
||||
const enabled = defineModel<boolean>('enabled', { required: true })
|
||||
const requestCountry = defineModel<OpenAiRequestCountry>('requestCountry', { required: true })
|
||||
const modes = computed<Array<SelectOption<OpenAiRequestCountry['mode']> & { help: string }>>(() => [
|
||||
{
|
||||
value: 'follow_location',
|
||||
label: t('settings.location.countryModes.follow_location'),
|
||||
help: t('settings.location.countryHints.follow_location'),
|
||||
},
|
||||
{
|
||||
value: 'default',
|
||||
label: t('settings.location.countryModes.default'),
|
||||
help: t('settings.location.countryHints.default'),
|
||||
},
|
||||
{
|
||||
value: 'passthrough',
|
||||
label: t('settings.location.countryModes.passthrough'),
|
||||
help: t('settings.location.countryHints.passthrough'),
|
||||
},
|
||||
{
|
||||
value: 'custom',
|
||||
label: t('settings.location.countryModes.custom'),
|
||||
help: t('settings.location.countryHints.custom'),
|
||||
},
|
||||
])
|
||||
|
||||
function setMode(mode: OpenAiRequestCountry['mode'] | undefined) {
|
||||
if (mode && mode !== requestCountry.value.mode) {
|
||||
requestCountry.value = mode === 'custom'
|
||||
? { mode, country: '' }
|
||||
: { mode }
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
@@ -25,6 +60,31 @@ const enabled = defineModel<boolean>('enabled', { required: true })
|
||||
</div>
|
||||
<div class="grid min-w-0 gap-4.5 mt-4 max-w-6xl">
|
||||
<RequestLocationFields v-model="location" :form-prop="enabled ? 'requestLocation' : undefined" :disabled="disabled || !enabled" />
|
||||
<div class="grid gap-4.5 sm:grid-cols-2">
|
||||
<ZFormItem :label="t('settings.location.requestCountry')">
|
||||
<template #label-extra>
|
||||
<HelpPopover :label="t('settings.location.countryHelp')">
|
||||
<dl class="grid grid-cols-[auto_1fr] gap-x-3 gap-y-2">
|
||||
<template v-for="mode in modes" :key="mode.value">
|
||||
<dt class="font-emphasis text-z-text">
|
||||
{{ mode.label }}
|
||||
</dt>
|
||||
<dd>{{ mode.help }}</dd>
|
||||
</template>
|
||||
</dl>
|
||||
</HelpPopover>
|
||||
</template>
|
||||
<ZSelect :model-value="requestCountry.mode" :options="modes" :aria-label="t('settings.location.requestCountry')" :disabled="disabled" @update:model-value="setMode" />
|
||||
</ZFormItem>
|
||||
<CountryCodeField
|
||||
v-if="requestCountry.mode === 'custom'"
|
||||
:model-value="requestCountry.country"
|
||||
form-prop="openaiRequestCountry.country"
|
||||
:label="t('settings.location.customCountryCode')"
|
||||
:disabled="disabled"
|
||||
@update:model-value="requestCountry = { mode: 'custom', country: $event }"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
</ZCard>
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import type { ProtectionDraft } from '../components/error-protection/form'
|
||||
import type { rotationOptions } from '../constants'
|
||||
import type { AccountAffinity, CodexPrivacyPolicy, ResetWindowWeights, SmartSchedulingConfig } from '@/api'
|
||||
import type { AccountAffinity, CodexPrivacyPolicy, OpenAiRequestCountry, ResetWindowWeights, SmartSchedulingConfig } from '@/api'
|
||||
import type { ProviderRequestProfiles, ProviderRequestProfileUpdates } from '@/api/modules/settings/profiles'
|
||||
import { cloneDeep, isEqual } from 'es-toolkit'
|
||||
import { computed, reactive, ref, shallowRef } from 'vue'
|
||||
@@ -34,6 +34,8 @@ export function useSettingsForm() {
|
||||
quotaResetWindowWeights: undefined as ResetWindowWeights | undefined,
|
||||
providerRequestProfiles: {} as ProviderRequestProfiles,
|
||||
codexPrivacyPolicy: { enabled: false, onError: 'skip_rule', rules: [] } as CodexPrivacyPolicy,
|
||||
openaiOauthGuardianReviewModel: true,
|
||||
openaiRequestCountry: { mode: 'follow_location' } as OpenAiRequestCountry,
|
||||
requestLocationEnabled: false,
|
||||
requestLocation: {
|
||||
country: '',
|
||||
@@ -133,6 +135,8 @@ export function useSettingsForm() {
|
||||
function applySettings(data: Awaited<ReturnType<typeof getSettings>>) {
|
||||
form.codexPrivacyPolicy = cloneDeep(data.codexPrivacyPolicy)
|
||||
form.configRevision = data.configRevision
|
||||
form.openaiOauthGuardianReviewModel = data.openaiOauthGuardianReviewModel ?? true
|
||||
form.openaiRequestCountry = data.openaiRequestCountry ?? { mode: 'follow_location' }
|
||||
form.requestLocationEnabled = data.requestLocationEnabled
|
||||
form.requestLocation = { ...data.requestLocation }
|
||||
form.refreshMarginSeconds = data.refreshMarginSeconds
|
||||
@@ -266,8 +270,15 @@ export function useSettingsForm() {
|
||||
form.providerRequestProfiles,
|
||||
),
|
||||
codexPrivacyPolicy: cloneDeep(form.codexPrivacyPolicy),
|
||||
openaiRequestCountry: form.openaiRequestCountry.mode === 'custom'
|
||||
? {
|
||||
mode: 'custom',
|
||||
country: form.openaiRequestCountry.country.trim().toUpperCase(),
|
||||
}
|
||||
: { mode: form.openaiRequestCountry.mode },
|
||||
requestLocationEnabled: form.requestLocationEnabled,
|
||||
requestLocation,
|
||||
openaiOauthGuardianReviewModel: form.openaiOauthGuardianReviewModel,
|
||||
modelMappings: Object.fromEntries(mappings.value.map(row => [
|
||||
row.requestedModel.trim(),
|
||||
row.upstreamModel.trim(),
|
||||
|
||||
@@ -244,12 +244,15 @@ watch(section, (value) => {
|
||||
<RequestLocationCard
|
||||
v-model="form.requestLocation"
|
||||
v-model:enabled="form.requestLocationEnabled"
|
||||
v-model:request-country="form.openaiRequestCountry"
|
||||
:disabled="disabled"
|
||||
:detecting-location="detectingLocation"
|
||||
@detect-location="detectLocation"
|
||||
/>
|
||||
<PrivacyPolicyCard v-model="form.codexPrivacyPolicy" :disabled="disabled" />
|
||||
<ModelAliasesCard
|
||||
v-model:oauth-guardian-review-model="form.openaiOauthGuardianReviewModel"
|
||||
:disabled="disabled"
|
||||
:mappings="mappings"
|
||||
:loading="loading"
|
||||
@add-mapping="addMapping"
|
||||
|
||||
Reference in New Issue
Block a user