Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:art: Improve workspace path update

Daniele493630d6d74
diff --git a/kernel/api/workspace.go b/kernel/api/workspace.go
index 9fcbddd177..a3dc8a24f3 100644
--- a/kernel/api/workspace.go
+++ b/kernel/api/workspace.go
@@ -152,7 +152,7 @@ func removeWorkspaceDir(c *gin.Context) {
return
}
- workspacePaths = gulu.Str.RemoveElem(workspacePaths, path)
+ workspacePaths = util.RemoveWorkspacePath(workspacePaths, path)
if err = util.WriteWorkspacePaths(workspacePaths); err != nil {
ret.Code = -1
@@ -318,8 +318,8 @@ func setWorkspaceDir(c *gin.Context) {
}
workspacePaths = append(workspacePaths, path)
- workspacePaths = gulu.Str.RemoveDuplicatedElem(workspacePaths)
- workspacePaths = gulu.Str.RemoveElem(workspacePaths, path)
+ workspacePaths = util.DeduplicateWorkspacePaths(workspacePaths)
+ workspacePaths = util.RemoveWorkspacePath(workspacePaths, path)
workspacePaths = append(workspacePaths, path) // 切换的工作空间固定放在最后一个
if err = util.WriteWorkspacePaths(workspacePaths); err != nil {
diff --git a/kernel/model/conf.go b/kernel/model/conf.go
index 6dd7bcc350..75170f2c0e 100644
--- a/kernel/model/conf.go
+++ b/kernel/model/conf.go
@@ -799,7 +799,7 @@ func Close(force, setCurrentWorkspace bool, execInstallPkg int) (exitCode int) {
if err != nil {
logging.LogErrorf("read workspace paths failed: %s", err)
} else {
- workspacePaths = gulu.Str.RemoveElem(workspacePaths, util.WorkspaceDir)
+ workspacePaths = util.RemoveWorkspacePath(workspacePaths, util.WorkspaceDir)
workspacePaths = append(workspacePaths, util.WorkspaceDir)
util.WriteWorkspacePaths(workspacePaths)
}
diff --git a/kernel/util/working.go b/kernel/util/working.go
index 313a2c0be0..1a34a26237 100644
--- a/kernel/util/working.go
+++ b/kernel/util/working.go
@@ -334,6 +334,38 @@ func initWorkspaceDir(workspaceArg string) {
ShortcutsPath = filepath.Join(userHomeConfDir, "shortcuts")
}
+func DeduplicateWorkspacePaths(paths []string) []string {
+ if !gulu.OS.IsWindows() {
+ return gulu.Str.RemoveDuplicatedElem(paths)
+ }
+ seen := map[string]bool{}
+ var result []string
+ for _, p := range paths {
+ key := strings.ToLower(p)
+ if seen[key] {
+ continue
+ }
+ seen[key] = true
+ result = append(result, p)
+ }
+ return result
+}
+
+func RemoveWorkspacePath(paths []string, target string) []string {
+ if !gulu.OS.IsWindows() {
+ return gulu.Str.RemoveElem(paths, target)
+ }
+ targetLower := strings.ToLower(target)
+ var result []string
+ for _, p := range paths {
+ if strings.ToLower(p) == targetLower {
+ continue
+ }
+ result = append(result, p)
+ }
+ return result
+}
+
func ReadWorkspacePaths() (ret []string, err error) {
ret = []string{}
workspaceConf := filepath.Join(HomeDir, ".config", "siyuan", "workspace.json")
@@ -369,12 +401,12 @@ func ReadWorkspacePaths() (ret []string, err error) {
}
}
ret = tmp
- ret = gulu.Str.RemoveDuplicatedElem(ret)
+ ret = DeduplicateWorkspacePaths(ret)
return
}
func WriteWorkspacePaths(workspacePaths []string) (err error) {
- workspacePaths = gulu.Str.RemoveDuplicatedElem(workspacePaths)
+ workspacePaths = DeduplicateWorkspacePaths(workspacePaths)
workspaceConf := filepath.Join(HomeDir, ".config", "siyuan", "workspace.json")
data, err := gulu.JSON.MarshalJSON(workspacePaths)
if err != nil {