diff --git a/dashboard/src/lib/format.ts b/dashboard/src/lib/format.ts index 7198765e..42ab3af4 100644 --- a/dashboard/src/lib/format.ts +++ b/dashboard/src/lib/format.ts @@ -4,6 +4,24 @@ export function fmtUsd(n: number | null | undefined): string { return `$${n.toFixed(n < 0.01 ? 6 : 4)}`; } +/** Keep in sync with src/cli/ui/theme/tokens.ts USD_TO_CNY. */ +const USD_TO_CNY = 7.2; + +/** USD-internal cost rendered in the wallet's display currency. Undefined currency → CNY (matches CLI default). */ +export function fmtCost( + usd: number | null | undefined, + currency: string | null | undefined, + fractionDigits?: number, +): string { + if (usd === null || usd === undefined) return "—"; + const cur = currency ?? "CNY"; + const amount = cur === "CNY" ? usd * USD_TO_CNY : usd; + if (amount === 0) return cur === "CNY" ? "¥0" : "$0"; + const sym = cur === "CNY" ? "¥" : cur === "USD" ? "$" : `${cur} `; + const digits = fractionDigits ?? (Math.abs(amount) < 0.01 ? 6 : 4); + return `${sym}${amount.toFixed(digits)}`; +} + export function fmtPct(n: number | null | undefined): string { if (n === null || n === undefined) return "—"; return `${(n * 100).toFixed(1)}%`; diff --git a/dashboard/src/panels/chat.ts b/dashboard/src/panels/chat.ts index bc86f4db..16a1df24 100644 --- a/dashboard/src/panels/chat.ts +++ b/dashboard/src/panels/chat.ts @@ -14,7 +14,7 @@ import { } from "../components/chat-internals.js"; import { MODE, TOKEN, api } from "../lib/api.js"; import { appBus, showToast } from "../lib/bus.js"; -import { fmtUsd } from "../lib/format.js"; +import { fmtCost, fmtUsd } from "../lib/format.js"; import { html } from "../lib/html.js"; import { t, useLang } from "../i18n/index.js"; @@ -791,6 +791,7 @@ function SideRail({ stats, budgetUsd, activePlan }: SideRailProps) { const showBudget = stats != null && typeof budgetUsd === "number" && budgetUsd > 0; const budgetPct = showBudget ? Math.min(120, (stats.totalCostUsd / budgetUsd) * 100) : 0; const budgetTone = budgetPct >= 100 ? "err" : budgetPct >= 80 ? "warn" : ""; + const walletCurrency = stats?.balance?.[0]?.currency; return html`