Skip to content

Commit

Permalink
revered to old chatgpt provider
Browse files Browse the repository at this point in the history
  • Loading branch information
ishandutta2007 committed Jul 23, 2023
1 parent e899f8f commit 505024f
Show file tree
Hide file tree
Showing 6 changed files with 306 additions and 30 deletions.
4 changes: 2 additions & 2 deletions build.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ async function runEsbuild() {
bundle: true,
outdir: outdir,
treeShaking: true,
minify: true,
drop: ['console', 'debugger'],
// minify: true,
// drop: ['console', 'debugger'],
legalComments: 'none',
define: {
'process.env.NODE_ENV': '"production"',
Expand Down
48 changes: 27 additions & 21 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 @@ -12,6 +12,7 @@
"@geist-ui/core": "^2.3.8",
"@geist-ui/icons": "^1.0.2",
"@primer/octicons-react": "^17.9.0",
"dayjs": "^1.11.9",
"eventsource-parser": "^0.0.5",
"expiry-map": "^2.0.0",
"github-markdown-css": "^5.1.0",
Expand Down
140 changes: 133 additions & 7 deletions src/background/providers/chatgpt.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import dayjs from 'dayjs'
import ExpiryMap from 'expiry-map'
import { v4 as uuidv4 } from 'uuid'
import { ADAY, APPSHORTNAME, HALFHOUR } from '../../utils/consts'
import { fetchSSE } from '../fetch-sse'
import { GenerateAnswerParams, Provider } from '../types'
dayjs().format()

async function request(token: string, method: string, path: string, data?: unknown) {
return fetch(`https://chat.openai.com/backend-api${path}`, {
Expand All @@ -14,6 +17,34 @@ async function request(token: string, method: string, path: string, data?: unkno
})
}

async function request_new(
token: string,
method: string,
path: string,
data?: unknown,
callback?: unknown,
) {
return fetch(`https://chat.openai.com/backend-api${path}`, {
method,
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${token}`,
},
body: data === undefined ? undefined : JSON.stringify(data),
})
.then(function (response) {
console.log('fetch', token != null, method, path, 'response', response)
return response.json()
})
.then(function (data) {
console.log('response data', data)
if (callback) callback(token, data)
})
.catch((error) => {
console.error('fetch', token, method, path, 'error', error)
})
}

export async function sendMessageFeedback(token: string, data: unknown) {
await request(token, 'POST', '/conversation/message_feedback', data)
}
Expand All @@ -26,6 +57,61 @@ export async function setConversationProperty(
await request(token, 'PATCH', `/conversation/${conversationId}`, propertyObject)
}

const browsertabIdConversationIdMap = new Map()
const windowIdConversationIdMap = new Map()

function deleteRecentConversations(token, data) {
const now = dayjs()
const startTime = dayjs(performance.timeOrigin)
console.log('startTime', startTime)
const convs = data.items
console.log('convs', convs)
for (let i = 0; i < convs.length; i++) {
const conv_i_time = dayjs(convs[i].create_time)
console.log(
'conv' + i,
convs[i].id,
conv_i_time,
conv_i_time - startTime,
now - conv_i_time,
now - conv_i_time < ADAY,
)
if (
HALFHOUR < now - conv_i_time &&
now - conv_i_time < ADAY &&
convs[i].title.indexOf(APPSHORTNAME + ':') != -1
) {
setTimeout(function () {
console.log('Deleting', token != null, convs[i].id)
setConversationProperty(token, convs[i].id, { is_visible: false })
const cloneBTCMap = new Map(browsertabIdConversationIdMap)
cloneBTCMap.forEach((ConversationId, tabId, map) => {
console.log('Looking for', ConversationId, tabId, 'in', map)
if (ConversationId == convs[i].id) {
console.log('Deleting ', ConversationId, tabId, 'from', map)
browsertabIdConversationIdMap.delete(tabid)
console.log(
'browsertabIdConversationIdMap after Deleting ',
browsertabIdConversationIdMap,
)
}
})
const cloneWCMap = new Map(windowIdConversationIdMap)
cloneWCMap.forEach((conversationIdsConcatinated, windowId, map) => {
console.log('Looking for', conversationIdsConcatinated, windowId, 'in', map)
if (conversationIdsConcatinated.indexOf(convs[i].id) != -1) {
console.log('Deleting ', convs[i].id, windowId, 'from', map)
conversationIdsConcatinated = conversationIdsConcatinated.replace(convs[i].id, '')
conversationIdsConcatinated = conversationIdsConcatinated.replace(',,', ',')
windowIdConversationIdMap.set(windowid, conversationIdsConcatinated)
console.log('windowIdConversationIdMap after Deleting ', windowIdConversationIdMap)
}
})
}, i * 1000)
}
}
}

const KEY_ACCESS_TOKEN = 'accessToken'

const cache = new ExpiryMap(10 * 1000)
Expand All @@ -49,6 +135,14 @@ export async function getChatGPTAccessToken(): Promise<string> {
export class ChatGPTProvider implements Provider {
constructor(private token: string) {
this.token = token
//Brute:
request_new(
token,
'GET',
'/conversations?offset=0&limit=100&order=updated',
undefined,
deleteRecentConversations,
)
}

private async fetchModels(): Promise<
Expand All @@ -71,14 +165,43 @@ export class ChatGPTProvider implements Provider {
async generateAnswer(params: GenerateAnswerParams) {
let conversationId: string | undefined

const countWords = (text) => {
return text.trim().split(/\s+/).length
}

const getConversationTitle = (bigtext: string) => {
let ret = bigtext.split('\n', 1)[0]
console.log('ret', ret)
try {
ret = ret.split('of the problem:')[1]
} catch (e) {
console.log(e)
}
ret = ret.split('.', 1)[0]
// console.log('ret',ret)
// try {
// ret = APPSHORTNAME + ':' + ret.split(':')[1].trim()
// } catch (e) {
// console.log(e)
// ret = APPSHORTNAME + ':' + ret.trim().slice(0, 8) + '..'
// }
console.log('ret', ret)
return APPSHORTNAME + ':' + ret
}

const renameConversationTitle = (convId: string) => {
const titl: string = getConversationTitle(params.prompt)
console.log('renameConversationTitle:', this.token, convId, titl)
setConversationProperty(this.token, convId, { title: titl })
}
const cleanup = () => {
if (conversationId) {
setConversationProperty(this.token, conversationId, { is_visible: false })
// setConversationProperty(this.token, conversationId, { is_visible: false })
}
}

const modelName = await this.getModelName()
console.log('Using model:', modelName, params.conversationId, params.parentMessageId)
console.debug('Using model:', modelName)

await fetchSSE('https://chat.openai.com/backend-api/conversation', {
method: 'POST',
Expand Down Expand Up @@ -114,21 +237,24 @@ export class ChatGPTProvider implements Provider {
try {
data = JSON.parse(message)
} catch (err) {
if (new Date(message) !== 'Invalid Date' && !isNaN(new Date(message)))
console.debug('This is known issue')
else console.error(err)
console.error(err)
return
}
const text = data.message?.content?.parts?.[0] + '✏'
const text = data.message?.content?.parts?.[0]
if (text) {
if (countWords(text) == 1 && data.message?.author?.role == 'assistant') {
if (params.prompt.indexOf('of the problem:') !== -1) {
renameConversationTitle(data.conversation_id)
}
}
conversationId = data.conversation_id
params.onEvent({
type: 'answer',
data: {
text,
messageId: data.message.id,
conversationId: data.conversation_id,
parentMessageId: data.parent_message_id,
conversationId: data.conversation_id,
},
})
}
Expand Down
Loading

0 comments on commit 505024f

Please sign in to comment.