|
1 | 1 | import { createLogger } from '@sim/logger' |
2 | 2 | import { type NextRequest, NextResponse } from 'next/server' |
3 | 3 | import { checkInternalAuth } from '@/lib/auth/hybrid' |
| 4 | +import { getMaxExecutionTimeout } from '@/lib/core/execution-limits' |
4 | 5 | import { downloadFileFromStorage } from '@/lib/uploads/utils/file-utils.server' |
5 | 6 | import type { UserFile } from '@/executor/types' |
6 | 7 | import type { VideoRequestBody } from '@/tools/video/types' |
@@ -326,11 +327,12 @@ async function generateWithRunway( |
326 | 327 |
|
327 | 328 | logger.info(`[${requestId}] Runway task created: ${taskId}`) |
328 | 329 |
|
329 | | - const maxAttempts = 120 // 10 minutes with 5-second intervals |
| 330 | + const pollIntervalMs = 5000 |
| 331 | + const maxAttempts = Math.ceil(getMaxExecutionTimeout() / pollIntervalMs) |
330 | 332 | let attempts = 0 |
331 | 333 |
|
332 | 334 | while (attempts < maxAttempts) { |
333 | | - await sleep(5000) // Poll every 5 seconds |
| 335 | + await sleep(pollIntervalMs) |
334 | 336 |
|
335 | 337 | const statusResponse = await fetch(`https://api.dev.runwayml.com/v1/tasks/${taskId}`, { |
336 | 338 | headers: { |
@@ -370,7 +372,7 @@ async function generateWithRunway( |
370 | 372 | attempts++ |
371 | 373 | } |
372 | 374 |
|
373 | | - throw new Error('Runway generation timed out after 10 minutes') |
| 375 | + throw new Error('Runway generation timed out') |
374 | 376 | } |
375 | 377 |
|
376 | 378 | async function generateWithVeo( |
@@ -429,11 +431,12 @@ async function generateWithVeo( |
429 | 431 |
|
430 | 432 | logger.info(`[${requestId}] Veo operation created: ${operationName}`) |
431 | 433 |
|
432 | | - const maxAttempts = 60 // 5 minutes with 5-second intervals |
| 434 | + const pollIntervalMs = 5000 |
| 435 | + const maxAttempts = Math.ceil(getMaxExecutionTimeout() / pollIntervalMs) |
433 | 436 | let attempts = 0 |
434 | 437 |
|
435 | 438 | while (attempts < maxAttempts) { |
436 | | - await sleep(5000) |
| 439 | + await sleep(pollIntervalMs) |
437 | 440 |
|
438 | 441 | const statusResponse = await fetch( |
439 | 442 | `https://generativelanguage.googleapis.com/v1beta/${operationName}`, |
@@ -485,7 +488,7 @@ async function generateWithVeo( |
485 | 488 | attempts++ |
486 | 489 | } |
487 | 490 |
|
488 | | - throw new Error('Veo generation timed out after 5 minutes') |
| 491 | + throw new Error('Veo generation timed out') |
489 | 492 | } |
490 | 493 |
|
491 | 494 | async function generateWithLuma( |
@@ -541,11 +544,12 @@ async function generateWithLuma( |
541 | 544 |
|
542 | 545 | logger.info(`[${requestId}] Luma generation created: ${generationId}`) |
543 | 546 |
|
544 | | - const maxAttempts = 120 // 10 minutes |
| 547 | + const pollIntervalMs = 5000 |
| 548 | + const maxAttempts = Math.ceil(getMaxExecutionTimeout() / pollIntervalMs) |
545 | 549 | let attempts = 0 |
546 | 550 |
|
547 | 551 | while (attempts < maxAttempts) { |
548 | | - await sleep(5000) |
| 552 | + await sleep(pollIntervalMs) |
549 | 553 |
|
550 | 554 | const statusResponse = await fetch( |
551 | 555 | `https://api.lumalabs.ai/dream-machine/v1/generations/${generationId}`, |
@@ -592,7 +596,7 @@ async function generateWithLuma( |
592 | 596 | attempts++ |
593 | 597 | } |
594 | 598 |
|
595 | | - throw new Error('Luma generation timed out after 10 minutes') |
| 599 | + throw new Error('Luma generation timed out') |
596 | 600 | } |
597 | 601 |
|
598 | 602 | async function generateWithMiniMax( |
@@ -658,14 +662,13 @@ async function generateWithMiniMax( |
658 | 662 |
|
659 | 663 | logger.info(`[${requestId}] MiniMax task created: ${taskId}`) |
660 | 664 |
|
661 | | - // Poll for completion (6-10 minutes typical) |
662 | | - const maxAttempts = 120 // 10 minutes with 5-second intervals |
| 665 | + const pollIntervalMs = 5000 |
| 666 | + const maxAttempts = Math.ceil(getMaxExecutionTimeout() / pollIntervalMs) |
663 | 667 | let attempts = 0 |
664 | 668 |
|
665 | 669 | while (attempts < maxAttempts) { |
666 | | - await sleep(5000) |
| 670 | + await sleep(pollIntervalMs) |
667 | 671 |
|
668 | | - // Query task status |
669 | 672 | const statusResponse = await fetch( |
670 | 673 | `https://api.minimax.io/v1/query/video_generation?task_id=${taskId}`, |
671 | 674 | { |
@@ -743,7 +746,7 @@ async function generateWithMiniMax( |
743 | 746 | attempts++ |
744 | 747 | } |
745 | 748 |
|
746 | | - throw new Error('MiniMax generation timed out after 10 minutes') |
| 749 | + throw new Error('MiniMax generation timed out') |
747 | 750 | } |
748 | 751 |
|
749 | 752 | // Helper function to strip subpaths from Fal.ai model IDs for status/result endpoints |
@@ -861,11 +864,12 @@ async function generateWithFalAI( |
861 | 864 | // Get base model ID (without subpath) for status and result endpoints |
862 | 865 | const baseModelId = getBaseModelId(falModelId) |
863 | 866 |
|
864 | | - const maxAttempts = 96 // 8 minutes with 5-second intervals |
| 867 | + const pollIntervalMs = 5000 |
| 868 | + const maxAttempts = Math.ceil(getMaxExecutionTimeout() / pollIntervalMs) |
865 | 869 | let attempts = 0 |
866 | 870 |
|
867 | 871 | while (attempts < maxAttempts) { |
868 | | - await sleep(5000) |
| 872 | + await sleep(pollIntervalMs) |
869 | 873 |
|
870 | 874 | const statusResponse = await fetch( |
871 | 875 | `https://queue.fal.run/${baseModelId}/requests/${requestIdFal}/status`, |
@@ -938,7 +942,7 @@ async function generateWithFalAI( |
938 | 942 | attempts++ |
939 | 943 | } |
940 | 944 |
|
941 | | - throw new Error('Fal.ai generation timed out after 8 minutes') |
| 945 | + throw new Error('Fal.ai generation timed out') |
942 | 946 | } |
943 | 947 |
|
944 | 948 | function getVideoDimensions( |
|
0 commit comments