model_sync.go 17 KB

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