diff --git a/codex-rs/app-server-protocol/schema/json/ServerNotification.json b/codex-rs/app-server-protocol/schema/json/ServerNotification.json index 82914f3a6f..b9d17b3310 100644 --- a/codex-rs/app-server-protocol/schema/json/ServerNotification.json +++ b/codex-rs/app-server-protocol/schema/json/ServerNotification.json @@ -2425,6 +2425,16 @@ } ] }, + "currentUsageLimitNudge": { + "anyOf": [ + { + "$ref": "#/definitions/UsageLimitNudge" + }, + { + "type": "null" + } + ] + }, "limitId": { "type": [ "string", @@ -4373,6 +4383,30 @@ ], "type": "string" }, + "UsageLimitNudge": { + "properties": { + "action": { + "$ref": "#/definitions/UsageLimitNudgeAction" + }, + "threshold": { + "format": "uint8", + "minimum": 0.0, + "type": "integer" + } + }, + "required": [ + "action", + "threshold" + ], + "type": "object" + }, + "UsageLimitNudgeAction": { + "enum": [ + "add_credits", + "upgrade" + ], + "type": "string" + }, "UserInput": { "oneOf": [ { diff --git a/codex-rs/app-server-protocol/schema/json/codex_app_server_protocol.schemas.json b/codex-rs/app-server-protocol/schema/json/codex_app_server_protocol.schemas.json index f856b43d66..218b037a1b 100644 --- a/codex-rs/app-server-protocol/schema/json/codex_app_server_protocol.schemas.json +++ b/codex-rs/app-server-protocol/schema/json/codex_app_server_protocol.schemas.json @@ -12770,6 +12770,16 @@ } ] }, + "currentUsageLimitNudge": { + "anyOf": [ + { + "$ref": "#/definitions/v2/UsageLimitNudge" + }, + { + "type": "null" + } + ] + }, "limitId": { "type": [ "string", @@ -17804,6 +17814,30 @@ "title": "TurnSteerResponse", "type": "object" }, + "UsageLimitNudge": { + "properties": { + "action": { + "$ref": "#/definitions/v2/UsageLimitNudgeAction" + }, + "threshold": { + "format": "uint8", + "minimum": 0.0, + "type": "integer" + } + }, + "required": [ + "action", + "threshold" + ], + "type": "object" + }, + "UsageLimitNudgeAction": { + "enum": [ + "add_credits", + "upgrade" + ], + "type": "string" + }, "UserInput": { "oneOf": [ { diff --git a/codex-rs/app-server-protocol/schema/json/codex_app_server_protocol.v2.schemas.json b/codex-rs/app-server-protocol/schema/json/codex_app_server_protocol.v2.schemas.json index c17efe7a45..0d276892e0 100644 --- a/codex-rs/app-server-protocol/schema/json/codex_app_server_protocol.v2.schemas.json +++ b/codex-rs/app-server-protocol/schema/json/codex_app_server_protocol.v2.schemas.json @@ -9423,6 +9423,16 @@ } ] }, + "currentUsageLimitNudge": { + "anyOf": [ + { + "$ref": "#/definitions/UsageLimitNudge" + }, + { + "type": "null" + } + ] + }, "limitId": { "type": [ "string", @@ -15690,6 +15700,30 @@ "title": "TurnSteerResponse", "type": "object" }, + "UsageLimitNudge": { + "properties": { + "action": { + "$ref": "#/definitions/UsageLimitNudgeAction" + }, + "threshold": { + "format": "uint8", + "minimum": 0.0, + "type": "integer" + } + }, + "required": [ + "action", + "threshold" + ], + "type": "object" + }, + "UsageLimitNudgeAction": { + "enum": [ + "add_credits", + "upgrade" + ], + "type": "string" + }, "UserInput": { "oneOf": [ { diff --git a/codex-rs/app-server-protocol/schema/json/v2/AccountRateLimitsUpdatedNotification.json b/codex-rs/app-server-protocol/schema/json/v2/AccountRateLimitsUpdatedNotification.json index 96fefb228b..e6a1db7c2f 100644 --- a/codex-rs/app-server-protocol/schema/json/v2/AccountRateLimitsUpdatedNotification.json +++ b/codex-rs/app-server-protocol/schema/json/v2/AccountRateLimitsUpdatedNotification.json @@ -61,6 +61,16 @@ } ] }, + "currentUsageLimitNudge": { + "anyOf": [ + { + "$ref": "#/definitions/UsageLimitNudge" + }, + { + "type": "null" + } + ] + }, "limitId": { "type": [ "string", @@ -141,6 +151,30 @@ "usedPercent" ], "type": "object" + }, + "UsageLimitNudge": { + "properties": { + "action": { + "$ref": "#/definitions/UsageLimitNudgeAction" + }, + "threshold": { + "format": "uint8", + "minimum": 0.0, + "type": "integer" + } + }, + "required": [ + "action", + "threshold" + ], + "type": "object" + }, + "UsageLimitNudgeAction": { + "enum": [ + "add_credits", + "upgrade" + ], + "type": "string" } }, "properties": { diff --git a/codex-rs/app-server-protocol/schema/json/v2/GetAccountRateLimitsResponse.json b/codex-rs/app-server-protocol/schema/json/v2/GetAccountRateLimitsResponse.json index 8f7db24e7f..2a58fbb1a5 100644 --- a/codex-rs/app-server-protocol/schema/json/v2/GetAccountRateLimitsResponse.json +++ b/codex-rs/app-server-protocol/schema/json/v2/GetAccountRateLimitsResponse.json @@ -61,6 +61,16 @@ } ] }, + "currentUsageLimitNudge": { + "anyOf": [ + { + "$ref": "#/definitions/UsageLimitNudge" + }, + { + "type": "null" + } + ] + }, "limitId": { "type": [ "string", @@ -141,6 +151,30 @@ "usedPercent" ], "type": "object" + }, + "UsageLimitNudge": { + "properties": { + "action": { + "$ref": "#/definitions/UsageLimitNudgeAction" + }, + "threshold": { + "format": "uint8", + "minimum": 0.0, + "type": "integer" + } + }, + "required": [ + "action", + "threshold" + ], + "type": "object" + }, + "UsageLimitNudgeAction": { + "enum": [ + "add_credits", + "upgrade" + ], + "type": "string" } }, "properties": { diff --git a/codex-rs/app-server-protocol/schema/typescript/v2/RateLimitSnapshot.ts b/codex-rs/app-server-protocol/schema/typescript/v2/RateLimitSnapshot.ts index dc8417a304..89502f758f 100644 --- a/codex-rs/app-server-protocol/schema/typescript/v2/RateLimitSnapshot.ts +++ b/codex-rs/app-server-protocol/schema/typescript/v2/RateLimitSnapshot.ts @@ -5,5 +5,6 @@ import type { PlanType } from "../PlanType"; import type { CreditsSnapshot } from "./CreditsSnapshot"; import type { RateLimitReachedType } from "./RateLimitReachedType"; import type { RateLimitWindow } from "./RateLimitWindow"; +import type { UsageLimitNudge } from "./UsageLimitNudge"; -export type RateLimitSnapshot = { limitId: string | null, limitName: string | null, primary: RateLimitWindow | null, secondary: RateLimitWindow | null, credits: CreditsSnapshot | null, planType: PlanType | null, rateLimitReachedType: RateLimitReachedType | null, }; +export type RateLimitSnapshot = { limitId: string | null, limitName: string | null, primary: RateLimitWindow | null, secondary: RateLimitWindow | null, credits: CreditsSnapshot | null, planType: PlanType | null, rateLimitReachedType: RateLimitReachedType | null, currentUsageLimitNudge: UsageLimitNudge | null, }; diff --git a/codex-rs/app-server-protocol/schema/typescript/v2/UsageLimitNudge.ts b/codex-rs/app-server-protocol/schema/typescript/v2/UsageLimitNudge.ts new file mode 100644 index 0000000000..5c16b58ee5 --- /dev/null +++ b/codex-rs/app-server-protocol/schema/typescript/v2/UsageLimitNudge.ts @@ -0,0 +1,6 @@ +// GENERATED CODE! DO NOT MODIFY BY HAND! + +// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually. +import type { UsageLimitNudgeAction } from "./UsageLimitNudgeAction"; + +export type UsageLimitNudge = { threshold: number, action: UsageLimitNudgeAction, }; diff --git a/codex-rs/app-server-protocol/schema/typescript/v2/UsageLimitNudgeAction.ts b/codex-rs/app-server-protocol/schema/typescript/v2/UsageLimitNudgeAction.ts new file mode 100644 index 0000000000..82180b074c --- /dev/null +++ b/codex-rs/app-server-protocol/schema/typescript/v2/UsageLimitNudgeAction.ts @@ -0,0 +1,5 @@ +// GENERATED CODE! DO NOT MODIFY BY HAND! + +// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually. + +export type UsageLimitNudgeAction = "add_credits" | "upgrade"; diff --git a/codex-rs/app-server-protocol/schema/typescript/v2/index.ts b/codex-rs/app-server-protocol/schema/typescript/v2/index.ts index d369ba3423..c3b08f81ce 100644 --- a/codex-rs/app-server-protocol/schema/typescript/v2/index.ts +++ b/codex-rs/app-server-protocol/schema/typescript/v2/index.ts @@ -428,6 +428,8 @@ export type { TurnStartedNotification } from "./TurnStartedNotification"; export type { TurnStatus } from "./TurnStatus"; export type { TurnSteerParams } from "./TurnSteerParams"; export type { TurnSteerResponse } from "./TurnSteerResponse"; +export type { UsageLimitNudge } from "./UsageLimitNudge"; +export type { UsageLimitNudgeAction } from "./UsageLimitNudgeAction"; export type { UserInput } from "./UserInput"; export type { WarningNotification } from "./WarningNotification"; export type { WebSearchAction } from "./WebSearchAction"; diff --git a/codex-rs/app-server-protocol/src/protocol/v2.rs b/codex-rs/app-server-protocol/src/protocol/v2.rs index cbcc12c3a7..068ed30ddd 100644 --- a/codex-rs/app-server-protocol/src/protocol/v2.rs +++ b/codex-rs/app-server-protocol/src/protocol/v2.rs @@ -101,6 +101,8 @@ use codex_protocol::protocol::SubAgentSource as CoreSubAgentSource; use codex_protocol::protocol::ThreadGoalStatus as CoreThreadGoalStatus; use codex_protocol::protocol::TokenUsage as CoreTokenUsage; use codex_protocol::protocol::TokenUsageInfo as CoreTokenUsageInfo; +use codex_protocol::protocol::UsageLimitNudge as CoreUsageLimitNudge; +use codex_protocol::protocol::UsageLimitNudgeAction as CoreUsageLimitNudgeAction; use codex_protocol::request_permissions::PermissionGrantScope as CorePermissionGrantScope; use codex_protocol::request_permissions::RequestPermissionProfile as CoreRequestPermissionProfile; use codex_protocol::user_input::ByteRange as CoreByteRange; @@ -7867,6 +7869,7 @@ pub struct RateLimitSnapshot { pub credits: Option, pub plan_type: Option, pub rate_limit_reached_type: Option, + pub current_usage_limit_nudge: Option, } impl From for RateLimitSnapshot { @@ -7881,6 +7884,54 @@ impl From for RateLimitSnapshot { rate_limit_reached_type: value .rate_limit_reached_type .map(RateLimitReachedType::from), + current_usage_limit_nudge: value + .current_usage_limit_nudge + .as_ref() + .and_then(CoreUsageLimitNudge::from_payload) + .map(UsageLimitNudge::from), + } + } +} + +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)] +#[serde(rename_all = "camelCase")] +#[ts(export_to = "v2/")] +pub struct UsageLimitNudge { + pub threshold: u8, + pub action: UsageLimitNudgeAction, +} + +impl From for UsageLimitNudge { + fn from(value: CoreUsageLimitNudge) -> Self { + Self { + threshold: value.threshold.as_percent(), + action: value.action.into(), + } + } +} + +#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq, JsonSchema, TS)] +#[serde(rename_all = "snake_case")] +#[ts(export_to = "v2/", rename_all = "snake_case")] +pub enum UsageLimitNudgeAction { + AddCredits, + Upgrade, +} + +impl From for UsageLimitNudgeAction { + fn from(value: CoreUsageLimitNudgeAction) -> Self { + match value { + CoreUsageLimitNudgeAction::AddCredits => Self::AddCredits, + CoreUsageLimitNudgeAction::Upgrade => Self::Upgrade, + } + } +} + +impl From for CoreUsageLimitNudgeAction { + fn from(value: UsageLimitNudgeAction) -> Self { + match value { + UsageLimitNudgeAction::AddCredits => Self::AddCredits, + UsageLimitNudgeAction::Upgrade => Self::Upgrade, } } } @@ -11309,4 +11360,75 @@ mod tests { "unexpected error: {err}" ); } + + #[test] + fn rate_limit_snapshot_preserves_current_usage_limit_nudge() { + let active: RateLimitSnapshot = serde_json::from_value(json!({ + "limitId": "codex", + "limitName": null, + "primary": null, + "secondary": null, + "credits": null, + "planType": null, + "rateLimitReachedType": null, + "currentUsageLimitNudge": { + "threshold": 75, + "action": "upgrade" + } + })) + .expect("active snapshot should deserialize"); + assert_eq!( + active.current_usage_limit_nudge, + Some(UsageLimitNudge { + threshold: 75, + action: UsageLimitNudgeAction::Upgrade, + }) + ); + assert_eq!( + serde_json::to_value(&active) + .expect("active snapshot should serialize") + .get("currentUsageLimitNudge"), + Some(&json!({ + "threshold": 75, + "action": "upgrade" + })) + ); + + let inactive: RateLimitSnapshot = serde_json::from_value(json!({ + "limitId": "codex", + "limitName": null, + "primary": null, + "secondary": null, + "credits": null, + "planType": null, + "rateLimitReachedType": null, + "currentUsageLimitNudge": null + })) + .expect("inactive snapshot should deserialize"); + assert_eq!(inactive.current_usage_limit_nudge, None); + assert_eq!( + serde_json::to_value(&inactive) + .expect("inactive snapshot should serialize") + .get("currentUsageLimitNudge"), + Some(&json!(null)) + ); + + let missing: RateLimitSnapshot = serde_json::from_value(json!({ + "limitId": "codex", + "limitName": null, + "primary": null, + "secondary": null, + "credits": null, + "planType": null, + "rateLimitReachedType": null + })) + .expect("missing snapshot should deserialize"); + assert_eq!(missing.current_usage_limit_nudge, None); + assert_eq!( + serde_json::to_value(&missing) + .expect("missing snapshot should serialize") + .get("currentUsageLimitNudge"), + Some(&json!(null)) + ); + } } diff --git a/codex-rs/app-server/README.md b/codex-rs/app-server/README.md index dab47ec3a2..111cb7c098 100644 --- a/codex-rs/app-server/README.md +++ b/codex-rs/app-server/README.md @@ -1720,7 +1720,7 @@ Field notes: ```json { "method": "account/rateLimits/read", "id": 7 } -{ "id": 7, "result": { "rateLimits": { "primary": { "usedPercent": 25, "windowDurationMins": 15, "resetsAt": 1730947200 }, "secondary": null, "rateLimitReachedType": null } } } +{ "id": 7, "result": { "rateLimits": { "primary": { "usedPercent": 75, "windowDurationMins": 15, "resetsAt": 1730947200 }, "secondary": null, "rateLimitReachedType": null, "currentUsageLimitNudge": { "threshold": 75, "action": "add_credits" } } } } { "method": "account/rateLimits/updated", "params": { "rateLimits": { … } } } ``` @@ -1730,6 +1730,7 @@ Field notes: - `windowDurationMins` is the quota window length. - `resetsAt` is a Unix timestamp (seconds) for the next reset. - `rateLimitReachedType` identifies the backend-classified limit state when one has been reached. +- `currentUsageLimitNudge` is the current backend-selected near-limit prompt, or `null` when no prompt should be shown. ### 8) Notify a workspace owner about a limit diff --git a/codex-rs/app-server/src/bespoke_event_handling.rs b/codex-rs/app-server/src/bespoke_event_handling.rs index 628034da72..10a1893863 100644 --- a/codex-rs/app-server/src/bespoke_event_handling.rs +++ b/codex-rs/app-server/src/bespoke_event_handling.rs @@ -3521,6 +3521,7 @@ mod tests { }), plan_type: None, rate_limit_reached_type: None, + current_usage_limit_nudge: None, }; handle_token_count_event( diff --git a/codex-rs/app-server/src/outgoing_message.rs b/codex-rs/app-server/src/outgoing_message.rs index 34441f83a0..775638cce9 100644 --- a/codex-rs/app-server/src/outgoing_message.rs +++ b/codex-rs/app-server/src/outgoing_message.rs @@ -787,6 +787,7 @@ mod tests { credits: None, plan_type: Some(PlanType::Plus), rate_limit_reached_type: None, + current_usage_limit_nudge: None, }, }); @@ -806,7 +807,8 @@ mod tests { "secondary": null, "credits": null, "planType": "plus", - "rateLimitReachedType": null + "rateLimitReachedType": null, + "currentUsageLimitNudge": null } }, }), diff --git a/codex-rs/app-server/tests/suite/v2/rate_limits.rs b/codex-rs/app-server/tests/suite/v2/rate_limits.rs index b15960c1e9..35f3ac6ee1 100644 --- a/codex-rs/app-server/tests/suite/v2/rate_limits.rs +++ b/codex-rs/app-server/tests/suite/v2/rate_limits.rs @@ -183,6 +183,7 @@ async fn get_account_rate_limits_returns_snapshot() -> Result<()> { credits: None, plan_type: Some(AccountPlanType::Pro), rate_limit_reached_type: Some(RateLimitReachedType::WorkspaceMemberUsageLimitReached), + current_usage_limit_nudge: None, }, rate_limits_by_limit_id: Some( [ @@ -206,6 +207,7 @@ async fn get_account_rate_limits_returns_snapshot() -> Result<()> { rate_limit_reached_type: Some( RateLimitReachedType::WorkspaceMemberUsageLimitReached, ), + current_usage_limit_nudge: None, }, ), ( @@ -222,6 +224,7 @@ async fn get_account_rate_limits_returns_snapshot() -> Result<()> { credits: None, plan_type: Some(AccountPlanType::Pro), rate_limit_reached_type: None, + current_usage_limit_nudge: None, }, ), ] diff --git a/codex-rs/backend-client/src/client.rs b/codex-rs/backend-client/src/client.rs index 6365d527ed..a5ce9449ad 100644 --- a/codex-rs/backend-client/src/client.rs +++ b/codex-rs/backend-client/src/client.rs @@ -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, plan_type: Option, rate_limit_reached_type: Option, + current_usage_limit_nudge: Option, ) -> 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 { diff --git a/codex-rs/backend-client/src/types.rs b/codex-rs/backend-client/src/types.rs index d8d24ab9fc..044f5a5ce2 100644 --- a/codex-rs/backend-client/src/types.rs +++ b/codex-rs/backend-client/src/types.rs @@ -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; diff --git a/codex-rs/codex-api/src/rate_limits.rs b/codex-rs/codex-api/src/rate_limits.rs index 979500cdab..4d9798b6a5 100644 --- a/codex-rs/codex-api/src/rate_limits.rs +++ b/codex-rs/codex-api/src/rate_limits.rs @@ -94,6 +94,7 @@ pub fn parse_rate_limit_for_limit( credits, plan_type: None, rate_limit_reached_type: None, + current_usage_limit_nudge: None, }) } @@ -158,6 +159,7 @@ pub fn parse_rate_limit_event(payload: &str) -> Option { credits, plan_type: event.plan_type, rate_limit_reached_type: None, + current_usage_limit_nudge: None, }) } diff --git a/codex-rs/codex-backend-openapi-models/src/models/mod.rs b/codex-rs/codex-backend-openapi-models/src/models/mod.rs index c881822b37..8d42009502 100644 --- a/codex-rs/codex-backend-openapi-models/src/models/mod.rs +++ b/codex-rs/codex-backend-openapi-models/src/models/mod.rs @@ -44,3 +44,7 @@ pub use self::rate_limit_window_snapshot::RateLimitWindowSnapshot; pub(crate) mod credit_status_details; pub use self::credit_status_details::CreditStatusDetails; + +pub(crate) mod usage_limit_nudge; +pub use self::usage_limit_nudge::UsageLimitNudge; +pub use self::usage_limit_nudge::UsageLimitNudgeAction; diff --git a/codex-rs/codex-backend-openapi-models/src/models/rate_limit_status_payload.rs b/codex-rs/codex-backend-openapi-models/src/models/rate_limit_status_payload.rs index 56d4a8ee10..bf03982fd5 100644 --- a/codex-rs/codex-backend-openapi-models/src/models/rate_limit_status_payload.rs +++ b/codex-rs/codex-backend-openapi-models/src/models/rate_limit_status_payload.rs @@ -44,6 +44,12 @@ pub struct RateLimitStatusPayload { skip_serializing_if = "Option::is_none" )] pub rate_limit_reached_type: Option>, + #[serde( + rename = "current_usage_limit_nudge", + default, + skip_serializing_if = "Option::is_none" + )] + pub current_usage_limit_nudge: Option>, } impl RateLimitStatusPayload { @@ -54,6 +60,7 @@ impl RateLimitStatusPayload { credits: None, additional_rate_limits: None, rate_limit_reached_type: None, + current_usage_limit_nudge: None, } } } diff --git a/codex-rs/codex-backend-openapi-models/src/models/usage_limit_nudge.rs b/codex-rs/codex-backend-openapi-models/src/models/usage_limit_nudge.rs new file mode 100644 index 0000000000..fe7fcc7ec3 --- /dev/null +++ b/codex-rs/codex-backend-openapi-models/src/models/usage_limit_nudge.rs @@ -0,0 +1,28 @@ +/* + * codex-backend + * + * codex-backend + * + * The version of the OpenAPI document: 0.0.1 + * + * Generated by: https://openapi-generator.tech + */ + +use serde::Deserialize; +use serde::Serialize; + +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +pub struct UsageLimitNudge { + #[serde(rename = "threshold")] + pub threshold: u8, + #[serde(rename = "action")] + pub action: UsageLimitNudgeAction, +} + +#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize, Deserialize)] +pub enum UsageLimitNudgeAction { + #[serde(rename = "add_credits")] + AddCredits, + #[serde(rename = "upgrade")] + Upgrade, +} diff --git a/codex-rs/core/src/session/tests.rs b/codex-rs/core/src/session/tests.rs index 30e90bcc93..dbae31043d 100644 --- a/codex-rs/core/src/session/tests.rs +++ b/codex-rs/core/src/session/tests.rs @@ -2392,6 +2392,7 @@ async fn set_rate_limits_retains_previous_credits() { }), plan_type: Some(codex_protocol::account::PlanType::Plus), rate_limit_reached_type: None, + current_usage_limit_nudge: None, }; state.set_rate_limits(initial.clone()); @@ -2411,6 +2412,7 @@ async fn set_rate_limits_retains_previous_credits() { credits: None, plan_type: None, rate_limit_reached_type: None, + current_usage_limit_nudge: None, }; state.set_rate_limits(update.clone()); @@ -2424,6 +2426,7 @@ async fn set_rate_limits_retains_previous_credits() { credits: initial.credits, plan_type: initial.plan_type, rate_limit_reached_type: None, + current_usage_limit_nudge: None, }) ); } @@ -2499,6 +2502,7 @@ async fn set_rate_limits_updates_plan_type_when_present() { }), plan_type: Some(codex_protocol::account::PlanType::Plus), rate_limit_reached_type: None, + current_usage_limit_nudge: None, }; state.set_rate_limits(initial.clone()); @@ -2514,6 +2518,7 @@ async fn set_rate_limits_updates_plan_type_when_present() { credits: None, plan_type: Some(codex_protocol::account::PlanType::Pro), rate_limit_reached_type: None, + current_usage_limit_nudge: None, }; state.set_rate_limits(update.clone()); @@ -2527,6 +2532,7 @@ async fn set_rate_limits_updates_plan_type_when_present() { credits: initial.credits, plan_type: update.plan_type, rate_limit_reached_type: None, + current_usage_limit_nudge: None, }) ); } diff --git a/codex-rs/core/src/state/session_tests.rs b/codex-rs/core/src/state/session_tests.rs index 5e90cc881d..f7c50866f1 100644 --- a/codex-rs/core/src/state/session_tests.rs +++ b/codex-rs/core/src/state/session_tests.rs @@ -50,6 +50,7 @@ async fn set_rate_limits_defaults_limit_id_to_codex_when_missing() { credits: None, plan_type: None, rate_limit_reached_type: None, + current_usage_limit_nudge: None, }); assert_eq!( @@ -78,6 +79,7 @@ async fn set_rate_limits_defaults_to_codex_when_limit_id_missing_after_other_buc credits: None, plan_type: None, rate_limit_reached_type: None, + current_usage_limit_nudge: None, }); state.set_rate_limits(RateLimitSnapshot { limit_id: None, @@ -91,6 +93,7 @@ async fn set_rate_limits_defaults_to_codex_when_limit_id_missing_after_other_buc credits: None, plan_type: None, rate_limit_reached_type: None, + current_usage_limit_nudge: None, }); assert_eq!( @@ -123,6 +126,7 @@ async fn set_rate_limits_carries_credits_and_plan_type_from_codex_to_codex_other }), plan_type: Some(codex_protocol::account::PlanType::Plus), rate_limit_reached_type: None, + current_usage_limit_nudge: None, }); state.set_rate_limits(RateLimitSnapshot { @@ -137,6 +141,7 @@ async fn set_rate_limits_carries_credits_and_plan_type_from_codex_to_codex_other credits: None, plan_type: None, rate_limit_reached_type: None, + current_usage_limit_nudge: None, }); assert_eq!( @@ -157,6 +162,7 @@ async fn set_rate_limits_carries_credits_and_plan_type_from_codex_to_codex_other }), plan_type: Some(codex_protocol::account::PlanType::Plus), rate_limit_reached_type: None, + current_usage_limit_nudge: None, }) ); } diff --git a/codex-rs/memories/write/src/guard_tests.rs b/codex-rs/memories/write/src/guard_tests.rs index 139a8773ca..2cb1a48115 100644 --- a/codex-rs/memories/write/src/guard_tests.rs +++ b/codex-rs/memories/write/src/guard_tests.rs @@ -13,6 +13,7 @@ fn snapshot( credits: None, plan_type: None, rate_limit_reached_type: None, + current_usage_limit_nudge: None, } } diff --git a/codex-rs/protocol/src/error_tests.rs b/codex-rs/protocol/src/error_tests.rs index aef7478607..74fa090178 100644 --- a/codex-rs/protocol/src/error_tests.rs +++ b/codex-rs/protocol/src/error_tests.rs @@ -37,6 +37,7 @@ fn rate_limit_snapshot() -> RateLimitSnapshot { credits: None, plan_type: None, rate_limit_reached_type: None, + current_usage_limit_nudge: None, } } diff --git a/codex-rs/protocol/src/protocol.rs b/codex-rs/protocol/src/protocol.rs index c3e4f5abaa..1ea884dc7a 100644 --- a/codex-rs/protocol/src/protocol.rs +++ b/codex-rs/protocol/src/protocol.rs @@ -2122,6 +2122,9 @@ pub struct RateLimitSnapshot { pub credits: Option, pub plan_type: Option, pub rate_limit_reached_type: Option, + #[serde(default, skip_serializing_if = "Option::is_none")] + #[ts(optional)] + pub current_usage_limit_nudge: Option, } #[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, Serialize, JsonSchema, TS)] @@ -2135,6 +2138,58 @@ pub enum RateLimitReachedType { WorkspaceMemberUsageLimitReached, } +#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize, JsonSchema, TS)] +pub struct UsageLimitNudgePayload { + pub threshold: u8, + pub action: UsageLimitNudgeAction, +} + +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct UsageLimitNudge { + pub threshold: UsageLimitNudgeThreshold, + pub action: UsageLimitNudgeAction, +} + +impl UsageLimitNudge { + pub fn from_payload(payload: &UsageLimitNudgePayload) -> Option { + Some(Self { + threshold: UsageLimitNudgeThreshold::from_percent(payload.threshold)?, + action: payload.action, + }) + } +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum UsageLimitNudgeThreshold { + Percent75, + Percent90, +} + +impl UsageLimitNudgeThreshold { + fn from_percent(percent: u8) -> Option { + match percent { + 75 => Some(Self::Percent75), + 90 => Some(Self::Percent90), + _ => None, + } + } + + pub fn as_percent(self) -> u8 { + match self { + Self::Percent75 => 75, + Self::Percent90 => 90, + } + } +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, Serialize, JsonSchema, TS)] +#[serde(rename_all = "snake_case")] +#[ts(rename_all = "snake_case")] +pub enum UsageLimitNudgeAction { + AddCredits, + Upgrade, +} + #[derive(Debug, Clone, PartialEq, Deserialize, Serialize, JsonSchema, TS)] pub struct RateLimitWindow { /// Percentage (0-100) of the window that has been consumed. @@ -3961,6 +4016,48 @@ mod tests { assert_eq!(HookSource::Unknown.is_managed(), false); } + fn rate_limit_snapshot_with_nudge( + current_usage_limit_nudge: Option, + ) -> RateLimitSnapshot { + RateLimitSnapshot { + limit_id: Some("codex".to_string()), + limit_name: None, + primary: None, + secondary: None, + credits: None, + plan_type: None, + rate_limit_reached_type: None, + current_usage_limit_nudge, + } + } + + #[test] + fn current_usage_limit_nudge_payload_validates_thresholds() { + assert_eq!( + rate_limit_snapshot_with_nudge(Some(UsageLimitNudgePayload { + threshold: 75, + action: UsageLimitNudgeAction::AddCredits, + })) + .current_usage_limit_nudge + .as_ref() + .and_then(UsageLimitNudge::from_payload), + Some(UsageLimitNudge { + threshold: UsageLimitNudgeThreshold::Percent75, + action: UsageLimitNudgeAction::AddCredits, + }) + ); + assert_eq!( + rate_limit_snapshot_with_nudge(Some(UsageLimitNudgePayload { + threshold: 80, + action: UsageLimitNudgeAction::AddCredits, + })) + .current_usage_limit_nudge + .as_ref() + .and_then(UsageLimitNudge::from_payload), + None + ); + } + fn sorted_writable_roots(roots: Vec) -> Vec<(PathBuf, Vec)> { let mut sorted_roots: Vec<(PathBuf, Vec)> = roots .into_iter() diff --git a/codex-rs/tui/src/chatwidget/tests/helpers.rs b/codex-rs/tui/src/chatwidget/tests/helpers.rs index 6920689ef3..7349f446ca 100644 --- a/codex-rs/tui/src/chatwidget/tests/helpers.rs +++ b/codex-rs/tui/src/chatwidget/tests/helpers.rs @@ -114,6 +114,7 @@ pub(super) fn snapshot(percent: f64) -> RateLimitSnapshot { credits: None, plan_type: None, rate_limit_reached_type: None, + current_usage_limit_nudge: None, } } diff --git a/codex-rs/tui/src/chatwidget/tests/status_and_layout.rs b/codex-rs/tui/src/chatwidget/tests/status_and_layout.rs index e91bb28851..2acef983af 100644 --- a/codex-rs/tui/src/chatwidget/tests/status_and_layout.rs +++ b/codex-rs/tui/src/chatwidget/tests/status_and_layout.rs @@ -246,6 +246,7 @@ async fn rate_limit_snapshot_keeps_prior_credits_when_missing_from_headers() { }), plan_type: None, rate_limit_reached_type: None, + current_usage_limit_nudge: None, })); let initial_balance = chat .rate_limit_snapshots_by_limit_id @@ -266,6 +267,7 @@ async fn rate_limit_snapshot_keeps_prior_credits_when_missing_from_headers() { credits: None, plan_type: None, rate_limit_reached_type: None, + current_usage_limit_nudge: None, })); let display = chat @@ -305,6 +307,7 @@ async fn rate_limit_snapshot_updates_and_retains_plan_type() { credits: None, plan_type: Some(PlanType::Plus), rate_limit_reached_type: None, + current_usage_limit_nudge: None, })); assert_eq!(chat.plan_type, Some(PlanType::Plus)); @@ -324,6 +327,7 @@ async fn rate_limit_snapshot_updates_and_retains_plan_type() { credits: None, plan_type: Some(PlanType::Pro), rate_limit_reached_type: None, + current_usage_limit_nudge: None, })); assert_eq!(chat.plan_type, Some(PlanType::Pro)); @@ -343,6 +347,7 @@ async fn rate_limit_snapshot_updates_and_retains_plan_type() { credits: None, plan_type: None, rate_limit_reached_type: None, + current_usage_limit_nudge: None, })); assert_eq!(chat.plan_type, Some(PlanType::Pro)); } @@ -367,6 +372,7 @@ async fn rate_limit_snapshots_keep_separate_entries_per_limit_id() { }), plan_type: Some(PlanType::Pro), rate_limit_reached_type: None, + current_usage_limit_nudge: None, })); chat.on_rate_limit_snapshot(Some(RateLimitSnapshot { @@ -381,6 +387,7 @@ async fn rate_limit_snapshots_keep_separate_entries_per_limit_id() { credits: None, plan_type: Some(PlanType::Pro), rate_limit_reached_type: None, + current_usage_limit_nudge: None, })); let codex = chat @@ -434,6 +441,7 @@ async fn rate_limit_switch_prompt_skips_non_codex_limit() { credits: None, plan_type: None, rate_limit_reached_type: None, + current_usage_limit_nudge: None, })); assert!(matches!( diff --git a/codex-rs/tui/src/status/tests.rs b/codex-rs/tui/src/status/tests.rs index b511957d84..1cced2480b 100644 --- a/codex-rs/tui/src/status/tests.rs +++ b/codex-rs/tui/src/status/tests.rs @@ -226,6 +226,7 @@ async fn status_snapshot_includes_reasoning_details() { credits: None, plan_type: None, rate_limit_reached_type: None, + current_usage_limit_nudge: None, }; let rate_display = rate_limit_snapshot_display(&snapshot, captured_at); @@ -754,6 +755,7 @@ async fn status_snapshot_includes_monthly_limit() { credits: None, plan_type: None, rate_limit_reached_type: None, + current_usage_limit_nudge: None, }; let rate_display = rate_limit_snapshot_display(&snapshot, captured_at); @@ -806,6 +808,7 @@ async fn status_snapshot_shows_unlimited_credits() { }), plan_type: None, rate_limit_reached_type: None, + current_usage_limit_nudge: None, }; let rate_display = rate_limit_snapshot_display(&snapshot, captured_at); let model_slug = crate::legacy_core::test_support::get_model_offline(config.model.as_deref()); @@ -856,6 +859,7 @@ async fn status_snapshot_shows_positive_credits() { }), plan_type: None, rate_limit_reached_type: None, + current_usage_limit_nudge: None, }; let rate_display = rate_limit_snapshot_display(&snapshot, captured_at); let model_slug = crate::legacy_core::test_support::get_model_offline(config.model.as_deref()); @@ -906,6 +910,7 @@ async fn status_snapshot_hides_zero_credits() { }), plan_type: None, rate_limit_reached_type: None, + current_usage_limit_nudge: None, }; let rate_display = rate_limit_snapshot_display(&snapshot, captured_at); let model_slug = crate::legacy_core::test_support::get_model_offline(config.model.as_deref()); @@ -954,6 +959,7 @@ async fn status_snapshot_hides_when_has_no_credits_flag() { }), plan_type: None, rate_limit_reached_type: None, + current_usage_limit_nudge: None, }; let rate_display = rate_limit_snapshot_display(&snapshot, captured_at); let model_slug = crate::legacy_core::test_support::get_model_offline(config.model.as_deref()); @@ -1060,6 +1066,7 @@ async fn status_snapshot_truncates_in_narrow_terminal() { credits: None, plan_type: None, rate_limit_reached_type: None, + current_usage_limit_nudge: None, }; let rate_display = rate_limit_snapshot_display(&snapshot, captured_at); @@ -1225,6 +1232,7 @@ async fn status_snapshot_shows_refreshing_limits_notice() { credits: None, plan_type: None, rate_limit_reached_type: None, + current_usage_limit_nudge: None, }; let rate_display = rate_limit_snapshot_display(&snapshot, captured_at); @@ -1296,6 +1304,7 @@ async fn status_snapshot_includes_credits_and_limits() { }), plan_type: None, rate_limit_reached_type: None, + current_usage_limit_nudge: None, }; let rate_display = rate_limit_snapshot_display(&snapshot, captured_at); @@ -1350,6 +1359,7 @@ async fn status_snapshot_shows_unavailable_limits_message() { credits: None, plan_type: None, rate_limit_reached_type: None, + current_usage_limit_nudge: None, }; let captured_at = chrono::Local .with_ymd_and_hms(2024, 6, 7, 8, 9, 10) @@ -1407,6 +1417,7 @@ async fn status_snapshot_treats_refreshing_empty_limits_as_unavailable() { credits: None, plan_type: None, rate_limit_reached_type: None, + current_usage_limit_nudge: None, }; let captured_at = chrono::Local .with_ymd_and_hms(2024, 6, 7, 8, 9, 10) @@ -1478,6 +1489,7 @@ async fn status_snapshot_shows_stale_limits_message() { credits: None, plan_type: None, rate_limit_reached_type: None, + current_usage_limit_nudge: None, }; let rate_display = rate_limit_snapshot_display(&snapshot, captured_at); let now = captured_at + ChronoDuration::minutes(20); @@ -1549,6 +1561,7 @@ async fn status_snapshot_cached_limits_hide_credits_without_flag() { }), plan_type: None, rate_limit_reached_type: None, + current_usage_limit_nudge: None, }; let rate_display = rate_limit_snapshot_display(&snapshot, captured_at); let now = captured_at + ChronoDuration::minutes(20);