diff --git a/internal/agent/zcode.go b/internal/agent/zcode.go index 5778d8eed..6cd24a556 100644 --- a/internal/agent/zcode.go +++ b/internal/agent/zcode.go @@ -8,12 +8,14 @@ import ( "path/filepath" "slices" + "github.com/yetone/magpie/internal/appdir" "github.com/yetone/magpie/internal/edit" "github.com/yetone/magpie/internal/gateway" ) // ZCode (Zhipu's desktop app) keeps its model providers in -// ~/.zcode/v2/config.json, OpenCode's provider shape with a kind of its own: +// ~/.zcode/v2/config.json (under the folder the user moved its data to, when +// they did: appdir.ZCodeDataIn), OpenCode's provider shape with a kind of its own: // // {"provider":{"magpie":{"name":"magpie","kind":"anthropic", // "options":{"apiKey":"magpie","baseURL":"http://127.0.0.1:3425"}, @@ -53,7 +55,8 @@ import ( // Anthropic Messages. func zcode(home string) *Agent { - dir := filepath.Join(home, ".zcode") + // where ZCode keeps its data, moved there in its settings or not + dir := filepath.Join(appdir.ZCodeDataIn(home), ".zcode") path := filepath.Join(dir, "v2", "config.json") rules := filepath.Join(dir, "v2", "provider_config.json") key := "provider." + magpieID diff --git a/internal/agent/zcode_test.go b/internal/agent/zcode_test.go index 8c6e88430..f05b31cb2 100644 --- a/internal/agent/zcode_test.go +++ b/internal/agent/zcode_test.go @@ -491,3 +491,58 @@ func TestZCodeKeepsUserFormat(t *testing.T) { t.Fatalf("unknown format: %q %q", k, api) } } + +// A ZCode whose data the user moved in its settings (数据存储路径) reads its +// providers from the new folder: magpie's provider goes there, and the home +// folder's .zcode, where only setting.json is left, gets none (flower on +// Discord: magpie said connected, and ZCode's settings had no magpie). +func TestZCodeMovedDataFolder(t *testing.T) { + home := t.TempDir() + t.Setenv("HOME", home) + t.Setenv("USERPROFILE", 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"}}); err != nil { + t.Fatal(err) + } + moved := filepath.Join(t.TempDir(), "ZCodeData") + homeV2, movedV2 := filepath.Join(home, ".zcode", "v2"), filepath.Join(moved, ".zcode", "v2") + os.MkdirAll(homeV2, 0o755) + os.MkdirAll(movedV2, 0o755) + // what ZCode writes when the folder is moved: the choice stays at home, + // the data (an empty provider list among it) is copied over + setting, _ := json.Marshal(map[string]any{"locale": "zh-CN", "dataBaseDir": moved}) + os.WriteFile(filepath.Join(homeV2, "setting.json"), setting, 0o644) + empty := `{"schemaVersion":1,"config":{"providerConfigRules":{"providerRules":[]},"modelConfigRules":{"providerModelRules":[],"manualProviderModelRules":[]}}}` + os.WriteFile(filepath.Join(movedV2, "provider_config.json"), []byte(empty), 0o600) + + a := zcode(home) + f := a.Field("provider") + if err := f.Set("magpie"); err != nil { + t.Fatal(err) + } + if !zcodeRuled(filepath.Join(movedV2, "provider_config.json")) { + b, _ := os.ReadFile(filepath.Join(movedV2, "provider_config.json")) + t.Fatalf("no magpie in the moved folder's provider_config.json: %s", b) + } + if _, ok := edit.GetJSON(filepath.Join(movedV2, "config.json"), "provider.magpie"); !ok { + t.Fatal("no magpie in the moved folder's config.json") + } + for _, n := range []string{"provider_config.json", "config.json"} { + if _, err := os.Stat(filepath.Join(homeV2, n)); err == nil { + t.Fatalf("%s written in the home folder ZCode no longer reads", n) + } + } + if f.Get() != "magpie" { + t.Fatalf("get: %q", f.Get()) + } + + // a relative or empty dataBaseDir is not a folder ZCode moved to + for _, d := range []string{"", " ", "ZCodeData"} { + setting, _ := json.Marshal(map[string]any{"dataBaseDir": d}) + os.WriteFile(filepath.Join(homeV2, "setting.json"), setting, 0o644) + if got := zcode(home).Dir; got != filepath.Join(home, ".zcode") { + t.Fatalf("dataBaseDir %q: %s", d, got) + } + } +} diff --git a/internal/appdir/zcode.go b/internal/appdir/zcode.go new file mode 100644 index 000000000..59a2d316a --- /dev/null +++ b/internal/appdir/zcode.go @@ -0,0 +1,35 @@ +package appdir + +import ( + "encoding/json" + "os" + "path/filepath" + "strings" +) + +// ZCodeDataIn is the folder ZCode (Zhipu's desktop app) keeps its .zcode +// data in, with home as the user's home folder: the home folder, unless the +// user moved it in ZCode's settings (数据存储路径, "data storage path"). +// ZCode then copies its .zcode/v2 there and reads it from there from its +// next start, providers (provider_config.json, config.json) and sign-in +// (credentials.json) included; the choice stays in the home folder's +// .zcode/v2/setting.json as dataBaseDir, which ZCode's main process reads +// before anything else (readBootstrapDataBaseDirFromDisk). A value that +// isn't an absolute path is ignored, as Getenv ignores one. +// +// Skills, AGENTS.md and the CLI's config.json stay under the home folder +// whatever this says: ZCode reads those from HOME/USERPROFILE. +func ZCodeDataIn(home string) string { + var s struct { + DataBaseDir any `json:"dataBaseDir"` + } + b, err := os.ReadFile(filepath.Join(home, ".zcode", "v2", "setting.json")) + if err != nil || json.Unmarshal(b, &s) != nil { + return home + } + d, _ := s.DataBaseDir.(string) + if d = strings.TrimSpace(d); d == "" || !rooted(d) { + return home + } + return d +} diff --git a/internal/provider/zcode.go b/internal/provider/zcode.go index 397deb83e..a008fb835 100644 --- a/internal/provider/zcode.go +++ b/internal/provider/zcode.go @@ -53,6 +53,7 @@ import ( "sync" "time" + "github.com/yetone/magpie/internal/appdir" "github.com/yetone/magpie/internal/catalog" ) @@ -97,7 +98,8 @@ type zcodeKey struct { func zcodeCredentialsPath() string { home, _ := os.UserHomeDir() - return filepath.Join(home, ".zcode", "v2", "credentials.json") + // under the folder ZCode's data was moved to, when it was + return filepath.Join(appdir.ZCodeDataIn(home), ".zcode", "v2", "credentials.json") } // zcodeSecret is the key ZCode encrypts its credentials with. diff --git a/internal/provider/zcode_models.go b/internal/provider/zcode_models.go index 3b57a7110..02a4ee1d9 100644 --- a/internal/provider/zcode_models.go +++ b/internal/provider/zcode_models.go @@ -32,6 +32,7 @@ import ( "slices" "strings" + "github.com/yetone/magpie/internal/appdir" "github.com/yetone/magpie/internal/catalog" "github.com/yetone/magpie/internal/proc" ) @@ -229,7 +230,7 @@ func zcodeRemoteBuiltin(ctx context.Context) (zcodeBuiltin, error) { // it ships with, and the one it last took, under ~/.zcode. var zcodeBuiltinFiles = func() []string { home, _ := os.UserHomeDir() - fs, _ := filepath.Glob(filepath.Join(home, ".zcode", "v2", "runtime", "provider", "*", "*", "*", "zcode-builtin.json")) + fs, _ := filepath.Glob(filepath.Join(appdir.ZCodeDataIn(home), ".zcode", "v2", "runtime", "provider", "*", "*", "*", "zcode-builtin.json")) if runtime.GOOS == "darwin" { fs = append(fs, proc.SystemDirs("/Applications/ZCode.app/Contents/Resources/config/provider/zcode-builtin.json")...) }