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

usagePriceForm.ts

72 lines2.9 KB

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 };
24​
25/** Allow decimal/exponent edits and all deletions, including temporarily invalid values; saving validates
26 * the result. */
27export function isUsagePriceEdit(previous: string, next: string): boolean {
28 if (/^\s*(?:\d*[.,]?\d*|(?:\d+[.,]?\d*|[.,]\d+)e[+-]?\d*)\s*$/i.test(next)) return true;
29 if (next.length >= previous.length) return false;
30 let start = 0;
31 while (start < next.length && next[start] === previous[start]) start++;
32 return next.slice(start) === previous.slice(previous.length - (next.length - start));
34​
35/** Blank cache prices use the input rate; explicit zero means free. */
36export function parseUsagePriceForm(
37 form: UsagePriceForm,
38): { model: string; price: UsageModelPriceOverride } | null {
39 const model = form.model.trim();
40 if (model.length === 0) return null;
41 const rates: Partial<Record<(typeof USAGE_PRICE_FIELDS)[number]["key"], number>> = {};
42 for (const field of USAGE_PRICE_FIELDS) {
43 const raw = form[field.key].trim().replace(",", ".");
44 if (raw === "") {
45 if (field.optional) continue;
46 return null;
47 }
48 if (!/^(?:\d+(?:\.\d*)?|\.\d+)(?:e[+-]?\d+)?$/i.test(raw)) return null;
49 const value = Number(raw);
50 if (!Number.isFinite(value) || value < 0) return null;
51 rates[field.key] = value;
52 }
53 if (
54 rates.inputCostPerMillionTokens === undefined ||
55 rates.outputCostPerMillionTokens === undefined
56 ) {
57 return null;
58 }
59 return {
60 model,
61 price: {
62 inputCostPerMillionTokens: rates.inputCostPerMillionTokens,
63 outputCostPerMillionTokens: rates.outputCostPerMillionTokens,
64 ...(rates.cacheReadCostPerMillionTokens === undefined
65 ? {}
66 : { cacheReadCostPerMillionTokens: rates.cacheReadCostPerMillionTokens }),
67 ...(rates.cacheWriteCostPerMillionTokens === undefined
68 ? {}
69 : { cacheWriteCostPerMillionTokens: rates.cacheWriteCostPerMillionTokens }),
70 },
71 };