refactor: 大规模模块拆分与重组,新增 aether-admin crate

- 新建独立 aether-admin crate 承载 admin 相关共享契约与纯辅助函数
- 拆分 ai_pipeline 下 kiro/private_envelope/conversion/planner 等大文件为子模块目录
- 重组 admin handlers 各业务域(billing/oauth/provider/system/users 等)为目录结构,移除 shared.rs/builders.rs 等反模式
- 移除 ai_pipeline runtime adapters 旧实现(claude/openai/gemini/kiro/vertex/antigravity 等),改由 provider transport 统一承载
- 移除 control_facade/execution_facade/auth_snapshot_facade 等冗余 facade 层
- 拆分 query/billing 与 query/monitoring 模块、state/runtime/payments 与 security 模块
- 扩展架构测试覆盖 admin_billing/admin_model/admin_users 等新模块
- 删除 docs/architecture/refactor-execution-plan.md 已完成的执行计划文档
This commit is contained in:
fawney19
2026-04-09 00:10:38 +08:00
parent 4fb9882b54
commit 4fc95adfb9
663 changed files with 48471 additions and 40232 deletions
@@ -1,623 +0,0 @@
use super::support::{
admin_pool_provider_id_from_path, build_admin_pool_error_response,
ADMIN_POOL_BANNED_KEY_CLEANUP_EMPTY_MESSAGE,
ADMIN_POOL_PROVIDER_CATALOG_READER_UNAVAILABLE_DETAIL,
ADMIN_POOL_PROVIDER_CATALOG_WRITER_UNAVAILABLE_DETAIL,
};
use super::{payloads as pool_payloads, selection as pool_selection};
use crate::control::GatewayPublicRequestContext;
use crate::handlers::admin::provider::pool::runtime::{
clear_admin_provider_pool_cooldown, reset_admin_provider_pool_cost,
};
use crate::handlers::admin::shared::{
attach_admin_audit_response, encrypt_catalog_secret_with_fallbacks,
};
use crate::{AppState, GatewayError, LocalProviderDeleteTaskState};
use aether_data_contracts::repository::provider_catalog::{
StoredProviderCatalogEndpoint, StoredProviderCatalogKey,
};
use axum::{
body::{Body, Bytes},
http,
response::{IntoResponse, Response},
Json,
};
use serde_json::json;
use std::collections::BTreeSet;
use std::time::{SystemTime, UNIX_EPOCH};
use uuid::Uuid;
#[derive(Debug, Default, serde::Deserialize)]
struct AdminPoolBatchActionRequest {
#[serde(default)]
key_ids: Vec<String>,
#[serde(default)]
action: String,
#[serde(default)]
payload: Option<serde_json::Value>,
}
#[derive(Debug, Default, serde::Deserialize)]
struct AdminPoolBatchImportRequest {
#[serde(default)]
keys: Vec<AdminPoolBatchImportItem>,
#[serde(default)]
proxy_node_id: Option<String>,
}
#[derive(Debug, Default, serde::Deserialize)]
struct AdminPoolBatchImportItem {
#[serde(default)]
name: String,
#[serde(default)]
api_key: String,
#[serde(default)]
auth_type: String,
}
fn admin_pool_batch_delete_task_parts(request_path: &str) -> Option<(String, String)> {
let raw = request_path.strip_prefix("/api/admin/pool/")?;
let (provider_id, suffix) = raw.split_once("/keys/batch-delete-task/")?;
let provider_id = provider_id.trim();
let task_id = suffix.trim().trim_matches('/');
if provider_id.is_empty()
|| provider_id.contains('/')
|| task_id.is_empty()
|| task_id.contains('/')
{
return None;
}
Some((provider_id.to_string(), task_id.to_string()))
}
fn admin_pool_key_proxy_value(proxy_node_id: Option<&str>) -> Option<serde_json::Value> {
proxy_node_id
.map(str::trim)
.filter(|value| !value.is_empty())
.map(|value| json!({ "node_id": value, "enabled": true }))
}
fn build_admin_pool_batch_delete_task_payload(
task: &LocalProviderDeleteTaskState,
) -> serde_json::Value {
json!({
"task_id": task.task_id,
"provider_id": task.provider_id,
"status": task.status,
"stage": task.stage,
"total_keys": task.total_keys,
"deleted_keys": task.deleted_keys,
"total_endpoints": task.total_endpoints,
"deleted_endpoints": task.deleted_endpoints,
"message": task.message,
})
}
fn attach_admin_pool_batch_delete_task_terminal_audit(
provider_id: &str,
task_id: &str,
task_status: &str,
response: Response<Body>,
) -> Response<Body> {
match task_status {
"completed" => attach_admin_audit_response(
response,
"admin_pool_batch_delete_task_completed_viewed",
"view_pool_batch_delete_task_terminal_state",
"provider_key_batch_delete_task",
&format!("{provider_id}:{task_id}"),
),
"failed" => attach_admin_audit_response(
response,
"admin_pool_batch_delete_task_failed_viewed",
"view_pool_batch_delete_task_terminal_state",
"provider_key_batch_delete_task",
&format!("{provider_id}:{task_id}"),
),
_ => response,
}
}
fn admin_pool_resolved_api_formats(
endpoints: &[StoredProviderCatalogEndpoint],
existing_keys: &[StoredProviderCatalogKey],
) -> Vec<String> {
let mut formats = Vec::new();
let mut seen = BTreeSet::new();
for endpoint in endpoints.iter().filter(|endpoint| endpoint.is_active) {
let api_format = endpoint.api_format.trim();
if api_format.is_empty() || !seen.insert(api_format.to_string()) {
continue;
}
formats.push(api_format.to_string());
}
if !formats.is_empty() {
return formats;
}
for key in existing_keys {
for api_format in pool_payloads::admin_pool_api_formats(key) {
if !seen.insert(api_format.clone()) {
continue;
}
formats.push(api_format);
}
}
formats
}
async fn build_admin_pool_cleanup_banned_keys_response(
state: &AppState,
provider_id: String,
) -> Result<Response<Body>, GatewayError> {
if !state.has_provider_catalog_data_reader() {
return Ok(build_admin_pool_error_response(
http::StatusCode::SERVICE_UNAVAILABLE,
ADMIN_POOL_PROVIDER_CATALOG_READER_UNAVAILABLE_DETAIL,
));
}
if !state.has_provider_catalog_data_writer() {
return Ok(build_admin_pool_error_response(
http::StatusCode::SERVICE_UNAVAILABLE,
ADMIN_POOL_PROVIDER_CATALOG_WRITER_UNAVAILABLE_DETAIL,
));
}
let Some(provider) = state
.read_provider_catalog_providers_by_ids(std::slice::from_ref(&provider_id))
.await?
.into_iter()
.next()
else {
return Ok(build_admin_pool_error_response(
http::StatusCode::NOT_FOUND,
format!("Provider {provider_id} 不存在"),
));
};
let banned_keys = state
.list_provider_catalog_keys_by_provider_ids(std::slice::from_ref(&provider.id))
.await?
.into_iter()
.filter(pool_selection::admin_pool_key_is_known_banned)
.collect::<Vec<_>>();
if banned_keys.is_empty() {
return Ok(Json(json!({
"affected": 0,
"message": ADMIN_POOL_BANNED_KEY_CLEANUP_EMPTY_MESSAGE,
}))
.into_response());
}
let deleted_key_ids = banned_keys
.iter()
.map(|key| key.id.clone())
.collect::<Vec<_>>();
for key in &banned_keys {
clear_admin_provider_pool_cooldown(state, &provider.id, &key.id).await;
reset_admin_provider_pool_cost(state, &provider.id, &key.id).await;
}
let mut affected = 0usize;
for key_id in &deleted_key_ids {
if state.delete_provider_catalog_key(key_id).await? {
affected += 1;
}
}
state
.cleanup_deleted_provider_catalog_refs(&provider.id, &[], &deleted_key_ids)
.await?;
Ok(Json(json!({
"affected": affected,
"message": format!("已清理 {affected} 个异常账号"),
}))
.into_response())
}
async fn build_admin_pool_batch_import_response(
state: &AppState,
request_context: &GatewayPublicRequestContext,
request_body: Option<&Bytes>,
) -> Result<Response<Body>, GatewayError> {
if !state.has_provider_catalog_data_reader() {
return Ok(build_admin_pool_error_response(
http::StatusCode::SERVICE_UNAVAILABLE,
ADMIN_POOL_PROVIDER_CATALOG_READER_UNAVAILABLE_DETAIL,
));
}
if !state.has_provider_catalog_data_writer() {
return Ok(build_admin_pool_error_response(
http::StatusCode::SERVICE_UNAVAILABLE,
ADMIN_POOL_PROVIDER_CATALOG_WRITER_UNAVAILABLE_DETAIL,
));
}
let Some(provider_id) = admin_pool_provider_id_from_path(&request_context.request_path) else {
return Ok(build_admin_pool_error_response(
http::StatusCode::BAD_REQUEST,
"provider_id 无效",
));
};
let payload = match request_body {
Some(body) if !body.is_empty() => {
match serde_json::from_slice::<AdminPoolBatchImportRequest>(body) {
Ok(value) => value,
Err(_) => {
return Ok(build_admin_pool_error_response(
http::StatusCode::BAD_REQUEST,
"Invalid JSON request body",
));
}
}
}
_ => {
return Ok(build_admin_pool_error_response(
http::StatusCode::BAD_REQUEST,
"Invalid JSON request body",
));
}
};
if payload.keys.len() > 500 {
return Ok(build_admin_pool_error_response(
http::StatusCode::BAD_REQUEST,
"keys length must be less than or equal to 500",
));
}
let Some(provider) = state
.read_provider_catalog_providers_by_ids(std::slice::from_ref(&provider_id))
.await?
.into_iter()
.next()
else {
return Ok(build_admin_pool_error_response(
http::StatusCode::NOT_FOUND,
format!("Provider {provider_id} 不存在"),
));
};
let endpoints = state
.list_provider_catalog_endpoints_by_provider_ids(std::slice::from_ref(&provider.id))
.await?;
let existing_keys = state
.list_provider_catalog_keys_by_provider_ids(std::slice::from_ref(&provider.id))
.await?;
let api_formats = admin_pool_resolved_api_formats(&endpoints, &existing_keys);
if api_formats.is_empty() {
return Ok(build_admin_pool_error_response(
http::StatusCode::BAD_REQUEST,
"Provider 没有可用 endpoint 或现有 key,无法推断 api_formats",
));
}
let proxy = admin_pool_key_proxy_value(payload.proxy_node_id.as_deref());
let mut imported = 0usize;
let skipped = 0usize;
let mut errors = Vec::new();
let now_unix_secs = SystemTime::now()
.duration_since(UNIX_EPOCH)
.ok()
.map(|duration| duration.as_secs())
.unwrap_or(0);
for (index, item) in payload.keys.iter().enumerate() {
let api_key = item.api_key.trim();
if api_key.is_empty() {
errors.push(json!({
"index": index,
"reason": "api_key is empty",
}));
continue;
}
let Some(encrypted_api_key) = encrypt_catalog_secret_with_fallbacks(state, api_key) else {
errors.push(json!({
"index": index,
"reason": "gateway 未配置 provider key 加密密钥",
}));
continue;
};
let auth_type = item.auth_type.trim().to_ascii_lowercase();
let auth_type = if auth_type.is_empty() {
"api_key".to_string()
} else {
auth_type
};
let name = item.name.trim();
let mut record = match StoredProviderCatalogKey::new(
Uuid::new_v4().to_string(),
provider.id.clone(),
if name.is_empty() {
format!("imported-{index}")
} else {
name.to_string()
},
auth_type,
None,
true,
) {
Ok(value) => value,
Err(err) => {
errors.push(json!({
"index": index,
"reason": err.to_string(),
}));
continue;
}
};
record = match record.with_transport_fields(
Some(json!(api_formats)),
encrypted_api_key,
None,
None,
None,
None,
None,
proxy.clone(),
None,
) {
Ok(value) => value,
Err(err) => {
errors.push(json!({
"index": index,
"reason": err.to_string(),
}));
continue;
}
};
record.request_count = Some(0);
record.success_count = Some(0);
record.error_count = Some(0);
record.total_response_time_ms = Some(0);
record.health_by_format = Some(json!({}));
record.circuit_breaker_by_format = Some(json!({}));
record.created_at_unix_secs = Some(now_unix_secs);
record.updated_at_unix_secs = Some(now_unix_secs);
let Some(_) = state.create_provider_catalog_key(&record).await? else {
return Ok(build_admin_pool_error_response(
http::StatusCode::SERVICE_UNAVAILABLE,
ADMIN_POOL_PROVIDER_CATALOG_WRITER_UNAVAILABLE_DETAIL,
));
};
imported += 1;
}
Ok(Json(json!({
"imported": imported,
"skipped": skipped,
"errors": errors,
}))
.into_response())
}
async fn build_admin_pool_batch_action_response(
state: &AppState,
request_context: &GatewayPublicRequestContext,
request_body: Option<&Bytes>,
) -> Result<Response<Body>, GatewayError> {
if !state.has_provider_catalog_data_reader() {
return Ok(build_admin_pool_error_response(
http::StatusCode::SERVICE_UNAVAILABLE,
ADMIN_POOL_PROVIDER_CATALOG_READER_UNAVAILABLE_DETAIL,
));
}
if !state.has_provider_catalog_data_writer() {
return Ok(build_admin_pool_error_response(
http::StatusCode::SERVICE_UNAVAILABLE,
ADMIN_POOL_PROVIDER_CATALOG_WRITER_UNAVAILABLE_DETAIL,
));
}
let Some(provider_id) = admin_pool_provider_id_from_path(&request_context.request_path) else {
return Ok(build_admin_pool_error_response(
http::StatusCode::NOT_FOUND,
"Provider 不存在",
));
};
let payload = match request_body {
Some(body) if !body.is_empty() => {
match serde_json::from_slice::<AdminPoolBatchActionRequest>(body) {
Ok(value) => value,
Err(_) => {
return Ok(build_admin_pool_error_response(
http::StatusCode::BAD_REQUEST,
"Invalid JSON request body",
));
}
}
}
_ => {
return Ok(build_admin_pool_error_response(
http::StatusCode::BAD_REQUEST,
"Invalid JSON request body",
));
}
};
let Some(provider) = state
.read_provider_catalog_providers_by_ids(std::slice::from_ref(&provider_id))
.await?
.into_iter()
.next()
else {
return Ok(build_admin_pool_error_response(
http::StatusCode::NOT_FOUND,
format!("Provider {provider_id} 不存在"),
));
};
let action = payload.action.trim().to_ascii_lowercase();
let action_label = match action.as_str() {
"enable" => "enabled",
"disable" => "disabled",
"clear_proxy" => "proxy cleared",
"set_proxy" => "proxy set",
"delete" => "deleted",
_ => {
return Ok(build_admin_pool_error_response(
http::StatusCode::BAD_REQUEST,
format!(
"Invalid action: {action}. Supported locally: enable, disable, clear_proxy, set_proxy, delete"
),
));
}
};
let key_ids = payload
.key_ids
.into_iter()
.map(|value| value.trim().to_string())
.filter(|value| !value.is_empty())
.collect::<BTreeSet<_>>()
.into_iter()
.collect::<Vec<_>>();
if key_ids.is_empty() {
return Ok(build_admin_pool_error_response(
http::StatusCode::BAD_REQUEST,
"key_ids should not be empty",
));
}
let proxy_payload = if action == "set_proxy" {
match payload.payload {
Some(serde_json::Value::Object(map)) if !map.is_empty() => {
Some(serde_json::Value::Object(map))
}
_ => {
return Ok(build_admin_pool_error_response(
http::StatusCode::BAD_REQUEST,
"set_proxy action requires a non-empty payload with proxy config",
));
}
}
} else {
None
};
let keys = state
.read_provider_catalog_keys_by_ids(&key_ids)
.await?
.into_iter()
.filter(|key| key.provider_id == provider.id)
.collect::<Vec<_>>();
if action == "delete" {
let deleted_key_ids = keys.iter().map(|key| key.id.clone()).collect::<Vec<_>>();
for key in &keys {
clear_admin_provider_pool_cooldown(state, &provider.id, &key.id).await;
reset_admin_provider_pool_cost(state, &provider.id, &key.id).await;
}
let mut affected = 0usize;
for key_id in &deleted_key_ids {
if state.delete_provider_catalog_key(key_id).await? {
affected = affected.saturating_add(1);
}
}
state
.cleanup_deleted_provider_catalog_refs(&provider.id, &[], &deleted_key_ids)
.await?;
return Ok(Json(json!({
"affected": affected,
"message": format!("{affected} keys {action_label}"),
}))
.into_response());
}
let mut affected = 0usize;
for mut key in keys {
match action.as_str() {
"enable" => key.is_active = true,
"disable" => key.is_active = false,
"clear_proxy" => key.proxy = None,
"set_proxy" => key.proxy = proxy_payload.clone(),
_ => unreachable!(),
}
if state.update_provider_catalog_key(&key).await?.is_some() {
affected = affected.saturating_add(1);
}
}
Ok(Json(json!({
"affected": affected,
"message": format!("{affected} keys {action_label}"),
}))
.into_response())
}
async fn build_admin_pool_batch_delete_task_status_response(
state: &AppState,
request_context: &GatewayPublicRequestContext,
) -> Result<Response<Body>, GatewayError> {
let Some((provider_id, task_id)) =
admin_pool_batch_delete_task_parts(&request_context.request_path)
else {
return Ok(build_admin_pool_error_response(
http::StatusCode::NOT_FOUND,
"批量删除任务不存在",
));
};
let Some(task) = state.get_provider_delete_task(&task_id) else {
return Ok(build_admin_pool_error_response(
http::StatusCode::NOT_FOUND,
"批量删除任务不存在",
));
};
if task.provider_id != provider_id {
return Ok(build_admin_pool_error_response(
http::StatusCode::NOT_FOUND,
"批量删除任务不存在",
));
}
Ok(attach_admin_pool_batch_delete_task_terminal_audit(
&provider_id,
&task_id,
task.status.as_str(),
Json(build_admin_pool_batch_delete_task_payload(&task)).into_response(),
))
}
pub(super) async fn maybe_build_local_admin_pool_batch_response(
state: &AppState,
request_context: &GatewayPublicRequestContext,
request_body: Option<&Bytes>,
) -> Result<Option<Response<Body>>, GatewayError> {
match decision_route_kind(request_context) {
Some("cleanup_banned_keys") if request_context.request_method == http::Method::POST => {
let Some(provider_id) = admin_pool_provider_id_from_path(&request_context.request_path)
else {
return Ok(Some(build_admin_pool_error_response(
http::StatusCode::BAD_REQUEST,
"provider_id 无效",
)));
};
Ok(Some(
build_admin_pool_cleanup_banned_keys_response(state, provider_id).await?,
))
}
Some("batch_import_keys") => Ok(Some(
build_admin_pool_batch_import_response(state, request_context, request_body).await?,
)),
Some("batch_action_keys") => Ok(Some(
build_admin_pool_batch_action_response(state, request_context, request_body).await?,
)),
Some("batch_delete_task_status") => Ok(Some(
build_admin_pool_batch_delete_task_status_response(state, request_context).await?,
)),
_ => Ok(None),
}
}
fn decision_route_kind<'a>(request_context: &'a GatewayPublicRequestContext) -> Option<&'a str> {
request_context
.control_decision
.as_ref()?
.route_kind
.as_deref()
}
@@ -0,0 +1,61 @@
use super::{
admin_pool_provider_id_from_path, build_admin_pool_error_response, AdminPoolBatchActionRequest,
ADMIN_POOL_PROVIDER_CATALOG_READER_UNAVAILABLE_DETAIL,
ADMIN_POOL_PROVIDER_CATALOG_WRITER_UNAVAILABLE_DETAIL,
};
use crate::handlers::admin::request::{AdminAppState, AdminRequestContext};
use crate::GatewayError;
use axum::{
body::{Body, Bytes},
http,
response::Response,
};
pub(super) async fn build_admin_pool_batch_action_response(
state: &AdminAppState<'_>,
request_context: &AdminRequestContext<'_>,
request_body: Option<&Bytes>,
) -> Result<Response<Body>, GatewayError> {
if !state.has_provider_catalog_data_reader() {
return Ok(build_admin_pool_error_response(
http::StatusCode::SERVICE_UNAVAILABLE,
ADMIN_POOL_PROVIDER_CATALOG_READER_UNAVAILABLE_DETAIL,
));
}
if !state.has_provider_catalog_data_writer() {
return Ok(build_admin_pool_error_response(
http::StatusCode::SERVICE_UNAVAILABLE,
ADMIN_POOL_PROVIDER_CATALOG_WRITER_UNAVAILABLE_DETAIL,
));
}
let Some(provider_id) = admin_pool_provider_id_from_path(request_context.path()) else {
return Ok(build_admin_pool_error_response(
http::StatusCode::NOT_FOUND,
"Provider 不存在",
));
};
let payload = match request_body {
Some(body) if !body.is_empty() => {
match serde_json::from_slice::<AdminPoolBatchActionRequest>(body) {
Ok(value) => value,
Err(_) => {
return Ok(build_admin_pool_error_response(
http::StatusCode::BAD_REQUEST,
"Invalid JSON request body",
));
}
}
}
_ => {
return Ok(build_admin_pool_error_response(
http::StatusCode::BAD_REQUEST,
"Invalid JSON request body",
));
}
};
state
.build_admin_pool_batch_action_response(&provider_id, payload)
.await
}
@@ -0,0 +1,29 @@
use super::{
build_admin_pool_error_response, ADMIN_POOL_PROVIDER_CATALOG_READER_UNAVAILABLE_DETAIL,
ADMIN_POOL_PROVIDER_CATALOG_WRITER_UNAVAILABLE_DETAIL,
};
use crate::handlers::admin::request::AdminAppState;
use crate::GatewayError;
use axum::{body::Body, http, response::Response};
pub(super) async fn build_admin_pool_cleanup_banned_keys_response(
state: &AdminAppState<'_>,
provider_id: String,
) -> Result<Response<Body>, GatewayError> {
if !state.has_provider_catalog_data_reader() {
return Ok(build_admin_pool_error_response(
http::StatusCode::SERVICE_UNAVAILABLE,
ADMIN_POOL_PROVIDER_CATALOG_READER_UNAVAILABLE_DETAIL,
));
}
if !state.has_provider_catalog_data_writer() {
return Ok(build_admin_pool_error_response(
http::StatusCode::SERVICE_UNAVAILABLE,
ADMIN_POOL_PROVIDER_CATALOG_WRITER_UNAVAILABLE_DETAIL,
));
}
state
.build_admin_pool_cleanup_banned_keys_response(&provider_id)
.await
}
@@ -0,0 +1,68 @@
use super::{
admin_pool_provider_id_from_path, build_admin_pool_error_response, AdminPoolBatchImportRequest,
ADMIN_POOL_PROVIDER_CATALOG_READER_UNAVAILABLE_DETAIL,
ADMIN_POOL_PROVIDER_CATALOG_WRITER_UNAVAILABLE_DETAIL,
};
use crate::handlers::admin::request::{AdminAppState, AdminRequestContext};
use crate::GatewayError;
use axum::{
body::{Body, Bytes},
http,
response::Response,
};
pub(super) async fn build_admin_pool_batch_import_response(
state: &AdminAppState<'_>,
request_context: &AdminRequestContext<'_>,
request_body: Option<&Bytes>,
) -> Result<Response<Body>, GatewayError> {
if !state.has_provider_catalog_data_reader() {
return Ok(build_admin_pool_error_response(
http::StatusCode::SERVICE_UNAVAILABLE,
ADMIN_POOL_PROVIDER_CATALOG_READER_UNAVAILABLE_DETAIL,
));
}
if !state.has_provider_catalog_data_writer() {
return Ok(build_admin_pool_error_response(
http::StatusCode::SERVICE_UNAVAILABLE,
ADMIN_POOL_PROVIDER_CATALOG_WRITER_UNAVAILABLE_DETAIL,
));
}
let Some(provider_id) = admin_pool_provider_id_from_path(request_context.path()) else {
return Ok(build_admin_pool_error_response(
http::StatusCode::BAD_REQUEST,
"provider_id 无效",
));
};
let payload = match request_body {
Some(body) if !body.is_empty() => {
match serde_json::from_slice::<AdminPoolBatchImportRequest>(body) {
Ok(value) => value,
Err(_) => {
return Ok(build_admin_pool_error_response(
http::StatusCode::BAD_REQUEST,
"Invalid JSON request body",
));
}
}
}
_ => {
return Ok(build_admin_pool_error_response(
http::StatusCode::BAD_REQUEST,
"Invalid JSON request body",
));
}
};
if payload.keys.len() > 500 {
return Ok(build_admin_pool_error_response(
http::StatusCode::BAD_REQUEST,
"keys length must be less than or equal to 500",
));
}
state
.build_admin_pool_batch_import_response(&provider_id, payload)
.await
}
@@ -0,0 +1,63 @@
use super::attach_admin_audit_response;
use crate::LocalProviderDeleteTaskState;
use aether_admin::provider::pool as admin_provider_pool_pure;
pub(crate) use aether_admin::provider::pool::{
admin_pool_batch_delete_task_parts, AdminPoolBatchActionRequest, AdminPoolBatchImportItem,
AdminPoolBatchImportRequest,
};
use aether_data_contracts::repository::provider_catalog::{
StoredProviderCatalogEndpoint, StoredProviderCatalogKey,
};
use axum::{body::Body, response::Response};
pub(crate) fn admin_pool_key_proxy_value(proxy_node_id: Option<&str>) -> Option<serde_json::Value> {
admin_provider_pool_pure::admin_pool_key_proxy_value(proxy_node_id)
}
pub(crate) fn build_admin_pool_batch_delete_task_payload(
task: &LocalProviderDeleteTaskState,
) -> serde_json::Value {
admin_provider_pool_pure::build_admin_pool_batch_delete_task_payload(
&task.task_id,
&task.provider_id,
&task.status,
&task.stage,
task.total_keys,
task.deleted_keys,
task.total_endpoints,
task.deleted_endpoints,
&task.message,
)
}
pub(crate) fn attach_admin_pool_batch_delete_task_terminal_audit(
provider_id: &str,
task_id: &str,
task_status: &str,
response: Response<Body>,
) -> Response<Body> {
match task_status {
"completed" => attach_admin_audit_response(
response,
"admin_pool_batch_delete_task_completed_viewed",
"view_pool_batch_delete_task_terminal_state",
"provider_key_batch_delete_task",
&format!("{provider_id}:{task_id}"),
),
"failed" => attach_admin_audit_response(
response,
"admin_pool_batch_delete_task_failed_viewed",
"view_pool_batch_delete_task_terminal_state",
"provider_key_batch_delete_task",
&format!("{provider_id}:{task_id}"),
),
_ => response,
}
}
pub(crate) fn admin_pool_resolved_api_formats(
endpoints: &[StoredProviderCatalogEndpoint],
existing_keys: &[StoredProviderCatalogKey],
) -> Vec<String> {
admin_provider_pool_pure::admin_pool_resolved_api_formats(endpoints, existing_keys)
}
@@ -0,0 +1,38 @@
use super::{
admin_pool_batch_delete_task_parts, attach_admin_pool_batch_delete_task_terminal_audit,
build_admin_pool_batch_delete_task_payload, build_admin_pool_error_response,
};
use crate::handlers::admin::request::{AdminAppState, AdminRequestContext};
use crate::GatewayError;
use axum::{
body::Body,
http,
response::{IntoResponse, Response},
Json,
};
pub(super) async fn build_admin_pool_batch_delete_task_status_response(
state: &AdminAppState<'_>,
request_context: &AdminRequestContext<'_>,
) -> Result<Response<Body>, GatewayError> {
let Some((provider_id, task_id)) = admin_pool_batch_delete_task_parts(request_context.path())
else {
return Ok(build_admin_pool_error_response(
http::StatusCode::NOT_FOUND,
"批量删除任务不存在",
));
};
let task = match state.get_admin_pool_batch_delete_task_for_provider(&provider_id, &task_id) {
Ok(task) => task,
Err(response) => {
return Ok(response);
}
};
Ok(attach_admin_pool_batch_delete_task_terminal_audit(
&provider_id,
&task_id,
task.status.as_str(),
Json(build_admin_pool_batch_delete_task_payload(&task)).into_response(),
))
}
@@ -1,24 +1,62 @@
use crate::control::GatewayPublicRequestContext;
use crate::{AppState, GatewayError};
use crate::handlers::admin::request::{AdminAppState, AdminRequestContext};
use crate::GatewayError;
use axum::{
body::{Body, Bytes},
http,
response::Response,
};
#[path = "batch_routes/action.rs"]
mod batch_action;
#[path = "batch_routes/cleanup.rs"]
mod batch_cleanup;
#[path = "batch_routes/import.rs"]
mod batch_import;
#[path = "batch_routes/shared.rs"]
mod batch_shared;
#[path = "batch_routes/task_status.rs"]
mod batch_task_status;
pub(crate) mod payloads;
#[path = "read_routes/keys.rs"]
mod read_keys;
#[path = "read_routes/overview.rs"]
mod read_overview;
#[path = "read_routes/presets.rs"]
mod read_presets;
#[path = "read_routes/resolve_selection.rs"]
mod read_resolve_selection;
pub(crate) mod selection;
mod support;
mod batch_routes;
mod payloads;
mod read_routes;
mod selection;
pub(crate) use self::batch_shared::{
admin_pool_batch_delete_task_parts, admin_pool_key_proxy_value,
admin_pool_resolved_api_formats, attach_admin_pool_batch_delete_task_terminal_audit,
build_admin_pool_batch_delete_task_payload, AdminPoolBatchActionRequest,
AdminPoolBatchImportRequest,
};
pub(crate) use self::support::{
admin_pool_provider_id_from_path, parse_admin_pool_page, parse_admin_pool_page_size,
parse_admin_pool_search, parse_admin_pool_status_filter, AdminPoolResolveSelectionRequest,
ADMIN_POOL_BANNED_KEY_CLEANUP_EMPTY_MESSAGE,
ADMIN_POOL_PROVIDER_CATALOG_READER_UNAVAILABLE_DETAIL,
ADMIN_POOL_PROVIDER_CATALOG_WRITER_UNAVAILABLE_DETAIL,
};
use self::support::{build_admin_pool_error_response, is_admin_pool_route};
pub(crate) use self::{payloads as pool_payloads, selection as pool_selection};
pub(crate) use crate::handlers::admin::provider::pool::config::admin_provider_pool_config;
pub(crate) use crate::handlers::admin::provider::pool::runtime::{
read_admin_provider_pool_cooldown_counts, read_admin_provider_pool_cooldown_key_ids,
read_admin_provider_pool_runtime_state,
};
pub(crate) use crate::handlers::admin::provider::shared::support::AdminProviderPoolRuntimeState;
pub(crate) use crate::handlers::admin::shared::attach_admin_audit_response;
pub(crate) use aether_data_contracts::repository::provider_catalog::ProviderCatalogKeyListQuery;
pub(crate) async fn maybe_build_local_admin_pool_response(
state: &AppState,
request_context: &GatewayPublicRequestContext,
state: &AdminAppState<'_>,
request_context: &AdminRequestContext<'_>,
request_body: Option<&Bytes>,
) -> Result<Option<Response<Body>>, GatewayError> {
let Some(decision) = request_context.control_decision.as_ref() else {
let Some(decision) = request_context.decision() else {
return Ok(None);
};
@@ -30,30 +68,97 @@ pub(crate) async fn maybe_build_local_admin_pool_response(
return Ok(None);
}
if let Some(response) = batch_routes::maybe_build_local_admin_pool_batch_response(
state,
request_context,
request_body,
)
.await?
match request_context
.decision()
.and_then(|decision| decision.route_kind.as_deref())
{
return Ok(Some(response));
}
if let Some(response) = read_routes::maybe_build_local_admin_pool_read_response(
state,
request_context,
request_body,
)
.await?
{
return Ok(Some(response));
Some("overview")
if request_context.method() == http::Method::GET
&& matches!(
request_context.path().trim_end_matches('/'),
"/api/admin/pool/overview"
) =>
{
return Ok(Some(
read_overview::build_admin_pool_overview_response(state).await?,
));
}
Some("scheduling_presets")
if request_context.method() == http::Method::GET
&& matches!(
request_context.path().trim_end_matches('/'),
"/api/admin/pool/scheduling-presets"
) =>
{
return Ok(Some(
read_presets::build_admin_pool_scheduling_presets_response(),
));
}
Some("list_keys") => {
return Ok(Some(
read_keys::build_admin_pool_list_keys_response(state, request_context).await?,
));
}
Some("resolve_selection") => {
return Ok(Some(
read_resolve_selection::build_admin_pool_resolve_selection_response(
state,
request_context,
request_body,
)
.await?,
));
}
Some("cleanup_banned_keys") if request_context.method() == http::Method::POST => {
let Some(provider_id) = admin_pool_provider_id_from_path(request_context.path()) else {
return Ok(Some(build_admin_pool_error_response(
http::StatusCode::BAD_REQUEST,
"provider_id 无效",
)));
};
return Ok(Some(
batch_cleanup::build_admin_pool_cleanup_banned_keys_response(state, provider_id)
.await?,
));
}
Some("batch_import_keys") => {
return Ok(Some(
batch_import::build_admin_pool_batch_import_response(
state,
request_context,
request_body,
)
.await?,
));
}
Some("batch_action_keys") => {
return Ok(Some(
batch_action::build_admin_pool_batch_action_response(
state,
request_context,
request_body,
)
.await?,
));
}
Some("batch_delete_task_status") => {
return Ok(Some(
batch_task_status::build_admin_pool_batch_delete_task_status_response(
state,
request_context,
)
.await?,
));
}
_ => {}
}
Ok(Some(build_admin_pool_error_response(
http::StatusCode::NOT_FOUND,
format!(
"Unsupported admin pool route {} {}",
request_context.request_method, request_context.request_path
request_context.method(),
request_context.path()
),
)))
}
@@ -1,221 +1,34 @@
use crate::handlers::admin::provider::shared::support::{
AdminProviderPoolConfig, AdminProviderPoolRuntimeState,
};
use crate::handlers::admin::shared::unix_secs_to_rfc3339;
use aether_admin::provider::pool as admin_provider_pool_pure;
use aether_data_contracts::repository::provider_catalog::StoredProviderCatalogKey;
use serde_json::json;
pub(super) fn admin_pool_api_formats(key: &StoredProviderCatalogKey) -> Vec<String> {
key.api_formats
.as_ref()
.and_then(serde_json::Value::as_array)
.map(|values| {
values
.iter()
.filter_map(serde_json::Value::as_str)
.map(ToOwned::to_owned)
.collect::<Vec<_>>()
})
.unwrap_or_default()
}
fn admin_pool_string_list(value: Option<&serde_json::Value>) -> Option<Vec<String>> {
let values = value
.and_then(serde_json::Value::as_array)
.map(|items| {
items
.iter()
.filter_map(serde_json::Value::as_str)
.map(ToOwned::to_owned)
.collect::<Vec<_>>()
})
.unwrap_or_default();
if values.is_empty() {
None
} else {
Some(values)
}
}
fn admin_pool_json_object(
value: Option<&serde_json::Value>,
) -> Option<serde_json::Map<String, serde_json::Value>> {
value
.and_then(serde_json::Value::as_object)
.cloned()
.filter(|value| !value.is_empty())
}
fn admin_pool_health_score(key: &StoredProviderCatalogKey) -> f64 {
let scores = key
.health_by_format
.as_ref()
.and_then(serde_json::Value::as_object)
.map(|formats| {
formats
.values()
.filter_map(serde_json::Value::as_object)
.filter_map(|item| item.get("health_score"))
.filter_map(serde_json::Value::as_f64)
.collect::<Vec<_>>()
})
.unwrap_or_default();
if scores.is_empty() {
1.0
} else {
scores.into_iter().fold(1.0, f64::min)
}
}
fn admin_pool_circuit_breaker_open(key: &StoredProviderCatalogKey) -> bool {
key.circuit_breaker_by_format
.as_ref()
.and_then(serde_json::Value::as_object)
.map(|formats| {
formats
.values()
.filter_map(serde_json::Value::as_object)
.any(|item| {
item.get("open")
.and_then(serde_json::Value::as_bool)
.unwrap_or(false)
})
})
.unwrap_or(false)
}
fn admin_pool_scheduling_payload(
key: &StoredProviderCatalogKey,
cooldown_reason: Option<&str>,
cooldown_ttl_seconds: Option<u64>,
health_score: f64,
circuit_breaker_open: bool,
) -> (String, String, String, Vec<serde_json::Value>) {
if !key.is_active {
return (
"blocked".to_string(),
"inactive".to_string(),
"已禁用".to_string(),
vec![json!({
"code": "inactive",
"label": "已禁用",
"blocking": true,
"source": "manual",
"ttl_seconds": serde_json::Value::Null,
"detail": serde_json::Value::Null,
})],
);
}
if let Some(reason) = cooldown_reason {
return (
"degraded".to_string(),
"cooldown".to_string(),
"冷却中".to_string(),
vec![json!({
"code": "cooldown",
"label": "冷却中",
"blocking": true,
"source": "pool",
"ttl_seconds": cooldown_ttl_seconds,
"detail": reason,
})],
);
}
if circuit_breaker_open {
return (
"degraded".to_string(),
"circuit_breaker".to_string(),
"熔断中".to_string(),
vec![json!({
"code": "circuit_breaker",
"label": "熔断中",
"blocking": true,
"source": "health",
"ttl_seconds": serde_json::Value::Null,
"detail": serde_json::Value::Null,
})],
);
}
if health_score < 0.5 {
return (
"degraded".to_string(),
"health_low".to_string(),
"健康度较低".to_string(),
vec![json!({
"code": "health_low",
"label": "健康度较低",
"blocking": false,
"source": "health",
"ttl_seconds": serde_json::Value::Null,
"detail": serde_json::Value::Null,
})],
);
}
(
"available".to_string(),
"available".to_string(),
"可用".to_string(),
Vec::new(),
)
}
pub(super) fn build_admin_pool_key_payload(
key: &StoredProviderCatalogKey,
runtime: &AdminProviderPoolRuntimeState,
pool_config: Option<AdminProviderPoolConfig>,
) -> serde_json::Value {
let cooldown_reason = runtime.cooldown_reason_by_key.get(&key.id).cloned();
let cooldown_ttl_seconds = cooldown_reason
.as_ref()
.and_then(|_| runtime.cooldown_ttl_by_key.get(&key.id).copied());
let health_score = admin_pool_health_score(key);
let circuit_breaker_open = admin_pool_circuit_breaker_open(key);
let (scheduling_status, scheduling_reason, scheduling_label, scheduling_reasons) =
admin_pool_scheduling_payload(
key,
cooldown_reason.as_deref(),
cooldown_ttl_seconds,
health_score,
circuit_breaker_open,
);
json!({
"key_id": key.id,
"key_name": key.name,
"is_active": key.is_active,
"auth_type": key.auth_type,
"status_snapshot": key.status_snapshot.clone().unwrap_or_else(|| json!({})),
"health_score": health_score,
"circuit_breaker_open": circuit_breaker_open,
"api_formats": admin_pool_api_formats(key),
"rate_multipliers": admin_pool_json_object(key.rate_multipliers.as_ref()),
"internal_priority": key.internal_priority,
"rpm_limit": key.rpm_limit,
"cache_ttl_minutes": key.cache_ttl_minutes,
"max_probe_interval_minutes": key.max_probe_interval_minutes,
"note": key.note,
"allowed_models": admin_pool_string_list(key.allowed_models.as_ref()),
"capabilities": admin_pool_json_object(key.capabilities.as_ref()),
"auto_fetch_models": key.auto_fetch_models,
"locked_models": admin_pool_string_list(key.locked_models.as_ref()),
"model_include_patterns": admin_pool_string_list(key.model_include_patterns.as_ref()),
"model_exclude_patterns": admin_pool_string_list(key.model_exclude_patterns.as_ref()),
"proxy": key.proxy.clone(),
"fingerprint": key.fingerprint.clone(),
"cooldown_reason": cooldown_reason,
"cooldown_ttl_seconds": cooldown_ttl_seconds,
"cost_window_usage": runtime.cost_window_usage_by_key.get(&key.id).copied().unwrap_or(0),
"cost_limit": pool_config.map(|config| config.cost_limit_per_key_tokens),
"request_count": key.request_count.unwrap_or(0),
"total_tokens": 0,
"total_cost_usd": "0.00000000",
"sticky_sessions": runtime.sticky_sessions_by_key.get(&key.id).copied().unwrap_or(0),
"lru_score": runtime.lru_score_by_key.get(&key.id).copied(),
"created_at": key.created_at_unix_secs.and_then(unix_secs_to_rfc3339),
"last_used_at": key.last_used_at_unix_secs.and_then(unix_secs_to_rfc3339),
"scheduling_status": scheduling_status,
"scheduling_reason": scheduling_reason,
"scheduling_label": scheduling_label,
"scheduling_reasons": scheduling_reasons,
})
admin_provider_pool_pure::build_admin_pool_key_payload(
key,
&admin_provider_pool_pure::AdminPoolKeyPayloadContext {
cooldown_reason: runtime.cooldown_reason_by_key.get(&key.id).cloned(),
cooldown_ttl_seconds: runtime
.cooldown_reason_by_key
.get(&key.id)
.and_then(|_| runtime.cooldown_ttl_by_key.get(&key.id).copied()),
cost_window_usage: runtime
.cost_window_usage_by_key
.get(&key.id)
.copied()
.unwrap_or(0),
sticky_sessions: runtime
.sticky_sessions_by_key
.get(&key.id)
.copied()
.unwrap_or(0),
lru_score: runtime.lru_score_by_key.get(&key.id).copied(),
cost_limit: pool_config.and_then(|config| config.cost_limit_per_key_tokens),
},
)
}
@@ -1,507 +0,0 @@
use super::support::{
admin_pool_provider_id_from_path, build_admin_pool_error_response, parse_admin_pool_page,
parse_admin_pool_page_size, parse_admin_pool_search, parse_admin_pool_status_filter,
AdminPoolResolveSelectionRequest, ADMIN_POOL_PROVIDER_CATALOG_READER_UNAVAILABLE_DETAIL,
};
use super::{payloads as pool_payloads, selection as pool_selection};
use crate::control::GatewayPublicRequestContext;
use crate::handlers::admin::provider::pool::config::admin_provider_pool_config;
use crate::handlers::admin::provider::pool::runtime::{
read_admin_provider_pool_cooldown_counts, read_admin_provider_pool_cooldown_key_ids,
read_admin_provider_pool_runtime_state,
};
use crate::handlers::admin::provider::shared::support::AdminProviderPoolRuntimeState;
use crate::{AppState, GatewayError};
use aether_data_contracts::repository::provider_catalog::ProviderCatalogKeyListQuery;
use axum::{
body::{Body, Bytes},
http,
response::{IntoResponse, Response},
Json,
};
use serde_json::json;
use std::collections::BTreeMap;
async fn build_admin_pool_overview_payload(
state: &AppState,
) -> Result<serde_json::Value, GatewayError> {
let providers = state.list_provider_catalog_providers(false).await?;
let pool_enabled_providers = providers
.into_iter()
.filter_map(|provider| {
admin_provider_pool_config(&provider).map(|config| (provider, config))
})
.collect::<Vec<_>>();
let provider_ids = pool_enabled_providers
.iter()
.map(|(provider, _)| provider.id.clone())
.collect::<Vec<_>>();
let key_stats = if provider_ids.is_empty() {
Vec::new()
} else {
state
.list_provider_catalog_key_stats_by_provider_ids(&provider_ids)
.await?
};
let key_stats_by_provider = key_stats
.into_iter()
.map(|item| (item.provider_id.clone(), item))
.collect::<BTreeMap<_, _>>();
let redis_runner = state.redis_kv_runner();
let cooldown_counts_by_provider = match redis_runner.as_ref() {
Some(runner) if !provider_ids.is_empty() => {
read_admin_provider_pool_cooldown_counts(runner, &provider_ids).await
}
_ => BTreeMap::new(),
};
let mut items = Vec::with_capacity(pool_enabled_providers.len());
for (provider, _pool_config) in pool_enabled_providers {
let stats = key_stats_by_provider.get(&provider.id);
let total_keys = stats.map(|item| item.total_keys as usize).unwrap_or(0);
let active_keys = stats.map(|item| item.active_keys as usize).unwrap_or(0);
let cooldown_count = cooldown_counts_by_provider
.get(&provider.id)
.copied()
.unwrap_or(0);
items.push(json!({
"provider_id": provider.id,
"provider_name": provider.name,
"provider_type": provider.provider_type,
"total_keys": total_keys,
"active_keys": active_keys,
"cooldown_count": cooldown_count,
"pool_enabled": true,
}));
}
Ok(json!({ "items": items }))
}
async fn build_admin_pool_list_keys_response(
state: &AppState,
request_context: &GatewayPublicRequestContext,
) -> Result<Response<Body>, GatewayError> {
if !state.has_provider_catalog_data_reader() {
return Ok(build_admin_pool_error_response(
http::StatusCode::SERVICE_UNAVAILABLE,
ADMIN_POOL_PROVIDER_CATALOG_READER_UNAVAILABLE_DETAIL,
));
}
let Some(provider_id) = admin_pool_provider_id_from_path(&request_context.request_path) else {
return Ok(build_admin_pool_error_response(
http::StatusCode::BAD_REQUEST,
"provider_id 无效",
));
};
let query = request_context.request_query_string.as_deref();
let page = match parse_admin_pool_page(query) {
Ok(value) => value,
Err(detail) => {
return Ok(build_admin_pool_error_response(
http::StatusCode::BAD_REQUEST,
detail,
));
}
};
let page_size = match parse_admin_pool_page_size(query) {
Ok(value) => value,
Err(detail) => {
return Ok(build_admin_pool_error_response(
http::StatusCode::BAD_REQUEST,
detail,
));
}
};
let search = parse_admin_pool_search(query).map(|value| value.to_ascii_lowercase());
let status = match parse_admin_pool_status_filter(query) {
Ok(value) => value,
Err(detail) => {
return Ok(build_admin_pool_error_response(
http::StatusCode::BAD_REQUEST,
detail,
));
}
};
let Some(provider) = state
.read_provider_catalog_providers_by_ids(std::slice::from_ref(&provider_id))
.await?
.into_iter()
.next()
else {
return Ok(build_admin_pool_error_response(
http::StatusCode::NOT_FOUND,
format!("Provider {provider_id} 不存在"),
));
};
let pool_config = admin_provider_pool_config(&provider);
let page_offset = page.saturating_sub(1).saturating_mul(page_size);
let (keys, total) = if status == "cooldown" {
let cooldown_key_ids = if let Some(runner) = state.redis_kv_runner() {
read_admin_provider_pool_cooldown_key_ids(&runner, &provider.id).await
} else {
Vec::new()
};
let mut keys = if cooldown_key_ids.is_empty() {
Vec::new()
} else {
state
.list_provider_catalog_keys_by_ids(&cooldown_key_ids)
.await?
};
if let Some(keyword) = search.as_ref() {
keys.retain(|key| {
key.name.to_ascii_lowercase().contains(keyword)
|| key.id.to_ascii_lowercase().contains(keyword)
});
}
pool_selection::admin_pool_sort_keys(&mut keys);
let total = keys.len();
let keys = keys
.into_iter()
.skip(page_offset)
.take(page_size)
.collect::<Vec<_>>();
(keys, total)
} else {
let key_page = state
.list_provider_catalog_key_page(&ProviderCatalogKeyListQuery {
provider_id: provider.id.clone(),
search: search.clone(),
is_active: match status.as_str() {
"active" => Some(true),
"inactive" => Some(false),
_ => None,
},
offset: page_offset,
limit: page_size,
})
.await?;
(key_page.items, key_page.total)
};
let key_ids = keys.iter().map(|key| key.id.clone()).collect::<Vec<_>>();
let runtime = match (state.redis_kv_runner(), pool_config) {
(Some(runner), Some(pool_config)) if !key_ids.is_empty() => {
read_admin_provider_pool_runtime_state(&runner, &provider.id, &key_ids, pool_config)
.await
}
_ => AdminProviderPoolRuntimeState::default(),
};
let items = keys
.into_iter()
.map(|key| pool_payloads::build_admin_pool_key_payload(&key, &runtime, pool_config))
.collect::<Vec<_>>();
Ok(Json(json!({
"total": total,
"page": page,
"page_size": page_size,
"keys": items,
}))
.into_response())
}
async fn build_admin_pool_resolve_selection_response(
state: &AppState,
request_context: &GatewayPublicRequestContext,
request_body: Option<&Bytes>,
) -> Result<Response<Body>, GatewayError> {
if !state.has_provider_catalog_data_reader() {
return Ok(build_admin_pool_error_response(
http::StatusCode::SERVICE_UNAVAILABLE,
ADMIN_POOL_PROVIDER_CATALOG_READER_UNAVAILABLE_DETAIL,
));
}
let Some(provider_id) = admin_pool_provider_id_from_path(&request_context.request_path) else {
return Ok(build_admin_pool_error_response(
http::StatusCode::BAD_REQUEST,
"provider_id 无效",
));
};
let payload = match request_body {
None => AdminPoolResolveSelectionRequest::default(),
Some(body) if body.is_empty() => AdminPoolResolveSelectionRequest::default(),
Some(body) => match serde_json::from_slice::<AdminPoolResolveSelectionRequest>(body) {
Ok(value) => value,
Err(_) => {
return Ok(build_admin_pool_error_response(
http::StatusCode::BAD_REQUEST,
"Invalid JSON request body",
));
}
},
};
let Some(provider) = state
.read_provider_catalog_providers_by_ids(std::slice::from_ref(&provider_id))
.await?
.into_iter()
.next()
else {
return Ok(build_admin_pool_error_response(
http::StatusCode::NOT_FOUND,
format!("Provider {provider_id} 不存在"),
));
};
let provider_type = provider.provider_type.clone();
let search = payload.search.trim();
let mut quick_selectors = payload
.quick_selectors
.into_iter()
.map(pool_selection::admin_pool_normalize_text)
.filter(|value| {
matches!(
value.as_str(),
"banned"
| "no_5h_limit"
| "no_weekly_limit"
| "plan_free"
| "plan_team"
| "oauth_invalid"
| "proxy_unset"
| "proxy_set"
| "disabled"
| "enabled"
)
})
.collect::<Vec<_>>();
quick_selectors.sort();
quick_selectors.dedup();
let mut keys = state
.list_provider_catalog_keys_by_provider_ids(std::slice::from_ref(&provider.id))
.await?
.into_iter()
.filter(|key| {
pool_selection::admin_pool_matches_search(state, key, &provider_type, Some(search))
})
.filter(|key| {
quick_selectors.is_empty()
|| quick_selectors.iter().all(|selector| {
pool_selection::admin_pool_matches_quick_selector(
state,
key,
&provider_type,
selector,
)
})
})
.collect::<Vec<_>>();
keys.sort_by(|left, right| {
left.internal_priority
.cmp(&right.internal_priority)
.then_with(|| left.name.cmp(&right.name))
});
let items = keys
.iter()
.map(|key| {
json!({
"key_id": key.id,
"key_name": key.name,
"auth_type": key.auth_type,
})
})
.collect::<Vec<_>>();
Ok(Json(json!({
"total": items.len(),
"items": items,
}))
.into_response())
}
fn build_admin_pool_scheduling_presets_payload() -> serde_json::Value {
json!([
{
"name": "lru",
"label": "LRU 轮转",
"description": "最久未使用的 Key 优先",
"providers": [],
"modes": serde_json::Value::Null,
"default_mode": serde_json::Value::Null,
"mutex_group": "distribution_mode",
"evidence_hint": "依据 LRU 时间戳(最近未使用优先)",
},
{
"name": "cache_affinity",
"label": "缓存亲和",
"description": "优先复用最近使用过的 Key,利用 Prompt Caching",
"providers": [],
"modes": serde_json::Value::Null,
"default_mode": serde_json::Value::Null,
"mutex_group": "distribution_mode",
"evidence_hint": "依据 LRU 时间戳(最近使用优先,与 LRU 轮转相反)",
},
{
"name": "cost_first",
"label": "成本优先",
"description": "优先选择窗口消耗更低的账号",
"providers": [],
"modes": serde_json::Value::Null,
"default_mode": serde_json::Value::Null,
"mutex_group": serde_json::Value::Null,
"evidence_hint": "依据窗口成本/Token 用量,缺失时回退配额使用率",
},
{
"name": "free_first",
"label": "Free 优先",
"description": "优先消耗 Free 账号(依赖 plan_type)",
"providers": ["codex", "kiro"],
"modes": serde_json::Value::Null,
"default_mode": serde_json::Value::Null,
"mutex_group": serde_json::Value::Null,
"evidence_hint": "依据 plan_type(Free 账号优先调度)",
},
{
"name": "health_first",
"label": "健康优先",
"description": "优先选择健康分更高、失败更少的账号",
"providers": [],
"modes": serde_json::Value::Null,
"default_mode": serde_json::Value::Null,
"mutex_group": serde_json::Value::Null,
"evidence_hint": "依据 health_by_format 聚合分(含熔断/失败衰减)",
},
{
"name": "latency_first",
"label": "延迟优先",
"description": "优先选择最近延迟更低的账号",
"providers": [],
"modes": serde_json::Value::Null,
"default_mode": serde_json::Value::Null,
"mutex_group": serde_json::Value::Null,
"evidence_hint": "依据号池延迟窗口均值(latency_window_seconds)",
},
{
"name": "load_balance",
"label": "负载均衡",
"description": "随机分散 Key 使用,均匀分摊负载",
"providers": [],
"modes": serde_json::Value::Null,
"default_mode": serde_json::Value::Null,
"mutex_group": "distribution_mode",
"evidence_hint": "每次随机分值,实现完全均匀分散",
},
{
"name": "plus_first",
"label": "Plus 优先",
"description": "优先消耗 Plus/Pro 账号(依赖 plan_type)",
"providers": ["codex", "kiro"],
"modes": serde_json::Value::Null,
"default_mode": serde_json::Value::Null,
"mutex_group": serde_json::Value::Null,
"evidence_hint": "依据 plan_type(Plus/Pro 账号优先调度)",
},
{
"name": "priority_first",
"label": "优先级优先",
"description": "按账号优先级顺序调度(数字越小越优先)",
"providers": [],
"modes": serde_json::Value::Null,
"default_mode": serde_json::Value::Null,
"mutex_group": serde_json::Value::Null,
"evidence_hint": "依据 internal_priority(支持拖拽/手工编辑)",
},
{
"name": "quota_balanced",
"label": "额度平均",
"description": "优先选额度消耗最少的账号",
"providers": [],
"modes": serde_json::Value::Null,
"default_mode": serde_json::Value::Null,
"mutex_group": serde_json::Value::Null,
"evidence_hint": "依据账号配额使用率;无配额时回退到窗口成本使用",
},
{
"name": "recent_refresh",
"label": "额度刷新优先",
"description": "优先选即将刷新额度的账号",
"providers": ["codex", "kiro"],
"modes": serde_json::Value::Null,
"default_mode": serde_json::Value::Null,
"mutex_group": serde_json::Value::Null,
"evidence_hint": "依据账号额度重置倒计时(next_reset / reset_seconds)",
},
{
"name": "single_account",
"label": "单号优先",
"description": "集中使用同一账号(反向 LRU)",
"providers": [],
"modes": serde_json::Value::Null,
"default_mode": serde_json::Value::Null,
"mutex_group": "distribution_mode",
"evidence_hint": "先按账号优先级(internal_priority),同级再按反向 LRU 集中",
},
{
"name": "team_first",
"label": "Team 优先",
"description": "优先消耗 Team 账号(依赖 plan_type)",
"providers": ["codex", "kiro"],
"modes": serde_json::Value::Null,
"default_mode": serde_json::Value::Null,
"mutex_group": serde_json::Value::Null,
"evidence_hint": "依据 plan_type(Team 账号优先调度)",
}
])
}
pub(super) async fn maybe_build_local_admin_pool_read_response(
state: &AppState,
request_context: &GatewayPublicRequestContext,
request_body: Option<&axum::body::Bytes>,
) -> Result<Option<Response<Body>>, GatewayError> {
match request_context
.control_decision
.as_ref()
.and_then(|decision| decision.route_kind.as_deref())
{
Some("overview")
if request_context.request_method == http::Method::GET
&& matches!(
request_context.request_path.trim_end_matches('/'),
"/api/admin/pool/overview"
) =>
{
if !state.has_provider_catalog_data_reader() {
return Ok(Some(build_admin_pool_error_response(
http::StatusCode::SERVICE_UNAVAILABLE,
ADMIN_POOL_PROVIDER_CATALOG_READER_UNAVAILABLE_DETAIL,
)));
}
Ok(Some(
Json(build_admin_pool_overview_payload(state).await?).into_response(),
))
}
Some("scheduling_presets")
if request_context.request_method == http::Method::GET
&& matches!(
request_context.request_path.trim_end_matches('/'),
"/api/admin/pool/scheduling-presets"
) =>
{
Ok(Some(
Json(build_admin_pool_scheduling_presets_payload()).into_response(),
))
}
Some("list_keys") => Ok(Some(
build_admin_pool_list_keys_response(state, request_context).await?,
)),
Some("resolve_selection") => Ok(Some(
build_admin_pool_resolve_selection_response(state, request_context, request_body)
.await?,
)),
_ => Ok(None),
}
}
@@ -0,0 +1,146 @@
use super::{
admin_pool_provider_id_from_path, admin_provider_pool_config, build_admin_pool_error_response,
parse_admin_pool_page, parse_admin_pool_page_size, parse_admin_pool_search,
parse_admin_pool_status_filter, pool_payloads, pool_selection,
read_admin_provider_pool_cooldown_key_ids, read_admin_provider_pool_runtime_state,
AdminProviderPoolRuntimeState, ProviderCatalogKeyListQuery,
ADMIN_POOL_PROVIDER_CATALOG_READER_UNAVAILABLE_DETAIL,
};
use crate::handlers::admin::request::{AdminAppState, AdminRequestContext};
use crate::GatewayError;
use axum::{
body::Body,
http,
response::{IntoResponse, Response},
Json,
};
use serde_json::json;
pub(super) async fn build_admin_pool_list_keys_response(
state: &AdminAppState<'_>,
request_context: &AdminRequestContext<'_>,
) -> Result<Response<Body>, GatewayError> {
if !state.has_provider_catalog_data_reader() {
return Ok(build_admin_pool_error_response(
http::StatusCode::SERVICE_UNAVAILABLE,
ADMIN_POOL_PROVIDER_CATALOG_READER_UNAVAILABLE_DETAIL,
));
}
let Some(provider_id) = admin_pool_provider_id_from_path(request_context.path()) else {
return Ok(build_admin_pool_error_response(
http::StatusCode::BAD_REQUEST,
"provider_id 无效",
));
};
let query = request_context.query_string();
let page = match parse_admin_pool_page(query) {
Ok(value) => value,
Err(detail) => {
return Ok(build_admin_pool_error_response(
http::StatusCode::BAD_REQUEST,
detail,
));
}
};
let page_size = match parse_admin_pool_page_size(query) {
Ok(value) => value,
Err(detail) => {
return Ok(build_admin_pool_error_response(
http::StatusCode::BAD_REQUEST,
detail,
));
}
};
let search = parse_admin_pool_search(query).map(|value| value.to_ascii_lowercase());
let status = match parse_admin_pool_status_filter(query) {
Ok(value) => value,
Err(detail) => {
return Ok(build_admin_pool_error_response(
http::StatusCode::BAD_REQUEST,
detail,
));
}
};
let Some(provider) = state
.read_provider_catalog_providers_by_ids(std::slice::from_ref(&provider_id))
.await?
.into_iter()
.next()
else {
return Ok(build_admin_pool_error_response(
http::StatusCode::NOT_FOUND,
format!("Provider {provider_id} 不存在"),
));
};
let pool_config = admin_provider_pool_config(&provider);
let page_offset = page.saturating_sub(1).saturating_mul(page_size);
let (keys, total) = if status == "cooldown" {
let cooldown_key_ids = if let Some(runner) = state.redis_kv_runner() {
read_admin_provider_pool_cooldown_key_ids(&runner, &provider.id).await
} else {
Vec::new()
};
let mut keys = if cooldown_key_ids.is_empty() {
Vec::new()
} else {
state
.read_provider_catalog_keys_by_ids(&cooldown_key_ids)
.await?
};
if let Some(keyword) = search.as_ref() {
keys.retain(|key| {
key.name.to_ascii_lowercase().contains(keyword)
|| key.id.to_ascii_lowercase().contains(keyword)
});
}
pool_selection::admin_pool_sort_keys(&mut keys);
let total = keys.len();
let keys = keys
.into_iter()
.skip(page_offset)
.take(page_size)
.collect::<Vec<_>>();
(keys, total)
} else {
let key_page = state
.list_provider_catalog_key_page(&ProviderCatalogKeyListQuery {
provider_id: provider.id.clone(),
search: search.clone(),
is_active: match status.as_str() {
"active" => Some(true),
"inactive" => Some(false),
_ => None,
},
offset: page_offset,
limit: page_size,
})
.await?;
(key_page.items, key_page.total)
};
let key_ids = keys.iter().map(|key| key.id.clone()).collect::<Vec<_>>();
let runtime = match (state.redis_kv_runner(), pool_config) {
(Some(runner), Some(pool_config)) if !key_ids.is_empty() => {
read_admin_provider_pool_runtime_state(&runner, &provider.id, &key_ids, pool_config)
.await
}
_ => AdminProviderPoolRuntimeState::default(),
};
let items = keys
.into_iter()
.map(|key| pool_payloads::build_admin_pool_key_payload(&key, &runtime, pool_config))
.collect::<Vec<_>>();
Ok(Json(json!({
"total": total,
"page": page,
"page_size": page_size,
"keys": items,
}))
.into_response())
}
@@ -0,0 +1,70 @@
use super::{
admin_provider_pool_config, build_admin_pool_error_response,
read_admin_provider_pool_cooldown_counts,
ADMIN_POOL_PROVIDER_CATALOG_READER_UNAVAILABLE_DETAIL,
};
use crate::handlers::admin::request::AdminAppState;
use crate::GatewayError;
use aether_admin::provider::pool as admin_provider_pool_pure;
use axum::{
body::Body,
http,
response::{IntoResponse, Response},
Json,
};
use std::collections::BTreeMap;
pub(super) async fn build_admin_pool_overview_response(
state: &AdminAppState<'_>,
) -> Result<Response<Body>, GatewayError> {
if !state.has_provider_catalog_data_reader() {
return Ok(build_admin_pool_error_response(
http::StatusCode::SERVICE_UNAVAILABLE,
ADMIN_POOL_PROVIDER_CATALOG_READER_UNAVAILABLE_DETAIL,
));
}
let providers = state.list_provider_catalog_providers(false).await?;
let pool_enabled_providers = providers
.into_iter()
.filter_map(|provider| {
admin_provider_pool_config(&provider).map(|config| (provider, config))
})
.collect::<Vec<_>>();
let provider_ids = pool_enabled_providers
.iter()
.map(|(provider, _)| provider.id.clone())
.collect::<Vec<_>>();
let key_stats = if provider_ids.is_empty() {
Vec::new()
} else {
state
.list_provider_catalog_key_stats_by_provider_ids(&provider_ids)
.await?
};
let key_stats_by_provider = key_stats
.into_iter()
.map(|item| (item.provider_id.clone(), item))
.collect::<BTreeMap<_, _>>();
let redis_runner = state.redis_kv_runner();
let cooldown_counts_by_provider = match redis_runner.as_ref() {
Some(runner) if !provider_ids.is_empty() => {
read_admin_provider_pool_cooldown_counts(runner, &provider_ids).await
}
_ => BTreeMap::new(),
};
let providers = pool_enabled_providers
.into_iter()
.map(|(provider, _)| provider)
.collect::<Vec<_>>();
Ok(
Json(admin_provider_pool_pure::build_admin_pool_overview_payload(
&providers,
&key_stats_by_provider,
&cooldown_counts_by_provider,
))
.into_response(),
)
}
@@ -0,0 +1,10 @@
use aether_admin::provider::pool as admin_provider_pool_pure;
use axum::{
body::Body,
response::{IntoResponse, Response},
Json,
};
pub(super) fn build_admin_pool_scheduling_presets_response() -> Response<Body> {
Json(admin_provider_pool_pure::build_admin_pool_scheduling_presets_payload()).into_response()
}
@@ -0,0 +1,92 @@
use super::{
admin_pool_provider_id_from_path, build_admin_pool_error_response, pool_selection,
AdminPoolResolveSelectionRequest, ADMIN_POOL_PROVIDER_CATALOG_READER_UNAVAILABLE_DETAIL,
};
use crate::handlers::admin::request::{AdminAppState, AdminRequestContext};
use crate::GatewayError;
use aether_admin::provider::pool as admin_provider_pool_pure;
use axum::{
body::{Body, Bytes},
http,
response::{IntoResponse, Response},
Json,
};
pub(super) async fn build_admin_pool_resolve_selection_response(
state: &AdminAppState<'_>,
request_context: &AdminRequestContext<'_>,
request_body: Option<&Bytes>,
) -> Result<Response<Body>, GatewayError> {
if !state.has_provider_catalog_data_reader() {
return Ok(build_admin_pool_error_response(
http::StatusCode::SERVICE_UNAVAILABLE,
ADMIN_POOL_PROVIDER_CATALOG_READER_UNAVAILABLE_DETAIL,
));
}
let Some(provider_id) = admin_pool_provider_id_from_path(request_context.path()) else {
return Ok(build_admin_pool_error_response(
http::StatusCode::BAD_REQUEST,
"provider_id 无效",
));
};
let payload = match request_body {
None => AdminPoolResolveSelectionRequest::default(),
Some(body) if body.is_empty() => AdminPoolResolveSelectionRequest::default(),
Some(body) => match serde_json::from_slice::<AdminPoolResolveSelectionRequest>(body) {
Ok(value) => value,
Err(_) => {
return Ok(build_admin_pool_error_response(
http::StatusCode::BAD_REQUEST,
"Invalid JSON request body",
));
}
},
};
let Some(provider) = state
.read_provider_catalog_providers_by_ids(std::slice::from_ref(&provider_id))
.await?
.into_iter()
.next()
else {
return Ok(build_admin_pool_error_response(
http::StatusCode::NOT_FOUND,
format!("Provider {provider_id} 不存在"),
));
};
let provider_type = provider.provider_type.clone();
let search = payload.search.trim();
let quick_selectors =
admin_provider_pool_pure::admin_pool_sanitize_quick_selectors(payload.quick_selectors);
let mut keys = state
.list_provider_catalog_keys_by_provider_ids(std::slice::from_ref(&provider.id))
.await?
.into_iter()
.filter(|key| {
pool_selection::admin_pool_matches_search(state, key, &provider_type, Some(search))
})
.filter(|key| {
quick_selectors.is_empty()
|| quick_selectors.iter().all(|selector| {
pool_selection::admin_pool_matches_quick_selector(
state,
key,
&provider_type,
selector,
)
})
})
.collect::<Vec<_>>();
keys.sort_by(|left, right| {
left.internal_priority
.cmp(&right.internal_priority)
.then_with(|| left.name.cmp(&right.name))
});
Ok(Json(admin_provider_pool_pure::build_admin_pool_selection_payload(&keys)).into_response())
}
@@ -1,40 +1,20 @@
use crate::handlers::admin::shared::decrypt_catalog_secret_with_fallbacks;
use crate::AppState;
use crate::handlers::admin::request::AdminAppState;
use aether_admin::provider::pool as admin_provider_pool_pure;
use aether_data_contracts::repository::provider_catalog::StoredProviderCatalogKey;
fn admin_pool_reason_indicates_ban(reason: &str) -> bool {
let normalized = reason.trim().to_ascii_lowercase();
!normalized.is_empty()
&& [
"banned",
"forbidden",
"blocked",
"suspend",
"deactivated",
"disabled",
"verification",
"workspace",
"受限",
"封",
"禁",
]
.iter()
.any(|hint| normalized.contains(hint))
}
pub(super) fn admin_pool_normalize_text(value: impl AsRef<str>) -> String {
value.as_ref().trim().to_ascii_lowercase()
admin_provider_pool_pure::admin_pool_normalize_text(value)
}
fn admin_pool_parse_auth_config_json(
state: &AppState,
state: &AdminAppState<'_>,
key: &StoredProviderCatalogKey,
) -> Option<serde_json::Map<String, serde_json::Value>> {
let ciphertext = key.encrypted_auth_config.as_deref()?.trim();
if ciphertext.is_empty() {
return None;
}
let plaintext = decrypt_catalog_secret_with_fallbacks(state.encryption_key(), ciphertext)?;
let plaintext = state.decrypt_catalog_secret_with_fallbacks(ciphertext)?;
serde_json::from_str::<serde_json::Value>(&plaintext)
.ok()?
.as_object()
@@ -42,7 +22,7 @@ fn admin_pool_parse_auth_config_json(
}
fn admin_pool_derive_oauth_plan_type(
state: &AppState,
state: &AdminAppState<'_>,
key: &StoredProviderCatalogKey,
provider_type: &str,
) -> Option<String> {
@@ -106,141 +86,35 @@ fn admin_pool_derive_oauth_plan_type(
None
}
fn admin_pool_has_proxy(key: &StoredProviderCatalogKey) -> bool {
match key.proxy.as_ref() {
Some(serde_json::Value::Object(values)) => !values.is_empty(),
Some(serde_json::Value::String(value)) => !value.trim().is_empty(),
Some(serde_json::Value::Bool(value)) => *value,
Some(serde_json::Value::Number(_)) => true,
Some(serde_json::Value::Array(values)) => !values.is_empty(),
_ => false,
}
}
fn admin_pool_is_oauth_invalid(key: &StoredProviderCatalogKey) -> bool {
if key.auth_type.trim() != "oauth" {
return false;
}
if key
.oauth_invalid_reason
.as_deref()
.is_some_and(|value| !value.trim().is_empty())
{
return true;
}
key.expires_at_unix_secs
.is_some_and(|value| value > 0 && value <= chrono::Utc::now().timestamp().max(0) as u64)
}
pub(super) fn admin_pool_matches_quick_selector(
state: &AppState,
state: &AdminAppState<'_>,
key: &StoredProviderCatalogKey,
provider_type: &str,
selector: &str,
) -> bool {
match selector {
"banned" => admin_pool_key_is_known_banned(key),
"oauth_invalid" => admin_pool_is_oauth_invalid(key),
"proxy_unset" => !admin_pool_has_proxy(key),
"proxy_set" => admin_pool_has_proxy(key),
"disabled" => !key.is_active,
"enabled" => key.is_active,
"plan_free" => admin_pool_derive_oauth_plan_type(state, key, provider_type)
.is_some_and(|value| value.contains("free")),
"plan_team" => admin_pool_derive_oauth_plan_type(state, key, provider_type)
.is_some_and(|value| value.contains("team")),
"no_5h_limit" | "no_weekly_limit" => false,
_ => false,
}
let oauth_plan_type = admin_pool_derive_oauth_plan_type(state, key, provider_type);
admin_provider_pool_pure::admin_pool_matches_quick_selector(
key,
selector,
oauth_plan_type.as_deref(),
admin_provider_pool_pure::admin_pool_now_unix_secs(),
)
}
pub(super) fn admin_pool_matches_search(
state: &AppState,
state: &AdminAppState<'_>,
key: &StoredProviderCatalogKey,
provider_type: &str,
search: Option<&str>,
) -> bool {
let Some(search) = search else {
return true;
};
let search = admin_pool_normalize_text(search);
if search.is_empty() {
return true;
}
let oauth_plan_type = admin_pool_derive_oauth_plan_type(state, key, provider_type);
let mut search_fields = vec![
key.id.clone(),
key.name.clone(),
key.auth_type.clone(),
if key.is_active {
"已启用".to_string()
} else {
"已禁用".to_string()
},
if admin_pool_has_proxy(key) {
"独立代理".to_string()
} else {
"未配置代理".to_string()
},
];
if let Some(reason) = key.oauth_invalid_reason.as_ref() {
search_fields.push(reason.clone());
}
if let Some(note) = key.note.as_ref() {
search_fields.push(note.clone());
}
if let Some(plan_type) = oauth_plan_type {
search_fields.push(plan_type);
}
search_fields
.into_iter()
.any(|value| admin_pool_normalize_text(&value).contains(&search))
admin_provider_pool_pure::admin_pool_matches_search(key, search, oauth_plan_type.as_deref())
}
pub(super) fn admin_pool_key_is_known_banned(key: &StoredProviderCatalogKey) -> bool {
if key
.oauth_invalid_reason
.as_deref()
.is_some_and(admin_pool_reason_indicates_ban)
{
return true;
}
let Some(account) = key
.status_snapshot
.as_ref()
.and_then(serde_json::Value::as_object)
.and_then(|snapshot| snapshot.get("account"))
.and_then(serde_json::Value::as_object)
else {
return false;
};
if !account
.get("blocked")
.and_then(serde_json::Value::as_bool)
.unwrap_or(false)
{
return false;
}
account
.get("code")
.and_then(serde_json::Value::as_str)
.is_some_and(admin_pool_reason_indicates_ban)
|| account
.get("reason")
.and_then(serde_json::Value::as_str)
.is_some_and(admin_pool_reason_indicates_ban)
admin_provider_pool_pure::admin_pool_key_is_known_banned(key)
}
pub(super) fn admin_pool_sort_keys(keys: &mut [StoredProviderCatalogKey]) {
keys.sort_by(|left, right| {
left.internal_priority
.cmp(&right.internal_priority)
.then(left.name.cmp(&right.name))
.then(left.id.cmp(&right.id))
});
admin_provider_pool_pure::admin_pool_sort_keys(keys);
}
@@ -1,37 +1,28 @@
use crate::control::GatewayPublicRequestContext;
use crate::handlers::admin::request::AdminRequestContext;
use crate::handlers::admin::shared::query_param_value;
use crate::{AppState, GatewayError};
pub(crate) use aether_admin::provider::pool::AdminPoolResolveSelectionRequest;
use axum::{
body::{Body, Bytes},
body::Body,
http,
response::{IntoResponse, Response},
Json,
};
use serde::Deserialize;
use serde_json::json;
pub(super) const ADMIN_POOL_PROVIDER_CATALOG_READER_UNAVAILABLE_DETAIL: &str =
pub(crate) const ADMIN_POOL_PROVIDER_CATALOG_READER_UNAVAILABLE_DETAIL: &str =
"Admin pool overview requires provider catalog reader";
pub(super) const ADMIN_POOL_PROVIDER_CATALOG_WRITER_UNAVAILABLE_DETAIL: &str =
pub(crate) const ADMIN_POOL_PROVIDER_CATALOG_WRITER_UNAVAILABLE_DETAIL: &str =
"Admin pool cleanup requires provider catalog writer";
pub(super) const ADMIN_POOL_BANNED_KEY_CLEANUP_EMPTY_MESSAGE: &str = "未发现可清理的异常账号";
pub(crate) const ADMIN_POOL_BANNED_KEY_CLEANUP_EMPTY_MESSAGE: &str = "未发现可清理的异常账号";
#[derive(Debug, Default, Deserialize)]
pub(super) struct AdminPoolResolveSelectionRequest {
#[serde(default)]
pub(crate) search: String,
#[serde(default)]
pub(crate) quick_selectors: Vec<String>,
}
pub(super) fn build_admin_pool_error_response(
pub(crate) fn build_admin_pool_error_response(
status: http::StatusCode,
detail: impl Into<String>,
) -> Response<Body> {
(status, Json(json!({ "detail": detail.into() }))).into_response()
}
pub(super) fn parse_admin_pool_page(query: Option<&str>) -> Result<usize, String> {
pub(crate) fn parse_admin_pool_page(query: Option<&str>) -> Result<usize, String> {
match query_param_value(query, "page") {
None => Ok(1),
Some(value) => {
@@ -47,7 +38,7 @@ pub(super) fn parse_admin_pool_page(query: Option<&str>) -> Result<usize, String
}
}
pub(super) fn parse_admin_pool_page_size(query: Option<&str>) -> Result<usize, String> {
pub(crate) fn parse_admin_pool_page_size(query: Option<&str>) -> Result<usize, String> {
match query_param_value(query, "page_size") {
None => Ok(50),
Some(value) => {
@@ -63,13 +54,13 @@ pub(super) fn parse_admin_pool_page_size(query: Option<&str>) -> Result<usize, S
}
}
pub(super) fn parse_admin_pool_search(query: Option<&str>) -> Option<String> {
pub(crate) fn parse_admin_pool_search(query: Option<&str>) -> Option<String> {
query_param_value(query, "search")
.map(|value| value.trim().to_string())
.filter(|value| !value.is_empty())
}
pub(super) fn parse_admin_pool_status_filter(query: Option<&str>) -> Result<String, String> {
pub(crate) fn parse_admin_pool_status_filter(query: Option<&str>) -> Result<String, String> {
let value = query_param_value(query, "status")
.unwrap_or_else(|| "all".to_string())
.trim()
@@ -80,7 +71,7 @@ pub(super) fn parse_admin_pool_status_filter(query: Option<&str>) -> Result<Stri
}
}
pub(super) fn admin_pool_provider_id_from_path(request_path: &str) -> Option<String> {
pub(crate) fn admin_pool_provider_id_from_path(request_path: &str) -> Option<String> {
let raw = request_path.strip_prefix("/api/admin/pool/")?;
let mut segments = raw.split('/');
let provider_id = segments.next()?.trim();
@@ -92,38 +83,38 @@ pub(super) fn admin_pool_provider_id_from_path(request_path: &str) -> Option<Str
}
}
pub(super) fn is_admin_pool_route(request_context: &GatewayPublicRequestContext) -> bool {
let normalized_path = request_context.request_path.trim_end_matches('/');
pub(crate) fn is_admin_pool_route(request_context: &AdminRequestContext<'_>) -> bool {
let normalized_path = request_context.path().trim_end_matches('/');
let path = if normalized_path.is_empty() {
request_context.request_path.as_str()
request_context.path()
} else {
normalized_path
};
(request_context.request_method == http::Method::GET && path == "/api/admin/pool/overview")
|| (request_context.request_method == http::Method::GET
(request_context.method() == http::Method::GET && path == "/api/admin/pool/overview")
|| (request_context.method() == http::Method::GET
&& path == "/api/admin/pool/scheduling-presets")
|| (request_context.request_method == http::Method::GET
|| (request_context.method() == http::Method::GET
&& path.starts_with("/api/admin/pool/")
&& path.ends_with("/keys")
&& path.matches('/').count() == 5)
|| (request_context.request_method == http::Method::POST
|| (request_context.method() == http::Method::POST
&& path.starts_with("/api/admin/pool/")
&& path.ends_with("/keys/batch-import")
&& path.matches('/').count() == 6)
|| (request_context.request_method == http::Method::POST
|| (request_context.method() == http::Method::POST
&& path.starts_with("/api/admin/pool/")
&& path.ends_with("/keys/batch-action")
&& path.matches('/').count() == 6)
|| (request_context.request_method == http::Method::POST
|| (request_context.method() == http::Method::POST
&& path.starts_with("/api/admin/pool/")
&& path.ends_with("/keys/resolve-selection")
&& path.matches('/').count() == 6)
|| (request_context.request_method == http::Method::GET
|| (request_context.method() == http::Method::GET
&& path.starts_with("/api/admin/pool/")
&& path.contains("/keys/batch-delete-task/")
&& path.matches('/').count() == 7)
|| (request_context.request_method == http::Method::POST
|| (request_context.method() == http::Method::POST
&& path.starts_with("/api/admin/pool/")
&& path.ends_with("/keys/cleanup-banned")
&& path.matches('/').count() == 6)