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