A local-first multi-agent app development platform, built on PenguinHarness
/**
* Task stats line: after each Task finishes, shows "this turn's usage" at the
* **bottom-left** of the AI reply — input, output, output TPS, elapsed time, and cost (converted
* in real time from the current Model's pricing, hidden if no pricing is configured); all five
* share the same basis (this turn's usage), each expressed uniformly as icon + value (no text
* labels); the reply timestamp and the copy buttons sit at the end (the first copies this turn's
* assistant text as the Markdown it was written in, falling back to the stats themselves when
* there's no text; the second copies the same words as plain text and appears only when there is
* a reply body to strip), with the fork action to the right of copy — forking asks for
* confirmation first (it duplicates the conversation into a new Session up to this reply), while
* copy stays a plain immediate click. Below sm the row is slimmed
* to fit the width: the TPS chip is dropped and cost/elapsed lose excess decimals (compact
* formatter variants); desktop shows all five, formatted as always.
* At ≥sm the whole line is invisible but takes up space by default, surfacing only on hovering
* the reply or the line itself — matching the same convention, font size, and color as the user
* message footer: the AI's footer sits bottom-left, the user's sits bottom-right, symmetric on
* both sides. Below sm the line is always visible: phones have no hover (Tailwind v4 even gates
* hover: variants behind `@media (hover: hover)`), so a hover-revealed footer would simply
* never appear there.
* Arrow direction reads as "where the tokens go": **up arrow = input** (sent up to the model),
* **down arrow = output** (returned by the model).
* This line only answers "how much did this turn cost", **it doesn't break down the cache
* composition** — how much of the input hit cache is a debugging-level detail that belongs to the
* Trace page's turn card (which shows cache hits and hit rate); the current **context usage** is
* likewise not repeated here — it's expressed by the ring below the input box (see ContextGauge
* in context-gauge), which also discloses what that context is made of.
* This line sits right below this turn's AI reply and simultaneously **serves as that reply's
* footer**: it provides the reply timestamp and copy button at the end, so the assistant message
* itself doesn't render a separate one (otherwise two copy buttons would pop up in the same spot).
*/
import { useState } from "react";
import type { TracePosition } from "@adelie/server/api";
import {
ConfirmModal,
CopyButton,
ICONS,
Spinner,
StatChip,
markdownToPlainText,
} from "@adelie/ui";
import { formatTaskStats } from "../../lib/omni/task-stats";
import type { TaskStats } from "../../lib/omni/task-stats";
import {
formatMessageTime,
formatMoney,
formatTps,
humanizeDuration,
humanizeTokens,
} from "../../lib/format";
import { STAT_ICONS } from "../../lib/stat-icons";
import { refusalText } from "../../lib/api-error";
import { S } from "../../lib/strings";
import { useTheme } from "../../state/theme";
import { useLocale } from "../../state/locale";
export interface ForkTarget {
assistantText: string;
atMs?: number;
position?: TracePosition;
}
export function TaskStatsLine({
stats,
assistantText,
cost,
atMs,
forkPosition,
onFork,
}: {
/** This turn's stats; `null` = no token_usage for this turn (reply was aborted) -> only the timestamp and copy are shown, no stat numbers drawn. */
stats: TaskStats | null;
/** This turn's assistant text (the copy target); falls back to the stats when empty. */
assistantText?: string;
/** This turn's cost (USD); null = no pricing configured, cost isn't shown. */
cost?: number | null;
/** Timestamp of this turn's AI reply (this line is that reply's footer). */
atMs?: number;
forkPosition?: TracePosition;
onFork?: (target: ForkTarget) => Promise<void>;
}) {
const { currency } = useTheme();
const { locale } = useLocale();
const [forking, setForking] = useState(false);
// Fork is gated behind an explicit confirmation (review request): duplicating a whole
// conversation is a heavier action than the neighboring copy button suggests, so a stray
// click must not silently create a Session. Plain copy stays unconfirmed.
const [confirmingFork, setConfirmingFork] = useState(false);
// Why the last fork attempt did not happen. A refused fork leaves the conversation exactly
// as it was, so the question stays up and the reason lands inside it: the same rule the
// other confirm dialogs follow (7.19 / 7.20), instead of a toast that dismisses itself over
// the dialog the user is still reading. Cleared whenever the dialog is (re)opened.
const [forkRefusal, setForkRefusal] = useState<string | null>(null);
// This turn's usage (not a context snapshot): input = this Task's cached + uncached input.
const b = stats?.tokensByBucket;
const input = b ? b.cacheRead + b.cacheWrite : 0;
// Computed at click time (CopyButton takes a getter): the reply text, or the formatted
// stats line when there's no reply body.
const copyText = () =>
assistantText?.trim() || stats === null
? (assistantText ?? "")
: formatTaskStats(stats, {
stats: S.chat.statsLabel,
input: S.chat.statInput,
cached: S.chat.statCached,
output: S.chat.statOutput,
parenOpen: S.chat.statParenOpen,
parenClose: S.chat.statParenClose,
});
/**
* Runs the fork the dialog asks about, and settles the dialog from the answer: a fork that
* happened closes it (the caller has already navigated to the new Session), one that did not
* keeps it open with the reason. `onFork` rejects rather than toasting precisely so this
* decision can be made here.
*/
const forkNow = async (): Promise<void> => {
if (!onFork) return;
setForking(true);
setForkRefusal(null);
try {
await onFork({
assistantText: assistantText ?? "",
...(atMs !== undefined ? { atMs } : {}),
...(forkPosition !== undefined ? { position: forkPosition } : {}),
});
setConfirmingFork(false);
} catch (err) {
setForkRefusal(refusalText(err));
} finally {
setForking(false);
}
};
// ≥sm: invisible but **space-reserved** by default (sm:opacity-0, not hidden) — the user
// footer's hover-reveal convention; because the space is always reserved, appearing never
// pushes content below it down. Below sm the line is ALWAYS visible: hover doesn't exist on
// touch screens (and Tailwind v4 scopes hover: variants to `@media (hover: hover)`), so the
// hover gating made the stats unreachable on phones.
// One line always (no flex-wrap): with the fixed h-5, wrapped chips used to paint over the
// content below on phones. Below sm the row is slimmed to FIT rather than scroll: the TPS
// chip (the least decision-relevant number here) is dropped, the cost/elapsed values lose
// excess decimals, and the chip gap tightens one step — the en timestamp + priced row was
// otherwise ~7px over a 390 viewport. The sideways scroll (hidden scrollbar) stays only as
// the fallback for extreme values; at ≥sm everything fits and the scroll container is inert.
// The copy button sits outside the scrollable span, so it stays pinned at the row's end
// instead of scrolling out of reach.
return (
<div className="-mt-2 flex h-5 items-center justify-start gap-x-2 overflow-hidden whitespace-nowrap text-xs text-fg-subtle transition-opacity duration-150 group-hover:opacity-100 focus-within:opacity-100 sm:gap-x-3 sm:opacity-0">
<span className="no-scrollbar flex min-w-0 items-center gap-x-2 overflow-x-auto sm:gap-x-3">
{/* Timestamp leads: it's this reply's identity (when it was said), the stat numbers are an
annotation. When this turn has no token_usage (reply was aborted), only the timestamp
and copy remain — nothing is fabricated for what wasn't measured. */}
{atMs !== undefined && <span>{formatMessageTime(atMs, locale)}</span>}
{stats && b && (
<>
<StatChip
glyph={STAT_ICONS.input}
value={humanizeTokens(input)}
label={S.chat.statInput}
/>
<StatChip
glyph={STAT_ICONS.output}
value={humanizeTokens(b.output)}
label={S.chat.statOutput}
/>
<StatChip
glyph={STAT_ICONS.tps}
value={formatTps(stats.outputTps)}
label={S.chat.statTps}
wideOnly
/>
{cost != null && (
<StatChip
glyph={STAT_ICONS.cost}
value={formatMoney(cost, currency)}
compactValue={formatMoney(cost, currency, { compact: true })}
label={`${S.common.cost}(${currency})`}
/>
)}
<StatChip
glyph={STAT_ICONS.elapsed}
value={humanizeDuration(stats.elapsedDeltaMs)}
compactValue={humanizeDuration(stats.elapsedDeltaMs, { compact: true })}
label={S.chat.statElapsed}
/>
</>
)}
</span>
{/* Pinned at the row's end, outside the scrollable stats span. Two copies, because a
reply travels to two kinds of destination: the Markdown as it was written (the copy
glyph), and the same words with the markup taken out (plain text) — for pasting where
`##` and `**` would otherwise show up literally. They are told apart by their glyphs,
since two copy icons side by side would name neither. */}
<CopyButton text={copyText} label={S.chat.copyReply} size="sm" className="shrink-0" />
{assistantText?.trim() && (
<CopyButton
text={() => markdownToPlainText(assistantText)}
label={S.chat.copyReplyPlain}
glyph={ICONS.textLines}
size="sm"
className="shrink-0"
/>
)}
{/* Fork sits to the RIGHT of copy (review request): copy is the frequent action and
keeps its accustomed spot; the click only opens the confirmation below — the fork
request fires on Confirm, never directly. */}
{onFork && assistantText?.trim() && (
<>
<button
type="button"
disabled={forking}
data-tooltip={S.chat.forkSession}
aria-label={S.chat.forkSession}
onClick={() => {
setForkRefusal(null);
setConfirmingFork(true);
}}
className="flex h-5 w-5 shrink-0 items-center justify-center rounded transition-colors duration-150 hover:bg-gray-100 hover:text-gray-600 disabled:cursor-wait disabled:opacity-50 dark:hover:bg-gray-800 dark:hover:text-gray-300"
>
{forking ? (
<Spinner size="sm" label={S.common.loading} />
) : (
<svg
width="13"
height="13"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
strokeWidth="1.7"
strokeLinecap="round"
strokeLinejoin="round"
aria-hidden
>
<circle cx="6" cy="5" r="2" />
<circle cx="18" cy="7" r="2" />
<circle cx="6" cy="19" r="2" />
<path d="M6 7v8M8 11h4a6 6 0 0 0 6-2" />
</svg>
)}
</button>
<ConfirmModal
open={confirmingFork}
title={S.chat.forkSession}
tone="primary"
confirmLabel={S.chat.forkSessionConfirmAction}
cancelLabel={S.common.cancel}
busy={forking}
refusal={forkRefusal === null ? undefined : S.common.refused(forkRefusal)}
onClose={() => setConfirmingFork(false)}
onConfirm={() => void forkNow()}
>
<p className="text-sm text-gray-600 dark:text-gray-300">
{S.chat.forkSessionConfirmBody}
</p>
</ConfirmModal>
</>
)}
</div>
);
}