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

UsageModelDialog.tsx

157 lines5.4 KB

1import { formatPercent, formatTokens, formatUsd } from "@t3tools/shared/usageFormat";
2import { isModelCostUnknown, type ModelTotals } from "@t3tools/shared/usageMerge";
3​
4import { mergeAnsweredUsage, type EnvironmentUsageStatus } from "../../state/usage";
5import { ProviderInstanceIcon } from "../chat/ProviderInstanceIcon";
6import { Button } from "../ui/button";
7import {
8 Dialog,
9 DialogDescription,
10 DialogFooter,
11 DialogHeader,
12 DialogPanel,
13 DialogPopup,
14 DialogTitle,
15} from "../ui/dialog";
16import { UsageProviderChart, type UsageChartMetric } from "./UsageProviderChart";
17import { UsageShareBar } from "./UsageShareBar";
18import {
19 cacheHitRate,
20 costPerMillionTokens,
21 costTypeSegments,
22 speedCostSegments,
23 tokenTypeSegments,
24} from "./usageBreakdown";
25import { PROVIDER_PRESENTATION } from "./usageProviders";
26​
27export interface UsageChartWindow {
28 readonly days: readonly string[];
29 readonly hours: readonly string[];
30 readonly resolution: "day" | "hour";
31 readonly timeZone: string;
32 readonly referenceTime: string | undefined;
33 readonly hour12: boolean;
35​
36export function UsageModelDialog({
37 model,
38 environments,
39 metric,
40 chartWindow,
41 onSetPrice,
42 onClose,
43}: {
44 readonly model: ModelTotals;
45 readonly environments: readonly EnvironmentUsageStatus[];
46 readonly metric: UsageChartMetric;
47 readonly chartWindow: UsageChartWindow;
48 readonly onSetPrice: () => void;
49 readonly onClose: () => void;
50}) {
51 const usage = mergeAnsweredUsage(
52 environments,
53 (bucket) => bucket.provider === model.provider && bucket.model === model.model,
54 );
55 const providers = [model.provider];
56 const presentation = PROVIDER_PRESENTATION[model.provider];
57 const costUnknown = isModelCostUnknown(model);
58 const hitRate = cacheHitRate(model);
59 const perMillion = costPerMillionTokens(model);
60 const stats = [
61 { label: "Cost", value: costUnknown ? "Unpriced" : formatUsd(model.costUsd) },
62 { label: "Tokens", value: formatTokens(model.totalTokens) },
63 perMillion === null ? null : { label: "Per 1M tokens", value: formatUsd(perMillion) },
64 hitRate === null ? null : { label: "Cache hit", value: formatPercent(hitRate) },
65 ].filter((stat) => stat !== null);
66​
67 return (
68 <Dialog
69 open
70 onOpenChange={(open) => {
71 if (!open) onClose();
72 }}
73 >
74 <DialogPopup className="max-w-3xl">
75 <DialogHeader>
76 <div className="flex items-center gap-2">
77 <ProviderInstanceIcon
78 driverKind={presentation.driverKind}
79 displayName={presentation.label}
80 iconClassName="size-5"
81 />
82 <DialogTitle>{model.model}</DialogTitle>
83 </div>
84 <DialogDescription>
85 {presentation.label}
86 {costUnknown ? "" : ` · ${formatPercent(model.costShare)} of cost`}
87 </DialogDescription>
88 </DialogHeader>
89 <DialogPanel>
90 <div className="flex flex-col gap-8">
91 <div className="grid grid-cols-2 gap-x-6 gap-y-4 sm:grid-cols-4">
92 {stats.map((stat) => (
93 <div key={stat.label} className="flex min-w-0 flex-col gap-0.5">
94 <span className="text-xs text-muted-foreground">{stat.label}</span>
95 <span className="text-2xl font-semibold text-foreground">{stat.value}</span>
96 </div>
97 ))}
98 </div>
99​
100 {/* Unpriced cost is unknown, not zero, so its trend shows tokens. */}
101 <UsageProviderChart
102 providers={providers}
103 days={chartWindow.days}
104 daily={usage.daily}
105 hours={chartWindow.hours}
106 hourly={usage.hourly}
107 metric={costUnknown ? "tokens" : metric}
108 referenceTime={chartWindow.referenceTime}
109 resolution={chartWindow.resolution}
110 timeZone={chartWindow.timeZone}
111 hour12={chartWindow.hour12}
112 />
113​
114 <div className="grid gap-x-10 gap-y-8 sm:grid-cols-2">
115 {costUnknown ? null : (
116 <UsageShareBar
117 label="Cost by type"
118 segments={costTypeSegments(usage.categoryCost)}
119 format={formatUsd}
120 />
121 )}
122 <UsageShareBar
123 label="Tokens by type"
124 segments={tokenTypeSegments(model.tokens)}
125 format={formatTokens}
126 />
127 {usage.speedCost.fast + usage.speedCost.ultrafast > 0 ? (
128 <UsageShareBar
129 label="Cost by speed"
130 segments={speedCostSegments(usage.speedCost)}
131 format={formatUsd}
132 aside={<SpeedPremium premiumUsd={usage.speedCost.premium} />}
133 />
134 ) : null}
135 </div>
136 </div>
137 </DialogPanel>
138 {model.unpricedTokens > 0 ? (
139 <DialogFooter variant="bare" className="items-center sm:justify-between">
140 <span className="text-xs text-muted-foreground">
141 {formatTokens(model.unpricedTokens)} tokens have no known price
142 </span>
143 <Button onClick={onSetPrice}>Set price</Button>
144 </DialogFooter>
145 ) : null}
146 </DialogPopup>
147 </Dialog>
148 );
150​
151export function SpeedPremium({ premiumUsd }: { readonly premiumUsd: number }) {
152 return (
153 <span className="text-xs text-muted-foreground">
154 Premium <span className="text-foreground tabular-nums">{formatUsd(premiumUsd)}</span>
155 </span>
156 );