agent: rewrite the wiring an older dial left behind

On start-up magpie now renames what dial wrote into the agents' files: a
model spelled dial/… becomes magpie/… (which also writes the magpie
provider entry), and the dial provider entry, Codex's [model_providers.dial]
table and dial-models.json, and Claude Code's bearer token go. Once nothing
says dial it changes nothing.
This commit is contained in:
yetone
2026-09-24 00:17:32 +08:00
parent 402a96c39d
commit 1f44812457
3 changed files with 162 additions and 0 deletions
+62
View File
@@ -0,0 +1,62 @@
package agent
import (
"os"
"path/filepath"
"slices"
"strings"
"github.com/yetone/magpie/internal/edit"
"github.com/yetone/magpie/internal/gateway"
)
// legacyID is what agents knew the gateway by before magpie was called magpie.
const legacyID = "dial"
// RenameLegacy rewrites what an older dial left in the agents' files so it
// says magpie: a model spelled dial/… becomes magpie/… (which also writes
// the magpie provider entry the agent needs), and then the dial entry,
// Codex's table and catalog file, and Claude Code's token go. It runs at
// start-up; once nothing says dial any more it changes nothing. Errors are
// swallowed on purpose — a file magpie cannot rewrite is one the user can
// still fix from the picker.
func RenameLegacy() {
for _, a := range All() {
if _, err := os.Stat(a.Path); err != nil {
continue
}
if a.ID == "codex" {
// Set() would stash "dial" as the provider to go back to; say
// magpie first so it sees a config that is already routed.
if v, _ := edit.GetTOMLTop(a.Path, "model_provider"); v == legacyID {
_ = edit.SetTOMLTop(a.Path, edit.KV{Path: "model_provider", Value: magpieID})
if f := a.Field("model"); f != nil {
_ = f.Set(f.Get())
}
}
}
for i := range a.Fields {
f := &a.Fields[i]
if rest, ok := strings.CutPrefix(f.Get(), legacyID+"/"); ok {
_ = f.Set(magpieID + "/" + rest)
}
}
switch a.ID {
case "opencode":
_ = edit.DelJSON(a.Path, "provider."+legacyID)
case "crush":
_ = edit.DelJSON(a.Path, "providers."+legacyID)
case "pi":
_ = edit.DelJSON(filepath.Join(a.Dir, "models.json"), "providers."+legacyID)
case "codex":
if slices.Contains(edit.TOMLTables(a.Path), "model_providers."+legacyID) {
_ = edit.DelTOMLTable(a.Path, "model_providers."+legacyID)
}
os.Remove(filepath.Join(a.Dir, legacyID+"-models.json"))
case "claude":
if t, _ := edit.GetJSON(a.Path, "env.ANTHROPIC_AUTH_TOKEN"); t == legacyID {
_ = edit.SetJSON(a.Path, edit.KV{Path: "env.ANTHROPIC_AUTH_TOKEN", Value: gateway.Token})
}
}
}
}
+99
View File
@@ -0,0 +1,99 @@
package agent
import (
"os"
"path/filepath"
"strings"
"testing"
"github.com/yetone/magpie/internal/edit"
"github.com/yetone/magpie/internal/gateway"
"github.com/yetone/magpie/internal/provider"
)
func TestRenameLegacy(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"))
write := func(rel, body string) {
p := filepath.Join(home, rel)
if err := os.MkdirAll(filepath.Dir(p), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(p, []byte(body), 0o644); err != nil {
t.Fatal(err)
}
}
if err := provider.Save(provider.Provider{ID: "deepseek", Name: "DeepSeek", Chat: "https://api.deepseek.com/v1", Key: "k", Models: []string{"deepseek-flash"}}); err != nil {
t.Fatal(err)
}
write(".config/opencode/opencode.jsonc", `{
// keep me
"provider": {
"dial": {"npm": "@ai-sdk/openai-compatible", "name": "dial", "options": {"apiKey": "dial", "baseURL": "http://127.0.0.1:3425/v1"}, "models": {}},
"mine": {"name": "mine"}
},
"model": "dial/deepseek/deepseek-flash",
"theme": "dark"
}
`)
write(".codex/config.toml", "model_reasoning_effort = \"high\"\nmodel = \"deepseek/deepseek-flash\"\nmodel_provider = \"dial\"\nmodel_catalog_json = \""+filepath.Join(home, ".codex", "dial-models.json")+"\"\n\n[projects.\"/x\"]\ntrust_level = \"trusted\"\n\n[model_providers.dial]\nname = \"dial\"\nbase_url = \"http://127.0.0.1:3425/v1\"\nwire_api = \"responses\"\nexperimental_bearer_token = \"dial\"\n")
write(".codex/dial-models.json", "{}")
write(".pi/agent/settings.json", `{"defaultProvider": "dial", "defaultModel": "deepseek/deepseek-flash", "theme": "dark"}`)
write(".pi/agent/models.json", `{"providers": {"dial": {"api": "openai-completions", "apiKey": "dial", "baseUrl": "http://127.0.0.1:3425/v1", "models": []}}}`)
write(".claude/settings.json", `{"env": {"ANTHROPIC_BASE_URL": "`+gateway.URL()+`", "ANTHROPIC_AUTH_TOKEN": "dial", "ANTHROPIC_MODEL": "deepseek/deepseek-flash"}, "theme": "dark"}`)
RenameLegacy()
read := func(rel string) string {
b, err := os.ReadFile(filepath.Join(home, rel))
if err != nil {
t.Fatal(err)
}
return string(b)
}
oc := read(".config/opencode/opencode.jsonc")
if v, _ := edit.GetJSON(filepath.Join(home, ".config/opencode/opencode.jsonc"), "model"); v != "magpie/deepseek/deepseek-flash" {
t.Fatalf("opencode model: %q", v)
}
if !strings.Contains(oc, `"magpie": {`) || strings.Contains(oc, `"dial"`) || !strings.Contains(oc, "// keep me") || !strings.Contains(oc, `"mine"`) {
t.Fatalf("opencode:\n%s", oc)
}
cx := read(".codex/config.toml")
if strings.Contains(cx, "dial") || !strings.Contains(cx, "[model_providers.magpie]") || !strings.Contains(cx, `model_provider = "magpie"`) || !strings.Contains(cx, `[projects."/x"]`) {
t.Fatalf("codex:\n%s", cx)
}
if _, err := os.Stat(filepath.Join(home, ".codex", "dial-models.json")); err == nil {
t.Fatal("dial-models.json still there")
}
if _, err := os.Stat(filepath.Join(home, ".codex", "magpie-models.json")); err != nil {
t.Fatal("magpie-models.json missing")
}
if b := stashLoad(); b["codex.provider"] == "dial" {
t.Fatal("dial stashed as the provider to go back to")
}
if v, _ := edit.GetJSON(filepath.Join(home, ".pi/agent/settings.json"), "defaultProvider"); v != "magpie" {
t.Fatalf("pi provider: %q", v)
}
pm := read(".pi/agent/models.json")
if strings.Contains(pm, `"dial"`) || !strings.Contains(pm, `"magpie"`) {
t.Fatalf("pi models:\n%s", pm)
}
cl := read(".claude/settings.json")
if strings.Contains(cl, `"dial"`) || !strings.Contains(cl, `"ANTHROPIC_AUTH_TOKEN": "magpie"`) || !strings.Contains(cl, `"theme": "dark"`) {
t.Fatalf("claude:\n%s", cl)
}
// a second run leaves every file alone
before := map[string]string{}
for _, rel := range []string{".config/opencode/opencode.jsonc", ".codex/config.toml", ".pi/agent/settings.json", ".pi/agent/models.json", ".claude/settings.json"} {
before[rel] = read(rel)
}
RenameLegacy()
for rel, b := range before {
if read(rel) != b {
t.Fatalf("%s changed on the second run", rel)
}
}
}
+1
View File
@@ -68,6 +68,7 @@ func main() {
func run(args []string) error {
settings.Migrate()
agent.RenameLegacy()
if len(args) == 0 {
if hasGUI {
return runGUI(true)