koishi-plugin-newbingchat
Version:
不需要proxy即可使用的newbing AI,基于NewbingGoGo。
213 lines (212 loc) • 10.4 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.NewBingSessionClass = void 0;
const https_1 = __importDefault(require("https"));
const ws_1 = __importDefault(require("ws"));
const SendMessageManager_1 = __importDefault(require("./tools/SendMessageManager"));
const ChatOptionsSets_1 = __importDefault(require("./tools/ChatOptionsSets"));
class NewBingSessionClass {
constructor(userId, cookie, chatType) {
this.userId = userId;
this.cookie = cookie;
this.chatType = chatType;
this.status = -1;
this.tempRevMsg = null;
}
sendMessage(msg, callback) {
if (this.status > 0) {
if (msg.includes("结束对话") || msg.includes("结束聊天")) {
this.bingChatWs.close();
return this;
}
callback(this.status, "上一句对话正在思考...");
return this;
}
if (msg.includes("结束对话") || msg.includes("结束聊天")) {
callback(this.status, "对话已经结束了...");
this.bingChatWs.close();
return this;
}
this.status = 1;
this.tempRevMsg = null;
NewBingSessionClass.RepCallbackMap.set(this.userId, callback);
this.msgManager.sendChatMessage(this.bingChatWs, msg);
return this;
}
start(origin, msgFilter) {
return new Promise((resolve, reject) => {
if (this.bingChatWs != null) {
resolve();
return;
}
this.getAuth(origin).then((resjson) => {
this.bingChatWs = new ws_1.default('wss://' + origin + '/sydney/ChatHub');
this.bingChatWs.on('error', (err) => {
this.status = 0;
console.error(err);
});
this.bingChatWs.on('close', () => {
this.status = -1;
NewBingSessionClass.SessionMap.delete(this.userId);
if (this.tempRevMsg) {
NewBingSessionClass.RepCallbackMap.get(this.userId)(this.status, this.tempRevMsg);
}
NewBingSessionClass.RepCallbackMap.get(this.userId)(this.status, "与必应大小姐的链接已断开...");
NewBingSessionClass.RepCallbackMap.delete(this.userId);
});
this.bingChatWs.on('open', () => {
//console.log(JSON.stringify(resjson, null, 4));
this.msgManager = new SendMessageManager_1.default({ chatOptionsSets: new ChatOptionsSets_1.default() }, resjson.conversationId, resjson.clientId, resjson.conversationSignature, this.chatType, undefined);
this.msgManager.sendShakeHandsJson(this.bingChatWs);
resolve();
});
this.bingChatWs.on('message', (data) => {
let msgs = data.toString().split('\u001e');
for (let i = 0; i < msgs.length; i++) {
if (msgs[i] === '') {
continue;
}
let dataJson = JSON.parse(msgs[i]);
if (dataJson.type === 6) { // 心跳包
continue;
}
if (dataJson.type === 1) { // update
try {
let item = dataJson.arguments[0];
if (item.messages) {
this.tempRevMsg = item.messages[0].text;
}
else {
// 对话次数
this.throttling = item.throttling;
}
}
catch (err) {
console.error(err.stack);
console.log(JSON.stringify(dataJson, null, 4));
}
continue;
}
if (dataJson.type === 2) {
this.status = 0;
let item = dataJson.item;
switch (item.result.value) {
case "Success": {
break;
}
case "UnauthorizedRequest": {
NewBingSessionClass.RepCallbackMap.get(this.userId)(this.status, "[Error] 你需要更新你的cookie了");
this.bingChatWs.close();
return;
}
default: {
NewBingSessionClass.RepCallbackMap.get(this.userId)(this.status, "[Error] " + item.result.message);
this.bingChatWs.close();
return;
}
}
for (let elem of item.messages) {
if (elem.author != "bot" || !elem.text) {
continue;
}
if (elem.messageType) {
if (msgFilter[elem.messageType]) {
continue;
}
}
try {
//console.log(JSON.stringify(elem, null, 4));
//let msg = elem.adaptiveCards[0].body[0].text.replace(/\[\^\d\^\]/g, "");
let msg = elem.text.replace(/\[\^\d\^\]/g, "");
if (msg.endsWith("🙏")) {
if (this.tempRevMsg) {
NewBingSessionClass.RepCallbackMap.get(this.userId)(this.status, this.tempRevMsg);
NewBingSessionClass.RepCallbackMap.get(this.userId)(this.status, "必应大小姐的回答被掐断了,重新开始总是很棒的...");
}
else {
NewBingSessionClass.RepCallbackMap.get(this.userId)(this.status, msg);
}
this.tempRevMsg = null;
this.bingChatWs.close();
this.bingChatWs = null;
NewBingSessionClass.RepCallbackMap.delete(this.userId);
NewBingSessionClass.SessionMap.delete(this.userId);
return;
}
NewBingSessionClass.RepCallbackMap.get(this.userId)(this.status, msg);
if (elem.suggestedResponses) {
NewBingSessionClass.RepCallbackMap.get(this.userId)(this.status, NewBingSessionClass.handleSuggested(elem.suggestedResponses));
}
}
catch (err) {
console.error(err.stack);
console.log(JSON.stringify(elem, null, 4));
}
}
this.tempRevMsg = null;
if (this.throttling.numUserMessagesInConversation >= this.throttling.maxNumUserMessagesInConversation) {
NewBingSessionClass.RepCallbackMap.get(this.userId)(this.status, "对话已达到 20 次上限,会话已重置...");
this.bingChatWs.close();
}
}
}
});
});
});
}
getAuth(origin) {
return new Promise((resolve, reject) => {
const req = https_1.default.request({
hostname: origin,
port: 443,
path: '/turing/conversation/create',
method: 'GET',
headers: {
"cookie": this.cookie
}
}, res => {
if (res.headers['cf-mitigated']) { //可以通过
// /challenge?redirect=重定向url 通过cf验证后自动重定向。
}
let data = [];
res.on('data', chunk => {
data.push(chunk);
});
res.on('end', () => {
try {
resolve(JSON.parse(data.toString()));
}
catch (err) {
console.error(err);
}
});
});
req.on('error', err => {
reject(err);
});
req.end();
});
}
static handleSuggested(data) {
let res = [];
for (let i = 0; i < data.length; i++) {
res.push(data[i].text);
}
return "建议:\n" + res.join("\n");
}
static getChatSession(userId, config) {
if (!this.SessionMap.has(userId)) {
this.SessionMap.set(userId, new NewBingSessionClass(userId, config.cookies[0], config.chatType));
}
return this.SessionMap.get(userId);
}
}
NewBingSessionClass.SessionMap = new Map();
NewBingSessionClass.RepCallbackMap = new Map();
exports.NewBingSessionClass = NewBingSessionClass;
(function (NewBingSessionClass) {
})(NewBingSessionClass = exports.NewBingSessionClass || (exports.NewBingSessionClass = {}));
exports.NewBingSessionClass = NewBingSessionClass;