Skip to content

Commit f6e0c2d

Browse files
authored
refactor(ai): ignore video sound on models that can't switch it (anomalyco#63726)
1 parent ba77ff9 commit f6e0c2d

6 files changed

Lines changed: 35 additions & 25 deletions

File tree

‎packages/ai/README.md‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1087,7 +1087,9 @@ const program = Effect.gen(function* () {
10871087

10881088
The shared fields are `prompt`, `start`, `end`, `references`, `duration`, `aspect`, `resolution` and `sound`;
10891089
everything else is provider-specific and goes under `providerOptions`, typed from the selected model. A request only
1090-
fails before sending when its protocol has no way to express it, such as a reference video on xAI. Values the provider
1090+
fails before sending when its protocol has no way to express it, such as a reference video on xAI. `sound` is the
1091+
exception: on models whose sound can't be switched (MiniMax H3 and Gemini Omni always have it, Hailuo never does) it
1092+
isn't sent, and the model's own behavior applies. Values the provider
10911093
may reject, like an unsupported duration or too many references, go to the provider, and its error comes back typed.
10921094

10931095
`start` and `end` are images. They take a URL, data URL, base64 string or bytes, or an asset built with
@@ -1131,19 +1133,19 @@ Provider notes:
11311133
`/v1`, and a bare host (`https://api.minimax.io`) works as is. On H3, `start`, `end` and `references` become `content[]` items with `first_frame`, `last_frame` and `reference_image` /
11321134
`reference_video` / `reference_audio` roles, MiniMax file refs become `mm_file://` URLs, `aspect: "auto"` becomes
11331135
`ratio: "adaptive"`, and `resolution` is uppercased (`480P`, `2K`). H3 requires `resolution` and `duration`, plus a
1134-
`ratio` for text-only prompts; nothing is filled in, so MiniMax rejects a request without them. H3 always has sound,
1135-
so `sound: false` fails before sending. On Hailuo, `start` and `end` map to `first_frame_image` and
1136+
`ratio` for text-only prompts; nothing is filled in, so MiniMax rejects a request without them. H3 always has sound
1137+
and has no field for it, so `sound` isn't sent. On Hailuo, `start` and `end` map to `first_frame_image` and
11361138
`last_frame_image`, image references to `subject_reference`, and the result looks up the file's download URL, which
1137-
expires after one hour. Hailuo is silent and has no aspect field, so `sound: true`, an explicit `aspect`, and video
1138-
or audio references fail before sending. Cancel works on H3 (only while queued); Hailuo has no cancel. Native fields
1139+
expires after one hour. Hailuo is always silent, so `sound` isn't sent; it has no aspect field, so an explicit
1140+
`aspect` and video or audio references fail before sending. Cancel works on H3 (only while queued); Hailuo has no cancel. Native fields
11391141
such as `extra.prompt_expansion_mode` (H3), `prompt_optimizer` (Hailuo) and `callback_url` go under `providerOptions`.
11401142
- **Google** (`Google.video("gemini-omni-1.2-flash")`) starts a background Gemini Omni interaction. Omni has no
11411143
role field for media, so `start`, `end` and `references` go into `input` in that order (images, plus video
11421144
references), and `generation_config.video_config.task` is set to `image_to_video` for frames only or
11431145
`reference_to_video` for references only; with both, the prompt decides. `duration` becomes
11441146
`response_format.duration` (`"3s"`), and `aspect` and `resolution` go to `response_format` as is (resolution
11451147
lowercased); `aspect: "auto"` leaves the ratio out. It rejects `end` without `start` (a lone image reads as the
1146-
first frame) and `sound: false` (Omni always generates audio). Videos come back as inline bytes. `cancel` posts to
1148+
first frame). Omni always generates audio and has no field for it, so `sound` isn't sent. Videos come back as inline bytes. `cancel` posts to
11471149
`/interactions/{id}/cancel`. `providerOptions` takes native Interactions fields, such as `previous_interaction_id`
11481150
to edit or extend a generated video, `generation_config.video_config.task`, `service_tier` and `webhook_config`.
11491151

‎packages/ai/src/protocols/google-interactions-video.ts‎

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -129,11 +129,6 @@ const task = (request: Request) => {
129129
const fromRequest = Effect.fn("GoogleInteractionsVideo.fromRequest")(function* (request: Request) {
130130
if (request.end !== undefined && request.start === undefined)
131131
return yield* route.unsupported("video.end", `${route.name} reads a lone image as the start frame; send start too`)
132-
if (request.sound === false)
133-
return yield* route.unsupported(
134-
"video.sound",
135-
`${route.name} always generates audio and has no option to turn it off`,
136-
)
137132
const media = yield* Effect.forEach(
138133
[request.start, request.end, ...(request.references ?? [])].filter(
139134
(asset): asset is Media.Asset => asset !== undefined,

‎packages/ai/src/protocols/minimax-video.ts‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -177,9 +177,7 @@ const reference = (asset: Media.Asset) =>
177177
const resolution = (request: Request) => request.resolution?.toUpperCase()
178178

179179
const fromRequest = Effect.fn("MiniMaxVideo.fromRequest")(function* (request: Request) {
180-
// H3 always generates a soundtrack and has no field to turn it off.
181-
if (request.sound === false)
182-
return yield* route.unsupported("video.sound", `${route.name} always generates sound and can't be silenced`)
180+
// H3 always generates a soundtrack and has no field for `sound`, so it isn't sent.
183181
const content = [
184182
{ type: "text", text: request.prompt },
185183
...(request.start === undefined ? [] : [yield* item(request.start, "first_frame")]),
@@ -213,8 +211,7 @@ const hailuoFromRequest = Effect.fn("MiniMaxHailuoVideo.fromRequest")(function*
213211
`${hailuoRoute.name} takes only image (subject) references`,
214212
)
215213
// Hailuo output is always silent and its shape follows the model or the first frame, with no field for either.
216-
if (request.sound === true)
217-
return yield* hailuoRoute.unsupported("video.sound", `${hailuoRoute.name} generates silent video only`)
214+
// `sound` has nothing to send, so it isn't; an aspect ratio would change the shape the caller asked for.
218215
if (request.aspect !== undefined && request.aspect !== "auto")
219216
return yield* hailuoRoute.unsupported("video.aspect", `${hailuoRoute.name} has no aspect ratio input`)
220217
const subjects = yield* Effect.forEach(references, (asset) =>

‎packages/ai/test/generation.test.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,9 @@ describe("Generation", () => {
9797
const generation = new Generation(route, "t", { id: "gen_1", status: "queued" })
9898

9999
const fiber = yield* Effect.forkChild(
100-
generation.events({ poll: { interval: "1 second", timeout: "5 seconds" } }).pipe(Stream.runCollect, Effect.flip),
100+
generation
101+
.events({ poll: { interval: "1 second", timeout: "5 seconds" } })
102+
.pipe(Stream.runCollect, Effect.flip),
101103
)
102104
yield* TestClock.adjust("6 seconds")
103105
const error = yield* Fiber.join(fiber)

‎packages/ai/test/google-interactions-video.test.ts‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,9 @@ describe("ai.video.start / Google Interactions", () => {
101101
model,
102102
prompt: "Morph",
103103
start: Media.image(Media.fromDataUrl("data:image/png;base64,AQID")),
104-
end: Media.image(Media.ref("google", "https://generativelanguage.googleapis.com/v1beta/files/last", "image/jpeg")),
104+
end: Media.image(
105+
Media.ref("google", "https://generativelanguage.googleapis.com/v1beta/files/last", "image/jpeg"),
106+
),
105107
})
106108
expect(body.input).toEqual([
107109
{ type: "image", data: "AQID", mime_type: "image/png" },
@@ -147,9 +149,15 @@ describe("ai.video.start / Google Interactions", () => {
147149
}),
148150
)
149151

152+
it.effect("sends nothing for sound, since Omni always generates audio and has no field for it", () =>
153+
Effect.gen(function* () {
154+
const silent = yield* sent({ model, prompt: "x", sound: false })
155+
expect(JSON.stringify(silent.body)).not.toContain("audio")
156+
}),
157+
)
158+
150159
for (const rejected of [
151160
{ name: "an end frame without a start frame", input: { end: "data:image/png;base64,AQID" }, operation: "end" },
152-
{ name: "silent video", input: { sound: false }, operation: "sound" },
153161
])
154162
it.effect(`rejects ${rejected.name}, which Omni has no way to send, before sending`, () =>
155163
Effect.gen(function* () {

‎packages/ai/test/minimax-video.test.ts‎

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -136,12 +136,19 @@ describe("MiniMax Video / H3 start", () => {
136136
}),
137137
)
138138

139-
it.effect("rejects silent output, which H3 can't produce, before sending", () =>
139+
it.effect("sends nothing for sound, which neither API has a field for", () =>
140140
Effect.gen(function* () {
141-
const error = yield* ai.video.start({ model: h3, prompt: "x", sound: false }).pipe(Effect.flip)
142-
expect(error.reason._tag).toBe("UnsupportedOperation")
143-
expect(error.message).toContain("always generates sound")
144-
}).pipe(Effect.provide(layer(() => Effect.die("an unsupported request reached the network")))),
141+
const requests = yield* sent([
142+
{ model: h3, sound: false },
143+
{ model: h3, sound: true },
144+
{ model: hailuo, sound: true },
145+
])
146+
expect(requests.map((request) => "generate_audio" in request.body || "sound" in request.body)).toEqual([
147+
false,
148+
false,
149+
false,
150+
])
151+
}),
145152
)
146153

147154
it.effect("returns MiniMax's HTTP errors typed", () =>
@@ -242,7 +249,6 @@ describe("MiniMax Video / Hailuo start", () => {
242249
request: { references: [Media.url("https://example.test/v.mp3", { mediaType: "audio/mpeg" })] },
243250
tag: "UnsupportedOperation",
244251
},
245-
{ name: "sound", request: { sound: true }, tag: "UnsupportedOperation" },
246252
{ name: "an aspect ratio", request: { aspect: "16:9" as const }, tag: "UnsupportedOperation" },
247253
{ name: "a MiniMax file reference", request: { start: Media.ref("minimax", "file_1") }, tag: "InvalidRequest" },
248254
]) {

0 commit comments

Comments
 (0)