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