Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix(Bing): Support encryptedConversationSignature #7

Merged
merged 3 commits into from
Oct 14, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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