diff --git a/internal/provider/rename.go b/internal/provider/rename.go index e88c05ec4..80f274b0f 100644 --- a/internal/provider/rename.go +++ b/internal/provider/rename.go @@ -168,6 +168,9 @@ type Gave struct { type Renames struct { now map[string]string gave map[string][]gaveTo + // 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 } type gaveTo struct { @@ -190,6 +193,20 @@ func (r Renames) Of(id string, at time.Time) string { return id } +// ProbeOf is Of for one of magpie's own requests (probelog): one sent +// while a provider was being added, its Detect or Test before it had an +// id, is recorded under its host, and is the provider's on that host now. +// yyddbds on Discord: 大灰狼, on dahuilang.best, was listed twice in the +// Requests page's providers, once as dahuilang.best for the model lists +// asked while adding it. A host two providers are on, or none, stays as +// it is. +func (r Renames) ProbeOf(id string, at time.Time) string { + if to := r.hosts[strings.ToLower(id)]; to != "" { + return to + } + return r.Of(id, at) +} + // 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 } @@ -261,6 +278,27 @@ func renamesOf(ps []Provider) Renames { for _, gs := range r.gave { slices.SortStableFunc(gs, func(a, b gaveTo) int { return a.Until.Compare(b.Until) }) } + for _, p := range ps { + if p.Hidden { + continue + } + for _, u := range []string{p.Chat, p.Responses, p.Anthropic, p.Gemini, p.Decide} { + h := HostOf(u) + // a provider's id (one stored by hand can have a dot, b.ai) + // is that provider's, never a host's + if h == "" || held[h] { + continue + } + if r.hosts == nil { + r.hosts = map[string]string{} + } + if to, ok := r.hosts[h]; ok && to != p.ID { + r.hosts[h] = "" // two providers on it: which one is unknown + continue + } + r.hosts[h] = p.ID + } + } return r } diff --git a/internal/usage/probes.go b/internal/usage/probes.go index cc0bf6de9..9f13842e7 100644 --- a/internal/usage/probes.go +++ b/internal/usage/probes.go @@ -57,6 +57,8 @@ func clipProbeError(s string) string { // row's is; nil for none. // A provider renamed since is listed by the id it has now (Renames): the // Requests page's provider list named a renamed one twice (cybercaius). +// One asked while a provider was being added, under its host, is listed +// by the id of the provider on that host (Renames.ProbeOf). func probeChunk(probes []probeRow, since time.Time, renames provider.Renames) *rowChunk { var c *rowChunk for _, p := range probes { @@ -67,7 +69,7 @@ func probeChunk(probes []probeRow, since time.Time, renames provider.Renames) *r c = &rowChunk{} } r := p.Record - r.Provider = renames.Of(r.Provider, r.Time) + r.Provider = renames.ProbeOf(r.Provider, r.Time) c.add(Row{Record: r, Source: SourceProbe}, "", p.off, false) } if c == nil { diff --git a/internal/usage/rename_usage_test.go b/internal/usage/rename_usage_test.go index 925924371..da2ec1ef6 100644 --- a/internal/usage/rename_usage_test.go +++ b/internal/usage/rename_usage_test.go @@ -1,6 +1,8 @@ package usage import ( + "context" + "net/http" "slices" "testing" "time" @@ -80,3 +82,40 @@ func TestRenamedProviderShownOnce(t *testing.T) { t.Fatalf("summary %v", calls) } } + +// yyddbds on Discord: one custom provider, 大灰狼 on dahuilang.best, was +// listed twice in the Requests page's providers, 大灰狼 and +// dahuilang.best. The model lists asked while it was being added had no +// id to go under, so they were recorded under its host, and the page +// listed that host as a provider. They are the provider's on that host +// now, in a log written before this was fixed too, while a host no +// provider is on, or two are, stays as it was recorded. +func TestProbesOfAProviderBeingAddedListItOnce(t *testing.T) { + reportedHome(t) + then := time.Now().Add(-time.Hour) + // the Add sheet's Detect, before the provider has an id: written as + // probelog writes it, under the host of the address asked + adding := probelog.With(context.Background(), probelog.KindDetect, "", "") + probelog.Note(adding, "https://dahuilang.best/v1/models", http.MethodGet, then, 200, nil) + probelog.Note(adding, "https://other.example/v1/models", http.MethodGet, then, 200, nil) + probelog.Note(adding, "https://DaHuiLang.best/v1/chat/completions", http.MethodPost, then, 401, nil) + probelog.Note(adding, "https://shared.example:8443/v1/models", http.MethodGet, then, 200, nil) + id, err := provider.Add(provider.Provider{Name: "大灰狼", Key: "k", Chat: "https://dahuilang.best/v1", Models: []string{"gpt-9"}}) + if err != nil { + t.Fatal(err) + } + for i, k := range []string{"k1", "k2"} { + if err := provider.Save(provider.Provider{ID: "shared-" + k, Name: "Shared " + k, Key: k, Chat: "https://shared.example:8443/v" + string(rune('1'+i)), Models: []string{"m"}}); err != nil { + t.Fatal(err) + } + } + Append(Record{Time: then.Add(time.Minute), Agent: "codex", Session: "s1", Provider: id, Model: "gpt-9", Input: 100, Output: 10, Status: 200}) + + page := QueryPage(All, Filter{}, 0, 50) + if want := []string{id, "other.example", "shared.example:8443"}; !slices.Equal(page.Providers, want) { + t.Fatalf("providers %v, want %v", page.Providers, want) + } + if p := QueryPage(All, Filter{Provider: id}, 0, 50); p.Total != 3 || p.Sum.Calls != 1 { + t.Fatalf("%s's rows: total %d, sum %+v", id, p.Total, p.Sum) + } +}