Skip to content
TypeScript

fix(web): accept comma as decimal in usage price entry (#21939)

Mika Krul9cee35ac4a38
diff --git a/apps/web/src/components/usage/usagePriceForm.test.ts b/apps/web/src/components/usage/usagePriceForm.test.ts
index b87d293af08..b66f48f8446 100644
--- a/apps/web/src/components/usage/usagePriceForm.test.ts
+++ b/apps/web/src/components/usage/usagePriceForm.test.ts
@@ -32,7 +32,7 @@ describe("model price entry", () => {
});
});
- it.each(["", " ", "-1", "Infinity", "NaN", "1e999", "$2", "0x10"])(
+ it.each(["", " ", "-1", "Infinity", "NaN", "1e999", "$2", "0x10", "1,2,3", "1.2,3", "1,2.3"])(
"rejects invalid required prices: %j",
(inputCostPerMillionTokens) => {
expect(
@@ -47,6 +47,33 @@ describe("model price entry", () => {
},
);
+ it("reads a comma as the decimal point", () => {
+ const form = usagePriceForm("example-model", {
+ inputCostPerMillionTokens: 2,
+ outputCostPerMillionTokens: 8,
+ });
+ expect(
+ parseUsagePriceForm({
+ ...form,
+ inputCostPerMillionTokens: "2,5",
+ outputCostPerMillionTokens: "0,075",
+ }),
+ ).toEqual({
+ model: "example-model",
+ price: { inputCostPerMillionTokens: 2.5, outputCostPerMillionTokens: 0.075 },
+ });
+ expect(parseUsagePriceForm({ ...form, inputCostPerMillionTokens: "1,2,3" })).toBeNull();
+ expect(isUsagePriceEdit("", "2,5")).toBe(true);
+ expect(isUsagePriceEdit("", ",5")).toBe(true);
+ expect(isUsagePriceEdit("", "1,2,3")).toBe(false);
+ expect(isUsagePriceEdit("1,2", "1,2.3")).toBe(false);
+ for (const value of ["2,5", ",5", "0,075", "1,5e-7"]) {
+ for (let end = 1; end <= value.length; end++) {
+ expect(isUsagePriceEdit(value.slice(0, end - 1), value.slice(0, end))).toBe(true);
+ }
+ }
+ });
+
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);
diff --git a/apps/web/src/components/usage/usagePriceForm.ts b/apps/web/src/components/usage/usagePriceForm.ts
index 0b74fc1aa0a..500eda424ed 100644
--- a/apps/web/src/components/usage/usagePriceForm.ts
+++ b/apps/web/src/components/usage/usagePriceForm.ts
@@ -25,7 +25,7 @@ export function usagePriceForm(model = "", price?: UsageModelPriceOverride): Usa
/** Allow decimal/exponent edits and all deletions, including temporarily invalid values; saving 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 (/^\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++;
@@ -40,7 +40,7 @@ export function parseUsagePriceForm(
if (model.length === 0) return null;
const rates: Partial<Record<(typeof USAGE_PRICE_FIELDS)[number]["key"], number>> = {};
for (const field of USAGE_PRICE_FIELDS) {
- const raw = form[field.key].trim();
+ const raw = form[field.key].trim().replace(",", ".");
if (raw === "") {
if (field.optional) continue;
return null;