* 执行 API 调用(带重试) * @param {string} prompt * @param {number} retryCount * @returns {Promise }
(prompt, retryCount = 0)
| 447 | * @returns {Promise<Object>} |
| 448 | */ |
| 449 | async executeWithRetry(prompt, retryCount = 0) { |
| 450 | try { |
| 451 | this.logger.info(`Calling Gemini API (attempt ${retryCount + 1}/${this.config.MAX_RETRIES})`); |
| 452 | |
| 453 | const { options, data } = this.buildRequestOptions(prompt); |
| 454 | const response = await HttpClient.request(options, data); |
| 455 | const content = this.parseResponse(response); |
| 456 | |
| 457 | this.logger.info('API call successful', { contentLength: content.length }); |
| 458 | return { success: true, content }; |
| 459 | |
| 460 | } catch (error) { |
| 461 | this.logger.error(`API call failed (attempt ${retryCount + 1})`, { |
| 462 | error: error.message, |
| 463 | type: error.type || 'UNKNOWN' |
| 464 | }); |
| 465 | |
| 466 | // 判断是否需要重试 |
| 467 | if (retryCount < this.config.MAX_RETRIES - 1) { |
| 468 | const shouldRetry = this.shouldRetry(error); |
| 469 | |
| 470 | if (shouldRetry) { |
| 471 | const delay = this.config.RETRY_DELAY * Math.pow(2, retryCount); // 指数退避 |
| 472 | this.logger.info(`Retrying after ${delay}ms...`); |
| 473 | |
| 474 | await new Promise(resolve => setTimeout(resolve, delay)); |
| 475 | return this.executeWithRetry(prompt, retryCount + 1); |
| 476 | } |
| 477 | } |
| 478 | |
| 479 | // 不重试或重试次数用尽 |
| 480 | return { |
| 481 | success: false, |
| 482 | error: error.message, |
| 483 | type: error.type || ErrorTypes.NETWORK_ERROR, |
| 484 | details: error.details |
| 485 | }; |
| 486 | } |
| 487 | } |
| 488 | |
| 489 | /** |
| 490 | * 判断是否应该重试 |
no test coverage detected