11import { SYSTEM_PROMPT } from "./const" ;
22import { 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" ;
98import {
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
407417function supportLanguages ( ) {
408418 return supportLanguageList . map ( ( [ standardLang ] ) => standardLang ) ;
0 commit comments