provider: an AI Studio key's Gemini URL is filled in once, and stays empty when the user clears it

3e61d814e gave a provider at AI Studio's OpenAI-compatible URL AI
Studio's Gemini API base in normalize, which runs on every load and
save, so a user who cleared that URL got it back each time: magpie's
default overrode the user's value (code standards).

Now it is filled in once and recorded (GeminiFilled, geminiFilled in
providers.json). A URL cleared after that stays empty, from the CLI
(`magpie provider set <id> gemini=`), by hand in providers.json, or in
the editor: the form doesn't send geminiFilled, so its save carries it
over from the saved provider, as it does the other fields the form
doesn't show. Providers that already got the URL from 3e61d814e have
no record yet; their first load records it, the URL unchanged, so a
clear after that holds.

Tests: TestAIStudioGeminiClearedStays (provider: clear, then two loads
and saves), TestSaveKeepsGeminiCleared (gui: /api/provider/save with
the URL cleared). Broken by hand: the old fill (no GeminiFilled check)
turns both red, the URL back each time; the editor's save without the
carry-over turns TestSaveKeepsGeminiCleared red.

Run on mote at this change on 2ab923571: gofmt -l clean; GOOS=linux go
vet -tags nogui ./internal/... ok; GOOS=windows go vet ./internal/... ok;
go test ./internal/provider ok; go test ./internal/gui ok except
TestMainWindowCorners/macOS_26_SDK, #3949 (macOS 27 AppKit, untouched
here); go test ./internal/gateway -run Gemini/AIStudio ok.

Not tried: the editor itself in a browser (no JS change; the field and
its save were already there). A user who cleared the URL in the hours
between 3e61d814e and this commit got it back then; clearing it again
now holds.
This commit is contained in:
yetone
2026-11-03 02:37:56 +08:00
parent 1490045f59
commit 253aee08b4
5 changed files with 98 additions and 4 deletions
+42
View File
@@ -0,0 +1,42 @@
package gui
import (
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/yetone/magpie/internal/provider"
)
// The editor doesn't send that magpie filled an AI Studio key's Gemini URL
// in (GeminiFilled), so its save keeps it: the URL cleared in the form
// stays cleared, and isn't filled in again.
func TestSaveKeepsGeminiCleared(t *testing.T) {
home := t.TempDir()
t.Setenv("HOME", home)
t.Setenv("USERPROFILE", home)
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
t.Setenv("XDG_CACHE_HOME", t.TempDir())
if err := provider.Save(provider.Provider{ID: "google", Name: "Google Gemini", Preset: "google", Chat: provider.AIStudioChat, Key: "AIza-k"}); err != nil {
t.Fatal(err)
}
if p, _ := provider.Find("google"); p == nil || p.Gemini != provider.AIStudioGemini {
t.Fatalf("not filled in: %+v", p)
}
mux := http.NewServeMux()
providerRoutes(mux, nil)
w := httptest.NewRecorder()
mux.ServeHTTP(w, httptest.NewRequest("POST", "/api/provider/save", strings.NewReader(
`{"id":"google","name":"Google Gemini","preset":"google","chat":"`+provider.AIStudioChat+`","gemini":""}`)))
if w.Code != 200 {
t.Fatalf("%d %s", w.Code, w.Body)
}
got, err := provider.Find("google")
if err != nil {
t.Fatal(err)
}
if got.Gemini != "" || got.Key != "AIza-k" {
t.Fatalf("after clearing it in the editor: gemini %q key %q", got.Gemini, got.Key)
}
}
+5
View File
@@ -1799,6 +1799,11 @@ func providerRoutes(mux *http.ServeMux, w Windows) {
if !in.PromptAsWritten && old != nil {
in.PromptAsWritten = old.PromptAsWritten
}
// and that magpie filled the Gemini URL in once, which the
// form doesn't send: a URL cleared in it stays cleared
if !in.GeminiFilled && old != nil {
in.GeminiFilled = old.GeminiFilled
}
// and additional_tools taken, set with `magpie provider
// set <id> additional-tools=on` (#2660)
if !in.AdditionalTools && old != nil {
+9 -4
View File
@@ -56,11 +56,16 @@ const AIStudioGemini = "https://generativelanguage.googleapis.com/v1beta"
// place with the same key (cocoasoda on Discord): Gemini's own clients are
// relayed there as they ask, and googleSearch, which the OpenAI-compatible
// API has no tool for, is run there. Its other requests stay on chat
// completions, which Speaks names first. A Gemini URL of the provider's
// own is kept.
// completions, which Speaks names first. It is filled in once
// (GeminiFilled): a Gemini URL of the provider's own is kept, and one the
// user cleared stays empty.
func (p *Provider) aiStudioNative() {
if p.Gemini == "" && p.Account == nil && strings.EqualFold(p.Chat, AIStudioChat) {
p.Gemini = AIStudioGemini
if p.GeminiFilled || p.Account != nil || !strings.EqualFold(p.Chat, AIStudioChat) {
return
}
// one filled in before it was recorded is recorded as it is
if p.Gemini == "" || strings.EqualFold(p.Gemini, AIStudioGemini) {
p.Gemini, p.GeminiFilled = AIStudioGemini, true
}
}
+38
View File
@@ -145,3 +145,41 @@ func TestAIStudioSpeaksGemini(t *testing.T) {
}
}
}
// The Gemini URL magpie gives an AI Studio key is filled in once: one the
// user clears stays empty, on every load and save after.
func TestAIStudioGeminiClearedStays(t *testing.T) {
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
t.Setenv("XDG_CACHE_HOME", t.TempDir())
if err := Save(Provider{ID: "google", Name: "Google Gemini", Preset: "google", Chat: AIStudioChat, Key: "AIza-k"}); err != nil {
t.Fatal(err)
}
p, err := Find("google")
if err != nil || p.Gemini != AIStudioGemini || !p.GeminiFilled {
t.Fatalf("filled: %+v %v", p, err)
}
// one 3e61d814e filled in, before GeminiFilled, is recorded too
p.GeminiFilled = false
if err := Save(*p); err != nil {
t.Fatal(err)
}
if p, err = Find("google"); err != nil || p.Gemini != AIStudioGemini || !p.GeminiFilled {
t.Fatalf("an earlier fill isn't recorded: %+v %v", p, err)
}
p.Gemini = ""
if err := Save(*p); err != nil {
t.Fatal(err)
}
for range 2 {
got, err := Find("google")
if err != nil {
t.Fatal(err)
}
if got.Gemini != "" || slices.Contains(got.Speaks(), Gemini) {
t.Fatalf("the cleared Gemini URL came back: %q", got.Gemini)
}
if err := Save(*got); err != nil {
t.Fatal(err)
}
}
}
+4
View File
@@ -99,6 +99,10 @@ type Provider struct {
// under it, with the key in x-goog-api-key, and models under it lists
// them (#1346).
Gemini string `json:"gemini,omitempty"`
// GeminiFilled is that magpie filled Gemini in once, for a provider
// at AI Studio's OpenAI-compatible URL (aiStudioNative): a Gemini URL
// the user cleared after that stays empty.
GeminiFilled bool `json:"geminiFilled,omitempty"`
// Decide is the base of a decision API (TypeSafe's System One, which
// Jev answers), for routing groups' choices of model and effort. The
// provider may also serve conversations on the other endpoints.