From 93f8889c5798b6f47dfb3a3831c051a11078786d Mon Sep 17 00:00:00 2001 From: liuweiqing Date: Sat, 24 Feb 2024 11:20:15 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BC=98=E9=9B=85=E7=9A=84=E6=8A=A5?= =?UTF-8?q?=E9=94=99=E6=8F=90=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- components/QuillEditor.tsx | 15 +++++++++++---- components/chatAI.tsx | 15 +++++++++++---- package-lock.json | 20 +++++++++++++++++++- package.json | 1 + 4 files changed, 42 insertions(+), 9 deletions(-) diff --git a/components/QuillEditor.tsx b/components/QuillEditor.tsx index 830807cb..81e2c89b 100644 --- a/components/QuillEditor.tsx +++ b/components/QuillEditor.tsx @@ -45,9 +45,8 @@ import { debounce } from "lodash"; //i18n import { useTranslation } from "@/app/i18n/client"; //notification -import { ToastContainer } from "react-toastify"; +import { ToastContainer, toast } from "react-toastify"; import "react-toastify/dist/ReactToastify.css"; -import NotifyButton from "@/components/Notification"; // 确保路径正确 const toolbarOptions = [ ["bold", "italic", "underline", "strike"], // 加粗、斜体、下划线和删除线 @@ -431,6 +430,10 @@ const QEditor = ({ lng }) => { quill!, 800 )},搜索到的论文内容:${trimmedMessage},需要完成的论文主题:${topic},请根据搜索到的论文内容完成用户的论文`; + // toast.info(`搜索论文完成,内容:${content}`, { + // position: "top-right", + // autoClose: 5000, + // }); await sendMessageToOpenAI( content, quill!, @@ -465,9 +468,13 @@ const QEditor = ({ lng }) => { offset += 2; setGenerateNumber(i + 1); } catch (error) { - // console.error("Error fetching data:", error); + console.error("Paper2AI出现错误", error); // 在处理错误后,再次抛出这个错误 - throw new Error(`Paper2AI出现错误: ${error}`); + // throw new Error(`Paper2AI出现错误: ${error}`); + toast.error(`Paper2AI出现错误: ${error}`, { + position: "top-center", + autoClose: 5000, + }); } } setOpenProgressBar(false); diff --git a/components/chatAI.tsx b/components/chatAI.tsx index fd71e577..ac224335 100644 --- a/components/chatAI.tsx +++ b/components/chatAI.tsx @@ -81,11 +81,16 @@ const sendMessageToOpenAI = async ( (upsreamUrl || process.env.NEXT_PUBLIC_AI_URL) + "/v1/chat/completions", requestOptions ); - if (!response.ok || !response.body) { - throw new Error(""); + // 检查响应状态码是否为429 + if (response.status === 429) { + // 可以在这里处理429错误,例如通过UI通知用户 + throw new Error("请求过于频繁,请稍后再试。"); + } else if (!response.ok) { + // 处理其他类型的HTTP错误 + throw new Error(`HTTP错误,状态码:${response.status}`); } if (useEditorFlag && editor && cursorPosition !== null) { - const reader = response.body.getReader(); + const reader = response.body!.getReader(); const decoder = new TextDecoder(); //开始前先进行换行 // editor.focus(); @@ -106,7 +111,9 @@ const sendMessageToOpenAI = async ( // 如果有响应,返回响应的原始内容 if (response) { const rawResponse = await response.text(); - throw new Error(`请求发生错误: ${error}, Response: ${rawResponse}`); + throw new Error( + `请求发生错误: ${error.message}, Response: ${rawResponse}` + ); } // 如果没有响应,只抛出错误 throw error; diff --git a/package-lock.json b/package-lock.json index 0c363df1..1ad6c962 100644 --- a/package-lock.json +++ b/package-lock.json @@ -17,6 +17,7 @@ "@sentry/nextjs": "^7.101.1", "@supabase/ssr": "latest", "@supabase/supabase-js": "latest", + "@types/react-toastify": "^4.1.0", "add": "^2.0.6", "autoprefixer": "10.4.15", "axios": "^1.6.5", @@ -29,7 +30,7 @@ "i18next-resources-to-backend": "^1.2.0", "lodash": "^4.17.21", "negotiator": "^0.6.3", - "next": "*", + "next": "latest", "next-redux-wrapper": "^8.1.0", "openai": "^4.24.3", "postcss": "8.4.35", @@ -1075,6 +1076,15 @@ "@types/react": "*" } }, + "node_modules/@types/react-toastify": { + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/@types/react-toastify/-/react-toastify-4.1.0.tgz", + "integrity": "sha512-u7Ie/7LHBsPVz/iJxi/WlRDS7Gh9csCJACTDXx+pSLuZCm94xpkwzhM3jV1L5ZxP/in0Gp2tFbJ91VrSGr1gyQ==", + "deprecated": "This is a stub types definition. react-toastify provides its own type definitions, so you do not need this installed.", + "dependencies": { + "react-toastify": "*" + } + }, "node_modules/@types/react-transition-group": { "version": "4.4.10", "resolved": "https://registry.npmjs.org/@types/react-transition-group/-/react-transition-group-4.4.10.tgz", @@ -5301,6 +5311,14 @@ "@types/react": "*" } }, + "@types/react-toastify": { + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/@types/react-toastify/-/react-toastify-4.1.0.tgz", + "integrity": "sha512-u7Ie/7LHBsPVz/iJxi/WlRDS7Gh9csCJACTDXx+pSLuZCm94xpkwzhM3jV1L5ZxP/in0Gp2tFbJ91VrSGr1gyQ==", + "requires": { + "react-toastify": "*" + } + }, "@types/react-transition-group": { "version": "4.4.10", "resolved": "https://registry.npmjs.org/@types/react-transition-group/-/react-transition-group-4.4.10.tgz", diff --git a/package.json b/package.json index 88e8ea64..e8f28cb0 100644 --- a/package.json +++ b/package.json @@ -16,6 +16,7 @@ "@sentry/nextjs": "^7.101.1", "@supabase/ssr": "latest", "@supabase/supabase-js": "latest", + "@types/react-toastify": "^4.1.0", "add": "^2.0.6", "autoprefixer": "10.4.15", "axios": "^1.6.5",