diff --git a/internal/gui/assets/app.js b/internal/gui/assets/app.js index 22d4b6952..f12badc6f 100644 --- a/internal/gui/assets/app.js +++ b/internal/gui/assets/app.js @@ -9598,7 +9598,7 @@ function drawEditor(p, presetID) { const cx = input(draft.contexts || "", t("e.g. 128k · or gpt-6=1m, comma separated")); advIn.append(...field(t("Context window"), contextPicks(p, cx), t("How long a request the models take, told to the agents; empty leaves it to the vendor and models.dev"))); advIn.append(...outputField()); - advIn.append(...compactField()); + advIn.append(...compactField(p)); if (p.carriesCompaction) advIn.append(...nativeCompactionField(() => true)[0]); advIn.append(...field(t("Fallback"), renderFallback(p), fallbackHint(p))); const proxies = el("div", "stack"); @@ -10107,7 +10107,7 @@ function drawEditor(p, presetID) { if (!decideOnly(p || pr)) advIn.append(...field(t("Context window"), contextPicks(p, cx), t("How long a request the models take, told to the agents; empty leaves it to the vendor and models.dev"))); if (!decideOnly(p || pr)) advIn.append(...outputField()); if (!decideOnly(p || pr)) { - advIn.append(...compactField()); + advIn.append(...compactField(p)); const [row, refresh] = nativeCompactionField(carriesCompaction); advIn.append(...row); showCompaction = refresh; @@ -10423,7 +10423,7 @@ function contextsText(cx) { function contextPicks(p, cx) { cx.classList.add("contexts"); const size = (n) => contextsText({ "*": n }); - const picks = [128e3, 200e3, 256e3, 400e3, 512e3, 1e6].map((n) => ({ label: size(n).toUpperCase(), value: size(n) })); + const picks = [128e3, 200e3, 256e3, 400e3, 512e3, 1e6]; // above the window its vendor says, not the one set here: a model set to // its most still offers it const big = (p?.models || []).filter((m) => m.max > ("listed" in m ? m.listed || 0 : m.context || 0)); @@ -10438,57 +10438,49 @@ function contextPicks(p, cx) { title: big.map((m) => `${m.id}: ${size(m.max).toUpperCase()}`).join("\n"), value, most: true, }); } - const row = el("div", "cxpicks"); - const same = sameContexts; - const light = () => { - for (const [i, b] of [...row.children].entries()) { - const v = picks[i].value; - b.classList.toggle("on", !!cx.value.trim() && same(cx.value, typeof v === "function" ? v() : v)); - } - }; - for (const pk of picks) { - const b = el("button", "cxpick" + (pk.most ? " most" : ""), pk.label); - b.type = "button"; - if (pk.title) b.title = pk.title; - b.onclick = () => { - cx.value = typeof pk.value === "function" ? pk.value() : pk.value; - draft.contexts = cx.value; - light(); - limitsSaid(); - }; - row.append(b); - } - cx.oninput = () => { draft.contexts = cx.value; light(); limitsSaid(); }; - light(); - const wrap = el("div", "cxfield"); - wrap.append(cx, row); - return wrap; + return sizePicks(cx, "contexts", picks, null, limitsSaid); } // limitsSaid: Model settings' rows follow Advanced const limitsSaid = () => { for (const f of Object.values(draft?.onLimits || {})) f(); }; +// sizePicks: a field's usual sizes as chips under it (or picks of its own, +// whose value is a function); a chip sets its size for all the models. +// Compact at's (given p), with models to name, opens All models and each +// model instead, so several get one size at once (Yanzhongixuy on X). +function sizePicks(box, key, ns, p, after = () => {}) { + const row = el("div", "cxpicks"), now = () => parseContexts(box.value).map; + const picks = ns.map((n) => n.value ? n : { n, label: contextsText({ "*": n }).toUpperCase(), value: () => contextsText({ "*": n }) }); + const light = () => picks.forEach((pk, i) => row.children[i].classList.toggle("on", !!box.value.trim() && (p && Object.values(now() || {}).includes(pk.n) || sameContexts(box.value, pk.value())))); + const put = (v) => { box.value = draft[key] = v; light(); after(); }; + for (const pk of picks) { + const b = el("button", "cxpick" + (pk.most ? " most" : ""), pk.label), n = pk.n; + b.type = "button"; + if (pk.title) b.title = pk.title; + b.onclick = () => { + const map = now(), ids = [...new Set([...(p?.models?.length ? servedIds(p) : []), ...Object.keys(map || {})])].filter((id) => id !== "*"); + if (!p || !ids.length || !map) return put(pk.value()); + const set = (on) => { + const m = now() || {}; + for (const id of ["*", ...ids]) if (on.includes(id)) m[id] = n; else if (m[id] === n) delete m[id]; + put(contextsText(m)); + }; + openProtoMenu(b, [{ v: "*", name: "All models" }, ...ids.map((id) => ({ v: id, name: id, literalName: true }))], ["*", ...ids].filter((id) => map[id] === n), set, t("Compact at {n}", { n: pk.label }), "", "left", true, ids.length > 8 ? "Search models" : ""); + }; + row.append(b); + } + box.oninput = () => { draft[key] = box.value; light(); after(); }; + light(); + const wrap = el("div", "cxfield"); + wrap.append(box, row); + return wrap; +} + // outputField: the editor's Max output, set as the window is (ARNO, #875) function outputField() { const ox = input(draft.outputs || "", t("e.g. 32k · or gpt-6=128k, comma separated")); ox.classList.add("outputs"); - const sizes = [8e3, 16e3, 32e3, 64e3, 128e3].map((n) => contextsText({ "*": n })); - const row = el("div", "cxpicks"); - const same = sameContexts; - const light = () => { - for (const [i, b] of [...row.children].entries()) b.classList.toggle("on", !!ox.value.trim() && same(ox.value, sizes[i])); - }; - for (const v of sizes) { - const b = el("button", "cxpick", v.toUpperCase()); - b.type = "button"; - b.onclick = () => { ox.value = v; draft.outputs = v; light(); limitsSaid(); }; - row.append(b); - } - ox.oninput = () => { draft.outputs = ox.value; light(); limitsSaid(); }; - light(); - const wrap = el("div", "cxfield"); - wrap.append(ox, row); - return field(t("Max output"), wrap, t("The most a reply may hold, told to the agents as their max tokens and sent as max_tokens on Anthropic's API when the agent names none; empty leaves it to the vendor and models.dev")); + return field(t("Max output"), sizePicks(ox, "outputs", [8e3, 16e3, 32e3, 64e3, 128e3], null, limitsSaid), t("The most a reply may hold, told to the agents as their max tokens and sent as max_tokens on Anthropic's API when the agent names none; empty leaves it to the vendor and models.dev")); } // nativeCompactionField: Codex's own compaction on the provider (#1099), @@ -10506,25 +10498,10 @@ function nativeCompactionField(show) { // compactField is the editor's Compact at (#876): where Codex and Claude // Code compact a conversation on its models when their window is longer, // one for all of them and model=size for one, as the window is set. -function compactField() { +function compactField(p) { const cp = input(draft.compacts || "", t("e.g. 500k · or deepseek-v4-flash=272k, comma separated")); cp.classList.add("compacts"); - const sizes = [128e3, 200e3, 272e3, 500e3, 1e6].map((n) => contextsText({ "*": n })); - const row = el("div", "cxpicks"); - const same = sameContexts; - const light = () => { - for (const [i, b] of [...row.children].entries()) b.classList.toggle("on", !!cp.value.trim() && same(cp.value, sizes[i])); - }; - for (const v of sizes) { - const b = el("button", "cxpick", v.toUpperCase()); - b.type = "button"; - b.onclick = () => { cp.value = v; draft.compacts = v; light(); }; - row.append(b); - } - cp.oninput = () => { draft.compacts = cp.value; light(); }; - light(); - const wrap = el("div", "cxfield"); - wrap.append(cp, row); + const wrap = sizePicks(cp, "compacts", [128e3, 200e3, 272e3, 500e3, 1e6], p); return field(t("Compact at"), wrap, t("Where Codex, Claude Code and Grok Build compact a long conversation on these models; empty follows Long conversations, in their settings on the Agents page (else in Settings), and a size at or above a model's window runs it to its whole window")); } diff --git a/internal/gui/tests/compact-at.test.cjs b/internal/gui/tests/compact-at.test.cjs index d82c813e6..34ae9d362 100644 --- a/internal/gui/tests/compact-at.test.cjs +++ b/internal/gui/tests/compact-at.test.cjs @@ -9,8 +9,10 @@ // owner merged the two, 11-12), takes a size of its own beside Compact at / // Full window; a typed size posts settings/agent-compact for that agent with // it, and no click or typing scrolls the page. The editor's Compact at -// says that is where its empty value follows (#3425). -// English and Chinese, Chromium and WebKit; no backend, the API is faked here. +// says that is where its empty value follows (#3425). A size's chip, with +// models to name, opens a menu of All models and each model, so several get +// one size at once (Yanzhongixuy on X), at a phone's width too. +// English and Chinese (the chip's menu in every language), Chromium and WebKit; no backend, the API is faked here. const assert = require("node:assert/strict"); const fs = require("node:fs/promises"); const path = require("node:path"); @@ -66,17 +68,20 @@ function serve(lang, posts) { } const words = { - en: { field: "Compact at", save: "Save", bad: "Compact at: lots is not a number of tokens like 272k", at: (n) => `Compact at ${n}`, full: "Full window", follows: "empty follows Long conversations, in their settings on the Agents page (else in Settings)", notSettings: "Settings → Long conversations" }, - zh: { field: "压缩阈值", save: "保存", bad: "压缩阈值:lots 不是 272k 这样的 token 数", at: (n) => `${n} 时压缩`, full: "完整窗口", follows: "留空按 Agent 页里它们设置中的「长对话」(否则在「设置」里)", notSettings: "设置 → 长对话" }, + en: { all: "All models", head: (n) => `Compact at ${n}`, field: "Compact at", save: "Save", bad: "Compact at: lots is not a number of tokens like 272k", at: (n) => `Compact at ${n}`, full: "Full window", follows: "empty follows Long conversations, in their settings on the Agents page (else in Settings)", notSettings: "Settings → Long conversations" }, + "zh-TW": { all: "全部模型", head: (n) => `${n} 時壓縮`, save: "儲存" }, + ja: { all: "すべてのモデル", head: (n) => `${n} で圧縮`, save: "保存" }, + de: { all: "Alle Modelle", head: (n) => `Bei ${n} komprimieren`, save: "Speichern" }, + zh: { all: "全部模型", head: (n) => `${n} 时压缩`, field: "压缩阈值", save: "保存", bad: "压缩阈值:lots 不是 272k 这样的 token 数", at: (n) => `${n} 时压缩`, full: "完整窗口", follows: "留空按 Agent 页里它们设置中的「长对话」(否则在「设置」里)", notSettings: "设置 → 长对话" }, }; for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) { - for (const lang of ["en", "zh"]) { + for (const lang of ["en", "zh", "zh-TW", "ja", "de"]) { const w = words[lang]; - const open = async (t, view, height = 720) => { + const open = async (t, view, height = 720, width = 900) => { const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: process.env.PLAYWRIGHT_CHANNEL || "chromium" })); t.after(() => browser.close()); - const page = await (await browser.newContext({ viewport: { width: 900, height }, reducedMotion: "reduce" })).newPage(); + const page = await (await browser.newContext({ viewport: { width, height }, reducedMotion: "reduce" })).newPage(); page.setDefaultTimeout(5000); page.setDefaultNavigationTimeout(60000); const errors = [], posts = []; @@ -87,7 +92,7 @@ for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", return { page, errors, posts }; }; - test(`${engine} ${lang}: a provider's compaction threshold is set in its editor`, async (t) => { + if (w.field) test(`${engine} ${lang}: a provider's compaction threshold is set in its editor`, async (t) => { const { page, errors, posts } = await open(t, "providers"); await page.locator('.row.provider[data-id="deepseek"]').click(); assert(await page.getByText(w.field, { exact: true }).isVisible(), "the field is there"); @@ -118,7 +123,52 @@ for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", assert.deepEqual(errors, []); }); - test(`${engine} ${lang}: Long conversations compacts at a size the user types`, async (t) => { + for (const width of [900, 400]) test(`${engine} ${lang} ${width}px: a size's chip gives it to the models ticked`, async (t) => { + const { page, errors, posts } = await open(t, "providers", 720, width); + await page.locator('.row.provider[data-id="deepseek"]').click(); + const box = page.locator("input.compacts"); + const picks = page.locator("input.compacts + .cxpicks .cxpick"); + // flash is at 272K, so its chip is lit, and 500K for all of them + assert.equal(await picks.filter({ hasText: "272K" }).evaluate((b) => b.classList.contains("on")), true); + assert.equal(await picks.filter({ hasText: "128K" }).evaluate((b) => b.classList.contains("on")), false); + await picks.filter({ hasText: "272K" }).scrollIntoViewIfNeeded(); + const top = await page.evaluate(() => document.scrollingElement.scrollTop); + await picks.filter({ hasText: "272K" }).click(); + const menu = page.locator(".proto-menu"); + await menu.waitFor(); + assert.equal(await menu.locator(".pm-head").textContent(), w.head("272K")); + assert.deepEqual(await menu.locator(".pm-name").allTextContents(), [w.all, "deepseek-v4-flash", "deepseek-v4-pro"]); + const item = (name) => menu.locator(".pm-item").filter({ has: page.locator(".pm-name", { hasText: new RegExp(`^${name}$`) }) }); + const ticked = () => menu.locator('.pm-item[aria-checked="true"] .pm-name').allTextContents(); + assert.deepEqual(await ticked(), ["deepseek-v4-flash"]); + assert.equal(await menu.locator("select").count() + await page.locator(".cxfield select").count(), 0, "no native select"); + await item("deepseek-v4-pro").click(); + assert.equal(await box.inputValue(), "500k, deepseek-v4-flash=272k, deepseek-v4-pro=272k"); + await item(w.all).click(); + assert.equal(await box.inputValue(), "272k, deepseek-v4-flash=272k, deepseek-v4-pro=272k"); + await item("deepseek-v4-flash").click(); + assert.equal(await box.inputValue(), "272k, deepseek-v4-pro=272k"); + assert.deepEqual(await ticked(), [w.all, "deepseek-v4-pro"]); + assert(await menu.isVisible(), "the menu stays open while ticking"); + const r = await menu.boundingBox(); + assert(r.x >= 0 && r.x + r.width <= width, "the menu fits the window"); + assert.equal(await page.evaluate(() => document.scrollingElement.scrollTop), top, "ticking scrolled the page"); + await page.keyboard.press("Escape"); + await menu.waitFor({ state: "detached" }); + // another size leaves the models at 272K as they are + await picks.filter({ hasText: "1M" }).click(); + await item("deepseek-v4-flash").click(); + assert.equal(await box.inputValue(), "272k, deepseek-v4-flash=1m, deepseek-v4-pro=272k"); + await page.keyboard.press("Escape"); + await menu.waitFor({ state: "detached" }); + await page.getByRole("button", { name: w.save, exact: true }).click(); + await page.waitForFunction(() => !document.querySelector("input.compacts")); + assert.deepEqual(posts.map((p) => p.path), ["/api/provider/save"]); + assert.deepEqual(posts[0].body.compacts, { "*": 272000, "deepseek-v4-flash": 1000000, "deepseek-v4-pro": 272000 }); + assert.deepEqual(errors, []); + }); + + if (w.field) test(`${engine} ${lang}: Long conversations compacts at a size the user types`, async (t) => { const { page, errors, posts } = await open(t, "agents"); await openAgentSettings(page, "claude"); const row = page.locator('.row.agent-compact[data-agent="claude"]');