NorethSea's bridge (hermes-codex-thread-bridge) starts Codex's
app-server under a User-Agent of its own, so its calls were a row
apart from Codex on Usage, and its threads weren't Codex's in Sessions.
Rather than learn one bridge's name, the user says which agent a client
counts as.
- settings: clientAgents, client name (lowercased) -> agent id.
ClientAgentsNow reads just that field through filememo, so a pass over
the ledger costs a stat.
- usage: ReportedAs(ua) is AgentOf, then the user's mapping. It is used
for what is reported only: the summary, the agents list and filters,
request rows and pages, priced blocks, direct, Vias and gateway
sessions. The gateway's routing, keys and caps still go by AgentOf,
the client's own name. The mapping is read once per ledger pass
(readLogSnapshot) and is part of the keys of the summary price cache,
the vias cache and the request page cache, so a change redraws the
history already counted. A request row keeps the client's own name in
a new "client" field, shown as "Client" in its details.
- gui: POST /api/usage/arrange takes {count:{client,as}}. It refuses a
client magpie already knows as an agent, "other" or magpie's own, and
an "as" that isn't a known agent or is magpie. as "" counts it on its
own again. /api/settings keeps the mapping it doesn't send. On the
Usage page an unknown client's "…" offers "Count as an agent…" (a
picker of the known agents), the agent's row says "with <client>",
and the agent's "…" offers "Count <client> on its own". Strings are
in all 5 languages.
Run:
- go test -count=1 ./internal/usage ./internal/settings ./internal/gui: ok.
- TestClientCountedAsAgent goes red with reportedAs broken, and with the
mapping left out of the vias key or the request page key. With the
summary key alone left out it stays green, because the settings stat
already moves it.
- TestUsageArrangeCountClient and TestSettingsSaveKeepsWhatItDoesNotSend
(ClientAgents added) pass.
- Playwright usage-count-client.test.cjs (new) and usage-agents-hide
(its menu now has two items for an unknown client) pass in Chromium
and WebKit, in all 5 languages, at 1100 and 420: 54/54. With
countable() forced false, the new test goes red.
- gofmt -l is clean. go vet is clean for darwin, linux -tags nogui and
windows.
Not tried: a real hermes bridge (the records' UA is from the issue), and
the Client line in a request's details has no GUI test.
157 lines
4.7 KiB
Go
157 lines
4.7 KiB
Go
package usage
|
|
|
|
import (
|
|
"slices"
|
|
"sync"
|
|
|
|
"github.com/yetone/magpie/internal/catalog"
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
// swapKnown marks a row whose Swapped flag was settled when it was priced,
|
|
// so reading it back needs no second look at the two names.
|
|
const swapKnown = 64
|
|
|
|
// pricedAs is what a gateway call is priced as, the same for every call of
|
|
// one provider, model, served model and effort.
|
|
type pricedAs struct {
|
|
provider string
|
|
flags uint16
|
|
price *catalog.Price
|
|
}
|
|
|
|
// blockPricer prices the gateway's raw blocks as a query reads them. What a
|
|
// row's price, provider id, swap and routing are depends on a few of its
|
|
// names only, so they are worked out once for each set of names, not once
|
|
// per call: 750k calls name a few hundred.
|
|
type blockPricer struct {
|
|
aliases map[string]string
|
|
wires map[string]string
|
|
price func(Record) *catalog.Price
|
|
by map[[4]string]pricedAs
|
|
agents map[string][2]string
|
|
pooled []*[]packedRow
|
|
}
|
|
|
|
// pricedRows are the rows of the blocks a query priced, handed back when
|
|
// it is done with them: a query over the whole history decodes every block,
|
|
// and allocating and clearing their rows again each time was a third of it.
|
|
var pricedRows = sync.Pool{New: func() any { s := make([]packedRow, 0, logBlockRows); return &s }}
|
|
|
|
// release hands the rows of the blocks it priced back, once the query no
|
|
// longer reads them (a page's rows are copies).
|
|
func (b *blockPricer) release() {
|
|
for _, rows := range b.pooled {
|
|
pricedRows.Put(rows)
|
|
}
|
|
b.pooled = nil
|
|
}
|
|
|
|
func newBlockPricer(aliases, wires map[string]string, price func(Record) *catalog.Price) *blockPricer {
|
|
return &blockPricer{aliases: aliases, wires: wires, price: price, by: map[[4]string]pricedAs{}, agents: map[string][2]string{}}
|
|
}
|
|
|
|
// as is priceRow's answer for a gateway call of these names.
|
|
func (b *blockPricer) as(prov, model, served, effort string) pricedAs {
|
|
k := [4]string{prov, model, served, effort}
|
|
if a, ok := b.by[k]; ok {
|
|
return a
|
|
}
|
|
if id, ok := b.aliases[prov]; ok {
|
|
prov = id
|
|
}
|
|
a := pricedAs{provider: prov, flags: swapKnown}
|
|
sent := provider.SentNameIn(b.wires, prov, model, effort)
|
|
if served != "" && Swapped(sent, served) && !(SameSpelled(model, served) || ServingName(model, served) || RelayRenamed(model, served)) {
|
|
a.flags |= 2
|
|
}
|
|
if GroupRouted(sent, served) {
|
|
a.flags |= 32
|
|
}
|
|
a.price = b.price(Record{Provider: prov, Model: model, Served: served, Effort: effort})
|
|
b.by[k] = a
|
|
return a
|
|
}
|
|
|
|
// agent is reportedAs's answer for a client, the agent counted and the
|
|
// client's own name when the user's counting changed it.
|
|
func (b *blockPricer) agent(ua string) (string, string) {
|
|
a, ok := b.agents[ua]
|
|
if !ok {
|
|
a[0], a[1] = reportedAs(ua)
|
|
b.agents[ua] = a
|
|
}
|
|
return a[0], a[1]
|
|
}
|
|
|
|
// block is the priced copy of a raw block, made for one query and not kept.
|
|
func (b *blockPricer) block(packed *rowChunk) *rowChunk {
|
|
buf := pricedRows.Get().(*[]packedRow)
|
|
raw := packed.unpackInto(*buf)
|
|
// unpackInto decodes into the buffer whenever it is large enough
|
|
if raw != nil && raw != packed && cap(*buf) >= len(raw.Rows) {
|
|
b.pooled = append(b.pooled, buf)
|
|
} else {
|
|
pricedRows.Put(buf)
|
|
}
|
|
if raw == nil {
|
|
return nil
|
|
}
|
|
c := *raw
|
|
if raw == packed {
|
|
c.Rows = slices.Clone(raw.Rows) // a block too small to compress is shared
|
|
}
|
|
c.Strings = slices.Clip(c.Strings)
|
|
added := map[string]uint32{}
|
|
intern := func(s string, was uint32) uint32 {
|
|
if c.Strings[was] == s {
|
|
return was
|
|
}
|
|
if id, ok := added[s]; ok {
|
|
return id
|
|
}
|
|
id := uint32(len(c.Strings))
|
|
c.Strings = append(c.Strings, s)
|
|
added[s] = id
|
|
return id
|
|
}
|
|
type ids struct {
|
|
provider uint32
|
|
flags uint16
|
|
price *catalog.Price
|
|
}
|
|
names := map[[4]uint32]ids{}
|
|
agents := map[uint32][2]uint32{}
|
|
for i := range c.Rows {
|
|
p := &c.Rows[i]
|
|
k := [4]uint32{p.Text[1], p.Text[5], p.Text[7], p.Text[8]}
|
|
got, ok := names[k]
|
|
if !ok {
|
|
a := b.as(c.Strings[k[0]], c.Strings[k[1]], c.Strings[k[2]], c.Strings[k[3]])
|
|
got = ids{intern(a.provider, k[0]), a.flags, a.price}
|
|
names[k] = got
|
|
}
|
|
agent, ok := agents[p.Text[0]]
|
|
if !ok {
|
|
a, client := b.agent(c.Strings[p.Text[0]])
|
|
agent = [2]uint32{intern(a, p.Text[0]), intern(client, p.Text[rowClient])}
|
|
agents[p.Text[0]] = agent
|
|
}
|
|
p.Text[0], p.Text[1], p.Text[rowClient] = agent[0], got.provider, agent[1]
|
|
p.Flags = p.Flags&^(1|2|32) | got.flags
|
|
if p.Flags&rowOwnCost != 0 {
|
|
// a reported call that said what it cost costs that (#1874)
|
|
if p.Tokens[0]+p.Tokens[1] > 0 {
|
|
p.Flags |= 1
|
|
}
|
|
continue
|
|
}
|
|
p.Cost = 0
|
|
if got.price != nil && p.Tokens[0]+p.Tokens[1] > 0 {
|
|
p.Flags |= 1
|
|
p.Cost = got.price.CostSplit(int(p.Tokens[0]), int(p.Tokens[1]), int(p.Tokens[2]), int(p.Tokens[3]), int(p.Tokens[5]))
|
|
}
|
|
}
|
|
return &c
|
|
}
|