Skip to content

Commit

Permalink
fix: 调整对高版本HttpsProxyAgent支持的一个错误
Browse files Browse the repository at this point in the history
  • Loading branch information
ikechan8370 committed Jul 31, 2023
1 parent 9c172e9 commit 4ffc6c1
Showing 1 changed file with 18 additions and 15 deletions.
33 changes: 18 additions & 15 deletions utils/SydneyAIClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ if (!globalThis.fetch) {
// workaround for ver 7.x and ver 5.x
let proxy = HttpsProxyAgent
if (typeof proxy !== 'function') {
proxy = HttpsProxyAgent.HttpsProxyAgent
proxy = (p) => {
return new HttpsProxyAgent.HttpsProxyAgent(p)
}
}

async function getKeyv () {
Expand All @@ -41,7 +43,7 @@ async function getKeyv () {
const genRanHex = (size) => [...Array(size)].map(() => Math.floor(Math.random() * 16).toString(16)).join('')

export default class SydneyAIClient {
constructor(opts) {
constructor (opts) {
this.opts = {
...opts,
host: opts.host || Config.sydneyReverseProxy || 'https://edgeservices.bing.com/edgesvc'
Expand All @@ -52,7 +54,7 @@ export default class SydneyAIClient {
this.debug = opts.debug
}

async initCache() {
async initCache () {
if (!this.conversationsCache) {
const cacheOptions = this.opts.cache || {}
cacheOptions.namespace = cacheOptions.namespace || 'bing'
Expand All @@ -61,7 +63,7 @@ export default class SydneyAIClient {
}
}

async createNewConversation() {
async createNewConversation () {
await this.initCache()
const fetchOptions = {
headers: {
Expand Down Expand Up @@ -126,7 +128,7 @@ export default class SydneyAIClient {
}
}

async createWebSocketConnection() {
async createWebSocketConnection () {
await this.initCache()
// let WebSocket = await getWebSocket()
return new Promise((resolve, reject) => {
Expand Down Expand Up @@ -190,13 +192,13 @@ export default class SydneyAIClient {
})
}

async cleanupWebSocketConnection(ws) {
async cleanupWebSocketConnection (ws) {
clearInterval(ws.bingPingInterval)
ws.close()
ws.removeAllListeners()
}

async sendMessage(
async sendMessage (
message,
opts = {}
) {
Expand Down Expand Up @@ -572,9 +574,9 @@ export default class SydneyAIClient {
const message = messages.length
? messages[messages.length - 1]
: {
adaptiveCards: adaptiveCardsSoFar,
text: replySoFar.join('')
}
adaptiveCards: adaptiveCardsSoFar,
text: replySoFar.join('')
}
if (messages[0].contentOrigin === 'Apology') {
console.log('Apology found')
if (!replySoFar[0]) {
Expand Down Expand Up @@ -639,9 +641,9 @@ export default class SydneyAIClient {
const message = messages.length
? messages[messages.length - 1]
: {
adaptiveCards: adaptiveCardsSoFar,
text: replySoFar.join('')
}
adaptiveCards: adaptiveCardsSoFar,
text: replySoFar.join('')
}
// 获取到图片内容
if (message.contentType === 'IMAGE') {
message.imageTag = messages.filter(m => m.contentType === 'IMAGE').map(m => m.text).join('')
Expand Down Expand Up @@ -807,14 +809,15 @@ export default class SydneyAIClient {
return false
}
}

/**
* Iterate through messages, building an array based on the parentMessageId.
* Each message has an id and a parentMessageId. The parentMessageId is the id of the message that this message is a reply to.
* @param messages
* @param parentMessageId
* @returns {*[]} An array containing the messages in the order they should be displayed, starting with the root message.
*/
static getMessagesForConversation(messages, parentMessageId) {
static getMessagesForConversation (messages, parentMessageId) {
const orderedMessages = []
let currentMessageId = parentMessageId
while (currentMessageId) {
Expand All @@ -830,7 +833,7 @@ export default class SydneyAIClient {
}
}

async function generateRandomIP() {
async function generateRandomIP () {
let ip = await redis.get('CHATGPT:BING_IP')
if (ip) {
return ip
Expand Down

0 comments on commit 4ffc6c1

Please sign in to comment.