Files
magpie/internal/provider/import_opencode.go
T
yetoneandggbdpq 7ea0e047bc import: a Gemini-protocol relay's key comes in on the provider's Gemini endpoint (#2413)
CC Switch's and Alma's entries whose base speaks Gemini's own protocol
(GOOGLE_GEMINI_BASE_URL; Alma's gemini format, or a google type with a
base_url) were skipped as "magpie doesn't talk the Gemini protocol to
relays yet". That was true when the import was written (0b0f258c0), but
a custom provider has taken a Gemini base since #1346, as the editor,
the TUI (#2201) and import links (#2251) do.

endpoints carries the Gemini base now, and imported() checks it as it
does the other three (http(s), never magpie's own address); merge() and
firstURL() take it. The skip and its four translations go. An entry
with no base still comes in on Google's preset, a sign-in with no key is
still skipped, and presetAt still matches on chat/responses/anthropic,
so a Gemini-only entry stays a custom provider.

Review change (on top of ggbdpq's 79a2fc71): Gemini CLI's own
@google/genai (getRequestUrlInternal, read in gemini-cli 0.35.3's
bundle) puts the API version after GOOGLE_GEMINI_BASE_URL on every
request, always "v1beta". magpie's Gemini base holds the version, and
GeminiBase adds it only to a bare host, so a relay under a path
(https://relay/gemini) would have been asked at /gemini/models/… instead
of /gemini/v1beta/models/…. geminiCLIBase appends /v1beta to CC Switch's
value. Alma's base_url already holds it (its google provider defaults to
…/v1beta and asks ${base}/models, read in Alma.app's main bundle), so
Alma's is taken as it is. TestImportAlma gains a gemini-format row and a
google-type row with a base; before, only CC Switch was tested.

Run:
- TestImportCCSwitch and TestImportAlma with importapps.go and
  import_opencode.go put back to origin/main: FAIL ("gemini relay: …
  Skip:magpie doesn't talk…", "alma gemini relay: …"). With
  geminiCLIBase taken out: FAIL ("gemini relay: … Gemini:https://g.example.com").
  Restored: ok.
- TestImport*, TestGeminiURLIsItsHost: ok.
- Full suite, go test -tags nogui -timeout 45m ./... (set -o pipefail,
  sandbox HOME, load ~350) at 0224e826f (these four commits on
  ebf3f48f7) plus #2545's patch: every package ok except internal/agent,
  whose one red (TestRekeyGivesWSLAgentTheSharingKey) is #2545's, which
  was dropped (problems posted on #2545). internal/agent alone at
  0224e826f: ok. Rebased on dccfefdf7 (#2635, internal/library only):
  build, vet, internal/library ok. gofmt -l clean; go vet darwin (gui),
  GOOS=linux and GOOS=windows -tags nogui: clean. GUI (i18n-keys,
  gui-de/ja/zh-TW, import-*, key-import, login-import): all pass except
  login-import's Chromium case, which fails the same alone on origin/main
  ebf3f48f7 (#1307, output posted there).
Not tried: a real CC Switch or Alma database, or a real Gemini relay.

Co-authored-by: ggbdpq <ggbdpq@gmail.com>
2026-10-17 11:54:55 +08:00

87 lines
2.8 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.
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",
}
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
}