Files
magpie/internal/provider/import_opencode.go
T
yetone 066ece93bb provider: LongCat is a vendor preset, Meituan's LongCat API platform with chat completions, Responses and Anthropic messages (hoobnn on Discord)
LongCat makes its own models (LongCat-2.5, launched 11-02) and serves them
at api.longcat.chat. magpie had LongCat's icon but no preset for it.

The new preset is longcat ("LongCat", KindVendor, icon longcat, catalog
models.dev's longcat). It uses:
- chat completions and Responses at https://api.longcat.chat/openai/v1;
- Anthropic messages at https://api.longcat.chat/anthropic;
- docs at longcat.chat/platform/docs/zh/api-docs, keys at
  longcat.chat/platform/api_keys.
The key goes as a Bearer. magpie sends both headers, and the docs ask for
Bearer on both endpoints; x-api-key alone gets "missing_api_key". The model
list is /openai/v1/models. longcat.chat's and longcat.ai's accounts use the
same API host (both sites' api-docs pages name api.longcat.chat).

An entry at those endpoints imports as the preset, for example CC Switch's
Claude Code and Codex entries from LongCat's cc-switch page. OpenCode's
auth.json "longcat" (models.dev's id) maps to the preset too. The README
(en, zh), docs/reference.md and the site's import pages (en, zh, ja, and
import.html, with the icon) list it.

Sources, read 11-02: api-docs, api/models, api/model, api/model-params,
api/chat, api/messages, image-video-understanding, cc-switch, pricing/
longcat-2.5, change-log, faq.

Probed without a key, from this machine: /openai/v1/models and
/anthropic/v1/models answer 401 authentication_error "missing_api_key".
/openai/v1/chat/completions, /openai/v1/responses and
/anthropic/v1/messages with a bad Bearer answer 401 "incorrect api key".

Run at this head (base d24be3c25, origin/main at push), on mote:
- TestLongCatPreset and TestLongCatList (the docs' literal /models
  example) pass. TestLongCatPreset goes red with the OpenCode mapping taken
  out ("OpenCode import: … Skip:it has no base URL").
- gofmt -l and GOOS=linux / GOOS=windows go vet ./internal/provider are
  clean.
- go test -tags nogui -timeout 45m ./... is ok except three tests that
  have issues already:
  - TestEveryRouteHasACommandOrIsNamed (#4826) is red on a clean
    d24be3c25 too.
  - TestKeepOffWaitsForTheSetting (#3687) is red on a clean d24be3c25 too.
  - TestCodexPoolSaysTheClientsVersion (#4807) is red in the full package
    run and passes 3 of 3 alone on d24be3c25.
- GUI add-sheet and add-button pass in Chromium and WebKit (12/12). No GUI
  file changed: the add sheet draws presets from /api.

Not tried:
- a real LongCat key or a real request (none available).
- LongCat-2.5's window (1M), output cap (256K) and image input. These come
  from models.dev, which so far lists only LongCat-2.0 and
  LongCat-2.5-Preview, and LongCat's /models gives no facts. Until
  models.dev adds LongCat-2.5, magpie doesn't know them for it.
- LongCat's thinking switch ({"type":"enabled"|"disabled"}).
2026-11-02 13:44:11 +08:00

99 lines
3.7 KiB
Go

package provider
import (
"encoding/json"
"errors"
"maps"
"os"
"path/filepath"
"slices"
"strings"
"github.com/yetone/magpie/internal/appdir"
"github.com/yetone/magpie/internal/edit"
)
// OpenCode's own sign-ins, as `opencode auth login` keeps them: auth.json in
// its data folder, $XDG_DATA_HOME/opencode, else ~/.local/share/opencode
// (xdg-basedir's, on Windows too). It maps a provider id (models.dev's) to
// {"type":"api","key":…}, {"type":"oauth",…} or {"type":"wellknown",…}
// (packages/opencode/src/auth/index.ts). It holds keys only, never where
// they go: an id is imported when magpie has its preset.
func openCodeAuthPath() string {
if d := appdir.Getenv("XDG_DATA_HOME"); d != "" {
return filepath.Join(d, "opencode", "auth.json")
}
home, _ := os.UserHomeDir()
return filepath.Join(home, ".local", "share", "opencode", "auth.json")
}
// openCodePresets are OpenCode's provider ids by the magpie preset at the
// same address. An id at a host the preset doesn't serve (SiliconFlow's
// .com beside the preset's .cn) is left out: its key would be sent there.
// The ids' hosts are models.dev's api for each, what OpenCode itself sends
// to, and it is the preset's own address, not one of its regions. Where a
// region would be the right target — Zhipu's, Z.ai's, StepFun's and
// Volcengine's pay-as-you-go ids beside plan-first defaults, MiniMax's and
// Tencent's coding plans — auth.json says nothing to pick a region by, so
// those ids stay out and are skipped with a reason that says why.
var openCodePresets = map[string]string{
"opencode": "opencode-zen", "opencode-go": "opencode-go",
"openai": "openai", "anthropic": "anthropic", "google": "google", "deepseek": "deepseek",
"xai": "xai", "moonshotai": "moonshot", "moonshotai-cn": "moonshot-cn", "mistral": "mistral",
"groq": "groq", "openrouter": "openrouter", "togetherai": "together", "fireworks-ai": "fireworks",
"siliconflow-cn": "siliconflow", "nvidia": "nvidia", "aihubmix": "aihubmix", "302ai": "302ai",
"ollama-cloud": "ollama-cloud",
"zhipuai": "zhipu", "zai": "zai", "alibaba": "qwen", "alibaba-cn": "qwen-cn",
"minimax": "minimax", "modelscope": "modelscope", "kilo": "kilo", "cline-pass": "clinepass",
"kimi-code-plan-cn": "kimi-code-cn", "kimi-code-plan-global": "kimi-code",
"stepfun-step-plan": "stepfun-cn", "stepfun-ai-step-plan": "stepfun", "longcat": "longcat",
"volcengine-coding-plan": "volcengine",
"alibaba-token-plan-cn": "bailian-token-plan", "tencent-token-plan": "tencent-cloud",
}
func readOpenCodeAuth(path string) ([]AppImport, error) {
raw, err := os.ReadFile(path)
if err != nil {
return nil, err
}
var all map[string]json.RawMessage
if err := json.Unmarshal(edit.TrimBOM(raw), &all); err != nil {
return nil, errors.New("OpenCode's auth.json: " + err.Error())
}
var out []AppImport
for _, id := range slices.Sorted(maps.Keys(all)) {
var e struct {
Type string `json:"type"`
Key string `json:"key"`
}
if json.Unmarshal(all[id], &e) != nil {
continue // OpenCode passes over what it can't read too
}
it := AppImport{Ref: id, From: "auth.json"}
pr := Preset(openCodePresets[id])
name := id
if pr != nil {
name = pr.Name
}
skip := ""
switch {
case e.Type == "oauth" || e.Type == "wellknown":
skip = "it is a sign-in, not a key; add the subscription in magpie"
case e.Type != "api":
continue
case pr == nil:
skip = "it has no base URL" // OpenCode keeps only its key here
case strings.TrimSpace(e.Key) == "":
skip = "it has no API key"
default:
it.Provider, skip = imported(name, strings.TrimSpace(e.Key), endpoints{pr.Chat, pr.Responses, pr.Anthropic, ""}, nil)
}
if skip != "" {
it.Provider, it.Skip = Provider{Name: name}, skip
}
out = append(out, it)
}
return out, nil
}