diff --git a/internal/gui/assets/app.js b/internal/gui/assets/app.js index dd38eb3eb..a7ed7c12b 100644 --- a/internal/gui/assets/app.js +++ b/internal/gui/assets/app.js @@ -150,8 +150,9 @@ async function api(path, body, heads, signal) { try { data = text ? JSON.parse(text) : null; } catch { throw new Error(notJSON(res, text)); } - // a state sends each fields' option list once (shareOptions, statewrite.go) - if (data?.optionLists) for (const a of data.agents) for (const f of a.fields) if (f.list) f.options = data.optionLists[f.list - 1]; + // a state sends each fields' option list once, "*" for a fastFor of + // the agent's own (shareOptions, statewrite.go) + if (data?.optionLists) for (const a of data.agents) for (const f of a.fields) if (f.list) f.options = data.optionLists[f.list - 1].map((o) => o.fastFor == "*" ? { ...o, fastFor: a.id } : o); if (!res.ok) { const err = new Error(data?.error || `${res.status} ${res.statusText}`); if (data?.code === "runtime_unavailable") { @@ -5443,11 +5444,9 @@ function boltIcon(ultra, size) { // setFast marks the model fast or not in every list it is in for the agent: // the open picker's, and the agent's fields (its tiers pick the same model) function setFast(o, fast, ultra = false) { - const same = (x) => x.ref === o.ref && x.fastFor === o.fastFor; - for (const a of state.agents) for (const f of a.fields) for (const x of f.options || []) if (same(x)) { x.fast = fast; x.ultra = ultra; } - for (const x of [...(pick?.options || []), ...(pick?.items || [])]) if (same(x)) { x.fast = fast; x.ultra = ultra; } - o.fast = fast; - o.ultra = ultra; + const mark = (x) => { if (x.ref === o.ref && x.fastFor === o.fastFor) { x.fast = fast; x.ultra = ultra; } }; + for (const a of state.agents) for (const f of a.fields) (f.options || []).forEach(mark); + [...(pick?.options || []), ...(pick?.items || []), o].forEach(mark); // the field's button says it of the model it has const anchor = pick?.anchor; if (anchor?.classList.contains("field") && optionFor(pick.field, pick.field.value)?.ref === o.ref) { diff --git a/internal/gui/statewrite.go b/internal/gui/statewrite.go index 81ee430f9..2bd58c623 100644 --- a/internal/gui/statewrite.go +++ b/internal/gui/statewrite.go @@ -142,7 +142,8 @@ func (l lineless) Write(b []byte) (int, error) { // shareOptions is s with each fields' option list sent once, in // OptionLists, and named by the field's List: a list two fields offer -// alike is one entry. s's own agents are left as they were. The page +// alike is one entry, a FastFor naming the field's own agent sent as +// fastSelf. s's own agents are left as they were. The page // puts each field's list back as it reads the state (api in app.js). func shareOptions(s stateJSON) stateJSON { if s.OptionLists != nil { @@ -158,6 +159,7 @@ func shareOptions(s stateJSON) stateJSON { if len(opts) < 2 { continue } + opts = fastForSelf(opts, agents[i].ID) k := slices.IndexFunc(lists, func(l []agent.Option) bool { return sameOptions(l, opts) }) if k < 0 { lists = append(lists, opts) @@ -177,6 +179,27 @@ func shareOptions(s stateJSON) stateJSON { return s } +// fastSelf is the FastFor an option list is sent with for the agent +// whose list it is: each agent's catalog differed only there, so none +// were alike. The page puts the agent's id back (api in app.js). +const fastSelf = "*" + +// fastForSelf is opts with each FastFor that names id as fastSelf; opts +// itself when none does, a copy otherwise. +func fastForSelf(opts []agent.Option, id string) []agent.Option { + copied := false + for k := range opts { + if opts[k].FastFor != id || id == "" { + continue + } + if !copied { + opts, copied = slices.Clone(opts), true + } + opts[k].FastFor = fastSelf + } + return opts +} + // sameOptions reports whether a and b are the same options, in the same // order. func sameOptions(a, b []agent.Option) bool { diff --git a/internal/gui/statewrite_test.go b/internal/gui/statewrite_test.go index 7a329304c..f75925d43 100644 --- a/internal/gui/statewrite_test.go +++ b/internal/gui/statewrite_test.go @@ -133,7 +133,12 @@ func TestStateSendsEachOptionListOnce(t *testing.T) { {ID: "c", Fields: []fieldJSON{{Key: "model", Options: other}}}, {ID: "d", Fields: []fieldJSON{{Key: "model", Options: icons}}}, {ID: "e", Fields: []fieldJSON{{Key: "model", Options: []agent.Option{}}}}, + // each agent's catalog names it as the one its fast models are + // for: alike all the same + {ID: "f", Fields: []fieldJSON{{Key: "model", Options: fastFor("f")}}}, + {ID: "g", Fields: []fieldJSON{{Key: "model", Options: fastFor("g")}}}, }} + fCatalog := s.Agents[5].Fields[0].Options var before bytes.Buffer if err := json.NewEncoder(&before).Encode(s); err != nil { t.Fatal(err) @@ -154,8 +159,8 @@ func TestStateSendsEachOptionListOnce(t *testing.T) { if err := json.Unmarshal(w.body.Bytes(), &sent); err != nil { t.Fatal(err) } - if len(sent.OptionLists) != 3 { - t.Fatalf("%d option lists sent, want 3 (the catalog, the fast one's, the other icons'): %s", len(sent.OptionLists), w.body.Bytes()) + if len(sent.OptionLists) != 4 { + t.Fatalf("%d option lists sent, want 4 (the catalog, codex's fast one's, the other icons', f's and g's): %s", len(sent.OptionLists), w.body.Bytes()) } lists := map[string]int{} for _, a := range sent.Agents { @@ -174,6 +179,12 @@ func TestStateSendsEachOptionListOnce(t *testing.T) { if lists["c/model"] == lists["a/model"] || lists["d/model"] == lists["a/model"] { t.Fatalf("a list unlike the catalog was sent as it: %v", lists) } + if lists["f/model"] == 0 || lists["f/model"] != lists["g/model"] || lists["f/model"] == lists["a/model"] { + t.Fatalf("catalogs differing only by their own agent's fastFor are not one list: %v", lists) + } + if fCatalog[1].FastFor != "f" { + t.Fatal("sharing the list changed the agent's own options") + } if lists["a/effort"] != 0 || lists["e/model"] != 0 { t.Fatalf("a list of one or none was moved: %v", lists) } @@ -187,7 +198,20 @@ func TestStateSendsEachOptionListOnce(t *testing.T) { for _, f := range a.(map[string]any)["fields"].([]any) { f := f.(map[string]any) if l, ok := f["list"].(float64); ok { - f["options"] = ol[int(l)-1] + var opts []any + for _, o := range ol[int(l)-1].([]any) { + o := o.(map[string]any) + if o["fastFor"] == "*" { + c := map[string]any{} + for k, v := range o { + c[k] = v + } + c["fastFor"] = a.(map[string]any)["id"] + o = c + } + opts = append(opts, o) + } + f["options"] = opts delete(f, "list") } } @@ -209,3 +233,8 @@ func TestStateSendsEachOptionListOnce(t *testing.T) { t.Fatal("writing the state changed the caller's agents") } } + +// fastFor is a catalog whose fast model is sent fast for id. +func fastFor(id string) []agent.Option { + return []agent.Option{{Value: "x/1"}, {Value: "x/2", FastFor: id}, {Value: "x/3", FastFor: "codex"}} +} diff --git a/internal/gui/tests/state-option-lists.test.cjs b/internal/gui/tests/state-option-lists.test.cjs index ec3a8d809..225cca44a 100644 --- a/internal/gui/tests/state-option-lists.test.cjs +++ b/internal/gui/tests/state-option-lists.test.cjs @@ -5,7 +5,8 @@ // from 1, and api() puts the list back before anything reads the state. // Two agents' model pickers sharing one list each show it whole, a field // with its own options keeps them, and the state kept from the last read -// (?last=1), shared the same way, draws too. Chromium and WebKit, wide +// (?last=1), shared the same way, draws too. A fastFor sent as "*" is +// the agent's own id again in each agent's list. Chromium and WebKit, wide // and narrow, in every language. const assert = require("node:assert/strict"); const fs = require("node:fs/promises"); @@ -18,6 +19,8 @@ const catalog = [ { value: "magpie/codex/gpt-5.5", ref: "codex/gpt-5.5", label: "GPT-5.5", note: "", group: "ChatGPT" }, { value: "magpie/zai/glm-5", ref: "zai/glm-5", label: "GLM-5", note: "", group: "Z.ai" }, { value: "magpie/kimi/k2", ref: "kimi/k2", label: "Kimi K2", note: "", group: "Kimi" }, + // sent fast for the agent whose list it is: "*" on the wire + { value: "magpie/codex/gpt-5.5-fast", ref: "codex/gpt-5.5-fast", label: "GPT-5.5 Fast", note: "", group: "ChatGPT", fastFor: "*" }, ]; const effort = [{ value: "low", note: "" }, { value: "high", note: "" }]; @@ -81,10 +84,11 @@ for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", await page.goto("http://magpie.test/?view=agents"); // drawn from the kept state, before the fresh one comes await page.locator('.row.agent[data-id="crush"]').waitFor(); - const read = () => page.evaluate(() => state.agents.map((a) => [a.id, a.fields.map((f) => [f.key, (f.options || []).map((o) => o.value)])])); + const read = () => page.evaluate(() => state.agents.map((a) => [a.id, a.fields.map((f) => [f.key, (f.options || []).map((o) => o.value + (o.fastFor ? " " + o.fastFor : ""))])])); + const own = (id) => catalog.map((o) => o.value + (o.fastFor ? " " + id : "")); const want = [ - ["codex", [["model", catalog.map((o) => o.value)], ["effort", ["low", "high"]]]], - ["crush", [["model", catalog.map((o) => o.value)]]], + ["codex", [["model", own("codex")], ["effort", ["low", "high"]]]], + ["crush", [["model", own("crush")]]], ]; assert.deepEqual(await read(), want, "the kept state's lists put back"); release(); @@ -99,6 +103,13 @@ for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", await page.keyboard.press("Escape"); await page.locator("#pop").waitFor({ state: "hidden" }); } + // fast for one agent is fast for that agent only, though the list is shared + const fast = await page.evaluate(() => { + const opt = (id) => state.agents.find((a) => a.id === id).fields[0].options.find((o) => o.fastFor); + setFast(opt("codex"), true); + return [opt("codex").fast, !!opt("crush").fast]; + }); + assert.deepEqual(fast, [true, false], "codex's fast leaves crush's alone"); assert.deepEqual(errors, []); await page.context().close(); });