Skip to content

Commit 8656461

Browse files
committed
upgrade bing client (merge waylaidwanderer/node-chatgpt-api#481) and some other adaptations (#505, #519, #525)
1 parent 305c5b7 commit 8656461

File tree

6 files changed

+37
-19
lines changed

6 files changed

+37
-19
lines changed

src/background/index.mjs

+1
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ Browser.runtime.onMessage.addListener(async (message, sender) => {
216216
body: text,
217217
status: response.status,
218218
statusText: response.statusText,
219+
headers: Object.fromEntries(response.headers),
219220
},
220221
null,
221222
]

src/services/apis/bing-web.mjs

+2-2
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export async function generateAnswersWithBingWebApi(
4646
...(session.bingWeb_conversationId
4747
? {
4848
conversationId: session.bingWeb_conversationId,
49-
conversationSignature: session.bingWeb_conversationSignature,
49+
encryptedConversationSignature: session.bingWeb_encryptedConversationSignature,
5050
clientId: session.bingWeb_clientId,
5151
invocationId: session.bingWeb_invocationId,
5252
}
@@ -64,7 +64,7 @@ export async function generateAnswersWithBingWebApi(
6464
})
6565

6666
if (!sydneyMode) {
67-
session.bingWeb_conversationSignature = response.conversationSignature
67+
session.bingWeb_encryptedConversationSignature = response.encryptedConversationSignature
6868
session.bingWeb_conversationId = response.conversationId
6969
session.bingWeb_clientId = response.clientId
7070
session.bingWeb_invocationId = response.invocationId

src/services/apis/waylaidwanderer-api.mjs

+4-4
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ export async function generateAnswersWithWaylaidwandererApi(port, question, sess
2323
body: JSON.stringify({
2424
message: question,
2525
stream: true,
26-
...(session.bingWeb_conversationSignature && {
26+
...(session.bingWeb_encryptedConversationSignature && {
2727
conversationId: session.bingWeb_conversationId,
28-
conversationSignature: session.bingWeb_conversationSignature,
28+
encryptedConversationSignature: session.bingWeb_encryptedConversationSignature,
2929
clientId: session.bingWeb_clientId,
3030
invocationId: session.bingWeb_invocationId,
3131
}),
@@ -51,8 +51,8 @@ export async function generateAnswersWithWaylaidwandererApi(port, question, sess
5151
}
5252
if (data.conversationId) session.conversationId = data.conversationId
5353
if (data.parentMessageId) session.parentMessageId = data.parentMessageId
54-
if (data.conversationSignature)
55-
session.bingWeb_conversationSignature = data.conversationSignature
54+
if (data.encryptedConversationSignature)
55+
session.bingWeb_encryptedConversationSignature = data.encryptedConversationSignature
5656
if (data.conversationId) session.bingWeb_conversationId = data.conversationId
5757
if (data.clientId) session.bingWeb_clientId = data.clientId
5858
if (data.invocationId) session.bingWeb_invocationId = data.invocationId

src/services/clients/bing/index.mjs

+27-11
Original file line numberDiff line numberDiff line change
@@ -118,23 +118,33 @@ export default class BingAIClient {
118118
// } else {
119119
// fetchOptions.dispatcher = new Agent({ connect: { timeout: 20_000 } })
120120
// }
121-
const response = await fetchBg(`${this.options.host}/turing/conversation/create`, fetchOptions)
121+
const response = await fetchBg(
122+
`${this.options.host}/turing/conversation/create?bundleVersion=1.864.15`,
123+
fetchOptions,
124+
)
122125
const body = await response.text()
123126
try {
124-
return JSON.parse(body)
127+
const res = JSON.parse(body)
128+
res.encryptedConversationSignature =
129+
response.headers.get('x-sydney-encryptedconversationsignature') ?? null
130+
return res
125131
} catch (err) {
126132
throw new Error(`/turing/conversation/create: failed to parse response body.\n${body}`)
127133
}
128134
}
129135

130-
async createWebSocketConnection() {
136+
async createWebSocketConnection(encryptedConversationSignature) {
131137
return new Promise((resolve, reject) => {
132138
// let agent
133139
// if (this.options.proxy) {
134140
// agent = new HttpsProxyAgent(this.options.proxy)
135141
// }
136142

137-
const ws = new WebSocket('wss://sydney.bing.com/sydney/ChatHub')
143+
const ws = new WebSocket(
144+
`wss://sydney.bing.com/sydney/ChatHub?sec_access_token=${encodeURIComponent(
145+
encryptedConversationSignature,
146+
)}`,
147+
)
138148

139149
ws.onerror = (err) => {
140150
reject(err)
@@ -201,7 +211,7 @@ export default class BingAIClient {
201211
let {
202212
jailbreakConversationId = false, // set to `true` for the first message to enable jailbreak mode
203213
conversationId,
204-
conversationSignature,
214+
encryptedConversationSignature,
205215
clientId,
206216
onProgress,
207217
} = opts
@@ -219,13 +229,18 @@ export default class BingAIClient {
219229
onProgress = () => {}
220230
}
221231

222-
if (jailbreakConversationId || !conversationSignature || !conversationId || !clientId) {
232+
if (
233+
jailbreakConversationId ||
234+
!encryptedConversationSignature ||
235+
!conversationId ||
236+
!clientId
237+
) {
223238
const createNewConversationResponse = await this.createNewConversation()
224239
if (this.debug) {
225240
console.debug(createNewConversationResponse)
226241
}
227242
if (
228-
!createNewConversationResponse.conversationSignature ||
243+
!createNewConversationResponse.encryptedConversationSignature ||
229244
!createNewConversationResponse.conversationId ||
230245
!createNewConversationResponse.clientId
231246
) {
@@ -240,7 +255,8 @@ export default class BingAIClient {
240255
)
241256
}
242257
// eslint-disable-next-line
243-
;({ conversationSignature, conversationId, clientId } = createNewConversationResponse)
258+
;({ encryptedConversationSignature, conversationId, clientId } =
259+
createNewConversationResponse)
244260
}
245261

246262
// Due to this jailbreak, the AI will occasionally start responding as the user. It only happens rarely (and happens with the non-jailbroken Bing too), but since we are handling conversations ourselves now, we can use this system to ignore the part of the generated message that is replying as the user.
@@ -319,7 +335,7 @@ export default class BingAIClient {
319335
conversation.messages.push(userMessage)
320336
}
321337

322-
const ws = await this.createWebSocketConnection()
338+
const ws = await this.createWebSocketConnection(encryptedConversationSignature)
323339

324340
ws.onerror = (error) => {
325341
console.error(error)
@@ -367,7 +383,7 @@ export default class BingAIClient {
367383
: message,
368384
messageType: jailbreakConversationId ? 'SearchQuery' : 'Chat',
369385
},
370-
conversationSignature,
386+
encryptedConversationSignature,
371387
participant: {
372388
id: clientId,
373389
},
@@ -604,7 +620,7 @@ export default class BingAIClient {
604620

605621
const returnData = {
606622
conversationId,
607-
conversationSignature,
623+
encryptedConversationSignature,
608624
clientId,
609625
invocationId: invocationId + 1,
610626
conversationExpiryTime,

src/services/init-session.mjs

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import { v4 as uuidv4 } from 'uuid'
1616
* @property {string|null} conversationId - chatGPT web mode
1717
* @property {string|null} messageId - chatGPT web mode
1818
* @property {string|null} parentMessageId - chatGPT web mode
19-
* @property {string|null} bingWeb_conversationSignature
19+
* @property {string|null} bingWeb_encryptedConversationSignature
2020
* @property {string|null} bingWeb_conversationId
2121
* @property {string|null} bingWeb_clientId
2222
* @property {string|null} bingWeb_invocationId
@@ -64,7 +64,7 @@ export function initSession({
6464
parentMessageId: null,
6565

6666
// bing
67-
bingWeb_conversationSignature: null,
67+
bingWeb_encryptedConversationSignature: null,
6868
bingWeb_conversationId: null,
6969
bingWeb_clientId: null,
7070
bingWeb_invocationId: null,

src/utils/fetch-bg.mjs

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export function fetchBg(input, init) {
2222
new Response(body, {
2323
status: response.status,
2424
statusText: response.statusText,
25+
headers: new Headers(response.headers),
2526
}),
2627
)
2728
}

0 commit comments

Comments
 (0)