mirror of
https://github.com/zyycn/codex-proxy-rs.git
synced 2026-11-17 08:07:05 +00:00
Co-authored-by: Zyy <zyycn@users.noreply.github.com> Co-authored-by: Codex <noreply@openai.com>
1587 lines
53 KiB
Rust
1587 lines
53 KiB
Rust
mod plugins;
|
|
use std::{
|
|
collections::BTreeMap,
|
|
sync::{
|
|
Arc, Mutex,
|
|
atomic::{AtomicBool, Ordering},
|
|
},
|
|
};
|
|
|
|
use async_trait::async_trait;
|
|
use chrono::{DateTime, Duration, Utc};
|
|
use futures::future::BoxFuture;
|
|
use gateway_admin::{
|
|
AdminConfig, AdminServices, ClientConfig, InitialAdminPassword,
|
|
model::{
|
|
MutationContext, Revision,
|
|
account_groups::{
|
|
AccountGroupAccountSummary, AccountGroupCapacity, AccountGroupColor,
|
|
AccountGroupListQuery, AccountGroupMemberFact, AccountGroupMutation, AccountGroupPage,
|
|
AccountGroupRecord, AccountGroupUsage, DeleteAccountGroup, NewAccountGroup,
|
|
SetAccountGroupEnabled, UpdateAccountGroup,
|
|
},
|
|
accounts::{
|
|
AccountListQuery, AccountPage, AccountPageItem, AccountRuntimeSnapshot,
|
|
AccountUpdateResult, AccountUsage, AccountUsageWindowQuery, AccountUsageWindowResult,
|
|
AccountsUpdateResult, BatchUpdateAccounts, DeleteAccounts, UpdateAccount,
|
|
},
|
|
auth::{AdminAuditEvent, AuthSession},
|
|
client_distribution::{
|
|
ClientArchitecture, ClientDownloadPackage, ClientDownloadSource,
|
|
CodexDesktopWindowsDownloads,
|
|
},
|
|
client_keys::{
|
|
ClientKeyListQuery, ClientKeyPage, ClientKeyRecord, ClientKeySecret, DeleteClientKey,
|
|
NewClientKey, SetClientKeyEnabled, UpdateClientKey,
|
|
},
|
|
observability::{
|
|
DashboardObservation, DecimalAmount, DiagnosticDimension, DiagnosticObservation,
|
|
OpsError, OpsErrorPage, OpsErrorQuery, RequestMetricPoint, TimeRange, UsageDetail,
|
|
UsageFilter, UsageListRecord, UsageOverview, UsagePage, UsageQuery,
|
|
},
|
|
provider_credentials::{
|
|
AuthorizationCommit, AuthorizationStarted, CompleteAuthorization, CredentialDetails,
|
|
CredentialImportCommit, CredentialImportResult, CredentialMutationResult,
|
|
CredentialRotationCommit, PrepareCredentialImport, PrepareCredentialRefresh,
|
|
PrepareCredentialRotation, PreparedAuthorizationCommit, PreparedCredentialImport,
|
|
PreparedCredentialRotation, ProviderExport, ProviderExportCredentialInput,
|
|
ProviderModels, ProviderQuota,
|
|
},
|
|
settings::{
|
|
AdminApiKey, AdminApiKeyMutation, ModelMappings, ReplaceRuntimeSettings,
|
|
RotationStrategy, RuntimeSettings,
|
|
},
|
|
system::{SystemOperationAccepted, SystemUpdateDetail, SystemUpdateStatus, SystemVersion},
|
|
},
|
|
ports::{
|
|
client_distribution::ClientDistributionResolver,
|
|
provider::{ProviderAdmin, ProviderAdminError, ProviderAdminErrorKind},
|
|
store::{
|
|
AccountGroupStore, AccountRuntimeStore, AccountStore, AdminAccountStorePorts,
|
|
AdminStoreError, AdminStoreErrorKind, AdminStorePorts, AdminStoreResult, AuthStore,
|
|
ClientKeyStore, ObservabilityStore, SettingsStore,
|
|
},
|
|
system::{
|
|
SystemOperationError, SystemOperationErrorKind, SystemOperations,
|
|
SystemUpdateEventStream,
|
|
},
|
|
},
|
|
};
|
|
use gateway_api::auth::SessionState;
|
|
use gateway_core::{
|
|
account::{AccountStatusFacts, CredentialState, ProviderAccountId, QuotaState},
|
|
engine::{
|
|
execution::{ClientAuthenticationError, ClientKeyVerifier},
|
|
probe::{AccountProbe, AccountProbeError, AccountProbeRequest, AccountProbeResult},
|
|
},
|
|
policy::{ClientApiKeyId, RateLimits},
|
|
routing::{ConfigRevision, ProviderKind, PublicModelId, UpstreamModelId},
|
|
runtime::SnapshotControl,
|
|
};
|
|
|
|
mod account_groups;
|
|
mod accounts;
|
|
mod auth;
|
|
mod client_keys;
|
|
mod errors;
|
|
mod observability;
|
|
mod proxies;
|
|
mod settings;
|
|
mod system;
|
|
mod wire;
|
|
|
|
pub(super) struct AdminTestFixture {
|
|
pub services: AdminServices,
|
|
plugin_ports: Arc<plugins::TestPluginPorts>,
|
|
published_snapshot: gateway_core::runtime::RuntimeSnapshotHandle,
|
|
pub client_key: Arc<Mutex<Option<ClientKeyRecord>>>,
|
|
pub observations: Arc<Mutex<MemoryObservations>>,
|
|
pub auth: Arc<MemoryAuthStore>,
|
|
pub settings: Arc<MemorySettingsStore>,
|
|
pub usage_records: Arc<Mutex<Vec<UsageListRecord>>>,
|
|
pub usage_detail: Arc<Mutex<Option<UsageDetail>>>,
|
|
pub diagnostics: Arc<Mutex<Vec<DiagnosticObservation>>>,
|
|
pub ops_errors: Arc<Mutex<Vec<OpsError>>>,
|
|
pub dashboard_observation: Arc<Mutex<Option<DashboardObservation>>>,
|
|
pub dashboard_summary_range: Arc<Mutex<Option<TimeRange>>>,
|
|
pub provider_error: Arc<Mutex<Option<ProviderAdminError>>>,
|
|
pub account: Arc<Mutex<Option<AccountPageItem>>>,
|
|
}
|
|
|
|
impl AdminTestFixture {
|
|
pub async fn new() -> Self {
|
|
Self::with_system(Arc::new(UnusedSystem)).await
|
|
}
|
|
|
|
pub async fn with_system(system: Arc<dyn SystemOperations>) -> Self {
|
|
Self::with_dependencies(system, None).await
|
|
}
|
|
|
|
pub async fn with_key_verifier(
|
|
verifier: Arc<dyn ClientKeyVerifier>,
|
|
system: Arc<dyn SystemOperations>,
|
|
) -> Self {
|
|
Self::with_dependencies(system, Some(verifier)).await
|
|
}
|
|
|
|
async fn with_dependencies(
|
|
system: Arc<dyn SystemOperations>,
|
|
verifier: Option<Arc<dyn ClientKeyVerifier>>,
|
|
) -> Self {
|
|
let api_key = Arc::new(Mutex::new(None));
|
|
let auth = Arc::new(MemoryAuthStore::new(api_key.clone()));
|
|
let settings = Arc::new(MemorySettingsStore::new(api_key));
|
|
let client_key = Arc::new(Mutex::new(None));
|
|
let client_keys = Arc::new(MemoryClientKeyStore(client_key.clone()));
|
|
let observations = Arc::new(Mutex::new(MemoryObservations::default()));
|
|
let account_groups = Arc::new(MemoryAccountGroupStore::new());
|
|
let usage_records = Arc::new(Mutex::new(Vec::new()));
|
|
let usage_detail = Arc::new(Mutex::new(None));
|
|
let diagnostics = Arc::new(Mutex::new(Vec::new()));
|
|
let ops_errors = Arc::new(Mutex::new(Vec::new()));
|
|
let dashboard_observation = Arc::new(Mutex::new(None));
|
|
let dashboard_summary_range = Arc::new(Mutex::new(None));
|
|
let provider_error = Arc::new(Mutex::new(None));
|
|
let account = Arc::new(Mutex::new(None));
|
|
let unused = Arc::new(UnusedStore {
|
|
observations: observations.clone(),
|
|
usage_records: Arc::clone(&usage_records),
|
|
usage_detail: Arc::clone(&usage_detail),
|
|
diagnostics: Arc::clone(&diagnostics),
|
|
ops_errors: Arc::clone(&ops_errors),
|
|
dashboard_observation: Arc::clone(&dashboard_observation),
|
|
dashboard_summary_range: Arc::clone(&dashboard_summary_range),
|
|
account: Arc::clone(&account),
|
|
});
|
|
let plugin_ports = Arc::new(plugins::TestPluginPorts::default());
|
|
let published_snapshot = gateway_core::runtime::RuntimeSnapshotHandle::default();
|
|
let stores = AdminStorePorts::new(
|
|
AdminAccountStorePorts::new(
|
|
unused.clone(),
|
|
unused.clone(),
|
|
account_groups.clone(),
|
|
Arc::new(proxies::MemoryProxies::default()),
|
|
),
|
|
auth.clone(),
|
|
client_keys.clone(),
|
|
unused,
|
|
settings.clone(),
|
|
gateway_admin::ports::backup::BackupStorePorts::disabled(),
|
|
plugin_ports.clone(),
|
|
plugin_ports.clone(),
|
|
);
|
|
let providers: Vec<Arc<dyn ProviderAdmin>> = vec![
|
|
Arc::new(UnusedProvider::new("openai", Arc::clone(&provider_error))),
|
|
Arc::new(UnusedProvider::new("xai", Arc::clone(&provider_error))),
|
|
];
|
|
let bundle = gateway_admin::initialize(
|
|
AdminConfig {
|
|
session_ttl_minutes: 60,
|
|
default_username: "admin_1".to_owned(),
|
|
default_password: InitialAdminPassword::new("strong-admin-password"),
|
|
},
|
|
ClientConfig::default(),
|
|
stores,
|
|
gateway_admin::AdminRuntimePorts {
|
|
plugin_preparation: plugin_ports.clone(),
|
|
plugin_management: plugin_ports.clone(),
|
|
published_snapshot: published_snapshot.clone(),
|
|
plugin_inspector: plugin_ports.clone(),
|
|
plugin_distribution: plugin_ports.clone(),
|
|
pricing_source: Arc::new(StaticPricingSource),
|
|
providers: gateway_admin::ports::provider::ProviderAdminRegistry::new(providers)
|
|
.unwrap(),
|
|
snapshot: Arc::new(NoopSnapshot),
|
|
account_probe: Arc::new(NoopProbe),
|
|
proxy_probe: Arc::new(proxies::SuccessfulProbe),
|
|
client_distribution: Arc::new(StaticClientDistribution),
|
|
system,
|
|
client_key_verifier: verifier.unwrap_or_else(|| Arc::new(UnusedClientKeyVerifier)),
|
|
},
|
|
)
|
|
.await
|
|
.expect("initialize test admin services");
|
|
Self {
|
|
services: bundle.services(),
|
|
plugin_ports,
|
|
published_snapshot,
|
|
client_key,
|
|
observations,
|
|
auth,
|
|
settings,
|
|
usage_records,
|
|
usage_detail,
|
|
diagnostics,
|
|
ops_errors,
|
|
dashboard_observation,
|
|
dashboard_summary_range,
|
|
provider_error,
|
|
account,
|
|
}
|
|
}
|
|
|
|
pub fn state(&self) -> AdminTestState {
|
|
AdminTestState(self.services.clone())
|
|
}
|
|
}
|
|
|
|
struct StaticClientDistribution;
|
|
|
|
#[async_trait]
|
|
impl ClientDistributionResolver for StaticClientDistribution {
|
|
async fn resolve_codex_desktop_windows(&self, _: bool) -> CodexDesktopWindowsDownloads {
|
|
CodexDesktopWindowsDownloads {
|
|
resolved_at: Utc::now(),
|
|
cached: false,
|
|
warning: None,
|
|
packages: vec![ClientDownloadPackage {
|
|
architecture: ClientArchitecture::X64,
|
|
source: ClientDownloadSource::MicrosoftStore,
|
|
version: Some("26.825.6671.0".to_owned()),
|
|
file_name: "OpenAI.Codex_26.825.6671.0_x64__2p2nqsd0c76g0.msix".to_owned(),
|
|
size_bytes: Some(744_250_000),
|
|
download_url:
|
|
"https://dl.delivery.mp.microsoft.com/filestreamingservice/files/test"
|
|
.to_owned(),
|
|
expires_at: Some(Utc::now() + Duration::hours(1)),
|
|
}],
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(Clone)]
|
|
pub(super) struct AdminTestState(AdminServices);
|
|
|
|
impl SessionState for AdminTestState {
|
|
fn admin_services(&self) -> &AdminServices {
|
|
&self.0
|
|
}
|
|
}
|
|
|
|
pub(super) struct MemoryAuthStore {
|
|
pub(super) enabled: AtomicBool,
|
|
pub(super) unavailable: AtomicBool,
|
|
password_hash: Mutex<Option<String>>,
|
|
sessions: Mutex<BTreeMap<String, AuthSession>>,
|
|
audits: Mutex<Vec<AdminAuditEvent>>,
|
|
api_key: Arc<Mutex<Option<AdminApiKey>>>,
|
|
fail_audit: AtomicBool,
|
|
}
|
|
|
|
impl MemoryAuthStore {
|
|
fn new(api_key: Arc<Mutex<Option<AdminApiKey>>>) -> Self {
|
|
Self {
|
|
enabled: AtomicBool::new(false),
|
|
unavailable: AtomicBool::new(false),
|
|
password_hash: Mutex::new(None),
|
|
sessions: Mutex::new(BTreeMap::new()),
|
|
audits: Mutex::new(Vec::new()),
|
|
api_key,
|
|
fail_audit: AtomicBool::new(false),
|
|
}
|
|
}
|
|
|
|
pub fn insert_session(&self, session_id: &str) {
|
|
self.sessions.lock().expect("sessions").insert(
|
|
session_id.to_owned(),
|
|
AuthSession {
|
|
subject: gateway_admin::model::auth::SessionSubject::Admin {
|
|
credential_fingerprint: {
|
|
use base64::Engine as _;
|
|
use sha2::Digest as _;
|
|
base64::engine::general_purpose::URL_SAFE_NO_PAD.encode(
|
|
sha2::Sha256::digest(
|
|
self.password_hash
|
|
.lock()
|
|
.unwrap()
|
|
.as_ref()
|
|
.unwrap()
|
|
.as_bytes(),
|
|
),
|
|
)
|
|
},
|
|
admin_user_id: "admin_1".to_owned(),
|
|
},
|
|
expires_at: Utc::now() + Duration::hours(1),
|
|
},
|
|
);
|
|
}
|
|
|
|
pub fn set_api_key(&self, value: &str) {
|
|
*self.api_key.lock().expect("API key") = Some(AdminApiKey::new(value));
|
|
}
|
|
|
|
pub fn fail_audit(&self, fail: bool) {
|
|
self.fail_audit.store(fail, Ordering::SeqCst);
|
|
}
|
|
|
|
pub fn session_count(&self) -> usize {
|
|
self.sessions.lock().expect("sessions").len()
|
|
}
|
|
|
|
pub fn audit_count(&self) -> usize {
|
|
self.audits.lock().expect("audits").len()
|
|
}
|
|
}
|
|
|
|
#[async_trait]
|
|
impl AuthStore for MemoryAuthStore {
|
|
async fn load_password_hash(&self, _: &str) -> AdminStoreResult<Option<String>> {
|
|
Ok(self.password_hash.lock().expect("password hash").clone())
|
|
}
|
|
|
|
async fn change_password(
|
|
&self,
|
|
_: &str,
|
|
expected_hash: &str,
|
|
password_hash: &str,
|
|
audit: gateway_admin::model::auth::AdminAuditEvent,
|
|
) -> AdminStoreResult<bool> {
|
|
if self.fail_audit.load(Ordering::SeqCst) {
|
|
return Err(unavailable("audit"));
|
|
}
|
|
let mut stored = self.password_hash.lock().unwrap();
|
|
let Some(credentials) = stored
|
|
.as_mut()
|
|
.filter(|value| value.as_str() == expected_hash)
|
|
else {
|
|
return Ok(false);
|
|
};
|
|
*credentials = password_hash.to_owned();
|
|
self.audits.lock().unwrap().push(audit);
|
|
Ok(true)
|
|
}
|
|
|
|
async fn create_password_hash_if_absent(
|
|
&self,
|
|
_: &str,
|
|
password_hash: &str,
|
|
) -> AdminStoreResult<bool> {
|
|
let mut stored = self.password_hash.lock().expect("password hash");
|
|
if stored.is_some() {
|
|
return Ok(false);
|
|
}
|
|
*stored = Some(password_hash.to_owned());
|
|
Ok(true)
|
|
}
|
|
|
|
async fn load_admin_api_key(&self) -> AdminStoreResult<Option<AdminApiKey>> {
|
|
Ok(self.api_key.lock().expect("API key").clone())
|
|
}
|
|
|
|
async fn load_session(&self, session_id: &str) -> AdminStoreResult<Option<AuthSession>> {
|
|
Ok(self
|
|
.sessions
|
|
.lock()
|
|
.expect("sessions")
|
|
.get(session_id)
|
|
.cloned())
|
|
}
|
|
|
|
async fn store_session(&self, session_id: &str, session: &AuthSession) -> AdminStoreResult<()> {
|
|
self.sessions
|
|
.lock()
|
|
.expect("sessions")
|
|
.insert(session_id.to_owned(), session.clone());
|
|
Ok(())
|
|
}
|
|
|
|
async fn delete_session(&self, session_id: &str) -> AdminStoreResult<Option<AuthSession>> {
|
|
Ok(self.sessions.lock().expect("sessions").remove(session_id))
|
|
}
|
|
|
|
async fn client_key_enabled(
|
|
&self,
|
|
id: &gateway_core::policy::ClientApiKeyId,
|
|
) -> AdminStoreResult<bool> {
|
|
if self.unavailable.load(Ordering::SeqCst) {
|
|
return Err(unavailable("key status"));
|
|
}
|
|
Ok(id.as_str() == "key-42" && self.enabled.load(Ordering::SeqCst))
|
|
}
|
|
|
|
async fn consume_login_attempt(
|
|
&self,
|
|
_: std::net::IpAddr,
|
|
_: u32,
|
|
_: u32,
|
|
_: std::time::Duration,
|
|
) -> AdminStoreResult<Option<std::time::Duration>> {
|
|
Ok(None)
|
|
}
|
|
|
|
async fn append_audit_event(&self, event: AdminAuditEvent) -> AdminStoreResult<()> {
|
|
if self.fail_audit.load(Ordering::SeqCst) {
|
|
return Err(unavailable("auth audit"));
|
|
}
|
|
self.audits.lock().expect("audits").push(event);
|
|
Ok(())
|
|
}
|
|
}
|
|
|
|
pub(super) struct MemorySettingsStore {
|
|
pricing: Mutex<gateway_admin::model::pricing::StoredPricing>,
|
|
settings: Mutex<RuntimeSettings>,
|
|
api_key: Arc<Mutex<Option<AdminApiKey>>>,
|
|
}
|
|
|
|
struct StaticPricingSource;
|
|
#[async_trait]
|
|
impl gateway_admin::ports::pricing::PricingSource for StaticPricingSource {
|
|
async fn fetch(
|
|
&self,
|
|
) -> Result<gateway_admin::model::pricing::PricingSyncPreview, gateway_admin::model::AdminError>
|
|
{
|
|
Ok(gateway_admin::model::pricing::PricingSyncPreview {
|
|
prices: serde_json::from_value(serde_json::json!({"openai":{"gpt-5.4":{
|
|
"multiplierBps":10000,"bands":{"standard":{"input":"2.5","output":"15","cacheRead":"0.25","cacheWrite":"0"}}
|
|
}}})).expect("prices"), skipped: vec![],
|
|
})
|
|
}
|
|
}
|
|
|
|
impl MemorySettingsStore {
|
|
fn new(api_key: Arc<Mutex<Option<AdminApiKey>>>) -> Self {
|
|
Self {
|
|
settings: Mutex::new(test_runtime_settings()),
|
|
pricing: Mutex::default(),
|
|
api_key,
|
|
}
|
|
}
|
|
|
|
pub fn set_api_key(&self, value: &str) {
|
|
*self.api_key.lock().expect("API key") = Some(AdminApiKey::new(value));
|
|
}
|
|
}
|
|
|
|
#[async_trait]
|
|
impl SettingsStore for MemorySettingsStore {
|
|
async fn load_pricing(&self) -> AdminStoreResult<gateway_admin::model::pricing::StoredPricing> {
|
|
Ok(self.pricing.lock().expect("pricing").clone())
|
|
}
|
|
async fn sync_pricing(
|
|
&self,
|
|
changes: gateway_admin::model::pricing::PricingSyncChanges,
|
|
_: &MutationContext,
|
|
) -> AdminStoreResult<gateway_admin::model::Revision> {
|
|
let mut pricing = self.pricing.lock().expect("pricing");
|
|
for (provider, models) in changes {
|
|
let stored = pricing.synced.entry(provider).or_default();
|
|
for (model, price) in models {
|
|
if let Some(price) = price {
|
|
stored.insert(model, price);
|
|
} else {
|
|
stored.remove(&model);
|
|
}
|
|
}
|
|
}
|
|
pricing.synced.retain(|_, models| !models.is_empty());
|
|
pricing.synced_at = Some(Utc::now());
|
|
let mut settings = self.settings.lock().expect("settings");
|
|
settings.config_revision = next_revision(settings.config_revision);
|
|
Ok(settings.config_revision)
|
|
}
|
|
async fn update_pricing(
|
|
&self,
|
|
command: gateway_admin::model::pricing::UpdatePricing,
|
|
_: &MutationContext,
|
|
) -> AdminStoreResult<gateway_admin::model::Revision> {
|
|
use gateway_admin::model::pricing::PricingChange;
|
|
let mut pricing = self.pricing.lock().expect("pricing");
|
|
let gateway_admin::model::pricing::StoredPricing {
|
|
overrides, synced, ..
|
|
} = &mut *pricing;
|
|
let models = overrides.entry(command.provider.clone()).or_default();
|
|
let source = synced.entry(command.provider).or_default();
|
|
for model in command.models {
|
|
match &command.change {
|
|
PricingChange::Reset => {
|
|
models.remove(&model);
|
|
}
|
|
PricingChange::Delete => {
|
|
models.remove(&model);
|
|
source.remove(&model);
|
|
}
|
|
PricingChange::Replace(p) => {
|
|
models.insert(model, p.clone());
|
|
}
|
|
PricingChange::Multiplier(bps) => {
|
|
models
|
|
.entry(model)
|
|
.or_insert_with(|| gateway_core::metering::ModelPriceOverride {
|
|
multiplier_bps: 10_000,
|
|
bands: Default::default(),
|
|
})
|
|
.multiplier_bps = *bps;
|
|
}
|
|
}
|
|
}
|
|
let mut settings = self.settings.lock().expect("settings");
|
|
settings.config_revision = next_revision(settings.config_revision);
|
|
Ok(settings.config_revision)
|
|
}
|
|
async fn load_runtime_settings(&self) -> AdminStoreResult<RuntimeSettings> {
|
|
Ok(self.settings.lock().expect("settings").clone())
|
|
}
|
|
|
|
async fn admin_api_key_exists(&self) -> AdminStoreResult<bool> {
|
|
Ok(self.api_key.lock().expect("API key").is_some())
|
|
}
|
|
|
|
async fn replace_runtime_settings(
|
|
&self,
|
|
command: ReplaceRuntimeSettings,
|
|
_: &MutationContext,
|
|
) -> AdminStoreResult<RuntimeSettings> {
|
|
let mut settings = self.settings.lock().expect("settings");
|
|
let mut request_profiles = settings.request_profiles.clone();
|
|
for (provider, profile) in command.request_profile_updates {
|
|
if let Some(profile) = profile {
|
|
request_profiles.insert(provider, profile);
|
|
} else {
|
|
request_profiles.remove(&provider);
|
|
}
|
|
}
|
|
let updated = RuntimeSettings {
|
|
request_profiles,
|
|
request_location_enabled: command.request_location_enabled,
|
|
request_location: command.request_location,
|
|
config_revision: next_revision(settings.config_revision),
|
|
model_mappings: command.model_mappings,
|
|
refresh_margin_seconds: command.refresh_margin_seconds,
|
|
refresh_concurrency: command.refresh_concurrency,
|
|
max_concurrent_per_account: command.max_concurrent_per_account,
|
|
request_interval_ms: command.request_interval_ms,
|
|
max_waiting_per_key: command.max_waiting_per_key,
|
|
max_waiting_per_account: command.max_waiting_per_account,
|
|
concurrency_wait_timeout_seconds: command.concurrency_wait_timeout_seconds,
|
|
responses_max_decompressed_body_bytes: command.responses_max_decompressed_body_bytes,
|
|
rotation_strategy: command.rotation_strategy,
|
|
min_codex_desktop_version: command.min_codex_desktop_version,
|
|
min_codex_cli_version: command.min_codex_cli_version,
|
|
usage_retention_days: command.usage_retention_days,
|
|
ops_event_retention_days: command.ops_event_retention_days,
|
|
audit_retention_days: command.audit_retention_days,
|
|
account_auto_freeze_enabled: true,
|
|
account_auto_freeze_threshold: 12,
|
|
account_auto_freeze_window_seconds: 600,
|
|
account_auto_freeze_duration_seconds: 7_200,
|
|
account_auto_freeze_probe_enabled: true,
|
|
account_auto_freeze_probe_model: None,
|
|
account_auto_freeze_adaptive_concurrency: true,
|
|
account_warmup_enabled: false,
|
|
account_warmup_schedule_time: "08:00".to_owned(),
|
|
account_warmup_model: None,
|
|
updated_at: Utc::now(),
|
|
};
|
|
*settings = updated.clone();
|
|
Ok(updated)
|
|
}
|
|
|
|
async fn replace_admin_api_key(
|
|
&self,
|
|
key: AdminApiKey,
|
|
_: &MutationContext,
|
|
) -> AdminStoreResult<AdminApiKeyMutation> {
|
|
*self.api_key.lock().expect("API key") = Some(key);
|
|
let revision = self.settings.lock().expect("settings").config_revision;
|
|
Ok(AdminApiKeyMutation {
|
|
config_revision: next_revision(revision),
|
|
exists: true,
|
|
})
|
|
}
|
|
|
|
async fn delete_admin_api_key(
|
|
&self,
|
|
_: &MutationContext,
|
|
) -> AdminStoreResult<AdminApiKeyMutation> {
|
|
*self.api_key.lock().expect("API key") = None;
|
|
let revision = self.settings.lock().expect("settings").config_revision;
|
|
Ok(AdminApiKeyMutation {
|
|
config_revision: next_revision(revision),
|
|
exists: false,
|
|
})
|
|
}
|
|
}
|
|
|
|
pub(super) struct MemoryClientKeyStore(Arc<Mutex<Option<ClientKeyRecord>>>);
|
|
|
|
#[derive(Default)]
|
|
pub(super) struct MemoryObservations {
|
|
pub summary: Option<UsageOverview>,
|
|
pub trend: Option<Vec<RequestMetricPoint>>,
|
|
pub summaries: Vec<(TimeRange, UsageFilter)>,
|
|
pub trends: Vec<(TimeRange, UsageFilter)>,
|
|
pub records: Vec<UsageQuery>,
|
|
pub errors: Vec<OpsErrorQuery>,
|
|
}
|
|
|
|
pub(super) const PRIMARY_GROUP_ID: &str = "grp_11111111111111111111111111111111";
|
|
pub(super) const SECONDARY_GROUP_ID: &str = "grp_22222222222222222222222222222222";
|
|
|
|
struct MemoryAccountGroupState {
|
|
revision: Revision,
|
|
groups: BTreeMap<gateway_core::routing::AccountGroupId, AccountGroupRecord>,
|
|
}
|
|
|
|
pub(super) struct MemoryAccountGroupStore {
|
|
state: Mutex<MemoryAccountGroupState>,
|
|
}
|
|
|
|
impl MemoryAccountGroupStore {
|
|
fn new() -> Self {
|
|
let now = Utc::now();
|
|
let primary_id = group_id(PRIMARY_GROUP_ID);
|
|
let secondary_id = group_id(SECONDARY_GROUP_ID);
|
|
let groups = BTreeMap::from([
|
|
(
|
|
primary_id.clone(),
|
|
AccountGroupRecord {
|
|
disable_fast: false,
|
|
id: primary_id,
|
|
name: "Alpha routing".to_owned(),
|
|
description: Some("Primary traffic".to_owned()),
|
|
color: group_color("#2563ebff"),
|
|
enabled: true,
|
|
member_count: 2,
|
|
provider_counts: BTreeMap::from([
|
|
("openai".to_owned(), 1),
|
|
("xai".to_owned(), 1),
|
|
]),
|
|
client_key_count: 2,
|
|
account_summary: account_summary(1, 1, 2),
|
|
capacity: capacity(Some(0), 1),
|
|
usage: usage("1.25", "5.5"),
|
|
created_at: now,
|
|
updated_at: now,
|
|
},
|
|
),
|
|
(
|
|
secondary_id.clone(),
|
|
AccountGroupRecord {
|
|
disable_fast: false,
|
|
id: secondary_id,
|
|
name: "Beta routing".to_owned(),
|
|
description: None,
|
|
color: group_color("#64748B80"),
|
|
enabled: false,
|
|
member_count: 0,
|
|
provider_counts: BTreeMap::new(),
|
|
client_key_count: 0,
|
|
account_summary: account_summary(0, 0, 0),
|
|
capacity: capacity(Some(0), 0),
|
|
usage: usage("0", "0"),
|
|
created_at: now,
|
|
updated_at: now,
|
|
},
|
|
),
|
|
]);
|
|
Self {
|
|
state: Mutex::new(MemoryAccountGroupState {
|
|
revision: Revision::new(7).expect("revision"),
|
|
groups,
|
|
}),
|
|
}
|
|
}
|
|
}
|
|
|
|
#[async_trait]
|
|
impl AccountGroupStore for MemoryAccountGroupStore {
|
|
async fn list_account_groups(
|
|
&self,
|
|
query: AccountGroupListQuery,
|
|
) -> AdminStoreResult<AccountGroupPage> {
|
|
let state = self.state.lock().expect("account groups");
|
|
let search = query.search.as_ref().map(|value| value.to_lowercase());
|
|
let mut matching = state
|
|
.groups
|
|
.values()
|
|
.filter(|record| {
|
|
query
|
|
.enabled
|
|
.is_none_or(|enabled| record.enabled == enabled)
|
|
})
|
|
.filter(|record| {
|
|
search.as_ref().is_none_or(|search| {
|
|
record.name.to_lowercase().contains(search)
|
|
|| record
|
|
.description
|
|
.as_ref()
|
|
.is_some_and(|description| description.to_lowercase().contains(search))
|
|
})
|
|
})
|
|
.cloned()
|
|
.collect::<Vec<_>>();
|
|
matching.sort_by(|left, right| left.name.cmp(&right.name));
|
|
let total = matching.len() as u64;
|
|
let page_size = usize::from(query.page_size.get());
|
|
let offset = usize::try_from(query.page.saturating_sub(1))
|
|
.unwrap_or(usize::MAX)
|
|
.saturating_mul(page_size);
|
|
let items = matching.into_iter().skip(offset).take(page_size).collect();
|
|
Ok(AccountGroupPage {
|
|
config_revision: state.revision,
|
|
items,
|
|
total,
|
|
page: query.page,
|
|
page_size: query.page_size.get(),
|
|
})
|
|
}
|
|
|
|
async fn load_account_group_members(
|
|
&self,
|
|
group_ids: &[gateway_core::routing::AccountGroupId],
|
|
) -> AdminStoreResult<Vec<AccountGroupMemberFact>> {
|
|
if !group_ids.iter().any(|id| id.as_str() == PRIMARY_GROUP_ID) {
|
|
return Ok(Vec::new());
|
|
}
|
|
Ok(vec![
|
|
AccountGroupMemberFact {
|
|
group_id: group_id(PRIMARY_GROUP_ID),
|
|
account_id: "acct_group_ready".to_owned(),
|
|
status: AccountStatusFacts {
|
|
enabled: true,
|
|
credential_state: CredentialState::Ready,
|
|
access_token_expires_at: None,
|
|
quota: QuotaState::default(),
|
|
cooldown: None,
|
|
last_error_reason: None,
|
|
last_error_message: None,
|
|
},
|
|
total_slots: Some(1),
|
|
},
|
|
AccountGroupMemberFact {
|
|
group_id: group_id(PRIMARY_GROUP_ID),
|
|
account_id: "acct_group_disabled".to_owned(),
|
|
status: AccountStatusFacts {
|
|
enabled: false,
|
|
credential_state: CredentialState::Ready,
|
|
access_token_expires_at: None,
|
|
quota: QuotaState::default(),
|
|
cooldown: None,
|
|
last_error_reason: None,
|
|
last_error_message: None,
|
|
},
|
|
total_slots: Some(1),
|
|
},
|
|
])
|
|
}
|
|
|
|
async fn create_account_group(
|
|
&self,
|
|
command: NewAccountGroup,
|
|
_: &MutationContext,
|
|
) -> AdminStoreResult<AccountGroupMutation> {
|
|
let mut state = self.state.lock().expect("account groups");
|
|
let now = Utc::now();
|
|
let record = AccountGroupRecord {
|
|
disable_fast: command.disable_fast,
|
|
id: command.id.clone(),
|
|
name: command.name,
|
|
description: command.description,
|
|
color: command.color,
|
|
enabled: true,
|
|
member_count: 0,
|
|
provider_counts: BTreeMap::new(),
|
|
client_key_count: 0,
|
|
account_summary: account_summary(0, 0, 0),
|
|
capacity: capacity(Some(0), 0),
|
|
usage: usage("0", "0"),
|
|
created_at: now,
|
|
updated_at: now,
|
|
};
|
|
state.groups.insert(command.id.clone(), record);
|
|
mutation(&mut state, command.id, true)
|
|
}
|
|
|
|
async fn update_account_group(
|
|
&self,
|
|
command: UpdateAccountGroup,
|
|
_: &MutationContext,
|
|
) -> AdminStoreResult<AccountGroupMutation> {
|
|
let mut state = self.state.lock().expect("account groups");
|
|
let record = state
|
|
.groups
|
|
.get_mut(&command.id)
|
|
.ok_or_else(|| not_found("account group"))?;
|
|
record.name = command.name;
|
|
record.description = command.description;
|
|
record.color = command.color;
|
|
if let Some(disable_fast) = command.disable_fast {
|
|
record.disable_fast = disable_fast;
|
|
}
|
|
record.updated_at = Utc::now();
|
|
mutation(&mut state, command.id, true)
|
|
}
|
|
|
|
async fn set_account_group_enabled(
|
|
&self,
|
|
command: SetAccountGroupEnabled,
|
|
_: &MutationContext,
|
|
) -> AdminStoreResult<AccountGroupMutation> {
|
|
let mut state = self.state.lock().expect("account groups");
|
|
let record = state
|
|
.groups
|
|
.get_mut(&command.id)
|
|
.ok_or_else(|| not_found("account group"))?;
|
|
record.enabled = command.enabled;
|
|
record.updated_at = Utc::now();
|
|
mutation(&mut state, command.id, true)
|
|
}
|
|
|
|
async fn delete_account_group(
|
|
&self,
|
|
command: DeleteAccountGroup,
|
|
_: &MutationContext,
|
|
) -> AdminStoreResult<AccountGroupMutation> {
|
|
let mut state = self.state.lock().expect("account groups");
|
|
if state.groups.remove(&command.id).is_none() {
|
|
return Err(not_found("account group"));
|
|
}
|
|
mutation(&mut state, command.id, false)
|
|
}
|
|
}
|
|
|
|
fn group_id(value: &str) -> gateway_core::routing::AccountGroupId {
|
|
gateway_core::routing::AccountGroupId::new(value).expect("account group ID")
|
|
}
|
|
|
|
fn mutation(
|
|
state: &mut MemoryAccountGroupState,
|
|
id: gateway_core::routing::AccountGroupId,
|
|
include_record: bool,
|
|
) -> AdminStoreResult<AccountGroupMutation> {
|
|
state.revision = next_revision(state.revision);
|
|
Ok(AccountGroupMutation {
|
|
config_revision: state.revision,
|
|
record: include_record.then(|| state.groups.get(&id).expect("group record").clone()),
|
|
id,
|
|
})
|
|
}
|
|
|
|
#[async_trait]
|
|
impl ClientKeyStore for MemoryClientKeyStore {
|
|
async fn reset_client_key_budget(
|
|
&self,
|
|
command: gateway_admin::model::client_keys::ResetClientKeyBudget,
|
|
_: &MutationContext,
|
|
) -> AdminStoreResult<()> {
|
|
use gateway_admin::model::client_keys::ClientKeyBudgetPeriod;
|
|
let mut record = self.0.lock().unwrap();
|
|
let record = record
|
|
.as_mut()
|
|
.filter(|record| record.id == command.id)
|
|
.ok_or_else(|| {
|
|
AdminStoreError::new(AdminStoreErrorKind::NotFound, "client key", "missing key")
|
|
})?;
|
|
if matches!(
|
|
command.period,
|
|
ClientKeyBudgetPeriod::Daily | ClientKeyBudgetPeriod::All
|
|
) {
|
|
record.budget.daily_used_usd = gateway_core::metering::Decimal::ZERO;
|
|
}
|
|
if matches!(
|
|
command.period,
|
|
ClientKeyBudgetPeriod::Weekly | ClientKeyBudgetPeriod::All
|
|
) {
|
|
record.budget.weekly_used_usd = gateway_core::metering::Decimal::ZERO;
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
async fn get_client_key(
|
|
&self,
|
|
id: &ClientApiKeyId,
|
|
) -> AdminStoreResult<Option<ClientKeyRecord>> {
|
|
Ok(self
|
|
.0
|
|
.lock()
|
|
.expect("client key")
|
|
.clone()
|
|
.filter(|key| &key.id == id))
|
|
}
|
|
|
|
async fn list_client_keys(&self, _: ClientKeyListQuery) -> AdminStoreResult<ClientKeyPage> {
|
|
Ok(ClientKeyPage {
|
|
config_revision: Revision::new(1).expect("revision"),
|
|
items: Vec::new(),
|
|
total: 0,
|
|
next_cursor: None,
|
|
})
|
|
}
|
|
|
|
async fn reveal_client_key(
|
|
&self,
|
|
id: &ClientApiKeyId,
|
|
) -> AdminStoreResult<Option<ClientKeySecret>> {
|
|
if let Some(record) = self.0.lock().expect("client key").clone() {
|
|
return Ok((record.id == *id)
|
|
.then(|| ClientKeySecret::new(record, format!("sk_{}", "a".repeat(43)))));
|
|
}
|
|
let now = Utc::now();
|
|
Ok(Some(ClientKeySecret::new(
|
|
ClientKeyRecord {
|
|
request_profile_overrides: Default::default(),
|
|
budget: Default::default(),
|
|
id: id.clone(),
|
|
name: "revealed".to_owned(),
|
|
label: None,
|
|
groups: Vec::new(),
|
|
provider_kinds: vec![ProviderKind::new("openai").expect("provider kind")],
|
|
prefix: "sk_aaaaaaaaa".to_owned(),
|
|
enabled: true,
|
|
limits: RateLimits::unlimited(),
|
|
last_used_at: None,
|
|
created_at: now,
|
|
updated_at: now,
|
|
},
|
|
format!("sk_{}", "a".repeat(43)),
|
|
)))
|
|
}
|
|
|
|
async fn create_client_key(
|
|
&self,
|
|
_: NewClientKey,
|
|
_: &MutationContext,
|
|
) -> AdminStoreResult<(Revision, ClientKeyRecord)> {
|
|
Err(unavailable("client key create"))
|
|
}
|
|
|
|
async fn update_client_key(
|
|
&self,
|
|
_: UpdateClientKey,
|
|
_: &MutationContext,
|
|
) -> AdminStoreResult<(Revision, ClientKeyRecord)> {
|
|
Err(unavailable("client key update"))
|
|
}
|
|
|
|
async fn set_client_key_enabled(
|
|
&self,
|
|
_: SetClientKeyEnabled,
|
|
_: &MutationContext,
|
|
) -> AdminStoreResult<(Revision, ClientKeyRecord)> {
|
|
Err(unavailable("client key enabled"))
|
|
}
|
|
|
|
async fn delete_client_key(
|
|
&self,
|
|
_: DeleteClientKey,
|
|
_: &MutationContext,
|
|
) -> AdminStoreResult<Revision> {
|
|
Err(unavailable("client key delete"))
|
|
}
|
|
}
|
|
|
|
struct UnusedStore {
|
|
observations: Arc<Mutex<MemoryObservations>>,
|
|
usage_records: Arc<Mutex<Vec<UsageListRecord>>>,
|
|
usage_detail: Arc<Mutex<Option<UsageDetail>>>,
|
|
diagnostics: Arc<Mutex<Vec<DiagnosticObservation>>>,
|
|
ops_errors: Arc<Mutex<Vec<OpsError>>>,
|
|
dashboard_observation: Arc<Mutex<Option<DashboardObservation>>>,
|
|
dashboard_summary_range: Arc<Mutex<Option<TimeRange>>>,
|
|
account: Arc<Mutex<Option<AccountPageItem>>>,
|
|
}
|
|
|
|
struct UnusedClientKeyVerifier;
|
|
|
|
impl ClientKeyVerifier for UnusedClientKeyVerifier {
|
|
fn verify_client_key(&self, _: &str) -> Result<ClientApiKeyId, ClientAuthenticationError> {
|
|
Err(ClientAuthenticationError::InvalidKey)
|
|
}
|
|
}
|
|
|
|
#[async_trait]
|
|
impl AccountStore for UnusedStore {
|
|
async fn list_plugin_accounts(
|
|
&self,
|
|
_: gateway_admin::model::provider_credentials::PluginAccountListQuery,
|
|
) -> AdminStoreResult<gateway_admin::model::provider_credentials::PluginAccountPage> {
|
|
Err(unavailable("plugin account list"))
|
|
}
|
|
|
|
async fn list_accounts(
|
|
&self,
|
|
_: AccountListQuery,
|
|
_: AccountRuntimeSnapshot,
|
|
) -> AdminStoreResult<AccountPage> {
|
|
Err(unavailable("account list"))
|
|
}
|
|
|
|
async fn load_account(
|
|
&self,
|
|
id: &str,
|
|
_: AccountRuntimeSnapshot,
|
|
) -> AdminStoreResult<Option<AccountPageItem>> {
|
|
if let Some(account) = self.account.lock().expect("account").as_ref() {
|
|
return Ok((account.account.id == id).then(|| account.clone()));
|
|
}
|
|
Err(unavailable("account"))
|
|
}
|
|
|
|
async fn load_account_usage(
|
|
&self,
|
|
_: TimeRange,
|
|
_: &[String],
|
|
) -> AdminStoreResult<Vec<AccountUsage>> {
|
|
Err(unavailable("account usage"))
|
|
}
|
|
|
|
async fn load_account_usage_by_windows(
|
|
&self,
|
|
_: &[AccountUsageWindowQuery],
|
|
) -> AdminStoreResult<Vec<AccountUsageWindowResult>> {
|
|
Err(unavailable("account quota window usage"))
|
|
}
|
|
|
|
async fn load_quota_forecast_history(
|
|
&self,
|
|
_: &AccountUsageWindowQuery,
|
|
) -> AdminStoreResult<gateway_admin::model::quota_forecast_sampling::QuotaForecastHistory> {
|
|
Err(unavailable("quota forecast history"))
|
|
}
|
|
|
|
async fn credential_details(
|
|
&self,
|
|
_: &ProviderKind,
|
|
_: &ProviderAccountId,
|
|
) -> AdminStoreResult<Option<CredentialDetails>> {
|
|
Err(unavailable("credential"))
|
|
}
|
|
|
|
async fn credential_details_by_id(
|
|
&self,
|
|
_: &ProviderAccountId,
|
|
) -> AdminStoreResult<Option<CredentialDetails>> {
|
|
Err(unavailable("plugin credential"))
|
|
}
|
|
|
|
async fn load_credentials_for_export(
|
|
&self,
|
|
_: &ProviderKind,
|
|
_: &[ProviderAccountId],
|
|
) -> AdminStoreResult<Vec<ProviderExportCredentialInput>> {
|
|
Err(unavailable("credential export"))
|
|
}
|
|
|
|
async fn load_credential_for_plugin(
|
|
&self,
|
|
_: &ProviderAccountId,
|
|
) -> AdminStoreResult<Option<ProviderExportCredentialInput>> {
|
|
Err(unavailable("plugin credential export"))
|
|
}
|
|
|
|
async fn commit_credential_import(
|
|
&self,
|
|
_: CredentialImportCommit,
|
|
_: &MutationContext,
|
|
) -> AdminStoreResult<CredentialImportResult> {
|
|
Err(unavailable("credential import"))
|
|
}
|
|
|
|
async fn authorization_receipt(
|
|
&self,
|
|
_: &gateway_admin::model::provider_credentials::AuthorizationReceiptKey,
|
|
) -> AdminStoreResult<
|
|
Option<gateway_admin::model::provider_credentials::CredentialMutationResult>,
|
|
> {
|
|
Ok(None)
|
|
}
|
|
|
|
async fn commit_authorization(
|
|
&self,
|
|
_: AuthorizationCommit,
|
|
_: &MutationContext,
|
|
) -> AdminStoreResult<gateway_admin::model::provider_credentials::AuthorizationCommitResult>
|
|
{
|
|
Err(unavailable("authorization"))
|
|
}
|
|
|
|
async fn commit_credential_rotation(
|
|
&self,
|
|
_: CredentialRotationCommit,
|
|
_: &MutationContext,
|
|
) -> AdminStoreResult<CredentialMutationResult> {
|
|
Err(unavailable("credential rotation"))
|
|
}
|
|
|
|
async fn commit_credential_refresh(
|
|
&self,
|
|
_: CredentialRotationCommit,
|
|
_: &MutationContext,
|
|
) -> AdminStoreResult<CredentialMutationResult> {
|
|
Err(unavailable("credential refresh"))
|
|
}
|
|
|
|
async fn update_account(
|
|
&self,
|
|
_: UpdateAccount,
|
|
_: &MutationContext,
|
|
) -> AdminStoreResult<AccountUpdateResult> {
|
|
Err(unavailable("account enabled"))
|
|
}
|
|
|
|
async fn lower_concurrency_limit(
|
|
&self,
|
|
_: &gateway_core::account::ProviderAccountId,
|
|
_: gateway_core::account::AccountConcurrencyLimit,
|
|
_: &MutationContext,
|
|
) -> AdminStoreResult<Option<gateway_admin::model::accounts::AccountUpdateResult>> {
|
|
Ok(None)
|
|
}
|
|
|
|
async fn recover_account(
|
|
&self,
|
|
_: &ProviderAccountId,
|
|
_: &MutationContext,
|
|
) -> AdminStoreResult<AccountUpdateResult> {
|
|
Err(unavailable("account recovery"))
|
|
}
|
|
|
|
async fn batch_update_accounts(
|
|
&self,
|
|
_: BatchUpdateAccounts,
|
|
_: &MutationContext,
|
|
) -> AdminStoreResult<AccountsUpdateResult> {
|
|
Err(unavailable("account batch update"))
|
|
}
|
|
|
|
async fn delete_accounts(
|
|
&self,
|
|
_: DeleteAccounts,
|
|
_: &MutationContext,
|
|
) -> AdminStoreResult<Revision> {
|
|
Err(unavailable("account delete"))
|
|
}
|
|
|
|
async fn record_credential_export(
|
|
&self,
|
|
_: &[ProviderAccountId],
|
|
_: &MutationContext,
|
|
) -> AdminStoreResult<()> {
|
|
Err(unavailable("credential export audit"))
|
|
}
|
|
}
|
|
|
|
#[async_trait]
|
|
impl AccountRuntimeStore for UnusedStore {
|
|
async fn active_rate_limits(&self) -> AdminStoreResult<AccountRuntimeSnapshot> {
|
|
Ok(AccountRuntimeSnapshot {
|
|
cooldown: BTreeMap::new(),
|
|
in_flight: Some(BTreeMap::new()),
|
|
})
|
|
}
|
|
|
|
async fn account_runtime(&self, _: &[String]) -> AdminStoreResult<AccountRuntimeSnapshot> {
|
|
Ok(AccountRuntimeSnapshot {
|
|
cooldown: BTreeMap::new(),
|
|
in_flight: Some(BTreeMap::new()),
|
|
})
|
|
}
|
|
|
|
async fn active_freezes(
|
|
&self,
|
|
) -> AdminStoreResult<BTreeMap<String, gateway_admin::model::accounts::AccountFreeze>> {
|
|
Ok(BTreeMap::new())
|
|
}
|
|
|
|
async fn capacity_peaks(
|
|
&self,
|
|
_account_ids: &[String],
|
|
) -> AdminStoreResult<BTreeMap<String, u32>> {
|
|
Ok(BTreeMap::new())
|
|
}
|
|
|
|
async fn finish_freeze(
|
|
&self,
|
|
_account_id: &str,
|
|
_expected: &gateway_admin::model::accounts::AccountFreeze,
|
|
_postpone_until: Option<DateTime<Utc>>,
|
|
) -> AdminStoreResult<bool> {
|
|
Ok(false)
|
|
}
|
|
}
|
|
|
|
#[async_trait]
|
|
impl ObservabilityStore for UnusedStore {
|
|
async fn dashboard_summary(
|
|
&self,
|
|
range: TimeRange,
|
|
_: DateTime<Utc>,
|
|
) -> AdminStoreResult<DashboardObservation> {
|
|
*self
|
|
.dashboard_summary_range
|
|
.lock()
|
|
.expect("dashboard summary range") = Some(range);
|
|
self.dashboard_observation
|
|
.lock()
|
|
.expect("dashboard observation")
|
|
.clone()
|
|
.ok_or_else(|| unavailable("dashboard"))
|
|
}
|
|
|
|
async fn dashboard_trend(&self, _: TimeRange) -> AdminStoreResult<Vec<RequestMetricPoint>> {
|
|
Err(unavailable("dashboard trend"))
|
|
}
|
|
|
|
async fn usage_trend(
|
|
&self,
|
|
range: TimeRange,
|
|
filter: UsageFilter,
|
|
) -> AdminStoreResult<Vec<RequestMetricPoint>> {
|
|
let mut data = self.observations.lock().expect("observations");
|
|
data.trends.push((range, filter));
|
|
data.trend.clone().ok_or_else(|| unavailable("usage trend"))
|
|
}
|
|
|
|
fn usage_calculated_billing_facts(
|
|
&self,
|
|
_: TimeRange,
|
|
_: UsageFilter,
|
|
) -> gateway_admin::ports::store::UsageCalculatedBillingStream<'_> {
|
|
Box::pin(futures::stream::once(async {
|
|
Err(unavailable("usage billing facts"))
|
|
}))
|
|
}
|
|
|
|
async fn list_usage_records(&self, query: UsageQuery) -> AdminStoreResult<UsagePage> {
|
|
self.observations
|
|
.lock()
|
|
.expect("observations")
|
|
.records
|
|
.push(query.clone());
|
|
let items = self.usage_records.lock().expect("usage records").clone();
|
|
Ok(UsagePage {
|
|
current_page: query.current_page,
|
|
page_size: query.page_size.get(),
|
|
total: items.len() as u64,
|
|
items,
|
|
})
|
|
}
|
|
|
|
async fn usage_record_detail(&self, _: &str) -> AdminStoreResult<UsageDetail> {
|
|
self.usage_detail
|
|
.lock()
|
|
.expect("usage detail")
|
|
.clone()
|
|
.ok_or_else(|| unavailable("usage detail"))
|
|
}
|
|
|
|
async fn usage_summary(
|
|
&self,
|
|
range: TimeRange,
|
|
filter: UsageFilter,
|
|
) -> AdminStoreResult<UsageOverview> {
|
|
let mut data = self.observations.lock().expect("observations");
|
|
data.summaries.push((range, filter));
|
|
data.summary
|
|
.clone()
|
|
.map(|summary| UsageOverview { range, ..summary })
|
|
.ok_or_else(|| unavailable("usage summary"))
|
|
}
|
|
|
|
async fn usage_diagnostics(
|
|
&self,
|
|
_: TimeRange,
|
|
_: UsageFilter,
|
|
_: DiagnosticDimension,
|
|
) -> AdminStoreResult<Vec<DiagnosticObservation>> {
|
|
Ok(self.diagnostics.lock().expect("diagnostics").clone())
|
|
}
|
|
|
|
async fn list_ops_errors(&self, query: OpsErrorQuery) -> AdminStoreResult<OpsErrorPage> {
|
|
self.observations
|
|
.lock()
|
|
.expect("observations")
|
|
.errors
|
|
.push(query.clone());
|
|
let items = self.ops_errors.lock().expect("ops errors").clone();
|
|
Ok(OpsErrorPage {
|
|
current_page: query.current_page,
|
|
page_size: query.page_size.get(),
|
|
total: items.len() as u64,
|
|
items,
|
|
})
|
|
}
|
|
}
|
|
|
|
struct UnusedProvider {
|
|
kind: ProviderKind,
|
|
error: Arc<Mutex<Option<ProviderAdminError>>>,
|
|
}
|
|
|
|
impl UnusedProvider {
|
|
fn new(kind: &str, error: Arc<Mutex<Option<ProviderAdminError>>>) -> Self {
|
|
Self {
|
|
kind: ProviderKind::new(kind).expect("provider kind"),
|
|
error,
|
|
}
|
|
}
|
|
}
|
|
|
|
#[async_trait]
|
|
impl ProviderAdmin for UnusedProvider {
|
|
fn pricing_catalog(&self) -> gateway_admin::model::pricing::ProviderPricingCatalog {
|
|
serde_json::from_value(serde_json::json!({
|
|
"builtin-model": {
|
|
"multiplierBps": 10000,
|
|
"bands": {"standard": {"input": "1", "output": "2", "cacheRead": "0", "cacheWrite": "1"}}
|
|
}
|
|
})).unwrap()
|
|
}
|
|
|
|
fn provider_kind(&self) -> &ProviderKind {
|
|
&self.kind
|
|
}
|
|
|
|
async fn account_unavailable(&self, _: &ProviderAccountId) {}
|
|
|
|
async fn connection_test_operation(
|
|
&self,
|
|
_: &gateway_core::routing::UpstreamModelId,
|
|
_: &str,
|
|
) -> Result<gateway_core::operation::Operation, ProviderAdminError> {
|
|
Err(unsupported_provider())
|
|
}
|
|
|
|
fn dashboard_wire_profile(
|
|
&self,
|
|
) -> Option<gateway_admin::model::observability::DashboardWireProfile> {
|
|
None
|
|
}
|
|
|
|
fn calculated_billing(
|
|
&self,
|
|
_: &gateway_admin::model::observability::ProviderBillingInput,
|
|
) -> Result<
|
|
Option<gateway_admin::model::observability::CalculatedBillingBreakdown>,
|
|
ProviderAdminError,
|
|
> {
|
|
Ok(None)
|
|
}
|
|
|
|
async fn prepare_import(
|
|
&self,
|
|
_: PrepareCredentialImport,
|
|
) -> Result<PreparedCredentialImport, ProviderAdminError> {
|
|
Err(self
|
|
.error
|
|
.lock()
|
|
.expect("provider error")
|
|
.clone()
|
|
.unwrap_or_else(unsupported_provider))
|
|
}
|
|
|
|
async fn start_authorization(
|
|
&self,
|
|
_: gateway_admin::model::provider_credentials::PendingAuthorizationMutation,
|
|
) -> Result<AuthorizationStarted, ProviderAdminError> {
|
|
Err(unsupported_provider())
|
|
}
|
|
|
|
async fn complete_authorization(
|
|
&self,
|
|
_: CompleteAuthorization,
|
|
) -> Result<PreparedAuthorizationCommit, ProviderAdminError> {
|
|
Err(unsupported_provider())
|
|
}
|
|
|
|
async fn prepare_rotation(
|
|
&self,
|
|
_: PrepareCredentialRotation,
|
|
) -> Result<PreparedCredentialRotation, ProviderAdminError> {
|
|
Err(unsupported_provider())
|
|
}
|
|
|
|
async fn prepare_refresh(
|
|
&self,
|
|
_: PrepareCredentialRefresh,
|
|
) -> Result<PreparedCredentialRotation, ProviderAdminError> {
|
|
Err(self
|
|
.error
|
|
.lock()
|
|
.expect("provider error")
|
|
.clone()
|
|
.unwrap_or_else(unsupported_provider))
|
|
}
|
|
|
|
async fn quota(
|
|
&self,
|
|
_: gateway_admin::model::provider_credentials::ProviderQuotaRequest,
|
|
) -> Result<ProviderQuota, ProviderAdminError> {
|
|
Err(unsupported_provider())
|
|
}
|
|
|
|
async fn models(
|
|
&self,
|
|
_: &ProviderAccountId,
|
|
_: bool,
|
|
) -> Result<ProviderModels, ProviderAdminError> {
|
|
Err(unsupported_provider())
|
|
}
|
|
|
|
async fn export_credentials(
|
|
&self,
|
|
_: Vec<ProviderExportCredentialInput>,
|
|
) -> Result<ProviderExport, ProviderAdminError> {
|
|
Err(unsupported_provider())
|
|
}
|
|
}
|
|
|
|
struct NoopSnapshot;
|
|
|
|
impl SnapshotControl for NoopSnapshot {
|
|
fn publish_committed(&self, _: ConfigRevision) -> BoxFuture<'_, ()> {
|
|
Box::pin(async {})
|
|
}
|
|
}
|
|
|
|
struct NoopProbe;
|
|
|
|
impl AccountProbe for NoopProbe {
|
|
fn probe(
|
|
&self,
|
|
_: AccountProbeRequest,
|
|
_: Option<Arc<gateway_core::routing::RuntimeSnapshot>>,
|
|
) -> BoxFuture<'_, Result<AccountProbeResult, AccountProbeError>> {
|
|
Box::pin(async { panic!("unused account probe") })
|
|
}
|
|
}
|
|
|
|
struct UnusedSystem;
|
|
|
|
#[async_trait]
|
|
impl SystemOperations for UnusedSystem {
|
|
async fn version(&self) -> Result<SystemVersion, SystemOperationError> {
|
|
Err(unavailable_system())
|
|
}
|
|
|
|
async fn update_detail(&self, _: bool) -> Result<SystemUpdateDetail, SystemOperationError> {
|
|
Err(unavailable_system())
|
|
}
|
|
|
|
fn update_events(&self) -> SystemUpdateEventStream {
|
|
Box::pin(futures::stream::empty())
|
|
}
|
|
|
|
async fn perform_update(
|
|
&self,
|
|
_: Option<String>,
|
|
_: Arc<dyn gateway_admin::ports::system::SystemUpdatePreflight>,
|
|
) -> Result<SystemOperationAccepted, SystemOperationError> {
|
|
Err(unavailable_system())
|
|
}
|
|
|
|
async fn update_status(&self) -> Result<SystemUpdateStatus, SystemOperationError> {
|
|
Err(unavailable_system())
|
|
}
|
|
|
|
async fn rollback(
|
|
&self,
|
|
_: Arc<dyn gateway_admin::ports::system::SystemUpdatePreflight>,
|
|
) -> Result<SystemOperationAccepted, SystemOperationError> {
|
|
Err(unavailable_system())
|
|
}
|
|
|
|
async fn restart(&self) -> Result<SystemOperationAccepted, SystemOperationError> {
|
|
Err(unavailable_system())
|
|
}
|
|
}
|
|
|
|
fn test_runtime_settings() -> RuntimeSettings {
|
|
let mappings: ModelMappings = BTreeMap::from([
|
|
(
|
|
PublicModelId::new("coding-default").expect("public model"),
|
|
UpstreamModelId::new("gpt-5.4").expect("upstream model"),
|
|
),
|
|
(
|
|
PublicModelId::new("grok-latest").expect("public model"),
|
|
UpstreamModelId::new("grok-4.5").expect("upstream model"),
|
|
),
|
|
]);
|
|
RuntimeSettings {
|
|
request_profiles: Default::default(),
|
|
request_location_enabled: false,
|
|
request_location: Default::default(),
|
|
config_revision: Revision::new(7).expect("revision"),
|
|
model_mappings: mappings,
|
|
refresh_margin_seconds: 3_600,
|
|
refresh_concurrency: 2,
|
|
max_concurrent_per_account: 3,
|
|
request_interval_ms: 50,
|
|
max_waiting_per_key: 0,
|
|
max_waiting_per_account: 0,
|
|
concurrency_wait_timeout_seconds: 30,
|
|
responses_max_decompressed_body_bytes: 64 * 1024 * 1024,
|
|
rotation_strategy: RotationStrategy::Smart,
|
|
min_codex_desktop_version: None,
|
|
min_codex_cli_version: None,
|
|
usage_retention_days: 31,
|
|
ops_event_retention_days: 30,
|
|
audit_retention_days: 90,
|
|
account_auto_freeze_enabled: true,
|
|
account_auto_freeze_threshold: 12,
|
|
account_auto_freeze_window_seconds: 600,
|
|
account_auto_freeze_duration_seconds: 7_200,
|
|
account_auto_freeze_probe_enabled: true,
|
|
account_auto_freeze_probe_model: None,
|
|
account_auto_freeze_adaptive_concurrency: true,
|
|
account_warmup_enabled: false,
|
|
account_warmup_schedule_time: "08:00".to_owned(),
|
|
account_warmup_model: None,
|
|
updated_at: Utc::now(),
|
|
}
|
|
}
|
|
|
|
fn next_revision(revision: Revision) -> Revision {
|
|
Revision::new(revision.get().saturating_add(1)).expect("next revision")
|
|
}
|
|
|
|
fn account_summary(available: u64, limited: u64, total: u64) -> AccountGroupAccountSummary {
|
|
AccountGroupAccountSummary {
|
|
available,
|
|
limited,
|
|
total,
|
|
}
|
|
}
|
|
|
|
fn group_color(value: &str) -> AccountGroupColor {
|
|
AccountGroupColor::parse(value).expect("group color")
|
|
}
|
|
|
|
fn capacity(used_slots: Option<u64>, total_slots: u64) -> AccountGroupCapacity {
|
|
AccountGroupCapacity {
|
|
used_slots,
|
|
total_slots: Some(total_slots),
|
|
}
|
|
}
|
|
|
|
fn usage(today_usd: &str, retained_total_usd: &str) -> AccountGroupUsage {
|
|
AccountGroupUsage {
|
|
today_usd: today_usd.parse::<DecimalAmount>().expect("today cost"),
|
|
retained_total_usd: retained_total_usd
|
|
.parse::<DecimalAmount>()
|
|
.expect("retained total cost"),
|
|
}
|
|
}
|
|
|
|
fn unavailable(resource: &'static str) -> AdminStoreError {
|
|
AdminStoreError::new(
|
|
AdminStoreErrorKind::Unavailable,
|
|
resource,
|
|
"unused test port",
|
|
)
|
|
}
|
|
|
|
fn not_found(resource: &'static str) -> AdminStoreError {
|
|
AdminStoreError::new(AdminStoreErrorKind::NotFound, resource, "not found")
|
|
}
|
|
|
|
fn unsupported_provider() -> ProviderAdminError {
|
|
ProviderAdminError::new(ProviderAdminErrorKind::Unsupported)
|
|
}
|
|
|
|
fn unavailable_system() -> SystemOperationError {
|
|
SystemOperationError::new(SystemOperationErrorKind::Internal, "unused test operation")
|
|
}
|