Compare commits

..
23 Commits
Author SHA1 Message Date
ZyyandCodex a770419818 chore: bump version to 3.54.0
Co-authored-by: Codex <noreply@openai.com>
2026-11-20 22:28:03 +08:00
ZyyandCodex 5b37b574e6 refactor(store): consolidate unreleased database migrations
Co-authored-by: Codex <noreply@openai.com>
2026-11-20 22:27:41 +08:00
0df6c40091 fix(openai): require replay for HTTP continuations after SSE fallback (#756)
Co-authored-by: Zyy <zyycn@users.noreply.github.com>
Co-authored-by: Codex <noreply@openai.com>
2026-11-20 22:20:56 +08:00
ZyyandCodex 4b5443d2f2 fix(settings): separate the dedicated review model from model mapping
Co-authored-by: Codex <noreply@openai.com>
2026-11-20 21:55:48 +08:00
ZyyandCodex b02f13c9cc refactor(admin)!: remove legacy management API fields
Use provider profile maps, saved proxy IDs, and structured dashboard metrics across the admin API and UI. Require the quota reset mode on full settings updates.

Migrate existing Redis freezes atomically with cooldown writes and reuse validated OpenAI credentials during runtime decoding.

Co-authored-by: Codex <noreply@openai.com>
2026-11-20 21:38:58 +08:00
ZyyandCodex 878d2858e8 fix(settings)!: require Guardian model and request country on updates
Plugins and admin HTTP clients must send both OpenAI settings on every replacement. Reject incomplete writes before changing the stored revision.

Co-authored-by: Codex <noreply@openai.com>
2026-11-20 21:04:35 +08:00
ZyyandCodex 369f17d654 fix(openai): preserve account and Guardian header precedence
Co-authored-by: Codex <noreply@openai.com>
2026-11-20 21:02:57 +08:00
Zyy 3709ed2484 feat(openai): align Codex Guardian routing and request country (#754) 2026-11-20 20:14:00 +08:00
Zyy 48a5c9d690 fix(openai): preserve credential errors during model catalog refresh 2026-11-20 17:30:24 +08:00
Zyy 6fc6283f3a fix(gateway): link successful replays to incomplete continuations 2026-11-20 17:30:24 +08:00
ZyyandCodex 605b60e58f refactor(ui): use canonical Tailwind classes
Co-authored-by: Codex <noreply@openai.com>
2026-11-20 17:12:03 +08:00
1bf948a24f feat(keys): add usage statistics and manual budget window alignment (#752)
* feat(keys): add usage statistics and manual budget window alignment

* fix(keys): retain dialog focus when retrying usage statistics

* fix(store): exclude pricing scenarios from missing cost detection

Co-authored-by: Codex <noreply@openai.com>

---------

Co-authored-by: Zyy <zyycn@users.noreply.github.com>
Co-authored-by: Codex <noreply@openai.com>
2026-11-20 17:04:16 +08:00
Zyy d90b9d129e fix(openai): omit unreferenced item IDs from replayed text messages 2026-11-20 15:42:02 +08:00
ZyyandCodex 4b1cae89b2 feat(admin): add image model shortcut to account allowlists
Co-authored-by: Codex <noreply@openai.com>
2026-11-20 14:46:28 +08:00
ZyyandCodex 6cfe7ce2fc feat(admin): add account group policies and bulk API key editing (#751)
Co-authored-by: Codex <noreply@openai.com>
2026-11-20 14:39:30 +08:00
ZyyandCodex 1427dfc64a feat(openai): set request country from the configured location
Co-authored-by: Codex <noreply@openai.com>
2026-11-20 12:25:27 +08:00
ZyyandCodex 9996fae5b6 fix(ui): distinguish scheduled and quota reset warmup
Co-authored-by: Codex <noreply@openai.com>
2026-11-20 12:20:33 +08:00
6420d86aa6 fix(openai): expire access tokens revoked during quota refresh (#750)
Co-authored-by: Zyy <zyycn@users.noreply.github.com>
Co-authored-by: Codex <noreply@openai.com>
2026-11-20 11:02:57 +08:00
ZyyandCodex f8fa386a7e fix(admin): simplify account error messages
Co-authored-by: Codex <noreply@openai.com>
2026-11-20 10:43:53 +08:00
github-actions[bot] 7c2c532b44 chore(release): update release channels to 3.53.1 2026-11-19 10:57:51 +00:00
ZyyandCodex 188a6817d1 chore: bump version to 3.53.1
Co-authored-by: Codex <noreply@openai.com>
2026-11-19 18:41:20 +08:00
ZyyandCodex d44bc64d1a fix(usage): improve request record layout and error summaries
Co-authored-by: Codex <noreply@openai.com>
2026-11-19 18:10:51 +08:00
github-actions[bot] 6006cc8394 chore(release): update release channels to 3.53.0 2026-11-19 09:36:04 +00:00
276 changed files with 10508 additions and 2398 deletions
@@ -183,7 +183,7 @@ async fn initialize_inner(
PluginDistributionPorts::new(plugin_distribution, store.proxies()),
snapshot.clone(),
plugin_preparation,
published_snapshot,
published_snapshot.clone(),
store.plugin_state(),
)),
key_usage,
@@ -201,6 +201,7 @@ async fn initialize_inner(
store.account_groups(),
store.account_runtime(),
snapshot.clone(),
published_snapshot,
)),
client_keys: Arc::new(DefaultClientKeyService::new(
store.client_keys(),
@@ -4,7 +4,7 @@ use std::collections::BTreeMap;
use chrono::{DateTime, Utc};
use gateway_core::{
account::{AccountStatusFacts, FastMode},
account::{AccountModelAccess, AccountStatusFacts, FastMode},
routing::AccountGroupId,
};
@@ -112,6 +112,8 @@ pub struct AccountGroupListQuery {
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AccountGroupRecord {
pub allow_image_generation: bool,
pub allow_subagents: bool,
pub model_access: AccountModelAccess,
pub fast_mode: FastMode,
pub id: AccountGroupId,
pub name: String,
@@ -152,6 +154,8 @@ pub struct AccountGroupOptionsPage {
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CreateAccountGroup {
pub allow_image_generation: bool,
pub allow_subagents: bool,
pub model_access: AccountModelAccess,
pub fast_mode: FastMode,
pub name: String,
pub description: Option<String>,
@@ -162,6 +166,8 @@ pub struct CreateAccountGroup {
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct NewAccountGroup {
pub allow_image_generation: bool,
pub allow_subagents: bool,
pub model_access: AccountModelAccess,
pub fast_mode: FastMode,
pub id: AccountGroupId,
pub name: String,
@@ -173,6 +179,8 @@ pub struct NewAccountGroup {
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct UpdateAccountGroup {
pub allow_image_generation: Option<bool>,
pub allow_subagents: Option<bool>,
pub model_access: Option<AccountModelAccess>,
pub fast_mode: Option<FastMode>,
pub id: AccountGroupId,
pub name: String,
@@ -36,6 +36,7 @@ pub enum MutationAuditOperation {
PluginSourceCredentialCreate,
PluginSourceCredentialDelete,
ClientApiKeyUpdateBudgetLimits,
ClientApiKeyBatchUpdate,
ClientApiKeyCreate,
ClientApiKeyUpdate,
ClientApiKeyEnabled { enabled: bool },
@@ -97,6 +98,7 @@ impl MutationAuditOperation {
Self::PluginSourceCredentialCreate => ("create", "plugin_source_credential"),
Self::PluginSourceCredentialDelete => ("delete", "plugin_source_credential"),
Self::ClientApiKeyUpdateBudgetLimits => ("update_budget_limits", "client_api_key"),
Self::ClientApiKeyBatchUpdate => ("batch_update", "client_api_key"),
Self::ClientApiKeyCreate => ("create", "client_api_key"),
Self::ClientApiKeyUpdate => ("update", "client_api_key"),
Self::ClientApiKeyEnabled { enabled } => {
@@ -218,6 +218,66 @@ pub struct UpdateClientKey {
pub weekly_limit_usd: Option<gateway_core::metering::Decimal>,
}
/// 批量编辑只覆盖明确选择的互联选项
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, serde::Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct ClientPeerSharingPatch {
pub enabled: Option<bool>,
pub share_pool: Option<bool>,
pub share_quota: Option<bool>,
pub share_plans: Option<bool>,
}
impl ClientPeerSharingPatch {
#[must_use]
pub fn is_empty(self) -> bool {
self == Self::default()
}
#[must_use]
pub fn apply(
self,
current: gateway_core::policy::ClientPeerSharing,
) -> gateway_core::policy::ClientPeerSharing {
gateway_core::policy::ClientPeerSharing {
enabled: self.enabled.unwrap_or(current.enabled),
share_pool: self.share_pool.unwrap_or(current.share_pool),
share_quota: self.share_quota.unwrap_or(current.share_quota),
share_plans: self.share_plans.unwrap_or(current.share_plans),
}
}
}
/// 对各个密钥独立应用同一份局部设置
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct BatchUpdateClientKeys {
pub ids: Vec<ClientApiKeyId>,
pub group_ids: Option<Vec<AccountGroupId>>,
pub enabled: Option<bool>,
pub max_concurrency: Option<u64>,
pub requests_per_minute: Option<u64>,
pub daily_limit_usd: Option<gateway_core::metering::Decimal>,
pub weekly_limit_usd: Option<gateway_core::metering::Decimal>,
pub peer_sharing: ClientPeerSharingPatch,
pub request_profile_override_updates: ProviderRequestProfileOverrideUpdates,
pub installation_seed: Option<uuid::Uuid>,
}
impl BatchUpdateClientKeys {
#[must_use]
pub fn has_changes(&self) -> bool {
self.group_ids.is_some()
|| self.enabled.is_some()
|| self.max_concurrency.is_some()
|| self.requests_per_minute.is_some()
|| self.daily_limit_usd.is_some()
|| self.weekly_limit_usd.is_some()
|| !self.peer_sharing.is_empty()
|| !self.request_profile_override_updates.is_empty()
|| self.installation_seed.is_some()
}
}
/// 修改 Client Key 启用状态
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SetClientKeyEnabled {
@@ -257,9 +317,69 @@ impl ClientKeyBudgetWindowPeriod {
}
}
/// 手动对齐只需要账号身份,列表不读取凭据、额度或用量
#[derive(serde::Serialize, Debug)]
pub struct BudgetReferenceAccount {
pub id: String,
pub name: String,
pub email: Option<String>,
pub provider: String,
}
#[derive(serde::Serialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct BudgetReferenceAccountPage {
pub items: Vec<BudgetReferenceAccount>,
pub next_cursor: Option<String>,
}
/// 管理员可预览的账号窗口保留 Provider 的稳定标识
pub struct ClientKeyBudgetReference {
pub key: String,
pub label: String,
pub period: ClientKeyBudgetWindowPeriod,
pub bounds: ExplicitClientKeyBudgetWindow,
}
impl ClientKeyBudgetReference {
/// 只取整账号当前的日/周窗口,对齐结束后恢复密钥自身周期
pub fn from_current_window(
window: super::provider_credentials::ProviderQuotaWindow,
now: DateTime<Utc>,
) -> Option<Self> {
use super::provider_credentials::QuotaLocalUsageAttribution;
if window.local_usage_attribution != QuotaLocalUsageAttribution::AccountWide
|| window
.upstream
.as_ref()
.is_some_and(|upstream| upstream.stale)
{
return None;
}
let period = match window.window_seconds? {
86_400 => ClientKeyBudgetWindowPeriod::Daily,
604_800 => ClientKeyBudgetWindowPeriod::Weekly,
_ => return None,
};
let (starts_at, ends_at) = window.observed_bounds()?;
(starts_at..ends_at).contains(&now).then_some(Self {
key: window.key,
label: window.label,
period,
bounds: ExplicitClientKeyBudgetWindow {
starts_at,
ends_at,
resume_calendar: true,
},
})
}
}
/// 按请求完成时间归属的左闭右开预算窗口
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ExplicitClientKeyBudgetWindow {
/// 一次性对齐的窗口到期后恢复原生滚动
pub resume_calendar: bool,
pub starts_at: DateTime<Utc>,
pub ends_at: DateTime<Utc>,
}
@@ -648,6 +648,56 @@ pub struct AttemptMetrics {
pub costs: Vec<CurrencyCost>,
}
/// 管理员按当前指标排列模型与趋势,值只来自固定的聚合列
#[derive(Debug, Clone, Copy, Default, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum KeyUsageMetric {
#[default]
CostUsd,
Tokens,
Requests,
}
#[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct KeyUsageTotals {
pub requests: u64,
pub tokens: Option<u64>,
pub cost_usd: Option<String>,
pub has_missing_cost: bool,
pub has_estimated_cost: bool,
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct KeyModelRow {
pub model: String,
pub totals: KeyUsageTotals,
pub peak_requests: u64,
pub active_days: u32,
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct KeyTrendPoint {
pub date: chrono::NaiveDate,
pub value: Option<String>,
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct KeyTrendSeries {
/// 空字符串表示未记录模型,None 表示合并的其他模型
pub model: Option<String>,
pub points: Vec<KeyTrendPoint>,
}
#[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize)]
pub struct KeyStatisticsOverview {
pub totals: KeyUsageTotals,
pub series: Vec<KeyTrendSeries>,
#[serde(rename = "modelCount")]
pub model_count: u64,
}
/// 时间序列粒度
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Granularity {
@@ -700,6 +700,7 @@ pub struct PreparedCredentialRotationFacts {
pub preserve_profile: bool,
/// 连接配置变更保留当前凭据健康状态与错误事实
pub preserve_credential_state: bool,
pub preserve_access_token_expiry: bool,
pub provider_material: ProviderDocument,
pub has_refresh_token: bool,
pub access_token_expires_at: Option<DateTime<Utc>>,
@@ -819,6 +820,14 @@ pub struct ProviderQuotaWindow {
pub provider_data: Option<ProviderDocument>,
}
impl ProviderQuotaWindow {
pub(crate) fn observed_bounds(&self) -> Option<(DateTime<Utc>, DateTime<Utc>)> {
let ends_at = self.reset_at?;
let duration = chrono::Duration::try_seconds(i64::try_from(self.window_seconds?).ok()?)?;
Some((ends_at.checked_sub_signed(duration)?, ends_at))
}
}
/// 账号用量统计周期,完整周/月额度优先,随后使用 Provider 提供的本地滚动日统计
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum AccountUsagePeriod {
@@ -9,7 +9,6 @@ use super::{PageSize, Revision, account_groups::AccountGroupRef};
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum AccountProxySelection {
Direct,
Url(OutboundProxy),
Saved(String),
}
@@ -45,6 +45,8 @@ pub use gateway_core::account::RotationStrategy;
/// 运行设置的共同值;版本、Provider 画像更新和数据库秘密由外层类型拥有
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct RuntimeSettingsValues {
pub openai_oauth_guardian_review_model: bool,
pub openai_request_country: gateway_core::account::OpenAiRequestCountry,
pub codex_privacy_policy: gateway_core::settings::privacy::CodexPrivacyPolicy,
pub request_location_enabled: bool,
pub request_location: gateway_core::account::RequestLocation,
@@ -73,7 +75,6 @@ pub struct RuntimeSettingsValues {
pub account_warmup_enabled: bool,
pub account_warmup_cron: String,
pub account_warmup_model: Option<String>,
#[serde(default)]
pub reset_credit_on_low_weekly_quota: bool,
pub reset_credit_lead_minutes: u32,
pub reset_credit_max_weekly_remaining_percent: Option<u32>,
@@ -200,6 +201,9 @@ impl RuntimeSettingsValues {
{
return Err("reset_credit_max_weekly_remaining_percent");
}
self.openai_request_country
.validate()
.map_err(|_| "openai_request_country")?;
self.request_location
.validate()
.map_err(|_| "request_location")?;
@@ -231,6 +231,19 @@ pub trait ProviderAdmin: Send + Sync {
self.preview_client_profile(key)
}
/// 在批量更新中合并本地身份;缺省、清除和替换分别保留各自语义,不能执行 I/O
fn merge_batch_key_profile(
&self,
current: Option<&gateway_core::account::OpaqueProviderData>,
update: Option<&Option<gateway_core::account::OpaqueProviderData>>,
installation_seed: Option<uuid::Uuid>,
) -> Result<Option<gateway_core::account::OpaqueProviderData>, ProviderAdminError> {
if installation_seed.is_some() {
return Err(ProviderAdminError::new(ProviderAdminErrorKind::Unsupported));
}
Ok(update.map_or_else(|| current.cloned(), Clone::clone))
}
/// 提供最终套餐展示名称;未覆盖时采用通用的大驼峰格式
fn plan_type_display(&self, plan_type: &str) -> String {
default_plan_type_display(plan_type)
@@ -22,10 +22,11 @@ use crate::model::{
},
auth::{AdminAuditEvent, AuthSession},
client_keys::{
ClientKeyBudgetMutationOrigin, ClientKeyBudgetWindow, ClientKeyBudgetWindowPeriod,
ClientKeyListQuery, ClientKeyPage, ClientKeyRecord, ClientKeySecret, DeleteClientKey,
NewClientKey, ResetClientKeyBudget, SetClientKeyBudgetWindow, SetClientKeyEnabled,
UpdateClientKey, UpdateClientKeyBudgetLimits,
BatchUpdateClientKeys, BudgetReferenceAccountPage, ClientKeyBudgetMutationOrigin,
ClientKeyBudgetWindow, ClientKeyBudgetWindowPeriod, ClientKeyListQuery, ClientKeyPage,
ClientKeyRecord, ClientKeySecret, DeleteClientKey, NewClientKey, ResetClientKeyBudget,
SetClientKeyBudgetWindow, SetClientKeyEnabled, UpdateClientKey,
UpdateClientKeyBudgetLimits,
},
observability::{
DashboardObservation, DashboardQuery, DiagnosticDimension, DiagnosticsObservation,
@@ -109,6 +110,13 @@ impl AdminStoreError {
pub type AdminStoreResult<T> = Result<T, AdminStoreError>;
/// 在持有密钥行锁时用 Provider 准备好的纯合并操作更新原有画像
pub type BatchClientKeyProfileMerger = Box<
dyn Fn(&mut crate::model::client_keys::ProviderRequestProfileOverrides) -> AdminStoreResult<()>
+ Send
+ Sync,
>;
/// 账号目录与公共账号写操作
#[async_trait]
pub trait AccountStore:
@@ -377,6 +385,18 @@ pub trait AuthStore: Send + Sync {
/// Client API Key 资料读取与管理写入
#[async_trait]
pub trait ClientKeyStore: Send + Sync {
async fn client_key_budget(
&self,
id: &gateway_core::policy::ClientApiKeyId,
) -> AdminStoreResult<Option<gateway_core::engine::budget::ClientBudgetStatus>>;
async fn budget_reference_accounts(
&self,
id: &gateway_core::policy::ClientApiKeyId,
search: &str,
after: &str,
) -> AdminStoreResult<BudgetReferenceAccountPage>;
async fn client_key_budget_window(
&self,
id: &gateway_core::policy::ClientApiKeyId,
@@ -415,6 +435,13 @@ pub trait ClientKeyStore: Send + Sync {
context: &MutationContext,
) -> AdminStoreResult<(Revision, ClientKeyRecord)>;
async fn batch_update_client_keys(
&self,
command: BatchUpdateClientKeys,
merge_profiles: BatchClientKeyProfileMerger,
context: &MutationContext,
) -> AdminStoreResult<Revision>;
async fn set_client_key_enabled(
&self,
command: SetClientKeyEnabled,
@@ -518,6 +545,19 @@ pub trait ObservabilityStore: Send + Sync {
granularity: Granularity,
) -> AdminStoreResult<Vec<RequestMetricPoint>>;
async fn client_key_statistics(
&self,
range: TimeRange,
id: &gateway_core::policy::ClientApiKeyId,
query: crate::model::observability::KeyUsageMetric,
) -> AdminStoreResult<crate::model::observability::KeyStatisticsOverview>;
async fn client_key_models(
&self,
range: TimeRange,
id: &gateway_core::policy::ClientApiKeyId,
) -> AdminStoreResult<Vec<crate::model::observability::KeyModelRow>>;
async fn usage_trend(
&self,
range: TimeRange,
@@ -9,8 +9,8 @@ use std::{
use async_trait::async_trait;
use gateway_core::{
account::{AccountStatus, resolve_account_status},
routing::AccountGroupId,
runtime::SnapshotControl,
routing::{AccountGroupId, UpstreamModelId},
runtime::{RuntimeSnapshotHandle, SnapshotControl},
};
use uuid::Uuid;
@@ -32,6 +32,7 @@ use super::{map_store_error, publish_committed};
/// 面向 API 的账号分组管理服务
#[async_trait]
pub trait AccountGroupService: Send + Sync {
fn models(&self, group: &AccountGroupId) -> Result<Vec<UpstreamModelId>, AdminError>;
async fn list(&self, query: AccountGroupListQuery) -> Result<AccountGroupPage, AdminError>;
async fn list_options(
&self,
@@ -64,6 +65,7 @@ pub(crate) struct DefaultAccountGroupService {
store: Arc<dyn AccountGroupStore>,
runtime: Arc<dyn AccountRuntimeStore>,
snapshot: Arc<dyn SnapshotControl>,
published_snapshot: RuntimeSnapshotHandle,
}
impl DefaultAccountGroupService {
@@ -73,12 +75,14 @@ impl DefaultAccountGroupService {
store: Arc<dyn AccountGroupStore>,
runtime: Arc<dyn AccountRuntimeStore>,
snapshot: Arc<dyn SnapshotControl>,
published_snapshot: RuntimeSnapshotHandle,
) -> Self {
Self {
upstream_info,
store,
runtime,
snapshot,
published_snapshot,
}
}
@@ -131,6 +135,14 @@ impl DefaultAccountGroupService {
#[async_trait]
impl AccountGroupService for DefaultAccountGroupService {
fn models(&self, group: &AccountGroupId) -> Result<Vec<UpstreamModelId>, AdminError> {
let snapshot = self
.published_snapshot
.acquire()
.map_err(|_| AdminError::unavailable("模型目录尚未就绪"))?;
Ok(snapshot.upstream_models_for_group(group))
}
async fn list(&self, query: AccountGroupListQuery) -> Result<AccountGroupPage, AdminError> {
let mut page = self
.store
@@ -167,6 +179,8 @@ impl AccountGroupService for DefaultAccountGroupService {
.create_account_group(
NewAccountGroup {
allow_image_generation: command.allow_image_generation,
allow_subagents: command.allow_subagents,
model_access: command.model_access,
id,
name: command.name,
description: command.description,
@@ -20,11 +20,13 @@ use gateway_core::{
use crate::{
model::{
AdminError, MutationContext,
account_groups::AccountGroupRef,
accounts::{
AccountConnectionTestEvent, AccountConnectionTestEventStream, AccountListQuery,
AccountPageItem, AccountUpdateResult, AccountUsage, AccountUsageWindowQuery,
AccountsUpdateResult, BatchUpdateAccounts, UpdateAccount,
},
client_keys::ClientKeyBudgetReference,
observability::TimeRange,
provider_credentials::{
AccountDirectoryItem, AccountDirectoryPage, AccountExportBundle, AccountPersonalInfo,
@@ -107,6 +109,12 @@ pub trait AccountsService: Send + Sync {
refresh: bool,
) -> Result<AccountDirectoryItem, AdminError>;
async fn key_budget_references(
&self,
account_id: &ProviderAccountId,
key_groups: &[AccountGroupRef],
) -> Result<Vec<ClientKeyBudgetReference>, AdminError>;
async fn quota_forecast(
&self,
account_id: &ProviderAccountId,
@@ -972,6 +980,48 @@ impl AccountsService for DefaultAccountsService {
self.load_directory_item(account_id, refresh).await
}
async fn key_budget_references(
&self,
account_id: &ProviderAccountId,
key_groups: &[AccountGroupRef],
) -> Result<Vec<ClientKeyBudgetReference>, AdminError> {
let (stored, provider) = self.provider_for_account(account_id).await?;
if !key_groups.is_empty()
&& !stored.account.groups.iter().any(|account_group| {
account_group.enabled && key_groups.iter().any(|group| group.id == account_group.id)
})
{
return Err(AdminError::bad_request("参考账号不在此密钥的账号范围内"));
}
// 只取已观测的额度边界,预览与确认都不触发上游刷新或账号用量聚合
let quota = match super::peer_link::upstream::read_quota(
self.upstream_info.as_deref(),
provider.as_ref(),
&stored.account,
ProviderQuotaRequest {
account_id: account_id.clone(),
refresh: false,
rolling_usage: None,
},
)
.await
{
Ok(quota) => quota,
Err(error)
if error.kind() == crate::ports::provider::ProviderAdminErrorKind::Unsupported =>
{
return Ok(Vec::new());
}
Err(error) => return Err(map_provider_error(error, "provider quota")),
};
let now = Utc::now();
Ok(quota
.windows
.into_iter()
.filter_map(|window| ClientKeyBudgetReference::from_current_window(window, now))
.collect())
}
async fn quota_forecast(
&self,
account_id: &ProviderAccountId,
@@ -1304,9 +1354,7 @@ fn quota_usage_window(
if window.local_usage_attribution != QuotaLocalUsageAttribution::AccountWide {
return None;
}
let reset_at = window.reset_at?;
let seconds = i64::try_from(window.window_seconds?).ok()?;
let start = reset_at.checked_sub_signed(Duration::try_seconds(seconds)?)?;
let (start, reset_at) = window.observed_bounds()?;
let range = TimeRange::new(start, reset_at).ok()?;
// 上游百分比以该 reset 边界定义;以当前时间回推会让本地 Token 属于另一窗口
Some(AccountUsageWindowQuery {
@@ -1,11 +1,12 @@
//! Client API Key 管理用例
use std::sync::Arc;
use std::{collections::BTreeSet, sync::Arc};
use async_trait::async_trait;
use base64::{Engine as _, engine::general_purpose::URL_SAFE_NO_PAD};
use gateway_core::{
engine::budget::ClientBudgetStatus, policy::ClientApiKeyId, runtime::SnapshotControl,
account::OpaqueProviderData, engine::budget::ClientBudgetStatus, policy::ClientApiKeyId,
routing::ProviderKind, runtime::SnapshotControl,
};
use rand_core::{OsRng, RngCore as _};
use uuid::Uuid;
@@ -14,15 +15,17 @@ use crate::{
model::{
AdminError, MutationContext,
client_keys::{
ClientKeyBudgetMutationOrigin, ClientKeyBudgetWindow, ClientKeyBudgetWindowPeriod,
ClientKeyCursorValue, ClientKeyListQuery, ClientKeyMutation, ClientKeyPage,
ClientKeyRecord, ClientKeySecret, ClientKeySortField, CreateClientKey,
CreatedClientKey, DeleteClientKey, NewClientKey, ResetClientKeyBudget,
SetClientKeyBudgetWindow, SetClientKeyEnabled, UpdateClientKey,
BatchUpdateClientKeys, BudgetReferenceAccountPage, ClientKeyBudgetMutationOrigin,
ClientKeyBudgetWindow, ClientKeyBudgetWindowPeriod, ClientKeyCursorValue,
ClientKeyListQuery, ClientKeyMutation, ClientKeyPage, ClientKeyRecord, ClientKeySecret,
ClientKeySortField, CreateClientKey, CreatedClientKey, DeleteClientKey, NewClientKey,
ResetClientKeyBudget, SetClientKeyBudgetWindow, SetClientKeyEnabled, UpdateClientKey,
UpdateClientKeyBudgetLimits,
},
},
ports::store::{AdminStoreError, AdminStoreErrorKind, ClientKeyStore},
ports::store::{
AdminStoreError, AdminStoreErrorKind, BatchClientKeyProfileMerger, ClientKeyStore,
},
};
use super::{map_store_error, publish_committed};
@@ -30,6 +33,13 @@ use super::{map_store_error, publish_committed};
/// API 消费的 Client Key 管理服务
#[async_trait]
pub trait ClientKeyService: Send + Sync {
async fn budget_reference_accounts(
&self,
id: &gateway_core::policy::ClientApiKeyId,
search: &str,
after: &str,
) -> Result<BudgetReferenceAccountPage, AdminError>;
async fn budget_window(
&self,
id: &ClientApiKeyId,
@@ -55,6 +65,11 @@ pub trait ClientKeyService: Send + Sync {
context: &MutationContext,
command: UpdateClientKey,
) -> Result<ClientKeyMutation, AdminError>;
async fn batch_update(
&self,
context: &MutationContext,
command: BatchUpdateClientKeys,
) -> Result<crate::model::Revision, AdminError>;
async fn set_enabled(
&self,
context: &MutationContext,
@@ -99,10 +114,123 @@ impl DefaultClientKeyService {
providers,
}
}
fn validate_profile(
&self,
kind: &ProviderKind,
profile: &OpaqueProviderData,
) -> Result<(), AdminError> {
self.providers
.require(kind)
.and_then(|provider| provider.preview_key_client_profile(profile, None))
.map(|_| ())
.map_err(|error| super::map_provider_error(error, "client profile"))
}
fn batch_profile_merger(
&self,
command: &BatchUpdateClientKeys,
) -> Result<BatchClientKeyProfileMerger, AdminError> {
let mut kinds = command
.request_profile_override_updates
.keys()
.cloned()
.collect::<BTreeSet<_>>();
let seed_kind = ProviderKind::new("openai").expect("static Provider kind is valid");
if command.installation_seed.is_some() {
kinds.insert(seed_kind.clone());
}
let mut changes = Vec::with_capacity(kinds.len());
for kind in kinds {
let provider = self
.providers
.require(&kind)
.map_err(|error| super::map_provider_error(error, "client profile"))?;
let update = command.request_profile_override_updates.get(&kind).cloned();
let seed = if kind == seed_kind {
command.installation_seed
} else {
None
};
// 先校验输入,行锁内只依据各把密钥的现值完成合并
if let Some(Some(profile)) = update.as_ref() {
provider
.merge_batch_key_profile(None, update.as_ref(), seed)
.and_then(|_| provider.preview_key_client_profile(profile, None))
.map_err(|error| super::map_provider_error(error, "client profile"))?;
}
changes.push((kind, provider, update, seed));
}
Ok(Box::new(move |profiles| {
for (kind, provider, update, seed) in &changes {
let next = provider
.merge_batch_key_profile(profiles.get(kind), update.as_ref(), *seed)
.map_err(|error| {
AdminStoreError::new(
AdminStoreErrorKind::Invalid,
"client profile",
"invalid stored client profile",
)
.with_source(error)
})?;
if let Some(profile) = next {
profiles.insert(kind.clone(), profile);
} else {
profiles.remove(kind);
}
}
Ok(())
}))
}
}
#[async_trait]
impl ClientKeyService for DefaultClientKeyService {
async fn batch_update(
&self,
context: &MutationContext,
command: BatchUpdateClientKeys,
) -> Result<crate::model::Revision, AdminError> {
if command.ids.is_empty()
|| command.ids.len() > 1000
|| command
.ids
.iter()
.collect::<std::collections::BTreeSet<_>>()
.len()
!= command.ids.len()
{
return Err(AdminError::invalid("请选择 1 至 1000 个不同的 API Key"));
}
if !command.has_changes() {
return Err(AdminError::invalid("请至少选择一项要修改的密钥设置"));
}
if let Some(groups) = &command.group_ids {
crate::model::client_keys::validate_group_ids(groups)
.map_err(|_| AdminError::invalid("API Key 分组不合法"))?;
}
let merge_profiles = self.batch_profile_merger(&command)?;
let revision = self
.store
.batch_update_client_keys(command, merge_profiles, context)
.await
.map_err(|error| map_store_error(error, "client API key"))?;
publish_committed(self.snapshot.as_ref(), revision).await?;
Ok(revision)
}
async fn budget_reference_accounts(
&self,
id: &gateway_core::policy::ClientApiKeyId,
search: &str,
after: &str,
) -> Result<BudgetReferenceAccountPage, AdminError> {
self.store
.budget_reference_accounts(id, search.trim(), after)
.await
.map_err(|error| map_store_error(error, "client API key"))
}
async fn budget_window(
&self,
id: &ClientApiKeyId,
@@ -141,7 +269,11 @@ impl ClientKeyService for DefaultClientKeyService {
}
async fn budget(&self, id: &ClientApiKeyId) -> Result<ClientBudgetStatus, AdminError> {
self.get(id).await.map(|key| key.budget)
self.store
.client_key_budget(id)
.await
.map_err(|error| map_store_error(error, "client API key"))?
.ok_or_else(|| AdminError::not_found("API Key 不存在"))
}
async fn update_budget_limits(
@@ -203,10 +335,7 @@ impl ClientKeyService for DefaultClientKeyService {
crate::model::client_keys::validate_group_ids(&command.group_ids)
.map_err(|_| AdminError::invalid("API Key 分组不合法"))?;
for (provider, profile) in &command.request_profile_overrides {
self.providers
.require(provider)
.and_then(|provider| provider.preview_key_client_profile(profile, None))
.map_err(|error| super::map_provider_error(error, "client profile"))?;
self.validate_profile(provider, profile)?;
}
validate_peer_sharing(command.peer_sharing, &command.group_ids)?;
let id = ClientApiKeyId::new(format!("key_{}", Uuid::now_v7().simple()))
@@ -250,10 +379,7 @@ impl ClientKeyService for DefaultClientKeyService {
.map_err(|_| AdminError::invalid("API Key 分组不合法"))?;
for (provider, profile) in &command.request_profile_override_updates {
if let Some(profile) = profile {
self.providers
.require(provider)
.and_then(|provider| provider.preview_key_client_profile(profile, None))
.map_err(|error| super::map_provider_error(error, "client profile"))?;
self.validate_profile(provider, profile)?;
}
}
if let Some(sharing) = command.peer_sharing {
@@ -150,7 +150,6 @@ pub(super) async fn pending_authorization(
None => {
use crate::model::proxies::AccountProxySelection;
proxy = match &command.outbound_proxy {
Some(AccountProxySelection::Url(value)) => Some(value.clone()),
Some(AccountProxySelection::Saved(id)) => {
let record = proxies
.get(id)
@@ -60,6 +60,19 @@ pub trait ObservabilityService: Send + Sync {
-> Result<Trend, AdminError>;
async fn usage_records(&self, query: UsageQuery) -> Result<UsagePage, AdminError>;
async fn usage_record_detail(&self, request_id: &str) -> Result<UsageDetail, AdminError>;
async fn client_key_statistics(
&self,
range: TimeRange,
id: &gateway_core::policy::ClientApiKeyId,
query: crate::model::observability::KeyUsageMetric,
) -> Result<crate::model::observability::KeyStatisticsOverview, AdminError>;
async fn client_key_models(
&self,
range: TimeRange,
id: &gateway_core::policy::ClientApiKeyId,
) -> Result<Vec<crate::model::observability::KeyModelRow>, AdminError>;
async fn usage_summary(
&self,
range: TimeRange,
@@ -287,6 +300,29 @@ impl ObservabilityService for DefaultObservabilityService {
Ok(detail)
}
async fn client_key_statistics(
&self,
range: TimeRange,
id: &gateway_core::policy::ClientApiKeyId,
query: crate::model::observability::KeyUsageMetric,
) -> Result<crate::model::observability::KeyStatisticsOverview, AdminError> {
self.store
.client_key_statistics(range, id, query)
.await
.map_err(|error| map_store_error(error, "client key statistics"))
}
async fn client_key_models(
&self,
range: TimeRange,
id: &gateway_core::policy::ClientApiKeyId,
) -> Result<Vec<crate::model::observability::KeyModelRow>, AdminError> {
self.store
.client_key_models(range, id)
.await
.map_err(|error| map_store_error(error, "client key statistics"))
}
async fn usage_summary(
&self,
range: TimeRange,
@@ -226,6 +226,23 @@ impl ObservabilityStore for CachedObservabilityStore {
) -> AdminStoreResult<Vec<RequestMetricPoint>> {
self.inner.dashboard_trend(range, granularity).await
}
async fn client_key_statistics(
&self,
range: TimeRange,
id: &gateway_core::policy::ClientApiKeyId,
query: crate::model::observability::KeyUsageMetric,
) -> AdminStoreResult<crate::model::observability::KeyStatisticsOverview> {
self.inner.client_key_statistics(range, id, query).await
}
async fn client_key_models(
&self,
range: TimeRange,
id: &gateway_core::policy::ClientApiKeyId,
) -> AdminStoreResult<Vec<crate::model::observability::KeyModelRow>> {
self.inner.client_key_models(range, id).await
}
async fn usage_trend(
&self,
range: TimeRange,
@@ -53,6 +53,8 @@ impl PluginResourceAccess for DefaultPluginResourceAccess {
resource_key,
NewAccountGroup {
allow_image_generation: command.allow_image_generation,
allow_subagents: command.allow_subagents,
model_access: command.model_access,
id,
name: command.name,
description: command.description,
@@ -116,10 +116,10 @@ fn validate_settings(command: &ReplaceRuntimeSettings) -> Result<(), AdminError>
.map(|(requested, upstream)| (requested.as_str(), upstream.as_str())),
)
.map_err(|_| AdminError::invalid("运行时设置不满足约束"))?;
command
.values
.validate()
.map_err(|_| AdminError::invalid("运行时设置不满足约束"))
command.values.validate().map_err(|field| match field {
"openai_request_country" => AdminError::invalid("请求国家必须是两位大写 ASCII 字母"),
_ => AdminError::invalid("运行时设置不满足约束"),
})
}
#[async_trait]
@@ -267,7 +267,7 @@ impl SettingsService for DefaultSettingsService {
)
})
{
return Err(AdminError::invalid("xAI 不支持此价格档位"));
return Err(AdminError::invalid("当前平台不支持此价格档位"));
}
if !pricing.bands.contains_key("standard")
&& command
@@ -335,3 +335,42 @@ fn pending_mutation_v1_should_keep_existing_storage_fields_and_reject_invalid_do
document["schema_version"] = json!(2);
assert!(PendingAuthorizationMutation::from_storage_v1(document).is_err());
}
#[test]
fn manual_key_alignment_only_accepts_the_current_account_wide_day_or_week() {
use chrono::{Duration, Utc};
use gateway_admin::model::{
client_keys::{ClientKeyBudgetReference, ClientKeyBudgetWindowPeriod},
upstream_info::UpstreamQuotaWindow,
};
let now = Utc::now();
for (seconds, period) in [
(86_400, ClientKeyBudgetWindowPeriod::Daily),
(604_800, ClientKeyBudgetWindowPeriod::Weekly),
] {
let mut window = usage_window("account", "quota", seconds);
let end = now + Duration::hours(2);
window.reset_at = Some(end);
let reference = ClientKeyBudgetReference::from_current_window(window.clone(), now).unwrap();
let selected = reference.period;
let bounds = reference.bounds;
assert_eq!(selected, period);
assert_eq!(bounds.starts_at, end - Duration::seconds(seconds as i64));
assert_eq!(bounds.ends_at, end);
assert!(bounds.resume_calendar);
assert!(ClientKeyBudgetReference::from_current_window(window.clone(), end).is_none());
window.upstream = Some(UpstreamQuotaWindow {
account_id: None,
observed_at: Some(now),
stale: true,
amount: None,
});
assert!(ClientKeyBudgetReference::from_current_window(window.clone(), now).is_none());
window.upstream = None;
window.local_usage_attribution = QuotaLocalUsageAttribution::Unavailable;
assert!(ClientKeyBudgetReference::from_current_window(window.clone(), now).is_none());
}
let mut short = usage_window("primary", "short", 18_000);
short.reset_at = Some(now + Duration::hours(1));
assert!(ClientKeyBudgetReference::from_current_window(short, now).is_none());
}
@@ -344,6 +344,8 @@ fn group_record() -> AccountGroupRecord {
let now = Utc::now();
AccountGroupRecord {
allow_image_generation: true,
allow_subagents: true,
model_access: Default::default(),
fast_mode: FastMode::Default,
id: group_id(),
name: "Primary".to_owned(),
@@ -420,6 +422,8 @@ async fn invalid_group_fields_are_rejected_before_store_mutations() {
&context,
CreateAccountGroup {
allow_image_generation: true,
allow_subagents: true,
model_access: Default::default(),
fast_mode: FastMode::Default,
name: " invalid".to_owned(),
description: None,
@@ -434,6 +438,8 @@ async fn invalid_group_fields_are_rejected_before_store_mutations() {
&context,
UpdateAccountGroup {
allow_image_generation: None,
allow_subagents: None,
model_access: None,
fast_mode: None,
id: record.id,
name: "valid".to_owned(),
@@ -266,6 +266,7 @@ impl FakeProviderAdmin {
plan_type: account.plan_type.clone(),
preserve_profile: false,
preserve_credential_state: false,
preserve_access_token_expiry: false,
provider_material: document(),
has_refresh_token: account.has_refresh_token,
access_token_expires_at: account
@@ -1173,6 +1174,8 @@ impl SettingsStore for StaticSettingsStore {
rotation_strategy: RotationStrategy::Smart,
updated_at: Utc::now(),
values: gateway_admin::model::settings::RuntimeSettingsValues {
openai_oauth_guardian_review_model: false,
openai_request_country: Default::default(),
codex_privacy_policy: Default::default(),
request_location_enabled: false,
request_location: Default::default(),
@@ -3513,6 +3516,7 @@ fn plugin_rotation_facts(account: &AccountRecord) -> PreparedCredentialRotationF
plan_type: account.plan_type.clone(),
preserve_profile: true,
preserve_credential_state: false,
preserve_access_token_expiry: false,
provider_material: document(),
has_refresh_token: account.has_refresh_token,
access_token_expires_at: account.access_token_expires_at,
@@ -35,6 +35,26 @@ struct TestClientKeyStore {
#[async_trait]
impl ClientKeyStore for TestClientKeyStore {
async fn budget_reference_accounts(
&self,
_: &gateway_core::policy::ClientApiKeyId,
_: &str,
_: &str,
) -> gateway_admin::ports::store::AdminStoreResult<
gateway_admin::model::client_keys::BudgetReferenceAccountPage,
> {
Err(unused())
}
async fn batch_update_client_keys(
&self,
_: gateway_admin::model::client_keys::BatchUpdateClientKeys,
_: gateway_admin::ports::store::BatchClientKeyProfileMerger,
_: &MutationContext,
) -> AdminStoreResult<Revision> {
Err(unused())
}
async fn client_key_budget_window(
&self,
_: &gateway_core::policy::ClientApiKeyId,
@@ -80,6 +100,15 @@ impl ClientKeyStore for TestClientKeyStore {
Ok(())
}
async fn client_key_budget(
&self,
id: &gateway_core::policy::ClientApiKeyId,
) -> AdminStoreResult<Option<gateway_core::engine::budget::ClientBudgetStatus>> {
self.get_client_key(id)
.await
.map(|key| key.map(|row| row.budget))
}
async fn get_client_key(
&self,
_: &ClientApiKeyId,
@@ -172,6 +201,34 @@ impl ClientKeyStore for TestClientKeyStore {
}
}
#[tokio::test]
async fn batch_keys_require_a_change_and_unique_targets() {
use gateway_admin::model::client_keys::BatchUpdateClientKeys;
let services = super::AdminHarness::new()
.client_keys(Arc::new(TestClientKeyStore::default()))
.build()
.await;
let id = ClientApiKeyId::new("key_a").unwrap();
for patch in [
BatchUpdateClientKeys {
ids: vec![id.clone()],
..Default::default()
},
BatchUpdateClientKeys {
ids: vec![id.clone(), id],
enabled: Some(false),
..Default::default()
},
] {
let error = services
.client_keys()
.batch_update(&mutation_context(), patch)
.await
.unwrap_err();
assert_eq!(error.kind(), AdminErrorKind::Invalid);
}
}
#[tokio::test]
async fn reset_budget_forwards_scope_and_returns_only_key_identity() {
use gateway_admin::model::client_keys::{ClientKeyBudgetPeriod, ResetClientKeyBudget};
@@ -107,8 +107,7 @@ async fn openai_import_should_expose_only_explicit_public_errors_without_committ
] {
let events = events();
let provider = FakeProviderAdmin::new("openai", events.clone());
provider
.fail_next_with_public_message(provider_kind, "Codex PAT 验证失败,请检查令牌后重试");
provider.fail_next_with_public_message(provider_kind, "PAT 验证失败,请检查令牌");
let store = FakeAccountStore::new("openai", events.clone());
let services = service(provider, store.clone()).await;
let error = services
@@ -124,7 +123,7 @@ async fn openai_import_should_expose_only_explicit_public_errors_without_committ
.await
.expect_err("validation must fail before committing accounts");
assert_eq!(error.kind(), admin_kind);
assert_eq!(error.to_string(), "Codex PAT 验证失败,请检查令牌后重试");
assert_eq!(error.to_string(), "PAT 验证失败,请检查令牌");
assert!(!format!("{error:?}").contains("secret token"));
assert_eq!(recorded(&events), ["provider.prepare_import"]);
assert!(store.audit_requests().is_empty());
@@ -33,6 +33,8 @@ fn runtime_settings(enabled: bool, probe_enabled: bool) -> RuntimeSettings {
rotation_strategy: gateway_admin::model::settings::RotationStrategy::Smart,
updated_at: Utc::now(),
values: gateway_admin::model::settings::RuntimeSettingsValues {
openai_oauth_guardian_review_model: false,
openai_request_country: Default::default(),
codex_privacy_policy: Default::default(),
request_location_enabled: false,
request_location: Default::default(),
@@ -719,6 +719,26 @@ impl AccountRuntimeStore for UnavailableStore {
#[async_trait]
impl ClientKeyStore for UnavailableStore {
async fn budget_reference_accounts(
&self,
_: &gateway_core::policy::ClientApiKeyId,
_: &str,
_: &str,
) -> gateway_admin::ports::store::AdminStoreResult<
gateway_admin::model::client_keys::BudgetReferenceAccountPage,
> {
Err(unavailable("reference accounts"))
}
async fn batch_update_client_keys(
&self,
_: gateway_admin::model::client_keys::BatchUpdateClientKeys,
_: gateway_admin::ports::store::BatchClientKeyProfileMerger,
_: &MutationContext,
) -> AdminStoreResult<Revision> {
Err(unavailable("client key batch update"))
}
async fn client_key_budget_window(
&self,
_: &gateway_core::policy::ClientApiKeyId,
@@ -763,6 +783,15 @@ impl ClientKeyStore for UnavailableStore {
Err(unavailable("client key budget reset"))
}
async fn client_key_budget(
&self,
id: &gateway_core::policy::ClientApiKeyId,
) -> AdminStoreResult<Option<gateway_core::engine::budget::ClientBudgetStatus>> {
self.get_client_key(id)
.await
.map(|key| key.map(|row| row.budget))
}
async fn get_client_key(
&self,
_: &ClientApiKeyId,
@@ -844,6 +873,23 @@ impl ObservabilityStore for UnavailableStore {
Err(unavailable("dashboard trend"))
}
async fn client_key_statistics(
&self,
_: TimeRange,
_: &gateway_core::policy::ClientApiKeyId,
_: gateway_admin::model::observability::KeyUsageMetric,
) -> AdminStoreResult<gateway_admin::model::observability::KeyStatisticsOverview> {
Ok(Default::default())
}
async fn client_key_models(
&self,
_: TimeRange,
_: &gateway_core::policy::ClientApiKeyId,
) -> AdminStoreResult<Vec<gateway_admin::model::observability::KeyModelRow>> {
Ok(Default::default())
}
async fn usage_trend(
&self,
_: TimeRange,
@@ -1266,6 +1266,23 @@ impl ObservabilityStore for FixtureObservabilityStore {
Ok(self.trend.lock().expect("trend").clone())
}
async fn client_key_statistics(
&self,
_: TimeRange,
_: &gateway_core::policy::ClientApiKeyId,
_: gateway_admin::model::observability::KeyUsageMetric,
) -> AdminStoreResult<gateway_admin::model::observability::KeyStatisticsOverview> {
Ok(Default::default())
}
async fn client_key_models(
&self,
_: TimeRange,
_: &gateway_core::policy::ClientApiKeyId,
) -> AdminStoreResult<Vec<gateway_admin::model::observability::KeyModelRow>> {
Ok(Default::default())
}
async fn usage_trend(
&self,
_: TimeRange,
@@ -1414,6 +1431,8 @@ impl SettingsStore for FixtureSettingsStore {
rotation_strategy: RotationStrategy::Smart,
updated_at: Utc::now(),
values: gateway_admin::model::settings::RuntimeSettingsValues {
openai_oauth_guardian_review_model: false,
openai_request_country: Default::default(),
codex_privacy_policy: Default::default(),
request_location_enabled: false,
request_location: 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",
@@ -325,6 +325,8 @@ async fn settings_should_reject_zero_refresh_margin_before_store_call() {
model_mappings: Default::default(),
rotation_strategy: RotationStrategy::Smart,
values: gateway_admin::model::settings::RuntimeSettingsValues {
openai_oauth_guardian_review_model: true,
openai_request_country: Default::default(),
codex_privacy_policy: Default::default(),
request_location_enabled: false,
request_location: Default::default(),
@@ -21,12 +21,15 @@ use gateway_admin::model::{
UpdateAccountGroup,
},
};
use gateway_core::{account::FastMode, routing::AccountGroupId};
use gateway_core::{
account::{AccountModelAccess, FastMode},
routing::AccountGroupId,
};
use serde::{Deserialize, Serialize};
use super::{
AdminAuth, AdminEnvelope, AdminError, AdminJson, AdminQuery, AdminResponse, PageMeta,
WireValidationError, wire::map_admin_service_error,
WireValidationError, accounts::AccountModelView, wire::map_admin_service_error,
};
/// 账号、代理与 Client Key 共用的分组引用
@@ -90,15 +93,19 @@ impl ListAccountGroupsQuery {
}
}
const fn default_allow_image_generation() -> bool {
const fn default_allowed() -> bool {
true
}
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
struct CreateAccountGroupRequest {
#[serde(default = "default_allow_image_generation")]
#[serde(default = "default_allowed")]
allow_image_generation: bool,
#[serde(default = "default_allowed")]
allow_subagents: bool,
#[serde(default)]
model_access: AccountModelAccess,
#[serde(default)]
fast_mode: FastMode,
name: String,
@@ -110,6 +117,8 @@ struct CreateAccountGroupRequest {
#[serde(rename_all = "camelCase", deny_unknown_fields)]
struct UpdateAccountGroupRequest {
allow_image_generation: Option<bool>,
allow_subagents: Option<bool>,
model_access: Option<AccountModelAccess>,
fast_mode: Option<FastMode>,
id: String,
name: String,
@@ -127,6 +136,8 @@ struct AccountGroupIdRequest {
#[serde(rename_all = "camelCase")]
struct AccountGroupView {
allow_image_generation: bool,
allow_subagents: bool,
model_access: AccountModelAccess,
fast_mode: FastMode,
id: String,
name: String,
@@ -175,6 +186,8 @@ impl From<(AccountGroupRecord, crate::time::TimePresenter)> for AccountGroupView
description: record.description,
fast_mode: record.fast_mode,
allow_image_generation: record.allow_image_generation,
allow_subagents: record.allow_subagents,
model_access: record.model_access,
color: record.color.as_str().to_owned(),
enabled: record.enabled,
member_count: record.member_count,
@@ -304,6 +317,7 @@ where
{
Router::new()
.route("/api/admin/account-groups", get(list::<S>))
.route("/api/admin/account-groups/models", get(models::<S>))
.route("/api/admin/account-groups/options", get(list_options::<S>))
.route("/api/admin/account-groups/create", post(create::<S>))
.route("/api/admin/account-groups/update", post(update::<S>))
@@ -332,6 +346,32 @@ where
))
}
#[derive(Serialize)]
struct AccountGroupModelsData {
models: Vec<AccountModelView>,
}
async fn models<S>(
_auth: AdminAuth,
State(state): State<S>,
AdminQuery(query): AdminQuery<AccountGroupIdRequest>,
) -> Result<impl IntoResponse, AdminError>
where
S: SessionState + Send + Sync,
{
let models = state
.admin_services()
.account_groups()
.models(&group_id(query.id)?)
.map_err(map_service_error)?;
Ok(AdminResponse::new(
StatusCode::OK,
AdminEnvelope::ok(AccountGroupModelsData {
models: models.into_iter().map(AccountModelView::from).collect(),
}),
))
}
async fn list<S>(
_auth: AdminAuth,
State(state): State<S>,
@@ -371,6 +411,8 @@ where
&auth.context().mutation_context(),
CreateAccountGroup {
allow_image_generation: request.allow_image_generation,
allow_subagents: request.allow_subagents,
model_access: request.model_access,
name: request.name,
description: request.description,
fast_mode: request.fast_mode,
@@ -400,6 +442,8 @@ where
&auth.context().mutation_context(),
UpdateAccountGroup {
allow_image_generation: request.allow_image_generation,
allow_subagents: request.allow_subagents,
model_access: request.model_access,
id: group_id(request.id)?,
name: request.name,
description: request.description,
@@ -118,7 +118,6 @@ pub struct StartAccountAuthorizationRequest {
pub provider: String,
pub name: String,
pub account_id: Option<String>,
pub outbound_proxy_url: Option<super::wire::AccountProxyUpdate>,
}
impl StartAccountAuthorizationRequest {
@@ -148,10 +147,7 @@ impl StartAccountAuthorizationRequest {
context,
name: self.name.trim().to_owned(),
reauthorization,
outbound_proxy: super::wire::proxy_selection(
self.outbound_proxy_id,
self.outbound_proxy_url,
)?,
outbound_proxy: super::wire::proxy_selection(self.outbound_proxy_id)?,
},
))
}
@@ -219,7 +215,6 @@ fn validate_authorization_flow(
pub struct UpdateAccountRequest {
pub connection: Option<AccountConnectionUpdateRequest>,
pub outbound_proxy_id: Option<String>,
pub outbound_proxy_url: Option<super::wire::AccountProxyUpdate>,
pub account_id: String,
pub name: Option<String>,
pub notes: Option<String>,
@@ -255,10 +250,7 @@ impl UpdateAccountRequest {
.connection
.map(AccountConnectionUpdateRequest::into_document);
let settings = UpdateAccount {
outbound_proxy: super::wire::proxy_selection(
self.outbound_proxy_id,
self.outbound_proxy_url,
)?,
outbound_proxy: super::wire::proxy_selection(self.outbound_proxy_id)?,
account_id: self.account_id,
name: self.name.map(|name| name.trim().to_owned()),
notes: self.notes,
@@ -53,13 +53,7 @@ pub(super) fn account_models_data(
models: result
.models
.into_iter()
.map(|model| {
let id = model.id.to_string();
AccountModelView {
label: id.clone(),
id,
}
})
.map(|model| AccountModelView::from(model.id))
.collect(),
observed_at: observed_at.map(|value| time.rfc3339(&value)),
observed_at_display: observed_at
@@ -2,46 +2,22 @@
use super::*;
#[derive(Debug, Clone)]
pub struct AccountProxyUpdate(pub Option<gateway_core::account::OutboundProxy>);
impl<'de> Deserialize<'de> for AccountProxyUpdate {
fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
let value = String::deserialize(deserializer)?;
if value.is_empty() {
return Ok(Self(None));
}
gateway_core::account::OutboundProxy::parse(&value)
.map(|proxy| Self(Some(proxy)))
.map_err(serde::de::Error::custom)
}
}
pub(super) fn proxy_selection(
id: Option<String>,
url: Option<AccountProxyUpdate>,
) -> Result<Option<gateway_admin::model::proxies::AccountProxySelection>, WireValidationError> {
use gateway_admin::model::proxies::AccountProxySelection;
match (id, url) {
(Some(_), Some(_)) => Err(WireValidationError::new("outboundProxyId")),
(Some(id), None) if id.is_empty() => Ok(Some(AccountProxySelection::Direct)),
(Some(id), None) => {
if id.len() > 128
|| !id
.bytes()
.all(|byte| byte.is_ascii_alphanumeric() || matches!(byte, b'_' | b'-'))
{
return Err(WireValidationError::new("outboundProxyId"));
}
Ok(Some(AccountProxySelection::Saved(id)))
}
(None, Some(value)) => Ok(Some(
value
.0
.map_or(AccountProxySelection::Direct, AccountProxySelection::Url),
)),
(None, None) => Ok(None),
let Some(id) = id else { return Ok(None) };
if id.is_empty() {
return Ok(Some(AccountProxySelection::Direct));
}
if id.len() > 128
|| !id
.bytes()
.all(|byte| byte.is_ascii_alphanumeric() || matches!(byte, b'_' | b'-'))
{
return Err(WireValidationError::new("outboundProxyId"));
}
Ok(Some(AccountProxySelection::Saved(id)))
}
/// 账号列表查询参数
@@ -64,7 +40,6 @@ pub struct BatchUpdateAccountsRequest {
pub quota_settings: Option<gateway_core::account::AccountQuotaSettings>,
pub auto_use_quota_credits: Option<bool>,
pub outbound_proxy_id: Option<String>,
pub outbound_proxy_url: Option<AccountProxyUpdate>,
pub account_ids: Vec<String>,
pub enabled: Option<bool>,
#[serde(default, deserialize_with = "deserialize_optional_nullable_limit")]
@@ -127,7 +102,6 @@ impl BatchUpdateAccountsRequest {
&& self.group_ids.is_none()
&& self.model_access.is_none()
&& self.outbound_proxy_id.is_none()
&& self.outbound_proxy_url.is_none()
{
return Err(WireValidationError::new("accountSettings"));
}
@@ -139,7 +113,7 @@ impl BatchUpdateAccountsRequest {
Ok(BatchUpdateAccounts {
quota_settings: self.quota_settings,
auto_use_quota_credits: self.auto_use_quota_credits,
outbound_proxy: proxy_selection(self.outbound_proxy_id, self.outbound_proxy_url)?,
outbound_proxy: proxy_selection(self.outbound_proxy_id)?,
account_ids: self.account_ids,
enabled: self.enabled,
concurrency_limit: self
@@ -743,6 +717,16 @@ pub struct AccountModelView {
pub label: String,
}
impl From<UpstreamModelId> for AccountModelView {
fn from(model: UpstreamModelId) -> Self {
let id = model.to_string();
Self {
label: id.clone(),
id,
}
}
}
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct AccountModelsData {
@@ -0,0 +1,291 @@
//! 管理员查看单个密钥的模型统计与手动对齐本期额度的接口
use axum::{Router, extract::State, http::StatusCode, response::IntoResponse, routing::get};
use chrono::{DateTime, Utc};
use gateway_admin::model::observability::{KeyStatisticsOverview, KeyUsageMetric, TimeRange};
use gateway_admin::{
AdminServices,
model::client_keys::{
ClientKeyBudgetMutationOrigin, ClientKeyBudgetReference, ClientKeyBudgetWindowPeriod,
SetClientKeyBudgetWindow,
},
};
use gateway_core::{account::ProviderAccountId, policy::ClientApiKeyId};
use serde::{Deserialize, Serialize};
use super::{
AdminAuth, AdminEnvelope, AdminError, AdminJson, AdminQuery, AdminResponse,
client_keys::ClientBudgetView, wire::map_admin_service_error,
};
use crate::{auth::SessionState, time::TimePresenter};
pub(super) fn router<S: SessionState + Clone + Send + Sync + 'static>() -> Router<S> {
Router::new()
.route(
"/api/admin/client-keys/budget-reference-accounts",
get(accounts::<S>),
)
.route("/api/admin/client-keys/statistics", get(statistics::<S>))
.route("/api/admin/client-keys/statistics/models", get(models::<S>))
.route(
"/api/admin/client-keys/budget-alignment",
get(preview::<S>).post(align::<S>),
)
}
#[derive(Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
struct StatisticsQuery {
id: String,
period: Option<String>,
#[serde(default)]
metric: KeyUsageMetric,
as_of: Option<i64>,
}
fn key_id(value: String) -> Result<ClientApiKeyId, AdminError> {
ClientApiKeyId::new(value).map_err(|_| AdminError::bad_request("API Key ID 无效"))
}
#[derive(Serialize)]
struct Statistics {
budget: ClientBudgetView,
dates: Vec<chrono::NaiveDate>,
#[serde(rename = "asOf")]
as_of: i64,
#[serde(flatten)]
overview: KeyStatisticsOverview,
}
async fn statistics<S: SessionState>(
_auth: AdminAuth,
State(state): State<S>,
AdminQuery(query): AdminQuery<StatisticsQuery>,
) -> Result<impl IntoResponse, AdminError> {
let services = state.admin_services();
let id = key_id(query.id.clone())?;
let tz = services.timezone();
let range = statistics_range(&query, tz)?;
let budget = services
.client_keys()
.budget(&id)
.await
.map_err(map_admin_service_error)?;
let overview = services
.observability()
.client_key_statistics(range, &id, query.metric)
.await
.map_err(map_admin_service_error)?;
let end = tz.local(range.end).date_naive();
let dates = tz
.local(range.start)
.date_naive()
.iter_days()
.take_while(|date| *date <= end)
.collect();
Ok(AdminResponse::new(
StatusCode::OK,
AdminEnvelope::ok(Statistics {
budget: ClientBudgetView::from((budget, TimePresenter::new(tz))),
dates,
as_of: range.end.timestamp_millis(),
overview,
}),
))
}
fn statistics_range(
query: &StatisticsQuery,
tz: gateway_core::time::DeploymentTimeZone,
) -> Result<TimeRange, AdminError> {
let period = query.period.as_deref().unwrap_or("7d");
if !matches!(period, "7d" | "30d") {
return Err(AdminError::bad_request("统计周期须为 7 天或 30 天"));
}
crate::time::query_range(
None,
None,
Some(period),
Some(query.as_of.unwrap_or_else(|| Utc::now().timestamp_millis())),
tz,
"7d",
)
.map_err(|_| AdminError::bad_request("统计时间范围无效"))
}
async fn models<S: SessionState>(
_auth: AdminAuth,
State(state): State<S>,
AdminQuery(query): AdminQuery<StatisticsQuery>,
) -> Result<impl IntoResponse, AdminError> {
let services = state.admin_services();
let range = statistics_range(&query, services.timezone())?;
let id = key_id(query.id)?;
services
.client_keys()
.budget(&id)
.await
.map_err(map_admin_service_error)?;
let items = services
.observability()
.client_key_models(range, &id)
.await
.map_err(map_admin_service_error)?;
Ok(AdminResponse::new(StatusCode::OK, AdminEnvelope::ok(items)))
}
#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct AccountSearch {
id: String,
#[serde(default)]
search: String,
#[serde(default)]
cursor: String,
}
async fn accounts<S: SessionState>(
_auth: AdminAuth,
State(state): State<S>,
AdminQuery(query): AdminQuery<AccountSearch>,
) -> Result<impl IntoResponse, AdminError> {
let page = state
.admin_services()
.client_keys()
.budget_reference_accounts(&key_id(query.id)?, &query.search, &query.cursor)
.await
.map_err(map_admin_service_error)?;
Ok(AdminResponse::new(StatusCode::OK, AdminEnvelope::ok(page)))
}
#[derive(Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
struct ReferenceQuery {
id: String,
account_id: String,
}
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
struct ReferenceWindow {
key: String,
label: String,
period: &'static str,
starts_at: DateTime<Utc>,
ends_at: DateTime<Utc>,
starts_at_display: String,
ends_at_display: String,
}
#[derive(Serialize)]
struct Preview {
revision: u64,
windows: Vec<ReferenceWindow>,
}
async fn reference_windows(
services: &AdminServices,
id: &ClientApiKeyId,
account: String,
) -> Result<Vec<ClientKeyBudgetReference>, AdminError> {
let account_id =
ProviderAccountId::new(account).map_err(|_| AdminError::bad_request("参考账号 ID 无效"))?;
let key = services
.client_keys()
.get(id)
.await
.map_err(map_admin_service_error)?;
services
.accounts()
.key_budget_references(&account_id, &key.groups)
.await
.map_err(map_admin_service_error)
}
async fn preview<S: SessionState>(
_auth: AdminAuth,
State(state): State<S>,
AdminQuery(query): AdminQuery<ReferenceQuery>,
) -> Result<impl IntoResponse, AdminError> {
let services = state.admin_services();
let id = key_id(query.id)?;
let time = TimePresenter::new(services.timezone());
let windows = reference_windows(services, &id, query.account_id)
.await?
.into_iter()
.map(
|ClientKeyBudgetReference {
key,
label,
period,
bounds,
}| ReferenceWindow {
key,
label,
period: period.as_str(),
starts_at: bounds.starts_at,
ends_at: bounds.ends_at,
starts_at_display: time.datetime(&bounds.starts_at),
ends_at_display: time.datetime(&bounds.ends_at),
},
)
.collect();
// 日周窗口共享配置版本,普通请求的消费累计不会让预览失效
let revision = services
.client_keys()
.budget_window(&id, ClientKeyBudgetWindowPeriod::Weekly)
.await
.map_err(map_admin_service_error)?
.revision;
Ok(AdminResponse::new(
StatusCode::OK,
AdminEnvelope::ok(Preview { revision, windows }),
))
}
#[derive(Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
struct AlignRequest {
id: String,
account_id: String,
window_key: String,
starts_at: DateTime<Utc>,
ends_at: DateTime<Utc>,
revision: u64,
}
async fn align<S: SessionState>(
auth: AdminAuth,
State(state): State<S>,
AdminJson(input): AdminJson<AlignRequest>,
) -> Result<impl IntoResponse, AdminError> {
let services = state.admin_services();
let id = key_id(input.id)?;
let reference = reference_windows(services, &id, input.account_id)
.await?
.into_iter()
.find(|reference| {
reference.key == input.window_key
&& reference.bounds.starts_at == input.starts_at
&& reference.bounds.ends_at == input.ends_at
})
.ok_or_else(|| AdminError::conflict("参考窗口已变化,请重新预览"))?;
let revision = services
.client_keys()
.set_budget_window(
&auth.context().mutation_context(),
SetClientKeyBudgetWindow {
id,
period: reference.period,
expected_revision: input.revision,
window: Some(reference.bounds),
},
ClientKeyBudgetMutationOrigin::Admin,
)
.await
.map_err(map_admin_service_error)?;
Ok(AdminResponse::new(
StatusCode::OK,
AdminEnvelope::ok(revision),
))
}
@@ -7,11 +7,12 @@ use std::{collections::BTreeMap, fmt};
use base64::{Engine as _, engine::general_purpose::URL_SAFE_NO_PAD};
use chrono::{DateTime, Utc};
use gateway_admin::model::client_keys::{
ClientKeyBudgetPeriod, ClientKeyCursor, ClientKeyCursorValue as DomainCursorValue,
ClientKeyListQuery, ClientKeyMutation, ClientKeyPage, ClientKeyPageSize, ClientKeyRecord,
ClientKeySecret, ClientKeySort as DomainSort, ClientKeySortField as DomainSortField,
CreateClientKey, CreatedClientKey, DeleteClientKey, ResetClientKeyBudget, SetClientKeyEnabled,
SortDirection, UpdateClientKey,
BatchUpdateClientKeys, ClientKeyBudgetPeriod, ClientKeyCursor,
ClientKeyCursorValue as DomainCursorValue, ClientKeyListQuery, ClientKeyMutation,
ClientKeyPage, ClientKeyPageSize, ClientKeyRecord, ClientKeySecret,
ClientKeySort as DomainSort, ClientKeySortField as DomainSortField, CreateClientKey,
CreatedClientKey, DeleteClientKey, ResetClientKeyBudget, SetClientKeyEnabled, SortDirection,
UpdateClientKey,
};
use gateway_core::{
engine::budget::ClientBudgetLimits,
@@ -156,8 +157,6 @@ pub struct CreateClientKeyRequest {
peer_sharing: gateway_core::policy::ClientPeerSharing,
#[serde(default)]
provider_request_profile_overrides: ProviderRequestProfileOverrides,
openai_client_profile_override: Option<serde_json::Map<String, serde_json::Value>>,
xai_client_profile_override: Option<serde_json::Map<String, serde_json::Value>>,
custom_key: Option<String>,
name: String,
label: Option<String>,
@@ -186,11 +185,8 @@ impl CreateClientKeyRequest {
let group_ids = validate_group_ids(self.group_ids)?;
validate_limit(self.max_concurrency, "maxConcurrency")?;
validate_limit(self.requests_per_minute, "requestsPerMinute")?;
let request_profile_overrides = normalize_request_profile_overrides(
self.provider_request_profile_overrides,
self.openai_client_profile_override,
self.xai_client_profile_override,
)?;
let request_profile_overrides =
normalize_request_profile_overrides(self.provider_request_profile_overrides)?;
Ok(CreateClientKey {
peer_sharing: self.peer_sharing,
request_profile_overrides,
@@ -223,10 +219,6 @@ pub struct UpdateClientKeyRequest {
peer_sharing: Option<gateway_core::policy::ClientPeerSharing>,
#[serde(default)]
provider_request_profile_overrides: ProviderRequestProfileOverrideUpdates,
#[serde(default, deserialize_with = "deserialize_profile_override")]
openai_client_profile_override: Option<Option<serde_json::Map<String, serde_json::Value>>>,
#[serde(default, deserialize_with = "deserialize_profile_override")]
xai_client_profile_override: Option<Option<serde_json::Map<String, serde_json::Value>>>,
id: String,
name: String,
label: Option<String>,
@@ -246,11 +238,8 @@ impl UpdateClientKeyRequest {
let group_ids = validate_group_ids(self.group_ids)?;
validate_limit(self.max_concurrency, "maxConcurrency")?;
validate_limit(self.requests_per_minute, "requestsPerMinute")?;
let request_profile_override_updates = normalize_request_profile_override_updates(
self.provider_request_profile_overrides,
self.openai_client_profile_override,
self.xai_client_profile_override,
)?;
let request_profile_override_updates =
normalize_request_profile_override_updates(self.provider_request_profile_overrides)?;
Ok(UpdateClientKey {
peer_sharing: self.peer_sharing,
request_profile_override_updates,
@@ -335,16 +324,54 @@ impl ClientKeyMutationRequest {
}
}
#[derive(Debug, Clone, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub(super) struct ClientBudgetView {
daily_limit_usd: String,
weekly_limit_usd: String,
daily_used_usd: String,
weekly_used_usd: String,
daily_resets_at: Option<DateTime<Utc>>,
daily_resets_at_display: Option<String>,
weekly_resets_at: Option<DateTime<Utc>>,
weekly_resets_at_display: Option<String>,
}
impl
From<(
gateway_core::engine::budget::ClientBudgetStatus,
crate::time::TimePresenter,
)> for ClientBudgetView
{
fn from(
(budget, time): (
gateway_core::engine::budget::ClientBudgetStatus,
crate::time::TimePresenter,
),
) -> Self {
Self {
daily_limit_usd: budget.limits.daily_usd.canonical(),
weekly_limit_usd: budget.limits.weekly_usd.canonical(),
daily_used_usd: budget.daily_used_usd.canonical(),
weekly_used_usd: budget.weekly_used_usd.canonical(),
daily_resets_at_display: budget
.daily_resets_at()
.map(|value| time.datetime(&value.into())),
daily_resets_at: budget.daily_resets_at().map(DateTime::from),
weekly_resets_at_display: budget
.weekly_resets_at()
.map(|value| time.datetime(&value.into())),
weekly_resets_at: budget.weekly_resets_at().map(DateTime::from),
}
}
}
/// 不含完整 Key 的管理端安全视图
#[derive(Debug, Clone, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ClientKeyView {
peer_sharing: gateway_core::policy::ClientPeerSharing,
provider_request_profile_overrides: ProviderRequestProfileOverrides,
/// 固定兼容字段;值始终从 provider_request_profile_overrides 派生
openai_client_profile_override: Option<serde_json::Map<String, serde_json::Value>>,
/// 固定兼容字段;值始终从 provider_request_profile_overrides 派生
xai_client_profile_override: Option<serde_json::Map<String, serde_json::Value>>,
id: String,
name: String,
label: Option<String>,
@@ -355,14 +382,8 @@ pub struct ClientKeyView {
enabled: bool,
max_concurrency: u64,
requests_per_minute: u64,
daily_limit_usd: String,
weekly_limit_usd: String,
daily_used_usd: String,
weekly_used_usd: String,
daily_resets_at: Option<DateTime<Utc>>,
daily_resets_at_display: Option<String>,
weekly_resets_at: Option<DateTime<Utc>>,
weekly_resets_at_display: Option<String>,
#[serde(flatten)]
budget: ClientBudgetView,
created_at: DateTime<Utc>,
created_at_display: String,
updated_at: DateTime<Utc>,
@@ -385,10 +406,6 @@ impl From<(ClientKeyRecord, crate::time::TimePresenter)> for ClientKeyView {
.map(|(provider, profile)| (provider.as_str().to_owned(), profile.into_inner()))
.collect::<ProviderRequestProfileOverrides>();
Self {
openai_client_profile_override: provider_request_profile_overrides
.get("openai")
.cloned(),
xai_client_profile_override: provider_request_profile_overrides.get("xai").cloned(),
provider_request_profile_overrides,
peer_sharing: record.peer_sharing,
id: record.id.to_string(),
@@ -409,20 +426,7 @@ impl From<(ClientKeyRecord, crate::time::TimePresenter)> for ClientKeyView {
enabled: record.enabled,
max_concurrency: record.limits.max_concurrency,
requests_per_minute: record.limits.requests_per_minute,
daily_limit_usd: record.budget.limits.daily_usd.canonical(),
weekly_limit_usd: record.budget.limits.weekly_usd.canonical(),
daily_used_usd: record.budget.daily_used_usd.canonical(),
weekly_used_usd: record.budget.weekly_used_usd.canonical(),
daily_resets_at_display: record
.budget
.daily_resets_at()
.map(|value| time.datetime(&value.into())),
daily_resets_at: record.budget.daily_resets_at().map(DateTime::from),
weekly_resets_at_display: record
.budget
.weekly_resets_at()
.map(|value| time.datetime(&value.into())),
weekly_resets_at: record.budget.weekly_resets_at().map(DateTime::from),
budget: ClientBudgetView::from((record.budget, time)),
created_at_display: time.datetime(&record.created_at),
created_at: record.created_at,
updated_at_display: time.datetime(&record.updated_at),
@@ -739,11 +743,9 @@ fn validate_limit(value: u64, field: &'static str) -> Result<(), WireValidationE
fn normalize_request_profile_overrides(
profiles: ProviderRequestProfileOverrides,
openai: Option<serde_json::Map<String, serde_json::Value>>,
xai: Option<serde_json::Map<String, serde_json::Value>>,
) -> Result<gateway_admin::model::client_keys::ProviderRequestProfileOverrides, WireValidationError>
{
let mut normalized = profiles
profiles
.into_iter()
.map(|(provider, profile)| {
if !matches!(provider.as_str(), "openai" | "xai") {
@@ -756,35 +758,16 @@ fn normalize_request_profile_overrides(
gateway_core::account::OpaqueProviderData::new(profile),
))
})
.collect::<Result<gateway_admin::model::client_keys::ProviderRequestProfileOverrides, _>>(
)?;
for (provider, profile) in [("openai", openai), ("xai", xai)] {
let Some(profile) = profile else {
continue;
};
validate_request_profile_size(&profile)?;
let provider = ProviderKind::new(provider).expect("static Provider kind is valid");
let profile = gateway_core::account::OpaqueProviderData::new(profile);
if normalized
.get(&provider)
.is_some_and(|current| current != &profile)
{
return Err(WireValidationError::new("providerRequestProfileOverrides"));
}
normalized.insert(provider, profile);
}
Ok(normalized)
.collect()
}
fn normalize_request_profile_override_updates(
profiles: ProviderRequestProfileOverrideUpdates,
openai: Option<Option<serde_json::Map<String, serde_json::Value>>>,
xai: Option<Option<serde_json::Map<String, serde_json::Value>>>,
) -> Result<
gateway_admin::model::client_keys::ProviderRequestProfileOverrideUpdates,
WireValidationError,
> {
let mut normalized = profiles
profiles
.into_iter()
.map(|(provider, profile)| {
if !matches!(provider.as_str(), "openai" | "xai") {
@@ -794,34 +777,12 @@ fn normalize_request_profile_override_updates(
validate_request_profile_size(profile)?;
}
Ok((
ProviderKind::new(provider).map_err(|_| {
WireValidationError::new("providerRequestProfileOverrides")
})?,
ProviderKind::new(provider)
.map_err(|_| WireValidationError::new("providerRequestProfileOverrides"))?,
profile.map(gateway_core::account::OpaqueProviderData::new),
))
})
.collect::<Result<
gateway_admin::model::client_keys::ProviderRequestProfileOverrideUpdates,
_,
>>()?;
for (provider, profile) in [("openai", openai), ("xai", xai)] {
let Some(profile) = profile else {
continue;
};
if let Some(profile) = profile.as_ref() {
validate_request_profile_size(profile)?;
}
let provider = ProviderKind::new(provider).expect("static Provider kind is valid");
let profile = profile.map(gateway_core::account::OpaqueProviderData::new);
if normalized
.get(&provider)
.is_some_and(|current| current != &profile)
{
return Err(WireValidationError::new("providerRequestProfileOverrides"));
}
normalized.insert(provider, profile);
}
Ok(normalized)
.collect()
}
fn validate_request_profile_size(
@@ -866,7 +827,12 @@ where
S: SessionState + Clone + Send + Sync + 'static,
{
Router::new()
.merge(super::client_key_usage::router::<S>())
.route("/api/admin/client-keys", get(list_client_keys::<S>))
.route(
"/api/admin/client-keys/batch-update",
post(batch_update_client_keys::<S>),
)
.route(
"/api/admin/client-keys/create",
post(create_client_key::<S>),
@@ -913,6 +879,94 @@ where
Ok(AdminResponse::new(StatusCode::OK, AdminEnvelope::ok(data)))
}
#[derive(Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct BatchUpdateClientKeysRequest {
ids: Vec<String>,
group_ids: Option<Vec<String>>,
enabled: Option<bool>,
max_concurrency: Option<u64>,
requests_per_minute: Option<u64>,
daily_limit_usd: Option<String>,
weekly_limit_usd: Option<String>,
peer_sharing: Option<gateway_admin::model::client_keys::ClientPeerSharingPatch>,
#[serde(default)]
provider_request_profile_overrides: ProviderRequestProfileOverrideUpdates,
installation_seed: Option<String>,
}
impl BatchUpdateClientKeysRequest {
/// 复用单把密钥的字段校验,缺省和显式零值分别保留
pub fn into_command(self) -> Result<BatchUpdateClientKeys, WireValidationError> {
if let Some(limit) = self.max_concurrency {
validate_limit(limit, "maxConcurrency")?;
}
if let Some(limit) = self.requests_per_minute {
validate_limit(limit, "requestsPerMinute")?;
}
let installation_seed = self
.installation_seed
.map(|value| {
if value.len() != uuid::fmt::Hyphenated::LENGTH {
return Err(WireValidationError::new("installationSeed"));
}
uuid::Uuid::try_parse(&value)
.map_err(|_| WireValidationError::new("installationSeed"))
})
.transpose()?;
Ok(BatchUpdateClientKeys {
ids: self
.ids
.into_iter()
.map(|id| client_key_id(id, "id"))
.collect::<Result<_, _>>()?,
group_ids: self.group_ids.map(validate_group_ids).transpose()?,
enabled: self.enabled,
max_concurrency: self.max_concurrency,
requests_per_minute: self.requests_per_minute,
daily_limit_usd: parse_budget(self.daily_limit_usd, "dailyLimitUsd")?,
weekly_limit_usd: parse_budget(self.weekly_limit_usd, "weeklyLimitUsd")?,
peer_sharing: self.peer_sharing.unwrap_or_default(),
request_profile_override_updates: normalize_request_profile_override_updates(
self.provider_request_profile_overrides,
)?,
installation_seed,
})
}
}
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
struct BatchUpdatedClientKeysData {
config_revision: gateway_admin::model::Revision,
updated: usize,
}
async fn batch_update_client_keys<S>(
auth: AdminAuth,
State(state): State<S>,
AdminJson(request): AdminJson<BatchUpdateClientKeysRequest>,
) -> Result<impl IntoResponse, AdminError>
where
S: SessionState + Send + Sync,
{
let command = request.into_command().map_err(map_wire_error)?;
let updated = command.ids.len();
let config_revision = state
.admin_services()
.client_keys()
.batch_update(&auth.context().mutation_context(), command)
.await
.map_err(map_service_error)?;
Ok(AdminResponse::new(
StatusCode::OK,
AdminEnvelope::ok(BatchUpdatedClientKeysData {
config_revision,
updated,
}),
))
}
async fn create_client_key<S>(
auth: AdminAuth,
State(state): State<S>,
@@ -1087,7 +1141,8 @@ fn map_wire_error(error: WireValidationError) -> AdminError {
"requestsPerMinute" => "每分钟请求上限超出支持范围",
"dailyLimitUsd" => "每日美元限额须非负,整数和小数各最多 10 位",
"weeklyLimitUsd" => "每周美元限额须非负,整数和小数各最多 10 位",
"providerRequestProfileOverrides" => "API Key 客户端身份配置无效或字段冲突",
"providerRequestProfileOverrides" => "API Key 客户端身份配置无效",
"installationSeed" => "安装身份种子必须是带连字符的 UUID",
"limit" => "每页数量须为 1 至 65535",
"search" => "搜索内容过长或包含控制字符",
"sortBy" => "排序字段不受支持",
@@ -1100,10 +1155,3 @@ fn map_wire_error(error: WireValidationError) -> AdminError {
fn map_service_error(error: gateway_admin::model::AdminError) -> AdminError {
map_admin_service_error(error)
}
// 省略表示不修改,null 表示恢复跟随通用设置
fn deserialize_profile_override<'de, D: serde::Deserializer<'de>>(
deserializer: D,
) -> Result<Option<Option<serde_json::Map<String, serde_json::Value>>>, D::Error> {
Option::<serde_json::Map<String, serde_json::Value>>::deserialize(deserializer).map(Some)
}
@@ -15,6 +15,7 @@ pub mod account_groups;
pub mod accounts;
pub mod auth;
pub mod backups;
mod client_key_usage;
pub mod client_keys;
mod compression;
pub(crate) mod diagnostics;
@@ -925,25 +925,9 @@ pub(crate) fn dashboard_view(
.filter(|window| window.used_percent.is_none())
.and_then(|window| window.local_usage.as_ref())
.map(|usage| usage.request_count);
let (metric, metric_value_raw, metric_label, metric_value) = match local_requests {
Some(count) => (
DashboardAccountMetric::Requests,
Some(count),
"次数".to_owned(),
if count > 0 {
format_compact_number(count)
} else {
"—".to_owned()
},
),
None => (
DashboardAccountMetric::Tokens,
credential.total_tokens,
"今日 Token".to_owned(),
credential
.total_tokens
.map_or_else(|| "—".to_owned(), format_compact_number),
),
let (metric, metric_value_raw) = match local_requests {
Some(count) => (DashboardAccountMetric::Requests, Some(count)),
None => (DashboardAccountMetric::Tokens, credential.total_tokens),
};
account_usage_views.push(DashboardAccountUsageView {
upstream: credential.upstream.map(|info| {
@@ -978,13 +962,10 @@ pub(crate) fn dashboard_view(
.map(|window| crate::admin::accounts::quota_window_view(window, time)),
metric,
metric_value_raw,
metric_label,
metric_value,
last_used_at: credential.last_used_at,
last_used_relative: credential
.last_used_at
.map(|value| time.relative_view(value, range.end)),
last_used: time.relative_optional(credential.last_used_at, range.end),
});
}
let unavailable_accounts = provider_accounts
@@ -447,11 +447,8 @@ pub struct DashboardAccountUsageView {
pub usage_window: Option<crate::admin::accounts::AccountQuotaWindowView>,
pub metric: DashboardAccountMetric,
pub metric_value_raw: Option<u64>,
pub metric_label: String,
pub metric_value: String,
pub last_used_at: Option<DateTime<Utc>>,
pub last_used_relative: Option<crate::time::RelativeTimeView>,
pub last_used: String,
}
/// Provider 账号池的持久事实汇总
@@ -20,9 +20,24 @@ use serde::{Deserialize, Serialize};
use super::{
AdminAuth, AdminEnvelope, AdminError, AdminJson, AdminQuery, AdminResponse, PageMeta,
accounts::{AccountGroupRefView, AccountProxyUpdate},
accounts::AccountGroupRefView,
};
#[derive(Debug)]
struct ProxyUrl(Option<gateway_core::account::OutboundProxy>);
impl<'de> Deserialize<'de> for ProxyUrl {
fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
let value = String::deserialize(deserializer)?;
if value.is_empty() {
return Ok(Self(None));
}
gateway_core::account::OutboundProxy::parse(&value)
.map(|proxy| Self(Some(proxy)))
.map_err(serde::de::Error::custom)
}
}
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
struct ListQuery {
@@ -56,7 +71,7 @@ struct CreateRequest {
auto_location: bool,
location: Option<gateway_core::account::RequestLocation>,
name: String,
proxy_url: AccountProxyUpdate,
proxy_url: ProxyUrl,
}
#[derive(Debug, Deserialize)]
@@ -70,7 +85,7 @@ struct UpdateRequest {
id: String,
revision: u64,
name: String,
proxy_url: Option<AccountProxyUpdate>,
proxy_url: Option<ProxyUrl>,
}
fn deserialize_location_update<'de, D>(
@@ -105,7 +120,7 @@ struct ProbeRequest {
use_deployment_proxy: bool,
#[serde(default)]
detect_location: bool,
proxy_url: AccountProxyUpdate,
proxy_url: ProxyUrl,
}
#[derive(Serialize)]
@@ -1,4 +1,4 @@
//! Runtime settings、旧设置页聚合投影与明文 Admin API Key wire
//! 管理端运行设置、模型定价与 Admin API Key 的 HTTP 接口
use crate::auth::SessionState;
@@ -36,6 +36,8 @@ pub type ProviderRequestProfileUpdates =
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct RuntimeSettingsFields {
pub openai_oauth_guardian_review_model: bool,
pub openai_request_country: gateway_core::account::OpenAiRequestCountry,
pub codex_privacy_policy: gateway_core::settings::privacy::CodexPrivacyPolicy,
pub request_location_enabled: bool,
pub request_location: gateway_core::account::RequestLocation,
@@ -64,7 +66,6 @@ pub struct RuntimeSettingsFields {
pub account_warmup_enabled: bool,
pub account_warmup_cron: String,
pub account_warmup_model: Option<String>,
#[serde(default)]
pub reset_credit_on_low_weekly_quota: bool,
pub reset_credit_lead_minutes: u32,
pub reset_credit_max_weekly_remaining_percent: Option<u32>,
@@ -80,10 +81,6 @@ pub struct RuntimeSettingsView {
pub smart_scheduling_defaults: gateway_core::account::SmartSchedulingConfig,
pub quota_reset_window_weights_defaults: gateway_core::account::ResetWindowWeights,
pub provider_request_profiles: ProviderRequestProfiles,
/// 固定兼容字段;值始终从 provider_request_profiles 派生
pub openai_client_profile: Option<serde_json::Map<String, serde_json::Value>>,
/// 固定兼容字段;值始终从 provider_request_profiles 派生
pub xai_client_profile: Option<serde_json::Map<String, serde_json::Value>>,
pub model_mappings: ModelMappings,
pub rotation_strategy: String,
pub updated_at: DateTime<Utc>,
@@ -99,12 +96,6 @@ pub struct UpdateRuntimeSettingsRequest {
pub config_revision: u64,
#[serde(default)]
pub provider_request_profiles: ProviderRequestProfileUpdates,
/// 兼容既有 wire;与泛化字段冲突时拒绝整个请求
#[serde(default, deserialize_with = "deserialize_profile_update")]
pub openai_client_profile: Option<serde_json::Map<String, serde_json::Value>>,
/// 兼容既有 wire;与泛化字段冲突时拒绝整个请求
#[serde(default, deserialize_with = "deserialize_profile_update")]
pub xai_client_profile: Option<serde_json::Map<String, serde_json::Value>>,
pub model_mappings: ModelMappings,
pub rotation_strategy: String,
}
@@ -121,11 +112,8 @@ impl UpdateRuntimeSettingsRequest {
fn into_command(self) -> Result<ReplaceRuntimeSettings, WireValidationError> {
validate_model_mappings(&self.model_mappings)?;
let request_profile_updates = normalize_request_profile_updates(
self.provider_request_profiles,
self.openai_client_profile,
self.xai_client_profile,
)?;
let request_profile_updates =
normalize_request_profile_updates(self.provider_request_profiles)?;
Ok(ReplaceRuntimeSettings {
expected_revision: gateway_admin::model::Revision::new(self.config_revision)
.map_err(|_| WireValidationError::new("configRevision"))?,
@@ -147,8 +135,6 @@ impl From<(RuntimeSettings, crate::time::TimePresenter)> for RuntimeSettingsView
.collect::<ProviderRequestProfiles>();
Self {
config_revision: settings.config_revision.get(),
openai_client_profile: provider_request_profiles.get("openai").cloned(),
xai_client_profile: provider_request_profiles.get("xai").cloned(),
provider_request_profiles,
model_mappings: wire_model_mappings(settings.model_mappings),
smart_scheduling_defaults: gateway_core::account::SmartSchedulingConfig::default(),
@@ -595,8 +581,9 @@ 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(),
"providerRequestProfiles" => "服务商客户端身份配置无效".to_owned(),
"minCodexDesktopVersion" => "Codex Desktop 最低版本格式不合法".to_owned(),
"minCodexCliVersion" => "Codex CLI 最低版本格式不合法".to_owned(),
field => format!("{field} 字段不合法"),
@@ -610,10 +597,8 @@ fn map_service_error(error: gateway_admin::model::AdminError) -> AdminError {
fn normalize_request_profile_updates(
profiles: ProviderRequestProfileUpdates,
openai: Option<serde_json::Map<String, serde_json::Value>>,
xai: Option<serde_json::Map<String, serde_json::Value>>,
) -> Result<gateway_admin::model::settings::ProviderRequestProfileUpdates, WireValidationError> {
let mut normalized = profiles
profiles
.into_iter()
.map(|(provider, profile)| {
if !matches!(provider.as_str(), "openai" | "xai") {
@@ -626,30 +611,7 @@ fn normalize_request_profile_updates(
profile.map(gateway_core::account::OpaqueProviderData::new),
))
})
.collect::<Result<gateway_admin::model::settings::ProviderRequestProfileUpdates, _>>()?;
for (provider, profile) in [("openai", openai), ("xai", xai)] {
let Some(profile) = profile else {
continue;
};
let provider = gateway_core::routing::ProviderKind::new(provider)
.expect("static Provider kind is valid");
let profile = gateway_core::account::OpaqueProviderData::new(profile);
if normalized
.get(&provider)
.is_some_and(|current| current.as_ref() != Some(&profile))
{
return Err(WireValidationError::new("providerRequestProfiles"));
}
normalized.insert(provider, Some(profile));
}
Ok(normalized)
}
// 字段省略时保留已有配置;显式 null 不能清空唯一的通用默认
fn deserialize_profile_update<'de, D: serde::Deserializer<'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)
.collect()
}
async fn detect_location<S>(
@@ -746,6 +708,8 @@ impl RuntimeSettingsFields {
self,
) -> Result<gateway_admin::model::settings::RuntimeSettingsValues, WireValidationError> {
let values = gateway_admin::model::settings::RuntimeSettingsValues {
openai_oauth_guardian_review_model: self.openai_oauth_guardian_review_model,
openai_request_country: self.openai_request_country,
codex_privacy_policy: self.codex_privacy_policy,
request_location_enabled: self.request_location_enabled,
request_location: self
@@ -795,6 +759,8 @@ impl RuntimeSettingsFields {
impl From<gateway_admin::model::settings::RuntimeSettingsValues> for RuntimeSettingsFields {
fn from(settings: gateway_admin::model::settings::RuntimeSettingsValues) -> Self {
crate::admin::settings::RuntimeSettingsFields {
openai_oauth_guardian_review_model: settings.openai_oauth_guardian_review_model,
openai_request_country: settings.openai_request_country,
codex_privacy_policy: settings.codex_privacy_policy,
request_location_enabled: settings.request_location_enabled,
request_location: settings.request_location,
@@ -848,6 +814,7 @@ fn settings_validation_error(field: &'static str) -> WireValidationError {
"max_waiting_per_key" => "maxWaitingPerKey",
"min_codex_cli_version" => "minCodexCliVersion",
"min_codex_desktop_version" => "minCodexDesktopVersion",
"openai_request_country" => "openaiRequestCountry",
"openai_account_affinity" => "openaiAccountAffinity",
"openai_guardian_reserved_concurrency" => "openaiGuardianReservedConcurrency",
"openai_session_affinity_ttl_hours" => "openaiSessionAffinityTtlHours",
@@ -115,9 +115,14 @@ async fn models_response(
}
})
.collect::<Result<Vec<_>, ()>>();
let Ok(models) = models else {
let Ok(mut models) = models else {
return catalog_unavailable_response();
};
if !client.policy().account_scope().allows_subagents() {
for model in &mut models {
model["multi_agent_version"] = json!("disabled");
}
}
return (
StatusCode::OK,
// 目录经过账号选择/别名/多 Provider 聚合,不能借用上游 ETag 表示改写后的正文
@@ -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);
}
@@ -120,6 +172,7 @@ impl OpenAiRequestHeaders {
let mut context = Map::new();
insert_protocol_context(&mut context, "turn_state", self.turn_state.as_ref());
insert_protocol_context(&mut context, "turn_metadata", self.turn_metadata.as_ref());
insert_protocol_context(&mut context, "subagent", self.subagent.as_ref());
if matches!(source, RequestDecodeSource::WebSocketFrame) {
context.insert(
gateway_protocol::openai::WEBSOCKET_FRAME_CONTEXT_KEY.to_owned(),
@@ -328,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();
@@ -78,6 +78,58 @@ async fn options_route_should_return_only_lightweight_group_fields() {
assert_eq!(value["data"]["configRevision"], 7);
}
#[tokio::test]
async fn models_route_reads_the_published_group_catalog() {
use gateway_core::{
account::ProviderAccountId,
routing::{
ConfigRevision, ModelCapabilities, ProviderKind, ProviderModel, RuntimeAccount,
RuntimeAccountDirectory, RuntimeSnapshot, UpstreamModelId,
},
};
use std::{
collections::{BTreeMap, BTreeSet},
sync::Arc,
};
let fixture = authenticated_fixture().await;
let uri = format!("/api/admin/account-groups/models?id={PRIMARY_GROUP_ID}");
let unavailable = request(router(&fixture), Method::GET, &uri, None, true).await;
assert_eq!(unavailable.status(), StatusCode::SERVICE_UNAVAILABLE);
let provider = ProviderKind::new("openai").unwrap();
let snapshot = RuntimeSnapshot::new(
ConfigRevision::new(7).unwrap(),
gateway_core::settings::SettingsValues::new(1, "smart", Default::default(), None, None),
vec![provider.clone()],
vec![ProviderModel::new(
provider.clone(),
UpstreamModelId::new("model-a").unwrap(),
ModelCapabilities::new(
BTreeSet::from([gateway_core::operation::OperationKind::Generate]),
None,
),
)],
vec![],
)
.unwrap()
.with_account_directory(Arc::new(RuntimeAccountDirectory::new(BTreeMap::from([(
ProviderAccountId::new("acct_member").unwrap(),
RuntimeAccount::new(
provider,
BTreeSet::from([super::group_id(PRIMARY_GROUP_ID)]),
),
)]))));
fixture.published_snapshot.publish(snapshot);
let response = request(router(&fixture), Method::GET, &uri, None, true).await;
assert_eq!(response.status(), StatusCode::OK);
assert_eq!(
response_json(response).await["data"],
json!({"models":[{"id":"model-a", "label":"model-a"}]})
);
let unauthorized = request(router(&fixture), Method::GET, &uri, None, false).await;
assert_eq!(unauthorized.status(), StatusCode::UNAUTHORIZED);
}
#[tokio::test]
async fn create_route_should_create_an_empty_group() {
let fixture = authenticated_fixture().await;
@@ -106,6 +158,11 @@ async fn create_route_should_create_an_empty_group() {
assert_eq!(value["data"]["record"]["color"], "#A855F780");
assert_eq!(value["data"]["record"]["fastMode"], "default");
assert_eq!(value["data"]["record"]["allowImageGeneration"], true);
assert_eq!(value["data"]["record"]["allowSubagents"], true);
assert_eq!(
value["data"]["record"]["modelAccess"],
json!({"mode":"all","models":[]})
);
assert_eq!(value["data"]["configRevision"], 8);
}
@@ -189,6 +246,8 @@ async fn delete_route_should_return_deleted_id_without_a_live_record() {
async fn list_route_should_reject_unknown_and_invalid_pagination_fields() {
let fixture = authenticated_fixture().await;
for uri in [
"/api/admin/account-groups/models",
"/api/admin/account-groups/models?id=bad",
"/api/admin/account-groups?other=true",
"/api/admin/account-groups?page=0",
"/api/admin/account-groups?pageSize=0",
@@ -345,6 +404,8 @@ async fn fast_mode_group_updates_preserve_omitted_and_null_values() {
for (value, expected) in [
(Some(json!("disabled")), "disabled"),
(None, "disabled"),
(Some(json!("ultrafast")), "ultrafast"),
(None, "ultrafast"),
(Some(json!("enabled")), "enabled"),
(None, "enabled"),
(Some(Value::Null), "enabled"),
@@ -376,6 +437,7 @@ async fn fast_mode_rejects_invalid_values_and_creates_groups_in_each_mode() {
for (mode, expected) in [
(json!("enabled"), StatusCode::CREATED),
(json!("disabled"), StatusCode::CREATED),
(json!("ultrafast"), StatusCode::CREATED),
(json!("default"), StatusCode::CREATED),
(json!("force"), StatusCode::UNPROCESSABLE_ENTITY),
(json!(true), StatusCode::BAD_REQUEST),
@@ -430,3 +492,53 @@ async fn image_generation_flag_round_trips_and_omitted_updates_preserve_it() {
);
}
}
#[tokio::test]
async fn subagent_and_model_policies_round_trip_independently() {
let fixture = authenticated_fixture().await;
let policy = json!({"mode":"allowlist", "models":["model-a"]});
let created = request(
router(&fixture),
Method::POST,
"/api/admin/account-groups/create",
Some(json!({
"name":"Model policy", "color":"#2563EBFF", "modelAccess":policy, "allowSubagents":false
})),
true,
)
.await;
assert_eq!(created.status(), StatusCode::CREATED);
let created = response_json(created).await;
assert_eq!(created["data"]["record"]["modelAccess"], policy);
assert_eq!(created["data"]["record"]["allowSubagents"], false);
let mut update = json!({"id":created["data"]["id"], "name":"Model policy", "color":"#2563EBFF", "allowSubagents":true});
let send = |body| {
request(
router(&fixture),
Method::POST,
"/api/admin/account-groups/update",
Some(body),
true,
)
};
let changed = send(update.clone()).await;
assert_eq!(changed.status(), StatusCode::OK);
let changed = response_json(changed).await;
assert_eq!(changed["data"]["record"]["modelAccess"], policy);
assert_eq!(changed["data"]["record"]["allowSubagents"], true);
update.as_object_mut().unwrap().remove("allowSubagents");
update["modelAccess"] = json!({"mode":"denylist","models":["model-b"]});
let changed = send(update.clone()).await;
assert_eq!(changed.status(), StatusCode::OK);
let changed = response_json(changed).await;
assert_eq!(
changed["data"]["record"]["modelAccess"],
update["modelAccess"]
);
assert_eq!(changed["data"]["record"]["allowSubagents"], true);
update["modelAccess"] = json!({"mode":"allowlist","models":[]});
assert_eq!(
send(update).await.status(),
StatusCode::UNPROCESSABLE_ENTITY
);
}
@@ -12,6 +12,63 @@ use serde_json::json;
use super::{AdminTestFixture, AdminTestState};
#[test]
fn batch_key_form_preserves_omissions_and_accepts_explicit_zero_false_and_inheritance() {
use gateway_api::admin::client_keys::BatchUpdateClientKeysRequest;
use gateway_core::routing::ProviderKind;
let parse = |value| {
serde_json::from_value::<BatchUpdateClientKeysRequest>(value)
.unwrap()
.into_command()
};
let patch = parse(json!({
"ids":["key_a","key_b"], "enabled":false,
"maxConcurrency":0, "dailyLimitUsd":"0", "weeklyLimitUsd":"1.75",
"peerSharing":{"shareQuota":false}, "providerRequestProfileOverrides":{"xai":null}
}))
.unwrap();
assert_eq!(patch.enabled, Some(false));
assert_eq!(patch.max_concurrency, Some(0));
assert_eq!(patch.daily_limit_usd.unwrap().canonical(), "0");
assert_eq!(patch.weekly_limit_usd.unwrap().canonical(), "1.75");
assert_eq!(patch.peer_sharing.share_quota, Some(false));
assert_eq!(patch.peer_sharing.enabled, None);
assert_eq!(patch.requests_per_minute, None);
assert_eq!(patch.group_ids, None);
assert_eq!(patch.installation_seed, None);
assert_eq!(
patch
.request_profile_override_updates
.get(&ProviderKind::new("xai").unwrap()),
Some(&None)
);
for (field, value) in [
("dailyLimitUsd", json!("-0.01")),
("weeklyLimitUsd", json!("1.00000000001")),
("maxConcurrency", json!(u64::MAX)),
("requestsPerMinute", json!(u64::MAX)),
(
"installationSeed",
json!("A3A4E0BDF84447068DF8BFF10DE95B3B"),
),
("providerRequestProfileOverrides", json!({"unknown": null})),
] {
let mut payload = json!({ "ids":["key_a"] });
payload[field] = value;
assert_eq!(parse(payload).unwrap_err().field(), field);
}
let seed = "A3A4E0BD-F844-4706-8DF8-BFF10DE95B3B";
assert_eq!(
parse(json!({"ids":["key_a"], "installationSeed":seed}))
.unwrap()
.installation_seed
.unwrap()
.to_string(),
seed.to_lowercase()
);
}
#[test]
fn reset_budget_requires_an_explicit_supported_period_and_valid_key() {
use gateway_api::admin::client_keys::ResetClientKeyBudgetRequest;
@@ -572,45 +629,26 @@ async fn list_route_should_accept_the_full_nonzero_u16_page_size() {
#[test]
fn profile_override_distinguishes_omission_from_explicit_inheritance() {
let mut payload = json!({"id":"key_profile", "name":"profile", "groupIds":[], "maxConcurrency":0, "requestsPerMinute":0});
let decode = |body| {
let mut command = serde_json::from_value::<UpdateClientKeyRequest>(body)
.unwrap()
.into_command()
.unwrap();
command
.request_profile_override_updates
.remove(&gateway_core::routing::ProviderKind::new("openai").expect("Provider kind"))
};
assert_eq!(decode(payload.clone()), None);
payload["openaiClientProfileOverride"] = json!(null);
assert_eq!(decode(payload.clone()), Some(None));
payload["openaiClientProfileOverride"] =
json!({"client":"cli", "platform":"linux", "versionMode":"latest"});
assert!(decode(payload).unwrap().is_some());
for provider in ["openai", "xai"] {
let decode = |body| {
serde_json::from_value::<UpdateClientKeyRequest>(body)
.unwrap()
.into_command()
.unwrap()
.request_profile_override_updates
.remove(&gateway_core::routing::ProviderKind::new(provider).unwrap())
};
let mut payload = json!({"id":"key_profile", "name":"profile", "groupIds":[], "maxConcurrency":0, "requestsPerMinute":0});
assert_eq!(decode(payload.clone()), None);
payload["providerRequestProfileOverrides"] = json!({(provider):null});
assert_eq!(decode(payload.clone()), Some(None));
payload["providerRequestProfileOverrides"][provider] = json!({"versionMode":"latest"});
assert!(decode(payload).unwrap().is_some());
}
}
#[test]
fn xai_profile_override_distinguishes_omission_from_explicit_inheritance() {
let mut payload = json!({"id":"key_profile", "name":"profile", "groupIds":[], "maxConcurrency":0, "requestsPerMinute":0});
let decode = |body| {
let mut command = serde_json::from_value::<UpdateClientKeyRequest>(body)
.unwrap()
.into_command()
.unwrap();
command
.request_profile_override_updates
.remove(&gateway_core::routing::ProviderKind::new("xai").expect("Provider kind"))
};
assert_eq!(decode(payload.clone()), None);
payload["xaiClientProfileOverride"] = json!(null);
assert_eq!(decode(payload.clone()), Some(None));
payload["xaiClientProfileOverride"] = json!({"versionMode":"latest"});
assert!(decode(payload).unwrap().is_some());
}
#[test]
fn profile_overrides_accept_matching_native_values_and_reject_conflicts_and_unknown_providers() {
fn profile_overrides_accept_native_providers_and_reject_unknown_providers() {
let profile = json!({"preset":"desktop"});
let xai = json!({"preset":"managed"});
let request: CreateClientKeyRequest = serde_json::from_value(json!({
@@ -622,7 +660,6 @@ fn profile_overrides_accept_matching_native_values_and_reject_conflicts_and_unkn
"openai": profile,
"xai": xai,
},
"openaiClientProfileOverride": {"preset":"desktop"},
}))
.unwrap();
let command = request.into_command().unwrap();
@@ -633,20 +670,6 @@ fn profile_overrides_accept_matching_native_values_and_reject_conflicts_and_unkn
.contains_key(&gateway_core::routing::ProviderKind::new("xai").expect("Provider kind"))
);
let conflict: CreateClientKeyRequest = serde_json::from_value(json!({
"name":"profile",
"groupIds":[],
"maxConcurrency":0,
"requestsPerMinute":0,
"providerRequestProfileOverrides": {"openai":{"preset":"desktop"}},
"openaiClientProfileOverride": {"preset":"cli"},
}))
.unwrap();
assert_eq!(
conflict.into_command().unwrap_err().field(),
"providerRequestProfileOverrides"
);
let unknown: CreateClientKeyRequest = serde_json::from_value(json!({
"name":"profile",
"groupIds":[],
@@ -675,20 +698,80 @@ fn profile_overrides_accept_matching_native_values_and_reject_conflicts_and_unkn
);
}
#[test]
fn clearing_an_xai_profile_override_conflicts_with_an_explicit_xai_value() {
let request: UpdateClientKeyRequest = serde_json::from_value(json!({
"id":"key_profile",
"name":"profile",
"groupIds":[],
"maxConcurrency":0,
"requestsPerMinute":0,
"providerRequestProfileOverrides": {"xai":null},
"xaiClientProfileOverride": {"preset":"desktop"},
}))
.unwrap();
assert_eq!(
request.into_command().unwrap_err().field(),
"providerRequestProfileOverrides"
);
#[tokio::test]
async fn key_statistics_route_returns_calendar_days_and_current_budget_only_to_admin() {
use crate::support::{cookie_request, key_fixture, response_json};
use axum::http::{Method, StatusCode};
use gateway_core::policy::ClientApiKeyId;
use tower::ServiceExt as _;
let fixture = key_fixture().await;
fixture.auth.insert_session("stats-admin");
let record = fixture
.services
.client_keys()
.reveal(&ClientApiKeyId::new("key-42").unwrap())
.await
.unwrap()
.record;
*fixture.client_key.lock().unwrap() = Some(record);
let app = client_keys::router::<AdminTestState>().with_state(fixture.state());
for (query, cookie, status, days) in [
("id=key-42", "", StatusCode::UNAUTHORIZED, 0),
(
"id=missing",
"cpr_session=stats-admin",
StatusCode::NOT_FOUND,
0,
),
(
"id=key-42&period=today",
"cpr_session=stats-admin",
StatusCode::BAD_REQUEST,
0,
),
("id=key-42", "cpr_session=stats-admin", StatusCode::OK, 7),
(
"id=key-42&period=30d",
"cpr_session=stats-admin",
StatusCode::OK,
30,
),
] {
let mut request = cookie_request(
Method::GET,
&format!("/api/admin/client-keys/statistics?{query}"),
cookie,
);
request
.headers_mut()
.insert("x-request-id", "req_stats".parse().unwrap());
let response = app.clone().oneshot(request).await.unwrap();
assert_eq!(response.status(), status);
if days > 0 {
let body = response_json(response).await;
assert_eq!(body["data"]["dates"].as_array().unwrap().len(), days);
assert!(body["data"]["budget"]["dailyUsedUsd"].is_string());
assert!(body["data"]["series"].as_array().unwrap().is_empty());
assert_eq!(body["data"]["modelCount"], 0);
}
}
for (cookie, status) in [
("", StatusCode::UNAUTHORIZED),
("cpr_session=stats-admin", StatusCode::OK),
] {
let mut request = cookie_request(
Method::GET,
"/api/admin/client-keys/statistics/models?id=key-42",
cookie,
);
request
.headers_mut()
.insert("x-request-id", "req_models".parse().unwrap());
let response = app.clone().oneshot(request).await.unwrap();
assert_eq!(response.status(), status);
if status == StatusCode::OK {
assert_eq!(response_json(response).await["data"], serde_json::json!([]));
}
}
}
@@ -442,29 +442,14 @@ mod provider {
*fixture.account.lock().unwrap() = Some(account(provider));
for (kind, status, code, message) in [
(Kind::Invalid, 400, 40001, "刷新令牌已被使用,请重新授权"),
(
Kind::Conflict,
409,
40901,
"令牌刷新繁忙,请等待当前刷新完成后重试",
),
(
Kind::BadGateway,
502,
50201,
"OpenAI 返回的 Codex PAT 身份资料不完整或格式无效,请稍后重试",
),
(
Kind::Unavailable,
503,
50301,
"暂时无法向 OpenAI 验证 Codex PAT,请稍后重试",
),
(Kind::Conflict, 409, 40901, "令牌刷新繁忙,请稍后重试"),
(Kind::BadGateway, 502, 50201, "PAT 身份信息无效"),
(Kind::Unavailable, 503, 50301, "PAT 验证服务暂不可用"),
(
Kind::Ambiguous,
502,
50202,
"令牌刷新结果未知,请先核对账号状态,不要立即重复刷新",
"刷新结果未知,核对账号状态前请勿重试",
),
] {
*fixture.provider_error.lock().unwrap() = Some(
+65 -37
View File
@@ -641,43 +641,7 @@ impl SettingsStore for MemorySettingsStore {
model_mappings: command.model_mappings,
rotation_strategy: command.rotation_strategy,
updated_at: Utc::now(),
values: gateway_admin::model::settings::RuntimeSettingsValues {
codex_privacy_policy: command.values.codex_privacy_policy,
request_location_enabled: command.values.request_location_enabled,
request_location: command.values.request_location,
refresh_margin_seconds: command.values.refresh_margin_seconds,
refresh_concurrency: command.values.refresh_concurrency,
max_concurrent_per_account: command.values.max_concurrent_per_account,
max_waiting_per_key: command.values.max_waiting_per_key,
max_waiting_per_account: command.values.max_waiting_per_account,
key_queue_timeout_seconds: command.values.key_queue_timeout_seconds,
account_queue_timeout_seconds: command.values.account_queue_timeout_seconds,
switch_account_on_queue_failure: command.values.switch_account_on_queue_failure,
max_retry_after_seconds: command.values.max_retry_after_seconds,
openai_guardian_reserved_concurrency: command
.values
.openai_guardian_reserved_concurrency,
openai_account_affinity: command.values.openai_account_affinity,
max_account_rotations: command.values.max_account_rotations,
openai_session_affinity_ttl_hours: command.values.openai_session_affinity_ttl_hours,
responses_max_decompressed_body_bytes: command
.values
.responses_max_decompressed_body_bytes,
smart_scheduling: command.values.smart_scheduling,
quota_reset_window_weights: command.values.quota_reset_window_weights,
min_codex_desktop_version: command.values.min_codex_desktop_version,
min_codex_cli_version: command.values.min_codex_cli_version,
usage_retention_days: command.values.usage_retention_days,
ops_event_retention_days: command.values.ops_event_retention_days,
audit_retention_days: command.values.audit_retention_days,
account_error_protection: command.values.account_error_protection,
account_warmup_enabled: false,
account_warmup_cron: "0 8 * * *".to_owned(),
account_warmup_model: None,
reset_credit_lead_minutes: 10,
reset_credit_on_low_weekly_quota: false,
reset_credit_max_weekly_remaining_percent: None,
},
values: command.values,
};
*settings = updated.clone();
Ok(updated)
@@ -745,6 +709,8 @@ impl MemoryAccountGroupStore {
primary_id.clone(),
AccountGroupRecord {
allow_image_generation: true,
allow_subagents: true,
model_access: Default::default(),
fast_mode: FastMode::Default,
id: primary_id,
name: "Alpha routing".to_owned(),
@@ -768,6 +734,8 @@ impl MemoryAccountGroupStore {
secondary_id.clone(),
AccountGroupRecord {
allow_image_generation: true,
allow_subagents: true,
model_access: Default::default(),
fast_mode: FastMode::Default,
id: secondary_id,
name: "Beta routing".to_owned(),
@@ -931,6 +899,8 @@ impl AccountGroupStore for MemoryAccountGroupStore {
let now = Utc::now();
let record = AccountGroupRecord {
allow_image_generation: command.allow_image_generation,
allow_subagents: command.allow_subagents,
model_access: command.model_access,
fast_mode: command.fast_mode,
id: command.id.clone(),
name: command.name,
@@ -966,6 +936,12 @@ impl AccountGroupStore for MemoryAccountGroupStore {
if let Some(fast_mode) = command.fast_mode {
record.fast_mode = fast_mode;
}
if let Some(allowed) = command.allow_subagents {
record.allow_subagents = allowed;
}
if let Some(model_access) = command.model_access {
record.model_access = model_access;
}
if let Some(allowed) = command.allow_image_generation {
record.allow_image_generation = allowed;
}
@@ -1020,6 +996,30 @@ fn mutation(
#[async_trait]
impl ClientKeyStore for MemoryClientKeyStore {
async fn budget_reference_accounts(
&self,
_: &gateway_core::policy::ClientApiKeyId,
_: &str,
_: &str,
) -> gateway_admin::ports::store::AdminStoreResult<
gateway_admin::model::client_keys::BudgetReferenceAccountPage,
> {
Err(unavailable("reference accounts"))
}
async fn batch_update_client_keys(
&self,
_: gateway_admin::model::client_keys::BatchUpdateClientKeys,
_: gateway_admin::ports::store::BatchClientKeyProfileMerger,
_: &MutationContext,
) -> AdminStoreResult<Revision> {
Err(AdminStoreError::new(
AdminStoreErrorKind::Unavailable,
"client key",
"unused batch update",
))
}
async fn client_key_budget_window(
&self,
_: &gateway_core::policy::ClientApiKeyId,
@@ -1088,6 +1088,15 @@ impl ClientKeyStore for MemoryClientKeyStore {
Ok(())
}
async fn client_key_budget(
&self,
id: &gateway_core::policy::ClientApiKeyId,
) -> AdminStoreResult<Option<gateway_core::engine::budget::ClientBudgetStatus>> {
self.get_client_key(id)
.await
.map(|key| key.map(|row| row.budget))
}
async fn get_client_key(
&self,
id: &ClientApiKeyId,
@@ -1543,6 +1552,23 @@ impl ObservabilityStore for UnusedStore {
Err(unavailable("dashboard trend"))
}
async fn client_key_statistics(
&self,
_: TimeRange,
_: &gateway_core::policy::ClientApiKeyId,
_: gateway_admin::model::observability::KeyUsageMetric,
) -> AdminStoreResult<gateway_admin::model::observability::KeyStatisticsOverview> {
Ok(Default::default())
}
async fn client_key_models(
&self,
_: TimeRange,
_: &gateway_core::policy::ClientApiKeyId,
) -> AdminStoreResult<Vec<gateway_admin::model::observability::KeyModelRow>> {
Ok(Default::default())
}
async fn usage_trend(
&self,
range: TimeRange,
@@ -1934,6 +1960,8 @@ fn test_runtime_settings() -> RuntimeSettings {
rotation_strategy: RotationStrategy::Smart,
updated_at: Utc::now(),
values: gateway_admin::model::settings::RuntimeSettingsValues {
openai_oauth_guardian_review_model: true,
openai_request_country: Default::default(),
codex_privacy_policy: Default::default(),
request_location_enabled: false,
request_location: Default::default(),
@@ -2039,16 +2039,15 @@ async fn dashboard_summary_identifies_trend_metrics_and_account_usage_values() {
assert_eq!(status, StatusCode::OK, "{kind}: {value}");
let accounts = value["data"]["accountUsage"].as_array().unwrap();
assert_eq!(accounts.len(), 5);
for (account, (metric, raw, display, last_used_at)) in accounts.iter().zip([
("tokens", Some(2_000), "2K", Some(now)),
("tokens", Some(0), "0", None),
("tokens", None, "—", None),
("requests", Some(9), "9", Some(now)),
("requests", Some(0), "—", None),
for (account, (metric, raw, last_used_at)) in accounts.iter().zip([
("tokens", Some(2_000), Some(now)),
("tokens", Some(0), None),
("tokens", None, None),
("requests", Some(9), Some(now)),
("requests", Some(0), None),
]) {
assert_eq!(account["metric"], metric);
assert_eq!(account["metricValueRaw"], json!(raw));
assert_eq!(account["metricValue"], display);
assert_eq!(account["lastUsedAt"], json!(last_used_at));
}
let summary = value["data"]["trend"]["summary"].as_array().unwrap();
@@ -2082,50 +2081,42 @@ async fn dashboard_relative_time_preserves_server_anchor_boundaries_and_deployme
("Asia/Kathmandu", "2026-06-15 05:45:01"),
("America/New_York", "2026-06-14 20:00:01"),
] {
for (elapsed_ms, expected, display) in [
(None, json!(null), "—"),
for (elapsed_ms, expected) in [
(None, json!(null)),
(
Some(-1_000),
json!({ "kind": "absolute", "value": future_display }),
future_display,
),
(Some(-1), json!({ "kind": "just_now" }), "刚刚"),
(Some(0), json!({ "kind": "just_now" }), "刚刚"),
(Some(59_999), json!({ "kind": "just_now" }), "刚刚"),
(Some(-1), json!({ "kind": "just_now" })),
(Some(0), json!({ "kind": "just_now" })),
(Some(59_999), json!({ "kind": "just_now" })),
(
Some(60_000),
json!({ "kind": "elapsed", "value": 1, "unit": "minute" }),
"1 分钟前",
),
(
Some(119_999),
json!({ "kind": "elapsed", "value": 1, "unit": "minute" }),
"1 分钟前",
),
(
Some(3_599_999),
json!({ "kind": "elapsed", "value": 59, "unit": "minute" }),
"59 分钟前",
),
(
Some(3_600_000),
json!({ "kind": "elapsed", "value": 1, "unit": "hour" }),
"1 小时前",
),
(
Some(86_399_999),
json!({ "kind": "elapsed", "value": 23, "unit": "hour" }),
"23 小时前",
),
(
Some(86_400_000),
json!({ "kind": "elapsed", "value": 1, "unit": "day" }),
"1 天前",
),
(
Some(34_560_000_000),
json!({ "kind": "elapsed", "value": 400, "unit": "day" }),
"400 天前",
),
] {
// 每个边界使用独立缓存,避免相同锚点复用上一轮观测数据
@@ -2197,7 +2188,6 @@ async fn dashboard_relative_time_preserves_server_anchor_boundaries_and_deployme
account["lastUsedRelative"], expected,
"{zone}: {elapsed_ms:?}"
);
assert_eq!(account["lastUsed"], display, "{zone}: {elapsed_ms:?}");
assert_eq!(account["lastUsedAt"], json!(last_used_at));
}
}
@@ -40,6 +40,8 @@ async fn response_json(response: axum::response::Response) -> Value {
fn update_body() -> Value {
json!({
"configRevision": 7,
"openaiOauthGuardianReviewModel": true,
"openaiRequestCountry": {"mode": "follow_location"},
"codexPrivacyPolicy": {"enabled":false,"onError":"skip_rule","rules":[]},
"requestLocationEnabled": false,
"requestLocation": {"country":"US", "region":"Ohio", "city":"Piketon", "timezone":"America/New_York"},
@@ -73,6 +75,7 @@ fn update_body() -> Value {
"accountWarmupEnabled": false,
"accountWarmupCron": "0 8 * * *",
"accountWarmupModel": null,
"resetCreditOnLowWeeklyQuota": false,
"resetCreditLeadMinutes": 10,
"resetCreditMaxWeeklyRemainingPercent": null
})
@@ -536,6 +539,17 @@ fn settings_service_contract_flattens_values_and_rejects_unknown_fields() {
serde_json::from_value::<ReplaceRuntimeSettings>(wire.clone()).unwrap(),
command
);
for field in [
"openai_oauth_guardian_review_model",
"openai_request_country",
"reset_credit_on_low_weekly_quota",
] {
let mut incomplete = wire.clone();
incomplete.as_object_mut().unwrap().remove(field);
assert!(serde_json::from_value::<ReplaceRuntimeSettings>(incomplete.clone()).is_err());
incomplete[field] = Value::Null;
assert!(serde_json::from_value::<ReplaceRuntimeSettings>(incomplete).is_err());
}
wire["unexpected_setting"] = json!(true);
assert!(serde_json::from_value::<ReplaceRuntimeSettings>(wire).is_err());
}
@@ -902,33 +916,19 @@ async fn settings_update_rejects_unknown_fields_without_writing() {
assert_eq!(*fixture.settings.settings.lock().unwrap(), initial);
}
#[test]
fn global_profile_can_be_omitted_but_cannot_be_cleared() {
for field in ["openaiClientProfile", "xaiClientProfile"] {
let mut body = update_body();
body.as_object_mut().unwrap().remove(field);
assert!(serde_json::from_value::<UpdateRuntimeSettingsRequest>(body.clone()).is_ok());
body[field] = json!(null);
assert!(serde_json::from_value::<UpdateRuntimeSettingsRequest>(body.clone()).is_err());
body[field] = json!({"versionMode":"latest"});
assert!(serde_json::from_value::<UpdateRuntimeSettingsRequest>(body).is_ok());
}
}
#[tokio::test]
async fn global_profiles_decode_native_providers_and_reject_conflicting_openai_settings() {
let mut body = update_body();
body["providerRequestProfiles"] = json!({
"openai":{"preset":"desktop"},
"xai":{"preset":"managed"},
});
body["openaiClientProfile"] = json!({"preset":"desktop"});
let decoded = serde_json::from_value::<UpdateRuntimeSettingsRequest>(body.clone()).unwrap();
assert!(decoded.provider_request_profiles.contains_key("xai"));
body["openaiClientProfile"] = json!({"preset":"cli"});
async fn global_profiles_map_only_updates_submitted_providers() {
let fixture = AdminTestFixture::new().await;
fixture.auth.insert_session("valid-session");
{
let mut settings = fixture.settings.settings.lock().unwrap();
settings.request_profiles = serde_json::from_value(json!({
"openai":{"preset":"desktop"}, "xai":{"versionMode":"latest"}
}))
.unwrap();
}
let mut body = update_body();
body["providerRequestProfiles"] = json!({"xai":null});
let response = app(fixture.state())
.oneshot(request(
Method::POST,
@@ -937,7 +937,12 @@ async fn global_profiles_decode_native_providers_and_reject_conflicting_openai_s
))
.await
.unwrap();
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
assert_eq!(response.status(), StatusCode::OK);
let saved = response_json(response).await;
assert_eq!(
saved["data"]["providerRequestProfiles"],
json!({"openai":{"preset":"desktop"}})
);
}
#[tokio::test]
@@ -1416,6 +1421,99 @@ async fn settings_update_rejects_a_stale_version_without_replacing_the_saved_val
);
}
#[tokio::test]
async fn runtime_settings_reject_incomplete_and_stale_replacements() {
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"});
body["resetCreditOnLowWeeklyQuota"] = json!(true);
let response = save(body.clone()).await.unwrap();
assert_eq!(response.status(), StatusCode::OK);
let saved = response_json(response).await["data"].clone();
assert_eq!(saved["resetCreditOnLowWeeklyQuota"], true);
assert_eq!(saved["openaiOauthGuardianReviewModel"], false);
assert_eq!(saved["openaiRequestCountry"], body["openaiRequestCountry"]);
for field in [
"openaiOauthGuardianReviewModel",
"openaiRequestCountry",
"resetCreditOnLowWeeklyQuota",
] {
let mut incomplete = update_body();
incomplete["configRevision"] = saved["configRevision"].clone();
incomplete.as_object_mut().unwrap().remove(field);
assert_eq!(
save(incomplete.clone()).await.unwrap().status(),
StatusCode::UNPROCESSABLE_ENTITY
);
incomplete[field] = Value::Null;
assert_eq!(
save(incomplete).await.unwrap().status(),
StatusCode::UNPROCESSABLE_ENTITY
);
}
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, saved);
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;
@@ -1,4 +1,4 @@
//! 真实 HTTP / WebSocket 路由与 Core 执行入口共同拒绝生图,不触发准入或上游
//! 真实 HTTP / WebSocket 路由与 Core 执行入口共同执行分组功能授权,不触发准入或上游
use super::*;
use axum::{
@@ -11,14 +11,15 @@ use std::{sync::Arc, time::Duration};
use tokio_tungstenite::tungstenite::{Message, client::IntoClientRequest};
use tower::ServiceExt;
fn execution() -> Arc<DefaultExecutionService> {
pub(super) fn execution() -> Arc<DefaultExecutionService> {
Arc::new(DefaultExecutionService::new(
RuntimeSnapshotHandle::new(snapshot_with_image_policy(
"sk_image_policy",
RuntimeSnapshotHandle::new(snapshot_with_group_permissions(
"sk_group_permissions",
"openai",
"key_image_policy",
"key_group_permissions",
&["acct_api_test"],
false,
false,
)),
Arc::new(UnusedExecutionStore),
ProviderRegistry::default(),
@@ -54,7 +55,7 @@ async fn denied_image_requests_return_403_before_admission_or_upstream() {
.clone()
.oneshot(
Request::post(path)
.header(AUTHORIZATION, "Bearer sk_image_policy")
.header(AUTHORIZATION, "Bearer sk_group_permissions")
.header("content-type", "application/json")
.body(Body::from(body.to_string()))
.unwrap(),
@@ -71,7 +72,7 @@ async fn denied_image_requests_return_403_before_admission_or_upstream() {
}
#[tokio::test]
async fn each_websocket_create_enforces_image_policy() {
async fn each_websocket_create_enforces_group_permissions() {
let app = api_router(execution()).await;
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
let address = listener.local_addr().unwrap();
@@ -81,18 +82,33 @@ async fn each_websocket_create_enforces_image_policy() {
let mut request = format!("ws://{address}/v1/responses")
.into_client_request()
.unwrap();
request
.headers_mut()
.insert(AUTHORIZATION, "Bearer sk_image_policy".parse().unwrap());
request.headers_mut().insert(
AUTHORIZATION,
"Bearer sk_group_permissions".parse().unwrap(),
);
let (mut socket, _) = tokio_tungstenite::connect_async(request).await.unwrap();
for image_fields in [
json!({"tools":[{"type":"image_generation"}]}),
json!({"tool_choice":{"type":"image_generation"}}),
for (fields, code) in [
(
json!({"tools":[{"type":"image_generation"}]}),
"image_generation_disabled",
),
(
json!({"client_metadata":{"x-codex-turn-metadata":r#"{"subagent_kind":"thread_spawn"}"#}}),
"subagents_disabled",
),
(
json!({"tool_choice":{"type":"image_generation"}}),
"image_generation_disabled",
),
(
json!({"client_metadata":{"x-openai-subagent":"collab_spawn"}}),
"subagents_disabled",
),
] {
let mut body = json!({"type":"response.create","model":"model-a","input":"test"});
body.as_object_mut()
.unwrap()
.extend(image_fields.as_object().unwrap().clone());
.extend(fields.as_object().unwrap().clone());
socket
.send(Message::Text(body.to_string().into()))
.await
@@ -105,8 +121,38 @@ async fn each_websocket_create_enforces_image_policy() {
let body: Value = serde_json::from_str(frame.to_text().unwrap()).unwrap();
assert_eq!(body["type"], "error", "{body}");
assert_eq!(body["status"], 403);
assert_eq!(body["error"]["code"], "image_generation_disabled");
assert_eq!(body["error"]["code"], code);
}
socket.close(None).await.unwrap();
server.abort();
}
#[tokio::test]
async fn collaboration_requests_are_denied_before_admission_or_upstream() {
let app = api_router(execution()).await;
for (header, value) in [
("x-openai-subagent", "collab_spawn"),
(
"x-codex-turn-metadata",
r#"{"subagent_kind":"thread_spawn"}"#,
),
] {
let response = app
.clone()
.oneshot(
Request::post("/v1/responses")
.header(AUTHORIZATION, "Bearer sk_group_permissions")
.header("content-type", "application/json")
.header(header, value)
.body(Body::from(r#"{"model":"model-a","input":"test"}"#))
.unwrap(),
)
.await
.unwrap();
assert_eq!(response.status(), StatusCode::FORBIDDEN);
let body: Value =
serde_json::from_slice(&to_bytes(response.into_body(), usize::MAX).await.unwrap())
.unwrap();
assert_eq!(body["error"]["code"], "subagents_disabled");
}
}
+13 -4
View File
@@ -4,7 +4,7 @@ mod auth;
mod chat_completions;
mod endpoint;
mod error;
mod image_policy;
mod group_permissions;
mod images;
mod live;
mod messages;
@@ -300,15 +300,23 @@ fn snapshot_with_accounts(
client_key_id: &str,
accounts: &[&str],
) -> RuntimeSnapshot {
snapshot_with_image_policy(plaintext, provider_name, client_key_id, accounts, true)
snapshot_with_group_permissions(
plaintext,
provider_name,
client_key_id,
accounts,
true,
true,
)
}
fn snapshot_with_image_policy(
fn snapshot_with_group_permissions(
plaintext: &str,
provider_name: &str,
client_key_id: &str,
accounts: &[&str],
allow_image_generation: bool,
allow_subagents: bool,
) -> RuntimeSnapshot {
let provider = ProviderKind::new(provider_name).expect("provider");
let account_directory = Arc::new(RuntimeAccountDirectory::new(BTreeMap::from_iter(
@@ -345,7 +353,8 @@ fn snapshot_with_image_policy(
Arc::clone(&account_directory),
ClientRoutingScope::all_accounts(),
)
.with_image_generation(allow_image_generation),
.with_image_generation(allow_image_generation)
.with_subagents(allow_subagents),
),
true,
RateLimits::unlimited(),
@@ -406,13 +406,13 @@ async fn model_query_middleware_short_circuit_sets_json_content_type_without_cal
}
#[tokio::test]
async fn native_catalog_preserves_complete_objects_and_only_rewrites_alias_slug() {
async fn native_catalog_preserves_upstream_fields_except_alias_and_group_subagent_policy() {
let original = serde_json::json!({
"slug": "gpt-native", "display_name": "Official name", "priority": 47,
"base_instructions": "Official instructions\nDo not replace.",
"model_messages": {"token_budget": "upstream template", "future_section": [null, {"a": true}]},
"service_tiers": [{"id": "priority", "name": "Fast", "description": "Official speed tier"}],
"tool_mode": "code_mode_only", "use_responses_lite": true,
"tool_mode": "code_mode_only", "use_responses_lite": true, "multi_agent_version": "v2",
"supports_reasoning_summary_parameter": true, "default_reasoning_summary": "detailed",
"future_field": {"nested": [1, null, "value"]}, "explicit_null": null
});
@@ -424,35 +424,49 @@ async fn native_catalog_preserves_complete_objects_and_only_rewrites_alias_slug(
)
.expect("payload"),
};
let execution = Arc::new(ModelsExecution {
client: authenticated_client("sk_models_test"),
presentation: None,
native: Some(Ok(vec![entry("gpt-native"), entry("my-alias")])),
requested_versions: Mutex::default(),
middleware: None,
});
let response = api_router(execution.clone())
.await
.oneshot(authorized_request(
"/v1/models?client_version=0.154.0-alpha.3",
))
.await
.expect("response");
assert_eq!(response.status(), StatusCode::OK);
assert_eq!(response.headers()["cache-control"], "private, no-store");
assert!(response.headers().get("etag").is_none());
let body = to_bytes(response.into_body(), usize::MAX)
.await
.expect("body");
let value: serde_json::Value = serde_json::from_slice(&body).expect("JSON");
assert_eq!(value["models"][0], original);
let mut alias = original;
alias["slug"] = serde_json::json!("my-alias");
assert_eq!(value["models"][1], alias);
assert_eq!(
*execution.requested_versions.lock().expect("versions"),
["0.154.0-alpha.3"]
);
for (allowed, client) in [
(true, authenticated_client("sk_models_test")),
(
false,
super::group_permissions::execution()
.authenticate("sk_group_permissions")
.unwrap(),
),
] {
let execution = Arc::new(ModelsExecution {
client,
presentation: None,
native: Some(Ok(vec![entry("gpt-native"), entry("my-alias")])),
requested_versions: Mutex::default(),
middleware: None,
});
let response = api_router(execution.clone())
.await
.oneshot(authorized_request(
"/v1/models?client_version=0.154.0-alpha.3",
))
.await
.expect("response");
assert_eq!(response.status(), StatusCode::OK);
assert_eq!(response.headers()["cache-control"], "private, no-store");
assert!(response.headers().get("etag").is_none());
let body = to_bytes(response.into_body(), usize::MAX)
.await
.expect("body");
let value: serde_json::Value = serde_json::from_slice(&body).expect("JSON");
let mut expected = original.clone();
if !allowed {
expected["multi_agent_version"] = serde_json::json!("disabled");
}
assert_eq!(value["models"][0], expected);
let mut alias = expected;
alias["slug"] = serde_json::json!("my-alias");
assert_eq!(value["models"][1], alias);
assert_eq!(
*execution.requested_versions.lock().expect("versions"),
["0.154.0-alpha.3"]
);
}
}
#[tokio::test]
@@ -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
@@ -1,4 +1,4 @@
//! 账号分组的 Fast 三态策略与多分组优先级
//! 账号分组的快速模式策略与多分组优先级
use serde::{Deserialize, Serialize};
@@ -10,6 +10,7 @@ pub enum FastMode {
Default,
Enabled,
Disabled,
Ultrafast,
}
impl FastMode {
@@ -18,6 +19,7 @@ impl FastMode {
match self {
Self::Default => "default",
Self::Enabled => "enabled",
Self::Ultrafast => "ultrafast",
Self::Disabled => "disabled",
}
}
@@ -27,17 +29,19 @@ impl FastMode {
match value {
"default" => Some(Self::Default),
"enabled" => Some(Self::Enabled),
"ultrafast" => Some(Self::Ultrafast),
"disabled" => Some(Self::Disabled),
_ => None,
}
}
/// 多分组冲突时关闭优先于开启,默认不覆盖其他分组
/// 多分组冲突时禁用优先;同时强制两种加速档位时选用较低的 Fast
#[must_use]
pub const fn merge(self, other: Self) -> Self {
match (self, other) {
(Self::Disabled, _) | (_, Self::Disabled) => Self::Disabled,
(Self::Enabled, _) | (_, Self::Enabled) => Self::Enabled,
(Self::Ultrafast, _) | (_, Self::Ultrafast) => Self::Ultrafast,
_ => Self::Default,
}
}
@@ -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::{
@@ -1292,6 +1292,7 @@ pub struct CredentialCasUpdateParts {
pub expected_revision: CredentialRevision,
pub profile: ProviderAccountUpdate,
pub preserve_profile: bool,
pub preserve_access_token_expiry: bool,
pub credential: PlaintextCredential,
pub has_refresh_token: bool,
pub access_token_expires_at: Option<SystemTime>,
@@ -1306,6 +1307,7 @@ pub struct CredentialCasUpdate {
expected_revision: CredentialRevision,
profile: ProviderAccountUpdate,
preserve_profile: bool,
preserve_access_token_expiry: bool,
credential: PlaintextCredential,
has_refresh_token: bool,
access_token_expires_at: Option<SystemTime>,
@@ -1321,6 +1323,10 @@ impl fmt::Debug for CredentialCasUpdate {
.field("expected_revision", &self.expected_revision)
.field("profile", &self.profile)
.field("preserve_profile", &self.preserve_profile)
.field(
"preserve_access_token_expiry",
&self.preserve_access_token_expiry,
)
.field("credential", &self.credential)
.field("has_refresh_token", &self.has_refresh_token)
.field("access_token_expires_at", &self.access_token_expires_at)
@@ -1356,6 +1362,7 @@ impl CredentialCasUpdate {
expected_revision,
profile,
preserve_profile: false,
preserve_access_token_expiry: false,
credential,
has_refresh_token,
access_token_expires_at,
@@ -1371,6 +1378,13 @@ impl CredentialCasUpdate {
self
}
/// 沿用同一访问令牌时,保留提交时的有效期,避免覆盖在途收到的撤销事实
#[must_use]
pub const fn preserving_access_token_expiry(mut self) -> Self {
self.preserve_access_token_expiry = true;
self
}
/// 将刷新调度与账号错误事实放入同一个 revision CAS
#[must_use]
pub fn with_account_state(
@@ -1437,6 +1451,7 @@ impl CredentialCasUpdate {
expected_revision: self.expected_revision,
profile: self.profile,
preserve_profile: self.preserve_profile,
preserve_access_token_expiry: self.preserve_access_token_expiry,
credential: self.credential,
has_refresh_token: self.has_refresh_token,
access_token_expires_at: self.access_token_expires_at,
@@ -88,6 +88,12 @@ impl AccountModelAccess {
AccountModelAccessMode::Denylist => !self.models.contains(upstream_model),
}
}
pub(crate) fn allows_optional_model(&self, upstream_model: Option<&str>) -> bool {
upstream_model.map_or(self.mode == AccountModelAccessMode::All, |model| {
self.allows(model)
})
}
}
#[derive(Deserialize)]
@@ -1,6 +1,6 @@
//! 请求认证时冻结的账号范围与目录,不依赖路由选择器
use super::{FastMode, ProviderAccountId};
use super::{AccountModelAccess, FastMode, ProviderAccountId};
use crate::identity::ProviderKind;
use crate::validation::{IdentifierError, RoutingError};
use std::{
@@ -107,6 +107,7 @@ pub struct RuntimeAccountDirectory {
accounts: BTreeMap<ProviderAccountId, RuntimeAccount>,
providers_with_accounts: BTreeSet<ProviderKind>,
providers_by_group: BTreeMap<AccountGroupId, BTreeSet<ProviderKind>>,
group_model_access: BTreeMap<AccountGroupId, AccountModelAccess>,
}
impl RuntimeAccountDirectory {
@@ -127,9 +128,28 @@ impl RuntimeAccountDirectory {
accounts,
providers_with_accounts,
providers_by_group,
group_model_access: BTreeMap::new(),
}
}
#[must_use]
pub fn with_group_model_access(
mut self,
policies: BTreeMap<AccountGroupId, AccountModelAccess>,
) -> Self {
self.group_model_access = policies;
self
}
pub(crate) fn accounts_in_group(
&self,
group: &AccountGroupId,
) -> impl Iterator<Item = (&ProviderAccountId, &RuntimeAccount)> {
self.accounts
.iter()
.filter(move |(_, account)| account.group_ids.contains(group))
}
#[must_use]
pub fn account(&self, account_id: &ProviderAccountId) -> Option<&RuntimeAccount> {
self.accounts.get(account_id)
@@ -267,6 +287,7 @@ impl ClientRoutingScope {
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FrozenAccountScope {
allow_image_generation: bool,
allow_subagents: bool,
fast_mode: FastMode,
request_profiles: BTreeMap<ProviderKind, super::OpaqueProviderData>,
directory: Arc<RuntimeAccountDirectory>,
@@ -303,6 +324,11 @@ impl FrozenAccountScope {
self.allow_image_generation
}
#[must_use]
pub const fn allows_subagents(&self) -> bool {
self.allow_subagents
}
/// 与 Key 授权范围一同冻结;具体字段只由对应 Provider 解释
#[must_use]
pub fn with_request_profiles(
@@ -323,6 +349,12 @@ impl FrozenAccountScope {
&self.request_profiles
}
#[must_use]
pub const fn with_subagents(mut self, allowed: bool) -> Self {
self.allow_subagents = allowed;
self
}
/// Key 绑定分组的冻结 Fast 策略,与账号成员资格无关
#[must_use]
pub const fn with_fast_mode(mut self, fast_mode: FastMode) -> Self {
@@ -343,6 +375,7 @@ impl FrozenAccountScope {
};
Self {
allow_image_generation: true,
allow_subagents: true,
fast_mode: FastMode::Default,
request_profiles: BTreeMap::new(),
directory,
@@ -399,6 +432,14 @@ impl FrozenAccountScope {
#[must_use]
pub fn allows(&self, account_id: &ProviderAccountId) -> bool {
self.allows_with_policy(account_id, |_| true)
}
fn allows_with_policy(
&self,
account_id: &ProviderAccountId,
accepts: impl Fn(&AccountModelAccess) -> bool,
) -> bool {
let Some(account) = self.directory.account(account_id) else {
return false;
};
@@ -408,6 +449,7 @@ impl FrozenAccountScope {
.allowed_account_ids
.as_ref()
.is_some_and(|accounts| !accounts.contains(account_id))
|| !accepts(account.model_access())
{
return false;
}
@@ -417,18 +459,43 @@ impl FrozenAccountScope {
enabled_group_ids, ..
} => account
.group_ids()
.iter()
.any(|group_id| enabled_group_ids.contains(group_id)),
.intersection(enabled_group_ids)
.any(|id| {
self.directory
.group_model_access
.get(id)
.is_none_or(&accepts)
}),
}
}
#[must_use]
pub fn allows_model(&self, account_id: &ProviderAccountId, upstream_model: &str) -> bool {
self.allows(account_id)
&& self
.directory
.account(account_id)
.is_some_and(|account| account.model_access.allows(upstream_model))
self.allows_with_policy(account_id, |access| access.allows(upstream_model))
}
/// 不猜测原生端点的默认模型,账号与授权它的分组均须允许任意模型
#[must_use]
pub fn allows_unidentified_model(&self, account_id: &ProviderAccountId) -> bool {
self.allows_with_policy(account_id, |access| access.allows_optional_model(None))
}
/// 只报告所有已启用绑定分组均明确拒绝模型的情况,空池沿用原路由错误
#[must_use]
pub fn groups_deny_model(&self, upstream_model: Option<&str>) -> bool {
let ClientRoutingScope::Restricted {
enabled_group_ids, ..
} = &self.client_scope
else {
return false;
};
!enabled_group_ids.is_empty()
&& enabled_group_ids.iter().all(|id| {
self.directory
.group_model_access
.get(id)
.is_some_and(|access| !access.allows_optional_model(upstream_model))
})
}
/// 目录按整个授权账号池过滤,不能只看用于获取元数据的账号政策
@@ -173,6 +173,15 @@ pub trait ProviderAccountStore: Send + Sync {
change: QuotaAccessChange,
) -> Result<QuotaWriteOutcome, StoreError>;
/// 按请求使用的凭据版本缩短访问令牌有效期,保留在途续期的版本和重试计划
async fn expire_access_token(
&self,
_account: &ProviderAccount,
_observed_at: std::time::SystemTime,
) -> Result<bool, StoreError> {
Err(StoreError::new(crate::error::StoreErrorKind::InvalidData))
}
async fn apply_state_change(&self, change: AccountStateChange) -> Result<(), StoreError>;
async fn update_account(&self, update: ProviderAccountUpdate) -> Result<(), StoreError>;
@@ -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())
@@ -24,6 +24,11 @@ pub(super) fn authentication_gateway_error(error: ClientAuthenticationError) ->
pub(super) fn map_routing_error(error: crate::validation::RoutingError) -> GatewayError {
match error {
crate::validation::RoutingError::GroupModelDenied => GatewayError::new(
GatewayErrorKind::PolicyDenied,
crate::validation::GROUP_MODEL_DENIED_MESSAGE,
)
.with_client_code(crate::validation::GROUP_MODEL_DENIED_CODE),
crate::validation::RoutingError::ModelNotFound {
model,
mapped_model,
@@ -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,
},
@@ -478,9 +478,10 @@ impl DefaultExecutionService {
let budget_key_id = request.client.policy.key_id().clone();
let mut entered_execution = false;
let result = async {
crate::engine::image_generation::check_image_generation(
authorization.account_scope.allows_image_generation(),
crate::engine::group_permissions::check_group_permissions(
&authorization.account_scope,
&request.operation,
&[],
)?;
if authorization.cancellation.is_cancelled() {
return Err(GatewayError::new(
@@ -507,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 {
@@ -573,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)
@@ -0,0 +1,36 @@
//! Key 绑定分组的功能权限在请求入口与上游发送前生效
use crate::{
account::scope::FrozenAccountScope, engine::middleware::MiddlewareHeader,
error::DeniedGroupFeature, operation::Operation,
};
pub(super) fn check_group_permissions(
scope: &FrozenAccountScope,
operation: &Operation,
headers: &[MiddlewareHeader],
) -> Result<(), DeniedGroupFeature> {
if !scope.allows_image_generation() && operation.image_generation_requested() {
return Err(DeniedGroupFeature::ImageGeneration);
}
if !scope.allows_subagents()
&& let Operation::Generate(request) = operation
&& request.protocol_payload().protocol() == gateway_protocol::openai::OPENAI_PROTOCOL
{
let payload = request.protocol_payload();
let header = |name: &str| {
headers
.iter()
.find(|header| header.name().eq_ignore_ascii_case(name))
.and_then(|header| std::str::from_utf8(header.value()).ok())
};
if gateway_protocol::openai::codex::collaboration_subagent_requested(
payload.body(),
payload.context(),
header,
) {
return Err(DeniedGroupFeature::CollaborativeSubagents);
}
}
Ok(())
}
@@ -1,20 +0,0 @@
//! 冻结分组生图权限的请求与发送前门禁
use crate::{
error::{GatewayError, GatewayErrorKind},
operation::Operation,
};
pub(super) fn check_image_generation(
allowed: bool,
operation: &Operation,
) -> Result<(), GatewayError> {
if !allowed && operation.image_generation_requested() {
return Err(GatewayError::new(
GatewayErrorKind::PolicyDenied,
"Image generation is not allowed by the API key's account groups.",
)
.with_client_code("image_generation_disabled"));
}
Ok(())
}
@@ -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)
+53 -12
View File
@@ -10,7 +10,7 @@ pub mod continuation;
pub mod coordinator;
pub mod execution;
pub mod extensions;
mod image_generation;
mod group_permissions;
pub mod middleware;
pub mod nested;
pub mod observation;
@@ -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 {
@@ -777,19 +822,15 @@ impl AttemptContext {
terminal: provider::ProviderMiddlewareTerminal,
) -> Result<provider::ProviderStream, ProviderError> {
// 入口已校验原请求,此处复查中间件最终提交的 operation,每次重试均生效
let terminal: provider::ProviderMiddlewareTerminal = if self
let terminal: provider::ProviderMiddlewareTerminal = if let Some(scope) = self
.account_scope()
.is_some_and(|scope| !scope.allows_image_generation())
.filter(|scope| !scope.allows_image_generation() || !scope.allows_subagents())
{
let scope = Arc::clone(scope);
Box::new(move |operation, headers| {
Box::pin(async move {
image_generation::check_image_generation(false, &operation).map_err(|_| {
ProviderError::new(
ProviderErrorKind::RequestPolicyDenied,
UpstreamSendState::NotSent,
)
.with_retry_prohibited()
})?;
group_permissions::check_group_permissions(&scope, &operation, &headers)
.map_err(ProviderError::group_feature_denied)?;
terminal(operation, headers).await
})
})
@@ -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)
}
}
+42 -3
View File
@@ -508,6 +508,7 @@ impl fmt::Debug for ClientVisibleUpstreamResponse {
/// Core 会在任何持久化或日志记录前取走
/// 这些事件,不能把它们当作诊断上下文使用
pub struct ProviderError {
denied_group_feature: Option<DeniedGroupFeature>,
kind: ProviderErrorKind,
send_state: UpstreamSendState,
upstream_status: Option<u16>,
@@ -556,6 +557,7 @@ impl ProviderError {
Self {
kind,
send_state,
denied_group_feature: None,
upstream_status: None,
upstream_values: None,
retry_after: None,
@@ -579,6 +581,16 @@ impl ProviderError {
}
}
pub(crate) fn group_feature_denied(feature: DeniedGroupFeature) -> Self {
let mut error = Self::new(
ProviderErrorKind::RequestPolicyDenied,
UpstreamSendState::NotSent,
)
.with_retry_prohibited();
error.denied_group_feature = Some(feature);
error
}
/// 附加合法上游状态码
#[must_use]
pub fn with_status(mut self, status: u16) -> Self {
@@ -1026,6 +1038,9 @@ impl ProviderError {
/// 绑定迁移完成后仅替换客户端恢复投影,保留原始失败的诊断与计费事实
pub(crate) fn with_client_recovery_from(mut self, recovery: Self) -> Self {
if let Some(reason) = recovery.continuation_unavailable_reason() {
self = self.with_continuation_unavailable_reason(reason);
}
self.client_visible_upstream_error = recovery.client_visible_upstream_error;
self.client_visible_upstream_response = recovery.client_visible_upstream_response;
self.continuation_failure = recovery.continuation_failure;
@@ -1057,6 +1072,7 @@ impl ProviderError {
pub fn stable_snapshot(&self) -> Self {
Self {
kind: self.kind,
denied_group_feature: self.denied_group_feature,
send_state: self.send_state,
upstream_status: self.upstream_status,
upstream_values: self.upstream_values.clone(),
@@ -1241,6 +1257,28 @@ impl GatewayErrorKind {
}
}
#[derive(Clone, Copy)]
pub(crate) enum DeniedGroupFeature {
ImageGeneration,
CollaborativeSubagents,
}
impl From<DeniedGroupFeature> for GatewayError {
fn from(feature: DeniedGroupFeature) -> Self {
let (code, message) = match feature {
DeniedGroupFeature::ImageGeneration => (
"image_generation_disabled",
"Image generation is not allowed by the API key's account groups.",
),
DeniedGroupFeature::CollaborativeSubagents => (
"subagents_disabled",
"Collaborative subagents are not allowed by the API key's account groups.",
),
};
Self::new(GatewayErrorKind::PolicyDenied, message).with_client_code(code)
}
}
/// 协议无关、可安全暴露的网关错误
#[derive(Clone, Error)]
#[error("{message}")]
@@ -1342,9 +1380,10 @@ impl GatewayError {
GatewayErrorKind::UpstreamUnavailable,
"The upstream account cannot access the service.",
),
ProviderErrorKind::RequestPolicyDenied => {
Self::new(GatewayErrorKind::PolicyDenied, "Request blocked by policy.")
}
ProviderErrorKind::RequestPolicyDenied => error.denied_group_feature.map_or_else(
|| Self::new(GatewayErrorKind::PolicyDenied, "Request blocked by policy."),
Self::from,
),
ProviderErrorKind::RateLimited | ProviderErrorKind::QuotaExhausted => Self::new(
GatewayErrorKind::RateLimited,
"Upstream capacity is temporarily unavailable.",
@@ -1,6 +1,6 @@
//! 下游 Client API Key 的准入策略
//!
//! Client API Key 冻结账号分组权限;模型名称不参与权限判断
//! Client API Key 在请求开始时冻结分组成员范围及其模型策略
mod client_version;
mod peer_sharing;
+17 -4
View File
@@ -532,21 +532,28 @@ 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>,
model_presentation: Option<ModelPresentation>,
emulated_features: BTreeSet<Feature>,
account_scope: Arc<FrozenAccountScope>,
}
impl ProviderCandidate {
/// 与路由计划冻结的上游模型目录,重试时不读取新的目录代次
/// 模型按直连账号身份选择,Provider 必须在发送前核对实际凭据。
#[must_use]
pub const fn model_presentation(&self) -> Option<&ModelPresentation> {
self.model_presentation.as_ref()
pub const fn requires_direct_authentication(&self) -> bool {
self.requires_direct_authentication
}
#[must_use]
@@ -578,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,
@@ -632,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;
@@ -83,7 +83,9 @@ impl SnapshotClientPolicyFacts {
/// Store 读取到的账号分组事实
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SnapshotAccountGroupFacts {
model_access: crate::account::AccountModelAccess,
allow_image_generation: bool,
allow_subagents: bool,
fast_mode: FastMode,
id: AccountGroupId,
name: String,
@@ -91,12 +93,24 @@ pub struct SnapshotAccountGroupFacts {
}
impl SnapshotAccountGroupFacts {
#[must_use]
pub fn with_model_access(mut self, model_access: crate::account::AccountModelAccess) -> Self {
self.model_access = model_access;
self
}
#[must_use]
pub const fn with_image_generation(mut self, allowed: bool) -> Self {
self.allow_image_generation = allowed;
self
}
#[must_use]
pub const fn with_subagents(mut self, allowed: bool) -> Self {
self.allow_subagents = allowed;
self
}
#[must_use]
pub const fn with_fast_mode(mut self, fast_mode: FastMode) -> Self {
self.fast_mode = fast_mode;
@@ -106,10 +120,12 @@ impl SnapshotAccountGroupFacts {
#[must_use]
pub fn new(id: AccountGroupId, name: String, enabled: bool) -> Self {
Self {
model_access: crate::account::AccountModelAccess::all(),
id,
name,
enabled,
allow_image_generation: true,
allow_subagents: true,
fast_mode: FastMode::Default,
}
}
@@ -549,12 +565,24 @@ async fn compile_runtime_snapshot(
account.direct_authentication_kind().map(str::to_owned),
);
}
let account_directory = Arc::new(RuntimeAccountDirectory::new(accounts));
// 整份快照共享分组模型政策;默认规则无需为每个 Key 或成员复制
let group_model_access = groups
.values_mut()
.filter(|group| {
group.enabled
&& group.model_access.mode() != crate::account::AccountModelAccessMode::All
})
.map(|group| (group.id.clone(), std::mem::take(&mut group.model_access)))
.collect();
let account_directory = Arc::new(
RuntimeAccountDirectory::new(accounts).with_group_model_access(group_model_access),
);
let mut client_policies = Vec::with_capacity(facts.client_policies.len());
for policy in facts.client_policies {
let mut fast_mode = FastMode::Default;
let mut allow_image_generation = true;
let mut allow_subagents = true;
let account_scope = if policy.group_ids.is_empty() {
FrozenAccountScope::new(
Arc::clone(&account_directory),
@@ -574,6 +602,7 @@ async fn compile_runtime_snapshot(
// 停用只影响选号,仍绑定的分组继续贡献请求策略
fast_mode = fast_mode.merge(group.fast_mode);
allow_image_generation &= group.allow_image_generation;
allow_subagents &= group.allow_subagents;
bound_groups.push(RoutingGroupSnapshot::new(
group.id.clone(),
group.name.clone(),
@@ -597,6 +626,7 @@ async fn compile_runtime_snapshot(
Arc::new(
account_scope
.with_image_generation(allow_image_generation)
.with_subagents(allow_subagents)
.with_fast_mode(fast_mode)
.with_request_profiles(policy.request_profiles),
),
@@ -967,6 +997,54 @@ impl RuntimeSnapshot {
.collect()
}
/// 分组编辑器使用成员账号的实际模型,可编辑已停用或已限制模型的分组
#[must_use]
pub fn upstream_models_for_group(&self, group: &AccountGroupId) -> Vec<UpstreamModelId> {
let mut models = BTreeSet::new();
for (account_id, account) in self.account_directory.accounts_in_group(group) {
let provider = account.provider_kind();
if !self.providers.contains(provider) {
continue;
}
let access = account.model_access();
if access.mode() == crate::account::AccountModelAccessMode::Allowlist {
// 原生图片、Search 和透传模型可能只有账号白名单明确声明了 ID
models.extend(
access
.models()
.iter()
.filter_map(|id| UpstreamModelId::new(id.clone()).ok()),
);
}
let sources = self.model_catalog_accounts.get(provider);
if let Some(catalog) = self.provider_models.get(provider) {
models.extend(
catalog
.keys()
.filter(|model| {
access.allows(model.as_str())
&& sources
.and_then(|sources| sources.get(model.as_str()))
.is_none_or(|sources| sources.contains(account_id))
})
.cloned(),
);
}
for contribution in self.contributed_models() {
if &contribution.provider == provider && contribution.accounts.contains(account_id)
{
let target = self.mapped_model(contribution.id.as_str());
if access.allows(&target)
&& let Ok(model) = UpstreamModelId::new(target)
{
models.insert(model);
}
}
}
}
models.into_iter().collect()
}
/// 合并冻结账号范围内实际存在 Provider 的公开模型
#[must_use]
pub fn public_models_for_scope(&self, scope: &FrozenAccountScope) -> Vec<PublicModelId> {
@@ -1210,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
@@ -1237,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 的账号模型政策裁决探测资格
@@ -1253,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(
@@ -1263,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();
@@ -1272,19 +1423,28 @@ 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)
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()))
{
return Err(RoutingError::GroupModelDenied);
}
.map_err(|_| RoutingError::InvalidIdentifier)?;
// 归属先于账号范围、能力与路由约束判定,原 Provider 被排除不能变成跨平台透传
let known_model = self.is_known_upstream_model(&upstream_model)
|| self
@@ -1300,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;
}
@@ -1324,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() {
@@ -1384,17 +1553,8 @@ impl RuntimeSnapshot {
native.unwrap_or_default()
};
candidates.push(ProviderCandidate {
requires_direct_authentication: direct_model.is_some(),
provider: provider.clone(),
model_presentation: self
.provider_model_presentations
.get(provider)
.and_then(|models| models.get(&upstream_model))
.cloned()
.or_else(|| {
contributions
.first()
.map(|model| model.presentation.clone())
}),
upstream_model: Some(upstream_model.clone()),
emulated_features,
account_scope: candidate_scope,
@@ -1434,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),
@@ -1471,6 +1632,12 @@ impl RuntimeSnapshot {
.as_ref()
.is_none_or(|required| required == provider)
&& !context.blocked_providers.contains(provider);
if available
&& upstream_model
.is_some_and(|model| account_scope.groups_deny_model(Some(model.as_str())))
{
return Err(RoutingError::GroupModelDenied);
}
if !available
|| upstream_model
.is_some_and(|model| !account_scope.allows_provider_model(provider, model.as_str()))
@@ -1505,14 +1672,8 @@ impl RuntimeSnapshot {
});
}
let candidate = ProviderCandidate {
requires_direct_authentication: false,
provider: provider.clone(),
model_presentation: upstream_model
.and_then(|model| {
self.provider_model_presentations
.get(provider)
.and_then(|models| models.get(model))
})
.cloned(),
upstream_model: upstream_model.cloned(),
emulated_features: BTreeSet::new(),
account_scope: Arc::clone(&account_scope),
@@ -1524,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())
@@ -25,6 +25,8 @@ 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,
pub(crate) openai_oauth_guardian_review_model: bool,
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,
@@ -108,6 +110,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 +234,8 @@ impl SettingsValues {
switch_account_on_queue_failure: false,
max_retry_after_seconds: 240,
openai_guardian_reserved_concurrency: 0,
openai_oauth_guardian_review_model: true,
openai_request_country: Default::default(),
openai_account_affinity: crate::account::AccountAffinity::default(),
max_account_rotations: 3,
openai_session_affinity_ttl_hours: 24,
@@ -54,6 +54,12 @@ pub enum OperationError {
},
}
/// Core 路由和 Provider 原生端点共用的分组模型拒绝码
pub const GROUP_MODEL_DENIED_CODE: &str = "group_model_denied";
/// 拒绝原因不回显请求模型或分组名称
pub const GROUP_MODEL_DENIED_MESSAGE: &str =
"Model is not allowed by the API key's account groups.";
/// 路由快照或 Route Plan 不满足不变量
#[derive(Debug, Clone, PartialEq, Eq, Error)]
pub enum RoutingError {
@@ -72,6 +78,9 @@ pub enum RoutingError {
/// Key 的账号范围中当前没有任何可参与路由的账号
#[error("account routing scope is empty")]
EmptyAccountScope,
/// 已启用的绑定分组均明确禁止请求模型
#[error("{GROUP_MODEL_DENIED_MESSAGE}")]
GroupModelDenied,
/// 快照中存在重复实体
#[error("duplicate {entity} `{id}`")]
DuplicateEntity {
@@ -2493,11 +2493,12 @@ fn native_continuation_uses_provider_defined_owner_then_cross_account_recovery()
#[test]
fn native_capacity_above_wait_limit_rotates_before_requesting_client_replay() {
for (seconds, replacement, rotations, expected_attempts) in [
(309, true, 1, 2),
(309, false, 1, 2),
(240, true, 1, 1),
(309, true, 0, 1),
for (seconds, replacement, rotations, committed_prelude, expected_attempts) in [
(309, true, 1, false, 2),
(317, true, 1, true, 2),
(309, false, 1, false, 2),
(240, true, 1, false, 1),
(309, true, 0, false, 1),
] {
let Operation::Generate(generate) = generate_operation() else {
unreachable!()
@@ -2516,11 +2517,14 @@ fn native_capacity_above_wait_limit_rotates_before_requesting_client_replay() {
Default::default(),
240,
);
let original_body = Bytes::from_static(br#"{"error":{"code":"server_is_overloaded","headers":{"retry-after":"309","retry-after-ms":"309000"}}}"#);
let original_body = json!({"error": {
"code": "server_is_overloaded",
"headers": {"retry-after": seconds.to_string(), "retry-after-ms": (seconds * 1000).to_string()}
}}).to_string();
let original = transient_rejection()
.with_retry_after(Duration::from_secs(seconds))
.with_raw_upstream_error(gateway_core::error::RawUpstreamError::new(
String::from_utf8(original_body.to_vec()).unwrap(),
original_body.clone(),
))
.with_continuation_recovery_disposition(
ContinuationRecoveryDisposition::ClientReplayRequired,
@@ -2530,6 +2534,7 @@ fn native_capacity_above_wait_limit_rotates_before_requesting_client_replay() {
ProviderErrorKind::ContinuationRecoveryRequired,
UpstreamSendState::NotSent,
)
.with_continuation_unavailable_reason("account_rotated")
.with_continuation_recovery_disposition(
ContinuationRecoveryDisposition::ClientReplayRequired,
)
@@ -2544,10 +2549,18 @@ fn native_capacity_above_wait_limit_rotates_before_requesting_client_replay() {
UpstreamSendState::NotSent,
)
};
let mut items = Vec::new();
if committed_prelude {
items.push(Ok(ProviderEvent::canonical(GatewayEvent::Started(
ResponseMeta::new("response-1", "gpt-5"),
))
.with_replayable_prelude()));
}
items.push(Err(original));
let (coordinator, store, provider) = coordinator(vec![
Script::Stream {
Script::ObservedStream {
account_id: "acct_one",
items: vec![Err(original)],
items,
},
Script::Error(recovery),
]);
@@ -2568,26 +2581,31 @@ fn native_capacity_above_wait_limit_rotates_before_requesting_client_replay() {
CancellationToken::new(),
))
.unwrap();
let mut collection = Box::pin(session.collect_uncommitted());
if committed_prelude {
block_on(session.next_event()).unwrap().unwrap();
block_on(session.commit_downstream(Some(200))).unwrap();
}
let mut next = Box::pin(session.next_event());
let std::task::Poll::Ready(Err(EngineError::Provider(error))) =
block_on(async { futures::poll!(&mut collection) })
block_on(async { futures::poll!(&mut next) })
else {
panic!("native capacity recovery must not wait on the old account");
};
drop(collection);
drop(next);
assert_eq!(error.kind(), ProviderErrorKind::UpstreamCapacityUnavailable);
assert_eq!(error.send_state(), UpstreamSendState::Sent);
assert_eq!(error.retry_after(), Some(Duration::from_secs(seconds)));
assert_eq!(
error.raw_upstream_error().unwrap().as_str().as_bytes(),
original_body.as_ref()
);
assert_eq!(error.raw_upstream_error().unwrap().as_str(), original_body);
assert_eq!(
error
.client_visible_upstream_error()
.and_then(|detail| detail.code()),
(replacement && expected_attempts == 2).then_some("previous_response_not_found")
);
assert_eq!(
error.continuation_unavailable_reason(),
(replacement && expected_attempts == 2).then_some("account_rotated")
);
let contexts = provider.contexts.lock().unwrap();
assert_eq!(contexts.len(), expected_attempts);
if contexts.len() == 2 {
@@ -2602,9 +2620,16 @@ fn native_capacity_above_wait_limit_rotates_before_requesting_client_replay() {
);
assert!(contexts[1].required_account().is_none());
}
let state = store.state.lock().unwrap();
let finalization = &state.finalizations[0];
assert_eq!(finalization.attempt_count, 1);
assert_eq!(
store.state.lock().unwrap().finalizations[0].attempt_count,
1
finalization.outcome,
if committed_prelude {
ExecutionOutcome::Incomplete
} else {
ExecutionOutcome::Failed
}
);
}
}
@@ -6268,7 +6293,12 @@ fn deadline_before_first_event_is_persisted_as_failed_attempt() {
#[test]
fn global_location_and_group_fast_policy_reach_every_account_retry() {
for mode in [FastMode::Default, FastMode::Enabled, FastMode::Disabled] {
for mode in [
FastMode::Default,
FastMode::Enabled,
FastMode::Ultrafast,
FastMode::Disabled,
] {
let operation = generate_operation();
let location = gateway_core::account::RequestLocation {
timezone: "Asia/Tokyo".parse().unwrap(),
@@ -5081,10 +5081,20 @@ fn request_settings_recompute_routing_and_admission_without_changing_sibling_or_
#[test]
fn invalid_request_settings_leave_the_prepared_execution_unchanged() {
use gateway_core::routing::request_settings::ExecutionSettings;
let service = service(Arc::new(Admissions::default()), Arc::new(Budget::default()));
let client = service.authenticate("sk_start_test").unwrap();
let prepared = service.prepare_verified_execution(client).unwrap();
let baseline = prepared.request_settings().execution_values().unwrap();
for field in [
"openai_oauth_guardian_review_model",
"openai_request_country",
] {
let mut incomplete = serde_json::to_value(&baseline).unwrap();
incomplete["runtime"].as_object_mut().unwrap().remove(field);
assert!(serde_json::from_value::<ExecutionSettings>(incomplete).is_err());
}
let mut invalid = serde_json::to_value(&baseline).unwrap();
invalid["runtime"]["responses_max_decompressed_body_bytes"] = json!(0);
invalid["fast_mode"] = json!("disabled");
@@ -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
@@ -736,31 +736,43 @@ impl MiddlewarePlan for AddingImageTool {
}
#[test]
fn attempt_middleware_cannot_add_image_generation_to_a_denied_scope() {
fn attempt_middleware_cannot_add_a_group_denied_feature() {
use gateway_core::account::{AccountSelectionPolicy, RotationStrategy};
use gateway_core::engine::{AccountAttemptContext, AttemptContext, RequestAttemptContext};
use gateway_core::error::ProviderErrorKind;
use gateway_core::routing::{ClientRoutingScope, FrozenAccountScope, RuntimeAccountDirectory};
use gateway_core::upstream::UpstreamSendState;
for allowed in [false, true] {
for (allowed, image) in [(true, true), (false, true), (true, false), (false, false)] {
let scope = FrozenAccountScope::new(
Arc::new(RuntimeAccountDirectory::new(Default::default())),
ClientRoutingScope::all_accounts(),
)
.with_image_generation(allowed);
let request = RequestAttemptContext::new(
ModelRequestId::new("req_image_policy").unwrap(),
ClientApiKeyId::new("key_image_policy").unwrap(),
)
.with_middleware(
Some(FrozenMiddlewarePlan::new(
.with_image_generation(!image || allowed)
.with_subagents(image || allowed);
let plan = if image {
FrozenMiddlewarePlan::new(
Arc::new(AddingImageTool),
ExtensionSetReference::new(
ExtensionSetId::new("image-policy-test".to_owned()).unwrap(),
Arc::new(ReadyGeneration),
),
)),
)
} else {
header_plan(
"subagent-policy-test",
vec![MiddlewareHeader::new(
"x-openai-subagent",
Bytes::from_static(b"collab_spawn"),
)],
)
};
let request = RequestAttemptContext::new(
ModelRequestId::new("req_group_permissions").unwrap(),
ClientApiKeyId::new("key_group_permissions").unwrap(),
)
.with_middleware(
Some(plan),
Arc::from([]),
"/v1/responses".into(),
ClientTransport::HttpJson,
@@ -782,9 +794,17 @@ fn attempt_middleware_cannot_add_image_generation_to_a_denied_scope() {
ProviderKind::new("openai").unwrap(),
Some("gpt-test".into()),
ProviderAccountId::new("acct_middleware").unwrap(),
Box::new(move |operation, _| {
Box::new(move |operation, headers| {
Box::pin(async move {
assert!(operation.image_generation_requested());
if image {
assert!(operation.image_generation_requested());
} else {
assert!(
headers
.iter()
.any(|header| header.name() == "x-openai-subagent")
);
}
count.fetch_add(1, Ordering::SeqCst);
Ok(empty_provider_stream())
})
@@ -796,10 +816,19 @@ fn attempt_middleware_cannot_add_image_generation_to_a_denied_scope() {
} else {
let error = match result {
Err(error) => error,
Ok(_) => panic!("image tool must be denied"),
Ok(_) => panic!("group feature must be denied"),
};
assert_eq!(error.kind(), ProviderErrorKind::RequestPolicyDenied);
assert_eq!(error.send_state(), UpstreamSendState::NotSent);
let gateway = gateway_core::error::GatewayError::from_provider(&error);
assert_eq!(
gateway.client_error_code(),
Some(if image {
"image_generation_disabled"
} else {
"subagents_disabled"
})
);
assert!(error.retry_is_prohibited());
}
}
@@ -728,6 +728,161 @@ impl ProviderCatalogPort for PublishingCatalog {
}
}
#[test]
fn group_model_access_requires_a_common_group_for_account_and_mapped_model() {
use gateway_core::{
account::{AccountModelAccess, AccountModelAccessMode, ProviderAccountId},
routing::AccountGroupId,
validation::RoutingError,
};
let group = |n| AccountGroupId::new(format!("grp_{n:032x}")).unwrap();
let account = |id| ProviderAccountId::new(id).unwrap();
let policy = |mode, models: &[&str]| {
AccountModelAccess::new(mode, models.iter().map(|id| (*id).to_owned()).collect()).unwrap()
};
let (a, b, shared) = (account("acct_a"), account("acct_b"), account("acct_shared"));
let (first, second, disabled) = (group(1), group(2), group(3));
let key = ClientApiKeyId::new("key_groups").unwrap();
let facts = SnapshotFacts::new(
revision(1),
revision(1),
SettingsValues::new(
3,
"smart",
[("alias".into(), "listed-model".into())].into(),
None,
None,
),
vec![SnapshotClientPolicyFacts::new(
key.clone(),
PlaintextClientApiKey::new("sk_groups").unwrap(),
vec![first.clone(), second.clone(), disabled.clone()],
RateLimits::unlimited(),
)],
vec![
SnapshotAccountGroupFacts::new(first.clone(), "First".into(), true)
.with_model_access(policy(AccountModelAccessMode::Allowlist, &["listed-model"])),
SnapshotAccountGroupFacts::new(second.clone(), "Second".into(), true)
.with_model_access(policy(
AccountModelAccessMode::Denylist,
&["listed-model", "blocked"],
)),
SnapshotAccountGroupFacts::new(disabled.clone(), "Disabled".into(), false),
],
vec![
SnapshotProviderAccountFacts::new(a.clone(), "alpha"),
SnapshotProviderAccountFacts::new(b.clone(), "alpha"),
SnapshotProviderAccountFacts::new(shared.clone(), "alpha").with_model_access(policy(
AccountModelAccessMode::Allowlist,
&["listed-model", "image-model"],
)),
],
vec![
SnapshotAccountGroupMemberFacts::new(first.clone(), a.clone()),
SnapshotAccountGroupMemberFacts::new(second.clone(), b.clone()),
SnapshotAccountGroupMemberFacts::new(first.clone(), shared.clone()),
SnapshotAccountGroupMemberFacts::new(second.clone(), shared.clone()),
SnapshotAccountGroupMemberFacts::new(disabled.clone(), b.clone()),
],
);
let snapshot = block_on(
RuntimeSnapshotCompiler::new(
Arc::new(TestSnapshotStore::new(Ok(facts))),
Arc::new(TestCatalog::Discovery),
)
.compile(),
)
.unwrap();
let scope = snapshot.client_policy(&key).unwrap().account_scope();
let alias = PublicModelId::new("alias").unwrap();
let plan = snapshot
.plan(
&alias,
&super::operation(),
Arc::clone(scope),
&Default::default(),
)
.unwrap();
let candidate = &plan.candidates()[0];
assert_eq!(candidate.upstream_model().unwrap().as_str(), "listed-model");
assert!(candidate.account_scope().allows_model(&a, "listed-model"));
assert!(!candidate.account_scope().allows_model(&b, "listed-model"));
assert!(scope.allows_model(&b, "other"));
assert!(scope.allows_model(&shared, "listed-model"));
assert!(scope.allows_model(&shared, "image-model"));
assert!(!scope.allows_model(&shared, "other"));
assert!(!scope.allows_unidentified_model(&b));
assert!(scope.groups_deny_model(None));
assert!(snapshot.contains_public_model_for_scope(&alias, scope));
let b_only = scope.restricted_to(&Default::default(), &[b.clone()].into());
assert!(!snapshot.public_models_for_scope(&b_only).contains(&alias));
assert!(!snapshot.contains_public_model_for_scope(&alias, &b_only));
assert!(!snapshot.allows_public_model_for_account(&alias, scope, &b));
let blocked = PublicModelId::new("blocked").unwrap();
assert!(matches!(
snapshot.plan(
&blocked,
&super::operation(),
Arc::clone(scope),
&Default::default()
),
Err(RoutingError::GroupModelDenied)
));
assert!(matches!(
snapshot.plan_provider_endpoint(
&ProviderKind::new("alpha").unwrap(),
Some(&UpstreamModelId::new("blocked").unwrap()),
&super::operation(),
Arc::clone(scope),
&Default::default()
),
Err(RoutingError::GroupModelDenied)
));
assert!(snapshot.all_account_scope().allows_model(&b, "blocked"));
assert!(snapshot.all_account_scope().allows_unidentified_model(&b));
assert_eq!(
snapshot.upstream_models_for_group(&second),
["image-model", "listed-model"].map(|id| UpstreamModelId::new(id).unwrap())
);
assert_eq!(
snapshot.upstream_models_for_group(&disabled),
vec![UpstreamModelId::new("listed-model").unwrap()]
);
assert!(snapshot.upstream_models_for_group(&group(4)).is_empty());
// 第二组允许此模型,但适配器只绑定到第一组的账号
let adapted = PublicModelId::new("adapter-only").unwrap();
let scope = Arc::clone(scope);
let snapshot = snapshot.with_extensions(Some(ExtensionSetReference::new(
ExtensionSetId::new("group-adapter".into()).unwrap(),
Arc::new(ModelLease(vec![gateway_core::routing::ContributedModel {
owner: "fixture".into(),
id: adapted.clone(),
provider: ProviderKind::new("alpha").unwrap(),
accounts: [a].into(),
capabilities: super::capabilities(),
presentation: ModelPresentation::new(Some("Adapter".into()), None),
}])),
)));
assert!(!snapshot.contains_public_model_for_scope(&adapted, &scope));
assert!(
snapshot
.plan(&adapted, &super::operation(), scope, &Default::default())
.is_err()
);
let upstream = UpstreamModelId::new("adapter-only").unwrap();
assert!(
snapshot
.upstream_models_for_group(&first)
.contains(&upstream)
);
assert!(
!snapshot
.upstream_models_for_group(&second)
.contains(&upstream)
);
}
#[test]
fn compiler_should_reject_revision_changed_during_consistent_read() {
let store = Arc::new(SequencedSnapshotStore {
@@ -1617,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()
@@ -1628,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(
@@ -1646,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
@@ -1659,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"
@@ -1676,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()
@@ -1686,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()
@@ -1769,6 +1943,17 @@ fn fast_mode_merges_only_bound_groups_without_changing_account_scope() {
(FastMode::Disabled, FastMode::Default, FastMode::Disabled),
(FastMode::Disabled, FastMode::Enabled, FastMode::Disabled),
(FastMode::Disabled, FastMode::Disabled, FastMode::Disabled),
(FastMode::Default, FastMode::Ultrafast, FastMode::Ultrafast),
(FastMode::Ultrafast, FastMode::Default, FastMode::Ultrafast),
(
FastMode::Ultrafast,
FastMode::Ultrafast,
FastMode::Ultrafast,
),
(FastMode::Enabled, FastMode::Ultrafast, FastMode::Enabled),
(FastMode::Ultrafast, FastMode::Enabled, FastMode::Enabled),
(FastMode::Disabled, FastMode::Ultrafast, FastMode::Disabled),
(FastMode::Ultrafast, FastMode::Disabled, FastMode::Disabled),
] {
for group_enabled in [false, true] {
for bound in [false, true] {
@@ -1847,7 +2032,7 @@ fn fast_mode_merges_only_bound_groups_without_changing_account_scope() {
}
#[test]
fn image_generation_merges_bound_groups_with_deny_precedence() {
fn group_feature_permissions_merge_independently_with_deny_precedence() {
use gateway_core::account::ProviderAccountId;
use gateway_core::routing::AccountGroupId;
for (allowed, other_allowed) in [(true, true), (true, false), (false, true), (false, false)] {
@@ -1877,13 +2062,15 @@ fn image_generation_merges_bound_groups_with_deny_precedence() {
"Restricted".to_owned(),
group_enabled,
)
.with_image_generation(allowed),
.with_image_generation(allowed)
.with_subagents(!allowed),
SnapshotAccountGroupFacts::new(
open_group_id.clone(),
"Open".to_owned(),
true,
)
.with_image_generation(other_allowed),
.with_image_generation(other_allowed)
.with_subagents(!other_allowed),
],
vec![SnapshotProviderAccountFacts::new(
account_id.clone(),
@@ -1909,6 +2096,10 @@ fn image_generation_merges_bound_groups_with_deny_precedence() {
.account_scope()
.clone();
assert!(scope.allows(&account_id));
assert_eq!(
scope.allows_subagents(),
!bound || (!allowed && !other_allowed)
);
assert_eq!(
scope.allows_image_generation(),
!bound || (allowed && other_allowed),
@@ -1995,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"
);
}
@@ -2,11 +2,14 @@
use super::{MiddlewareEntry, PluginMiddlewarePlan};
use futures::future::BoxFuture;
use gateway_core::engine::extensions::ExtensionCallScope;
use gateway_core::engine::middleware::{
MiddlewareContext, MiddlewareError, MiddlewareMount, MiddlewareNext, MiddlewarePlan,
MiddlewareRequest, MiddlewareResponse,
};
use gateway_core::middleware::{Middleware, Next};
use gateway_core::policy::ClientApiKeyId;
use gateway_core::routing::{AccountGroupId, ProviderKind};
use std::sync::Arc;
// 协议适配只提供选择条件与单层调用;顺序、上下文捕获和续体拼接共用这一个入口
@@ -49,10 +52,34 @@ 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.matches_processing(
route.extensions,
route.client_api_key,
route.account_groups,
Some(provider),
Some(model),
)
};
!matches(route.model) || matches(target.as_str())
})
}
fn has_outbound(&self, context: &MiddlewareContext) -> bool {
self.middleware
.get(&MiddlewareMount::Outbound)
.is_some_and(|entries| entries.iter().any(|entry| outbound_matches(entry, context)))
.is_some_and(|entries| entries.iter().any(|entry| matches_context(entry, context)))
}
fn handle_outbound(
@@ -67,7 +94,7 @@ impl MiddlewarePlan for PluginMiddlewarePlan {
self.chain(
MiddlewareMount::Outbound,
context,
outbound_matches,
matches_context,
|entry, context, request, next| {
Box::pin(super::outbound::invoke(entry, context, request, next))
},
@@ -158,15 +185,7 @@ impl MiddlewarePlan for PluginMiddlewarePlan {
self.chain(
context.mount(),
context,
|entry, context| {
!context.extension_scope().contains(&entry.instance_id)
&& entry.scope.matches_processing(
context.client_key_id(),
context.account_group_ids(),
context.provider(),
context.model(),
)
},
matches_context,
|entry, context, request, next| {
Box::pin(super::request::invoke(entry, context, request, next))
},
@@ -176,13 +195,26 @@ impl MiddlewarePlan for PluginMiddlewarePlan {
}
}
// 存在性检查与实际组合使用同一选择规则,未命中时不进入原生报文准备
fn outbound_matches(entry: &MiddlewareEntry, context: &MiddlewareContext) -> bool {
!context.extension_scope().contains(&entry.instance_id)
&& entry.scope.matches_processing(
context.client_key_id(),
context.account_group_ids(),
context.provider(),
context.model(),
)
impl MiddlewareEntry {
fn matches_processing(
&self,
extensions: &ExtensionCallScope,
key: &ClientApiKeyId,
groups: &[AccountGroupId],
provider: Option<&ProviderKind>,
model: Option<&str>,
) -> bool {
!extensions.contains(&self.instance_id)
&& self.scope.matches_processing(key, groups, provider, model)
}
}
fn matches_context(entry: &MiddlewareEntry, context: &MiddlewareContext) -> bool {
entry.matches_processing(
context.extension_scope(),
context.client_key_id(),
context.account_group_ids(),
context.provider(),
context.model(),
)
}
@@ -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),
)
@@ -267,6 +267,7 @@ fn prepare_replace(
plan_type: facts.plan_type,
preserve_profile: false,
preserve_credential_state: false,
preserve_access_token_expiry: false,
provider_material: ProviderDocument::new(OpaqueProviderData::new(facts.material)),
has_refresh_token: facts.has_refresh_token,
access_token_expires_at: credential_timestamp(facts.access_token_expires_at_ms)
@@ -86,6 +86,7 @@ impl PluginClientKeys {
.window
.map(|window| {
Ok(ExplicitClientKeyBudgetWindow {
resume_calendar: false,
starts_at: chrono::DateTime::from_timestamp_micros(window.starts_at_us)
.ok_or_else(invalid)?,
ends_at: chrono::DateTime::from_timestamp_micros(window.ends_at_us)
@@ -74,6 +74,8 @@ impl PluginResources {
request.resource_key,
CreateAccountGroup {
allow_image_generation: true,
allow_subagents: true,
model_access: Default::default(),
fast_mode: gateway_core::account::FastMode::Default,
name: request.name,
description: request.description,
@@ -25,6 +25,8 @@ use std::{path::Path, sync::Arc, time::Duration};
fn group(name: &str) -> CreateAccountGroup {
CreateAccountGroup {
allow_image_generation: true,
allow_subagents: true,
model_access: Default::default(),
name: name.into(),
description: None,
fast_mode: FastMode::Default,
@@ -1,16 +1,18 @@
//! 验证中间件 Fast 三态设置与运行时故障隔离
//! 验证中间件 快速模式设置与运行时故障隔离
use super::*;
use gateway_core::{policy::ClientPolicy, routing::request_settings::RequestSettings};
use serde_json::{Value, json};
#[tokio::test]
async fn middleware_preserves_three_state_fast_and_sources() {
async fn middleware_preserves_fast_modes_and_sources() {
for (mode, expected) in [
(FastMode::Default, FastMode::Default),
(FastMode::Enabled, FastMode::Enabled),
(FastMode::Disabled, FastMode::Disabled),
(FastMode::Enabled, FastMode::Disabled),
(FastMode::Ultrafast, FastMode::Ultrafast),
(FastMode::Enabled, FastMode::Ultrafast),
(FastMode::Disabled, FastMode::Default),
] {
let snapshot = gateway_core::routing::RuntimeSnapshot::new(
@@ -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;
}
}
@@ -80,6 +80,25 @@ async fn explicit_service_dispatch_composes_once_and_preserves_native_transactio
let mut command = ReplaceRuntimeSettings::from(original);
command.values.refresh_margin_seconds = 999;
command.values.openai_oauth_guardian_review_model = false;
command.values.openai_request_country = gateway_core::account::OpenAiRequestCountry::Custom {
country: "CA".into(),
};
for field in [
"openai_oauth_guardian_review_model",
"openai_request_country",
] {
let mut args = serde_json::to_value((mutation(), &command)).unwrap();
args[1].as_object_mut().unwrap().remove(field);
assert!(
services
.public_services()
.call("settings.replace", args)
.await
.is_err(),
"{field}"
);
}
let saved = services
.public_services()
.call(
@@ -100,6 +119,11 @@ async fn explicit_service_dispatch_composes_once_and_preserves_native_transactio
.await
.unwrap();
assert_eq!(current.values.refresh_margin_seconds, 1234);
assert!(!current.values.openai_oauth_guardian_review_model);
assert_eq!(
current.values.openai_request_country,
command.values.openai_request_country
);
assert_eq!(
core.snapshots()
.snapshot_for_diagnostics()
@@ -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] {
@@ -382,9 +432,14 @@ async fn account_scoped_adapters_share_a_model_without_sharing_their_targets() {
}
#[tokio::test]
async fn adapter_preserves_three_state_fast_in_request_metadata() {
async fn adapter_preserves_group_fast_mode_in_request_metadata() {
use gateway_core::account::FastMode;
for mode in [FastMode::Default, FastMode::Enabled, FastMode::Disabled] {
for mode in [
FastMode::Default,
FastMode::Enabled,
FastMode::Ultrafast,
FastMode::Disabled,
] {
let server = MockServer::start().await;
Mock::given(method("POST"))
.and(path("/adapter/responses"))
@@ -188,6 +188,8 @@ impl Environment {
.create_account_group(
NewAccountGroup {
allow_image_generation: true,
allow_subagents: true,
model_access: Default::default(),
fast_mode: FastMode::Default,
id: id.clone(),
name: format!("fixture {}", id.as_str()),
@@ -349,6 +349,7 @@ HTTP 总入口统一包裹原生路由,`dispatch_http` 复用同一路由与
`Load` 返回完整运行设置与 `config_revision`。`ReplaceRuntimeSettings::from(settings)` 保留该版本并构造更新命令;
`Replace` 接收 `(MutationContext, ReplaceRuntimeSettings)`,通过已有校验、事务、审计和快照发布保存设置。
每次写入都必须提供 `openai_oauth_guardian_review_model`、`openai_request_country` 和 `reset_credit_on_low_weekly_quota`。
版本过期返回 `ServiceError { kind: "conflict", message }`。读取结果的普通改写不会持久化,也不会回滚已经提交的事务
`accounts::GetResetCreditAutomation` 接收账号 ID,返回 `ResetCreditAutomationStatus`:开关、全局提前分钟数、
@@ -493,7 +494,7 @@ OpenAI 与 xAI 的上游 headers 先由账号和画像生成,插件同名字
实际账号租约、凭据版本与计费归属仍记录真实选中事实,不随插件修改 header 名称而伪造
request 阶段 `request.head.settings` 是本次调用的有效设置对象,包含 `runtime`、`fast_mode`、`client_limits` 和 `timeout_ms`。
`fast_mode` 使用 `default`、`enabled`、`disabled` 三态,具体档位语义见[账号分组](../../../../../docs/api.md#6-账号分组)。
`fast_mode` 使用 `default`、`enabled`、`ultrafast`、`disabled` 四个值,具体档位语义见[账号分组](../../../../../docs/api.md#6-账号分组)。
`runtime` 使用宿主快照的设置字段,画像已合并 Client Key 设置;可以修改映射、画像、调度、位置及计价等执行参数。
SDK 仅在对象改变时提交完整替换,`false`、`0`、空映射和合同允许的 `null` 不表示跳过;不做隐式深合并。
线协议省略 `next.settings` 才表示保留;显式 `null` 仍按完整替换校验,因不符合设置对象合同而拒绝。
@@ -41,7 +41,7 @@ Provider、业务协议和传输名称不能写入宿主兼容清单的 capabili
`TypedCall<UpstreamAdapterRequest>` 包含只读的 Key、账号、凭据版本、模型、协议、Header 和宿主续接投影;
原始请求正文位于 `call.payload`,原始账号凭据不进入插件输入。宿主首次消费冷流才调用插件
`fast_mode` 是本次模型执行的有效设置,取值为 `default`、`enabled` 或 `disabled`。OpenAI 在 attempt 中间件之前将 Fast 设置应用到正文基线,
`fast_mode` 是本次模型执行的有效设置,取值为 `default`、`enabled`、`ultrafast` 或 `disabled`。OpenAI 在 attempt 中间件之前将 Fast 设置应用到正文基线,
适配器收到经过 attempt 中间件处理的正文;attempt 中间件显式改写档位后,宿主不会在发送前再次应用 Fast 策略。
其他请求设置与 Key 作用域的覆盖规则见[模型请求与 attempt](capabilities.md#模型请求与-attempt)
@@ -136,6 +136,8 @@ pub struct PrivacyPreviewResult {
#[derive(Clone, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct RuntimeSettings {
pub openai_oauth_guardian_review_model: bool,
pub openai_request_country: OpenAiRequestCountry,
pub codex_privacy_policy: CodexPrivacyPolicy,
pub request_location_enabled: bool,
pub request_location: RequestLocation,
@@ -164,7 +166,6 @@ pub struct RuntimeSettings {
pub account_warmup_enabled: bool,
pub account_warmup_cron: String,
pub account_warmup_model: Option<String>,
#[serde(default)]
pub reset_credit_on_low_weekly_quota: bool,
pub reset_credit_lead_minutes: u32,
pub reset_credit_max_weekly_remaining_percent: Option<u32>,
@@ -177,6 +178,8 @@ pub struct RuntimeSettings {
#[derive(Clone, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ReplaceRuntimeSettings {
pub openai_oauth_guardian_review_model: bool,
pub openai_request_country: OpenAiRequestCountry,
pub codex_privacy_policy: CodexPrivacyPolicy,
pub request_location_enabled: bool,
pub request_location: RequestLocation,
@@ -205,7 +208,6 @@ pub struct ReplaceRuntimeSettings {
pub account_warmup_enabled: bool,
pub account_warmup_cron: String,
pub account_warmup_model: Option<String>,
#[serde(default)]
pub reset_credit_on_low_weekly_quota: bool,
pub reset_credit_lead_minutes: u32,
pub reset_credit_max_weekly_remaining_percent: Option<u32>,
@@ -249,6 +251,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 {
@@ -389,6 +399,8 @@ impl Operation for PreviewClientProfile {
impl From<RuntimeSettings> for ReplaceRuntimeSettings {
fn from(settings: RuntimeSettings) -> Self {
Self {
openai_oauth_guardian_review_model: settings.openai_oauth_guardian_review_model,
openai_request_country: settings.openai_request_country,
codex_privacy_policy: settings.codex_privacy_policy,
request_location_enabled: settings.request_location_enabled,
request_location: settings.request_location,
@@ -104,7 +104,7 @@ pub struct UpstreamAdapterRequest {
pub credential_revision: u64,
pub protocol: String,
pub client_transport: String,
/// 宿主冻结的 Fast 策略:default、enabled 或 disabled
/// 宿主冻结的 Fast 策略,可取 default、enabled、ultrafast 或 disabled
pub fast_mode: String,
/// 宿主发送调用前相对 Core 请求计时起点已经过的毫秒数
pub request_elapsed_ms: u64,
@@ -182,11 +182,7 @@ pub fn codex_responses_request_semantics_with_turn_metadata(
let reasoning_effort = reasoning_effort_override
.clone()
.or_else(|| reasoning_effort(body.get("reasoning")));
// 权威上下文与显式 turnMetadata 即使为空也不退回其他别名
let parsed_turn_metadata = turn_metadata
.or_else(|| body.get("turnMetadata").and_then(Value::as_str))
.or_else(|| request_turn_metadata(body))
.and_then(|value| serde_json::from_str::<Value>(value).ok());
let parsed_turn_metadata = parsed_turn_metadata(body, turn_metadata);
let mut request_kind = parsed_turn_metadata
.as_ref()
.and_then(|metadata| non_empty_string(metadata.get("request_kind")))
@@ -229,6 +225,34 @@ fn reasoning_effort(reasoning: Option<&Value>) -> Option<String> {
.or_else(|| effort.as_u64().map(|value| value.to_string()))
}
/// 只识别协作线程自己的生成请求,review、compact、guardian 和父线程的工具历史均不受限
#[must_use]
pub fn collaboration_subagent_requested<'a>(
body: &Map<String, Value>,
context: &Map<String, Value>,
header: impl Fn(&str) -> Option<&'a str>,
) -> bool {
let is_spawn = |kind: &str| matches!(kind.trim(), "thread_spawn" | "collab_spawn");
let subagent =
header("x-openai-subagent").or_else(|| non_empty_string(context.get("subagent")));
let turn_metadata =
header("x-codex-turn-metadata").or_else(|| context_turn_metadata(body, context));
subagent.is_some_and(is_spawn)
|| client_metadata_string(body, "x-openai-subagent").is_some_and(is_spawn)
|| parsed_turn_metadata(body, turn_metadata)
.as_ref()
.and_then(|metadata| non_empty_string(metadata.get("subagent_kind")))
.is_some_and(is_spawn)
}
fn parsed_turn_metadata(body: &Map<String, Value>, turn_metadata: Option<&str>) -> Option<Value> {
// 权威上下文与显式 turnMetadata 即使为空也不退回其他别名
turn_metadata
.or_else(|| body.get("turnMetadata").and_then(Value::as_str))
.or_else(|| request_turn_metadata(body))
.and_then(|value| serde_json::from_str(value).ok())
}
fn request_turn_metadata(body: &Map<String, Value>) -> Option<&str> {
non_empty_string(body.get("turnMetadata"))
.or_else(|| non_empty_string(body.get("turn_metadata")))
@@ -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,
};

Some files were not shown because too many files have changed in this diff Show More