option.go 23 KB

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