@@ -77,12 +77,14 @@ function resolveVoyageBatchDeps(overrides: Partial<VoyageBatchDeps> | undefined)
7777function buildVoyageBatchRequest < T > ( params : {
7878 client : VoyageEmbeddingClient ;
7979 path : string ;
80+ signal ?: AbortSignal ;
8081 onResponse : ( res : Response ) => Promise < T > ;
8182} ) {
8283 const baseUrl = normalizeBatchBaseUrl ( params . client ) ;
8384 return {
8485 url : `${ baseUrl } /${ params . path } ` ,
8586 ssrfPolicy : params . client . ssrfPolicy ,
87+ signal : params . signal ,
8688 init : {
8789 headers : buildBatchHeaders ( params . client , { json : true } ) ,
8890 } ,
@@ -130,12 +132,14 @@ async function fetchVoyageBatchStatus(params: {
130132 batchId : string ;
131133 deps : VoyageBatchDeps ;
132134 maxResponseBytes ?: number ;
135+ signal ?: AbortSignal ;
133136} ) : Promise < VoyageBatchStatus > {
134137 const maxBytes = params . maxResponseBytes ?? VOYAGE_BATCH_RESPONSE_MAX_BYTES ;
135138 return await params . deps . withRemoteHttpResponse (
136139 buildVoyageBatchRequest ( {
137140 client : params . client ,
138141 path : `batches/${ params . batchId } ` ,
142+ signal : params . signal ,
139143 onResponse : async ( res ) => {
140144 await assertOkOrThrowProviderError ( res , "voyage.batch-status" ) ;
141145 return await readProviderJsonResponse < VoyageBatchStatus > ( res , "voyage-batch-status" , {
@@ -191,15 +195,34 @@ async function waitForVoyageBatch(params: {
191195 deps : VoyageBatchDeps ;
192196} ) : Promise < BatchCompletionResult > {
193197 const start = params . deps . now ( ) ;
198+ const deadline = start + params . timeoutMs ;
194199 let current : VoyageBatchStatus | undefined = params . initial ;
195200 while ( true ) {
196- const status =
197- current ??
198- ( await fetchVoyageBatchStatus ( {
199- client : params . client ,
200- batchId : params . batchId ,
201- deps : params . deps ,
202- } ) ) ;
201+ let status : VoyageBatchStatus ;
202+ if ( current ) {
203+ status = current ;
204+ } else {
205+ const remainingRequestMs = deadline - params . deps . now ( ) ;
206+ if ( remainingRequestMs <= 0 ) {
207+ throw new Error ( `voyage batch ${ params . batchId } timed out after ${ params . timeoutMs } ms` ) ;
208+ }
209+ const signal = AbortSignal . timeout ( Math . max ( 1 , remainingRequestMs ) ) ;
210+ try {
211+ status = await fetchVoyageBatchStatus ( {
212+ client : params . client ,
213+ batchId : params . batchId ,
214+ deps : params . deps ,
215+ signal,
216+ } ) ;
217+ } catch ( error ) {
218+ if ( signal . aborted ) {
219+ throw new Error ( `voyage batch ${ params . batchId } timed out after ${ params . timeoutMs } ms` , {
220+ cause : error ,
221+ } ) ;
222+ }
223+ throw error ;
224+ }
225+ }
203226 const state = status . status ?? "unknown" ;
204227 await throwIfBatchCompletionError ( {
205228 provider : "voyage" ,
@@ -231,11 +254,13 @@ async function waitForVoyageBatch(params: {
231254 if ( ! params . wait ) {
232255 throw new Error ( `voyage batch ${ params . batchId } still ${ state } ; wait disabled` ) ;
233256 }
234- if ( params . deps . now ( ) - start > params . timeoutMs ) {
257+ const remainingMs = deadline - params . deps . now ( ) ;
258+ if ( remainingMs <= 0 ) {
235259 throw new Error ( `voyage batch ${ params . batchId } timed out after ${ params . timeoutMs } ms` ) ;
236260 }
237- params . debug ?.( `voyage batch ${ params . batchId } ${ state } ; waiting ${ params . pollIntervalMs } ms` ) ;
238- await params . deps . sleep ( params . pollIntervalMs ) ;
261+ const waitMs = Math . min ( params . pollIntervalMs , remainingMs ) ;
262+ params . debug ?.( `voyage batch ${ params . batchId } ${ state } ; waiting ${ waitMs } ms` ) ;
263+ await params . deps . sleep ( waitMs ) ;
239264 current = undefined ;
240265 }
241266}
0 commit comments