models: a provider's model can go by a name of the user's, and offer only some of its reasoning levels — Claude Opus 5.5 · Claude Code can be Opus 5.5, and a model with six levels can offer just low, medium and high. Both are kept in settings.json by provider/model (modelNames, modelEfforts), apart from the vendor's list, so a Refresh, a restart or an upgrade leaves them, and the same model from another provider keeps its own; a provider renamed takes them along. They are applied where the catalog is made, so the gateway's /v1/models, the lists magpie writes into the agents (Codex's model catalog, ZCode, Pi, OpenCode, OMP …) and magpie's own pages all show them, and a change rewrites the agents' lists as any other change of the catalog does. Only what is shown changes: the id, where requests go, and the levels the vendor is asked at stay as they were; a found routing group keeps the model's own name. The provider's editor has Names & levels under its models — a name typed in place, a tick per level, Restore default — and the terminal has magpie model name <provider/model> <name>|--reset and magpie model efforts <provider/model> low,medium,high|--reset. Issue #92
This commit is contained in:
@@ -0,0 +1,87 @@
|
||||
package agent
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/yetone/magpie/internal/catalog"
|
||||
"github.com/yetone/magpie/internal/provider"
|
||||
)
|
||||
|
||||
// A model the user names, or keeps some reasoning levels of, is named so
|
||||
// and offered only those in the lists magpie wrote into the agents' files,
|
||||
// rewritten as soon as it is changed; a reset gives the model's own back.
|
||||
func TestModelNameAndLevelsReachAgentFiles(t *testing.T) {
|
||||
home := syncHome(t)
|
||||
os.WriteFile(catalog.CachePath(), []byte(`{"zai":{"models":{"glm-4.6":{"id":"glm-4.6","name":"GLM-4.6","limit":{"context":204800},
|
||||
"reasoning_options":[{"type":"effort","values":["low","medium","high"]}]}}}}`), 0o644)
|
||||
catalog.Reset()
|
||||
if err := provider.Save(provider.Provider{ID: "relay", Name: "Relay", Catalog: "zai", Key: "k", Chat: "http://127.0.0.1:1/v1", Models: []string{"glm-4.6"}}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
piModels := filepath.Join(home, ".pi", "agent", "models.json")
|
||||
writeFile(t, piModels, `{"providers":{"magpie":{"name":"magpie","models":[]}}}`)
|
||||
codexDir := filepath.Join(home, ".codex")
|
||||
codexCat := filepath.Join(codexDir, "magpie-models.json")
|
||||
writeFile(t, filepath.Join(codexDir, "config.toml"), "model = \"relay/glm-4.6\"\nmodel_provider = \"magpie\"\nmodel_catalog_json = \""+codexCat+"\"\n")
|
||||
writeFile(t, codexCat, `{"models":[]}`)
|
||||
catalog.Changed = SyncCatalog
|
||||
t.Cleanup(func() { catalog.Changed = nil })
|
||||
|
||||
if err := provider.SetModelName("relay/glm-4.6", "GLM"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := provider.SetModelEfforts("relay/glm-4.6", []string{"low", "high"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var pi struct {
|
||||
Providers map[string]struct {
|
||||
Models []map[string]any `json:"models"`
|
||||
} `json:"providers"`
|
||||
}
|
||||
if err := json.Unmarshal([]byte(readFile(piModels)), &pi); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ms := pi.Providers["magpie"].Models
|
||||
if len(ms) != 1 || ms[0]["id"] != "relay/glm-4.6" || ms[0]["name"] != "GLM" {
|
||||
t.Fatalf("pi models.json: %v", ms)
|
||||
}
|
||||
var cx struct {
|
||||
Models []struct {
|
||||
Slug string `json:"slug"`
|
||||
Name string `json:"display_name"`
|
||||
Efforts []struct {
|
||||
Effort string `json:"effort"`
|
||||
} `json:"supported_reasoning_levels"`
|
||||
} `json:"models"`
|
||||
}
|
||||
if err := json.Unmarshal([]byte(readFile(codexCat)), &cx); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(cx.Models) != 1 || cx.Models[0].Slug != "relay/glm-4.6" || cx.Models[0].Name != "GLM" {
|
||||
t.Fatalf("codex catalog: %+v", cx.Models)
|
||||
}
|
||||
var levels []string
|
||||
for _, e := range cx.Models[0].Efforts {
|
||||
levels = append(levels, e.Effort)
|
||||
}
|
||||
if strings.Join(levels, ",") != "low,high" {
|
||||
t.Fatalf("codex levels: %v", levels)
|
||||
}
|
||||
|
||||
if err := provider.SetModelName("relay/glm-4.6", ""); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := provider.SetModelEfforts("relay/glm-4.6", nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got := readFile(codexCat); !strings.Contains(got, `"GLM-4.6 · Relay"`) || !strings.Contains(got, `"medium"`) {
|
||||
t.Fatalf("codex catalog after reset:\n%s", got)
|
||||
}
|
||||
if got := readFile(piModels); !strings.Contains(got, `"GLM-4.6 · Relay"`) {
|
||||
t.Fatalf("pi models.json after reset:\n%s", got)
|
||||
}
|
||||
}
|
||||
@@ -73,11 +73,7 @@ func magpieModels(agent string) []catalog.Model {
|
||||
var out []catalog.Model
|
||||
shown, _ := provider.CatalogFor(agent)
|
||||
for _, e := range shown {
|
||||
by := e.Provider.Name
|
||||
if e.Group != "" {
|
||||
by = "routing group"
|
||||
}
|
||||
out = append(out, catalog.Model{ID: e.ID, Name: e.Name + " · " + by, Provider: firstOf(e.Provider.Catalogs()), Efforts: e.Efforts, Images: e.Images, Context: e.Context, Output: e.Output})
|
||||
out = append(out, catalog.Model{ID: e.ID, Name: e.Label(), Provider: firstOf(e.Provider.Catalogs()), Efforts: e.Efforts, Images: e.Images, Context: e.Context, Output: e.Output})
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
@@ -0,0 +1,64 @@
|
||||
package gateway
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/yetone/magpie/internal/catalog"
|
||||
"github.com/yetone/magpie/internal/provider"
|
||||
)
|
||||
|
||||
// The name the user gave a provider's model, and the reasoning levels they
|
||||
// kept of it, are what /v1/models lists; the same model from another
|
||||
// provider keeps its own, and the id stays the same.
|
||||
func TestModelsListUsersNameAndLevels(t *testing.T) {
|
||||
fresh(t)
|
||||
os.MkdirAll(filepath.Dir(catalog.CachePath()), 0o755)
|
||||
data := `{"a":{"models":{"sol":{"id":"sol","name":"Sol","reasoning_options":[{"type":"effort","values":["low","medium","high","max"]}]}}},
|
||||
"b":{"models":{"sol":{"id":"sol","name":"Sol","reasoning_options":[{"type":"effort","values":["low","medium","high","max"]}]}}}}`
|
||||
if err := os.WriteFile(catalog.CachePath(), []byte(data), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
catalog.Reset()
|
||||
t.Cleanup(catalog.Reset)
|
||||
for _, id := range []string{"a", "b"} {
|
||||
if err := provider.Save(provider.Provider{ID: id, Name: strings.ToUpper(id), Catalog: id, Key: "k", Chat: "http://127.0.0.1:1/v1", Models: []string{"sol"}}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
if err := provider.SetModelName("a/sol", "Sol 5"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := provider.SetModelEfforts("a/sol", []string{"low", "high"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
rec := httptest.NewRecorder()
|
||||
New().Handler().ServeHTTP(rec, httptest.NewRequest("GET", "/v1/models", nil))
|
||||
var out struct {
|
||||
Data []struct {
|
||||
ID string `json:"id"`
|
||||
Name string `json:"display_name"`
|
||||
Levels []struct {
|
||||
Effort string `json:"effort"`
|
||||
} `json:"supported_reasoning_levels"`
|
||||
} `json:"data"`
|
||||
}
|
||||
if err := json.Unmarshal(rec.Body.Bytes(), &out); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got := map[string]string{}
|
||||
for _, m := range out.Data {
|
||||
var ls []string
|
||||
for _, l := range m.Levels {
|
||||
ls = append(ls, l.Effort)
|
||||
}
|
||||
got[m.ID] = m.Name + " " + strings.Join(ls, ",")
|
||||
}
|
||||
if got["a/sol"] != "Sol 5 low,high" || got["b/sol"] != "Sol low,medium,high,max" {
|
||||
t.Fatalf("%v\n%s", got, rec.Body.String())
|
||||
}
|
||||
}
|
||||
@@ -966,6 +966,19 @@ body.window .ag-sk-icon { width: 22px; height: 22px; border-radius: 6px; }
|
||||
.mfoot .text.action { flex: none; }
|
||||
.mfoot input { max-width: 200px; height: 27px !important; }
|
||||
.mfoot .text { padding: 2px 7px; }
|
||||
.mfoot .text.on { color: var(--fg); background: var(--card-2); }
|
||||
|
||||
/* the models' own names and reasoning levels, one row each */
|
||||
.mnames { display: grid; gap: 4px; max-height: 240px; overflow-y: auto; padding: 1px; }
|
||||
.mnames[hidden] { display: none; }
|
||||
.mnames > .hint { font-size: 11px; color: var(--faint); }
|
||||
.mname { display: flex; flex-wrap: wrap; align-items: center; gap: 4px 10px; padding: 5px 8px; border: 1px solid var(--line); border-radius: 8px; background: var(--card); }
|
||||
.mname .mwho { display: flex; align-items: center; gap: 8px; flex: 1 1 220px; min-width: 0; }
|
||||
.mname .mwho input { flex: 1; min-width: 0; max-width: 220px; height: 25px; }
|
||||
.mname .mwho code { font-size: 11px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; }
|
||||
.mname .mlevels { display: flex; flex-wrap: wrap; gap: 2px 10px; font-size: 11.5px; }
|
||||
.editor .mname label.tick { gap: 4px; }
|
||||
.mname .text { flex: none; padding: 2px 7px; font-size: 11.5px; }
|
||||
|
||||
/* ---------- settings ---------- */
|
||||
#view-settings .row-head { margin: 4px 0 6px; padding: 0 2px; }
|
||||
|
||||
@@ -31,6 +31,7 @@ let usage = null; // last usage summary
|
||||
let pick = null; // { agent, field, options, items, cursor, anchor }
|
||||
let editing = null; // provider id being edited; { preset } or { custom: true } for a new one
|
||||
let draft = null; // the editor's working copy
|
||||
let naming = null; // the provider whose models' names and levels are open in its editor
|
||||
let adding = false; // the preset sheet is open
|
||||
let importing = null; // a magpie://import link waiting for a yes: { provider, error, replaces }
|
||||
let importingApps = null; // the Import from other apps dialog: { sources, picks }
|
||||
@@ -2768,6 +2769,7 @@ function renderEndpoints(p, src) {
|
||||
function renderModels(p) {
|
||||
const box = el("div", "models");
|
||||
const chips = el("div", "mchips");
|
||||
const names = el("div", "mnames");
|
||||
const q = p.models.length > 24 ? input("", t("filter {n} models…", { n: p.models.length })) : null;
|
||||
const draw = () => {
|
||||
chips.replaceChildren();
|
||||
@@ -2775,10 +2777,11 @@ function renderModels(p) {
|
||||
let shown = 0;
|
||||
for (const m of p.models) {
|
||||
const on = draft.chosen.includes(m.id);
|
||||
if (f && !m.id.toLowerCase().includes(f) && !(m.name || "").toLowerCase().includes(f) && !on) continue;
|
||||
if (f && !m.id.toLowerCase().includes(f) && !(m.name || "").toLowerCase().includes(f) && !(m.default || "").toLowerCase().includes(f) && !on) continue;
|
||||
const c = el("button", "mchip" + (on ? " on" : ""));
|
||||
c.append(el("span", "", m.name && m.name !== m.id ? m.name : m.id));
|
||||
if (m.name && m.name !== m.id) c.title = m.id;
|
||||
if (m.default) c.title = `${m.id} · ${m.default}`;
|
||||
else if (m.name && m.name !== m.id) c.title = m.id;
|
||||
c.onclick = () => { draft.chosen = on ? draft.chosen.filter((x) => x !== m.id) : [...draft.chosen, m.id]; draw(); };
|
||||
chips.append(c);
|
||||
if (++shown >= 80 && !f) { chips.append(el("span", "hint", t("… {n} more, filter to find them", { n: p.models.length - shown }))); break; }
|
||||
@@ -2792,11 +2795,66 @@ function renderModels(p) {
|
||||
chips.append(c);
|
||||
}
|
||||
if (!p.models.length && !draft.chosen.length) chips.append(el("span", "hint", t("The vendor's list is empty. Refresh, or type a model id.")));
|
||||
drawNames();
|
||||
why.textContent = draft.unlisted ? t("Agents don't see them: only the routing groups they are in use them.")
|
||||
: t(draft.chosen.length ? "Agents see the models picked." : "None picked: agents see the vendor's list, up to {n}.", { n: 24 });
|
||||
};
|
||||
// the names and reasoning levels of the models agents see: saved at once,
|
||||
// apart from the editor's Save, as they change nothing but what is shown
|
||||
const drawNames = () => {
|
||||
names.replaceChildren();
|
||||
names.hidden = naming !== p.id;
|
||||
if (names.hidden) return;
|
||||
const ids = draft.chosen.length ? draft.chosen : p.models.filter((m) => m.on).map((m) => m.id);
|
||||
if (!ids.length) { names.append(el("span", "hint", t("Pick a model first."))); return; }
|
||||
for (const id of ids) {
|
||||
const m = p.models.find((x) => x.id === id) || { id, name: id };
|
||||
const own = m.default || m.name || m.id;
|
||||
const row = el("div", "mname");
|
||||
const name = input(m.default ? m.name : "", own);
|
||||
name.title = t("The name agents and magpie show for {id}; empty for its own", { id: m.id });
|
||||
const save = () => {
|
||||
const v = name.value.trim();
|
||||
if (v === (m.default ? m.name : "")) return;
|
||||
accountAction("provider/name", { id: p.id, model: m.id, modelName: v }, v ? t("{id} is called {name}", { id: m.id, name: v }) : t("{id} has its own name again", { id: m.id }));
|
||||
};
|
||||
name.onchange = save;
|
||||
name.onkeydown = (e) => { e.stopPropagation(); if (e.key === "Enter") name.blur(); else if (e.key === "Escape") { name.value = m.default ? m.name : ""; name.blur(); } };
|
||||
const who = el("div", "mwho");
|
||||
who.append(name, el("code", "", m.id));
|
||||
row.append(who);
|
||||
const levels = m.efforts || [];
|
||||
if (levels.length > 1) {
|
||||
const lv = el("div", "mlevels");
|
||||
lv.title = t("Reasoning levels agents are offered");
|
||||
const kept = m.kept?.length ? m.kept : levels;
|
||||
for (const l of levels) {
|
||||
const [tk, cb] = tick(t(l), kept.includes(l));
|
||||
cb.onchange = () => {
|
||||
const next = levels.filter((x) => x === l ? cb.checked : kept.includes(x));
|
||||
if (!next.length) { cb.checked = true; status(t("Keep at least one level"), "err"); return; }
|
||||
accountAction("provider/efforts", { id: p.id, model: m.id, efforts: next.length === levels.length ? [] : next }, t("{id}: {levels}", { id: m.id, levels: next.map((x) => t(x)).join(", ") }));
|
||||
};
|
||||
lv.append(tk);
|
||||
}
|
||||
row.append(lv);
|
||||
}
|
||||
if (m.default || m.kept?.length) {
|
||||
const reset = el("button", "text action", t("Restore default"));
|
||||
reset.title = t("Its own name and every reasoning level it has");
|
||||
reset.onclick = async () => {
|
||||
reset.classList.add("busy");
|
||||
try { if (m.default) await api("provider/name", { id: p.id, model: m.id, modelName: "" }); }
|
||||
catch (e) { status(e.message, "err"); reset.classList.remove("busy"); return; }
|
||||
accountAction("provider/efforts", { id: p.id, model: m.id, efforts: [] }, t("{id} is as its provider has it again", { id: m.id }));
|
||||
};
|
||||
row.append(reset);
|
||||
}
|
||||
names.append(row);
|
||||
}
|
||||
};
|
||||
if (q) { q.oninput = draw; box.append(q); }
|
||||
box.append(chips);
|
||||
box.append(chips, names);
|
||||
const foot = el("div", "mfoot");
|
||||
const add = input("", t("add a model id…"));
|
||||
add.onkeydown = (e) => {
|
||||
@@ -2818,7 +2876,10 @@ function renderModels(p) {
|
||||
renderProviders();
|
||||
} catch (e) { status(e.message, "err"); refresh.classList.remove("busy"); }
|
||||
};
|
||||
foot.append(add, refresh);
|
||||
const rename = el("button", "text action" + (naming === p.id ? " on" : ""), t("Names & levels"));
|
||||
rename.title = t("Rename the models agents see, or offer fewer of their reasoning levels");
|
||||
rename.onclick = () => { naming = naming === p.id ? null : p.id; rename.classList.toggle("on", naming === p.id); drawNames(); };
|
||||
foot.append(add, refresh, rename);
|
||||
if (p.fetched) foot.append(el("span", "hint", t("vendor list · {when}", { when: p.fetched })));
|
||||
// a signed-in account's list, until the vendor gives one, is magpie's own
|
||||
else if (p.models.length) foot.append(el("span", "hint", t(p.account ? "magpie's list · Refresh asks the vendor" : "from models.dev · Refresh asks the vendor")));
|
||||
|
||||
@@ -375,6 +375,18 @@ const I18N = {
|
||||
"vendor list · {when}": "供应商列表 · {when}",
|
||||
"from models.dev · Refresh asks the vendor": "来自 models.dev · 刷新可向供应商获取",
|
||||
"magpie's list · Refresh asks the vendor": "magpie 内置列表 · 刷新可向供应商获取",
|
||||
"Names & levels": "名称与推理档位",
|
||||
"Rename the models agents see, or offer fewer of their reasoning levels": "重命名 Agent 看到的模型,或只提供部分推理档位",
|
||||
"Pick a model first.": "请先选择模型。",
|
||||
"The name agents and magpie show for {id}; empty for its own": "Agent 和 magpie 为 {id} 显示的名称;留空则用默认名称",
|
||||
"{id} is called {name}": "{id} 已改名为 {name}",
|
||||
"{id} has its own name again": "{id} 已恢复默认名称",
|
||||
"Reasoning levels agents are offered": "提供给 Agent 的推理档位",
|
||||
"Keep at least one level": "至少保留一个档位",
|
||||
"{id}: {levels}": "{id}:{levels}",
|
||||
"Restore default": "恢复默认",
|
||||
"Its own name and every reasoning level it has": "恢复默认名称和全部推理档位",
|
||||
"{id} is as its provider has it again": "{id} 已恢复为供应商的默认设置",
|
||||
|
||||
// gateway
|
||||
"running": "运行中",
|
||||
|
||||
+45
-12
@@ -1,6 +1,7 @@
|
||||
package gui
|
||||
|
||||
import (
|
||||
"cmp"
|
||||
"context"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
@@ -22,7 +23,9 @@ import (
|
||||
|
||||
type modelJSON struct {
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Name string `json:"name"` // the user's name for it, if they gave one
|
||||
Default string `json:"default,omitempty"` // its own name, when the user gave it another
|
||||
Kept []string `json:"kept,omitempty"` // the reasoning levels the user keeps of Efforts, when not all
|
||||
Efforts []string `json:"efforts,omitempty"`
|
||||
On bool `json:"on"` // exposed to agents
|
||||
}
|
||||
@@ -56,16 +59,16 @@ type providerJSON struct {
|
||||
Optional bool `json:"optional"`
|
||||
} `json:"key"`
|
||||
Ready bool `json:"ready"`
|
||||
Chosen []string `json:"chosen"` // the user's explicit picks, if any
|
||||
Fallback []string `json:"fallback"` // where requests go when this one can't take them
|
||||
Routing string `json:"routing"` // how requests spread over its keys or accounts
|
||||
Affinity string `json:"affinity"` // how long a conversation stays with who answered it
|
||||
Models []modelJSON `json:"models"` // everything the vendor lists, exposed ones flagged
|
||||
Exposed int `json:"exposed"` // how many reach the agents
|
||||
Unlisted bool `json:"unlisted"` // its models serve only through routing groups
|
||||
Chosen []string `json:"chosen"` // the user's explicit picks, if any
|
||||
Fallback []string `json:"fallback"` // where requests go when this one can't take them
|
||||
Routing string `json:"routing"` // how requests spread over its keys or accounts
|
||||
Affinity string `json:"affinity"` // how long a conversation stays with who answered it
|
||||
Models []modelJSON `json:"models"` // everything the vendor lists, exposed ones flagged
|
||||
Exposed int `json:"exposed"` // how many reach the agents
|
||||
Unlisted bool `json:"unlisted"` // its models serve only through routing groups
|
||||
Contexts map[string]int `json:"contexts,omitempty"` // the windows the user set, "*" for all its models
|
||||
Fetched string `json:"fetched"` // "3h ago" when the list came from the vendor
|
||||
Agents []providerAgent `json:"agents"` // detected agents, current ones flagged
|
||||
Fetched string `json:"fetched"` // "3h ago" when the list came from the vendor
|
||||
Agents []providerAgent `json:"agents"` // detected agents, current ones flagged
|
||||
Sponsored bool `json:"sponsored"`
|
||||
KeyList []provider.KeyInfo `json:"keyList"` // its keys, in the order requests try them
|
||||
Account *accountJSON `json:"account,omitempty"` // a signed-in agent, see provider.Account
|
||||
@@ -205,14 +208,26 @@ func providerInfo(p provider.Provider, agents []agentUse) providerJSON {
|
||||
exposed[m.ID] = true
|
||||
}
|
||||
seen := map[string]bool{}
|
||||
names, kept := p.ModelNames(), p.ModelEfforts()
|
||||
named := func(m catalog.Model, on bool) modelJSON {
|
||||
j := modelJSON{ID: m.ID, Name: m.Name, Efforts: provider.EffortsOf(m), On: on}
|
||||
if n, ok := names[m.ID]; ok {
|
||||
j.Default = cmp.Or(m.Name, m.ID)
|
||||
j.Name = n
|
||||
}
|
||||
if k, ok := kept[m.ID]; ok {
|
||||
j.Kept = slices.DeleteFunc(slices.Clone(j.Efforts), func(e string) bool { return !slices.Contains(k, e) })
|
||||
}
|
||||
return j
|
||||
}
|
||||
for _, m := range p.Available() {
|
||||
seen[m.ID] = true
|
||||
out.Models = append(out.Models, modelJSON{ID: m.ID, Name: m.Name, Efforts: m.Efforts, On: exposed[m.ID]})
|
||||
out.Models = append(out.Models, named(m, exposed[m.ID]))
|
||||
}
|
||||
// picks the vendor list does not know go first, so they are visible
|
||||
for _, m := range p.Exposed() {
|
||||
if !seen[m.ID] {
|
||||
out.Models = append([]modelJSON{{ID: m.ID, Name: m.Name, Efforts: m.Efforts, On: true}}, out.Models...)
|
||||
out.Models = append([]modelJSON{named(m, true)}, out.Models...)
|
||||
}
|
||||
}
|
||||
out.Exposed = len(exposed)
|
||||
@@ -352,6 +367,13 @@ func providerRoutes(mux *http.ServeMux, w Windows) {
|
||||
ClearBalanceToken bool `json:"clearBalanceToken"`
|
||||
// From is the id the provider had: another is a rename
|
||||
From string `json:"from"`
|
||||
// Model and ModelName, for name: the name the user gives one
|
||||
// of its models, "" for its own again
|
||||
Model string `json:"model"`
|
||||
ModelName string `json:"modelName"`
|
||||
// Efforts, for efforts: the reasoning levels it offers, none
|
||||
// for all it has
|
||||
Efforts []string `json:"efforts"`
|
||||
}
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
fail(rw, err)
|
||||
@@ -449,6 +471,17 @@ func providerRoutes(mux *http.ServeMux, w Windows) {
|
||||
}
|
||||
writeJSON(rw, map[string]string{"key": p.Key})
|
||||
return
|
||||
case "name":
|
||||
// the agents' own model lists follow, through catalog.Changed
|
||||
if err := provider.SetModelName(in.ID+"/"+req.Model, req.ModelName); err != nil {
|
||||
fail(rw, err)
|
||||
return
|
||||
}
|
||||
case "efforts":
|
||||
if err := provider.SetModelEfforts(in.ID+"/"+req.Model, req.Efforts); err != nil {
|
||||
fail(rw, err)
|
||||
return
|
||||
}
|
||||
case "route":
|
||||
if err := provider.SetRouting(in.ID, in.Routing); err != nil {
|
||||
fail(rw, err)
|
||||
|
||||
@@ -196,11 +196,7 @@ func CodexListed() []catalog.Model {
|
||||
if e.Provider.Account != nil && e.Provider.Account.Agent == "codex" {
|
||||
continue
|
||||
}
|
||||
by := e.Provider.Name
|
||||
if e.Group != "" {
|
||||
by = "routing group"
|
||||
}
|
||||
ms = append(ms, catalog.Model{ID: e.ID, Name: e.Name + " · " + by, Efforts: e.Efforts, Images: e.Images, Context: e.Context})
|
||||
ms = append(ms, catalog.Model{ID: e.ID, Name: e.Label(), Efforts: e.Efforts, Images: e.Images, Context: e.Context})
|
||||
}
|
||||
return ms
|
||||
}
|
||||
|
||||
@@ -148,11 +148,18 @@ func autoGroups(entries []Entry) []Group {
|
||||
if len(es) < 2 {
|
||||
continue
|
||||
}
|
||||
g := Group{ID: "auto-" + Slug(k), Name: es[0].Name, Auto: true}
|
||||
// the model's own name: one a user gave it is that provider's alone
|
||||
own := func(e Entry) string {
|
||||
if e.Default != "" {
|
||||
return e.Default
|
||||
}
|
||||
return e.Name
|
||||
}
|
||||
g := Group{ID: "auto-" + Slug(k), Name: own(es[0]), Auto: true}
|
||||
for _, e := range es {
|
||||
g.Members = append(g.Members, e.ID)
|
||||
if g.Name == es[0].Model && e.Name != e.Model {
|
||||
g.Name = e.Name // a vendor that names it, over one that only lists its id
|
||||
if g.Name == es[0].Model && own(e) != e.Model {
|
||||
g.Name = own(e) // a vendor that names it, over one that only lists its id
|
||||
}
|
||||
}
|
||||
out = append(out, g)
|
||||
|
||||
@@ -0,0 +1,211 @@
|
||||
package provider
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"slices"
|
||||
"strings"
|
||||
|
||||
"github.com/yetone/magpie/internal/catalog"
|
||||
"github.com/yetone/magpie/internal/settings"
|
||||
)
|
||||
|
||||
// A model's name is the vendor's, or models.dev's, and agents are shown it
|
||||
// with the provider after it (Claude Opus 5.5 · Claude Code). The user can
|
||||
// give one of a provider's models a name of their own instead (Opus 5.5):
|
||||
// it is kept in settings' ModelNames by "<provider id>/<model id>", apart
|
||||
// from the vendor's list, so a Refresh, a restart or an upgrade leaves it,
|
||||
// and the same model another provider serves keeps its own. Only the name
|
||||
// changes: the model's id, and where requests for it go, stay as they were.
|
||||
|
||||
// The user can also keep only some of the reasoning levels a model has
|
||||
// (low, medium and high of its six): settings' ModelEfforts, kept the same
|
||||
// way. The lists magpie hands out — the gateway's, and those it writes into
|
||||
// the agents' files — offer only those, in the vendor's order; a request
|
||||
// for another still reaches the vendor as it did.
|
||||
|
||||
// ModelName is the name the user gave a provider's model, if any.
|
||||
func ModelName(pid, model string) (string, bool) {
|
||||
return modelNameIn(settings.Load().ModelNames, pid, model)
|
||||
}
|
||||
|
||||
func modelNameIn(names map[string]string, pid, model string) (string, bool) {
|
||||
n, ok := names[pid+"/"+model]
|
||||
return n, ok && n != ""
|
||||
}
|
||||
|
||||
// splitRef is "provider/model" as the provider it names, by its id now,
|
||||
// and the model.
|
||||
func splitRef(ref string) (*Provider, string, error) {
|
||||
r := strings.TrimPrefix(strings.TrimSpace(ref), "magpie/")
|
||||
if strings.HasPrefix(r, GroupPrefix) {
|
||||
return nil, "", errors.New("that is a routing group, not a provider's model")
|
||||
}
|
||||
pid, model, ok := strings.Cut(r, "/")
|
||||
if !ok || pid == "" || model == "" {
|
||||
return nil, "", fmt.Errorf("name a model as provider/model, not %q", ref)
|
||||
}
|
||||
p, err := Find(pid)
|
||||
if err != nil {
|
||||
return nil, "", err
|
||||
}
|
||||
return p, model, nil
|
||||
}
|
||||
|
||||
// SetModelName names a provider's model, spelled "provider/model"; an empty
|
||||
// name gives it back its own. The agents that keep the models in files of
|
||||
// their own are told (catalog.Touched), as for any other change of the
|
||||
// catalog.
|
||||
func SetModelName(ref, name string) error {
|
||||
p, model, err := splitRef(ref)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
name = strings.Join(strings.Fields(name), " ")
|
||||
if len([]rune(name)) > 80 {
|
||||
return errors.New("a model's name is at most 80 characters")
|
||||
}
|
||||
s := settings.Load()
|
||||
key := p.ID + "/" + model
|
||||
if s.ModelNames[key] == name {
|
||||
return nil
|
||||
}
|
||||
if name == "" {
|
||||
delete(s.ModelNames, key)
|
||||
} else {
|
||||
if s.ModelNames == nil {
|
||||
s.ModelNames = map[string]string{}
|
||||
}
|
||||
s.ModelNames[key] = name
|
||||
}
|
||||
if err := settings.Save(s); err != nil {
|
||||
return err
|
||||
}
|
||||
catalog.Touched()
|
||||
return nil
|
||||
}
|
||||
|
||||
// SetModelEfforts keeps only these of a provider's model's reasoning
|
||||
// levels in the lists magpie hands out; none, or all it has, offers them
|
||||
// all again. They must be levels the model has.
|
||||
func SetModelEfforts(ref string, efforts []string) error {
|
||||
p, model, err := splitRef(ref)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
all := p.Efforts(model)
|
||||
var keep []string
|
||||
for _, e := range efforts {
|
||||
e = strings.ToLower(strings.TrimSpace(e))
|
||||
if e == "" {
|
||||
continue
|
||||
}
|
||||
if !slices.Contains(all, e) {
|
||||
if len(all) == 0 {
|
||||
return fmt.Errorf("%s/%s has no reasoning levels to choose from", p.ID, model)
|
||||
}
|
||||
return fmt.Errorf("%s/%s has no reasoning level %q (it has %s)", p.ID, model, e, strings.Join(all, ", "))
|
||||
}
|
||||
if !slices.Contains(keep, e) {
|
||||
keep = append(keep, e)
|
||||
}
|
||||
}
|
||||
keep = effortsKept(all, keep) // in the vendor's order
|
||||
if len(keep) == len(all) {
|
||||
keep = nil
|
||||
}
|
||||
s := settings.Load()
|
||||
key := p.ID + "/" + model
|
||||
if slices.Equal(s.ModelEfforts[key], keep) {
|
||||
return nil
|
||||
}
|
||||
if len(keep) == 0 {
|
||||
delete(s.ModelEfforts, key)
|
||||
} else {
|
||||
if s.ModelEfforts == nil {
|
||||
s.ModelEfforts = map[string][]string{}
|
||||
}
|
||||
s.ModelEfforts[key] = keep
|
||||
}
|
||||
if err := settings.Save(s); err != nil {
|
||||
return err
|
||||
}
|
||||
catalog.Touched()
|
||||
return nil
|
||||
}
|
||||
|
||||
// ModelEfforts are the reasoning levels the user kept of the provider's
|
||||
// models, by model id.
|
||||
func (p Provider) ModelEfforts() map[string][]string {
|
||||
out := map[string][]string{}
|
||||
for k, es := range settings.Load().ModelEfforts {
|
||||
if m, ok := strings.CutPrefix(k, p.ID+"/"); ok && len(es) > 0 {
|
||||
out[m] = es
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// effortsKept is all without the levels the user didn't keep, in its own
|
||||
// order; all of them when none of those kept is among them any more (the
|
||||
// vendor's list changed).
|
||||
func effortsKept(all, kept []string) []string {
|
||||
if len(kept) == 0 {
|
||||
return all
|
||||
}
|
||||
out := slices.DeleteFunc(slices.Clone(all), func(e string) bool { return !slices.Contains(kept, e) })
|
||||
if len(out) == 0 {
|
||||
return all
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// renameModelPrefs moves the names and levels given to a provider's models
|
||||
// to the id it has now.
|
||||
func renameModelPrefs(s *settings.Settings, from, to string) bool {
|
||||
named := renameKeys(s.ModelNames, from, to)
|
||||
return renameKeys(s.ModelEfforts, from, to) || named
|
||||
}
|
||||
|
||||
func renameKeys[V any](m map[string]V, from, to string) bool {
|
||||
moved := map[string]V{}
|
||||
for k, v := range m {
|
||||
if rest, ok := strings.CutPrefix(k, from+"/"); ok {
|
||||
delete(m, k)
|
||||
moved[to+"/"+rest] = v
|
||||
}
|
||||
}
|
||||
for k, v := range moved {
|
||||
m[k] = v
|
||||
}
|
||||
return len(moved) > 0
|
||||
}
|
||||
|
||||
// Label is how an agent's list names the entry: the name the user gave it,
|
||||
// else its own with its provider's after it, or "routing group" for a
|
||||
// group's.
|
||||
func (e Entry) Label() string {
|
||||
if e.Default != "" {
|
||||
return e.Name
|
||||
}
|
||||
by := e.Provider.Name
|
||||
if e.Group != "" {
|
||||
by = "routing group"
|
||||
}
|
||||
return e.Name + " · " + by
|
||||
}
|
||||
|
||||
// ModelNames are the names the user gave the provider's models, by model id.
|
||||
func (p Provider) ModelNames() map[string]string {
|
||||
out := map[string]string{}
|
||||
for k, n := range settings.Load().ModelNames {
|
||||
if m, ok := strings.CutPrefix(k, p.ID+"/"); ok && n != "" {
|
||||
out[m] = n
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// EffortsOf is the reasoning levels one of a provider's listed models has,
|
||||
// before any the user left out.
|
||||
func EffortsOf(m catalog.Model) []string { return effortsOf(m) }
|
||||
@@ -0,0 +1,181 @@
|
||||
package provider
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"slices"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/yetone/magpie/internal/catalog"
|
||||
"github.com/yetone/magpie/internal/settings"
|
||||
)
|
||||
|
||||
// prefsHome is a sandbox home where two providers serve the same model,
|
||||
// which models.dev names and gives four reasoning levels.
|
||||
func prefsHome(t *testing.T) {
|
||||
t.Helper()
|
||||
home := t.TempDir()
|
||||
t.Setenv("HOME", home)
|
||||
t.Setenv("XDG_CONFIG_HOME", filepath.Join(home, ".config"))
|
||||
t.Setenv("XDG_CACHE_HOME", filepath.Join(home, ".cache"))
|
||||
t.Setenv("CLAUDE_CONFIG_DIR", filepath.Join(home, ".claude"))
|
||||
t.Setenv("CODEX_HOME", filepath.Join(home, ".codex"))
|
||||
// a `security` that finds nothing: no Keychain of the user's is read
|
||||
if runtime.GOOS != "windows" {
|
||||
bin := t.TempDir()
|
||||
os.WriteFile(filepath.Join(bin, "security"), []byte("#!/bin/sh\nexit 44\n"), 0o755)
|
||||
t.Setenv("PATH", bin+string(os.PathListSeparator)+os.Getenv("PATH"))
|
||||
}
|
||||
os.MkdirAll(filepath.Dir(catalog.CachePath()), 0o755)
|
||||
data := `{"a":{"models":{"sol":{"id":"sol","name":"Sol","reasoning_options":[{"type":"effort","values":["low","medium","high","max"]}]}}},
|
||||
"b":{"models":{"sol":{"id":"sol","name":"Sol","reasoning_options":[{"type":"effort","values":["low","medium","high","max"]}]}}}}`
|
||||
if err := os.WriteFile(catalog.CachePath(), []byte(data), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
catalog.Reset()
|
||||
t.Cleanup(catalog.Reset)
|
||||
for _, id := range []string{"a", "b"} {
|
||||
if err := Save(Provider{ID: id, Name: strings.ToUpper(id), Catalog: id, Key: "k", Chat: "http://127.0.0.1:1/v1", Models: []string{"sol"}}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func entry(t *testing.T, id string) Entry {
|
||||
t.Helper()
|
||||
for _, e := range Catalog() {
|
||||
if e.ID == id {
|
||||
return e
|
||||
}
|
||||
}
|
||||
t.Fatalf("no %s in the catalog", id)
|
||||
return Entry{}
|
||||
}
|
||||
|
||||
// A model's name of the user's is kept by provider/model in settings, is
|
||||
// what the catalog and the lists agents get call it, leaves the same model
|
||||
// from another provider (and the group they make) alone, and goes when
|
||||
// reset. The agents are told each time.
|
||||
func TestModelName(t *testing.T) {
|
||||
prefsHome(t)
|
||||
touched := 0
|
||||
catalog.Changed = func() { touched++ }
|
||||
t.Cleanup(func() { catalog.Changed = nil })
|
||||
|
||||
if err := SetModelName("a/sol", " My Sol "); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if touched != 1 {
|
||||
t.Fatalf("agents told %d times", touched)
|
||||
}
|
||||
if got := settings.Load().ModelNames["a/sol"]; got != "My Sol" {
|
||||
t.Fatalf("settings hold %q", got)
|
||||
}
|
||||
if n, ok := ModelName("a", "sol"); !ok || n != "My Sol" {
|
||||
t.Fatal(n, ok)
|
||||
}
|
||||
a, b := entry(t, "a/sol"), entry(t, "b/sol")
|
||||
if a.Name != "My Sol" || a.Default != "Sol" || a.Label() != "My Sol" || a.Model != "sol" {
|
||||
t.Fatalf("%+v %q", a, a.Label())
|
||||
}
|
||||
if b.Name != "Sol" || b.Default != "" || b.Label() != "Sol · B" {
|
||||
t.Fatalf("%+v %q", b, b.Label())
|
||||
}
|
||||
if g := entry(t, "group/auto-sol"); g.Name != "Sol" {
|
||||
t.Fatalf("the group took a provider's name: %+v", g)
|
||||
}
|
||||
if p, m, ok := Resolve("a/sol"); !ok || p.ID != "a" || m != "sol" {
|
||||
t.Fatal("a named model no longer resolves", p.ID, m, ok)
|
||||
}
|
||||
var listed []string
|
||||
for _, m := range CodexListed() {
|
||||
listed = append(listed, m.ID+"="+m.Name)
|
||||
}
|
||||
if !slices.Contains(listed, "a/sol=My Sol") || !slices.Contains(listed, "b/sol=Sol · B") {
|
||||
t.Fatal(listed)
|
||||
}
|
||||
if got := (Provider{ID: "a"}).ModelNames(); got["sol"] != "My Sol" || len(got) != 1 {
|
||||
t.Fatal(got)
|
||||
}
|
||||
|
||||
// a provider's name, or a Find by it, is the same provider
|
||||
if err := SetModelName("B/sol", "Sol from B"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if b := entry(t, "b/sol"); b.Name != "Sol from B" {
|
||||
t.Fatalf("%+v", b)
|
||||
}
|
||||
// a rename takes its names with it
|
||||
if err := Rename("b", "c"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if c := entry(t, "c/sol"); c.Name != "Sol from B" {
|
||||
t.Fatalf("%+v", c)
|
||||
}
|
||||
if _, ok := settings.Load().ModelNames["b/sol"]; ok {
|
||||
t.Fatal("the old id's name stayed")
|
||||
}
|
||||
|
||||
if err := SetModelName("a/sol", ""); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if a := entry(t, "a/sol"); a.Name != "Sol" || a.Default != "" || a.Label() != "Sol · A" {
|
||||
t.Fatalf("%+v", a)
|
||||
}
|
||||
if _, ok := settings.Load().ModelNames["a/sol"]; ok {
|
||||
t.Fatal("a reset name is still kept")
|
||||
}
|
||||
for _, bad := range []string{"sol", "nobody/sol", "group/auto-sol", "a/"} {
|
||||
if err := SetModelName(bad, "x"); err == nil {
|
||||
t.Errorf("%s was named", bad)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Keeping some of a model's reasoning levels offers only those, in the
|
||||
// vendor's order, where magpie lists it (and so in the groups it is in);
|
||||
// asking for one it hasn't fails, and all of them, or none, is a reset.
|
||||
func TestModelEfforts(t *testing.T) {
|
||||
prefsHome(t)
|
||||
if err := SetModelEfforts("a/sol", []string{"high", "low", "high"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got := settings.Load().ModelEfforts["a/sol"]; !slices.Equal(got, []string{"low", "high"}) {
|
||||
t.Fatal(got)
|
||||
}
|
||||
if got := entry(t, "a/sol").Efforts; !slices.Equal(got, []string{"low", "high"}) {
|
||||
t.Fatal(got)
|
||||
}
|
||||
if got := entry(t, "b/sol").Efforts; !slices.Equal(got, []string{"low", "medium", "high", "max"}) {
|
||||
t.Fatal(got)
|
||||
}
|
||||
if got := entry(t, "group/auto-sol").Efforts; !slices.Equal(got, []string{"low", "high"}) {
|
||||
t.Fatal(got)
|
||||
}
|
||||
// the vendor is still asked at any level it has
|
||||
if got := (Provider{ID: "a", Catalog: "a"}).Efforts("sol"); len(got) != 4 {
|
||||
t.Fatal(got)
|
||||
}
|
||||
if err := SetModelEfforts("a/sol", []string{"ultra"}); err == nil {
|
||||
t.Fatal("kept a level the model hasn't")
|
||||
}
|
||||
if err := SetModelEfforts("a/sol", []string{"low", "medium", "high", "max"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, ok := settings.Load().ModelEfforts["a/sol"]; ok {
|
||||
t.Fatal("every level kept is still a narrowing")
|
||||
}
|
||||
SetModelEfforts("a/sol", []string{"max"})
|
||||
if err := SetModelEfforts("a/sol", nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got := entry(t, "a/sol").Efforts; len(got) != 4 {
|
||||
t.Fatal(got)
|
||||
}
|
||||
// levels kept that the vendor no longer has offer them all
|
||||
if got := effortsKept([]string{"low", "high"}, []string{"max"}); !slices.Equal(got, []string{"low", "high"}) {
|
||||
t.Fatal(got)
|
||||
}
|
||||
}
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/yetone/magpie/internal/catalog"
|
||||
"github.com/yetone/magpie/internal/settings"
|
||||
)
|
||||
|
||||
func errorf(format string, a ...any) error { return fmt.Errorf(format, a...) }
|
||||
@@ -307,9 +308,10 @@ func (p Provider) Chosen(id string) bool {
|
||||
|
||||
// Entry is one model as the agents see it.
|
||||
type Entry struct {
|
||||
ID string `json:"id"` // what the agent sends magpie
|
||||
Model string `json:"model"` // what magpie sends the vendor
|
||||
Name string `json:"name"`
|
||||
ID string `json:"id"` // what the agent sends magpie
|
||||
Model string `json:"model"` // what magpie sends the vendor
|
||||
Name string `json:"name"` // the user's name for it, when they gave one (SetModelName)
|
||||
Default string `json:"default,omitempty"` // the model's own name, when the user gave it another
|
||||
Efforts []string `json:"efforts,omitempty"`
|
||||
Provider Provider `json:"-"` // a group's: its first member's
|
||||
Group string `json:"group,omitempty"` // set on a routing group (group.go)
|
||||
@@ -349,6 +351,7 @@ func Served() []Entry {
|
||||
// providerEntries is the catalog without its groups.
|
||||
func providerEntries() []Entry {
|
||||
var out []Entry
|
||||
s := settings.Load()
|
||||
for _, p := range All() {
|
||||
if !p.Ready() {
|
||||
continue
|
||||
@@ -371,8 +374,13 @@ func providerEntries() []Entry {
|
||||
if m.ImageInput != nil {
|
||||
images = *m.ImageInput
|
||||
}
|
||||
out = append(out, Entry{ID: p.ID + "/" + m.ID, Model: m.ID, Family: p.Family, Name: m.Name, Efforts: effortsOf(m), Provider: p,
|
||||
Images: images, ImageInput: m.ImageInput, Context: ctx, Output: output})
|
||||
e := Entry{ID: p.ID + "/" + m.ID, Model: m.ID, Family: p.Family, Name: m.Name, Efforts: effortsOf(m), Provider: p,
|
||||
Images: images, ImageInput: m.ImageInput, Context: ctx, Output: output}
|
||||
if n, ok := modelNameIn(s.ModelNames, p.ID, m.ID); ok {
|
||||
e.Name, e.Default = n, m.Name
|
||||
}
|
||||
e.Efforts = effortsKept(e.Efforts, s.ModelEfforts[e.ID])
|
||||
out = append(out, e)
|
||||
}
|
||||
}
|
||||
return out
|
||||
|
||||
@@ -77,7 +77,7 @@ func Rename(from, to string) error {
|
||||
return err
|
||||
}
|
||||
s := settings.Load()
|
||||
changed := false
|
||||
changed := renameModelPrefs(&s, from, to)
|
||||
for a, names := range s.Visible {
|
||||
for k, n := range names {
|
||||
if strings.EqualFold(n, from) {
|
||||
|
||||
@@ -49,6 +49,14 @@ type Settings struct {
|
||||
// families (the tag a provider or group is given), provider ids and
|
||||
// group ids its lists hold. An agent it doesn't name is shown them all.
|
||||
Visible map[string][]string `json:"visible,omitempty"`
|
||||
// ModelNames are the names the user gave models, by "<provider
|
||||
// id>/<model id>": agents, the gateway's model list and magpie itself
|
||||
// show them for the vendor's (see provider.SetModelName).
|
||||
ModelNames map[string]string `json:"modelNames,omitempty"`
|
||||
// ModelEfforts are the reasoning levels the user keeps of a model's,
|
||||
// by "<provider id>/<model id>": the lists magpie hands out offer only
|
||||
// those (see provider.SetModelEfforts).
|
||||
ModelEfforts map[string][]string `json:"modelEfforts,omitempty"`
|
||||
// The main window's size when it was last resized, width and height,
|
||||
// so it opens at it again after a restart.
|
||||
Window []int `json:"window,omitempty"`
|
||||
|
||||
@@ -223,6 +223,7 @@ func modelOptions(id string) []agent.Option {
|
||||
}
|
||||
var out []agent.Option
|
||||
seen := map[string]bool{}
|
||||
names := p.ModelNames()
|
||||
add := func(id, name string) {
|
||||
if seen[id] {
|
||||
return
|
||||
@@ -232,6 +233,9 @@ func modelOptions(id string) []agent.Option {
|
||||
if on[id] {
|
||||
note = "● on"
|
||||
}
|
||||
if n, ok := names[id]; ok {
|
||||
name = n // the user's (magpie model name)
|
||||
}
|
||||
if name != "" && name != id {
|
||||
note += " · " + name
|
||||
}
|
||||
|
||||
@@ -12,8 +12,8 @@ import (
|
||||
|
||||
"github.com/yetone/magpie/internal/agent"
|
||||
"github.com/yetone/magpie/internal/catalog"
|
||||
"github.com/yetone/magpie/internal/davsync"
|
||||
"github.com/yetone/magpie/internal/claudebridge"
|
||||
"github.com/yetone/magpie/internal/davsync"
|
||||
"github.com/yetone/magpie/internal/gateway"
|
||||
"github.com/yetone/magpie/internal/netproxy"
|
||||
"github.com/yetone/magpie/internal/profile"
|
||||
@@ -54,6 +54,8 @@ const usage = `magpie — one place to pick every agent's model
|
||||
magpie provider fallback <id> <provider/model>… use these when it's out of quota or down
|
||||
magpie import [-y] <link> add the provider a magpie://import?… link describes
|
||||
magpie models [<agent>] every model agents can pick, as provider/model; an agent's, and why others aren't
|
||||
magpie model name <provider/model> <name>|--reset the name a model goes by, everywhere
|
||||
magpie model efforts <provider/model> <l>,<l>|--reset the reasoning levels a model offers (magpie model help)
|
||||
magpie visible [<agent> <family|provider|group>,… | all]
|
||||
which models an agent is shown: families (magpie provider/group set <id> family=…)
|
||||
magpie groups routing groups: several models agents pick as one, group/<id>
|
||||
@@ -159,6 +161,8 @@ func run(args []string) error {
|
||||
return providerCmd(args)
|
||||
case "models":
|
||||
return models(args[1:])
|
||||
case "model":
|
||||
return modelCmd(args[1:])
|
||||
case "visible":
|
||||
return visibleCmd(args[1:])
|
||||
case "groups":
|
||||
|
||||
+185
@@ -0,0 +1,185 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"maps"
|
||||
"slices"
|
||||
"strings"
|
||||
|
||||
"github.com/yetone/magpie/internal/provider"
|
||||
"github.com/yetone/magpie/internal/settings"
|
||||
)
|
||||
|
||||
// A model's name and reasoning levels, from the terminal: magpie model.
|
||||
|
||||
const modelUsage = `usage:
|
||||
magpie model name <provider/model> the name the model goes by
|
||||
magpie model name <provider/model> <name> name it so everywhere: in magpie, in the gateway's
|
||||
model list, and in the lists magpie writes into the agents
|
||||
magpie model name <provider/model> --reset give it back its own name
|
||||
magpie model efforts <provider/model> the reasoning levels it offers, and those it has
|
||||
magpie model efforts <provider/model> <l>,<l> offer only these of them, e.g. low,medium,high
|
||||
magpie model efforts <provider/model> --reset offer every level it has again
|
||||
magpie model names the models you named or narrowed
|
||||
|
||||
Only what agents are shown changes: they still pick the model, and requests still reach it,
|
||||
as <provider/model>. The same model from another provider keeps its own name and levels.
|
||||
|
||||
e.g. magpie model name claude/claude-opus-5-5 "Opus 5.5"
|
||||
magpie model efforts openai/gpt-6 low,medium,high`
|
||||
|
||||
func modelCmd(args []string) error {
|
||||
if len(args) == 0 {
|
||||
return fmt.Errorf("%s", modelUsage)
|
||||
}
|
||||
switch args[0] {
|
||||
case "names", "ls", "list":
|
||||
return modelNames()
|
||||
case "name", "rename":
|
||||
return modelName(args[1:])
|
||||
case "efforts", "effort", "levels":
|
||||
return modelEfforts(args[1:])
|
||||
case "help", "-h", "--help":
|
||||
fmt.Println(modelUsage)
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("unknown command %q\n\n%s", args[0], modelUsage)
|
||||
}
|
||||
|
||||
// modelRef is the provider and model a typed provider/model names.
|
||||
func modelRef(s string) (*provider.Provider, string, error) {
|
||||
ref := strings.TrimPrefix(strings.TrimSpace(s), "magpie/")
|
||||
pid, model, ok := strings.Cut(ref, "/")
|
||||
if !ok || model == "" {
|
||||
return nil, "", fmt.Errorf("name a model as provider/model, not %q (magpie models lists them)", s)
|
||||
}
|
||||
p, err := provider.Find(pid)
|
||||
if err != nil {
|
||||
return nil, "", err
|
||||
}
|
||||
return p, model, nil
|
||||
}
|
||||
|
||||
func isReset(args []string) bool {
|
||||
return len(args) == 1 && slices.Contains([]string{"--reset", "-r", "--default", "default", "reset"}, args[0])
|
||||
}
|
||||
|
||||
func modelName(args []string) error {
|
||||
if len(args) == 0 {
|
||||
return fmt.Errorf("%s", modelUsage)
|
||||
}
|
||||
p, model, err := modelRef(args[0])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
own := model
|
||||
for _, m := range p.Available() {
|
||||
if m.ID == model && m.Name != "" {
|
||||
own = m.Name
|
||||
break
|
||||
}
|
||||
}
|
||||
id := p.ID + "/" + model
|
||||
rest := args[1:]
|
||||
if len(rest) == 0 {
|
||||
if n, ok := provider.ModelName(p.ID, model); ok {
|
||||
fmt.Println(bold.Render(n), muted.Render("· "+id+" · its own name is "+own))
|
||||
} else {
|
||||
fmt.Println(bold.Render(own), muted.Render("· "+id+" · its own name"))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
name := strings.Join(rest, " ")
|
||||
if isReset(rest) {
|
||||
name = ""
|
||||
}
|
||||
if err := provider.SetModelName(id, name); err != nil {
|
||||
return err
|
||||
}
|
||||
if name == "" {
|
||||
fmt.Println(green.Render("✓"), id, muted.Render("is called"), bold.Render(own), muted.Render("again"))
|
||||
} else {
|
||||
fmt.Println(green.Render("✓"), id, muted.Render("is called"), bold.Render(strings.Join(strings.Fields(name), " ")))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func modelEfforts(args []string) error {
|
||||
if len(args) == 0 {
|
||||
return fmt.Errorf("%s", modelUsage)
|
||||
}
|
||||
p, model, err := modelRef(args[0])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
id := p.ID + "/" + model
|
||||
all := p.Efforts(model)
|
||||
rest := args[1:]
|
||||
if len(rest) == 0 {
|
||||
if len(all) == 0 {
|
||||
fmt.Println(muted.Render(id + " has no reasoning levels"))
|
||||
return nil
|
||||
}
|
||||
kept, narrowed := p.ModelEfforts()[model]
|
||||
for _, e := range all {
|
||||
if !narrowed || slices.Contains(kept, e) {
|
||||
fmt.Print(bold.Render(e), " ")
|
||||
} else {
|
||||
fmt.Print(faint.Render(e), " ")
|
||||
}
|
||||
}
|
||||
if narrowed {
|
||||
fmt.Println(muted.Render("· only the bold ones are offered · --reset offers them all"))
|
||||
} else {
|
||||
fmt.Println(muted.Render("· all offered"))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
var levels []string
|
||||
if !isReset(rest) {
|
||||
for _, a := range rest {
|
||||
for _, l := range strings.FieldsFunc(a, func(r rune) bool { return r == ',' || r == ' ' || r == '/' }) {
|
||||
levels = append(levels, l)
|
||||
}
|
||||
}
|
||||
}
|
||||
if err := provider.SetModelEfforts(id, levels); err != nil {
|
||||
return err
|
||||
}
|
||||
if kept, ok := p.ModelEfforts()[model]; ok {
|
||||
fmt.Println(green.Render("✓"), id, muted.Render("offers"), bold.Render(strings.Join(kept, ", ")))
|
||||
} else {
|
||||
fmt.Println(green.Render("✓"), id, muted.Render("offers every level it has"), faint.Render(strings.Join(all, ", ")))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func modelNames() error {
|
||||
s := settings.Load()
|
||||
keys := slices.Sorted(maps.Keys(s.ModelNames))
|
||||
for k := range s.ModelEfforts {
|
||||
if !slices.Contains(keys, k) {
|
||||
keys = append(keys, k)
|
||||
}
|
||||
}
|
||||
slices.Sort(keys)
|
||||
if len(keys) == 0 {
|
||||
fmt.Println(muted.Render("no model is named or narrowed yet · magpie model name <provider/model> <name>"))
|
||||
return nil
|
||||
}
|
||||
w := 0
|
||||
for _, k := range keys {
|
||||
w = max(w, len(k))
|
||||
}
|
||||
for _, k := range keys {
|
||||
line := " " + pad(k, w)
|
||||
if n := s.ModelNames[k]; n != "" {
|
||||
line += " " + n
|
||||
}
|
||||
if es := s.ModelEfforts[k]; len(es) > 0 {
|
||||
line += " " + muted.Render(strings.Join(es, "/"))
|
||||
}
|
||||
fmt.Println(line)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/yetone/magpie/internal/catalog"
|
||||
"github.com/yetone/magpie/internal/provider"
|
||||
"github.com/yetone/magpie/internal/settings"
|
||||
)
|
||||
|
||||
// magpie model name and --reset keep and drop the user's name for a
|
||||
// provider's model, a name of several words included.
|
||||
func TestModelNameCmd(t *testing.T) {
|
||||
groupsHome(t)
|
||||
catalog.Changed = nil // no agent's files are rewritten here
|
||||
if err := modelCmd([]string{"name", "a/m", "My", "Model"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if n, ok := provider.ModelName("a", "m"); !ok || n != "My Model" {
|
||||
t.Fatal(n, ok)
|
||||
}
|
||||
if err := modelCmd([]string{"name", "a/m"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := modelCmd([]string{"names"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := modelCmd([]string{"name", "a/m", "--reset"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, ok := provider.ModelName("a", "m"); ok {
|
||||
t.Fatal("still named")
|
||||
}
|
||||
if err := modelCmd([]string{"name", "m", "x"}); err == nil {
|
||||
t.Fatal("named a model without its provider")
|
||||
}
|
||||
// a model without reasoning levels has none to keep
|
||||
if err := modelCmd([]string{"efforts", "a/m", "low"}); err == nil {
|
||||
t.Fatal("kept a level of a model without any")
|
||||
}
|
||||
if err := modelCmd([]string{"efforts", "a/m", "--reset"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if es := settings.Load().ModelEfforts; len(es) != 0 {
|
||||
t.Fatal(es)
|
||||
}
|
||||
}
|
||||
@@ -548,12 +548,16 @@ func showProvider(p provider.Provider) error {
|
||||
src = p.Host() + " · fetched " + ago(t)
|
||||
}
|
||||
kv("models", fmt.Sprintf("%d exposed of %d %s", len(ms), len(p.Available()), muted.Render("from "+src)))
|
||||
names := p.ModelNames()
|
||||
for i, m := range ms {
|
||||
if i == 12 {
|
||||
fmt.Println(faint.Render(fmt.Sprintf(" … %d more", len(ms)-12)))
|
||||
break
|
||||
}
|
||||
line := " " + p.ID + "/" + m.ID
|
||||
if n, ok := names[m.ID]; ok {
|
||||
m.Name = n
|
||||
}
|
||||
if m.Name != "" && m.Name != m.ID {
|
||||
line += muted.Render(" " + m.Name)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user