Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

usagePriceForm.ts

77 lines2.9 KB

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