Open History to see the last commit
1import type { UsageTokenTotals } from "@t3tools/contracts";
2import {
3 isModelCostUnknown,
4 type CategoryCost,
5 type ModelTotals,
6 type SpeedCost,
7} from "@t3tools/shared/usageMerge";
8import { formatDateTimeShort, formatHourShort } from "@t3tools/shared/usageFormat";
9
10import { msg } from "gt-react";
11
12import type { ShareSegment } from "./UsageShareBar";
13
14// A 24-hour window across spring-forward opens and closes on the same wall hour; date those two rows.
15export function breakdownHourLabel(
16 hourStart: string,
17 hours: readonly string[],
18 timeZone: string,
19 hour12: boolean,
20 locale?: string,
21) {
22 const label = (hour: string) => formatHourShort(hour, timeZone, hour12, locale);
23 const first = hours[0];
24 const last = hours.at(-1);
25 const endsCollide =
26 first !== undefined && last !== undefined && first !== last && label(first) === label(last);
27 return endsCollide && (hourStart === first || hourStart === last)
28 ? formatDateTimeShort(hourStart, timeZone, hour12, locale)
29 : label(hourStart);
30}
31
32export function sortModelsByTokens(models: readonly ModelTotals[]) {
33 return models.toSorted(
34 (left, right) => right.totalTokens - left.totalTokens || right.costUsd - left.costUsd,
35 );
36}
37
38/** Unknown cost yields null cost share; unpriced models still have token shares. */
39export function modelShare(model: ModelTotals, metric: "cost" | "tokens"): number | null {
40 if (metric === "tokens") return model.tokenShare;
41 return isModelCostUnknown(model) ? null : model.costShare;
42}
43
44/** Returns null without input; cache writes count as misses because input was fully processed. */
45export function cacheHitRate({ tokens }: ModelTotals): number | null {
46 const input = tokens.uncachedInputTokens + tokens.cachedInputTokens + tokens.cacheCreationTokens;
47 return input === 0 ? null : tokens.cachedInputTokens / input;
48}
49
50/** Effective USD per million priced tokens, or `null` when none were priced. */
51export function costPerMillionTokens(model: ModelTotals): number | null {
52 const pricedTokens = model.totalTokens - model.unpricedTokens;
53 return pricedTokens <= 0 || isModelCostUnknown(model)
54 ? null
55 : (model.costUsd / pricedTokens) * 1_000_000;
56}
57
58const ink = (percent: number) =>
59 `color-mix(in oklab, var(--foreground) ${percent}%, var(--background))`;
60
61const TYPE_COLORS = {
62 input: ink(60),
63 cacheRead: ink(30),
64 cacheWrite: ink(72),
65 output: ink(100),
66 other: ink(44),
67};
68
69export function costTypeSegments(cost: CategoryCost): readonly ShareSegment[] {
70 return [
71 {
72 label: msg({ context: "input tokens" })`Input`,
73 value: cost.input,
74 color: TYPE_COLORS.input,
75 },
76 { label: msg("Cache read"), value: cost.cacheRead, color: TYPE_COLORS.cacheRead },
77 { label: msg("Cache write"), value: cost.cacheWrite, color: TYPE_COLORS.cacheWrite },
78 { label: msg("Output"), value: cost.output, color: TYPE_COLORS.output },
79 {
80 label: msg("Other"),
81 value: cost.unsplit >= 0.005 ? cost.unsplit : 0,
82 color: TYPE_COLORS.other,
83 },
84 ];
85}
86
87export function tokenTypeSegments(
88 tokens: Omit<UsageTokenTotals, "reasoningTokens">,
89): readonly ShareSegment[] {
90 return [
91 {
92 label: msg({ context: "input tokens" })`Input`,
93 value: tokens.uncachedInputTokens,
94 color: TYPE_COLORS.input,
95 },
96 { label: msg("Cache read"), value: tokens.cachedInputTokens, color: TYPE_COLORS.cacheRead },
97 { label: msg("Cache write"), value: tokens.cacheCreationTokens, color: TYPE_COLORS.cacheWrite },
98 { label: msg("Output"), value: tokens.outputTokens, color: TYPE_COLORS.output },
99 ];
100}
101
102export function speedCostSegments(cost: SpeedCost): readonly ShareSegment[] {
103 return [
104 { label: msg("Standard"), value: cost.standard, color: ink(34) },
105 { label: msg("Fast"), value: cost.fast, color: ink(66) },
106 { label: msg("Ultrafast"), value: cost.ultrafast, color: ink(100) },
107 ];
108}