Files
magpie/internal/provider/minimax_checkin.go
T
yetone 87487c5ef0 plugin, provider: a plugin can press its vendor's daily check-in itself (auth.checkin), and magpie runs it as its own check-ins (Lemon on Discord #feedback: provider 每日签到的方法能放到插件里面去做吗; ARNO earlier asked for Qoder's) — the check-ins magpie had (WorkBuddy, Trae CN, MiniMax Code, Qoder) are each written in magpie against the vendor's endpoint, even for a subscription that moved to its plugin, so a plugin author had no way to give their provider one. A plugin's auth hook may now have checkin(getAuth, provider) beside usage: the host lists the provider with checkin: true (plugin.Provider.Checkin) and answers checkin({provider, account}) (plugin.AccountCheckin), refreshing the account as usage does, with {outcome, credit, streak, message}; outcome is claimed, done, ineligible, inactive, captcha or failed, and a throw or an outcome outside these is failed with the reason. internal/provider/plugin_checkin.go runs these through checkin.go's checkiner, as the built-ins are: each signed-in account in use once a Beijing day (first look 2 minutes after start, then every 30), a failure asked again after wbCheckinRetry, every other outcome settled for the day, results kept in plugin-checkin.json by provider|account; a failure is recorded as failed and resets nothing. captcha is new (CheckinCaptcha): the vendor wants a captcha, which magpie never solves, so it is said as "check in in its own app", not as a failure, and not asked again that day. The results mark the account's Usage card (checkinBy "plugin:<provider>", WithCheckins), so the card's check-in row, its Auto check-in and Check in now work for it (POST /api/settings/plugin-checkin {provider, on}, POST /api/usage/plugin-checkin {provider}); Settings → Usage gains a Plugins tab, shown while such a plugin is signed in, a Daily check-in row per provider with Off/On and how each account went; magpie accounts checkin and c on the TUI's Usage page include them (the TUI's "no account" line names check-in plugins). The switch (settings.PluginCheckins, by provider) is off by default; for WorkBuddy, Trae CN, MiniMax Code and Qoder it follows that vendor's existing switch until set on its own, and magpie's own check-in through the plugin's fetch leaves a provider whose plugin checks in to it (pluginChecksIn), so no account is checked in twice. The Settings page's save carries PluginCheckins over (TestSettingsSaveKeepsWhatItDoesNotSend's fixture names it). No plugin declares the hook yet; Qoder's check-in stays magpie's own here. TestPluginCheckinRunsAsTheBuiltinsDo (the fake plugin with FAKE_CHECKIN, five accounts answering claimed, done, captcha, a throw and an unknown outcome: off presses nothing; on, each outcome kept with its vendor; a minute on nothing pressed again; past the retry only the two failures; the next day all five; PluginCheckins and the cards marked; another provider's press asks none) and TestPluginCheckinTakesOverTheVendors (a qoder plugin without the hook leaves magpie's Qoder check-in its account, with it none; PluginCheckinOn's fallbacks) pass -race -count=10, and the latter fails with pluginChecksIn stubbed false; TestTUIChecksPluginsIn passes -race -count=20. plugin-checkin.test.cjs (the card's row with the captcha said, Auto check-in posting {provider, on}, no page move; the Settings Plugins tab at 440px with the row fitting, no sideways scroll, On posting) passes on Chromium and WebKit in en, zh, ja and de; usage-checkin, i18n-keys, gui-ja, gui-de and settings-* pass. go test -tags nogui ./internal/..., go vet, gofmt -l, linux (nogui) and windows builds pass. Not tried with a real plugin or account: no published plugin has the hook yet. docs/subsystems/provider-plugins.md (the hook's contract) and docs/reference.md say it.
2026-10-06 18:40:45 +08:00

317 lines
10 KiB
Go

package provider
// MiniMax Code's daily check-in (签到, #811: a week of daily credits, more
// on days 4 and 7): MiniMax Code is served only by its community plugin,
// @magpie-community/opencode-minimax-auth (provider "minimax-code", the
// China site, and "minimax-code-global", the international one; 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 MiniMax Code's web page does: it asks the check-in's panel on the
// account's site (agent.minimax.cn, agent.minimax.io), and claims while
// today's is claimable. Both go
// through the plugin's fetch, which sends them as the account (its access
// token, renewed when near its end); the page's own signature
// (x-timestamp, x-signature) goes with them, though MiniMax doesn't check
// it now. The international site has the same check-in (ARNO on Discord):
// its page has the Daily check-in panel, and its /signin/status and /claim
// answer as the China site's do, refusing a bad signature and then asking
// for a sign-in.
//
// What came of it is kept in minimax-checkin.json by account id, as
// WorkBuddy's and Trae CN's are (checkin.go).
import (
"context"
"crypto/md5"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"path/filepath"
"sort"
"strconv"
"strings"
"sync"
"time"
"github.com/yetone/magpie/internal/plugin"
"github.com/yetone/magpie/internal/settings"
)
// MiniMaxCodeID is the MiniMax Code plugin's provider (the China site),
// MiniMaxCodeGlobalID its international site's.
const (
MiniMaxCodeID = "minimax-code"
MiniMaxCodeGlobalID = "minimax-code-global"
)
// miniMaxCheckinURLs are MiniMax Code's check-in on each site, by the
// plugin's provider: /status, then /claim, each with the day's time zone.
var miniMaxCheckinURLs = map[string]string{
MiniMaxCodeID: "https://agent.minimax.cn/minimax-cloud/api/v1/signin",
MiniMaxCodeGlobalID: "https://agent.minimax.io/minimax-cloud/api/v1/signin",
}
// miniMaxSignSecret is what MiniMax Code's page signs its requests with:
// x-signature is md5(seconds + it + the body).
const miniMaxSignSecret = "I*7Cf%WZ#S&%1RlZJ&C2"
// A day of MiniMax's check-in panel, by its status.
const (
mmDayUpcoming = 1
mmDayClaimable = 2
mmDayClaimed = 3
mmDayDisabled = 4
)
// miniMaxDay is one day of the panel: the week's day_no, its credits.
type miniMaxDay struct {
DayNo int `json:"day_no"`
Points float64 `json:"points"`
Status int `json:"status"`
IsToday bool `json:"is_today"`
}
type miniMaxBase struct {
Code int `json:"status_code"`
Msg string `json:"status_msg"`
}
type miniMaxStatus struct {
Data struct {
Days []miniMaxDay `json:"days"`
} `json:"data"`
Base *miniMaxBase `json:"base_resp"`
}
type miniMaxClaim struct {
Data struct {
ClaimResult int `json:"claim_result"` // 1 claimed now, 2 already today
DayNo int `json:"day_no"`
Points float64 `json:"points"`
} `json:"data"`
Base *miniMaxBase `json:"base_resp"`
}
// miniMaxAccount is a MiniMax Code account checked in: site is the
// plugin's provider it is signed in to ("" the China site's), via sends a
// request as it.
type miniMaxAccount struct {
User string
On bool
site string
uid string
card string
via func(*http.Request) (*http.Response, error)
}
func (a miniMaxAccount) siteID() string {
if a.site == "" {
return MiniMaxCodeID
}
return a.site
}
func miniMaxCheckinPath() string {
return filepath.Join(filepath.Dir(Path()), "minimax-checkin.json")
}
func miniMaxCheckinKey(a miniMaxAccount) string {
if a.uid == "" {
return ""
}
return a.siteID() + "|" + a.uid
}
var miniMaxCheckinMu sync.Mutex
func newMiniMaxCheckiner(accounts func() []miniMaxAccount) checkiner {
return checkiner{path: miniMaxCheckinPath(), now: time.Now, mu: &miniMaxCheckinMu, by: "minimax", label: "minimax", accounts: func() []checkinAcct {
var out []checkinAcct
for _, a := range accounts() {
out = append(out, checkinAcct{User: a.User, On: a.On, key: miniMaxCheckinKey(a), do: func(ctx context.Context) WorkBuddyCheckin { return miniMaxCheckin(ctx, a) }})
}
return out
}}
}
// miniMaxCheckin checks a in for the day as MiniMax Code's page does: the
// panel first, and a claim only while today's is claimable. The streak is
// today's day of the week's panel.
func miniMaxCheckin(ctx context.Context, a miniMaxAccount) WorkBuddyCheckin {
var st miniMaxStatus
if err := miniMaxCall(ctx, a, http.MethodGet, "/status", &st); err != nil {
return wbCheckinFailed(err)
}
if st.Base != nil && st.Base.Code != 0 {
return WorkBuddyCheckin{Outcome: CheckinFailed, Msg: fmt.Sprintf("code %d: %s", st.Base.Code, st.Base.Msg)}
}
var today *miniMaxDay
for i := range st.Data.Days {
if st.Data.Days[i].IsToday {
today = &st.Data.Days[i]
break
}
}
switch {
case today == nil:
return WorkBuddyCheckin{Outcome: CheckinInactive}
case today.Status == mmDayClaimed:
return WorkBuddyCheckin{Outcome: CheckinDone, Credit: today.Points, Streak: today.DayNo}
case today.Status == mmDayDisabled:
return WorkBuddyCheckin{Outcome: CheckinIneligible, Msg: "today's check-in is closed for this account"}
}
var got miniMaxClaim
if err := miniMaxCall(ctx, a, http.MethodPost, "/claim", &got); err != nil {
return wbCheckinFailed(err)
}
if got.Base != nil && got.Base.Code != 0 {
return WorkBuddyCheckin{Outcome: CheckinFailed, Msg: fmt.Sprintf("code %d: %s", got.Base.Code, got.Base.Msg)}
}
day, credit := got.Data.DayNo, got.Data.Points
if day == 0 {
day = today.DayNo
}
if credit == 0 {
credit = today.Points
}
switch got.Data.ClaimResult {
case 1:
return WorkBuddyCheckin{Outcome: CheckinClaimed, Credit: credit, Streak: day}
case 2:
return WorkBuddyCheckin{Outcome: CheckinDone, Credit: credit, Streak: day}
}
return WorkBuddyCheckin{Outcome: CheckinFailed, Msg: fmt.Sprintf("claim_result %d", got.Data.ClaimResult)}
}
// miniMaxCall asks the check-in's path as a, signed as MiniMax Code's page
// signs it, and reads its answer.
func miniMaxCall(ctx context.Context, a miniMaxAccount, method, path string, dst any) error {
body := ""
if method == http.MethodPost {
body = "{}"
}
var rd io.Reader
if body != "" {
rd = strings.NewReader(body)
}
req, err := http.NewRequestWithContext(ctx, method, miniMaxCheckinURLs[a.siteID()]+path+"?timezone_id=Asia%2FShanghai", rd)
if err != nil {
return err
}
ts := strconv.FormatInt(time.Now().Unix(), 10)
sum := md5.Sum([]byte(ts + miniMaxSignSecret + body))
req.Header.Set("Accept", "application/json")
req.Header.Set("Content-Type", "application/json")
req.Header.Set("x-timestamp", ts)
req.Header.Set("x-signature", hex.EncodeToString(sum[:]))
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("MiniMax Code's sign-in has expired — sign in again")
}
if err := json.Unmarshal(b, dst); err != nil || res.StatusCode < 200 || res.StatusCode >= 300 {
if err == nil {
// MiniMax's own answer, in an error status
var e struct {
Base *miniMaxBase `json:"base_resp"`
}
if json.Unmarshal(b, &e) == nil && e.Base != nil && e.Base.Code != 0 {
return fmt.Errorf("code %d: %s", e.Base.Code, e.Base.Msg)
}
}
return &accountStatusError{status: res.StatusCode}
}
return nil
}
// CheckInMiniMax checks each MiniMax Code account in use in for today now,
// those not in yet, and says how each stands.
func CheckInMiniMax(ctx context.Context) []WorkBuddyCheckin {
return newMiniMaxCheckiner(miniMaxCheckinAccounts).checkinNow(ctx, true)
}
// MiniMaxCheckins is each MiniMax Code account's last check-in, as kept,
// by its name now; asks nothing.
func MiniMaxCheckins() []WorkBuddyCheckin {
st := readCheckins(miniMaxCheckinPath())
var out []WorkBuddyCheckin
listed := map[string]bool{}
for _, a := range miniMaxCheckinAccounts() {
key := miniMaxCheckinKey(a)
if r, ok := st[key]; ok && a.On && key != "" && !listed[key] {
listed[key] = true
r.User, r.By = a.User, "minimax"
out = append(out, r)
}
}
sort.SliceStable(out, func(i, j int) bool { return out[i].User < out[j].User })
return out
}
// HasMiniMax says whether a MiniMax Code account is signed in, on either
// site.
func HasMiniMax() bool { return len(miniMaxCheckinAccounts()) > 0 }
// miniMaxCards are the usage cards of accts, for WithCheckins.
func miniMaxCards(accts []miniMaxAccount) []checkinCard {
var out []checkinCard
for _, a := range accts {
if key := miniMaxCheckinKey(a); a.On && key != "" {
out = append(out, checkinCard{User: a.User, card: a.card, key: key})
}
}
return out
}
// miniMaxCheckinAccounts are the MiniMax Code plugin's accounts on both
// sites, each sent through the plugin's fetch, which signs it in as the
// account.
func miniMaxCheckinAccounts() []miniMaxAccount {
var out []miniMaxAccount
for _, site := range []string{MiniMaxCodeID, MiniMaxCodeGlobalID} {
if pp, ok := PluginOf(site); ok && !pluginChecksIn(pp) {
out = append(out, miniMaxSiteAccounts(site, pp)...)
}
}
return out
}
func miniMaxSiteAccounts(site string, pp plugin.Provider) []miniMaxAccount {
auths := plugin.Auths(pp.ID)
card := PluginID(pp.ID)
var out []miniMaxAccount
for _, l := range pluginLogins(pp) {
key := l.acct.Key
uid := str(auths[key]["realUserID"])
if uid == "" {
uid = str(auths[key]["uid"])
}
out = append(out, miniMaxAccount{User: l.User, On: l.On, site: site, uid: 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
}
// KeepMiniMaxCheckedIn checks the MiniMax Code accounts in each day while
// settings say to, as KeepTraeCheckedIn does Trae CN's.
func KeepMiniMaxCheckedIn(ctx context.Context) {
keepCheckedIn(ctx, "minimax", newMiniMaxCheckiner(miniMaxCheckinAccounts), func() bool { return settings.Load().MiniMaxCheckin })
}