Files
magpie/internal/agent/agy.go
T
yetone 250bdd5c4f gui: Names in agents' lists names the connected agents it changes nothing in (love1sbug on X)
love1sbug on X found Names in agents' lists seemed to do nothing. Claude
Code and Qwen Code follow it now; a sweep of every agent in a sandbox
HOME (each set to one relay model, the setting switched, the files
compared) found the rest that follow it, and those that can't:

- handed the models' ids alone, their formats having no name for one:
  Goose, jcode, Hermes Agent, Reasonix Studio, AtomCode, fx, Antigravity
  CLI, AstrBot, Mister Morph;
- with no list of magpie's models: Gemini CLI, Snow CLI;
- reading the gateway's /v1/models themselves, whose display_name is the
  name alone on purpose (a remote magpie relies on it): Empryo, Cursor
  Private Inference.

Those carry agent.NoNames (copied to an agent in WSL), sent to the GUI
as noNames. Where the setting is offered (Settings › Provider in model
names, Routing's Names in agents' lists, a provider's Names & levels) a
line names the connected ones: "Changes nothing in Goose, Hermes Agent:
magpie's names don't reach their model lists", in every language. With
none connected nothing is said, so the Settings row keeps no line under
its name (1ooogle on X, model-suffix-own's check). A page drawn before
the agents are read (Routing, an open provider editor) says it once they
are (redrawNoNames, beside redrawSuffix).

name-suffix-no-names.test.cjs checks the three places in chromium and
webkit, en/zh/zh-TW/ja/de, at 900px and 390px with no sideways scroll,
that an agent not connected or following it isn't named, that none
says nothing, and, with /api/state answered 1.5s late, that all three
still say it. On the old assets 12 of the first 13 chromium tests fail;
the late case failed on this commit's first version too (found when a
loaded run failed the editor's case; reproduced with the injected delay).

Run: that test and i18n-keys, gateway-mode, model-suffix-own,
name-suffix-remote, name-suffix-saved, name-suffix-where-named,
settings-general/groups/search/sections in both engines; go
test -tags nogui ./internal/agent ./internal/gui ./internal/provider ./internal/gateway (before the rebase);
gofmt -l; go vet darwin/linux/windows. With 11 files at once at load
80-120, webkit failed once on this test's "no page errors" check (ja
900px, the late case) beside settings-sections' "Settings in parts";
not reproduced in 3 runs since (10 files, then 8 copies at once), and
the errors weren't kept.
Not tried: the GUI against a real magpie with Goose connected.
2026-10-13 14:06:24 +08:00

193 lines
6.6 KiB
Go

package agent
// Antigravity's CLI, agy, keeps its settings in
// ~/.gemini/antigravity-cli/settings.json. With "modelProvider": "gemini" it
// asks a Gemini API endpoint rather than Google's own backend, and lists the
// models of customModelsConfig.customModels, each {apiProvider, modelName},
// the one picked being "model". magpie's gateway speaks the Gemini API, so
// magpie adds one custom model per catalog model, keyed
// "magpie/<provider>/<model>", and sets modelProvider and model; the user's
// own are stashed and put back when magpie steps out. The endpoint and key
// agy takes only from its environment (GOOGLE_GEMINI_BASE_URL and
// GEMINI_API_KEY; no .env file), so magpie can't set them: the agent's row
// gives the command that starts agy with them (see Agent.Launch). That
// names the model with --model too: agy 1.2 asks for a custom model picked
// in settings.json or its /model picker with no model name ("model is
// empty"); only one given on the command line resolves.
import (
"encoding/json"
"path/filepath"
"runtime"
"sort"
"strings"
"github.com/tidwall/gjson"
"github.com/tidwall/jsonc"
"github.com/yetone/magpie/internal/edit"
"github.com/yetone/magpie/internal/gateway"
)
const agyModels = "customModelsConfig.customModels"
// AgyLaunch is the command that starts agy on magpie's gateway and one of
// its custom models, in the shell of this system. Its key names agy: its
// requests go out as Google's Gemini SDK's (User-Agent google-genai-sdk/…),
// with nothing of agy's own, so the gateway knows them by the key
// (x-goog-api-key) alone. A command copied before, with the plain token,
// still reaches magpie.
func AgyLaunch(model string) string {
key := agentKey("agy")
if runtime.GOOS == "windows" {
return `$env:GEMINI_API_KEY="` + key + `"; $env:GOOGLE_GEMINI_BASE_URL="` + gateway.URL() + `"; agy --model '` + strings.ReplaceAll(model, "'", "''") + `'`
}
return "GEMINI_API_KEY=" + key + " GOOGLE_GEMINI_BASE_URL=" + gateway.URL() + " agy --model '" + strings.ReplaceAll(model, "'", `'\''`) + "'"
}
// agyCustom reads customModels as it is, raw JSON by key.
func agyCustom(path string) map[string]json.RawMessage {
out := map[string]json.RawMessage{}
raw, err := edit.Read(path)
if err != nil || len(raw) == 0 {
return out
}
gjson.GetBytes(jsonc.ToJSONInPlace(raw), agyModels).ForEach(func(k, v gjson.Result) bool {
out[k.String()] = json.RawMessage(v.Raw)
return true
})
return out
}
func agy(home string) *Agent {
dir := filepath.Join(home, ".gemini", "antigravity-cli")
path := filepath.Join(dir, "settings.json")
get := jsonGet(path, "model")
provider := jsonGet(path, "modelProvider")
stashModel, stashProvider := "agy:"+path+":model", "agy:"+path+":modelProvider"
// setModels writes customModels with the user's own and, when with,
// magpie's; none left takes the key out
setModels := func(with bool) error {
ms := map[string]any{}
for k, v := range agyCustom(path) {
if !strings.HasPrefix(k, magpieID+"/") {
ms[k] = v
}
}
if with {
for _, m := range magpieModels("agy") {
ms[magpieID+"/"+m.ID] = map[string]string{"apiProvider": "API_PROVIDER_GOOGLE_GEMINI", "modelName": m.ID}
}
}
if len(ms) > 0 {
return edit.SetJSON(path, edit.KV{Path: agyModels, Value: ms})
}
if err := edit.DelJSON(path, agyModels); err != nil {
return err
}
if v, ok := edit.GetJSON(path, "customModelsConfig"); ok && strings.TrimSpace(v) == "{}" {
return edit.DelJSON(path, "customModelsConfig")
}
return nil
}
// put sets key to v, or takes it out for ""
put := func(key, v string) error {
if v == "" {
return edit.DelJSON(path, key)
}
return edit.SetJSON(path, edit.KV{Path: key, Value: v})
}
return &Agent{
ID: "agy", Name: "Antigravity CLI", Icon: "antigravity-color", Aliases: []string{"antigravity-cli"}, Spelled: prefixed, NoNames: true,
Bin: "agy", Dir: dir, Path: path,
Launch: func() string {
if v := get(); usesMagpie(v) {
return AgyLaunch(v)
}
return ""
},
Sync: func() error {
if !usesMagpie(get()) {
return nil
}
return setModels(true)
},
Notice: func() string {
v := get()
if !usesMagpie(v) {
return ""
}
return noticeAgy.say("command", AgyLaunch(v))
},
Check: func() string {
v := get()
if !usesMagpie(v) {
return ""
}
if _, ok := agyCustom(path)[v]; !ok {
return "Antigravity CLI's custom model " + v + " (settings.json) is gone, so it no longer reaches magpie"
}
if p := provider(); p != "gemini" {
return "Antigravity CLI's modelProvider (settings.json) is " + orDefault(p) + ", not gemini, so it asks Google directly"
}
return ""
},
Fields: []Field{{
Key: "model", Label: "model",
Get: get,
Set: func(v string) error {
cur := get()
if ref, ok := strings.CutPrefix(v, magpieID+"/"); ok && isMagpie(ref) {
if !usesMagpie(cur) {
stash(map[string]string{stashModel: cur, stashProvider: provider()})
}
if err := setModels(true); err != nil {
return err
}
return edit.SetJSON(path, edit.KV{Path: "modelProvider", Value: "gemini"}, edit.KV{Path: "model", Value: v})
}
if usesMagpie(cur) {
// back to what the user had
if v == "" {
v = unstash(stashModel)
} else {
forget(stashModel)
}
if err := put("modelProvider", unstash(stashProvider)); err != nil {
return err
}
}
if err := put("model", v); err != nil {
return err
}
return setModels(false)
},
Options: func(cur map[string]string) []Option {
custom := agyCustom(path)
// its own say they go to their provider, not through magpie,
// as every agent's own do (EZN7L2C3, #834)
var own []Option
for k := range custom {
if !strings.HasPrefix(k, magpieID+"/") {
to := "Google"
if p := gjson.GetBytes(custom[k], "apiProvider").String(); strings.Contains(p, "ANTHROPIC") {
to = "Anthropic"
} else if strings.Contains(p, "OPENAI") {
to = "OpenAI"
}
own = append(own, Option{Value: k, Icon: modelIcon("", gjson.GetBytes(custom[k], "modelName").String()), Direct: to})
}
}
sort.Slice(own, func(i, j int) bool { return own[i].Value < own[j].Value })
if c := cur["model"]; c != "" && !usesMagpie(c) && custom[c] == nil {
own = append([]Option{{Value: c, Icon: modelIcon("", c), Direct: "Google"}}, own...)
}
return append(group("Antigravity CLI", own), viaMagpie("agy", magpieID+"/")...)
},
}},
}
}
// what agy says after a change (notice.go)
var noticeAgy = newNotice("agy takes magpie's gateway and model only from how it is started — start it with {command}")