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