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: Init plugin system https://github.com/siyuan-note/siyuan/issues/8041
Liang Ding109462a22f5a
diff --git a/kernel/api/plugin.go b/kernel/api/plugin.goindex ddcd71ca46..1efd7e690c 100644--- a/kernel/api/plugin.go+++ b/kernel/api/plugin.go@@ -22,6 +22,7 @@ import ("github.com/88250/gulu""github.com/gin-gonic/gin""github.com/siyuan-note/siyuan/kernel/model"+ "github.com/siyuan-note/siyuan/kernel/util")func loadPetals(c *gin.Context) {@@ -31,3 +32,17 @@ func loadPetals(c *gin.Context) {petals := model.LoadPetals()ret.Data = petals}++func setPetalEnabled(c *gin.Context) {+ ret := gulu.Ret.NewResult()+ defer c.JSON(http.StatusOK, ret)++ arg, ok := util.JsonArg(c, ret)+ if !ok {+ return+ }++ packageName := arg["packageName"].(string)+ enabled := arg["enabled"].(bool)+ model.SetPetalEnabled(packageName, enabled)+}diff --git a/kernel/api/router.go b/kernel/api/router.goindex 32d33f9189..8fff995689 100644--- a/kernel/api/router.go+++ b/kernel/api/router.go@@ -345,4 +345,5 @@ func ServeAPI(ginServer *gin.Engine) {ginServer.Handle("POST", "/api/ai/chatGPTWithAction", model.CheckAuth, model.CheckReadonly, chatGPTWithAction)ginServer.Handle("POST", "/api/petal/loadPetals", model.CheckAuth, model.CheckReadonly, loadPetals)+ ginServer.Handle("POST", "/api/petal/setPetalEnabled", model.CheckAuth, model.CheckReadonly, setPetalEnabled)}diff --git a/kernel/model/plugin.go b/kernel/model/plugin.goindex 9061a330b7..28355f9f85 100644--- a/kernel/model/plugin.go+++ b/kernel/model/plugin.go@@ -40,7 +40,46 @@ type Petal struct {I18n map[string]interface{} `json:"i18n"` // i18n text}+func SetPetalEnabled(name string, enabled bool) {+ petals := []*Petal{}+ petalDir := filepath.Join(util.DataDir, "storage", "petal")+ confPath := filepath.Join(petalDir, "petals.json")+ data, err := filelock.ReadFile(confPath)+ if nil != err {+ logging.LogErrorf("read petal file [%s] failed: %s", confPath, err)+ return+ }++ if err = gulu.JSON.UnmarshalJSON(data, &petals); nil != err {+ logging.LogErrorf("unmarshal petals failed: %s", err)+ return+ }++ plugins := bazaar.InstalledPlugins()+ for _, plugin := range plugins {+ id := hash(plugin.URL)+ petal := getPetalByID(id, petals)+ if nil == petal {+ continue+ }++ petal.Enabled = enabled+ break+ }++ if data, err = gulu.JSON.MarshalIndentJSON(petals, "", "\t"); nil != err {+ logging.LogErrorf("marshal petals failed: %s", err)+ return+ }+ if err = filelock.WriteFile(confPath, data); nil != err {+ logging.LogErrorf("write petals [%s] failed: %s", confPath, err)+ return+ }+}+func LoadPetals() (ret []*Petal) {+ ret = []*Petal{}+petalDir := filepath.Join(util.DataDir, "storage", "petal")if err := os.MkdirAll(petalDir, 0755); nil != err {logging.LogErrorf("create petal dir [%s] failed: %s", petalDir, err)@@ -48,8 +87,6 @@ func LoadPetals() (ret []*Petal) {}confPath := filepath.Join(petalDir, "petals.json")-- ret = []*Petal{}if !gulu.File.IsExist(confPath) {data, err := gulu.JSON.MarshalIndentJSON(ret, "", "\t")if nil != err {diff --git a/kernel/server/serve.go b/kernel/server/serve.goindex 69ce7833a8..c568828e10 100644--- a/kernel/server/serve.go+++ b/kernel/server/serve.go@@ -72,6 +72,7 @@ func Serve(fastMode bool) {serveWebSocket(ginServer)serveExport(ginServer)serveWidgets(ginServer)+ servePlugins(ginServer)serveEmojis(ginServer)serveTemplates(ginServer)api.ServeAPI(ginServer)@@ -174,6 +175,10 @@ func serveWidgets(ginServer *gin.Engine) {ginServer.Static("/widgets/", filepath.Join(util.DataDir, "widgets"))}+func servePlugins(ginServer *gin.Engine) {+ ginServer.Static("/plugins/", filepath.Join(util.DataDir, "plugins"))+}+func serveEmojis(ginServer *gin.Engine) {ginServer.Static("/emojis/", filepath.Join(util.DataDir, "emojis"))}