Skip to content

Commit 84a4347

Browse files
committed
Increase API request retry attempts to five and expand retry logic to handle stream abortion errors
1 parent f53c0be commit 84a4347

1 file changed

Lines changed: 56 additions & 21 deletions

File tree

‎packages/vscode/src/utils/make-api-request.ts‎

Lines changed: 56 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,13 @@ const process_stream_chunk = async (
5959
}
6060
}
6161

62+
class StreamAbortError extends Error {
63+
constructor(message: string) {
64+
super(message)
65+
this.name = 'StreamAbortError'
66+
}
67+
}
68+
6269
export const make_api_request = async (params: {
6370
endpoint_url: string
6471
api_key?: string
@@ -118,8 +125,8 @@ export const make_api_request = async (params: {
118125
params.on_thinking_chunk(chunk)
119126
}
120127

121-
const MAX_RETRIES = 3
122-
const RETRY_DELAYS = [2000, 5000, 10000]
128+
const MAX_RETRIES = 5
129+
const RETRY_DELAYS = [1000, 2000, 3000, 4000, 5000]
123130

124131
for (let attempt = 0; attempt <= MAX_RETRIES; attempt++) {
125132
try {
@@ -261,6 +268,8 @@ export const make_api_request = async (params: {
261268
response.data.setEncoding('utf8')
262269

263270
return new Promise((resolve, reject) => {
271+
let stream_closed = false
272+
264273
response.data.on('data', async (chunk: string) => {
265274
const { updated_buffer, new_content } = await process_stream_chunk(
266275
chunk,
@@ -282,6 +291,8 @@ export const make_api_request = async (params: {
282291
})
283292

284293
response.data.on('end', () => {
294+
stream_closed = true
295+
285296
if (buffer.trim()) {
286297
try {
287298
const trimmed_line = buffer.trim()
@@ -346,29 +357,53 @@ export const make_api_request = async (params: {
346357
})
347358

348359
response.data.on('error', (error: Error) => {
349-
Logger.error({
350-
function_name: 'make_api_request',
351-
message: 'Stream error',
352-
data: error
353-
})
354-
reject(error)
360+
if (!stream_closed) {
361+
Logger.error({
362+
function_name: 'make_api_request',
363+
message: 'Stream error',
364+
data: error
365+
})
366+
// Convert stream errors to a retryable error type
367+
reject(new StreamAbortError(error.message))
368+
}
369+
})
370+
371+
// Handle abort events specifically
372+
response.data.on('aborted', () => {
373+
if (!stream_closed) {
374+
Logger.warn({
375+
function_name: 'make_api_request',
376+
message: 'Stream aborted',
377+
})
378+
reject(new StreamAbortError('Stream was aborted'))
379+
}
355380
})
356381
})
357382
} catch (error) {
358-
if (
359-
axios.isAxiosError(error) &&
360-
(error.response?.status == 503 ||
361-
error.code == 'ECONNRESET' ||
362-
error.code == 'ECONNABORTED') &&
363-
attempt < MAX_RETRIES
364-
) {
383+
// Check if it's a retryable error
384+
const is_retryable_error =
385+
(axios.isAxiosError(error) &&
386+
(error.response?.status == 503 ||
387+
error.code == 'ECONNRESET' ||
388+
error.code == 'ECONNABORTED')) ||
389+
error instanceof StreamAbortError
390+
391+
if (is_retryable_error && attempt < MAX_RETRIES) {
365392
const delay = RETRY_DELAYS[attempt]
366-
const reason =
367-
error.response?.status == 503
368-
? 'status 503'
369-
: error.code == 'ECONNABORTED'
370-
? 'a timeout'
371-
: 'a socket hang up (ECONNRESET)'
393+
let reason = 'an unknown error'
394+
395+
if (axios.isAxiosError(error)) {
396+
if (error.response?.status == 503) {
397+
reason = 'status 503'
398+
} else if (error.code == 'ECONNABORTED') {
399+
reason = 'a timeout'
400+
} else if (error.code == 'ECONNRESET') {
401+
reason = 'a socket hang up (ECONNRESET)'
402+
}
403+
} else if (error instanceof StreamAbortError) {
404+
reason = 'stream abortion'
405+
}
406+
372407
Logger.warn({
373408
function_name: 'make_api_request',
374409
message: `API request failed with ${reason}. Retrying in ${

0 commit comments

Comments
 (0)