fix: crypto undefined on some websites, leading to session creation failure (#231)

This commit is contained in:
josc146
2023-04-20 22:12:08 +08:00
parent 8af03398d5
commit 4fb5d735ea
6 changed files with 24 additions and 9 deletions
+9
View File
@@ -35,6 +35,7 @@
"remark-breaks": "^3.0.2",
"remark-gfm": "^3.0.1",
"remark-math": "^5.1.1",
"uuid": "^9.0.0",
"webextension-polyfill": "^0.10.0"
},
"devDependencies": {
@@ -8918,6 +8919,14 @@
"integrity": "sha512-EPD5q1uXyFxJpCrLnCc1nHnq3gOa6DZBocAIiI2TaSCA7VCJ1UJDMagCzIkXNsUYfD1daK//LTEQ8xiIbrHtcw==",
"dev": true
},
"node_modules/uuid": {
"version": "9.0.0",
"resolved": "https://registry.npmmirror.com/uuid/-/uuid-9.0.0.tgz",
"integrity": "sha512-MXcSTerfPa4uqyzStbRoTgt5XIe3x5+42+q1sDuy3R5MDk66URdLMOZe5aPX/SQd+kuYAh0FdP/pO28IkQyTeg==",
"bin": {
"uuid": "dist/bin/uuid"
}
},
"node_modules/uvu": {
"version": "0.5.6",
"resolved": "https://registry.npmmirror.com/uvu/-/uvu-0.5.6.tgz",
+1
View File
@@ -48,6 +48,7 @@
"remark-breaks": "^3.0.2",
"remark-gfm": "^3.0.1",
"remark-math": "^5.1.1",
"uuid": "^9.0.0",
"webextension-polyfill": "^0.10.0"
},
"devDependencies": {
+7 -5
View File
@@ -1,5 +1,7 @@
// https://github.com/waylaidwanderer/node-chatgpt-api
import { v4 as uuidv4 } from 'uuid'
/**
* https://stackoverflow.com/a/58326357
* @param {number} size
@@ -52,7 +54,7 @@ export default class BingAIClient {
'sec-fetch-dest': 'empty',
'sec-fetch-mode': 'cors',
'sec-fetch-site': 'same-origin',
'x-ms-client-request-id': crypto.randomUUID(),
'x-ms-client-request-id': uuidv4(),
'x-ms-useragent':
'azsdk-js-api-client-factory/1.0.0-beta.1 core-rest-pipeline/1.10.0 OS/Win32',
cookie: this.options.cookies || `_U=${this.options.userToken}`,
@@ -164,7 +166,7 @@ export default class BingAIClient {
invocationId = 0,
systemMessage,
context,
parentMessageId = jailbreakConversationId === true ? crypto.randomUUID() : null,
parentMessageId = jailbreakConversationId === true ? uuidv4() : null,
abortController = new AbortController(),
} = opts
@@ -201,7 +203,7 @@ export default class BingAIClient {
const stopToken = '\n\n[user](#message)'
if (jailbreakConversationId === true) {
jailbreakConversationId = crypto.randomUUID()
jailbreakConversationId = uuidv4()
}
const conversationKey = jailbreakConversationId
@@ -266,7 +268,7 @@ export default class BingAIClient {
}
const userMessage = {
id: crypto.randomUUID(),
id: uuidv4(),
parentMessageId,
role: 'User',
message,
@@ -503,7 +505,7 @@ export default class BingAIClient {
const { message: reply, conversationExpiryTime } = await messagePromise
const replyMessage = {
id: crypto.randomUUID(),
id: uuidv4(),
parentMessageId: userMessage.id,
role: 'Bing',
message: reply.text,
+3 -2
View File
@@ -1,4 +1,5 @@
import Browser from 'webextension-polyfill'
import { v4 as uuidv4 } from 'uuid'
import {
deleteConversation,
generateAnswersWithChatgptWebApi,
@@ -71,9 +72,9 @@ Browser.runtime.onConnect.addListener((port) => {
try {
if (chatgptWebModelKeys.includes(session.modelName)) {
const accessToken = await getChatGptAccessToken()
session.messageId = crypto.randomUUID()
session.messageId = uuidv4()
if (session.parentMessageId == null) {
session.parentMessageId = crypto.randomUUID()
session.parentMessageId = uuidv4()
}
await generateAnswersWithChatgptWebApi(port, session.question, session, accessToken)
} else if (bingWebModelKeys.includes(session.modelName)) {
+2 -1
View File
@@ -15,6 +15,7 @@ import { useTranslation } from 'react-i18next'
import DeleteButton from '../DeleteButton'
import { useConfig } from '../../hooks/use-config.mjs'
import { createSession } from '../../config/localSession.mjs'
import { v4 as uuidv4 } from 'uuid'
const logo = Browser.runtime.getURL('logo.png')
@@ -306,7 +307,7 @@ function ConversationCard(props) {
...session,
sessionName: new Date().toLocaleString(),
autoClean: false,
sessionId: crypto.randomUUID(),
sessionId: uuidv4(),
}
setSession(newSession)
createSession(newSession).then(() =>
+2 -1
View File
@@ -1,4 +1,5 @@
import { Models } from '../config/index.mjs'
import { v4 as uuidv4 } from 'uuid'
/**
* @typedef {object} Session
@@ -43,7 +44,7 @@ export function initSession({
conversationRecords,
sessionName,
sessionId: crypto.randomUUID(),
sessionId: uuidv4(),
aiName: modelName ? Models[modelName].desc : null,
modelName,