Files
magpie/internal/gateway/dashscope_stream_test.go
T
yetone 8d95ebc8c3 provider: Bailian Token Plan is a preset of its own, with its decision model for routing groups; the Qwen AI platform's decision model too, and Bailian's shows Bailian's icon (#1506, IamMiao)
Alibaba sells two Token Plans, both with sk-sp- keys, and a key works only
on its own host. qwen-token-plan is the Qwen AI platform's
(token-plan.maas.qianwenaiapi.com), but its website and keys links pointed
at Bailian's console. Bailian's own plan
(token-plan.cn-beijing.maas.aliyuncs.com) had no preset.

What changed:
- presets: new bailian-token-plan (chat /compatible-mode/v1, Anthropic
  /apps/anthropic, decide on /compatible-mode/v1/systemone, models from the
  docs' list, shared with qwen-token-plan as alibabaPlanModels).
  qwen-token-plan's links now point at the Qwen AI platform. New
  qwen-decision for Qwen's pay-as-you-go decision model at
  maas.qianwenaiapi.com/compatible-mode/v1/systemone. bailian-decision shows
  the Bailian icon instead of Qwen's.
- decide: bailianDecides also matches the bare maas.qianwenaiapi.com host.
  A chat preset that also decides on a Bailian/Qwen host (bailianApart)
  lists decision-model-preview as its only decision model, so it is offered
  for routing groups and never given to an agent as a chat model.
- gateway: dashScope() covers bailian-token-plan and maas.qianwenaiapi.com.
- CLI help, docs/reference.md and the import docs (en, zh, ja) name the new
  preset.

Usage: neither plan's docs give a usage or quota API; the remaining quota
is only on the console (百炼控制台 Token Plan > 我的订阅, Qwen 工作台
token-plan/individual), which needs a browser session. So no usage card is
added. An exhausted plan's 429 insufficient_quota is already read as out of
credit.

Not tried against the real API: no Bailian or Qwen key was available. The
stand-in is an httptest server copying the docs' systemone curl (Bearer
sk-sp-, model decision-model-preview) and its error table (401 on a wrong
key, 422 on a bad body). qwen-decision sends "Authorization: Bearer <key>";
the Qwen doc's curl shows the bare key, which is untested.

Run:
- go test -tags nogui -count=1 ./... (temp HOME)
- TestBailianTokenPlan, TestQwenDecisionPreset and TestDashScopeHosts fail
  with the old decide.go and gateway.go swapped in
- gofmt -l; GOOS=linux and GOOS=windows go vet -tags nogui on .
  internal/provider, internal/gateway, internal/gui
- GUI: system-one, add-sheet, gateway-decide-info, routing-kind tests in
  chromium and webkit (20 pass)
2026-10-10 21:14:38 +08:00

85 lines
3.6 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"},
} {
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"}) {
t.Error("another vendor is DashScope")
}
}