Skip to content

Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.

9.6 KB
Raw Blame
package provider

// Trae CN's daily check-in (每日签到, #694: 100 credits a day): Trae CN is
// served only by its community plugin, @magpie-community/opencode-trae-auth
// (provider "trae-cn"; no built-in, no mover). While the setting is on,
// whichever magpie runs the gateway presses it for each of the plugin's
// accounts once a Beijing day, as Trae's IDE does: it asks the check-in's
// status on api.trae.cn, and claims when it is on and today's isn't in.
// Both go through the plugin's fetch (0.1.4 and later), which sends them
// as the account (its Cloud-IDE-JWT, renewed when near its end, and its
// device id).
//
// What came of it is kept in trae-checkin.json by account id, as
// WorkBuddy's is (checkin.go): an answer holds for the day, a request that
// got none is tried again later. Trae counts a check-in by device as well
// as by account (9095: this device is in for today); that is an answer
// for the day, and magpie doesn't send another device id to get round it.
// Nor does a claim answered 9074 (当前参与用户太多,请稍后再试) get another
// try that day: no retry ever got past it (#808, ~5 days), and claim
// after claim may read as abuse. Why Trae refuses isn't known for certain.
// It isn't that Trae pays only its own app: Buddy Switch checks in
// accounts that aren't its (#1920). What #808 found is that on one
// account Trae CN's own client's claim went through while the plugin's,
// on a device id the plugin had made up, didn't. So since plugin 0.2.14
// the account this machine's Trae CN client is signed in as checks in on
// the client's own device id (read, never written); every other account
// keeps its own, and magpie never makes up or rotates one (#1705). An
// account refused 9074 is told that Trae CN refused it, and to check in
// in the Trae CN app.

import (
	"cmp"
	"context"
	"encoding/json"
	"errors"
	"fmt"
	"io"
	"net/http"
	"path/filepath"
	"sort"
	"strings"
	"sync"
	"time"

	"github.com/yetone/magpie/internal/plugin"
	"github.com/yetone/magpie/internal/settings"
)

// TraeRefusedWords is what an account Trae CN refused a claim 9074 is
// told, in the TUI and CLI (the GUI says it in each language): what Trae
// did, and where the check-in can still be done.
const TraeRefusedWords = "Trae CN refused this check-in (9074: too many taking part); check in in the Trae CN app"

// TraeCNID is the Trae CN plugin's provider.
const TraeCNID = "trae-cn"

// traeCheckinURL is Trae CN's check-in page: /status, then /claim.
var traeCheckinURL = "https://api.trae.cn/trae/api/v2/ug/checkin_credits"

// Trae CN's check-in answers, the codes it refuses with.
const (
	traeSignedOut     = 1001 // the token isn't taken
	traeOwnAppOnly    = 9074 // a claim refused (当前参与用户太多); final for the day
	traeDeviceChecked = 9095 // this device is in for today
)

// traeCheckinStatus is the check-in as an account sees it.
type traeCheckinStatus struct {
	Code         int     `json:"code"`
	Message      string  `json:"message"`
	Enable       bool    `json:"enable"`
	CheckedIn    bool    `json:"checked_in"`
	Credits      float64 `json:"credits"`
	ExtraCredits float64 `json:"extra_credits"`
}

// traeAccount is a Trae CN account checked in: via sends a request as it.
type traeAccount struct {
	User string
	On   bool
	uid  string
	card string
	via  func(*http.Request) (*http.Response, error)
}

func traeCheckinPath() string { return filepath.Join(filepath.Dir(Path()), "trae-checkin.json") }

func traeCheckinKey(a traeAccount) string {
	if a.uid == "" {
		return ""
	}
	return TraeCNID + "|" + a.uid
}

var traeCheckinMu sync.Mutex

func newTraeCheckiner(accounts func() []traeAccount) checkiner {
	return checkiner{path: traeCheckinPath(), now: time.Now, mu: &traeCheckinMu, by: "trae", label: "trae", accounts: func() []checkinAcct {
		var out []checkinAcct
		for _, a := range accounts() {
			out = append(out, checkinAcct{User: a.User, On: a.On, key: traeCheckinKey(a), provider: cmp.Or(a.card, TraeCNID), do: func(ctx context.Context) WorkBuddyCheckin { return traeCheckin(ctx, a) }})
		}
		return out
	}}
}

// traeCheckin checks a in for the day as Trae's IDE does: the status
// first, and a claim only while it is on and today's isn't in.
func traeCheckin(ctx context.Context, a traeAccount) WorkBuddyCheckin {
	var st traeCheckinStatus
	if err := traeCall(ctx, a, "/status", &st); err != nil {
		return wbCheckinFailed(err)
	}
	if st.Code != 0 {
		return traeRefused(st.Code, st.Message)
	}
	switch {
	case !st.Enable:
		return WorkBuddyCheckin{Outcome: CheckinInactive}
	case st.CheckedIn:
		return WorkBuddyCheckin{Outcome: CheckinDone, Credit: st.Credits}
	}
	var got traeCheckinStatus
	if err := traeCall(ctx, a, "/claim", &got); err != nil {
		return wbCheckinFailed(err)
	}
	if got.Code == traeOwnAppOnly {
		// status said on and not in, so the claim itself is refused, and
		// asking again that day never changed it (#808); the account is
		// told to check in in Trae CN's app
		return WorkBuddyCheckin{Outcome: CheckinOwnApp, Msg: orStr(got.Message, "Trae CN refused the check-in (9074)")}
	}
	if got.Code != 0 {
		return traeRefused(got.Code, got.Message)
	}
	credit := got.Credits
	if credit == 0 {
		credit = st.Credits
	}
	return WorkBuddyCheckin{Outcome: CheckinClaimed, Credit: credit + got.ExtraCredits}
}

// traeRefused is what a code Trae answered with comes to: an answer for
// the day, or a failure to try again.
func traeRefused(code int, msg string) WorkBuddyCheckin {
	switch code {
	case traeDeviceChecked:
		return WorkBuddyCheckin{Outcome: CheckinIneligible, Msg: orStr(msg, "this device has checked in today")}
	case traeSignedOut:
		return WorkBuddyCheckin{Outcome: CheckinFailed, Msg: "Trae CN's sign-in has expired — sign in again"}
	case traeOwnAppOnly:
		// on /status, before any claim: tried again later as before
		return WorkBuddyCheckin{Outcome: CheckinFailed, Msg: orStr(msg, "asked too often")}
	}
	return WorkBuddyCheckin{Outcome: CheckinFailed, Msg: fmt.Sprintf("code %d: %s", code, msg)}
}

func orStr(s, or string) string {
	if strings.TrimSpace(s) == "" {
		return or
	}
	return s
}

// traeCall posts {} to the check-in's path as a and reads its answer.
func traeCall(ctx context.Context, a traeAccount, path string, dst *traeCheckinStatus) error {
	req, err := http.NewRequestWithContext(ctx, http.MethodPost, traeCheckinURL+path, strings.NewReader("{}"))
	if err != nil {
		return err
	}
	req.Header.Set("Content-Type", "application/json")
	req.Header.Set("Accept", "application/json")
	res, err := a.via(req)
	if err != nil {
		return err
	}
	defer res.Body.Close()
	b, _ := io.ReadAll(io.LimitReader(res.Body, 1<<20))
	if strings.EqualFold(res.Header.Get("X-Magpie-Sign-In"), "expired") || res.StatusCode == http.StatusUnauthorized {
		return errors.New("Trae CN's sign-in has expired — sign in again")
	}
	if err := json.Unmarshal(b, dst); err != nil || res.StatusCode < 200 || res.StatusCode >= 300 {
		if res.StatusCode == http.StatusBadRequest && err != nil {
			// a plugin before 0.1.4 serves chat completions only
			return errors.New("the Trae CN plugin can't check in: update it to 0.1.4 or later")
		}
		if err == nil && dst.Code != 0 {
			return nil // Trae's own answer, in an error status
		}
		return &accountStatusError{status: res.StatusCode}
	}
	return nil
}

// CheckInTrae checks each Trae CN account in use in for today now, those
// not in yet, and says how each stands.
func CheckInTrae(ctx context.Context) []WorkBuddyCheckin {
	return newTraeCheckiner(traeCheckinAccounts).checkinNow(ctx, true)
}

// TraeCheckins is each Trae CN account's last check-in, as kept, by its
// name now; asks nothing.
func TraeCheckins() []WorkBuddyCheckin {
	st := shownCheckins(traeCheckinPath())
	var out []WorkBuddyCheckin
	listed := map[string]bool{}
	for _, a := range traeCheckinAccounts() {
		key := traeCheckinKey(a)
		if r, ok := st[key]; ok && a.On && key != "" && !listed[key] {
			listed[key] = true
			r.User, r.By = a.User, "trae"
			out = append(out, r)
		}
	}
	sort.SliceStable(out, func(i, j int) bool { return out[i].User < out[j].User })
	return out
}

// HasTrae says whether a Trae CN account is signed in.
func HasTrae() bool { return len(traeCheckinAccounts()) > 0 }

// traeCards are the usage cards of accts, for WithCheckins.
func traeCards(accts []traeAccount) []checkinCard {
	var out []checkinCard
	for _, a := range accts {
		if key := traeCheckinKey(a); a.On && key != "" {
			out = append(out, checkinCard{User: a.User, card: a.card, key: key})
		}
	}
	return out
}

// traeCheckinAccounts are the Trae CN plugin's accounts, each sent through
// the plugin's fetch, which signs it as the account.
func traeCheckinAccounts() []traeAccount {
	pp, ok := PluginOf(TraeCNID)
	if !ok || pluginChecksIn(pp) {
		return nil
	}
	auths := plugin.Auths(pp.ID)
	card := PluginID(pp.ID)
	var out []traeAccount
	for _, l := range pluginLogins(pp) {
		key := l.acct.Key
		out = append(out, traeAccount{User: l.User, On: l.On, uid: str(auths[key]["uid"]), card: card,
			via: func(req *http.Request) (*http.Response, error) {
				h := map[string]string{}
				for k, vs := range req.Header {
					h[strings.ToLower(k)] = vs[0]
				}
				var body []byte
				if req.Body != nil {
					body, _ = io.ReadAll(req.Body)
				}
				return plugin.Fetch(req.Context(), plugin.FetchRequest{Provider: pp.ID, Account: key, URL: req.URL.String(), Method: req.Method, Headers: h, Body: body})
			}})
	}
	return out
}

// KeepTraeCheckedIn checks the Trae CN accounts in each day while settings
// say to, as KeepWorkBuddyCheckedIn does WorkBuddy's.
func KeepTraeCheckedIn(ctx context.Context) {
	keepCheckedIn(ctx, "trae", newTraeCheckiner(traeCheckinAccounts), func() bool { return settings.Load().TraeCheckin })
}