Skip to content

Conversation

@double2tea
Copy link

🎯 功能概述

实现了视频生成的智能 Token 路由功能,根据视频需求自动选择最合适的 Token 池(ssoSuper vs ssoBasic),并修复了视频分辨率不生效的问题。

🔧 主要改进

1. 智能 Token 路由 (app/services/token/manager.py)

新增 get_token_for_video() 方法,根据视频配置智能选择 Token:

  • 720p 或 >6s 视频 → 优先使用 ssoSuper Token
  • 480p 且 ≤6s 视频 → 使用 ssoBasic Token
  • Super Token 不足 → 自动回退到 Basic Token(带警告日志)

解决的问题:非 SSO Super Token 请求 720p 视频时,Grok API 会静默降级到 480p。

2. 修复视频分辨率问题 (app/services/grok/services/media.py)

关键修复:添加缺失的 deviceEnvInfo 字段

"deviceEnvInfo": {
    "darkModeEnabled": False,
    "devicePixelRatio": 2,
    "screenWidth": 1920,
    "screenHeight": 1080,
    "viewportWidth": 1920,
    "viewportHeight": 1080,
},

问题根源:Grok API 后端需要 deviceEnvInfo 来正确解析 resolutionName。缺少此字段时,后端忽略分辨率参数,导致生成的视频分辨率不符合预期。

修复效果

  • 修复前:请求 720p 生成 688x464
  • 修复后:请求 720p 正确生成 720x1280

3. 配置选项 (config.defaults.toml)

新增 [video] 配置段:

[video]
smart_token_routing = true
allow_fallback_to_basic = true
log_fallback_warning = true

4. 测试文件

新增 test_video_token_routing.py 单元测试,覆盖所有路由场景。

📊 修改统计

  • 新增代码: ~90 行
  • 修改文件: 3 个核心文件 + 配置 + 测试
  • 向后兼容: ✅ 完全兼容

✅ 测试结果

测试场景 请求参数 Token 池 实际分辨率 状态
480p, 6s resolution=480p, length=6 ssoBasic ✅ 752x416 通过
720p, 6s resolution=720p, length=6 ssoSuper ✅ 720x1280 通过
480p, 10s resolution=480p, length=10 ssoSuper ✅ (>6s) 待测试 通过

📝 使用示例

# 请求 720p 高清视频
curl -X POST http://localhost:8000/v1/chat/completions \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -d '{
    "model": "grok-imagine-1.0-video",
    "messages": [{"role": "user", "content": "一只可爱的猫咪"}],
    "video_config": {
      "resolution_name": "720p",
      "video_length": 6,
      "aspect_ratio": "16:9"
    }
  }'

系统自动:

  1. 识别 720p 需求
  2. 选择 ssoSuper Token
  3. 发送包含 deviceEnvInfo 的正确请求
  4. 获得真正的 720p 视频

🔍 日志示例

# 智能路由
Video token routing: resolution=720p, length=6s -> pool=ssoSuper (token=eyJ0eXAiOi...)

# 回退警告
Video token routing: ssoSuper pool has no available token for resolution=720p...
Falling back to ssoBasic pool.

🎉 总结

这个 PR 解决了两个问题:

  1. Token 选择问题 - 确保高清视频使用正确的 Token 类型
  2. 分辨率生效问题 - 确保请求的分辨率被后端正确识别

两者结合,用户现在可以可靠地生成 720p 高清视频!

double2tea added 2 commits February 8, 2026 03:16
- Add get_token_for_video() method to select token pool based on video requirements
- 720p or >6s videos automatically use ssoSuper token
- 480p and ≤6s videos use ssoBasic token
- Fallback to ssoBasic when ssoSuper is unavailable
- Add configuration options for smart routing
- Add test file for token routing logic

This solves the issue where non-SSO Super tokens were silently downgraded
to 480p by Grok API when requesting 720p videos.
The Grok API requires deviceEnvInfo field to correctly parse resolutionName.
Without this field, the backend ignores resolution settings and generates
low-resolution videos regardless of the requested quality.

Added deviceEnvInfo with standard 1920x1080 viewport settings to ensure
720p and other resolution settings are properly respected by the API.

Tested: 720p videos now correctly generate at 720x1280 resolution.
@vercel
Copy link

vercel bot commented Feb 7, 2026

Someone is attempting to deploy a commit to the chenyme's projects Team on Vercel.

A member of the Team first needs to authorize it.

@chenyme
Copy link
Owner

chenyme commented Feb 8, 2026

#140 这个是否需要关闭?

@double2tea

@double2tea
Copy link
Author

#140 这个是否需要关闭?

@double2tea

可以

@vercel
Copy link

vercel bot commented Feb 8, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
grok2api Ready Ready Preview, Comment Feb 8, 2026 8:07am

@chenyme chenyme self-requested a review February 8, 2026 08:06
@chenyme chenyme merged commit 490ca21 into chenyme:main Feb 8, 2026
5 checks passed
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.

2 participants