Files
magpie/internal/agent/zed.go
T
XYenonandAmp 6e6338f8d6 agent: configure Zed models and gateway credentials automatically (#584)
* agent: configure Zed models and gateway credentials automatically

Add Zed to the CLI and Agents page with an OpenAI-compatible magpie provider, a synced model catalog and default model selection. Preserve existing JSONC settings and restore the previous provider and default model on reset.

Store the gateway credential in Secret Service, macOS Keychain or Windows Credential Manager so Zed needs no manual API key entry. Cover config preservation, catalog sync, model limits, Flatpak paths and credential failures.

Verified with the full nogui test suite, go vet, macOS and Windows cross compilation, and a local Zed conversation through the gateway.

Amp-Thread-ID: https://ampcode.com/threads/T-01a0fd03-00d7-75fd-956f-c505e07ba60d
Co-authored-by: Amp <amp@ampcode.com>

* agent: store Zed credentials directly through Secret Service D-Bus

Replace the Linux and FreeBSD secret-tool subprocess with the existing godbus client. Open a session, create the default collection when needed, unlock it and replace the matching Zed credential. Handle collection, unlock and item prompts, dismissal and timeout.

Verify the protocol on isolated D-Bus instances with no helper executables on PATH, including immediate completion signals and failed writes. The full nogui suite, targeted vet and race checks, macOS and Windows cross compilation pass; local credential readback confirms replacement without duplication.

Amp-Thread-ID: https://ampcode.com/threads/T-01a0fd03-00d7-75fd-956f-c505e07ba60d
Co-authored-by: Amp <amp@ampcode.com>

* docs: remove the separate Zed configuration paragraph

Amp-Thread-ID: https://ampcode.com/threads/T-01a0fd03-00d7-75fd-956f-c505e07ba60d
Co-authored-by: Amp <amp@ampcode.com>

* agent: fix Zed reset, keychain access and translated notices

Avoid restoring magpie’s own provider after a model change in Zed, and cover reselection followed by reset. Remove the unsupported Flatpak config path.

Authorize the installed Zed app and security command to access the macOS gateway credential. Translate Zed’s notice and apply translation when selecting or reapplying an agent model.

Verified with the full nogui test suite, go vet, Zed race tests, cross compilation and browser checks. macOS keychain behavior still needs runtime verification on a Mac.

Amp-Thread-ID: https://ampcode.com/threads/T-01a0fd70-98a1-737f-93bd-77e181f904c1
Co-authored-by: XYenon <register@xyenon.bid>

* agent: preserve Zed model picks when resetting magpie

Restore the stashed default model only while Zed still selects magpie. Preserve a model chosen in Zed itself and discard the stale model stash, while restoring the previous provider.

Find Zed Preview in Applications folders and Spotlight without requiring a CLI, and use any in the isolated D-Bus tests.

Verified the reset regression fails before the fix and passes afterward. The full nogui suite, go vet, Zed race tests and macOS/Windows test cross compilation pass.

Amp-Thread-ID: https://ampcode.com/threads/T-01a0fd70-98a1-737f-93bd-77e181f904c1
Co-authored-by: XYenon <register@xyenon.bid>

* agent: retain Zed provider backup when reselecting magpie

When Zed’s provider already points at the gateway, update only the model stash and preserve the original provider backup. Reset can then restore the user’s provider after a native model pick followed by another magpie pick.

Add a regression covering the full sequence and checking the restored provider, model catalog, custom headers and latest native model. Confirm it fails before the fix and passes afterward; all Zed tests, the full nogui suite and go vet pass.

Amp-Thread-ID: https://ampcode.com/threads/T-01a0fd70-98a1-737f-93bd-77e181f904c1
Co-authored-by: XYenon <register@xyenon.bid>

---------

Co-authored-by: Amp <amp@ampcode.com>
2026-10-03 01:51:53 +08:00

143 lines
4.6 KiB
Go

package agent
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"runtime"
"strings"
"github.com/yetone/magpie/internal/edit"
)
const zedProvider = "language_models.openai_compatible.magpie"
const zedModel = "agent.default_model"
// Zed stores API keys in the OS credential store, keyed by the API URL.
var zedCredential = saveZedCredential
// Zed uses ~/.config on macOS, XDG on Linux and Roaming AppData on Windows.
func zed(home, cfg string) *Agent {
switch runtime.GOOS {
case "darwin":
cfg = filepath.Join(home, ".config")
case "windows":
cfg = os.Getenv("APPDATA")
if cfg == "" {
cfg = filepath.Join(home, "AppData", "Roaming")
}
return zedAt(filepath.Join(cfg, "Zed"))
}
return zedAt(filepath.Join(cfg, "zed"))
}
func zedAt(dir string) *Agent {
path := filepath.Join(dir, "settings.json")
get := func(k string) string { v, _ := edit.GetJSON(path, k); return v }
model := pairGet(func(k string) (string, bool) { return edit.GetJSON(path, k) }, zedModel+".provider", zedModel+".model")
key := "zed:" + path + ":"
return atomic(&Agent{
ID: "zed", Name: "Zed", Icon: "zed", Bin: "zed", Dir: dir, Path: path,
UA: []string{"zed"},
Notice: func() string {
if usesMagpie(model()) && Running(`(^|/)(zed|zeditor|zed-editor)( |$)`) {
return "Restart Zed if it still asks for an API key: magpie has configured its gateway credential in the system credential store."
}
return ""
},
Sync: func() error {
if get(zedProvider+".api_url") != gatewayV1() {
return nil
}
return syncJSON(path, zedProvider+".available_models", func() any {
return zedProviderJSON()["available_models"]
})
},
Check: func() string {
if !usesMagpie(model()) {
return ""
}
return wiringOff("Zed", path, func(k string) (string, bool) { return edit.GetJSON(path, zedProvider+"."+k) }, "api_url", gatewayV1())
},
Fields: []Field{{
Key: "model", Label: "model", Get: model,
Set: func(v string) error {
if ref, ok := strings.CutPrefix(v, magpieID+"/"); ok && isMagpie(ref) {
if err := zedCredential(gatewayV1()); err != nil {
return fmt.Errorf("configure Zed gateway credential: %w", err)
}
if !usesMagpie(model()) {
previous := map[string]string{key + "model": get(zedModel)}
if get(zedProvider+".api_url") != gatewayV1() {
previous[key+"provider"] = get(zedProvider)
}
stash(previous)
}
return edit.SetJSON(path,
edit.KV{Path: zedProvider, Value: zedProviderJSON()},
edit.KV{Path: zedModel, Value: map[string]string{"provider": magpieID, "model": ref}})
}
// Validate a native selection before removing magpie's wiring.
p, m, ok := strings.Cut(v, "/")
if v != "" && (!ok || p == "" || m == "") {
return fmt.Errorf("expected provider/model, got %q", v)
}
if get(zedProvider+".api_url") == gatewayV1() || usesMagpie(model()) {
for _, entry := range []struct{ name, field string }{{"provider", zedProvider}, {"model", zedModel}} {
if entry.name == "model" && !usesMagpie(model()) {
forget(key + entry.name)
continue
}
if was := unstash(key + entry.name); was != "" {
if err := edit.SetJSON(path, edit.KV{Path: entry.field, Value: json.RawMessage(was)}); err != nil {
return err
}
} else if entry.name == "provider" || usesMagpie(model()) {
if err := edit.DelJSON(path, entry.field); err != nil {
return err
}
}
}
} else if v == "" {
return edit.DelJSON(path, zedModel)
}
if v == "" {
return nil
}
return edit.SetJSON(path, edit.KV{Path: zedModel, Value: map[string]string{"provider": p, "model": m}})
},
Options: func(cur map[string]string) []Option {
var own []Option
if v := cur["model"]; v != "" && !usesMagpie(v) {
own = append(own, Option{Value: v, Icon: modelIcon("", v)})
}
return append(group("Zed", own), viaMagpie("zed", magpieID+"/")...)
},
}},
}, path, stashPath())
}
func zedProviderJSON() map[string]any {
models := []any{}
for _, m := range magpieModels("zed") {
context := m.Context
if context == 0 {
context = 128000 // Zed requires a context window for every custom model.
}
entry := map[string]any{
"name": m.ID, "display_name": m.Name, "max_tokens": context,
"capabilities": map[string]any{
"tools": true, "images": m.Images,
"parallel_tool_calls": false, "prompt_cache_key": false,
"chat_completions": true,
},
}
if output := maxTokens(m); output > 0 {
entry["max_output_tokens"] = min(output, context)
}
models = append(models, entry)
}
return map[string]any{"api_url": gatewayV1(), "available_models": models}
}