MCPcopy Create free account
hub / github.com/MoonshotAI/kimi-code / chatWithRetry

Function chatWithRetry

packages/agent-core/src/loop/retry.ts:28–68  ·  view source on GitHub ↗
(input: ChatWithRetryInput)

Source from the content-addressed store, hash-verified

26}
27
28export 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
70function logRequestFailure(
71 input: ChatWithRetryInput,

Callers 2

retry.test.tsFile · 0.90
executeLoopStepFunction · 0.90

Calls 8

paramsForAttemptFunction · 0.85
logRequestFailureFunction · 0.85
retryBackoffDelaysFunction · 0.85
retryErrorFieldsFunction · 0.85
sleepForRetryFunction · 0.85
throwIfAbortedMethod · 0.80
chatMethod · 0.65
isRetryableErrorMethod · 0.65

Tested by

no test coverage detected