Skip to content

Commit ea35390

Browse files
committed
Fix bing with sec_access_token
1 parent 729d846 commit ea35390

3 files changed

Lines changed: 30 additions & 11 deletions

File tree

src/app/bots/bing/api.ts

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { random } from 'lodash-es'
2-
import { FetchError, ofetch } from 'ofetch'
2+
import { FetchError, FetchResponse, ofetch } from 'ofetch'
33
import { uuid } from '~utils'
44
import { ChatError, ErrorCode } from '~utils/errors'
55
import { ConversationResponse } from './types'
@@ -17,29 +17,38 @@ export async function createConversation(): Promise<ConversationResponse> {
1717
'x-ms-useragent': 'azsdk-js-api-client-factory/1.0.0-beta.1 core-rest-pipeline/1.10.3 OS/macOS',
1818
}
1919

20-
let resp: ConversationResponse
20+
let rawResponse: FetchResponse<ConversationResponse>
2121
try {
22-
resp = await ofetch(API_ENDPOINT, { headers, redirect: 'error' })
23-
if (!resp.result) {
22+
rawResponse = await ofetch.raw<ConversationResponse>(API_ENDPOINT, { headers, redirect: 'error' })
23+
if (!rawResponse._data?.result) {
2424
throw new Error('Invalid response')
2525
}
2626
} catch (err) {
2727
console.error('retry bing create', err)
28-
resp = await ofetch(API_ENDPOINT, {
28+
rawResponse = await ofetch.raw<ConversationResponse>(API_ENDPOINT, {
2929
headers: { ...headers, 'x-forwarded-for': randomIP() },
3030
redirect: 'error',
3131
})
32-
if (!resp) {
32+
if (!rawResponse._data) {
3333
throw new FetchError(`Failed to fetch (${API_ENDPOINT})`)
3434
}
3535
}
3636

37-
if (resp.result.value !== 'Success') {
38-
const message = `${resp.result.value}: ${resp.result.message}`
39-
if (resp.result.value === 'UnauthorizedRequest') {
37+
const data = rawResponse._data
38+
39+
if (data.result.value !== 'Success') {
40+
const message = `${data.result.value}: ${data.result.message}`
41+
if (data.result.value === 'UnauthorizedRequest') {
4042
throw new ChatError(message, ErrorCode.BING_UNAUTHORIZED)
4143
}
4244
throw new Error(message)
4345
}
44-
return resp
46+
47+
const conversationSignature = rawResponse.headers.get('x-sydney-conversationsignature')!
48+
const encryptedConversationSignature = rawResponse.headers.get('x-sydney-encryptedconversationsignature') || undefined
49+
50+
data.conversationSignature = data.conversationSignature || conversationSignature
51+
data.encryptedConversationSignature = encryptedConversationSignature
52+
53+
return data
4554
}

src/app/bots/bing/index.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ export class BingWebBot extends AbstractBot {
107107
this.conversationContext = {
108108
conversationId: conversation.conversationId,
109109
conversationSignature: conversation.conversationSignature,
110+
encryptedConversationSignature: conversation.encryptedConversationSignature,
110111
clientId: conversation.clientId,
111112
invocationId: 0,
112113
conversationStyle: bingConversationStyle,
@@ -120,7 +121,7 @@ export class BingWebBot extends AbstractBot {
120121
imageUrl = await this.uploadImage(params.image)
121122
}
122123

123-
const wsp = new WebSocketAsPromised('wss://sydney.bing.com/sydney/ChatHub', {
124+
const wsp = new WebSocketAsPromised(this.buildWssUrl(conversation.encryptedConversationSignature), {
124125
packMessage: websocketUtils.packMessage,
125126
unpackMessage: websocketUtils.unpackMessage,
126127
})
@@ -245,4 +246,11 @@ export class BingWebBot extends AbstractBot {
245246
}
246247
return `https://www.bing.com/images/blob?bcid=${resp.blobId}`
247248
}
249+
250+
private buildWssUrl(encryptedConversationSignature: string | undefined) {
251+
if (!encryptedConversationSignature) {
252+
return 'wss://sydney.bing.com/sydney/ChatHub'
253+
}
254+
return `wss://sydney.bing.com/sydney/ChatHub?sec_access_token=${encodeURIComponent(encryptedConversationSignature)}`
255+
}
248256
}

src/app/bots/bing/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ export interface ConversationResponse {
44
conversationId: string
55
clientId: string
66
conversationSignature: string
7+
encryptedConversationSignature?: string
78
result: {
89
value: string
910
message: null
@@ -28,6 +29,7 @@ export interface ConversationInfo {
2829
conversationSignature: string
2930
invocationId: number
3031
conversationStyle: BingConversationStyle
32+
encryptedConversationSignature?: string
3133
}
3234

3335
export interface BingChatResponse {

0 commit comments

Comments
 (0)