option.go 20 KB

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