Просмотр исходного кода

🚀 fix(model-sync): avoid unnecessary upstream fetch while keeping overwrite updates working

- Only short-circuit when there are no missing models AND no overwrite fields requested
- Preserve overwrite behavior even when the missing-model list is empty
- Always return empty arrays (not null) for list fields to keep API responses stable
- Clarify SyncUpstreamModels behavior in comments (create missing models + optional overwrite updates)
t0ng7u 2 дней назад
Родитель
Сommit
83fbaba768
2 измененных файлов с 29 добавлено и 5 удалено
  1. 28 4
      controller/model_sync.go
  2. 1 1
      service/convert.go

+ 28 - 4
controller/model_sync.go

@@ -249,7 +249,9 @@ func ensureVendorID(vendorName string, vendorByName map[string]upstreamVendor, v
 	return 0
 }
 
-// SyncUpstreamModels 同步上游模型与供应商,仅对「未配置模型」生效
+// SyncUpstreamModels 同步上游模型与供应商:
+// - 默认仅创建「未配置模型」
+// - 可通过 overwrite 选择性覆盖更新本地已有模型的字段(前提:sync_official <> 0)
 func SyncUpstreamModels(c *gin.Context) {
 	var req syncRequest
 	// 允许空体
@@ -261,6 +263,28 @@ func SyncUpstreamModels(c *gin.Context) {
 		return
 	}
 
+	// 若既无缺失模型需要创建,也未指定覆盖更新字段,则无需请求上游数据,直接返回
+	if len(missing) == 0 && len(req.Overwrite) == 0 {
+		modelsURL, vendorsURL := getUpstreamURLs(req.Locale)
+		c.JSON(http.StatusOK, gin.H{
+			"success": true,
+			"data": gin.H{
+				"created_models":  0,
+				"created_vendors": 0,
+				"updated_models":  0,
+				"skipped_models":  []string{},
+				"created_list":    []string{},
+				"updated_list":    []string{},
+				"source": gin.H{
+					"locale":      req.Locale,
+					"models_url":  modelsURL,
+					"vendors_url": vendorsURL,
+				},
+			},
+		})
+		return
+	}
+
 	// 2) 拉取上游 vendors 与 models
 	timeoutSec := common.GetEnvOrDefault("SYNC_HTTP_TIMEOUT_SECONDS", 15)
 	ctx, cancel := context.WithTimeout(c.Request.Context(), time.Duration(timeoutSec)*time.Second)
@@ -307,9 +331,9 @@ func SyncUpstreamModels(c *gin.Context) {
 	createdModels := 0
 	createdVendors := 0
 	updatedModels := 0
-	var skipped []string
-	var createdList []string
-	var updatedList []string
+	skipped := make([]string, 0)
+	createdList := make([]string, 0)
+	updatedList := make([]string, 0)
 
 	// 本地缓存:vendorName -> id
 	vendorIDCache := make(map[string]int)

+ 1 - 1
service/convert.go

@@ -401,7 +401,7 @@ func StreamResponseOpenAI2Claude(openAIResponse *dto.ChatCompletionsStreamRespon
 						},
 					})
 				}
-				
+
 				if len(toolCall.Function.Arguments) > 0 {
 					claudeResponses = append(claudeResponses, &dto.ClaudeResponse{
 						Index: &idx,