Skip to content

Commit

Permalink
bugfix and change model for chatgot web
Browse files Browse the repository at this point in the history
  • Loading branch information
vaayne committed Jun 2, 2023
1 parent fd3bf4f commit 173573d
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 51 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
5 changes: 4 additions & 1 deletion src/components/combobox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,10 @@ export default function ComboxComponent() {
<button
type="button"
onClick={handleMessage}
className="rounded border border-transparent bg-indigo-600 px-2.5 py-1.5 text-xs font-medium text-white shadow-sm hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-indigo-500 focus:ring-offset-2">
disabled={isLoading}
className={`rounded border border-transparent bg-indigo-600 px-2.5 py-1.5 text-xs font-medium text-white shadow-sm hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-indigo-500 focus:ring-offset-2 ${
isLoading ? "opacity-30" : ""
}`}>
<Send size={16} className={isLoading ? "animate-spin" : ""} />
</button>
</div>
Expand Down
15 changes: 13 additions & 2 deletions src/content.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 = ""
Expand Down Expand Up @@ -165,6 +173,9 @@ const Index = () => {
}

streamPort.postMessage(body)
// // wait 3 seconds
// await new Promise((resolve) => setTimeout(resolve, 5000))
// setIsLoading(false)
}

const handleCopy = async () => {
Expand Down
2 changes: 2 additions & 0 deletions src/lib/api/bard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]")
}
}
2 changes: 2 additions & 0 deletions src/lib/api/bing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down Expand Up @@ -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]")
}
}
1 change: 1 addition & 0 deletions src/lib/api/chatgpt-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ async function chat(

await parseSSEResponse(resp, (message) => {
if (message === "[DONE]") {
port.postMessage("[DONE]")
return
}
try {
Expand Down
86 changes: 39 additions & 47 deletions src/lib/api/chatgpt-web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> {
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())
Expand All @@ -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<Response> => {
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 = ""
Expand All @@ -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 {
Expand All @@ -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",
Expand Down
2 changes: 2 additions & 0 deletions src/lib/api/notion-completion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ async function NotionCompletion(
} catch (err) {
console.error(err)
message = err.message
} finally {
port.postMessage("[DONE]")
}
}
port.postMessage(message)
Expand Down

0 comments on commit 173573d

Please sign in to comment.