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
@@ -4,9 +4,10 @@ use chrono::Utc;
use sqlx::Row;
use crate::data::GatewayDataState;
use aether_data_contracts::DataLayerError;
use super::{
pending_cleanup_batch_size, pending_cleanup_timeout_minutes,
pending_cleanup_batch_size, pending_cleanup_timeout_minutes, postgres_error,
SELECT_COMPLETED_PENDING_REQUEST_IDS_SQL, SELECT_STALE_PENDING_USAGE_BATCH_SQL,
UPDATE_FAILED_PENDING_CANDIDATES_SQL, UPDATE_FAILED_STALE_USAGE_SQL,
UPDATE_FAILED_VOID_STALE_USAGE_SQL, UPDATE_RECOVERED_STALE_USAGE_SQL,
@@ -44,7 +45,7 @@ pub(super) struct PendingCleanupBatchPlan {
pub(crate) async fn cleanup_stale_pending_requests_once(
data: &GatewayDataState,
) -> Result<PendingCleanupSummary, aether_data::DataLayerError> {
) -> Result<PendingCleanupSummary, DataLayerError> {
let Some(pool) = data.postgres_pool() else {
return Ok(PendingCleanupSummary::default());
};
@@ -57,29 +58,34 @@ pub(crate) async fn cleanup_stale_pending_requests_once(
let mut summary = PendingCleanupSummary::default();
loop {
let mut tx = pool.begin().await?;
let mut tx = pool.begin().await.map_err(postgres_error)?;
let stale_rows = sqlx::query(SELECT_STALE_PENDING_USAGE_BATCH_SQL)
.bind(active_statuses.clone())
.bind(cutoff_time)
.bind(i64::try_from(batch_size).unwrap_or(i64::MAX))
.fetch_all(&mut *tx)
.await?;
.await
.map_err(postgres_error)?;
if stale_rows.is_empty() {
tx.rollback().await?;
tx.rollback().await.map_err(postgres_error)?;
break;
}
let stale_rows = stale_rows
.into_iter()
.map(|row| {
Ok(StalePendingUsageRow {
id: row.try_get::<String, _>("id")?,
request_id: row.try_get::<String, _>("request_id")?,
status: row.try_get::<String, _>("status")?,
billing_status: row.try_get::<String, _>("billing_status")?,
Ok::<StalePendingUsageRow, DataLayerError>(StalePendingUsageRow {
id: row.try_get::<String, _>("id").map_err(postgres_error)?,
request_id: row
.try_get::<String, _>("request_id")
.map_err(postgres_error)?,
status: row.try_get::<String, _>("status").map_err(postgres_error)?,
billing_status: row
.try_get::<String, _>("billing_status")
.map_err(postgres_error)?,
})
})
.collect::<Result<Vec<_>, sqlx::Error>>()?;
.collect::<Result<Vec<_>, DataLayerError>>()?;
let request_ids = stale_rows
.iter()
.map(|row| row.request_id.clone())
@@ -90,7 +96,8 @@ pub(crate) async fn cleanup_stale_pending_requests_once(
sqlx::query(SELECT_COMPLETED_PENDING_REQUEST_IDS_SQL)
.bind(request_ids)
.fetch_all(&mut *tx)
.await?
.await
.map_err(postgres_error)?
.into_iter()
.filter_map(|row| row.try_get::<String, _>("request_id").ok())
.collect::<HashSet<_>>()
@@ -102,7 +109,8 @@ pub(crate) async fn cleanup_stale_pending_requests_once(
sqlx::query(UPDATE_RECOVERED_STALE_USAGE_SQL)
.bind(usage_id)
.execute(&mut *tx)
.await?;
.await
.map_err(postgres_error)?;
}
for failed_row in &plan.failed_usage_rows {
if failed_row.should_void_billing {
@@ -111,13 +119,15 @@ pub(crate) async fn cleanup_stale_pending_requests_once(
.bind(&failed_row.error_message)
.bind(now)
.execute(&mut *tx)
.await?;
.await
.map_err(postgres_error)?;
} else {
sqlx::query(UPDATE_FAILED_STALE_USAGE_SQL)
.bind(&failed_row.id)
.bind(&failed_row.error_message)
.execute(&mut *tx)
.await?;
.await
.map_err(postgres_error)?;
}
}
if !plan.recovered_request_ids.is_empty() {
@@ -125,7 +135,8 @@ pub(crate) async fn cleanup_stale_pending_requests_once(
.bind(plan.recovered_request_ids.clone())
.bind(now)
.execute(&mut *tx)
.await?;
.await
.map_err(postgres_error)?;
}
if !plan.failed_request_ids.is_empty() {
sqlx::query(UPDATE_FAILED_PENDING_CANDIDATES_SQL)
@@ -133,10 +144,11 @@ pub(crate) async fn cleanup_stale_pending_requests_once(
.bind(now)
.bind(active_statuses.clone())
.execute(&mut *tx)
.await?;
.await
.map_err(postgres_error)?;
}
tx.commit().await?;
tx.commit().await.map_err(postgres_error)?;
summary.failed += plan.failed_usage_rows.len();
summary.recovered += plan.recovered_usage_ids.len();
}