jiabubu on Discord (#feedback): the Routing page's request list showed no credits for a request that spent them. The gateway already read a reply's usage.credit (#2512) into the usage ledger, which the Usage page shows, but the route trace's RouteUsage had no field for it. RouteUsage now carries Credits (the reply's, nil when it said none). The Routing page's request row shows "N credits" after its cost, or alone when the request has no known price (it read "—" before), with the Usage page's help text under the pointer; By session sums them in the session's cost, shown even when no request in it was priced. The help text "What it cost in its plan's credits, as the reply said" moves from the Usage plugin's i18n.js to the core's, since two plugins say it now. Tests: TestPluginPlanCreditsReachTheLedger also checks the route's usage carries 0.01 on every endpoint (red without the field: Credits <nil>). routing-credits.test.cjs: credits alone, priced plus credits, none, and a session's sum, in chromium and webkit, en zh zh-TW ja de, at 1100 and 420, with no overflow. Broken by hand: the old client.js makes routing-credits 10 of 10 red. Run at the pushed head on mote: gofmt -l clean; go test -run 'TestDailyCredits|TestPluginPlanCredits|Credits' ./internal/provider ./internal/gateway ok; GOOS=linux go vet -tags nogui and GOOS=windows go vet of provider, gateway, gui ok (rebased on origin/mainb92b3bc5b). The full GUI suite (WSL, both engines, every language) ran at the earlier base 50265256b; its 58 files red on head but not in main's run were run again, and the 11 files still in question were run on head and on origin/mainb92b3bc5bside by side: routing-credits and i18n-keys pass, and every other red is a test main fails too, with an open issue (#4489 usage-ledger-ttft, #4719 usage-speed, #3593/#4560/#4585 routing-rules-line, #4713 routing-time-went, #2559/#4966 routing-bodies, #4400 sessions-bar-names, #1745 group-member-info, #5052 account-eye); head shows a few more languages of those same tests. Not tried: a real WorkBuddy request through the GUI; history written before this release has no credits in its routes and reads as before.
44 lines
2.1 KiB
Go
44 lines
2.1 KiB
Go
package gateway
|
|
|
|
import (
|
|
"testing"
|
|
)
|
|
|
|
// WorkBuddy served by its plugin keeps the credits a reply said on its
|
|
// row, however the agent asked: chat, Messages or Responses, streamed or
|
|
// whole, translated to WorkBuddy's chat stream (jiabubu on Discord: a
|
|
// request's credits should be said on its row, the Usage page's and the
|
|
// Routing page's request list too). TestPlanCreditsReachTheLedger
|
|
// holds a provider of its own to it; this is the plugin host's way.
|
|
func TestPluginPlanCreditsReachTheLedger(t *testing.T) {
|
|
stream := sse(`data: {"id":"c","model":"fake-1","choices":[{"index":0,"delta":{"content":"hi"}}]}`,
|
|
`data: {"id":"c","model":"fake-1","choices":[{"index":0,"delta":{},"finish_reason":"stop"}],`+wbCreditUsage+`}`, "data: [DONE]")
|
|
for _, c := range []struct{ name, path, body string }{
|
|
{"chat stream", "/v1/chat/completions", `{"model":"workbuddy-ai/fake-1","stream":true,"messages":[{"role":"user","content":"hi"}]}`},
|
|
{"chat whole", "/v1/chat/completions", `{"model":"workbuddy-ai/fake-1","messages":[{"role":"user","content":"hi"}]}`},
|
|
{"messages", "/v1/messages", `{"model":"workbuddy-ai/fake-1","max_tokens":10,"stream":true,"messages":[{"role":"user","content":"hi"}]}`},
|
|
{"messages whole", "/v1/messages", `{"model":"workbuddy-ai/fake-1","max_tokens":10,"messages":[{"role":"user","content":"hi"}]}`},
|
|
{"responses", "/v1/responses", `{"model":"workbuddy-ai/fake-1","stream":true,"input":"hi"}`},
|
|
{"responses whole", "/v1/responses", `{"model":"workbuddy-ai/fake-1","input":"hi"}`},
|
|
} {
|
|
t.Run(c.name, func(t *testing.T) {
|
|
f := &fake{t: t, reply: stream}
|
|
movedFake(t, "workbuddy-ai", f)
|
|
s := New()
|
|
code, body := postTo(t, s, c.path, c.body)
|
|
if code != 200 {
|
|
t.Fatalf("%d %s", code, body)
|
|
}
|
|
r := lastUsage(t)
|
|
if r.Credits == nil || *r.Credits != 0.01 {
|
|
t.Fatalf("%s %s: credits %v, want 0.01", r.Provider, r.Endpoint, r.Credits)
|
|
}
|
|
// the Routing page's row: its trace's usage says them too
|
|
rt := lastRoute(s)
|
|
if n := len(rt.Usage); n == 0 || rt.Usage[n-1].Credits == nil || *rt.Usage[n-1].Credits != 0.01 {
|
|
t.Fatalf("the route's usage: %+v, want credits 0.01 on the last", rt.Usage)
|
|
}
|
|
})
|
|
}
|
|
}
|