diff --git a/README.md b/README.md index c65849b32..307456e46 100644 --- a/README.md +++ b/README.md @@ -83,6 +83,7 @@ and there is a terminal version (`magpie tui`) and a plain CLI. | Cursor CLI | `~/.cursor/cli-config.json` | model | | Zed | `~/.config/zed/settings.json` (`$XDG_CONFIG_HOME` on Linux, `%APPDATA%\Zed` on Windows) | model (a `magpie` OpenAI-compatible provider; its catalog in Zed's picker) | | VS Code (Chat) | `~/Library/Application Support/Code/User/settings.json` + `chatLanguageModels.json` (`~/.config/Code/User` on Linux, `%APPDATA%\Code\User` on Windows) | model (`chat.defaultModel`; a `magpie` Custom Endpoint group, its catalog in Chat's model picker; VS Code 1.122+, no Copilot sign-in or key needed) | +| JetBrains Air | `acp.json` in `~/Library/Application Support/JetBrains/Air` (`~/.config/JetBrains/Air` on Linux, `%APPDATA%\JetBrains\Air` on Windows) + `magpie-opencode.json` beside it | model (a `Magpie` ACP agent: OpenCode's `opencode acp` on magpie's provider alone, its models and routing groups in Air's model menu; needs OpenCode installed) | | Copilot CLI | `~/.copilot/settings.json` | model | | Crush | `~/.config/crush/crush.json` | large, small | | DeepSeek Harness (dsh) | `~/.dsh/profiles/*/cordis.patch.yml` (`$DSH_HOME`; a custom provider, Magpie), or `~/.dsh/config.yaml` before dsh 0.1.5 | model, effort | diff --git a/internal/agent/agents.go b/internal/agent/agents.go index 7b8356cc4..13803af55 100644 --- a/internal/agent/agents.go +++ b/internal/agent/agents.go @@ -75,6 +75,7 @@ func All() []*Agent { cursor(home), zed(home, cfg), vscode(home, cfg), + air(home, cfg), copilot(home), crush(home, cfg), dsh(home), diff --git a/internal/agent/air.go b/internal/agent/air.go new file mode 100644 index 000000000..78be63234 --- /dev/null +++ b/internal/agent/air.go @@ -0,0 +1,168 @@ +package agent + +// JetBrains Air, JetBrains' agentic development environment, runs its own +// agents (Claude Agent, Codex, Gemini CLI, Junie) on the accounts signed in +// under Settings | Account | AI Providers: a sign-in or an API key, with no +// base URL of its own to point at magpie (AIR-4541 asks for one). What it +// does take is any agent that speaks the Agent Client Protocol, listed in +// acp.json in its config folder — ~/Library/Application Support/JetBrains/Air +// on macOS, $XDG_CONFIG_HOME/JetBrains/Air on Linux, %APPDATA%\JetBrains\Air +// on Windows — as +// +// {"agent_servers": {"": {"command": …, "args": […], "env": {…}}}} +// +// and the models such an agent reports at session/new are its model menu +// in Air. So magpie adds an agent of its own there, "Magpie": OpenCode's ACP +// server (opencode acp), handed with OPENCODE_CONFIG a config file magpie +// keeps beside acp.json that has magpie's provider, its models, the one +// picked as the model a task starts on, and enabled_providers narrowed to +// magpie, so Air's menu lists magpie's models and routing groups and nothing +// of OpenCode's own. OpenCode merges that file over the user's own config +// and leaves the rest of it (their plugins, agents, MCP servers) as it is. +// Air's own agents, and the accounts they run on, aren't touched. +// +// Checked with OpenCode 2.0.20's opencode acp under a sandbox HOME, as Air +// starts it: session/new's model option lists magpie's models alone, the +// one set here current. Not with Air itself. One gap is OpenCode's: when a +// provider's key is in the environment it is started with (OPENAI_API_KEY, +// DEEPSEEK_API_KEY, … set, not empty), 2.0.20's ACP server lists that +// provider and none of its config's — this file's, the global one or a +// project's alike — so magpie's models don't show; an emptied key, or one +// kept in OpenCode's auth.json, doesn't do it. + +import ( + "fmt" + "os" + "os/exec" + "path/filepath" + "runtime" + "strings" + + "github.com/yetone/magpie/internal/edit" + "github.com/yetone/magpie/internal/gateway" +) + +// airAgent is the name magpie's agent has in acp.json, and so in Air's +// agent menu. +const airAgent = "Magpie" + +// airDir is where Air keeps its settings and acp.json. +func airDir(home, cfg string) string { + switch runtime.GOOS { + case "darwin": + return filepath.Join(home, "Library", "Application Support", "JetBrains", "Air") + case "windows": + d := os.Getenv("APPDATA") + if d == "" { + d = filepath.Join(home, "AppData", "Roaming") + } + return filepath.Join(d, "JetBrains", "Air") + } + return filepath.Join(cfg, "JetBrains", "Air") +} + +// airOpenCode is the OpenCode Air is to start: the one on PATH, else the +// one OpenCode's installer puts in ~/.opencode/bin, as a full path, since +// Air, an app, may not have the shell's PATH; "" when there is none. +var airOpenCode = func(home string) string { + if p, err := exec.LookPath("opencode"); err == nil { + if abs, err := filepath.Abs(p); err == nil { + return abs + } + return p + } + name := "opencode" + if runtime.GOOS == "windows" { + name += ".exe" + } + if p := filepath.Join(home, ".opencode", "bin", name); isFile(p) { + return p + } + return "" +} + +func air(home, cfg string) *Agent { + dir := airDir(home, cfg) + path := filepath.Join(dir, "acp.json") + // the OpenCode config magpie's agent runs on, magpie's own file + ocPath := filepath.Join(dir, "magpie-opencode.json") + entry := "agent_servers." + airAgent + provider := func() any { return magpieProviderJSONFor("opencode", "air") } + // ours: acp.json's Magpie is the agent magpie added, on its file + ours := func() bool { + v, _ := edit.GetJSON(path, entry+".env.OPENCODE_CONFIG") + return v == ocPath + } + // model is the one a task starts on, read from magpie's own file: with + // Magpie gone from acp.json it still reads, for Check to say so and a + // set again to put the entry back + model := func() string { v, _ := edit.GetJSON(ocPath, "model"); return v } + return atomic(&Agent{ + ID: "air", Name: "JetBrains Air", Icon: "air", Aliases: []string{"jetbrains-air"}, + // no Bin: air is other tools' name too (Go's live reloader); its + // requests are its OpenCode's, and counted as OpenCode's + Dir: dir, Path: path, + Notice: func() string { + if !ours() { + return "" + } + if airOpenCode(home) == "" { + return "Air runs magpie's models through OpenCode's ACP server, and OpenCode isn't installed here: install OpenCode (npm i -g opencode-ai), then pick Magpie in a new Air task." + } + return "In Air, start a new task and pick Magpie in its agent menu: magpie's models and routing groups are in its model menu." + }, + Check: func() string { + if !usesMagpie(model()) { + return "" + } + if !ours() { + return "Air's Magpie agent (" + filepath.Base(path) + ") is gone, so Air no longer reaches magpie" + } + return wiringOff("Air", ocPath, func(k string) (string, bool) { return edit.GetJSON(ocPath, "provider."+magpieID+".options."+k) }, + "baseURL", gatewayV1(), "apiKey", gateway.Token) + }, + Sync: func() error { + return syncJSONInOrder(ocPath, "provider."+magpieID, provider) + }, + Fields: []Field{{ + Key: "model", Label: "model", + Get: model, + Set: func(v string) error { + if v == "" { + if err := edit.DelJSON(path, entry); err != nil { + return err + } + if err := edit.Remove(ocPath); err != nil && !os.IsNotExist(err) { + return err + } + return nil + } + ref, ok := strings.CutPrefix(v, magpieID+"/") + if !ok || !isMagpie(ref) { + return fmt.Errorf("Air takes only magpie's models here (its own agents are picked in Air): %s isn't one", v) + } + // each key new to the file goes in at its top: last first, + // so a new file reads $schema, model, …, provider + if err := edit.SetJSON(ocPath, + edit.KV{Path: "provider." + magpieID, Value: provider()}, + edit.KV{Path: "enabled_providers", Value: []string{magpieID}}, + // titles and summaries too, as the user's own small + // model may be a provider left out here + edit.KV{Path: "small_model", Value: v}, + edit.KV{Path: "model", Value: v}, + edit.KV{Path: "$schema", Value: "https://opencode.ai/config.json"}); err != nil { + return err + } + bin := airOpenCode(home) + if bin == "" { + bin = "opencode" + } + return edit.SetJSON(path, edit.KV{Path: entry, Value: map[string]any{ + "command": bin, "args": []string{"acp"}, + "env": map[string]string{"OPENCODE_CONFIG": ocPath}, + }}) + }, + Options: func(map[string]string) []Option { return viaMagpie("air", magpieID+"/") }, + }}, + }, path, ocPath) +} diff --git a/internal/agent/air_test.go b/internal/agent/air_test.go new file mode 100644 index 000000000..6c4e61aa6 --- /dev/null +++ b/internal/agent/air_test.go @@ -0,0 +1,173 @@ +package agent + +import ( + "os" + "path/filepath" + "strings" + "testing" + + "github.com/yetone/magpie/internal/edit" + "github.com/yetone/magpie/internal/provider" +) + +// airHome is a sandbox home with Air's config folder and an OpenCode at a +// known place. +func airHome(t *testing.T, bin string) (home, dir string) { + home, _ = codexHome(t, "", "") + dir = airDir(home, filepath.Join(home, ".config")) + was := airOpenCode + airOpenCode = func(string) string { return bin } + t.Cleanup(func() { airOpenCode = was }) + return home, dir +} + +// Air's model menu is the ACP agent's: magpie adds one, Magpie, to +// acp.json beside the user's own agents — OpenCode's ACP server on a config +// of magpie's that has magpie's provider alone — and taking it off takes +// out that entry and that file, the user's agents kept. +func TestAirAddsMagpieAgent(t *testing.T) { + home, dir := airHome(t, "/opt/bin/opencode") + acp := filepath.Join(dir, "acp.json") + writeFile(t, acp, `{ + "agent_servers": { + "Goose": {"command": "goose", "args": ["acp"], "env": {}} + } +} +`) + a := air(home, filepath.Join(home, ".config")) + if !a.Detected() || a.Path != acp { + t.Fatalf("detected %v, path %s", a.Detected(), a.Path) + } + if v := a.Field("model").Get(); v != "" { + t.Fatalf("model before = %q", v) + } + opts := a.Field("model").Options(nil) + want := "" + for _, o := range opts { + if o.Ref == "fake/m1" { + want = o.Value + } + } + if want != "magpie/fake/m1" { + t.Fatalf("options: %+v", opts) + } + if err := a.Apply("model", want); err != nil { + t.Fatal(err) + } + cfg := filepath.Join(dir, "magpie-opencode.json") + for k, v := range map[string]string{ + "agent_servers.Goose.command": "goose", + "agent_servers.Magpie.command": "/opt/bin/opencode", + "agent_servers.Magpie.args.0": "acp", + "agent_servers.Magpie.env.OPENCODE_CONFIG": cfg, + } { + if got, _ := edit.GetJSON(acp, k); got != v { + t.Fatalf("%s = %q, want %q\n%s", k, got, v, readFile(acp)) + } + } + for k, v := range map[string]string{ + "model": "magpie/fake/m1", + "small_model": "magpie/fake/m1", + "enabled_providers": `["magpie"]`, + "provider.magpie.options.baseURL": gatewayV1(), + "provider.magpie.options.apiKey": "magpie", + "provider.magpie.npm": "@ai-sdk/openai-compatible", + } { + if got, _ := edit.GetJSON(cfg, k); strings.Join(strings.Fields(got), "") != v { + t.Fatalf("%s = %q, want %q\n%s", k, got, v, readFile(cfg)) + } + } + if _, ok := edit.GetJSON(cfg, `provider.magpie.models.fake/m1`); !ok { + t.Fatalf("no fake/m1 in magpie's models: %s", readFile(cfg)) + } + if v := a.Field("model").Get(); v != want { + t.Fatalf("model = %q", v) + } + if d := a.Drift(); d != nil { + t.Fatalf("drift: %+v", d) + } + if n := a.Notice(); !strings.Contains(n, "pick Magpie") { + t.Fatalf("notice: %q", n) + } + // one of Air's own agents isn't set from here + if err := a.Apply("model", "anthropic/claude-sonnet-5"); err == nil { + t.Fatal("took a model that isn't magpie's") + } + if err := a.Apply("model", ""); err != nil { + t.Fatal(err) + } + if _, ok := edit.GetJSON(acp, "agent_servers.Magpie"); ok { + t.Fatalf("Magpie still in acp.json: %s", readFile(acp)) + } + if v, _ := edit.GetJSON(acp, "agent_servers.Goose.command"); v != "goose" { + t.Fatalf("the user's agent went: %s", readFile(acp)) + } + if _, err := os.Stat(cfg); !os.IsNotExist(err) { + t.Fatalf("magpie's OpenCode config left: %v", err) + } + if v := a.Field("model").Get(); v != "" { + t.Fatalf("model after = %q", v) + } +} + +// With no acp.json yet (Air makes it on the first "Edit ACP JSON File"), +// magpie writes one; without OpenCode found it names it by its command and +// the notice says to install it. +func TestAirWithoutOpenCode(t *testing.T) { + home, dir := airHome(t, "") + os.MkdirAll(dir, 0o755) + a := air(home, filepath.Join(home, ".config")) + if err := a.Apply("model", "magpie/fake/m1"); err != nil { + t.Fatal(err) + } + acp := filepath.Join(dir, "acp.json") + if v, _ := edit.GetJSON(acp, "agent_servers.Magpie.command"); v != "opencode" { + t.Fatalf("command = %q\n%s", v, readFile(acp)) + } + if n := a.Notice(); !strings.Contains(n, "install OpenCode") { + t.Fatalf("notice: %q", n) + } +} + +// Magpie's entry taken out of acp.json by hand while its config still +// names one of magpie's models is wiring gone, which setting it again puts +// back; providers added later reach the config's model list, and Sync +// writes no config magpie never set up. +func TestAirCheckAndSync(t *testing.T) { + home, dir := airHome(t, "/opt/bin/opencode") + a := air(home, filepath.Join(home, ".config")) + acp, cfg := filepath.Join(dir, "acp.json"), filepath.Join(dir, "magpie-opencode.json") + if err := a.Sync(); err != nil { + t.Fatal(err) + } + if _, err := os.Stat(cfg); !os.IsNotExist(err) { + t.Fatal("Sync wrote a config magpie never set up") + } + if err := a.Apply("model", "magpie/fake/m1"); err != nil { + t.Fatal(err) + } + if err := provider.Save(provider.Provider{ID: "fake2", Name: "Fake 2", Key: "k", Chat: "http://127.0.0.1:1/v1", Models: []string{"m2"}}); err != nil { + t.Fatal(err) + } + if err := a.Sync(); err != nil { + t.Fatal(err) + } + if _, ok := edit.GetJSON(cfg, `provider.magpie.models.fake2/m2`); !ok { + t.Fatalf("a provider added later isn't listed: %s", readFile(cfg)) + } + if err := edit.DelJSON(acp, "agent_servers.Magpie"); err != nil { + t.Fatal(err) + } + if d := a.Drift(); d == nil || d.Kind != "unwired" || !strings.Contains(d.Detail, "Magpie agent") { + t.Fatalf("drift: %+v", d) + } + if err := a.Reapply(); err != nil { + t.Fatal(err) + } + if v, _ := edit.GetJSON(acp, "agent_servers.Magpie.env.OPENCODE_CONFIG"); v != cfg { + t.Fatalf("not put back: %s", readFile(acp)) + } + if d := a.Drift(); d != nil { + t.Fatalf("drift after setting it again: %+v", d) + } +} diff --git a/internal/gui/assets/i18n.js b/internal/gui/assets/i18n.js index dec35fcf4..263841381 100644 --- a/internal/gui/assets/i18n.js +++ b/internal/gui/assets/i18n.js @@ -871,6 +871,8 @@ const I18N = { "Codex's background service is still signed in as {user}": "Codex 的后台服务仍登录着 {user}", "Restart it to use the new account. Running Codex sessions will be interrupted.": "重启它才会用上新账号。正在运行的 Codex 会话会被中断。", "Restart Zed if it still asks for an API key: magpie has configured its gateway credential in the system credential store.": "如果 Zed 仍要求输入 API 密钥,请重启 Zed:magpie 已在系统凭据存储中配置了网关凭据。", + "In Air, start a new task and pick Magpie in its agent menu: magpie's models and routing groups are in its model menu.": "在 Air 里新建一个任务,在智能体菜单中选 Magpie:magpie 的模型和路由组都在它的模型菜单里。", + "Air runs magpie's models through OpenCode's ACP server, and OpenCode isn't installed here: install OpenCode (npm i -g opencode-ai), then pick Magpie in a new Air task.": "Air 通过 OpenCode 的 ACP 服务使用 magpie 的模型,但这台机器上没有安装 OpenCode:请先安装 OpenCode(npm i -g opencode-ai),再在 Air 的新任务里选 Magpie。", "magpie's models are in VS Code's Chat model picker, under magpie (VS Code 1.122 or later). If they don't show, run Developer: Reload Window in VS Code.": "magpie 的模型在 VS Code 聊天的模型选择器里,位于 magpie 分组下(需 VS Code 1.122 或更高版本)。如果没有出现,请在 VS Code 中运行 Developer: Reload Window。", "Later": "稍后", "Restart": "重启", diff --git a/internal/gui/assets/icons/air.svg b/internal/gui/assets/icons/air.svg new file mode 100644 index 000000000..540f06b2c --- /dev/null +++ b/internal/gui/assets/icons/air.svg @@ -0,0 +1,4 @@ + + + +