provider: a custom provider can name several catalogs (catalog=openai, deepseek) — a gateway reselling many vendors gets names and reasoning levels for all of them, prefixed ids like deepseek/deepseek-chat included (#17)
This commit is contained in:
@@ -49,12 +49,19 @@ func isMagpie(v string) bool {
|
||||
return ok && strings.Contains(v, "/")
|
||||
}
|
||||
|
||||
func firstOf(xs []string) string {
|
||||
if len(xs) > 0 {
|
||||
return xs[0]
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// magpieModels is the catalog as catalog.Models, for agents that keep their
|
||||
// own model files.
|
||||
func magpieModels() []catalog.Model {
|
||||
var out []catalog.Model
|
||||
for _, e := range provider.Catalog() {
|
||||
out = append(out, catalog.Model{ID: e.ID, Name: e.Name + " · " + e.Provider.Name, Provider: e.Provider.Catalog, Efforts: e.Efforts})
|
||||
out = append(out, catalog.Model{ID: e.ID, Name: e.Name + " · " + e.Provider.Name, Provider: firstOf(e.Provider.Catalogs()), Efforts: e.Efforts})
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
@@ -183,7 +183,11 @@ func Decorate(live []Model, known []Model) []Model {
|
||||
}
|
||||
out := make([]Model, 0, len(live))
|
||||
for _, m := range live {
|
||||
if k, ok := byID[m.ID]; ok {
|
||||
k, ok := byID[m.ID]
|
||||
if i := strings.LastIndexByte(m.ID, '/'); !ok && i >= 0 {
|
||||
k, ok = byID[m.ID[i+1:]] // a gateway's "deepseek/deepseek-chat"
|
||||
}
|
||||
if ok {
|
||||
if m.Name == "" || m.Name == m.ID {
|
||||
m.Name = k.Name
|
||||
}
|
||||
|
||||
@@ -1517,9 +1517,9 @@ function renderEditor(p, presetID) {
|
||||
if (draft.api === "anthropic") add("OpenAI URL", "chat", "https://…/v1", "if the vendor also serves chat completions");
|
||||
else add("Anthropic URL", "anthropic", "https://…", "if the vendor also serves Anthropic messages");
|
||||
add("Responses URL", "responses", "https://…/v1", "if the vendor serves the OpenAI Responses API (Codex uses it natively)");
|
||||
const cat = input(draft.catalog, t("models.dev id, e.g. openai"));
|
||||
const cat = input(draft.catalog, t("models.dev ids, e.g. openai, deepseek"));
|
||||
cat.oninput = () => { draft.catalog = cat.value; };
|
||||
inner.append(...field(t("Catalog"), cat, t("Display names and reasoning levels for the models")));
|
||||
inner.append(...field(t("Catalog"), cat, t("Display names and reasoning levels for the models; for a gateway that serves several vendors, list them all, first match wins")));
|
||||
more.append(inner);
|
||||
ed.append(more);
|
||||
}
|
||||
|
||||
@@ -167,9 +167,9 @@ const I18N = {
|
||||
"if the vendor also serves Anthropic messages": "如果供应商也提供 Anthropic messages",
|
||||
"Responses URL": "Responses 地址",
|
||||
"if the vendor serves the OpenAI Responses API (Codex uses it natively)": "如果供应商提供 OpenAI Responses API(Codex 原生使用)",
|
||||
"models.dev id, e.g. openai": "models.dev 上的 ID,例如 openai",
|
||||
"models.dev ids, e.g. openai, deepseek": "models.dev 上的 ID,例如 openai, deepseek",
|
||||
"Catalog": "目录",
|
||||
"Display names and reasoning levels for the models": "模型的显示名称与推理等级",
|
||||
"Display names and reasoning levels for the models; for a gateway that serves several vendors, list them all, first match wins": "模型的显示名称与推理等级;网关同时提供多家厂商的模型时,把它们都列上,按顺序先匹配到的为准",
|
||||
"Headers": "请求头",
|
||||
"Extra HTTP headers sent to the vendor, applied after auth. For gateways that need a private scheme.": "发送给供应商的额外 HTTP 请求头,在鉴权之后应用。适用于需要私有鉴权方案的网关。",
|
||||
"Header-Name": "请求头名称",
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
package provider
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/yetone/magpie/internal/catalog"
|
||||
)
|
||||
|
||||
// A gateway that resells several vendors names each catalog; its models
|
||||
// are looked up in them in order (#17).
|
||||
func TestSeveralCatalogs(t *testing.T) {
|
||||
isolate(t)
|
||||
t.Setenv("HOME", t.TempDir())
|
||||
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
|
||||
cache := t.TempDir()
|
||||
t.Setenv("XDG_CACHE_HOME", cache)
|
||||
os.MkdirAll(filepath.Join(cache, "magpie"), 0o755)
|
||||
if err := os.WriteFile(catalog.CachePath(), []byte(`{
|
||||
"openai": {"models": {"gpt-5.5": {"id":"gpt-5.5","name":"GPT-5.5","reasoning_options":[{"type":"effort","values":["low","high"]}]}}},
|
||||
"deepseek": {"models": {"deepseek-chat": {"id":"deepseek-chat","name":"DeepSeek V4","reasoning_options":[{"type":"effort","values":["high","max"]}]}}}
|
||||
}`), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
catalog.Reset()
|
||||
t.Cleanup(catalog.Reset)
|
||||
|
||||
if err := Save(Provider{ID: "gw", Name: "Gateway", Key: "k", Chat: "https://gw.example/v1", Catalog: "OpenAI,, deepseek openai"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
p, err := Find("gw")
|
||||
if err != nil || p.Catalog != "openai, deepseek" {
|
||||
t.Fatalf("catalog %q %v", p.Catalog, err)
|
||||
}
|
||||
names := map[string]string{}
|
||||
for _, m := range p.Available() {
|
||||
names[m.ID] = m.Name
|
||||
}
|
||||
if names["gpt-5.5"] != "GPT-5.5" || names["deepseek-chat"] != "DeepSeek V4" {
|
||||
t.Fatalf("available %v", names)
|
||||
}
|
||||
// the vendor's own list, ids prefixed the gateway's way
|
||||
live := catalog.Decorate([]catalog.Model{{ID: "deepseek/deepseek-chat"}, {ID: "internal-x"}}, p.Available())
|
||||
if live[0].Name != "DeepSeek V4" || len(live[0].Efforts) != 2 || live[1].Efforts != nil {
|
||||
t.Fatalf("decorated %+v", live)
|
||||
}
|
||||
}
|
||||
@@ -24,7 +24,15 @@ func (p Provider) Available() []catalog.Model {
|
||||
if signedIn {
|
||||
known = p.Account.models()
|
||||
} else {
|
||||
known = catalog.Provider(p.Catalog)
|
||||
seen := map[string]bool{}
|
||||
for _, id := range p.Catalogs() {
|
||||
for _, m := range catalog.Provider(id) {
|
||||
if !seen[m.ID] {
|
||||
seen[m.ID] = true
|
||||
known = append(known, m)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if live, _, ok := catalog.Live(p.ID); ok {
|
||||
return catalog.Decorate(live, known)
|
||||
@@ -41,6 +49,13 @@ func (p Provider) Available() []catalog.Model {
|
||||
return out
|
||||
}
|
||||
|
||||
func (p Provider) firstCatalog() string {
|
||||
if cs := p.Catalogs(); len(cs) > 0 {
|
||||
return cs[0]
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// Fetched reports when the vendor's own list was last fetched.
|
||||
func (p Provider) Fetched() (time.Time, bool) {
|
||||
_, t, ok := catalog.Live(p.ID)
|
||||
@@ -80,7 +95,7 @@ func (p Provider) Exposed() []catalog.Model {
|
||||
if m, ok := byID[id]; ok {
|
||||
out = append(out, m)
|
||||
} else {
|
||||
out = append(out, catalog.Model{ID: id, Name: id, Provider: p.Catalog})
|
||||
out = append(out, catalog.Model{ID: id, Name: id, Provider: p.firstCatalog()})
|
||||
}
|
||||
}
|
||||
return out
|
||||
|
||||
@@ -267,6 +267,7 @@ func normalize(p Provider) Provider {
|
||||
}
|
||||
p.Models = cleanList(p.Models)
|
||||
p.Fallback = cleanList(p.Fallback)
|
||||
p.Catalog = strings.Join(p.Catalogs(), ", ")
|
||||
p.Headers = cleanHeaders(p.Headers)
|
||||
if p.Preset != "" {
|
||||
// Presets own their request shape; custom headers are supported only
|
||||
@@ -290,6 +291,13 @@ func normalize(p Provider) Provider {
|
||||
return p
|
||||
}
|
||||
|
||||
// Catalogs are the models.dev ids the provider's models are looked up in,
|
||||
// first match wins: a gateway that resells several vendors names them all
|
||||
// ("openai, deepseek").
|
||||
func (p Provider) Catalogs() []string {
|
||||
return cleanList(strings.FieldsFunc(strings.ToLower(p.Catalog), func(r rune) bool { return r == ',' || r == ' ' }))
|
||||
}
|
||||
|
||||
func cleanList(xs []string) []string {
|
||||
var out []string
|
||||
for _, x := range xs {
|
||||
|
||||
@@ -257,9 +257,12 @@ func summarize(p Period, now time.Time, recs []Record) Summary {
|
||||
}
|
||||
var pr *catalog.Price
|
||||
for _, p := range provider.All() {
|
||||
if p.ID == r.Provider && p.Catalog != "" {
|
||||
if v, ok := catalog.PriceOf(p.Catalog, r.Model); ok {
|
||||
pr = &v
|
||||
if p.ID == r.Provider {
|
||||
for _, c := range p.Catalogs() {
|
||||
if v, ok := catalog.PriceOf(c, r.Model); ok {
|
||||
pr = &v
|
||||
break
|
||||
}
|
||||
}
|
||||
break
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user