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 28593 additions and 20666 deletions

View File

@@ -6,9 +6,9 @@ use axum::Json;
use serde::Deserialize;
use serde_json::json;
use crate::AppState;
use crate::{AppState, GatewayError};
use aether_data::repository::audit::RequestAuditBundle;
use aether_data::repository::usage::StoredRequestUsageAudit;
use aether_data_contracts::repository::usage::StoredRequestUsageAudit;
#[derive(Debug, Deserialize)]
pub(crate) struct GetRequestAuditBundleQuery {
@@ -20,9 +20,10 @@ pub(crate) async fn get_request_usage_audit(
Path(request_id): Path<String>,
) -> Result<Json<StoredRequestUsageAudit>, axum::response::Response> {
let usage = state
.data
.read_request_usage_audit(&request_id)
.await
.map_err(IntoResponse::into_response)?;
.map_err(|err| GatewayError::Internal(err.to_string()).into_response())?;
match usage {
Some(usage) => Ok(Json(usage)),
@@ -45,9 +46,10 @@ pub(crate) async fn get_request_audit_bundle(
) -> Result<Json<RequestAuditBundle>, axum::response::Response> {
let attempted_only = query.attempted_only.unwrap_or(false);
let bundle = state
.data
.read_request_audit_bundle(&request_id, attempted_only, current_unix_secs())
.await
.map_err(IntoResponse::into_response)?;
.map_err(|err| GatewayError::Internal(err.to_string()).into_response())?;
match bundle {
Some(bundle) => Ok(Json(bundle)),

View File

@@ -1,10 +1,8 @@
use aether_data::repository::video_tasks::VideoTaskLookupKey;
use aether_usage_runtime::{
build_locally_actionable_report_context_from_request_candidate,
build_locally_actionable_report_context_from_video_task,
};
use aether_data_contracts::repository::video_tasks::VideoTaskLookupKey;
use aether_usage_runtime::build_locally_actionable_report_context_from_video_task;
use serde_json::Value;
use crate::request_candidate_runtime::resolve_locally_actionable_request_candidate_report_context;
use crate::video_tasks::{resolve_video_task_report_lookup, VideoTaskReportLookup};
use crate::AppState;
@@ -20,7 +18,7 @@ pub(crate) async fn resolve_locally_actionable_report_context(
}
if let Some(resolved) =
resolve_locally_actionable_report_context_from_request_candidates(state, &context).await
resolve_locally_actionable_request_candidate_report_context(state, &context).await
{
return Some(resolved);
}
@@ -30,7 +28,7 @@ pub(crate) async fn resolve_locally_actionable_report_context(
.unwrap_or(context);
if let Some(resolved) =
resolve_locally_actionable_report_context_from_request_candidates(state, &context).await
resolve_locally_actionable_request_candidate_report_context(state, &context).await
{
return Some(resolved);
}
@@ -38,26 +36,6 @@ pub(crate) async fn resolve_locally_actionable_report_context(
report_context_is_locally_actionable(Some(&context)).then_some(context)
}
async fn resolve_locally_actionable_report_context_from_request_candidates(
state: &AppState,
context: &Value,
) -> Option<Value> {
let request_id = context
.get("request_id")
.and_then(Value::as_str)
.map(str::trim)
.filter(|value| !value.is_empty())?;
let existing_candidates = state
.read_request_candidates_by_request_id(request_id)
.await
.ok()?;
if existing_candidates.len() != 1 {
return None;
}
build_locally_actionable_report_context_from_request_candidate(context, &existing_candidates[0])
}
async fn resolve_locally_actionable_report_context_from_video_task(
state: &AppState,
context: &Value,

View File

@@ -1,12 +1,14 @@
use std::collections::BTreeMap;
use aether_contracts::ExecutionError;
use aether_data_contracts::repository::candidates::RequestCandidateStatus;
use aether_scheduler_core::execution_error_details;
use tracing::{debug, warn};
use uuid::Uuid;
use crate::scheduler::{
current_unix_secs, execution_error_details, record_report_request_candidate_status,
};
use crate::clock::current_unix_secs;
use crate::log_ids::short_request_id;
use crate::request_candidate_runtime::record_report_request_candidate_status;
use crate::{AppState, GatewayError};
mod context;
@@ -33,7 +35,7 @@ fn log_local_report_handled(
trace_id = %trace_id,
report_scope,
report_kind = %report_kind,
report_request_id = report_request_id(report_context),
report_request_id = %short_request_id(report_request_id(report_context)),
has_report_context = report_context.is_some(),
"gateway handled execution report locally"
);
@@ -52,7 +54,7 @@ fn log_dropped_report(
trace_id = %trace_id,
report_scope,
report_kind = %report_kind,
report_request_id = report_request_id(report_context),
report_request_id = %short_request_id(report_request_id(report_context)),
has_report_context = report_context.is_some(),
"gateway dropped execution report because local handling context was not actionable"
);
@@ -111,6 +113,8 @@ pub(crate) fn spawn_sync_report(
trace_id: String,
payload: GatewaySyncReportRequest,
) {
let report_request_id_for_log =
short_request_id(report_request_id(payload.report_context.as_ref()));
tokio::spawn(async move {
if let Err(err) = submit_sync_report(&state, &trace_id, payload).await {
warn!(
@@ -118,6 +122,7 @@ pub(crate) fn spawn_sync_report(
log_type = "ops",
trace_id = %trace_id,
report_scope = "sync",
report_request_id = %report_request_id_for_log,
error = ?err,
"gateway failed to submit sync execution report"
);
@@ -187,9 +192,9 @@ async fn handle_local_sync_report(state: &AppState, payload: &GatewaySyncReportR
let (error_type, error_message) =
execution_error_details(None::<&ExecutionError>, payload.body_json.as_ref());
let status = if sync_report_represents_failure(payload, error_type.as_deref()) {
aether_data::repository::candidates::RequestCandidateStatus::Failed
RequestCandidateStatus::Failed
} else {
aether_data::repository::candidates::RequestCandidateStatus::Success
RequestCandidateStatus::Success
};
let latency_ms = payload
.telemetry
@@ -218,7 +223,7 @@ async fn handle_local_stream_report(state: &AppState, payload: &GatewayStreamRep
record_report_request_candidate_status(
state,
payload.report_context.as_ref(),
aether_data::repository::candidates::RequestCandidateStatus::Success,
RequestCandidateStatus::Success,
Some(payload.status_code),
None,
None,
@@ -272,7 +277,7 @@ async fn apply_local_gemini_file_mapping_side_effect(
event_name = "gemini_file_mapping_store_failed",
log_type = "ops",
report_kind = %payload.report_kind,
report_request_id = report_request_id(payload.report_context.as_ref()),
report_request_id = %short_request_id(report_request_id(payload.report_context.as_ref())),
file_name = %entry.file_name,
error = ?err,
"gateway failed to persist gemini file mapping locally"
@@ -296,7 +301,7 @@ async fn apply_local_gemini_file_mapping_side_effect(
event_name = "gemini_file_mapping_delete_failed",
log_type = "ops",
report_kind = %payload.report_kind,
report_request_id = report_request_id(payload.report_context.as_ref()),
report_request_id = %short_request_id(report_request_id(payload.report_context.as_ref())),
file_name = %file_name,
error = ?err,
"gateway failed to delete gemini file mapping locally"
@@ -366,16 +371,17 @@ mod tests {
use std::collections::BTreeMap;
use std::sync::Arc;
use aether_data::repository::candidates::{
InMemoryRequestCandidateRepository, RequestCandidateReadRepository, RequestCandidateStatus,
StoredRequestCandidate,
};
use aether_data::repository::candidates::InMemoryRequestCandidateRepository;
use aether_data::repository::gemini_file_mappings::{
GeminiFileMappingReadRepository, InMemoryGeminiFileMappingRepository,
};
use aether_data::repository::usage::InMemoryUsageReadRepository;
use aether_data::repository::video_tasks::{
InMemoryVideoTaskRepository, UpsertVideoTask, VideoTaskStatus, VideoTaskWriteRepository,
use aether_data::repository::video_tasks::InMemoryVideoTaskRepository;
use aether_data_contracts::repository::candidates::{
RequestCandidateReadRepository, RequestCandidateStatus, StoredRequestCandidate,
};
use aether_data_contracts::repository::video_tasks::{
UpsertVideoTask, VideoTaskStatus, VideoTaskWriteRepository,
};
use serde_json::json;

View File

@@ -4,7 +4,8 @@ pub(crate) use aether_usage_runtime::{build_usage_queue_worker, write_event_reco
mod tests {
use std::sync::Arc;
use aether_data::repository::usage::{InMemoryUsageReadRepository, UsageReadRepository};
use aether_data::repository::usage::InMemoryUsageReadRepository;
use aether_data_contracts::repository::usage::UsageReadRepository;
use super::write_event_record;
use crate::data::GatewayDataState;