CyberCaius on Discord, on the latest release: the Usage page's provider
filter listed Leee and leee.baby, and leee.baby's rows were the model
lists ("模型列表", magpie 自身, 200) asked while Leee was being added.
In the first report the same shape was example on api.example.me with
example.me in the filter, which we had put down to another address typed.
The Add sheet's model list runs before the provider has an id, so
probelog records it under the host asked. 8e2ba36de (v0.1.1697) and
94c2aaecb (v0.1.1726) give such a row to the provider saved on that
exact host, or to the one the log's calls to it went to. The reporter
asks a relay's bare domain (leee.baby, example.me), which answers
/v1/models, and saves the provider on another host of it
(api.example.me). No provider is on leee.baby, no call went there, so
the row stayed the host.
Now a probe recorded under a host no provider is on, and no call went
to, goes by the host's site: its registrable domain (provider.SiteOf,
publicsuffix eTLD+1, port and case aside). The one provider saved on a
host of that site takes it (Renames.OnSite), and with none saved there,
the one the log's calls to that site went to (sitesOf over
logSnapshot.hosts), so it stays with a deleted provider too. A site two
providers are on stays the host; a tenant of a hosting suffix
(x.vercel.app), an address and a bare name (localhost) have no site and
stay as they were. It is done when the log is read: rows written before
this release, the reporter's 16:57 and 17:32 ones, are listed under the
provider too, with nothing to migrate and nothing to set.
Run (rebased on 97332140a):
- go test -tags nogui ./internal/usage ./internal/usageapi
./internal/probelog: ok.
- ./internal/provider -skip TestUnreadLoginsWriteErrorsKeepCredentials
(#3632 crashes the binary): ok but TestFetchAsksASharedBaseOnce,
TestFetchAsksOneListOnceForTwoBasesOfIt and
TestFetchUnionsProtocolListsWithoutChat, open as #3854.
- New TestProbesOfAProviderOnAnotherHostOfItsSiteListIt: model lists at
leee.baby, Leee.Baby and www.leee.baby:443 before Add on
api.leee.baby; two providers on *.two.example, mine.vercel.app beside
theirs.vercel.app, 127.0.0.1:7 beside 127.0.0.1:8. Providers are
[127.0.0.1:7 leee mine.vercel.app two.example] with no call made,
after a call, and after Delete; leee's filter has its 4 rows.
- New TestSiteOf.
- gofmt -l; go vet darwin, GOOS=linux -tags nogui, GOOS=windows for
internal/provider and internal/usage.
- Sandbox HOME, real binary, `magpie web`: POST /api/provider/list at
http://leee.localhost:3488/v1 (a fake OpenAI-compatible server), then
/api/provider/save on http://api.leee.localhost:3488/v1 as Leee2.
Built at e54a7d50b, /api/usage/requests listed leee and
leee.localhost:3488; the fixed build, over the same usage.jsonl, lists
leee and leee2 with the row under leee2.
Broken by hand: probeProvider without the saved-site branch fails the
new test at "no calls" with [127.0.0.1:7 leee.baby mine.vercel.app
two.example www.leee.baby:443]; without the log-site branch it fails at
"deleted" with leee.baby and www.leee.baby:443 back. On e54a7d50b the
test fails with those two names, the reporter's extra entry.
Not tried: the reporter's own usage.jsonl and providers.json (Leee's
saved host is inferred from the example.me report, not seen); a
reporter whose two providers share one site (stays the host, by
design); the GUI suite (no GUI file changed); the full go test ./...
133 lines
4.6 KiB
Go
133 lines
4.6 KiB
Go
package usage
|
|
|
|
import (
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/probelog"
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
// SourceProbe is Record.Source for a request magpie sent a provider on its
|
|
// own, outside any agent's turn (probelog): the editor's Test, Detect
|
|
// APIs, a model list's refresh. Its Kind says which (probelog.KindTest,
|
|
// KindDetect, KindList). Such a row is listed on the request log, told
|
|
// apart there, and counted nowhere: no total, cost, chart, group, budget,
|
|
// window hold or observer reads it. The log's index keeps it apart from
|
|
// the calls (logSnapshot.probes) so a reader of those never meets it.
|
|
const SourceProbe = "probe"
|
|
|
|
// IsProbe is a request magpie sent on its own (SourceProbe).
|
|
func (r Record) IsProbe() bool { return r.Source == SourceProbe }
|
|
|
|
// IsProbe is a row of such a request. A packed row keeps the row's Source,
|
|
// not its record's, so the request log asks the row.
|
|
func (r Row) IsProbe() bool { return r.Source == SourceProbe || r.Record.IsProbe() }
|
|
|
|
// probeRow is a probe's record and where its line ends in the log, as a
|
|
// gateway row's Order is.
|
|
type probeRow struct {
|
|
Record
|
|
off int64
|
|
}
|
|
|
|
func init() {
|
|
probelog.Sink = func(p probelog.Row) {
|
|
Append(Record{Time: p.Time, Agent: "magpie", Provider: p.Provider, ProviderAccount: p.Account,
|
|
Host: p.Host, Model: p.Model, Requested: p.Model, Status: p.Status, Millis: p.Millis,
|
|
Error: clipProbeError(p.Error), Endpoint: p.Endpoint, Kind: p.Kind, Source: SourceProbe})
|
|
}
|
|
}
|
|
|
|
// clipProbeError keeps an error's first 500 bytes, as a vendor's answer can
|
|
// be a whole page.
|
|
func clipProbeError(s string) string {
|
|
const most = 500
|
|
if len(s) <= most {
|
|
return s
|
|
}
|
|
cut := most
|
|
for cut > 0 && s[cut]&0xC0 == 0x80 { // not inside a character
|
|
cut--
|
|
}
|
|
return s[:cut] + "…"
|
|
}
|
|
|
|
// probeChunk is the probes made since since as one chunk of the request
|
|
// log's rows, each in the order its line has in the log as a gateway
|
|
// 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), and once no
|
|
// 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
|
|
}
|
|
if c == nil {
|
|
c = &rowChunk{}
|
|
}
|
|
r := p.Record
|
|
r.Provider = probeProvider(r, renames, hosts, sites)
|
|
c.add(Row{Record: r, Source: SourceProbe}, "", p.off, false)
|
|
}
|
|
if c == nil {
|
|
return nil
|
|
}
|
|
c.dict, c.shapeIDs = nil, nil
|
|
return 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; 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
|
|
}
|
|
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
|
|
}
|