Skip to content

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

:bug: Preserve decision profile resets during input migration https://github.com/siyuan-note/siyuan/pull/20710

Danielbf91edae78ff
diff --git a/kernel/api/ai_decision_test.go b/kernel/api/ai_decision_test.go
index 6ff3220c3c..0f2c13438a 100644
--- a/kernel/api/ai_decision_test.go
+++ b/kernel/api/ai_decision_test.go
@@ -5,6 +5,7 @@ import (
"io"
"net/http"
"net/http/httptest"
+ "reflect"
"strings"
"sync/atomic"
"testing"
@@ -222,3 +223,53 @@ func TestAPIContractSettingDecisionLegacySave(t *testing.T) {
})
}
}
+
+func TestAPIContractSettingDecisionProfilesReset(t *testing.T) {
+ for _, route := range []struct {
+ name string
+ handler gin.HandlerFunc
+ }{{"setAI", setAI}, {"patch", patchSetting}} {
+ for _, test := range []struct {
+ name string
+ body string
+ key string
+ profile *conf.DecisionProfile
+ }{
+ {"reset profiles", `{"profiles":null}`, "", nil},
+ {"reset with legacy key", `{"profiles":null,"apiKey":"reset-key"}`, "reset-key", nil},
+ {"case-insensitive reset", `{"Profiles":null,"APIKey":"reset-key"}`, "reset-key", nil},
+ {"reset with null legacy fields", `{"profiles":null,"endpoint":null,"apiKey":null,"name":null,"timeout":null}`, "", nil},
+ {"reset with complete legacy profile", `{"profiles":null,"endpoint":"https://new.example","apiKey":"reset-key","name":"new-model","timeout":45}`, "reset-key",
+ &conf.DecisionProfile{Endpoint: "https://new.example", APIKey: "reset-key", Name: "new-model", Timeout: 45}},
+ } {
+ t.Run(route.name+"/"+test.name, func(t *testing.T) {
+ aiContractConfiguration(t)
+ previousReadOnly := util.ReadOnly
+ util.ReadOnly = true
+ t.Cleanup(func() { util.ReadOnly = previousReadOnly })
+ decision := model.Conf.AI.Load().Decision
+ decision.Enabled, decision.Provider = true, "openai"
+ for _, profile := range decision.Profiles {
+ profile.Endpoint, profile.APIKey, profile.Name, profile.Timeout = "https://old.example", "old-key", "old-model", 90
+ }
+ body := `{"decision":` + test.body + `}`
+ if route.name == "patch" {
+ body = `{"ai":` + body + `}`
+ }
+ code, message, _ := settingContractRequest(t, route.name, route.handler, body)
+ if code != 0 {
+ t.Fatalf("profile reset failed: %s", message)
+ }
+ expected := conf.NewAI().Decision
+ expected.Enabled, expected.Provider = true, "openai"
+ if test.profile != nil {
+ expected.Profiles["typesafe"] = test.profile
+ }
+ expected.Profiles["typesafe"].APIKey = test.key
+ if !reflect.DeepEqual(model.Conf.AI.Load().Decision, expected) {
+ t.Fatal("profile reset retained old settings or lost legacy fields and unrelated decision settings")
+ }
+ })
+ }
+ }
+}
diff --git a/kernel/api/setting_input.go b/kernel/api/setting_input.go
index 03cf2f4298..ade009009c 100644
--- a/kernel/api/setting_input.go
+++ b/kernel/api/setting_input.go
@@ -56,8 +56,9 @@ func migrateDecisionSettingPatch(patch []byte) ([]byte, error) {
if err := json.Unmarshal(fields[decisionKey], &legacy); err != nil {
return nil, err
}
- // 已提交 profiles 时以现代字段为准;显式 null 表示重置,平铺字段仍需迁移。
- if len(legacy.Profiles) > 0 && !bytes.Equal(bytes.TrimSpace(legacy.Profiles), []byte("null")) {
+ // 显式提交 profiles 时保留输入;null 先清除旧配置,再由 Decision.Normalize 从平铺字段重建。
+ // 不能把 null 改成对象,否则递归合并会保留旧凭据和端点。
+ if len(legacy.Profiles) > 0 {
return patch, nil
}