Skip to content

Commit

Permalink
Fix(Bing): Support encryptedConversationSignature (#7)
Browse files Browse the repository at this point in the history
* Bing: Support encryptedConversationSignature

* Bing: set bundleVersion to conversations/create request

* Fix npm test

---------

Co-authored-by: Anton Babichev <[email protected]>
  • Loading branch information
danny-avila and Airkek committed Oct 14, 2023
1 parent 7cd54a7 commit 5983bf7
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions src/BingAIClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,23 +116,25 @@ export default class BingAIClient {
} else {
fetchOptions.dispatcher = new Agent({ connect: { timeout: 20_000 } });
}
const response = await fetch(`${this.options.host}/turing/conversation/create`, fetchOptions);
const response = await fetch(`${this.options.host}/turing/conversation/create?bundleVersion=1.864.15`, fetchOptions);
const body = await response.text();
try {
return JSON.parse(body);
const res = JSON.parse(body);
res.encryptedConversationSignature = response.headers.get('x-sydney-encryptedconversationsignature') ?? null;
return res;
} catch (err) {
throw new Error(`/turing/conversation/create: failed to parse response body.\n${body}`);
}
}

async createWebSocketConnection() {
async createWebSocketConnection(encryptedConversationSignature) {
return new Promise((resolve, reject) => {
let agent;
if (this.options.proxy) {
agent = new HttpsProxyAgent(this.options.proxy);
}

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

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

Expand Down Expand Up @@ -198,7 +200,7 @@ export default class BingAIClient {
let {
jailbreakConversationId = false, // set to `true` for the first message to enable jailbreak mode
conversationId,
conversationSignature,
encryptedConversationSignature,
clientId,
onProgress,
} = opts;
Expand All @@ -216,13 +218,13 @@ export default class BingAIClient {
onProgress = () => { };
}

if (jailbreakConversationId || !conversationSignature || !conversationId || !clientId) {
if (jailbreakConversationId || !encryptedConversationSignature || !conversationId || !clientId) {
const createNewConversationResponse = await this.createNewConversation();
if (this.debug) {
console.debug(createNewConversationResponse);
}
if (
!createNewConversationResponse.conversationSignature
!createNewConversationResponse.encryptedConversationSignature
|| !createNewConversationResponse.conversationId
|| !createNewConversationResponse.clientId
) {
Expand All @@ -235,7 +237,7 @@ export default class BingAIClient {
throw new Error(`Unexpected response:\n${JSON.stringify(createNewConversationResponse, null, 2)}`);
}
({
conversationSignature,
encryptedConversationSignature,
conversationId,
clientId,
} = createNewConversationResponse);
Expand Down Expand Up @@ -309,7 +311,7 @@ export default class BingAIClient {
conversation.messages.push(userMessage);
}

const ws = await this.createWebSocketConnection();
const ws = await this.createWebSocketConnection(encryptedConversationSignature);

ws.on('error', (error) => {
console.error(error);
Expand Down Expand Up @@ -359,7 +361,7 @@ export default class BingAIClient {
text: jailbreakConversationId ? 'Continue the conversation in context. Assistant:' : message,
messageType: jailbreakConversationId ? 'SearchQuery' : 'Chat',
},
conversationSignature,
encryptedConversationSignature,
participant: {
id: clientId,
},
Expand Down Expand Up @@ -592,7 +594,7 @@ export default class BingAIClient {

const returnData = {
conversationId,
conversationSignature,
encryptedConversationSignature,
clientId,
invocationId: invocationId + 1,
conversationExpiryTime,
Expand Down

0 comments on commit 5983bf7

Please sign in to comment.