Commit Graph
3 Commits
Author SHA1 Message Date
yetone 253aee08b4 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.
2026-11-03 02:37:56 +08:00
yetone 3e61d814e5 gateway: an AI Studio key speaks the Gemini API too, and searches by googleSearch
cocoasoda (Discord) asked for a native Gemini API mode on the Gemini API
provider, so it can search with Google Search on the API credit a Gemini
subscription comes with.

The Google Gemini preset (and any provider whose Chat URL is AI Studio's
OpenAI-compatible one) now gets AI Studio's Gemini API base,
https://generativelanguage.googleapis.com/v1beta, at the same key, filled
in by normalize, so existing providers get it on load. A Gemini URL of the
provider's own is kept; other hosts, Vertex and sign-ins get nothing.
Ordinary turns stay on Chat as before. What changes:

- A Gemini client (Gemini CLI) is relayed natively, its googleSearch tool
  included, instead of being translated onto Chat.
- A web search with no tools of the client's own (Claude Code's
  WebSearch) goes to the Gemini API with tools [{"googleSearch":{}}], and
  the grounding comes back as the client's search result.
- A turn with function tools stays on Chat: Gemini doesn't take
  googleSearch beside function declarations, so before this the switch to
  Gemini dropped the search silently. Its model is given magpie's search
  tool, and the same key answers it by a tool-less googleSearch request
  (searchesOwn / ownSearcher, as Google sign-ins do since #757), on the
  user's own credit, not another provider's.
- The model list isn't asked twice: AI Studio's OpenAI-compatible list is
  its Gemini API's list.

Run on mote at this change on 2c90922b0 (pushed on 898c314b7, which adds
GUI files and a WorkBuddy mover min only): gofmt -l clean;
GOOS=windows go vet ./... ok; GOOS=linux go vet -tags nogui ./... ok;
go test ./internal/provider ok (194s). The whole ./internal/gateway run
was still going under other sessions' suites when this was pushed. The
new gateway tests (below), TestCodeAssistSearchesWithoutTools,
TestCustomGeminiUpstream and TestAIStudioThinking passed at the same
code before the rebase, which brought in only GUI and provider-pick
commits.

New tests: TestAIStudioSpeaksGemini, TestAIStudioKeySearchesByGoogleSearch,
TestAIStudioKeySearchesBesideTools, TestAIStudioKeyRelaysGeminiClient,
TestGeminiKeySearchesScope. The upstream is a fake transport for
generativelanguage.googleapis.com with Google's grounding example's shape
(webSearchQueries, groundingChunks, groundingSupports); no network.

Broken by hand, each red:
- normalize without aiStudioNative: TestAIStudioSpeaksGemini and all three
  gateway tests (Chat asked, 404 on the Gemini client).
- searchesFor without the AI Studio branch: BesideTools (the turn went
  to Gemini, where googleSearch would be dropped) and Scope.
- searchesOwn without geminiKeySearches: BesideTools (the search tool
  call went back to the client unanswered).

Not tried: a real AI Studio key (none here), so Google's live replies are
from its docs, not seen. Gemini 3's googleSearch beside function tools
(includeServerSideToolInvocations) is not used. The derived Gemini URL
can't be cleared by the user while the Chat URL is AI Studio's; normalize
fills it again.
2026-11-03 02:21:35 +08:00
yetone 844b9ca3ac provider, gateway, gui: a custom provider's Base URL can be a Gemini API (generateContent): …/v1beta, its models listed from it, the key sent in x-goog-api-key (#1346, NagaseMinato)
The custom provider editor's Base URL has a fifth API, "Gemini compatible"
(Gemini 兼容 / Gemini 互換 / Gemini-kompatibel), beside OpenAI, Anthropic and
Responses; More endpoints has a Gemini URL; the CLI takes gemini=. A
Gemini URL is kept as its base (…/v1beta, a relay's path prefix kept, a
pasted models/…:generateContent cut off). Every client's request, Chat,
Messages, Responses or Gemini, streamed or not, goes upstream to
models/{model}:streamGenerateContent?alt=sse under it, with no model in
the body and the key in x-goog-api-key; models are listed from its
/models, a page at a time, keeping those that serve generateContent.
Detect doesn't ask a Gemini API and is hidden while it is picked.
Factory's Gemini route and Bearer token are unchanged.

With five APIs the pick no longer fits a 900px editor in English and
German, so it wraps to a second row, and its thumb now follows the
picked option onto that row.

Verified against fake httptest upstreams only; no real Gemini key or
relay was tried:
- TestCustomGeminiUpstream (gateway): chat, chat stream, messages, a
  Gemini client's stream and non-stream, behind /proxy/v1beta. Fails with
  "chat: 404" without the gateway.go change.
- TestCustomGeminiProvider, TestGeminiBase, TestFactoryGeminiUnchanged
  (provider).
- go vet, HOME=$(mktemp -d) go test -tags nogui ./... (44 packages ok),
  GOOS=linux and GOOS=windows go vet on the touched packages.
- provider-gemini.test.cjs (new, en/zh/ja/de, Chromium + WebKit, 8 pass;
  fails with "900px: the thumb is 22px off" without the slide() fix),
  and in both engines: provider-base-api, provider-detect,
  provider-detect-models, provider-add-models, provider-output-picks,
  provider-search, provider-url-each-api, seg-contrast, settings-groups,
  menu-scroll, click-scroll, ledger-hscroll, strip-scrollbar,
  view-scrollbar, usage-ledger-chart, system-one, gui-ja, gui-de,
  gui-zh-tw: all pass.
2026-10-09 01:10:25 +08:00