model_sync.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  1. package controller
  2. import (
  3. "context"
  4. "encoding/json"
  5. "errors"
  6. "fmt"
  7. "io"
  8. "math/rand"
  9. "net"
  10. "net/http"
  11. "strings"
  12. "sync"
  13. "time"
  14. "github.com/QuantumNous/new-api/common"
  15. "github.com/QuantumNous/new-api/model"
  16. "github.com/gin-gonic/gin"
  17. "gorm.io/gorm"
  18. )
  19. // 上游地址
  20. const (
  21. upstreamModelsURL = "https://basellm.github.io/llm-metadata/api/newapi/models.json"
  22. upstreamVendorsURL = "https://basellm.github.io/llm-metadata/api/newapi/vendors.json"
  23. )
  24. func normalizeLocale(locale string) (string, bool) {
  25. l := strings.ToLower(strings.TrimSpace(locale))
  26. switch l {
  27. case "en", "zh", "ja":
  28. return l, true
  29. default:
  30. return "", false
  31. }
  32. }
  33. func getUpstreamBase() string {
  34. return common.GetEnvOrDefaultString("SYNC_UPSTREAM_BASE", "https://basellm.github.io/llm-metadata")
  35. }
  36. func getUpstreamURLs(locale string) (modelsURL, vendorsURL string) {
  37. base := strings.TrimRight(getUpstreamBase(), "/")
  38. if l, ok := normalizeLocale(locale); ok && l != "" {
  39. return fmt.Sprintf("%s/api/i18n/%s/newapi/models.json", base, l),
  40. fmt.Sprintf("%s/api/i18n/%s/newapi/vendors.json", base, l)
  41. }
  42. return fmt.Sprintf("%s/api/newapi/models.json", base), fmt.Sprintf("%s/api/newapi/vendors.json", base)
  43. }
  44. type upstreamEnvelope[T any] struct {
  45. Success bool `json:"success"`
  46. Message string `json:"message"`
  47. Data []T `json:"data"`
  48. }
  49. type upstreamModel struct {
  50. Description string `json:"description"`
  51. Endpoints json.RawMessage `json:"endpoints"`
  52. Icon string `json:"icon"`
  53. ModelName string `json:"model_name"`
  54. NameRule int `json:"name_rule"`
  55. Status int `json:"status"`
  56. Tags string `json:"tags"`
  57. VendorName string `json:"vendor_name"`
  58. }
  59. type upstreamVendor struct {
  60. Description string `json:"description"`
  61. Icon string `json:"icon"`
  62. Name string `json:"name"`
  63. Status int `json:"status"`
  64. }
  65. var (
  66. etagCache = make(map[string]string)
  67. bodyCache = make(map[string][]byte)
  68. cacheMutex sync.RWMutex
  69. )
  70. type overwriteField struct {
  71. ModelName string `json:"model_name"`
  72. Fields []string `json:"fields"`
  73. }
  74. type syncRequest struct {
  75. Overwrite []overwriteField `json:"overwrite"`
  76. Locale string `json:"locale"`
  77. }
  78. func newHTTPClient() *http.Client {
  79. timeoutSec := common.GetEnvOrDefault("SYNC_HTTP_TIMEOUT_SECONDS", 10)
  80. dialer := &net.Dialer{Timeout: time.Duration(timeoutSec) * time.Second}
  81. transport := &http.Transport{
  82. MaxIdleConns: 100,
  83. IdleConnTimeout: 90 * time.Second,
  84. TLSHandshakeTimeout: time.Duration(timeoutSec) * time.Second,
  85. ExpectContinueTimeout: 1 * time.Second,
  86. ResponseHeaderTimeout: time.Duration(timeoutSec) * time.Second,
  87. }
  88. if common.TLSInsecureSkipVerify {
  89. transport.TLSClientConfig = common.InsecureTLSConfig
  90. }
  91. transport.DialContext = func(ctx context.Context, network, addr string) (net.Conn, error) {
  92. host, _, err := net.SplitHostPort(addr)
  93. if err != nil {
  94. host = addr
  95. }
  96. if strings.HasSuffix(host, "github.io") {
  97. if conn, err := dialer.DialContext(ctx, "tcp4", addr); err == nil {
  98. return conn, nil
  99. }
  100. return dialer.DialContext(ctx, "tcp6", addr)
  101. }
  102. return dialer.DialContext(ctx, network, addr)
  103. }
  104. return &http.Client{Transport: transport}
  105. }
  106. var (
  107. httpClientOnce sync.Once
  108. httpClient *http.Client
  109. )
  110. func getHTTPClient() *http.Client {
  111. httpClientOnce.Do(func() {
  112. httpClient = newHTTPClient()
  113. })
  114. return httpClient
  115. }
  116. func fetchJSON[T any](ctx context.Context, url string, out *upstreamEnvelope[T]) error {
  117. var lastErr error
  118. attempts := common.GetEnvOrDefault("SYNC_HTTP_RETRY", 3)
  119. if attempts < 1 {
  120. attempts = 1
  121. }
  122. baseDelay := 200 * time.Millisecond
  123. maxMB := common.GetEnvOrDefault("SYNC_HTTP_MAX_MB", 10)
  124. maxBytes := int64(maxMB) << 20
  125. for attempt := 0; attempt < attempts; attempt++ {
  126. req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
  127. if err != nil {
  128. return err
  129. }
  130. // ETag conditional request
  131. cacheMutex.RLock()
  132. if et := etagCache[url]; et != "" {
  133. req.Header.Set("If-None-Match", et)
  134. }
  135. cacheMutex.RUnlock()
  136. resp, err := getHTTPClient().Do(req)
  137. if err != nil {
  138. lastErr = err
  139. // backoff with jitter
  140. sleep := baseDelay * time.Duration(1<<attempt)
  141. jitter := time.Duration(rand.Intn(150)) * time.Millisecond
  142. time.Sleep(sleep + jitter)
  143. continue
  144. }
  145. func() {
  146. defer resp.Body.Close()
  147. switch resp.StatusCode {
  148. case http.StatusOK:
  149. // read body into buffer for caching and flexible decode
  150. limited := io.LimitReader(resp.Body, maxBytes)
  151. buf, err := io.ReadAll(limited)
  152. if err != nil {
  153. lastErr = err
  154. return
  155. }
  156. // cache body and ETag
  157. cacheMutex.Lock()
  158. if et := resp.Header.Get("ETag"); et != "" {
  159. etagCache[url] = et
  160. }
  161. bodyCache[url] = buf
  162. cacheMutex.Unlock()
  163. // Try decode as envelope first
  164. if err := json.Unmarshal(buf, out); err != nil {
  165. // Try decode as pure array
  166. var arr []T
  167. if err2 := json.Unmarshal(buf, &arr); err2 != nil {
  168. lastErr = err
  169. return
  170. }
  171. out.Success = true
  172. out.Data = arr
  173. out.Message = ""
  174. } else {
  175. if !out.Success && len(out.Data) == 0 && out.Message == "" {
  176. out.Success = true
  177. }
  178. }
  179. lastErr = nil
  180. case http.StatusNotModified:
  181. // use cache
  182. cacheMutex.RLock()
  183. buf := bodyCache[url]
  184. cacheMutex.RUnlock()
  185. if len(buf) == 0 {
  186. lastErr = errors.New("cache miss for 304 response")
  187. return
  188. }
  189. if err := json.Unmarshal(buf, out); err != nil {
  190. var arr []T
  191. if err2 := json.Unmarshal(buf, &arr); err2 != nil {
  192. lastErr = err
  193. return
  194. }
  195. out.Success = true
  196. out.Data = arr
  197. out.Message = ""
  198. } else {
  199. if !out.Success && len(out.Data) == 0 && out.Message == "" {
  200. out.Success = true
  201. }
  202. }
  203. lastErr = nil
  204. default:
  205. lastErr = errors.New(resp.Status)
  206. }
  207. }()
  208. if lastErr == nil {
  209. return nil
  210. }
  211. sleep := baseDelay * time.Duration(1<<attempt)
  212. jitter := time.Duration(rand.Intn(150)) * time.Millisecond
  213. time.Sleep(sleep + jitter)
  214. }
  215. return lastErr
  216. }
  217. func ensureVendorID(vendorName string, vendorByName map[string]upstreamVendor, vendorIDCache map[string]int, createdVendors *int) int {
  218. if vendorName == "" {
  219. return 0
  220. }
  221. if id, ok := vendorIDCache[vendorName]; ok {
  222. return id
  223. }
  224. var existing model.Vendor
  225. if err := model.DB.Where("name = ?", vendorName).First(&existing).Error; err == nil {
  226. vendorIDCache[vendorName] = existing.Id
  227. return existing.Id
  228. }
  229. uv := vendorByName[vendorName]
  230. v := &model.Vendor{
  231. Name: vendorName,
  232. Description: uv.Description,
  233. Icon: coalesce(uv.Icon, ""),
  234. Status: chooseStatus(uv.Status, 1),
  235. }
  236. if err := v.Insert(); err == nil {
  237. *createdVendors++
  238. vendorIDCache[vendorName] = v.Id
  239. return v.Id
  240. }
  241. vendorIDCache[vendorName] = 0
  242. return 0
  243. }
  244. // SyncUpstreamModels 同步上游模型与供应商:
  245. // - 默认仅创建「未配置模型」
  246. // - 可通过 overwrite 选择性覆盖更新本地已有模型的字段(前提:sync_official <> 0)
  247. func SyncUpstreamModels(c *gin.Context) {
  248. var req syncRequest
  249. // 允许空体
  250. _ = c.ShouldBindJSON(&req)
  251. // 1) 获取未配置模型列表
  252. missing, err := model.GetMissingModels()
  253. if err != nil {
  254. common.SysError("failed to get missing models: " + err.Error())
  255. c.JSON(http.StatusOK, gin.H{"success": false, "message": "获取模型列表失败,请稍后重试"})
  256. return
  257. }
  258. // 若既无缺失模型需要创建,也未指定覆盖更新字段,则无需请求上游数据,直接返回
  259. if len(missing) == 0 && len(req.Overwrite) == 0 {
  260. modelsURL, vendorsURL := getUpstreamURLs(req.Locale)
  261. c.JSON(http.StatusOK, gin.H{
  262. "success": true,
  263. "data": gin.H{
  264. "created_models": 0,
  265. "created_vendors": 0,
  266. "updated_models": 0,
  267. "skipped_models": []string{},
  268. "created_list": []string{},
  269. "updated_list": []string{},
  270. "source": gin.H{
  271. "locale": req.Locale,
  272. "models_url": modelsURL,
  273. "vendors_url": vendorsURL,
  274. },
  275. },
  276. })
  277. return
  278. }
  279. // 2) 拉取上游 vendors 与 models
  280. timeoutSec := common.GetEnvOrDefault("SYNC_HTTP_TIMEOUT_SECONDS", 15)
  281. ctx, cancel := context.WithTimeout(c.Request.Context(), time.Duration(timeoutSec)*time.Second)
  282. defer cancel()
  283. modelsURL, vendorsURL := getUpstreamURLs(req.Locale)
  284. var vendorsEnv upstreamEnvelope[upstreamVendor]
  285. var modelsEnv upstreamEnvelope[upstreamModel]
  286. var fetchErr error
  287. var wg sync.WaitGroup
  288. wg.Add(2)
  289. go func() {
  290. defer wg.Done()
  291. // vendor 失败不拦截
  292. _ = fetchJSON(ctx, vendorsURL, &vendorsEnv)
  293. }()
  294. go func() {
  295. defer wg.Done()
  296. if err := fetchJSON(ctx, modelsURL, &modelsEnv); err != nil {
  297. fetchErr = err
  298. }
  299. }()
  300. wg.Wait()
  301. if fetchErr != nil {
  302. c.JSON(http.StatusOK, gin.H{"success": false, "message": "获取上游模型失败: " + fetchErr.Error(), "locale": req.Locale, "source_urls": gin.H{"models_url": modelsURL, "vendors_url": vendorsURL}})
  303. return
  304. }
  305. // 建立映射
  306. vendorByName := make(map[string]upstreamVendor)
  307. for _, v := range vendorsEnv.Data {
  308. if v.Name != "" {
  309. vendorByName[v.Name] = v
  310. }
  311. }
  312. modelByName := make(map[string]upstreamModel)
  313. for _, m := range modelsEnv.Data {
  314. if m.ModelName != "" {
  315. modelByName[m.ModelName] = m
  316. }
  317. }
  318. // 3) 执行同步:仅创建缺失模型;若上游缺失该模型则跳过
  319. createdModels := 0
  320. createdVendors := 0
  321. updatedModels := 0
  322. skipped := make([]string, 0)
  323. createdList := make([]string, 0)
  324. updatedList := make([]string, 0)
  325. // 本地缓存:vendorName -> id
  326. vendorIDCache := make(map[string]int)
  327. for _, name := range missing {
  328. up, ok := modelByName[name]
  329. if !ok {
  330. skipped = append(skipped, name)
  331. continue
  332. }
  333. // 若本地已存在且设置为不同步,则跳过(极端情况:缺失列表与本地状态不同步时)
  334. var existing model.Model
  335. if err := model.DB.Where("model_name = ?", name).First(&existing).Error; err == nil {
  336. if existing.SyncOfficial == 0 {
  337. skipped = append(skipped, name)
  338. continue
  339. }
  340. }
  341. // 确保 vendor 存在
  342. vendorID := ensureVendorID(up.VendorName, vendorByName, vendorIDCache, &createdVendors)
  343. // 创建模型
  344. mi := &model.Model{
  345. ModelName: name,
  346. Description: up.Description,
  347. Icon: up.Icon,
  348. Tags: up.Tags,
  349. VendorID: vendorID,
  350. Status: chooseStatus(up.Status, 1),
  351. NameRule: up.NameRule,
  352. }
  353. if err := mi.Insert(); err == nil {
  354. createdModels++
  355. createdList = append(createdList, name)
  356. } else {
  357. skipped = append(skipped, name)
  358. }
  359. }
  360. // 4) 处理可选覆盖(更新本地已有模型的差异字段)
  361. if len(req.Overwrite) > 0 {
  362. // vendorIDCache 已用于创建阶段,可复用
  363. for _, ow := range req.Overwrite {
  364. up, ok := modelByName[ow.ModelName]
  365. if !ok {
  366. continue
  367. }
  368. var local model.Model
  369. if err := model.DB.Where("model_name = ?", ow.ModelName).First(&local).Error; err != nil {
  370. continue
  371. }
  372. // 跳过被禁用官方同步的模型
  373. if local.SyncOfficial == 0 {
  374. continue
  375. }
  376. // 映射 vendor
  377. newVendorID := ensureVendorID(up.VendorName, vendorByName, vendorIDCache, &createdVendors)
  378. // 应用字段覆盖(事务)
  379. _ = model.DB.Transaction(func(tx *gorm.DB) error {
  380. needUpdate := false
  381. if containsField(ow.Fields, "description") {
  382. local.Description = up.Description
  383. needUpdate = true
  384. }
  385. if containsField(ow.Fields, "icon") {
  386. local.Icon = up.Icon
  387. needUpdate = true
  388. }
  389. if containsField(ow.Fields, "tags") {
  390. local.Tags = up.Tags
  391. needUpdate = true
  392. }
  393. if containsField(ow.Fields, "vendor") {
  394. local.VendorID = newVendorID
  395. needUpdate = true
  396. }
  397. if containsField(ow.Fields, "name_rule") {
  398. local.NameRule = up.NameRule
  399. needUpdate = true
  400. }
  401. if containsField(ow.Fields, "status") {
  402. local.Status = chooseStatus(up.Status, local.Status)
  403. needUpdate = true
  404. }
  405. if !needUpdate {
  406. return nil
  407. }
  408. if err := tx.Save(&local).Error; err != nil {
  409. return err
  410. }
  411. updatedModels++
  412. updatedList = append(updatedList, ow.ModelName)
  413. return nil
  414. })
  415. }
  416. }
  417. c.JSON(http.StatusOK, gin.H{
  418. "success": true,
  419. "data": gin.H{
  420. "created_models": createdModels,
  421. "created_vendors": createdVendors,
  422. "updated_models": updatedModels,
  423. "skipped_models": skipped,
  424. "created_list": createdList,
  425. "updated_list": updatedList,
  426. "source": gin.H{
  427. "locale": req.Locale,
  428. "models_url": modelsURL,
  429. "vendors_url": vendorsURL,
  430. },
  431. },
  432. })
  433. }
  434. func containsField(fields []string, key string) bool {
  435. key = strings.ToLower(strings.TrimSpace(key))
  436. for _, f := range fields {
  437. if strings.ToLower(strings.TrimSpace(f)) == key {
  438. return true
  439. }
  440. }
  441. return false
  442. }
  443. func coalesce(a, b string) string {
  444. if strings.TrimSpace(a) != "" {
  445. return a
  446. }
  447. return b
  448. }
  449. func chooseStatus(primary, fallback int) int {
  450. if primary == 0 && fallback != 0 {
  451. return fallback
  452. }
  453. if primary != 0 {
  454. return primary
  455. }
  456. return 1
  457. }
  458. // SyncUpstreamPreview 预览上游与本地的差异(仅用于弹窗选择)
  459. func SyncUpstreamPreview(c *gin.Context) {
  460. // 1) 拉取上游数据
  461. timeoutSec := common.GetEnvOrDefault("SYNC_HTTP_TIMEOUT_SECONDS", 15)
  462. ctx, cancel := context.WithTimeout(c.Request.Context(), time.Duration(timeoutSec)*time.Second)
  463. defer cancel()
  464. locale := c.Query("locale")
  465. modelsURL, vendorsURL := getUpstreamURLs(locale)
  466. var vendorsEnv upstreamEnvelope[upstreamVendor]
  467. var modelsEnv upstreamEnvelope[upstreamModel]
  468. var fetchErr error
  469. var wg sync.WaitGroup
  470. wg.Add(2)
  471. go func() {
  472. defer wg.Done()
  473. _ = fetchJSON(ctx, vendorsURL, &vendorsEnv)
  474. }()
  475. go func() {
  476. defer wg.Done()
  477. if err := fetchJSON(ctx, modelsURL, &modelsEnv); err != nil {
  478. fetchErr = err
  479. }
  480. }()
  481. wg.Wait()
  482. if fetchErr != nil {
  483. c.JSON(http.StatusOK, gin.H{"success": false, "message": "获取上游模型失败: " + fetchErr.Error(), "locale": locale, "source_urls": gin.H{"models_url": modelsURL, "vendors_url": vendorsURL}})
  484. return
  485. }
  486. vendorByName := make(map[string]upstreamVendor)
  487. for _, v := range vendorsEnv.Data {
  488. if v.Name != "" {
  489. vendorByName[v.Name] = v
  490. }
  491. }
  492. modelByName := make(map[string]upstreamModel)
  493. upstreamNames := make([]string, 0, len(modelsEnv.Data))
  494. for _, m := range modelsEnv.Data {
  495. if m.ModelName != "" {
  496. modelByName[m.ModelName] = m
  497. upstreamNames = append(upstreamNames, m.ModelName)
  498. }
  499. }
  500. // 2) 本地已有模型
  501. var locals []model.Model
  502. if len(upstreamNames) > 0 {
  503. _ = model.DB.Where("model_name IN ? AND sync_official <> 0", upstreamNames).Find(&locals).Error
  504. }
  505. // 本地 vendor 名称映射
  506. vendorIdSet := make(map[int]struct{})
  507. for _, m := range locals {
  508. if m.VendorID != 0 {
  509. vendorIdSet[m.VendorID] = struct{}{}
  510. }
  511. }
  512. vendorIDs := make([]int, 0, len(vendorIdSet))
  513. for id := range vendorIdSet {
  514. vendorIDs = append(vendorIDs, id)
  515. }
  516. idToVendorName := make(map[int]string)
  517. if len(vendorIDs) > 0 {
  518. var dbVendors []model.Vendor
  519. _ = model.DB.Where("id IN ?", vendorIDs).Find(&dbVendors).Error
  520. for _, v := range dbVendors {
  521. idToVendorName[v.Id] = v.Name
  522. }
  523. }
  524. // 3) 缺失且上游存在的模型
  525. missingList, _ := model.GetMissingModels()
  526. var missing []string
  527. for _, name := range missingList {
  528. if _, ok := modelByName[name]; ok {
  529. missing = append(missing, name)
  530. }
  531. }
  532. // 4) 计算冲突字段
  533. type conflictField struct {
  534. Field string `json:"field"`
  535. Local interface{} `json:"local"`
  536. Upstream interface{} `json:"upstream"`
  537. }
  538. type conflictItem struct {
  539. ModelName string `json:"model_name"`
  540. Fields []conflictField `json:"fields"`
  541. }
  542. var conflicts []conflictItem
  543. for _, local := range locals {
  544. up, ok := modelByName[local.ModelName]
  545. if !ok {
  546. continue
  547. }
  548. fields := make([]conflictField, 0, 6)
  549. if strings.TrimSpace(local.Description) != strings.TrimSpace(up.Description) {
  550. fields = append(fields, conflictField{Field: "description", Local: local.Description, Upstream: up.Description})
  551. }
  552. if strings.TrimSpace(local.Icon) != strings.TrimSpace(up.Icon) {
  553. fields = append(fields, conflictField{Field: "icon", Local: local.Icon, Upstream: up.Icon})
  554. }
  555. if strings.TrimSpace(local.Tags) != strings.TrimSpace(up.Tags) {
  556. fields = append(fields, conflictField{Field: "tags", Local: local.Tags, Upstream: up.Tags})
  557. }
  558. // vendor 对比使用名称
  559. localVendor := idToVendorName[local.VendorID]
  560. if strings.TrimSpace(localVendor) != strings.TrimSpace(up.VendorName) {
  561. fields = append(fields, conflictField{Field: "vendor", Local: localVendor, Upstream: up.VendorName})
  562. }
  563. if local.NameRule != up.NameRule {
  564. fields = append(fields, conflictField{Field: "name_rule", Local: local.NameRule, Upstream: up.NameRule})
  565. }
  566. if local.Status != chooseStatus(up.Status, local.Status) {
  567. fields = append(fields, conflictField{Field: "status", Local: local.Status, Upstream: up.Status})
  568. }
  569. if len(fields) > 0 {
  570. conflicts = append(conflicts, conflictItem{ModelName: local.ModelName, Fields: fields})
  571. }
  572. }
  573. c.JSON(http.StatusOK, gin.H{
  574. "success": true,
  575. "data": gin.H{
  576. "missing": missing,
  577. "conflicts": conflicts,
  578. "source": gin.H{
  579. "locale": locale,
  580. "models_url": modelsURL,
  581. "vendors_url": vendorsURL,
  582. },
  583. },
  584. })
  585. }