onkashiko on X: DeepSeek, Kimi (China), Zhipu and other domestic vendors
price in yuan, the rest in dollars, and one currency for every cost made
the Usage page read wrong for half of them.
Settings > Usage > Currency gains a third choice, By provider. With it,
a figure of one provider's calls (a model's, an account's, a key's row,
a request, a routing try, a quota card's whole) shows in the currency
that provider sells in, and every sum across providers (the total,
agents, sessions, charts, the tray, Bark's daily, the day page) in the
currency a "Totals in" row picks, a row shown only while By provider is
chosen. GUI, TUI and CLI agree. Costs stay stored in dollars; only the
display converts, at the live rate (internal/fx).
Which providers sell in yuan (provider.PricesInYuan): a .cn host; a
preset at its own site that is a yuan preset (deepseek, zhipu,
volcengine, tencent-cloud, bailian, siliconflow…), ends in -cn, or is in
its China region; a plugin id ending in -cn; a provider of one's own
with a yuan models.dev catalog served at that catalog's site. A relay
made from DeepSeek's preset, Z.ai, Kimi's and MiniMax's international
endpoints stay in dollars: they share a catalog with their Chinese
twins, so the catalog alone can't tell.
Known limit: models.dev has only dollar prices (converted from the
vendors' yuan at ~6.7-7.0), so a yuan figure is that dollar price at
today's rate and can differ a few percent from the vendor's own list.
A second ask from onkashiko: set the currency of the menu bar and of
the totals apart, sums converted by themselves.
So Settings > Usage also gains an Advanced fold (closed by default, open while
set) with one row, Menu bar currency: Same as totals (the default), $ or
¥ (settings.TrayCurrency, TrayCostIn). It sets the menu bar's totals
cards and the tray tooltip's cost; the Usage page, the panel, the TUI,
the CLI and Bark's daily message stay in the totals' currency
(totalsCurrency). Like the other menu bar settings it stays on this
computer (KeepOwn). Every sum is still made in dollars and converted
once, at the live rate, so a mixed-currency total is never added up
from yuan and dollars.
Settings' currency rows (Currency, Totals in, Menu bar currency and the
rate in the tooltip) moved out of app.js into currencyprefs.js, imported
when Settings draws its usage prefs, and costInYuan is packed: the core
is 3 bytes under coreScriptBudget, with no raise.
Run:
- go test -count=1 at this tree on mote: internal/settings,
settingsapi, tui, cli, catalog, provider, fx and gui all ok
(TestCoreScriptBudget included). The full go test ./... and the whole
GUI suite are queued behind other sessions' suites on suite.lock
(mote and mica, load 35 and 60) and had not started at push.
- GUI files at this tree (mica): currency-provider, currency, i18n-keys,
i18n-en-no-cjk, tray-cost, tray-totals, every settings-*, old-webkit:
362 pass, 28 red. Every red is red the same at origin/main ac0b5c287
on mote, and filed: settings-search (#6133, #6353), settings-hints
(#6107, #6353), settings-lines-wrap de 440px (#6353), old-webkit
appearance.js (#6467) and :has() (#5919). The one red of this change,
settings-groups' list of Usage's parts (now with the Advanced fold),
is fixed here: 14 of 14.
- currency-provider.test.cjs (new): en, zh, zh-TW, ja, de at 900 and
420px, both engines; By provider, Totals in ¥, then Advanced: $ for
the menu bar with totals in ¥, totals stay ¥21.60, the fold opens
itself after a reload. Plus currency.test.cjs (3 choices now),
i18n-keys, i18n-en-no-cjk, tray-cost: green in both engines.
- TestTrayCostIn (values, refused eur/provider, KeepOwn),
TestFXByProvider (trayCurrency vs totalsCurrency), TestCoreScriptBudget.
- gofmt -l clean; GOOS=linux go vet -tags nogui ./...,
GOOS=windows go vet ./..., GOOS=linux go build -tags nogui,
GOOS=windows go build on mote at this tree: ok.
- Broken by hand, each red: costInYuan ignoring the provider (GUI
test: deepseek showed $1.00 for ¥7.20), PricesInYuan without the
preset's site check (a relay made from DeepSeek's preset read as
yuan), CostIn without its By provider branch (settings test),
TrayCostIn ignoring TrayCurrency (TestTrayCostIn, TestFXByProvider),
the fold not opening while set (webkit), the row saving into
totalCurrency (chromium).
- Real app in a sandbox HOME on mote (dev build, magpie web and magpie
usage) with DeepSeek, OpenAI and Z.ai presets and one call each: the
window and the CLI both show deepseek-v4-pro ≈¥15.44, gpt-4.1 ≈$1.00,
glm-4.6 ≈$0.290 and the total ≈¥24.07 at 6.685; /api/settings' fx
carries yuan ["deepseek"].
Not tried: Windows and Linux windows and trays, the real menu bar with
Menu bar currency changed (drawn natively; covered through trayCurrency
in Go), a real Kimi CN or Zhipu account.
115 lines
3.4 KiB
Go
115 lines
3.4 KiB
Go
package gui
|
|
|
|
import (
|
|
"encoding/base64"
|
|
"io/fs"
|
|
"os"
|
|
"path"
|
|
"strings"
|
|
|
|
"github.com/yetone/magpie/internal/provider"
|
|
"github.com/yetone/magpie/internal/settings"
|
|
"github.com/yetone/magpie/internal/usage"
|
|
"github.com/yetone/magpie/internal/usageapi"
|
|
)
|
|
|
|
// The usage:day read (plugin.PageReads): a day of usage for a plugin's
|
|
// page, by agent and by model, with the logos the Usage page draws for
|
|
// them. A page loads nothing but its own files and data: pictures
|
|
// (pagePolicy), so each logo comes as a data: URL, once for each name.
|
|
|
|
// pageIcon is a logo as a page draws it: Mono a one-colour mark the page
|
|
// fills with its text colour, as the window's mask icons are (app.js's
|
|
// icon), else a picture drawn as it is.
|
|
type pageIcon struct {
|
|
Src string `json:"src"`
|
|
Mono bool `json:"mono,omitempty"`
|
|
}
|
|
|
|
// maxPageIcon is the largest logo a read carries: a picture of the user's
|
|
// own past it is drawn as the generic one.
|
|
const maxPageIcon = 256 << 10
|
|
|
|
// pageIconOf is the logo named name, as app.js's icon finds it: a picture
|
|
// the user gave a provider (file:…), else the bundled SVG or PNG; ok is
|
|
// false for "generic", "" and a name with no file.
|
|
func pageIconOf(name string) (pageIcon, bool) {
|
|
if name == "" || name == "generic" {
|
|
return pageIcon{}, false
|
|
}
|
|
if own, ok := strings.CutPrefix(name, "file:"); ok {
|
|
f, _ := provider.IconServed(own)
|
|
if f == "" {
|
|
return pageIcon{}, false
|
|
}
|
|
b, err := os.ReadFile(f)
|
|
if err != nil || len(b) > maxPageIcon {
|
|
return pageIcon{}, false
|
|
}
|
|
return pageIcon{Src: dataURL(own, b)}, true
|
|
}
|
|
// a URL or a path is no bundled name (app.js draws it as generic)
|
|
if strings.ContainsAny(name, ":/\\") {
|
|
return pageIcon{}, false
|
|
}
|
|
for _, ext := range []string{".svg", ".png"} {
|
|
b, err := fs.ReadFile(assets, "assets/icons/"+name+ext)
|
|
if err != nil || len(b) > maxPageIcon {
|
|
continue
|
|
}
|
|
return pageIcon{Src: dataURL(name+ext, b), Mono: ext == ".svg" && !strings.HasSuffix(name, "-color")}, true
|
|
}
|
|
return pageIcon{}, false
|
|
}
|
|
|
|
func dataURL(name string, b []byte) string {
|
|
ctype := "image/svg+xml"
|
|
switch strings.ToLower(path.Ext(name)) {
|
|
case ".png":
|
|
ctype = "image/png"
|
|
case ".jpg", ".jpeg":
|
|
ctype = "image/jpeg"
|
|
case ".webp":
|
|
ctype = "image/webp"
|
|
}
|
|
return "data:" + ctype + ";base64," + base64.StdEncoding.EncodeToString(b)
|
|
}
|
|
|
|
// pageDay is the usage:day read: params {date: "YYYY-MM-DD", from:
|
|
// "YYYY-MM-DD" or "all", tz: an IANA zone}, all optional (today, the day
|
|
// alone, this computer's zone): with a from it is the days from there to
|
|
// date (usageapi.SpanOf), a week's or a month's or all time's.
|
|
func pageDay(params map[string]any) (any, error) {
|
|
date, _ := params["date"].(string)
|
|
from, _ := params["from"].(string)
|
|
tz, _ := params["tz"].(string)
|
|
d, err := usageapi.SpanOf(from, date, usage.Zone(tz))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
icons := map[string]pageIcon{}
|
|
want := func(name string) {
|
|
if _, ok := icons[name]; ok {
|
|
return
|
|
}
|
|
if ic, ok := pageIconOf(name); ok {
|
|
icons[name] = ic
|
|
}
|
|
}
|
|
for _, a := range d.Agents {
|
|
want(a.Icon)
|
|
for _, m := range a.Models {
|
|
want(m.Icon)
|
|
}
|
|
}
|
|
// a cost as Settings shows a sum of several providers': in USD, or in
|
|
// CNY at the rate known now
|
|
cur := settings.Load().TotalCostIn()
|
|
return struct {
|
|
usageapi.Day
|
|
Icons map[string]pageIcon `json:"icons"`
|
|
Currency string `json:"currency"`
|
|
Rate float64 `json:"rate"` // CNY per USD, 0 when unknown
|
|
}{d, icons, cur, currentFX().Rate}, nil
|
|
}
|