Skip to content

Commit 5983bf7

Browse files
danny-avilaAirkek
andauthored
Fix(Bing): Support encryptedConversationSignature (#7)
* Bing: Support encryptedConversationSignature * Bing: set bundleVersion to conversations/create request * Fix npm test --------- Co-authored-by: Anton Babichev <a.babichev@vk.team>
1 parent 7cd54a7 commit 5983bf7

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

src/BingAIClient.js

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -116,23 +116,25 @@ export default class BingAIClient {
116116
} else {
117117
fetchOptions.dispatcher = new Agent({ connect: { timeout: 20_000 } });
118118
}
119-
const response = await fetch(`${this.options.host}/turing/conversation/create`, fetchOptions);
119+
const response = await fetch(`${this.options.host}/turing/conversation/create?bundleVersion=1.864.15`, fetchOptions);
120120
const body = await response.text();
121121
try {
122-
return JSON.parse(body);
122+
const res = JSON.parse(body);
123+
res.encryptedConversationSignature = response.headers.get('x-sydney-encryptedconversationsignature') ?? null;
124+
return res;
123125
} catch (err) {
124126
throw new Error(`/turing/conversation/create: failed to parse response body.\n${body}`);
125127
}
126128
}
127129

128-
async createWebSocketConnection() {
130+
async createWebSocketConnection(encryptedConversationSignature) {
129131
return new Promise((resolve, reject) => {
130132
let agent;
131133
if (this.options.proxy) {
132134
agent = new HttpsProxyAgent(this.options.proxy);
133135
}
134136

135-
const ws = new WebSocket('wss://sydney.bing.com/sydney/ChatHub', { agent, headers: this.headers });
137+
const ws = new WebSocket(`wss://sydney.bing.com/sydney/ChatHub?sec_access_token=${encodeURIComponent(encryptedConversationSignature)}`, { agent, headers: this.headers });
136138

137139
ws.on('error', err => reject(err));
138140

@@ -198,7 +200,7 @@ export default class BingAIClient {
198200
let {
199201
jailbreakConversationId = false, // set to `true` for the first message to enable jailbreak mode
200202
conversationId,
201-
conversationSignature,
203+
encryptedConversationSignature,
202204
clientId,
203205
onProgress,
204206
} = opts;
@@ -216,13 +218,13 @@ export default class BingAIClient {
216218
onProgress = () => { };
217219
}
218220

219-
if (jailbreakConversationId || !conversationSignature || !conversationId || !clientId) {
221+
if (jailbreakConversationId || !encryptedConversationSignature || !conversationId || !clientId) {
220222
const createNewConversationResponse = await this.createNewConversation();
221223
if (this.debug) {
222224
console.debug(createNewConversationResponse);
223225
}
224226
if (
225-
!createNewConversationResponse.conversationSignature
227+
!createNewConversationResponse.encryptedConversationSignature
226228
|| !createNewConversationResponse.conversationId
227229
|| !createNewConversationResponse.clientId
228230
) {
@@ -235,7 +237,7 @@ export default class BingAIClient {
235237
throw new Error(`Unexpected response:\n${JSON.stringify(createNewConversationResponse, null, 2)}`);
236238
}
237239
({
238-
conversationSignature,
240+
encryptedConversationSignature,
239241
conversationId,
240242
clientId,
241243
} = createNewConversationResponse);
@@ -309,7 +311,7 @@ export default class BingAIClient {
309311
conversation.messages.push(userMessage);
310312
}
311313

312-
const ws = await this.createWebSocketConnection();
314+
const ws = await this.createWebSocketConnection(encryptedConversationSignature);
313315

314316
ws.on('error', (error) => {
315317
console.error(error);
@@ -359,7 +361,7 @@ export default class BingAIClient {
359361
text: jailbreakConversationId ? 'Continue the conversation in context. Assistant:' : message,
360362
messageType: jailbreakConversationId ? 'SearchQuery' : 'Chat',
361363
},
362-
conversationSignature,
364+
encryptedConversationSignature,
363365
participant: {
364366
id: clientId,
365367
},
@@ -592,7 +594,7 @@ export default class BingAIClient {
592594

593595
const returnData = {
594596
conversationId,
595-
conversationSignature,
597+
encryptedConversationSignature,
596598
clientId,
597599
invocationId: invocationId + 1,
598600
conversationExpiryTime,

0 commit comments

Comments
 (0)