The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { msg } from "gt-react"; |
| 2 | import type { UsageModelPriceOverride } from "@t3tools/contracts"; |
| 3 | |
| 4 | export const USAGE_PRICE_FIELDS = [ |
| 5 | { key: "inputCostPerMillionTokens", label: msg("Input"), optional: false }, |
| 6 | { key: "outputCostPerMillionTokens", label: msg("Output"), optional: false }, |
| 7 | { key: "cacheReadCostPerMillionTokens", label: msg("Cache read"), optional: true }, |
| 8 | { key: "cacheWriteCostPerMillionTokens", label: msg("Cache write"), optional: true }, |
| 9 | ] as const; |
| 10 | |
| 11 | export type UsagePriceForm = { model: string } & Record< |
| 12 | (typeof USAGE_PRICE_FIELDS)[number]["key"], |
| 13 | string |
| 14 | >; |
| 15 | |
| 16 | export function usagePriceForm(model = "", price?: UsageModelPriceOverride): UsagePriceForm { |
| 17 | return { |
| 18 | model, |
| 19 | inputCostPerMillionTokens: price?.inputCostPerMillionTokens.toString() ?? "", |
| 20 | outputCostPerMillionTokens: price?.outputCostPerMillionTokens.toString() ?? "", |
| 21 | cacheReadCostPerMillionTokens: price?.cacheReadCostPerMillionTokens?.toString() ?? "", |
| 22 | cacheWriteCostPerMillionTokens: price?.cacheWriteCostPerMillionTokens?.toString() ?? "", |
| 23 | }; |
| 24 | } |
| 25 | |
| 26 | /** Allow decimal/exponent edits and all deletions, including temporarily invalid values; saving validates |
| 27 | * the result. */ |
| 28 | export function isUsagePriceEdit(previous: string, next: string): boolean { |
| 29 | if (/^\s*(?:\d*[.,]?\d*|(?:\d+[.,]?\d*|[.,]\d+)e[+-]?\d*)\s*$/i.test(next)) return true; |
| 30 | if (next.length >= previous.length) return false; |
| 31 | let start = 0; |
| 32 | while (start < next.length && next[start] === previous[start]) start++; |
| 33 | return next.slice(start) === previous.slice(previous.length - (next.length - start)); |
| 34 | } |
| 35 | |
| 36 | /** Blank cache prices use the input rate; explicit zero means free. */ |
| 37 | export function parseUsagePriceForm( |
| 38 | form: UsagePriceForm, |
| 39 | ): { model: string; price: UsageModelPriceOverride } | null { |
| 40 | const model = form.model.trim(); |
| 41 | if (model.length === 0) return null; |
| 42 | const rates: Partial<Record<(typeof USAGE_PRICE_FIELDS)[number]["key"], number>> = {}; |
| 43 | for (const field of USAGE_PRICE_FIELDS) { |
| 44 | const raw = form[field.key].trim().replace(",", "."); |
| 45 | if (raw === "") { |
| 46 | if (field.optional) continue; |
| 47 | return null; |
| 48 | } |
| 49 | if (!/^(?:\d+(?:\.\d*)?|\.\d+)(?:e[+-]?\d+)?$/i.test(raw)) return null; |
| 50 | const value = Number(raw); |
| 51 | if (!Number.isFinite(value) || value < 0) return null; |
| 52 | rates[field.key] = value; |
| 53 | } |
| 54 | if ( |
| 55 | rates.inputCostPerMillionTokens === undefined || |
| 56 | rates.outputCostPerMillionTokens === undefined |
| 57 | ) { |
| 58 | return null; |
| 59 | } |
| 60 | return { |
| 61 | model, |
| 62 | price: { |
| 63 | inputCostPerMillionTokens: rates.inputCostPerMillionTokens, |
| 64 | outputCostPerMillionTokens: rates.outputCostPerMillionTokens, |
| 65 | ...(rates.cacheReadCostPerMillionTokens === undefined |
| 66 | ? {} |
| 67 | : { cacheReadCostPerMillionTokens: rates.cacheReadCostPerMillionTokens }), |
| 68 | ...(rates.cacheWriteCostPerMillionTokens === undefined |
| 69 | ? {} |
| 70 | : { cacheWriteCostPerMillionTokens: rates.cacheWriteCostPerMillionTokens }), |
| 71 | }, |
| 72 | }; |
| 73 | } |