diff --git a/internal/agent/omp.go b/internal/agent/omp.go index f95f0208d..e78a6407e 100644 --- a/internal/agent/omp.go +++ b/internal/agent/omp.go @@ -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 +} diff --git a/internal/agent/omp_test.go b/internal/agent/omp_test.go index 30adc17c5..0efe91d79 100644 --- a/internal/agent/omp_test.go +++ b/internal/agent/omp_test.go @@ -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) + } +}