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 ( {item.label} ); } 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 ( {prefix} {display} tok/s ); } 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 (
{showUsage && usage ? ( <> {t.tokenUsage.label} {t.tokenUsage.input}: {formatTokenCount(usage.inputTokens)} {t.tokenUsage.output}: {formatTokenCount(usage.outputTokens)} {t.tokenUsage.total}: {formatTokenCount(usage.totalTokens)} ) : null} {showSpeed && resolvedMetric ? ( <> {resolvedMetric.durationMs > 0 && ( {formatDuration(resolvedMetric.durationMs)} )} {/* 生成结束(非流式中)后只显示与本次速度对应那一档的颜色含义说明。 */} {!resolvedMetric.isActive && ( )} ) : null}
); } 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) => ( ))} ); } /** * 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 ( {children} ); }