Files
magpie/internal/gui/assets/autoresetown.js
T
yetone 28f27e07b7 editor: a provider's warm-up or check-in that is on opens its Advanced (ChrisYagao on X)
ChrisYagao on X asked where his warm-up went. Nothing was lost: warm-up
moved from Settings into each provider's editor (11-09), then into that
editor's folded Advanced (11-10), so a warm-up that was on vanished from
sight with no trace of where it went.

Fix: the editor's Advanced opens on its own while that provider's
warm-up (on reset or daily times) or daily check-in is on
(provPrefsOn), unless the reader opened or closed it by hand, the same
rule as Routing's Advanced. renderWarmAtOwn moves into the lazily
loaded autoresetown.js (warmAtOwnPaint), so app.js and the core script
budget each drop by 1,529 bytes. reader.cjs's openProvPrefs waits for
the modal's animations to end before measuring (warm-at-times WebKit
900px measured mid-animation, 0.66px overhang).

Old data: no setting changes and nothing migrates; a warm-up that was
on shows again the first time its editor opens.

What was run:
- warm-advanced.test.cjs (new): red 10/10 on the old app.js
  ("codex, warm up on reset: Advanced open"), green after; chromium and
  webkit, en zh zh-TW ja de.
- At the pushed head (rebased on origin/main), on mica: warm-advanced,
  warm-at-times, warm-at-own, warm-now, quota-warm, plugin-checkin,
  editor-advanced-align, routing-advanced: 160 passed, 0 failed.
- 29 related GUI files (every openProvPrefs user, warm-*, quota-warm,
  plugin-checkin, routing-advanced, editor-advanced-align,
  codex-auto-reset*, reset-providers, search-mix, model-api-preset,
  i18n-keys, segs-press-redraw, settings-groups) alone on mica: 603
  passed, 31 failed; every red is red at origin/main 8e1534f5f too:
  codex-auto-reset-settings webkit de #4790, i18n-keys "a plugin's
  words are its own" #6877, model-api-preset chromium #5838,
  reset-providers 420px #6196, settings-groups "Spend extra usage"
  #6597, segs-press-redraw currencySegs (filed #6990).
- go test ./... -timeout 45m on mote: ok, no FAIL. After the rebase,
  go test ./internal/gui/ (budgets) on mica: ok.
- GOOS=linux go vet -tags nogui ./... ok, GOOS=windows go vet ./... ok,
  gofmt -l clean.

Not tried: the whole GUI suite ran once only, by accident, at an
earlier head before the reader.cjs fix, under load on mica (24,676
passed, 3,151 failed); those reds can't be attributed and are not a
record for this head. Not tried in the real app with ChrisYagao's
settings.
2026-11-17 23:27:42 +08:00

202 lines
9.0 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// Codex's auto-use rows, in its provider's editor (PROV_PREFS): each ChatGPT account's switches
// and, under them, how early a reset about to expire is used. app.js
// imports this when renderSettings draws them (renderAutoResetOwn), so it weighs
// nothing on the core. Each ChatGPT account's own warm-up time is drawn here too
// (warmAtOwnPaint), moved out of app.js for its budget.
// renderAutoResetOwn lists each ChatGPT account under
// "Codex: auto-use resets", with its switch, the same say as on the
// account's card (settings' codexAutoReset, the CLI's quota auto-reset).
// Avalon on Discord: it was only on the accounts' cards, not in the
// window's Settings. The row shows while a Codex
// account is signed in, or one is still on.
window.autoResetOwnPaint = function (s) {
warmAtOwnPaint(s);
const head = $("#codexAutoResetRow");
const list = head.parentElement;
list.querySelectorAll(".ar-own").forEach((r) => r.remove());
const on = s.codexAutoReset || [];
const users = [...(s.codexUsers || [])];
for (const u of on) if (!users.some((x) => x.toLowerCase() === u)) users.push(u);
head.hidden = !users.length;
// each account's resets, read as the providers' editor reads them,
// with Use beside them (husky_bobo on X looked for it here)
const held = users.length ? loginUsageOf("codex") : null;
if (users.length && !held) loginUsage.codex?.loading?.then(() => { if (!head.hidden && loginUsage.codex?.data) autoResetOwnPaint(prefs || s); });
let after = head;
for (const user of users) {
const r = el("div", "row pref ar-own");
r.dataset.user = user;
const who = el("div", "who");
const name = el("div", "name", user);
name.title = user;
who.append(name);
const q = held && Object.entries(held).find(([u]) => u.toLowerCase() === user.toLowerCase())?.[1];
if (q?.resets?.count && !q.error && !q.resets.byWindow) {
const qu = { ...q, provider: "codex", user };
const sub = el("div", "sub ar-own-resets");
const use = el("button", "text", t("Use a reset"));
use.title = resetUseTitle(qu);
use.setAttribute("aria-haspopup", "dialog");
use.onclick = (e) => { e.stopPropagation(); askReset(qu); };
sub.append(resetsWords(qu.resets, qu), " ", use);
who.append(sub);
}
// each of the two times its own Off/On (#2078)
const was = on.includes(user.toLowerCase()) ? autoResetWhenOf(s.codexAutoResetOnly?.[user.toLowerCase()]) : { week: false, expiring: false };
const cases = el("div", "ar-own-cases");
for (const c of AUTO_RESET_CASES) {
const line = el("div", "ar-own-case");
line.dataset.case = c.id;
const say = el("span", "ar-own-say", t(c.say));
say.title = t(was[c.id] ? c.on : c.off, resetLeadVars());
const pick = segs([["off", t("Off")], ["on", t("On")]], was[c.id] ? "on" : "off", async (v) => {
try {
prefs = await writingPrefs(api("settings/codex-auto-reset", { user, on: v === "on", when: c.id }));
if (state) state.settings = prefs;
const now = (prefs.codexAutoReset || []).includes(user.toLowerCase()) ? autoResetWhenOf(prefs.codexAutoResetOnly?.[user.toLowerCase()]) : { week: false, expiring: false };
status(autoResetStatus(user, now), "ok");
renderSettings();
if (quotas) renderQuotas(); // the account's card says the same
} catch (e) {
statusTErr(e);
renderSettings();
}
});
pick.setAttribute("aria-label", t("Auto-use resets") + " · " + t(c.say) + " · " + user);
line.append(say, pick);
cases.append(line);
}
r.append(who, cases);
after.after(r);
after = r;
}
renderResetLead(s, head.hidden);
};
// renderResetLead: how long before a reset about to run out unused it is
// used, under the accounts' switches, one for them all; a pick from the
// app's menu (settings/codex-reset-lead). A value set outside the choices
// (Custom…, or the CLI) is shown and kept (shangyuan0818 on Discord).
function renderResetLead(s, hidden) {
const row = $("#codexResetLeadRow");
if (!row) return;
row.hidden = hidden;
// a redraw leaves Custom…'s field being typed in
const own = $("#codexResetLeadPick .reset-lead-own");
if (own && !own.dataset.done) return;
const cur = resetLeadMinutes(s);
const leads = RESET_LEADS.includes(cur) ? RESET_LEADS : [...RESET_LEADS, cur].sort((a, b) => a - b);
const name = (m) => t("{lead} before", { lead: resetLeadWords(m) });
const b = el("button", "sess-pick reset-lead");
b.type = "button";
b.setAttribute("aria-haspopup", "menu");
b.setAttribute("aria-label", t("How early before one expires") + ": " + name(cur));
// every choice in one cell, the current one seen: the button is as wide
// as the widest, so a pick doesn't rewrap the row and move the page
const say = el("span", "rl-say");
for (const m of leads) {
const w = el("span", m === cur ? "on" : "", name(m));
if (m !== cur) w.setAttribute("aria-hidden", "true");
say.append(w);
}
b.append(say, svg(CHEV, 11, 1.6));
b.onclick = (e) => {
e.stopPropagation();
if (b.classList.contains("open")) return closeProtoMenu();
const opts = leads.map((m) => ({ v: String(m), name: name(m), note: m === 30 ? "Default" : "", literalName: true }));
openProtoMenu(b, [...opts, { v: "\x01", name: "Custom…" }], String(cur), (v) => v === "\x01" ? resetLeadOwn(b, cur, set) : set(Number(v)), "Used before it expires", "sess-menu", "right");
};
const set = async (m) => {
if (m === cur) return renderSettings();
try {
prefs = await writingPrefs(api("settings/codex-reset-lead", { minutes: m }));
if (state) state.settings = prefs;
renderSettings();
if (quotas) renderQuotas(); // the cards' titles say it too
} catch (err) {
statusTErr(err);
renderSettings();
}
};
$("#codexResetLeadPick").replaceChildren(b);
}
// resetLeadOwn: Custom…'s field of minutes in the button's place; Enter or
// leaving saves, Escape doesn't, one out of range is said, not written.
function resetLeadOwn(b, cur, set) {
const least = RESET_LEADS[0], most = RESET_LEADS.at(-1);
const i = input(String(cur), least + "–" + most);
i.className = "words compact-num reset-lead-own";
i.inputMode = "numeric";
i.setAttribute("aria-label", t("Minutes"));
let done = false;
const finish = (save) => {
if (done) return;
done = true;
i.dataset.done = "1";
const v = i.value.trim(), m = /^\d+$/.test(v) ? Number(v) : NaN;
if (!save || !v) return renderSettings();
if (!(m >= least && m <= most)) {
status(t("Minutes is a whole number from 3 to 1440"), "err");
return renderSettings();
}
set(m);
};
i.onkeydown = (e) => { e.stopPropagation(); if (e.key === "Enter") finish(true); else if (e.key === "Escape") finish(false); };
i.onblur = () => finish(true);
// in the button's box: the row doesn't rewrap and move the page
i.style.width = b.offsetWidth + "px";
i.style.height = b.offsetHeight + "px";
b.replaceWith(i);
i.focus(NO_SCROLL);
}
// warmAtOwnPaint: each ChatGPT account's own daily time (#957,
// codexWarmAtOf), shown with two accounts or one with its own
function warmAtOwnPaint(s) {
const list = $("#codexWarmList");
list.querySelectorAll(".warm-own").forEach((r) => r.remove());
const own = s.codexWarmAtOf || {};
const users = [...(s.codexUsers || [])];
for (const u of Object.keys(own)) if (!users.some((x) => x.toLowerCase() === u)) users.push(u);
if (users.length < 2 && !Object.keys(own).length) return;
const save = async (user, at) => {
try {
prefs = await writingPrefs(api("settings/codex-warm-at", { user, at }));
if (state) state.settings = prefs;
renderSettings();
} catch (e) {
statusTErr(e);
renderSettings();
}
};
for (const user of users) {
const at = own[user.toLowerCase()];
const mode = at === undefined ? "same" : at === "off" ? "off" : "own";
const r = el("div", "row pref warm-own");
r.dataset.user = user;
const who = el("div", "who");
const name = el("div", "name", user);
name.title = user;
const sub = el("div", "sub", mode === "own" ? t("Its own time each day")
: mode === "off" ? t("Not started at a time of day")
: s.codexWarmAt ? t("Same as above, {at}", { at: warmTimes(s.codexWarmAts, s.codexWarmAt).join(", ") }) : t("Same as above, off"));
sub.title = t("Give accounts different times and one window starts as another runs out.");
who.append(name, sub);
const box = el("div", "warm-at");
if (mode === "own") {
const i = input(at, "06:00", "time");
i.className = "at";
i.setAttribute("aria-label", t("{who}'s time of day", { who: user }));
blurOnEnter(i);
i.onchange = () => { if (i.value && i.value !== at) save(user, i.value); };
box.append(i);
}
box.append(segs([["same", t("Same")], ["own", t("Own")], ["off", t("Off")]], mode, (v) =>
save(user, v === "same" ? "" : v === "off" ? "off" : s.codexWarmAt || "06:00")));
r.append(who, box);
$("#warmNowRow").before(r); // Warm up now stays last
}
}