feat: expose spend controls in codex usage snapshots

This commit is contained in:
Edward Frazer
2026-05-15 23:45:09 -07:00
parent 8df2d96860
commit d322741267
24 changed files with 1352 additions and 1 deletions
@@ -1421,6 +1421,25 @@
},
"type": "object"
},
"GroupSpendControlLimitSnapshot": {
"properties": {
"details": {
"$ref": "#/definitions/SpendControlLimitSnapshot"
},
"groupId": {
"type": "string"
},
"groupName": {
"type": "string"
}
},
"required": [
"details",
"groupId",
"groupName"
],
"type": "object"
},
"GuardianApprovalReview": {
"description": "[UNSTABLE] Temporary approval auto-review payload used by `item/autoApprovalReview/*` notifications. This shape is expected to change soon.",
"properties": {
@@ -2598,6 +2617,16 @@
"type": "null"
}
]
},
"spendControl": {
"anyOf": [
{
"$ref": "#/definitions/SpendControlSnapshot"
},
{
"type": "null"
}
]
}
},
"type": "object"
@@ -2854,6 +2883,137 @@
"description": "Notification emitted when watched local skill files change.\n\nTreat this as an invalidation signal and re-run `skills/list` with the client's current parameters when refreshed skill metadata is needed.",
"type": "object"
},
"SpendControlLimitSnapshot": {
"properties": {
"limit": {
"type": "string"
},
"remaining": {
"type": "string"
},
"remainingPercent": {
"format": "int32",
"type": "integer"
},
"resetAfterSeconds": {
"format": "int32",
"type": "integer"
},
"resetAt": {
"format": "int32",
"type": "integer"
},
"used": {
"type": "string"
},
"usedPercent": {
"format": "int32",
"type": "integer"
}
},
"required": [
"limit",
"remaining",
"remainingPercent",
"resetAfterSeconds",
"resetAt",
"used",
"usedPercent"
],
"type": "object"
},
"SpendControlLimitType": {
"enum": [
"individual",
"group_default",
"workspace_default",
"role_budget",
"group_shared",
"workspace_shared"
],
"type": "string"
},
"SpendControlSnapshot": {
"properties": {
"groupDefaultLimit": {
"anyOf": [
{
"$ref": "#/definitions/GroupSpendControlLimitSnapshot"
},
{
"type": "null"
}
]
},
"groupSharedLimit": {
"anyOf": [
{
"$ref": "#/definitions/GroupSpendControlLimitSnapshot"
},
{
"type": "null"
}
]
},
"individualLimit": {
"anyOf": [
{
"$ref": "#/definitions/SpendControlLimitSnapshot"
},
{
"type": "null"
}
]
},
"reached": {
"type": "boolean"
},
"reachedLimitType": {
"anyOf": [
{
"$ref": "#/definitions/SpendControlLimitType"
},
{
"type": "null"
}
]
},
"roleBudgetLimit": {
"anyOf": [
{
"$ref": "#/definitions/SpendControlLimitSnapshot"
},
{
"type": "null"
}
]
},
"workspaceDefaultLimit": {
"anyOf": [
{
"$ref": "#/definitions/SpendControlLimitSnapshot"
},
{
"type": "null"
}
]
},
"workspaceSharedLimit": {
"anyOf": [
{
"$ref": "#/definitions/SpendControlLimitSnapshot"
},
{
"type": "null"
}
]
}
},
"required": [
"reached"
],
"type": "object"
},
"SubAgentSource": {
"oneOf": [
{
@@ -9226,6 +9226,25 @@
},
"type": "object"
},
"GroupSpendControlLimitSnapshot": {
"properties": {
"details": {
"$ref": "#/definitions/v2/SpendControlLimitSnapshot"
},
"groupId": {
"type": "string"
},
"groupName": {
"type": "string"
}
},
"required": [
"details",
"groupId",
"groupName"
],
"type": "object"
},
"GuardianApprovalReview": {
"description": "[UNSTABLE] Temporary approval auto-review payload used by `item/autoApprovalReview/*` notifications. This shape is expected to change soon.",
"properties": {
@@ -13028,6 +13047,16 @@
"type": "null"
}
]
},
"spendControl": {
"anyOf": [
{
"$ref": "#/definitions/v2/SpendControlSnapshot"
},
{
"type": "null"
}
]
}
},
"type": "object"
@@ -14971,6 +15000,137 @@
],
"type": "string"
},
"SpendControlLimitSnapshot": {
"properties": {
"limit": {
"type": "string"
},
"remaining": {
"type": "string"
},
"remainingPercent": {
"format": "int32",
"type": "integer"
},
"resetAfterSeconds": {
"format": "int32",
"type": "integer"
},
"resetAt": {
"format": "int32",
"type": "integer"
},
"used": {
"type": "string"
},
"usedPercent": {
"format": "int32",
"type": "integer"
}
},
"required": [
"limit",
"remaining",
"remainingPercent",
"resetAfterSeconds",
"resetAt",
"used",
"usedPercent"
],
"type": "object"
},
"SpendControlLimitType": {
"enum": [
"individual",
"group_default",
"workspace_default",
"role_budget",
"group_shared",
"workspace_shared"
],
"type": "string"
},
"SpendControlSnapshot": {
"properties": {
"groupDefaultLimit": {
"anyOf": [
{
"$ref": "#/definitions/v2/GroupSpendControlLimitSnapshot"
},
{
"type": "null"
}
]
},
"groupSharedLimit": {
"anyOf": [
{
"$ref": "#/definitions/v2/GroupSpendControlLimitSnapshot"
},
{
"type": "null"
}
]
},
"individualLimit": {
"anyOf": [
{
"$ref": "#/definitions/v2/SpendControlLimitSnapshot"
},
{
"type": "null"
}
]
},
"reached": {
"type": "boolean"
},
"reachedLimitType": {
"anyOf": [
{
"$ref": "#/definitions/v2/SpendControlLimitType"
},
{
"type": "null"
}
]
},
"roleBudgetLimit": {
"anyOf": [
{
"$ref": "#/definitions/v2/SpendControlLimitSnapshot"
},
{
"type": "null"
}
]
},
"workspaceDefaultLimit": {
"anyOf": [
{
"$ref": "#/definitions/v2/SpendControlLimitSnapshot"
},
{
"type": "null"
}
]
},
"workspaceSharedLimit": {
"anyOf": [
{
"$ref": "#/definitions/v2/SpendControlLimitSnapshot"
},
{
"type": "null"
}
]
}
},
"required": [
"reached"
],
"type": "object"
},
"SubAgentSource": {
"oneOf": [
{
@@ -5726,6 +5726,25 @@
},
"type": "object"
},
"GroupSpendControlLimitSnapshot": {
"properties": {
"details": {
"$ref": "#/definitions/SpendControlLimitSnapshot"
},
"groupId": {
"type": "string"
},
"groupName": {
"type": "string"
}
},
"required": [
"details",
"groupId",
"groupName"
],
"type": "object"
},
"GuardianApprovalReview": {
"description": "[UNSTABLE] Temporary approval auto-review payload used by `item/autoApprovalReview/*` notifications. This shape is expected to change soon.",
"properties": {
@@ -9577,6 +9596,16 @@
"type": "null"
}
]
},
"spendControl": {
"anyOf": [
{
"$ref": "#/definitions/SpendControlSnapshot"
},
{
"type": "null"
}
]
}
},
"type": "object"
@@ -12795,6 +12824,137 @@
],
"type": "string"
},
"SpendControlLimitSnapshot": {
"properties": {
"limit": {
"type": "string"
},
"remaining": {
"type": "string"
},
"remainingPercent": {
"format": "int32",
"type": "integer"
},
"resetAfterSeconds": {
"format": "int32",
"type": "integer"
},
"resetAt": {
"format": "int32",
"type": "integer"
},
"used": {
"type": "string"
},
"usedPercent": {
"format": "int32",
"type": "integer"
}
},
"required": [
"limit",
"remaining",
"remainingPercent",
"resetAfterSeconds",
"resetAt",
"used",
"usedPercent"
],
"type": "object"
},
"SpendControlLimitType": {
"enum": [
"individual",
"group_default",
"workspace_default",
"role_budget",
"group_shared",
"workspace_shared"
],
"type": "string"
},
"SpendControlSnapshot": {
"properties": {
"groupDefaultLimit": {
"anyOf": [
{
"$ref": "#/definitions/GroupSpendControlLimitSnapshot"
},
{
"type": "null"
}
]
},
"groupSharedLimit": {
"anyOf": [
{
"$ref": "#/definitions/GroupSpendControlLimitSnapshot"
},
{
"type": "null"
}
]
},
"individualLimit": {
"anyOf": [
{
"$ref": "#/definitions/SpendControlLimitSnapshot"
},
{
"type": "null"
}
]
},
"reached": {
"type": "boolean"
},
"reachedLimitType": {
"anyOf": [
{
"$ref": "#/definitions/SpendControlLimitType"
},
{
"type": "null"
}
]
},
"roleBudgetLimit": {
"anyOf": [
{
"$ref": "#/definitions/SpendControlLimitSnapshot"
},
{
"type": "null"
}
]
},
"workspaceDefaultLimit": {
"anyOf": [
{
"$ref": "#/definitions/SpendControlLimitSnapshot"
},
{
"type": "null"
}
]
},
"workspaceSharedLimit": {
"anyOf": [
{
"$ref": "#/definitions/SpendControlLimitSnapshot"
},
{
"type": "null"
}
]
}
},
"required": [
"reached"
],
"type": "object"
},
"SubAgentSource": {
"oneOf": [
{
@@ -22,6 +22,25 @@
],
"type": "object"
},
"GroupSpendControlLimitSnapshot": {
"properties": {
"details": {
"$ref": "#/definitions/SpendControlLimitSnapshot"
},
"groupId": {
"type": "string"
},
"groupName": {
"type": "string"
}
},
"required": [
"details",
"groupId",
"groupName"
],
"type": "object"
},
"PlanType": {
"enum": [
"free",
@@ -112,6 +131,16 @@
"type": "null"
}
]
},
"spendControl": {
"anyOf": [
{
"$ref": "#/definitions/SpendControlSnapshot"
},
{
"type": "null"
}
]
}
},
"type": "object"
@@ -141,6 +170,137 @@
"usedPercent"
],
"type": "object"
},
"SpendControlLimitSnapshot": {
"properties": {
"limit": {
"type": "string"
},
"remaining": {
"type": "string"
},
"remainingPercent": {
"format": "int32",
"type": "integer"
},
"resetAfterSeconds": {
"format": "int32",
"type": "integer"
},
"resetAt": {
"format": "int32",
"type": "integer"
},
"used": {
"type": "string"
},
"usedPercent": {
"format": "int32",
"type": "integer"
}
},
"required": [
"limit",
"remaining",
"remainingPercent",
"resetAfterSeconds",
"resetAt",
"used",
"usedPercent"
],
"type": "object"
},
"SpendControlLimitType": {
"enum": [
"individual",
"group_default",
"workspace_default",
"role_budget",
"group_shared",
"workspace_shared"
],
"type": "string"
},
"SpendControlSnapshot": {
"properties": {
"groupDefaultLimit": {
"anyOf": [
{
"$ref": "#/definitions/GroupSpendControlLimitSnapshot"
},
{
"type": "null"
}
]
},
"groupSharedLimit": {
"anyOf": [
{
"$ref": "#/definitions/GroupSpendControlLimitSnapshot"
},
{
"type": "null"
}
]
},
"individualLimit": {
"anyOf": [
{
"$ref": "#/definitions/SpendControlLimitSnapshot"
},
{
"type": "null"
}
]
},
"reached": {
"type": "boolean"
},
"reachedLimitType": {
"anyOf": [
{
"$ref": "#/definitions/SpendControlLimitType"
},
{
"type": "null"
}
]
},
"roleBudgetLimit": {
"anyOf": [
{
"$ref": "#/definitions/SpendControlLimitSnapshot"
},
{
"type": "null"
}
]
},
"workspaceDefaultLimit": {
"anyOf": [
{
"$ref": "#/definitions/SpendControlLimitSnapshot"
},
{
"type": "null"
}
]
},
"workspaceSharedLimit": {
"anyOf": [
{
"$ref": "#/definitions/SpendControlLimitSnapshot"
},
{
"type": "null"
}
]
}
},
"required": [
"reached"
],
"type": "object"
}
},
"properties": {
@@ -22,6 +22,25 @@
],
"type": "object"
},
"GroupSpendControlLimitSnapshot": {
"properties": {
"details": {
"$ref": "#/definitions/SpendControlLimitSnapshot"
},
"groupId": {
"type": "string"
},
"groupName": {
"type": "string"
}
},
"required": [
"details",
"groupId",
"groupName"
],
"type": "object"
},
"PlanType": {
"enum": [
"free",
@@ -112,6 +131,16 @@
"type": "null"
}
]
},
"spendControl": {
"anyOf": [
{
"$ref": "#/definitions/SpendControlSnapshot"
},
{
"type": "null"
}
]
}
},
"type": "object"
@@ -141,6 +170,137 @@
"usedPercent"
],
"type": "object"
},
"SpendControlLimitSnapshot": {
"properties": {
"limit": {
"type": "string"
},
"remaining": {
"type": "string"
},
"remainingPercent": {
"format": "int32",
"type": "integer"
},
"resetAfterSeconds": {
"format": "int32",
"type": "integer"
},
"resetAt": {
"format": "int32",
"type": "integer"
},
"used": {
"type": "string"
},
"usedPercent": {
"format": "int32",
"type": "integer"
}
},
"required": [
"limit",
"remaining",
"remainingPercent",
"resetAfterSeconds",
"resetAt",
"used",
"usedPercent"
],
"type": "object"
},
"SpendControlLimitType": {
"enum": [
"individual",
"group_default",
"workspace_default",
"role_budget",
"group_shared",
"workspace_shared"
],
"type": "string"
},
"SpendControlSnapshot": {
"properties": {
"groupDefaultLimit": {
"anyOf": [
{
"$ref": "#/definitions/GroupSpendControlLimitSnapshot"
},
{
"type": "null"
}
]
},
"groupSharedLimit": {
"anyOf": [
{
"$ref": "#/definitions/GroupSpendControlLimitSnapshot"
},
{
"type": "null"
}
]
},
"individualLimit": {
"anyOf": [
{
"$ref": "#/definitions/SpendControlLimitSnapshot"
},
{
"type": "null"
}
]
},
"reached": {
"type": "boolean"
},
"reachedLimitType": {
"anyOf": [
{
"$ref": "#/definitions/SpendControlLimitType"
},
{
"type": "null"
}
]
},
"roleBudgetLimit": {
"anyOf": [
{
"$ref": "#/definitions/SpendControlLimitSnapshot"
},
{
"type": "null"
}
]
},
"workspaceDefaultLimit": {
"anyOf": [
{
"$ref": "#/definitions/SpendControlLimitSnapshot"
},
{
"type": "null"
}
]
},
"workspaceSharedLimit": {
"anyOf": [
{
"$ref": "#/definitions/SpendControlLimitSnapshot"
},
{
"type": "null"
}
]
}
},
"required": [
"reached"
],
"type": "object"
}
},
"properties": {
@@ -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 { SpendControlLimitSnapshot } from "./SpendControlLimitSnapshot";
export type GroupSpendControlLimitSnapshot = { groupId: string, groupName: string, details: SpendControlLimitSnapshot, };
@@ -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 { SpendControlSnapshot } from "./SpendControlSnapshot";
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, spendControl: SpendControlSnapshot | null, planType: PlanType | null, rateLimitReachedType: RateLimitReachedType | null, };
@@ -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 SpendControlLimitSnapshot = { limit: string, used: string, remaining: string, usedPercent: number, remainingPercent: number, resetAfterSeconds: number, resetAt: number, };
@@ -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 SpendControlLimitType = "individual" | "group_default" | "workspace_default" | "role_budget" | "group_shared" | "workspace_shared";
@@ -0,0 +1,8 @@
// 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 { GroupSpendControlLimitSnapshot } from "./GroupSpendControlLimitSnapshot";
import type { SpendControlLimitSnapshot } from "./SpendControlLimitSnapshot";
import type { SpendControlLimitType } from "./SpendControlLimitType";
export type SpendControlSnapshot = { reached: boolean, reachedLimitType: SpendControlLimitType | null, individualLimit: SpendControlLimitSnapshot | null, groupDefaultLimit: GroupSpendControlLimitSnapshot | null, workspaceDefaultLimit: SpendControlLimitSnapshot | null, roleBudgetLimit: SpendControlLimitSnapshot | null, groupSharedLimit: GroupSpendControlLimitSnapshot | null, workspaceSharedLimit: SpendControlLimitSnapshot | null, };
@@ -138,6 +138,7 @@ export type { GetAccountRateLimitsResponse } from "./GetAccountRateLimitsRespons
export type { GetAccountResponse } from "./GetAccountResponse";
export type { GitInfo } from "./GitInfo";
export type { GrantedPermissionProfile } from "./GrantedPermissionProfile";
export type { GroupSpendControlLimitSnapshot } from "./GroupSpendControlLimitSnapshot";
export type { GuardianApprovalReview } from "./GuardianApprovalReview";
export type { GuardianApprovalReviewAction } from "./GuardianApprovalReviewAction";
export type { GuardianApprovalReviewStatus } from "./GuardianApprovalReviewStatus";
@@ -344,6 +345,9 @@ export type { SkillsListEntry } from "./SkillsListEntry";
export type { SkillsListParams } from "./SkillsListParams";
export type { SkillsListResponse } from "./SkillsListResponse";
export type { SortDirection } from "./SortDirection";
export type { SpendControlLimitSnapshot } from "./SpendControlLimitSnapshot";
export type { SpendControlLimitType } from "./SpendControlLimitType";
export type { SpendControlSnapshot } from "./SpendControlSnapshot";
export type { SubagentMigration } from "./SubagentMigration";
export type { TerminalInteractionNotification } from "./TerminalInteractionNotification";
export type { TextElement } from "./TextElement";
@@ -3,9 +3,13 @@ use codex_experimental_api_macros::ExperimentalApi;
use codex_protocol::account::PlanType;
use codex_protocol::account::ProviderAccount;
use codex_protocol::protocol::CreditsSnapshot as CoreCreditsSnapshot;
use codex_protocol::protocol::GroupSpendControlLimitSnapshot as CoreGroupSpendControlLimitSnapshot;
use codex_protocol::protocol::RateLimitReachedType as CoreRateLimitReachedType;
use codex_protocol::protocol::RateLimitSnapshot as CoreRateLimitSnapshot;
use codex_protocol::protocol::RateLimitWindow as CoreRateLimitWindow;
use codex_protocol::protocol::SpendControlLimitSnapshot as CoreSpendControlLimitSnapshot;
use codex_protocol::protocol::SpendControlLimitType as CoreSpendControlLimitType;
use codex_protocol::protocol::SpendControlSnapshot as CoreSpendControlSnapshot;
use schemars::JsonSchema;
use serde::Deserialize;
use serde::Serialize;
@@ -260,6 +264,7 @@ pub struct RateLimitSnapshot {
pub primary: Option<RateLimitWindow>,
pub secondary: Option<RateLimitWindow>,
pub credits: Option<CreditsSnapshot>,
pub spend_control: Option<SpendControlSnapshot>,
pub plan_type: Option<PlanType>,
pub rate_limit_reached_type: Option<RateLimitReachedType>,
}
@@ -272,6 +277,7 @@ impl From<CoreRateLimitSnapshot> for RateLimitSnapshot {
primary: value.primary.map(RateLimitWindow::from),
secondary: value.secondary.map(RateLimitWindow::from),
credits: value.credits.map(CreditsSnapshot::from),
spend_control: value.spend_control.map(SpendControlSnapshot::from),
plan_type: value.plan_type,
rate_limit_reached_type: value
.rate_limit_reached_type
@@ -371,6 +377,114 @@ impl From<CoreCreditsSnapshot> for CreditsSnapshot {
}
}
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)]
#[serde(rename_all = "camelCase")]
#[ts(export_to = "v2/")]
pub struct SpendControlSnapshot {
pub reached: bool,
pub reached_limit_type: Option<SpendControlLimitType>,
pub individual_limit: Option<SpendControlLimitSnapshot>,
pub group_default_limit: Option<GroupSpendControlLimitSnapshot>,
pub workspace_default_limit: Option<SpendControlLimitSnapshot>,
pub role_budget_limit: Option<SpendControlLimitSnapshot>,
pub group_shared_limit: Option<GroupSpendControlLimitSnapshot>,
pub workspace_shared_limit: Option<SpendControlLimitSnapshot>,
}
impl From<CoreSpendControlSnapshot> for SpendControlSnapshot {
fn from(value: CoreSpendControlSnapshot) -> Self {
Self {
reached: value.reached,
reached_limit_type: value.reached_limit_type.map(SpendControlLimitType::from),
individual_limit: value.individual_limit.map(SpendControlLimitSnapshot::from),
group_default_limit: value
.group_default_limit
.map(GroupSpendControlLimitSnapshot::from),
workspace_default_limit: value
.workspace_default_limit
.map(SpendControlLimitSnapshot::from),
role_budget_limit: value.role_budget_limit.map(SpendControlLimitSnapshot::from),
group_shared_limit: value
.group_shared_limit
.map(GroupSpendControlLimitSnapshot::from),
workspace_shared_limit: value
.workspace_shared_limit
.map(SpendControlLimitSnapshot::from),
}
}
}
#[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 SpendControlLimitType {
Individual,
GroupDefault,
WorkspaceDefault,
RoleBudget,
GroupShared,
WorkspaceShared,
}
impl From<CoreSpendControlLimitType> for SpendControlLimitType {
fn from(value: CoreSpendControlLimitType) -> Self {
match value {
CoreSpendControlLimitType::Individual => Self::Individual,
CoreSpendControlLimitType::GroupDefault => Self::GroupDefault,
CoreSpendControlLimitType::WorkspaceDefault => Self::WorkspaceDefault,
CoreSpendControlLimitType::RoleBudget => Self::RoleBudget,
CoreSpendControlLimitType::GroupShared => Self::GroupShared,
CoreSpendControlLimitType::WorkspaceShared => Self::WorkspaceShared,
}
}
}
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)]
#[serde(rename_all = "camelCase")]
#[ts(export_to = "v2/")]
pub struct SpendControlLimitSnapshot {
pub limit: String,
pub used: String,
pub remaining: String,
pub used_percent: i32,
pub remaining_percent: i32,
pub reset_after_seconds: i32,
pub reset_at: i32,
}
impl From<CoreSpendControlLimitSnapshot> for SpendControlLimitSnapshot {
fn from(value: CoreSpendControlLimitSnapshot) -> Self {
Self {
limit: value.limit,
used: value.used,
remaining: value.remaining,
used_percent: value.used_percent,
remaining_percent: value.remaining_percent,
reset_after_seconds: value.reset_after_seconds,
reset_at: value.reset_at,
}
}
}
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)]
#[serde(rename_all = "camelCase")]
#[ts(export_to = "v2/")]
pub struct GroupSpendControlLimitSnapshot {
pub group_id: String,
pub group_name: String,
pub details: SpendControlLimitSnapshot,
}
impl From<CoreGroupSpendControlLimitSnapshot> for GroupSpendControlLimitSnapshot {
fn from(value: CoreGroupSpendControlLimitSnapshot) -> Self {
Self {
group_id: value.group_id,
group_name: value.group_name,
details: SpendControlLimitSnapshot::from(value.details),
}
}
}
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)]
#[serde(rename_all = "camelCase")]
#[ts(export_to = "v2/")]
@@ -788,6 +788,7 @@ mod tests {
}),
secondary: None,
credits: None,
spend_control: None,
plan_type: Some(PlanType::Plus),
rate_limit_reached_type: None,
},
@@ -181,6 +181,7 @@ async fn get_account_rate_limits_returns_snapshot() -> Result<()> {
resets_at: Some(secondary_reset_timestamp),
}),
credits: None,
spend_control: None,
plan_type: Some(AccountPlanType::Pro),
rate_limit_reached_type: Some(RateLimitReachedType::WorkspaceMemberUsageLimitReached),
},
@@ -202,6 +203,7 @@ async fn get_account_rate_limits_returns_snapshot() -> Result<()> {
resets_at: Some(secondary_reset_timestamp),
}),
credits: None,
spend_control: None,
plan_type: Some(AccountPlanType::Pro),
rate_limit_reached_type: Some(
RateLimitReachedType::WorkspaceMemberUsageLimitReached,
@@ -220,6 +222,7 @@ async fn get_account_rate_limits_returns_snapshot() -> Result<()> {
}),
secondary: None,
credits: None,
spend_control: None,
plan_type: Some(AccountPlanType::Pro),
rate_limit_reached_type: None,
},
+148
View File
@@ -12,9 +12,13 @@ use codex_login::CodexAuth;
use codex_login::default_client::get_codex_user_agent;
use codex_protocol::account::PlanType as AccountPlanType;
use codex_protocol::protocol::CreditsSnapshot;
use codex_protocol::protocol::GroupSpendControlLimitSnapshot;
use codex_protocol::protocol::RateLimitReachedType;
use codex_protocol::protocol::RateLimitSnapshot;
use codex_protocol::protocol::RateLimitWindow;
use codex_protocol::protocol::SpendControlLimitSnapshot;
use codex_protocol::protocol::SpendControlLimitType;
use codex_protocol::protocol::SpendControlSnapshot;
use reqwest::StatusCode;
use reqwest::header::CONTENT_TYPE;
use reqwest::header::HeaderMap;
@@ -457,6 +461,7 @@ impl Client {
/*limit_name*/ None,
payload.rate_limit.flatten().map(|details| *details),
payload.credits.flatten().map(|details| *details),
payload.spend_control.flatten().map(|details| *details),
plan_type,
rate_limit_reached_type,
)];
@@ -467,6 +472,7 @@ impl Client {
Some(details.limit_name),
details.rate_limit.flatten().map(|rate_limit| *rate_limit),
/*credits*/ None,
/*spend_control*/ None,
plan_type,
/*rate_limit_reached_type*/ None,
)
@@ -480,6 +486,7 @@ impl Client {
limit_name: Option<String>,
rate_limit: Option<crate::types::RateLimitStatusDetails>,
credits: Option<crate::types::CreditStatusDetails>,
spend_control: Option<crate::types::SpendControlStatusDetails>,
plan_type: Option<AccountPlanType>,
rate_limit_reached_type: Option<RateLimitReachedType>,
) -> RateLimitSnapshot {
@@ -496,6 +503,7 @@ impl Client {
primary,
secondary,
credits: Self::map_credits(credits),
spend_control: Self::map_spend_control(spend_control),
plan_type,
rate_limit_reached_type,
}
@@ -564,6 +572,87 @@ impl Client {
})
}
fn map_spend_control(
spend_control: Option<crate::types::SpendControlStatusDetails>,
) -> Option<SpendControlSnapshot> {
let details = spend_control?;
Some(SpendControlSnapshot {
reached: details.reached,
reached_limit_type: details
.reached_limit_type
.flatten()
.map(Self::map_spend_control_limit_type),
individual_limit: details
.individual_limit
.flatten()
.map(|limit| Self::map_spend_control_limit(*limit)),
group_default_limit: details
.group_default_limit
.flatten()
.map(|limit| Self::map_group_spend_control_limit(*limit)),
workspace_default_limit: details
.workspace_default_limit
.flatten()
.map(|limit| Self::map_spend_control_limit(*limit)),
role_budget_limit: details
.role_budget_limit
.flatten()
.map(|limit| Self::map_spend_control_limit(*limit)),
group_shared_limit: details
.group_shared_limit
.flatten()
.map(|limit| Self::map_group_spend_control_limit(*limit)),
workspace_shared_limit: details
.workspace_shared_limit
.flatten()
.map(|limit| Self::map_spend_control_limit(*limit)),
})
}
fn map_group_spend_control_limit(
limit: crate::types::GroupSpendControlLimitDetails,
) -> GroupSpendControlLimitSnapshot {
GroupSpendControlLimitSnapshot {
group_id: limit.group_id,
group_name: limit.group_name,
details: Self::map_spend_control_limit(*limit.details),
}
}
fn map_spend_control_limit(
limit: crate::types::SpendControlLimitDetails,
) -> SpendControlLimitSnapshot {
SpendControlLimitSnapshot {
limit: limit.limit,
used: limit.used,
remaining: limit.remaining,
used_percent: limit.used_percent,
remaining_percent: limit.remaining_percent,
reset_after_seconds: limit.reset_after_seconds,
reset_at: limit.reset_at,
}
}
fn map_spend_control_limit_type(
limit_type: crate::types::SpendControlLimitType,
) -> SpendControlLimitType {
match limit_type {
crate::types::SpendControlLimitType::Individual => SpendControlLimitType::Individual,
crate::types::SpendControlLimitType::GroupDefault => {
SpendControlLimitType::GroupDefault
}
crate::types::SpendControlLimitType::WorkspaceDefault => {
SpendControlLimitType::WorkspaceDefault
}
crate::types::SpendControlLimitType::RoleBudget => SpendControlLimitType::RoleBudget,
crate::types::SpendControlLimitType::GroupShared => SpendControlLimitType::GroupShared,
crate::types::SpendControlLimitType::WorkspaceShared => {
SpendControlLimitType::WorkspaceShared
}
}
}
fn map_plan_type(plan_type: crate::types::PlanType) -> AccountPlanType {
match plan_type {
crate::types::PlanType::Free => AccountPlanType::Free,
@@ -658,6 +747,7 @@ mod tests {
balance: Some(Some("9.99".to_string())),
..Default::default()
}))),
spend_control: None,
rate_limit_reached_type: Some(Some(BackendRateLimitReachedType {
kind: RateLimitReachedKind::WorkspaceMemberCreditsDepleted,
})),
@@ -712,6 +802,7 @@ mod tests {
rate_limit: None,
}])),
credits: None,
spend_control: None,
rate_limit_reached_type: None,
};
@@ -724,6 +815,59 @@ mod tests {
assert_eq!(snapshots[1].limit_name.as_deref(), Some("codex_other"));
}
#[test]
fn usage_payload_maps_spend_controls_v2_details() {
let payload = RateLimitStatusPayload {
plan_type: crate::types::PlanType::Enterprise,
rate_limit: None,
additional_rate_limits: None,
credits: None,
spend_control: Some(Some(Box::new(crate::types::SpendControlStatusDetails {
reached: true,
reached_limit_type: Some(Some(crate::types::SpendControlLimitType::GroupShared)),
individual_limit: None,
group_default_limit: None,
workspace_default_limit: None,
role_budget_limit: None,
group_shared_limit: Some(Some(Box::new(
crate::types::GroupSpendControlLimitDetails {
group_id: "group-1".to_string(),
group_name: "Engineering".to_string(),
details: Box::new(crate::types::SpendControlLimitDetails {
limit: "100".to_string(),
used: "82".to_string(),
remaining: "18".to_string(),
used_percent: 82,
remaining_percent: 18,
reset_after_seconds: 60,
reset_at: 120,
}),
},
))),
workspace_shared_limit: None,
}))),
rate_limit_reached_type: None,
};
let snapshots = Client::rate_limit_snapshots_from_payload(payload);
let spend_control = snapshots[0]
.spend_control
.as_ref()
.expect("spend control should be mapped");
assert!(spend_control.reached);
assert_eq!(
spend_control.reached_limit_type,
Some(SpendControlLimitType::GroupShared)
);
let group_shared_limit = spend_control
.group_shared_limit
.as_ref()
.expect("group shared limit should be mapped");
assert_eq!(group_shared_limit.group_id, "group-1");
assert_eq!(group_shared_limit.group_name, "Engineering");
assert_eq!(group_shared_limit.details.remaining_percent, 18);
}
#[test]
fn preferred_snapshot_selection_matches_get_rate_limits_behavior() {
let snapshots = [
@@ -737,6 +881,7 @@ mod tests {
}),
secondary: None,
credits: None,
spend_control: None,
plan_type: Some(AccountPlanType::Pro),
rate_limit_reached_type: None,
},
@@ -750,6 +895,7 @@ mod tests {
}),
secondary: None,
credits: None,
spend_control: None,
plan_type: Some(AccountPlanType::Pro),
rate_limit_reached_type: None,
},
@@ -795,6 +941,7 @@ mod tests {
rate_limit: None,
credits: None,
additional_rate_limits: None,
spend_control: None,
rate_limit_reached_type: Some(Some(BackendRateLimitReachedType { kind })),
};
@@ -810,6 +957,7 @@ mod tests {
rate_limit: None,
credits: None,
additional_rate_limits: None,
spend_control: None,
rate_limit_reached_type: None,
};
+4
View File
@@ -1,11 +1,15 @@
pub use codex_backend_openapi_models::models::ConfigFileResponse;
pub use codex_backend_openapi_models::models::CreditStatusDetails;
pub use codex_backend_openapi_models::models::GroupSpendControlLimitDetails;
pub use codex_backend_openapi_models::models::PaginatedListTaskListItem;
pub use codex_backend_openapi_models::models::PlanType;
pub use codex_backend_openapi_models::models::RateLimitReachedKind;
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::SpendControlLimitDetails;
pub use codex_backend_openapi_models::models::SpendControlLimitType;
pub use codex_backend_openapi_models::models::SpendControlStatusDetails;
pub use codex_backend_openapi_models::models::TaskListItem;
use serde::Deserialize;
+2
View File
@@ -92,6 +92,7 @@ pub fn parse_rate_limit_for_limit(
primary,
secondary,
credits,
spend_control: None,
plan_type: None,
rate_limit_reached_type: None,
})
@@ -156,6 +157,7 @@ pub fn parse_rate_limit_event(payload: &str) -> Option<RateLimitSnapshot> {
primary,
secondary,
credits,
spend_control: None,
plan_type: event.plan_type,
rate_limit_reached_type: None,
})
@@ -0,0 +1,37 @@
/*
* codex-backend
*
* codex-backend
*
* The version of the OpenAPI document: 0.0.1
*
* Generated by: https://openapi-generator.tech
*/
use crate::models;
use serde::Deserialize;
use serde::Serialize;
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct GroupSpendControlLimitDetails {
#[serde(rename = "group_id")]
pub group_id: String,
#[serde(rename = "group_name")]
pub group_name: String,
#[serde(rename = "details")]
pub details: Box<models::SpendControlLimitDetails>,
}
impl GroupSpendControlLimitDetails {
pub fn new(
group_id: String,
group_name: String,
details: models::SpendControlLimitDetails,
) -> Self {
Self {
group_id,
group_name,
details: Box::new(details),
}
}
}
@@ -44,3 +44,13 @@ pub use self::rate_limit_window_snapshot::RateLimitWindowSnapshot;
pub(crate) mod credit_status_details;
pub use self::credit_status_details::CreditStatusDetails;
pub(crate) mod spend_control_limit_details;
pub use self::spend_control_limit_details::SpendControlLimitDetails;
pub(crate) mod group_spend_control_limit_details;
pub use self::group_spend_control_limit_details::GroupSpendControlLimitDetails;
pub(crate) mod spend_control_status_details;
pub use self::spend_control_status_details::SpendControlLimitType;
pub use self::spend_control_status_details::SpendControlStatusDetails;
@@ -37,6 +37,13 @@ pub struct RateLimitStatusPayload {
skip_serializing_if = "Option::is_none"
)]
pub additional_rate_limits: Option<Option<Vec<models::AdditionalRateLimitDetails>>>,
#[serde(
rename = "spend_control",
default,
with = "::serde_with::rust::double_option",
skip_serializing_if = "Option::is_none"
)]
pub spend_control: Option<Option<Box<models::SpendControlStatusDetails>>>,
#[serde(
rename = "rate_limit_reached_type",
default,
@@ -53,6 +60,7 @@ impl RateLimitStatusPayload {
rate_limit: None,
credits: None,
additional_rate_limits: None,
spend_control: None,
rate_limit_reached_type: None,
}
}
@@ -0,0 +1,52 @@
/*
* 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, Default, Debug, PartialEq, Serialize, Deserialize)]
pub struct SpendControlLimitDetails {
#[serde(rename = "limit")]
pub limit: String,
#[serde(rename = "used")]
pub used: String,
#[serde(rename = "remaining")]
pub remaining: String,
#[serde(rename = "used_percent")]
pub used_percent: i32,
#[serde(rename = "remaining_percent")]
pub remaining_percent: i32,
#[serde(rename = "reset_after_seconds")]
pub reset_after_seconds: i32,
#[serde(rename = "reset_at")]
pub reset_at: i32,
}
impl SpendControlLimitDetails {
pub fn new(
limit: String,
used: String,
remaining: String,
used_percent: i32,
remaining_percent: i32,
reset_after_seconds: i32,
reset_at: i32,
) -> Self {
Self {
limit,
used,
remaining,
used_percent,
remaining_percent,
reset_after_seconds,
reset_at,
}
}
}
@@ -0,0 +1,99 @@
/*
* codex-backend
*
* codex-backend
*
* The version of the OpenAPI document: 0.0.1
*
* Generated by: https://openapi-generator.tech
*/
use crate::models;
use serde::Deserialize;
use serde::Serialize;
#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize, Deserialize)]
pub enum SpendControlLimitType {
#[serde(rename = "individual")]
Individual,
#[serde(rename = "group_default")]
GroupDefault,
#[serde(rename = "workspace_default")]
WorkspaceDefault,
#[serde(rename = "role_budget")]
RoleBudget,
#[serde(rename = "group_shared")]
GroupShared,
#[serde(rename = "workspace_shared")]
WorkspaceShared,
}
#[derive(Clone, Default, Debug, PartialEq, Serialize, Deserialize)]
pub struct SpendControlStatusDetails {
#[serde(rename = "reached")]
pub reached: bool,
#[serde(
rename = "reached_limit_type",
default,
with = "::serde_with::rust::double_option",
skip_serializing_if = "Option::is_none"
)]
pub reached_limit_type: Option<Option<SpendControlLimitType>>,
#[serde(
rename = "individual_limit",
default,
with = "::serde_with::rust::double_option",
skip_serializing_if = "Option::is_none"
)]
pub individual_limit: Option<Option<Box<models::SpendControlLimitDetails>>>,
#[serde(
rename = "group_default_limit",
default,
with = "::serde_with::rust::double_option",
skip_serializing_if = "Option::is_none"
)]
pub group_default_limit: Option<Option<Box<models::GroupSpendControlLimitDetails>>>,
#[serde(
rename = "workspace_default_limit",
default,
with = "::serde_with::rust::double_option",
skip_serializing_if = "Option::is_none"
)]
pub workspace_default_limit: Option<Option<Box<models::SpendControlLimitDetails>>>,
#[serde(
rename = "role_budget_limit",
default,
with = "::serde_with::rust::double_option",
skip_serializing_if = "Option::is_none"
)]
pub role_budget_limit: Option<Option<Box<models::SpendControlLimitDetails>>>,
#[serde(
rename = "group_shared_limit",
default,
with = "::serde_with::rust::double_option",
skip_serializing_if = "Option::is_none"
)]
pub group_shared_limit: Option<Option<Box<models::GroupSpendControlLimitDetails>>>,
#[serde(
rename = "workspace_shared_limit",
default,
with = "::serde_with::rust::double_option",
skip_serializing_if = "Option::is_none"
)]
pub workspace_shared_limit: Option<Option<Box<models::SpendControlLimitDetails>>>,
}
impl SpendControlStatusDetails {
pub fn new(reached: bool) -> Self {
Self {
reached,
reached_limit_type: None,
individual_limit: None,
group_default_limit: None,
workspace_default_limit: None,
role_budget_limit: None,
group_shared_limit: None,
workspace_shared_limit: None,
}
}
}
+1
View File
@@ -35,6 +35,7 @@ fn rate_limit_snapshot() -> RateLimitSnapshot {
resets_at: Some(secondary_reset_at),
}),
credits: None,
spend_control: None,
plan_type: None,
rate_limit_reached_type: None,
}
+43
View File
@@ -2083,6 +2083,7 @@ pub struct RateLimitSnapshot {
pub primary: Option<RateLimitWindow>,
pub secondary: Option<RateLimitWindow>,
pub credits: Option<CreditsSnapshot>,
pub spend_control: Option<SpendControlSnapshot>,
pub plan_type: Option<crate::account::PlanType>,
pub rate_limit_reached_type: Option<RateLimitReachedType>,
}
@@ -2117,6 +2118,48 @@ pub struct CreditsSnapshot {
pub balance: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize, JsonSchema, TS)]
pub struct SpendControlSnapshot {
pub reached: bool,
pub reached_limit_type: Option<SpendControlLimitType>,
pub individual_limit: Option<SpendControlLimitSnapshot>,
pub group_default_limit: Option<GroupSpendControlLimitSnapshot>,
pub workspace_default_limit: Option<SpendControlLimitSnapshot>,
pub role_budget_limit: Option<SpendControlLimitSnapshot>,
pub group_shared_limit: Option<GroupSpendControlLimitSnapshot>,
pub workspace_shared_limit: Option<SpendControlLimitSnapshot>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, Serialize, JsonSchema, TS)]
#[serde(rename_all = "snake_case")]
#[ts(rename_all = "snake_case")]
pub enum SpendControlLimitType {
Individual,
GroupDefault,
WorkspaceDefault,
RoleBudget,
GroupShared,
WorkspaceShared,
}
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize, JsonSchema, TS)]
pub struct SpendControlLimitSnapshot {
pub limit: String,
pub used: String,
pub remaining: String,
pub used_percent: i32,
pub remaining_percent: i32,
pub reset_after_seconds: i32,
pub reset_at: i32,
}
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize, JsonSchema, TS)]
pub struct GroupSpendControlLimitSnapshot {
pub group_id: String,
pub group_name: String,
pub details: SpendControlLimitSnapshot,
}
// Includes prompts, tools and space to call compact.
const BASELINE_TOKENS: i64 = 12000;