Files
magpie/internal/provider/import_agy.go
T
yetone 1bfc6378c9 providers: Antigravity CLI's own Gemini API key is found and imported (shenggxhz on X)
shenggxhz set agy up on an AI Studio key: "modelProvider": "gemini" in
~/.gemini/antigravity-cli/settings.json, GEMINI_API_KEY exported in the
shell. `agy models` then listed Gemini 4 Carbon and Hydrogen. magpie's
model refresh didn't, because the agy-cli subscription runs agy on its
Google sign-in, whose models are the account's in Antigravity, and the
key was used nowhere in magpie.

The Providers page now finds that key on its own. A new import source,
"Antigravity CLI" (readAgySettings), reads agy's settings.json and offers
the key as the Google Gemini preset, which lists the key's models from
the Gemini API. With GOOGLE_GEMINI_BASE_URL set, it uses that endpoint's
Gemini API instead. The variables are taken from magpie's environment,
else from the login shell's: a key exported only in .zshrc is found when
magpie is opened from the Dock. proc.GeminiKeyVars are only read
(LoginEnv), never set in magpie's environment. agy on its sign-in offers
nothing. With no key the entry shows as not importable, and a key that
points at magpie itself is refused.

Not changed: the agy-cli plugin's own `agy models` reads the same
setting and, with no key in magpie's environment, fails and keeps the
old list silently. Filed as magpie-community/plugins#1218.

Run:
- red on the old code: with the appReaders entry removed, all four
  TestAgyGeminiKey* tests fail (no "agy" source); green with it.
- a real login shell on mica, in a sandbox HOME (key exported only in
  .zshrc, MAGPIE env without it): DiscoverSources gives status new,
  preset google, the key read.
- real agy 1.5.9 in a sandbox HOME: API-key mode lists the Gemini 4
  models from a list built into agy; without the key it exits 1 with the
  modelProvider error.
- go vet (darwin, GOOS=linux, GOOS=windows) for proc and provider, plus
  darwin internal/gui; gofmt -l clean.
- full `go test -timeout 45m ./...` on Linux (magpie-desktop): every
  package ok except internal/usage's TestRequestStateKeptAcrossACatalogFetchedAgain
  (#6572, filed, untouched here) and the root, internal/cli and
  internal/gui, which don't build there (no gtk4/webkitgtk-6.0 dev
  packages). Those three, plus provider and proc, ran on mote (darwin): ok.
  A first full run on mica died of a full disk (117G of shared Go build
  cache), so it tested nothing.
- GUI on Linux, Chromium + WebKit: icon-tile, import-collision,
  import-opencode, provider-discovery, import-row and i18n-keys pass,
  92 of 93. The one red is icon-tile's dim-color (#6759, red on main).
- appIcon became a table so the agy icon fits app.js's code budget
  without raising it (TestCoreScriptBudget ok).

Not tried: a real AI Studio key or a real Gemini request; the GUI import
sheet in a browser (the source shows through the existing discovery and
import sheets, with Antigravity's icon); Windows (LoginEnv is "" there,
so only magpie's own environment is read).
2026-11-16 17:59:26 +08:00

85 lines
2.7 KiB
Go

package provider
import (
"encoding/json"
"errors"
"os"
"path/filepath"
"strings"
"github.com/tidwall/jsonc"
"github.com/yetone/magpie/internal/edit"
"github.com/yetone/magpie/internal/proc"
)
// Antigravity's CLI, agy, on a Gemini API key of the user's own: with
// "modelProvider": "gemini" in ~/.gemini/antigravity-cli/settings.json it
// skips the Google sign-in and asks the Gemini API with GEMINI_API_KEY
// from its environment (GOOGLE_GEMINI_BASE_URL for another endpoint), and
// `agy models` lists the Gemini API's models, Gemini 4 Carbon and Hydrogen
// among them. magpie's agy-cli subscription runs agy on its Google
// sign-in, whose models are the account's in Antigravity, so that key is
// served nowhere in magpie (shenggxhz on X: agy listed Gemini 4, magpie's
// refresh didn't). The key is a Gemini API key, and the Providers page
// offers it as the Google Gemini provider, which serves those models.
//
// The variables are agy's environment: magpie's own, else the login
// shell's (proc.GeminiKeyVars), as a terminal's agy has them. They are
// only read.
// agyLoginEnv is what a terminal opened now has for a variable (a var for
// tests).
var agyLoginEnv = proc.LoginEnv
func agySettingsPath() string {
home, _ := os.UserHomeDir()
return filepath.Join(home, ".gemini", "antigravity-cli", "settings.json")
}
// agyEnv is a variable as agy started in a terminal has it.
func agyEnv(k string) string {
if v := strings.TrimSpace(os.Getenv(k)); v != "" {
return v
}
return strings.TrimSpace(agyLoginEnv(k))
}
func readAgySettings(path string) ([]AppImport, error) {
raw, err := edit.Read(path)
if err != nil {
return nil, err
}
if len(strings.TrimSpace(string(raw))) == 0 {
return nil, nil
}
var s map[string]any
if err := json.Unmarshal(jsonc.ToJSON(edit.TrimBOM(raw)), &s); err != nil {
return nil, errors.New("agy's settings.json: " + err.Error())
}
// agy on its Google sign-in: the agy-cli subscription's
if mp, _ := s["modelProvider"].(string); !strings.EqualFold(strings.TrimSpace(mp), "gemini") {
return nil, nil
}
it := AppImport{Ref: "gemini-api-key", From: "settings.json"}
var eps endpoints
if base := agyEnv("GOOGLE_GEMINI_BASE_URL"); base != "" {
eps.gemini = geminiCLIBase(base)
} else {
pr := Preset("google")
eps.chat, eps.responses, eps.anthropic = pr.Chat, pr.Responses, pr.Anthropic
}
key := agyEnv("GEMINI_API_KEY")
if key == "" {
// GEMINI_API_KEY isn't set: agy itself refuses to start
it.Provider, it.Skip = Provider{Name: "Google Gemini"}, "it has no API key"
return []AppImport{it}, nil
}
p, skip := imported("Google Gemini", key, eps, nil)
if skip != "" {
it.Provider, it.Skip = Provider{Name: "Google Gemini"}, skip
return []AppImport{it}, nil
}
it.Provider = p
return []AppImport{it}, nil
}