TypeScript
fix(web): custom model price cells take only numbers (#19245)
Rakshith Bhat7a88b19576f6
diff --git a/apps/web/src/components/usage/UsagePriceOverrides.tsx b/apps/web/src/components/usage/UsagePriceOverrides.tsxindex 6217ea61ba6..efa0f453533 100644--- a/apps/web/src/components/usage/UsagePriceOverrides.tsx+++ b/apps/web/src/components/usage/UsagePriceOverrides.tsx@@ -26,7 +26,7 @@ import { Label } from "../ui/label";import { Menu, MenuCheckboxItem, MenuPopup, MenuSeparator, MenuTrigger } from "../ui/menu";import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "../ui/table";import { Tooltip, TooltipTrigger, TooltipPopup } from "../ui/tooltip";-import { USAGE_PRICE_FIELDS } from "./usagePriceForm";+import { isUsagePriceEdit, USAGE_PRICE_FIELDS } from "./usagePriceForm";import {isEmptyUsagePriceDraft,usageAliasCell,@@ -467,13 +467,15 @@ export function UsagePriceOverrides({) : (USAGE_PRICE_FIELDS.map((field) => {const cell = usagePriceCell(selected, row.model, field.key);+ const value =+ row.values[field.key] ?? (row.isNew ? "" : cell.value);return (<TableCell key={field.key}><Inputsize="compact"inputMode="decimal"aria-label={`${field.label} price for ${row.model || "new model"}`}- value={row.values[field.key] ?? (row.isNew ? "" : cell.value)}+ value={value}placeholder={row.isNew? field.optional@@ -484,9 +486,10 @@ export function UsagePriceOverrides({autoComplete="off"disabled={locked}font="mono"- onChange={(event) =>- editCell(row, field.key, event.target.value)- }+ onChange={(event) => {+ if (isUsagePriceEdit(value, event.target.value))+ editCell(row, field.key, event.target.value);+ }}/></TableCell>);diff --git a/apps/web/src/components/usage/usagePriceForm.test.ts b/apps/web/src/components/usage/usagePriceForm.test.tsindex 47c55565141..742308b4266 100644--- a/apps/web/src/components/usage/usagePriceForm.test.ts+++ b/apps/web/src/components/usage/usagePriceForm.test.ts@@ -1,6 +1,6 @@import { describe, expect, it } from "vite-plus/test";-import { parseUsagePriceForm, usagePriceForm } from "./usagePriceForm.ts";+import { isUsagePriceEdit, parseUsagePriceForm, usagePriceForm } from "./usagePriceForm.ts";describe("model price entry", () => {it("preserves exact model IDs and fractional rates", () => {@@ -70,4 +70,20 @@ describe("model price entry", () => {expect(parseUsagePriceForm({ ...form, cacheWriteCostPerMillionTokens: "invalid" })).toBeNull();expect(parseUsagePriceForm({ ...form, model: " " })).toBeNull();});++ it("lets price cells take only numbers while typing", () => {+ for (const price of ["0.075", ".5", "15", "1e-7", " 2.5 "])+ for (let end = 1; end <= price.length; end++)+ expect(isUsagePriceEdit(price.slice(0, end - 1), price.slice(0, end))).toBe(true);+ for (const next of ["a", "3a", "$3", "-1", "1.2.3", "e", "1,000"])+ expect(isUsagePriceEdit("", next)).toBe(false);+ });++ it("never blocks deleting part of a price, but blocks replacing it with a non-number", () => {+ expect(isUsagePriceEdit("1e-7", "e-7")).toBe(true);+ expect(isUsagePriceEdit("1e-7", "1-7")).toBe(true);+ expect(isUsagePriceEdit("1e-7", "a7")).toBe(false);+ expect(isUsagePriceEdit("2.5", "x")).toBe(false);+ expect(isUsagePriceEdit("123456", "1.2.3")).toBe(false);+ });});diff --git a/apps/web/src/components/usage/usagePriceForm.ts b/apps/web/src/components/usage/usagePriceForm.tsindex c52178300cd..c120284acf2 100644--- a/apps/web/src/components/usage/usagePriceForm.ts+++ b/apps/web/src/components/usage/usagePriceForm.ts@@ -22,6 +22,20 @@ export function usagePriceForm(model = "", price?: UsageModelPriceOverride): Usa};}+/**+ * Whether a price cell may change from `previous` to `next` while typing:+ * digits with an optional decimal point, plus the exponent form saved rates can+ * stringify to. Deleting a run of characters is always allowed so a mid-value+ * delete such as `1e-7` to `e-7` is not stuck; saving still validates the result.+ */+export function isUsagePriceEdit(previous: string, next: string): boolean {+ if (/^\s*(?:\d*\.?\d*|(?:\d+\.?\d*|\.\d+)e[+-]?\d*)\s*$/i.test(next)) return true;+ if (next.length >= previous.length) return false;+ let start = 0;+ while (start < next.length && next[start] === previous[start]) start++;+ return next.slice(start) === previous.slice(previous.length - (next.length - start));+}+/** Blank cache prices use the input rate; explicit zero means free. */export function parseUsagePriceForm(form: UsagePriceForm,