MCPcopy Create free account
hub / github.com/QuantumNous/new-api / SyncUpstreamPreview

Function SyncUpstreamPreview

controller/model_sync.go:499–634  ·  view source on GitHub ↗

SyncUpstreamPreview 预览上游与本地的差异(仅用于弹窗选择)

(c *gin.Context)

Source from the content-addressed store, hash-verified

497
498// SyncUpstreamPreview 预览上游与本地的差异(仅用于弹窗选择)
499func SyncUpstreamPreview(c *gin.Context) {
500 // 1) 拉取上游数据
501 timeoutSec := common.GetEnvOrDefault("SYNC_HTTP_TIMEOUT_SECONDS", 15)
502 ctx, cancel := context.WithTimeout(c.Request.Context(), time.Duration(timeoutSec)*time.Second)
503 defer cancel()
504
505 locale := c.Query("locale")
506 modelsURL, vendorsURL := getUpstreamURLs(locale)
507
508 var vendorsEnv upstreamEnvelope[upstreamVendor]
509 var modelsEnv upstreamEnvelope[upstreamModel]
510 var fetchErr error
511 var wg sync.WaitGroup
512 wg.Add(2)
513 go func() {
514 defer wg.Done()
515 _ = fetchJSON(ctx, vendorsURL, &vendorsEnv)
516 }()
517 go func() {
518 defer wg.Done()
519 if err := fetchJSON(ctx, modelsURL, &modelsEnv); err != nil {
520 fetchErr = err
521 }
522 }()
523 wg.Wait()
524 if fetchErr != nil {
525 c.JSON(http.StatusOK, gin.H{"success": false, "message": "获取上游模型失败: " + fetchErr.Error(), "locale": locale, "source_urls": gin.H{"models_url": modelsURL, "vendors_url": vendorsURL}})
526 return
527 }
528
529 vendorByName := make(map[string]upstreamVendor)
530 for _, v := range vendorsEnv.Data {
531 if v.Name != "" {
532 vendorByName[v.Name] = v
533 }
534 }
535 modelByName := make(map[string]upstreamModel)
536 upstreamNames := make([]string, 0, len(modelsEnv.Data))
537 for _, m := range modelsEnv.Data {
538 if m.ModelName != "" {
539 modelByName[m.ModelName] = m
540 upstreamNames = append(upstreamNames, m.ModelName)
541 }
542 }
543
544 // 2) 本地已有模型
545 var locals []model.Model
546 if len(upstreamNames) > 0 {
547 _ = model.DB.Where("model_name IN ? AND sync_official <> 0", upstreamNames).Find(&locals).Error
548 }
549
550 // 本地 vendor 名称映射
551 vendorIdSet := make(map[int]struct{})
552 for _, m := range locals {
553 if m.VendorID != 0 {
554 vendorIdSet[m.VendorID] = struct{}{}
555 }
556 }

Callers

nothing calls this directly

Calls 9

GetEnvOrDefaultFunction · 0.92
GetMissingModelsFunction · 0.92
getUpstreamURLsFunction · 0.85
fetchJSONFunction · 0.85
chooseStatusFunction · 0.85
QueryMethod · 0.80
AddMethod · 0.80
DoneMethod · 0.80
ErrorMethod · 0.45

Tested by

no test coverage detected