From 3905dcbd5d60f9ea93ab0d1b8c6a6618b0f59a96 Mon Sep 17 00:00:00 2001 From: Daniel Riccio Date: Fri, 4 Jul 2025 17:32:42 -0500 Subject: [PATCH] fix: address review comments - extract shared quota detection utility and fix translation typos --- src/i18n/locales/vi/embeddings.json | 2 +- src/i18n/locales/zh-CN/embeddings.json | 2 +- src/i18n/locales/zh-TW/embeddings.json | 4 +-- .../code-index/embedders/openai-compatible.ts | 30 ++----------------- src/services/code-index/embedders/openai.ts | 30 ++----------------- .../embedders/utils/quota-detection.ts | 30 +++++++++++++++++++ 6 files changed, 38 insertions(+), 60 deletions(-) create mode 100644 src/services/code-index/embedders/utils/quota-detection.ts diff --git a/src/i18n/locales/vi/embeddings.json b/src/i18n/locales/vi/embeddings.json index c10a44e6c9..15fe3b418b 100644 --- a/src/i18n/locales/vi/embeddings.json +++ b/src/i18n/locales/vi/embeddings.json @@ -3,7 +3,7 @@ "authenticationFailed": "Không thể tạo nhúng: Xác thực không thành công. Vui lòng kiểm tra khóa API của bạn.", "failedWithStatus": "Không thể tạo nhúng sau {{attempts}} lần thử: HTTP {{statusCode}} - {{errorMessage}}", "failedWithError": "Không thể tạo nhúng sau {{attempts}} lần thử: {{errorMessage}}", - "insufficientQuota": "Không tạo được embedding: Không đủ hạn ngạch. Vui lòng kiểm tra số dư tài khoản OpenAI của bạn và nạp thêm tín dụng để tiếp tục.", + "insufficientQuota": "Không tạo được nhúng: Không đủ hạn ngạch. Vui lòng kiểm tra số dư tài khoản OpenAI của bạn và nạp thêm tín dụng để tiếp tục.", "failedMaxAttempts": "Không thể tạo nhúng sau {{attempts}} lần thử", "textExceedsTokenLimit": "Văn bản tại chỉ mục {{index}} vượt quá giới hạn mã thông báo tối đa ({{itemTokens}} > {{maxTokens}}). Bỏ qua.", "textWithPrefixExceedsTokenLimit": "Văn bản tại chỉ mục {{index}} có tiền tố vượt quá giới hạn mã thông báo tối đa ({{estimatedTokens}} > {{maxTokens}}). Không thêm tiền tố.", diff --git a/src/i18n/locales/zh-CN/embeddings.json b/src/i18n/locales/zh-CN/embeddings.json index 5db7512419..b682ff3411 100644 --- a/src/i18n/locales/zh-CN/embeddings.json +++ b/src/i18n/locales/zh-CN/embeddings.json @@ -3,7 +3,7 @@ "authenticationFailed": "创建嵌入失败:身份验证失败。请检查您的 API 密钥。", "failedWithStatus": "尝试 {{attempts}} 次后创建嵌入失败:HTTP {{statusCode}} - {{errorMessage}}", "failedWithError": "尝试 {{attempts}} 次后创建嵌入失败:{{errorMessage}}", - "insufficientQuota": "创建嵌入失败:配额不足。请检查您的 OpenAI 帐户余额並添加积分以继续。", + "insufficientQuota": "创建嵌入失败:配额不足。请检查您的 OpenAI 帐户余额并添加积分以继续。", "failedMaxAttempts": "尝试 {{attempts}} 次后创建嵌入失败", "textExceedsTokenLimit": "索引 {{index}} 处的文本超过最大令牌限制 ({{itemTokens}} > {{maxTokens}})。正在跳过。", "textWithPrefixExceedsTokenLimit": "索引 {{index}} 处带前缀的文本超过了最大令牌限制 ({{estimatedTokens}} > {{maxTokens}})。不添加前缀。", diff --git a/src/i18n/locales/zh-TW/embeddings.json b/src/i18n/locales/zh-TW/embeddings.json index a31b61848a..464f58b5c0 100644 --- a/src/i18n/locales/zh-TW/embeddings.json +++ b/src/i18n/locales/zh-TW/embeddings.json @@ -3,10 +3,10 @@ "authenticationFailed": "建立內嵌失敗:驗證失敗。請檢查您的 API 金鑰。", "failedWithStatus": "嘗試 {{attempts}} 次後建立內嵌失敗:HTTP {{statusCode}} - {{errorMessage}}", "failedWithError": "嘗試 {{attempts}} 次後建立內嵌失敗:{{errorMessage}}", - "insufficientQuota": "建立嵌入失敗:額度不足。請檢查您的 OpenAI 帳戶餘額並新增點數以繼續。", + "insufficientQuota": "建立內嵌失敗:額度不足。請檢查您的 OpenAI 帳戶餘額並新增點數以繼續。", "failedMaxAttempts": "嘗試 {{attempts}} 次後建立內嵌失敗", "textExceedsTokenLimit": "索引 {{index}} 處的文字超過最大權杖限制 ({{itemTokens}} > {{maxTokens}})。正在略過。", - "textWithPrefixExceedsTokenLimit": "索引 {{index}} 處帶前綴的文本超過了最大令牌限制 ({{estimatedTokens}} > {{maxTokens}})。不添加前綴。", + "textWithPrefixExceedsTokenLimit": "索引 {{index}} 處帶前綴的文本超過了最大權杖限制 ({{estimatedTokens}} > {{maxTokens}})。不添加前綴。", "rateLimitRetry": "已達到速率限制,將在 {{delayMs}} 毫秒後重試(嘗試次數 {{attempt}}/{{maxRetries}})", "ollama": { "couldNotReadErrorBody": "無法讀取錯誤內容", diff --git a/src/services/code-index/embedders/openai-compatible.ts b/src/services/code-index/embedders/openai-compatible.ts index 68afcacaf1..622a83a805 100644 --- a/src/services/code-index/embedders/openai-compatible.ts +++ b/src/services/code-index/embedders/openai-compatible.ts @@ -8,6 +8,7 @@ import { } from "../constants" import { getDefaultModelId, getModelQueryPrefix } from "../../../shared/embeddingModels" import { t } from "../../../i18n" +import { isInsufficientQuotaError } from "./utils/quota-detection" interface EmbeddingItem { embedding: string | number[] @@ -277,7 +278,7 @@ export class OpenAICompatibleEmbedder implements IEmbedder { const hasMoreAttempts = attempts < MAX_RETRIES - 1 // Add quota detection - const isQuotaError = this.isInsufficientQuotaError(error) + const isQuotaError = isInsufficientQuotaError(error) if (isRateLimitError && !isQuotaError && hasMoreAttempts) { const delayMs = INITIAL_DELAY_MS * Math.pow(2, attempts) @@ -337,31 +338,4 @@ export class OpenAICompatibleEmbedder implements IEmbedder { name: "openai-compatible", } } - - /** - * Detects if an error is due to insufficient quota/credits - * @param error The error object to check - * @returns True if the error indicates insufficient quota - */ - private isInsufficientQuotaError(error: any): boolean { - if (error?.status !== 429) return false - - const errorMessage = - error?.message?.toLowerCase() || - error?.response?.data?.error?.message?.toLowerCase() || - error?.error?.message?.toLowerCase() || - "" - - const quotaKeywords = [ - "insufficient_quota", - "insufficient quota", - "quota exceeded", - "insufficient funds", - "billing", - "payment required", - "credits", - ] - - return quotaKeywords.some((keyword) => errorMessage.includes(keyword)) - } } diff --git a/src/services/code-index/embedders/openai.ts b/src/services/code-index/embedders/openai.ts index 79010ca0b1..f2e14be6bf 100644 --- a/src/services/code-index/embedders/openai.ts +++ b/src/services/code-index/embedders/openai.ts @@ -8,6 +8,7 @@ import { MAX_BATCH_RETRIES as MAX_RETRIES, INITIAL_RETRY_DELAY_MS as INITIAL_DELAY_MS, } from "../constants" +import { isInsufficientQuotaError } from "./utils/quota-detection" import { getModelQueryPrefix } from "../../../shared/embeddingModels" import { t } from "../../../i18n" @@ -142,7 +143,7 @@ export class OpenAiEmbedder extends OpenAiNativeHandler implements IEmbedder { const hasMoreAttempts = attempts < MAX_RETRIES - 1 // Add quota detection - const isQuotaError = this.isInsufficientQuotaError(error) + const isQuotaError = isInsufficientQuotaError(error) if (isRateLimitError && !isQuotaError && hasMoreAttempts) { const delayMs = INITIAL_DELAY_MS * Math.pow(2, attempts) @@ -199,31 +200,4 @@ export class OpenAiEmbedder extends OpenAiNativeHandler implements IEmbedder { name: "openai", } } - - /** - * Detects if an error is due to insufficient quota/credits - * @param error The error object to check - * @returns True if the error indicates insufficient quota - */ - private isInsufficientQuotaError(error: any): boolean { - if (error?.status !== 429) return false - - const errorMessage = - error?.message?.toLowerCase() || - error?.response?.data?.error?.message?.toLowerCase() || - error?.error?.message?.toLowerCase() || - "" - - const quotaKeywords = [ - "insufficient_quota", - "insufficient quota", - "quota exceeded", - "insufficient funds", - "billing", - "payment required", - "credits", - ] - - return quotaKeywords.some((keyword) => errorMessage.includes(keyword)) - } } diff --git a/src/services/code-index/embedders/utils/quota-detection.ts b/src/services/code-index/embedders/utils/quota-detection.ts new file mode 100644 index 0000000000..28dc3aa0cb --- /dev/null +++ b/src/services/code-index/embedders/utils/quota-detection.ts @@ -0,0 +1,30 @@ +/** + * Utility functions for detecting quota-related errors from OpenAI API + */ + +/** + * Detects if an error is due to insufficient quota/credits + * @param error The error object to check + * @returns True if the error indicates insufficient quota + */ +export function isInsufficientQuotaError(error: any): boolean { + if (error?.status !== 429) return false + + const errorMessage = + error?.message?.toLowerCase() || + error?.response?.data?.error?.message?.toLowerCase() || + error?.error?.message?.toLowerCase() || + "" + + const quotaKeywords = [ + "insufficient_quota", + "insufficient quota", + "quota exceeded", + "insufficient funds", + "billing", + "payment required", + "credits", + ] + + return quotaKeywords.some((keyword) => errorMessage.includes(keyword)) +}