refactor: openAI config system setting (#1333)

This commit is contained in:
boojack
2023-03-11 12:26:40 +08:00
committed by GitHub
parent 7c92805aac
commit e5cbb8cd56
8 changed files with 108 additions and 181 deletions

View File

@@ -27,10 +27,8 @@ const (
SystemSettingCustomizedProfileName SystemSettingName = "customizedProfile"
// SystemSettingStorageServiceIDName is the key type of storage service ID.
SystemSettingStorageServiceIDName SystemSettingName = "storageServiceId"
// SystemSettingOpenAIAPIKeyName is the key type of OpenAI API key.
SystemSettingOpenAIAPIKeyName SystemSettingName = "openAIApiKey"
// SystemSettingOpenAIAPIHost is the key type of OpenAI API path.
SystemSettingOpenAIAPIHost SystemSettingName = "openAIApiHost"
// SystemSettingOpenAIConfigName is the key type of OpenAI config.
SystemSettingOpenAIConfigName SystemSettingName = "openAIConfig"
)
// CustomizedProfile is the struct definition for SystemSettingCustomizedProfileName system setting item.
@@ -49,6 +47,11 @@ type CustomizedProfile struct {
ExternalURL string `json:"externalUrl"`
}
type OpenAIConfig struct {
Key string `json:"key"`
Host string `json:"host"`
}
func (key SystemSettingName) String() string {
switch key {
case SystemSettingServerID:
@@ -67,24 +70,17 @@ func (key SystemSettingName) String() string {
return "customizedProfile"
case SystemSettingStorageServiceIDName:
return "storageServiceId"
case SystemSettingOpenAIAPIKeyName:
return "openAIApiKey"
case SystemSettingOpenAIAPIHost:
return "openAIApiHost"
case SystemSettingOpenAIConfigName:
return "openAIConfig"
}
return ""
}
var (
SystemSettingAllowSignUpValue = []bool{true, false}
SystemSettingDisablePublicMemosValue = []bool{true, false}
)
type SystemSetting struct {
Name SystemSettingName
Name SystemSettingName `json:"name"`
// Value is a JSON string with basic value.
Value string
Description string
Value string `json:"value"`
Description string `json:"description"`
}
type SystemSettingUpsert struct {
@@ -102,35 +98,12 @@ func (upsert SystemSettingUpsert) Validate() error {
if err != nil {
return fmt.Errorf("failed to unmarshal system setting allow signup value")
}
invalid := true
for _, v := range SystemSettingAllowSignUpValue {
if value == v {
invalid = false
break
}
}
if invalid {
return fmt.Errorf("invalid system setting allow signup value")
}
} else if upsert.Name == SystemSettingDisablePublicMemosName {
value := false
err := json.Unmarshal([]byte(upsert.Value), &value)
if err != nil {
return fmt.Errorf("failed to unmarshal system setting disable public memos value")
}
invalid := true
for _, v := range SystemSettingDisablePublicMemosValue {
if value == v {
invalid = false
break
}
}
if invalid {
return fmt.Errorf("invalid system setting disable public memos value")
}
} else if upsert.Name == SystemSettingAdditionalStyleName {
value := ""
err := json.Unmarshal([]byte(upsert.Value), &value)
@@ -169,17 +142,11 @@ func (upsert SystemSettingUpsert) Validate() error {
return fmt.Errorf("failed to unmarshal system setting storage service id value")
}
return nil
} else if upsert.Name == SystemSettingOpenAIAPIKeyName {
value := ""
} else if upsert.Name == SystemSettingOpenAIConfigName {
value := OpenAIConfig{}
err := json.Unmarshal([]byte(upsert.Value), &value)
if err != nil {
return fmt.Errorf("failed to unmarshal system setting openai api key value")
}
} else if upsert.Name == SystemSettingOpenAIAPIHost {
value := ""
err := json.Unmarshal([]byte(upsert.Value), &value)
if err != nil {
return fmt.Errorf("failed to unmarshal system setting openai api host value")
return fmt.Errorf("failed to unmarshal system setting openai api config value")
}
} else {
return fmt.Errorf("invalid system setting name")