Skip to content

Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.

4.9 KB
Raw Blame
import { Sparkles } from "lucide-react";
import { Card, CardContent, CardHeader, CardTitle } from "../ui/card";
import { formatCost, formatTokens } from "../lib/format";
import { ProportionBar } from "../shared/proportion-bar";

/** Structural slice of the savings report this tile renders — the full
 *  savings payload and the overview-summary headline both fit. */
export interface SavingsMiniData {
  available: boolean;
  saved_input_tokens?: number;
  measured_saved_input_tokens?: number;
  inferred_saved_input_tokens?: number;
  unpriced_saved_input_tokens?: number;
  estimated_usd_saved?: number;
}

interface SavingsMiniProps {
  /** The savings report (or the overview-summary headline);
   *  null/undefined when unavailable. */
  data?: SavingsMiniData | null;
  /** Repo id, for the "View costs →" link. */
  repoId: string;
  /**
   * Whether savings can ever be tracked on this deployment. Savings come from
   * a local `.repowise/omissions` sidecar written by the CLI, so hosted has no
   * way to populate them: it passes `false` to get the indexing-cost framing
   * instead of an empty state pitching a CLI the viewer isn't running.
   * Defaults to true (the local CLI dashboard).
   */
  trackable?: boolean;
}

/**
 * Compact overview tile for agent token savings — the headline number, what the
 * priced part of it was worth, and the measured-vs-inferred evidence split,
 * with a jump to the full Costs results page.
 *
 * The evidence split rides on the tile rather than the surface split because a
 * lone headline reads as one confident number, and a counterfactual estimate
 * and a measured before/after are not the same claim.
 */
export function SavingsMini({ data, repoId, trackable = true }: SavingsMiniProps) {
  const total = data?.saved_input_tokens ?? 0;
  const measured = data?.measured_saved_input_tokens ?? 0;
  const inferred = data?.inferred_saved_input_tokens ?? 0;
  const hasData = !!data?.available && total > 0;
  const costsHref = `/repos/${repoId}/costs`;


  return (
    <Card>
      <CardHeader className="pb-2">
        <CardTitle className="text-sm flex items-center justify-between">
          <span className="flex items-center gap-2">
            <Sparkles className="h-4 w-4 text-[var(--color-accent-secondary)]" />
            {trackable ? "Agent savings" : "Costs"}
          </span>
          <a
            href={costsHref}
            className="text-[10px] text-[var(--color-accent-primary)] hover:underline font-normal"
          >
            View costs →
          </a>
        </CardTitle>
      </CardHeader>
      <CardContent className="pt-0">
        {hasData ? (
          <div className="space-y-3">
            <div className="flex items-baseline gap-2">
              <span className="text-3xl font-semibold tabular-nums text-[var(--color-text-primary)]">
                {formatTokens(total)}
              </span>
              <span className="text-base font-semibold tabular-nums text-[var(--color-success)]">
                {formatCost(data!.estimated_usd_saved ?? 0)}
              </span>
            </div>
            <p className="text-xs text-[var(--color-text-secondary)] -mt-1">
              {inferred > 0 ? "estimated tokens saved for your agent" : "tokens saved for your agent"}
              {(data!.unpriced_saved_input_tokens ?? 0) > 0 ? (
                <span className="text-[var(--color-text-tertiary)]">
                  {" "}
                  · some events carry no recorded rate
                </span>
              ) : null}
            </p>

            {/* Measured leads in either order: it is the stronger claim. */}
            <ProportionBar
              label="Tokens saved by evidence"
              sort={false}
              segments={[
                { key: "measured", label: "Measured", value: measured, detail: formatTokens(measured) },
                { key: "inferred", label: "Inferred", value: inferred, detail: formatTokens(inferred) },
              ]}
            />
          </div>
        ) : (
          <div className="space-y-2 py-1">
            <p className="text-xs text-[var(--color-text-secondary)] leading-snug">
              {trackable ? (
                <>
                  Trim what your agent reads with <code>repowise distill</code> and the MCP tools —
                  savings show up here.
                </>
              ) : (
                // Agent savings are written by the local CLI sidecar, so there
                // is nothing to earn here on hosted. Point at what the Costs
                // page *does* show instead of teasing a number that never lands.
                <>What this repo&apos;s indexing and doc generation cost, per sync.</>
              )}
            </p>
            <a
              href={costsHref}
              className="inline-block text-xs text-[var(--color-accent-primary)] hover:underline"
            >
              Open the Costs page →
            </a>
          </div>
        )}
      </CardContent>
    </Card>
  );
}