model_sync.go 16 KB

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