-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
242 lines (219 loc) · 7.23 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/**
* 由于各大服务商的语言代码都不大一样,
* 所以我定义了一份 Bob 专用的语言代码,以便 Bob 主程序和插件之间互传语种。
* Bob 语言代码列表 https://ripperhe.gitee.io/bob/#/plugin/addtion/language
*
* 转换的代码建议以下面的方式实现,
* `xxx` 代表服务商特有的语言代码,请替换为真实的,
* 具体支持的语种数量请根据实际情况而定。
*
* Bob 语言代码转服务商语言代码(以为 'zh-Hans' 为例): var lang = langMap.get('zh-Hans');
* 服务商语言代码转 Bob 语言代码: var standardLang = langMapReverse.get('xxx');
*/
var util = require("$util");
var langMap = {
'auto': '中文简体',
'zh-Hans': '中文简体',
'zh-Hant': '中文繁体',
'yue': '粤语',
'wyw': '文言文',
'pysx': '拼音缩写',
'en': '英语',
'ja': '日语',
'ko': '韩语',
'fr': '法语',
'de': '德语',
'es': '西班牙语',
'it': '意大利语',
'ru': '俄语',
'pt': '葡萄牙语',
'nl': '荷兰语',
'pl': '波兰语',
'ar': '阿拉伯语'
};
var usaHttp = "https://chat.vacuity.me/vac-chat-api/chat/ext/loginTranslate";
var usaWss = "wss://chat.vacuity.me/vac-chat-api/stream/chat/chat";
// var usaHttp = "http://127.0.0.1:8081/vac-chat-api/chat/ext/loginTranslate";
// var usaWss = "ws://127.0.0.1:8081/vac-chat-api/stream/chat/chat";
var socket = '';
var readyState = false;
var connectIng = true;
function supportLanguages() {
return ['auto', 'zh-Hans', 'zh-Hant', 'yue', 'wyw', 'pysx', 'en', 'ja', 'ko', 'fr', 'de', 'es', 'it', 'ru', 'pt', 'nl', 'pl', 'ar'];
}
function translate(query, completion) {
var streamSupFlag = false;
var useStreamFlag = $option.useStreamFlag;
try {
var env = $env;
if (typeof env !== "undefined") {
appVersion = $env.appVersion;
if (compareVersions(appVersion, '1.8.0') >= 0) {
streamSupFlag = true
}
} else {
$log.info('get env error, process as old version');
}
} catch (error) {
$log.info('get env error, process as old version');
}
$log.info('streamSupFlag');
$log.info(streamSupFlag);
$log.info('useStreamFlag');
$log.info(useStreamFlag);
if (streamSupFlag && useStreamFlag === 'y') {
// newTrans(query, completion);
$log.info("vac-body-stream" + initReqBody(query));
websocketTrans(query, completion);
} else {
oldTranslate(query, completion);
}
}
function oldTranslate(query, completion) {
$log.info("vac-body" + initReqBody(query));
$http.request({
method: "POST",
url: usaHttp,
header: {
"Content-Type": "application/json;charset=UTF-8"
},
body: initReqBody(query),
handler: function (resp) {
$log.info('请求结果');
$log.info(util.desc(resp));
var data = resp.data;
$log.info(util.desc(data));
$log.info(util.desc(data.code));
if (data.code == '0') {
translateResult = {
'toParagraphs': [data.data.content]
}
completion({'result': translateResult});
} else {
serviceError = {
'type': 'api',
'message': data.msg,
'addition': data.msg,
}
completion({'error': data.msg});
}
}
});
}
var websocket = null;
var count = 0;
var timerId = 0;
var signal = $signal.new()
function initWebsocket(msg) {
vacUrl = usaWss;
if (websocket == null) {
$log.info(`initWebsocket` + vacUrl);
websocket = $websocket.new({
url: vacUrl,
allowSelfSignedSSLCertificates: true,
timeoutInterval: 100,
header: {
"Sec-WebSocket-Protocol": "someother protocols",
"Sec-WebSocket-Version": "14",
}
})
websocket.open();
websocket.listenOpen(function (socket) {
$log.info(`did open`);
websocket.sendString(msg);
websocket.listenError(function (socket, error) {
$log.info(`did error: code=${error.code}; message=${error.message}; type=${error.type}`);
})
websocket.listenReceiveData(function (socket, data) {
$log.info(`did receive data: length=${data.length}`);
count = 0;
signal.send({"message": data})
})
socket.listenReceiveString(function (socket, string) {
$log.info(`did receive string: ${string}`);
count = 0;
signal.send({"message": string})
})
})
websocket.listenClose(function (socket, code, reason) {
$log.info(`did close: code=${code}; reason=${reason}`);
})
count = 0;
}
}
function sendSocketMsg(msg) {
$log.info(`sendSocketMsg`)
count = 0;
if (websocket == null || websocket.readyState == 2 || websocket.readyState == 3) {
websocket = null;
initWebsocket(msg);
} else {
if (websocket.readyState == 1) {
$log.info('readyState == 1' + msg)
websocket.sendString(msg);
} else {
var stateTimerId = $timer.schedule({
interval: 1,
repeats: true,
handler: function () {
$log.info(`checkready...state=${websocket.readyState}`)
if (websocket.readyState == 1) {
$timer.invalidate(stateTimerId);
websocket.sendString(msg);
}
}
});
}
}
}
function websocketTrans(query, completion) {
$log.info(`websocketTrans`)
// 移除所有订阅制
signal.removeAllSubscriber();
resTxt = '';
sendSocketMsg(JSON.stringify(initReqBody(query)));
signal.subscribe(function (data) {
msg = data.message
if (msg == '###FINISH###') {
query.onCompletion({
result: {
toParagraphs: [resTxt],
}
});
return;
} else {
resTxt = resTxt + msg
translateResult = {
'toParagraphs': [resTxt]
}
query.onStream({'result': translateResult});
}
})
}
function initReqBody(query) {
var account = $option.loginAccount;
var password = $option.loginPassword;
var modelType = $option.modelType;
var prompt = $option.prompt;
var content = query['text'];
return {
email: account,
password: password,
content: content,
modelType: modelType,
targetLanguage: langMap[query['to']],
translateFrom: 'bob',
prompt: prompt
};
}
function compareVersions(version1, version2) {
const v1Parts = version1.split('.').map(Number);
const v2Parts = version2.split('.').map(Number);
for (let i = 0; i < Math.max(v1Parts.length, v2Parts.length); i++) {
const v1Part = v1Parts[i] || 0;
const v2Part = v2Parts[i] || 0;
if (v1Part > v2Part) return 1;
if (v1Part < v2Part) return -1;
}
return 0;
}