[codex] Thread backend-selected near-limit prompt state through client models

This commit is contained in:
James Chu
2026-05-03 23:33:48 -04:00
parent ff27d01676
commit 53dbdbfa9d
28 changed files with 558 additions and 3 deletions
+66
View File
@@ -15,6 +15,8 @@ use codex_protocol::protocol::CreditsSnapshot;
use codex_protocol::protocol::RateLimitReachedType;
use codex_protocol::protocol::RateLimitSnapshot;
use codex_protocol::protocol::RateLimitWindow;
use codex_protocol::protocol::UsageLimitNudgeAction;
use codex_protocol::protocol::UsageLimitNudgePayload;
use reqwest::StatusCode;
use reqwest::header::CONTENT_TYPE;
use reqwest::header::HeaderMap;
@@ -452,6 +454,9 @@ impl Client {
.rate_limit_reached_type
.flatten()
.and_then(|details| Self::map_rate_limit_reached_type(details.kind));
let current_usage_limit_nudge = payload
.current_usage_limit_nudge
.map(|nudge| Self::map_usage_limit_nudge(*nudge));
let mut snapshots = vec![Self::make_rate_limit_snapshot(
Some("codex".to_string()),
/*limit_name*/ None,
@@ -459,6 +464,7 @@ impl Client {
payload.credits.flatten().map(|details| *details),
plan_type,
rate_limit_reached_type,
current_usage_limit_nudge,
)];
if let Some(additional) = payload.additional_rate_limits.flatten() {
snapshots.extend(additional.into_iter().map(|details| {
@@ -469,6 +475,7 @@ impl Client {
/*credits*/ None,
plan_type,
/*rate_limit_reached_type*/ None,
/*current_usage_limit_nudge*/ None,
)
}));
}
@@ -482,6 +489,7 @@ impl Client {
credits: Option<crate::types::CreditStatusDetails>,
plan_type: Option<AccountPlanType>,
rate_limit_reached_type: Option<RateLimitReachedType>,
current_usage_limit_nudge: Option<UsageLimitNudgePayload>,
) -> RateLimitSnapshot {
let (primary, secondary) = match rate_limit {
Some(details) => (
@@ -498,6 +506,19 @@ impl Client {
credits: Self::map_credits(credits),
plan_type,
rate_limit_reached_type,
current_usage_limit_nudge,
}
}
fn map_usage_limit_nudge(nudge: crate::types::UsageLimitNudge) -> UsageLimitNudgePayload {
UsageLimitNudgePayload {
threshold: nudge.threshold,
action: match nudge.action {
crate::types::UsageLimitNudgeAction::AddCredits => {
UsageLimitNudgeAction::AddCredits
}
crate::types::UsageLimitNudgeAction::Upgrade => UsageLimitNudgeAction::Upgrade,
},
}
}
@@ -661,6 +682,7 @@ mod tests {
rate_limit_reached_type: Some(Some(BackendRateLimitReachedType {
kind: RateLimitReachedKind::WorkspaceMemberCreditsDepleted,
})),
current_usage_limit_nudge: None,
};
let snapshots = Client::rate_limit_snapshots_from_payload(payload);
@@ -713,6 +735,7 @@ mod tests {
}])),
credits: None,
rate_limit_reached_type: None,
current_usage_limit_nudge: None,
};
let snapshots = Client::rate_limit_snapshots_from_payload(payload);
@@ -739,6 +762,7 @@ mod tests {
credits: None,
plan_type: Some(AccountPlanType::Pro),
rate_limit_reached_type: None,
current_usage_limit_nudge: None,
},
RateLimitSnapshot {
limit_id: Some("codex".to_string()),
@@ -752,6 +776,7 @@ mod tests {
credits: None,
plan_type: Some(AccountPlanType::Pro),
rate_limit_reached_type: None,
current_usage_limit_nudge: None,
},
];
@@ -796,6 +821,7 @@ mod tests {
credits: None,
additional_rate_limits: None,
rate_limit_reached_type: Some(Some(BackendRateLimitReachedType { kind })),
current_usage_limit_nudge: None,
};
let snapshots = Client::rate_limit_snapshots_from_payload(payload);
@@ -811,12 +837,52 @@ mod tests {
credits: None,
additional_rate_limits: None,
rate_limit_reached_type: None,
current_usage_limit_nudge: None,
};
let snapshots = Client::rate_limit_snapshots_from_payload(payload);
assert_eq!(snapshots[0].rate_limit_reached_type, None);
}
#[test]
fn usage_payload_preserves_current_usage_limit_nudge() {
let payload = RateLimitStatusPayload {
plan_type: crate::types::PlanType::Plus,
rate_limit: None,
credits: None,
additional_rate_limits: None,
rate_limit_reached_type: None,
current_usage_limit_nudge: Some(Box::new(crate::types::UsageLimitNudge {
threshold: 75,
action: crate::types::UsageLimitNudgeAction::AddCredits,
})),
};
let snapshots = Client::rate_limit_snapshots_from_payload(payload);
assert_eq!(
snapshots[0].current_usage_limit_nudge,
Some(UsageLimitNudgePayload {
threshold: 75,
action: UsageLimitNudgeAction::AddCredits,
})
);
}
#[test]
fn usage_payload_preserves_missing_current_usage_limit_nudge() {
let payload = RateLimitStatusPayload {
plan_type: crate::types::PlanType::Plus,
rate_limit: None,
credits: None,
additional_rate_limits: None,
rate_limit_reached_type: None,
current_usage_limit_nudge: None,
};
let snapshots = Client::rate_limit_snapshots_from_payload(payload);
assert_eq!(snapshots[0].current_usage_limit_nudge, None);
}
#[test]
fn add_credits_nudge_email_uses_expected_paths_and_bodies() {
let codex_client = Client {
+2
View File
@@ -7,6 +7,8 @@ pub use codex_backend_openapi_models::models::RateLimitStatusDetails;
pub use codex_backend_openapi_models::models::RateLimitStatusPayload;
pub use codex_backend_openapi_models::models::RateLimitWindowSnapshot;
pub use codex_backend_openapi_models::models::TaskListItem;
pub use codex_backend_openapi_models::models::UsageLimitNudge;
pub use codex_backend_openapi_models::models::UsageLimitNudgeAction;
use serde::Deserialize;
use serde::de::Deserializer;