Skip to content

Commit 036298f

Browse files
committed
fix(music): bound generated track downloads
1 parent 3eca409 commit 036298f

5 files changed

Lines changed: 232 additions & 14 deletions

File tree

‎extensions/fal/music-generation-provider.test.ts‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,18 @@ function postRequest(): Record<string, unknown> {
4545
return request as Record<string, unknown>;
4646
}
4747

48+
function streamedAudioResponse(bytes: string): Response {
49+
return new Response(
50+
new ReadableStream({
51+
start(controller) {
52+
controller.enqueue(new TextEncoder().encode(bytes));
53+
controller.close();
54+
},
55+
}),
56+
{ headers: { "content-type": "audio/mpeg" } },
57+
);
58+
}
59+
4860
describe("fal music generation provider", () => {
4961
afterEach(() => {
5062
assertOkOrThrowHttpErrorMock.mockClear();
@@ -111,6 +123,33 @@ describe("fal music generation provider", () => {
111123
expect(result.metadata?.audioUrl).toBe("https://v3b.fal.media/files/b/kangaroo/out.mp3");
112124
});
113125

126+
it("rejects generated music downloads that exceed the configured media cap", async () => {
127+
postJsonRequestMock.mockResolvedValue({
128+
response: {
129+
json: async () => ({
130+
audio: {
131+
url: "https://v3b.fal.media/files/b/out.mp3",
132+
content_type: "audio/mpeg",
133+
},
134+
}),
135+
},
136+
release: vi.fn(async () => {}),
137+
});
138+
vi.stubGlobal(
139+
"fetch",
140+
vi.fn(async () => streamedAudioResponse("too-large")),
141+
);
142+
143+
await expect(
144+
buildFalMusicGenerationProvider().generateMusic({
145+
provider: "fal",
146+
model: "fal-ai/minimax-music/v2.6",
147+
prompt: "short track",
148+
cfg: { agents: { defaults: { mediaMaxMb: 0.000001 } } },
149+
}),
150+
).rejects.toThrow("fal generated music download exceeds 1 bytes");
151+
});
152+
114153
it("rejects MiniMax lyrics requests that also ask for instrumental output", async () => {
115154
await expect(
116155
buildFalMusicGenerationProvider().generateMusic({

‎extensions/fal/music-generation-provider.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const DEFAULT_FAL_MUSIC_MODEL = "fal-ai/minimax-music/v2.6";
1313
const FAL_ACE_STEP_MODEL = "fal-ai/ace-step/prompt-to-audio";
1414
const FAL_STABLE_AUDIO_MODEL = "fal-ai/stable-audio-25/text-to-audio";
1515
const DEFAULT_TIMEOUT_MS = 180_000;
16+
const DEFAULT_GENERATED_MUSIC_MAX_BYTES = 16 * 1024 * 1024;
1617

1718
const FAL_MUSIC_MODELS = [
1819
DEFAULT_FAL_MUSIC_MODEL,
@@ -24,6 +25,14 @@ function resolveFalMusicModel(model: string | undefined): string {
2425
return normalizeOptionalString(model) ?? DEFAULT_FAL_MUSIC_MODEL;
2526
}
2627

28+
function resolveGeneratedMusicMaxBytes(req: MusicGenerationRequest): number {
29+
const configured = req.cfg.agents?.defaults?.mediaMaxMb;
30+
if (typeof configured === "number" && Number.isFinite(configured) && configured > 0) {
31+
return Math.floor(configured * 1024 * 1024);
32+
}
33+
return DEFAULT_GENERATED_MUSIC_MAX_BYTES;
34+
}
35+
2736
function buildFalMinimaxBody(req: MusicGenerationRequest): Record<string, unknown> {
2837
const lyrics = normalizeOptionalString(req.lyrics);
2938
if (lyrics && req.instrumental === true) {
@@ -162,6 +171,7 @@ export function buildFalMusicGenerationProvider(): MusicGenerationProvider {
162171
fetchFn: fetch,
163172
provider: "fal",
164173
requestFailedMessage: "fal generated music download failed",
174+
maxBytes: resolveGeneratedMusicMaxBytes(req),
165175
});
166176
const lyrics =
167177
typeof payload === "object" && payload && !Array.isArray(payload)

‎extensions/minimax/music-generation-provider.test.ts‎

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,18 @@ function mockCallArg(mock: { mock: { calls: unknown[][] } }, index = 0): Record<
4949
return call[0] as Record<string, unknown>;
5050
}
5151

52+
function streamedAudioResponse(bytes: string): Response {
53+
return new Response(
54+
new ReadableStream({
55+
start(controller) {
56+
controller.enqueue(new TextEncoder().encode(bytes));
57+
controller.close();
58+
},
59+
}),
60+
{ headers: { "content-type": "audio/mpeg" } },
61+
);
62+
}
63+
5264
describe("minimax music generation provider", () => {
5365
it("declares explicit mode capabilities", () => {
5466
expectExplicitMusicGenerationCapabilities(buildMinimaxMusicGenerationProvider());
@@ -161,6 +173,58 @@ describe("minimax music generation provider", () => {
161173
expect(result.tracks[0]?.buffer).toEqual(terminalAudio);
162174
});
163175

176+
it("rejects streamed generated music that exceeds the configured media cap", async () => {
177+
postJsonRequestMock.mockResolvedValue({
178+
response: new Response(
179+
`data: ${JSON.stringify({
180+
data: { status: 2, audio: Buffer.from("too-large").toString("hex") },
181+
base_resp: { status_code: 0 },
182+
})}`,
183+
{
184+
headers: { "content-type": "text/event-stream" },
185+
},
186+
),
187+
release: vi.fn(async () => {}),
188+
});
189+
190+
const provider = buildMinimaxMusicGenerationProvider();
191+
await expect(
192+
provider.generateMusic({
193+
provider: "minimax",
194+
model: "music-2.6",
195+
prompt: "short track",
196+
cfg: { agents: { defaults: { mediaMaxMb: 0.000001 } } },
197+
}),
198+
).rejects.toThrow("MiniMax generated music download exceeds 1 bytes");
199+
});
200+
201+
it("rejects inline generated music that exceeds the configured media cap before decoding", async () => {
202+
postJsonRequestMock.mockResolvedValue({
203+
response: new Response(
204+
JSON.stringify({
205+
data: {
206+
audio: Buffer.from("too-large").toString("hex"),
207+
},
208+
base_resp: { status_code: 0 },
209+
}),
210+
{
211+
headers: { "content-type": "application/json" },
212+
},
213+
),
214+
release: vi.fn(async () => {}),
215+
});
216+
217+
const provider = buildMinimaxMusicGenerationProvider();
218+
await expect(
219+
provider.generateMusic({
220+
provider: "minimax",
221+
model: "music-2.6",
222+
prompt: "short track",
223+
cfg: { agents: { defaults: { mediaMaxMb: 0.000001 } } },
224+
}),
225+
).rejects.toThrow("MiniMax generated music download exceeds 1 bytes");
226+
});
227+
164228
it("downloads tracks when url output is returned in data.audio", async () => {
165229
mockMusicGenerationResponse({
166230
task_id: "task-url",
@@ -192,6 +256,32 @@ describe("minimax music generation provider", () => {
192256
expect(result.metadata?.audioUrl).toBe("https://example.com/url-audio.mp3");
193257
});
194258

259+
it("rejects generated music downloads that exceed the configured media cap", async () => {
260+
postJsonRequestMock.mockResolvedValue({
261+
response: new Response(
262+
JSON.stringify({
263+
data: {
264+
audio: "https://example.com/too-large.mp3",
265+
},
266+
base_resp: { status_code: 0 },
267+
}),
268+
{ headers: { "content-type": "application/json" } },
269+
),
270+
release: vi.fn(async () => {}),
271+
});
272+
fetchWithTimeoutMock.mockResolvedValueOnce(streamedAudioResponse("too-large"));
273+
274+
const provider = buildMinimaxMusicGenerationProvider();
275+
await expect(
276+
provider.generateMusic({
277+
provider: "minimax",
278+
model: "music-2.6",
279+
prompt: "short track",
280+
cfg: { agents: { defaults: { mediaMaxMb: 0.000001 } } },
281+
}),
282+
).rejects.toThrow("MiniMax generated music download exceeds 1 bytes");
283+
});
284+
195285
it("honors explicit long caller timeouts for request and download fallbacks", async () => {
196286
mockMusicGenerationResponse({
197287
data: {

0 commit comments

Comments
 (0)