1
+ import type {
2
+ HttpResponse ,
3
+ PluginValidate ,
4
+ ServiceError ,
5
+ TextTranslate ,
6
+ TextTranslateQuery
7
+ } from "@bob-translate/types" ;
1
8
import { DEFAULT_PROMPT , languageMapping } from "./const" ;
2
9
import { langMap , supportLanguageList } from "./lang" ;
3
10
import type {
4
- BobHttpResponse ,
5
- BobServiceError ,
6
- BobTranslateQuery ,
7
- BobValidateCompletion ,
11
+ ChatCompletion ,
12
+ ModelList ,
8
13
PolishingMode ,
9
14
} from "./types" ;
10
15
import {
@@ -16,11 +21,9 @@ import {
16
21
replacePromptKeywords
17
22
} from "./utils" ;
18
23
19
- function pluginTimeoutInterval ( ) {
20
- return 60 ;
21
- }
24
+ const pluginTimeoutInterval = ( ) => 60 ;
22
25
23
- function pluginValidate ( completion : BobValidateCompletion ) {
26
+ const pluginValidate : PluginValidate = ( completion ) => {
24
27
const { apiKeys, apiUrl, deploymentName } = $option ;
25
28
if ( ! apiKeys ) {
26
29
handleValidateError ( completion , {
@@ -68,17 +71,20 @@ function pluginValidate(completion: BobValidateCompletion) {
68
71
max_tokens : 5
69
72
} ,
70
73
handler : function ( resp ) {
71
- if ( resp . data . error ) {
74
+ const data = resp . data as {
75
+ error : string ;
76
+ }
77
+ if ( data . error ) {
72
78
const { statusCode } = resp . response ;
73
79
const reason = ( statusCode >= 400 && statusCode < 500 ) ? "param" : "api" ;
74
80
handleValidateError ( completion , {
75
81
type : reason ,
76
- message : resp . data . error ,
82
+ message : data . error ,
77
83
troubleshootingLink : "https://bobtranslate.com/service/translate/azureopenai.html"
78
84
} ) ;
79
85
return ;
80
86
}
81
- if ( resp . data . choices . length > 0 ) {
87
+ if ( ( resp . data as ChatCompletion ) . choices . length > 0 ) {
82
88
completion ( {
83
89
result : true ,
84
90
} )
@@ -91,17 +97,20 @@ function pluginValidate(completion: BobValidateCompletion) {
91
97
url : baseUrl + apiUrlPath ,
92
98
header : header ,
93
99
handler : function ( resp ) {
94
- if ( resp . data . error ) {
100
+ const data = resp . data as {
101
+ error : string ;
102
+ }
103
+ if ( data . error ) {
95
104
const { statusCode } = resp . response ;
96
105
const reason = ( statusCode >= 400 && statusCode < 500 ) ? "param" : "api" ;
97
106
handleValidateError ( completion , {
98
107
type : reason ,
99
- message : resp . data . error ,
108
+ message : data . error ,
100
109
troubleshootingLink : "https://bobtranslate.com/service/translate/openai.html"
101
110
} ) ;
102
111
return ;
103
112
}
104
- const modelList = resp . data
113
+ const modelList = resp . data as ModelList ;
105
114
if ( modelList . data ?. length > 0 ) {
106
115
completion ( {
107
116
result : true ,
@@ -119,20 +128,20 @@ function supportLanguages() {
119
128
return supportLanguageList . map ( ( [ standardLang ] ) => standardLang ) ;
120
129
}
121
130
122
- function isBobServiceError ( error : unknown ) : error is BobServiceError {
131
+ const isServiceError = ( error : unknown ) : error is ServiceError => {
123
132
return (
124
133
typeof error === 'object' &&
125
134
error !== null &&
126
135
'message' in error &&
127
- typeof ( error as BobServiceError ) . message === 'string'
136
+ typeof ( error as ServiceError ) . message === 'string'
128
137
) ;
129
138
}
130
139
131
- function generateSystemPrompt (
140
+ const generateSystemPrompt = (
132
141
basePrompt : string | null ,
133
142
polishingMode : PolishingMode ,
134
- query : BobTranslateQuery
135
- ) : string {
143
+ query : TextTranslateQuery
144
+ ) : string => {
136
145
const isDetailedPolishingMode = polishingMode === "detailed" ;
137
146
138
147
const promptInfo = languageMapping [ query . detectFrom ] || {
@@ -148,15 +157,15 @@ function generateSystemPrompt(
148
157
return systemPrompt ;
149
158
}
150
159
151
- function buildRequestBody (
160
+ const buildRequestBody = (
152
161
model : string ,
153
- query : BobTranslateQuery
154
- ) {
162
+ query : TextTranslateQuery
163
+ ) => {
155
164
const { customSystemPrompt, customUserPrompt, polishingMode } = $option ;
156
165
157
166
const systemPrompt = generateSystemPrompt (
158
167
replacePromptKeywords ( customSystemPrompt , query ) ,
159
- polishingMode ,
168
+ polishingMode as PolishingMode ,
160
169
query
161
170
) ;
162
171
@@ -189,7 +198,11 @@ function buildRequestBody(
189
198
} ;
190
199
}
191
200
192
- function handleStreamResponse ( query : BobTranslateQuery , targetText : string , textFromResponse : string ) {
201
+ const handleStreamResponse = (
202
+ query : TextTranslateQuery ,
203
+ targetText : string ,
204
+ textFromResponse : string
205
+ ) => {
193
206
if ( textFromResponse !== '[DONE]' ) {
194
207
try {
195
208
const dataObj = JSON . parse ( textFromResponse ) ;
@@ -207,7 +220,7 @@ function handleStreamResponse(query: BobTranslateQuery, targetText: string, text
207
220
} ) ;
208
221
}
209
222
} catch ( error ) {
210
- if ( isBobServiceError ( error ) ) {
223
+ if ( isServiceError ( error ) ) {
211
224
handleGeneralError ( query , {
212
225
type : error . type || 'param' ,
213
226
message : error . message || 'Failed to parse JSON' ,
@@ -224,8 +237,8 @@ function handleStreamResponse(query: BobTranslateQuery, targetText: string, text
224
237
return targetText ;
225
238
}
226
239
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 ;
229
242
230
243
if ( ! choices || choices . length === 0 ) {
231
244
handleGeneralError ( query , {
@@ -236,26 +249,26 @@ function handleGeneralResponse(query: BobTranslateQuery, result: BobHttpResponse
236
249
return ;
237
250
}
238
251
239
- let targetText = choices [ 0 ] . message . content . trim ( ) ;
252
+ let targetText = choices [ 0 ] . message . content ? .trim ( ) ;
240
253
241
254
// 使用正则表达式删除字符串开头和结尾的特殊字符
242
- targetText = targetText . replace ( / ^ ( 『 | 「 | " | “ ) | ( 』 | 」 | " | ” ) $ / g, "" ) ;
255
+ targetText = targetText ? .replace ( / ^ ( 『 | 「 | " | “ ) | ( 』 | 」 | " | ” ) $ / g, "" ) ;
243
256
244
257
// 判断并删除字符串末尾的 `" =>`
245
- if ( targetText . endsWith ( '" =>' ) ) {
258
+ if ( targetText ? .endsWith ( '" =>' ) ) {
246
259
targetText = targetText . slice ( 0 , - 4 ) ;
247
260
}
248
261
249
262
query . onCompletion ( {
250
263
result : {
251
264
from : query . detectFrom ,
252
265
to : query . detectTo ,
253
- toParagraphs : targetText . split ( "\n" ) ,
266
+ toParagraphs : targetText ! . split ( "\n" ) ,
254
267
} ,
255
268
} ) ;
256
269
}
257
270
258
- function translate ( query : BobTranslateQuery ) {
271
+ const translate : TextTranslate = ( query ) => {
259
272
if ( ! langMap . get ( query . detectTo ) ) {
260
273
handleGeneralError ( query , {
261
274
type : "unsupportedLanguage" ,
0 commit comments