Message timing(消息耗时)
展示当前消息的流式统计,包括首个 token 的等待时间、总耗时和生成速度。
loading
安装
bash
npx shadcn@latest add "@assistant-ui/message-timing"1
运行时组件从 assistant-ui 运行时读取状态。如果项目还没有运行时,先初始化,再用运行时提供器包裹应用。
bash
npx assistant-ui@latest init1
tsx
import { AssistantRuntimeProvider } from "@assistant-ui/react";
import { useChatRuntime, AssistantChatTransport } from "@assistant-ui/ai-sdk";
export default function App() {
const runtime = useChatRuntime({
transport: new AssistantChatTransport({ api: "/api/chat" }),
});
return (
<AssistantRuntimeProvider runtime={runtime}>
{/* 应用组件 */}
</AssistantRuntimeProvider>
);
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
快速开始
在消息作用域读取流式统计,再选择放在操作栏或消息末尾。
读取消息用时
useMessageTiming 从当前消息读取运行时记录的时间和吞吐信息。
tsx
"use client";
import { useMessageTiming } from "@assistant-ui/react";
import {
Tooltip,
TooltipContent,
TooltipProvider,
TooltipTrigger,
} from "@/components/ui/tooltip";
import type { FC } from "react";
const formatTimingMs = (ms: number | undefined): string => {
if (ms === undefined) return "—";
if (ms < 1000) return `${Math.round(ms)}ms`;
return `${(ms / 1000).toFixed(2)}s`;
};
export const MessageTiming: FC<{
className?: string;
side?: "top" | "right" | "bottom" | "left";
}> = ({ className, side = "right" }) => {
const timing = useMessageTiming();
if (timing?.totalStreamTime === undefined) return null;
return (
<TooltipProvider>
<Tooltip>
<TooltipTrigger
render={
<button
type="button"
aria-label="消息用时"
className={className}
/>
}
>
{formatTimingMs(timing.totalStreamTime)}
</TooltipTrigger>
<TooltipContent side={side} sideOffset={8}>
{/* first token, total, speed, chunks; see Anatomy below */}
</TooltipContent>
</Tooltip>
</TooltipProvider>
);
};1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
添加到消息操作栏
在 ActionBarPrimitive.Root 内放入 MessageTiming,与复制或重新生成按钮并列。
tsx
import { ActionBarPrimitive } from "@assistant-ui/react";
import { TooltipIconButton } from "@/components/assistant-ui/elements/tooltip-icon-button";
import { MessageTiming } from "@/components/assistant-ui/elements/message-timing.aui";
function AssistantActionBar() {
return (
<ActionBarPrimitive.Root hideWhenRunning autohide="not-last">
<ActionBarPrimitive.Copy asChild>
<TooltipIconButton tooltip="Copy">{/* ... */}</TooltipIconButton>
</ActionBarPrimitive.Copy>
<MessageTiming />
</ActionBarPrimitive.Root>
);
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
结构
触发区呈现简短用时,提示区域展开首 token、总耗时和生成速度等明细。
tsx
<button aria-label="消息用时">{/* e.g. "1.24s" */}</button>
<div>
<div>First token <span>{/* only when firstTokenTime is set */}</span></div>
<div>Total <span>{/* always, once the badge is visible */}</span></div>
<div>Speed <span>{/* only when tokensPerSecond is set */}</span></div>
<div>Chunks <span>{/* always, once the badge is visible */}</span></div>
</div>1
2
3
4
5
6
7
2
3
4
5
6
7
示例
选择工具提示出现的方向,也可在自己的界面中沿用组件的格式规则。
工具提示方向
用 side 指定统计提示相对于触发器显示的位置。
tsx
<MessageTiming side="left" />1
显示格式
用时按持续时间显示。尚未获得的统计值留空,生成速度以每秒 token 数显示。
API 参考
可以从运行时读取用时,也可以给独立组件传入已知统计值。
消息状态
统计与当前消息绑定,由实际流式传输记录,静态消息可能没有这些数据。
MessageTiming 属性
设置提示的位置并传入所需类名。组件在相应消息作用域读取统计。
耗时页脚
独立布局将简短耗时放在消息末尾,使用提示展示完整统计。
sh
npx shadcn@latest add "@assistant-ui/elements-message-timing"1
tsx
"use client";
import { useMessageTiming } from "@assistant-ui/react";
import {
MessageTiming,
type TimingStat,
} from "@/components/assistant-ui/elements/message-timing";
const formatMs = (ms: number) =>
ms < 1000 ? `${Math.round(ms)}ms` : `${(ms / 1000).toFixed(2)}s`;
function TimingFooter() {
const timing = useMessageTiming();
if (!timing) return null;
const stats: TimingStat[] = [
timing.firstTokenTime !== undefined && {
label: "ttft",
value: formatMs(timing.firstTokenTime),
},
timing.totalStreamTime !== undefined && {
label: "total",
value: formatMs(timing.totalStreamTime),
},
timing.tokensPerSecond !== undefined && {
label: "tok/s",
value: timing.tokensPerSecond.toFixed(1),
},
{ label: "chunks", value: String(timing.totalChunks) },
].filter((stat): stat is TimingStat => stat !== false);
return <MessageTiming stats={stats} />;
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
tsx
import {
MessageTiming,
type TimingStat,
} from "@/components/assistant-ui/elements/message-timing";
const stats: TimingStat[] = [
{ label: "ttft", value: "0.4s" },
{ label: "total", value: "2.6s" },
{ label: "tok/s", value: "61" },
];
<MessageTiming stats={stats} />;1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
MessageTiming
独立版本从外部属性接收统计数据,适用于已有服务端记录或静态预览。