diff --git a/internal/provider/migrate_workbuddy.go b/internal/provider/migrate_workbuddy.go index 5e5347e1b..a604f307d 100644 --- a/internal/provider/migrate_workbuddy.go +++ b/internal/provider/migrate_workbuddy.go @@ -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 diff --git a/internal/provider/workbuddy.go b/internal/provider/workbuddy.go index a89295608..cb1fa042d 100644 --- a/internal/provider/workbuddy.go +++ b/internal/provider/workbuddy.go @@ -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"` diff --git a/internal/provider/workbuddy_enterprise_test.go b/internal/provider/workbuddy_enterprise_test.go new file mode 100644 index 000000000..675dfc08a --- /dev/null +++ b/internal/provider/workbuddy_enterprise_test.go @@ -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) + } +}