Files
magpie/internal/agent/antigravity.go
T
yetone 7c712847b7 agent: Antigravity's desktop app is a row, wired through its gateway setting (ryougi_hachi on X)
Antigravity (Google's desktop app, 2.15 and later; not Antigravity IDE)
takes a gateway from ~/.antigravity/settings.json: Google's Antigravity
Enterprise Gateway, documented at
docs.cloud.google.com/gemini/enterprise/docs/ai-developer-tools/connect-enterprise-gateway.
magpie writes gateway {enabled, url: magpie's, apiKey: magpie-antigravity,
wireProtocol: openai} and models.defaultModelId, and takes out models.list
(it would narrow the picker). The user's gateway, default model and list
are stashed and put back on disconnect or when a model of their own is
picked; caCertPath and the rest of the file are untouched. Sync keeps what
the user added to magpie's block, and so does a set again (a moved port,
Reconnect). Check speaks when admin_settings.json or AGY_LLM_GATEWAY_URL
wins over the file, and when the block's url or key is no longer magpie's
(wiringOff), so drift and a port move see it. AGY_LLM_GATEWAY_URL joins
agentenv.Vars, cleared in every sandbox. Found by its bundle
(com.google.antigravity, 2.15+) or the file its backend writes
(~/.gemini/antigravity/agyhub_summaries_proto.pb); ~/.antigravity alone is
the IDE's and agy's too. The notices are translated (zh, zh-TW, ja, de).
`magpie antigravity` now names the app; agy stays `magpie agy` /
`antigravity-cli`.

Run (worktree base 647323409, on mote):
- Real app backend in a sandbox HOME: Antigravity.app 2.28.2's
  language_server (hub), wired by this branch's magpie built from the
  worktree (`magpie antigravity magpie/rec/pro`, MAGPIE_ADDR on a port of
  its own, a recording upstream as a custom provider).
  GetCascadeModelConfigData listed rec/pro from magpie's /v1/models; a
  cascade ("Say hi") went app -> magpie -> upstream as
  /v1/chat/completions (stream, 12 tools) and its answer came back into
  the trajectory. `magpie usage` counted both calls to Antigravity.
  `magpie antigravity default` gave back the user's genai gateway, model
  and list byte for byte (JSON-equal).
- Before that the same file was checked with the real agy CLI 1.4.12.
- go vet (darwin, GOOS=linux, GOOS=windows) for internal/agent; gofmt.
- go test -tags nogui -count=1 ./internal/agent ./internal/agentenv
  ./internal/gui ./internal/usage ./internal/cli: all ok at this tree.
  The first run was red in four guard tests (TestFolderVarsAreListed,
  TestDriftUnwiredEveryAgent/antigravity, TestReverseProxyIsStillMagpie,
  TestPortMovesEveryAgent): Check had no wiringOff and the variable
  wasn't listed. Both fixed here.
- GUI (node --test, Chromium + WebKit): i18n-keys, agent-notice-lang,
  about-notices, 46 of 46 ok.

Broken by hand: renamed the gateway's stash key and dropped the
models.list deletion: TestAntigravityGateway, TestAntigravityGatewayFromNothing
and TestAntigravityOwnModelBack each went red; the desktopApps entry
missing made TestAntigravityDetected red; writing magpie's block whole on a set
again made TestAntigravitySync red (the user's customHeaders lost). The new tests are red on the
old code (no such agent).

Not tried: the Electron window itself (the backend was driven over its
RPCs, headless); the app on Windows or Linux (its install paths there
aren't known; the backend's file finds it); whether Google sign-in comes
back on its own after disconnect; the full ./... suite, because another
session's full suite was running on mote.
2026-11-01 16:49:55 +08:00

212 lines
7.9 KiB
Go

package agent
// Antigravity, Google's desktop app (2.15 and later), sends its requests to
// a gateway of the user's instead of Google when ~/.antigravity/settings.json
// turns one on: Google's Antigravity Enterprise Gateway, documented at
// docs.cloud.google.com/gemini/enterprise/docs/ai-developer-tools/connect-enterprise-gateway
// (ryougi_hachi on X):
//
// {"gateway":{"enabled":true,"url":…,"apiKey":…,"wireProtocol":"openai"},
// "models":{"defaultModelId":…,"list":[{"modelId":…}]}}
//
// On the openai wire protocol the app asks <url>/v1/models for its model
// picker (models.list, when there is one, narrows it) and sends a chat to
// <url>/v1/chat/completions, streamed, with apiKey as a bearer token and a
// User-Agent of antigravity/hub/<version> (auth_method=gateway). magpie's
// gateway answers both, so what magpie writes is that block at its own
// address, with a key that names the app, and the model the app starts on.
// The user's own gateway, default model and model list are stashed and put
// back when magpie steps out. On a gateway the app asks for no Google
// sign-in and uses none: Google's docs say the two can't be used at once.
//
// The same file is read by agy (Antigravity's CLI) too, below its own
// ~/.gemini/antigravity-cli/settings.json: an agy connected on its own row
// (modelProvider gemini) keeps that route, one that isn't follows the app's
// gateway. admin_settings.json, which an organisation installs, and the
// AGY_LLM_GATEWAY_* variables win over this file, field by field.
import (
"encoding/json"
"os"
"path/filepath"
"runtime"
"strings"
"github.com/yetone/magpie/internal/edit"
"github.com/yetone/magpie/internal/gateway"
"github.com/yetone/magpie/internal/proc"
)
const (
antigravityModel = "models.defaultModelId"
antigravityList = "models.list"
)
// antigravityAdmin is where an organisation's admin_settings.json is kept
// on this system (Google's docs), whose gateway outranks the user's; under
// a test's home in a test binary (proc.SystemDirs).
func antigravityAdmin() string {
p := "/etc/antigravity/admin_settings.json"
switch runtime.GOOS {
case "darwin":
p = "/Library/Application Support/Antigravity/admin_settings.json"
case "windows":
d := os.Getenv("ProgramData")
if d == "" {
return ""
}
p = filepath.Join(d, "Antigravity", "admin_settings.json")
}
return proc.SystemDirs(p)[0]
}
// antigravityAdminPath is antigravityAdmin, swapped by a test.
var antigravityAdminPath = antigravityAdmin
func antigravity(home string) *Agent {
dir := filepath.Join(home, ".antigravity")
path := filepath.Join(dir, "settings.json")
model := jsonGet(path, antigravityModel)
stashGateway, stashModel, stashList := "antigravity:"+path+":gateway", "antigravity:"+path+":defaultModelId", "antigravity:"+path+":list"
// wired: the file's gateway is on and carries a key magpie gave it
wired := func() bool {
on, _ := edit.GetJSON(path, "gateway.enabled")
key, _ := edit.GetJSON(path, "gateway.apiKey")
return on == "true" && gatewayKey(key)
}
gatewayBlock := func() map[string]any {
return map[string]any{"enabled": true, "url": gateway.URL(), "apiKey": agentKey("antigravity"), "wireProtocol": "openai"}
}
// putRaw writes back what was stashed as it was, or takes key out
putRaw := func(key, raw string) error {
if raw == "" {
return edit.DelJSON(path, key)
}
return edit.SetJSON(path, edit.KV{Path: key, Value: json.RawMessage(raw)})
}
return &Agent{
ID: "antigravity", Name: "Antigravity", Icon: "antigravity-color", Aliases: []string{"antigravity-app", "agy-app"},
Spelled: prefixed,
// ~/.antigravity is also where Antigravity IDE (a VS Code fork) and
// agy keep theirs, so it says nothing of the app: the app is its
// bundle (2.15 or later, the first to take a gateway) or the file
// only its own backend writes
Dir: dir, Path: path, dirShared: true,
sign: func() bool {
if v := (&Agent{ID: "antigravity"}).AppVersion(); v != "" && !Newer("2.15.0", v) {
return true
}
return isFile(filepath.Join(home, ".gemini", "antigravity", "agyhub_summaries_proto.pb"))
},
// its picker lists the gateway's /v1/models as asked with its key,
// by id
ListsModels: true, NoNames: true,
Notice: func() string {
if wired() {
return noticeAntigravity.String()
}
return ""
},
Sync: func() error {
if !wired() {
return nil
}
return syncJSON(path, "gateway", theirsKept(path, "gateway", func() any { return gatewayBlock() }))
},
Check: func() string {
if !wired() {
return ""
}
if p := antigravityAdminPath(); p != "" {
if u, ok := edit.GetJSON(p, "gateway.url"); ok && u != "" {
return noticeAntigravityAdmin.say("path", p)
}
}
if u := os.Getenv("AGY_LLM_GATEWAY_URL"); u != "" && u != gateway.URL() {
return noticeAntigravityEnv.say("url", u)
}
return wiringOff("Antigravity", path, func(k string) (string, bool) { return edit.GetJSON(path, "gateway."+k) },
"url", gateway.URL(), "apiKey", agentKey("antigravity"))
},
Fields: []Field{{
Key: "model", Label: "model",
Get: func() string {
if v := model(); wired() && v != "" {
return magpieID + "/" + v
}
return model()
},
Set: func(v string) error {
on := wired()
if ref, ok := strings.CutPrefix(v, magpieID+"/"); ok && isMagpie(ref) {
if !on {
g, _ := edit.GetJSONRaw(path, "gateway")
m, _ := edit.GetJSONRaw(path, antigravityModel)
l, _ := edit.GetJSONRaw(path, antigravityList)
stash(map[string]string{stashGateway: g, stashModel: m, stashList: l})
}
// a list of the user's would hide magpie's models from
// the picker; it is stashed with their gateway
if err := edit.DelJSON(path, antigravityList); err != nil {
return err
}
// set again on magpie, what the user added to its block stays
block := any(gatewayBlock())
if on {
block = theirsKept(path, "gateway", func() any { return gatewayBlock() })()
}
return edit.SetJSON(path, edit.KV{Path: "gateway", Value: block}, edit.KV{Path: antigravityModel, Value: ref})
}
if on {
// back to what the user had
if err := putRaw("gateway", unstash(stashGateway)); err != nil {
return err
}
if err := putRaw(antigravityList, unstash(stashList)); err != nil {
return err
}
if v == "" {
if err := putRaw(antigravityModel, unstash(stashModel)); err != nil {
return err
}
return antigravityTidy(path)
}
forget(stashModel)
}
if v == "" {
if err := edit.DelJSON(path, antigravityModel); err != nil {
return err
}
} else if err := edit.SetJSON(path, edit.KV{Path: antigravityModel, Value: v}); err != nil {
return err
}
return antigravityTidy(path)
},
Options: func(cur map[string]string) []Option {
// the user's own model is Google's, on their sign-in, or their
// own gateway's
var own []Option
if c := cur["model"]; c != "" && !prefixed(c) {
own = []Option{{Value: c, Icon: modelIcon("", c), Direct: "Google"}}
}
return withVia(group("Antigravity", own), "antigravity", magpieID+"/")
},
}},
}
}
// antigravityTidy takes out a models object magpie left empty.
func antigravityTidy(path string) error {
if v, ok := edit.GetJSON(path, "models"); ok && strings.TrimSpace(v) == "{}" {
return edit.DelJSON(path, "models")
}
return nil
}
// what antigravity says after a change (notice.go)
var (
noticeAntigravity = newNotice("Antigravity reads its gateway at start-up — restart Antigravity to use magpie's models. On magpie it doesn't use your Google sign-in; turning it off here gives that back. An agy not connected on its own row follows it too.")
noticeAntigravityAdmin = newNotice("Antigravity's {path} sets a gateway of its own, which wins over ~/.antigravity/settings.json, so it doesn't reach magpie")
noticeAntigravityEnv = newNotice("AGY_LLM_GATEWAY_URL is set to {url}, which wins over ~/.antigravity/settings.json, so Antigravity started with it doesn't reach magpie")
)