agents: omp's model picker lists the models of the providers in its own models.yml (#326)

* agents: omp's model pickers list the models of the providers in its own models.yml

Every role's picker (model, subagents, smol and slow, all made by role())
only looked up the current value's provider on models.dev, so a provider
the user added to omp's models.yml (a company gateway, a relay) showed none
of its models. Its models now come first, grouped by provider, before
models.dev's for the current provider, each value once; magpie's own entry
is left out, and a provider with only discovery, whose models omp learns at
run time, offers none.

* agents: omp's models.yml options are spelled as ownOptions spells them, and a model offered twice keeps its name

Review of the models.yml options: they put the model's name in the label where ownOptions puts it in the note, and dedup kept whichever came first, so a models.yml entry with no name hid models.dev's name for the same model (zai/glm-5 lost "GLM-5"). The name now goes in the note, with the label left empty, and an option offered twice takes the note, icon and group icon from whichever copy has them.

* agents: a provider both in omp's models.yml and on models.dev stays one group in the picker

From #326's review: with the current value's provider in models.yml (say
anthropic, with a model of the user's) and another provider after it, the
provider's models.dev models came after that one, so its group was drawn
twice ("Anthropic → codemaker → zzrelay → Anthropic"; the picker draws a
heading at each change of group). The options are now sorted, stably, by
where each group first comes. TestOmpOwnModels fails without it (Z.AI's
models split in two), and checks the subagents' picker offers models.yml's
models as the model's does.
This commit is contained in:
cyxer000
2026-10-01 11:10:49 +08:00
committed by GitHub
parent e8315b18ed
commit 7a3df78ff7
2 changed files with 163 additions and 1 deletions
+72 -1
View File
@@ -14,11 +14,14 @@ import (
"path/filepath"
"regexp"
"slices"
"sort"
"strings"
"github.com/yetone/magpie/internal/catalog"
"github.com/yetone/magpie/internal/edit"
"github.com/yetone/magpie/internal/gateway"
"github.com/yetone/magpie/internal/provider"
"gopkg.in/yaml.v3"
)
// ompEfforts are the thinking levels omp knows.
@@ -236,7 +239,7 @@ func omp(home string) *Agent {
return dropMagpie()
},
Options: func(cur map[string]string) []Option {
opts := append(ownOptions("", cur[key]), viaMagpie("omp", magpieID+"/")...)
opts := append(ompOwnOptions(pick("models"), cur[key]), viaMagpie("omp", magpieID+"/")...)
// a catalog model with a thinking level is offered as it
// reads, beside the model: that keeps the level, the model
// alone clears it
@@ -430,3 +433,71 @@ func ompProvider() ompProviderEntry {
return ompProviderEntry{BaseURL: gatewayV1(), API: "openai-completions", Auth: "none",
Headers: map[string]string{"User-Agent": "omp"}, Models: ms}
}
// ompOwnOptions lists the models of the providers the user added to omp's
// models.yml, then the ones models.dev knows for the current value's
// provider, spelled as ownOptions spells them (the model's name as the
// note). A value offered twice is offered once, with the name and icon
// either one had. A provider in both stays one group, where it first comes:
// the picker draws a heading at each change of group. A provider with only
// discovery has its models listed by omp asking it at run time, out of
// magpie's sight, so it offers none here.
func ompOwnOptions(modelsFile, cur string) []Option {
var f struct {
Providers map[string]struct {
Models []struct {
ID string `yaml:"id"`
Name string `yaml:"name"`
} `yaml:"models"`
} `yaml:"providers"`
}
if b, err := os.ReadFile(modelsFile); err == nil {
yaml.Unmarshal(b, &f)
}
providers := make([]string, 0, len(f.Providers))
for p := range f.Providers {
if p != magpieID {
providers = append(providers, p)
}
}
sort.Strings(providers)
var opts []Option
for _, p := range providers {
name := catalog.ProviderName(p)
if name == "" {
name = p
}
for _, m := range f.Providers[p].Models {
if m.ID != "" {
opts = append(opts, Option{Value: p + "/" + m.ID, Note: m.Name, Icon: modelIcon(p, m.ID), Group: name, GroupIcon: providerIcon(p)})
}
}
}
at := map[string]int{}
var out []Option
for _, o := range append(opts, ownOptions("", cur)...) {
i, dup := at[o.Value]
if !dup {
at[o.Value] = len(out)
out = append(out, o)
continue
}
if out[i].Note == "" {
out[i].Note = o.Note
}
if out[i].Icon == "" {
out[i].Icon = o.Icon
}
if out[i].GroupIcon == "" {
out[i].GroupIcon = o.GroupIcon
}
}
first := map[string]int{}
for i, o := range out {
if _, ok := first[o.Group]; !ok {
first[o.Group] = i
}
}
sort.SliceStable(out, func(i, j int) bool { return first[out[i].Group] < first[out[j].Group] })
return out
}
+91
View File
@@ -7,6 +7,7 @@ import (
"strings"
"testing"
"github.com/yetone/magpie/internal/catalog"
"github.com/yetone/magpie/internal/edit"
"github.com/yetone/magpie/internal/provider"
"github.com/yetone/magpie/internal/usage"
@@ -604,3 +605,93 @@ func TestOmpListRoleRenamed(t *testing.T) {
t.Fatalf("reset:\n%s", b)
}
}
// The model picker offers the models of the providers the user added to
// models.yml, beside models.dev's for the current provider, spelled as
// ownOptions spells them (the name as the note): not magpie's own entry, nor
// a provider whose models omp only discovers at run time. A model named in
// both keeps what models.dev knows of it, and a provider in both stays one
// group. Every role offers them, the subagents' as the model's.
func TestOmpOwnModels(t *testing.T) {
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"))
for _, k := range []string{"PI_CODING_AGENT_DIR", "PI_CONFIG_DIR", "OMP_PROFILE", "PI_PROFILE"} {
t.Setenv(k, "")
}
os.MkdirAll(filepath.Dir(catalog.CachePath()), 0o755)
os.WriteFile(catalog.CachePath(), []byte(`{"zai":{"name":"Z.AI","models":{"glm-5":{"id":"glm-5","name":"GLM-5"},"glm-6":{"id":"glm-6","name":"GLM-6"}}}}`), 0o644)
catalog.Reset()
t.Cleanup(catalog.Reset)
dir := filepath.Join(home, ".omp", "agent")
os.MkdirAll(dir, 0o755)
os.WriteFile(filepath.Join(dir, "config.yml"), []byte("modelRoles:\n default: zai/glm-5\n"), 0o644)
os.WriteFile(filepath.Join(dir, "models.yml"), []byte(`providers:
codemaker:
baseUrl: https://codemaker.example/v1
api: openai-completions
models:
- id: claude-opus-5-5
name: Claude Opus 5.5
- id: glm-5
zai:
models:
- id: glm-5
lan:
baseUrl: http://127.0.0.1:8000/v1
api: openai-completions
discovery:
type: openai-models-list
zzrelay:
models:
- id: m1
magpie:
baseUrl: http://127.0.0.1:1/v1
auth: none
models:
- id: deepseek/pro
`), 0o644)
opts := omp(home).Field("model").Options(map[string]string{"model": "zai/glm-5"})
byValue := map[string]Option{}
for _, o := range opts {
if _, dup := byValue[o.Value]; dup {
t.Fatalf("%s offered twice: %+v", o.Value, opts)
}
byValue[o.Value] = o
}
if o := byValue["codemaker/claude-opus-5-5"]; o.Note != "Claude Opus 5.5" || o.Label != "" || o.Group != "codemaker" {
t.Fatalf("codemaker model: %+v in %+v", o, opts)
}
if o, ok := byValue["codemaker/glm-5"]; !ok || o.Note != "" {
t.Fatalf("model without a name: %+v in %+v", o, opts)
}
// models.yml's zai glm-5 has no name; models.dev's has one
if o := byValue["zai/glm-5"]; o.Note != "GLM-5" || o.Icon == "" {
t.Fatalf("zai/glm-5 lost models.dev's name or icon: %+v in %+v", o, opts)
}
// magpie has no provider here, so its entry's model comes from nowhere else
for v := range byValue {
if strings.HasPrefix(v, "lan/") || v == "magpie/deepseek/pro" {
t.Fatalf("%s offered: %+v", v, opts)
}
}
// zai is in models.yml and on models.dev, zzrelay after it: one group each
seen := map[string]bool{}
for i, o := range opts {
if i > 0 && o.Group != opts[i-1].Group {
if seen[o.Group] {
t.Fatalf("%s's models split in two: %+v", o.Group, opts)
}
}
seen[o.Group] = true
}
if _, ok := byValue["zai/glm-6"]; !ok {
t.Fatalf("models.dev's zai/glm-6 not offered: %+v", opts)
}
sub := omp(home).Field("subagent").Options(map[string]string{"model": "zai/glm-5"})
if !slices.ContainsFunc(sub, func(o Option) bool { return o.Value == "codemaker/claude-opus-5-5" }) {
t.Fatalf("the subagents' picker lacks models.yml's models: %+v", sub)
}
}