Files
magpie/internal/provider/codex_resets.go
T
yetone 3dc8f00f06 providers: each account of a subscription holding several can go through its own proxy — the provider's, none, or its own address (Discord, gakki: 是否可以为不同的codex账号设置不用的代理,现在是同一个供应商下使用相同的代理) — the per-provider proxy (#237) was one for all of Codex's signed-in accounts. A subscription's entry in providers.json now keeps accountProxies, by the account's name in lower case, each checked by settings.CheckProxy on save (ftp:// refused), addresses trimmed, accounts following the provider's proxy left out. Provider.ProxyChoice picks an account's own proxy, else the provider's, else the global one, so everything that already went through Provider.Via follows the account: gateway turns and fallback across accounts, usage and quota, token refresh, Codex's warm-up, reset and saved accounts' model lists, Claude's warm-up and the *_PROXY of Claude Code runs (ViaLogin / ProxyOfLogin), and Codex's own sign-in relayed as it came (ViaSignedIn). Extra accounts of Copilot, ZCode, Kiro and the like, made afresh without the provider's settings, now read them from providers.json, so they honour the provider's proxy too. A save without accountProxies keeps them; {} clears them. The provider editor, for a subscription with two or more accounts, lists each under the Proxy row: Provider's proxy | Direct | Custom, the address field's room kept so a pick moves nothing; Custom with no address is refused before anything is posted, its field focused without scrolling. en and zh together; no border stripes. Tested with internal/gateway/account_proxy_test.go in a sandbox HOME against fake proxies and a fake ChatGPT backend: three Codex accounts (own proxy, direct, following the provider) each reach the backend their own way in one turn and for usage; clearing an account's proxy sends it through the provider's, clearing the provider's through the global one, direct staying direct; ftp:// refused; TestClaudeAccountProxyEnv covers each Claude account's HTTPS_PROXY. Both fail with the per-account lookup off. account-proxy.test.cjs, Chromium and WebKit, en and zh (new), provider-proxy, click-scroll and old-webkit pass. go vet ./internal/..., go build -tags nogui (and GOOS=linux, GOOS=windows) and go test -tags nogui ./... pass. Not tried against a real proxy, real accounts or the real Wails window; Grok's per-account usage and Kiro's usage on a key still use the provider's proxy.
2026-09-30 04:52:03 +08:00

241 lines
8.0 KiB
Go

package provider
// Codex rate-limit resets: credits a ChatGPT account is granted that each
// start its current usage windows again at once. The usage reading says
// how many the account holds; the credits' own endpoint says when they run
// out, and spends one. They are shown on the account's card so they aren't
// forgotten, and spent only when the user says so.
import (
"bytes"
"context"
"crypto/rand"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"strings"
"time"
)
// ResetCredits is how many rate-limit resets an account holds and the
// soonest one of them runs out; Until is nil when none does, or when that
// couldn't be read.
type ResetCredits struct {
Count int `json:"count"`
Until *time.Time `json:"until,omitempty"`
// ByWindow: each reset is for one window, FiveHour of the five hours'
// and Weekly of the week's, and is spent on the vendor's own page (a
// GLM Coding team plan's, zcode_team.go), not from magpie
ByWindow bool `json:"byWindow,omitempty"`
FiveHour int `json:"fiveHour,omitempty"`
Weekly int `json:"weekly,omitempty"`
}
// Words are the resets counted: "2 resets", or by window, "2 five-hour
// resets · 1 weekly reset".
func (r ResetCredits) Words() string {
n := func(c int, unit string) string {
if c == 1 {
return "1 " + unit
}
return fmt.Sprintf("%d %ss", c, unit)
}
if !r.ByWindow {
return n(r.Count, "reset")
}
var out []string
if r.FiveHour > 0 {
out = append(out, n(r.FiveHour, "five-hour reset"))
}
if r.Weekly > 0 {
out = append(out, n(r.Weekly, "weekly reset"))
}
return strings.Join(out, " · ")
}
// codexResetDetailsTimeout bounds the second look, at when the resets run
// out: without it the count alone is shown.
var codexResetDetailsTimeout = 4 * time.Second
type codexResetCreditsWire struct {
AvailableCount int `json:"available_count"`
Credits []struct {
ID string `json:"id"`
ResetType string `json:"reset_type"`
Status string `json:"status"`
GrantedAt string `json:"granted_at"`
ExpiresAt *string `json:"expires_at"`
} `json:"credits"`
}
// first is the credit still to be used that runs out first, and when:
// one that never runs out comes after all that do. id is "" when none is
// left.
func (w codexResetCreditsWire) first() (id string, at *time.Time) {
for _, c := range w.Credits {
if c.Status != "available" || c.ID == "" {
continue
}
var t *time.Time
if c.ExpiresAt != nil {
if p, err := time.Parse(time.RFC3339, *c.ExpiresAt); err == nil {
t = &p
}
}
if id == "" || t != nil && (at == nil || t.Before(*at)) {
id, at = c.ID, t
}
}
return id, at
}
// soonest is when the first of the credits still to be used runs out, nil
// when none of them does.
func (w codexResetCreditsWire) soonest() *time.Time {
_, at := w.first()
return at
}
// codexResets is the resets an account holds, from the count its usage
// reading gave: nil when it holds none. When it does, the credits are
// asked when they run out, best effort — a failure leaves the count.
func codexResets(ctx context.Context, base, token, accountID string, count int) *ResetCredits {
if count <= 0 {
return nil
}
out := &ResetCredits{Count: count}
ctx, cancel := context.WithTimeout(ctx, codexResetDetailsTimeout)
defer cancel()
var w codexResetCreditsWire
if accountJSON(ctx, base+"/wham/rate-limit-reset-credits", token, map[string]string{"chatgpt-account-id": accountID}, &w) == nil {
out.Until = w.soonest()
}
return out
}
// ResetOutcome is what spending a reset did: Code is the vendor's word for
// it (reset, nothing_to_reset, no_credit, already_redeemed) and Windows
// how many windows started again.
type ResetOutcome struct {
Code string `json:"code"`
Windows int `json:"windows"`
}
// Text says an outcome in words.
func (o ResetOutcome) Text() string {
switch o.Code {
case "reset":
if o.Windows == 1 {
return "1 window started again"
}
return fmt.Sprintf("%d windows started again", o.Windows)
case "nothing_to_reset":
return "nothing to reset — no window has been used, and the reset is kept"
case "no_credit":
return "no reset left on the account"
case "already_redeemed":
return "that reset was already used"
}
return o.Code
}
// UseCodexReset spends one of the rate-limit resets of the Codex account
// user (the one Codex is signed in to when ""), starting its current
// usage windows again: the one that runs out first, so none that would
// last longer goes before it. It can't be undone: callers ask first. The
// account's usage is read afresh after.
func UseCodexReset(ctx context.Context, user string) (ResetOutcome, error) {
user, tok, accountID, err := codexUserToken(ViaLogin(ctx, "codex", user), user)
if err != nil {
return ResetOutcome{}, err
}
ctx = ViaLogin(ctx, "codex", user) // through the account's own proxy
base := strings.TrimSuffix(CodexBase, "/codex")
// which of them to spend is named, as Codex itself does: left to the
// vendor, it may be one that lasts longer
var w codexResetCreditsWire
if err := accountJSON(ctx, base+"/wham/rate-limit-reset-credits", tok, map[string]string{"chatgpt-account-id": accountID}, &w); err != nil {
return ResetOutcome{}, fmt.Errorf("couldn't read which reset runs out first, so none was used: %w", err)
}
credit, _ := w.first()
if credit == "" {
return ResetOutcome{Code: "no_credit"}, nil
}
out, err := consumeCodexReset(ctx, base, tok, accountID, credit, newRedeemID())
if err != nil {
return out, err
}
StaleAllowance("codex", user)
if out.Code == "reset" {
renewedNow("codex", user)
}
subscriptionUsageCache.Lock()
subscriptionUsageCache.at = time.Time{}
subscriptionUsageCache.data = nil
subscriptionUsageCache.Unlock()
return out, nil
}
// codexUserToken signs in as the Codex account user: the one Codex itself
// is signed in to, or one magpie keeps — and says which it was.
func codexUserToken(ctx context.Context, user string) (who, tok, accountID string, err error) {
// the one signed in now, read from Codex's own file: the list of those
// kept may not have caught up with it yet
if live, ok := liveLogin("codex"); user == "" || ok && strings.EqualFold(live.User, user) {
tok, accountID, err = codexToken(ctx, codexAuthPath())
return live.User, tok, accountID, err
}
for _, l := range Logins("codex") {
if strings.EqualFold(l.User, user) {
tok, accountID, err = savedLoginToken(ctx, "codex", l.User)
return l.User, tok, accountID, err
}
}
return "", "", "", fmt.Errorf("no Codex account %q", user)
}
// consumeCodexReset spends the reset credit; id makes a retry of the same
// request spend it once.
func consumeCodexReset(ctx context.Context, base, token, accountID, credit, id string) (ResetOutcome, error) {
var out ResetOutcome
body, _ := json.Marshal(map[string]string{"credit_id": credit, "redeem_request_id": id})
req, err := http.NewRequestWithContext(ctx, http.MethodPost, base+"/wham/rate-limit-reset-credits/consume", bytes.NewReader(body))
if err != nil {
return out, err
}
req.Header.Set("Authorization", "Bearer "+token)
req.Header.Set("Accept", "application/json")
req.Header.Set("Content-Type", "application/json")
if accountID != "" {
req.Header.Set("chatgpt-account-id", accountID)
}
res, err := http.DefaultClient.Do(req)
if err != nil {
return out, err
}
defer res.Body.Close()
b, _ := io.ReadAll(io.LimitReader(res.Body, 1<<20))
if res.StatusCode < 200 || res.StatusCode >= 300 {
return out, fmt.Errorf("Codex reset: %s", http.StatusText(res.StatusCode))
}
var wire struct {
Code string `json:"code"`
WindowsReset int `json:"windows_reset"`
}
if err := json.Unmarshal(b, &wire); err != nil || wire.Code == "" {
return out, errors.New("Codex reset: an answer that says nothing")
}
return ResetOutcome{Code: wire.Code, Windows: wire.WindowsReset}, nil
}
// newRedeemID is a random UUID (v4), the consume call's idempotency key.
func newRedeemID() string {
var b [16]byte
rand.Read(b[:])
b[6] = b[6]&0x0f | 0x40
b[8] = b[8]&0x3f | 0x80
return fmt.Sprintf("%x-%x-%x-%x-%x", b[0:4], b[4:6], b[6:8], b[8:10], b[10:])
}