diff --git a/README.md b/README.md index 3b02931cd..d7875c0b4 100644 --- a/README.md +++ b/README.md @@ -72,6 +72,7 @@ and there is a terminal version (`magpie tui`) and a plain CLI. | Copilot CLI | `~/.copilot/settings.json` | model | | Crush | `~/.config/crush/crush.json` | large, small | | DeepSeek Harness (dsh) | `~/.dsh/config.yaml` (`$DSH_HOME`) | model | +| Command Code | `~/.commandcode/settings.json` (+ `providers.json`) | model | Provider-scoped agents (OpenCode, Pi, Goose, Crush) take `provider/model`. Only agents that are installed or configured are shown. diff --git a/internal/agent/agents.go b/internal/agent/agents.go index 756a4fad4..ee762d67a 100644 --- a/internal/agent/agents.go +++ b/internal/agent/agents.go @@ -33,6 +33,7 @@ func All() []*Agent { copilot(home), crush(home, cfg), dsh(home), + commandCode(home), } } diff --git a/internal/agent/commandcode.go b/internal/agent/commandcode.go new file mode 100644 index 000000000..d8cea1b21 --- /dev/null +++ b/internal/agent/commandcode.go @@ -0,0 +1,103 @@ +package agent + +// Command Code (commandcode.ai) keeps its settings in ~/.commandcode/ +// settings.json, the model as "provider/model", and takes providers of the +// user's own in ~/.commandcode/providers.json. magpie adds itself there as +// the provider "magpie", keyless, with the catalog as its models; a model +// through magpie is "magpie//", which Command Code splits +// at the first "/". Command Code still wants its own sign-in for these. + +import ( + "os" + "path/filepath" + "slices" + "strings" + + "github.com/yetone/magpie/internal/edit" +) + +// ccEfforts are the reasoning levels Command Code knows. +var ccEfforts = []string{"low", "medium", "high", "xhigh", "max"} + +func commandCode(home string) *Agent { + dir := filepath.Join(home, ".commandcode") + path := filepath.Join(dir, "settings.json") + providers := filepath.Join(dir, "providers.json") + get := func(k string) string { v, _ := edit.GetJSON(path, k); return v } + dropMagpie := func() error { + if _, err := os.Stat(providers); err != nil { + return nil + } + return edit.DelJSON(providers, "provider."+magpieID) + } + return &Agent{ + ID: "commandcode", Name: "Command Code", Icon: "commandcode", Aliases: []string{"command-code", "cmd"}, + Bin: "command-code", Dir: dir, Path: path, + Notice: func() string { + notes := []string{"Command Code wants its own sign-in (cmd login) even for models through magpie."} + if Running(`(^|/)(cmd|cmdc|command-code|commandcode)( |$)`) { + notes = append(notes, "It reads its settings at start-up — restart open Command Code sessions to use this.") + } + return strings.Join(notes, " ") + }, + Fields: []Field{{ + Key: "model", Label: "model", + Get: func() string { return get("model") }, + Set: func(v string) error { + if v == "" { + keys := []string{"model"} + if get("modelProvider") == magpieID { + keys = append(keys, "modelProvider") + } + if err := edit.DelJSON(path, keys...); err != nil { + return err + } + return dropMagpie() + } + if ref, ok := strings.CutPrefix(v, magpieID+"/"); ok && isMagpie(ref) { + if err := edit.SetJSON(providers, edit.KV{Path: "provider." + magpieID, Value: ccProviderJSON()}); err != nil { + return err + } + return edit.SetJSON(path, edit.KV{Path: "model", Value: v}, edit.KV{Path: "modelProvider", Value: magpieID}) + } + if get("modelProvider") == magpieID { + if err := edit.DelJSON(path, "modelProvider"); err != nil { + return err + } + } + if err := edit.SetJSON(path, edit.KV{Path: "model", Value: v}); err != nil { + return err + } + return dropMagpie() + }, + Options: func(cur map[string]string) []Option { + var out []Option + if v := cur["model"]; v != "" && !usesMagpie(v) { + out = append(out, Option{Value: v, Icon: modelIcon("", v)}) + } + return append(out, viaMagpie(magpieID+"/")...) + }, + }}, + } +} + +// ccProviderJSON is magpie's entry in providers.json. The key is false: +// the gateway takes any, and Command Code refuses one written out. +func ccProviderJSON() any { + ms := map[string]any{} + for _, m := range magpieModels() { + e := map[string]any{"name": m.Name} + var efforts []string + for _, x := range m.Efforts { + if slices.Contains(ccEfforts, x) { + efforts = append(efforts, x) + } + } + if len(efforts) > 0 { + e["reasoning"] = true + e["reasoningEfforts"] = efforts + } + ms[m.ID] = e + } + return map[string]any{"name": "magpie", "api": "openai-completions", "baseURL": gatewayV1(), "apiKey": false, "models": ms} +} diff --git a/internal/agent/commandcode_test.go b/internal/agent/commandcode_test.go new file mode 100644 index 000000000..bc4f52e08 --- /dev/null +++ b/internal/agent/commandcode_test.go @@ -0,0 +1,70 @@ +package agent + +import ( + "encoding/json" + "os" + "path/filepath" + "testing" + + "github.com/yetone/magpie/internal/provider" +) + +func TestCommandCode(t *testing.T) { + home := t.TempDir() + t.Setenv("HOME", home) + t.Setenv("XDG_CONFIG_HOME", filepath.Join(home, ".config")) + t.Setenv("XDG_CACHE_HOME", filepath.Join(home, ".cache")) + if err := provider.Save(provider.Provider{ID: "deepseek", Name: "DeepSeek", Chat: "https://api.deepseek.com/v1", Key: "k", Models: []string{"pro", "flash"}}); err != nil { + t.Fatal(err) + } + dir := filepath.Join(home, ".commandcode") + settingsPath, providersPath := filepath.Join(dir, "settings.json"), filepath.Join(dir, "providers.json") + read := func(p string) map[string]any { + var m map[string]any + b, _ := os.ReadFile(p) + json.Unmarshal(b, &m) + return m + } + os.MkdirAll(dir, 0o755) + os.WriteFile(settingsPath, []byte(`{"theme":"dark","model":"deepseek/deepseek-v4-flash"}`), 0o644) + os.WriteFile(providersPath, []byte(`{"provider":{"mine":{"name":"Mine","api":"openai-completions","baseURL":"https://x","apiKey":"$X","models":{"a":{}}}}}`), 0o644) + + f := commandCode(home).Field("model") + if f.Get() != "deepseek/deepseek-v4-flash" { + t.Fatalf("get: %q", f.Get()) + } + if err := f.Set("magpie/deepseek/pro"); err != nil { + t.Fatal(err) + } + s, p := read(settingsPath), read(providersPath) + if s["model"] != "magpie/deepseek/pro" || s["modelProvider"] != "magpie" || s["theme"] != "dark" { + t.Fatalf("settings: %v", s) + } + ps := p["provider"].(map[string]any) + m, ok := ps["magpie"].(map[string]any) + if !ok || ps["mine"] == nil || m["apiKey"] != false || m["api"] != "openai-completions" { + t.Fatalf("providers: %v", p) + } + if ms := m["models"].(map[string]any); ms["deepseek/pro"] == nil || ms["deepseek/flash"] == nil { + t.Fatalf("models: %v", ms) + } + + // its own model: magpie steps out + if err := f.Set("moonshotai/kimi-k3"); err != nil { + t.Fatal(err) + } + s, p = read(settingsPath), read(providersPath) + if s["model"] != "moonshotai/kimi-k3" || s["modelProvider"] != nil || p["provider"].(map[string]any)["magpie"] != nil || p["provider"].(map[string]any)["mine"] == nil { + t.Fatalf("own: %v %v", s, p) + } + + // through magpie, then reset + f.Set("magpie/deepseek/flash") + if err := f.Set(""); err != nil { + t.Fatal(err) + } + s, p = read(settingsPath), read(providersPath) + if s["model"] != nil || s["modelProvider"] != nil || s["theme"] != "dark" || p["provider"].(map[string]any)["magpie"] != nil { + t.Fatalf("reset: %v %v", s, p) + } +} diff --git a/internal/gui/assets/icons/commandcode.svg b/internal/gui/assets/icons/commandcode.svg new file mode 100644 index 000000000..697f1834c --- /dev/null +++ b/internal/gui/assets/icons/commandcode.svg @@ -0,0 +1 @@ +Command Code diff --git a/site/public/icons/commandcode.svg b/site/public/icons/commandcode.svg new file mode 100644 index 000000000..697f1834c --- /dev/null +++ b/site/public/icons/commandcode.svg @@ -0,0 +1 @@ +Command Code diff --git a/site/public/index.html b/site/public/index.html index caf0d24b3..9d835208c 100644 --- a/site/public/index.html +++ b/site/public/index.html @@ -389,6 +389,7 @@ footer a:hover { color: var(--ink); }
  • Copilot CLI
  • Crush
  • DeepSeek Harness
  • +
  • Command Code
  • Every model