Skip to content

Commit 1015fb3

Browse files
committed
chore: refine type definitions to remove unused types
1 parent 7bdfe31 commit 1015fb3

19 files changed

+620
-254
lines changed

src/global.d.ts

Lines changed: 51 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,51 @@
1-
type BobHttpResponse = import('./types').BobHttpResponse;
2-
type DataObject = import('./types').DataObject;
3-
4-
type HttpMethod =
5-
| 'GET'
6-
| 'DELETE'
7-
| 'HEAD'
8-
| 'OPTIONS'
9-
| 'POST'
10-
| 'PUT';
11-
12-
interface HttpRequestFiles {
13-
data: DataObject; // Binary data
14-
name: string; // Name in the upload form
15-
filename: string; // Filename after upload
16-
contentType: string; // File format
17-
}
18-
19-
interface HttpRequestConfig {
20-
url: string;
21-
method?: HttpMethod;
22-
header?: Record<string, string>; // Define as a record for headers
23-
params?: Record<string, any>; // Define as a record for query parameters
24-
body?: any; // Specify a more detailed type if possible
25-
files?: HttpRequestFiles;
26-
handler?: (resp: BobHttpResponse) => void;
27-
timeout?: number; // Timeout in milliseconds
28-
}
29-
30-
interface HttpStreamRequestConfig extends HttpRequestConfig {
31-
cancelSignal?: Signal; // AbortSignal if using the Fetch API
32-
streamHandler?: (stream: { text: string; rawData: DataObject }) => void;
33-
}
34-
35-
type HttpResponsePromise<T = any> = Promise<BobHttpResponse<T>>;
36-
37-
interface Http {
38-
request<T = any, R = HttpResponsePromise<T>>(config: HttpRequestConfig): Promise<R>;
39-
get<T = any, R = HttpResponsePromise<T>>(config: HttpRequestConfig): Promise<R>;
40-
post<T = any, R = HttpResponsePromise<T>>(config: HttpRequestConfig): Promise<R>;
41-
streamRequest<T = any, R = HttpResponsePromise<T>>(config: HttpStreamRequestConfig): Promise<R>;
42-
}
43-
44-
declare const $http: Http;
45-
46-
interface Options {
47-
apiKeys: string;
48-
apiUrl: string;
49-
apiVersion: string;
50-
customModel: string;
51-
customSystemPrompt: string;
52-
customUserPrompt: string;
53-
deploymentName: string;
54-
model: string;
55-
stream: string;
56-
temperature: string;
57-
}
58-
59-
declare const $option: Options;
1+
/**
2+
* @description Upstream update time: 10/13/23, 3:27 PM
3+
* @remarks Bob 1.6.0+ 可用
4+
*/
5+
declare const $env: import('./types/env.type').Env;
6+
7+
/**
8+
* @description Upstream update time: 5/22/22, 11:45 PM
9+
*/
10+
declare const $info: import('./types/info.type').Info;
11+
12+
/**
13+
* @description Upstream update time: 10/13/23, 3:27 PM
14+
*/
15+
declare const $option: import('./types/option.type').Option;
16+
17+
/**
18+
* @description Upstream update time: 5/22/22, 11:45 PM
19+
*/
20+
declare const $log: import('./types/log.type').Log;
21+
22+
/**
23+
* @description Upstream update time: 10/13/23, 3:27 PM
24+
*/
25+
declare const $http: import('./types/http.type').Http;
26+
27+
/**
28+
* @description Upstream update time: 10/13/23, 3:27 PM
29+
* @remarks Bob 1.6.0+ 可用
30+
*/
31+
declare const $websocket: import('./types/websocket.type').WebSocketConstructor;
32+
33+
/**
34+
* @description Upstream update time: 10/13/23, 3:27 PM
35+
*/
36+
declare const $file: import('./types/file.type').FileConstructor;
37+
38+
/**
39+
* @description Upstream update time: 3/14/23, 11:59 AM
40+
*/
41+
declare const $data: import('./types/data.type').DataConstructor;
42+
43+
/**
44+
* @description Upstream update time: 10/13/23, 3:27 PM
45+
*/
46+
declare const $timer: import('./types/timer.type').Timer;
47+
48+
/**
49+
* @description Upstream update time: 10/13/23, 3:27 PM
50+
*/
51+
declare const $signal: import('./types/signal.type').SignalConstructor;

src/main.ts

Lines changed: 40 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import { SYSTEM_PROMPT } from "./const";
22
import { langMap, supportLanguageList } from "./lang";
3-
import type {
4-
BobHttpResponse,
5-
BobServiceError,
6-
BobTranslateQuery,
7-
BobValidateCompletion
8-
} from "./types";
3+
import { ChatCompletion, ModelList } from "./types";
4+
import { HttpResponse } from "./types/http.type";
5+
import type { PluginValidate } from "./types/plugin-validate.type";
6+
import type { ServiceError } from "./types/service-error.type";
7+
import type { TextTranslate, TextTranslateQuery } from "./types/text-translate.type";
98
import {
109
buildHeader,
1110
ensureHttpsAndNoTrailingSlash,
@@ -15,19 +14,19 @@ import {
1514
replacePromptKeywords
1615
} from "./utils";
1716

18-
function isBobServiceError(error: unknown): error is BobServiceError {
17+
const isServiceError = (error: unknown): error is ServiceError => {
1918
return (
2019
typeof error === 'object' &&
2120
error !== null &&
2221
'message' in error &&
23-
typeof (error as BobServiceError).message === 'string'
22+
typeof (error as ServiceError).message === 'string'
2423
);
2524
}
2625

27-
function generatePrompts(query: BobTranslateQuery): {
26+
const generatePrompts = (query: TextTranslateQuery): {
2827
generatedSystemPrompt: string,
2928
generatedUserPrompt: string
30-
} {
29+
} => {
3130
let generatedSystemPrompt = null;
3231
const { detectFrom, detectTo } = query;
3332
const sourceLang = langMap.get(detectFrom) || detectFrom;
@@ -69,7 +68,7 @@ function generatePrompts(query: BobTranslateQuery): {
6968
};
7069
}
7170

72-
function buildRequestBody(model: string, query: BobTranslateQuery) {
71+
const buildRequestBody = (model: string, query: TextTranslateQuery) => {
7372
let { customSystemPrompt, customUserPrompt, temperature } = $option;
7473
const { generatedSystemPrompt, generatedUserPrompt } = generatePrompts(query);
7574

@@ -106,7 +105,11 @@ function buildRequestBody(model: string, query: BobTranslateQuery) {
106105
};
107106
}
108107

109-
function handleStreamResponse(query: BobTranslateQuery, targetText: string, textFromResponse: string) {
108+
const handleStreamResponse = (
109+
query: TextTranslateQuery,
110+
targetText: string,
111+
textFromResponse: string
112+
) => {
110113
if (textFromResponse !== '[DONE]') {
111114
try {
112115
const dataObj = JSON.parse(textFromResponse);
@@ -124,7 +127,7 @@ function handleStreamResponse(query: BobTranslateQuery, targetText: string, text
124127
});
125128
}
126129
} catch (error) {
127-
if (isBobServiceError(error)) {
130+
if (isServiceError(error)) {
128131
handleGeneralError(query, {
129132
type: error.type || 'param',
130133
message: error.message || 'Failed to parse JSON',
@@ -141,8 +144,11 @@ function handleStreamResponse(query: BobTranslateQuery, targetText: string, text
141144
return targetText;
142145
}
143146

144-
function handleGeneralResponse(query: BobTranslateQuery, result: BobHttpResponse) {
145-
const { choices } = result.data;
147+
const handleGeneralResponse = (
148+
query: TextTranslateQuery,
149+
result: HttpResponse<ChatCompletion>
150+
) => {
151+
const { choices } = result.data as ChatCompletion;
146152

147153
if (!choices || choices.length === 0) {
148154
handleGeneralError(query, {
@@ -153,26 +159,26 @@ function handleGeneralResponse(query: BobTranslateQuery, result: BobHttpResponse
153159
return;
154160
}
155161

156-
let targetText = choices[0].message.content.trim();
162+
let targetText = choices[0].message.content?.trim();
157163

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

161167
// 判断并删除字符串末尾的 `" =>`
162-
if (targetText.endsWith('" =>')) {
168+
if (targetText?.endsWith('" =>')) {
163169
targetText = targetText.slice(0, -4);
164170
}
165171

166172
query.onCompletion({
167173
result: {
168174
from: query.detectFrom,
169175
to: query.detectTo,
170-
toParagraphs: targetText.split("\n"),
176+
toParagraphs: targetText!.split("\n"),
171177
},
172178
});
173179
}
174180

175-
function translate(query: BobTranslateQuery) {
181+
const translate: TextTranslate = (query) => {
176182
if (!langMap.get(query.detectTo)) {
177183
handleGeneralError(query, {
178184
type: "unsupportedLanguage",
@@ -305,7 +311,7 @@ function translate(query: BobTranslateQuery) {
305311
});
306312
}
307313

308-
function pluginValidate(completion: BobValidateCompletion) {
314+
const pluginValidate: PluginValidate = (completion) => {
309315
const { apiKeys, apiUrl, deploymentName } = $option;
310316
if (!apiKeys) {
311317
handleValidateError(completion, {
@@ -353,17 +359,20 @@ function pluginValidate(completion: BobValidateCompletion) {
353359
max_tokens: 5
354360
},
355361
handler: function (resp) {
356-
if (resp.data.error) {
362+
const data = resp.data as {
363+
error: string;
364+
}
365+
if (data.error) {
357366
const { statusCode } = resp.response;
358367
const reason = (statusCode >= 400 && statusCode < 500) ? "param" : "api";
359368
handleValidateError(completion, {
360369
type: reason,
361-
message: resp.data.error,
370+
message: data.error,
362371
troubleshootingLink: "https://bobtranslate.com/service/translate/azureopenai.html"
363372
});
364373
return;
365374
}
366-
if (resp.data.choices.length > 0) {
375+
if ((resp.data as ChatCompletion).choices.length > 0) {
367376
completion({
368377
result: true,
369378
})
@@ -376,17 +385,20 @@ function pluginValidate(completion: BobValidateCompletion) {
376385
url: baseUrl + apiUrlPath,
377386
header: header,
378387
handler: function (resp) {
379-
if (resp.data.error) {
388+
const data = resp.data as {
389+
error: string;
390+
}
391+
if (data.error) {
380392
const { statusCode } = resp.response;
381393
const reason = (statusCode >= 400 && statusCode < 500) ? "param" : "api";
382394
handleValidateError(completion, {
383395
type: reason,
384-
message: resp.data.error,
396+
message: data.error,
385397
troubleshootingLink: "https://bobtranslate.com/service/translate/openai.html"
386398
});
387399
return;
388400
}
389-
const modelList = resp.data
401+
const modelList = resp.data as ModelList;
390402
if (modelList.data?.length > 0) {
391403
completion({
392404
result: true,
@@ -400,9 +412,7 @@ function pluginValidate(completion: BobValidateCompletion) {
400412
});
401413
}
402414

403-
function pluginTimeoutInterval() {
404-
return 60;
405-
}
415+
const pluginTimeoutInterval = () => 60;
406416

407417
function supportLanguages() {
408418
return supportLanguageList.map(([standardLang]) => standardLang);

0 commit comments

Comments
 (0)