Files
magpie/internal/gui/assets/resetask.js
T
yetone 323159f1cc usage: a Codex reset's confirmation picks which of the account's resets is used (01huadalang)
01huadalang on Discord: resets were used only automatically, or by hand
the one that runs out first. Which account was already the user's to pick
(each account's card has its own Use); which of its resets was not.

- Usage card's "Use a reset" and the menu bar panel's "Use one...": when
  the account holds two or more resets listed with their ids, the
  confirmation has a "Reset to use" pop-up (the app's menu, not <select>),
  soonest first and picked unless another is. Once another is picked it
  names the account and that reset ("The one used is me@'s reset that
  runs out ..."), and says the sooner one is kept and lost if unused.
  Escape closes the open menu first, not the confirmation.
- POST /api/usage/codex-reset takes an optional credit. The server reads
  the account's resets again and spends that credit only if it is still
  held; a gone one is refused and no other is spent in its place. With no
  credit it is as before (the soonest). Auto-use is unchanged.
- ResetCard carries the credit id; `magpie quota reset --card n` is the
  CLI's sibling, its prompt naming the reset.
- askReset's dialog moved out to resetask.js, imported when Use is
  pressed (like signsheets.js), so the core's scripts are 1,530,733 bytes,
  under the 1,533,105 budget with no raise (the pick alone took it 2,068
  over). Five keys in zh, zh-TW, ja, de.

Run on mote (mote-run), at the base 2597c9636:
- gofmt -l clean; go vet ./..., GOOS=linux go vet -tags nogui ./...,
  GOOS=windows go vet ./... ok.
- go test -tags nogui ./internal/provider ./internal/cli ok;
  ./internal/gui: only TestSettingsSaveKeepsWhatItDoesNotSend (#4514) and
  TestColdTurnsTaken (#4510) red, both filed and the same on main;
  TestCoreScriptBudget ok after the move.
- GUI, Chromium and WebKit, 78/78 each: reset-pick-card (new: en, zh,
  zh-TW, ja, de; Usage at 900 and 390px, panel at 440px), codex-resets-used,
  account-every-reset, codex-reset-lead, reset-each-title, reset-providers,
  reset-reminder, panel-usage, old-webkit, i18n-keys, codex-auto-reset,
  codex-auto-reset-settings, warm-reset-asleep, quota-credits,
  usage-accounts.
- Red on the old code: reset-pick-card against origin/main's assets fails
  at "an account with three resets offers the pick"; TestUseCodexResetCard
  and TestCodexResetsCounted fail without the change.
- Broken by hand, each red then restored: the chosen credit ignored
  (spent [soon]); the held check skipped (a used credit spent); ids dropped
  from each; the credit left out of the POST; the Escape guard removed.

Again at the pushed head (rebased on 88372a39d): go vet ./... ok;
provider, cli ok; gui only #4514 red (TestColdTurnsTaken passed this
time); reset-pick-card, old-webkit, i18n-keys in Chromium 15/15.

Not run: the full go test ./... and the whole GUI suite. mote had two to
three other full Go suites and several whole GUI suites running the whole
time (load 120 to 250), and the WSL box too; one suite per machine.
Not tried: a real account's reset spent (none was, by design: fakes only),
so that Codex's consume takes a credit_id other than the soonest is
inferred from it being the same field the soonest is sent in.
2026-11-01 04:51:55 +08:00

98 lines
4.7 KiB
JavaScript

// A Codex reset's confirmation (askReset in app.js): spending a reset
// can't be taken back, so it asks first, names the account and which of
// its resets is used, and lets the user pick another (01huadalang on
// Discord). app.js imports this when a reset's Use is pressed, so it
// weighs nothing on the core.
window.askResetSheet = function (q) {
const ed = el("div", "editor reset-ask");
const head = el("div", "ehead");
head.append(icon(q.icon || "codex"), el("b", "", t("Use a Codex reset?")));
ed.append(head);
const who = q.alias || q.user || q.name;
ed.append(el("p", "lib-confirm", t(q.resets.count === 1
? "{who} has 1 reset. Using it starts its windows again at once, as if none of them had been used. It can't be undone."
: "{who} has {n} resets. Using one starts its windows again at once, as if none of them had been used. It can't be undone.", { who, n: q.resets.count })));
// which of them is used: the one that runs out first unless the user
// picks another of those listed one by one (01huadalang on Discord),
// and the dialog names the one it spends
const cards = q.resets.each?.length > 1 && q.resets.each.every((c) => c.id) ? q.resets.each : null;
let chosen = 0;
const which = el("p", "lib-confirm reset-which");
const sooner = el("p", "lib-confirm reset-sooner");
const cardName = (i, at = resetClock) => {
const c = cards[i];
return c.until ? t("Reset {n} runs out {when}", { n: i + 1, when: at(new Date(c.until)) }) : t("Reset {n} never runs out", { n: i + 1 });
};
const sayWhich = () => {
const full = (d) => d.toLocaleString(intlLang());
if (!chosen) {
which.textContent = q.resets.until ? t("The one used is the one that runs out first, {when}.", { when: full(new Date(q.resets.until)) }) : "";
which.hidden = !q.resets.until;
sooner.hidden = true;
return;
}
const c = cards[chosen];
which.textContent = c.until ? t("The one used is {who}'s reset that runs out {when}.", { who, when: full(new Date(c.until)) })
: t("The one used is {who}'s reset that never runs out.", { who });
which.hidden = false;
// one that runs out sooner is left, and lost if it isn't used in time
sooner.textContent = cards[0].until ? t("The one that runs out first, {when}, is kept, and is lost if it isn't used by then.", { when: full(new Date(cards[0].until)) }) : "";
sooner.hidden = !cards[0].until || cards[0].until === c.until;
};
if (cards) {
const row = el("div", "reset-pick");
const pick = el("button", "sess-pick reset-card");
pick.type = "button";
pick.setAttribute("aria-haspopup", "menu");
pick.setAttribute("aria-expanded", "false");
pick.append(el("span", "", cardName(0)), svg(CHEV, 11, 1.6));
pick.title = cardName(0, (d) => d.toLocaleString(intlLang()));
pick.onclick = (e) => {
e.stopPropagation();
if (pick.classList.contains("open")) return closeProtoMenu();
openProtoMenu(pick, cards.map((c, i) => ({ v: String(i), name: cardName(i), note: i ? "" : t("Used unless you pick another"), literalName: true })), String(chosen), (v) => {
chosen = Number(v);
pick.firstChild.textContent = cardName(chosen);
pick.title = cardName(chosen, (d) => d.toLocaleString(intlLang()));
pick.classList.toggle("set", chosen > 0);
sayWhich();
}, "Reset to use", "sess-menu");
};
row.append(el("span", "reset-pick-label", t("Reset to use")), pick);
ed.append(row);
}
sayWhich();
ed.append(which, sooner);
// nothing used yet: a reset would start nothing again
if (!q.windows?.some((w) => w.used > 0)) ed.append(el("p", "lib-confirm reset-idle", t("None of its windows has been used yet, so there is nothing to start again.")));
const bar = el("div", "bar");
const go = el("button", "text primary", t("Use a reset"));
go.onclick = async (e) => {
e.stopPropagation();
go.disabled = true;
go.classList.add("busy");
try {
const out = await api("usage/codex-reset", chosen ? { user: q.user || "", credit: cards[chosen].id } : { user: q.user || "" });
closeConfirmAsk();
status(who + ": " + resetOutcome(out), out.code === "reset" ? "ok" : "err");
// the providers' editor reads the account's windows and resets
// on its own, kept a minute: read again, as Usage is (#1491)
delete loginUsage.codex;
if (editing && view === "providers") renderProviders();
loadQuotas();
} catch (err) {
go.disabled = false;
go.classList.remove("busy");
statusErr(err);
}
};
const cancel = el("button", "text", t("Cancel"));
cancel.onclick = cancelAsk;
bar.append(el("span", "grow"), cancel, go);
ed.append(bar);
confirmAsk = ed;
openModal(ed);
$("#modal").classList.add("lib");
go.focus();
};