option.go 21 KB

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