option.go 21 KB

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