Skip to content

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

:art: Improve configuration initialization https://github.com/siyuan-note/siyuan/issues/17954 (#17989)

Jeffrey Chen5afdb09f2407
diff --git a/kernel/model/conf.go b/kernel/model/conf.go
index 4b7071b90f..30bed1a92e 100644
--- a/kernel/model/conf.go
+++ b/kernel/model/conf.go
@@ -33,7 +33,6 @@ import (
"github.com/88250/lute"
"github.com/88250/lute/ast"
"github.com/Xuanwo/go-locale"
- "github.com/jinzhu/copier"
"github.com/siyuan-note/eventbus"
"github.com/siyuan-note/filelock"
"github.com/siyuan-note/logging"
@@ -98,22 +97,6 @@ func NewAppConf() *AppConf {
}
}
-// mergeConfWithDefaults 将部分解析失败的配置合并到完整默认配置中。
-// 利用 copier 的 IgnoreEmpty 跳过零值/nil 字段,保留已成功解析的配置项,
-// 未成功解析的字段回退到 NewAppConf() 的默认值。
-func mergeConfWithDefaults() {
- fresh := NewAppConf()
- if err := copier.CopyWithOption(fresh, Conf, copier.Option{IgnoreEmpty: true, DeepCopy: true}); err != nil {
- logging.LogErrorf("merge conf with defaults failed: %s", err)
- return
- }
- // copier 会通过 unsafe 复制未导出字段,导致 fresh 的锁(json.Unmarshal 后为 nil)覆盖默认值,
- // 必须在合并后重新初始化锁,否则后续 Lock/Unlock 会 panic。
- fresh.m = &sync.RWMutex{}
- fresh.userLock = &sync.RWMutex{}
- Conf = fresh
-}
-
func (conf *AppConf) GetUILayout() *conf.UILayout {
conf.m.Lock()
defer conf.m.Unlock()
@@ -147,38 +130,17 @@ func InitConf() {
if data, err := os.ReadFile(confPath); err != nil {
logging.LogErrorf("load conf [%s] failed: %s", confPath, err)
} else {
- if conf.NeedsAIMigration(data) {
- if err = gulu.JSON.UnmarshalJSON(data, Conf); err != nil {
- logging.LogWarnf("parse conf failed, merge available config with defaults: %s", err)
- mergeConfWithDefaults()
- } else {
- Conf.AI = conf.MigrateAI(data)
- if nil != Conf.Search && Conf.Search.HanSensitive == nil {
- Conf.Search.SetHanSensitive(true)
- }
- if nil != Conf.AI {
- Conf.AI.DecryptAPIKeys()
- }
- if nil != Conf.Secrets {
- Conf.Secrets.Decrypt()
- }
- Conf.Save()
- logging.LogInfof("migrated AI config [%s]", confPath)
- }
- } else if err = gulu.JSON.UnmarshalJSON(data, Conf); err != nil {
- logging.LogWarnf("parse conf failed, merge available config with defaults: %s", err)
- mergeConfWithDefaults()
+ // 解析失败时保留已成功写入的字段;未导出字段(m、userLock)与未触及的导出字段保持 NewAppConf() 初值。
+ if err = gulu.JSON.UnmarshalJSON(data, Conf); err != nil {
+ logging.LogWarnf("parse conf failed, parsed fields retained: %s", err)
} else {
logging.LogInfof("loaded conf [%s]", confPath)
- if nil != Conf.Search && Conf.Search.HanSensitive == nil {
- Conf.Search.SetHanSensitive(true)
- }
- if nil != Conf.AI {
- Conf.AI.DecryptAPIKeys()
- }
- if nil != Conf.Secrets {
- Conf.Secrets.Decrypt()
- }
+ }
+
+ if conf.NeedsAIMigration(data) {
+ Conf.AI = conf.MigrateAI(data)
+ Conf.Save()
+ logging.LogInfof("migrated AI config [%s]", confPath)
}
}
}
@@ -575,6 +537,9 @@ func InitConf() {
if 1 > Conf.Search.BacklinkMentionKeywordsLimit {
Conf.Search.BacklinkMentionKeywordsLimit = 512
}
+ if nil == Conf.Search.HanSensitive {
+ Conf.Search.SetHanSensitive(true)
+ }
sql.SetHanSensitive(Conf.Search.HanSensitiveVal())
if nil == Conf.Stat {
@@ -629,11 +594,15 @@ func InitConf() {
if nil == Conf.AI {
Conf.AI = conf.NewAI()
+ } else {
+ Conf.AI.DecryptAPIKeys()
}
Conf.AI.Normalize()
if nil == Conf.Secrets {
Conf.Secrets = conf.NewSecrets()
+ } else {
+ Conf.Secrets.Decrypt()
}
if nil == Conf.Variables {