Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@
}

.unit {
opacity: 0.72;
font-weight: 500;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { act, render, screen } from "@testing-library/react";
import { act, render, screen, waitFor } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { afterEach, describe, expect, it, vi } from "vitest";
import { createTranslator } from "@/shared/i18n";
import { OutputTokenRate } from "./OutputTokenRate";
Expand All @@ -8,6 +9,21 @@ const t = createTranslator("zh");
afterEach(() => vi.useRealTimers());

describe("OutputTokenRate", () => {
it("keeps the compact unit visible and explains it on hover", async () => {
const user = userEvent.setup();
render(<OutputTokenRate rate={20} expiresAt={performance.now() + 2500} t={t} />);
const badge = screen.getByLabelText(t("tokenRateLabel", { rate: 20 }));

expect(badge).toHaveTextContent(/^20t\/s$/);
expect(screen.queryByRole("tooltip")).not.toBeInTheDocument();

await user.hover(badge);
expect(await screen.findByRole("tooltip")).toHaveTextContent(t("tokenRateTooltip", { rate: 20 }));

await user.unhover(badge);
await waitFor(() => expect(screen.queryByRole("tooltip")).not.toBeInTheDocument());
});

it.each([
[19.9, 20, "medium"],
[34.9, 35, "fast"],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { useEffect, useRef, useState } from "react";
import { Tooltip } from "@/components/ui";
import type { TranslateFn } from "@/models/conversations";
import styles from "./OutputTokenRate.module.css";

Expand Down Expand Up @@ -47,12 +48,18 @@ export function OutputTokenRate({ rate, expiresAt, t }: { rate?: number; expires
const accessibleLabel = t("tokenRateLabel", { rate: rounded });
const tier = rounded >= 35 ? "fast" : rounded >= 20 ? "medium" : "slow";
return (
<span className={styles.rate} data-tier={tier} title={accessibleLabel} aria-label={accessibleLabel}>
<span className={styles.pulse} aria-hidden="true" />
<span aria-hidden="true">{rounded}</span>
<span className={styles.unit} aria-hidden="true">
t/s
<Tooltip
disableHoverableContent
content={t("tokenRateTooltip", { rate: rounded })}
contentProps={{ side: "top", sideOffset: 6 }}
>
<span className={styles.rate} data-tier={tier} aria-label={accessibleLabel}>
<span className={styles.pulse} aria-hidden="true" />
<span aria-hidden="true">{rounded}</span>
<span className={styles.unit} aria-hidden="true">
t/s
</span>
</span>
</span>
</Tooltip>
);
}
2 changes: 2 additions & 0 deletions web/packages/app/src/shared/i18n/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1122,6 +1122,7 @@ export const messages = {
collaborationWorkStopFailed: "停止失败",
progressToolList: "工具调用",
tokenRateLabel: "估算输出速率为 {rate} tokens/秒",
tokenRateTooltip: "实时估算的输出速率为 {rate} tokens/秒。回复结束后,速率会隐藏。",
progressToolDetails: "第 {number} 条:{command},{status}",
progressCommand: "命令",
progressInput: "输入",
Expand Down Expand Up @@ -4797,6 +4798,7 @@ export const messages = {
collaborationWorkStopFailed: "Could not stop",
progressToolList: "Tool calls",
tokenRateLabel: "Estimated output rate: {rate} tokens per second",
tokenRateTooltip: "The estimated output rate is {rate} tokens per second. The rate disappears when the reply ends.",
progressToolDetails: "Call {number}: {command}, {status}",
progressCommand: "Command",
progressInput: "Input",
Expand Down
Loading