An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:art: 每个工作空间单独持久化 localStorage https://github.com/siyuan-note/siyuan/issues/6404
Liang Dingde4d40c0f6f6
diff --git a/kernel/api/router.go b/kernel/api/router.goindex f149f83b79..43fe160095 100644--- a/kernel/api/router.go+++ b/kernel/api/router.go@@ -53,8 +53,11 @@ func ServeAPI(ginServer *gin.Engine) {ginServer.Handle("POST", "/api/system/getConf", model.CheckAuth, getConf)ginServer.Handle("POST", "/api/system/checkUpdate", model.CheckAuth, checkUpdate)ginServer.Handle("POST", "/api/system/exportLog", model.CheckAuth, exportLog)- ginServer.Handle("POST", "/api/system/setLocalStorage", model.CheckAuth, setLocalStorage)- ginServer.Handle("POST", "/api/system/getLocalStorage", model.CheckAuth, getLocalStorage)++ ginServer.Handle("POST", "/api/storage/setLocalStorage", model.CheckAuth, setLocalStorage)+ ginServer.Handle("POST", "/api/storage/getLocalStorage", model.CheckAuth, getLocalStorage)+ ginServer.Handle("POST", "/api/storage/setLocalStorageVal", model.CheckAuth, setLocalStorageVal)+ ginServer.Handle("POST", "/api/storage/removeLocalStorageVal", model.CheckAuth, removeLocalStorageVal)ginServer.Handle("POST", "/api/account/login", model.CheckAuth, login)ginServer.Handle("POST", "/api/account/checkActivationcode", model.CheckAuth, checkActivationcode)diff --git a/kernel/api/storage.go b/kernel/api/storage.gonew file mode 100644index 0000000000..b32cad2215--- /dev/null+++ b/kernel/api/storage.go@@ -0,0 +1,97 @@+// SiYuan - Build Your Eternal Digital Garden+// Copyright (c) 2020-present, b3log.org+//+// This program is free software: you can redistribute it and/or modify+// it under the terms of the GNU Affero General Public License as published by+// the Free Software Foundation, either version 3 of the License, or+// (at your option) any later version.+//+// This program is distributed in the hope that it will be useful,+// but WITHOUT ANY WARRANTY; without even the implied warranty of+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+// GNU Affero General Public License for more details.+//+// You should have received a copy of the GNU Affero General Public License+// along with this program. If not, see <https://www.gnu.org/licenses/>.++package api++import (+ "net/http"++ "github.com/88250/gulu"+ "github.com/gin-gonic/gin"+ "github.com/siyuan-note/siyuan/kernel/model"+ "github.com/siyuan-note/siyuan/kernel/util"+)++func removeLocalStorageVal(c *gin.Context) {+ ret := gulu.Ret.NewResult()+ defer c.JSON(http.StatusOK, ret)++ arg, ok := util.JsonArg(c, ret)+ if !ok {+ return+ }++ key := arg["key"].(string)++ err := model.RemoveLocalStorageVal(key)+ if nil != err {+ ret.Code = -1+ ret.Msg = err.Error()+ return+ }+}++func setLocalStorageVal(c *gin.Context) {+ ret := gulu.Ret.NewResult()+ defer c.JSON(http.StatusOK, ret)++ arg, ok := util.JsonArg(c, ret)+ if !ok {+ return+ }++ key := arg["key"].(string)+ val := arg["val"].(interface{})++ err := model.SetLocalStorageVal(key, val)+ if nil != err {+ ret.Code = -1+ ret.Msg = err.Error()+ return+ }+}++func getLocalStorage(c *gin.Context) {+ ret := gulu.Ret.NewResult()+ defer c.JSON(http.StatusOK, ret)++ data, err := model.GetLocalStorage()+ if nil != err {+ ret.Code = -1+ ret.Msg = err.Error()+ return+ }+ ret.Data = data+}++func setLocalStorage(c *gin.Context) {+ ret := gulu.Ret.NewResult()+ defer c.JSON(http.StatusOK, ret)++ arg, ok := util.JsonArg(c, ret)+ if !ok {+ return+ }++ val := arg["val"].(interface{})++ err := model.SetLocalStorage(val)+ if nil != err {+ ret.Code = -1+ ret.Msg = err.Error()+ return+ }+}diff --git a/kernel/api/system.go b/kernel/api/system.goindex 60abdb9bfc..1c9c59ce93 100644--- a/kernel/api/system.go+++ b/kernel/api/system.go@@ -26,7 +26,6 @@ import ("github.com/88250/gulu""github.com/gin-gonic/gin"- "github.com/siyuan-note/filelock""github.com/siyuan-note/logging""github.com/siyuan-note/siyuan/kernel/conf""github.com/siyuan-note/siyuan/kernel/model"@@ -164,66 +163,6 @@ func getConf(c *gin.Context) {}}-func getLocalStorage(c *gin.Context) {- ret := gulu.Ret.NewResult()- defer c.JSON(http.StatusOK, ret)-- lsPath := filepath.Join(util.DataDir, "storage/local.json")- if !gulu.File.IsExist(lsPath) {- return- }-- data, err := filelock.ReadFile(lsPath)- if nil != err {- ret.Code = -1- ret.Msg = err.Error()- return- }-- ls := map[string]interface{}{}- if err = gulu.JSON.UnmarshalJSON(data, &ls); nil != err {- ret.Code = -1- ret.Msg = err.Error()- return- }-- ret.Data = ls-}--func setLocalStorage(c *gin.Context) {- ret := gulu.Ret.NewResult()- defer c.JSON(http.StatusOK, ret)-- arg, ok := util.JsonArg(c, ret)- if !ok {- return- }-- val := arg["val"].(interface{})-- dirPath := filepath.Join(util.DataDir, "storage")- if err := os.MkdirAll(dirPath, 0755); nil != err {- ret.Code = -1- ret.Msg = err.Error()- return- }-- data, err := gulu.JSON.MarshalJSON(val)- if nil != err {- ret.Code = -1- ret.Msg = err.Error()- return- }-- lsPath := filepath.Join(dirPath, "local.json")- err = filelock.WriteFile(lsPath, data)- if nil != err {- ret.Code = -1- ret.Msg = err.Error()- return- }-}-func setUILayout(c *gin.Context) {ret := gulu.Ret.NewResult()defer c.JSON(http.StatusOK, ret)diff --git a/kernel/model/localstorage.go b/kernel/model/localstorage.gonew file mode 100644index 0000000000..3a75a73fc8--- /dev/null+++ b/kernel/model/localstorage.go@@ -0,0 +1,110 @@+// SiYuan - Build Your Eternal Digital Garden+// Copyright (c) 2020-present, b3log.org+//+// This program is free software: you can redistribute it and/or modify+// it under the terms of the GNU Affero General Public License as published by+// the Free Software Foundation, either version 3 of the License, or+// (at your option) any later version.+//+// This program is distributed in the hope that it will be useful,+// but WITHOUT ANY WARRANTY; without even the implied warranty of+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+// GNU Affero General Public License for more details.+//+// You should have received a copy of the GNU Affero General Public License+// along with this program. If not, see <https://www.gnu.org/licenses/>.++package model++import (+ "os"+ "path/filepath"+ "sync"++ "github.com/88250/gulu"+ "github.com/siyuan-note/filelock"+ "github.com/siyuan-note/logging"+ "github.com/siyuan-note/siyuan/kernel/util"+)++var localStorageLock = sync.Mutex{}++func RemoveLocalStorageVal(key string) (err error) {+ localStorageLock.Lock()+ defer localStorageLock.Unlock()++ localStorage, err := getLocalStorage()+ if nil != err {+ return+ }++ delete(localStorage, key)+ return+}++func SetLocalStorageVal(key string, val interface{}) (err error) {+ localStorageLock.Lock()+ defer localStorageLock.Unlock()++ localStorage, err := getLocalStorage()+ if nil != err {+ return+ }++ localStorage[key] = val+ return setLocalStorage(localStorage)+}++func SetLocalStorage(val interface{}) (err error) {+ localStorageLock.Lock()+ defer localStorageLock.Unlock()+ return setLocalStorage(val)+}++func GetLocalStorage() (ret map[string]interface{}, err error) {+ localStorageLock.Lock()+ defer localStorageLock.Unlock()+ return getLocalStorage()+}++func setLocalStorage(val interface{}) (err error) {+ dirPath := filepath.Join(util.DataDir, "storage")+ if err = os.MkdirAll(dirPath, 0755); nil != err {+ logging.LogErrorf("create local storage dir failed: %s", err)+ return+ }++ data, err := gulu.JSON.MarshalJSON(val)+ if nil != err {+ logging.LogErrorf("marshal local storage failed: %s", err)+ return+ }++ lsPath := filepath.Join(dirPath, "local.json")+ err = filelock.WriteFile(lsPath, data)+ if nil != err {+ logging.LogErrorf("write local storage failed: %s", err)+ return+ }+ return+}++func getLocalStorage() (ret map[string]interface{}, err error) {+ lsPath := filepath.Join(util.DataDir, "storage/local.json")+ if !gulu.File.IsExist(lsPath) {+ return+ }++ data, err := filelock.ReadFile(lsPath)+ if nil != err {+ logging.LogErrorf("read local storage failed: %s", err)+ return+ }++ ret = map[string]interface{}{}+ if err = gulu.JSON.UnmarshalJSON(data, &ret); nil != err {+ logging.LogErrorf("unmarshal local storage failed: %s", err)+ return+ }+ return+}