Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
249 lines
7.8 KiB
TypeScript
249 lines
7.8 KiB
TypeScript
import type { Message } from "@langchain/langgraph-sdk";
|
|
import { CoinsIcon, GaugeIcon } from "lucide-react";
|
|
|
|
import { useI18n } from "@/core/i18n/hooks";
|
|
import { formatTokenCount, getUsageMetadata } from "@/core/messages/usage";
|
|
import { useLocalSettings } from "@/core/settings";
|
|
import { cn } from "@/lib/utils";
|
|
|
|
import {
|
|
MessageMetricsContext,
|
|
useMessageMetric,
|
|
useMessageMetrics,
|
|
type MessageMetric,
|
|
} from "./use-message-metrics";
|
|
|
|
const FULL_SCALE_TOKENS_PER_SEC = 80;
|
|
|
|
function classifySpeed(tps: number): { color: string; label: string } {
|
|
if (tps >= 50) return { color: "#16a34a", label: "极快" };
|
|
if (tps >= 30) return { color: "#65a30d", label: "流畅" };
|
|
if (tps >= 15) return { color: "#eab308", label: "一般" };
|
|
if (tps >= 5) return { color: "#f97316", label: "偏慢" };
|
|
return { color: "#ef4444", label: "缓慢" };
|
|
}
|
|
|
|
export function formatDuration(ms: number): string {
|
|
if (ms < 1000) return `${Math.round(ms)} ms`;
|
|
if (ms < 60_000) return `${(ms / 1000).toFixed(1)} s`;
|
|
const mins = Math.floor(ms / 60_000);
|
|
const secs = Math.round((ms % 60_000) / 1000);
|
|
return `${mins}m ${secs}s`;
|
|
}
|
|
|
|
/**
|
|
* 速度条颜色含义图例 —— 生成结束后显示在速度条下方一行,向用户解释当前
|
|
* 进度条颜色代表的「模型生成延迟」状态。进度条本身仍是 5 档(极快/流畅/一般/
|
|
* 偏慢/缓慢)的连续配色;这里把它归并成用户视角的 3 类延迟 + 「无响应」:
|
|
* 绿(#16a34a,极快/流畅) → 延迟正常
|
|
* 黄(#eab308,一般) → 延迟微长
|
|
* 红(#ef4444,偏慢/缓慢) → 延迟长,可能失败
|
|
* 灰(#9ca3af) → 无响应(无进度条/0 tok/s),任务失败,请重试
|
|
* 根据本次问答的实际速度,只显示对应那一档的提示。
|
|
*/
|
|
function legendForSpeed(tps: number): { color: string; label: string } {
|
|
if (tps <= 0) return { color: "#9ca3af", label: "无响应,任务失败,请重试" };
|
|
if (tps >= 30) return { color: "#16a34a", label: "延迟正常" };
|
|
if (tps >= 15) return { color: "#eab308", label: "延迟微长" };
|
|
return { color: "#ef4444", label: "延迟长,可能失败" };
|
|
}
|
|
|
|
export function SpeedLegend({
|
|
className,
|
|
tokensPerSec,
|
|
}: {
|
|
className?: string;
|
|
tokensPerSec: number;
|
|
}) {
|
|
const item = legendForSpeed(tokensPerSec);
|
|
return (
|
|
<span
|
|
className={cn(
|
|
"text-muted-foreground inline-flex items-center gap-1",
|
|
className,
|
|
)}
|
|
>
|
|
<span
|
|
aria-hidden
|
|
className="inline-block size-2 shrink-0 rounded-full"
|
|
style={{ backgroundColor: item.color }}
|
|
/>
|
|
{item.label}
|
|
</span>
|
|
);
|
|
}
|
|
|
|
export function SpeedPill({ metric }: { metric: MessageMetric }) {
|
|
const fill = Math.max(0, Math.min(1, metric.tokensPerSec / FULL_SCALE_TOKENS_PER_SEC));
|
|
const { color, label } = classifySpeed(metric.tokensPerSec);
|
|
const display =
|
|
metric.tokensPerSec >= 100
|
|
? Math.round(metric.tokensPerSec).toString()
|
|
: metric.tokensPerSec.toFixed(1);
|
|
const prefix = metric.fromUsage ? "" : "~";
|
|
|
|
return (
|
|
<span
|
|
className="inline-flex items-center gap-1.5"
|
|
title={`${label} · ${metric.fromUsage ? "按 output_tokens 计算" : "字符估算 ≈ 2 字符 / token"}`}
|
|
>
|
|
<GaugeIcon className="size-3" style={{ color }} />
|
|
<span className="font-mono tabular-nums" style={{ color }}>
|
|
{prefix}
|
|
{display}
|
|
</span>
|
|
<span className="text-muted-foreground">tok/s</span>
|
|
<span className="relative inline-block h-1 w-12 overflow-hidden rounded-full bg-current/15 align-middle">
|
|
<span
|
|
className="absolute inset-y-0 left-0 rounded-full transition-[width,background-color] duration-200 ease-out"
|
|
style={{ width: `${fill * 100}%`, backgroundColor: color }}
|
|
/>
|
|
</span>
|
|
</span>
|
|
);
|
|
}
|
|
|
|
export function MessageTokenUsage({
|
|
className,
|
|
enabled = false,
|
|
isLoading = false,
|
|
message,
|
|
metric,
|
|
}: {
|
|
className?: string;
|
|
enabled?: boolean;
|
|
isLoading?: boolean;
|
|
message: Message;
|
|
metric?: MessageMetric;
|
|
}) {
|
|
const { t } = useI18n();
|
|
const [settings] = useLocalSettings();
|
|
const speedEnabled = settings.display.streamSpeedMeterEnabled;
|
|
const contextMetric = useMessageMetric(message.id as string | undefined);
|
|
const resolvedMetric = metric ?? contextMetric;
|
|
|
|
if (message.type !== "ai") {
|
|
return null;
|
|
}
|
|
|
|
// Show speed-only line while streaming (no usage yet) if the user opted in,
|
|
// otherwise wait until usage arrives so the bar doesn't render on its own.
|
|
const usage = getUsageMetadata(message);
|
|
const showUsage = enabled && !isLoading && Boolean(usage);
|
|
// The speed pill is shown once per turn — only on the turn-final AI message
|
|
// — so a turn with several LLM calls no longer renders a pill per call.
|
|
const showSpeed =
|
|
speedEnabled &&
|
|
resolvedMetric !== undefined &&
|
|
resolvedMetric.durationMs > 0 &&
|
|
resolvedMetric.isTurnFinal;
|
|
|
|
if (!showUsage && !showSpeed) {
|
|
return null;
|
|
}
|
|
|
|
return (
|
|
<div
|
|
className={cn(
|
|
"text-muted-foreground border-border/60 mt-1 flex flex-wrap items-center gap-x-3 gap-y-1 border-t pt-2 text-[11px]",
|
|
className,
|
|
)}
|
|
>
|
|
{showUsage && usage ? (
|
|
<>
|
|
<span className="inline-flex items-center gap-1 font-medium">
|
|
<CoinsIcon className="size-3" />
|
|
{t.tokenUsage.label}
|
|
</span>
|
|
<span>
|
|
{t.tokenUsage.input}: {formatTokenCount(usage.inputTokens)}
|
|
</span>
|
|
<span>
|
|
{t.tokenUsage.output}: {formatTokenCount(usage.outputTokens)}
|
|
</span>
|
|
<span className="font-medium">
|
|
{t.tokenUsage.total}: {formatTokenCount(usage.totalTokens)}
|
|
</span>
|
|
</>
|
|
) : null}
|
|
{showSpeed && resolvedMetric ? (
|
|
<>
|
|
<SpeedPill metric={resolvedMetric} />
|
|
{resolvedMetric.durationMs > 0 && (
|
|
<span className="font-mono tabular-nums">
|
|
{formatDuration(resolvedMetric.durationMs)}
|
|
</span>
|
|
)}
|
|
{/* 生成结束(非流式中)后只显示与本次速度对应那一档的颜色含义说明。 */}
|
|
{!resolvedMetric.isActive && (
|
|
<SpeedLegend
|
|
className="basis-full"
|
|
tokensPerSec={resolvedMetric.tokensPerSec}
|
|
/>
|
|
)}
|
|
</>
|
|
) : null}
|
|
</div>
|
|
);
|
|
}
|
|
|
|
export function MessageTokenUsageList({
|
|
className,
|
|
enabled = false,
|
|
isLoading = false,
|
|
messages,
|
|
}: {
|
|
className?: string;
|
|
enabled?: boolean;
|
|
isLoading?: boolean;
|
|
messages: Message[];
|
|
}) {
|
|
const [settings] = useLocalSettings();
|
|
const speedEnabled = settings.display.streamSpeedMeterEnabled;
|
|
|
|
if (!enabled && !speedEnabled) {
|
|
return null;
|
|
}
|
|
|
|
const aiMessages = messages.filter((message) => message.type === "ai");
|
|
if (aiMessages.length === 0) {
|
|
return null;
|
|
}
|
|
|
|
return (
|
|
<>
|
|
{aiMessages.map((message, index) => (
|
|
<MessageTokenUsage
|
|
className={className}
|
|
enabled={enabled}
|
|
isLoading={isLoading}
|
|
key={message.id ?? index}
|
|
message={message}
|
|
/>
|
|
))}
|
|
</>
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Hoist the metrics map computation to the top of the message list so
|
|
* descendant {@link MessageTokenUsage} components can read it via context
|
|
* instead of each running its own hook.
|
|
*/
|
|
export function MessageMetricsProvider({
|
|
messages,
|
|
isLoading,
|
|
children,
|
|
}: {
|
|
messages: Message[];
|
|
isLoading: boolean;
|
|
children: React.ReactNode;
|
|
}) {
|
|
const metrics = useMessageMetrics(messages, isLoading);
|
|
return (
|
|
<MessageMetricsContext.Provider value={metrics}>
|
|
{children}
|
|
</MessageMetricsContext.Provider>
|
|
);
|
|
}
|