An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:sparkles: New settings UI https://github.com/siyuan-note/siyuan/issues/17601
Danielea892bca7ebc
diff --git a/kernel/api/agent.go b/kernel/api/agent.goindex 51caf9e52c..e6a681ded6 100644--- a/kernel/api/agent.go+++ b/kernel/api/agent.go@@ -74,7 +74,7 @@ func agentChat(c *gin.Context) {c.JSON(http.StatusOK, ret)return}- client := util.NewOpenAIClient(string(selectedProvider.APIKey), selectedProvider.BaseURL)+ client := util.NewOpenAIClient(selectedProvider.APIKey, selectedProvider.BaseURL)confirmTimeout := time.Duration(model.Conf.AI.Agent.ConfirmTimeout) * time.Secondif confirmTimeout <= 0 {@@ -198,7 +198,7 @@ func agentChatTitle(c *gin.Context) {c.JSON(http.StatusOK, ret)return}- client := util.NewOpenAIClient(string(selectedProvider.APIKey), selectedProvider.BaseURL)+ client := util.NewOpenAIClient(selectedProvider.APIKey, selectedProvider.BaseURL)title := agent.GenerateTitle(client, selectedModel.Name, req.Message, req.Language)ret := gulu.Ret.NewResult()diff --git a/kernel/api/setting.go b/kernel/api/setting.goindex 2e65666528..2c46c4f748 100644--- a/kernel/api/setting.go+++ b/kernel/api/setting.go@@ -233,9 +233,6 @@ func setAI(c *gin.Context) {if nil == ai.Agent {ai.Agent = model.Conf.AI.Agent}- if nil == ai.Embedding && nil != model.Conf.AI.Embedding {- ai.Embedding = model.Conf.AI.Embedding- }for i, p := range ai.Providers {if nil == p {diff --git a/kernel/conf/ai.go b/kernel/conf/ai.goindex 48813228e1..3e4e1a20b7 100644--- a/kernel/conf/ai.go+++ b/kernel/conf/ai.go@@ -17,6 +17,7 @@package confimport (+ "encoding/hex""encoding/json""os""strconv"@@ -26,27 +27,6 @@ import ("github.com/siyuan-note/siyuan/kernel/util")-type EncryptedString string--func (s EncryptedString) MarshalJSON() ([]byte, error) {- return json.Marshal(util.AESEncrypt(string(s)))-}--func (s *EncryptedString) UnmarshalJSON(data []byte) error {- var str string- json.Unmarshal(data, &str)- if dec := util.AESDecrypt(str); len(dec) > 0 {- *s = EncryptedString(dec)- } else {- *s = EncryptedString(str)- }- return nil-}--func (s EncryptedString) String() string {- return string(s)-}-type AI struct {MCP *MCP `json:"mcp"`Embedding *Embedding `json:"embedding"`@@ -64,7 +44,7 @@ type Embedding struct {ID string `json:"id,omitempty"`DisplayName string `json:"displayName,omitempty"`Enabled bool `json:"enabled,omitempty"`- APIKey EncryptedString `json:"apiKey"`+ APIKey string `json:"apiKey"`BaseURL string `json:"baseURL"`Name string `json:"name"`Timeout int `json:"timeout"`@@ -74,7 +54,7 @@ type Provider struct {ID string `json:"id,omitempty"`DisplayName string `json:"displayName,omitempty"`Enabled bool `json:"enabled,omitempty"`- APIKey EncryptedString `json:"apiKey"`+ APIKey string `json:"apiKey"`BaseURL string `json:"baseURL"`RequestTimeout int `json:"requestTimeout"`Models []*Model `json:"models"`@@ -118,7 +98,7 @@ func NewAI() *AI {BaseURL: "https://api.openai.com/v1",RequestTimeout: 30,}- provider.APIKey = EncryptedString(os.Getenv("SIYUAN_OPENAI_API_KEY"))+ provider.APIKey = os.Getenv("SIYUAN_OPENAI_API_KEY")if timeout := os.Getenv("SIYUAN_OPENAI_API_TIMEOUT"); "" != timeout {if v, err := strconv.Atoi(timeout); err == nil {@@ -174,7 +154,7 @@ func NewAI() *AI {embeddingModel := os.Getenv("SIYUAN_OPENAI_EMBEDDING_MODEL")if "" != embeddingKey && "" != embeddingBaseURL && "" != embeddingModel {ai.Embedding = &Embedding{- APIKey: EncryptedString(embeddingKey),+ APIKey: embeddingKey,BaseURL: embeddingBaseURL,Name: embeddingModel,Timeout: 30,@@ -186,7 +166,7 @@ func NewAI() *AI {func (ai *AI) HasAnyProvider() bool {for _, p := range ai.Providers {- if p != nil && len(string(p.APIKey)) > 0 {+ if p != nil && len(p.APIKey) > 0 {for _, m := range p.Models {if m.Name != "" {return true@@ -200,7 +180,7 @@ func (ai *AI) HasAnyProvider() bool {func (ai *AI) GetModel(id string) (*Provider, *Model) {if id == "" {for _, p := range ai.Providers {- if p == nil || len(string(p.APIKey)) == 0 {+ if p == nil || len(p.APIKey) == 0 {continue}for _, m := range p.Models {@@ -220,7 +200,7 @@ func (ai *AI) GetModel(id string) (*Provider, *Model) {continue}for _, m := range p.Models {- if m.ID == id && len(string(p.APIKey)) > 0 {+ if m.ID == id && len(p.APIKey) > 0 {return p, m}}@@ -231,7 +211,7 @@ func (ai *AI) GetModel(id string) (*Provider, *Model) {continue}for _, m := range p.Models {- if m.DisplayName == id && len(string(p.APIKey)) > 0 {+ if m.DisplayName == id && len(p.APIKey) > 0 {return p, m}}@@ -242,7 +222,7 @@ func (ai *AI) GetModel(id string) (*Provider, *Model) {continue}for _, m := range p.Models {- if m.Name == id && len(string(p.APIKey)) > 0 {+ if m.Name == id && len(p.APIKey) > 0 {return p, m}}@@ -284,6 +264,38 @@ func (ai *AI) Normalize() {}}+func (ai *AI) DecryptAPIKeys() {+ for _, p := range ai.Providers {+ if p == nil || p.APIKey == "" {+ continue+ }+ if dec := util.AESDecrypt(p.APIKey); len(dec) > 0 {+ if plain, err := hex.DecodeString(string(dec)); err == nil {+ p.APIKey = string(plain)+ }+ }+ }+ if ai.Embedding != nil && ai.Embedding.APIKey != "" {+ if dec := util.AESDecrypt(ai.Embedding.APIKey); len(dec) > 0 {+ if plain, err := hex.DecodeString(string(dec)); err == nil {+ ai.Embedding.APIKey = string(plain)+ }+ }+ }+}++func (ai *AI) EncryptAPIKeys() {+ for _, p := range ai.Providers {+ if p == nil {+ continue+ }+ p.APIKey = util.AESEncrypt(p.APIKey)+ }+ if ai.Embedding != nil {+ ai.Embedding.APIKey = util.AESEncrypt(ai.Embedding.APIKey)+ }+}+func NeedsAIMigration(data []byte) bool {var topRaw map[string]json.RawMessageif err := json.Unmarshal(data, &topRaw); err != nil {@@ -397,7 +409,7 @@ func migrateProvider(raw map[string]any) *Provider {return &Provider{ID: getString(raw, "id"),Enabled: true,- APIKey: EncryptedString(getString(raw, "apiKey")),+ APIKey: getString(raw, "apiKey"),BaseURL: getString(raw, "apiBaseURL"),RequestTimeout: getInt(raw, "apiTimeout"),}@@ -426,7 +438,7 @@ func migrateEmbedding(raw map[string]any) *Embedding {ID: getString(raw, "id"),DisplayName: getString(raw, "name"),Enabled: getBool(raw, "enabled"),- APIKey: EncryptedString(getString(raw, "apiKey")),+ APIKey: getString(raw, "apiKey"),BaseURL: getString(raw, "apiBaseURL"),Name: getString(raw, "apiModel"),Timeout: getInt(raw, "apiTimeout"),diff --git a/kernel/model/ai.go b/kernel/model/ai.goindex 028204c575..65d5cbdc1c 100644--- a/kernel/model/ai.go+++ b/kernel/model/ai.go@@ -101,7 +101,7 @@ func chatGPTContinueWrite(msg string, contextMsgs []string, cloud bool) (ret strgpt = &CloudGPT{}} else {gpt = &OpenAIGPT{- c: util.NewOpenAIClient(string(prov.APIKey), prov.BaseURL),+ c: util.NewOpenAIClient(prov.APIKey, prov.BaseURL),m: m,timeout: prov.RequestTimeout,}diff --git a/kernel/model/conf.go b/kernel/model/conf.goindex 62330d286d..a0a8a0bd5f 100644--- a/kernel/model/conf.go+++ b/kernel/model/conf.go@@ -140,6 +140,9 @@ func InitConf() {if nil != Conf.Search && Conf.Search.HanSensitive == nil {Conf.Search.SetHanSensitive(true)}+ if nil != Conf.AI {+ Conf.AI.DecryptAPIKeys()+ }}}}@@ -592,7 +595,7 @@ func InitConf() {}for _, p := range Conf.AI.Providers {- if p == nil || len(string(p.APIKey)) == 0 {+ if p == nil || len(p.APIKey) == 0 {continue}for _, m := range p.Models {@@ -615,7 +618,7 @@ func InitConf() {}}- if Conf.AI.Embedding != nil && len(string(Conf.AI.Embedding.APIKey)) > 0 {+ if Conf.AI.Embedding != nil && len(Conf.AI.Embedding.APIKey) > 0 {logging.LogInfof("embedding API enabled\n"+" baseURL=%s\n"+" model=%s",@@ -918,6 +921,11 @@ func (conf *AppConf) Save() {Conf.m.Lock()defer Conf.m.Unlock()+ if nil != Conf.AI {+ Conf.AI.EncryptAPIKeys()+ defer Conf.AI.DecryptAPIKeys()+ }+newData, _ := gulu.JSON.MarshalIndentJSON(Conf, "", " ")confPath := filepath.Join(util.ConfDir, "conf.json")oldData, err := filelock.ReadFile(confPath)diff --git a/kernel/model/embedding.go b/kernel/model/embedding.goindex a621f23a37..39cc993a37 100644--- a/kernel/model/embedding.go+++ b/kernel/model/embedding.go@@ -438,12 +438,12 @@ func SemanticSearchBlock(query string, boxes, paths []string, types, subTypes ma}func isEmbeddingEnabled() bool {- return nil != Conf.AI.Embedding && len(string(Conf.AI.Embedding.APIKey)) > 0+ return nil != Conf.AI.Embedding && len(Conf.AI.Embedding.APIKey) > 0}func embeddingKey() string {if nil != Conf.AI.Embedding && "" != Conf.AI.Embedding.APIKey {- return string(Conf.AI.Embedding.APIKey)+ return Conf.AI.Embedding.APIKey}if v := os.Getenv("SIYUAN_OPENAI_EMBEDDING_API_KEY"); "" != v {return v