From 173573d684b55df714c35c63e5934d335feeb7ba Mon Sep 17 00:00:00 2001 From: Vaayne Date: Fri, 2 Jun 2023 22:21:55 +0800 Subject: [PATCH] bugfix and change model for chatgot web --- CHANGELOG.md | 6 +++ package.json | 2 +- src/components/combobox.tsx | 5 +- src/content.tsx | 15 +++++- src/lib/api/bard.ts | 2 + src/lib/api/bing.ts | 2 + src/lib/api/chatgpt-api.ts | 1 + src/lib/api/chatgpt-web.ts | 86 +++++++++++++++----------------- src/lib/api/notion-completion.ts | 2 + 9 files changed, 70 insertions(+), 51 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index beb7a7d..f6cffb8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +## v0.0.22 - 2023-06-02 +### Fixed +- Resolved authentication issue on ChatGPT Web +- Added animation to send button for better user experience +- Revised ChatGPT Mobile model to text-davinci-002-render-sha-mobile, improving speed and overall performance. + ## v0.0.21 - 2023-06-01 ### Fixed - Fix NotionAI not working issue diff --git a/package.json b/package.json index f1268b1..08b31f1 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "notionai-plus", "displayName": "NotionAI Plus", - "version": "0.0.21", + "version": "0.0.22", "description": "NotionAI Plus is a browser extension that brings the power of NotionAI to any website you visit.", "scripts": { "dev": "plasmo dev", diff --git a/src/components/combobox.tsx b/src/components/combobox.tsx index 83ecbbf..21fd343 100644 --- a/src/components/combobox.tsx +++ b/src/components/combobox.tsx @@ -189,7 +189,10 @@ export default function ComboxComponent() { diff --git a/src/content.tsx b/src/content.tsx index 3fb1334..7905f5e 100644 --- a/src/content.tsx +++ b/src/content.tsx @@ -72,7 +72,11 @@ const Index = () => { } streamPort.onMessage.addListener(function (msg) { - setResponseMessage(msg) + if (msg === "[DONE]") { + setIsLoading(false) + } else { + setResponseMessage(msg) + } }) // init on page load @@ -130,7 +134,11 @@ const Index = () => { handleToast("Please input context") return } - // setIsLoading(true) + if (isLoading) { + handleToast("AI is processing, please wait") + return + } + setIsLoading(true) let lprompt: string = "" let language: string = "" @@ -165,6 +173,9 @@ const Index = () => { } streamPort.postMessage(body) + // // wait 3 seconds + // await new Promise((resolve) => setTimeout(resolve, 5000)) + // setIsLoading(false) } const handleCopy = async () => { diff --git a/src/lib/api/bard.ts b/src/lib/api/bard.ts index 5b42298..bdcd4ab 100644 --- a/src/lib/api/bard.ts +++ b/src/lib/api/bard.ts @@ -75,5 +75,7 @@ export async function BardChat(prompt: string, port: chrome.runtime.Port) { port.postMessage( "Sorry, Bard Chat is not available at the moment. error: " + err.message ) + } finally { + port.postMessage("[DONE]") } } diff --git a/src/lib/api/bing.ts b/src/lib/api/bing.ts index 4e6fd93..b3530a7 100644 --- a/src/lib/api/bing.ts +++ b/src/lib/api/bing.ts @@ -257,6 +257,7 @@ async function chat(prompt: string, port: chrome.runtime.Port) { } else if (event.type === 3) { wsp.removeAllListeners() wsp.close() + port.postMessage("[DONE]") } else if (event.type === 1) { if (event.arguments[0].messages) { const text = convertMessageToMarkdown(event.arguments[0].messages[0]) @@ -290,5 +291,6 @@ export async function BingChat(prompt: string, port: chrome.runtime.Port) { port.postMessage( "Sorry, Bing Chat is not available at the moment. error: " + err.message ) + port.postMessage("[DONE]") } } diff --git a/src/lib/api/chatgpt-api.ts b/src/lib/api/chatgpt-api.ts index fc7b57e..ede4fe6 100644 --- a/src/lib/api/chatgpt-api.ts +++ b/src/lib/api/chatgpt-api.ts @@ -36,6 +36,7 @@ async function chat( await parseSSEResponse(resp, (message) => { if (message === "[DONE]") { + port.postMessage("[DONE]") return } try { diff --git a/src/lib/api/chatgpt-web.ts b/src/lib/api/chatgpt-web.ts index 77beff2..c263ac6 100644 --- a/src/lib/api/chatgpt-web.ts +++ b/src/lib/api/chatgpt-web.ts @@ -3,54 +3,34 @@ import { v4 as uuidv4 } from "uuid" import { storage } from "~lib/storage" import { parseSSEResponse } from "~lib/utils/sse" -const CHATGPT_MODEL = "text-davinci-002-render-sha" +const CHATGPT_MODEL = "text-davinci-002-render-sha-mobile" const CHATGPT_HOST = "https://chat.openai.com" const CACHE_KEY_TOKEN = "chatgpt-token" const CACHE_KEY_CONVERSATION_ID = "chatgpt-conversation-id" -async function getAccessToken(): Promise { - const cacheToken = await storage.get(CACHE_KEY_TOKEN) - if (cacheToken) { - return cacheToken as string - } - +async function getAccessToken() { const resp = await fetch(`${CHATGPT_HOST}/api/auth/session`) - if (resp.status === 401 || resp.status === 403) { - throw new Error( - "401 UNAUTHORIZED, Please login to https://chat.openai.com/" - ) - } - if (!resp.ok) { - throw new Error(`ChatGPT return error, status: ${resp.status}`) - } - - const data = await resp.json() - if (!data.accessToken) { - throw new Error("401 UNAUTHORIZED") + if (resp.ok) { + const data = await resp.json() + if (data.accessToken) { + await storage.set(CACHE_KEY_TOKEN, data.accessToken) + return + } } - await storage.set(CACHE_KEY_TOKEN, data.accessToken) - return data.accessToken + throw new Error(`${resp.status}, Please login to https://chat.openai.com/`) } async function ChatGPTWebChat(prompt: string, port: chrome.runtime.Port) { - let message = "" - for (let i = 0; i < 3; i++) { - try { - await chat(prompt, port) - return - } catch (err) { - await storage.remove(CACHE_KEY_TOKEN) - console.error(err) - message = err.message - } + try { + return await chat(prompt, port) + } catch (err) { + console.error(err) + port.postMessage(err.message) } - port.postMessage(message) } async function chat(prompt: string, port: chrome.runtime.Port) { - const accessToken = await getAccessToken() - const cacheConversationId = await storage.get(CACHE_KEY_CONVERSATION_ID) if (!cacheConversationId) { await storage.set(CACHE_KEY_CONVERSATION_ID, uuidv4()) @@ -70,19 +50,30 @@ async function chat(prompt: string, port: chrome.runtime.Port) { model: CHATGPT_MODEL } const url = `${CHATGPT_HOST}/backend-api/conversation` - const resp = await fetch(url, { - method: "POST", - headers: { - "Content-Type": "application/json", - Authorization: `Bearer ${accessToken}` - }, - body: JSON.stringify(data) - }) - if (!resp.ok) { - const errMsg = `ChatGPT return error, status: ${resp.status}` - console.error(errMsg) - throw new Error(errMsg) + const sendRequest = async (): Promise => { + const accessToken = await storage.get(CACHE_KEY_TOKEN) + return await fetch(url, { + method: "POST", + headers: { + "Content-Type": "application/json", + Authorization: `Bearer ${accessToken}` + }, + body: JSON.stringify(data) + }) + } + + let resp = await sendRequest() + + if (resp.status === 401 || resp.status === 403) { + try { + await getAccessToken() + resp = await sendRequest() + } catch (err) { + console.error(err) + port.postMessage(err.message) + return + } } let conversationId: string = "" @@ -91,6 +82,7 @@ async function chat(prompt: string, port: chrome.runtime.Port) { if (message === "[DONE]") { // console.debug("chatgpt sse message done, start remove conversation") removeConversation(conversationId) + port.postMessage("[DONE]") return } try { @@ -110,7 +102,7 @@ async function chat(prompt: string, port: chrome.runtime.Port) { } async function removeConversation(id: string) { - const accessToken = await getAccessToken() + const accessToken = await storage.get(CACHE_KEY_TOKEN) try { await fetch(`${CHATGPT_HOST}/backend-api/conversation/${id}`, { method: "PATCH", diff --git a/src/lib/api/notion-completion.ts b/src/lib/api/notion-completion.ts index 73055fe..1174da8 100644 --- a/src/lib/api/notion-completion.ts +++ b/src/lib/api/notion-completion.ts @@ -112,6 +112,8 @@ async function NotionCompletion( } catch (err) { console.error(err) message = err.message + } finally { + port.postMessage("[DONE]") } } port.postMessage(message)