- Updated the test documentation in `doc/test.md` to clarify the purpose and structure of regression and unit tests, including changes to execution summaries and test file organization. - Modified ESLint configuration in `eslint.config.mjs` to exclude specific directories and adjust rules for `.mjs` files. - Removed unnecessary ESLint disable comments from various script files to enforce cleaner code practices. - Introduced a new test case for image analysis in `tests/case/ai/tool/utility/image-analyze-contract.test.ts` to validate the behavior of the image recognition tool. - Enhanced existing unit tests in `tests/case/unit/webCrawler/web-search-sidecar-retry.test.ts` and `tests/case/unit/workbench/assistant-markdown-memo.test.ts` for better reliability and clarity.
25 lines
773 B
JavaScript
25 lines
773 B
JavaScript
|
|
const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms))
|
|
|
|
export const fetchWithRetry = async (url, options = {}, retryOptions = {}) => {
|
|
const attempts = Number(retryOptions.attempts || 5)
|
|
const delayMs = Number(retryOptions.delayMs || 2_000)
|
|
let lastError
|
|
let lastResponse
|
|
|
|
for (let attempt = 1; attempt <= attempts; attempt += 1) {
|
|
try {
|
|
const response = await fetch(url, options)
|
|
lastResponse = response
|
|
if (response.ok || response.status < 500 || attempt === attempts) return response
|
|
} catch (error) {
|
|
lastError = error
|
|
if (attempt === attempts) throw error
|
|
}
|
|
await delay(delayMs * attempt)
|
|
}
|
|
|
|
if (lastResponse) return lastResponse
|
|
throw lastError || new Error(`request failed: ${url}`)
|
|
}
|