Files
magpie/internal/gateway/dashscope_stream_test.go
T
yetone 3e5aa11519 provider: Qwen Cloud, Alibaba's international Qwen platform, is a preset with its regions and Token Plan (judi0713)
judi0713 on Discord (v0.1.2288) asked for Qwen Cloud (qwencloud.com).
Its keys are its own, made at home.qwencloud.com, and serve neither
DashScope's nor Bailian's hosts, so none of magpie's Qwen presets took
them.

Two presets, the way Alibaba's other pay-as-you-go/Token Plan pair is
modelled, because each key gets 401 at the other's host:

- qwencloud: pay as you go, sk-ws- keys, at
  https://{region}.maas.qwencloudapi.com/compatible-mode/v1 (Chat) and
  /apps/anthropic (Messages), in six regions named by their ids as
  Alibaba's docs name them: ap-southeast-1 (the default), us-east-1,
  eu-central-1, ap-northeast-1, cn-hongkong, cn-beijing. Catalog alibaba.
- qwencloud-token-plan: sk-sp- keys, token-plan.maas.qwencloudapi.com,
  Singapore only, with the plan overview's text models.

Endpoints, regions, key prefixes and the plan's models are from
docs.qwencloud.com ("Regions and endpoints" and the Token Plan
overview), not guessed. Responses is left out, as in the other
Qwen presets.

The gateway's dashScope() now knows both presets and the
*.maas.qwencloudapi.com hosts, so a Qwen Cloud stream gets the same
enable_thinking handling a DashScope one does. Import (presetAt), the
CLI's region= and magpie://import?preset=qwencloud&region=... map to it.
docs/reference.md, the CLI help and the site's import page (en, zh, ja)
name them. Region names are ids, so no string is new to i18n.js and
nothing in the GUI changed.

Run (mote, at 708213f65 on c272e5582; after it the commit was rebased on
876d477d7, which touches none of these files, and the new helpers' comment
was moved above bedrockRegions' doc comment, no code changed; at the pushed
head the provider tests below ran again, ok):
- gofmt -l: clean.
- go vet -tags nogui ./..., GOOS=linux go vet -tags nogui ./...,
  GOOS=windows go vet ./...: ok.
- go test -tags nogui ./... -timeout 45m -count=1: every package ok
  (gateway 804s, provider 205s) except two reds already filed, in files
  this doesn't touch: internal/proc TestNoCommandBypassesProc
  (claudetui/child_unix.go's exec.Command, #4491) and internal/update
  TestRelaunchOpenTriedAgain (TempDir cleanup not empty, #3881).
- At the pushed head: go test ./internal/provider -run
  'TestQwenCloud|TestQwenTokenPlan|Preset|TestParseImport': ok.
- No GUI change, so no GUI run.

Broken by hand:
- Preset ids renamed: TestQwenCloud red ("no preset qwencloud-token-plan").
- gateway.go's dashScope() back to main: TestDashScopeHosts red on the 5
  new Qwen Cloud entries; the maas.qwencloudapi.com.example case stays
  not-DashScope.

Not tried:
- No real Qwen Cloud key: no request went to qwencloudapi.com.
- The decision model (decide.go's bailianDecides hosts) doesn't know
  Qwen Cloud, and image models on these presets are the next commit's.
- The site isn't deployed by this commit.
2026-10-31 09:58:04 +08:00

92 lines
4.1 KiB
Go

package gateway
import (
"encoding/json"
"strings"
"testing"
"github.com/yetone/magpie/internal/provider"
)
// dashScopeRefuse answers as DashScope's compatible mode does a thinking
// model asked for one JSON body with its thinking left on.
func dashScopeRefuse(body []byte) (int, string) {
var q struct {
Stream bool `json:"stream"`
EnableThinking *bool `json:"enable_thinking"`
}
json.Unmarshal(body, &q)
if q.Stream || q.EnableThinking != nil && !*q.EnableThinking {
return 0, ""
}
return 400, `{"error":{"message":"<400> InternalError.Algo.InvalidParameter: parameter.enable_thinking must be set to false for non-streaming calls","type":"invalid_request_error","param":null,"code":"invalid_parameter_error"},"id":"chatcmpl-1","request_id":"1"}`
}
// A group's classifier asks its model for one JSON body; on the Qwen
// Token Plan a model that thinks turns that away (ZekeXiao on X), so it is
// asked streamed and its answer given back whole.
func TestDashScopeWholeAskedStreamed(t *testing.T) {
f := &fake{t: t, refuse: dashScopeRefuse, reply: sse(
`data: {"id":"c1","object":"chat.completion.chunk","model":"m1","choices":[{"index":0,"delta":{"role":"assistant","reasoning_content":"A greeting."}}]}`,
`data: {"id":"c1","object":"chat.completion.chunk","model":"m1","choices":[{"index":0,"delta":{"content":"1"}}]}`,
`data: {"id":"c1","object":"chat.completion.chunk","model":"m1","choices":[{"index":0,"delta":{},"finish_reason":"stop"}],"usage":{"prompt_tokens":40,"completion_tokens":5,"total_tokens":45}}`,
`data: [DONE]`)}
up := setup(t, provider.Chat, f)
if err := provider.Save(provider.Provider{ID: "fake", Name: "Qwen Token Plan", Key: "k", Models: []string{"m1"},
Preset: "qwen-token-plan", Chat: up.URL + "/compatible-mode/v1"}); err != nil {
t.Fatal(err)
}
srv := New()
code, body := postTo(t, srv, "/v1/chat/completions", `{"model":"m1","stream":false,"temperature":0,"max_tokens":2048,"messages":[{"role":"user","content":"hi"}]}`)
if code != 200 {
t.Fatalf("%d: %s", code, body)
}
var out struct {
Choices []struct {
Message struct {
Content string `json:"content"`
} `json:"message"`
} `json:"choices"`
}
if err := json.Unmarshal([]byte(body), &out); err != nil || len(out.Choices) != 1 || out.Choices[0].Message.Content != "1" {
t.Fatalf("answer %s (%v)", body, err)
}
if !strings.Contains(string(f.got), `"stream":true`) {
t.Errorf("asked %s", f.got)
}
// another vendor's request for one body goes as it was asked
f2 := &fake{t: t, reply: chatWholeOK, ctype: "application/json"}
setup(t, provider.Chat, f2)
code, body = postTo(t, New(), "/v1/chat/completions", `{"model":"m1","stream":false,"messages":[{"role":"user","content":"hi"}]}`)
if code != 200 || strings.Contains(string(f2.got), `"stream":true`) {
t.Fatalf("%d, asked %s: %s", code, f2.got, body)
}
}
// Both Token Plans are DashScope's (#1506): the Qwen AI platform's and
// Bailian's, by preset or by host, and the Qwen AI platform's own host.
func TestDashScopeHosts(t *testing.T) {
for _, p := range []provider.Provider{
{Preset: "qwen-token-plan"}, {Preset: "bailian-token-plan"},
{Chat: "https://token-plan.cn-beijing.maas.aliyuncs.com/compatible-mode/v1"},
{Chat: "https://token-plan.maas.qianwenaiapi.com/compatible-mode/v1"},
{Chat: "https://maas.qianwenaiapi.com/compatible-mode/v1"},
{Chat: "https://dashscope.aliyuncs.com/compatible-mode/v1"},
// Qwen Cloud's (judi0713): its presets, its plan's host, a region's
// and the regionless Singapore one
{Preset: "qwencloud"}, {Preset: "qwencloud-token-plan"},
{Chat: "https://token-plan.maas.qwencloudapi.com/compatible-mode/v1"},
{Chat: "https://us-east-1.maas.qwencloudapi.com/compatible-mode/v1"},
{Chat: "https://maas.qwencloudapi.com/compatible-mode/v1"},
} {
if !dashScope(p) {
t.Errorf("not DashScope: %+v", p)
}
}
if dashScope(provider.Provider{Chat: "https://api.deepseek.com/v1"}) || dashScope(provider.Provider{Chat: "https://qianwenaiapi.com.example/v1"}) ||
dashScope(provider.Provider{Chat: "https://maas.qwencloudapi.com.example/v1"}) {
t.Error("another vendor is DashScope")
}
}