provider: an enterprise WorkBuddy account's card shows its credits, from the enterprise meter WorkBuddy's usage page reads (#1904, CnsMaple)

An enterprise seat's /billing/meter/get-user-resource-summary answers with
no packages, so the card showed no quota. WorkBuddy's own usage page reads
/billing/meter/get-enterprise-user-usage (credit / limitNum, with the cycle's
start and reset in Beijing time). When the summary gives no credits, wbQuota
now asks that meter and shows a Credits window ("196.13 / 2000", resetting at
cycleResetTime). A personal account's refusal there (400 10001 "uid or
enterpriseID is empty", seen on a real WorkBuddy AI Pro account on
2026-10-13) is ignored and the card stays as before. A paid account with
packages never asks it.

Moved users get the same from @magpie-community/opencode-workbuddy-auth
0.1.19 (plugins 169e4e6, on npm); the mover's min is raised to 0.1.19.

Run: workbuddy_enterprise_test.go, built from the reporter's literal
response bytes. TestWorkBuddyEnterpriseCredits and
TestWorkBuddyPersonalNotEnterprise pass, and both fail with the fallback
taken out. go test -run TestWorkBuddy ./internal/provider passes. Not tried
with a real enterprise account; "credit" is read as used, which the
reporter is asked to confirm.

Also run, rebased on e1b70c13d: go test -tags nogui ./internal/provider
(the whole package, 430s) and go test ./internal/gateway -run WorkBuddy
pass under a sandbox HOME; gofmt clean; go vet for darwin, linux and
windows (-tags nogui) pass. The reporter's follow-up (04:27Z) saw
"Credits 218.67 / 2000" on the same enterprise account after reinstalling,
while get-user-resource-summary still answered empty Packages: the
reinstall installed the plugin's 0.1.19, which carries this fallback.
This commit is contained in:
yetone
2026-10-13 13:00:04 +08:00
parent 63a361b5ff
commit 7565d93c8b
3 changed files with 140 additions and 1 deletions
+1 -1
View File
@@ -16,7 +16,7 @@ func init() {
for _, w := range []*wbSite{wbCN, wbAI} {
movers[w.id] = &mover{
pkg: "@magpie-community/opencode-workbuddy-auth",
min: "0.1.18", // WorkBuddy 5.6+ keeps its desktop sign-in encrypted, and the plugin says so and points to the browser sign-in instead of a 502 (plugins #18); 0.1.17: a restricted account's 403 11140 ends in provider.WBRestrictedHint, so it rests 2h as restricted (#1849); 0.1.16: WorkBuddy AI's GLM models are text-only, so magpie's image-describing model reads their images (Yirasumi on Discord); 0.1.15: a system prompt sent as a developer message passes the channel check, an object tool_choice is sent as WorkBuddy takes it, and max_completion_tokens goes as max_tokens so the cap holds (plugins #96); 0.1.14: the Credits window says when the soonest credits left run out, for Smart routing to spend them first (cdredfox on X); an enterprise account names its enterprise, so it lists and uses the enterprise's models, not the personal plan's (plugins #75); a chat's tools get shapes WorkBuddy AI's models take, so ZCode's and DeepSeek Harness's chats on deepseek-v4.1-flash aren't 400 "Invalid request parameters" (wyh on Discord); one user send is one request in the usage list: every step carries one X-Conversation-Request-ID, and both builds name the client (使用端) (plugins #48); credits used up (code 14018) say so, with the nested error message (plugins #25); Codex's and Claude Code's chats pass WorkBuddy's channel check (#182); a failure's status and its sign-in mark as the built-in's; each model's credit rate; the sign-in renewed ahead of time through auth.refresh; the credits' count beside their share (#659)
min: "0.1.19", // an enterprise account's card shows its credits, from the enterprise meter WorkBuddy's usage page reads (#1904); 0.1.18: WorkBuddy 5.6+ keeps its desktop sign-in encrypted, and the plugin says so and points to the browser sign-in instead of a 502 (plugins #18); 0.1.17: a restricted account's 403 11140 ends in provider.WBRestrictedHint, so it rests 2h as restricted (#1849); 0.1.16: WorkBuddy AI's GLM models are text-only, so magpie's image-describing model reads their images (Yirasumi on Discord); 0.1.15: a system prompt sent as a developer message passes the channel check, an object tool_choice is sent as WorkBuddy takes it, and max_completion_tokens goes as max_tokens so the cap holds (plugins #96); 0.1.14: the Credits window says when the soonest credits left run out, for Smart routing to spend them first (cdredfox on X); an enterprise account names its enterprise, so it lists and uses the enterprise's models, not the personal plan's (plugins #75); a chat's tools get shapes WorkBuddy AI's models take, so ZCode's and DeepSeek Harness's chats on deepseek-v4.1-flash aren't 400 "Invalid request parameters" (wyh on Discord); one user send is one request in the usage list: every step carries one X-Conversation-Request-ID, and both builds name the client (使用端) (plugins #48); credits used up (code 14018) say so, with the nested error message (plugins #25); Codex's and Claude Code's chats pass WorkBuddy's channel check (#182); a failure's status and its sign-in mark as the built-in's; each model's credit rate; the sign-in renewed ahead of time through auth.refresh; the credits' count beside their share (#659)
agents: []string{w.id},
out: func() ([]Moving, error) {
var out []Moving
+46
View File
@@ -691,10 +691,56 @@ func wbQuota(ctx context.Context, a wbAccount) SubscriptionQuota {
w := QuotaWindow{Name: "Credits", Used: 100 * used / total, Display: fmt.Sprintf("%s / %s", compactNumber(used), compactNumber(total)),
Amount: used, Limit: total, Unit: "credits"}
q.Windows = append(q.Windows, w)
return q
}
// an enterprise (team) account's summary has no packages: its credits
// are the enterprise meter's, which WorkBuddy's own usage page reads
// (#1904, CnsMaple). A personal account's refusal of it (10001 "uid or
// enterpriseID is empty") leaves the card as the summary said.
var ent wbEnterpriseUsage
if wbCall(ctx, http.MethodPost, a.site.api()+"/billing/meter/get-enterprise-user-usage", wbAuthHeaders(a.site, c), map[string]any{}, &ent) == nil {
if w, ok := ent.window(); ok {
q.Plan = firstNonEmpty(a.Plan, "Enterprise")
q.Windows = append(q.Windows, w)
}
}
return q
}
// wbEnterpriseUsage is an enterprise account's credits for the cycle, flat:
// Credit used of LimitNum, its times in Beijing time ("2026-11-09 00:00:00").
type wbEnterpriseUsage struct {
Credit wbNum `json:"credit"`
LimitNum wbNum `json:"limitNum"`
CycleStartTime string `json:"cycleStartTime"`
CycleResetTime string `json:"cycleResetTime"`
}
// window is u as a Credits window, resetting at the cycle's reset; false
// when it grants none.
func (u wbEnterpriseUsage) window() (QuotaWindow, bool) {
limit, used := float64(u.LimitNum), float64(u.Credit)
if limit <= 0 {
return QuotaWindow{}, false
}
w := QuotaWindow{Name: "Credits", Used: 100 * used / limit, Display: fmt.Sprintf("%s / %s", compactNumber(used), compactNumber(limit)),
Amount: used, Limit: limit, Unit: "credits"}
at := func(v string) time.Time {
t, err := time.ParseInLocation("2006-01-02 15:04:05", strings.TrimSpace(v), beijing)
if err != nil {
return time.Time{}
}
return t
}
if reset := at(u.CycleResetTime); !reset.IsZero() {
w.ResetsAt = &reset
if start := at(u.CycleStartTime); !start.IsZero() && reset.After(start) {
w.Span = reset.Sub(start)
}
}
return w, true
}
type wbResourceSummary struct {
Packages []struct {
PackageCode string `json:"PackageCode"`
@@ -0,0 +1,93 @@
package provider
import (
"context"
"net/http"
"net/http/httptest"
"testing"
"time"
)
// #1904 (CnsMaple): an enterprise account's resource summary has no
// packages, and its credits are the enterprise meter's, which WorkBuddy's
// own usage page reads. The two answers are the reporter's, byte for byte,
// from one token at one moment.
const wbEntSummary = `{
"code": 0,
"msg": "OK",
"data": {
"Packages": [],
"SubscriptionPackageCode": "",
"SubscriptionPackageName": "",
"IsPaidUser": false,
"IsProtectedPriceUser": false
}
}`
const wbEntUsage = `{
"code": 0,
"msg": "OK",
"data": {
"credit": 196.13,
"cycleStartTime": "2026-10-08 00:00:00",
"cycleEndTime": "2026-11-08 23:59:59",
"limitNum": 2000,
"cycleResetTime": "2026-11-09 00:00:00"
}
}`
// and a personal account's answer to the enterprise meter, from a real
// WorkBuddy AI Pro account on 2026-10-13
const wbPersonalEnt = `{"code":10001,"msg":"uid or enterpriseID is empty","requestId":"36dd0f64-cb93-41e1-a964-9e3c87fea235"}`
func wbEntServer(t *testing.T, ent func(w http.ResponseWriter)) *[]string {
t.Helper()
var paths []string
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
paths = append(paths, r.URL.Path)
switch r.URL.Path {
case "/billing/meter/get-user-resource-summary":
w.Write([]byte(wbEntSummary))
case "/billing/meter/get-enterprise-user-usage":
ent(w)
default:
w.WriteHeader(404)
}
}))
t.Cleanup(srv.Close)
old := wbEndpoint
wbEndpoint = srv.URL
t.Cleanup(func() { wbEndpoint = old })
return &paths
}
func wbEntAccount(uid string) wbAccount {
return wbAccount{Login: Login{User: "ent"}, site: wbCN, creds: wbCreds{Access: "ent-access", UID: uid, ExpiresAt: time.Now().Add(24 * time.Hour).UnixMilli()}}
}
func TestWorkBuddyEnterpriseCredits(t *testing.T) {
wbEntServer(t, func(w http.ResponseWriter) { w.Write([]byte(wbEntUsage)) })
q := wbQuota(context.Background(), wbEntAccount("ent-u1"))
if q.Error != "" || q.Plan != "Enterprise" || len(q.Windows) != 1 {
t.Fatalf("an enterprise account's card: %+v", q)
}
w := q.Windows[0]
reset := time.Date(2026, 11, 8, 16, 0, 0, 0, time.UTC)
if w.Name != "Credits" || w.Display != "196.13 / 2000" || w.Amount != 196.13 || w.Limit != 2000 || w.Unit != "credits" ||
w.Used != 100*196.13/2000 || w.ResetsAt == nil || !w.ResetsAt.Equal(reset) || w.Span != 32*24*time.Hour {
t.Fatalf("window: %+v (resets %v)", w, w.ResetsAt)
}
}
// A personal account with no credits is refused the enterprise meter: its
// card stays as the summary said, with no error.
func TestWorkBuddyPersonalNotEnterprise(t *testing.T) {
paths := wbEntServer(t, func(w http.ResponseWriter) {
w.WriteHeader(400)
w.Write([]byte(wbPersonalEnt))
})
q := wbQuota(context.Background(), wbEntAccount("ent-u2"))
if q.Error != "" || q.Plan != "Free" || len(q.Windows) != 0 || len(*paths) != 2 {
t.Fatalf("a personal account's card: %+v, asked %v", q, *paths)
}
}