fix: expose Console grok-imagine-video-1.5 for free accounts

Move video 1.5 onto the Console catalog/media path instead of Build free
capability injection. Gateway now forwards the selected upstream video
model, and Console GenerateVideo sends grok-imagine-video-1.5 upstream.
Build free accounts again follow the original Super-only 1.5 rule.
This commit is contained in:
lindahao committed 2026-08-12 15:25:48 +08:00
1 parent 3f7a0f91ff
commit c93cabbf71
10 files changed
+85 -44

No files matched your search

@@ -40,4 +40,7 @@ func TestConsoleRoutesUseStableProviderNamespace(t *testing.T) {
if !seen["Console/grok-imagine-video"]["video"] {
t.Fatal("Console video route is missing")
}
if !seen["Console/grok-imagine-video-1.5"]["video"] {
t.Fatal("Console video 1.5 route is missing")
}
}
@@ -340,7 +340,8 @@ func (s *Service) runVideoJob(parent context.Context, job media.Job, route model
}
lastProgress := job.Progress
result, err := adapter.GenerateVideo(ctx, provider.VideoRequest{
Credential: lease.Credential, Billing: lease.Billing, JobID: job.ID, Prompt: job.Prompt, Duration: job.Seconds, AspectRatio: job.Size, Resolution: job.Quality,
Credential: lease.Credential, Billing: lease.Billing, JobID: job.ID, Model: route.UpstreamModel,
Prompt: job.Prompt, Duration: job.Seconds, AspectRatio: job.Size, Resolution: job.Quality,
ReferenceURLs: referenceURLs,
Progress: func(value int) {
value = min(99, max(1, value))
@@ -260,8 +260,8 @@ func TestSyncAccountNormalizesBuildVideo15ByBillingSuper(t *testing.T) {
assertSupports(superPrimary.ID, video15, true)
assertSupports(superFallback.ID, video15, true)
assertSupports(freeAccount.ID, video15, true)
assertSupports(unknownAccount.ID, video15, true)
assertSupports(freeAccount.ID, video15, false)
assertSupports(unknownAccount.ID, video15, false)
assertSupports(superPrimary.ID, "grok-4.5", true)
assertSupports(superFallback.ID, "grok-code-fast-1", true)
assertSupports(freeAccount.ID, "grok-4.5", true)
@@ -374,12 +374,11 @@ type buildCapabilityNormalizerAdapter struct {
}
func (a *buildCapabilityNormalizerAdapter) NormalizeAccountModelCapabilities(models []string, billing *account.Billing, credential account.Credential) []string {
// Match cli.Adapter rules: Build OAuth adds Composer; all Build accounts
// ensure video 1.5 so Free can schedule grok-imagine-video-1.5.
_ = billing
// Match cli.Adapter rules: Build OAuth adds Composer; Super (paid or
// entitlement) ensures 1.5; otherwise remove video 1.5 exactly.
const video15 = "grok-imagine-video-1.5"
buildAccount := credential.Provider == account.ProviderBuild
composer := buildAccount && credential.AuthType == account.AuthTypeOAuth
super := account.IsBuildSuper(credential, billing)
composer := credential.Provider == account.ProviderBuild && credential.AuthType == account.AuthTypeOAuth
result := make([]string, 0, len(models)+2)
seen := make(map[string]struct{}, len(models)+2)
hasVideo15 := false
@@ -391,12 +390,15 @@ func (a *buildCapabilityNormalizerAdapter) NormalizeAccountModelCapabilities(mod
continue
}
if modelName == video15 {
if !super {
continue
}
hasVideo15 = true
}
seen[modelName] = struct{}{}
result = append(result, modelName)
}
if buildAccount && !hasVideo15 {
if super && !hasVideo15 {
result = append(result, video15)
}
if composer {
@@ -752,7 +752,7 @@ func discoveredRouteDefaults(provider account.Provider, upstreamModel string) (s
// Discovery only needs one existing managed capability to remain
// idempotent and must never synthesize a Responses route.
return upstreamModel, model.CapabilityImage
case "grok-imagine-video":
case "grok-imagine-video", "grok-imagine-video-1.5":
return upstreamModel, model.CapabilityVideo
default:
return upstreamModel, model.CapabilityResponses
@@ -619,12 +619,11 @@ func (a *Adapter) ListModels(ctx context.Context, credential account.Credential)
// NormalizeAccountModelCapabilities normalizes capabilities that the OAuth
// session contract exposes independently of the account's sparse /models list.
// Composer is available to Build OAuth sessions even though the live catalog can
// return only grok-4.5. Build accounts always include video 1.5 so Free can use
// grok-imagine-video-1.5 on the primary Build path. BuildAPIFallback is ignored.
// return only grok-4.5. Super always includes video 1.5; Free and Unknown remove
// video 1.5 exactly. BuildAPIFallback is ignored.
func (a *Adapter) NormalizeAccountModelCapabilities(models []string, billing *account.Billing, credential account.Credential) []string {
_ = billing
buildAccount := credential.Provider == account.ProviderBuild
composer := buildAccount && credential.AuthType == account.AuthTypeOAuth
super := account.IsBuildSuper(credential, billing)
composer := credential.Provider == account.ProviderBuild && credential.AuthType == account.AuthTypeOAuth
result := make([]string, 0, len(models)+2)
seen := make(map[string]struct{}, len(models)+2)
hasVideo15 := false
@@ -637,12 +636,15 @@ func (a *Adapter) NormalizeAccountModelCapabilities(models []string, billing *ac
continue
}
if model == buildVideoModel {
if !super {
continue
}
hasVideo15 = true
}
seen[model] = struct{}{}
result = append(result, model)
}
if buildAccount && !hasVideo15 {
if super && !hasVideo15 {
result = append(result, buildVideoModel)
}
if composer {
@@ -566,7 +566,7 @@ func TestGetBillingUsesCreditsAndLiveSubscriptionTier(t *testing.T) {
}
}
func TestNormalizeAccountModelCapabilitiesBuildAddsVideo15(t *testing.T) {
func TestNormalizeAccountModelCapabilitiesSuperAddsVideo15(t *testing.T) {
adapter := &Adapter{}
build := account.Credential{Provider: account.ProviderBuild}
// Super / paid: add 1.5 even when primary Build returns only grok-4.5.
@@ -583,21 +583,16 @@ func TestNormalizeAccountModelCapabilitiesBuildAddsVideo15(t *testing.T) {
if len(got) != 3 || got[0] != "grok-4.5" || got[1] != buildVideoModel || got[2] != "grok-code-fast-1" {
t.Fatalf("super catalog = %#v", got)
}
// Free: keep/add 1.5 so free Build accounts can use video generation.
// Free: remove 1.5 even when the catalog exposes it.
got = adapter.NormalizeAccountModelCapabilities([]string{"grok-4.5", buildVideoModel}, &account.Billing{Used: 1, PlanName: "free"}, build)
if len(got) != 2 || got[0] != "grok-4.5" || got[1] != buildVideoModel {
if len(got) != 1 || got[0] != "grok-4.5" {
t.Fatalf("free catalog = %#v", got)
}
// Unknown (no Billing): still keep 1.5 for Build accounts.
// Unknown (no Billing): treat as Free and remove 1.5.
got = adapter.NormalizeAccountModelCapabilities([]string{buildVideoModel, "grok-4.5"}, nil, build)
if len(got) != 2 || got[0] != buildVideoModel || got[1] != "grok-4.5" {
if len(got) != 1 || got[0] != "grok-4.5" {
t.Fatalf("unknown catalog = %#v", got)
}
// Free catalog without 1.5: inject it.
got = adapter.NormalizeAccountModelCapabilities([]string{"grok-4.5"}, &account.Billing{PlanName: "free"}, build)
if len(got) != 2 || got[0] != "grok-4.5" || got[1] != buildVideoModel {
t.Fatalf("free inject catalog = %#v", got)
}
// Do not depend on BuildAPIFallback; an empty catalog with Billing Super adds only 1.5.
got = adapter.NormalizeAccountModelCapabilities(nil, &account.Billing{PlanName: "SuperGrok", CreditUsagePercent: 1}, build)
if len(got) != 1 || got[0] != buildVideoModel {
@@ -615,11 +610,11 @@ func TestNormalizeAccountModelCapabilitiesAddsComposerOnlyForBuildOAuth(t *testi
adapter := &Adapter{}
oauth := account.Credential{Provider: account.ProviderBuild, AuthType: account.AuthTypeOAuth}
got := adapter.NormalizeAccountModelCapabilities([]string{"grok-4.5"}, &account.Billing{PlanName: "free"}, oauth)
if len(got) != 3 || got[0] != "grok-4.5" || got[1] != buildVideoModel || got[2] != modeldomain.GrokComposer25Fast {
if len(got) != 2 || got[0] != "grok-4.5" || got[1] != modeldomain.GrokComposer25Fast {
t.Fatalf("OAuth Free capabilities = %#v", got)
}
got = adapter.NormalizeAccountModelCapabilities([]string{"grok-4.5", modeldomain.GrokComposer25Fast, modeldomain.GrokComposer25Fast}, nil, oauth)
if len(got) != 3 || got[0] != "grok-4.5" || got[1] != modeldomain.GrokComposer25Fast || got[2] != buildVideoModel {
if len(got) != 2 || got[0] != "grok-4.5" || got[1] != modeldomain.GrokComposer25Fast {
t.Fatalf("Composer capability was not deduplicated: %#v", got)
}
for _, credential := range []account.Credential{
@@ -627,15 +622,8 @@ func TestNormalizeAccountModelCapabilitiesAddsComposerOnlyForBuildOAuth(t *testi
{Provider: account.ProviderWeb, AuthType: account.AuthTypeOAuth},
} {
got = adapter.NormalizeAccountModelCapabilities([]string{"grok-4.5"}, nil, credential)
switch credential.Provider {
case account.ProviderBuild:
if len(got) != 2 || got[0] != "grok-4.5" || got[1] != buildVideoModel {
t.Fatalf("Build SSO capabilities = %#v", got)
}
default:
if len(got) != 1 || got[0] != "grok-4.5" {
t.Fatalf("Composer leaked outside Build OAuth for %#v: %#v", credential, got)
}
if len(got) != 1 || got[0] != "grok-4.5" {
t.Fatalf("Composer leaked outside Build OAuth for %#v: %#v", credential, got)
}
}
}
@@ -38,6 +38,8 @@ var mediaCatalog = []struct {
{PublicID: "grok-imagine-image-quality", UpstreamModel: "grok-imagine-image-quality", Capabilities: []modeldomain.Capability{modeldomain.CapabilityImage, modeldomain.CapabilityImageEdit}},
{PublicID: "grok-imagine-image", UpstreamModel: "grok-imagine-image", Capabilities: []modeldomain.Capability{modeldomain.CapabilityImage, modeldomain.CapabilityImageEdit}},
{PublicID: "grok-imagine-video", UpstreamModel: "grok-imagine-video", Capabilities: []modeldomain.Capability{modeldomain.CapabilityVideo}},
// Free Console accounts can use the 1.5 video model on the official Console media API.
{PublicID: "grok-imagine-video-1.5", UpstreamModel: "grok-imagine-video-1.5", Capabilities: []modeldomain.Capability{modeldomain.CapabilityVideo}},
}
// Effort-suffixed aliases only include levels each Provider/model combination
@@ -52,6 +52,7 @@ func TestCatalogContainsAllConsoleModelsAndAliases(t *testing.T) {
{publicID: "Console/grok-imagine-image", capability: modeldomain.CapabilityImage}: "grok-imagine-image",
{publicID: "Console/grok-imagine-image", capability: modeldomain.CapabilityImageEdit}: "grok-imagine-image",
{publicID: "Console/grok-imagine-video", capability: modeldomain.CapabilityVideo}: "grok-imagine-video",
{publicID: "Console/grok-imagine-video-1.5", capability: modeldomain.CapabilityVideo}: "grok-imagine-video-1.5",
}
routes := Routes()
if len(routes) != len(expected) {
@@ -91,7 +92,7 @@ func TestCatalogContainsAllConsoleModelsAndAliases(t *testing.T) {
adapter := NewAdapter(Config{}, nil, nil, nil)
for model, want := range map[string]string{
"grok-4.5": QuotaMode, "grok-imagine-image-quality": QuotaModeImage,
"grok-imagine-image": QuotaModeImage, "grok-imagine-video": QuotaModeVideo,
"grok-imagine-image": QuotaModeImage, "grok-imagine-video": QuotaModeVideo, "grok-imagine-video-1.5": QuotaModeVideo,
} {
if got := adapter.QuotaMode(model); got != want {
t.Fatalf("QuotaMode(%q) = %q, want %q", model, got, want)
@@ -1331,6 +1332,42 @@ func TestConsoleVideoRejectsTooManyReferenceImages(t *testing.T) {
}
}
func TestConsoleVideoUsesImagineVideo15Model(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
if serveTestDPoPToken(t, writer, request) {
return
}
verifyTestDPoPProof(t, request)
writer.Header().Set("Content-Type", "application/json")
switch {
case request.Method == http.MethodPost && request.URL.Path == "/v1/videos/generations":
var payload map[string]any
if err := json.NewDecoder(request.Body).Decode(&payload); err != nil {
t.Error(err)
}
if payload["model"] != "grok-imagine-video-1.5" {
t.Errorf("video model = %#v", payload["model"])
}
_, _ = writer.Write([]byte(`{"request_id":"upstream-video-15"}`))
case request.Method == http.MethodGet && request.URL.Path == "/v1/videos/upstream-video-15":
_, _ = writer.Write([]byte(`{"status":"done","progress":100,"video":{"url":"https://vidgen.x.ai/result-15.mp4"}}`))
default:
http.NotFound(writer, request)
}
}))
t.Cleanup(server.Close)
adapter, credential := newConsoleTestAdapter(t, server.URL)
result, err := adapter.GenerateVideo(context.Background(), provider.VideoRequest{
Credential: credential, Model: "grok-imagine-video-1.5", Prompt: "animate", Duration: 6, AspectRatio: "16:9", Resolution: "720p",
})
if err != nil {
t.Fatal(err)
}
if result.URL != "https://vidgen.x.ai/result-15.mp4" {
t.Fatalf("video result = %#v", result)
}
}
func TestParseConsoleVideoStatusRejectsUnknownState(t *testing.T) {
if _, _, err := parseConsoleVideoStatus([]byte(`{"status":"mystery"}`), nil); err == nil || !strings.Contains(err.Error(), "状态无效") {
t.Fatalf("unknown status error = %v", err)
@@ -396,20 +396,24 @@ func trustedConsoleImageHost(host string) bool {
}
func (a *Adapter) GenerateVideo(ctx context.Context, request provider.VideoRequest) (provider.VideoResult, error) {
if !ResolveMedia("grok-imagine-video", modeldomain.CapabilityVideo) {
return provider.VideoResult{}, errors.New("Console 视频模型未注册")
modelName := strings.TrimSpace(request.Model)
if modelName == "" {
modelName = "grok-imagine-video"
}
if !ResolveMedia(modelName, modeldomain.CapabilityVideo) {
return provider.VideoResult{}, fmt.Errorf("Console 视频模型未注册: %s", modelName)
}
if len(request.ReferenceURLs) > consoleMaxVideoImages {
return provider.VideoResult{}, fmt.Errorf("Console grok-imagine-video 最多支持 %d 张参考图,当前为 %d 张", consoleMaxVideoImages, len(request.ReferenceURLs))
return provider.VideoResult{}, fmt.Errorf("Console %s 最多支持 %d 张参考图,当前为 %d 张", modelName, consoleMaxVideoImages, len(request.ReferenceURLs))
}
if request.Duration < 1 || request.Duration > 15 {
return provider.VideoResult{}, errors.New("duration 必须在 1 到 15 秒之间")
}
if request.Resolution != "" && request.Resolution != "480p" && request.Resolution != "720p" {
return provider.VideoResult{}, errors.New("grok-imagine-video 仅支持 480p 或 720p")
return provider.VideoResult{}, fmt.Errorf("%s 仅支持 480p 或 720p", modelName)
}
payload := map[string]any{
"model": "grok-imagine-video", "duration": request.Duration,
"model": modelName, "duration": request.Duration,
}
if prompt := strings.TrimSpace(request.Prompt); prompt != "" {
payload["prompt"] = prompt
+3 -1
View File
@@ -278,7 +278,9 @@ type VideoRequest struct {
// Billing is used only to determine XAI eligibility in Build auto mode; nil means the account tier is unknown.
Billing *account.Billing
// JobID binds the local video job to XAI ZDR upload tickets and result assets.
JobID string
JobID string
// Model is the selected upstream video model when the Provider supports more than one.
Model string
Prompt string
Duration int
AspectRatio string