Files
magpie/internal/gui/assets/modellimits.js
T
yetone eb56de0b59 gui: a model's Same as can be picked from models.dev's list, filtered (timfang4162 on X)
In Model settings the Same as box, which binds a provider's model to
another model's metadata (window, reply limit, levels, images, list
price), was a bare text field. A button beside it now opens the app's own
menu (not a <select>) with the models of the models.dev catalog magpie has
already synced: id, name, window and price on each line, a Filter… box over
id, name and maker, the current one ticked. A pick fills the box, so it is
staged and saved with the editor's Save like a typed id, and a value typed
afterwards still wins; nothing about how Same as is read changed.

GET /api/catalog/models lists the cache with one entry per model
(catalog.SameModel), taken from the provider that lists its window and
price. It fetches nothing. The picker lives in modellimits.js, loaded with
the model rows, so the core script budget is not raised (five comments in
renderModels cut to pay for the strings, 613 bytes of room before).

Run on mote at this tree:
- TestCatalogModelsListsTheSyncedCatalogOnce; broken by hand (last
  provider wins instead of the one with window and price) it fails with
  "kimi-k3 = {ID: ... }; want the provider that lists its window and
  price".
- model-same-pick.test.cjs, 22 cases (chromium and webkit, en zh zh-TW ja
  de, 400 and 900px): filter by maker, name and id, Enter picks, the
  current one ticked, one catalog read, no scroll, no <select>, saved as
  modelPrefs same; a failed read says so. With the filter argument taken
  out of openProtoMenu it fails. provider-same-as.test.cjs still passes.
- gofmt, go vet (darwin, linux nogui, windows) on internal/gui,
  TestCoreScriptBudget.
Full GUI suite at this tree (rebased on 5c05ac41b): the only reds are not this change's, each also red or flaky at origin/main 1f99f6f0b run alone, and filed: account-arrange #6010, account-eye #5052, account-refresh-holds #5814, accounts-own-signin's Antigravity test #6038 (a webkit access-control flake that moves between languages).
Not tried: the real app's WebKitGTK; the full suite is started after the
push.
2026-11-11 10:56:41 +08:00

101 lines
5.3 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.
// A model's own context window and max output in the provider editor's
// Model settings (sayagoodbyeplz on Discord: WorkBuddy's deepseek-v4.1-flash
// replies 128K, not models.dev's 384K). A size typed here is staged as
// model=size in Advanced's Context window and Max output, which the Save
// sends; Advanced's field is told so it says the same. The placeholder is
// what Advanced gives every model ("*"), else what is listed. app.js
// imports this when it draws a model's row, so it weighs nothing on the core.
// Its own system prompt (liucl on X) is staged as modelPrefs[id].system,
// in a box shown at the row's end (app.css's order), before unsaved.
window.modelLimitsFill = function (at, m, p, changed) {
const id = m.id;
return [...[
["contexts", "Context window", m.listed || m.context, p.contexts, t("The window agents are told {id} takes; empty: the vendor's or models.dev's", { id }), (v) => t("Context window: {v} is not a length like 128k or 1m", { v })],
["outputs", "Max output", m.listedOutput || m.output, p.outputs, t("The most a reply of {id} may hold, told to agents as their max tokens and kept to by magpie; empty: the vendor's or models.dev's", { id }), (v) => t("Max output: {v} is not a number of tokens like 32k", { v })],
].map(([k, label, listed, saved, tip, said]) => {
const box = el("label", "msame mlimit");
box.dataset.limit = k;
const now = () => parseContexts(draft[k] || "").map || {};
const own = () => now()[id];
const shownOf = () => own() ? tokenSize(own()) : "";
// a window over the listed one isn't told, so "*" shows as the listed
const placeholder = () => { const all = now()["*"]; return all ? tokenSize(k === "contexts" && listed && listed < all ? listed : all) : listed ? tokenSize(listed) : ""; };
const i = input(shownOf(), placeholder());
i.title = tip;
i.spellcheck = false;
i.onchange = () => {
const typed = i.value.trim();
const cx = parseContexts(draft[k] || "");
if (cx.error) { i.value = shownOf(); status(said(cx.error), "warn"); return; }
const n = typed === "" ? 0 : tokensTyped(typed);
if (!(n > 0) && typed !== "") { i.value = shownOf(); status(said(typed), "warn"); return; }
if (n) cx.map[id] = n; else delete cx.map[id];
draft[k] = contextsText(cx.map);
const adv = at.closest(".editor")?.querySelector("input." + k);
if (adv) { adv.value = draft[k]; adv.dispatchEvent(new Event("input")); }
i.value = shownOf();
changed();
};
i.onkeydown = (e) => { e.stopPropagation(); if (e.key === "Enter") i.blur(); else if (e.key === "Escape") { i.value = shownOf(); i.blur(); } };
box.append(el("span", "", t(label)), i);
at.append(box);
const again = () => { if (document.activeElement !== i) i.value = shownOf(); i.placeholder = placeholder(); };
return { i, own, again, saved: () => saved?.[id] || 0 };
}), modelSystemFill(at, m, changed)];
};
function modelSystemFill(at, m, changed) {
const id = m.id, prefs = draft.modelPrefs = draft.modelPrefs || {};
const own = () => (prefs[id]?.system ?? m.system) || 0;
const box = el("label", "msys");
const i = el("textarea");
i.rows = 2;
i.spellcheck = false;
i.value = own() || "";
box.title = t("Sent with every request for {id}, after the agent’s own system prompt, whichever agent sends it; empty: none", { id });
i.oninput = i.onchange = () => {
const v = i.value.trim(), x = prefs[id] = prefs[id] || {};
if (v === (m.system || "")) delete x.system; else x.system = v;
changed();
};
let was = "";
i.onfocus = () => { was = i.value; };
i.onkeydown = (e) => { e.stopPropagation(); if (e.key === "Escape") { i.value = was; i.onchange(); i.blur(); } };
box.append(el("span", "", t("System prompt")), i);
at.append(box);
return { i, own, again: () => { if (document.activeElement !== i) i.value = own() || ""; }, saved: () => m.system || 0 };
}
// The Same as box's picker (timfang4162 on X): a button beside it opens the
// models of the models.dev catalog magpie has synced (/api/catalog/models) in
// the app's menu, filtered by id, name and maker. A pick fills the box, which
// stages it as a typed one does.
let catalogModels = null;
window.modelSamePick = (box, same) => {
const label = t("Pick from models.dev"), b = el("button", "text action mpick");
b.type = "button";
b.append(svg(CHEV, 12));
b.title = label;
b.setAttribute("aria-label", label);
b.setAttribute("aria-haspopup", "menu");
const wrap = el("div", "msame-wrap");
box.replaceWith(wrap);
wrap.append(box, b);
b.onclick = async (e) => {
e.stopPropagation();
if (!catalogModels) {
try {
const got = await api("catalog/models");
if (Array.isArray(got) && got.length) catalogModels = got;
} catch {}
}
if (!catalogModels) { status(t("models.dev's list could not be read"), "warn"); return; }
const opts = catalogModels.map((c) => ({
v: c.id, name: c.id, literalName: true,
note: [c.name, c.context ? tokenSize(c.context) : "", c.input || c.cost ? "$" + c.input + " / $" + c.cost : ""].filter(Boolean).join(" · "),
find: [c.name, c.maker].filter(Boolean).join(" "),
}));
openProtoMenu(b, opts, same.value.trim(), (v) => { same.value = v; same.onchange(); }, "Pick from models.dev", "mpick-menu", "left", false, "Filter…");
};
};