option.go 21 KB

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