Files
magpie/internal/gui/assets/agentspeed.js
T
yetone 738ede4a70 agents: Speed beside Model and Effort, with what decides it (Tengs999 on X)
An agent whose model has a fast mode now shows Speed next to Model and
Effort: in the window's Details and in the tray panel's opened row. It
reads Usual, Fast (one bolt) or Ultrafast (two), and its menu (the app's
openProtoMenu, never a native select) offers Default, Fast and, where the
model has it, Ultrafast. It sets the very pick the model's bolt in the
picker sets: POST /api/agent-fast {for, ref, fast, ultra}; no new state.
A refused post puts the old pick back.

What decides the speed is said as the gateway decides it (speedNow): the
agent's own pick, else the model's own tier (ModelTiers) -> "Fast · model
setting", else for Codex its service tier in Settings -> "Fast · Codex
setting", or "As Codex asks" when that tier is unset. The tooltip says
it in a sentence, and Default's note in the menu says what Default gives.
Words in en, zh, zh-TW, ja and de.

speedNow and speedField live in agentspeed.js, imported when such a row
draws, and before a fast model's Details first slides open so it measures
the line. What is left in app.js (two helpers and three call sites) is
499 bytes; both script budgets go up by that, with the reason written.

Run (mote/mica, at this head):
- internal/gui/tests/agent-speed.test.cjs: Chromium and WebKit, en zh
  zh-TW ja de, window 1100px and panel 440px, all green. Broken by hand:
  dropping the model-tier line in speedNow turns it red ("Fast · model
  setting" expected, "Usual" actual); the panel's lazy line not taking a
  .field's place was found by it (0 Speed fields) and fixed.
- go test ./internal/gui/: ok (TestCoreScriptBudget included). gofmt -l
  clean.
- GUI files that reach the agent rows and the guards (agent-menu-label,
  fast-pick, fast-ultra, desktop-tiers, i18n-keys, kimi-subagent-pool and
  more) at this change and at origin/main 074614077, and
  agent-details-ready and old-webkit again at this head and origin/main
  7c3e68908 (the same tests red with the same numbers, e.g. 823.98 ->
  903.92): the same reds on both, each filed: agent-details-ready
  #6373/#6369, agent-menu-label (WebKit) #6371, aside-native #6212,
  codex-memories-settings #6372, desktop-tiers #6475, fast-pick/fast-ultra
  #6201, found-skill-pick-remove #5013/#4097, grip-inset #6293, i18n-keys
  ", with an image" #6877, old-webkit headerpresets.js #6030 and app.css
  :has() #5919.

Not tried: the whole GUI suite and go test ./... (both boxes were busy
with other sessions' suites); the real app, and Linux WebKitGTK.
2026-11-17 18:59:24 +08:00

85 lines
5.0 KiB
JavaScript

// Speed beside Model and Effort (Tengs999 on X): a line of an agent's
// opened row, in the tray panel and under the window's New sessions, for a
// model with a fast mode. app.js imports this the first time the line shows
// (speedLazy, and Details before it measures), so it weighs nothing on the
// core.
// speedNow: the speed agent a's requests for the model option o go at, and
// what decides it, as the gateway decides it: the agent's own fast pick
// first (fastPick, which goes over Codex's Standard), else the model's own
// speed (its Model settings, ModelTiers), else, for Codex, its service tier
// in Settings (codexTiered), which an Ultrafast ask also keeps over a Fast
// pick (withFast). from: "agent", "model", "codex", "ask" (as Codex's /fast
// asks) or "" (the usual speed)
function speedNow(a, o) {
const s = prefs || state.settings || {};
let tier = o.fast ? (o.ultra ? "ultrafast" : "priority") : "", from = o.fast ? "agent" : "";
const own = !tier && s.modelTiers?.[o.ref];
if (own === "priority" || own === "ultrafast") tier = own, from = "model";
if (kindOf(a) === "codex") {
const ct = s.codexServiceTier || "";
if (ct === "ultrafast" && !o.ultraOk && !tier) tier = "priority", from = "codex";
else if (ct === "ultrafast" && o.ultraOk && tier !== "ultrafast") tier = "ultrafast", from = "codex";
else if (ct === "priority" && !tier) tier = "priority", from = "codex";
else if (!ct && !tier) from = "ask";
}
return { tier, from };
}
const SPEED_NAME = { "": "Usual", priority: "Fast", ultrafast: "Ultrafast" };
// speedField: the Speed beside Model and Effort (Tengs999 on X), for a
// model with a fast mode: the speed the requests go at now, what decides
// it, and the app's menu that sets the agent's own fast pick, the very
// switch the model's bolt in the picker is (#954, #1954)
window.speedField = function (a) {
const f = a.fields.find((x) => x.key === "model");
const o = f?.value && optionFor(f, f.value);
if (!o?.fastFor || !o.ref) return null;
const now = speedNow(a, o), name = o.label || o.value, ag = { agent: a.name, model: name };
const b = el("button", "field ag-speed" + (now.tier ? " set" : ""));
b.type = "button";
b.dataset.key = "\0speed";
b.setAttribute("aria-haspopup", "menu");
const v = el("span", "v" + (now.tier ? "" : " empty"));
if (now.tier) v.append(boltIcon(now.tier === "ultrafast", 11));
v.append(t(now.from === "ask" ? "As Codex asks" : SPEED_NAME[now.tier]));
if (now.from === "model" || now.from === "codex") v.append(el("span", "ag-speed-by", " · " + t(now.from === "model" ? "model setting" : "Codex setting")));
b.append(el("span", "k", t("Speed")), v, el("span", "chev", svg(CHEV, 11, 1.7)));
const tierName = t(SPEED_NAME[now.tier]);
b.title = t("{label}: {value}", { label: t("Speed"), value: now.from === "ask" ? t("As Codex asks") + " (" + t("/fast in Codex") + ")" : tierName })
+ (now.from === "model" ? "\n" + t("{model}'s own speed, in its Model settings, sends it {tier} for every agent with no pick of its own", { ...ag, tier: tierName })
: now.from === "codex" ? "\n" + t("Codex's service tier in Settings sends every Codex request {tier}, over this pick", { tier: tierName })
: "");
b.setAttribute("aria-label", b.title);
b.onclick = (ev) => {
ev.stopPropagation();
if (b.classList.contains("open")) return closeProtoMenu();
// the default as the gateway goes on without a pick of the agent's own
const bare = speedNow(a, { ...o, fast: false, ultra: false });
const dflt = bare.from === "model" ? t("{tier}, as the model is set for every agent", { tier: t(SPEED_NAME[bare.tier]) })
: bare.from === "codex" ? t("{tier}, as Codex's service tier is set in Settings", { tier: t(SPEED_NAME[bare.tier]) })
: bare.from === "ask" ? t("/fast in Codex") : t("the usual speed");
const opts = [
{ v: "", name: "Default", note: dflt },
{ v: "priority", name: "Fast", note: o.ownFast ? "asked of your provider, where it serves it" : "quicker, at a higher price" },
...(o.ultraOk ? [{ v: "ultrafast", name: "Ultrafast", note: "where the account offers it, Fast where it doesn't" }] : []),
];
const cur = o.fast ? (o.ultra ? "ultrafast" : "priority") : "";
openProtoMenu(b, opts, cur, async (v) => {
if (v === cur) return;
const was = { fast: !!o.fast, ultra: !!(o.fast && o.ultra) };
setFast(o, !!v, v === "ultrafast");
renderAgents();
try {
await api("agent-fast", { for: o.fastFor, ref: o.ref, fast: !!v, ...(v === "ultrafast" ? { ultra: true } : {}) });
status(!v && bare.tier ? t("{label}: {value}", { label: t("Speed for {agent}", ag), value: dflt })
: t(v === "ultrafast" ? "{model} is sent Ultrafast for {agent}" : v ? "{model} is sent fast for {agent}" : "{model} is sent at the usual speed for {agent}", ag), "ok");
} catch (e) {
setFast(o, was.fast, was.ultra);
renderAgents();
statusErr(e);
}
}, t("Speed for {agent}", ag), "", "left");
};
return b;
};