Skip to content

feat(xai): add grok-imagine-video task plugin - #1

Merged
datehoer merged 1 commit into
mainfrom
feat/xai-imagine-video-plugin
Sep 6, 2026
Merged

datehoer merged 1 commit into
mainfrom
feat/xai-imagine-video-plugin

Conversation

@datehoer

@datehoer datehoer commented Sep 6, 2026

Copy link
Copy Markdown
Owner

Agent

  • Tool: Grok
  • Tool version: Grok 4.6
  • Model (full id): grok-4.6
  • Host: CLI
  • Date (UTC): 2026-09-06

Links

datehoer/new-api 已关闭 Issues,无法在本仓库新建 Issue。上游已有同类 bug:xAI 渠道列出 grok-imagine-video,但 type 48 没有视频任务插件。

User request

用户说:「New API的话,它不支持这个视频模型吗?那它应该怎么做?」随后确认:「可以,你先改吧,而且我们也fork过这个项目,可以用我们fork的来修改然后build镜像。」改完后又说:「创建一个pr吧」。

Out of scope — refuse

  • Matched: no
  • If yes, what was told to the user (stop here; do not open a PR):

这不是新建逆向渠道,也不是第三方封装。xAI 渠道(type 48)本来就在 New API 里,模型列表已经包含 grok-imagine-video。本 PR 只补这个渠道缺失的视频任务插件,把 OpenAI Video 协议转成上游 /v1/videos/generations。

Kind

  • Bug fix
  • New feature
  • Performance / refactor
  • Docs
  • Other:

广告出来的模型走不通,属于 bug;同时补上 grok-imagine-video-1.5 和 OpenAI Video 端点声明。

Issue facts

取自上游 QuantumNous#6358,并与本次本地实测一致。

  • Actual behavior: POST /v1/videos 和 POST /v1/video/generations 返回 invalid_api_platform: 48,请求没有出网关。
  • Impact: 通过 New API 无法使用 xAI / grok2api 视频模型;聊天和图片同一条渠道正常。
  • Frequency: 每次视频请求必现。
  • Evidence that the problem is in new-api rather than the client or upstream: 直连 grok2api POST /v1/videos/generations 返回 200 并生成 mp4;同一 key 走 New API 在进入上游前就被拒。
  • Applicable types and their fields (relay / billing / frontend / deployment; write "not applicable" otherwise):
    • relay: POST /v1/videos、POST /v1/video/generations,channel type 48,model grok-imagine-video
    • billing: not applicable(本次不改计费公式,沿用任务插件 usage facts:seconds、resolution)
    • frontend: not applicable
    • deployment: 需重新构建镜像以嵌入 plugins/tasks/xai/plugin.js

Change

内置任务插件 plugins/tasks/xai/plugin.js,channelTypes: [48],把客户端 OpenAI Video 请求翻译成 xAI/grok2api 的 generations API。

  • relay/relay_adaptor.go 把 platform "48" 映射到插件 key xai,RelayTask 不再因为没有 adaptor 直接 400。
  • 插件提交到 POST /v1/videos/generations,轮询 GET /v1/videos/{id},把上游 pending/done/failed 映射成任务状态;done 与上游 fix(task): support xAI grok-imagine-video task polling QuantumNous/new-api#5927 描述一致。
  • seconds/size 转成 duration/aspect_ratio/resolution;也接受 grok 原生字段。
  • common/endpoint_type.go 给 grok-imagine-video* 声明 openai-video 端点。
  • xAI 模型列表增加 grok-imagine-video-1.5。

聊天、图片仍走原有 xAI adaptor,不改通道类型。

Research

Duplicate / prior art

Docs and code

  • https://docs.newapi.ai/ : 视频接口文档按 Sora/可灵等任务插件渠道说明;xAI 渠道文档覆盖聊天和图片,没有 type 48 的视频任务插件。
  • https://deepwiki.com/QuantumNous/new-api : 任务插件按 channelTypes 绑定,GetTaskAdaptor 通过 taskPluginKeys 解析;48 原先不在表里。
  • README / repo docs: README 指向 Video Interface,实现落在 plugins/tasks/* 和 relay/relay_adaptor.go。
  • Code paths and what they imply for this change:
    • constant.ChannelTypeXai = 48
    • relay/channel/xai/constants.go 已列出 grok-imagine-video
    • relay/relay_adaptor.go 的 taskPluginKeys 原先没有 48,导致 invalid_api_platform: 48
    • plugins/embed.go 会自动注册 plugins/tasks/*/plugin.js

Alternatives considered

  • Option A: 把 XAI 通道改成 Sora/OpenAI 视频类型,复用 sora 插件。
  • Option B: 给 type 48 单独做内置视频任务插件(本 PR)。
  • Why this approach: 方案 A 会破坏现有聊天和图片。xAI 视频的路径、状态值(done)和字段(duration/resolution)与 Sora 不同,需要独立映射。Gemini 也是对话 adaptor + 视频插件共存。

Files

Path Why
plugins/tasks/xai/plugin.js type 48 的视频任务插件
relay/relay_adaptor.go "48" → xai
relay/channel/xai/constants.go 增加 grok-imagine-video-1.5
common/model.go IsVideoGenerationModel
common/endpoint_type.go 视频模型声明 openai-video
plugins/builtin_plugins_test.go 内置插件清单与 channel type 绑定
plugins/xai_responses_test.go 协议、提交、轮询、artifact 测试
relay/xai_task_plugin_test.go platform 48 能解析到插件
common/endpoint_type_xai_video_test.go 端点类型测试

Behavior

  • Before: POST /v1/videos 返回 invalid_api_platform: 48。
  • After: 同一请求创建任务并轮询完成,可从 GET /v1/videos/{id}/content 下载 mp4。grok-chat-fast 对话不受影响。
  • Explicit non-goals / leftover work: 不解决 Console/Build 免费号没有视频额度的问题;不改计费单价;未把本分支推到 QuantumNous/new-api。

Verification

Only what was actually run.

  • Commands and results:
    • go test ./plugins ./relay ./common ./relay/channel/xai → pass
    • docker compose build new-api → new-api:datehoer 构建成功
    • 重建容器后 GET https://api.hotday.uk/v1/models 含 grok-imagine-video、grok-imagine-video-1.5,supported endpoint 含 openai-video
  • Manual steps and observed result:
    • POST /v1/videos model=grok-imagine-video seconds=6 size=1280x720 → 200 status=queued,随后 completed
    • GET /v1/videos/{id}/content → video/mp4,约 8.1MB
    • POST /v1/chat/completions model=grok-chat-fast → 仍返回 pong
    • grok-imagine-video-1.5 会进入 xAI 通道;若上游 Console 视频额度为 0,会返回上游限额/饱和,而不再是 platform 48
  • UI: screenshot or recording (or why none): 无 UI 改动,这是 API 行为。
  • Tests added or updated, or why none: 见上方测试文件。
  • Databases / providers / platforms exercised: MySQL 中 XAI 通道(id=32, type=48)指向 grok2api;Web grok-imagine-video 720p 文生视频。
  • Not verified: 官方 api.x.ai 直连、图生视频、1080p、Remix。

Risks

Scope check

  • Single focused change: yes
  • Secrets included: no
  • Out of scope (Coding Plan / reverse-engineered channel / third-party wrapper / Codex): no

xAI channel type 48 advertised grok-imagine-video but had no video
task adaptor, so POST /v1/videos failed with invalid_api_platform: 48.
Add a built-in plugin that maps OpenAI video requests onto the xAI /
grok2api generations API, including grok-imagine-video-1.5.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 6, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-09-06T13:29:15.329289Z 3217393 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 32173938e2

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

version: "1.0.0",
author: { name: "datehoer" },
channelTypes: [48],
models: ["grok-imagine-video", "grok-imagine-video-1.5"],

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Add default billing expressions for the video models

On a fresh installation without an administrator pricing override, both newly routable models fail before reaching xAI: neither model has a built-in billing expression or legacy ratio, so ModelPriceHelperPerCall returns model_price_error when AcceptUnsetRatioModel is false. Add self-contained expressions for both video models while preserving explicit administrator overrides.

AGENTS.md reference: AGENTS.md:L110-L110

Useful? React with 👍 / 👎.

Comment thread common/endpoint_type.go
Comment on lines +59 to +60
if IsVideoGenerationModel(modelName) {
endpointTypes = append([]constant.EndpointType{constant.EndpointTypeOpenAIVideo}, endpointTypes...)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Restrict video endpoint advertising to XAI channels

When an administrator configures a non-XAI channel with a model name containing grok-imagine-video, this unconditional model-name check advertises openai-video for that ability. Requests cannot use that advertised endpoint because the only matching plugin declares channelTypes: [48], and channel selection rejects every other type through the task-plugin identity filter. Gate this endpoint addition on an XAI-compatible channel type.

Useful? React with 👍 / 👎.

Comment on lines +165 to +166
let aspect = trimmed(req.aspect_ratio || metadata.aspect_ratio).toLowerCase();
let resolution = trimmed(req.resolution || metadata.resolution).toLowerCase();

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Preserve native fields on the legacy video route

For requests sent through the supported legacy POST /v1/video/generations route, these top-level native fields never reach the plugin: that route is intentionally not claimed by openai_video and its generic validation unmarshals into relay/common.TaskSubmitReq, which has neither aspect_ratio nor resolution. Consequently, a request such as grok-imagine-video-1.5 with resolution: "1080p" silently becomes the 720p default here. Preserve the native fields in the legacy request DTO or normalize them before invoking the plugin.

Useful? React with 👍 / 👎.

@@ -0,0 +1,195 @@
package plugins_test

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Consolidate the XAI regression tests

This focused feature creates three separate test files—common/endpoint_type_xai_video_test.go, this file, and relay/xai_task_plugin_test.go—in addition to extending an existing plugin test. Consolidate the regression cases into at most one new suitable test file as required by the backend test convention.

AGENTS.md reference: AGENTS.md:L126-L126

Useful? React with 👍 / 👎.

@datehoer
datehoer merged commit aaf8ccc into main Sep 6, 2026
2 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.

1 participant