model_sync.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633
  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. c.JSON(http.StatusOK, gin.H{"success": false, "message": err.Error()})
  255. return
  256. }
  257. // 若既无缺失模型需要创建,也未指定覆盖更新字段,则无需请求上游数据,直接返回
  258. if len(missing) == 0 && len(req.Overwrite) == 0 {
  259. modelsURL, vendorsURL := getUpstreamURLs(req.Locale)
  260. c.JSON(http.StatusOK, gin.H{
  261. "success": true,
  262. "data": gin.H{
  263. "created_models": 0,
  264. "created_vendors": 0,
  265. "updated_models": 0,
  266. "skipped_models": []string{},
  267. "created_list": []string{},
  268. "updated_list": []string{},
  269. "source": gin.H{
  270. "locale": req.Locale,
  271. "models_url": modelsURL,
  272. "vendors_url": vendorsURL,
  273. },
  274. },
  275. })
  276. return
  277. }
  278. // 2) 拉取上游 vendors 与 models
  279. timeoutSec := common.GetEnvOrDefault("SYNC_HTTP_TIMEOUT_SECONDS", 15)
  280. ctx, cancel := context.WithTimeout(c.Request.Context(), time.Duration(timeoutSec)*time.Second)
  281. defer cancel()
  282. modelsURL, vendorsURL := getUpstreamURLs(req.Locale)
  283. var vendorsEnv upstreamEnvelope[upstreamVendor]
  284. var modelsEnv upstreamEnvelope[upstreamModel]
  285. var fetchErr error
  286. var wg sync.WaitGroup
  287. wg.Add(2)
  288. go func() {
  289. defer wg.Done()
  290. // vendor 失败不拦截
  291. _ = fetchJSON(ctx, vendorsURL, &vendorsEnv)
  292. }()
  293. go func() {
  294. defer wg.Done()
  295. if err := fetchJSON(ctx, modelsURL, &modelsEnv); err != nil {
  296. fetchErr = err
  297. }
  298. }()
  299. wg.Wait()
  300. if fetchErr != nil {
  301. c.JSON(http.StatusOK, gin.H{"success": false, "message": "获取上游模型失败: " + fetchErr.Error(), "locale": req.Locale, "source_urls": gin.H{"models_url": modelsURL, "vendors_url": vendorsURL}})
  302. return
  303. }
  304. // 建立映射
  305. vendorByName := make(map[string]upstreamVendor)
  306. for _, v := range vendorsEnv.Data {
  307. if v.Name != "" {
  308. vendorByName[v.Name] = v
  309. }
  310. }
  311. modelByName := make(map[string]upstreamModel)
  312. for _, m := range modelsEnv.Data {
  313. if m.ModelName != "" {
  314. modelByName[m.ModelName] = m
  315. }
  316. }
  317. // 3) 执行同步:仅创建缺失模型;若上游缺失该模型则跳过
  318. createdModels := 0
  319. createdVendors := 0
  320. updatedModels := 0
  321. skipped := make([]string, 0)
  322. createdList := make([]string, 0)
  323. updatedList := make([]string, 0)
  324. // 本地缓存:vendorName -> id
  325. vendorIDCache := make(map[string]int)
  326. for _, name := range missing {
  327. up, ok := modelByName[name]
  328. if !ok {
  329. skipped = append(skipped, name)
  330. continue
  331. }
  332. // 若本地已存在且设置为不同步,则跳过(极端情况:缺失列表与本地状态不同步时)
  333. var existing model.Model
  334. if err := model.DB.Where("model_name = ?", name).First(&existing).Error; err == nil {
  335. if existing.SyncOfficial == 0 {
  336. skipped = append(skipped, name)
  337. continue
  338. }
  339. }
  340. // 确保 vendor 存在
  341. vendorID := ensureVendorID(up.VendorName, vendorByName, vendorIDCache, &createdVendors)
  342. // 创建模型
  343. mi := &model.Model{
  344. ModelName: name,
  345. Description: up.Description,
  346. Icon: up.Icon,
  347. Tags: up.Tags,
  348. VendorID: vendorID,
  349. Status: chooseStatus(up.Status, 1),
  350. NameRule: up.NameRule,
  351. }
  352. if err := mi.Insert(); err == nil {
  353. createdModels++
  354. createdList = append(createdList, name)
  355. } else {
  356. skipped = append(skipped, name)
  357. }
  358. }
  359. // 4) 处理可选覆盖(更新本地已有模型的差异字段)
  360. if len(req.Overwrite) > 0 {
  361. // vendorIDCache 已用于创建阶段,可复用
  362. for _, ow := range req.Overwrite {
  363. up, ok := modelByName[ow.ModelName]
  364. if !ok {
  365. continue
  366. }
  367. var local model.Model
  368. if err := model.DB.Where("model_name = ?", ow.ModelName).First(&local).Error; err != nil {
  369. continue
  370. }
  371. // 跳过被禁用官方同步的模型
  372. if local.SyncOfficial == 0 {
  373. continue
  374. }
  375. // 映射 vendor
  376. newVendorID := ensureVendorID(up.VendorName, vendorByName, vendorIDCache, &createdVendors)
  377. // 应用字段覆盖(事务)
  378. _ = model.DB.Transaction(func(tx *gorm.DB) error {
  379. needUpdate := false
  380. if containsField(ow.Fields, "description") {
  381. local.Description = up.Description
  382. needUpdate = true
  383. }
  384. if containsField(ow.Fields, "icon") {
  385. local.Icon = up.Icon
  386. needUpdate = true
  387. }
  388. if containsField(ow.Fields, "tags") {
  389. local.Tags = up.Tags
  390. needUpdate = true
  391. }
  392. if containsField(ow.Fields, "vendor") {
  393. local.VendorID = newVendorID
  394. needUpdate = true
  395. }
  396. if containsField(ow.Fields, "name_rule") {
  397. local.NameRule = up.NameRule
  398. needUpdate = true
  399. }
  400. if containsField(ow.Fields, "status") {
  401. local.Status = chooseStatus(up.Status, local.Status)
  402. needUpdate = true
  403. }
  404. if !needUpdate {
  405. return nil
  406. }
  407. if err := tx.Save(&local).Error; err != nil {
  408. return err
  409. }
  410. updatedModels++
  411. updatedList = append(updatedList, ow.ModelName)
  412. return nil
  413. })
  414. }
  415. }
  416. c.JSON(http.StatusOK, gin.H{
  417. "success": true,
  418. "data": gin.H{
  419. "created_models": createdModels,
  420. "created_vendors": createdVendors,
  421. "updated_models": updatedModels,
  422. "skipped_models": skipped,
  423. "created_list": createdList,
  424. "updated_list": updatedList,
  425. "source": gin.H{
  426. "locale": req.Locale,
  427. "models_url": modelsURL,
  428. "vendors_url": vendorsURL,
  429. },
  430. },
  431. })
  432. }
  433. func containsField(fields []string, key string) bool {
  434. key = strings.ToLower(strings.TrimSpace(key))
  435. for _, f := range fields {
  436. if strings.ToLower(strings.TrimSpace(f)) == key {
  437. return true
  438. }
  439. }
  440. return false
  441. }
  442. func coalesce(a, b string) string {
  443. if strings.TrimSpace(a) != "" {
  444. return a
  445. }
  446. return b
  447. }
  448. func chooseStatus(primary, fallback int) int {
  449. if primary == 0 && fallback != 0 {
  450. return fallback
  451. }
  452. if primary != 0 {
  453. return primary
  454. }
  455. return 1
  456. }
  457. // SyncUpstreamPreview 预览上游与本地的差异(仅用于弹窗选择)
  458. func SyncUpstreamPreview(c *gin.Context) {
  459. // 1) 拉取上游数据
  460. timeoutSec := common.GetEnvOrDefault("SYNC_HTTP_TIMEOUT_SECONDS", 15)
  461. ctx, cancel := context.WithTimeout(c.Request.Context(), time.Duration(timeoutSec)*time.Second)
  462. defer cancel()
  463. locale := c.Query("locale")
  464. modelsURL, vendorsURL := getUpstreamURLs(locale)
  465. var vendorsEnv upstreamEnvelope[upstreamVendor]
  466. var modelsEnv upstreamEnvelope[upstreamModel]
  467. var fetchErr error
  468. var wg sync.WaitGroup
  469. wg.Add(2)
  470. go func() {
  471. defer wg.Done()
  472. _ = fetchJSON(ctx, vendorsURL, &vendorsEnv)
  473. }()
  474. go func() {
  475. defer wg.Done()
  476. if err := fetchJSON(ctx, modelsURL, &modelsEnv); err != nil {
  477. fetchErr = err
  478. }
  479. }()
  480. wg.Wait()
  481. if fetchErr != nil {
  482. c.JSON(http.StatusOK, gin.H{"success": false, "message": "获取上游模型失败: " + fetchErr.Error(), "locale": locale, "source_urls": gin.H{"models_url": modelsURL, "vendors_url": vendorsURL}})
  483. return
  484. }
  485. vendorByName := make(map[string]upstreamVendor)
  486. for _, v := range vendorsEnv.Data {
  487. if v.Name != "" {
  488. vendorByName[v.Name] = v
  489. }
  490. }
  491. modelByName := make(map[string]upstreamModel)
  492. upstreamNames := make([]string, 0, len(modelsEnv.Data))
  493. for _, m := range modelsEnv.Data {
  494. if m.ModelName != "" {
  495. modelByName[m.ModelName] = m
  496. upstreamNames = append(upstreamNames, m.ModelName)
  497. }
  498. }
  499. // 2) 本地已有模型
  500. var locals []model.Model
  501. if len(upstreamNames) > 0 {
  502. _ = model.DB.Where("model_name IN ? AND sync_official <> 0", upstreamNames).Find(&locals).Error
  503. }
  504. // 本地 vendor 名称映射
  505. vendorIdSet := make(map[int]struct{})
  506. for _, m := range locals {
  507. if m.VendorID != 0 {
  508. vendorIdSet[m.VendorID] = struct{}{}
  509. }
  510. }
  511. vendorIDs := make([]int, 0, len(vendorIdSet))
  512. for id := range vendorIdSet {
  513. vendorIDs = append(vendorIDs, id)
  514. }
  515. idToVendorName := make(map[int]string)
  516. if len(vendorIDs) > 0 {
  517. var dbVendors []model.Vendor
  518. _ = model.DB.Where("id IN ?", vendorIDs).Find(&dbVendors).Error
  519. for _, v := range dbVendors {
  520. idToVendorName[v.Id] = v.Name
  521. }
  522. }
  523. // 3) 缺失且上游存在的模型
  524. missingList, _ := model.GetMissingModels()
  525. var missing []string
  526. for _, name := range missingList {
  527. if _, ok := modelByName[name]; ok {
  528. missing = append(missing, name)
  529. }
  530. }
  531. // 4) 计算冲突字段
  532. type conflictField struct {
  533. Field string `json:"field"`
  534. Local interface{} `json:"local"`
  535. Upstream interface{} `json:"upstream"`
  536. }
  537. type conflictItem struct {
  538. ModelName string `json:"model_name"`
  539. Fields []conflictField `json:"fields"`
  540. }
  541. var conflicts []conflictItem
  542. for _, local := range locals {
  543. up, ok := modelByName[local.ModelName]
  544. if !ok {
  545. continue
  546. }
  547. fields := make([]conflictField, 0, 6)
  548. if strings.TrimSpace(local.Description) != strings.TrimSpace(up.Description) {
  549. fields = append(fields, conflictField{Field: "description", Local: local.Description, Upstream: up.Description})
  550. }
  551. if strings.TrimSpace(local.Icon) != strings.TrimSpace(up.Icon) {
  552. fields = append(fields, conflictField{Field: "icon", Local: local.Icon, Upstream: up.Icon})
  553. }
  554. if strings.TrimSpace(local.Tags) != strings.TrimSpace(up.Tags) {
  555. fields = append(fields, conflictField{Field: "tags", Local: local.Tags, Upstream: up.Tags})
  556. }
  557. // vendor 对比使用名称
  558. localVendor := idToVendorName[local.VendorID]
  559. if strings.TrimSpace(localVendor) != strings.TrimSpace(up.VendorName) {
  560. fields = append(fields, conflictField{Field: "vendor", Local: localVendor, Upstream: up.VendorName})
  561. }
  562. if local.NameRule != up.NameRule {
  563. fields = append(fields, conflictField{Field: "name_rule", Local: local.NameRule, Upstream: up.NameRule})
  564. }
  565. if local.Status != chooseStatus(up.Status, local.Status) {
  566. fields = append(fields, conflictField{Field: "status", Local: local.Status, Upstream: up.Status})
  567. }
  568. if len(fields) > 0 {
  569. conflicts = append(conflicts, conflictItem{ModelName: local.ModelName, Fields: fields})
  570. }
  571. }
  572. c.JSON(http.StatusOK, gin.H{
  573. "success": true,
  574. "data": gin.H{
  575. "missing": missing,
  576. "conflicts": conflicts,
  577. "source": gin.H{
  578. "locale": locale,
  579. "models_url": modelsURL,
  580. "vendors_url": vendorsURL,
  581. },
  582. },
  583. })
  584. }