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,8 +4,9 @@ use aether_crypto::{encrypt_python_fernet_plaintext, DEVELOPMENT_ENCRYPTION_KEY}
use aether_data::repository::auth::{
InMemoryAuthApiKeySnapshotRepository, StoredAuthApiKeyExportRecord, StoredAuthApiKeySnapshot,
};
use aether_data::repository::usage::{InMemoryUsageReadRepository, StoredRequestUsageAudit};
use aether_data::repository::usage::InMemoryUsageReadRepository;
use aether_data::repository::wallet::{InMemoryWalletRepository, StoredWalletSnapshot};
use aether_data_contracts::repository::usage::StoredRequestUsageAudit;
use axum::body::Body;
use axum::routing::any;
use axum::{extract::Request, Router};
@@ -203,15 +204,15 @@ async fn gateway_handles_admin_api_keys_list_locally_with_trusted_admin_principa
AppState::new()
.expect("gateway should build")
.with_data_state_for_tests(
crate::data::GatewayDataState::with_auth_wallet_and_usage_for_tests(
auth_repository,
Arc::new(InMemoryWalletRepository::seed(
Vec::<StoredWalletSnapshot>::new(),
)),
usage_repository,
)
.with_encryption_key_for_tests(DEVELOPMENT_ENCRYPTION_KEY),
),
crate::data::GatewayDataState::with_auth_wallet_and_usage_for_tests(
auth_repository,
Arc::new(InMemoryWalletRepository::seed(vec![
sample_standalone_wallet("key-1"),
])),
usage_repository,
)
.with_encryption_key_for_tests(DEVELOPMENT_ENCRYPTION_KEY),
),
);
let (gateway_url, gateway_handle) = start_server(gateway).await;
@@ -234,7 +235,15 @@ async fn gateway_handles_admin_api_keys_list_locally_with_trusted_admin_principa
json!("sk-key-1-p...text")
);
assert_eq!(payload["api_keys"][0]["total_requests"], json!(7));
assert_eq!(payload["api_keys"][0]["total_tokens"], json!(90));
assert_eq!(
payload["api_keys"][0]["total_tokens"],
serde_json::Value::Null
);
assert_eq!(
payload["api_keys"][0]["wallet"]["id"],
json!("wallet-key-1")
);
assert_eq!(payload["api_keys"][0]["wallet"]["balance"], json!(20.0));
assert_eq!(*upstream_hits.lock().expect("mutex should lock"), 0);
gateway_handle.abort();
@@ -267,13 +276,13 @@ async fn gateway_handles_admin_api_keys_detail_locally_with_trusted_admin_princi
AppState::new()
.expect("gateway should build")
.with_data_state_for_tests(
crate::data::GatewayDataState::with_auth_wallet_and_usage_for_tests(
auth_repository,
wallet_repository,
usage_repository,
)
.with_encryption_key_for_tests(DEVELOPMENT_ENCRYPTION_KEY),
),
crate::data::GatewayDataState::with_auth_wallet_and_usage_for_tests(
auth_repository,
wallet_repository,
usage_repository,
)
.with_encryption_key_for_tests(DEVELOPMENT_ENCRYPTION_KEY),
),
);
let (gateway_url, gateway_handle) = start_server(gateway).await;
@@ -299,6 +308,56 @@ async fn gateway_handles_admin_api_keys_detail_locally_with_trusted_admin_princi
upstream_handle.abort();
}
#[tokio::test]
async fn gateway_includes_usage_summary_when_admin_api_keys_list_requests_it() {
let (_upstream_url, _upstream_hits, upstream_handle) =
start_api_keys_upstream("/api/admin/api-keys").await;
let auth_repository = Arc::new(
InMemoryAuthApiKeySnapshotRepository::seed(vec![(
None,
sample_standalone_api_key_snapshot("key-1", "user-1", true),
)])
.with_export_records([sample_standalone_export_record(
"key-1",
"user-1",
"sk-key-1-plaintext",
true,
)]),
);
let usage_repository = Arc::new(InMemoryUsageReadRepository::seed(vec![sample_usage_row(
"usage-1", "req-1", "key-1", 90,
)]));
let gateway = build_router_with_state(
AppState::new()
.expect("gateway should build")
.with_data_state_for_tests(
crate::data::GatewayDataState::with_auth_wallet_and_usage_for_tests(
auth_repository,
Arc::new(InMemoryWalletRepository::seed(
Vec::<StoredWalletSnapshot>::new(),
)),
usage_repository,
)
.with_encryption_key_for_tests(DEVELOPMENT_ENCRYPTION_KEY),
),
);
let (gateway_url, gateway_handle) = start_server(gateway).await;
let response = admin_request(reqwest::Client::new().get(format!(
"{gateway_url}/api/admin/api-keys?include_usage_summary=true"
)))
.send()
.await
.expect("request should succeed");
assert_eq!(response.status(), StatusCode::OK);
let payload: serde_json::Value = response.json().await.expect("json body should parse");
assert_eq!(payload["api_keys"][0]["total_tokens"], json!(90));
gateway_handle.abort();
upstream_handle.abort();
}
#[tokio::test]
async fn gateway_handles_admin_api_keys_full_key_locally_with_trusted_admin_principal() {
let (upstream_url, upstream_hits, upstream_handle) =
@@ -359,11 +418,11 @@ async fn gateway_handles_admin_api_keys_create_locally_with_trusted_admin_princi
AppState::new()
.expect("gateway should build")
.with_data_state_for_tests(
crate::data::GatewayDataState::with_auth_api_key_repository_for_tests(
Arc::clone(&repository),
)
.with_encryption_key_for_tests(DEVELOPMENT_ENCRYPTION_KEY),
),
crate::data::GatewayDataState::with_auth_api_key_repository_for_tests(Arc::clone(
&repository,
))
.with_encryption_key_for_tests(DEVELOPMENT_ENCRYPTION_KEY),
),
);
let (gateway_url, gateway_handle) = start_server(gateway).await;
@@ -431,11 +490,11 @@ async fn gateway_handles_admin_api_keys_update_locally_with_trusted_admin_princi
AppState::new()
.expect("gateway should build")
.with_data_state_for_tests(
crate::data::GatewayDataState::with_auth_api_key_repository_for_tests(
Arc::clone(&repository),
)
.with_encryption_key_for_tests(DEVELOPMENT_ENCRYPTION_KEY),
),
crate::data::GatewayDataState::with_auth_api_key_repository_for_tests(Arc::clone(
&repository,
))
.with_encryption_key_for_tests(DEVELOPMENT_ENCRYPTION_KEY),
),
);
let (gateway_url, gateway_handle) = start_server(gateway).await;
@@ -487,11 +546,11 @@ async fn gateway_handles_admin_api_keys_toggle_locally_with_trusted_admin_princi
AppState::new()
.expect("gateway should build")
.with_data_state_for_tests(
crate::data::GatewayDataState::with_auth_api_key_repository_for_tests(
Arc::clone(&repository),
)
.with_encryption_key_for_tests(DEVELOPMENT_ENCRYPTION_KEY),
),
crate::data::GatewayDataState::with_auth_api_key_repository_for_tests(Arc::clone(
&repository,
))
.with_encryption_key_for_tests(DEVELOPMENT_ENCRYPTION_KEY),
),
);
let (gateway_url, gateway_handle) = start_server(gateway).await;
@@ -533,11 +592,11 @@ async fn gateway_handles_admin_api_keys_delete_locally_with_trusted_admin_princi
AppState::new()
.expect("gateway should build")
.with_data_state_for_tests(
crate::data::GatewayDataState::with_auth_api_key_repository_for_tests(
Arc::clone(&repository),
)
.with_encryption_key_for_tests(DEVELOPMENT_ENCRYPTION_KEY),
),
crate::data::GatewayDataState::with_auth_api_key_repository_for_tests(Arc::clone(
&repository,
))
.with_encryption_key_for_tests(DEVELOPMENT_ENCRYPTION_KEY),
),
);
let (gateway_url, gateway_handle) = start_server(gateway).await;