The best way to control Claude Code, Codex and more from any computer or phone

main
ts 81 lines 2.97 KB
Raw
1 import type { UsageTokenTotals } from "@t3tools/contracts";
2 import {
3 isModelCostUnknown,
4 type CategoryCost,
5 type ModelTotals,
6 type SpeedCost,
7 } from "@t3tools/shared/usageMerge";
8
9 import { msg } from "gt-react";
10
11 import type { ShareSegment } from "./UsageShareBar";
12
13 export function sortModelsByTokens(models: readonly ModelTotals[]) {
14 return models.toSorted(
15 (left, right) => right.totalTokens - left.totalTokens || right.costUsd - left.costUsd,
16 );
17 }
18
19 /** Unknown cost yields null cost share; unpriced models still have token shares. */
20 export function modelShare(model: ModelTotals, metric: "cost" | "tokens"): number | null {
21 if (metric === "tokens") return model.tokenShare;
22 return isModelCostUnknown(model) ? null : model.costShare;
23 }
24
25 /** Returns null without input; cache writes count as misses because input was fully processed. */
26 export function cacheHitRate({ tokens }: ModelTotals): number | null {
27 const input = tokens.uncachedInputTokens + tokens.cachedInputTokens + tokens.cacheCreationTokens;
28 return input === 0 ? null : tokens.cachedInputTokens / input;
29 }
30
31 /** Effective USD per million priced tokens, or `null` when none were priced. */
32 export function costPerMillionTokens(model: ModelTotals): number | null {
33 const pricedTokens = model.totalTokens - model.unpricedTokens;
34 return pricedTokens <= 0 || isModelCostUnknown(model)
35 ? null
36 : (model.costUsd / pricedTokens) * 1_000_000;
37 }
38
39 const ink = (percent: number) =>
40 `color-mix(in oklab, var(--foreground) ${percent}%, var(--background))`;
41
42 const TYPE_COLORS = {
43 input: ink(60),
44 cacheRead: ink(30),
45 cacheWrite: ink(72),
46 output: ink(100),
47 other: ink(44),
48 };
49
50 export function costTypeSegments(cost: CategoryCost): readonly ShareSegment[] {
51 return [
52 { label: msg("Input"), value: cost.input, color: TYPE_COLORS.input },
53 { label: msg("Cache read"), value: cost.cacheRead, color: TYPE_COLORS.cacheRead },
54 { label: msg("Cache write"), value: cost.cacheWrite, color: TYPE_COLORS.cacheWrite },
55 { label: msg("Output"), value: cost.output, color: TYPE_COLORS.output },
56 {
57 label: msg("Other"),
58 value: cost.unsplit >= 0.005 ? cost.unsplit : 0,
59 color: TYPE_COLORS.other,
60 },
61 ];
62 }
63
64 export function tokenTypeSegments(
65 tokens: Omit<UsageTokenTotals, "reasoningTokens">,
66 ): readonly ShareSegment[] {
67 return [
68 { label: msg("Input"), value: tokens.uncachedInputTokens, color: TYPE_COLORS.input },
69 { label: msg("Cache read"), value: tokens.cachedInputTokens, color: TYPE_COLORS.cacheRead },
70 { label: msg("Cache write"), value: tokens.cacheCreationTokens, color: TYPE_COLORS.cacheWrite },
71 { label: msg("Output"), value: tokens.outputTokens, color: TYPE_COLORS.output },
72 ];
73 }
74
75 export function speedCostSegments(cost: SpeedCost): readonly ShareSegment[] {
76 return [
77 { label: msg("Standard"), value: cost.standard, color: ink(34) },
78 { label: msg("Fast"), value: cost.fast, color: ink(66) },
79 { label: msg("Ultrafast"), value: cost.ultrafast, color: ink(100) },
80 ];
81 }