Skip to content

fix(distributor): resolve model for GET /v1/video/generations/:task_id - #5133

Merged
seefs001 merged 3 commits into
QuantumNous:mainfrom
Pluviobyte:fix/video-task-fetch-model-limit
Jun 4, 2026
Merged

seefs001 merged 3 commits into
QuantumNous:mainfrom
Pluviobyte:fix/video-task-fetch-model-limit

Conversation

@Pluviobyte

@Pluviobyte Pluviobyte commented May 27, 2026 •

Copy link
Copy Markdown
Contributor

📝 变更描述 / Description

修复 GET /v1/video/generations/:task_id 在 token 启用「可用模型限制」时被误判为 This token has no access to model 的问题。

根因:middleware/distributor.go 的 getModelRequest() 在 GET 分支只设置了 shouldSelectChannel = false,却没有把 modelRequest.Model 填上。后续 Distribute() 里的 modelLimitEnable 校验会拿空字符串去匹配 token 的可用模型列表,必然失败。

由于任务提交(POST)时 task.Properties.OriginModelName 已经持久化到数据库,本 PR 只是在 GET 分支里多加一次 model.GetByTaskId(userId, taskId) 查询,回填 modelRequest.Model,让校验跑在正确的模型上 —— 与 POST 路径行为对齐。

🚀 变更类型 / Type of change

  • 🐛 Bug 修复 (Bug fix)
  • ✨ 新功能 (New feature)
  • ⚡ 性能优化 / 重构 (Refactor)
  • 📝 文档更新 (Documentation)

🔗 关联任务 / Related Issue

✅ 提交前检查项 / Checklist

  • 人工确认: 我已亲自整理并撰写此描述,没有直接粘贴未经处理的 AI 输出。
  • 非重复提交: 已搜索 #4834 在 Issues/PRs 中无其它在修的 PR。
  • Bug fix 说明: Issue 报告者已给出明确根因和复现步骤,本 PR 就是按其分析修复。
  • 变更理解: 改动只影响 GET /v1/video/generations/:task_id 的 model 解析,不影响 POST 路径与其它端点;DB 查询用 (user_id, task_id) 复合条件,不会泄漏跨用户任务。
  • 范围聚焦: 仅一处 10 行新增,无任何无关改动。
  • 本地验证: go vet ./middleware/ ✅,go build ./middleware/ ✅,gofmt -l 干净,已有 middleware 包测试无回归。
  • 安全合规: 不引入新凭据;GetByTaskId 已经按 user_id 隔离。

📸 运行证明 / Proof of Work

Bug 现场(来自 #4834)

GET /v1/video/generations/task_It5QKuOy8LKF0z2GDpiQk1f4m2H7GRqk
Authorization: Bearer sk-xxxx

修复前:

HTTP/1.1 403 Forbidden
{
  "error": {
    "code": "",
    "message": "This token has no access to model  (request id: ...)",
    "type": "new_api_error"
  }
}

注意 model 后面是两个空格 —— 因为 model 名称是空字符串拼进了错误消息里,这是肉眼可见的「model 没解析出来」。

修复点

middleware/distributor.go GET 分支新增:

if taskId := c.Param("task_id"); taskId != "" {
    userId := c.GetInt("id")
    if task, exist, err := model.GetByTaskId(userId, taskId); err == nil && exist && task != nil {
        modelRequest.Model = task.Properties.OriginModelName
    }
}

边界行为

场景 行为
正常:task 存在且 OriginModelName 非空 ✅ 模型限制按真实模型校验
task_id 为空 退回原行为(modelRequest.Model 仍为空)
task 不属于当前 user GetByTaskId 按 user_id 过滤返回不存在,行为不变(既存安全语义保留)
DB 错误 静默忽略,行为不变(不会比修复前更糟)
OriginModelName 为空(历史脏数据) 退回原行为

本地验证

$ GOTOOLCHAIN=go1.25.1 go vet ./middleware/
(no output)
$ GOTOOLCHAIN=go1.25.1 gofmt -l middleware/distributor.go
(no output)
$ GOTOOLCHAIN=go1.25.1 go build ./middleware/
(success)
$ GOTOOLCHAIN=go1.25.1 go test ./middleware/
ok  	github.com/QuantumNous/new-api/middleware	0.031s

完整 build 需要前端 web/classic/dist,超出此 PR 范围。

备注

middleware/distributor.go 当前没有单元测试,GetByTaskId 依赖真实 DB,所以本 PR 未追加单测;如维护者希望补,我可以另外引入 sqlmock 在下一 PR 里加。

Made with Cursor

Summary by CodeRabbit

  • Bug Fixes
    • When fetching video-generation tasks by ID, the system now backfills the model from the persisted task record for access validation, preventing spurious permission errors.
    • Channel selection remains disabled for these fetch-by-ID flows to preserve existing behavior.

@coderabbitai

coderabbitai Bot commented May 27, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: f2f6fbd4-b2ef-4ad2-869d-8e42cd508a85

📥 Commits

Reviewing files that changed from the base of the PR and between 82e4ff6 and cb0a9db.

📒 Files selected for processing (1)
  • middleware/distributor.go
🚧 Files skipped from review as they are similar to previous changes (1)
  • middleware/distributor.go

Walkthrough

Backfills modelRequest.Model for relay-mode GET fetch-by-ID video endpoints by loading the persisted task and using its Properties.OriginModelName so downstream token model-access validation uses the original model name.

Changes

Video Task Status Model Backfill

Layer / File(s) Summary
getTaskOriginModelName helper
middleware/distributor.go
New helper reads task_id from route params (with fallback), calls model.GetByTaskId(userId, taskId), and returns task.Properties.OriginModelName when the task exists; no-op when model-limit is disabled or task missing.
Backfill model for fetch-by-ID video flows
middleware/distributor.go
getModelRequest now sets modelRequest.Model for GET /v1/videos/:task_id and GET /v1/video/generations/:task_id by calling getTaskOriginModelName instead of leaving it unset.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Suggested reviewers

  • seefs001

Poem

🐰 I hopped to the task and peeked inside,
Found the model name where it used to hide,
Now fetch-by-ID calls know what to say,
Token checks pass and errors melt away,
Hop, sip, and ship — the pipeline's bright today!

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 33.33% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main fix: resolving the model field for GET /v1/video/generations/:task_id requests to enable token model-limit validation.
Linked Issues check ✅ Passed The changes fully address issue #4834 by adding model lookup via getTaskOriginModelName() for GET requests, enabling model-limit validation to work correctly with restricted API keys.
Out of Scope Changes check ✅ Passed All changes are scoped to the distributor.go middleware to fix the identified bug; no unrelated modifications are present in the changeset.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

When a token has "available models" restriction enabled, polling a video
task status via GET /v1/video/generations/:task_id failed with:

  This token has no access to model

Because the GET branch in getModelRequest() set shouldSelectChannel=false
but never populated modelRequest.Model. The later modelLimitEnable check
then ran against an empty model name and rejected the request, even
though the same token was perfectly allowed to submit the task in the
first place.

The original (POST) request was already persisted in the task table via
Properties.OriginModelName. This change looks it up by (user_id,
task_id) and back-fills modelRequest.Model so the model-limit check
runs against the correct model — matching the behavior of the POST
submit path.

Fixes QuantumNous#4834

Co-authored-by: Cursor <[email protected]>
@Pluviobyte
Pluviobyte force-pushed the fix/video-task-fetch-model-limit branch from f74964f to 385bb50 Compare May 27, 2026 07:30

@panjinhe panjinhe left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed by Codex: small focused fix, checks passing.

@panjinhe

Copy link
Copy Markdown

Please disregard my previous approval on this PR. It was submitted by mistake while I was checking the wrong repository.

@seefs001
seefs001 merged commit 87cc22d into QuantumNous:main Jun 4, 2026
Soein added a commit to Soein/new-api that referenced this pull request Jun 6, 2026
… id 等)

主要变更(QuantumNous/new-api → adc390c):
- feat: 渠道被禁用后可配置是否清空渠道粘性 (QuantumNous#5306)
- feat(web): profile 页显示 user id (QuantumNous#5317)
- fix: 复用 channel handler 的 stream scanner buffer (QuantumNous#5225)
- fix: 收窄 OpenAI o 系列模型适配范围 (QuantumNous#5293)
- fix(relay): GLM Anthropic 兼容避免 chunked encoding (QuantumNous#5307)
- fix: 新增 relay idle 连接超时配置 (QuantumNous#5309)
- fix: 限制匿名请求体大小 (QuantumNous#5244)
- fix(distributor): 修复 video generations task_id 模型解析 (QuantumNous#5133)
- fix(dify): 远程图片字段赋值前初始化 file pointer (QuantumNous#5134)
- fix(i18n): 优化 thinking adapter 文案 (QuantumNous#5242)

去合规一致性:上游未触及任何合规文件,合并完整保留本地去合规状态
(payment_setting.go / payment-settings-section.tsx / recharge-form-card.tsx /
risk-acknowledgement-dialog.tsx 均未被改动)。

i18n:web/default 6 语言(en/zh/fr/ja/ru/vi)key 并集三路合并 + i18n:sync
规范化,4584 keys/语言,missing/extras/untranslated 全为 0。

验证:go build ./... ✅ + bun run typecheck ✅
Ember-Moth pushed a commit to Herta-Network/new-api that referenced this pull request Jun 7, 2026
yangshare added a commit to yangshare/new-api that referenced this pull request Jun 16, 2026
合并上游 QuantumNous/new-api v1.0.0-rc.11,主要新增 Claude Opus 4.8、
OpenAI 图片流式中继、渠道粘性清空选项、安全审计日志、模型定价双栏重构、
Dialog prop-based API 重构。

冲突解决(20 文件):
- 13 个 dialog 采用上游新结构,保留 dev sm: 响应式前缀与 ESLint 注释
- common-logs-columns / details-dialog 的 multikey 徽章功能完整保留
- channels-table 采用上游 useDebouncedColumnFilter
- model-pricing-sheet / model-ratio-visual-editor 采用 upstream 版本
- codex-oauth-dialog 删除(跟随上游 QuantumNous#5461)

撞车修复核对通过:视频任务 GET 验证(QuantumNous#4834/QuantumNous#5133)、匿名请求体限制(QuantumNous#5244)
逻辑均正确保留,无重复。

验证:前端 typecheck + 生产 build 通过,后端 go build 通过,i18n 全语言对齐。
zhaodechao2008 pushed a commit to zhaodechao2008/new-api that referenced this pull request Jul 27, 2026
MoonFlower-King pushed a commit to MoonFlower-King/rivermoon-codex-plugin that referenced this pull request Sep 2, 2026
The plugin existed to put a RiverMoon relay's media models into Codex, but it
only ever did images, and the tokens it provisioned were hardcoded to
model_limits "gpt-image-2" — so a video model would have been refused even
after a successful login.

Capabilities now come from a channel list built in config. Each channel
becomes one model-limited token on the user's account, and grants carry the
resulting keys as a map instead of two named fields, so the next capability is
a configuration change rather than another field threaded through OAuth.
Grants issued before the map still resolve.

Video generation is asynchronous, so generate_video submits the job, waits up
to VIDEO_INLINE_WAIT_MS, and then hands back a task_id for check_video to
poll. Both the tool description and the skill are explicit that a running task
must never be resubmitted, since every submission is billed. A finished clip
is returned as a URL; an mp4 is far past any inline limit.

Video stays hidden until VIDEO_MODELS names a model the relay exposes, so a
relay without video channels advertises no video tools. Task queries need
NewAPI with QuantumNous/new-api#5133; older releases refuse a query made with
a model-limited token, and that response is now reported as what it is.

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01FJveNjnJgo6jG53QfkUuGN
junjundesk pushed a commit to junjundesk/new-api that referenced this pull request Oct 3, 2026
NaturalSelect pushed a commit to NaturalSelect/new-api that referenced this pull request Nov 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

apiKey设置可用模型为指定模型时,使用获取视频生成任务状态接口查询任务会报错:"This token has no access to model"

3 participants