Skip to content

Commit

Permalink
fix: crypto undefined on some websites, leading to session creation f…
Browse files Browse the repository at this point in the history
…ailure (#231)
  • Loading branch information
josStorer committed Apr 20, 2023
1 parent 7c02c84 commit bd824b3
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 9 deletions.
9 changes: 9 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
12 changes: 7 additions & 5 deletions src/background/clients/bing/index.mjs
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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}`,
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -266,7 +268,7 @@ export default class BingAIClient {
}

const userMessage = {
id: crypto.randomUUID(),
id: uuidv4(),
parentMessageId,
role: 'User',
message,
Expand Down Expand Up @@ -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,
Expand Down
5 changes: 3 additions & 2 deletions src/background/index.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Browser from 'webextension-polyfill'
import { v4 as uuidv4 } from 'uuid'
import {
deleteConversation,
generateAnswersWithChatgptWebApi,
Expand Down Expand Up @@ -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)) {
Expand Down
3 changes: 2 additions & 1 deletion src/components/ConversationCard/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down Expand Up @@ -306,7 +307,7 @@ function ConversationCard(props) {
...session,
sessionName: new Date().toLocaleString(),
autoClean: false,
sessionId: crypto.randomUUID(),
sessionId: uuidv4(),
}
setSession(newSession)
createSession(newSession).then(() =>
Expand Down
3 changes: 2 additions & 1 deletion src/utils/init-session.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Models } from '../config/index.mjs'
import { v4 as uuidv4 } from 'uuid'

/**
* @typedef {object} Session
Expand Down Expand Up @@ -43,7 +44,7 @@ export function initSession({
conversationRecords,

sessionName,
sessionId: crypto.randomUUID(),
sessionId: uuidv4(),

aiName: modelName ? Models[modelName].desc : null,
modelName,
Expand Down

0 comments on commit bd824b3

Please sign in to comment.