@@ -23,22 +23,23 @@ import {
2323 * `{ version: 1, audio: { type: "asset_id", asset_id } }`. The recording is uploaded
2424 * first as 192 kb/s stereo MP3 through the same origin's asset direct-upload routes.
2525 *
26- * Response: `{ data: StemsJob }`, or a bare `StemsJob`. While `status` is `pending` or `processing`,
27- * `GET <that url>/<id>` returns the job again. A `completed` job lists one URL per stem;
28- * the names are vocals, drums, bass, guitar, piano and other.
26+ * Response: `{ data: StemsJob }` once the stems are made, which can take minutes. A failure is an HTTP error
27+ * whose body is `{ error: { code, message } }`. The same key and body resent answer the same job, and 409
28+ * `request_in_progress` while the first request still runs. A `completed` job lists one URL per stem; the
29+ * names are vocals, drums, bass, guitar, piano and other.
2930 */
3031interface StemsJob {
3132 id : string ;
32- status : "pending" | "processing" | " completed" | "failed" ;
33+ status : "pending" | "completed" | "failed" ;
3334 stems ?: { name : string ; url : string } [ ] ;
34- error ?: { message ?: string } ;
35+ failure_message ?: string ;
3536}
3637
3738const URL_ENV = "HYPERFRAMES_AUDIO_STEMS_URL" ;
3839const KEY_ENV = "HYPERFRAMES_AUDIO_API_KEY" ;
3940const TIMEOUT_ENV = "HYPERFRAMES_AUDIO_STEMS_TIMEOUT_MS" ;
4041const DEFAULT_TIMEOUT_MS = 30 * 60_000 ;
41- const POLL_MS = 5000 ;
42+ const RETRY_MS = 5000 ;
4243const DONE = "stems.json" ;
4344const STEM_NAMES = [ "vocals" , "drums" , "bass" , "guitar" , "piano" , "other" ] as const ;
4445
@@ -62,7 +63,25 @@ export function cachedStems(dir: string): Record<string, string> | null {
6263 return Object . values ( paths ) . every ( ( p ) => existsSync ( p ) ) ? paths : null ;
6364}
6465
66+ class StillRunning extends Error { }
67+
68+ const DROPPED = new Set ( [ "UND_ERR_HEADERS_TIMEOUT" , "UND_ERR_SOCKET" ] ) ;
69+
70+ const dropped = ( error : unknown ) =>
71+ error instanceof TypeError &&
72+ DROPPED . has ( ( error . cause as { code ?: string } | undefined ) ?. code ?? "" ) ;
73+
74+ async function stillRunning ( response : Response ) : Promise < boolean > {
75+ if ( response . status !== 409 ) return false ;
76+ const body = ( await response
77+ . clone ( )
78+ . json ( )
79+ . catch ( ( ) => null ) ) as { error ?: { code ?: string } } | null ;
80+ return body ?. error ?. code === "request_in_progress" ;
81+ }
82+
6583async function job ( response : Response ) : Promise < StemsJob > {
84+ if ( await stillRunning ( response ) ) throw new StillRunning ( ) ;
6685 if ( ! response . ok ) throw await serviceFailure ( response , "the stems service" ) ;
6786 const body = ( await response . json ( ) ) as { data ?: StemsJob } | StemsJob | null ;
6887 const value = ( body && "data" in body ? body . data : body ) as StemsJob | undefined ;
@@ -82,6 +101,21 @@ async function encoded(path: string, dir: string, signal: AbortSignal): Promise<
82101 return out ;
83102}
84103
104+ async function separated (
105+ url : string ,
106+ request : RequestInit ,
107+ signal : AbortSignal ,
108+ ) : Promise < StemsJob > {
109+ for ( ; ; ) {
110+ try {
111+ return await job ( await fetch ( url , { ...request , signal } ) ) ;
112+ } catch ( error ) {
113+ if ( ! ( error instanceof StillRunning || dropped ( error ) ) ) throw error ;
114+ await sleep ( RETRY_MS , undefined , { signal } ) ;
115+ }
116+ }
117+ }
118+
85119/** Separate the recording through the configured service and save each stem into `dir`. */
86120export async function fetchStems (
87121 path : string ,
@@ -106,32 +140,23 @@ export async function fetchStems(
106140 idempotencyKey : randomUUID ( ) ,
107141 signal,
108142 } ) ;
109- let current = await job (
110- await fetch ( url , {
143+ const current = await separated (
144+ url ,
145+ {
111146 method : "POST" ,
112- signal,
113147 redirect : "error" ,
114148 headers : {
115149 ...headers ,
116150 "idempotency-key" : randomUUID ( ) ,
117151 "content-type" : "application/json" ,
118152 } ,
119153 body : JSON . stringify ( { version : 1 , audio : { type : "asset_id" , asset_id : asset . asset_id } } ) ,
120- } ) ,
154+ } ,
155+ signal ,
121156 ) ;
122- while ( current . status === "pending" || current . status === "processing" ) {
123- await sleep ( POLL_MS , undefined , { signal } ) ;
124- current = await job (
125- await fetch ( `${ url . replace ( / \/ $ / , "" ) } /${ encodeURIComponent ( current . id ) } ` , {
126- redirect : "error" ,
127- headers,
128- signal,
129- } ) ,
130- ) ;
131- }
132157 if ( current . status !== "completed" )
133158 throw new Error (
134- `stem separation failed${ current . error ?. message ? `: ${ current . error . message } ` : "" } ` ,
159+ `stem separation failed${ current . failure_message ? `: ${ current . failure_message } ` : "" } ` ,
135160 ) ;
136161 const stems = current . stems ?? [ ] ;
137162 if ( ! stems . some ( ( s ) => s . name === VOICE_STEM ) )
0 commit comments