On v0.1.4137 a provider on the Qwen AI platform's Token Plan
(qwen-token-plan, token-plan.maas.qianwenaiapi.com) had no quota card at
all. The plan key (sk-sp-) only authorizes inference; the platform tells
the plan's usage only to a sign-in on the platform, and magpie had none.
Now each such provider can sign in to the platform, as the QianWen CLI
1.10.0 does (qianwen_plan.go, UA qianwen-cli/1.10.0):
- a device sign-in with PKCE S256 at t.qianwenai.com/cli/device/code and
/cli/device/token, approved in the user's browser; the editor's
"Sign in to the Qwen AI platform" opens the page and waits for it;
- the session is the provider's own, kept by provider id in
qianwen-auth.json (0600), moved on Rename and dropped on Delete;
- cli.qianwenai.com/data/v2/api.json (GenerateCLIAccessToken) mints a
console token, kept and minted once more when the gateway answers
NotLogined; ConsoleNeedLogin or a fresh token refused signs it out;
- the usage is the Alibaba Token Plan's BroadScopeAspnGateway call at
cs-data.qianwenai.com with consoleSite QIANWENAI, region cn-beijing,
read by the shared aliAskUsage/aliReadWindows (alibaba_usage.go now
takes the console per call). The sk-sp- key is never sent there.
- Plan windows (planquota.go) and routing's key allowance
(key_allowance.go, keyTag "qianwen") read it; not signed in, the card
says so with Open settings (NeedsAccessKey).
- GUI routes /api/qianwen/{id}/signin (POST start, GET state),
/signin/cancel, /signout; the editor's field lives in qianwenplan.js,
imported when such a provider's editor opens. The core script budget
is raised by 326 for the import and one quotaError line (its reason in
core_budget_test.go). The routes are in the CLI parity's notYet, as
StepFun's are.
Run, on mote at the base df2b819c6 (= origin/main then):
- gofmt -l: clean. go vet (darwin), GOOS=linux go vet -tags nogui,
GOOS=windows go vet on internal/provider, internal/gui, internal/cli: ok.
- go test -count=1 -timeout 45m ./internal/provider ./internal/gui
./internal/cli: provider ok; gui and cli red on TestCoreScriptBudget
and TestEveryRouteHasACommandOrIsNamed, both mine; fixed (code moved to
qianwenplan.js, budget +326, routes named), then gui ok, cli ok.
- go test -race -run Qianwen ./internal/provider: ok.
- GUI (chromium + webkit, 900/420px, en zh zh-TW ja de where the file
has them): qianwen-plan 20/20, stepfun-per-provider,
usage-access-key-card, volc-access-key, quota-balance-error,
i18n-en-no-cjk all ok; earlier also opencode-zen-balance,
siliconflow-balance, usage-card-refresh, usage-remote-key-limits,
usage-remote-cards ok. Reds not mine: i18n-keys "a plugin's words are
its own" (#6619, fixed on main by ea6470cb7), old-webkit's
headerpresets.js (#6030) and app.css :has() (#5919).
- Rebased on origin/main 79a30eb9c and run again at that tree on mote:
go test -count=1 -timeout 45m ./internal/provider ./internal/gui
./internal/cli all ok; qianwen-plan + i18n-keys 24 pass, 0 fail. Rebased again onto
fc75d90af before the push; b507b5f69 and fc75d90af touch only
internal/sessions and third_party/yaml.v3, not run again.
Broken by hand, each red, then restored: the NotLogined retry, the
code_verifier, renameQianwen, the planquota channel, forgetQianwen on
Delete, the session expiry check, qianwenWatch not called, the sign-in
quotaError mapping.
Not tried: a real sign-in. No Qwen AI platform account is available here,
and approving the device sign-in grants an OAuth permission. Fixtures are
built from the QianWen CLI 1.10.0 source and the live device/code and
unauthenticated replies; the usage reply's shape is the Alibaba Token
Plan's, which the CLI reads with the same code.
98 lines
3.3 KiB
Go
98 lines
3.3 KiB
Go
package gui
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
// A Qwen AI platform Token Plan's sign-in (@imthe99galaxy on X: no quota
|
|
// for the plan) is its provider's own: the editor is told each one's and
|
|
// whose, and signing one out leaves the other signed in. A provider that
|
|
// isn't on the plan has no sign-in to start, ask or end.
|
|
func TestQianwenSignInRoutes(t *testing.T) {
|
|
t.Setenv("HOME", t.TempDir())
|
|
t.Setenv("USERPROFILE", os.Getenv("HOME"))
|
|
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
|
|
t.Setenv("XDG_CACHE_HOME", t.TempDir())
|
|
const base = "https://token-plan.maas.qianwenaiapi.com/compatible-mode/v1"
|
|
for _, p := range []provider.Provider{
|
|
{ID: "qwen-token-plan", Name: "Qwen Token Plan", Key: "sk-sp-1", Chat: base},
|
|
{ID: "qwen-work", Name: "Qwen Work", Key: "sk-sp-2", Chat: base},
|
|
{ID: "bailian", Name: "Bailian", Key: "sk-sp-3", Chat: "https://token-plan.cn-beijing.maas.aliyuncs.com/compatible-mode/v1"},
|
|
} {
|
|
if err := provider.Save(p); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
auth := filepath.Join(filepath.Dir(provider.Path()), "qianwen-auth.json")
|
|
if err := os.WriteFile(auth, []byte(`{"qwen-token-plan":{"access":"a1","email":"a@example.com"},"qwen-work":{"access":"a2"}}`), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
mux := http.NewServeMux()
|
|
providerRoutes(mux, nil)
|
|
plans := func() map[string]string {
|
|
t.Helper()
|
|
w := httptest.NewRecorder()
|
|
mux.ServeHTTP(w, httptest.NewRequest("GET", "/api/providers", nil))
|
|
var got struct {
|
|
Providers []struct {
|
|
ID string
|
|
QianwenPlan *struct {
|
|
SignedIn bool
|
|
Account string
|
|
}
|
|
}
|
|
}
|
|
if err := json.Unmarshal(w.Body.Bytes(), &got); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
out := map[string]string{}
|
|
for _, p := range got.Providers {
|
|
if q := p.QianwenPlan; q != nil {
|
|
out[p.ID] = "out"
|
|
if q.SignedIn {
|
|
out[p.ID] = "in " + q.Account
|
|
}
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
if got := plans(); got["qwen-token-plan"] != "in a@example.com" || got["qwen-work"] != "in " || len(got) != 2 {
|
|
t.Fatalf("before: %v", got)
|
|
}
|
|
for _, id := range []string{"bailian", "nobody"} {
|
|
for _, c := range []struct{ method, path string }{{"POST", "/signin"}, {"GET", "/signin"}, {"POST", "/signin/cancel"}, {"POST", "/signout"}} {
|
|
w := httptest.NewRecorder()
|
|
mux.ServeHTTP(w, httptest.NewRequest(c.method, "/api/qianwen/"+id+c.path, strings.NewReader("{}")))
|
|
if w.Code != http.StatusNotFound {
|
|
t.Errorf("%s %s for %s: %d, want 404", c.method, c.path, id, w.Code)
|
|
}
|
|
}
|
|
}
|
|
w := httptest.NewRecorder()
|
|
mux.ServeHTTP(w, httptest.NewRequest("GET", "/api/qianwen/qwen-work/signin", nil))
|
|
if w.Code != 200 || !strings.Contains(w.Body.String(), `"state":""`) {
|
|
t.Errorf("no sign-in under way: %d %s", w.Code, w.Body)
|
|
}
|
|
w = httptest.NewRecorder()
|
|
mux.ServeHTTP(w, httptest.NewRequest("POST", "/api/qianwen/qwen-work/signin/cancel", nil))
|
|
if w.Code != http.StatusNoContent {
|
|
t.Errorf("cancel: %d", w.Code)
|
|
}
|
|
w = httptest.NewRecorder()
|
|
mux.ServeHTTP(w, httptest.NewRequest("POST", "/api/qianwen/qwen-token-plan/signout", strings.NewReader("{}")))
|
|
if w.Code != 200 {
|
|
t.Fatalf("signout: %d %s", w.Code, w.Body)
|
|
}
|
|
if got := plans(); got["qwen-token-plan"] != "out" || got["qwen-work"] != "in " {
|
|
t.Fatalf("after signing one out: %v", got)
|
|
}
|
|
}
|