feat(webui): visualize context usage by logical round (#5649)
* feat(webui): visualize per-request context reuse * refactor(webui): report usage by logical round * fix(webui): align context chart with logical rounds * refactor(webui): derive turn usage from logical rounds * fix(webui): polish context usage details
This commit is contained in:
1 parent
8fb7bc34ec
commit
6b82d642fc
35 files changed
+994
-274
No files matched your search
@@ -36,7 +36,6 @@ import { cn } from "@/lib/utils";
|
||||
import { copyTextToClipboard } from "@/lib/clipboard";
|
||||
import {
|
||||
fmtDateTime,
|
||||
formatCompactTokenCount,
|
||||
formatMessageEndTime,
|
||||
} from "@/lib/format";
|
||||
import { toMediaAttachment } from "@/lib/media";
|
||||
@@ -54,7 +53,6 @@ import type {
|
||||
UIMessage,
|
||||
MessageDeliveryErrorKind,
|
||||
MessageDeliveryStatus,
|
||||
TurnUsage,
|
||||
} from "@/lib/types";
|
||||
|
||||
interface MessageBubbleProps {
|
||||
@@ -181,71 +179,6 @@ function MessageCopyButton({ content }: { content: string }) {
|
||||
);
|
||||
}
|
||||
|
||||
function compactDuration(milliseconds: number): string {
|
||||
const seconds = milliseconds / 1_000;
|
||||
if (seconds < 10) return `${seconds.toFixed(1)}s`;
|
||||
if (seconds < 60) return `${Math.round(seconds)}s`;
|
||||
const minutes = Math.floor(seconds / 60);
|
||||
return `${minutes}m ${Math.round(seconds % 60)}s`;
|
||||
}
|
||||
|
||||
function TurnUsageMeta({
|
||||
usage,
|
||||
latencyMs,
|
||||
}: {
|
||||
usage: TurnUsage;
|
||||
latencyMs?: number;
|
||||
}) {
|
||||
const { t } = useTranslation();
|
||||
const prompt = usage.prompt_tokens;
|
||||
const completion = usage.completion_tokens;
|
||||
const approximate = (usage.estimated_tokens ?? 0) > 0 ? "~" : "";
|
||||
const parts: string[] = [];
|
||||
if (typeof prompt === "number") parts.push(`${approximate}${formatCompactTokenCount(prompt)} in`);
|
||||
if (typeof completion === "number") parts.push(`${approximate}${formatCompactTokenCount(completion)} out`);
|
||||
if (
|
||||
typeof usage.cached_tokens === "number"
|
||||
&& typeof prompt === "number"
|
||||
&& prompt > 0
|
||||
) {
|
||||
parts.push(`${Math.round(Math.min(1, usage.cached_tokens / prompt) * 100)}% cached`);
|
||||
}
|
||||
if (typeof latencyMs === "number" && latencyMs >= 0) parts.push(compactDuration(latencyMs));
|
||||
if (parts.length === 0) return null;
|
||||
|
||||
const details: string[] = [];
|
||||
if (approximate) {
|
||||
details.push(t("message.usage.estimated", { defaultValue: "Includes estimated usage" }));
|
||||
}
|
||||
const usageMeta = (
|
||||
<span
|
||||
data-turn-usage
|
||||
tabIndex={details.length ? 0 : undefined}
|
||||
className={cn(
|
||||
"text-[11px] leading-none text-muted-foreground/70 tabular-nums",
|
||||
details.length && "cursor-help",
|
||||
)}
|
||||
>
|
||||
{parts.join(" · ")}
|
||||
</span>
|
||||
);
|
||||
|
||||
if (details.length === 0) return usageMeta;
|
||||
|
||||
return (
|
||||
<Tooltip>
|
||||
<TooltipTrigger asChild>{usageMeta}</TooltipTrigger>
|
||||
<TooltipContent
|
||||
side="top"
|
||||
align="start"
|
||||
className="max-w-96 whitespace-nowrap"
|
||||
>
|
||||
{details.join(" · ")}
|
||||
</TooltipContent>
|
||||
</Tooltip>
|
||||
);
|
||||
}
|
||||
|
||||
function deliveryErrorCopy(
|
||||
kind: MessageDeliveryErrorKind | undefined,
|
||||
t: (key: string) => string,
|
||||
@@ -549,9 +482,8 @@ export function MessageBubble({
|
||||
&& (!empty || hasReasoning || media.length > 0);
|
||||
const assistantTimestampTitle = showAssistantTimestamp ? fmtDateTime(assistantTimestamp) : "";
|
||||
const showAutomationTrigger = showAssistantTimestamp && automationSourceLabel.length > 0;
|
||||
const showUsage = message.role === "assistant" && !!message.usage && !message.isStreaming;
|
||||
const showAssistantFooterRow =
|
||||
showCopyButton || showForkButton || showAssistantTimestamp || showUsage;
|
||||
showCopyButton || showForkButton || showAssistantTimestamp;
|
||||
const showAssistantFooterSlot =
|
||||
message.role === "assistant"
|
||||
&& (!empty || hasReasoning || media.length > 0);
|
||||
@@ -617,12 +549,6 @@ export function MessageBubble({
|
||||
<TooltipContent side="top" align="center">{forkLabel}</TooltipContent>
|
||||
</Tooltip>
|
||||
) : null}
|
||||
{showUsage ? (
|
||||
<TurnUsageMeta
|
||||
usage={message.usage!}
|
||||
latencyMs={message.latencyMs}
|
||||
/>
|
||||
) : null}
|
||||
{showAssistantTimestamp ? (
|
||||
<MessageTimestamp
|
||||
{...(showCompletedAt ? { "data-assistant-completed-at": true } : {})}
|
||||
|
||||
Reference in new issue
Block a user