Files
magpie/internal/gateway/zcode_trial.go
T
yetone 394a92f0f3 gateway: a ZCode trial entry zcode.z.ai's firewall blocks rests alone
A -Trial entry goes to zcode.z.ai's Start Plan only. On an account with
a GLM Coding Plan too, the plain models go to api.z.ai, which kept
answering while zcode.z.ai served its block page (jinpy666,
plugins#545). The block page on a Trial rested the whole account, so
the coding plan's models stopped too. zcodeTrialBlocked now rests such
a refusal on the account/model, like the Trial refusals of #3661.

Unchanged: the block page on a plain model still rests the account (on
a gift-only account, windy_66454's, the plain model is the same gift on
the same host), 3012 risk control rests the account as restricted, and
another vendor's block page rests its account.

Run (mote, at this tree):
- TestZCodeTrialRefusalRestsTheTrialAlone's new "trial edge block" case:
  red without the routing.go line (rested "zcode@api key", want
  "zcode@api key/GLM-5.3-Flash-Trial"), green now; "plain edge block",
  "trial risk control" and "another vendor's block" rest the account.
- go test ./internal/gateway -run 'ZCode|Edge|Relay|Restricted|WorkBuddy|Blocked|Refusal': ok.
- gofmt -l clean; go vet darwin/linux/windows ok.
Not tried: a real dual account while zcode.z.ai blocks.
2026-10-31 11:12:56 +08:00

68 lines
3.1 KiB
Go

package gateway
import (
"net/http"
"regexp"
"strings"
"github.com/yetone/magpie/internal/provider"
)
// zcodeEndedWords and zcodeSpentWords are the ZCode plugin's own answers
// for a Start Plan trial entry (<model>-Trial) it can't serve, given before
// anything is sent: a 400 "ZCode has no active limited quota for
// GLM-5.3-Trial" when no live plan holds the model any more (the trial
// ended), and a 429
// "ZCode's limited quota for glm-5.3 is exhausted or temporarily
// unavailable" when its bucket is spent. The plugin's words, after any
// "<provider>: " put in front of them.
var (
zcodeEndedWords = regexp.MustCompile(`(?:^|: )ZCode has no active limited quota for (\S+)$`)
zcodeSpentWords = regexp.MustCompile(`(?:^|: )ZCode's limited quota for (\S+) is exhausted or temporarily unavailable$`)
)
// zcodeTrialRefused says c, a ZCode plugin account asked for one of its
// Start Plan trial entries, was refused for that entry's own quota
// (zcodeEndedWords, zcodeSpentWords), naming the model asked for. The
// trial is that model's: the account's GLM Coding Plan still serves its
// own models, so only the trial entry rests (#3661, orion-nebula-dev: two
// expired trials rested "zcode@zcode", and GLM-5.3 and GLM-5.3-Flash with
// it). Any other
// refusal of a ZCode account, an account-wide 429 or 1113 among them,
// rests the account as before.
func zcodeTrialRefused(c candidate, status int, body []byte) bool {
if c.p.PluginProvider() != "zcode" || !strings.HasSuffix(strings.ToLower(c.model), "-trial") {
return false
}
said := strings.TrimSpace(vendorMessage(body))
switch status {
case http.StatusBadRequest:
m := zcodeEndedWords.FindStringSubmatch(said)
return m != nil && strings.EqualFold(m[1], c.model)
case http.StatusTooManyRequests:
// the plugin names the bucket's own model, the entry less -Trial
m := zcodeSpentWords.FindStringSubmatch(said)
return m != nil && strings.EqualFold(m[1]+"-Trial", c.model)
}
return false
}
// zcodeTrialBlocked says c, a ZCode plugin account asked for one of its
// Start Plan trial entries, got zcode.z.ai's edge firewall's block page
// (Alibaba Cloud's 405, provider.EdgeBlocked, as the plugin's startBlocked
// words it). A trial entry is sent to zcode.z.ai alone; a dual account's
// plain models go to its GLM Coding Plan at api.z.ai, which answered while
// zcode.z.ai blocked the address (plugins#545, jinpy666). So the block
// rests the trial entry, and the account's plain model is still asked: on
// a gift-only account it goes to zcode.z.ai too, is blocked the same, and
// that rests the account (windy_66454 on Discord, 10-31: glm-trial's two
// members, GLM-5.3-Flash and GLM-5.3-Flash-Trial, shared one account rest
// and the plain one was never asked). ZCode's risk control blocking the
// account (code 3012, failRestricted) is the account's, and is read before.
func zcodeTrialBlocked(c candidate, status int, body []byte) bool {
if status < 400 || c.p.PluginProvider() != "zcode" || !strings.HasSuffix(strings.ToLower(c.model), "-trial") {
return false
}
return provider.EdgeBlocked(wordsOf(body)) && !restrictedSaid(status, wordsOf(body))
}