All files / src/components/session UsageBar.tsx

0% Statements 0/33
0% Branches 0/1
0% Functions 0/1
0% Lines 0/33

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                                                                                       
import type { UsagePayload } from "../../types";
 
function formatTokens(n: number): string {
  if (n >= 1000) return `${(n / 1000).toFixed(1)}k`;
  return String(n);
}
 
function formatDuration(seconds: number): string {
  if (seconds < 60) return `${seconds.toFixed(1)}s`;
  const h = Math.floor(seconds / 3600);
  const m = Math.floor((seconds % 3600) / 60);
  const s = Math.floor(seconds % 60);
  if (h > 0) return `${String(h).padStart(2, "0")}:${String(m).padStart(2, "0")}:${String(s).padStart(2, "0")}`;
  return `${String(m).padStart(2, "0")}:${String(s).padStart(2, "0")}`;
}
 
export function UsageBar({
  sessionUsage,
  turnUsage,
}: {
  sessionUsage: UsagePayload | null;
  turnUsage: { usage: UsagePayload | null; elapsedSeconds?: number } | null;
}) {
  const tokens = formatTokens(sessionUsage?.total_tokens ?? 0);
  const cost = `$${(sessionUsage?.estimated_cost_usd ?? 0).toFixed(2)}`;
  const lastTurn = turnUsage
    ? `${formatTokens(turnUsage.usage?.total_tokens ?? 0)} / ${formatDuration(turnUsage.elapsedSeconds ?? 0)}`
    : null;
 
  return (
    <div className="usage-pills">
      <span className="usage-pills__item" title="Session tokens">{tokens}</span>
      <span className="usage-pills__sep" />
      <span className="usage-pills__item" title="Estimated cost">{cost}</span>
      {lastTurn ? (
        <>
          <span className="usage-pills__sep" />
          <span className="usage-pills__item" title="Last turn">{lastTurn}</span>
        </>
      ) : null}
    </div>
  );
}