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