option.go 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  1. package model
  2. import (
  3. "strconv"
  4. "strings"
  5. "time"
  6. "github.com/QuantumNous/new-api/common"
  7. "github.com/QuantumNous/new-api/setting"
  8. "github.com/QuantumNous/new-api/setting/config"
  9. "github.com/QuantumNous/new-api/setting/operation_setting"
  10. "github.com/QuantumNous/new-api/setting/performance_setting"
  11. "github.com/QuantumNous/new-api/setting/ratio_setting"
  12. "github.com/QuantumNous/new-api/setting/system_setting"
  13. )
  14. type Option struct {
  15. Key string `json:"key" gorm:"primaryKey"`
  16. Value string `json:"value"`
  17. }
  18. func AllOption() ([]*Option, error) {
  19. var options []*Option
  20. var err error
  21. err = DB.Find(&options).Error
  22. return options, err
  23. }
  24. func InitOptionMap() {
  25. common.OptionMapRWMutex.Lock()
  26. common.OptionMap = make(map[string]string)
  27. // 添加原有的系统配置
  28. common.OptionMap["FileUploadPermission"] = strconv.Itoa(common.FileUploadPermission)
  29. common.OptionMap["FileDownloadPermission"] = strconv.Itoa(common.FileDownloadPermission)
  30. common.OptionMap["ImageUploadPermission"] = strconv.Itoa(common.ImageUploadPermission)
  31. common.OptionMap["ImageDownloadPermission"] = strconv.Itoa(common.ImageDownloadPermission)
  32. common.OptionMap["PasswordLoginEnabled"] = strconv.FormatBool(common.PasswordLoginEnabled)
  33. common.OptionMap["PasswordRegisterEnabled"] = strconv.FormatBool(common.PasswordRegisterEnabled)
  34. common.OptionMap["EmailVerificationEnabled"] = strconv.FormatBool(common.EmailVerificationEnabled)
  35. common.OptionMap["GitHubOAuthEnabled"] = strconv.FormatBool(common.GitHubOAuthEnabled)
  36. common.OptionMap["LinuxDOOAuthEnabled"] = strconv.FormatBool(common.LinuxDOOAuthEnabled)
  37. common.OptionMap["TelegramOAuthEnabled"] = strconv.FormatBool(common.TelegramOAuthEnabled)
  38. common.OptionMap["WeChatAuthEnabled"] = strconv.FormatBool(common.WeChatAuthEnabled)
  39. common.OptionMap["TurnstileCheckEnabled"] = strconv.FormatBool(common.TurnstileCheckEnabled)
  40. common.OptionMap["RegisterEnabled"] = strconv.FormatBool(common.RegisterEnabled)
  41. common.OptionMap["AutomaticDisableChannelEnabled"] = strconv.FormatBool(common.AutomaticDisableChannelEnabled)
  42. common.OptionMap["AutomaticEnableChannelEnabled"] = strconv.FormatBool(common.AutomaticEnableChannelEnabled)
  43. common.OptionMap["LogConsumeEnabled"] = strconv.FormatBool(common.LogConsumeEnabled)
  44. common.OptionMap["DisplayInCurrencyEnabled"] = strconv.FormatBool(common.DisplayInCurrencyEnabled)
  45. common.OptionMap["DisplayTokenStatEnabled"] = strconv.FormatBool(common.DisplayTokenStatEnabled)
  46. common.OptionMap["DrawingEnabled"] = strconv.FormatBool(common.DrawingEnabled)
  47. common.OptionMap["TaskEnabled"] = strconv.FormatBool(common.TaskEnabled)
  48. common.OptionMap["DataExportEnabled"] = strconv.FormatBool(common.DataExportEnabled)
  49. common.OptionMap["ChannelDisableThreshold"] = strconv.FormatFloat(common.ChannelDisableThreshold, 'f', -1, 64)
  50. common.OptionMap["EmailDomainRestrictionEnabled"] = strconv.FormatBool(common.EmailDomainRestrictionEnabled)
  51. common.OptionMap["EmailAliasRestrictionEnabled"] = strconv.FormatBool(common.EmailAliasRestrictionEnabled)
  52. common.OptionMap["EmailDomainWhitelist"] = strings.Join(common.EmailDomainWhitelist, ",")
  53. common.OptionMap["SMTPServer"] = ""
  54. common.OptionMap["SMTPFrom"] = ""
  55. common.OptionMap["SMTPPort"] = strconv.Itoa(common.SMTPPort)
  56. common.OptionMap["SMTPAccount"] = ""
  57. common.OptionMap["SMTPToken"] = ""
  58. common.OptionMap["SMTPSSLEnabled"] = strconv.FormatBool(common.SMTPSSLEnabled)
  59. common.OptionMap["Notice"] = ""
  60. common.OptionMap["About"] = ""
  61. common.OptionMap["HomePageContent"] = ""
  62. common.OptionMap["Footer"] = common.Footer
  63. common.OptionMap["SystemName"] = common.SystemName
  64. common.OptionMap["Logo"] = common.Logo
  65. common.OptionMap["ServerAddress"] = ""
  66. common.OptionMap["WorkerUrl"] = system_setting.WorkerUrl
  67. common.OptionMap["WorkerValidKey"] = system_setting.WorkerValidKey
  68. common.OptionMap["WorkerAllowHttpImageRequestEnabled"] = strconv.FormatBool(system_setting.WorkerAllowHttpImageRequestEnabled)
  69. common.OptionMap["PayAddress"] = ""
  70. common.OptionMap["CustomCallbackAddress"] = ""
  71. common.OptionMap["EpayId"] = ""
  72. common.OptionMap["EpayKey"] = ""
  73. common.OptionMap["Price"] = strconv.FormatFloat(operation_setting.Price, 'f', -1, 64)
  74. common.OptionMap["USDExchangeRate"] = strconv.FormatFloat(operation_setting.USDExchangeRate, 'f', -1, 64)
  75. common.OptionMap["MinTopUp"] = strconv.Itoa(operation_setting.MinTopUp)
  76. common.OptionMap["StripeMinTopUp"] = strconv.Itoa(setting.StripeMinTopUp)
  77. common.OptionMap["StripeApiSecret"] = setting.StripeApiSecret
  78. common.OptionMap["StripeWebhookSecret"] = setting.StripeWebhookSecret
  79. common.OptionMap["StripePriceId"] = setting.StripePriceId
  80. common.OptionMap["StripeUnitPrice"] = strconv.FormatFloat(setting.StripeUnitPrice, 'f', -1, 64)
  81. common.OptionMap["StripePromotionCodesEnabled"] = strconv.FormatBool(setting.StripePromotionCodesEnabled)
  82. common.OptionMap["CreemApiKey"] = setting.CreemApiKey
  83. common.OptionMap["CreemProducts"] = setting.CreemProducts
  84. common.OptionMap["CreemTestMode"] = strconv.FormatBool(setting.CreemTestMode)
  85. common.OptionMap["CreemWebhookSecret"] = setting.CreemWebhookSecret
  86. common.OptionMap["WaffoEnabled"] = strconv.FormatBool(setting.WaffoEnabled)
  87. common.OptionMap["WaffoApiKey"] = setting.WaffoApiKey
  88. common.OptionMap["WaffoPrivateKey"] = setting.WaffoPrivateKey
  89. common.OptionMap["WaffoPublicCert"] = setting.WaffoPublicCert
  90. common.OptionMap["WaffoSandboxPublicCert"] = setting.WaffoSandboxPublicCert
  91. common.OptionMap["WaffoSandboxApiKey"] = setting.WaffoSandboxApiKey
  92. common.OptionMap["WaffoSandboxPrivateKey"] = setting.WaffoSandboxPrivateKey
  93. common.OptionMap["WaffoSandbox"] = strconv.FormatBool(setting.WaffoSandbox)
  94. common.OptionMap["WaffoMerchantId"] = setting.WaffoMerchantId
  95. common.OptionMap["WaffoNotifyUrl"] = setting.WaffoNotifyUrl
  96. common.OptionMap["WaffoReturnUrl"] = setting.WaffoReturnUrl
  97. common.OptionMap["WaffoSubscriptionReturnUrl"] = setting.WaffoSubscriptionReturnUrl
  98. common.OptionMap["WaffoCurrency"] = setting.WaffoCurrency
  99. common.OptionMap["WaffoUnitPrice"] = strconv.FormatFloat(setting.WaffoUnitPrice, 'f', -1, 64)
  100. common.OptionMap["WaffoMinTopUp"] = strconv.Itoa(setting.WaffoMinTopUp)
  101. common.OptionMap["WaffoPayMethods"] = setting.WaffoPayMethods2JsonString()
  102. common.OptionMap["TopupGroupRatio"] = common.TopupGroupRatio2JSONString()
  103. common.OptionMap["Chats"] = setting.Chats2JsonString()
  104. common.OptionMap["AutoGroups"] = setting.AutoGroups2JsonString()
  105. common.OptionMap["DefaultUseAutoGroup"] = strconv.FormatBool(setting.DefaultUseAutoGroup)
  106. common.OptionMap["PayMethods"] = operation_setting.PayMethods2JsonString()
  107. common.OptionMap["GitHubClientId"] = ""
  108. common.OptionMap["GitHubClientSecret"] = ""
  109. common.OptionMap["TelegramBotToken"] = ""
  110. common.OptionMap["TelegramBotName"] = ""
  111. common.OptionMap["WeChatServerAddress"] = ""
  112. common.OptionMap["WeChatServerToken"] = ""
  113. common.OptionMap["WeChatAccountQRCodeImageURL"] = ""
  114. common.OptionMap["TurnstileSiteKey"] = ""
  115. common.OptionMap["TurnstileSecretKey"] = ""
  116. common.OptionMap["QuotaForNewUser"] = strconv.Itoa(common.QuotaForNewUser)
  117. common.OptionMap["QuotaForInviter"] = strconv.Itoa(common.QuotaForInviter)
  118. common.OptionMap["QuotaForInvitee"] = strconv.Itoa(common.QuotaForInvitee)
  119. common.OptionMap["QuotaRemindThreshold"] = strconv.Itoa(common.QuotaRemindThreshold)
  120. common.OptionMap["PreConsumedQuota"] = strconv.Itoa(common.PreConsumedQuota)
  121. common.OptionMap["ModelRequestRateLimitCount"] = strconv.Itoa(setting.ModelRequestRateLimitCount)
  122. common.OptionMap["ModelRequestRateLimitDurationMinutes"] = strconv.Itoa(setting.ModelRequestRateLimitDurationMinutes)
  123. common.OptionMap["ModelRequestRateLimitSuccessCount"] = strconv.Itoa(setting.ModelRequestRateLimitSuccessCount)
  124. common.OptionMap["ModelRequestRateLimitGroup"] = setting.ModelRequestRateLimitGroup2JSONString()
  125. common.OptionMap["ModelRatio"] = ratio_setting.ModelRatio2JSONString()
  126. common.OptionMap["ModelPrice"] = ratio_setting.ModelPrice2JSONString()
  127. common.OptionMap["CacheRatio"] = ratio_setting.CacheRatio2JSONString()
  128. common.OptionMap["CreateCacheRatio"] = ratio_setting.CreateCacheRatio2JSONString()
  129. common.OptionMap["GroupRatio"] = ratio_setting.GroupRatio2JSONString()
  130. common.OptionMap["GroupGroupRatio"] = ratio_setting.GroupGroupRatio2JSONString()
  131. common.OptionMap["UserUsableGroups"] = setting.UserUsableGroups2JSONString()
  132. common.OptionMap["CompletionRatio"] = ratio_setting.CompletionRatio2JSONString()
  133. common.OptionMap["ImageRatio"] = ratio_setting.ImageRatio2JSONString()
  134. common.OptionMap["AudioRatio"] = ratio_setting.AudioRatio2JSONString()
  135. common.OptionMap["AudioCompletionRatio"] = ratio_setting.AudioCompletionRatio2JSONString()
  136. common.OptionMap["TopUpLink"] = common.TopUpLink
  137. //common.OptionMap["ChatLink"] = common.ChatLink
  138. //common.OptionMap["ChatLink2"] = common.ChatLink2
  139. common.OptionMap["QuotaPerUnit"] = strconv.FormatFloat(common.QuotaPerUnit, 'f', -1, 64)
  140. common.OptionMap["RetryTimes"] = strconv.Itoa(common.RetryTimes)
  141. common.OptionMap["DataExportInterval"] = strconv.Itoa(common.DataExportInterval)
  142. common.OptionMap["DataExportDefaultTime"] = common.DataExportDefaultTime
  143. common.OptionMap["DefaultCollapseSidebar"] = strconv.FormatBool(common.DefaultCollapseSidebar)
  144. common.OptionMap["MjNotifyEnabled"] = strconv.FormatBool(setting.MjNotifyEnabled)
  145. common.OptionMap["MjAccountFilterEnabled"] = strconv.FormatBool(setting.MjAccountFilterEnabled)
  146. common.OptionMap["MjModeClearEnabled"] = strconv.FormatBool(setting.MjModeClearEnabled)
  147. common.OptionMap["MjForwardUrlEnabled"] = strconv.FormatBool(setting.MjForwardUrlEnabled)
  148. common.OptionMap["MjActionCheckSuccessEnabled"] = strconv.FormatBool(setting.MjActionCheckSuccessEnabled)
  149. common.OptionMap["CheckSensitiveEnabled"] = strconv.FormatBool(setting.CheckSensitiveEnabled)
  150. common.OptionMap["DemoSiteEnabled"] = strconv.FormatBool(operation_setting.DemoSiteEnabled)
  151. common.OptionMap["SelfUseModeEnabled"] = strconv.FormatBool(operation_setting.SelfUseModeEnabled)
  152. common.OptionMap["ModelRequestRateLimitEnabled"] = strconv.FormatBool(setting.ModelRequestRateLimitEnabled)
  153. common.OptionMap["CheckSensitiveOnPromptEnabled"] = strconv.FormatBool(setting.CheckSensitiveOnPromptEnabled)
  154. common.OptionMap["StopOnSensitiveEnabled"] = strconv.FormatBool(setting.StopOnSensitiveEnabled)
  155. common.OptionMap["SensitiveWords"] = setting.SensitiveWordsToString()
  156. common.OptionMap["StreamCacheQueueLength"] = strconv.Itoa(setting.StreamCacheQueueLength)
  157. common.OptionMap["AutomaticDisableKeywords"] = operation_setting.AutomaticDisableKeywordsToString()
  158. common.OptionMap["AutomaticDisableStatusCodes"] = operation_setting.AutomaticDisableStatusCodesToString()
  159. common.OptionMap["AutomaticRetryStatusCodes"] = operation_setting.AutomaticRetryStatusCodesToString()
  160. common.OptionMap["ExposeRatioEnabled"] = strconv.FormatBool(ratio_setting.IsExposeRatioEnabled())
  161. // 自动添加所有注册的模型配置
  162. modelConfigs := config.GlobalConfig.ExportAllConfigs()
  163. for k, v := range modelConfigs {
  164. common.OptionMap[k] = v
  165. }
  166. common.OptionMapRWMutex.Unlock()
  167. loadOptionsFromDatabase()
  168. }
  169. func loadOptionsFromDatabase() {
  170. options, _ := AllOption()
  171. for _, option := range options {
  172. err := updateOptionMap(option.Key, option.Value)
  173. if err != nil {
  174. common.SysLog("failed to update option map: " + err.Error())
  175. }
  176. }
  177. }
  178. func SyncOptions(frequency int) {
  179. for {
  180. time.Sleep(time.Duration(frequency) * time.Second)
  181. common.SysLog("syncing options from database")
  182. loadOptionsFromDatabase()
  183. }
  184. }
  185. func UpdateOption(key string, value string) error {
  186. // Save to database first
  187. option := Option{
  188. Key: key,
  189. }
  190. // https://gorm.io/docs/update.html#Save-All-Fields
  191. DB.FirstOrCreate(&option, Option{Key: key})
  192. option.Value = value
  193. // Save is a combination function.
  194. // If save value does not contain primary key, it will execute Create,
  195. // otherwise it will execute Update (with all fields).
  196. DB.Save(&option)
  197. // Update OptionMap
  198. return updateOptionMap(key, value)
  199. }
  200. func updateOptionMap(key string, value string) (err error) {
  201. common.OptionMapRWMutex.Lock()
  202. defer common.OptionMapRWMutex.Unlock()
  203. common.OptionMap[key] = value
  204. // 检查是否是模型配置 - 使用更规范的方式处理
  205. if handleConfigUpdate(key, value) {
  206. return nil // 已由配置系统处理
  207. }
  208. // 处理传统配置项...
  209. if strings.HasSuffix(key, "Permission") {
  210. intValue, _ := strconv.Atoi(value)
  211. switch key {
  212. case "FileUploadPermission":
  213. common.FileUploadPermission = intValue
  214. case "FileDownloadPermission":
  215. common.FileDownloadPermission = intValue
  216. case "ImageUploadPermission":
  217. common.ImageUploadPermission = intValue
  218. case "ImageDownloadPermission":
  219. common.ImageDownloadPermission = intValue
  220. }
  221. }
  222. if strings.HasSuffix(key, "Enabled") || key == "DefaultCollapseSidebar" || key == "DefaultUseAutoGroup" {
  223. boolValue := value == "true"
  224. switch key {
  225. case "PasswordRegisterEnabled":
  226. common.PasswordRegisterEnabled = boolValue
  227. case "PasswordLoginEnabled":
  228. common.PasswordLoginEnabled = boolValue
  229. case "EmailVerificationEnabled":
  230. common.EmailVerificationEnabled = boolValue
  231. case "GitHubOAuthEnabled":
  232. common.GitHubOAuthEnabled = boolValue
  233. case "LinuxDOOAuthEnabled":
  234. common.LinuxDOOAuthEnabled = boolValue
  235. case "WeChatAuthEnabled":
  236. common.WeChatAuthEnabled = boolValue
  237. case "TelegramOAuthEnabled":
  238. common.TelegramOAuthEnabled = boolValue
  239. case "TurnstileCheckEnabled":
  240. common.TurnstileCheckEnabled = boolValue
  241. case "RegisterEnabled":
  242. common.RegisterEnabled = boolValue
  243. case "EmailDomainRestrictionEnabled":
  244. common.EmailDomainRestrictionEnabled = boolValue
  245. case "EmailAliasRestrictionEnabled":
  246. common.EmailAliasRestrictionEnabled = boolValue
  247. case "AutomaticDisableChannelEnabled":
  248. common.AutomaticDisableChannelEnabled = boolValue
  249. case "AutomaticEnableChannelEnabled":
  250. common.AutomaticEnableChannelEnabled = boolValue
  251. case "LogConsumeEnabled":
  252. common.LogConsumeEnabled = boolValue
  253. case "DisplayInCurrencyEnabled":
  254. // 兼容旧字段:同步到新配置 general_setting.quota_display_type(运行时生效)
  255. // true -> USD, false -> TOKENS
  256. newVal := "USD"
  257. if !boolValue {
  258. newVal = "TOKENS"
  259. }
  260. if cfg := config.GlobalConfig.Get("general_setting"); cfg != nil {
  261. _ = config.UpdateConfigFromMap(cfg, map[string]string{"quota_display_type": newVal})
  262. }
  263. case "DisplayTokenStatEnabled":
  264. common.DisplayTokenStatEnabled = boolValue
  265. case "DrawingEnabled":
  266. common.DrawingEnabled = boolValue
  267. case "TaskEnabled":
  268. common.TaskEnabled = boolValue
  269. case "DataExportEnabled":
  270. common.DataExportEnabled = boolValue
  271. case "DefaultCollapseSidebar":
  272. common.DefaultCollapseSidebar = boolValue
  273. case "MjNotifyEnabled":
  274. setting.MjNotifyEnabled = boolValue
  275. case "MjAccountFilterEnabled":
  276. setting.MjAccountFilterEnabled = boolValue
  277. case "MjModeClearEnabled":
  278. setting.MjModeClearEnabled = boolValue
  279. case "MjForwardUrlEnabled":
  280. setting.MjForwardUrlEnabled = boolValue
  281. case "MjActionCheckSuccessEnabled":
  282. setting.MjActionCheckSuccessEnabled = boolValue
  283. case "CheckSensitiveEnabled":
  284. setting.CheckSensitiveEnabled = boolValue
  285. case "DemoSiteEnabled":
  286. operation_setting.DemoSiteEnabled = boolValue
  287. case "SelfUseModeEnabled":
  288. operation_setting.SelfUseModeEnabled = boolValue
  289. case "CheckSensitiveOnPromptEnabled":
  290. setting.CheckSensitiveOnPromptEnabled = boolValue
  291. case "ModelRequestRateLimitEnabled":
  292. setting.ModelRequestRateLimitEnabled = boolValue
  293. case "StopOnSensitiveEnabled":
  294. setting.StopOnSensitiveEnabled = boolValue
  295. case "SMTPSSLEnabled":
  296. common.SMTPSSLEnabled = boolValue
  297. case "WorkerAllowHttpImageRequestEnabled":
  298. system_setting.WorkerAllowHttpImageRequestEnabled = boolValue
  299. case "DefaultUseAutoGroup":
  300. setting.DefaultUseAutoGroup = boolValue
  301. case "ExposeRatioEnabled":
  302. ratio_setting.SetExposeRatioEnabled(boolValue)
  303. }
  304. }
  305. switch key {
  306. case "EmailDomainWhitelist":
  307. common.EmailDomainWhitelist = strings.Split(value, ",")
  308. case "SMTPServer":
  309. common.SMTPServer = value
  310. case "SMTPPort":
  311. intValue, _ := strconv.Atoi(value)
  312. common.SMTPPort = intValue
  313. case "SMTPAccount":
  314. common.SMTPAccount = value
  315. case "SMTPFrom":
  316. common.SMTPFrom = value
  317. case "SMTPToken":
  318. common.SMTPToken = value
  319. case "ServerAddress":
  320. system_setting.ServerAddress = value
  321. case "WorkerUrl":
  322. system_setting.WorkerUrl = value
  323. case "WorkerValidKey":
  324. system_setting.WorkerValidKey = value
  325. case "PayAddress":
  326. operation_setting.PayAddress = value
  327. case "Chats":
  328. err = setting.UpdateChatsByJsonString(value)
  329. case "AutoGroups":
  330. err = setting.UpdateAutoGroupsByJsonString(value)
  331. case "CustomCallbackAddress":
  332. operation_setting.CustomCallbackAddress = value
  333. case "EpayId":
  334. operation_setting.EpayId = value
  335. case "EpayKey":
  336. operation_setting.EpayKey = value
  337. case "Price":
  338. operation_setting.Price, _ = strconv.ParseFloat(value, 64)
  339. case "USDExchangeRate":
  340. operation_setting.USDExchangeRate, _ = strconv.ParseFloat(value, 64)
  341. case "MinTopUp":
  342. operation_setting.MinTopUp, _ = strconv.Atoi(value)
  343. case "StripeApiSecret":
  344. setting.StripeApiSecret = value
  345. case "StripeWebhookSecret":
  346. setting.StripeWebhookSecret = value
  347. case "StripePriceId":
  348. setting.StripePriceId = value
  349. case "StripeUnitPrice":
  350. setting.StripeUnitPrice, _ = strconv.ParseFloat(value, 64)
  351. case "StripeMinTopUp":
  352. setting.StripeMinTopUp, _ = strconv.Atoi(value)
  353. case "StripePromotionCodesEnabled":
  354. setting.StripePromotionCodesEnabled = value == "true"
  355. case "CreemApiKey":
  356. setting.CreemApiKey = value
  357. case "CreemProducts":
  358. setting.CreemProducts = value
  359. case "CreemTestMode":
  360. setting.CreemTestMode = value == "true"
  361. case "CreemWebhookSecret":
  362. setting.CreemWebhookSecret = value
  363. case "WaffoEnabled":
  364. setting.WaffoEnabled = value == "true"
  365. case "WaffoApiKey":
  366. setting.WaffoApiKey = value
  367. case "WaffoPrivateKey":
  368. setting.WaffoPrivateKey = value
  369. case "WaffoPublicCert":
  370. setting.WaffoPublicCert = value
  371. case "WaffoSandboxPublicCert":
  372. setting.WaffoSandboxPublicCert = value
  373. case "WaffoSandboxApiKey":
  374. setting.WaffoSandboxApiKey = value
  375. case "WaffoSandboxPrivateKey":
  376. setting.WaffoSandboxPrivateKey = value
  377. case "WaffoSandbox":
  378. setting.WaffoSandbox = value == "true"
  379. case "WaffoMerchantId":
  380. setting.WaffoMerchantId = value
  381. case "WaffoNotifyUrl":
  382. setting.WaffoNotifyUrl = value
  383. case "WaffoReturnUrl":
  384. setting.WaffoReturnUrl = value
  385. case "WaffoSubscriptionReturnUrl":
  386. setting.WaffoSubscriptionReturnUrl = value
  387. case "WaffoCurrency":
  388. setting.WaffoCurrency = value
  389. case "WaffoUnitPrice":
  390. setting.WaffoUnitPrice, _ = strconv.ParseFloat(value, 64)
  391. case "WaffoMinTopUp":
  392. setting.WaffoMinTopUp, _ = strconv.Atoi(value)
  393. case "TopupGroupRatio":
  394. err = common.UpdateTopupGroupRatioByJSONString(value)
  395. case "GitHubClientId":
  396. common.GitHubClientId = value
  397. case "GitHubClientSecret":
  398. common.GitHubClientSecret = value
  399. case "LinuxDOClientId":
  400. common.LinuxDOClientId = value
  401. case "LinuxDOClientSecret":
  402. common.LinuxDOClientSecret = value
  403. case "LinuxDOMinimumTrustLevel":
  404. common.LinuxDOMinimumTrustLevel, _ = strconv.Atoi(value)
  405. case "Footer":
  406. common.Footer = value
  407. case "SystemName":
  408. common.SystemName = value
  409. case "Logo":
  410. common.Logo = value
  411. case "WeChatServerAddress":
  412. common.WeChatServerAddress = value
  413. case "WeChatServerToken":
  414. common.WeChatServerToken = value
  415. case "WeChatAccountQRCodeImageURL":
  416. common.WeChatAccountQRCodeImageURL = value
  417. case "TelegramBotToken":
  418. common.TelegramBotToken = value
  419. case "TelegramBotName":
  420. common.TelegramBotName = value
  421. case "TurnstileSiteKey":
  422. common.TurnstileSiteKey = value
  423. case "TurnstileSecretKey":
  424. common.TurnstileSecretKey = value
  425. case "QuotaForNewUser":
  426. common.QuotaForNewUser, _ = strconv.Atoi(value)
  427. case "QuotaForInviter":
  428. common.QuotaForInviter, _ = strconv.Atoi(value)
  429. case "QuotaForInvitee":
  430. common.QuotaForInvitee, _ = strconv.Atoi(value)
  431. case "QuotaRemindThreshold":
  432. common.QuotaRemindThreshold, _ = strconv.Atoi(value)
  433. case "PreConsumedQuota":
  434. common.PreConsumedQuota, _ = strconv.Atoi(value)
  435. case "ModelRequestRateLimitCount":
  436. setting.ModelRequestRateLimitCount, _ = strconv.Atoi(value)
  437. case "ModelRequestRateLimitDurationMinutes":
  438. setting.ModelRequestRateLimitDurationMinutes, _ = strconv.Atoi(value)
  439. case "ModelRequestRateLimitSuccessCount":
  440. setting.ModelRequestRateLimitSuccessCount, _ = strconv.Atoi(value)
  441. case "ModelRequestRateLimitGroup":
  442. err = setting.UpdateModelRequestRateLimitGroupByJSONString(value)
  443. case "RetryTimes":
  444. common.RetryTimes, _ = strconv.Atoi(value)
  445. case "DataExportInterval":
  446. common.DataExportInterval, _ = strconv.Atoi(value)
  447. case "DataExportDefaultTime":
  448. common.DataExportDefaultTime = value
  449. case "ModelRatio":
  450. err = ratio_setting.UpdateModelRatioByJSONString(value)
  451. case "GroupRatio":
  452. err = ratio_setting.UpdateGroupRatioByJSONString(value)
  453. case "GroupGroupRatio":
  454. err = ratio_setting.UpdateGroupGroupRatioByJSONString(value)
  455. case "UserUsableGroups":
  456. err = setting.UpdateUserUsableGroupsByJSONString(value)
  457. case "CompletionRatio":
  458. err = ratio_setting.UpdateCompletionRatioByJSONString(value)
  459. case "ModelPrice":
  460. err = ratio_setting.UpdateModelPriceByJSONString(value)
  461. case "CacheRatio":
  462. err = ratio_setting.UpdateCacheRatioByJSONString(value)
  463. case "CreateCacheRatio":
  464. err = ratio_setting.UpdateCreateCacheRatioByJSONString(value)
  465. case "ImageRatio":
  466. err = ratio_setting.UpdateImageRatioByJSONString(value)
  467. case "AudioRatio":
  468. err = ratio_setting.UpdateAudioRatioByJSONString(value)
  469. case "AudioCompletionRatio":
  470. err = ratio_setting.UpdateAudioCompletionRatioByJSONString(value)
  471. case "TopUpLink":
  472. common.TopUpLink = value
  473. //case "ChatLink":
  474. // common.ChatLink = value
  475. //case "ChatLink2":
  476. // common.ChatLink2 = value
  477. case "ChannelDisableThreshold":
  478. common.ChannelDisableThreshold, _ = strconv.ParseFloat(value, 64)
  479. case "QuotaPerUnit":
  480. common.QuotaPerUnit, _ = strconv.ParseFloat(value, 64)
  481. case "SensitiveWords":
  482. setting.SensitiveWordsFromString(value)
  483. case "AutomaticDisableKeywords":
  484. operation_setting.AutomaticDisableKeywordsFromString(value)
  485. case "AutomaticDisableStatusCodes":
  486. err = operation_setting.AutomaticDisableStatusCodesFromString(value)
  487. case "AutomaticRetryStatusCodes":
  488. err = operation_setting.AutomaticRetryStatusCodesFromString(value)
  489. case "StreamCacheQueueLength":
  490. setting.StreamCacheQueueLength, _ = strconv.Atoi(value)
  491. case "PayMethods":
  492. err = operation_setting.UpdatePayMethodsByJsonString(value)
  493. case "WaffoPayMethods":
  494. // WaffoPayMethods is read directly from OptionMap via setting.GetWaffoPayMethods().
  495. // The value is already stored in OptionMap at the top of this function (line: common.OptionMap[key] = value).
  496. // No additional in-memory variable to update.
  497. }
  498. return err
  499. }
  500. // handleConfigUpdate 处理分层配置更新,返回是否已处理
  501. func handleConfigUpdate(key, value string) bool {
  502. parts := strings.SplitN(key, ".", 2)
  503. if len(parts) != 2 {
  504. return false // 不是分层配置
  505. }
  506. configName := parts[0]
  507. configKey := parts[1]
  508. // 获取配置对象
  509. cfg := config.GlobalConfig.Get(configName)
  510. if cfg == nil {
  511. return false // 未注册的配置
  512. }
  513. // 更新配置
  514. configMap := map[string]string{
  515. configKey: value,
  516. }
  517. config.UpdateConfigFromMap(cfg, configMap)
  518. // 特定配置的后处理
  519. if configName == "performance_setting" {
  520. // 同步磁盘缓存配置到 common 包
  521. performance_setting.UpdateAndSync()
  522. }
  523. return true // 已处理
  524. }