Skip to content

Commit

Permalink
feat(stream): add sup with stream
Browse files Browse the repository at this point in the history
  • Loading branch information
vacuityv committed Aug 8, 2023
1 parent 142c1fb commit 1d1bfc1
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 10 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# bob-plugin-vac-gptranslate

为Bob编写的通过调用chatgpt接口服务实现翻译的插件。
为Bob编写的通过调用chatgpt接口服务实现翻译的插件。支持流式传输(需要bob版本>=1.8.0)

## 效果展示

Expand Down
16 changes: 16 additions & 0 deletions info.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,22 @@
"identifier": "loginPassword",
"type": "text",
"title": "登录密码"
},
{
"identifier": "useStreamFlag",
"type": "menu",
"title": "流式传输",
"defaultValue": "false",
"menuValues": [
{
"title": "不使用",
"value": "n"
},
{
"title": "使用(需要版本>=1.8.0)",
"value": "y"
}
]
}
]
}
86 changes: 77 additions & 9 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,26 +33,51 @@ var langMap = {
'ar': '阿拉伯语'
};

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 account = $option.loginAccount;
var password = $option.loginPassword;
var content = query['text'];

var streamSupFlag = false;
var useStreamFlag = $option.useStreamFlag;
try {
var env = $env;
if (typeof env !== "undefined") {
appVersion = $env.appVersion;
if (appVersion >= '1.8.0') {
streamSupFlag = true;
}
}
} 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);
} else {
oldTranslate(query, completion);
}
}

function oldTranslate(query, completion) {

$http.request({
method: "POST",
url: "https://chat.vacuity.me/vac-chat-api/chat/ext/loginTranslate",
header: {
"Content-Type": "application/json;charset=UTF-8"
},
body: {
email: account,
password: password,
content: content,
targetLanguage: langMap[query['to']],
},
body: initReqBody(query),
handler: function (resp) {
$log.info('请求结果');
$log.info(util.desc(resp));
Expand All @@ -75,3 +100,46 @@ function translate(query, completion) {
}
});
}


function newTrans(query, completion) {

resTxt = '';
$http.streamRequest({
method: "POST",
url: "https://chat.vacuity.me/vac-chat-api/chat/ext/loginStreamTranslate",
header: {
"Content-Type": "application/json;charset=UTF-8"
},
body: initReqBody(query),
streamHandler: function (resp) {
var txt = resp.text;
resTxt = resTxt + txt;
translateResult = {
'toParagraphs': [resTxt]
}
query.onStream({'result': translateResult});
},
handler: function (data, rawData, response, error) {
query.onCompletion({
result: {
toParagraphs: [resTxt],
}
});
}
});
}

function initReqBody(query) {
var account = $option.loginAccount;
var password = $option.loginPassword;
var content = query['text'];

return {
email: account,
password: password,
content: content,
targetLanguage: langMap[query['to']],
};
}

0 comments on commit 1d1bfc1

Please sign in to comment.