Files
magpie/internal/gui/assets/balfound.js
T
yetone 0d0a1a5678 balance probe: kept across restarts, Auto-detect switch, Detect again, not-detected line in the editor (#3146, Niapyaya on X)
A custom provider at a New API / Sub API / One API relay with no Balance URL
has its balance query found by magpie (provider/balance_probe.go). This keeps
what was found, or not found, in balance-probes.json (keyed by origins and a
hash of the key, written through temp-and-rename, an unreadable file read as
empty), so a restart does not probe again. The editor gets an Auto-detect
switch (NoBalanceProbe), a Detect again button (ForgetBalanceProbe) and a line
saying where the balance was found, or that none was, with the Balance URL
field as the way out. A found balance is a balance card on the Usage page.

Run at the rebased head (parent 6a3f162f0 = origin/main), on mica:
- gofmt -l: clean.
- go vet ./... (darwin), GOOS=linux -tags nogui, GOOS=windows: clean (at the
  previous base e6bf4adf1; go vet ./internal/... again at this head).
- go test -count=1 internal/provider internal/gui: ok, at this head.
- GUI, both engines, five languages: balance-found, gui-de/ja/zh-tw, i18n-keys,
  i18n-en-no-cjk, provider-billing, provider-detect*: all ok except gui-de and
  gui-ja "has every zh string" ({agent} twice in sessions' "isn't installed
  here"), which are red the same at e6bf4adf1 (filed).
- At e6bf4adf1, 79 provider/gui/i18n files: 80 reds, the same 80 on the
  unmodified base (provider-fetch-again #5232/#5057, provider-picks-stay
  #4397, provider-models-rest #4396, provider-typed #4673, menu-scroll #1550,
  provider-max-output and provider-many-keys filed with the rest).
Not tried: a real New API / Sub API relay with a real key; the full Go suite
(started after the tag).
2026-11-12 03:13:50 +08:00

62 lines
2.8 KiB
JavaScript

// A relay added by hand with no Balance URL has its balance found by
// magpie where relays tell a key it (provider/balance_probe.go). Under the
// Balance URL the editor says where, with Use to copy it into the fields,
// so an amount read at the wrong query can be put right. app.js imports
// this when the editor draws a relay's balance with no Balance URL.
let balFoundAt = null;
window.balFound = function (box, p, draft, bal, pathOf) {
const line = el("div", "bal-found");
line.hidden = true;
balFoundAt = { line, draft, bal, pathOf, p };
box.append(line);
// the look for it can be turned off, for a relay that has no balance
let row = null;
if (p.balanceProbe) {
const [rk, cb] = tick(t("Auto-detect"), !draft.noBalanceProbe);
row = rk;
cb.onchange = () => { draft.noBalanceProbe = !cb.checked; if (!cb.checked) line.hidden = true; };
box.append(row);
}
bal.addEventListener("input", () => {
const typed = !!bal.value.trim();
line.hidden = !line.dataset.show || typed;
if (row) row.hidden = typed;
});
window.balFoundShow({ found: p.balanceFound, foundField: p.balanceFoundField, undetected: p.balanceUndetected });
};
// what the editor's Check balance found (app.js), drawn again
window.balFoundShow = function (r) {
const s = balFoundAt;
if (!s || !s.line.isConnected) return;
const { line, draft, bal, pathOf } = s;
line.replaceChildren();
const miss = !r.found && !!r.undetected && !draft.noBalanceProbe;
line.dataset.url = r.found || "";
line.dataset.show = r.found || miss ? "1" : "";
line.hidden = !line.dataset.show || !!(draft.balanceURL || "").trim();
if (miss) {
const none = el("span", "", t("Not detected"));
none.title = t("magpie asked this relay where relays tell a key its balance, and found none. Put in a Balance URL of your own, or detect again");
const again = el("button", "text action", t("Detect again"));
again.type = "button";
again.onclick = () => document.querySelector(".editor .bal-check")?.click();
line.append(none, again);
return;
}
if (!r.found) return;
const txt = el("span", "", t("Found by magpie: {url}", { url: r.found + (r.foundField ? " " + r.foundField : "") }));
txt.title = t("magpie asked this relay where relays tell a key its balance, and reads it here. If the amount is wrong, put in your own Balance URL and field; magpie looks again each day");
const use = el("button", "text action", t("Use"));
use.type = "button";
use.title = t("Put this query in the Balance URL and Balance field, to change it");
use.onclick = () => {
draft.balanceURL = bal.value = r.found;
draft.balancePath = r.foundField || "";
const f = pathOf();
if (f) { f.value = draft.balancePath; f.placeholder = balanceFieldOf(r.found); }
line.hidden = true;
bal.focus();
};
line.append(txt, use);
};