diff --git a/src/content.tsx b/src/content.tsx index 8349427..f6847d4 100644 --- a/src/content.tsx +++ b/src/content.tsx @@ -117,7 +117,7 @@ const Index = () => { document.activeElement.nodeName.toUpperCase() === "TEXTAREA" || document.activeElement.nodeName.toUpperCase() === "INPUT") ) { - console.log("select text from input") + // console.log("select text from input") // Set as original for later setSelectedElement(document.activeElement as HTMLElement) } diff --git a/src/lib/api/chatgpt-api.ts b/src/lib/api/chatgpt-api.ts index f8dfb2c..e8bd06a 100644 --- a/src/lib/api/chatgpt-api.ts +++ b/src/lib/api/chatgpt-api.ts @@ -1,5 +1,3 @@ -import { ofetch } from "ofetch" - import type { PlasmoMessaging } from "@plasmohq/messaging" import { parseSSEResponse } from "~lib/utils/sse" @@ -21,7 +19,7 @@ async function chat( { role: "user", content: prompt } ] } - const resp = await ofetch(url, { + const resp = await fetch(url, { method: "POST", headers: { "Content-Type": "application/json", @@ -29,6 +27,13 @@ async function chat( }, body: JSON.stringify(data) }) + + if (!resp.ok) { + const errMsg = `ChatGPTAPI return error, status: ${resp.status}` + console.error(errMsg) + throw new Error(errMsg) + } + let content: string = "" await parseSSEResponse(resp, (message) => { @@ -60,7 +65,6 @@ async function ChatGPTApiChat( api_key: string, res: PlasmoMessaging.Response ) { - console.log(`ChatStream: ${url}, ${instraction}, ${prompt}, ${api_key}`) if (!api_key) { res.send("Please set your OpenAI API key in the extension options page.") return diff --git a/src/lib/api/chatgpt-web.ts b/src/lib/api/chatgpt-web.ts index 7b697e5..553df70 100644 --- a/src/lib/api/chatgpt-web.ts +++ b/src/lib/api/chatgpt-web.ts @@ -1,4 +1,3 @@ -import { ofetch } from "ofetch" import { v4 as uuidv4 } from "uuid" import type { PlasmoMessaging } from "@plasmohq/messaging" @@ -18,7 +17,7 @@ async function getAccessToken(): Promise { return cacheToken as string } - const resp = await ofetch(`${CHATGPT_HOST}/api/auth/session`) + const resp = await fetch(`${CHATGPT_HOST}/api/auth/session`) const data = await resp.json() if (!data.accessToken) { throw new Error("401 UNAUTHORIZED") @@ -41,7 +40,7 @@ async function ChatGPTWebChat( console.error(err) message = err.message } - console.log(message) + // console.log(message) } res.send(message) } @@ -68,7 +67,7 @@ async function chat(prompt: string, res: PlasmoMessaging.Response) { model: CHATGPT_MODEL } const url = `${CHATGPT_HOST}/backend-api/conversation` - const resp = await ofetch(url, { + const resp = await fetch(url, { method: "POST", headers: { "Content-Type": "application/json", @@ -77,6 +76,12 @@ async function chat(prompt: string, res: PlasmoMessaging.Response) { body: JSON.stringify(data) }) + if (!resp.ok) { + const errMsg = `ChatGPT return error, status: ${resp.status}` + console.error(errMsg) + throw new Error(errMsg) + } + let conversationId: string = "" await parseSSEResponse(resp, (message) => { @@ -104,7 +109,7 @@ async function chat(prompt: string, res: PlasmoMessaging.Response) { async function removeConversation(id: string) { const accessToken = await getAccessToken() try { - const resp = await fetch(`${CHATGPT_HOST}/backend-api/conversation/${id}`, { + await fetch(`${CHATGPT_HOST}/backend-api/conversation/${id}`, { method: "PATCH", headers: { "Content-Type": "application/json", @@ -114,7 +119,7 @@ async function removeConversation(id: string) { is_visible: false }) }) - console.log(await resp.json()) + // console.log(await resp.json()) } catch (err) { console.error(err) } diff --git a/src/lib/api/notion-completion.ts b/src/lib/api/notion-completion.ts index 865ed68..2a21d53 100644 --- a/src/lib/api/notion-completion.ts +++ b/src/lib/api/notion-completion.ts @@ -1,4 +1,3 @@ -import { ofetch } from "ofetch" import { v4 as uuidv4 } from "uuid" import type { PlasmoMessaging } from "@plasmohq/messaging" @@ -62,12 +61,17 @@ async function complation( accept: "application/x-ndjson" } - const resp = await ofetch(url, { + const resp = await fetch(url, { method: "POST", headers: headers, - body: JSON.stringify(data), - retry: 3 + body: JSON.stringify(data) }) + + if (!resp.ok) { + const errMsg = `NotionAI return error, status: ${resp.status}` + console.error(errMsg) + throw new Error(errMsg) + } let fullMessage: string = "" const onMessage = (msg: any) => { // console.log(`msg: ${JSON.stringify(msg)}`) diff --git a/src/lib/api/readability.ts b/src/lib/api/readability.ts index 734a141..cc74603 100644 --- a/src/lib/api/readability.ts +++ b/src/lib/api/readability.ts @@ -27,7 +27,7 @@ async function Parse(url: string): Promise { // console.log(`readability response: ${JSON.stringify(data)}`) return data as ParseResult } catch (error) { - console.log(`readability error: ${error}`) + console.error(`readability error: ${error}`) throw new Error(error) } }