Skip to content

Commit 3905dcb

Browse files
committed
fix: address review comments - extract shared quota detection utility and fix translation typos
1 parent f2255d9 commit 3905dcb

File tree

6 files changed

+38
-60
lines changed

6 files changed

+38
-60
lines changed

src/i18n/locales/vi/embeddings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"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.",
44
"failedWithStatus": "Không thể tạo nhúng sau {{attempts}} lần thử: HTTP {{statusCode}} - {{errorMessage}}",
55
"failedWithError": "Không thể tạo nhúng sau {{attempts}} lần thử: {{errorMessage}}",
6-
"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.",
6+
"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.",
77
"failedMaxAttempts": "Không thể tạo nhúng sau {{attempts}} lần thử",
88
"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.",
99
"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ố.",

src/i18n/locales/zh-CN/embeddings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"authenticationFailed": "创建嵌入失败:身份验证失败。请检查您的 API 密钥。",
44
"failedWithStatus": "尝试 {{attempts}} 次后创建嵌入失败:HTTP {{statusCode}} - {{errorMessage}}",
55
"failedWithError": "尝试 {{attempts}} 次后创建嵌入失败:{{errorMessage}}",
6-
"insufficientQuota": "创建嵌入失败:配额不足。请检查您的 OpenAI 帐户余额並添加积分以继续",
6+
"insufficientQuota": "创建嵌入失败:配额不足。请检查您的 OpenAI 帐户余额并添加积分以继续",
77
"failedMaxAttempts": "尝试 {{attempts}} 次后创建嵌入失败",
88
"textExceedsTokenLimit": "索引 {{index}} 处的文本超过最大令牌限制 ({{itemTokens}} > {{maxTokens}})。正在跳过。",
99
"textWithPrefixExceedsTokenLimit": "索引 {{index}} 处带前缀的文本超过了最大令牌限制 ({{estimatedTokens}} > {{maxTokens}})。不添加前缀。",

src/i18n/locales/zh-TW/embeddings.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
"authenticationFailed": "建立內嵌失敗:驗證失敗。請檢查您的 API 金鑰。",
44
"failedWithStatus": "嘗試 {{attempts}} 次後建立內嵌失敗:HTTP {{statusCode}} - {{errorMessage}}",
55
"failedWithError": "嘗試 {{attempts}} 次後建立內嵌失敗:{{errorMessage}}",
6-
"insufficientQuota": "建立嵌入失敗:額度不足。請檢查您的 OpenAI 帳戶餘額並新增點數以繼續。",
6+
"insufficientQuota": "建立內嵌失敗:額度不足。請檢查您的 OpenAI 帳戶餘額並新增點數以繼續。",
77
"failedMaxAttempts": "嘗試 {{attempts}} 次後建立內嵌失敗",
88
"textExceedsTokenLimit": "索引 {{index}} 處的文字超過最大權杖限制 ({{itemTokens}} > {{maxTokens}})。正在略過。",
9-
"textWithPrefixExceedsTokenLimit": "索引 {{index}} 處帶前綴的文本超過了最大令牌限制 ({{estimatedTokens}} > {{maxTokens}})。不添加前綴。",
9+
"textWithPrefixExceedsTokenLimit": "索引 {{index}} 處帶前綴的文本超過了最大權杖限制 ({{estimatedTokens}} > {{maxTokens}})。不添加前綴。",
1010
"rateLimitRetry": "已達到速率限制,將在 {{delayMs}} 毫秒後重試(嘗試次數 {{attempt}}/{{maxRetries}})",
1111
"ollama": {
1212
"couldNotReadErrorBody": "無法讀取錯誤內容",

src/services/code-index/embedders/openai-compatible.ts

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
} from "../constants"
99
import { getDefaultModelId, getModelQueryPrefix } from "../../../shared/embeddingModels"
1010
import { t } from "../../../i18n"
11+
import { isInsufficientQuotaError } from "./utils/quota-detection"
1112

1213
interface EmbeddingItem {
1314
embedding: string | number[]
@@ -277,7 +278,7 @@ export class OpenAICompatibleEmbedder implements IEmbedder {
277278
const hasMoreAttempts = attempts < MAX_RETRIES - 1
278279

279280
// Add quota detection
280-
const isQuotaError = this.isInsufficientQuotaError(error)
281+
const isQuotaError = isInsufficientQuotaError(error)
281282

282283
if (isRateLimitError && !isQuotaError && hasMoreAttempts) {
283284
const delayMs = INITIAL_DELAY_MS * Math.pow(2, attempts)
@@ -337,31 +338,4 @@ export class OpenAICompatibleEmbedder implements IEmbedder {
337338
name: "openai-compatible",
338339
}
339340
}
340-
341-
/**
342-
* Detects if an error is due to insufficient quota/credits
343-
* @param error The error object to check
344-
* @returns True if the error indicates insufficient quota
345-
*/
346-
private isInsufficientQuotaError(error: any): boolean {
347-
if (error?.status !== 429) return false
348-
349-
const errorMessage =
350-
error?.message?.toLowerCase() ||
351-
error?.response?.data?.error?.message?.toLowerCase() ||
352-
error?.error?.message?.toLowerCase() ||
353-
""
354-
355-
const quotaKeywords = [
356-
"insufficient_quota",
357-
"insufficient quota",
358-
"quota exceeded",
359-
"insufficient funds",
360-
"billing",
361-
"payment required",
362-
"credits",
363-
]
364-
365-
return quotaKeywords.some((keyword) => errorMessage.includes(keyword))
366-
}
367341
}

src/services/code-index/embedders/openai.ts

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
MAX_BATCH_RETRIES as MAX_RETRIES,
99
INITIAL_RETRY_DELAY_MS as INITIAL_DELAY_MS,
1010
} from "../constants"
11+
import { isInsufficientQuotaError } from "./utils/quota-detection"
1112
import { getModelQueryPrefix } from "../../../shared/embeddingModels"
1213
import { t } from "../../../i18n"
1314

@@ -142,7 +143,7 @@ export class OpenAiEmbedder extends OpenAiNativeHandler implements IEmbedder {
142143
const hasMoreAttempts = attempts < MAX_RETRIES - 1
143144

144145
// Add quota detection
145-
const isQuotaError = this.isInsufficientQuotaError(error)
146+
const isQuotaError = isInsufficientQuotaError(error)
146147

147148
if (isRateLimitError && !isQuotaError && hasMoreAttempts) {
148149
const delayMs = INITIAL_DELAY_MS * Math.pow(2, attempts)
@@ -199,31 +200,4 @@ export class OpenAiEmbedder extends OpenAiNativeHandler implements IEmbedder {
199200
name: "openai",
200201
}
201202
}
202-
203-
/**
204-
* Detects if an error is due to insufficient quota/credits
205-
* @param error The error object to check
206-
* @returns True if the error indicates insufficient quota
207-
*/
208-
private isInsufficientQuotaError(error: any): boolean {
209-
if (error?.status !== 429) return false
210-
211-
const errorMessage =
212-
error?.message?.toLowerCase() ||
213-
error?.response?.data?.error?.message?.toLowerCase() ||
214-
error?.error?.message?.toLowerCase() ||
215-
""
216-
217-
const quotaKeywords = [
218-
"insufficient_quota",
219-
"insufficient quota",
220-
"quota exceeded",
221-
"insufficient funds",
222-
"billing",
223-
"payment required",
224-
"credits",
225-
]
226-
227-
return quotaKeywords.some((keyword) => errorMessage.includes(keyword))
228-
}
229203
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* Utility functions for detecting quota-related errors from OpenAI API
3+
*/
4+
5+
/**
6+
* Detects if an error is due to insufficient quota/credits
7+
* @param error The error object to check
8+
* @returns True if the error indicates insufficient quota
9+
*/
10+
export function isInsufficientQuotaError(error: any): boolean {
11+
if (error?.status !== 429) return false
12+
13+
const errorMessage =
14+
error?.message?.toLowerCase() ||
15+
error?.response?.data?.error?.message?.toLowerCase() ||
16+
error?.error?.message?.toLowerCase() ||
17+
""
18+
19+
const quotaKeywords = [
20+
"insufficient_quota",
21+
"insufficient quota",
22+
"quota exceeded",
23+
"insufficient funds",
24+
"billing",
25+
"payment required",
26+
"credits",
27+
]
28+
29+
return quotaKeywords.some((keyword) => errorMessage.includes(keyword))
30+
}

0 commit comments

Comments
 (0)