Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.
usage: a probe at another host of a provider's site is listed under it (CyberCaius)
yetone22bc7a4477ca
diff --git a/internal/provider/rename.go b/internal/provider/rename.goindex 959c1c093..1705097cc 100644--- a/internal/provider/rename.go+++ b/internal/provider/rename.go@@ -4,12 +4,15 @@ import ("encoding/json""errors""fmt"+ "net""os""slices""strings""sync/atomic""time"+ "golang.org/x/net/publicsuffix"+"github.com/yetone/magpie/internal/catalog""github.com/yetone/magpie/internal/settings")@@ -171,6 +174,10 @@ type Renames struct {// hosts are the providers by the host each is on, for magpie's own// requests to a provider being added, which has no id yet (ProbeOf)hosts map[string]string+ // sites are the providers by the site (SiteOf) of each host they are+ // on, "" for a site two are on: a model list asked at a provider's+ // bare domain before it was saved on its api. subdomain (OnSite)+ sites map[string]string}type gaveTo struct {@@ -213,6 +220,41 @@ func (r Renames) OnHost(host string) bool {return ok}+// OnSite is the provider saved now on a host of host's site (SiteOf),+// for a probe recorded under a host no provider is on: CyberCaius on+// Discord, Leee's model list was asked at leee.baby before it was saved+// on another host of that site, and the Usage page listed leee.baby+// beside it. on is whether any provider is on that site; id is "" when+// two are, or host has no site.+func (r Renames) OnSite(host string) (id string, on bool) {+ site := SiteOf(host)+ if site == "" {+ return "", false+ }+ id, on = r.sites[site]+ return id, on+}++// SiteOf is the registrable domain of host (a URL's host, with or without+// its port), lowercased: api.leee.baby:443 is leee.baby. A host on a+// public suffix's own names (a.vercel.app is vercel.app's tenant a), an+// address or a name with no suffix (localhost) has none, "".+func SiteOf(host string) string {+ h := strings.ToLower(strings.TrimSpace(host))+ if i := strings.LastIndex(h, ":"); i >= 0 && !strings.Contains(h[i+1:], "]") && strings.Count(h, ":") == 1 {+ h = h[:i]+ }+ h = strings.TrimSuffix(h, ".")+ if h == "" || strings.ContainsAny(h, ":[]") || net.ParseIP(h) != nil || !strings.Contains(h, ".") {+ return ""+ }+ site, err := publicsuffix.EffectiveTLDPlusOne(h)+ if err != nil {+ return ""+ }+ return site+}+// Timed is whether the id's calls go to more than one provider by when// they were made, so Of needs each call's time.func (r Renames) Timed(id string) bool { return len(r.gave[id]) > 0 }@@ -305,6 +347,19 @@ func renamesOf(ps []Provider) Renames {r.hosts[h] = p.ID}}+ for h, id := range r.hosts {+ site := SiteOf(h)+ if site == "" {+ continue+ }+ if r.sites == nil {+ r.sites = map[string]string{}+ }+ if to, ok := r.sites[site]; ok && to != id {+ id = "" // two providers on it, or a host two are on+ }+ r.sites[site] = id+ }return r}diff --git a/internal/provider/rename_test.go b/internal/provider/rename_test.goindex 82c561312..1056ee33f 100644--- a/internal/provider/rename_test.go+++ b/internal/provider/rename_test.go@@ -168,3 +168,18 @@ func TestRenamesByWhenTheIDWasTaken(t *testing.T) {t.Fatalf("unreadable: %+v", got)}}++// A host's site is its registrable domain whatever its subdomain, case or+// port, and none for an address, a bare name or a tenant of a hosting+// platform's suffix (CyberCaius: leee.baby and api.leee.baby are one).+func TestSiteOf(t *testing.T) {+ for host, want := range map[string]string{+ "leee.baby": "leee.baby", "API.Leee.Baby": "leee.baby", "www.leee.baby:443": "leee.baby",+ "api.example.co.uk": "example.co.uk", "mine.vercel.app": "mine.vercel.app",+ "127.0.0.1:8080": "", "[::1]:8": "", "localhost:3000": "", "": "",+ } {+ if got := SiteOf(host); got != want {+ t.Errorf("SiteOf(%q) = %q, want %q", host, got, want)+ }+ }+}diff --git a/internal/usage/probes.go b/internal/usage/probes.goindex d0fa6834e..3ea11e58c 100644--- a/internal/usage/probes.go+++ b/internal/usage/probes.go@@ -2,6 +2,7 @@ package usageimport ("strings"+ "sync""time""github.com/yetone/magpie/internal/probelog"@@ -63,8 +64,12 @@ func clipProbeError(s string) string {// provider is on it, a deleted one's, by the id the log's calls to that// host were made under (hosts, logSnapshot.hosts): cybercaius on Discord,// a deleted provider's tests were listed under its host beside its id.+// A host no provider was on goes by its site (provider.SiteOf) the same+// way: CyberCaius again, Leee's model list asked at leee.baby before it+// was saved on another host of that site.func probeChunk(probes []probeRow, since time.Time, renames provider.Renames, hosts map[string]string) *rowChunk {var c *rowChunk+ sites := sync.OnceValue(func() map[string]string { return sitesOf(hosts) })for _, p := range probes {if !since.IsZero() && p.Time.Before(since) {continue@@ -73,7 +78,7 @@ func probeChunk(probes []probeRow, since time.Time, renames provider.Renames, hoc = &rowChunk{}}r := p.Record- r.Provider = probeProvider(r, renames, hosts)+ r.Provider = probeProvider(r, renames, hosts, sites)c.add(Row{Record: r, Source: SourceProbe}, "", p.off, false)}if c == nil {@@ -83,12 +88,30 @@ func probeChunk(probes []probeRow, since time.Time, renames provider.Renames, horeturn c}+// sitesOf is hosts by site (provider.SiteOf): the provider the log's+// calls to that site went to, "" for a site two providers' calls went to.+func sitesOf(hosts map[string]string) map[string]string {+ out := map[string]string{}+ for h, id := range hosts {+ site := provider.SiteOf(h)+ if site == "" {+ continue+ }+ if to, ok := out[site]; ok && to != id {+ id = ""+ }+ out[site] = id+ }+ return out+}+// probeProvider is the provider a probe row is listed under: its id as it// is now, and for one recorded under its host before the provider had an// id, the provider saved on that host, or, with none on it now, the one-// the log's calls to that host went to. A host two providers were on, or-// none, stays the host.-func probeProvider(r Record, renames provider.Renames, hosts map[string]string) string {+// the log's calls to that host went to; with neither, the same by the+// host's site. A host or site two providers were on, or none, stays the+// host.+func probeProvider(r Record, renames provider.Renames, hosts map[string]string, sites func() map[string]string) string {id := renames.ProbeOf(r.Provider, r.Time)if id != r.Provider || !strings.EqualFold(r.Provider, r.Host) || renames.OnHost(r.Host) {return id@@ -96,5 +119,14 @@ func probeProvider(r Record, renames provider.Renames, hosts map[string]string)if to := hosts[strings.ToLower(r.Host)]; to != "" {return renames.Of(to, r.Time)}+ if to, on := renames.OnSite(r.Host); on {+ if to != "" {+ return to+ }+ } else if site := provider.SiteOf(r.Host); site != "" {+ if to := sites()[site]; to != "" {+ return renames.Of(to, r.Time)+ }+ }return strings.ToLower(id) // one host, however it was typed}diff --git a/internal/usage/rename_usage_test.go b/internal/usage/rename_usage_test.goindex a6e5f09b7..53045d99b 100644--- a/internal/usage/rename_usage_test.go+++ b/internal/usage/rename_usage_test.go@@ -164,3 +164,63 @@ func TestProbesOfADeletedProviderStayWithIt(t *testing.T) {t.Fatalf("twice.example's rows: total %d", p.Total)}}++// CyberCaius on Discord: Leee's model list was asked at leee.baby while it+// was being added, and it was saved on another host of that site, so the+// Usage page's provider filter listed Leee and leee.baby (as example and+// example.me before: a provider on api.example.me). A probe recorded under+// a host no provider is on is the one provider's on a host of the same site+// (its registrable domain, whatever the subdomain or port), and after that+// one is deleted, the one the log's calls to that site went to. A site two+// providers are on, a hosting platform's names (vercel.app is a suffix of+// its own) and an address stay as they were recorded.+func TestProbesOfAProviderOnAnotherHostOfItsSiteListIt(t *testing.T) {+ reportedHome(t)+ then := time.Now().Add(-time.Hour)+ adding := probelog.With(context.Background(), probelog.KindList, "", "")+ for _, u := range []string{+ "https://leee.baby/v1/models", "https://Leee.Baby/v1/models", "https://www.leee.baby:443/v1/models",+ "https://two.example/v1/models", "https://mine.vercel.app/v1/models", "http://127.0.0.1:7/v1/models",+ } {+ probelog.Note(adding, u, http.MethodGet, then, 200, nil)+ }+ id, err := provider.Add(provider.Provider{Name: "Leee", Key: "k", Chat: "https://api.leee.baby/v1", Models: []string{"gpt-9"}})+ if err != nil {+ t.Fatal(err)+ }+ saved, err := provider.Find(id)+ if err != nil {+ t.Fatal(err)+ }+ for _, p := range []provider.Provider{+ {ID: "two-a", Name: "Two A", Key: "a", Chat: "https://a.two.example/v1", Models: []string{"m"}},+ {ID: "two-b", Name: "Two B", Key: "b", Chat: "https://b.two.example/v1", Models: []string{"m"}},+ {ID: "theirs", Name: "Theirs", Key: "c", Chat: "https://theirs.vercel.app/v1", Models: []string{"m"}},+ {ID: "local", Name: "Local", Key: "d", Chat: "http://127.0.0.1:8/v1", Models: []string{"m"}},+ } {+ if err := provider.Save(p); err != nil {+ t.Fatal(err)+ }+ }+ list := func() []string { return QueryPage(All, Filter{}, 0, 50).Providers }+ want := []string{"127.0.0.1:7", id, "mine.vercel.app", "two.example"}+ if !slices.Equal(list(), want) { // saved, no call made yet+ t.Fatalf("no calls: providers %v, want %v", list(), want)+ }+ Append(Record{Time: then.Add(time.Minute), Agent: "codex", Session: "s1", Provider: id, Host: saved.Where(), Model: "gpt-9", Input: 100, Output: 10, Status: 200})+ if !slices.Equal(list(), want) {+ t.Fatalf("saved: providers %v, want %v", list(), want)+ }+ if p := QueryPage(All, Filter{Provider: id}, 0, 50); p.Total != 4 || p.Sum.Calls != 1 {+ t.Fatalf("%s's rows: total %d, sum %+v", id, p.Total, p.Sum)+ }+ if err := provider.Delete(id); err != nil {+ t.Fatal(err)+ }+ if !slices.Equal(list(), want) {+ t.Fatalf("deleted: providers %v, want %v", list(), want)+ }+ if p := QueryPage(All, Filter{Provider: id}, 0, 50); p.Total != 4 {+ t.Fatalf("deleted %s's rows: total %d", id, p.Total)+ }+}