MCPcopy
hub / github.com/QuantumNous/new-api / SyncUpstreamModels

Function SyncUpstreamModels

controller/model_sync.go:268–469  ·  view source on GitHub ↗

SyncUpstreamModels 同步上游模型与供应商: - 默认仅创建「未配置模型」 - 可通过 overwrite 选择性覆盖更新本地已有模型的字段(前提:sync_official <> 0)

(c *gin.Context)

Source from the content-addressed store, hash-verified

266// - 默认仅创建「未配置模型」
267// - 可通过 overwrite 选择性覆盖更新本地已有模型的字段(前提:sync_official <> 0)
268func SyncUpstreamModels(c *gin.Context) {
269 var req syncRequest
270 // 允许空体
271 _ = c.ShouldBindJSON(&req)
272 // 1) 获取未配置模型列表
273 missing, err := model.GetMissingModels()
274 if err != nil {
275 common.SysError("failed to get missing models: " + err.Error())
276 c.JSON(http.StatusOK, gin.H{"success": false, "message": "获取模型列表失败,请稍后重试"})
277 return
278 }
279
280 // 若既无缺失模型需要创建,也未指定覆盖更新字段,则无需请求上游数据,直接返回
281 if len(missing) == 0 && len(req.Overwrite) == 0 {
282 modelsURL, vendorsURL := getUpstreamURLs(req.Locale)
283 c.JSON(http.StatusOK, gin.H{
284 "success": true,
285 "data": gin.H{
286 "created_models": 0,
287 "created_vendors": 0,
288 "updated_models": 0,
289 "skipped_models": []string{},
290 "created_list": []string{},
291 "updated_list": []string{},
292 "source": gin.H{
293 "locale": req.Locale,
294 "models_url": modelsURL,
295 "vendors_url": vendorsURL,
296 },
297 },
298 })
299 return
300 }
301
302 // 2) 拉取上游 vendors 与 models
303 timeoutSec := common.GetEnvOrDefault("SYNC_HTTP_TIMEOUT_SECONDS", 15)
304 ctx, cancel := context.WithTimeout(c.Request.Context(), time.Duration(timeoutSec)*time.Second)
305 defer cancel()
306
307 modelsURL, vendorsURL := getUpstreamURLs(req.Locale)
308 var vendorsEnv upstreamEnvelope[upstreamVendor]
309 var modelsEnv upstreamEnvelope[upstreamModel]
310 var fetchErr error
311 var wg sync.WaitGroup
312 wg.Add(2)
313 go func() {
314 defer wg.Done()
315 // vendor 失败不拦截
316 _ = fetchJSON(ctx, vendorsURL, &vendorsEnv)
317 }()
318 go func() {
319 defer wg.Done()
320 if err := fetchJSON(ctx, modelsURL, &modelsEnv); err != nil {
321 fetchErr = err
322 }
323 }()
324 wg.Wait()
325 if fetchErr != nil {

Callers

nothing calls this directly

Calls 13

InsertMethod · 0.95
GetMissingModelsFunction · 0.92
SysErrorFunction · 0.92
GetEnvOrDefaultFunction · 0.92
getUpstreamURLsFunction · 0.85
fetchJSONFunction · 0.85
ensureVendorIDFunction · 0.85
chooseStatusFunction · 0.85
containsFieldFunction · 0.85
AddMethod · 0.80
DoneMethod · 0.80
SaveMethod · 0.80

Tested by

no test coverage detected