(input: ChatWithRetryInput)
| 26 | } |
| 27 | |
| 28 | export async function chatWithRetry(input: ChatWithRetryInput): Promise<LLMChatResponse> { |
| 29 | const maxAttempts = input.maxAttempts ?? DEFAULT_MAX_RETRY_ATTEMPTS; |
| 30 | |
| 31 | if (input.llm.isRetryableError === undefined || maxAttempts <= 1) { |
| 32 | const effectiveMaxAttempts = Math.max(maxAttempts, 1); |
| 33 | try { |
| 34 | return await input.llm.chat(paramsForAttempt(input, 1, effectiveMaxAttempts)); |
| 35 | } catch (error) { |
| 36 | logRequestFailure(input, error, 1, effectiveMaxAttempts); |
| 37 | throw error; |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | const delays = retryBackoffDelays(maxAttempts); |
| 42 | |
| 43 | for (let attempt = 1; ; attempt += 1) { |
| 44 | try { |
| 45 | return await input.llm.chat(paramsForAttempt(input, attempt, maxAttempts)); |
| 46 | } catch (error) { |
| 47 | if (attempt >= maxAttempts || !input.llm.isRetryableError(error)) { |
| 48 | logRequestFailure(input, error, attempt, maxAttempts); |
| 49 | throw error; |
| 50 | } |
| 51 | |
| 52 | const delayMs = delays[attempt - 1] ?? 0; |
| 53 | input.params.signal.throwIfAborted(); |
| 54 | input.dispatchEvent({ |
| 55 | type: 'step.retrying', |
| 56 | turnId: input.turnId, |
| 57 | step: input.currentStep, |
| 58 | stepUuid: input.stepUuid, |
| 59 | failedAttempt: attempt, |
| 60 | nextAttempt: attempt + 1, |
| 61 | maxAttempts, |
| 62 | delayMs, |
| 63 | ...retryErrorFields(error), |
| 64 | }); |
| 65 | await sleepForRetry(delayMs, input.params.signal); |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | function logRequestFailure( |
| 71 | input: ChatWithRetryInput, |
no test coverage detected