Display usage spend in CNY (¥) instead of USD ($) - #136
Merged
Conversation
The total-spend / per-model spend shown in Settings comes verbatim from the OpenClaw gateway `sessions.usage` RPC, which reports cost in USD (OpenClaw's `estimateUsageCost()` is priced from a USD model catalog). MicroClaw only relays the number, so there is no MicroClaw-side math to fix — the arithmetic is OpenClaw's responsibility and is correct. This adds a live USD->CNY conversion at the display boundary: - New `exchange-rate.ts` fetches the USD->CNY rate from Frankfurter (ECB data, no API key), caches it in memory (12h TTL), and falls back to a hardcoded ~7.2 rate when offline so the UI never breaks. - Expose the rate via a new `usage:get-exchange-rate` IPC and include it in the `usage:get-stats` payload. - SettingsView converts the relayed USD values to CNY and renders `¥` via a new `settings.currencySymbol` i18n key, preserving existing decimal precision. Fixes #127 Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: 1fe75171-fea2-45ef-9401-bf8a4ea0f2ba
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The Total Spend / per-model spend figures shown in Settings → Usage were displayed with a
$prefix, implying USD. This PR converts those values to CNY and relabels the symbol to ¥.Where the number comes from (and why the math is already correct)
MicroClaw does not compute cost itself. It calls the OpenClaw gateway RPC
sessions.usageand relaystotals.totalCost,byModel[].totals.totalCost,daily[].cost, andsessions[].usage.totalCostverbatim (each with|| 0) indesktop/src/main.ts.The value OpenClaw returns is USD: OpenClaw's
src/utils/usage-format.tsestimateUsageCost()computes(input*price.input + output*price.output + cacheRead*… + cacheWrite*…) / 1_000_000, where prices come from the USD-denominated remote model catalog (catalog.openclaw.ai/models/v1/catalog.json). Its JSDoc says "Estimates USD usage cost" and there is aformatUsd()helper. There is no currency/FX logic anywhere in OpenClaw or MicroClaw.Conclusion for the "verify calculation accuracy" part of #127: the arithmetic lives in OpenClaw and is correct; MicroClaw only relays it, so there is no MicroClaw-side math bug. The
$label was factually accurate, which is exactly why we can't just swap the glyph — doing so would mislabel a genuine USD value as CNY. A real USD→CNY conversion is required.What this PR changes
desktop/src/exchange-rate.ts, new). Fetches the current USD→CNY rate fromhttps://api.frankfurter.app/latest?from=USD&to=CNY(ECB data, no API key). In-memory cache with a 12h TTL. Robust fallback to a hardcoded~7.2constant (USD_TO_CNY_FALLBACK_RATE, clearly commented) when the fetch fails, so the UI never breaks offline. Injectable fetcher for tests, mirroringupdate-checker.ts.usage:get-exchange-rateIPC (+ preloadusage.getExchangeRate()), and the rate is also included in the existingusage:get-statspayload (exchangeRate/currency).SettingsView.vue(the only place spend is rendered) multiplies the relayed USD values by the rate via a singletoCny()helper — no double conversion; raw USD is untouched everywhere else.$→¥. Via a newsettings.currencySymboli18n key in bothen-US.tsandzh-CN.ts, preserving the existing precision (.toFixed(4)for spend,.toFixed(2)for budget).Validation
desktopvitest: 653 passed (incl. 6 newexchange-rate.test.tscases: cache hit / TTL refetch / live / fallback / stale-reuse).desktop/renderervitest: 194 passed;vue-tsctypecheck clean; eslint 0 errors.0.00342USD ×7.2≈ ¥0.0246.Closes #127