Skip to content

Commit d5b1244

Browse files
committed
chore: introduce @bob-translate/types package to improve type definitions
1 parent 715f93a commit d5b1244

File tree

6 files changed

+152
-253
lines changed

6 files changed

+152
-253
lines changed

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,13 @@
1515
"build": "rollup --config rollup.config.ts --configPlugin typescript"
1616
},
1717
"devDependencies": {
18-
"typescript": "5.5.4",
18+
"@bob-translate/types": "1.0.2",
1919
"@rollup/plugin-terser": "0.4.4",
2020
"@rollup/plugin-typescript": "11.1.6",
2121
"rollup": "4.20.0",
2222
"rollup-plugin-copy": "3.5.0",
23-
"tslib": "2.6.3"
23+
"tslib": "2.6.3",
24+
"typescript": "5.5.4"
2425
},
2526
"engines": {
2627
"node": ">=20"

pnpm-lock.yaml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/global.d.ts

Lines changed: 0 additions & 59 deletions
This file was deleted.

src/main.ts

Lines changed: 45 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
1+
import type {
2+
HttpResponse,
3+
PluginValidate,
4+
ServiceError,
5+
TextTranslate,
6+
TextTranslateQuery
7+
} from "@bob-translate/types";
18
import { DEFAULT_PROMPT, languageMapping } from "./const";
29
import { langMap, supportLanguageList } from "./lang";
310
import type {
4-
BobHttpResponse,
5-
BobServiceError,
6-
BobTranslateQuery,
7-
BobValidateCompletion,
11+
ChatCompletion,
12+
ModelList,
813
PolishingMode,
914
} from "./types";
1015
import {
@@ -16,11 +21,9 @@ import {
1621
replacePromptKeywords
1722
} from "./utils";
1823

19-
function pluginTimeoutInterval() {
20-
return 60;
21-
}
24+
const pluginTimeoutInterval = () => 60;
2225

23-
function pluginValidate(completion: BobValidateCompletion) {
26+
const pluginValidate: PluginValidate = (completion) => {
2427
const { apiKeys, apiUrl, deploymentName } = $option;
2528
if (!apiKeys) {
2629
handleValidateError(completion, {
@@ -68,17 +71,20 @@ function pluginValidate(completion: BobValidateCompletion) {
6871
max_tokens: 5
6972
},
7073
handler: function (resp) {
71-
if (resp.data.error) {
74+
const data = resp.data as {
75+
error: string;
76+
}
77+
if (data.error) {
7278
const { statusCode } = resp.response;
7379
const reason = (statusCode >= 400 && statusCode < 500) ? "param" : "api";
7480
handleValidateError(completion, {
7581
type: reason,
76-
message: resp.data.error,
82+
message: data.error,
7783
troubleshootingLink: "https://bobtranslate.com/service/translate/azureopenai.html"
7884
});
7985
return;
8086
}
81-
if (resp.data.choices.length > 0) {
87+
if ((resp.data as ChatCompletion).choices.length > 0) {
8288
completion({
8389
result: true,
8490
})
@@ -91,17 +97,20 @@ function pluginValidate(completion: BobValidateCompletion) {
9197
url: baseUrl + apiUrlPath,
9298
header: header,
9399
handler: function (resp) {
94-
if (resp.data.error) {
100+
const data = resp.data as {
101+
error: string;
102+
}
103+
if (data.error) {
95104
const { statusCode } = resp.response;
96105
const reason = (statusCode >= 400 && statusCode < 500) ? "param" : "api";
97106
handleValidateError(completion, {
98107
type: reason,
99-
message: resp.data.error,
108+
message: data.error,
100109
troubleshootingLink: "https://bobtranslate.com/service/translate/openai.html"
101110
});
102111
return;
103112
}
104-
const modelList = resp.data
113+
const modelList = resp.data as ModelList;
105114
if (modelList.data?.length > 0) {
106115
completion({
107116
result: true,
@@ -119,20 +128,20 @@ function supportLanguages() {
119128
return supportLanguageList.map(([standardLang]) => standardLang);
120129
}
121130

122-
function isBobServiceError(error: unknown): error is BobServiceError {
131+
const isServiceError = (error: unknown): error is ServiceError => {
123132
return (
124133
typeof error === 'object' &&
125134
error !== null &&
126135
'message' in error &&
127-
typeof (error as BobServiceError).message === 'string'
136+
typeof (error as ServiceError).message === 'string'
128137
);
129138
}
130139

131-
function generateSystemPrompt(
140+
const generateSystemPrompt = (
132141
basePrompt: string | null,
133142
polishingMode: PolishingMode,
134-
query: BobTranslateQuery
135-
): string {
143+
query: TextTranslateQuery
144+
): string => {
136145
const isDetailedPolishingMode = polishingMode === "detailed";
137146

138147
const promptInfo = languageMapping[query.detectFrom] || {
@@ -148,15 +157,15 @@ function generateSystemPrompt(
148157
return systemPrompt;
149158
}
150159

151-
function buildRequestBody(
160+
const buildRequestBody = (
152161
model: string,
153-
query: BobTranslateQuery
154-
) {
162+
query: TextTranslateQuery
163+
) => {
155164
const { customSystemPrompt, customUserPrompt, polishingMode } = $option;
156165

157166
const systemPrompt = generateSystemPrompt(
158167
replacePromptKeywords(customSystemPrompt, query),
159-
polishingMode,
168+
polishingMode as PolishingMode,
160169
query
161170
);
162171

@@ -189,7 +198,11 @@ function buildRequestBody(
189198
};
190199
}
191200

192-
function handleStreamResponse(query: BobTranslateQuery, targetText: string, textFromResponse: string) {
201+
const handleStreamResponse = (
202+
query: TextTranslateQuery,
203+
targetText: string,
204+
textFromResponse: string
205+
) => {
193206
if (textFromResponse !== '[DONE]') {
194207
try {
195208
const dataObj = JSON.parse(textFromResponse);
@@ -207,7 +220,7 @@ function handleStreamResponse(query: BobTranslateQuery, targetText: string, text
207220
});
208221
}
209222
} catch (error) {
210-
if (isBobServiceError(error)) {
223+
if (isServiceError(error)) {
211224
handleGeneralError(query, {
212225
type: error.type || 'param',
213226
message: error.message || 'Failed to parse JSON',
@@ -224,8 +237,8 @@ function handleStreamResponse(query: BobTranslateQuery, targetText: string, text
224237
return targetText;
225238
}
226239

227-
function handleGeneralResponse(query: BobTranslateQuery, result: BobHttpResponse) {
228-
const { choices } = result.data;
240+
const handleGeneralResponse = (query: TextTranslateQuery, result: HttpResponse) => {
241+
const { choices } = result.data as ChatCompletion;
229242

230243
if (!choices || choices.length === 0) {
231244
handleGeneralError(query, {
@@ -236,26 +249,26 @@ function handleGeneralResponse(query: BobTranslateQuery, result: BobHttpResponse
236249
return;
237250
}
238251

239-
let targetText = choices[0].message.content.trim();
252+
let targetText = choices[0].message.content?.trim();
240253

241254
// 使用正则表达式删除字符串开头和结尾的特殊字符
242-
targetText = targetText.replace(/^(||"|)|(||"|)$/g, "");
255+
targetText = targetText?.replace(/^(||"|)|(||"|)$/g, "");
243256

244257
// 判断并删除字符串末尾的 `" =>`
245-
if (targetText.endsWith('" =>')) {
258+
if (targetText?.endsWith('" =>')) {
246259
targetText = targetText.slice(0, -4);
247260
}
248261

249262
query.onCompletion({
250263
result: {
251264
from: query.detectFrom,
252265
to: query.detectTo,
253-
toParagraphs: targetText.split("\n"),
266+
toParagraphs: targetText!.split("\n"),
254267
},
255268
});
256269
}
257270

258-
function translate(query: BobTranslateQuery) {
271+
const translate: TextTranslate = (query) => {
259272
if (!langMap.get(query.detectTo)) {
260273
handleGeneralError(query, {
261274
type: "unsupportedLanguage",

0 commit comments

Comments
 (0)