diff --git a/src/Sessions.i18n.ts b/src/Sessions.i18n.ts index baca5547..fbdb194b 100644 --- a/src/Sessions.i18n.ts +++ b/src/Sessions.i18n.ts @@ -3,9 +3,9 @@ import { messages } from "@/i18n"; /** * 会话页的文案。 * - * 费用那一格的限定语和概览页说的是同一批事(估算、无法计价、无用量、 - * 订阅),措辞跟着 `Dashboard.i18n.ts` 走 —— 同一件事在两页上不该有两种 - * 说法。区别只在量词:概览按请求数,这里按轮次。 + * 费用那一格的说明和概览页说的是同一批事(估算、无法计价、无用量), + * 措辞跟着 `Dashboard.i18n.ts` 走 —— 同一件事在两页上不该有两种说法。 + * 区别只在量词:概览按请求数,这里按轮次。 */ export const sessionsText = messages( { @@ -22,24 +22,23 @@ export const sessionsText = messages( cacheSavings: "缓存节省", cost: "费用", - /** 轮次一列里跟在总轮数后面的失败数 */ - failedTurns: (n: number) => `${n} 失败`, + /** 组头上失败数的悬停说明。格子里只有红点和数字 */ + failedTip: (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: - "这些轮次没有用量数据:上游未报告,或连接在报告之前已结束。费用无法计算,未计入合计", + unpricedTurnsTip: (n: number) => `${n} 轮所用的模型未定价,费用未计入合计`, + noUsageTurnsTip: (n: number) => + `${n} 轮没有用量数据:上游未报告,或连接在报告之前已结束。这些轮次的费用无法计算,未计入合计`, // 详情 /** `at` 是开始时刻写出来的样子 */ @@ -74,7 +73,8 @@ export const sessionsText = messages( cacheSavings: "Cache savings", cost: "Cost", - failedTurns: (n: number) => `${n} failed`, + failedTip: (n: number) => + n === 1 ? "1 turn in this session failed." : `${n} turns in this session failed.`, // 和 format.i18n.ts 的单位写法一致:s、min、h seconds: (n: number) => `${n} s`, @@ -85,14 +85,14 @@ export const sessionsText = messages( `${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.", + unpricedTurnsTip: (n: number) => + n === 1 + ? "1 turn used a model with no price, so its cost is not included in the total." + : `${n} turns used models with no price, so their cost is not included in the total.`, + noUsageTurnsTip: (n: number) => + n === 1 + ? "1 turn has no usage data: the upstream did not report it, or the connection ended before it was reported. Its cost cannot be calculated and is not included in the total." + : `${n} 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.`, detailTitle: (at: string, turns: number, duration: string) => `Session at ${at} · ${turns === 1 ? "1 turn" : `${turns} turns`} · ${duration}`, diff --git a/src/traffic/SessionCost.tsx b/src/traffic/SessionCost.tsx index 39143725..0e3557b3 100644 --- a/src/traffic/SessionCost.tsx +++ b/src/traffic/SessionCost.tsx @@ -1,43 +1,40 @@ import { Tip } from "@/ui/tip"; import { useText } from "@/i18n"; -import { usd, type SessionView } from "@/types"; +import type { SessionView } from "@/types"; import { sessionsText } from "@/Sessions.i18n"; +import { costCell } from "./costCell"; /** - * 一次会话的花费。 + * 一次会话的费用,在组头上。**一行,一个数**:写什么见 `costCell`。 * - * **三态**:有价格的加起来,没价格的单独说,一轮都没有价格时 - * 不显示 $0 —— 那是在撒谎。 + * 有说明的金额带虚线下划线,和请求行里估算的金额同一个记号;「无法计价」 + * 本来就淡一档,悬停照样有说明。 */ export function SessionCost({ s }: { s: SessionView }) { - const t = useText(sessionsText); - if (s.priced_turns === 0) { - return ( - - {t.unpriced} - - ); - } + const c = costCell(s, useText(sessionsText)); + if (c.notes.length === 0) return <>{c.text}; return ( - <> - {s.cost_micros_estimated > 0 ? ( - // **估算不能冒充实测**:合计里有估算的部分,就要带着记号 - - ~{usd(s.cost_micros)} - - ) : ( - usd(s.cost_micros) - )} - {s.unpriced_turns > 0 && ( - - {t.unpricedTurns(s.unpriced_turns)} - - )} - {s.no_usage_turns > 0 && ( - - {t.noUsageTurns(s.no_usage_turns)} - - )} - + }> + + {c.text} + + + ); +} + +/** 悬停里的几句话,一句一段。气泡本身是横排的 flex,要包成一块 */ +function Lines({ lines }: { lines: string[] }) { + return ( +
+ {lines.map((l) => ( +

{l}

+ ))} +
); } diff --git a/src/traffic/SessionRow.tsx b/src/traffic/SessionRow.tsx index 38e65e3e..c479bf1c 100644 --- a/src/traffic/SessionRow.tsx +++ b/src/traffic/SessionRow.tsx @@ -19,6 +19,13 @@ import { failedIn, type Group } from "./grouping"; * 只有「上游」这一列在两个粒度上问的不是同一件事:一条请求走了哪一家, * 而一次任务可能走过好几家 —— **而那恰恰是两个视图原来都说不出的话** * (「这次任务中途切到 openrouter 了两回」)。 + * + * **组头的每一格都不比请求行宽多少。**列宽是按请求行调出来的:默认窗口 + * 下平表正好放得下,费用在最右(见 `RequestTable` 里上游那一格)。组头 + * 原来把失败数、费用的限定语、整串模型和上游都摊在一行里,归组之后表宽 + * 出一百多像素,费用被挤出视野。所以组头只写一眼要看的那部分,其余的进 + * 悬停:失败数是一个红点加数字,费用缺了轮次写成下限,名字只有第一个 + * 决定列宽。 */ export function SessionRow({ g, @@ -71,20 +78,32 @@ export function SessionRow({ {open ? : } {t.turnCount(turns)} + {/* + 失败数:红点加数字,和请求行里失败那一条是同一个红点。「几轮 + 失败」这几个字进悬停 —— 写成「1 失败」要 42px,英文「1 failed」 + 要 50px,红点加数字是 22px。 + */} {failed > 0 && ( - - {ts.failedTurns(failed)} - + + + + {failed} + {ts.failedTip(failed)} + + )} {when(started)} {showClient && {s?.client ?? rows[0]?.client}} - - {s?.models.join(ts.modelSep) ?? ""} + + - - {providers.join(" · ")} + + {dur(ended - started)} @@ -104,3 +123,34 @@ export function SessionRow({ ); } + +/** + * 组头里的一串名字(模型、上游)。**决定列宽的只有第一项。** + * + * 一次任务用过三个模型、走过四个上游很常见,连起来写有两三百像素 —— + * 让它撑列宽,费用就被挤出视野。所以第一项完整显示(和请求行的模型 + * 一样最多 13rem),分隔符跟在它后面;其余的那段宽度记作 0 + * (`w-0 flex-1`),这一列有多宽就显示多少,最少留出一个省略号: + * 窄的时候是「claude-sonnet-5、…」,宽的时候整串都在。悬停看全。 + * + * `whitespace-pre` 是为了留住「 · 」两边的空格 —— 两段各是一个块, + * 行尾和行首的空格会被吞掉。 + */ +function Names({ items, sep }: { items: string[]; sep: string }) { + const [first, ...rest] = items; + if (first === undefined) return null; + return ( + +
+ + {rest.length > 0 ? first + sep : first} + + {rest.length > 0 && ( + + {rest.join(sep)} + + )} +
+
+ ); +} diff --git a/src/traffic/costCell.test.ts b/src/traffic/costCell.test.ts new file mode 100644 index 00000000..b496d506 --- /dev/null +++ b/src/traffic/costCell.test.ts @@ -0,0 +1,71 @@ +import { describe, expect, it } from "vitest"; +import { sessionsText } from "@/Sessions.i18n"; +import type { SessionView } from "@/types"; +import { costCell } from "./costCell"; + +const t = sessionsText.zh; + +/** 一次十轮、全部按价目表算出价格的会话;各条用例只改相关的那几个数 */ +const session = (x: Partial): SessionView => ({ + id: "s", + client: "claude-code", + started_ms: 0, + ended_ms: 60_000, + turns: 10, + cost_micros: 233_000, + cost_micros_estimated: 0, + priced_turns: 10, + unpriced_turns: 0, + no_usage_turns: 0, + input_tokens: 0, + output_tokens: 0, + cache_read_tokens: 0, + cache_write_tokens: 0, + cache_saved_micros: 0, + peak_input_tokens: 0, + models: ["claude-sonnet-5"], + errors: 0, + ...x, +}); + +describe("会话费用那一格", () => { + it("全部算出价格时只有金额,没有悬停", () => { + expect(costCell(session({}), t)).toEqual({ text: "$0.233", muted: false, notes: [] }); + }); + + /** + * 这条是改成一行的理由:限定语原来跟在金额后面,一格 150px。缺了轮次 + * 就是下限,缺几轮、为什么缺进悬停。 + */ + it("合计缺了轮次时写成下限", () => { + const c = costCell(session({ priced_turns: 9, unpriced_turns: 1 }), t); + expect(c.text).toBe("≥$0.233"); + expect(c.notes).toEqual([t.unpricedTurnsTip(1)]); + expect(costCell(session({ priced_turns: 8, no_usage_turns: 2 }), t).text).toBe("≥$0.233"); + }); + + it("有估算的部分时带记号,悬停写出估算了多少", () => { + const c = costCell(session({ cost_micros: 3_412_000, cost_micros_estimated: 61_000 }), t); + expect(c.text).toBe("~$3.41"); + expect(c.notes).toEqual([t.estimatedTip("$0.061")]); + }); + + it("既有估算又缺了轮次时两个记号都在", () => { + const c = costCell( + session({ cost_micros_estimated: 5_000, priced_turns: 9, unpriced_turns: 1 }), + t, + ); + expect(c.text).toBe("≥~$0.233"); + expect(c.notes).toEqual([t.estimatedTip("$0.0050"), t.unpricedTurnsTip(1)]); + }); + + it("一轮都没有价格时写「无法计价」,不写 $0", () => { + const c = costCell( + session({ cost_micros: 0, priced_turns: 0, unpriced_turns: 8, no_usage_turns: 2 }), + t, + ); + expect(c.text).toBe(t.unpriced); + expect(c.muted).toBe(true); + expect(c.notes).toEqual([t.noPricedTurnsTip, t.unpricedTurnsTip(8), t.noUsageTurnsTip(2)]); + }); +}); diff --git a/src/traffic/costCell.ts b/src/traffic/costCell.ts new file mode 100644 index 00000000..75e3c9d1 --- /dev/null +++ b/src/traffic/costCell.ts @@ -0,0 +1,44 @@ +import type { sessionsText } from "@/Sessions.i18n"; +import { usd, type SessionView } from "@/types"; + +type Text = (typeof sessionsText)["zh"]; + +/** + * 一次会话的费用那一格写什么。**一个数或一个词,说明进悬停。** + * + * **三态**:有价格的加起来,没价格的单独说,一轮都没有价格时不显示 + * $0 —— 那是在撒谎。 + * + * **合计里缺了轮次,金额就写成下限(「≥$0.233」)。**缺的是无法计价和 + * 没有用量的那几轮:它们产生了费用,只是算不出来。缺几轮、为什么缺写在 + * 悬停里。原来这些限定语跟在金额后面(「$0.233 +1 轮无法计价」),这一格 + * 因此有 150px,归组表比默认窗口宽出一截,被挤出视野的正是费用这一列。 + * + * **纯的,因为「什么时候写成下限」要能被测。** + */ +export function costCell( + s: SessionView, + t: Text, +): { + text: string; + /** 没有金额可写(一轮都没有价格),淡一档 */ + muted: boolean; + /** 悬停里的几句话,一句一段。空的就没有悬停 */ + notes: string[]; +} { + // 合计之外的轮次,各说各的 + const outside = [ + ...(s.unpriced_turns > 0 ? [t.unpricedTurnsTip(s.unpriced_turns)] : []), + ...(s.no_usage_turns > 0 ? [t.noUsageTurnsTip(s.no_usage_turns)] : []), + ]; + if (s.priced_turns === 0) { + return { text: t.unpriced, muted: true, notes: [t.noPricedTurnsTip, ...outside] }; + } + // **估算不能冒充实测**:合计里有估算的部分,就要带着记号 + const estimated = s.cost_micros_estimated > 0; + return { + text: (outside.length > 0 ? "≥" : "") + (estimated ? "~" : "") + usd(s.cost_micros), + muted: false, + notes: [...(estimated ? [t.estimatedTip(usd(s.cost_micros_estimated))] : []), ...outside], + }; +}