Files
magpie/internal/provider/import_opencode.go
T
ggbdpq eaa71168a2 import: OpenCode's ids for vendors magpie has a preset at the same address come in on it (#2405)
openCodePresets held 19 of OpenCode's provider ids. 15 more are at the
address of a magpie preset, and the import picker listed each as
skipped, "it has no base URL", which isn't so: zhipuai, zai, alibaba,
alibaba-cn, minimax, modelscope, kilo, cline-pass, kimi-code-plan-cn,
kimi-code-plan-global, stepfun-step-plan, stepfun-ai-step-plan,
volcengine-coding-plan, alibaba-token-plan-cn and tencent-token-plan.
Each now comes in on its preset, the way openai's key does.

Left out, where auth.json says nothing to pick a region by: the coding
plan ids of Zhipu, Z.ai, MiniMax and Tencent, the pay-as-you-go ids of
plan-first presets (stepfun, stepfun-ai, volcengine), and the Xiaomi
MiMo token plans, served by a plugin now.

Patch by ggbdpq (Discord gua2100), as posted in #2405. Review: each of
the 15 targets' default endpoints was printed from Preset() at this
tree and matches the host models.dev gives the id (zhipu and zai
default to the pay-as-you-go paas/v4, volcengine to api/coding/v3,
stepfun and stepfun-cn to step_plan, tencent-cloud to plan/v3), so no
key is sent to a host OpenCode wouldn't send it to. No preset added.

Run (sandbox HOME, at origin/main f8cd964ae plus this; the touched tests again on mote after the rebase):
- go test -tags nogui -run OpenCode ./internal/provider/: ok
- Broken by hand: import_opencode.go put back to origin/main, test
  kept: TestImportOpenCodeVendorPresets FAIL, 15 lines "skipped: it has
  no base URL". Restored: ok.
- go test -tags nogui ./internal/provider/ with the known main reds
  skipped (#3632 TestUnreadLoginsWriteErrorsKeepCredentials, #3854
  TestFetchAsksASharedBaseOnce and two siblings): ok.
- gofmt -l internal/provider: clean. go vet ./internal/provider/ on
  darwin, GOOS=windows, GOOS=linux: ok.
Not tried: a real OpenCode auth.json with these vendors' keys, a
request on each imported provider, the GUI import picker; go test
./... (one suite per machine).
2026-10-28 03:10:46 +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",
"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
}