MCPcopy
hub / github.com/simstudioai/sim / executeWithRetry

Method executeWithRetry

packages/ts-sdk/src/index.ts:402–447  ·  view source on GitHub ↗

* Execute workflow with automatic retry on rate limit * @param workflowId - The ID of the workflow to execute * @param input - Input data to pass to the workflow * @param options - Execution options (timeout, stream, async, etc.) * @param retryOptions - Retry configuration (maxRetries, d

(
    workflowId: string,
    input?: any,
    options: ExecutionOptions = {},
    retryOptions: RetryOptions = {}
  )

Source from the content-addressed store, hash-verified

400 * @param retryOptions - Retry configuration (maxRetries, delays, etc.)
401 */
402 async executeWithRetry(
403 workflowId: string,
404 input?: any,
405 options: ExecutionOptions = {},
406 retryOptions: RetryOptions = {}
407 ): Promise<WorkflowExecutionResult | AsyncExecutionResult> {
408 const {
409 maxRetries = 3,
410 initialDelay = 1000,
411 maxDelay = 30000,
412 backoffMultiplier = 2,
413 } = retryOptions
414
415 let lastError: SimStudioError | null = null
416 let delay = initialDelay
417
418 for (let attempt = 0; attempt <= maxRetries; attempt++) {
419 try {
420 return await this.executeWorkflow(workflowId, input, options)
421 } catch (error: any) {
422 if (!(error instanceof SimStudioError) || error.code !== 'RATE_LIMIT_EXCEEDED') {
423 throw error
424 }
425
426 lastError = error
427
428 if (attempt === maxRetries) {
429 break
430 }
431
432 const waitTime =
433 error.status === 429 && this.rateLimitInfo?.retryAfter
434 ? this.rateLimitInfo.retryAfter
435 : Math.min(delay, maxDelay)
436
437 // standalone package — cannot depend on @sim/utils
438 const jitter = waitTime * (0.75 + Math.random() * 0.5)
439
440 await new Promise((resolve) => setTimeout(resolve, jitter))
441
442 delay *= backoffMultiplier
443 }
444 }
445
446 throw lastError || new SimStudioError('Max retries exceeded', 'MAX_RETRIES_EXCEEDED')
447 }
448
449 /**
450 * Get current rate limit information

Callers 1

index.test.tsFile · 0.80

Calls 1

executeWorkflowMethod · 0.95

Tested by

no test coverage detected