You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

1088 lines
32 KiB

  1. package model
  2. import (
  3. "database/sql/driver"
  4. "encoding/json"
  5. "errors"
  6. "fmt"
  7. "math/rand"
  8. "strings"
  9. "sync"
  10. "github.com/QuantumNous/new-api/common"
  11. "github.com/QuantumNous/new-api/constant"
  12. "github.com/QuantumNous/new-api/dto"
  13. "github.com/QuantumNous/new-api/types"
  14. "github.com/samber/lo"
  15. "gorm.io/gorm"
  16. )
  17. type Channel struct {
  18. Id int `json:"id"`
  19. Type int `json:"type" gorm:"default:0"`
  20. Key string `json:"key" gorm:"not null"`
  21. OpenAIOrganization *string `json:"openai_organization"`
  22. TestModel *string `json:"test_model"`
  23. Status int `json:"status" gorm:"default:1"`
  24. Name string `json:"name" gorm:"index"`
  25. PublicName string `json:"public_name" gorm:"size:255;default:''"`
  26. Weight *uint `json:"weight" gorm:"default:0"`
  27. CreatedTime int64 `json:"created_time" gorm:"bigint"`
  28. TestTime int64 `json:"test_time" gorm:"bigint"`
  29. ResponseTime int `json:"response_time"` // in milliseconds
  30. BaseURL *string `json:"base_url" gorm:"column:base_url;default:''"`
  31. Other string `json:"other"`
  32. Balance float64 `json:"balance"` // in USD
  33. BalanceUpdatedTime int64 `json:"balance_updated_time" gorm:"bigint"`
  34. Models string `json:"models"`
  35. Group string `json:"group" gorm:"type:varchar(64);default:'default'"`
  36. UsedQuota int64 `json:"used_quota" gorm:"bigint;default:0"`
  37. ModelMapping *string `json:"model_mapping" gorm:"type:text"`
  38. //MaxInputTokens *int `json:"max_input_tokens" gorm:"default:0"`
  39. StatusCodeMapping *string `json:"status_code_mapping" gorm:"type:varchar(1024);default:''"`
  40. Priority *int64 `json:"priority" gorm:"bigint;default:0"`
  41. AutoBan *int `json:"auto_ban" gorm:"default:1"`
  42. OtherInfo string `json:"other_info"`
  43. Tag *string `json:"tag" gorm:"index"`
  44. Setting *string `json:"setting" gorm:"type:text"` // 渠道额外设置
  45. ParamOverride *string `json:"param_override" gorm:"type:text"`
  46. HeaderOverride *string `json:"header_override" gorm:"type:text"`
  47. Remark *string `json:"remark" gorm:"type:varchar(255)" validate:"max=255"`
  48. // add after v0.8.5
  49. ChannelInfo ChannelInfo `json:"channel_info" gorm:"type:json"`
  50. OtherSettings string `json:"settings" gorm:"column:settings"` // 其他设置,存储azure版本等不需要检索的信息,详见dto.ChannelOtherSettings
  51. // cache info
  52. Keys []string `json:"-" gorm:"-"`
  53. // Asset credential metadata is populated only for management API responses.
  54. AssetCredentialConfigured bool `json:"asset_credential_configured,omitempty" gorm:"-"`
  55. AssetCredentialPoolID string `json:"asset_credential_pool_id,omitempty" gorm:"-"`
  56. }
  57. type ChannelInfo struct {
  58. IsMultiKey bool `json:"is_multi_key"` // 是否多Key模式
  59. MultiKeySize int `json:"multi_key_size"` // 多Key模式下的Key数量
  60. MultiKeyStatusList map[int]int `json:"multi_key_status_list"` // key状态列表,key index -> status
  61. MultiKeyDisabledReason map[int]string `json:"multi_key_disabled_reason,omitempty"` // key禁用原因列表,key index -> reason
  62. MultiKeyDisabledTime map[int]int64 `json:"multi_key_disabled_time,omitempty"` // key禁用时间列表,key index -> time
  63. MultiKeyPollingIndex int `json:"multi_key_polling_index"` // 多Key模式下轮询的key索引
  64. MultiKeyMode constant.MultiKeyMode `json:"multi_key_mode"`
  65. }
  66. // Value implements driver.Valuer interface
  67. func (c ChannelInfo) Value() (driver.Value, error) {
  68. return common.Marshal(&c)
  69. }
  70. func ChannelDisplayName(publicName, name string) string {
  71. if publicName != "" {
  72. return publicName
  73. }
  74. return name
  75. }
  76. // Scan implements sql.Scanner interface
  77. func (c *ChannelInfo) Scan(value interface{}) error {
  78. bytesValue, _ := value.([]byte)
  79. return common.Unmarshal(bytesValue, c)
  80. }
  81. func (channel *Channel) GetKeys() []string {
  82. if channel.Key == "" {
  83. return []string{}
  84. }
  85. if len(channel.Keys) > 0 {
  86. return channel.Keys
  87. }
  88. trimmed := strings.TrimSpace(channel.Key)
  89. // If the key starts with '[', try to parse it as a JSON array (e.g., for Vertex AI scenarios)
  90. if strings.HasPrefix(trimmed, "[") {
  91. var arr []json.RawMessage
  92. if err := common.Unmarshal([]byte(trimmed), &arr); err == nil {
  93. res := make([]string, len(arr))
  94. for i, v := range arr {
  95. res[i] = string(v)
  96. }
  97. return res
  98. }
  99. }
  100. // Otherwise, fall back to splitting by newline
  101. keys := strings.Split(strings.Trim(channel.Key, "\n"), "\n")
  102. return keys
  103. }
  104. func (channel *Channel) GetNextEnabledKey() (string, int, *types.NewAPIError) {
  105. // If not in multi-key mode, return the original key string directly.
  106. if !channel.ChannelInfo.IsMultiKey {
  107. return channel.Key, 0, nil
  108. }
  109. // Obtain all keys (split by \n)
  110. keys := channel.GetKeys()
  111. if len(keys) == 0 {
  112. // No keys available, return error, should disable the channel
  113. return "", 0, types.NewError(errors.New("no keys available"), types.ErrorCodeChannelNoAvailableKey)
  114. }
  115. lock := GetChannelPollingLock(channel.Id)
  116. lock.Lock()
  117. defer lock.Unlock()
  118. statusList := channel.ChannelInfo.MultiKeyStatusList
  119. // helper to get key status, default to enabled when missing
  120. getStatus := func(idx int) int {
  121. if statusList == nil {
  122. return common.ChannelStatusEnabled
  123. }
  124. if status, ok := statusList[idx]; ok {
  125. return status
  126. }
  127. return common.ChannelStatusEnabled
  128. }
  129. // Collect indexes of enabled keys
  130. enabledIdx := make([]int, 0, len(keys))
  131. for i := range keys {
  132. if getStatus(i) == common.ChannelStatusEnabled {
  133. enabledIdx = append(enabledIdx, i)
  134. }
  135. }
  136. // If no specific status list or none enabled, return an explicit error so caller can
  137. // properly handle a channel with no available keys (e.g. mark channel disabled).
  138. // Returning the first key here caused requests to keep using an already-disabled key.
  139. if len(enabledIdx) == 0 {
  140. return "", 0, types.NewError(errors.New("no enabled keys"), types.ErrorCodeChannelNoAvailableKey)
  141. }
  142. switch channel.ChannelInfo.MultiKeyMode {
  143. case constant.MultiKeyModeRandom:
  144. // Randomly pick one enabled key
  145. selectedIdx := enabledIdx[rand.Intn(len(enabledIdx))]
  146. return keys[selectedIdx], selectedIdx, nil
  147. case constant.MultiKeyModePolling:
  148. // Use channel-specific lock to ensure thread-safe polling
  149. channelInfo, err := CacheGetChannelInfo(channel.Id)
  150. if err != nil {
  151. return "", 0, types.NewError(err, types.ErrorCodeGetChannelFailed, types.ErrOptionWithSkipRetry())
  152. }
  153. //println("before polling index:", channel.ChannelInfo.MultiKeyPollingIndex)
  154. defer func() {
  155. if common.DebugEnabled {
  156. println(fmt.Sprintf("channel %d polling index: %d", channel.Id, channel.ChannelInfo.MultiKeyPollingIndex))
  157. }
  158. if !common.MemoryCacheEnabled {
  159. _ = channel.SaveChannelInfo()
  160. } else {
  161. // CacheUpdateChannel(channel)
  162. }
  163. }()
  164. // Start from the saved polling index and look for the next enabled key
  165. start := channelInfo.MultiKeyPollingIndex
  166. if start < 0 || start >= len(keys) {
  167. start = 0
  168. }
  169. for i := 0; i < len(keys); i++ {
  170. idx := (start + i) % len(keys)
  171. if getStatus(idx) == common.ChannelStatusEnabled {
  172. // update polling index for next call (point to the next position)
  173. channel.ChannelInfo.MultiKeyPollingIndex = (idx + 1) % len(keys)
  174. return keys[idx], idx, nil
  175. }
  176. }
  177. // Fallback – should not happen, but return first enabled key
  178. return keys[enabledIdx[0]], enabledIdx[0], nil
  179. default:
  180. // Unknown mode, default to first enabled key (or original key string)
  181. return keys[enabledIdx[0]], enabledIdx[0], nil
  182. }
  183. }
  184. func (channel *Channel) SaveChannelInfo() error {
  185. return DB.Model(channel).Update("channel_info", channel.ChannelInfo).Error
  186. }
  187. func (channel *Channel) GetModels() []string {
  188. if channel.Models == "" {
  189. return []string{}
  190. }
  191. return strings.Split(strings.Trim(channel.Models, ","), ",")
  192. }
  193. func (channel *Channel) GetGroups() []string {
  194. if channel.Group == "" {
  195. return []string{}
  196. }
  197. groups := strings.Split(strings.Trim(channel.Group, ","), ",")
  198. for i, group := range groups {
  199. groups[i] = strings.TrimSpace(group)
  200. }
  201. return groups
  202. }
  203. func (channel *Channel) GetOtherInfo() map[string]interface{} {
  204. otherInfo := make(map[string]interface{})
  205. if channel.OtherInfo != "" {
  206. err := common.Unmarshal([]byte(channel.OtherInfo), &otherInfo)
  207. if err != nil {
  208. common.SysLog(fmt.Sprintf("failed to unmarshal other info: channel_id=%d, tag=%s, name=%s, error=%v", channel.Id, channel.GetTag(), channel.Name, err))
  209. }
  210. }
  211. return otherInfo
  212. }
  213. func (channel *Channel) SetOtherInfo(otherInfo map[string]interface{}) {
  214. otherInfoBytes, err := json.Marshal(otherInfo)
  215. if err != nil {
  216. common.SysLog(fmt.Sprintf("failed to marshal other info: channel_id=%d, tag=%s, name=%s, error=%v", channel.Id, channel.GetTag(), channel.Name, err))
  217. return
  218. }
  219. channel.OtherInfo = string(otherInfoBytes)
  220. }
  221. func (channel *Channel) GetTag() string {
  222. if channel.Tag == nil {
  223. return ""
  224. }
  225. return *channel.Tag
  226. }
  227. func (channel *Channel) SetTag(tag string) {
  228. channel.Tag = &tag
  229. }
  230. func (channel *Channel) GetAutoBan() bool {
  231. if channel.AutoBan == nil {
  232. return false
  233. }
  234. return *channel.AutoBan == 1
  235. }
  236. func (channel *Channel) Save() error {
  237. return DB.Save(channel).Error
  238. }
  239. func (channel *Channel) SaveWithoutKey() error {
  240. if channel.Id == 0 {
  241. return errors.New("channel ID is 0")
  242. }
  243. return DB.Omit("key").Save(channel).Error
  244. }
  245. func GetAllChannels(startIdx int, num int, selectAll bool, idSort bool) ([]*Channel, error) {
  246. var channels []*Channel
  247. var err error
  248. order := "priority desc"
  249. if idSort {
  250. order = "id desc"
  251. }
  252. if selectAll {
  253. err = DB.Order(order).Find(&channels).Error
  254. } else {
  255. err = DB.Order(order).Limit(num).Offset(startIdx).Omit("key").Find(&channels).Error
  256. }
  257. return channels, err
  258. }
  259. func GetChannelsByTag(tag string, idSort bool, selectAll bool) ([]*Channel, error) {
  260. var channels []*Channel
  261. order := "priority desc"
  262. if idSort {
  263. order = "id desc"
  264. }
  265. query := DB.Where("tag = ?", tag).Order(order)
  266. if !selectAll {
  267. query = query.Omit("key")
  268. }
  269. err := query.Find(&channels).Error
  270. return channels, err
  271. }
  272. func SearchChannels(keyword string, group string, model string, idSort bool) ([]*Channel, error) {
  273. var channels []*Channel
  274. modelsCol := "`models`"
  275. // 如果是 PostgreSQL,使用双引号
  276. if common.UsingPostgreSQL {
  277. modelsCol = `"models"`
  278. }
  279. baseURLCol := "`base_url`"
  280. // 如果是 PostgreSQL,使用双引号
  281. if common.UsingPostgreSQL {
  282. baseURLCol = `"base_url"`
  283. }
  284. order := "priority desc"
  285. if idSort {
  286. order = "id desc"
  287. }
  288. // 构造基础查询
  289. baseQuery := DB.Model(&Channel{}).Omit("key")
  290. // 构造WHERE子句
  291. var whereClause string
  292. var args []interface{}
  293. if group != "" && group != "null" {
  294. var groupCondition string
  295. if common.UsingMySQL {
  296. groupCondition = `CONCAT(',', ` + commonGroupCol + `, ',') LIKE ?`
  297. } else {
  298. // sqlite, PostgreSQL
  299. groupCondition = `(',' || ` + commonGroupCol + ` || ',') LIKE ?`
  300. }
  301. whereClause = "(id = ? OR name LIKE ? OR " + commonKeyCol + " = ? OR " + baseURLCol + " LIKE ?) AND " + modelsCol + ` LIKE ? AND ` + groupCondition
  302. args = append(args, common.String2Int(keyword), "%"+keyword+"%", keyword, "%"+keyword+"%", "%"+model+"%", "%,"+group+",%")
  303. } else {
  304. whereClause = "(id = ? OR name LIKE ? OR " + commonKeyCol + " = ? OR " + baseURLCol + " LIKE ?) AND " + modelsCol + " LIKE ?"
  305. args = append(args, common.String2Int(keyword), "%"+keyword+"%", keyword, "%"+keyword+"%", "%"+model+"%")
  306. }
  307. // 执行查询
  308. err := baseQuery.Where(whereClause, args...).Order(order).Find(&channels).Error
  309. if err != nil {
  310. return nil, err
  311. }
  312. return channels, nil
  313. }
  314. func GetChannelById(id int, selectAll bool) (*Channel, error) {
  315. channel := &Channel{Id: id}
  316. var err error = nil
  317. if selectAll {
  318. err = DB.First(channel, "id = ?", id).Error
  319. } else {
  320. err = DB.Omit("key").First(channel, "id = ?", id).Error
  321. }
  322. if err != nil {
  323. return nil, err
  324. }
  325. if channel == nil {
  326. return nil, errors.New("channel not found")
  327. }
  328. return channel, nil
  329. }
  330. func BatchInsertChannels(channels []Channel) error {
  331. if len(channels) == 0 {
  332. return nil
  333. }
  334. tx := DB.Begin()
  335. if tx.Error != nil {
  336. return tx.Error
  337. }
  338. defer func() {
  339. if r := recover(); r != nil {
  340. tx.Rollback()
  341. }
  342. }()
  343. for _, chunk := range lo.Chunk(channels, 50) {
  344. if err := tx.Create(&chunk).Error; err != nil {
  345. tx.Rollback()
  346. return err
  347. }
  348. for _, channel_ := range chunk {
  349. if err := channel_.AddAbilities(tx); err != nil {
  350. tx.Rollback()
  351. return err
  352. }
  353. }
  354. }
  355. return tx.Commit().Error
  356. }
  357. func BatchDeleteChannels(ids []int) error {
  358. if len(ids) == 0 {
  359. return nil
  360. }
  361. // 使用事务 分批删除channel表和abilities表
  362. tx := DB.Begin()
  363. if tx.Error != nil {
  364. return tx.Error
  365. }
  366. for _, chunk := range lo.Chunk(ids, 200) {
  367. if err := DeleteChannelAssetCredentialsWithTx(tx, chunk); err != nil {
  368. tx.Rollback()
  369. return err
  370. }
  371. if err := tx.Where("channel_id in (?)", chunk).Delete(&Ability{}).Error; err != nil {
  372. tx.Rollback()
  373. return err
  374. }
  375. if err := tx.Where("id in (?)", chunk).Delete(&Channel{}).Error; err != nil {
  376. tx.Rollback()
  377. return err
  378. }
  379. }
  380. return tx.Commit().Error
  381. }
  382. func (channel *Channel) GetPriority() int64 {
  383. if channel.Priority == nil {
  384. return 0
  385. }
  386. return *channel.Priority
  387. }
  388. func (channel *Channel) GetWeight() int {
  389. if channel.Weight == nil {
  390. return 0
  391. }
  392. return int(*channel.Weight)
  393. }
  394. func (channel *Channel) GetBaseURL() string {
  395. if channel.BaseURL == nil {
  396. return ""
  397. }
  398. url := *channel.BaseURL
  399. if url == "" {
  400. url = constant.ChannelBaseURLs[channel.Type]
  401. }
  402. return url
  403. }
  404. func (channel *Channel) GetModelMapping() string {
  405. if channel.ModelMapping == nil {
  406. return ""
  407. }
  408. return *channel.ModelMapping
  409. }
  410. func (channel *Channel) GetStatusCodeMapping() string {
  411. if channel.StatusCodeMapping == nil {
  412. return ""
  413. }
  414. return *channel.StatusCodeMapping
  415. }
  416. func (channel *Channel) Insert() error {
  417. var err error
  418. err = DB.Create(channel).Error
  419. if err != nil {
  420. return err
  421. }
  422. err = channel.AddAbilities(nil)
  423. return err
  424. }
  425. func (channel *Channel) Update() error {
  426. return DB.Transaction(func(tx *gorm.DB) error {
  427. return channel.UpdateWithTx(tx)
  428. })
  429. }
  430. func (channel *Channel) UpdateWithTx(tx *gorm.DB) error {
  431. // If this is a multi-key channel, recalculate MultiKeySize based on the current key list to avoid inconsistency after editing keys
  432. if channel.ChannelInfo.IsMultiKey {
  433. var keyStr string
  434. if channel.Key != "" {
  435. keyStr = channel.Key
  436. } else {
  437. // If key is not provided, read the existing key from the database
  438. var existing Channel
  439. if err := tx.First(&existing, "id = ?", channel.Id).Error; err == nil {
  440. keyStr = existing.Key
  441. }
  442. }
  443. // Parse the key list (supports newline separation or JSON array)
  444. keys := []string{}
  445. if keyStr != "" {
  446. trimmed := strings.TrimSpace(keyStr)
  447. if strings.HasPrefix(trimmed, "[") {
  448. var arr []json.RawMessage
  449. if err := common.Unmarshal([]byte(trimmed), &arr); err == nil {
  450. keys = make([]string, len(arr))
  451. for i, v := range arr {
  452. keys[i] = string(v)
  453. }
  454. }
  455. }
  456. if len(keys) == 0 { // fallback to newline split
  457. keys = strings.Split(strings.Trim(keyStr, "\n"), "\n")
  458. }
  459. }
  460. channel.ChannelInfo.MultiKeySize = len(keys)
  461. // Clean up status data that exceeds the new key count to prevent index out of range
  462. if channel.ChannelInfo.MultiKeyStatusList != nil {
  463. for idx := range channel.ChannelInfo.MultiKeyStatusList {
  464. if idx >= channel.ChannelInfo.MultiKeySize {
  465. delete(channel.ChannelInfo.MultiKeyStatusList, idx)
  466. }
  467. }
  468. }
  469. }
  470. err := tx.Model(channel).Updates(channel).Error
  471. if err != nil {
  472. return err
  473. }
  474. if err = tx.First(channel, "id = ?", channel.Id).Error; err != nil {
  475. return err
  476. }
  477. err = channel.UpdateAbilities(tx)
  478. return err
  479. }
  480. func InsertChannelWithAssetCredential(channel *Channel, credential *ChannelAssetCredential) error {
  481. return DB.Transaction(func(tx *gorm.DB) error {
  482. if err := tx.Create(channel).Error; err != nil {
  483. return err
  484. }
  485. if err := channel.AddAbilities(tx); err != nil {
  486. return err
  487. }
  488. credential.ChannelId = channel.Id
  489. return UpsertChannelAssetCredentialWithTx(tx, credential)
  490. })
  491. }
  492. func (channel *Channel) UpdateResponseTime(responseTime int64) {
  493. err := DB.Model(channel).Select("response_time", "test_time").Updates(Channel{
  494. TestTime: common.GetTimestamp(),
  495. ResponseTime: int(responseTime),
  496. }).Error
  497. if err != nil {
  498. common.SysLog(fmt.Sprintf("failed to update response time: channel_id=%d, error=%v", channel.Id, err))
  499. }
  500. }
  501. func (channel *Channel) UpdateBalance(balance float64) {
  502. err := DB.Model(channel).Select("balance_updated_time", "balance").Updates(Channel{
  503. BalanceUpdatedTime: common.GetTimestamp(),
  504. Balance: balance,
  505. }).Error
  506. if err != nil {
  507. common.SysLog(fmt.Sprintf("failed to update balance: channel_id=%d, error=%v", channel.Id, err))
  508. }
  509. }
  510. func (channel *Channel) Delete() error {
  511. return DB.Transaction(func(tx *gorm.DB) error {
  512. return channel.DeleteWithTx(tx)
  513. })
  514. }
  515. func (channel *Channel) DeleteWithTx(tx *gorm.DB) error {
  516. if err := DeleteChannelAssetCredentialWithTx(tx, channel.Id); err != nil {
  517. return err
  518. }
  519. if err := tx.Where("channel_id = ?", channel.Id).Delete(&Ability{}).Error; err != nil {
  520. return err
  521. }
  522. return tx.Delete(channel).Error
  523. }
  524. var channelStatusLock sync.Mutex
  525. // channelPollingLocks stores locks for each channel.id to ensure thread-safe polling
  526. var channelPollingLocks sync.Map
  527. // GetChannelPollingLock returns or creates a mutex for the given channel ID
  528. func GetChannelPollingLock(channelId int) *sync.Mutex {
  529. if lock, exists := channelPollingLocks.Load(channelId); exists {
  530. return lock.(*sync.Mutex)
  531. }
  532. // Create new lock for this channel
  533. newLock := &sync.Mutex{}
  534. actual, _ := channelPollingLocks.LoadOrStore(channelId, newLock)
  535. return actual.(*sync.Mutex)
  536. }
  537. // CleanupChannelPollingLocks removes locks for channels that no longer exist
  538. // This is optional and can be called periodically to prevent memory leaks
  539. func CleanupChannelPollingLocks() {
  540. var activeChannelIds []int
  541. DB.Model(&Channel{}).Pluck("id", &activeChannelIds)
  542. activeChannelSet := make(map[int]bool)
  543. for _, id := range activeChannelIds {
  544. activeChannelSet[id] = true
  545. }
  546. channelPollingLocks.Range(func(key, value interface{}) bool {
  547. channelId := key.(int)
  548. if !activeChannelSet[channelId] {
  549. channelPollingLocks.Delete(channelId)
  550. }
  551. return true
  552. })
  553. }
  554. func handlerMultiKeyUpdate(channel *Channel, usingKey string, status int, reason string) {
  555. keys := channel.GetKeys()
  556. if len(keys) == 0 {
  557. channel.Status = status
  558. } else {
  559. var keyIndex int
  560. for i, key := range keys {
  561. if key == usingKey {
  562. keyIndex = i
  563. break
  564. }
  565. }
  566. if channel.ChannelInfo.MultiKeyStatusList == nil {
  567. channel.ChannelInfo.MultiKeyStatusList = make(map[int]int)
  568. }
  569. if status == common.ChannelStatusEnabled {
  570. delete(channel.ChannelInfo.MultiKeyStatusList, keyIndex)
  571. } else {
  572. channel.ChannelInfo.MultiKeyStatusList[keyIndex] = status
  573. if channel.ChannelInfo.MultiKeyDisabledReason == nil {
  574. channel.ChannelInfo.MultiKeyDisabledReason = make(map[int]string)
  575. }
  576. if channel.ChannelInfo.MultiKeyDisabledTime == nil {
  577. channel.ChannelInfo.MultiKeyDisabledTime = make(map[int]int64)
  578. }
  579. channel.ChannelInfo.MultiKeyDisabledReason[keyIndex] = reason
  580. channel.ChannelInfo.MultiKeyDisabledTime[keyIndex] = common.GetTimestamp()
  581. }
  582. if len(channel.ChannelInfo.MultiKeyStatusList) >= channel.ChannelInfo.MultiKeySize {
  583. channel.Status = common.ChannelStatusAutoDisabled
  584. info := channel.GetOtherInfo()
  585. info["status_reason"] = "All keys are disabled"
  586. info["status_time"] = common.GetTimestamp()
  587. channel.SetOtherInfo(info)
  588. }
  589. }
  590. }
  591. func UpdateChannelStatus(channelId int, usingKey string, status int, reason string) bool {
  592. if common.MemoryCacheEnabled {
  593. channelStatusLock.Lock()
  594. defer channelStatusLock.Unlock()
  595. channelCache, _ := CacheGetChannel(channelId)
  596. if channelCache == nil {
  597. return false
  598. }
  599. if channelCache.ChannelInfo.IsMultiKey {
  600. // Use per-channel lock to prevent concurrent map read/write with GetNextEnabledKey
  601. pollingLock := GetChannelPollingLock(channelId)
  602. pollingLock.Lock()
  603. // 如果是多Key模式,更新缓存中的状态
  604. handlerMultiKeyUpdate(channelCache, usingKey, status, reason)
  605. pollingLock.Unlock()
  606. //CacheUpdateChannel(channelCache)
  607. //return true
  608. } else {
  609. // 如果缓存渠道存在,且状态已是目标状态,直接返回
  610. if channelCache.Status == status {
  611. return false
  612. }
  613. CacheUpdateChannelStatus(channelId, status)
  614. }
  615. }
  616. shouldUpdateAbilities := false
  617. defer func() {
  618. if shouldUpdateAbilities {
  619. err := UpdateAbilityStatus(channelId, status == common.ChannelStatusEnabled)
  620. if err != nil {
  621. common.SysLog(fmt.Sprintf("failed to update ability status: channel_id=%d, error=%v", channelId, err))
  622. }
  623. }
  624. }()
  625. channel, err := GetChannelById(channelId, true)
  626. if err != nil {
  627. return false
  628. } else {
  629. if channel.Status == status {
  630. return false
  631. }
  632. if channel.ChannelInfo.IsMultiKey {
  633. beforeStatus := channel.Status
  634. // Protect map writes with the same per-channel lock used by readers
  635. pollingLock := GetChannelPollingLock(channelId)
  636. pollingLock.Lock()
  637. handlerMultiKeyUpdate(channel, usingKey, status, reason)
  638. pollingLock.Unlock()
  639. if beforeStatus != channel.Status {
  640. shouldUpdateAbilities = true
  641. }
  642. } else {
  643. info := channel.GetOtherInfo()
  644. info["status_reason"] = reason
  645. info["status_time"] = common.GetTimestamp()
  646. channel.SetOtherInfo(info)
  647. channel.Status = status
  648. shouldUpdateAbilities = true
  649. }
  650. err = channel.SaveWithoutKey()
  651. if err != nil {
  652. common.SysLog(fmt.Sprintf("failed to update channel status: channel_id=%d, status=%d, error=%v", channel.Id, status, err))
  653. return false
  654. }
  655. }
  656. return true
  657. }
  658. func EnableChannelByTag(tag string) error {
  659. err := DB.Model(&Channel{}).Where("tag = ?", tag).Update("status", common.ChannelStatusEnabled).Error
  660. if err != nil {
  661. return err
  662. }
  663. err = UpdateAbilityStatusByTag(tag, true)
  664. return err
  665. }
  666. func DisableChannelByTag(tag string) error {
  667. err := DB.Model(&Channel{}).Where("tag = ?", tag).Update("status", common.ChannelStatusManuallyDisabled).Error
  668. if err != nil {
  669. return err
  670. }
  671. err = UpdateAbilityStatusByTag(tag, false)
  672. return err
  673. }
  674. func EditChannelByTag(tag string, newTag *string, modelMapping *string, models *string, group *string, priority *int64, weight *uint, paramOverride *string, headerOverride *string) error {
  675. updateData := Channel{}
  676. shouldReCreateAbilities := false
  677. updatedTag := tag
  678. // 如果 newTag 不为空且不等于 tag,则更新 tag
  679. if newTag != nil && *newTag != tag {
  680. updateData.Tag = newTag
  681. updatedTag = *newTag
  682. }
  683. if modelMapping != nil && *modelMapping != "" {
  684. updateData.ModelMapping = modelMapping
  685. }
  686. if models != nil && *models != "" {
  687. shouldReCreateAbilities = true
  688. updateData.Models = *models
  689. }
  690. if group != nil && *group != "" {
  691. shouldReCreateAbilities = true
  692. updateData.Group = *group
  693. }
  694. if priority != nil {
  695. updateData.Priority = priority
  696. }
  697. if weight != nil {
  698. updateData.Weight = weight
  699. }
  700. if paramOverride != nil {
  701. updateData.ParamOverride = paramOverride
  702. }
  703. if headerOverride != nil {
  704. updateData.HeaderOverride = headerOverride
  705. }
  706. err := DB.Model(&Channel{}).Where("tag = ?", tag).Updates(updateData).Error
  707. if err != nil {
  708. return err
  709. }
  710. if shouldReCreateAbilities {
  711. channels, err := GetChannelsByTag(updatedTag, false, false)
  712. if err == nil {
  713. for _, channel := range channels {
  714. err = channel.UpdateAbilities(nil)
  715. if err != nil {
  716. common.SysLog(fmt.Sprintf("failed to update abilities: channel_id=%d, tag=%s, error=%v", channel.Id, channel.GetTag(), err))
  717. }
  718. }
  719. }
  720. } else {
  721. err := UpdateAbilityByTag(tag, newTag, priority, weight)
  722. if err != nil {
  723. return err
  724. }
  725. }
  726. return nil
  727. }
  728. func UpdateChannelUsedQuota(id int, quota int) {
  729. if common.BatchUpdateEnabled {
  730. addNewRecord(BatchUpdateTypeChannelUsedQuota, id, quota)
  731. return
  732. }
  733. updateChannelUsedQuota(id, quota)
  734. }
  735. func updateChannelUsedQuota(id int, quota int) {
  736. err := DB.Model(&Channel{}).Where("id = ?", id).Update("used_quota", gorm.Expr("used_quota + ?", quota)).Error
  737. if err != nil {
  738. common.SysLog(fmt.Sprintf("failed to update channel used quota: channel_id=%d, delta_quota=%d, error=%v", id, quota, err))
  739. }
  740. }
  741. func DeleteChannelByStatus(status int64) (int64, error) {
  742. var ids []int
  743. if err := DB.Model(&Channel{}).Where("status = ?", status).Pluck("id", &ids).Error; err != nil {
  744. return 0, err
  745. }
  746. if len(ids) == 0 {
  747. return 0, nil
  748. }
  749. var rows int64
  750. err := DB.Transaction(func(tx *gorm.DB) error {
  751. if err := DeleteChannelAssetCredentialsWithTx(tx, ids); err != nil {
  752. return err
  753. }
  754. if err := tx.Where("channel_id IN ?", ids).Delete(&Ability{}).Error; err != nil {
  755. return err
  756. }
  757. result := tx.Where("id IN ?", ids).Delete(&Channel{})
  758. rows = result.RowsAffected
  759. return result.Error
  760. })
  761. return rows, err
  762. }
  763. func DeleteDisabledChannel() (int64, error) {
  764. var ids []int
  765. if err := DB.Model(&Channel{}).Where("status = ? or status = ?", common.ChannelStatusAutoDisabled, common.ChannelStatusManuallyDisabled).Pluck("id", &ids).Error; err != nil {
  766. return 0, err
  767. }
  768. if len(ids) == 0 {
  769. return 0, nil
  770. }
  771. var rows int64
  772. err := DB.Transaction(func(tx *gorm.DB) error {
  773. if err := DeleteChannelAssetCredentialsWithTx(tx, ids); err != nil {
  774. return err
  775. }
  776. if err := tx.Where("channel_id IN ?", ids).Delete(&Ability{}).Error; err != nil {
  777. return err
  778. }
  779. result := tx.Where("id IN ?", ids).Delete(&Channel{})
  780. rows = result.RowsAffected
  781. return result.Error
  782. })
  783. return rows, err
  784. }
  785. func GetPaginatedTags(offset int, limit int) ([]*string, error) {
  786. var tags []*string
  787. err := DB.Model(&Channel{}).Select("DISTINCT tag").Where("tag != ''").Offset(offset).Limit(limit).Find(&tags).Error
  788. return tags, err
  789. }
  790. func SearchTags(keyword string, group string, model string, idSort bool) ([]*string, error) {
  791. var tags []*string
  792. modelsCol := "`models`"
  793. // 如果是 PostgreSQL,使用双引号
  794. if common.UsingPostgreSQL {
  795. modelsCol = `"models"`
  796. }
  797. baseURLCol := "`base_url`"
  798. // 如果是 PostgreSQL,使用双引号
  799. if common.UsingPostgreSQL {
  800. baseURLCol = `"base_url"`
  801. }
  802. order := "priority desc"
  803. if idSort {
  804. order = "id desc"
  805. }
  806. // 构造基础查询
  807. baseQuery := DB.Model(&Channel{}).Omit("key")
  808. // 构造WHERE子句
  809. var whereClause string
  810. var args []interface{}
  811. if group != "" && group != "null" {
  812. var groupCondition string
  813. if common.UsingMySQL {
  814. groupCondition = `CONCAT(',', ` + commonGroupCol + `, ',') LIKE ?`
  815. } else {
  816. // sqlite, PostgreSQL
  817. groupCondition = `(',' || ` + commonGroupCol + ` || ',') LIKE ?`
  818. }
  819. whereClause = "(id = ? OR name LIKE ? OR " + commonKeyCol + " = ? OR " + baseURLCol + " LIKE ?) AND " + modelsCol + ` LIKE ? AND ` + groupCondition
  820. args = append(args, common.String2Int(keyword), "%"+keyword+"%", keyword, "%"+keyword+"%", "%"+model+"%", "%,"+group+",%")
  821. } else {
  822. whereClause = "(id = ? OR name LIKE ? OR " + commonKeyCol + " = ? OR " + baseURLCol + " LIKE ?) AND " + modelsCol + " LIKE ?"
  823. args = append(args, common.String2Int(keyword), "%"+keyword+"%", keyword, "%"+keyword+"%", "%"+model+"%")
  824. }
  825. subQuery := baseQuery.Where(whereClause, args...).
  826. Select("tag").
  827. Where("tag != ''").
  828. Order(order)
  829. err := DB.Table("(?) as sub", subQuery).
  830. Select("DISTINCT tag").
  831. Find(&tags).Error
  832. if err != nil {
  833. return nil, err
  834. }
  835. return tags, nil
  836. }
  837. func (channel *Channel) ValidateSettings() error {
  838. channelParams := &dto.ChannelSettings{}
  839. if channel.Setting != nil && *channel.Setting != "" {
  840. err := common.Unmarshal([]byte(*channel.Setting), channelParams)
  841. if err != nil {
  842. return err
  843. }
  844. }
  845. return nil
  846. }
  847. func (channel *Channel) GetSetting() dto.ChannelSettings {
  848. setting := dto.ChannelSettings{}
  849. if channel.Setting != nil && *channel.Setting != "" {
  850. err := common.Unmarshal([]byte(*channel.Setting), &setting)
  851. if err != nil {
  852. common.SysLog(fmt.Sprintf("failed to unmarshal setting: channel_id=%d, error=%v", channel.Id, err))
  853. channel.Setting = nil // 清空设置以避免后续错误
  854. _ = channel.Save() // 保存修改
  855. }
  856. }
  857. return setting
  858. }
  859. func (channel *Channel) SetSetting(setting dto.ChannelSettings) {
  860. settingBytes, err := common.Marshal(setting)
  861. if err != nil {
  862. common.SysLog(fmt.Sprintf("failed to marshal setting: channel_id=%d, error=%v", channel.Id, err))
  863. return
  864. }
  865. channel.Setting = common.GetPointer[string](string(settingBytes))
  866. }
  867. func (channel *Channel) GetOtherSettings() dto.ChannelOtherSettings {
  868. setting := dto.ChannelOtherSettings{}
  869. if channel.OtherSettings != "" {
  870. err := common.UnmarshalJsonStr(channel.OtherSettings, &setting)
  871. if err != nil {
  872. common.SysLog(fmt.Sprintf("failed to unmarshal setting: channel_id=%d, error=%v", channel.Id, err))
  873. channel.OtherSettings = "{}" // 清空设置以避免后续错误
  874. _ = channel.Save() // 保存修改
  875. }
  876. }
  877. return setting
  878. }
  879. func (channel *Channel) SetOtherSettings(setting dto.ChannelOtherSettings) {
  880. settingBytes, err := common.Marshal(setting)
  881. if err != nil {
  882. common.SysLog(fmt.Sprintf("failed to marshal setting: channel_id=%d, error=%v", channel.Id, err))
  883. return
  884. }
  885. channel.OtherSettings = string(settingBytes)
  886. }
  887. func (channel *Channel) GetParamOverride() map[string]interface{} {
  888. paramOverride := make(map[string]interface{})
  889. if channel.ParamOverride != nil && *channel.ParamOverride != "" {
  890. err := common.Unmarshal([]byte(*channel.ParamOverride), &paramOverride)
  891. if err != nil {
  892. common.SysLog(fmt.Sprintf("failed to unmarshal param override: channel_id=%d, error=%v", channel.Id, err))
  893. }
  894. }
  895. return paramOverride
  896. }
  897. func (channel *Channel) GetHeaderOverride() map[string]interface{} {
  898. headerOverride := make(map[string]interface{})
  899. if channel.HeaderOverride != nil && *channel.HeaderOverride != "" {
  900. err := common.Unmarshal([]byte(*channel.HeaderOverride), &headerOverride)
  901. if err != nil {
  902. common.SysLog(fmt.Sprintf("failed to unmarshal header override: channel_id=%d, error=%v", channel.Id, err))
  903. }
  904. }
  905. return headerOverride
  906. }
  907. func GetChannelsByIds(ids []int) ([]*Channel, error) {
  908. var channels []*Channel
  909. err := DB.Where("id in (?)", ids).Find(&channels).Error
  910. return channels, err
  911. }
  912. func BatchSetChannelTag(ids []int, tag *string) error {
  913. // 开启事务
  914. tx := DB.Begin()
  915. if tx.Error != nil {
  916. return tx.Error
  917. }
  918. // 更新标签
  919. err := tx.Model(&Channel{}).Where("id in (?)", ids).Update("tag", tag).Error
  920. if err != nil {
  921. tx.Rollback()
  922. return err
  923. }
  924. // update ability status
  925. channels, err := GetChannelsByIds(ids)
  926. if err != nil {
  927. tx.Rollback()
  928. return err
  929. }
  930. for _, channel := range channels {
  931. err = channel.UpdateAbilities(tx)
  932. if err != nil {
  933. tx.Rollback()
  934. return err
  935. }
  936. }
  937. // 提交事务
  938. return tx.Commit().Error
  939. }
  940. // CountAllChannels returns total channels in DB
  941. func CountAllChannels() (int64, error) {
  942. var total int64
  943. err := DB.Model(&Channel{}).Count(&total).Error
  944. return total, err
  945. }
  946. // CountAllTags returns number of non-empty distinct tags
  947. func CountAllTags() (int64, error) {
  948. var total int64
  949. err := DB.Model(&Channel{}).Where("tag is not null AND tag != ''").Distinct("tag").Count(&total).Error
  950. return total, err
  951. }
  952. // Get channels of specified type with pagination
  953. func GetChannelsByType(startIdx int, num int, idSort bool, channelType int) ([]*Channel, error) {
  954. var channels []*Channel
  955. order := "priority desc"
  956. if idSort {
  957. order = "id desc"
  958. }
  959. err := DB.Where("type = ?", channelType).Order(order).Limit(num).Offset(startIdx).Omit("key").Find(&channels).Error
  960. return channels, err
  961. }
  962. // Count channels of specific type
  963. func CountChannelsByType(channelType int) (int64, error) {
  964. var count int64
  965. err := DB.Model(&Channel{}).Where("type = ?", channelType).Count(&count).Error
  966. return count, err
  967. }
  968. // Return map[type]count for all channels
  969. func CountChannelsGroupByType() (map[int64]int64, error) {
  970. type result struct {
  971. Type int64 `gorm:"column:type"`
  972. Count int64 `gorm:"column:count"`
  973. }
  974. var results []result
  975. err := DB.Model(&Channel{}).Select("type, count(*) as count").Group("type").Find(&results).Error
  976. if err != nil {
  977. return nil, err
  978. }
  979. counts := make(map[int64]int64)
  980. for _, r := range results {
  981. counts[r.Type] = r.Count
  982. }
  983. return counts, nil
  984. }