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

UsageModelDialog.tsx

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