Files
magpie/internal/provider/import_opencode.go
T
yetone 617200a320 providers: Vercel AI Gateway is a preset (Archie627 on X)
Archie627 said magpie has no Vercel AI Gateway. It is now a preset in
Add: Chat Completions and Responses at https://ai-gateway.vercel.sh/v1,
Anthropic Messages at https://ai-gateway.vercel.sh (Vercel takes the key
as Bearer there too), models.dev's "vercel" catalog, the vercel icon the
vercel-jev decision preset already used, and its API keys page. An
OpenCode or URL import at that host becomes the preset. READMEs and the
site's import pages (en, zh, ja) list it.

Its /v1/models (public, 505 rows today) marks each row's "type". Read
as before, the decision models (liquid/d1, typesafe-ai/jev) and realtime
voices showed as chat models, ElevenLabs voices were dropped, and
transcribers (elevenlabs/scribe-v2) showed as chat. At Vercel's host
alone (catalog.VercelGateway) the type now decides: evaluation and
realtime rows are left out, speech, transcription, embedding and
reranking rows get their Serves, image rows draw and video rows film.
The 48 "-batch" ids are left out: Vercel's docs say "Synchronous
requests don't accept -batch IDs". context_window gives the window.
Another host's "type" is read as before.

Run: TestVercelPreset and TestVercelLiveList (real rows from Vercel's
list in testdata, served through an httptest server, no network);
go test ./internal/catalog ./internal/provider on Linux (wsl-run) and
macOS (mote, -run Vercel|Preset|Import before mote stopped taking SSH);
gofmt; go vet linux, windows, darwin (CGO off).
Broken by hand: VercelGateway returning false turns TestVercelLiveList
red (decision, realtime, batch and transcriber rows back in chat); the
-batch skip alone turned off turns it red too.
Not tried: no Vercel key, so no real chat, Responses or Messages call;
only the public GET /v1/models. No GUI file reads the preset list by
name (the Add sheet is built from /api/presets), so no GUI run.
2026-11-10 18:11:38 +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", "vercel": "vercel",
"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
}