refactor: 大规模模块拆分与代码精简,新增 ai-pipeline/data-contracts 独立 crate

- 新增 aether-ai-pipeline 和 aether-data-contracts crate,将 pipeline 逻辑与数据契约从 gateway 中解耦
- 重构 admin handlers:拆分单体模块为 auth/billing/endpoint/features/model/observability/provider/system 等独立子模块
- 合并 chat/cli 重复代码路径:精简 conversion、finalize、planner 中的 sync/chat/cli 分支
- 重构 scheduler/executor/data 层,引入 facade 模式降低模块间耦合
- 移除冗余的 intent 模块,将 plan_fallback/policy/stream_path/sync_path 迁移至 executor
- 前端适配:调整 admin API 调用和 provider 模型测试对话框
This commit is contained in:
fawney19
2026-04-07 02:50:19 +08:00
parent 763ff03a7b
commit 5d96d6673b
732 changed files with 28589 additions and 20662 deletions
@@ -0,0 +1,18 @@
use axum::{
body::Body,
http,
response::{IntoResponse, Response},
Json,
};
use serde_json::json;
pub(super) const ADMIN_GLOBAL_MODELS_DATA_UNAVAILABLE_DETAIL: &str =
"Admin global model data unavailable";
pub(super) fn build_admin_global_models_data_unavailable_response() -> Response<Body> {
(
http::StatusCode::SERVICE_UNAVAILABLE,
Json(json!({ "detail": ADMIN_GLOBAL_MODELS_DATA_UNAVAILABLE_DETAIL })),
)
.into_response()
}
@@ -0,0 +1,5 @@
mod helpers;
mod routes;
use self::helpers::*;
pub(crate) use self::routes::maybe_build_local_admin_global_models_response;
@@ -0,0 +1,525 @@
use super::super::{
build_admin_assign_global_model_to_providers_payload, build_admin_global_model_create_record,
build_admin_global_model_payload, build_admin_global_model_providers_payload,
build_admin_global_model_response, build_admin_global_model_routing_payload,
build_admin_global_model_update_record, build_admin_global_models_payload,
resolve_admin_global_model_by_id_or_err,
};
use super::helpers::{
build_admin_global_models_data_unavailable_response,
ADMIN_GLOBAL_MODELS_DATA_UNAVAILABLE_DETAIL,
};
use crate::control::GatewayPublicRequestContext;
use crate::handlers::admin::model::shared::{
admin_global_model_assign_to_providers_id, admin_global_model_id_from_path,
admin_global_model_providers_id, admin_global_model_routing_id, is_admin_global_models_root,
AdminBatchAssignToProvidersRequest, AdminBatchDeleteIdsRequest, AdminGlobalModelCreateRequest,
AdminGlobalModelUpdateRequest,
};
use crate::handlers::admin::shared::attach_admin_audit_response;
use crate::handlers::admin::shared::{query_param_optional_bool, query_param_value};
use crate::{AppState, GatewayError};
use axum::{
body::{Body, Bytes},
http,
response::{IntoResponse, Response},
Json,
};
use serde_json::json;
use std::time::{SystemTime, UNIX_EPOCH};
pub(crate) async fn maybe_build_local_admin_global_models_response(
state: &AppState,
request_context: &GatewayPublicRequestContext,
request_body: Option<&Bytes>,
) -> Result<Option<Response<Body>>, GatewayError> {
let Some(decision) = request_context.control_decision.as_ref() else {
return Ok(None);
};
if decision.route_family.as_deref() == Some("global_models_manage")
&& decision.route_kind.as_deref() == Some("routing_preview")
&& request_context.request_method == http::Method::GET
{
if !state.has_global_model_data_reader() || !state.has_provider_catalog_data_reader() {
return Ok(Some(build_admin_global_models_data_unavailable_response()));
}
let Some(global_model_id) = admin_global_model_routing_id(&request_context.request_path)
else {
return Ok(Some(
(
http::StatusCode::NOT_FOUND,
Json(json!({ "detail": "GlobalModel 不存在" })),
)
.into_response(),
));
};
return Ok(Some(
match build_admin_global_model_routing_payload(state, &global_model_id).await {
Some(payload) => Json(payload).into_response(),
None => (
http::StatusCode::NOT_FOUND,
Json(json!({ "detail": format!("GlobalModel {global_model_id} 不存在") })),
)
.into_response(),
},
));
}
if decision.route_family.as_deref() == Some("global_models_manage")
&& decision.route_kind.as_deref() == Some("list_global_models")
&& is_admin_global_models_root(&request_context.request_path)
{
if !state.has_global_model_data_reader() {
return Ok(Some(build_admin_global_models_data_unavailable_response()));
}
let skip = query_param_value(request_context.request_query_string.as_deref(), "skip")
.and_then(|value| value.parse::<usize>().ok())
.unwrap_or(0);
let limit = query_param_value(request_context.request_query_string.as_deref(), "limit")
.and_then(|value| value.parse::<usize>().ok())
.filter(|value| *value > 0 && *value <= 1000)
.unwrap_or(100);
let is_active =
query_param_optional_bool(request_context.request_query_string.as_deref(), "is_active");
let search = query_param_value(request_context.request_query_string.as_deref(), "search");
let Some(payload) =
build_admin_global_models_payload(state, skip, limit, is_active, search).await
else {
return Ok(Some(build_admin_global_models_data_unavailable_response()));
};
return Ok(Some(Json(payload).into_response()));
}
if decision.route_family.as_deref() == Some("global_models_manage")
&& decision.route_kind.as_deref() == Some("get_global_model")
&& request_context.request_method == http::Method::GET
{
if !state.has_global_model_data_reader() {
return Ok(Some(build_admin_global_models_data_unavailable_response()));
}
let Some(global_model_id) = admin_global_model_id_from_path(&request_context.request_path)
else {
return Ok(Some(
(
http::StatusCode::NOT_FOUND,
Json(json!({ "detail": "GlobalModel 不存在" })),
)
.into_response(),
));
};
return Ok(Some(
match build_admin_global_model_payload(state, &global_model_id).await {
Some(payload) => Json(payload).into_response(),
None => (
http::StatusCode::NOT_FOUND,
Json(json!({ "detail": format!("GlobalModel {global_model_id} 不存在") })),
)
.into_response(),
},
));
}
if decision.route_family.as_deref() == Some("global_models_manage")
&& decision.route_kind.as_deref() == Some("create_global_model")
&& request_context.request_method == http::Method::POST
&& is_admin_global_models_root(&request_context.request_path)
{
if !state.has_global_model_data_reader() || !state.has_global_model_data_writer() {
return Ok(Some(build_admin_global_models_data_unavailable_response()));
}
let Some(request_body) = request_body else {
return Ok(Some(
(
http::StatusCode::BAD_REQUEST,
Json(json!({ "detail": "请求体不能为空" })),
)
.into_response(),
));
};
let payload = match serde_json::from_slice::<AdminGlobalModelCreateRequest>(request_body) {
Ok(payload) => payload,
Err(_) => {
return Ok(Some(
(
http::StatusCode::BAD_REQUEST,
Json(json!({ "detail": "请求体必须是合法的 JSON 对象" })),
)
.into_response(),
));
}
};
let record = match build_admin_global_model_create_record(state, payload).await {
Ok(record) => record,
Err(detail) => {
return Ok(Some(
(
http::StatusCode::BAD_REQUEST,
Json(json!({ "detail": detail })),
)
.into_response(),
));
}
};
return Ok(Some(
match state.create_admin_global_model(&record).await? {
Some(created) => {
let provider_models = state
.list_admin_provider_models_by_global_model_id(&created.id)
.await
.unwrap_or_default();
let now_unix_secs = SystemTime::now()
.duration_since(UNIX_EPOCH)
.ok()
.map(|duration| duration.as_secs())
.unwrap_or(0);
attach_admin_audit_response(
(
http::StatusCode::CREATED,
Json(build_admin_global_model_response(
&created,
&provider_models,
now_unix_secs,
)),
)
.into_response(),
"admin_global_model_created",
"create_global_model",
"global_model",
&created.id,
)
}
None => (
http::StatusCode::SERVICE_UNAVAILABLE,
Json(json!({ "detail": ADMIN_GLOBAL_MODELS_DATA_UNAVAILABLE_DETAIL })),
)
.into_response(),
},
));
}
if decision.route_family.as_deref() == Some("global_models_manage")
&& decision.route_kind.as_deref() == Some("update_global_model")
&& request_context.request_method == http::Method::PATCH
{
if !state.has_global_model_data_reader() || !state.has_global_model_data_writer() {
return Ok(Some(build_admin_global_models_data_unavailable_response()));
}
let Some(global_model_id) = admin_global_model_id_from_path(&request_context.request_path)
else {
return Ok(Some(
(
http::StatusCode::NOT_FOUND,
Json(json!({ "detail": "GlobalModel 不存在" })),
)
.into_response(),
));
};
let existing = match resolve_admin_global_model_by_id_or_err(state, &global_model_id).await
{
Ok(model) => model,
Err(detail) => {
return Ok(Some(
(
http::StatusCode::NOT_FOUND,
Json(json!({ "detail": detail })),
)
.into_response(),
));
}
};
let Some(request_body) = request_body else {
return Ok(Some(
(
http::StatusCode::BAD_REQUEST,
Json(json!({ "detail": "请求体不能为空" })),
)
.into_response(),
));
};
let raw_value = match serde_json::from_slice::<serde_json::Value>(request_body) {
Ok(value) => value,
Err(_) => {
return Ok(Some(
(
http::StatusCode::BAD_REQUEST,
Json(json!({ "detail": "请求体必须是合法的 JSON 对象" })),
)
.into_response(),
));
}
};
let Some(raw_payload) = raw_value.as_object().cloned() else {
return Ok(Some(
(
http::StatusCode::BAD_REQUEST,
Json(json!({ "detail": "请求体必须是合法的 JSON 对象" })),
)
.into_response(),
));
};
let payload = match serde_json::from_value::<AdminGlobalModelUpdateRequest>(raw_value) {
Ok(payload) => payload,
Err(_) => {
return Ok(Some(
(
http::StatusCode::BAD_REQUEST,
Json(json!({ "detail": "请求体必须是合法的 JSON 对象" })),
)
.into_response(),
));
}
};
let record =
match build_admin_global_model_update_record(state, &existing, &raw_payload, payload)
.await
{
Ok(record) => record,
Err(detail) => {
return Ok(Some(
(
http::StatusCode::BAD_REQUEST,
Json(json!({ "detail": detail })),
)
.into_response(),
));
}
};
return Ok(Some(
match state.update_admin_global_model(&record).await? {
Some(updated) => {
let provider_models = state
.list_admin_provider_models_by_global_model_id(&updated.id)
.await
.unwrap_or_default();
let now_unix_secs = SystemTime::now()
.duration_since(UNIX_EPOCH)
.ok()
.map(|duration| duration.as_secs())
.unwrap_or(0);
attach_admin_audit_response(
Json(build_admin_global_model_response(
&updated,
&provider_models,
now_unix_secs,
))
.into_response(),
"admin_global_model_updated",
"update_global_model",
"global_model",
&updated.id,
)
}
None => (
http::StatusCode::NOT_FOUND,
Json(json!({ "detail": format!("GlobalModel {} 不存在", existing.id) })),
)
.into_response(),
},
));
}
if decision.route_family.as_deref() == Some("global_models_manage")
&& decision.route_kind.as_deref() == Some("delete_global_model")
&& request_context.request_method == http::Method::DELETE
{
if !state.has_global_model_data_reader() || !state.has_global_model_data_writer() {
return Ok(Some(build_admin_global_models_data_unavailable_response()));
}
let Some(global_model_id) = admin_global_model_id_from_path(&request_context.request_path)
else {
return Ok(Some(
(
http::StatusCode::NOT_FOUND,
Json(json!({ "detail": "GlobalModel 不存在" })),
)
.into_response(),
));
};
let existing = match resolve_admin_global_model_by_id_or_err(state, &global_model_id).await
{
Ok(model) => model,
Err(detail) => {
return Ok(Some(
(
http::StatusCode::NOT_FOUND,
Json(json!({ "detail": detail })),
)
.into_response(),
));
}
};
if !state.delete_admin_global_model(&existing.id).await? {
return Ok(Some(
(
http::StatusCode::NOT_FOUND,
Json(json!({ "detail": format!("GlobalModel {} 不存在", existing.id) })),
)
.into_response(),
));
}
return Ok(Some(attach_admin_audit_response(
http::StatusCode::NO_CONTENT.into_response(),
"admin_global_model_deleted",
"delete_global_model",
"global_model",
&existing.id,
)));
}
if decision.route_family.as_deref() == Some("global_models_manage")
&& decision.route_kind.as_deref() == Some("batch_delete_global_models")
&& request_context.request_method == http::Method::POST
&& request_context.request_path == "/api/admin/models/global/batch-delete"
{
if !state.has_global_model_data_reader() || !state.has_global_model_data_writer() {
return Ok(Some(build_admin_global_models_data_unavailable_response()));
}
let Some(request_body) = request_body else {
return Ok(Some(
(
http::StatusCode::BAD_REQUEST,
Json(json!({ "detail": "请求体不能为空" })),
)
.into_response(),
));
};
let payload = match serde_json::from_slice::<AdminBatchDeleteIdsRequest>(request_body) {
Ok(payload) => payload,
Err(_) => {
return Ok(Some(
(
http::StatusCode::BAD_REQUEST,
Json(json!({ "detail": "请求体必须是合法的 JSON 对象" })),
)
.into_response(),
));
}
};
let mut success_count = 0usize;
let mut failed = Vec::new();
for id in payload.ids {
let trimmed = id.trim();
if trimmed.is_empty() {
failed.push(json!({"id": id, "error": "not found"}));
continue;
}
let Some(existing) = state.get_admin_global_model_by_id(trimmed).await? else {
failed.push(json!({"id": trimmed, "error": "not found"}));
continue;
};
if state.delete_admin_global_model(&existing.id).await? {
success_count += 1;
} else {
failed.push(json!({"id": existing.id, "error": "delete failed"}));
}
}
return Ok(Some(attach_admin_audit_response(
Json(json!({
"success_count": success_count,
"failed": failed,
}))
.into_response(),
"admin_global_models_batch_deleted",
"batch_delete_global_models",
"global_models_batch",
"batch",
)));
}
if decision.route_family.as_deref() == Some("global_models_manage")
&& decision.route_kind.as_deref() == Some("assign_to_providers")
&& request_context.request_method == http::Method::POST
{
let Some(global_model_id) =
admin_global_model_assign_to_providers_id(&request_context.request_path)
else {
return Ok(Some(
(
http::StatusCode::NOT_FOUND,
Json(json!({ "detail": "GlobalModel 不存在" })),
)
.into_response(),
));
};
let Some(request_body) = request_body else {
return Ok(Some(
(
http::StatusCode::BAD_REQUEST,
Json(json!({ "detail": "请求体不能为空" })),
)
.into_response(),
));
};
let payload =
match serde_json::from_slice::<AdminBatchAssignToProvidersRequest>(request_body) {
Ok(payload) => payload,
Err(_) => {
return Ok(Some(
(
http::StatusCode::BAD_REQUEST,
Json(json!({ "detail": "请求体必须是合法的 JSON 对象" })),
)
.into_response(),
));
}
};
let payload = match build_admin_assign_global_model_to_providers_payload(
state,
&global_model_id,
payload.provider_ids,
payload.create_models.unwrap_or(false),
)
.await
{
Ok(payload) => payload,
Err(detail) => {
return Ok(Some(
(
http::StatusCode::BAD_REQUEST,
Json(json!({ "detail": detail })),
)
.into_response(),
));
}
};
return Ok(Some(attach_admin_audit_response(
Json(payload).into_response(),
"admin_global_model_assigned_to_providers",
"assign_global_model_to_providers",
"global_model",
&global_model_id,
)));
}
if decision.route_family.as_deref() == Some("global_models_manage")
&& decision.route_kind.as_deref() == Some("global_model_providers")
&& request_context.request_method == http::Method::GET
{
if !state.has_global_model_data_reader() || !state.has_provider_catalog_data_reader() {
return Ok(Some(build_admin_global_models_data_unavailable_response()));
}
let Some(global_model_id) = admin_global_model_providers_id(&request_context.request_path)
else {
return Ok(Some(
(
http::StatusCode::NOT_FOUND,
Json(json!({ "detail": "GlobalModel 不存在" })),
)
.into_response(),
));
};
return Ok(Some(
match build_admin_global_model_providers_payload(state, &global_model_id).await {
Some(payload) => Json(payload).into_response(),
None => (
http::StatusCode::NOT_FOUND,
Json(json!({ "detail": format!("GlobalModel {global_model_id} 不存在") })),
)
.into_response(),
},
));
}
Ok(None)
}