Files
magpie/internal/gui/assets/agentcompact.js
T
yetoneandchiperman ju 0f07fbd0c6 agents: a first Details opening reads its settings before it slides (#5360)
The first time an agent's Details opened, sheet() measured the row before
/api/settings (and, for a full-context row, agentcompact.js) had arrived,
slid to that half height, then jumped to the full one when they landed.
The click now loads what the row needs first (the button shows Loading…),
and only then measures once and slides. A later click supersedes a load
still in flight. agentcompact.js finds #fullContextSegs on the list it is
given, so it fills a row before it is in the page. This is chiperman's
patch (8886a860a) without its core budget raise: Agents-page comments in
app.js were cut instead (app.js 1,311,357 -> 1,311,024; core 1,533,459 of
1,533,802).

agent-expand-steady's slide check looked for the height animation only in
the click's own task. A first opening now waits for the settings read, so
it looks in every frame; it still fails when no frame is animated.

Run (mote, at base 8ee85c59f):
- agent-details-ready.test.cjs (new) + agent-expand-steady: 40/40.
- Every GUI file touching ag-link/ag-exp/agent-compact, click-scroll,
  i18n-keys: all green except i18n-keys "a plugin's words are its own"
  (red on origin/main 8ee85c59f too: #5332, #5426), agent-models-redraw
  chromium 560 "page closed" (4/4 alone), mobile-web "desktop matches
  origin/main" (needs .git, which mote-run's copy has none of), and
  agent-expand-steady's sync check (fixed above).
- TestCoreScriptBudget: ok.
Broken by hand: agent-details-ready on the old app.js fails with
"Details must end without a height jump: 202.984375 -> 729.6875".
Not tried: the real app with the reporter's agents; mobile-web.

Reported-by: chiperman
Co-authored-by: chiperman ju <chipermanju@gmail.com>
2026-11-06 13:43:30 +08:00

53 lines
2.9 KiB
JavaScript

// An agent's own Long conversations (molianyin4227 on Discord: models
// differ, so one size for every agent fits none): Every agent follows the
// row above, else this agent compacts at its own size or runs to the whole
// window (settings.AgentCompacts). app.js imports this when an agent's
// sheet holds Long conversations, so it weighs nothing on the core.
window.agentCompactFill = function (list, a) {
const kind = kindOf(a);
const row = el("div", "row pref agent-compact");
row.dataset.agent = kind;
const who = el("div", "who"), box = el("div");
who.append(el("div", "name", t("Long conversations in {agent}", { agent: a.name })), el("div", "sub", t("Its own size, for models that differ; Every agent follows Long conversations above")));
row.append(who, box);
let drawn;
const draw = () => {
// the opened row is built before the settings are read; drawn again then,
// and only when its own values changed, so the page under it holds still
const s = prefs || {}, own = (s.agentCompacts || {})[kind];
const key = JSON.stringify([own, s.compactAt]);
if (key === drawn) return;
drawn = key;
const mode = !own ? "all" : own.full ? "full" : "at";
// a size of its own starts from the one every agent has
const at = own ? own.at || 0 : s.compactAt || 0;
const set = (body) => writingPrefs(api("settings/agent-compact", { agent: kind, ...body }))
.then((ns) => { prefs = ns; renderSettings(); if (row.isConnected) draw(); }).catch(settingsErr);
const size = contextsText({ "*": at || 272000 });
const sg = segs([["all", t("Every agent")], ["at", t("Compact at {n}", { n: size.toUpperCase() })], ["full", t("Full window")]], mode,
(v) => set(v === "all" ? { own: false } : { own: true, full: v === "full", at }));
const num = input(at && own ? size : "", "272k");
num.className = "words compact-num";
num.hidden = mode !== "at";
num.title = t("Compact at");
num.setAttribute("aria-label", t("Compact at"));
num.onchange = () => {
const r = parseContexts(num.value);
if (r.error || Object.keys(r.map).some((k) => k !== "*")) return status(t("Compact at: {v} is not a number of tokens like 272k", { v: num.value.trim() }), "err");
set({ own: true, full: false, at: r.map["*"] || 0 });
};
const line = el("div", "compact-at");
line.append(sg, num);
box.replaceChildren(line);
};
draw();
list.querySelector(".agent-compact")?.remove();
const above = list.querySelector("#fullContextRow");
above ? above.after(row) : list.append(row);
// every agent's size, in its label, changes when the row above is drawn
const seen = new MutationObserver(() => row.isConnected ? draw() : seen.disconnect());
// Details can build this list before attaching it to the page.
const segsBox = list.querySelector("#fullContextSegs");
if (segsBox) seen.observe(segsBox, { childList: true });
};