Files
magpie/internal/provider/migrate_workbuddy.go
T
yetone 37809ac722 usage: WorkBuddy's credits are said beside their share — on the Routing page, the Usage page and the menu bar, used or left as the share is (#659)
Moody-Sin: the Routing page showed a WorkBuddy account as "已用 71%" only, which for a plan counted in credits says little. A quota window now carries its own count when the vendor counts in amounts — amount of limit, in unit (QuotaWindow, QuotaSpan for quota --json and /v1/magpie/quotas, and the routing trace's Weighed, from the window its share is of: Allowance.Count). WorkBuddy's built-in fills it from the resource summary, and @magpie-community/opencode-workbuddy-auth 0.1.8 tells it from auth.usage (amount / limit / unit, passed through host.js); the workbuddy mover's min is 0.1.8. The Routing page's stage and its list of what each account did, the Usage page's meter, the menu bar's tooltip and the TUI say "3,550 / 5,000 credits · 71% used", or "1,450 / 5,000 credits · 29% left" with Left on (积分 in Chinese); a window counted in shares alone reads as before.

quota-credits.test.cjs (WebKit, en/zh, used/left, Routing and Usage pages) fails on the old assets and passes; TestQuotaWindowCount, TestAllowanceCount, TestTraceTellsTheCount, TestPluginUsage (the count through the host) and TestWorkBuddyAccounts cover the Go side. Tried with the real WorkBuddy and WorkBuddy AI accounts in a sandbox magpie: moved onto the local plugin, /api/usage/quotas gives 33.29 / 3900 and 13.47 / 2250 credits and the Usage page reads "33.29 / 3,900 积分 · 已用 0.9%", "3,866.71 / 3,900 积分 · 剩余 99.1%" after a click on the count; not moved, the built-in gives the same for WorkBuddy. The Routing page with two real accounts of one provider wasn't tried (one account each). go vet, builds (darwin, GOOS=linux, GOOS=windows) and go test -tags nogui -count=1 ./... under a temp HOME pass.
2026-10-03 16:45:48 +08:00

84 lines
2.8 KiB
Go

package provider
import (
"encoding/json"
"errors"
"time"
)
// WorkBuddy's and WorkBuddy AI's accounts on
// @magpie-community/opencode-workbuddy-auth (one package, a provider for
// each build). The desktop app's own sign-in goes as a marker ({source:
// "desktop"}, the package from 0.1.1 on): the plugin reads the app's file
// on each request, as the built-in does, so no refresh token is held by
// both. An account magpie signed in goes whole.
func init() {
for _, w := range []*wbSite{wbCN, wbAI} {
movers[w.id] = &mover{
pkg: "@magpie-community/opencode-workbuddy-auth",
min: "0.1.8", // 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
for _, a := range wbLogins(w) {
m := Moving{User: a.User, First: a.Active, On: a.On, Lapsed: a.Lapsed != "", Own: a.own, Plan: a.Plan}
if a.own {
m.Auth = map[string]any{"type": "oauth", "source": "desktop", "access": "", "refresh": "", "expires": 0,
"accountId": a.User, "uid": a.creds.UID}
} else {
c := a.creds
m.Auth = map[string]any{"type": "oauth", "access": c.Access, "refresh": c.Refresh, "expires": c.ExpiresAt,
"accountId": a.User, "uid": c.UID, "domain": c.Domain}
if c.RefreshExpiresAt > 0 {
m.Auth["refreshExpiresAt"] = c.RefreshExpiresAt
}
if c.TokenType != "" {
m.Auth["tokenType"] = c.TokenType
}
}
out = append(out, m)
}
return out, nil
},
back: func(ls []savedLogin, user string, auth map[string]any) ([]savedLogin, string, error) {
if str(auth["source"]) != "" {
// the desktop app's: nothing of it was the plugin's
return ls, firstNonEmpty(user, str(auth["accountId"])), nil
}
c := wbCreds{UID: str(auth["uid"]), Access: str(auth["access"]), Refresh: str(auth["refresh"]),
ExpiresAt: msOf(auth["expires"]), RefreshExpiresAt: msOf(auth["refreshExpiresAt"]),
Domain: str(auth["domain"]), TokenType: str(auth["tokenType"])}
if c.UID == "" || c.Access == "" {
return ls, "", errors.New(w.name + ": an unreadable plugin sign-in")
}
if user == "" {
user = str(auth["accountId"])
}
i := backInto(&ls, w.id, user)
b, err := json.Marshal(c)
if err != nil {
return ls, "", err
}
ls[i].Auth, ls[i].Lapsed, ls[i].Seen = b, "", time.Now().UTC().Truncate(time.Second)
wbTokens.Lock()
delete(wbTokens.m, w.id+"|"+c.UID)
wbTokens.Unlock()
return ls, ls[i].User, nil
},
}
}
}
// msOf is a time in unix ms as JSON gave it.
func msOf(v any) int64 {
switch n := v.(type) {
case float64:
return int64(n)
case int64:
return n
case int:
return int64(n)
}
return 0
}