Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions crates/tw-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,13 @@ pub enum Event {
#[serde(default, skip_serializing_if = "Option::is_none")]
key_masked: Option<String>,
provider: String,
/// `provider` 那一家怎么收钱,和 `RequestRouted::billing` 同一套词。
///
/// **算数的是 `RequestRouted` 报的那个**(最终服务的那家)。这里先报
/// 一个,是因为不是每个请求都等得到那一条:上游应答之前客户端就走了
/// 的、WebSocket 升级没完成就断开的、被第二阶段规则拒绝的,手上只有
/// 这一个 —— 少了它,那一行说不出该按什么记账。
billing: String,
/// 客户端要的模型名。**成本要靠它查价**,而它只在请求体里 ——
/// 少了这个字段,落库那一步就只能记一笔没有模型的账
model: String,
Expand Down Expand Up @@ -190,6 +197,9 @@ pub enum Event {
/// **单独一个事件,因为成功和失败两条路都要发它。**挂在
/// `RequestFinished` 上的话,失败的那条路就没有尝试链 —— 而那恰恰
/// 是最需要看它的时候。
///
/// WebSocket 那条路也发:和上游的握手有了结果就发。那条路不做故障转移,
/// 尝试链只有一跳。
RequestRouted {
id: u64,
/// 命中了哪条规则。**日志和界面都要显示它** —— 「命中第 4 条」
Expand All @@ -204,6 +214,9 @@ pub enum Event {
///
/// **必须跟着这次请求走,不能事后查配置** —— 配置随时会被热重载,
/// 而一条三天前的记录该按它当时那家的计费方式算。
///
/// 一家都没接下时是 `per-token`:那一行没有用量、没有金额,记成订阅
/// 的话会被数进订阅额度的次数里。
billing: String,
},
/// 一个请求发出前,按出站脱敏的规则找到了东西。
Expand Down Expand Up @@ -522,6 +535,9 @@ pub struct AttemptView {
/// —— 请求本身有问题,换一个上游也一样被拒。
/// - `status`:上游返回 5xx 或 429,换下一个上游。
/// - `error`:没有收到响应(超时、无法连接)。
///
/// WebSocket 的那一跳是一次握手:上游同意升级(101)是 `served`,回了别的
/// 状态码是 `status`,连不上是 `error`。
pub outcome: String,
/// 上游返回的状态码。`error` 时没有
#[serde(default, skip_serializing_if = "Option::is_none")]
Expand Down Expand Up @@ -1931,12 +1947,14 @@ pub struct HistoryRow {
/// 客户端没等到响应结束就走了。**不是失败**(`error` 是空的);用量
/// 只算到断开那一刻,所以有金额的话一定是估算
pub cancelled: bool,
/// 服务它的那家怎么收钱:`per-token` / `subscription` / `unknown`
/// 服务它的那家怎么收钱:`per-token` / `subscription` / `free` / `unknown`。
/// 本地应答的是 `free`:网关自己答的,费用确实是零
pub billing: String,
/// 缓存命中省下了多少微分。`None` = 算不出来
#[serde(default, skip_serializing_if = "Option::is_none")]
pub cache_saved_micros: Option<i64>,
/// 路由决策与尝试链。没经过路由的(WebSocket、本地应答)没有它
/// 路由决策与尝试链。本地应答的没有它;路由还没报出结论请求就结束了的
/// 也没有:上游应答之前客户端就走了、被第二阶段规则拒绝
#[serde(default, skip_serializing_if = "Option::is_none")]
pub routing: Option<RoutingView>,
/// 按什么价格算的。没算出金额的没有它
Expand Down Expand Up @@ -3005,6 +3023,7 @@ mod tests {
client_hint: None,
session_fp: None,
provider: "p".into(),
billing: "per-token".into(),
model: "m".into(),
method: "POST".into(),
path: "/v1/messages".into(),
Expand Down
1 change: 1 addition & 0 deletions crates/tw-control/tests/in_flight.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ fn started(id: u64, model: &str) -> tw_api::Event {
client_hint: Some("claude-code".into()),
session_fp: None,
provider: "官方".into(),
billing: "per-token".into(),
model: model.into(),
method: "POST".into(),
path: "/v1/messages".into(),
Expand Down
43 changes: 27 additions & 16 deletions crates/tw-gateway/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1011,7 +1011,8 @@ pub fn router(state: AppState) -> Router {
///
/// 路由照走一遍 —— **一次升级也是一次请求**,`deny` 规则、熔断对它一样
/// 有效。之后把连接交给 [`crate::ws::proxy`],那里会在
/// 每一帧上重新点一遍管线的保护。
/// 每一帧上重新点一遍管线的保护。路由事件也由那边发:选中的那一家接没
/// 接下,要和它握完手才知道。
#[allow(clippy::too_many_arguments)]
async fn ws_upgrade(
state: AppState,
Expand Down Expand Up @@ -1074,7 +1075,6 @@ async fn ws_upgrade(
"The credential for upstream `{upstream}` could not be obtained: {detail}"
))
})?;
let url = crate::ws::upstream_url(&provider.base_url, uri.path(), query.as_deref());
let id = state.bus.next_id();
state.bus.emit(tw_api::Event::RequestStarted {
id,
Expand All @@ -1084,6 +1084,7 @@ async fn ws_upgrade(
peer: from.peer,
key_masked: from.key,
provider: name.clone(),
billing: state.billing_of(provider).slug().to_string(),
model: String::new(),
method: "WS".to_string(),
path: uri.path().to_string(),
Expand All @@ -1100,7 +1101,13 @@ async fn ws_upgrade(
now_ms() as i64,
None,
);
let provider = provider.clone();
let upstream = crate::ws::Upstream {
url: crate::ws::upstream_url(&provider.base_url, uri.path(), query.as_deref()),
headers: upstream_headers,
provider: provider.clone(),
rule: decision.matched_rule,
group: decision.via_group,
};
let rules = crate::ws::Rules {
redact_mode: rt.config.security.redact.mode,
redact: rt.redact.clone(),
Expand All @@ -1112,17 +1119,7 @@ async fn ws_upgrade(
let _live = live;
let mut ending = ending;
ending.responded(101);
crate::ws::proxy(
state,
sock,
url,
upstream_headers,
provider,
rules,
id,
ending,
)
.await;
crate::ws::proxy(state, sock, upstream, rules, id, ending).await;
}))
}

Expand Down Expand Up @@ -1670,6 +1667,11 @@ async fn pipeline(
}

let id = state.bus.next_id();
// 头一个候选。故障转移之后实际服务的是谁、按什么记账,由后面的
// `RequestRouted` 改过来
let first = alive
.first()
.and_then(|n| rt.config.providers.iter().find(|p| &p.name == *n));
state.bus.emit(tw_api::Event::RequestStarted {
id,
client: client_name.clone(),
Expand All @@ -1682,6 +1684,11 @@ async fn pipeline(
peer: from.peer,
key_masked: from.key,
provider: alive.first().map(|s| s.as_str()).unwrap_or("?").to_string(),
billing: first
.map(|p| state.billing_of(p))
.unwrap_or(tw_config::Billing::PerToken)
.slug()
.to_string(),
model: facts.model.clone(),
method: "POST".to_string(),
path: uri.path().to_string(),
Expand Down Expand Up @@ -2611,7 +2618,7 @@ pub async fn serve(state: AppState, addr: std::net::SocketAddr) -> std::io::Resu
}

/// 上游回了话的一跳:`served` 或者 `status`。
fn hop(
pub(crate) fn hop(
provider: &str,
outcome: &str,
status: u16,
Expand All @@ -2627,7 +2634,11 @@ fn hop(
}

/// 没有收到响应的一跳。
fn hop_failed(provider: &str, error: String, started: std::time::Instant) -> tw_api::AttemptView {
pub(crate) fn hop_failed(
provider: &str,
error: String,
started: std::time::Instant,
) -> tw_api::AttemptView {
tw_api::AttemptView {
provider: provider.to_string(),
outcome: "error".to_string(),
Expand Down
Loading
Loading