@@ -24,8 +24,11 @@ import {
2424} from "openclaw/plugin-sdk/memory-core-host-engine-embeddings" ;
2525import {
2626 assertOkOrThrowProviderError ,
27+ createProviderOperationDeadline ,
2728 readProviderJsonResponse ,
2829 readProviderTextResponse ,
30+ resolveProviderOperationTimeoutMs ,
31+ waitProviderOperationPollInterval ,
2932} from "openclaw/plugin-sdk/provider-http" ;
3033import { normalizeStringEntries } from "openclaw/plugin-sdk/string-coerce-runtime" ;
3134import type { OpenAiEmbeddingClient } from "./embedding-provider.js" ;
@@ -90,11 +93,13 @@ async function submitOpenAiBatch(params: {
9093async function fetchOpenAiBatchStatus ( params : {
9194 openAi : OpenAiEmbeddingClient ;
9295 batchId : string ;
96+ signal ?: AbortSignal ;
9397} ) : Promise < OpenAiBatchStatus > {
9498 return await fetchOpenAiBatchResource ( {
9599 openAi : params . openAi ,
96100 path : `/batches/${ params . batchId } ` ,
97101 label : "openai.batch-status" ,
102+ signal : params . signal ,
98103 parse : async ( res ) => readProviderJsonResponse < OpenAiBatchStatus > ( res , "openai.batch-status" ) ,
99104 } ) ;
100105}
@@ -134,13 +139,15 @@ async function fetchOpenAiBatchResource<T>(params: {
134139 openAi : OpenAiEmbeddingClient ;
135140 path : string ;
136141 label : string ;
142+ signal ?: AbortSignal ;
137143 parse : ( res : Response ) => Promise < T > ;
138144} ) : Promise < T > {
139145 const baseUrl = normalizeBatchBaseUrl ( params . openAi ) ;
140146 return await withRemoteHttpResponse ( {
141147 url : `${ baseUrl } ${ params . path } ` ,
142148 ssrfPolicy : params . openAi . ssrfPolicy ,
143149 fetchImpl : params . openAi . fetchImpl ,
150+ signal : params . signal ,
144151 init : {
145152 headers : buildBatchHeaders ( params . openAi , { json : true } ) ,
146153 } ,
@@ -256,34 +263,52 @@ async function waitForOpenAiBatch(params: {
256263 debug ?: ( message : string , data ?: Record < string , unknown > ) => void ;
257264 initial ?: OpenAiBatchStatus ;
258265} ) : Promise < BatchCompletionResult > {
259- const start = Date . now ( ) ;
266+ const deadline = createProviderOperationDeadline ( {
267+ label : `openai batch ${ params . batchId } ` ,
268+ timeoutMs : params . timeoutMs ,
269+ } ) ;
260270 const pollBackoff = createOpenAiBatchPollBackoff ( params ) ;
261271 let current : OpenAiBatchStatus | undefined = params . initial ;
262272 while ( true ) {
263273 let status : OpenAiBatchStatus ;
274+ let statusSignal : AbortSignal | undefined ;
264275 try {
265- status =
266- current ??
267- ( await fetchOpenAiBatchStatus ( {
276+ if ( current ) {
277+ status = current ;
278+ } else {
279+ statusSignal = AbortSignal . timeout (
280+ resolveProviderOperationTimeoutMs ( {
281+ deadline,
282+ defaultTimeoutMs : params . timeoutMs ,
283+ } ) ,
284+ ) ;
285+ status = await fetchOpenAiBatchStatus ( {
268286 openAi : params . openAi ,
269287 batchId : params . batchId ,
270- } ) ) ;
271- } catch ( error ) {
272- if ( ! params . wait || ! isRetryableOpenAiBatchPollError ( error ) ) {
273- throw error ;
288+ signal : statusSignal ,
289+ } ) ;
274290 }
275- if ( Date . now ( ) - start > params . timeoutMs ) {
291+ } catch ( error ) {
292+ if ( statusSignal ?. aborted ) {
276293 throw new Error ( `openai batch ${ params . batchId } timed out after ${ params . timeoutMs } ms` , {
277294 cause : error ,
278295 } ) ;
279296 }
297+ if ( ! params . wait || ! isRetryableOpenAiBatchPollError ( error ) ) {
298+ throw error ;
299+ }
280300 const delayMs = pollBackoff . nextDelayMs ( ) ;
281301 params . debug ?.(
282- `openai batch ${ params . batchId } status check failed: ${ formatOpenAiBatchDiagnostic ( error ) } ; waiting ${ delayMs } ms` ,
302+ `openai batch ${ params . batchId } status check failed: ${ formatOpenAiBatchDiagnostic ( error ) } ; waiting up to ${ delayMs } ms` ,
283303 ) ;
284- await new Promise ( ( resolve ) => {
285- setTimeout ( resolve , delayMs ) ;
286- } ) ;
304+ try {
305+ await waitProviderOperationPollInterval ( { deadline, pollIntervalMs : delayMs } ) ;
306+ resolveProviderOperationTimeoutMs ( { deadline, defaultTimeoutMs : params . timeoutMs } ) ;
307+ } catch {
308+ throw new Error ( `openai batch ${ params . batchId } timed out after ${ params . timeoutMs } ms` , {
309+ cause : error ,
310+ } ) ;
311+ }
287312 current = undefined ;
288313 continue ;
289314 }
@@ -316,18 +341,14 @@ async function waitForOpenAiBatch(params: {
316341 if ( ! params . wait ) {
317342 throw new Error ( `openai batch ${ params . batchId } still ${ state } ; wait disabled` ) ;
318343 }
319- if ( Date . now ( ) - start > params . timeoutMs ) {
320- throw new Error ( `openai batch ${ params . batchId } timed out after ${ params . timeoutMs } ms` ) ;
321- }
322344 const delayMs = pollBackoff . nextDelayMs ( ) ;
323345 params . debug ?.(
324346 `openai batch ${ params . batchId } ${ state } ${ formatOpenAiBatchProgress (
325347 status ,
326- ) } ; waiting ${ delayMs } ms`,
348+ ) } ; waiting up to ${ delayMs } ms`,
327349 ) ;
328- await new Promise ( ( resolve ) => {
329- setTimeout ( resolve , delayMs ) ;
330- } ) ;
350+ await waitProviderOperationPollInterval ( { deadline, pollIntervalMs : delayMs } ) ;
351+ resolveProviderOperationTimeoutMs ( { deadline, defaultTimeoutMs : params . timeoutMs } ) ;
331352 current = undefined ;
332353 }
333354}
0 commit comments