WorkBuddy answers an account it restricted for unusual activity with
403 {"code":11140,"msg":"request illegal","displayMsg":{"en":"Your
account has been restricted due to unusual activity",...}}. failure()
read it as an unknown failure, so the account rested 1-10 minutes and
was asked again, refused, all day.
What changed:
- provider: WBRestricted knows the refusal by code 11140 (top level,
data or error; number or string; whole, so 111400 isn't it) or by its
displayMsg words. wbExplain ends the built-in's error with
WBRestrictedHint.
- gateway: a new failRestricted ("restricted") rests restrictedRest, 2h:
one refused request per account per 2h, and a lifted restriction (or a
misread refusal) comes back the same afternoon; Unrest lifts it at
once. It is read from the hint in the error (restrictedSaid; both the
built-in and the plugin translate the vendor's body before failure()
sees it, so the raw JSON never reaches it in a chat try), or from the
raw body only for a WorkBuddy candidate (restrictedRefused,
isWorkBuddy), since another vendor's 11140 is its own. Bare "request
illegal" is not matched. 11128 stays promptRefused and rests nobody.
The stale-allowance shortcut skips it, as it does model refusals.
drawsWorkBuddy now uses isWorkBuddy.
- plugin: @magpie-community/opencode-workbuddy-auth 0.1.17 (plugins
caf3908) ends the error with the same hint; the mover's min is 0.1.17
so keepMovedCurrent brings moved users to it.
- gui: Routing labels the try "account restricted by the vendor", says it
rests because only the vendor lifting it brings it back and to appeal,
shows the hint translated, and the account's row has "The restriction
is lifted — try it again" (POST /api/gateway/unrest). en, zh, zh-TW, ja,
de.
- docs/subsystems/gateway-routing.md says the above.
Run: gofmt -l (clean); go vet darwin, GOOS=linux -tags nogui, GOOS=windows
on gateway, provider, gui; go test ./internal/gateway ./internal/provider
./internal/gui (ok). New Go tests use the reporter's literal body through
the vendor's shape, the built-in's and the plugin's, a string code and the
words alone; another vendor's 11140, a non-WorkBuddy account with the same
body, 11128/14018/111400 not restricted; an e2e through the fake plugin
host where the restricted account is skipped by the next request.
Broken by hand, each went red: rest case removed, restrictedSaid removed,
the match unscoped, the raw match removed, the provider hint removed.
GUI: routing-wb-restricted (chromium and webkit x en/zh/zh-TW/ja/de x
1100/420px, 20 pass), routing-wb-refused, i18n-keys; after the rebase
every GUI test of the Routing page (107 files, both engines): 103 green.
The 4 red ones fail the same in WebKit on origin/main 689edbd6b run
alone and are filed: group-member-info (#1745), routing-sessions
(#1307, #1672), routing-steady (#1804, passed on this head on a rerun),
routing-group-fold's group-of-eight (#1672).
Rebased onto 2c30e5cee (with #1672's test fixes) and run again: vet x3,
go test ./internal/gateway ./internal/provider ok. On the final base
5756651e3 (only GUI assets and READMEs changed since): routing-wb-restricted
20/20, routing-wb-refused, i18n-keys, routing-held-dot, mono-icon-redraw,
routing-sessions 38/38, routing-group-fold 40/40; routing-steady's WebKit
case red again, the flake filed as #1804.
Not tried against a real restricted account;
plugin users on <0.1.17 rest by minutes until their plugin updates.
287 lines
9.0 KiB
Go
287 lines
9.0 KiB
Go
package gateway
|
|
|
|
import (
|
|
"cmp"
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"slices"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/catalog"
|
|
"github.com/yetone/magpie/internal/netproxy"
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
// A WorkBuddy plan draws with the image models its product config lists:
|
|
// GET <site>/v3/config, signed as a chat is, names them among its models by
|
|
// tag ("text-to-image", "image-to-image"); which ones goes by the account,
|
|
// its build and the User-Agent its sign-in sends (the China build lists
|
|
// hunyuan-image-v3.0-art to the desktop app, hunyuan-image-alpha and its
|
|
// -edit to the CLI; WorkBuddy AI gpt-image-2.5-sunburst). They are asked as
|
|
// WorkBuddy's CLI asks them, at <site>/v2/images/generations and
|
|
// /v2/images/edits, in JSON, an edit's images as data URLs; the answer is
|
|
// OpenAI's wrapped in WorkBuddy's {code, msg, data}. Hunyuan answers with
|
|
// an image's URL, GPT Image with its bytes when asked for b64_json. Each
|
|
// image costs the plan's credits, so AutoDrawer never picks one.
|
|
//
|
|
// It is the same for the built-in and for a WorkBuddy moved onto its
|
|
// plugin (@magpie-community/opencode-workbuddy-auth): magpie shapes the
|
|
// request, the plugin's fetch signs and sends it.
|
|
|
|
// wbImagesFor is how long a plan's image models are kept before its config
|
|
// is asked again; wbImagesRetry how long a failed ask waits to be retried.
|
|
const (
|
|
wbImagesFor = time.Hour
|
|
wbImagesRetry = 5 * time.Minute
|
|
)
|
|
|
|
// drawsWorkBuddy is whether p is a WorkBuddy plan, the China build's or
|
|
// WorkBuddy AI's: the built-in's, or the WorkBuddy plugin's.
|
|
func drawsWorkBuddy(p provider.Provider) bool {
|
|
return isWorkBuddy(p) && p.Base(provider.Chat) != ""
|
|
}
|
|
|
|
// wbImageModel is an image model a WorkBuddy config lists.
|
|
type wbImageModel struct {
|
|
ID string `json:"id"`
|
|
Name string `json:"name"`
|
|
Tags []string `json:"tags"`
|
|
}
|
|
|
|
func (m wbImageModel) tagged(tag string) bool {
|
|
return slices.ContainsFunc(m.Tags, func(t string) bool { return strings.EqualFold(strings.TrimSpace(t), tag) })
|
|
}
|
|
|
|
// wbEditor is the model an edit of model's images is asked of: model when
|
|
// it takes images, else the one listed for that beside it (WorkBuddy's CLI
|
|
// pairs hunyuan-image-alpha with hunyuan-image-alpha-edit). "" for none.
|
|
func wbEditor(ms []wbImageModel, model string) string {
|
|
for _, m := range ms {
|
|
if m.ID == model && m.tagged("image-to-image") {
|
|
return m.ID
|
|
}
|
|
}
|
|
for _, m := range ms {
|
|
if m.ID == model+"-edit" && m.tagged("image-to-image") {
|
|
return m.ID
|
|
}
|
|
}
|
|
return ""
|
|
}
|
|
|
|
// wbImageList is what a plan's config last said, or why it couldn't.
|
|
type wbImageList struct {
|
|
models []wbImageModel
|
|
at time.Time
|
|
err error
|
|
asking bool
|
|
}
|
|
|
|
var wbImages = struct {
|
|
sync.Mutex
|
|
m map[string]*wbImageList
|
|
}{m: map[string]*wbImageList{}}
|
|
|
|
// wbConfigClient asks a config the gateway isn't asking for a request:
|
|
// through the account's proxy, as the gateway's own client does.
|
|
var wbConfigClient = &http.Client{Timeout: 30 * time.Second, Transport: netproxy.Dispatch(&http.Transport{
|
|
Proxy: netproxy.Func,
|
|
ForceAttemptHTTP2: true,
|
|
})}
|
|
|
|
func wbImagesKey(p provider.Provider) string { return p.ID + "\x00" + accountOf(p) }
|
|
|
|
// wbImageModelsOf is the image models p's config lists, as last asked
|
|
// within wbImagesFor. With wait it asks the config when that is older and
|
|
// answers with what it says; without, it answers at once with what it has
|
|
// (nothing, the first time) and asks in the background.
|
|
func wbImageModelsOf(ctx context.Context, client *http.Client, p provider.Provider, wait bool) ([]wbImageModel, error) {
|
|
key := wbImagesKey(p)
|
|
wbImages.Lock()
|
|
e := wbImages.m[key]
|
|
if e != nil && !e.at.IsZero() {
|
|
ttl := wbImagesFor
|
|
if e.err != nil {
|
|
ttl = wbImagesRetry
|
|
}
|
|
if time.Since(e.at) < ttl {
|
|
ms, err := e.models, e.err
|
|
wbImages.Unlock()
|
|
if len(ms) > 0 {
|
|
err = nil
|
|
}
|
|
return ms, err
|
|
}
|
|
}
|
|
if e == nil {
|
|
e = &wbImageList{}
|
|
wbImages.m[key] = e
|
|
}
|
|
if !wait {
|
|
ms := e.models
|
|
if !e.asking {
|
|
e.asking = true
|
|
go func() {
|
|
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
|
defer cancel()
|
|
wbAskImages(ctx, client, p, key)
|
|
}()
|
|
}
|
|
wbImages.Unlock()
|
|
return ms, nil
|
|
}
|
|
wbImages.Unlock()
|
|
return wbAskImages(ctx, client, p, key)
|
|
}
|
|
|
|
// wbAskImages asks p's config for its image models and keeps the answer:
|
|
// a failure keeps the models it had.
|
|
func wbAskImages(ctx context.Context, client *http.Client, p provider.Provider, key string) ([]wbImageModel, error) {
|
|
ms, err := wbFetchImageModels(ctx, client, p)
|
|
wbImages.Lock()
|
|
defer wbImages.Unlock()
|
|
e := wbImages.m[key]
|
|
if e == nil {
|
|
e = &wbImageList{}
|
|
wbImages.m[key] = e
|
|
}
|
|
e.asking, e.at, e.err = false, time.Now(), err
|
|
if err == nil {
|
|
e.models = ms
|
|
}
|
|
if err != nil && len(e.models) > 0 {
|
|
return e.models, nil
|
|
}
|
|
return ms, err
|
|
}
|
|
|
|
// wbFetchImageModels asks <site>/v3/config, signed as p's chats are, for
|
|
// the models it tags as drawing.
|
|
func wbFetchImageModels(ctx context.Context, client *http.Client, p provider.Provider) ([]wbImageModel, error) {
|
|
origin := p.Origin(ctx)
|
|
if origin == "" {
|
|
return nil, fmt.Errorf("%s: no site to ask for its image models", p.Name)
|
|
}
|
|
ctx = p.Via(ctx)
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, origin+"/v3/config", nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if err := p.Sign(ctx, req, provider.Chat, nil); err != nil {
|
|
return nil, err
|
|
}
|
|
req.Header.Set("Accept", "application/json")
|
|
res, err := p.Do(client, req) // a moved one's through its plugin
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer res.Body.Close()
|
|
b, _ := io.ReadAll(io.LimitReader(res.Body, 8<<20))
|
|
if res.StatusCode != http.StatusOK {
|
|
return nil, fmt.Errorf("%s's config: %d%s", p.Name, res.StatusCode, vendorSaid(vendorMessage(b)))
|
|
}
|
|
var env struct {
|
|
Code json.RawMessage `json:"code"`
|
|
Msg string `json:"msg"`
|
|
Data struct {
|
|
Models []wbImageModel `json:"models"`
|
|
} `json:"data"`
|
|
}
|
|
if err := json.Unmarshal(b, &env); err != nil {
|
|
return nil, fmt.Errorf("%s's config: %v", p.Name, err)
|
|
}
|
|
if c := strings.Trim(string(env.Code), `"`); c != "" && c != "0" && c != "null" {
|
|
return nil, fmt.Errorf("%s's config: %s (%s)", p.Name, env.Msg, c)
|
|
}
|
|
var out []wbImageModel
|
|
for _, m := range env.Data.Models {
|
|
if m.ID != "" && (m.tagged("text-to-image") || m.tagged("image-to-image")) {
|
|
out = append(out, m)
|
|
}
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
// wbDrawers are the models a WorkBuddy plan draws with: those its config
|
|
// tags text-to-image, as last asked (asked again in the background when
|
|
// that's old). One that edits too, itself or by the model beside it, takes
|
|
// images.
|
|
func wbDrawers(p provider.Provider) []catalog.Model {
|
|
ms, _ := wbImageModelsOf(context.Background(), wbConfigClient, p, false)
|
|
var out []catalog.Model
|
|
for _, m := range ms {
|
|
if !m.tagged("text-to-image") {
|
|
continue
|
|
}
|
|
out = append(out, catalog.Model{ID: m.ID, Name: cmp.Or(m.Name, m.ID), Provider: p.ID, Images: wbEditor(ms, m.ID) != ""})
|
|
}
|
|
return out
|
|
}
|
|
|
|
// drawWorkBuddy asks WorkBuddy's images API as its CLI does: GPT Image with
|
|
// OpenAI's parameters and b64_json, Hunyuan with the prompt, n and size
|
|
// alone; an edit goes to the model that edits (wbEditor), its images as
|
|
// data URLs.
|
|
func (s *Server) drawWorkBuddy(ctx context.Context, p provider.Provider, model string, d drawing) (drawn, int, error) {
|
|
target := model
|
|
if len(d.Images) > 0 {
|
|
ms, err := wbImageModelsOf(ctx, s.client, p, true)
|
|
if err != nil {
|
|
return drawn{}, 502, err
|
|
}
|
|
if target = wbEditor(ms, model); target == "" {
|
|
return drawn{}, 400, fmt.Errorf("%s draws %s from a prompt only: its config lists no model to edit images with", p.Name, model)
|
|
}
|
|
}
|
|
gpt := strings.Contains(strings.ToLower(target), "gpt-image")
|
|
req := map[string]any{"model": target, "prompt": d.Prompt, "n": d.N}
|
|
if d.Size != "" && (gpt || d.Size != "auto") {
|
|
req["size"] = d.Size
|
|
}
|
|
if gpt {
|
|
for k, v := range map[string]string{"quality": d.Quality, "background": d.Background, "output_format": d.Format} {
|
|
if v != "" {
|
|
req[k] = v
|
|
}
|
|
}
|
|
req["response_format"] = "b64_json"
|
|
}
|
|
url := strings.TrimRight(p.Base(provider.Chat), "/") + "/images/generations"
|
|
if len(d.Images) > 0 {
|
|
var imgs []string
|
|
for _, pic := range d.Images {
|
|
imgs = append(imgs, pic.dataURL())
|
|
}
|
|
req["image"] = imgs
|
|
if gpt && d.Mask != nil {
|
|
req["mask"] = d.Mask.dataURL()
|
|
}
|
|
url = strings.TrimRight(p.Base(provider.Chat), "/") + "/images/edits"
|
|
}
|
|
body, _ := json.Marshal(req)
|
|
b, code, err := s.send(ctx, p, url, "application/json", body, true)
|
|
if err != nil {
|
|
return drawn{}, code, err
|
|
}
|
|
// OpenAI's answer is WorkBuddy's data
|
|
var env struct {
|
|
Code json.RawMessage `json:"code"`
|
|
Msg string `json:"msg"`
|
|
Data json.RawMessage `json:"data"`
|
|
}
|
|
if json.Unmarshal(b, &env) == nil && len(env.Code) > 0 {
|
|
if c := strings.Trim(string(env.Code), `"`); c != "0" {
|
|
return drawn{}, 502, fmt.Errorf("%s: %s (%s)", provider.HostOf(url), cmp.Or(env.Msg, "error"), c)
|
|
}
|
|
if len(env.Data) > 0 && env.Data[0] == '{' {
|
|
b = env.Data
|
|
}
|
|
}
|
|
return readImagesAnswer(p, url, b, code)
|
|
}
|