mirror of
https://github.com/fawney19/Aether.git
synced 2026-09-02 17:30:23 +08:00
refactor: 优化调度候选排序与用量写入链路并改进 Fernet 缓存与前端批量列表
This commit is contained in:
@@ -206,18 +206,18 @@ pub(crate) async fn maybe_build_local_internal_proxy_response_impl(
|
||||
Json(build_internal_gateway_fallback_plan_payload(None)).into_response(),
|
||||
));
|
||||
};
|
||||
if let Some(auth_context) = payload.auth_context.clone() {
|
||||
let provided_auth_context = payload.auth_context.is_some();
|
||||
if let Some(auth_context) = payload.auth_context {
|
||||
resolved.auth_context = Some(auth_context);
|
||||
resolved.local_auth_rejection = None;
|
||||
}
|
||||
let auth_context = resolved.auth_context.clone();
|
||||
let auth_context = resolved.auth_context.as_ref();
|
||||
if auth_context
|
||||
.as_ref()
|
||||
.map(|value| !value.access_allowed)
|
||||
.unwrap_or(true)
|
||||
{
|
||||
let fallback_auth_context = if payload.auth_context.is_none() {
|
||||
auth_context.as_ref()
|
||||
let fallback_auth_context = if !provided_auth_context {
|
||||
auth_context
|
||||
} else {
|
||||
None
|
||||
};
|
||||
@@ -239,8 +239,8 @@ pub(crate) async fn maybe_build_local_internal_proxy_response_impl(
|
||||
)
|
||||
.await?
|
||||
else {
|
||||
let fallback_auth_context = if payload.auth_context.is_none() {
|
||||
auth_context.as_ref()
|
||||
let fallback_auth_context = if !provided_auth_context {
|
||||
auth_context
|
||||
} else {
|
||||
None
|
||||
};
|
||||
@@ -251,7 +251,7 @@ pub(crate) async fn maybe_build_local_internal_proxy_response_impl(
|
||||
.into_response(),
|
||||
));
|
||||
};
|
||||
if payload.auth_context.is_some() {
|
||||
if provided_auth_context {
|
||||
local_payload.auth_context = None;
|
||||
}
|
||||
return Ok(Some(Json(local_payload).into_response()));
|
||||
@@ -308,18 +308,18 @@ pub(crate) async fn maybe_build_local_internal_proxy_response_impl(
|
||||
Json(build_internal_gateway_fallback_plan_payload(None)).into_response(),
|
||||
));
|
||||
};
|
||||
if let Some(auth_context) = payload.auth_context.clone() {
|
||||
let provided_auth_context = payload.auth_context.is_some();
|
||||
if let Some(auth_context) = payload.auth_context {
|
||||
resolved.auth_context = Some(auth_context);
|
||||
resolved.local_auth_rejection = None;
|
||||
}
|
||||
let auth_context = resolved.auth_context.clone();
|
||||
let auth_context = resolved.auth_context.as_ref();
|
||||
if auth_context
|
||||
.as_ref()
|
||||
.map(|value| !value.access_allowed)
|
||||
.unwrap_or(true)
|
||||
{
|
||||
let fallback_auth_context = if payload.auth_context.is_none() {
|
||||
auth_context.as_ref()
|
||||
let fallback_auth_context = if !provided_auth_context {
|
||||
auth_context
|
||||
} else {
|
||||
None
|
||||
};
|
||||
@@ -339,8 +339,8 @@ pub(crate) async fn maybe_build_local_internal_proxy_response_impl(
|
||||
)
|
||||
.await?
|
||||
else {
|
||||
let fallback_auth_context = if payload.auth_context.is_none() {
|
||||
auth_context.as_ref()
|
||||
let fallback_auth_context = if !provided_auth_context {
|
||||
auth_context
|
||||
} else {
|
||||
None
|
||||
};
|
||||
@@ -351,7 +351,7 @@ pub(crate) async fn maybe_build_local_internal_proxy_response_impl(
|
||||
.into_response(),
|
||||
));
|
||||
};
|
||||
if payload.auth_context.is_some() {
|
||||
if provided_auth_context {
|
||||
local_payload.auth_context = None;
|
||||
}
|
||||
return Ok(Some(Json(local_payload).into_response()));
|
||||
@@ -406,7 +406,8 @@ pub(crate) async fn maybe_build_local_internal_proxy_response_impl(
|
||||
else {
|
||||
return Ok(Some(build_internal_gateway_proxy_public_response()));
|
||||
};
|
||||
if let Some(auth_context) = payload.auth_context.clone() {
|
||||
let provided_auth_context = payload.auth_context.is_some();
|
||||
if let Some(auth_context) = payload.auth_context {
|
||||
resolved.auth_context = Some(auth_context);
|
||||
resolved.local_auth_rejection = None;
|
||||
}
|
||||
@@ -421,7 +422,7 @@ pub(crate) async fn maybe_build_local_internal_proxy_response_impl(
|
||||
)
|
||||
.await?
|
||||
{
|
||||
if payload.auth_context.is_some() {
|
||||
if provided_auth_context {
|
||||
planned.auth_context = None;
|
||||
}
|
||||
return Ok(Some(Json(planned).into_response()));
|
||||
@@ -472,7 +473,8 @@ pub(crate) async fn maybe_build_local_internal_proxy_response_impl(
|
||||
else {
|
||||
return Ok(Some(build_internal_gateway_proxy_public_response()));
|
||||
};
|
||||
if let Some(auth_context) = payload.auth_context.clone() {
|
||||
let provided_auth_context = payload.auth_context.is_some();
|
||||
if let Some(auth_context) = payload.auth_context {
|
||||
resolved.auth_context = Some(auth_context);
|
||||
resolved.local_auth_rejection = None;
|
||||
}
|
||||
@@ -485,7 +487,7 @@ pub(crate) async fn maybe_build_local_internal_proxy_response_impl(
|
||||
)
|
||||
.await?
|
||||
{
|
||||
if payload.auth_context.is_some() {
|
||||
if provided_auth_context {
|
||||
planned.auth_context = None;
|
||||
}
|
||||
return Ok(Some(Json(planned).into_response()));
|
||||
@@ -542,7 +544,7 @@ pub(crate) async fn maybe_build_local_internal_proxy_response_impl(
|
||||
else {
|
||||
return Ok(None);
|
||||
};
|
||||
if let Some(auth_context) = payload.auth_context.clone() {
|
||||
if let Some(auth_context) = payload.auth_context {
|
||||
resolved.auth_context = Some(auth_context);
|
||||
resolved.local_auth_rejection = None;
|
||||
}
|
||||
@@ -626,7 +628,7 @@ pub(crate) async fn maybe_build_local_internal_proxy_response_impl(
|
||||
else {
|
||||
return Ok(None);
|
||||
};
|
||||
if let Some(auth_context) = payload.auth_context.clone() {
|
||||
if let Some(auth_context) = payload.auth_context {
|
||||
resolved.auth_context = Some(auth_context);
|
||||
resolved.local_auth_rejection = None;
|
||||
}
|
||||
@@ -683,8 +685,7 @@ pub(crate) async fn maybe_build_local_internal_proxy_response_impl(
|
||||
)));
|
||||
}
|
||||
};
|
||||
let trace_id = payload.trace_id.clone();
|
||||
crate::usage::submit_sync_report(state, &trace_id, payload).await?;
|
||||
crate::usage::submit_sync_report(state, payload).await?;
|
||||
return Ok(Some(Json(json!({ "ok": true })).into_response()));
|
||||
}
|
||||
Some("report_stream")
|
||||
@@ -707,8 +708,7 @@ pub(crate) async fn maybe_build_local_internal_proxy_response_impl(
|
||||
)));
|
||||
}
|
||||
};
|
||||
let trace_id = payload.trace_id.clone();
|
||||
crate::usage::submit_stream_report(state, &trace_id, payload).await?;
|
||||
crate::usage::submit_stream_report(state, payload).await?;
|
||||
return Ok(Some(Json(json!({ "ok": true })).into_response()));
|
||||
}
|
||||
Some("finalize_sync")
|
||||
@@ -739,16 +739,12 @@ pub(crate) async fn maybe_build_local_internal_proxy_response_impl(
|
||||
};
|
||||
let trace_id = payload.trace_id.clone();
|
||||
if let Some(outcome) = ai_pipeline_api::maybe_build_sync_finalize_outcome(
|
||||
&trace_id,
|
||||
trace_id.as_str(),
|
||||
&synthetic_decision,
|
||||
&payload,
|
||||
)? {
|
||||
if let Some(background_report) = outcome.background_report {
|
||||
crate::usage::spawn_sync_report(
|
||||
state.clone(),
|
||||
trace_id.clone(),
|
||||
background_report,
|
||||
);
|
||||
crate::usage::spawn_sync_report(state.clone(), background_report);
|
||||
}
|
||||
let mut response = outcome.response;
|
||||
response.headers_mut().insert(
|
||||
@@ -761,7 +757,7 @@ pub(crate) async fn maybe_build_local_internal_proxy_response_impl(
|
||||
state,
|
||||
trace_id.as_str(),
|
||||
&synthetic_decision,
|
||||
&payload,
|
||||
payload,
|
||||
)
|
||||
.await?
|
||||
{
|
||||
|
||||
@@ -11,7 +11,7 @@ use crate::control::GatewayControlDecision;
|
||||
use crate::execution_runtime::{
|
||||
maybe_build_local_sync_finalize_response, maybe_build_local_video_error_response,
|
||||
maybe_build_local_video_success_outcome, resolve_local_sync_error_background_report_kind,
|
||||
resolve_local_sync_success_background_report_kind,
|
||||
resolve_local_sync_success_background_report_kind, LocalVideoSyncSuccessBuild,
|
||||
};
|
||||
use crate::handlers::shared::{
|
||||
unix_secs_to_rfc3339, InternalTunnelHeartbeatRequest, InternalTunnelNodeStatusRequest,
|
||||
@@ -244,59 +244,66 @@ pub(crate) async fn maybe_build_internal_finalize_video_response(
|
||||
state: &AppState,
|
||||
trace_id: &str,
|
||||
decision: &GatewayControlDecision,
|
||||
payload: &crate::usage::GatewaySyncReportRequest,
|
||||
payload: crate::usage::GatewaySyncReportRequest,
|
||||
) -> Result<Option<Response<Body>>, GatewayError> {
|
||||
let Some(plan) = infer_internal_finalize_signature(payload).and_then(|signature| {
|
||||
build_internal_finalize_video_plan(
|
||||
payload.trace_id.as_str(),
|
||||
signature.as_str(),
|
||||
payload.report_context.as_ref(),
|
||||
)
|
||||
}) else {
|
||||
let Some((signature, plan)) =
|
||||
infer_internal_finalize_signature(&payload).and_then(|signature| {
|
||||
build_internal_finalize_video_plan(
|
||||
payload.trace_id.as_str(),
|
||||
signature.as_str(),
|
||||
payload.report_context.as_ref(),
|
||||
)
|
||||
.map(|plan| (signature, plan))
|
||||
})
|
||||
else {
|
||||
return Ok(None);
|
||||
};
|
||||
|
||||
if let Some(outcome) = maybe_build_local_video_success_outcome(
|
||||
let mut payload = match maybe_build_local_video_success_outcome(
|
||||
trace_id,
|
||||
decision,
|
||||
payload,
|
||||
&state.video_tasks,
|
||||
&plan,
|
||||
)? {
|
||||
if let Some(snapshot) = outcome.local_task_snapshot.clone() {
|
||||
state.video_tasks.record_snapshot(snapshot.clone());
|
||||
let _ = state.upsert_video_task_snapshot(&snapshot).await?;
|
||||
}
|
||||
match outcome.report_mode {
|
||||
crate::video_tasks::VideoTaskSyncReportMode::InlineSync => {
|
||||
crate::usage::submit_sync_report(state, trace_id, outcome.report_payload).await?;
|
||||
LocalVideoSyncSuccessBuild::Handled(outcome) => {
|
||||
let crate::execution_runtime::LocalVideoSyncSuccessOutcome {
|
||||
response,
|
||||
report_payload,
|
||||
original_report_context: _,
|
||||
report_mode,
|
||||
local_task_snapshot,
|
||||
} = outcome;
|
||||
if let Some(snapshot) = local_task_snapshot {
|
||||
let _ = state.upsert_video_task_snapshot(&snapshot).await?;
|
||||
state.video_tasks.record_snapshot(snapshot);
|
||||
}
|
||||
crate::video_tasks::VideoTaskSyncReportMode::Background => {
|
||||
crate::usage::spawn_sync_report(
|
||||
state.clone(),
|
||||
trace_id.to_string(),
|
||||
outcome.report_payload,
|
||||
);
|
||||
match report_mode {
|
||||
crate::video_tasks::VideoTaskSyncReportMode::InlineSync => {
|
||||
crate::usage::submit_sync_report(state, report_payload).await?;
|
||||
}
|
||||
crate::video_tasks::VideoTaskSyncReportMode::Background => {
|
||||
crate::usage::spawn_sync_report(state.clone(), report_payload);
|
||||
}
|
||||
}
|
||||
let mut response = response;
|
||||
response.headers_mut().insert(
|
||||
HeaderName::from_static(CONTROL_EXECUTED_HEADER),
|
||||
HeaderValue::from_static("true"),
|
||||
);
|
||||
return Ok(Some(response));
|
||||
}
|
||||
let mut response = outcome.response;
|
||||
response.headers_mut().insert(
|
||||
HeaderName::from_static(CONTROL_EXECUTED_HEADER),
|
||||
HeaderValue::from_static("true"),
|
||||
);
|
||||
return Ok(Some(response));
|
||||
}
|
||||
LocalVideoSyncSuccessBuild::NotHandled(payload) => payload,
|
||||
};
|
||||
|
||||
if let Some(mut response) =
|
||||
maybe_build_local_sync_finalize_response(trace_id, decision, payload)?
|
||||
maybe_build_local_sync_finalize_response(trace_id, decision, &payload)?
|
||||
{
|
||||
let request_path = infer_internal_finalize_signature(payload).and_then(|signature| {
|
||||
build_local_sync_finalize_request_path(
|
||||
payload.report_kind.as_str(),
|
||||
signature.as_str(),
|
||||
payload.report_context.as_ref(),
|
||||
)
|
||||
});
|
||||
let request_path = build_local_sync_finalize_request_path(
|
||||
payload.report_kind.as_str(),
|
||||
signature.as_str(),
|
||||
payload.report_context.as_ref(),
|
||||
);
|
||||
if let Some(request_path) = request_path {
|
||||
state
|
||||
.video_tasks
|
||||
@@ -311,9 +318,8 @@ pub(crate) async fn maybe_build_internal_finalize_video_response(
|
||||
if let Some(success_report_kind) =
|
||||
resolve_local_sync_success_background_report_kind(payload.report_kind.as_str())
|
||||
{
|
||||
let mut report_payload = payload.clone();
|
||||
report_payload.report_kind = success_report_kind.to_string();
|
||||
crate::usage::spawn_sync_report(state.clone(), trace_id.to_string(), report_payload);
|
||||
payload.report_kind = success_report_kind.to_string();
|
||||
crate::usage::spawn_sync_report(state.clone(), payload);
|
||||
}
|
||||
response.headers_mut().insert(
|
||||
HeaderName::from_static(CONTROL_EXECUTED_HEADER),
|
||||
@@ -322,14 +328,14 @@ pub(crate) async fn maybe_build_internal_finalize_video_response(
|
||||
return Ok(Some(response));
|
||||
}
|
||||
|
||||
if let Some(mut response) = maybe_build_local_video_error_response(trace_id, decision, payload)?
|
||||
if let Some(mut response) =
|
||||
maybe_build_local_video_error_response(trace_id, decision, &payload)?
|
||||
{
|
||||
if let Some(error_report_kind) =
|
||||
resolve_local_sync_error_background_report_kind(payload.report_kind.as_str())
|
||||
{
|
||||
let mut report_payload = payload.clone();
|
||||
report_payload.report_kind = error_report_kind.to_string();
|
||||
crate::usage::spawn_sync_report(state.clone(), trace_id.to_string(), report_payload);
|
||||
payload.report_kind = error_report_kind.to_string();
|
||||
crate::usage::spawn_sync_report(state.clone(), payload);
|
||||
}
|
||||
response.headers_mut().insert(
|
||||
HeaderName::from_static(CONTROL_EXECUTED_HEADER),
|
||||
|
||||
@@ -122,6 +122,10 @@ pub(crate) async fn build_public_providers_payload(
|
||||
.iter()
|
||||
.map(|provider| provider.id.clone())
|
||||
.collect::<Vec<_>>();
|
||||
let provider_ids_set = provider_ids
|
||||
.iter()
|
||||
.map(String::as_str)
|
||||
.collect::<BTreeSet<_>>();
|
||||
let endpoints = if provider_ids.is_empty() {
|
||||
Vec::new()
|
||||
} else {
|
||||
@@ -156,7 +160,7 @@ pub(crate) async fn build_public_providers_payload(
|
||||
.ok()
|
||||
.unwrap_or_default();
|
||||
for row in rows {
|
||||
if provider_ids.contains(&row.provider_id) {
|
||||
if provider_ids_set.contains(row.provider_id.as_str()) {
|
||||
models_by_provider
|
||||
.entry(row.provider_id.clone())
|
||||
.or_default()
|
||||
@@ -332,7 +336,6 @@ pub(crate) async fn build_api_format_health_monitor_payload(
|
||||
let mut endpoint_ids_by_format = BTreeMap::<String, Vec<String>>::new();
|
||||
let mut endpoint_to_format = BTreeMap::<String, String>::new();
|
||||
let mut provider_ids_by_format = BTreeMap::<String, BTreeSet<String>>::new();
|
||||
let mut active_provider_formats = BTreeSet::<(String, String)>::new();
|
||||
for endpoint in active_endpoints {
|
||||
endpoint_to_format.insert(endpoint.id.clone(), endpoint.api_format.clone());
|
||||
endpoint_ids_by_format
|
||||
@@ -343,7 +346,6 @@ pub(crate) async fn build_api_format_health_monitor_payload(
|
||||
.entry(endpoint.api_format.clone())
|
||||
.or_default()
|
||||
.insert(endpoint.provider_id.clone());
|
||||
active_provider_formats.insert((endpoint.provider_id, endpoint.api_format));
|
||||
}
|
||||
let all_endpoint_ids = endpoint_to_format.keys().cloned().collect::<Vec<_>>();
|
||||
|
||||
@@ -356,7 +358,9 @@ pub(crate) async fn build_api_format_health_monitor_payload(
|
||||
.unwrap_or_default();
|
||||
for key in keys.into_iter().filter(|key| key.is_active) {
|
||||
for api_format in provider_key_api_formats(&key) {
|
||||
if active_provider_formats.contains(&(key.provider_id.clone(), api_format.clone()))
|
||||
if provider_ids_by_format
|
||||
.get(&api_format)
|
||||
.is_some_and(|provider_ids| provider_ids.contains(key.provider_id.as_str()))
|
||||
{
|
||||
*key_counts_by_format.entry(api_format).or_default() += 1;
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ use aether_crypto::DEVELOPMENT_ENCRYPTION_KEY;
|
||||
use aether_crypto::{decrypt_python_fernet_ciphertext, encrypt_python_fernet_plaintext};
|
||||
use aether_data_contracts::repository::provider_catalog::StoredProviderCatalogKey;
|
||||
use serde_json::{json, Map, Value};
|
||||
use std::borrow::Cow;
|
||||
use std::time::{SystemTime, UNIX_EPOCH};
|
||||
|
||||
const OAUTH_ACCOUNT_BLOCK_PREFIX: &str = "[ACCOUNT_BLOCK] ";
|
||||
@@ -63,23 +64,27 @@ pub(crate) fn decrypt_catalog_secret_with_fallbacks(
|
||||
None
|
||||
}
|
||||
|
||||
pub(crate) fn effective_catalog_encryption_key(state: &AppState) -> Option<String> {
|
||||
pub(crate) fn effective_catalog_encryption_key(state: &AppState) -> Option<Cow<'_, str>> {
|
||||
let encryption_key = state.encryption_key().map(str::trim).unwrap_or("");
|
||||
if !encryption_key.is_empty() {
|
||||
return Some(encryption_key.to_string());
|
||||
return Some(Cow::Borrowed(encryption_key));
|
||||
}
|
||||
for env_key in ["AETHER_GATEWAY_DATA_ENCRYPTION_KEY", "ENCRYPTION_KEY"] {
|
||||
let Ok(candidate) = std::env::var(env_key) else {
|
||||
continue;
|
||||
};
|
||||
let candidate = candidate.trim();
|
||||
if !candidate.is_empty() {
|
||||
return Some(candidate.to_string());
|
||||
let trimmed = candidate.trim();
|
||||
if !trimmed.is_empty() {
|
||||
return Some(if trimmed.len() == candidate.len() {
|
||||
Cow::Owned(candidate)
|
||||
} else {
|
||||
Cow::Owned(trimmed.to_string())
|
||||
});
|
||||
}
|
||||
}
|
||||
#[cfg(test)]
|
||||
{
|
||||
return Some(DEVELOPMENT_ENCRYPTION_KEY.to_string());
|
||||
return Some(Cow::Borrowed(DEVELOPMENT_ENCRYPTION_KEY));
|
||||
}
|
||||
#[allow(unreachable_code)]
|
||||
None
|
||||
@@ -90,7 +95,7 @@ pub(crate) fn encrypt_catalog_secret_with_fallbacks(
|
||||
plaintext: &str,
|
||||
) -> Option<String> {
|
||||
let encryption_key = effective_catalog_encryption_key(state)?;
|
||||
encrypt_python_fernet_plaintext(&encryption_key, plaintext).ok()
|
||||
encrypt_python_fernet_plaintext(encryption_key.as_ref(), plaintext).ok()
|
||||
}
|
||||
|
||||
pub(crate) fn masked_catalog_api_key(state: &AppState, key: &StoredProviderCatalogKey) -> String {
|
||||
|
||||
Reference in New Issue
Block a user