From 87cc22d7ec4923ad08df826866fb3e51c95ed1d8 Mon Sep 17 00:00:00 2001 From: Rain <1050807841@qq.com> Date: Thu, 4 Jun 2026 18:48:30 +0800 Subject: [PATCH] fix(distributor): resolve model for GET /v1/video/generations/:task_id (#5133) --- middleware/distributor.go | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/middleware/distributor.go b/middleware/distributor.go index 771719b9..34865c0c 100644 --- a/middleware/distributor.go +++ b/middleware/distributor.go @@ -298,6 +298,7 @@ func getModelRequest(c *gin.Context) (*ModelRequest, bool, error) { } else if c.Request.Method == http.MethodGet { relayMode = relayconstant.RelayModeVideoFetchByID shouldSelectChannel = false + modelRequest.Model = getTaskOriginModelName(c) } c.Set("relay_mode", relayMode) } else if strings.Contains(c.Request.URL.Path, "/v1/video/generations") { @@ -312,6 +313,7 @@ func getModelRequest(c *gin.Context) (*ModelRequest, bool, error) { } else if c.Request.Method == http.MethodGet { relayMode = relayconstant.RelayModeVideoFetchByID shouldSelectChannel = false + modelRequest.Model = getTaskOriginModelName(c) } if _, ok := c.Get("relay_mode"); !ok { c.Set("relay_mode", relayMode) @@ -396,6 +398,31 @@ func getModelRequest(c *gin.Context) (*ModelRequest, bool, error) { return &modelRequest, shouldSelectChannel, nil } +// 修复 #4834: GET /v1/video/generations/:task_id && /v1/video/:task_id 此前不解析 model, +// 当 token 启用「可用模型限制」时,下游 modelLimitEnable 校验会因 +// modelRequest.Model 为空而误报 "This token has no access to model"。 +// 从已存储的任务记录中回填 OriginModelName 即可让校验走在正确的模型上。 +func getTaskOriginModelName(c *gin.Context) string { + if !common.GetContextKeyBool(c, constant.ContextKeyTokenModelLimitEnabled) { + return "" + } + + taskId := c.Param("task_id") + if taskId == "" { + // jimeng adapter + taskId = c.GetString("task_id") + } + if taskId == "" { + return "" + } + + userId := c.GetInt("id") + if task, exist, err := model.GetByTaskId(userId, taskId); err == nil && exist && task != nil { + return task.Properties.OriginModelName + } + return "" +} + func SetupContextForSelectedChannel(c *gin.Context, channel *model.Channel, modelName string) *types.NewAPIError { c.Set("original_model", modelName) // for retry if channel == nil {