Skip to content

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

:art: Plugin hot load

Liang Dingbf4b88a00770
diff --git a/kernel/api/plugin.go b/kernel/api/plugin.go
index 1efd7e690c..a442985029 100644
--- a/kernel/api/plugin.go
+++ b/kernel/api/plugin.go
@@ -44,5 +44,5 @@ func setPetalEnabled(c *gin.Context) {
packageName := arg["packageName"].(string)
enabled := arg["enabled"].(bool)
- model.SetPetalEnabled(packageName, enabled)
+ ret.Data = model.SetPetalEnabled(packageName, enabled)
}
diff --git a/kernel/model/plugin.go b/kernel/model/plugin.go
index f8d421d3a4..3c249fe128 100644
--- a/kernel/model/plugin.go
+++ b/kernel/model/plugin.go
@@ -38,7 +38,7 @@ type Petal struct {
I18n map[string]interface{} `json:"i18n"` // i18n text
}
-func SetPetalEnabled(name string, enabled bool) {
+func SetPetalEnabled(name string, enabled bool) (ret *Petal) {
petals := getPetals()
plugins := bazaar.InstalledPlugins()
@@ -54,18 +54,21 @@ func SetPetalEnabled(name string, enabled bool) {
return
}
- petal := getPetalByName(plugin.Name, petals)
- if nil == petal {
- petal = &Petal{
+ ret = getPetalByName(plugin.Name, petals)
+ if nil == ret {
+ ret = &Petal{
Name: plugin.Name,
Enabled: enabled,
}
- petals = append(petals, petal)
+ petals = append(petals, ret)
} else {
- petal.Enabled = enabled
+ ret.Enabled = enabled
}
savePetals(petals)
+
+ loadCode(ret)
+ return
}
func LoadPetals() (ret []*Petal) {
@@ -76,78 +79,81 @@ func LoadPetals() (ret []*Petal) {
continue
}
- pluginDir := filepath.Join(util.DataDir, "plugins", petal.Name)
- jsPath := filepath.Join(pluginDir, "index.js")
- if !gulu.File.IsExist(jsPath) {
- logging.LogErrorf("plugin [%s] js not found", petal.Name)
- continue
- }
+ loadCode(petal)
+ ret = append(ret, petal)
+ }
+ return
+}
+
+func loadCode(petal *Petal) {
+ pluginDir := filepath.Join(util.DataDir, "plugins", petal.Name)
+ jsPath := filepath.Join(pluginDir, "index.js")
+ if !gulu.File.IsExist(jsPath) {
+ logging.LogErrorf("plugin [%s] js not found", petal.Name)
+ return
+ }
- data, err := filelock.ReadFile(jsPath)
+ data, err := filelock.ReadFile(jsPath)
+ if nil != err {
+ logging.LogErrorf("read plugin [%s] js failed: %s", petal.Name, err)
+ return
+ }
+ petal.JS = string(data)
+
+ cssPath := filepath.Join(pluginDir, "index.css")
+ if gulu.File.IsExist(cssPath) {
+ data, err = filelock.ReadFile(cssPath)
if nil != err {
- logging.LogErrorf("read plugin [%s] js failed: %s", petal.Name, err)
- continue
+ logging.LogErrorf("read plugin [%s] css failed: %s", petal.Name, err)
+ } else {
+ petal.CSS = string(data)
}
- petal.JS = string(data)
+ }
- cssPath := filepath.Join(pluginDir, "index.css")
- if gulu.File.IsExist(cssPath) {
- data, err := filelock.ReadFile(cssPath)
- if nil != err {
- logging.LogErrorf("read plugin [%s] css failed: %s", petal.Name, err)
- } else {
- petal.CSS = string(data)
+ i18nDir := filepath.Join(pluginDir, "i18n")
+ if gulu.File.IsDir(i18nDir) {
+ langJSONs, readErr := os.ReadDir(i18nDir)
+ if nil != readErr {
+ logging.LogErrorf("read plugin [%s] i18n failed: %s", petal.Name, readErr)
+ } else {
+ preferredLang := Conf.Lang + ".json"
+ foundPreferredLang := false
+ foundEnUS := false
+ foundZhCN := false
+ for _, langJSON := range langJSONs {
+ if langJSON.Name() == preferredLang {
+ foundPreferredLang = true
+ break
+ }
+ if langJSON.Name() == "en_US.json" {
+ foundEnUS = true
+ }
+ if langJSON.Name() == "zh_CN.json" {
+ foundZhCN = true
+ }
+ }
+
+ if !foundPreferredLang {
+ if foundEnUS {
+ preferredLang = "en_US.json"
+ } else if foundZhCN {
+ preferredLang = "zh_CN.json"
+ } else {
+ preferredLang = langJSONs[0].Name()
+ }
}
- }
- i18nDir := filepath.Join(pluginDir, "i18n")
- if gulu.File.IsDir(i18nDir) {
- langJSONs, err := os.ReadDir(i18nDir)
+ data, err = filelock.ReadFile(filepath.Join(i18nDir, preferredLang))
if nil != err {
logging.LogErrorf("read plugin [%s] i18n failed: %s", petal.Name, err)
} else {
- preferredLang := Conf.Lang + ".json"
- foundPreferredLang := false
- foundEnUS := false
- foundZhCN := false
- for _, langJSON := range langJSONs {
- if langJSON.Name() == preferredLang {
- foundPreferredLang = true
- break
- }
- if langJSON.Name() == "en_US.json" {
- foundEnUS = true
- }
- if langJSON.Name() == "zh_CN.json" {
- foundZhCN = true
- }
- }
-
- if !foundPreferredLang {
- if foundEnUS {
- preferredLang = "en_US.json"
- } else if foundZhCN {
- preferredLang = "zh_CN.json"
- } else {
- preferredLang = langJSONs[0].Name()
- }
- }
-
- data, err := filelock.ReadFile(filepath.Join(i18nDir, preferredLang))
- if nil != err {
- logging.LogErrorf("read plugin [%s] i18n failed: %s", petal.Name, err)
- } else {
- petal.I18n = map[string]interface{}{}
- if err = gulu.JSON.UnmarshalJSON(data, &petal.I18n); nil != err {
- logging.LogErrorf("unmarshal plugin [%s] i18n failed: %s", petal.Name, err)
- }
+ petal.I18n = map[string]interface{}{}
+ if err = gulu.JSON.UnmarshalJSON(data, &petal.I18n); nil != err {
+ logging.LogErrorf("unmarshal plugin [%s] i18n failed: %s", petal.Name, err)
}
}
}
-
- ret = append(ret, petal)
}
- return
}
var petalsStoreLock = sync.Mutex{}