Files
Aether/apps/aether-gateway/src/control/execute.rs
T
fawney19 010ab127e2 feat: 扩展 cache creation token 细分统计与 effective_input_tokens 计费逻辑
- 新增 cache_creation_ephemeral_5m/1h_input_tokens 字段,区分不同 TTL 的缓存写入 token
- 引入 effective_input_tokens(扣除 cache read 后的有效输入 token),暴露给 usage 接口
- billing 规则生成器支持 5m/1h ephemeral cache 独立定价与分级计费
- usage_mapper 增加 Claude/Anthropic 格式映射,修复 OpenAI responses 格式字段兼容性
- 迁移逻辑增强:支持 checksum 容错、applied/pending 数量日志、逐步执行信息输出
- executor 抽离 LocalExecutionRequestOutcome 类型,统一 sync/stream 路径返回语义
- provider-transport auth 层新增 complete passthrough headers 构建逻辑
- 前端 usage 类型全面补充 effective_input_tokens、cache_creation_tokens、total_input_context 字段
2026-04-10 17:44:55 +08:00

69 lines
2.2 KiB
Rust

use axum::body::{Body, Bytes};
use axum::http::{HeaderName, HeaderValue, Response};
use crate::constants::CONTROL_EXECUTED_HEADER;
use crate::control::GatewayControlDecision;
use crate::executor::{
maybe_execute_stream_request, maybe_execute_sync_request, LocalExecutionRequestOutcome,
};
use crate::{AppState, GatewayError};
use super::resolve_execution_runtime_auth_context;
pub(crate) fn allows_control_execute_emergency(decision: &GatewayControlDecision) -> bool {
decision.is_execution_runtime_candidate()
}
pub(crate) async fn maybe_execute_via_control(
state: &AppState,
parts: &http::request::Parts,
body_bytes: Bytes,
trace_id: &str,
decision: Option<&GatewayControlDecision>,
require_stream: bool,
) -> Result<LocalExecutionRequestOutcome, GatewayError> {
let Some(decision) = decision else {
return Ok(LocalExecutionRequestOutcome::NoPath);
};
let mut local_decision = decision.clone();
if let Some(auth_context) = resolve_execution_runtime_auth_context(
state,
&local_decision,
&parts.headers,
&parts.uri,
trace_id,
)
.await?
{
local_decision.auth_context = Some(auth_context);
local_decision.local_auth_rejection = None;
}
let response = if require_stream {
maybe_execute_stream_request(state, parts, &body_bytes, trace_id, Some(&local_decision))
.await?
} else {
maybe_execute_sync_request(state, parts, &body_bytes, trace_id, Some(&local_decision))
.await?
};
Ok(match response {
LocalExecutionRequestOutcome::Responded(response) => {
LocalExecutionRequestOutcome::Responded(mark_control_executed(response))
}
LocalExecutionRequestOutcome::Exhausted(outcome) => {
LocalExecutionRequestOutcome::Exhausted(outcome)
}
LocalExecutionRequestOutcome::NoPath => LocalExecutionRequestOutcome::NoPath,
})
}
fn mark_control_executed(mut response: Response<Body>) -> Response<Body> {
response.headers_mut().insert(
HeaderName::from_static(CONTROL_EXECUTED_HEADER),
HeaderValue::from_static("true"),
);
response
}