Skip to content

Commit 4f87028

Browse files
committed
fix: remove trailing comma and handle empty API keys error
1 parent b4eabd8 commit 4f87028

File tree

3 files changed

+23
-10
lines changed

3 files changed

+23
-10
lines changed

.github/ISSUE_TEMPLATE/bug-report.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: "Bug report"
22
description: "Problems with the software"
3-
title: '[Bug] {{请输入标题,不要留空 - Please enter a title, do not leave it blank.}}'
3+
title: '[Bug] 请输入标题,不要留空 - Please enter a title, do not leave it blank.}}'
44
labels: ['bug']
55
body:
66
- type: markdown
@@ -59,9 +59,9 @@ body:
5959
attributes:
6060
label: 系统版本 | System version
6161
description: |
62-
请提供您正在使用的系统/浏览器版本
62+
请提供您正在使用的系统版本
6363
64-
Please provide the version of the System/Browser you are using. For example, `macOS 11.2.3`.
64+
Please provide the version of the System you are using. For example, `macOS 11.2.3`.
6565
validations:
6666
required: true
6767
- type: input

.github/ISSUE_TEMPLATE/feature.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
name: "Feature"
22
description: "Add new feature, improve code, and more"
3+
title: '[Feature] “请输入标题,不要留空 - Please enter a title, do not leave it blank.}}”'
34
labels: [ "enhancement" ]
45
body:
56
- type: markdown

src/main.js

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -169,11 +169,12 @@ function handleResponse(completion, isChatGPTModel, query, result) {
169169

170170
let targetText = (isChatGPTModel ? choices[0].message.content : choices[0].text).trim();
171171

172-
if (targetText.startsWith('"') || targetText.startsWith("「")) {
173-
targetText = targetText.slice(1);
174-
}
175-
if (targetText.endsWith('"') || targetText.endsWith("」")) {
176-
targetText = targetText.slice(0, -1);
172+
// 使用正则表达式删除字符串开头和结尾的特殊字符
173+
targetText = targetText.replace(/^(||"|)|(||"|)$/g, "");
174+
175+
// 判断并删除字符串末尾的 `" =>`
176+
if (targetText.endsWith('" =>')) {
177+
targetText = targetText.slice(0, -4);
177178
}
178179

179180
completion({
@@ -200,10 +201,21 @@ function translate(query, completion) {
200201
}
201202

202203
const { model, apiKeys, apiUrl, deploymentName } = $option;
203-
const modifiedApiUrl = ensureHttpsAndNoTrailingSlash(apiUrl);
204204

205-
const apiKeySelection = apiKeys.split(",").map(key => key.trim());
205+
if (!apiKeys) {
206+
completion({
207+
error: {
208+
type: "secretKey",
209+
message: "配置错误 - 请确保您在插件配置中填入了正确的 API Keys",
210+
addtion: "请在插件配置中填写 API Keys",
211+
},
212+
});
213+
}
214+
const trimmedApiKeys = apiKeys.endsWith(",") ? apiKeys.slice(0, -1) : apiKeys;
215+
const apiKeySelection = trimmedApiKeys.split(",").map(key => key.trim());
206216
const apiKey = apiKeySelection[Math.floor(Math.random() * apiKeySelection.length)];
217+
218+
const modifiedApiUrl = ensureHttpsAndNoTrailingSlash(apiUrl);
207219

208220
const isChatGPTModel = ChatGPTModels.includes(model);
209221
const isAzureServiceProvider = modifiedApiUrl.includes("openai.azure.com");

0 commit comments

Comments
 (0)