diff --git a/src/background/providers/chatgpt.ts b/src/background/providers/chatgpt.ts index b38babc..f2ed289 100644 --- a/src/background/providers/chatgpt.ts +++ b/src/background/providers/chatgpt.ts @@ -437,6 +437,15 @@ export class ChatGPTProvider implements Provider { params.onEvent({ type: 'done' }) cleanup() return + } else { + try { + if (JSON.parse(message).error) { + console.error(JSON.parse(message).error) + return + } + } catch (err) { + console.log(err) + } } let data try { diff --git a/src/background/providers/chatgpt_new.ts b/src/background/providers/chatgpt_new.ts index 49b45e7..208c193 100644 --- a/src/background/providers/chatgpt_new.ts +++ b/src/background/providers/chatgpt_new.ts @@ -1,140 +1,140 @@ -import ExpiryMap from 'expiry-map' -import { v4 as uuidv4 } from 'uuid' -import { fetchSSE } from '../fetch-sse' -import { GenerateAnswerParams, Provider } from '../types' +// import ExpiryMap from 'expiry-map' +// import { v4 as uuidv4 } from 'uuid' +// import { fetchSSE } from '../fetch-sse' +// import { GenerateAnswerParams, Provider } from '../types' -async function request(token: string, method: string, path: string, data?: 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), - }) -} +// async function request(token: string, method: string, path: string, data?: 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), +// }) +// } -export async function sendMessageFeedback(token: string, data: unknown) { - await request(token, 'POST', '/conversation/message_feedback', data) -} +// export async function sendMessageFeedback(token: string, data: unknown) { +// await request(token, 'POST', '/conversation/message_feedback', data) +// } -export async function setConversationProperty( - token: string, - conversationId: string, - propertyObject: object, -) { - await request(token, 'PATCH', `/conversation/${conversationId}`, propertyObject) -} +// export async function setConversationProperty( +// token: string, +// conversationId: string, +// propertyObject: object, +// ) { +// await request(token, 'PATCH', `/conversation/${conversationId}`, propertyObject) +// } -const KEY_ACCESS_TOKEN = 'accessToken' +// const KEY_ACCESS_TOKEN = 'accessToken' -const cache = new ExpiryMap(10 * 1000) +// const cache = new ExpiryMap(10 * 1000) -export async function getChatGPTAccessToken(): Promise { - if (cache.get(KEY_ACCESS_TOKEN)) { - return cache.get(KEY_ACCESS_TOKEN) - } - const resp = await fetch('https://chat.openai.com/api/auth/session') - if (resp.status === 403) { - throw new Error('CLOUDFLARE') - } - const data = await resp.json().catch(() => ({})) - if (!data.accessToken) { - throw new Error('UNAUTHORIZED') - } - cache.set(KEY_ACCESS_TOKEN, data.accessToken) - return data.accessToken -} +// export async function getChatGPTAccessToken(): Promise { +// if (cache.get(KEY_ACCESS_TOKEN)) { +// return cache.get(KEY_ACCESS_TOKEN) +// } +// const resp = await fetch('https://chat.openai.com/api/auth/session') +// if (resp.status === 403) { +// throw new Error('CLOUDFLARE') +// } +// const data = await resp.json().catch(() => ({})) +// if (!data.accessToken) { +// throw new Error('UNAUTHORIZED') +// } +// cache.set(KEY_ACCESS_TOKEN, data.accessToken) +// return data.accessToken +// } -export class ChatGPTProvider implements Provider { - constructor(private token: string) { - this.token = token - } +// export class ChatGPTProvider implements Provider { +// constructor(private token: string) { +// this.token = token +// } - private async fetchModels(): Promise< - { slug: string; title: string; description: string; max_tokens: number }[] - > { - const resp = await request(this.token, 'GET', '/models').then((r) => r.json()) - return resp.models - } +// private async fetchModels(): Promise< +// { slug: string; title: string; description: string; max_tokens: number }[] +// > { +// const resp = await request(this.token, 'GET', '/models').then((r) => r.json()) +// return resp.models +// } - private async getModelName(): Promise { - try { - const models = await this.fetchModels() - return models[0].slug - } catch (err) { - console.error(err) - return 'text-davinci-002-render' - } - } +// private async getModelName(): Promise { +// try { +// const models = await this.fetchModels() +// return models[0].slug +// } catch (err) { +// console.error(err) +// return 'text-davinci-002-render' +// } +// } - async generateAnswer(params: GenerateAnswerParams) { - let conversationId: string | undefined +// async generateAnswer(params: GenerateAnswerParams) { +// let conversationId: string | undefined - const cleanup = () => { - if (conversationId) { - setConversationProperty(this.token, conversationId, { is_visible: false }) - } - } +// const cleanup = () => { +// if (conversationId) { +// setConversationProperty(this.token, conversationId, { is_visible: false }) +// } +// } - const modelName = await this.getModelName() - console.log('Using model:', modelName, params.conversationId, params.parentMessageId) +// const modelName = await this.getModelName() +// console.log('Using model:', modelName, params.conversationId, params.parentMessageId) - await fetchSSE('https://chat.openai.com/backend-api/conversation', { - method: 'POST', - signal: params.signal, - headers: { - 'Content-Type': 'application/json', - Authorization: `Bearer ${this.token}`, - }, - body: JSON.stringify({ - action: 'next', - messages: [ - { - id: uuidv4(), - role: 'user', - content: { - content_type: 'text', - parts: [params.prompt], - }, - }, - ], - model: modelName, - parent_message_id: params.parentMessageId || uuidv4(), - conversation_id: params.conversationId, - arkose_token: params.arkoseToken, - }), - onMessage(message: string) { - console.debug('sse message', message) - if (message === '[DONE]') { - params.onEvent({ type: 'done' }) - cleanup() - return - } - let data - 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) - return - } - const text = data.message?.content?.parts?.[0] + '✏' - if (text) { - conversationId = data.conversation_id - params.onEvent({ - type: 'answer', - data: { - text, - messageId: data.message.id, - conversationId: data.conversation_id, - parentMessageId: data.parent_message_id, - }, - }) - } - }, - }) - return { cleanup } - } -} +// await fetchSSE('https://chat.openai.com/backend-api/conversation', { +// method: 'POST', +// signal: params.signal, +// headers: { +// 'Content-Type': 'application/json', +// Authorization: `Bearer ${this.token}`, +// }, +// body: JSON.stringify({ +// action: 'next', +// messages: [ +// { +// id: uuidv4(), +// role: 'user', +// content: { +// content_type: 'text', +// parts: [params.prompt], +// }, +// }, +// ], +// model: modelName, +// parent_message_id: params.parentMessageId || uuidv4(), +// conversation_id: params.conversationId, +// arkose_token: params.arkoseToken, +// }), +// onMessage(message: string) { +// console.debug('sse message', message) +// if (message === '[DONE]') { +// params.onEvent({ type: 'done' }) +// cleanup() +// return +// } +// let data +// 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) +// return +// } +// const text = data.message?.content?.parts?.[0] + '✏' +// if (text) { +// conversationId = data.conversation_id +// params.onEvent({ +// type: 'answer', +// data: { +// text, +// messageId: data.message.id, +// conversationId: data.conversation_id, +// parentMessageId: data.parent_message_id, +// }, +// }) +// } +// }, +// }) +// return { cleanup } +// } +// }