corddis_114 on Discord has a relay that gives out a key per protocol,
one of them for Gemini ("gemini sk-d…SarG"), and the key row's protocol
menu offered Any, Anthropic, Chat Completions and Responses only. A
custom provider already serves Gemini upstream (its Gemini URL, the
"Gemini compatible" tab, x-goog-api-key, the model list through
FetchGemini, probes), so only the per-key layer lacked it:
- keyProtocolOK takes "gemini"; WithKey leaves a Gemini key the Gemini
URL only and takes the Gemini URL from a key made for another
protocol (it had kept it, so an Anthropic key could be sent to
generateContent).
- keyFit: a gemini* model (modelFamily Gemini) goes to the key made for
Gemini first, the other keys in their order behind it; a GPT model is
never put on the Gemini key (it ranked 0 there before). Claude's rule
is unchanged.
- app.js: Gemini in PROTO_OPTS ("Gemini API (generateContent) · Gemini
CLI, Gemini models"), offered when the provider has a Gemini URL;
keyLacksURL says "No Gemini URL" for a Gemini key without one. The
note in zh, zh-TW, ja and de. The provider-level tabs and Routing's
"keys for Gemini" already had it.
Run on mote (go1.26.8):
- go test -timeout 45m ./internal/provider ./internal/gateway: both ok (provider 229s, gateway 938s).
- TestKeyMadeForGemini, TestKeyProtocolGemini, TestKeysMadeForOneProtocol,
TestKeyForAProtocolWithNoURLIsSaid, TestKeyPoolsByProtocol,
TestCustomGeminiUpstream ok; internal/gui's budget tests ok.
- GUI (node --test, Chromium and WebKit, en/zh/zh-TW/ja/de, 420 and
1000px): key-gemini (new, 40/40), key-nourl, proto-menu-overflow,
key-row-chips-fit, key-plan, key-import, key-weight-fit,
provider-many-keys, provider-gemini, provider-add-gemini,
provider-url-each-api, model-api-gemini, provider-base-api,
routing-keys-fold and i18n-keys ok; the only red in the 360 was
tencent-cloud's region-strip test (#4321, open).
- gofmt -l clean; go vet, GOOS=linux go vet -tags nogui, GOOS=windows
go vet on provider, gateway, gui ok.
Broken by hand: the old fallback.go turns TestKeyMadeForGemini red
(Gemini models went to the Chat or Anthropic key, GPT to the Gemini
one); the old keys.go turns TestKeyProtocolGemini red (unknown
protocol "gemini"), and WithKey keeping p.Gemini turns it red too (the
Anthropic key speaks [anthropic gemini]); the old app.js turns
key-gemini red in all 40.
Not tried: the reporter's relay or any real Gemini-compatible relay;
the real app's editor.
90 lines
4.2 KiB
Go
90 lines
4.2 KiB
Go
package gateway
|
|
|
|
import (
|
|
"bytes"
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
// A relay that gives out a key per protocol, Gemini's among them
|
|
// (corddis_114 on Discord): a Gemini model goes to the key made for the
|
|
// Gemini API, on generateContent, whatever the agent spoke, and Claude
|
|
// and GPT models never go there, though the Gemini key is the first.
|
|
func TestKeyMadeForGemini(t *testing.T) {
|
|
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
|
|
t.Setenv("XDG_CACHE_HOME", t.TempDir())
|
|
restingUntil.Lock()
|
|
restingUntil.m = map[string]time.Time{}
|
|
restingUntil.Unlock()
|
|
var seen []string
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
b, _ := io.ReadAll(r.Body)
|
|
key := r.Header.Get("x-api-key")
|
|
if key == "" {
|
|
key = r.Header.Get("x-goog-api-key")
|
|
}
|
|
if key == "" {
|
|
key = strings.TrimPrefix(r.Header.Get("Authorization"), "Bearer ")
|
|
}
|
|
seen = append(seen, r.URL.Path+" "+key)
|
|
gem := strings.HasPrefix(r.URL.Path, "/v1beta/models/")
|
|
want := map[string]string{"/v1/messages": "k-ant", "/v1/chat/completions": "k-oai"}[r.URL.Path]
|
|
if gem {
|
|
want = "k-gem"
|
|
}
|
|
if key != want {
|
|
w.WriteHeader(401)
|
|
io.WriteString(w, `{"error":{"message":"this key is for another protocol"}}`)
|
|
return
|
|
}
|
|
switch {
|
|
case gem:
|
|
w.Header().Set("Content-Type", "text/event-stream")
|
|
io.WriteString(w, sse(`data: {"candidates":[{"content":{"role":"model","parts":[{"text":"ok"}]},"finishReason":"STOP"}],"usageMetadata":{"promptTokenCount":1,"candidatesTokenCount":1,"totalTokenCount":2}}`))
|
|
case r.URL.Path == "/v1/messages":
|
|
w.Header().Set("Content-Type", "application/json")
|
|
io.WriteString(w, `{"id":"m1","type":"message","role":"assistant","model":"x","content":[{"type":"text","text":"ok"}],"stop_reason":"end_turn","usage":{"input_tokens":1,"output_tokens":1}}`)
|
|
case bytes.Contains(b, []byte(`"stream":true`)):
|
|
w.Header().Set("Content-Type", "text/event-stream")
|
|
io.WriteString(w, sse(
|
|
`data: {"id":"c1","object":"chat.completion.chunk","choices":[{"index":0,"delta":{"role":"assistant","content":"ok"}}]}`,
|
|
`data: {"id":"c1","object":"chat.completion.chunk","choices":[{"index":0,"delta":{},"finish_reason":"stop"}],"usage":{"prompt_tokens":1,"completion_tokens":1}}`,
|
|
`data: [DONE]`))
|
|
default:
|
|
w.Header().Set("Content-Type", "application/json")
|
|
io.WriteString(w, `{"id":"c1","object":"chat.completion","choices":[{"index":0,"message":{"role":"assistant","content":"ok"},"finish_reason":"stop"}],"usage":{"prompt_tokens":1,"completion_tokens":1}}`)
|
|
}
|
|
}))
|
|
defer srv.Close()
|
|
p := provider.Provider{ID: "relay", Name: "Relay", Chat: srv.URL + "/v1", Anthropic: srv.URL, Gemini: srv.URL + "/v1beta",
|
|
Key: "k-gem", KeyProtocol: provider.Gemini, Models: []string{"claude-opus-4-8", "gpt-5.5", "gemini-3-pro"},
|
|
Keys: []provider.KeyAccount{{Key: "k-ant", Protocol: provider.Anthropic}, {Key: "k-oai", Protocol: provider.Chat}}}
|
|
if err := provider.Save(p); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, x := range []struct{ path, body, want string }{
|
|
// Claude Code and an OpenAI agent asking for Gemini: the Gemini key
|
|
{"/v1/messages", `{"model":"relay/gemini-3-pro","max_tokens":9,"messages":[{"role":"user","content":"hi"}]}`, "/v1beta/models/gemini-3-pro:streamGenerateContent k-gem"},
|
|
{"/v1/chat/completions", `{"model":"relay/gemini-3-pro","messages":[{"role":"user","content":"hi"}]}`, "/v1beta/models/gemini-3-pro:streamGenerateContent k-gem"},
|
|
// Claude and GPT never go to the Gemini key, the first though it is
|
|
{"/v1/messages", `{"model":"relay/claude-opus-4-8","max_tokens":9,"messages":[{"role":"user","content":"hi"}]}`, "/v1/messages k-ant"},
|
|
{"/v1/messages", `{"model":"relay/gpt-5.5","max_tokens":9,"messages":[{"role":"user","content":"hi"}]}`, "/v1/chat/completions k-oai"},
|
|
{"/v1/chat/completions", `{"model":"relay/gpt-5.5","messages":[{"role":"user","content":"hi"}]}`, "/v1/chat/completions k-oai"},
|
|
} {
|
|
seen = nil
|
|
code, body := post(t, x.path, x.body)
|
|
if code != 200 || len(seen) != 1 || seen[0] != x.want {
|
|
t.Errorf("%s %s: %d %s, upstream saw %v", x.path, x.body, code, body, seen)
|
|
}
|
|
}
|
|
if got := modelFamily("google/gemini-2.5-flash"); got != provider.Gemini {
|
|
t.Errorf("gemini: %q", got)
|
|
}
|
|
}
|