An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:recycle: Move setting input migrations out of the patch handler
Jeffrey Chen6765d6fe8132
diff --git a/kernel/api/ai_decision_test.go b/kernel/api/ai_decision_test.goindex 3452609271..6ff3220c3c 100644--- a/kernel/api/ai_decision_test.go+++ b/kernel/api/ai_decision_test.go@@ -207,6 +207,8 @@ func TestAPIContractSettingDecisionLegacySave(t *testing.T) {}{{`{"decision":{"apiKey":null}}`, ""},{`{"decision":{"apiKey":"stale-key","profiles":{"typesafe":{"apiKey":"modern-key"}}}}`, "modern-key"},+ // profiles 显式 null 表示重置,平铺字段仍需迁移到 typesafe。+ {`{"decision":{"profiles":null,"apiKey":"reset-key"}}`, "reset-key"},} {body := test.bodyif route.name == "patch" {@@ -214,7 +216,7 @@ func TestAPIContractSettingDecisionLegacySave(t *testing.T) {}code, message, _ := settingContractRequest(t, route.name, route.handler, body)if code != 0 || model.Conf.AI.Load().Decision.Profiles["typesafe"].APIKey != test.key {- t.Fatalf("legacy clearing or modern precedence failed: %s", message)+ t.Fatalf("legacy clearing, reset or modern precedence failed: %s", message)}}})diff --git a/kernel/api/setting_input.go b/kernel/api/setting_input.gonew file mode 100644index 0000000000..03cf2f4298--- /dev/null+++ b/kernel/api/setting_input.go@@ -0,0 +1,158 @@+package api++import (+ "bytes"+ "encoding/json"+ "strings"++ "github.com/siyuan-note/siyuan/kernel/model"+)++// mergeSettingPatch 对设置写入统一执行输入迁移,再合并当前配置段。+// 旧接口与 patch 接口共用同一套迁移,避免两条入口对旧客户端输入的处理不一致。+func mergeSettingPatch(namespace string, patch []byte) ([]byte, error) {+ var err error+ switch namespace {+ case "ai":+ patch, err = migrateDecisionSettingPatch(patch)+ case "editor":+ patch, err = migrateEditorSettingPatch(patch)+ }+ if err != nil {+ return nil, err+ }+ return model.ConfSectionPatchJSON(namespace, patch)+}++// migrateDecisionSettingPatch 把旧客户端的平铺供应商字段迁移到 profiles.typesafe。+// 合并会保留段内已存在的 profiles,平铺字段若不迁移就会被默认配置遮蔽。+func migrateDecisionSettingPatch(patch []byte) ([]byte, error) {+ var fields map[string]json.RawMessage+ if err := json.Unmarshal(patch, &fields); err != nil {+ return nil, err+ }+ decisionKey := ""+ for key := range fields {+ // 同时出现两种写法时取精确的 decision,保证结果与键顺序无关。+ if strings.EqualFold(key, "decision") && ("" == decisionKey || "decision" == key) {+ decisionKey = key+ }+ }+ if "" == decisionKey {+ return patch, nil+ }+ var decision map[string]json.RawMessage+ if json.Unmarshal(fields[decisionKey], &decision) != nil || nil == decision {+ return patch, nil+ }++ var legacy struct {+ Profiles json.RawMessage `json:"profiles"`+ Endpoint json.RawMessage `json:"endpoint"`+ APIKey json.RawMessage `json:"apiKey"`+ Name json.RawMessage `json:"name"`+ Timeout json.RawMessage `json:"timeout"`+ }+ 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")) {+ return patch, nil+ }++ profile := map[string]json.RawMessage{}+ for _, field := range []struct {+ name string+ value json.RawMessage+ }{{"endpoint", legacy.Endpoint}, {"apiKey", legacy.APIKey}, {"name", legacy.Name}, {"timeout", legacy.Timeout}} {+ if 0 == len(field.value) {+ continue+ }+ // 旧设置字段允许 null,保持清空密钥和恢复默认参数的既有语义。+ value := field.value+ if bytes.Equal(bytes.TrimSpace(value), []byte("null")) {+ value = json.RawMessage(`""`)+ if "timeout" == field.name {+ value = json.RawMessage(`0`)+ }+ }+ profile[field.name] = value+ for key := range decision {+ if strings.EqualFold(key, field.name) {+ delete(decision, key)+ }+ }+ }+ if 0 == len(profile) {+ return patch, nil+ }++ profiles, err := json.Marshal(map[string]map[string]json.RawMessage{"typesafe": profile})+ if err != nil {+ return nil, err+ }+ decision["profiles"] = profiles+ for key := range fields {+ if strings.EqualFold(key, "decision") {+ delete(fields, key)+ }+ }+ fields["decision"], err = json.Marshal(decision)+ if err != nil {+ return nil, err+ }+ return json.Marshal(fields)+}++// migrateEditorSettingPatch 在旧客户端只提交首选字体时重建字体列表。+// 合并后的配置段始终带有 fontFamilies,而规范化会用该列表反向覆盖首选字体,+// 因此这里清空列表让规范化按新的首选字体重建;显式提交的字体列表优先。+func migrateEditorSettingPatch(patch []byte) ([]byte, error) {+ var legacy struct {+ FontFamilies json.RawMessage `json:"fontFamilies"`+ FontFamily json.RawMessage `json:"fontFamily"`+ FontWeight json.RawMessage `json:"fontWeight"`+ FontFamilyDisplay json.RawMessage `json:"fontFamilyDisplay"`+ }+ if err := json.Unmarshal(patch, &legacy); err != nil {+ return nil, err+ }+ // 提交非空字体列表时以客户端为准;其余情况都按首选字体重建,避免保留与字体不符的展示名。+ if (len(legacy.FontFamilies) > 0 && !bytes.Equal(bytes.TrimSpace(legacy.FontFamilies), []byte("null"))) ||+ (0 == len(legacy.FontFamily) && 0 == len(legacy.FontWeight)) || nil == model.Conf {+ return patch, nil+ }+ current := model.Conf.Editor.Load()+ if nil == current {+ return patch, nil+ }+ // 缺失时沿用当前值,显式 null 解析为零值,与规范化对空首选字体的处理一致。+ family, weight := current.FontFamily, float64(current.FontWeight)+ if len(legacy.FontFamily) > 0 {+ family = ""+ if err := json.Unmarshal(legacy.FontFamily, &family); err != nil {+ return patch, nil+ }+ }+ if len(legacy.FontWeight) > 0 {+ weight = 0+ if err := json.Unmarshal(legacy.FontWeight, &weight); err != nil {+ return patch, nil+ }+ }+ if family == current.FontFamily && weight == float64(current.FontWeight) {+ return patch, nil+ }++ var fields map[string]json.RawMessage+ if err := json.Unmarshal(patch, &fields); err != nil {+ return nil, err+ }+ fields["fontFamilies"] = json.RawMessage(`null`)+ // 未显式提交展示名时一并清空,由规范化按新的首选字体重新推导。+ if 0 == len(legacy.FontFamilyDisplay) {+ fields["fontFamilyDisplay"] = json.RawMessage(`null`)+ }+ return json.Marshal(fields)+}diff --git a/kernel/api/setting_patch.go b/kernel/api/setting_patch.goindex 9787f1c78e..bdcd9c5007 100644--- a/kernel/api/setting_patch.go+++ b/kernel/api/setting_patch.go@@ -5,7 +5,6 @@ import ("encoding/json""fmt""sort"- "strings""sync""sync/atomic"@@ -86,136 +85,6 @@ func serializeSetting[Request, Data any](namespace string,}}-// migrateDecisionSettingPatch 在合并前识别旧客户端的平铺字段,避免被现有 profiles 遮蔽。-func migrateDecisionSettingPatch(patch []byte) ([]byte, error) {- var ai map[string]json.RawMessage- if err := json.Unmarshal(patch, &ai); err != nil {- return nil, err- }- var input struct {- Decision json.RawMessage `json:"decision"`- }- if err := json.Unmarshal(patch, &input); err != nil {- return nil, err- }- var decision map[string]json.RawMessage- if json.Unmarshal(input.Decision, &decision) != nil || decision == nil {- return patch, nil- }- var legacy struct {- Profiles json.RawMessage `json:"profiles"`- Endpoint json.RawMessage `json:"endpoint"`- APIKey json.RawMessage `json:"apiKey"`- Name json.RawMessage `json:"name"`- Timeout json.RawMessage `json:"timeout"`- }- if err := json.Unmarshal(input.Decision, &legacy); err != nil {- return nil, err- }- if len(legacy.Profiles) > 0 {- return patch, nil- }- profile := map[string]json.RawMessage{}- for _, field := range []struct {- name string- value json.RawMessage- }{{"endpoint", legacy.Endpoint}, {"apiKey", legacy.APIKey}, {"name", legacy.Name}, {"timeout", legacy.Timeout}} {- if value := field.value; len(value) > 0 {- // 旧设置字段允许 null,保持清空密钥和恢复默认参数的既有语义。- if bytes.Equal(bytes.TrimSpace(value), []byte("null")) {- value = json.RawMessage(`""`)- if field.name == "timeout" {- value = json.RawMessage(`0`)- }- }- profile[field.name] = value- for key := range decision {- if strings.EqualFold(key, field.name) {- delete(decision, key)- }- }- }- }- if len(profile) == 0 {- return patch, nil- }- profiles, err := json.Marshal(map[string]map[string]json.RawMessage{"typesafe": profile})- if err != nil {- return nil, err- }- decision["profiles"] = profiles- for key := range ai {- if strings.EqualFold(key, "decision") {- delete(ai, key)- }- }- ai["decision"], err = json.Marshal(decision)- if err != nil {- return nil, err- }- return json.Marshal(ai)-}--// migrateEditorSettingPatch 在首选字体变化时重新生成字体列表,显式提交的字体列表优先。-func migrateEditorSettingPatch(patch []byte) ([]byte, error) {- var legacy struct {- FontFamilies json.RawMessage `json:"fontFamilies"`- FontFamily json.RawMessage `json:"fontFamily"`- FontWeight json.RawMessage `json:"fontWeight"`- FontFamilyDisplay json.RawMessage `json:"fontFamilyDisplay"`- }- if err := json.Unmarshal(patch, &legacy); err != nil {- return nil, err- }- if len(legacy.FontFamilies) > 0 || len(legacy.FontFamily) == 0 && len(legacy.FontWeight) == 0 || model.Conf == nil {- return patch, nil- }- current := model.Conf.Editor.Load()- if current == nil {- return patch, nil- }- family, weight := current.FontFamily, float64(current.FontWeight)- if len(legacy.FontFamily) > 0 {- family = ""- if err := json.Unmarshal(legacy.FontFamily, &family); err != nil {- return patch, nil- }- }- if len(legacy.FontWeight) > 0 {- weight = 0- if err := json.Unmarshal(legacy.FontWeight, &weight); err != nil {- return patch, nil- }- }- if family == current.FontFamily && weight == float64(current.FontWeight) {- return patch, nil- }- var fields map[string]json.RawMessage- if err := json.Unmarshal(patch, &fields); err != nil {- return nil, err- }- fields["fontFamilies"] = json.RawMessage(`null`)- if len(legacy.FontFamilyDisplay) == 0 {- fields["fontFamilyDisplay"] = json.RawMessage(`null`)- }- return json.Marshal(fields)-}--// mergeSettingPatch 对设置写入统一执行输入迁移,再合并当前配置段。-func mergeSettingPatch(namespace string, patch []byte) ([]byte, error) {- var err error- switch namespace {- case "ai":- patch, err = migrateDecisionSettingPatch(patch)- case "editor":- patch, err = migrateEditorSettingPatch(patch)- }- if err != nil {- return nil, err- }- return model.ConfSectionPatchJSON(namespace, patch)-}-// applySettingPatch 先把补丁合并进当前配置段再交给对应接口的处理函数,命名空间取自契约声明。func applySettingPatch[Request, Data any](c *gin.Context, patch []byte,endpoint apicontract.Endpoint[Request, Data], apply func(*gin.Context, Request) apicontract.Response[Data]) apicontract.Response[*apicontract.SettingPatchData] {diff --git a/kernel/api/setting_test.go b/kernel/api/setting_test.goindex a309b16529..60c5e9a379 100644--- a/kernel/api/setting_test.go+++ b/kernel/api/setting_test.go@@ -102,6 +102,7 @@ func TestAPIContractSettingEditorLegacyFont(t *testing.T) {{"explicit list wins", `{"fontFamily":"Verdana","fontWeight":600,"fontFamilies":[{"family":"Georgia","weight":500}]}`, "Georgia", 500, 1, ""},{"case-insensitive list wins", `{"fontFamily":"Verdana","FontFamilies":[{"family":"Georgia","weight":500}]}`, "Georgia", 500, 1, ""},{"null legacy font resets", `{"fontFamily":null}`, "", 400, 0, ""},+ {"null font list rebuilds", `{"fontFamily":"Verdana","fontFamilies":null}`, "Verdana", 400, 1, ""},{"explicit legacy display", `{"fontFamily":"Verdana","fontFamilyDisplay":"Verdana Regular"}`, "Verdana", 400, 1, "Verdana Regular"},} {t.Run(route.name+"/"+test.name, func(t *testing.T) {diff --git a/kernel/model/conf_patch_test.go b/kernel/model/conf_patch_test.goindex 474ca853be..186b2dd416 100644--- a/kernel/model/conf_patch_test.go+++ b/kernel/model/conf_patch_test.go@@ -37,3 +37,14 @@ func TestMergeConfSectionJSONFieldCaseDeterministic(t *testing.T) {}}}++func TestMergeConfJSONKeepsExactKeys(t *testing.T) {+ // 没有结构体类型的通用合并只按精确键名匹配,由调用方决定是否需要大小写兼容。+ merged, err := MergeConfJSON([]byte(`{"openFile":"F1"}`), []byte(`{"openfile":"F2"}`))+ if err != nil {+ t.Fatal(err)+ }+ if string(merged) != `{"openFile":"F1","openfile":"F2"}` {+ t.Fatalf("untyped merge conflated independently named keys: %s", merged)+ }+}