option.go 20 KB

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