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>
This commit is contained in:
yetone
2026-10-17 11:54:55 +08:00
committed by yetone
co-authored by ggbdpq
parent dccfefdf72
commit 7ea0e047bc
4 changed files with 58 additions and 24 deletions
-4
View File
@@ -1780,7 +1780,6 @@ const I18N = {
"it is the agent's own sign-in; add the subscription in magpie": "这是 agent 自己的登录,请在 magpie 里添加订阅",
"it is the agent's own sign-in": "这是 agent 自己的登录",
"it is a sign-in, not a key; add the subscription in magpie": "这是登录账号而不是密钥,请在 magpie 里添加订阅",
"magpie doesn't talk the Gemini protocol to relays yet": "magpie 暂不支持以 Gemini 协议连接中转",
"it has no Azure OpenAI endpoint": "没有 Azure OpenAI 终结点",
"it points at magpie itself": "它指向的就是 magpie 自己",
"it has no base URL": "没有 Base URL",
@@ -5466,7 +5465,6 @@ const I18N = {
"it is the agent's own sign-in; add the subscription in magpie": "這是 agent 自己的登入,請在 magpie 裡新增訂閱",
"it is the agent's own sign-in": "這是 agent 自己的登入",
"it is a sign-in, not a key; add the subscription in magpie": "這是登入帳號而不是金鑰,請在 magpie 裡新增訂閱",
"magpie doesn't talk the Gemini protocol to relays yet": "magpie 暫不支援以 Gemini 協議連線中轉",
"it has no Azure OpenAI endpoint": "沒有 Azure OpenAI 端點",
"it points at magpie itself": "它指向的就是 magpie 自己",
"it has no base URL": "沒有 Base URL",
@@ -9129,7 +9127,6 @@ const I18N = {
"it is the agent's own sign-in; add the subscription in magpie": "エージェント自身のサインインです。magpie でサブスクリプションを追加してください",
"it is the agent's own sign-in": "エージェント自身のサインインです",
"it is a sign-in, not a key; add the subscription in magpie": "キーではなくサインインです。magpie でサブスクリプションを追加してください",
"magpie doesn't talk the Gemini protocol to relays yet": "magpie はまだ中継サービスと Gemini プロトコルで通信できません",
"it has no Azure OpenAI endpoint": "Azure OpenAI のエンドポイントがありません",
"it points at magpie itself": "magpie 自身を指しています",
"it has no base URL": "ベース URL がありません",
@@ -12822,7 +12819,6 @@ const I18N = {
"it is the agent's own sign-in; add the subscription in magpie": "es ist die eigene Anmeldung des Agenten; fügen Sie das Abo in magpie hinzu",
"it is the agent's own sign-in": "es ist die eigene Anmeldung des Agenten",
"it is a sign-in, not a key; add the subscription in magpie": "es ist eine Anmeldung, kein Schlüssel; fügen Sie das Abo in magpie hinzu",
"magpie doesn't talk the Gemini protocol to relays yet": "magpie spricht mit Relays noch nicht das Gemini-Protokoll",
"it has no Azure OpenAI endpoint": "es hat keinen Azure-OpenAI-Endpunkt",
"it points at magpie itself": "es zeigt auf magpie selbst",
"it has no base URL": "es hat keine Basis-URL",
+1 -1
View File
@@ -75,7 +75,7 @@ func readOpenCodeAuth(path string) ([]AppImport, error) {
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)
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
+35 -16
View File
@@ -354,15 +354,15 @@ func hostOf(raw string) string {
}
// endpoints are the base URLs an entry names, by protocol.
type endpoints struct{ chat, responses, anthropic string }
type endpoints struct{ chat, responses, anthropic, gemini string }
// imported builds the provider for an entry: a preset's when the entry
// points at one magpie knows (its endpoints whole when the URL is the
// preset's own, only its name and logo when it is another path on the same
// host, like a coding plan), a custom one otherwise.
func imported(name, key string, e endpoints, models []string) (Provider, string) {
e.chat, e.responses, e.anthropic = cleanBase(e.chat), cleanBase(e.responses), cleanBase(e.anthropic)
for _, u := range []string{e.chat, e.responses, e.anthropic} {
e.chat, e.responses, e.anthropic, e.gemini = cleanBase(e.chat), cleanBase(e.responses), cleanBase(e.anthropic), cleanBase(e.gemini)
for _, u := range []string{e.chat, e.responses, e.anthropic, e.gemini} {
if u == "" {
continue
}
@@ -374,10 +374,10 @@ func imported(name, key string, e endpoints, models []string) (Provider, string)
return Provider{}, "it points at magpie itself"
}
}
if e.chat == "" && e.responses == "" && e.anthropic == "" {
if e.chat == "" && e.responses == "" && e.anthropic == "" && e.gemini == "" {
return Provider{}, "it has no base URL"
}
p := Provider{ID: Slug(name), Name: name, Key: key, Chat: e.chat, Responses: e.responses, Anthropic: e.anthropic, Models: models}
p := Provider{ID: Slug(name), Name: name, Key: key, Chat: e.chat, Responses: e.responses, Anthropic: e.anthropic, Gemini: e.gemini, Models: models}
if p.IsAzure() {
// an Azure OpenAI resource, from any app: the preset's, its
// endpoints on the resource's v1 API
@@ -651,7 +651,7 @@ func merge(have []AppImport, it AppImport) []AppImport {
if have[i].Skip != "" || h.Key != it.Provider.Key || h.Key == "" || !sameProvider(*h, it.Provider) && hostOf(firstURL(*h)) != hostOf(firstURL(it.Provider)) {
continue
}
for _, f := range []struct{ dst, src *string }{{&h.Chat, &it.Provider.Chat}, {&h.Responses, &it.Provider.Responses}, {&h.Anthropic, &it.Provider.Anthropic}} {
for _, f := range []struct{ dst, src *string }{{&h.Chat, &it.Provider.Chat}, {&h.Responses, &it.Provider.Responses}, {&h.Anthropic, &it.Provider.Anthropic}, {&h.Gemini, &it.Provider.Gemini}} {
if *f.dst == "" {
*f.dst = *f.src
}
@@ -664,7 +664,7 @@ func merge(have []AppImport, it AppImport) []AppImport {
}
func firstURL(p Provider) string {
for _, u := range []string{p.Chat, p.Responses, p.Anthropic} {
for _, u := range []string{p.Chat, p.Responses, p.Anthropic, p.Gemini} {
if u != "" {
return u
}
@@ -730,14 +730,18 @@ func ccSwitchEntry(e ccEntry) (name, key string, eps endpoints, models []string,
}
case "gemini":
key = str(env, "GEMINI_API_KEY", "GOOGLE_API_KEY")
if str(env, "GOOGLE_GEMINI_BASE_URL") != "" {
return name, "", eps, nil, "magpie doesn't talk the Gemini protocol to relays yet"
// a relay that speaks Gemini's own protocol (#1346): its base is
// the provider's Gemini endpoint, as the editor, the TUI and an
// import link take one
if base := str(env, "GOOGLE_GEMINI_BASE_URL"); base != "" {
eps.gemini = geminiCLIBase(base)
} else {
if key == "" {
return name, "", eps, nil, "it is the agent's own sign-in"
}
pr := Preset("google")
eps.chat, eps.responses, eps.anthropic = pr.Chat, pr.Responses, pr.Anthropic
}
if key == "" {
return name, "", eps, nil, "it is the agent's own sign-in"
}
pr := Preset("google")
eps.chat, eps.responses, eps.anthropic = pr.Chat, pr.Responses, pr.Anthropic
if m := str(env, "GEMINI_MODEL"); m != "" {
models = append(models, m)
}
@@ -783,6 +787,19 @@ func ccSwitchEntry(e ccEntry) (name, key string, eps endpoints, models []string,
return name, key, eps, models, ""
}
// geminiCLIBase is magpie's Gemini base for Gemini CLI's
// GOOGLE_GEMINI_BASE_URL. The CLI (@google/genai's getRequestUrlInternal)
// puts the API version, v1beta, after that base on every request, where
// magpie's Gemini base already holds it: a relay at
// https://relay.example.com/gemini is asked at …/gemini/v1beta/models/….
func geminiCLIBase(u string) string {
u = strings.TrimRight(strings.TrimSpace(u), "/")
if u == "" || strings.HasSuffix(u, "/v1beta") {
return u
}
return u + "/v1beta"
}
func mapOf(v any) map[string]any {
m, _ := v.(map[string]any)
if m == nil {
@@ -884,11 +901,13 @@ func readAlma(path string) ([]AppImport, error) {
}
case base == "" && Preset(almaPresets[typ]) != nil:
pr := Preset(almaPresets[typ])
eps = endpoints{pr.Chat, pr.Responses, pr.Anthropic}
eps = endpoints{pr.Chat, pr.Responses, pr.Anthropic, ""}
case format == "anthropic" || typ == "anthropic":
eps.anthropic = base
case format == "gemini" || typ == "google" && base != "":
skip = "magpie doesn't talk the Gemini protocol to relays yet"
// a relay that speaks Gemini's own protocol (#1346): its base
// is the provider's Gemini endpoint
eps.gemini = base
case responses || format == "openai-responses":
eps.responses = base
default:
+22 -3
View File
@@ -60,7 +60,8 @@ func TestImportCCSwitch(t *testing.T) {
`INSERT INTO providers VALUES ('relay','codex','Some Relay','{"auth":{"OPENAI_API_KEY":"sk-relay"},"config":"model_provider = \"relay\"\nmodel = \"gpt-5.5\"\n\n[model_providers.relay]\nname = \"relay\"\nbase_url = \"https://relay.example.com/v1\"\nwire_api = \"responses\"\n"}','','custom',3,0)`,
`INSERT INTO providers VALUES ('ds','claude','DeepSeek','{"env":{"ANTHROPIC_BASE_URL":"https://api.deepseek.com/anthropic","ANTHROPIC_AUTH_TOKEN":"sk-ds"}}','','cn_official',4,2)`,
`INSERT INTO providers VALUES ('dial','pi','dial','{"api":"openai-completions","apiKey":"magpie","baseUrl":"http://127.0.0.1:3425/v1"}','','custom',5,0)`,
`INSERT INTO providers VALUES ('g','gemini','Gemini relay','{"env":{"GOOGLE_GEMINI_BASE_URL":"https://g.example.com","GEMINI_API_KEY":"k"}}','','custom',6,0)`,
`INSERT INTO providers VALUES ('g','gemini','Gemini relay','{"env":{"GOOGLE_GEMINI_BASE_URL":"https://g.example.com","GEMINI_API_KEY":"k","GEMINI_MODEL":"gemini-3.8-flash"}}','','custom',6,0)`,
`INSERT INTO providers VALUES ('g2','gemini','Gemini relay under a path','{"env":{"GOOGLE_GEMINI_BASE_URL":"https://g2.example.com/gemini/","GEMINI_API_KEY":"k2"}}','','custom',7,0)`,
)
// DeepSeek is already here under its preset id, with another key
if err := Save(FromPresetKey(t, "deepseek", "sk-other")); err != nil {
@@ -74,8 +75,16 @@ func TestImportCCSwitch(t *testing.T) {
if it := items["pi/dial"]; it.Skip == "" {
t.Fatalf("magpie itself offered: %+v", it)
}
if it := items["gemini/g"]; it.Skip == "" {
t.Fatalf("gemini relay offered: %+v", it)
g := items["gemini/g"]
// a Gemini-protocol relay (#1346): the base goes to the provider's own
// Gemini endpoint, as the editor, the TUI and import links take one
// Gemini CLI puts v1beta after GOOGLE_GEMINI_BASE_URL on every request,
// so the base magpie asks at is that one with v1beta, under a path too
if g.Skip != "" || g.Provider.Gemini != "https://g.example.com/v1beta" || g.Provider.Key != "k" || !slices.Equal(g.Provider.Models, []string{"gemini-3.8-flash"}) {
t.Fatalf("gemini relay: %+v", g)
}
if g2 := items["gemini/g2"]; g2.Skip != "" || g2.Provider.Gemini != "https://g2.example.com/gemini/v1beta" || g2.Provider.Key != "k2" {
t.Fatalf("gemini relay under a path: %+v", g2)
}
relay := items["claude/relay"]
if relay.Skip != "" || relay.Status != "new" || relay.Provider.Anthropic != "https://relay.example.com" || relay.Provider.Responses != "https://relay.example.com/v1" || relay.From != "Claude Code, Codex" {
@@ -122,6 +131,8 @@ func TestImportAlma(t *testing.T) {
`INSERT INTO providers VALUES ('a2','My Proxy','custom','sk-p','[]','https://proxy.example.com/v1',0,'2','openai-chat',0,'{"X-Team":"a"}')`,
`INSERT INTO providers VALUES ('a3','Copilot','copilot','','[]',NULL,1,'3',NULL,0,NULL)`,
`INSERT INTO providers VALUES ('a4','junk','custom','x','[]','bbb',0,'4',NULL,0,NULL)`,
`INSERT INTO providers VALUES ('a5','Gemini relay','custom','sk-g','["gemini-3.8-flash"]','https://ag.example.com/v1beta',1,'5','gemini',0,NULL)`,
`INSERT INTO providers VALUES ('a6','Google here','google','sk-gg','[]','https://gg.example.com/api/v1beta',1,'6',NULL,0,NULL)`,
)
items := itemsOf(t, "alma")
if it := items["a1"]; it.Provider.Preset != "openrouter" || it.Provider.Key != "sk-or" || len(it.Provider.Models) != 1 {
@@ -133,6 +144,14 @@ func TestImportAlma(t *testing.T) {
if items["a3"].Skip == "" || items["a4"].Skip == "" {
t.Fatalf("sign-in or junk offered: %+v %+v", items["a3"], items["a4"])
}
// a Gemini-protocol relay (#2413): Alma asks at its base_url with the
// version in it, as magpie's Gemini base is
if it := items["a5"]; it.Skip != "" || it.Provider.Gemini != "https://ag.example.com/v1beta" || it.Provider.Key != "sk-g" || it.Provider.Chat != "" {
t.Fatalf("alma gemini relay: %+v", it)
}
if it := items["a6"]; it.Skip != "" || it.Provider.Gemini != "https://gg.example.com/api/v1beta" || it.Provider.Key != "sk-gg" {
t.Fatalf("alma google type at a base: %+v", it)
}
}
func TestImportCodexConfig(t *testing.T) {