From dc95946f86ea4b8e5764c54ab68e1d6850852da1 Mon Sep 17 00:00:00 2001 From: fylorn <249551762+fylorn@users.noreply.github.com> Date: Sat, 10 Oct 2026 22:59:33 +0800 Subject: [PATCH] Balance: say whose money the spent amount is The enterprise gateway's usage answer now says at the top level whether it is about the key (the key has limits of its own) or the owner user as a whole (it has none). `Spent` gains `scope` and the enterprise reader fills it from that field; other sources leave it empty. A key holder can then tell "this key spent $3.50" from "the account spent $3.50 across its keys". Co-Authored-By: Claude Opus 5.5 --- crates/tw-api/src/lib.rs | 3 +++ crates/tw-gateway/src/balances/parse.rs | 34 ++++++++++++++++++++++--- crates/tw-gateway/tests/balances.rs | 3 ++- 3 files changed, 35 insertions(+), 5 deletions(-) diff --git a/crates/tw-api/src/lib.rs b/crates/tw-api/src/lib.rs index ecc6ca6e..eb57765c 100644 --- a/crates/tw-api/src/lib.rs +++ b/crates/tw-api/src/lib.rs @@ -4838,6 +4838,9 @@ pub struct Spent { pub amount: f64, pub currency: String, pub period: SpentPeriod, + /// 花的是谁的:这把密钥自己的,还是它所属的用户整体的(几把密钥合计)。只有企业网关 + /// 说 —— 密钥自己设了限额时说的是密钥,没设时说的是用户整体;别的来源是空 + pub scope: Option, } slug_enum! { diff --git a/crates/tw-gateway/src/balances/parse.rs b/crates/tw-gateway/src/balances/parse.rs index 229feec2..fbf82be7 100644 --- a/crates/tw-gateway/src/balances/parse.rs +++ b/crates/tw-gateway/src/balances/parse.rs @@ -193,6 +193,7 @@ pub fn sub2api(v: &Value) -> Option { amount, currency: unit.clone(), period: SpentPeriod::Today, + scope: None, }), ..Default::default() }; @@ -286,6 +287,7 @@ pub fn newapi(subscription: &Value, usage: &Value) -> Option { amount: used, currency: USD.to_string(), period: SpentPeriod::Total, + scope: None, }); } else { r.quota = Some(BalanceQuota { @@ -316,7 +318,7 @@ pub fn is_thinkwatch(v: &Value) -> bool { /// /// 每一条限额是一个窗口,单位就是 `kind`(`requests`、`tokens`),带着它管的是这把密钥还是 /// 它所属的用户(`scope`);认不出的一条不要。这个月花了多少(`usage.cost_usd_month`)是 -/// 花费。**没有钱包,也没有总额度**:企业网关的限额都是按时间窗口的 +/// 花费,顶层的 `scope` 说它是这把密钥的还是用户整体的。**没有钱包,也没有总额度**:企业网关的限额都是按时间窗口的 pub fn thinkwatch(v: &Value) -> Option { let limits = v.get("limits")?.as_array()?; let windows = limits @@ -355,6 +357,11 @@ pub fn thinkwatch(v: &Value) -> Option { amount, currency: USD.to_string(), period: SpentPeriod::Month, + // 顶层的 `scope`:密钥自己设了限额时是这把密钥花的,没设时是用户整体花的 + scope: v + .get("scope") + .and_then(Value::as_str) + .and_then(BalanceScope::from_slug), }), ..Default::default() }) @@ -521,7 +528,8 @@ mod tests { Some(Spent { amount: 0.12, currency: "USD".into(), - period: SpentPeriod::Today + period: SpentPeriod::Today, + scope: None, }) ); } @@ -632,7 +640,8 @@ mod tests { Some(Spent { amount: 5.0, currency: "USD".into(), - period: SpentPeriod::Total + period: SpentPeriod::Total, + scope: None, }) ); assert_eq!(r.expires_at_ms, None, "0 是不到期"); @@ -658,6 +667,22 @@ mod tests { )) } + /// 顶层的 `scope` 落到花费上:密钥自己设了限额是 key,没设是用户整体;没写是空 + #[test] + fn enterprise_spent_says_whose_money_it_is() { + let with = |scope: &str| { + json(&format!( + r#"{{"scope":{scope},"usage":{{"cost_usd_month":3.5}},"limits":[],"expires_at":null}}"# + )) + }; + let spent = |v: &Value| thinkwatch(v).unwrap().spent.unwrap(); + assert_eq!(spent(&with(r#""key""#)).scope, Some(BalanceScope::Key)); + assert_eq!(spent(&with(r#""user""#)).scope, Some(BalanceScope::User)); + assert_eq!(spent(&with("null")).scope, None); + assert_eq!(spent(&with(r#""team""#)).scope, None); + assert_eq!(spent(&with(r#""user""#)).amount, 3.5); + } + fn scopes(r: &Reading) -> Vec<(String, Option)> { r.windows .iter() @@ -692,7 +717,8 @@ mod tests { Some(Spent { amount: 12.5, currency: "USD".into(), - period: SpentPeriod::Month + period: SpentPeriod::Month, + scope: None, }) ); assert_eq!(r.expires_at_ms, Some(ms("2027-01-01T00:00:00Z"))); diff --git a/crates/tw-gateway/tests/balances.rs b/crates/tw-gateway/tests/balances.rs index b7b125fd..ec085a7a 100644 --- a/crates/tw-gateway/tests/balances.rs +++ b/crates/tw-gateway/tests/balances.rs @@ -163,7 +163,8 @@ async fn auto_finds_a_sub2api_relay_and_reads_its_wallet_from_the_same_answer() Some(Spent { amount: 0.12, currency: "USD".into(), - period: SpentPeriod::Today + period: SpentPeriod::Today, + scope: None, }) ); assert_eq!(b.error, None);