|
10 | 10 | * endpoint speaks a D5-SAFE shape — FRACTIONS of the pool's cap, never a raw |
11 | 11 | * token count — so nothing here (or downstream) can leak a token number. |
12 | 12 | * |
13 | | - * Wire shape: `{ pool, breakdown }`. `pool` is the one reading that gates; |
| 13 | + * Wire shape: `{ pool, breakdown, tiers }`. `pool` is the one reading that gates; |
14 | 14 | * `breakdown` is an observation-only split of that same pool into app-building |
15 | 15 | * vs data Q&A (cloud ADR-0015). The reader is strict: it reads this shape and no |
16 | 16 | * other — the retired per-meter `{ meters: { build, dataChat } }` answer parses |
|
19 | 19 | * it, it does not require it), so its ABSENCE is a normal reading and never a |
20 | 20 | * parse failure. |
21 | 21 | * |
| 22 | + * `tiers` (cloud#5159) is optional the same way: a paid plan with a known caller |
| 23 | + * also reports the two tiers of its allowance — the caller's personal share and |
| 24 | + * the team pool it overflows into — plus the notice to show once turns draw on |
| 25 | + * the pool. Free, self-hosted and older backends omit it. A `tiers` that is not |
| 26 | + * the declared shape is dropped on its own and the `{ pool, breakdown }` reading |
| 27 | + * stands: the ring is drawn from `pool` alone, so a bad tier answer costs the |
| 28 | + * two-tier detail, never the ring. |
| 29 | + * |
22 | 30 | * Refetch triggers, cheap and event-driven (no busy polling): |
23 | 31 | * - on mount / apiBase change, |
24 | 32 | * - on the `AI_USAGE_REFRESH_EVENT` the chat engine fires after a turn finishes |
@@ -84,11 +92,45 @@ export interface AiUsageBreakdown { |
84 | 92 | dataChat: number | null; |
85 | 93 | } |
86 | 94 |
|
| 95 | +/** One tier of a paid plan's allowance: a fraction of THAT tier's cap. Never tokens. */ |
| 96 | +export interface AiUsageTier { |
| 97 | + /** 0..1 of this tier's cap. */ |
| 98 | + fraction: number; |
| 99 | + /** Best-effort reset instant (ISO); null when unknown — never guessed client-side. */ |
| 100 | + resetsAt: string | null; |
| 101 | +} |
| 102 | + |
| 103 | +/** The server's notice, in the four languages the cloud endpoint writes. */ |
| 104 | +export interface AiUsageTierNotice { |
| 105 | + en: string; |
| 106 | + zh: string; |
| 107 | + ja: string; |
| 108 | + es: string; |
| 109 | +} |
| 110 | + |
| 111 | +/** |
| 112 | + * The two tiers of a paid plan's allowance (cloud#5159): the caller's personal |
| 113 | + * share, and the team pool it overflows into once that share is used up. |
| 114 | + * Observation only — `pool` stays the reading that blocks. |
| 115 | + */ |
| 116 | +export interface AiUsageTiers { |
| 117 | + /** The caller's personal share; null for a pool-only caller (an AI service account). */ |
| 118 | + personal: AiUsageTier | null; |
| 119 | + /** The team pool, credit packs included. */ |
| 120 | + teamPool: AiUsageTier; |
| 121 | + /** The personal share is used up and turns draw on the team pool. */ |
| 122 | + usingTeamPool: boolean; |
| 123 | + /** Non-null exactly when `usingTeamPool`: the notice to show the caller. */ |
| 124 | + notice: AiUsageTierNotice | null; |
| 125 | +} |
| 126 | + |
87 | 127 | export interface AiUsageResponse { |
88 | 128 | /** The one metered pool — this is what blocks. */ |
89 | 129 | pool: AiMeterUsage; |
90 | 130 | /** Read-only split of the same pool. Absent when the runtime does not report it. */ |
91 | 131 | breakdown?: AiUsageBreakdown; |
| 132 | + /** The paid plan's two allowance tiers. Absent on Free, self-hosted and older backends. */ |
| 133 | + tiers?: AiUsageTiers; |
92 | 134 | } |
93 | 135 |
|
94 | 136 | export interface UseAiUsageOptions { |
@@ -135,20 +177,63 @@ function isBreakdown(v: unknown): v is AiUsageBreakdown { |
135 | 177 | return isFractionOrNull(b.build) && isFractionOrNull(b.dataChat); |
136 | 178 | } |
137 | 179 |
|
| 180 | +/** A tier, copied to exactly its declared fields; null when `v` is not one. */ |
| 181 | +function readTier(v: unknown): AiUsageTier | null { |
| 182 | + if (!v || typeof v !== 'object') return null; |
| 183 | + const t = v as Record<string, unknown>; |
| 184 | + if (typeof t.fraction !== 'number' || !Number.isFinite(t.fraction)) return null; |
| 185 | + if (t.resetsAt !== null && typeof t.resetsAt !== 'string') return null; |
| 186 | + return { fraction: t.fraction, resetsAt: t.resetsAt }; |
| 187 | +} |
| 188 | + |
| 189 | +/** The notice, copied to exactly its four languages; null when `v` is not one. */ |
| 190 | +function readNotice(v: unknown): AiUsageTierNotice | null { |
| 191 | + if (!v || typeof v !== 'object') return null; |
| 192 | + const n = v as Record<string, unknown>; |
| 193 | + if (typeof n.en !== 'string' || typeof n.zh !== 'string' || typeof n.ja !== 'string' || typeof n.es !== 'string') |
| 194 | + return null; |
| 195 | + return { en: n.en, zh: n.zh, ja: n.ja, es: n.es }; |
| 196 | +} |
| 197 | + |
| 198 | +/** |
| 199 | + * `tiers` in exactly its declared shape, or `undefined` when it is anything else |
| 200 | + * (see the file header: a bad `tiers` drops on its own). |
| 201 | + */ |
| 202 | +function readTiers(v: unknown): AiUsageTiers | undefined { |
| 203 | + if (!v || typeof v !== 'object') return undefined; |
| 204 | + const t = v as Record<string, unknown>; |
| 205 | + const teamPool = readTier(t.teamPool); |
| 206 | + if (!teamPool || typeof t.usingTeamPool !== 'boolean') return undefined; |
| 207 | + const personal = t.personal === null ? null : readTier(t.personal); |
| 208 | + if (personal === null && t.personal !== null) return undefined; |
| 209 | + const notice = t.notice === null ? null : readNotice(t.notice); |
| 210 | + if (notice === null && t.notice !== null) return undefined; |
| 211 | + return { personal, teamPool, usingTeamPool: t.usingTeamPool, notice }; |
| 212 | +} |
| 213 | + |
138 | 214 | /** |
139 | | - * Read the wire payload as `{ pool, breakdown? }` — and only as that. `null` |
140 | | - * means the answer is not this shape (see the file header for why no second |
141 | | - * shape is read). |
| 215 | + * Read the wire payload as `{ pool, breakdown?, tiers? }` — and only as that. |
| 216 | + * `null` means the answer is not this shape (see the file header for why no |
| 217 | + * second shape is read). |
142 | 218 | */ |
143 | 219 | function parseUsage(payload: unknown): AiUsageResponse | null { |
144 | 220 | if (!payload || typeof payload !== 'object') return null; |
145 | | - const { pool, breakdown } = payload as { pool?: unknown; breakdown?: unknown }; |
| 221 | + const { pool, breakdown, tiers } = payload as { pool?: unknown; breakdown?: unknown; tiers?: unknown }; |
146 | 222 | if (!isPool(pool)) return null; |
| 223 | + const out: AiUsageResponse = { pool }; |
147 | 224 | // Absent `breakdown` is a normal reading (optional upstream, cloud#2133), never |
148 | 225 | // a parse failure: the pool alone is everything the ring needs. |
149 | | - if (breakdown === undefined) return { pool }; |
150 | | - if (!isBreakdown(breakdown)) return null; |
151 | | - return { pool, breakdown: { build: breakdown.build, dataChat: breakdown.dataChat } }; |
| 226 | + if (breakdown !== undefined) { |
| 227 | + if (!isBreakdown(breakdown)) return null; |
| 228 | + out.breakdown = { build: breakdown.build, dataChat: breakdown.dataChat }; |
| 229 | + } |
| 230 | + // Absent `tiers` is the Free / self-hosted / older-backend reading. A |
| 231 | + // malformed one drops alone; the pool and its split still stand. |
| 232 | + if (tiers !== undefined) { |
| 233 | + const read = readTiers(tiers); |
| 234 | + if (read) out.tiers = read; |
| 235 | + } |
| 236 | + return out; |
152 | 237 | } |
153 | 238 |
|
154 | 239 | /** |
|
0 commit comments