From 08168fdb0bacc5f2c01dcbb8649697db64879f5a Mon Sep 17 00:00:00 2001 From: yetone Date: Sat, 10 Oct 2026 21:43:28 +0800 Subject: [PATCH] provider: a ClinePass API key card whose limits fail to read says so beside its credits, instead of looking like no ClinePass (magpie-community/plugins#79, AkaChou) clineKeyUsage dropped every usage-limits failure once the balance was read: only a 404 means the account has no ClinePass (as AxonHub's Cline checker reads it), but a 401, 403, 429, 5xx or timeout also left the card with its credits alone and nothing said. It now returns the balance with the error "ClinePass limits couldn't be read: " (clineLimitsUnread, the words the Cline plugin uses), and KeyBalances keeps that balance on the card beside the error. A transient failure (5xx, 429, timeout) still shows the last good reading through keepLast. The other half of #79 (windows dropped when the balance fails) was already right here: the limits read are returned without the balance; the test now covers it. TestClineKeyUsage gains keys whose limits answer 503 and 403 (balance kept, error says why) and one whose balance answers 502 (three windows kept, no error), with the real {success, data: {limits: [...]}} shape and 404 for no ClinePass. Without the change it fails: "a key whose limits fail with 503: {... Balance:$12.00 ... Error: ...}". gofmt -l clean; go vet, GOOS=linux and GOOS=windows go vet -tags nogui ./internal/provider pass; go test -tags nogui ./internal/provider passes under a temp HOME. Not tried with a real Cline key (none at hand). --- internal/provider/balance.go | 3 ++- internal/provider/cline_usage.go | 11 +++++++++ internal/provider/cline_usage_test.go | 32 +++++++++++++++++++++++---- 3 files changed, 41 insertions(+), 5 deletions(-) diff --git a/internal/provider/balance.go b/internal/provider/balance.go index 4c69be7e0..1b1863db4 100644 --- a/internal/provider/balance.go +++ b/internal/provider/balance.go @@ -926,7 +926,8 @@ func KeyBalances(ctx context.Context) []SubscriptionQuota { if clineKeyCard(j.p) { // ClinePass's limits beside the credits (cline_usage.go) if ws, amount, err := clineKeyUsage(ctx, j.p); err != nil { - q.Error = err.Error() + // a balance read beside limits that weren't is kept + q.Error, q.Balance = err.Error(), amount } else { q.Windows = append(q.Windows, ws...) q.Balance = amount diff --git a/internal/provider/cline_usage.go b/internal/provider/cline_usage.go index 257670257..c5855ca77 100644 --- a/internal/provider/cline_usage.go +++ b/internal/provider/cline_usage.go @@ -80,9 +80,20 @@ func clineKeyUsage(ctx context.Context, p Provider) (windows []QuotaWindow, bala } return nil, "", limErr } + if limErr != nil { + // the credits alone, but the limits failed for a reason other + // than no ClinePass: the card says so beside the balance, rather + // than look like an account without ClinePass (#79) + return nil, balance, fmt.Errorf("%s: %w", clineLimitsUnread, limErr) + } return windows, balance, nil } +// clineLimitsUnread is how a card whose ClinePass limits failed to read +// begins its error, the words the Cline plugin uses too: the GUI says them +// (quotaError) beside the balance read. +const clineLimitsUnread = "ClinePass limits couldn't be read" + // clineCredits is the account's credit balance, {balance} in millionths of // a dollar, at /users/{id}/balance with the id /users/me tells. func clineCredits(ctx context.Context, key string) (string, error) { diff --git a/internal/provider/cline_usage_test.go b/internal/provider/cline_usage_test.go index 28ca9d852..cf5a796ce 100644 --- a/internal/provider/cline_usage_test.go +++ b/internal/provider/cline_usage_test.go @@ -32,7 +32,7 @@ func TestClineKeyUsage(t *testing.T) { srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { key := strings.TrimPrefix(r.Header.Get("Authorization"), "Bearer ") - if key != "sk-pass" && key != "sk-free" { + if key != "sk-pass" && key != "sk-free" && key != "sk-down" && key != "sk-denied" && key != "sk-nobal" { w.WriteHeader(http.StatusUnauthorized) w.Write([]byte(`{"success":false,"error":"invalid API key"}`)) return @@ -42,13 +42,24 @@ func TestClineKeyUsage(t *testing.T) { w.Write([]byte(`{"success":true,"data":{"id":"usr_` + key + `","email":"a@b.c"}}`)) case "/api/v1/users/usr_sk-pass/balance": w.Write([]byte(`{"success":true,"data":{"balance":4250000,"userId":"usr_sk-pass"}}`)) - case "/api/v1/users/usr_sk-free/balance": + case "/api/v1/users/usr_sk-free/balance", "/api/v1/users/usr_sk-down/balance", "/api/v1/users/usr_sk-denied/balance": w.Write([]byte(`{"success":true,"data":{"balance":12000000}}`)) + case "/api/v1/users/usr_sk-nobal/balance": + w.WriteHeader(http.StatusBadGateway) case "/api/v1/users/me/plan/usage-limits": - if key == "sk-free" { + switch key { + case "sk-free": w.WriteHeader(http.StatusNotFound) w.Write([]byte(`{"success":false,"error":"no active plan"}`)) return + case "sk-down": + w.WriteHeader(http.StatusServiceUnavailable) + w.Write([]byte(`{"success":false,"error":"try again later"}`)) + return + case "sk-denied": + w.WriteHeader(http.StatusForbidden) + w.Write([]byte(`{"success":false,"error":"forbidden"}`)) + return } w.Write([]byte(`{"success":true,"data":{"limits":[ {"type":"monthly","percentUsed":12.5,"resetsAt":"2026-11-01T00:00:00Z"}, @@ -64,7 +75,8 @@ func TestClineKeyUsage(t *testing.T) { t.Cleanup(func() { clineUsageAPI = old }) if err := Save(Provider{ID: "clinepass", Name: "ClinePass", Preset: "clinepass", Chat: "https://api.cline.bot/api/v1", - Key: "sk-pass", KeyName: "pass", Keys: []KeyAccount{{Name: "free", Key: "sk-free"}, {Name: "bad", Key: "sk-bad"}}}); err != nil { + Key: "sk-pass", KeyName: "pass", Keys: []KeyAccount{{Name: "free", Key: "sk-free"}, {Name: "bad", Key: "sk-bad"}, + {Name: "down", Key: "sk-down"}, {Name: "denied", Key: "sk-denied"}, {Name: "nobal", Key: "sk-nobal"}}}); err != nil { t.Fatal(err) } got := map[string]SubscriptionQuota{} @@ -88,4 +100,16 @@ func TestClineKeyUsage(t *testing.T) { if bad := got["bad"]; !strings.Contains(bad.Error, "401") || bad.Balance != "" { t.Fatalf("a key Cline refuses: %+v", bad) } + // #79: limits that fail for a reason other than no ClinePass (a 404) + // say so beside the balance, rather than look like no ClinePass + for user, code := range map[string]string{"down": "503", "denied": "403"} { + q := got[user] + if !strings.HasPrefix(q.Error, "ClinePass limits couldn't be read: ") || !strings.Contains(q.Error, code) || q.Balance != "$12.00" || len(q.Windows) != 0 { + t.Fatalf("a key whose limits fail with %s: %+v", code, q) + } + } + // a balance that fails keeps the limits read + if q := got["nobal"]; q.Error != "" || q.Balance != "" || len(q.Windows) != 3 { + t.Fatalf("a key whose balance fails: %+v", q) + } }