From d322741267090211609b5a359792c84ed4ec5f22 Mon Sep 17 00:00:00 2001 From: Edward Frazer Date: Fri, 15 May 2026 23:45:09 -0700 Subject: [PATCH] feat: expose spend controls in codex usage snapshots --- .../schema/json/ServerNotification.json | 160 ++++++++++++++++++ .../codex_app_server_protocol.schemas.json | 160 ++++++++++++++++++ .../codex_app_server_protocol.v2.schemas.json | 160 ++++++++++++++++++ .../AccountRateLimitsUpdatedNotification.json | 160 ++++++++++++++++++ .../json/v2/GetAccountRateLimitsResponse.json | 160 ++++++++++++++++++ .../v2/GroupSpendControlLimitSnapshot.ts | 6 + .../schema/typescript/v2/RateLimitSnapshot.ts | 3 +- .../v2/SpendControlLimitSnapshot.ts | 5 + .../typescript/v2/SpendControlLimitType.ts | 5 + .../typescript/v2/SpendControlSnapshot.ts | 8 + .../schema/typescript/v2/index.ts | 4 + .../src/protocol/v2/account.rs | 114 +++++++++++++ codex-rs/app-server/src/outgoing_message.rs | 1 + .../app-server/tests/suite/v2/rate_limits.rs | 3 + codex-rs/backend-client/src/client.rs | 148 ++++++++++++++++ codex-rs/backend-client/src/types.rs | 4 + codex-rs/codex-api/src/rate_limits.rs | 2 + .../group_spend_control_limit_details.rs | 37 ++++ .../src/models/mod.rs | 10 ++ .../src/models/rate_limit_status_payload.rs | 8 + .../src/models/spend_control_limit_details.rs | 52 ++++++ .../models/spend_control_status_details.rs | 99 +++++++++++ codex-rs/protocol/src/error_tests.rs | 1 + codex-rs/protocol/src/protocol.rs | 43 +++++ 24 files changed, 1352 insertions(+), 1 deletion(-) create mode 100644 codex-rs/app-server-protocol/schema/typescript/v2/GroupSpendControlLimitSnapshot.ts create mode 100644 codex-rs/app-server-protocol/schema/typescript/v2/SpendControlLimitSnapshot.ts create mode 100644 codex-rs/app-server-protocol/schema/typescript/v2/SpendControlLimitType.ts create mode 100644 codex-rs/app-server-protocol/schema/typescript/v2/SpendControlSnapshot.ts create mode 100644 codex-rs/codex-backend-openapi-models/src/models/group_spend_control_limit_details.rs create mode 100644 codex-rs/codex-backend-openapi-models/src/models/spend_control_limit_details.rs create mode 100644 codex-rs/codex-backend-openapi-models/src/models/spend_control_status_details.rs diff --git a/codex-rs/app-server-protocol/schema/json/ServerNotification.json b/codex-rs/app-server-protocol/schema/json/ServerNotification.json index 6af19f89e2..8711c4f837 100644 --- a/codex-rs/app-server-protocol/schema/json/ServerNotification.json +++ b/codex-rs/app-server-protocol/schema/json/ServerNotification.json @@ -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": [ { 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 935ec98e88..bd4f66fdbf 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 @@ -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": [ { 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 ccf68b28d8..96c3343c3c 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 @@ -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": [ { 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..5e95e71f1f 100644 --- a/codex-rs/app-server-protocol/schema/json/v2/AccountRateLimitsUpdatedNotification.json +++ b/codex-rs/app-server-protocol/schema/json/v2/AccountRateLimitsUpdatedNotification.json @@ -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": { 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..5849168e7b 100644 --- a/codex-rs/app-server-protocol/schema/json/v2/GetAccountRateLimitsResponse.json +++ b/codex-rs/app-server-protocol/schema/json/v2/GetAccountRateLimitsResponse.json @@ -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": { diff --git a/codex-rs/app-server-protocol/schema/typescript/v2/GroupSpendControlLimitSnapshot.ts b/codex-rs/app-server-protocol/schema/typescript/v2/GroupSpendControlLimitSnapshot.ts new file mode 100644 index 0000000000..51cfbddf24 --- /dev/null +++ b/codex-rs/app-server-protocol/schema/typescript/v2/GroupSpendControlLimitSnapshot.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 { SpendControlLimitSnapshot } from "./SpendControlLimitSnapshot"; + +export type GroupSpendControlLimitSnapshot = { groupId: string, groupName: string, details: SpendControlLimitSnapshot, }; 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..dafea76281 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 { 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, }; diff --git a/codex-rs/app-server-protocol/schema/typescript/v2/SpendControlLimitSnapshot.ts b/codex-rs/app-server-protocol/schema/typescript/v2/SpendControlLimitSnapshot.ts new file mode 100644 index 0000000000..09d105e969 --- /dev/null +++ b/codex-rs/app-server-protocol/schema/typescript/v2/SpendControlLimitSnapshot.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 SpendControlLimitSnapshot = { limit: string, used: string, remaining: string, usedPercent: number, remainingPercent: number, resetAfterSeconds: number, resetAt: number, }; diff --git a/codex-rs/app-server-protocol/schema/typescript/v2/SpendControlLimitType.ts b/codex-rs/app-server-protocol/schema/typescript/v2/SpendControlLimitType.ts new file mode 100644 index 0000000000..7eec59ab9b --- /dev/null +++ b/codex-rs/app-server-protocol/schema/typescript/v2/SpendControlLimitType.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 SpendControlLimitType = "individual" | "group_default" | "workspace_default" | "role_budget" | "group_shared" | "workspace_shared"; diff --git a/codex-rs/app-server-protocol/schema/typescript/v2/SpendControlSnapshot.ts b/codex-rs/app-server-protocol/schema/typescript/v2/SpendControlSnapshot.ts new file mode 100644 index 0000000000..b1851ecd7a --- /dev/null +++ b/codex-rs/app-server-protocol/schema/typescript/v2/SpendControlSnapshot.ts @@ -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, }; 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 beb1973bb9..8093cfdea2 100644 --- a/codex-rs/app-server-protocol/schema/typescript/v2/index.ts +++ b/codex-rs/app-server-protocol/schema/typescript/v2/index.ts @@ -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"; diff --git a/codex-rs/app-server-protocol/src/protocol/v2/account.rs b/codex-rs/app-server-protocol/src/protocol/v2/account.rs index efb4a26f60..dba47d88b8 100644 --- a/codex-rs/app-server-protocol/src/protocol/v2/account.rs +++ b/codex-rs/app-server-protocol/src/protocol/v2/account.rs @@ -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, pub secondary: Option, pub credits: Option, + pub spend_control: Option, pub plan_type: Option, pub rate_limit_reached_type: Option, } @@ -272,6 +277,7 @@ impl From 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 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, + pub individual_limit: Option, + pub group_default_limit: Option, + pub workspace_default_limit: Option, + pub role_budget_limit: Option, + pub group_shared_limit: Option, + pub workspace_shared_limit: Option, +} + +impl From 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 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 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 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/")] diff --git a/codex-rs/app-server/src/outgoing_message.rs b/codex-rs/app-server/src/outgoing_message.rs index 97ef37f742..70debdb288 100644 --- a/codex-rs/app-server/src/outgoing_message.rs +++ b/codex-rs/app-server/src/outgoing_message.rs @@ -788,6 +788,7 @@ mod tests { }), secondary: None, credits: None, + spend_control: None, plan_type: Some(PlanType::Plus), rate_limit_reached_type: None, }, 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..17af6c44d0 100644 --- a/codex-rs/app-server/tests/suite/v2/rate_limits.rs +++ b/codex-rs/app-server/tests/suite/v2/rate_limits.rs @@ -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, }, diff --git a/codex-rs/backend-client/src/client.rs b/codex-rs/backend-client/src/client.rs index 6365d527ed..20e033023d 100644 --- a/codex-rs/backend-client/src/client.rs +++ b/codex-rs/backend-client/src/client.rs @@ -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, rate_limit: Option, credits: Option, + spend_control: Option, plan_type: Option, rate_limit_reached_type: Option, ) -> 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, + ) -> Option { + 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, }; diff --git a/codex-rs/backend-client/src/types.rs b/codex-rs/backend-client/src/types.rs index d8d24ab9fc..9f92a75ed8 100644 --- a/codex-rs/backend-client/src/types.rs +++ b/codex-rs/backend-client/src/types.rs @@ -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; diff --git a/codex-rs/codex-api/src/rate_limits.rs b/codex-rs/codex-api/src/rate_limits.rs index 979500cdab..9ff50d297b 100644 --- a/codex-rs/codex-api/src/rate_limits.rs +++ b/codex-rs/codex-api/src/rate_limits.rs @@ -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 { primary, secondary, credits, + spend_control: None, plan_type: event.plan_type, rate_limit_reached_type: None, }) diff --git a/codex-rs/codex-backend-openapi-models/src/models/group_spend_control_limit_details.rs b/codex-rs/codex-backend-openapi-models/src/models/group_spend_control_limit_details.rs new file mode 100644 index 0000000000..b4318c9fe4 --- /dev/null +++ b/codex-rs/codex-backend-openapi-models/src/models/group_spend_control_limit_details.rs @@ -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, +} + +impl GroupSpendControlLimitDetails { + pub fn new( + group_id: String, + group_name: String, + details: models::SpendControlLimitDetails, + ) -> Self { + Self { + group_id, + group_name, + details: Box::new(details), + } + } +} 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..4c76028799 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,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; 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..cc81a978bf 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 @@ -37,6 +37,13 @@ pub struct RateLimitStatusPayload { skip_serializing_if = "Option::is_none" )] pub additional_rate_limits: Option>>, + #[serde( + rename = "spend_control", + default, + with = "::serde_with::rust::double_option", + skip_serializing_if = "Option::is_none" + )] + pub spend_control: Option>>, #[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, } } diff --git a/codex-rs/codex-backend-openapi-models/src/models/spend_control_limit_details.rs b/codex-rs/codex-backend-openapi-models/src/models/spend_control_limit_details.rs new file mode 100644 index 0000000000..fe592eb8d2 --- /dev/null +++ b/codex-rs/codex-backend-openapi-models/src/models/spend_control_limit_details.rs @@ -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, + } + } +} diff --git a/codex-rs/codex-backend-openapi-models/src/models/spend_control_status_details.rs b/codex-rs/codex-backend-openapi-models/src/models/spend_control_status_details.rs new file mode 100644 index 0000000000..ae04e0d464 --- /dev/null +++ b/codex-rs/codex-backend-openapi-models/src/models/spend_control_status_details.rs @@ -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>, + #[serde( + rename = "individual_limit", + default, + with = "::serde_with::rust::double_option", + skip_serializing_if = "Option::is_none" + )] + pub individual_limit: Option>>, + #[serde( + rename = "group_default_limit", + default, + with = "::serde_with::rust::double_option", + skip_serializing_if = "Option::is_none" + )] + pub group_default_limit: Option>>, + #[serde( + rename = "workspace_default_limit", + default, + with = "::serde_with::rust::double_option", + skip_serializing_if = "Option::is_none" + )] + pub workspace_default_limit: Option>>, + #[serde( + rename = "role_budget_limit", + default, + with = "::serde_with::rust::double_option", + skip_serializing_if = "Option::is_none" + )] + pub role_budget_limit: Option>>, + #[serde( + rename = "group_shared_limit", + default, + with = "::serde_with::rust::double_option", + skip_serializing_if = "Option::is_none" + )] + pub group_shared_limit: Option>>, + #[serde( + rename = "workspace_shared_limit", + default, + with = "::serde_with::rust::double_option", + skip_serializing_if = "Option::is_none" + )] + pub workspace_shared_limit: Option>>, +} + +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, + } + } +} diff --git a/codex-rs/protocol/src/error_tests.rs b/codex-rs/protocol/src/error_tests.rs index aef7478607..870e042d6b 100644 --- a/codex-rs/protocol/src/error_tests.rs +++ b/codex-rs/protocol/src/error_tests.rs @@ -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, } diff --git a/codex-rs/protocol/src/protocol.rs b/codex-rs/protocol/src/protocol.rs index fac2616d04..21f635ff46 100644 --- a/codex-rs/protocol/src/protocol.rs +++ b/codex-rs/protocol/src/protocol.rs @@ -2083,6 +2083,7 @@ pub struct RateLimitSnapshot { pub primary: Option, pub secondary: Option, pub credits: Option, + pub spend_control: Option, pub plan_type: Option, pub rate_limit_reached_type: Option, } @@ -2117,6 +2118,48 @@ pub struct CreditsSnapshot { pub balance: Option, } +#[derive(Debug, Clone, PartialEq, Deserialize, Serialize, JsonSchema, TS)] +pub struct SpendControlSnapshot { + pub reached: bool, + pub reached_limit_type: Option, + pub individual_limit: Option, + pub group_default_limit: Option, + pub workspace_default_limit: Option, + pub role_budget_limit: Option, + pub group_shared_limit: Option, + pub workspace_shared_limit: Option, +} + +#[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;