Skip to content

Commit 8159356

Browse files
fix(web): custom model price cells take only numbers
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 107f602 commit 8159356

3 files changed

Lines changed: 37 additions & 6 deletions

File tree

‎apps/web/src/components/usage/UsagePriceOverrides.tsx‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import { Label } from "../ui/label";
2626
import { Menu, MenuCheckboxItem, MenuPopup, MenuSeparator, MenuTrigger } from "../ui/menu";
2727
import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "../ui/table";
2828
import { Tooltip, TooltipTrigger, TooltipPopup } from "../ui/tooltip";
29-
import { USAGE_PRICE_FIELDS } from "./usagePriceForm";
29+
import { isUsagePriceEdit, USAGE_PRICE_FIELDS } from "./usagePriceForm";
3030
import {
3131
isEmptyUsagePriceDraft,
3232
usageAliasCell,
@@ -474,13 +474,15 @@ export function UsagePriceOverrides({
474474
) : (
475475
USAGE_PRICE_FIELDS.map((field) => {
476476
const cell = usagePriceCell(selected, row.model, field.key);
477+
const value =
478+
row.values[field.key] ?? (row.isNew ? "" : cell.value);
477479
return (
478480
<TableCell key={field.key}>
479481
<Input
480482
size="compact"
481483
inputMode="decimal"
482484
aria-label={`${field.label} price for ${row.model || "new model"}`}
483-
value={row.values[field.key] ?? (row.isNew ? "" : cell.value)}
485+
value={value}
484486
placeholder={
485487
row.isNew
486488
? field.optional
@@ -491,9 +493,10 @@ export function UsagePriceOverrides({
491493
autoComplete="off"
492494
disabled={locked}
493495
font="mono"
494-
onChange={(event) =>
495-
editCell(row, field.key, event.target.value)
496-
}
496+
onChange={(event) => {
497+
if (isUsagePriceEdit(value, event.target.value))
498+
editCell(row, field.key, event.target.value);
499+
}}
497500
/>
498501
</TableCell>
499502
);

‎apps/web/src/components/usage/usagePriceForm.test.ts‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { describe, expect, it } from "vite-plus/test";
22

3-
import { parseUsagePriceForm, usagePriceForm } from "./usagePriceForm.ts";
3+
import { isUsagePriceEdit, parseUsagePriceForm, usagePriceForm } from "./usagePriceForm.ts";
44

55
describe("model price entry", () => {
66
it("preserves exact model IDs and fractional rates", () => {
@@ -70,4 +70,19 @@ describe("model price entry", () => {
7070
expect(parseUsagePriceForm({ ...form, cacheWriteCostPerMillionTokens: "invalid" })).toBeNull();
7171
expect(parseUsagePriceForm({ ...form, model: " " })).toBeNull();
7272
});
73+
74+
it("lets price cells take only numbers while typing", () => {
75+
for (const price of ["0.075", ".5", "15", "1e-7", " 2.5 "])
76+
for (let end = 1; end <= price.length; end++)
77+
expect(isUsagePriceEdit(price.slice(0, end - 1), price.slice(0, end))).toBe(true);
78+
for (const next of ["a", "3a", "$3", "-1", "1.2.3", "e", "1,000"])
79+
expect(isUsagePriceEdit("", next)).toBe(false);
80+
});
81+
82+
it("never blocks deleting part of a price, but blocks replacing it with letters", () => {
83+
expect(isUsagePriceEdit("1e-7", "e-7")).toBe(true);
84+
expect(isUsagePriceEdit("1e-7", "1-7")).toBe(true);
85+
expect(isUsagePriceEdit("1e-7", "a7")).toBe(false);
86+
expect(isUsagePriceEdit("2.5", "x")).toBe(false);
87+
});
7388
});

‎apps/web/src/components/usage/usagePriceForm.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,19 @@ export function usagePriceForm(model = "", price?: UsageModelPriceOverride): Usa
2222
};
2323
}
2424

25+
/**
26+
* Whether a price cell may change from `previous` to `next` while typing:
27+
* digits with an optional decimal point, plus the exponent form saved rates can
28+
* stringify to. Deleting from a number is always allowed so a mid-value delete
29+
* such as `1e-7` to `e-7` is not stuck; saving still validates the result.
30+
*/
31+
export function isUsagePriceEdit(previous: string, next: string): boolean {
32+
return (
33+
/^\s*(?:\d*\.?\d*|(?:\d+\.?\d*|\.\d+)e[+-]?\d*)\s*$/i.test(next) ||
34+
(next.length < previous.length && /^[\d.e+\-\s]*$/i.test(next))
35+
);
36+
}
37+
2538
/** Blank cache prices use the input rate; explicit zero means free. */
2639
export function parseUsagePriceForm(
2740
form: UsagePriceForm,

0 commit comments

Comments
 (0)