Compare commits
1
Commits
v0.1.13
...
7f6ca8d353
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7f6ca8d353 |
@@ -6,7 +6,8 @@
|
||||
//! `--input-format stream-json` process if startup latency hurts.
|
||||
|
||||
use crate::{
|
||||
ContentBlock, Error, Message, Provider, Request, Result, Role, StopReason, StreamEvent, Usage,
|
||||
ContentBlock, Error, Message, Provider, Quota, QuotaWindow, Request, Result, Role, StopReason,
|
||||
StreamEvent, Usage,
|
||||
};
|
||||
use futures::StreamExt;
|
||||
use futures::stream::BoxStream;
|
||||
@@ -124,6 +125,30 @@ fn usage(u: &Value) -> Usage {
|
||||
}
|
||||
}
|
||||
|
||||
/// `rate_limit_event` -> window. claude reports `utilization` (0..1) when known, else only a
|
||||
/// status; `surpassedThreshold` is the best percent then. ponytail: no percent -> no window.
|
||||
fn window(v: &Value) -> Option<QuotaWindow> {
|
||||
let i = &v["rate_limit_info"];
|
||||
let label = match i["rateLimitType"].as_str()? {
|
||||
"five_hour" => "5h".to_string(),
|
||||
"seven_day" => "7d".into(),
|
||||
"seven_day_opus" => "7d opus".into(),
|
||||
"seven_day_sonnet" => "7d sonnet".into(),
|
||||
o => o.replace('_', " "),
|
||||
};
|
||||
let used_pct = match (i["utilization"].as_f64(), i["surpassedThreshold"].as_f64()) {
|
||||
(Some(u), _) => u * 100.0,
|
||||
_ if i["status"] == "rejected" => 100.0,
|
||||
(_, Some(t)) => t * 100.0,
|
||||
_ => return None,
|
||||
};
|
||||
Some(QuotaWindow {
|
||||
label,
|
||||
used_pct,
|
||||
resets_at: i["resetsAt"].as_u64(),
|
||||
})
|
||||
}
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl Provider for ClaudeCli {
|
||||
async fn models(&self) -> Result<Vec<crate::models::ModelInfo>> {
|
||||
@@ -184,6 +209,7 @@ impl Provider for ClaudeCli {
|
||||
let mut lines = BufReader::new(out).lines();
|
||||
let (mut text, mut think) = (String::new(), String::new());
|
||||
let mut done = false;
|
||||
let mut quota = Quota::default();
|
||||
while let Ok(Some(l)) = lines.next_line().await {
|
||||
let Ok(v) = serde_json::from_str::<Value>(&l) else {
|
||||
continue;
|
||||
@@ -225,6 +251,13 @@ impl Provider for ClaudeCli {
|
||||
}
|
||||
}
|
||||
}
|
||||
Some("rate_limit_event") => {
|
||||
if let Some(w) = window(&v) {
|
||||
quota.windows.retain(|x| x.label != w.label);
|
||||
quota.windows.push(w);
|
||||
let _ = tx.send(Ok(StreamEvent::Quota(quota.clone())));
|
||||
}
|
||||
}
|
||||
Some("result") => {
|
||||
done = true;
|
||||
if v["is_error"].as_bool().unwrap_or(false) {
|
||||
@@ -288,6 +321,22 @@ mod tests {
|
||||
assert_eq!(tool_line(&b), "X: {\"a\":1}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rate_limit_event_maps_to_window() {
|
||||
let w = |i: Value| window(&json!({"rate_limit_info": i}));
|
||||
let a =
|
||||
w(json!({"rateLimitType": "five_hour", "utilization": 0.42, "resetsAt": 99})).unwrap();
|
||||
assert_eq!(
|
||||
(a.label.as_str(), a.used_pct.round(), a.resets_at),
|
||||
("5h", 42.0, Some(99))
|
||||
);
|
||||
let b = w(json!({"rateLimitType": "seven_day", "status": "rejected"})).unwrap();
|
||||
assert_eq!(b.used_pct, 100.0);
|
||||
let c = w(json!({"rateLimitType": "seven_day", "status": "allowed_warning", "surpassedThreshold": 0.75})).unwrap();
|
||||
assert_eq!(c.used_pct, 75.0);
|
||||
assert!(w(json!({"rateLimitType": "five_hour", "status": "allowed"})).is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn prompt_replays_history_only_on_fresh_session() {
|
||||
let m = vec![
|
||||
|
||||
Reference in new issue
Block a user