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
119 changes: 119 additions & 0 deletions src/Sessions.i18n.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
import { messages } from "@/i18n";

/**
* 会话页的文案。
*
* 费用那一格的限定语和概览页说的是同一批事(估算、无法计价、无用量、
* 订阅),措辞跟着 `Dashboard.i18n.tsx` 走 —— 同一件事在两页上不该有两种
* 说法。区别只在量词:概览按请求数,这里按轮次。
*/
export const sessionsText = messages(
{
loading: "读取中…",
emptyTitle: "暂无会话记录",
emptyBody: "同一对话中的请求将聚合为会话,并显示在此处。",

// 表头
started: "开始",
client: "客户端",
turns: "轮次",
duration: "时长",
peakContext: "上下文峰值",
cacheSavings: "缓存节省",
cost: "费用",

/** 轮次一列里跟在总轮数后面的失败数 */
failedTurns: (n: number) => `${n} 失败`,

// 时长。会话短的按秒算,长的按小时算
seconds: (n: number) => `${n} 秒`,
minutes: (n: number) => `${n} 分`,
hours: (n: string) => `${n} 小时`,

// 费用那一格。**估算、无法计价、无用量、订阅各说各的**
estimatedTip: (amount: string) =>
`其中 ${amount} 为估算值:请求在响应结束前断开或中断,输出用量计至断开时;或模型的单价取自其他平台。`,
unpriced: "无法计价",
noPricedTurnsTip: "此会话中没有可计价的轮次",
unpricedTurns: (n: number) => `+${n} 轮无法计价`,
unpricedTurnsTip: "这些轮次所用的模型未定价,费用未计入合计",
noUsageTurns: (n: number) => `+${n} 轮无用量`,
noUsageTurnsTip:
"这些轮次没有用量数据:上游未报告,或连接在报告之前已结束。费用无法计算,未计入合计",
subscriptionTurns: (n: number) => `订阅额度 ${n} 轮`,
subscriptionTip: "这些轮次由订阅制上游服务,计入订阅额度,不按用量产生费用",

// 详情
/** `at` 是开始时刻写出来的样子 */
detailTitle: (at: string, turns: number, duration: string) =>
`${at} 的会话 · ${turns} 轮 · ${duration}`,
/** 模型名之间的分隔符 */
modelSep: "、",
detailUsage: (models: string, input: string, output: string, cacheRead: string) =>
`${models} · 输入 ${input} / 输出 ${output} · 缓存读取 ${cacheRead}`,

growthTitle: "上下文增长(每轮的输入 token)",
/** 柱子的悬浮文字。`cached` 是缓存命中的那一段,没有时为 null */
growthBar: (shown: string, _n: number, cached: string | null) =>
`${shown} token${cached ? `,其中 ${cached} 为缓存命中` : ""}`,
growthLegend: (peak: string) => `绿色为缓存命中部分。峰值 ${peak} token。`,

waterfallTitle: "每轮费用",
/** 瀑布里单独一轮的标注 */
turnSubscription: "订阅额度",
turnFailed: "失败",
turnCancelled: "已取消",
},
{
loading: "Loading…",
emptyTitle: "No sessions yet",
emptyBody: "Requests from the same conversation are grouped into a session and appear here.",

started: "Started",
client: "Client",
turns: "Turns",
duration: "Duration",
peakContext: "Peak context",
cacheSavings: "Cache savings",
cost: "Cost",

failedTurns: (n: number) => `${n} failed`,

// 和 format.i18n.ts 的单位写法一致:s、min、h
seconds: (n: number) => `${n} s`,
minutes: (n: number) => `${n} min`,
hours: (n: string) => `${n} h`,

estimatedTip: (amount: string) =>
`${amount} of this is estimated: requests disconnected or were interrupted before the response finished, and output usage is counted up to the disconnect; or the model's price was taken from another platform.`,
unpriced: "Unpriced",
noPricedTurnsTip: "No turn in this session could be priced.",
unpricedTurns: (n: number) => `+${n} unpriced`,
unpricedTurnsTip:
"The models used by these turns have no price, so their cost is not included in the total.",
// 费用那一格在默认窗口宽度下只有 ~340px,几个限定语要挤在一行里:
// 单位交给「轮次」那一列,这里只写数目
noUsageTurns: (n: number) => `+${n} no usage`,
noUsageTurnsTip:
"These turns have no usage data: the upstream did not report it, or the connection ended before it was reported. Their cost cannot be calculated and is not included in the total.",
subscriptionTurns: (n: number) => `${n} on subscription`,
subscriptionTip:
"These turns were served by a subscription upstream. They count against the subscription quota and do not incur cost by usage.",

detailTitle: (at: string, turns: number, duration: string) =>
`Session at ${at} · ${turns === 1 ? "1 turn" : `${turns} turns`} · ${duration}`,
modelSep: ", ",
detailUsage: (models: string, input: string, output: string, cacheRead: string) =>
`${models} · Input ${input} / Output ${output} · Cache reads ${cacheRead}`,

growthTitle: "Context growth (input tokens per turn)",
growthBar: (shown: string, n: number, cached: string | null) =>
`${shown} ${n === 1 ? "token" : "tokens"}${cached ? `, ${cached} from cache` : ""}`,
growthLegend: (peak: string) => `Green marks the part served from cache. Peak ${peak} tokens.`,

waterfallTitle: "Cost per turn",
turnSubscription: "Subscription",
turnFailed: "Failed",
turnCancelled: "Canceled",
},
);
81 changes: 46 additions & 35 deletions src/Sessions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { usd, type SessionDetail, type SessionView, type TurnView } from "./type
import { Empty, EmptyDescription, EmptyHeader, EmptyTitle } from "@/ui/empty";
import { toast } from "sonner";
import { useCoreEvent } from "./useCoreEvent";
import { textOf, useText } from "@/i18n";
import { sessionsText } from "./Sessions.i18n";
import {
Table,
TableBody,
Expand All @@ -31,6 +33,7 @@ import {
* 总额**。一个会撒谎的成本面板不如没有。
*/
export default function Sessions() {
const t = useText(sessionsText);
const [rows, setRows] = useState<SessionView[] | null>(null);
const [open, setOpen] = useState<SessionDetail | null>(null);
const [error, setError] = useState<string | null>(null);
Expand All @@ -53,16 +56,16 @@ export default function Sessions() {
// 原来每 10 秒重算一遍,空闲时每一遍都算出同一个答案。
useCoreEvent(["request_finished", "request_failed", "request_cancelled"], () => void load());

if (!rows) return <div className="p-5 tw-head text-muted-foreground">{error ?? "读取中…"}</div>;
if (!rows) return <div className="p-5 tw-head text-muted-foreground">{error ?? t.loading}</div>;

if (rows.length === 0) {
// 空状态永远在回答「接下来该做什么」
return (
<div className="p-5">
<Empty>
<EmptyHeader>
<EmptyTitle>暂无会话记录</EmptyTitle>
<EmptyDescription>同一对话中的请求将聚合为会话,并显示在此处。</EmptyDescription>
<EmptyTitle>{t.emptyTitle}</EmptyTitle>
<EmptyDescription>{t.emptyBody}</EmptyDescription>
</EmptyHeader>
</Empty>
</div>
Expand All @@ -74,13 +77,13 @@ export default function Sessions() {
<Table>
<TableHeader>
<TableRow>
<TableHead className="font-normal">开始</TableHead>
<TableHead className="font-normal">客户端</TableHead>
<TableHead className="text-right font-normal">轮次</TableHead>
<TableHead className="text-right font-normal">时长</TableHead>
<TableHead className="text-right font-normal">上下文峰值</TableHead>
<TableHead className="text-right font-normal">缓存节省</TableHead>
<TableHead className="text-right font-normal">费用</TableHead>
<TableHead className="font-normal">{t.started}</TableHead>
<TableHead className="font-normal">{t.client}</TableHead>
<TableHead className="text-right font-normal">{t.turns}</TableHead>
<TableHead className="text-right font-normal">{t.duration}</TableHead>
<TableHead className="text-right font-normal">{t.peakContext}</TableHead>
<TableHead className="text-right font-normal">{t.cacheSavings}</TableHead>
<TableHead className="text-right font-normal">{t.cost}</TableHead>
</TableRow>
</TableHeader>
<TableBody>
Expand All @@ -101,7 +104,7 @@ export default function Sessions() {
{/* 轮次和失败数之间要有间隔 —— 挨着写会读成「181 失败」 */}
<span>{s.turns}</span>
{s.errors > 0 && (
<span className="ml-2 text-red-600 dark:text-red-400">{s.errors} 失败</span>
<span className="ml-2 text-red-600 dark:text-red-400">{t.failedTurns(s.errors)}</span>
)}
</TableCell>
<TableCell className="text-right">{dur(s.ended_ms - s.started_ms)}</TableCell>
Expand Down Expand Up @@ -132,17 +135,18 @@ export default function Sessions() {
* N 次」,这里用同一个说法。
*/
function Cost({ s }: { s: SessionView }) {
const t = useText(sessionsText);
const subscription = s.subscription_turns > 0 && (
<Tip text="这些轮次由订阅制上游服务,计入订阅额度,不按用量产生费用">
<span className="text-muted-foreground">订阅额度 {s.subscription_turns} 轮</span>
<Tip text={t.subscriptionTip}>
<span className="text-muted-foreground">{t.subscriptionTurns(s.subscription_turns)}</span>
</Tip>
);
if (s.priced_turns === 0) {
// 全走订阅的会话没有金额可写,但也没有缺什么
if (subscription && s.unpriced_turns === 0 && s.no_usage_turns === 0) return subscription;
return (
<>
<Tip text="此会话中没有可计价的轮次"><span className="text-muted-foreground">无法计价</span></Tip>
<Tip text={t.noPricedTurnsTip}><span className="text-muted-foreground">{t.unpriced}</span></Tip>
{subscription && <Also>{subscription}</Also>}
</>
);
Expand All @@ -151,20 +155,20 @@ function Cost({ s }: { s: SessionView }) {
<>
{s.cost_micros_estimated > 0 ? (
// **估算不能冒充实测**:合计里有估算的部分,就要带着记号
<Tip text={`其中 ${usd(s.cost_micros_estimated)} 为估算值:请求在响应结束前断开或中断,输出用量计至断开时;或模型的单价取自其他平台。`}>
<Tip text={t.estimatedTip(usd(s.cost_micros_estimated))}>
<span className="underline decoration-dotted underline-offset-2">~{usd(s.cost_micros)}</span>
</Tip>
) : (
usd(s.cost_micros)
)}
{s.unpriced_turns > 0 && (
<Tip text="这些轮次所用的模型未定价,费用未计入合计">
<span className="ml-1 text-muted-foreground">+{s.unpriced_turns} 轮无法计价</span>
<Tip text={t.unpricedTurnsTip}>
<span className="ml-1 text-muted-foreground">{t.unpricedTurns(s.unpriced_turns)}</span>
</Tip>
)}
{s.no_usage_turns > 0 && (
<Tip text="这些轮次没有用量数据:上游未报告,或连接在报告之前已结束。费用无法计算,未计入合计">
<span className="ml-1 text-muted-foreground">+{s.no_usage_turns} 轮无用量</span>
<Tip text={t.noUsageTurnsTip}>
<span className="ml-1 text-muted-foreground">{t.noUsageTurns(s.no_usage_turns)}</span>
</Tip>
)}
{subscription && <Also>{subscription}</Also>}
Expand Down Expand Up @@ -193,9 +197,11 @@ function when(ms: number) {
}

function dur(ms: number) {
if (ms < 60_000) return `${Math.round(ms / 1000)} 秒`;
if (ms < 3_600_000) return `${Math.round(ms / 60_000)} 分`;
return `${(ms / 3_600_000).toFixed(1)} 小时`;
// 取文案要在调用的那一刻,不能提到模块级 —— 换了语言它不会跟着换
const t = textOf(sessionsText);
if (ms < 60_000) return t.seconds(Math.round(ms / 1000));
if (ms < 3_600_000) return t.minutes(Math.round(ms / 60_000));
return t.hours((ms / 3_600_000).toFixed(1));
}

function tokens(n: number) {
Expand All @@ -205,18 +211,23 @@ function tokens(n: number) {

/** 一次会话的细节:上下文增长曲线 + 每轮的成本瀑布。 */
function Detail({ d, onClose }: { d: SessionDetail; onClose: () => void }) {
const t = useText(sessionsText);
const { session: s, turns } = d;
return (
<Dialog open onOpenChange={(o) => !o && onClose()}>
<DialogContent className="max-h-[85vh] overflow-auto sm:max-w-3xl">
<DialogHeader>
<DialogTitle>
{when(s.started_ms)} 的会话 · {s.turns} 轮 · {dur(s.ended_ms - s.started_ms)}
{t.detailTitle(when(s.started_ms), s.turns, dur(s.ended_ms - s.started_ms))}
</DialogTitle>
</DialogHeader>
<div className="mt-1 tw-body text-muted-foreground">
{s.models.join("、")} · 输入 {tokens(s.input_tokens)} / 输出 {tokens(s.output_tokens)} ·
缓存读取 {tokens(s.cache_read_tokens)}
{t.detailUsage(
s.models.join(t.modelSep),
tokens(s.input_tokens),
tokens(s.output_tokens),
tokens(s.cache_read_tokens),
)}
</div>

<Growth turns={turns} />
Expand All @@ -233,10 +244,11 @@ function Detail({ d, onClose }: { d: SessionDetail; onClose: () => void }) {
* 那个东西 —— 折线会把那一跳平滑掉一部分。
*/
function Growth({ turns }: { turns: TurnView[] }) {
const text = useText(sessionsText);
const max = Math.max(1, ...turns.map((t) => t.input_tokens ?? 0));
return (
<section className="mt-4">
<div className="tw-body text-muted-foreground">上下文增长(每轮的输入 token)</div>
<div className="tw-body text-muted-foreground">{text.growthTitle}</div>
<div className="mt-1 flex h-16 items-end gap-px">
{turns.map((t) => {
const v = t.input_tokens ?? 0;
Expand All @@ -246,7 +258,7 @@ function Growth({ turns }: { turns: TurnView[] }) {
key={t.id}
className="flex-1 bg-neutral-200 dark:bg-neutral-700"
style={{ height: `${Math.max(2, (v / max) * 100)}%` }}
title={`${tokens(v)} token${cached > 0 ? `,其中 ${tokens(cached)} 为缓存命中` : ""}`}
title={text.growthBar(tokens(v), v, cached > 0 ? tokens(cached) : null)}
>
{/* 缓存命中的那一段单独染色 —— 看出哪一轮打断了缓存 */}
<div
Expand All @@ -257,19 +269,18 @@ function Growth({ turns }: { turns: TurnView[] }) {
);
})}
</div>
<div className="mt-1 tw-label text-neutral-400">
绿色为缓存命中部分。峰值 {tokens(max)} token。
</div>
<div className="mt-1 tw-label text-neutral-400">{text.growthLegend(tokens(max))}</div>
</section>
);
}

/** 每轮的成本瀑布 —— 找出那个 8 万 token 的文件读取。 */
function Waterfall({ turns }: { turns: TurnView[] }) {
const text = useText(sessionsText);
const max = Math.max(1, ...turns.map((t) => t.cost_micros ?? 0));
return (
<section className="mt-4">
<div className="tw-body text-muted-foreground">每轮费用</div>
<div className="tw-body text-muted-foreground">{text.waterfallTitle}</div>
<ul className="mt-1 space-y-0.5">
{turns.map((t, i) => (
<li key={t.id} className="flex items-center gap-2 tw-label">
Expand All @@ -284,16 +295,16 @@ function Waterfall({ turns }: { turns: TurnView[] }) {
<span className="w-16 text-right">
{/* **没有价格就说没有价格,不写 $0**;订阅那一轮也没有金额,但它不是没有价格 */}
{t.billing === "subscription" ? (
<span className="text-neutral-400">订阅额度</span>
<span className="text-neutral-400">{text.turnSubscription}</span>
) : t.cost_micros == null ? (
<span className="text-neutral-400">无法计价</span>
<span className="text-neutral-400">{text.unpriced}</span>
) : (
// 估算的金额要带记号:取消、断在中间的那几轮输出只计到断开时
(t.cost_estimated ? "~" : "") + usd(t.cost_micros)
)}
</span>
{t.error && <span className="text-red-600 dark:text-red-400">失败</span>}
{t.cancelled && <span className="text-neutral-400">已取消</span>}
{t.error && <span className="text-red-600 dark:text-red-400">{text.turnFailed}</span>}
{t.cancelled && <span className="text-neutral-400">{text.turnCancelled}</span>}
</li>
))}
</ul>
Expand Down
8 changes: 3 additions & 5 deletions src/i18n.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,10 @@ describe("界面语言", () => {
const CJK = /[\u3400-\u9fff\u3000-\u303f\uff00-\uffef]/;

/**
* 还没迁到词表里的文件。**只减不增**:迁完一个就从这里删掉,下面有一条
* 检查会在「已经迁完却还留在清单上」时报错。新文件一律不许进这个清单。
* 还没迁到词表里的文件。**现在是空的 —— 全都迁完了。**清单留着当守卫:
* 新写的文件一律不许进来,下面第一条检查就是那道门。
*/
const PENDING = new Set<string>([
"src/Sessions.tsx",
]);
const PENDING = new Set<string>([]);

function files(dir: string): string[] {
const out: string[] = [];
Expand Down
3 changes: 3 additions & 0 deletions src/i18n/terminology.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ known colloquialisms.
| 概览 | Overview | sidebar |
| 流量 | Traffic | sidebar; the list of requests |
| 会话 | Sessions / session | |
| 轮次 / 轮 | turns / turn | one request within a session |
| 上下文峰值 | peak context | |
| 缓存节省 | cache savings | |
| 发现 | Findings | sidebar |
| 防护 | Protection | sidebar |
| 上游 | Upstreams / upstream | |
Expand Down
Loading