diff --git a/internal/agent/t3code.go b/internal/agent/t3code.go index 8367a36ac..30f0fee42 100644 --- a/internal/agent/t3code.go +++ b/internal/agent/t3code.go @@ -34,6 +34,7 @@ package agent // asks the gateway. import ( + "encoding/json" "os" "path/filepath" "runtime" @@ -66,7 +67,7 @@ func t3code(home string) *Agent { // found by its bundle. No command: `t3` is a common name. detect: func() bool { return isDir(base) || t3App(home) }, Sync: func() error { - return syncJSON(path, t3Instance, func() any { return t3InstanceJSON(path, gateway.URL()) }) + return syncJSON(path, t3Instance, func() any { return t3Merged(path, gateway.URL()) }) }, // its custom models are the models picked for it (#1508) ListsModels: true, @@ -82,7 +83,7 @@ func t3code(home string) *Agent { if v == "" { return edit.DelJSON(path, t3Instance) } - return edit.SetJSON(path, edit.KV{Path: t3Instance, Value: t3InstanceJSON(path, gateway.URL())}) + return edit.SetJSON(path, edit.KV{Path: t3Instance, Value: t3Merged(path, gateway.URL())}) }, Options: func(map[string]string) []Option { return []Option{{Value: magpieID, Label: "magpie", Icon: "magpie", Note: "every magpie model as a provider in T3 Code, on Claude Code"}} @@ -151,6 +152,56 @@ func t3InstanceJSON(path, gw string) map[string]any { } } +// t3Env is the environment variables of magpie's instance that magpie +// sets: the gateway's address and key, and auto mode's review. +var t3Env = []string{"ANTHROPIC_BASE_URL", "ANTHROPIC_AUTH_TOKEN", claudeReviewEnv} + +// t3Merged is magpie's instance as t3InstanceJSON writes it, merged into +// the one at path: what the user set on it in T3 Code's own settings +// stays (#2030, Kanscape: its name and Auto-compact after were reset on +// every start). Its name, whether it is on, and every key magpie doesn't +// write (config.autoCompactWindow, config.launchArgs, accentColor) are +// theirs, as are the environment variables besides magpie's own. The +// custom models and the gateway's variables are magpie's, and so is a +// binary path or Claude home while T3's own Claude has one to carry over. +// Only magpie's instance is read: another, such as T3's own Claude, lends +// it nothing of the user's. +func t3Merged(path, gw string) any { + want := t3InstanceJSON(path, gw) + cur, ok := edit.GetJSON(path, t3Instance) + if !ok { + return want + } + var have map[string]json.RawMessage + if json.Unmarshal([]byte(cur), &have) != nil || have == nil { + return want + } + var name string + if json.Unmarshal(have["displayName"], &name) == nil && strings.TrimSpace(name) != "" { + want["displayName"] = name + } + var on bool + if json.Unmarshal(have["enabled"], &on) == nil { + want["enabled"] = on + } + var theirs []map[string]any + if json.Unmarshal(have["environment"], &theirs) == nil { + env := want["environment"].([]map[string]any) + for _, v := range theirs { + if n, _ := v["name"].(string); n != "" && !slices.Contains(t3Env, n) { + env = append(env, v) + } + } + want["environment"] = env + } + b, err := json.Marshal(want) + var w map[string]json.RawMessage + if err != nil || json.Unmarshal(b, &w) != nil { + return want + } + return mergeTheirs(have, w, []string{"environment"}) +} + // t3EffortLabels are T3 Code's names for Claude Code's levels, as its own // Claude models show them. var t3EffortLabels = map[string]string{"low": "Low", "medium": "Medium", "high": "High", "xhigh": "Extra High", "max": "Max"} diff --git a/internal/agent/t3code_test.go b/internal/agent/t3code_test.go index 1a8f80856..8b1a1d711 100644 --- a/internal/agent/t3code_test.go +++ b/internal/agent/t3code_test.go @@ -9,6 +9,7 @@ import ( "slices" "testing" + "github.com/yetone/magpie/internal/edit" "github.com/yetone/magpie/internal/gateway" "github.com/yetone/magpie/internal/provider" ) @@ -328,3 +329,107 @@ func TestT3CodeEfforts(t *testing.T) { } } } + +// What the user set on magpie's instance in T3 Code's own settings stays +// through every sync (#2030, Kanscape): its name, Auto-compact after, +// launch arguments, a variable of their own and whether it is on, while +// the models and the gateway's variables stay magpie's. T3's own Claude, +// an instance beside it with a name and window of its own, neither changes +// nor lends magpie's instance anything. +func TestT3CodeKeepsTheUsersSettings(t *testing.T) { + home := syncHome(t) + t.Setenv("T3CODE_HOME", "") + path := filepath.Join(home, ".t3", "userdata", "settings.json") + a := t3code(home) + os.MkdirAll(filepath.Dir(path), 0o755) + const claude = `{"driver":"claudeAgent","displayName":"Claude (sub)","enabled":true,"config":{"autoCompactWindow":"200000","launchArgs":"--chrome"}}` + os.WriteFile(path, []byte(`{"providerInstances":{"claudeAgent":`+claude+`}}`), 0o600) + f := a.Field("provider") + if err := f.Set(magpieID); err != nil { + t.Fatal(err) + } + read := func() map[string]json.RawMessage { + var file struct { + ProviderInstances map[string]json.RawMessage `json:"providerInstances"` + } + if err := json.Unmarshal([]byte(readFile(path)), &file); err != nil { + t.Fatalf("%v\n%s", err, readFile(path)) + } + return file.ProviderInstances + } + var mine map[string]any + json.Unmarshal(read()[magpieID], &mine) + if mine["displayName"] != "magpie" { + t.Fatalf("first written as %v", mine["displayName"]) + } + cfg := mine["config"].(map[string]any) + if _, ok := cfg["autoCompactWindow"]; ok { + t.Fatalf("the Claude instance's window lent to magpie's: %v", cfg) + } + + // the user edits magpie's instance in T3 Code, as T3 saves it + mine["displayName"] = "Magpie GW" + mine["enabled"] = false + mine["accentColor"] = "#ff8800" + cfg["autoCompactWindow"] = "300000" + cfg["launchArgs"] = "--verbose" + cfg["customModels"] = []any{map[string]any{"slug": "gone/old", "name": "old"}} + env := mine["environment"].([]any) + for _, v := range env { + if m := v.(map[string]any); m["name"] == "ANTHROPIC_BASE_URL" { + m["value"] = "http://127.0.0.1:1" + } + } + mine["environment"] = append(env, map[string]any{"name": "HTTPS_PROXY", "value": "http://proxy:8080", "sensitive": false}) + if err := edit.SetJSON(path, edit.KV{Path: t3Instance, Value: mine}); err != nil { + t.Fatal(err) + } + + for range 3 { + if err := a.Sync(); err != nil { + t.Fatal(err) + } + } + insts := read() + if !sameJSON(string(insts["claudeAgent"]), json.RawMessage(claude)) { + t.Errorf("T3's own Claude changed: %s", insts["claudeAgent"]) + } + var got struct { + DisplayName string `json:"displayName"` + Enabled *bool `json:"enabled"` + AccentColor string `json:"accentColor"` + Environment []struct { + Name, Value string + } `json:"environment"` + Config struct { + AutoCompactWindow string `json:"autoCompactWindow"` + LaunchArgs string `json:"launchArgs"` + CustomModels []struct { + Slug string `json:"slug"` + } `json:"customModels"` + } `json:"config"` + } + if err := json.Unmarshal(insts[magpieID], &got); err != nil { + t.Fatal(err) + } + if got.DisplayName != "Magpie GW" || got.Enabled == nil || *got.Enabled || got.AccentColor != "#ff8800" { + t.Errorf("the user's name, switch or colour not kept: %s", insts[magpieID]) + } + if got.Config.AutoCompactWindow != "300000" || got.Config.LaunchArgs != "--verbose" { + t.Errorf("the user's config not kept: %+v", got.Config) + } + envs := map[string]string{} + for _, v := range got.Environment { + envs[v.Name] = v.Value + } + if envs["HTTPS_PROXY"] != "http://proxy:8080" || envs["ANTHROPIC_BASE_URL"] != gateway.URL() || len(got.Environment) != len(env)+1 { + t.Errorf("environment: %v", got.Environment) + } + if slices.ContainsFunc(got.Config.CustomModels, func(m struct { + Slug string `json:"slug"` + }) bool { + return m.Slug == "gone/old" + }) || len(got.Config.CustomModels) == 0 { + t.Errorf("custom models not magpie's: %+v", got.Config.CustomModels) + } +}