@@ -6,8 +6,10 @@ import {
66 assertOkOrThrowHttpError ,
77 createProviderOperationDeadline ,
88 createProviderOperationTimeoutResolver ,
9+ executeProviderOperationWithRetry ,
910 fetchProviderDownloadResponse ,
1011 fetchProviderOperationResponse ,
12+ fetchWithTimeoutGuarded ,
1113 postJsonRequest ,
1214 readProviderJsonResponse ,
1315 resolveProviderOperationTimeoutMs ,
@@ -39,6 +41,11 @@ const MAX_POLL_ATTEMPTS = 120;
3941const MAX_DURATION_SECONDS = 10 ;
4042const DEFAULT_GENERATED_VIDEO_MAX_BYTES = 16 * 1024 * 1024 ;
4143
44+ type RunwayVideoRequestPolicy = {
45+ allowPrivateNetwork : boolean ;
46+ dispatcherPolicy ?: Parameters < typeof postJsonRequest > [ 0 ] [ "dispatcherPolicy" ] ;
47+ } ;
48+
4249type RunwayTaskStatus = "PENDING" | "RUNNING" | "THROTTLED" | "SUCCEEDED" | "FAILED" | "CANCELLED" ;
4350
4451type RunwayTaskCreateResponse = {
@@ -269,82 +276,180 @@ function buildCreateBody(req: VideoGenerationRequest): Record<string, unknown> {
269276 } ;
270277}
271278
272- async function pollRunwayTask ( params : {
273- taskId : string ;
274- headers : Headers ;
275- timeoutMs ?: number ;
276- baseUrl : string ;
277- fetchFn : typeof fetch ;
278- } ) : Promise < RunwayTaskDetailResponse > {
279+ async function pollRunwayTask (
280+ params : {
281+ taskId : string ;
282+ headers : Headers ;
283+ timeoutMs ?: number ;
284+ baseUrl : string ;
285+ fetchFn : typeof fetch ;
286+ } & RunwayVideoRequestPolicy ,
287+ ) : Promise < RunwayTaskDetailResponse > {
279288 const deadline = createProviderOperationDeadline ( {
280289 timeoutMs : params . timeoutMs ,
281290 label : `Runway video generation task ${ params . taskId } ` ,
282291 } ) ;
292+ const timeoutMs = createProviderOperationTimeoutResolver ( {
293+ deadline,
294+ defaultTimeoutMs : DEFAULT_TIMEOUT_MS ,
295+ } ) ;
296+ const guardedOptions =
297+ ! params . allowPrivateNetwork && ! params . dispatcherPolicy
298+ ? undefined
299+ : {
300+ ...( params . allowPrivateNetwork ? { ssrfPolicy : { allowPrivateNetwork : true } } : { } ) ,
301+ ...( params . dispatcherPolicy ? { dispatcherPolicy : params . dispatcherPolicy } : { } ) ,
302+ auditContext : "runway-video-status" ,
303+ } ;
304+ const taskUrl = `${ params . baseUrl } /v1/tasks/${ params . taskId } ` ;
305+ const taskInit = {
306+ method : "GET" ,
307+ headers : params . headers ,
308+ } ;
283309 for ( let attempt = 0 ; attempt < MAX_POLL_ATTEMPTS ; attempt += 1 ) {
284- const response = await fetchProviderOperationResponse ( {
285- stage : "poll" ,
286- url : `${ params . baseUrl } /v1/tasks/${ params . taskId } ` ,
287- init : {
288- method : "GET" ,
289- headers : params . headers ,
290- } ,
291- timeoutMs : createProviderOperationTimeoutResolver ( {
292- deadline,
293- defaultTimeoutMs : DEFAULT_TIMEOUT_MS ,
294- } ) ,
310+ const { response, release } = guardedOptions
311+ ? await executeProviderOperationWithRetry ( {
312+ provider : "runway" ,
313+ stage : "poll" ,
314+ operation : async ( ) => {
315+ const result = await fetchWithTimeoutGuarded (
316+ taskUrl ,
317+ taskInit ,
318+ typeof timeoutMs === "function" ? timeoutMs ( ) : timeoutMs ,
319+ params . fetchFn ,
320+ guardedOptions ,
321+ ) ;
322+ try {
323+ await assertOkOrThrowHttpError ( result . response , "Runway video status request failed" ) ;
324+ return result ;
325+ } catch ( error ) {
326+ await result . release ( ) ;
327+ throw error ;
328+ }
329+ } ,
330+ } )
331+ : {
332+ response : await fetchProviderOperationResponse ( {
333+ stage : "poll" ,
334+ url : taskUrl ,
335+ init : taskInit ,
336+ timeoutMs,
337+ fetchFn : params . fetchFn ,
338+ provider : "runway" ,
339+ requestFailedMessage : "Runway video status request failed" ,
340+ } ) ,
341+ release : async ( ) => { } ,
342+ } ;
343+ try {
344+ const payload = await readRunwayJsonResponse < RunwayTaskDetailResponse > (
345+ response ,
346+ "Runway video status request failed" ,
347+ ) ;
348+ const status = readRunwayTaskStatus ( payload ) ;
349+ switch ( status ) {
350+ case "SUCCEEDED" :
351+ return payload ;
352+ case "FAILED" :
353+ case "CANCELLED" :
354+ throw new Error (
355+ readRunwayFailureMessage ( payload . failure ) ||
356+ `Runway video generation ${ normalizeLowercaseStringOrEmpty ( status ) } ` ,
357+ ) ;
358+ default :
359+ await waitProviderOperationPollInterval ( { deadline, pollIntervalMs : POLL_INTERVAL_MS } ) ;
360+ break ;
361+ }
362+ } finally {
363+ await release ( ) ;
364+ }
365+ }
366+ throw new Error ( `Runway video generation task ${ params . taskId } did not finish in time` ) ;
367+ }
368+
369+ async function fetchRunwayDownloadResponse (
370+ params : {
371+ url : string ;
372+ init : RequestInit ;
373+ timeoutMs ?: ProviderOperationTimeoutMs ;
374+ fetchFn : typeof fetch ;
375+ } & RunwayVideoRequestPolicy ,
376+ ) {
377+ if ( ! params . allowPrivateNetwork && ! params . dispatcherPolicy ) {
378+ const response = await fetchProviderDownloadResponse ( {
379+ url : params . url ,
380+ init : params . init ,
381+ timeoutMs : params . timeoutMs ?? DEFAULT_TIMEOUT_MS ,
295382 fetchFn : params . fetchFn ,
296383 provider : "runway" ,
297- requestFailedMessage : "Runway video status request failed" ,
384+ requestFailedMessage : "Runway generated video download failed" ,
298385 } ) ;
299- const payload = await readRunwayJsonResponse < RunwayTaskDetailResponse > (
386+ return {
300387 response,
301- "Runway video status request failed" ,
302- ) ;
303- const status = readRunwayTaskStatus ( payload ) ;
304- switch ( status ) {
305- case "SUCCEEDED" :
306- return payload ;
307- case "FAILED" :
308- case "CANCELLED" :
309- throw new Error (
310- readRunwayFailureMessage ( payload . failure ) ||
311- `Runway video generation ${ normalizeLowercaseStringOrEmpty ( status ) } ` ,
312- ) ;
313- default :
314- await waitProviderOperationPollInterval ( { deadline, pollIntervalMs : POLL_INTERVAL_MS } ) ;
315- break ;
316- }
388+ release : async ( ) => { } ,
389+ } ;
317390 }
318- throw new Error ( `Runway video generation task ${ params . taskId } did not finish in time` ) ;
391+
392+ return await executeProviderOperationWithRetry ( {
393+ provider : "runway" ,
394+ stage : "download" ,
395+ operation : async ( ) => {
396+ const result = await fetchWithTimeoutGuarded (
397+ params . url ,
398+ params . init ,
399+ typeof params . timeoutMs === "function"
400+ ? params . timeoutMs ( )
401+ : ( params . timeoutMs ?? DEFAULT_TIMEOUT_MS ) ,
402+ params . fetchFn ,
403+ {
404+ ...( params . allowPrivateNetwork ? { ssrfPolicy : { allowPrivateNetwork : true } } : { } ) ,
405+ ...( params . dispatcherPolicy ? { dispatcherPolicy : params . dispatcherPolicy } : { } ) ,
406+ auditContext : "runway-video-download" ,
407+ } ,
408+ ) ;
409+ try {
410+ await assertOkOrThrowHttpError ( result . response , "Runway generated video download failed" ) ;
411+ return result ;
412+ } catch ( error ) {
413+ await result . release ( ) ;
414+ throw error ;
415+ }
416+ } ,
417+ } ) ;
319418}
320419
321- async function downloadRunwayVideos ( params : {
322- urls : string [ ] ;
323- timeoutMs ?: ProviderOperationTimeoutMs ;
324- fetchFn : typeof fetch ;
325- maxBytes : number ;
326- } ) : Promise < GeneratedVideoAsset [ ] > {
420+ async function downloadRunwayVideos (
421+ params : {
422+ urls : string [ ] ;
423+ timeoutMs ?: ProviderOperationTimeoutMs ;
424+ fetchFn : typeof fetch ;
425+ maxBytes : number ;
426+ } & RunwayVideoRequestPolicy ,
427+ ) : Promise < GeneratedVideoAsset [ ] > {
327428 const videos : GeneratedVideoAsset [ ] = [ ] ;
328429 for ( const [ index , url ] of params . urls . entries ( ) ) {
329- const response = await fetchProviderDownloadResponse ( {
430+ const { response, release } = await fetchRunwayDownloadResponse ( {
330431 url,
331432 init : { method : "GET" } ,
332- timeoutMs : params . timeoutMs ?? DEFAULT_TIMEOUT_MS ,
433+ timeoutMs : params . timeoutMs ,
333434 fetchFn : params . fetchFn ,
334- provider : "runway" ,
335- requestFailedMessage : "Runway generated video download failed" ,
336- } ) ;
337- const mimeType = normalizeOptionalString ( response . headers . get ( "content-type" ) ) ?? "video/mp4" ;
338- const buffer = await readResponseWithLimit ( response , params . maxBytes , {
339- onOverflow : ( { maxBytes } ) =>
340- new Error ( `Runway generated video download exceeds ${ maxBytes } bytes` ) ,
341- } ) ;
342- videos . push ( {
343- buffer,
344- mimeType,
345- fileName : `video-${ index + 1 } .${ extensionForMime ( mimeType ) ?. slice ( 1 ) ?? "mp4" } ` ,
346- metadata : { sourceUrl : url } ,
435+ allowPrivateNetwork : params . allowPrivateNetwork ,
436+ dispatcherPolicy : params . dispatcherPolicy ,
347437 } ) ;
438+ try {
439+ const mimeType = normalizeOptionalString ( response . headers . get ( "content-type" ) ) ?? "video/mp4" ;
440+ const buffer = await readResponseWithLimit ( response , params . maxBytes , {
441+ onOverflow : ( { maxBytes } ) =>
442+ new Error ( `Runway generated video download exceeds ${ maxBytes } bytes` ) ,
443+ } ) ;
444+ videos . push ( {
445+ buffer,
446+ mimeType,
447+ fileName : `video-${ index + 1 } .${ extensionForMime ( mimeType ) ?. slice ( 1 ) ?? "mp4" } ` ,
448+ metadata : { sourceUrl : url } ,
449+ } ) ;
450+ } finally {
451+ await release ( ) ;
452+ }
348453 }
349454 return videos ;
350455}
@@ -447,6 +552,8 @@ export function buildRunwayVideoGenerationProvider(): VideoGenerationProvider {
447552 } ) ,
448553 baseUrl,
449554 fetchFn,
555+ allowPrivateNetwork,
556+ dispatcherPolicy,
450557 } ) ;
451558 const outputUrls = readRunwayOutputUrls ( completed ) ;
452559 const videos = await downloadRunwayVideos ( {
@@ -457,6 +564,8 @@ export function buildRunwayVideoGenerationProvider(): VideoGenerationProvider {
457564 } ) ,
458565 fetchFn,
459566 maxBytes : resolveGeneratedVideoMaxBytes ( req ) ,
567+ allowPrivateNetwork,
568+ dispatcherPolicy,
460569 } ) ;
461570 return {
462571 videos,
0 commit comments