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
40 changes: 20 additions & 20 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ repository = "https://github.com/ThinkWatchProject/ThinkWatch-Core"

# 二进制走 CalVer,crate 走 SemVer —— 两者是两回事,卖给不同的人。
# 这里是 crate 的版本。
version = "0.8.0"
version = "0.8.1"

[workspace.dependencies]
# ── 内部 crate ───────────────────────────────────────────
Expand Down
4 changes: 4 additions & 0 deletions crates/tw-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1899,6 +1899,10 @@ pub struct ChatgptLoginStatus {
/// ChatGPT 账号的用量(`GET /providers/{name}/chatgpt/usage`)。
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ChatgptUsage {
/// 登的是哪个账号。**只有邮箱** —— 同一份回答里的用户 ID
/// 和账户 ID 不往外带:界面认账号靠邮箱,那两个读不出是谁
#[serde(default, skip_serializing_if = "Option::is_none")]
pub email: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub plan: Option<String>,
/// 额度窗口,词表同 [`QuotaWindow`]
Expand Down
14 changes: 8 additions & 6 deletions crates/tw-control/src/chatgpt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -928,7 +928,9 @@ async fn usage(
Ok(Json(usage))
}

/// `wham/usage` 的回答。**只取额度相关的几项**:同一份回答里还有邮箱和用户 ID,不往外带
/// `wham/usage` 的回答。**只取界面要用的几项**:额度、套餐,以及邮箱
/// —— 账号不止一个时,邮箱是用户分得清哪个是哪个的唯一一项。同一份回答里
/// 的用户 ID 和账户 ID 不往外带:界面用不上,而它们一旦出去就会进日志
fn parse_usage(v: &Value) -> tw_api::ChatgptUsage {
let window = |w: &Value| {
let secs = w.get("limit_window_seconds")?.as_u64().filter(|s| *s > 0)?;
Expand All @@ -942,6 +944,7 @@ fn parse_usage(v: &Value) -> tw_api::ChatgptUsage {
};
let limits = &v["rate_limit"];
tw_api::ChatgptUsage {
email: v["email"].as_str().map(str::to_string),
plan: v["plan_type"].as_str().map(str::to_string),
windows: [&limits["primary_window"], &limits["secondary_window"]]
.into_iter()
Expand Down Expand Up @@ -1039,7 +1042,7 @@ mod tests {
}

#[test]
fn usage_keeps_only_the_limits() {
fn usage_keeps_the_limits_and_who_is_signed_in() {
let v = serde_json::json!({
"email": "someone@example.com",
"user_id": "user-1",
Expand All @@ -1053,16 +1056,15 @@ mod tests {
"rate_limit_reset_credits": {"available_count": 2}
});
let u = parse_usage(&v);
assert_eq!(u.email.as_deref(), Some("someone@example.com"));
assert_eq!(u.plan.as_deref(), Some("plus"));
assert_eq!(u.windows.len(), 1);
assert_eq!(u.windows[0].window, "weekly");
assert_eq!(u.windows[0].used_percent, 21.0);
assert_eq!(u.windows[0].reset_in_secs, Some(410907));
assert_eq!(u.reset_credits, Some(2));
// 用户 ID 不往外带:界面读不出是谁,而它一旦出去就会进日志
let json = serde_json::to_string(&u).unwrap();
assert!(
!json.contains("someone@example.com") && !json.contains("user-1"),
"{json}"
);
assert!(!json.contains("user-1"), "{json}");
}
}
5 changes: 4 additions & 1 deletion crates/tw-control/tests/chatgpt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -817,9 +817,12 @@ async fn usage_shows_the_limits_without_personal_details() {
json!([{"window": "weekly", "used_percent": 21.0, "reset_in_secs": 410907}]),
"长度为 0 的窗口不算"
);
// 邮箱要给:账号不止一个时,它是用户分辨哪个是哪个的唯一一项
assert_eq!(v["email"], "someone@example.com");
// 用户 ID 和账户 ID 不给:界面读不出是谁,而它们一旦出去就会进日志
let text = v.to_string();
assert!(
!text.contains("someone@example.com") && !text.contains("user-1"),
!text.contains("user-1") && !text.contains("acct-1"),
"{text}"
);

Expand Down
Loading