Skip to content

Commit

Permalink
feat: 🎸 New caching logic
Browse files Browse the repository at this point in the history
  • Loading branch information
haozi committed Sep 14, 2023
1 parent e4b2054 commit 22545ea
Show file tree
Hide file tree
Showing 11 changed files with 136 additions and 125 deletions.
122 changes: 61 additions & 61 deletions dist/chromium/app/assets/index.js

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions dist/chromium/background.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/chromium/content_script.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/chromium/inject.js

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

2 changes: 1 addition & 1 deletion dist/chromium/manifest.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "new-bing-anywhere",
"version": "2.4.5",
"version": "2.4.6",
"private": true,
"description": "New Bing isn't just for Edge anymore. Anywhere you want",
"homepage": "https://github.com/ha0z1/New-Bing-Anywhere",
Expand Down
17 changes: 12 additions & 5 deletions src/app/src/apis/_bing/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { ls, getConfig } from '@@/utils'

import {
createPropmt,
getFromConversation,
bingChatGetSocketId,
bingChatPing,
bingChatCreateSession,
Expand All @@ -15,15 +14,15 @@ import {
export const createBingChat = async (options: Bing.CreateBingChatOptions): Promise<Bing.CreateBingChatResponce | undefined> => {
const { prompt, onMessage, needRefresh, session } = options
if (!prompt) return

const promptKey = `Prompt-v1-${prompt.trim()}`
const prefix = 'Prompt-v2.4'
const promptKey = `${prefix}-${prompt.trim()}`

if (needRefresh) {
await ls.remove(promptKey)
}
const promptCache = await ls.get<Bing.ConversationOptions>(promptKey)
if (promptCache) {
const data = await getFromConversation(promptCache)
const data = promptCache.data
if (checkHasText(data)) {
return { data: data!, conversationOptions: promptCache }
}
Expand Down Expand Up @@ -79,13 +78,21 @@ export const createBingChat = async (options: Bing.CreateBingChatOptions): Promi
...finalSession,
conversationId,
encryptedConversationSignature
}
},
data: type2Data.item
}

if (conversationId && source && participantId && encryptedConversationSignature) {
ls.set<Bing.ConversationOptions>(promptKey, conversationOptions as Bing.ConversationOptions)
}

setTimeout(async () => {
const _1M = 1024 * 1024
const bytesInUse = await chrome.storage.local.getBytesInUse()
if (bytesInUse > _1M) {
chrome.storage.local.clear()
}
})
return {
data: type2Data.item,
conversationOptions
Expand Down
6 changes: 3 additions & 3 deletions src/app/src/apis/_bing/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,9 +230,9 @@ export const bingChatCloseWebSocket = async (socketId: number) => {
webSockets[socketId] = null
}

export const getFromConversation = async (options: Bing.ConversationOptions): Promise<Bing.CoreData | null> => {
return await callBackground('bing.getFromConversation', [options])
}
// export const getFromConversation = async (options: Bing.ConversationOptions): Promise<Bing.CoreData | null> => {
// return await callBackground('bing.getFromConversation', [options])
// }

export const checkHasText = (data?: Partial<Bing.CoreData> | null | undefined) => {
return data?.result?.value === 'Success' && !!data?.messages?.reverse().find((msg) => !msg.messageType && msg.author === 'bot')?.text
Expand Down
95 changes: 49 additions & 46 deletions src/background/listeners/_bing.ts
Original file line number Diff line number Diff line change
@@ -1,53 +1,56 @@
import { type Bing } from '@@/types'
import { v4 as uuidv4 } from 'uuid'

export const getFromConversation = async (options: Bing.ConversationOptions): Promise<Bing.CoreData | null> => {
const API =
'https://sydney.bing.com/sydney/GetConversation?' +
`conversationId=${encodeURIComponent(encodeURIComponent(options.session.conversationId))}&` +
`source=${encodeURIComponent(options.source)}&` +
`participantId=${encodeURIComponent(options.participantId)}&` +
`bundleVersion=1.864.15&` +
// `conversationSignature=${encodeURIComponent(options.session.conversationSignature)}&` +
`traceId=${uuidv4()}`
// const conversationsCache = {}
// export const getFromConversation = async (options: Bing.ConversationOptions): Promise<Bing.CoreData | null> => {
// const key = JSON.stringify(options)

try {
const data = await fetch(API, {
headers: {
accept: '*/*',
'accept-language': 'en-US,en;q=0.9',
authorization: `Bearer ${options.session.encryptedConversationSignature}`,
'content-type': 'application/json',
'sec-ch-ua': '"Not.A/Brand";v="8", "Chromium";v="114", "Microsoft Edge";v="114"',
'sec-ch-ua-full-version': '"114.0.1823.82"',
'sec-ch-ua-full-version-list': '"Not.A/Brand";v="8.0.0.0", "Chromium";v="114.0.5735.201", "Microsoft Edge";v="114.0.1823.82"',
'sec-ch-ua-mobile': '?0',
'sec-ch-ua-platform': '"macOS"',
'sec-fetch-dest': 'empty',
'sec-fetch-mode': 'cors',
'sec-fetch-site': 'same-site',
'sec-gpc': '1',
'sec-ms-gec-version': '1-114.0.1823.82'
},
referrer: 'https://www.bing.com/search?q=Bing+AI&showconv=1&FORM=hpcodx',
referrerPolicy: 'origin-when-cross-origin',
body: null,
method: 'GET',
mode: 'cors',
credentials: 'include'
}).then((r) => r.json())
// const data = await fetch(API, {
// headers: {
// Authorization: `Bearer ${options.session.encryptedConversationSignature}`
// }
// }).then((r) => r.json())
return data
} catch (err: unknown) {
return null
// const { message } = err as { message: string }
// throw new Error(`Failed to get conversation from ${API}: ${message}}`)
}
}
// const API =
// 'https://sydney.bing.com/sydney/GetConversation?' +
// `conversationId=${encodeURIComponent(encodeURIComponent(options.session.conversationId))}&` +
// `source=${encodeURIComponent(options.source)}&` +
// `participantId=${encodeURIComponent(options.participantId)}&` +
// `bundleVersion=1.864.15&` +
// // `conversationSignature=${encodeURIComponent(options.session.conversationSignature)}&` +
// `traceId=${uuidv4()}`

// try {
// const data = await fetch(API, {
// headers: {
// accept: '*/*',
// 'accept-language': 'en-US,en;q=0.9',
// authorization: `Bearer ${options.session.encryptedConversationSignature}`,
// 'content-type': 'application/json',
// 'sec-ch-ua': '"Not.A/Brand";v="8", "Chromium";v="114", "Microsoft Edge";v="114"',
// 'sec-ch-ua-full-version': '"114.0.1823.82"',
// 'sec-ch-ua-full-version-list': '"Not.A/Brand";v="8.0.0.0", "Chromium";v="114.0.5735.201", "Microsoft Edge";v="114.0.1823.82"',
// 'sec-ch-ua-mobile': '?0',
// 'sec-ch-ua-platform': '"macOS"',
// 'sec-fetch-dest': 'empty',
// 'sec-fetch-mode': 'cors',
// 'sec-fetch-site': 'same-site',
// 'sec-gpc': '1',
// 'sec-ms-gec-version': '1-114.0.1823.82'
// },
// referrer: 'https://www.bing.com/search?q=Bing+AI&showconv=1&FORM=hpcodx',
// referrerPolicy: 'origin-when-cross-origin',
// body: null,
// method: 'GET',
// mode: 'cors',
// credentials: 'include'
// }).then((r) => r.json())
// // const data = await fetch(API, {
// // headers: {
// // Authorization: `Bearer ${options.session.encryptedConversationSignature}`
// // }
// // }).then((r) => r.json())
// return data
// } catch (err: unknown) {
// return null
// // const { message } = err as { message: string }
// // throw new Error(`Failed to get conversation from ${API}: ${message}}`)
// }
// }

const webSockets: Record<string, WebSocket | null> = {}

Expand Down
4 changes: 2 additions & 2 deletions src/background/listeners/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getURL, version } from '@@/utils'
import { bingChatCloseWebSocket, bingChatPing, getFromConversation as bingGetFromConversation } from './_bing'
import { bingChatCloseWebSocket, bingChatPing /*, getFromConversation as bingGetFromConversation */ } from './_bing'
import { getNotification, hideNotification } from './_notification'

const getEnv = async () => {
Expand Down Expand Up @@ -57,7 +57,7 @@ export default {
getNotification,
hideNotification,

'bing.getFromConversation': bingGetFromConversation,
// 'bing.getFromConversation': bingGetFromConversation,
'bing.bingChatPing': bingChatPing,
'bing.bingChatCloseWebSocket': bingChatCloseWebSocket
}
1 change: 1 addition & 0 deletions src/universe/types/_bing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export interface ConversationOptions {
session: Session
source: string
participantId: string
data?: CoreData
}

export interface CoreData {
Expand Down

0 comments on commit 22545ea

Please sign in to comment.