Files
magpie/internal/gateway/cursor_local_models_test.go
T
yetone 258eae592d agent: Cursor Private Inference, which magpie finds by its product.json and starts on magpie with a command its row copies, and whose /models gives each model's api_types and capabilities (#299, mamba on Discord) — the build (Cursor 3.23.x, the same app name, bundle id and ~/.cursor as Cursor) runs its agent locally and asks the endpoint in CURSOR_LOCAL_AGENT_BASE_URL / CURSOR_LOCAL_AGENT_API_KEY when its Open configuration has none; mamba confirmed it works with any account pointed at magpie. magpie writes nothing of Cursor's (its settings share Cursor's state.vscdb): the Agents page lists it when an app's product.json says "Cursor Private Inference" (/Applications, ~/Applications; %LOCALAPPDATA%\Programs on Windows; /opt and the like on Linux), and its row's one control, Copy launch command (复制启动命令), copies CURSOR_LOCAL_AGENT_BASE_URL=<gateway>/v1 CURSOR_LOCAL_AGENT_API_KEY=magpie-cursor-local '<app>' (PowerShell on Windows); in the tray panel the row copies it. Its notice says to quit it first (a second start hands over to the running one) and that a base URL in Open configuration comes first. /v1/models asked with its key gives each model api_types (the APIs its provider serves it on, all three for a group or a translated one; it asks Anthropic Messages only when no OpenAI one is listed) and capabilities (context_length, max_output_tokens, supports_vision); any other client's list is unchanged. usage.AgentOf matches a name before a UA prefix, so cursor-local isn't counted as Cursor. TestCursorLocal, TestCursorLocalModels and cursor-local-launch.test.cjs (WebKit, en and zh) fail on the old code; agy-launch and agent-* GUI tests pass on WebKit. Not tried with the real app (not installed here). go vet, builds (darwin, GOOS=linux nogui, GOOS=windows) and go test -tags nogui -count=1 ./... under a temp HOME pass.
2026-10-04 23:38:19 +08:00

76 lines
2.4 KiB
Go

package gateway
import (
"encoding/json"
"net/http/httptest"
"slices"
"testing"
"github.com/yetone/magpie/internal/catalog"
"github.com/yetone/magpie/internal/provider"
)
// Cursor Private Inference reads magpie's /models for each model's APIs
// (api_types: it asks Anthropic Messages only when no OpenAI one is
// listed) and its limits (capabilities.context_length) (#299): asked with
// its key, each model has them, the ones its provider serves it on; asked
// by any other client, the list is as it was.
func TestCursorLocalModels(t *testing.T) {
setup(t, provider.Anthropic, &fake{})
if err := provider.Save(provider.Provider{ID: "ds", Name: "DS", Key: "k", Chat: "http://127.0.0.1:1/v1", Models: []string{"flash"}}); err != nil {
t.Fatal(err)
}
if err := catalog.SaveLive("ds", "http://127.0.0.1:1/v1", []catalog.Model{{ID: "flash", Context: 128000, Output: 8192}}); err != nil {
t.Fatal(err)
}
list := func(key string) map[string]map[string]any {
t.Helper()
req := httptest.NewRequest("GET", "/v1/models", nil)
req.Header.Set("Authorization", "Bearer "+key)
rec := httptest.NewRecorder()
New().Handler().ServeHTTP(rec, req)
var out struct {
Data []map[string]any `json:"data"`
}
if err := json.Unmarshal(rec.Body.Bytes(), &out); err != nil {
t.Fatal(rec.Body.String())
}
got := map[string]map[string]any{}
for _, d := range out.Data {
got[d["id"].(string)] = d
}
return got
}
types := func(m map[string]any) []string {
var out []string
for _, v := range m["api_types"].([]any) {
out = append(out, v.(string))
}
return out
}
got := list(TokenFor("cursor-local"))
f, ds := got["fake/m1"], got["ds/flash"]
if f == nil || ds == nil {
t.Fatalf("models: %v", got)
}
if ts := types(f); !slices.Equal(ts, []string{"anthropic_messages"}) {
t.Errorf("an Anthropic model's api_types = %v", ts)
}
if ts := types(ds); !slices.Equal(ts, []string{"chat_completions"}) {
t.Errorf("a Chat model's api_types = %v", ts)
}
c, _ := ds["capabilities"].(map[string]any)
if c["context_length"] != float64(128000) || c["max_output_tokens"] != float64(8192) {
t.Errorf("capabilities = %v", ds["capabilities"])
}
for _, key := range []string{Token, TokenFor("cursor"), TokenFor("opencode")} {
for id, m := range list(key) {
if m["api_types"] != nil || m["capabilities"] != nil {
t.Errorf("%s: %s has %v %v", key, id, m["api_types"], m["capabilities"])
}
}
}
}