From af497c96ec066abe93ac05433382283acc3ccf93 Mon Sep 17 00:00:00 2001 From: Yidadaa Date: Sat, 20 May 2023 19:58:12 +0800 Subject: [PATCH] fix: #1612 infinite loading --- app/api/auth.ts | 4 -- app/client/controller.ts | 1 + app/client/platforms/openai.ts | 3 +- app/components/chat.tsx | 9 ++- app/store/chat.ts | 26 +++++--- app/utils/format.ts | 9 ++- package.json | 4 +- yarn.lock | 118 ++++++++++++++++----------------- 8 files changed, 90 insertions(+), 84 deletions(-) diff --git a/app/api/auth.ts b/app/api/auth.ts index fa9f9f4c..fffb63c1 100644 --- a/app/api/auth.ts +++ b/app/api/auth.ts @@ -55,10 +55,6 @@ export function auth(req: NextRequest) { req.headers.set("Authorization", `Bearer ${apiKey}`); } else { console.log("[Auth] admin did not provide an api key"); - return { - error: serverConfig.baseUrl?.includes(OPENAI_URL), - msg: "admin did not provide an api key", - }; } } else { console.log("[Auth] use user api key"); diff --git a/app/client/controller.ts b/app/client/controller.ts index 86cb99e7..d28e72b0 100644 --- a/app/client/controller.ts +++ b/app/client/controller.ts @@ -28,6 +28,7 @@ export const ChatControllerPool = { remove(sessionIndex: number, messageId: number) { const key = this.key(sessionIndex, messageId); + this.controllers[key]?.abort(); delete this.controllers[key]; }, diff --git a/app/client/platforms/openai.ts b/app/client/platforms/openai.ts index c9197f1e..84c4a2df 100644 --- a/app/client/platforms/openai.ts +++ b/app/client/platforms/openai.ts @@ -6,7 +6,7 @@ import Locale from "../../locales"; import { EventStreamContentType, fetchEventSource, -} from "@microsoft/fetch-event-source"; +} from "@fortaine/fetch-event-source"; import { prettyObject } from "@/app/utils/format"; export class ChatGPTApi implements LLMApi { @@ -145,6 +145,7 @@ export class ChatGPTApi implements LLMApi { }, onerror(e) { options.onError?.(e); + throw e; }, openWhenHidden: true, }); diff --git a/app/components/chat.tsx b/app/components/chat.tsx index fffa8b88..755901f2 100644 --- a/app/components/chat.tsx +++ b/app/components/chat.tsx @@ -58,6 +58,7 @@ import { Avatar } from "./emoji"; import { MaskAvatar, MaskConfig } from "./mask"; import { useMaskStore } from "../store/mask"; import { useCommand } from "../command"; +import { prettyObject } from "../utils/format"; const Markdown = dynamic(async () => (await import("./markdown")).Markdown, { loading: () => , @@ -496,13 +497,17 @@ export function Chat() { const stopTiming = Date.now() - REQUEST_TIMEOUT_MS; session.messages.forEach((m) => { // check if should stop all stale messages - if (new Date(m.date).getTime() < stopTiming) { + if (m.isError || new Date(m.date).getTime() < stopTiming) { if (m.streaming) { m.streaming = false; } if (m.content.length === 0) { - m.content = "No content in this message."; + m.isError = true; + m.content = prettyObject({ + error: true, + message: "empty response", + }); } } }); diff --git a/app/store/chat.ts b/app/store/chat.ts index e063a12c..888ac3a0 100644 --- a/app/store/chat.ts +++ b/app/store/chat.ts @@ -5,7 +5,7 @@ import { trimTopic } from "../utils"; import Locale from "../locales"; import { showToast } from "../components/ui-lib"; -import { ModelType, useAppConfig } from "./config"; +import { ModelType } from "./config"; import { createEmptyMask, Mask } from "./mask"; import { StoreKey } from "../constant"; import { api, RequestMessage } from "../client/api"; @@ -277,13 +277,17 @@ export const useChatStore = create()( config: { ...modelConfig, stream: true }, onUpdate(message) { botMessage.streaming = true; - botMessage.content = message; + if (message) { + botMessage.content = message; + } set(() => ({})); }, onFinish(message) { botMessage.streaming = false; - botMessage.content = message; - get().onNewMessage(botMessage); + if (message) { + botMessage.content = message; + get().onNewMessage(botMessage); + } ChatControllerPool.remove( sessionIndex, botMessage.id ?? messageIndex, @@ -292,12 +296,12 @@ export const useChatStore = create()( }, onError(error) { const isAborted = error.message.includes("aborted"); - if ( - botMessage.content !== Locale.Error.Unauthorized && - !isAborted - ) { - botMessage.content += "\n\n" + prettyObject(error); - } + botMessage.content = + "\n\n" + + prettyObject({ + error: true, + message: error.message, + }); botMessage.streaming = false; userMessage.isError = !isAborted; botMessage.isError = !isAborted; @@ -308,7 +312,7 @@ export const useChatStore = create()( botMessage.id ?? messageIndex, ); - console.error("[Chat] error ", error); + console.error("[Chat] failed ", error); }, onController(controller) { // collect controller for stop/retry diff --git a/app/utils/format.ts b/app/utils/format.ts index 1f71f4f0..d2912d20 100644 --- a/app/utils/format.ts +++ b/app/utils/format.ts @@ -1,8 +1,7 @@ export function prettyObject(msg: any) { - const prettyMsg = [ - "```json\n", - JSON.stringify(msg, null, " "), - "\n```", - ].join(""); + if (typeof msg !== "string") { + msg = JSON.stringify(msg, null, " "); + } + const prettyMsg = ["```json", msg, "```"].join("\n"); return prettyMsg; } diff --git a/package.json b/package.json index 792930f1..7acc6aad 100644 --- a/package.json +++ b/package.json @@ -14,13 +14,13 @@ }, "dependencies": { "@hello-pangea/dnd": "^16.2.0", - "@microsoft/fetch-event-source": "^2.0.1", + "@fortaine/fetch-event-source": "^3.0.6", "@svgr/webpack": "^6.5.1", "@vercel/analytics": "^0.1.11", "emoji-picker-react": "^4.4.7", "fuse.js": "^6.6.2", "mermaid": "^10.1.0", - "next": "^13.4.2", + "next": "^13.4.3", "node-fetch": "^3.3.1", "react": "^18.2.0", "react-dom": "^18.2.0", diff --git a/yarn.lock b/yarn.lock index e54a69e4..584db6dc 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1032,6 +1032,11 @@ resolved "https://registry.yarnpkg.com/@eslint/js/-/js-8.37.0.tgz#cf1b5fa24217fe007f6487a26d765274925efa7d" integrity sha512-x5vzdtOOGgFVDCUs81QRB2+liax8rFg3+7hqM+QhBG0/G3F1ZsoYl97UrqgHgQ9KKT7G6c4V+aTUCgu/n22v1A== +"@fortaine/fetch-event-source@^3.0.6": + version "3.0.6" + resolved "https://registry.npmmirror.com/@fortaine/fetch-event-source/-/fetch-event-source-3.0.6.tgz#b8552a2ca2c5202f5699b93a92be0188d422b06e" + integrity sha512-621GAuLMvKtyZQ3IA6nlDWhV1V/7PGOTNIGLUifxt0KzM+dZIweJ6F3XvQF3QnqeNfS1N7WQ0Kil1Di/lhChEw== + "@hello-pangea/dnd@^16.2.0": version "16.2.0" resolved "https://registry.npmmirror.com/@hello-pangea/dnd/-/dnd-16.2.0.tgz#58cbadeb56f8c7a381da696bb7aa3bfbb87876ec" @@ -1111,15 +1116,10 @@ dependencies: "@types/react" ">=16.0.0" -"@microsoft/fetch-event-source@^2.0.1": - version "2.0.1" - resolved "https://registry.npmmirror.com/@microsoft/fetch-event-source/-/fetch-event-source-2.0.1.tgz#9ceecc94b49fbaa15666e38ae8587f64acce007d" - integrity sha512-W6CLUJ2eBMw3Rec70qrsEW0jOm/3twwJv21mrmj2yORiaVmVYGS4sSS5yUwvQc1ZlDLYGPnClVWmUUMagKNsfA== - -"@next/env@13.4.2": - version "13.4.2" - resolved "https://registry.npmmirror.com/@next/env/-/env-13.4.2.tgz#cf3ebfd523a33d8404c1216e02ac8d856a73170e" - integrity sha512-Wqvo7lDeS0KGwtwg9TT9wKQ8raelmUxt+TQKWvG/xKfcmDXNOtCuaszcfCF8JzlBG1q0VhpI6CKaRMbVPMDWgw== +"@next/env@13.4.3": + version "13.4.3" + resolved "https://registry.npmmirror.com/@next/env/-/env-13.4.3.tgz#cb00bdd43a0619a79a52c9336df8a0aa84f8f4bf" + integrity sha512-pa1ErjyFensznttAk3EIv77vFbfSYT6cLzVRK5jx4uiRuCQo+m2wCFAREaHKIy63dlgvOyMlzh6R8Inu8H3KrQ== "@next/eslint-plugin-next@13.2.3": version "13.2.3" @@ -1128,50 +1128,50 @@ dependencies: glob "7.1.7" -"@next/swc-darwin-arm64@13.4.2": - version "13.4.2" - resolved "https://registry.npmmirror.com/@next/swc-darwin-arm64/-/swc-darwin-arm64-13.4.2.tgz#d0b497df972bd02eee3bc823d6a76c2cc8b733ef" - integrity sha512-6BBlqGu3ewgJflv9iLCwO1v1hqlecaIH2AotpKfVUEzUxuuDNJQZ2a4KLb4MBl8T9/vca1YuWhSqtbF6ZuUJJw== +"@next/swc-darwin-arm64@13.4.3": + version "13.4.3" + resolved "https://registry.npmmirror.com/@next/swc-darwin-arm64/-/swc-darwin-arm64-13.4.3.tgz#2d6c99dd5afbcce37e4ba0f64196317a1259034d" + integrity sha512-yx18udH/ZmR4Bw4M6lIIPE3JxsAZwo04iaucEfA2GMt1unXr2iodHUX/LAKNyi6xoLP2ghi0E+Xi1f4Qb8f1LQ== -"@next/swc-darwin-x64@13.4.2": - version "13.4.2" - resolved "https://registry.npmmirror.com/@next/swc-darwin-x64/-/swc-darwin-x64-13.4.2.tgz#09a800bed8dfe4beec4cbf14092f9c22db24470b" - integrity sha512-iZuYr7ZvGLPjPmfhhMl0ISm+z8EiyLBC1bLyFwGBxkWmPXqdJ60mzuTaDSr5WezDwv0fz32HB7JHmRC6JVHSZg== +"@next/swc-darwin-x64@13.4.3": + version "13.4.3" + resolved "https://registry.npmmirror.com/@next/swc-darwin-x64/-/swc-darwin-x64-13.4.3.tgz#162b15fb8a54d9f64e69c898ebeb55b7dac9bddd" + integrity sha512-Mi8xJWh2IOjryAM1mx18vwmal9eokJ2njY4nDh04scy37F0LEGJ/diL6JL6kTXi0UfUCGbMsOItf7vpReNiD2A== -"@next/swc-linux-arm64-gnu@13.4.2": - version "13.4.2" - resolved "https://registry.npmmirror.com/@next/swc-linux-arm64-gnu/-/swc-linux-arm64-gnu-13.4.2.tgz#b7ade28834564120b0b25ffa0b79d75982d290bc" - integrity sha512-2xVabFtIge6BJTcJrW8YuUnYTuQjh4jEuRuS2mscyNVOj6zUZkom3CQg+egKOoS+zh2rrro66ffSKIS+ztFJTg== +"@next/swc-linux-arm64-gnu@13.4.3": + version "13.4.3" + resolved "https://registry.npmmirror.com/@next/swc-linux-arm64-gnu/-/swc-linux-arm64-gnu-13.4.3.tgz#aee57422f11183d6a2e4a2e8aa23b9285873e18f" + integrity sha512-aBvtry4bxJ1xwKZ/LVPeBGBwWVwxa4bTnNkRRw6YffJnn/f4Tv4EGDPaVeYHZGQVA56wsGbtA6nZMuWs/EIk4Q== -"@next/swc-linux-arm64-musl@13.4.2": - version "13.4.2" - resolved "https://registry.npmmirror.com/@next/swc-linux-arm64-musl/-/swc-linux-arm64-musl-13.4.2.tgz#f5420548234d35251630ddaa2e9a7dc32337a887" - integrity sha512-wKRCQ27xCUJx5d6IivfjYGq8oVngqIhlhSAJntgXLt7Uo9sRT/3EppMHqUZRfyuNBTbykEre1s5166z+pvRB5A== +"@next/swc-linux-arm64-musl@13.4.3": + version "13.4.3" + resolved "https://registry.npmmirror.com/@next/swc-linux-arm64-musl/-/swc-linux-arm64-musl-13.4.3.tgz#c10b6aaaa47b341c6c9ea15f8b0ddb37e255d035" + integrity sha512-krT+2G3kEsEUvZoYte3/2IscscDraYPc2B+fDJFipPktJmrv088Pei/RjrhWm5TMIy5URYjZUoDZdh5k940Dyw== -"@next/swc-linux-x64-gnu@13.4.2": - version "13.4.2" - resolved "https://registry.npmmirror.com/@next/swc-linux-x64-gnu/-/swc-linux-x64-gnu-13.4.2.tgz#0241dc011d73f08df9d9998cffdfcf08d1971520" - integrity sha512-NpCa+UVhhuNeaFVUP1Bftm0uqtvLWq2JTm7+Ta48+2Uqj2mNXrDIvyn1DY/ZEfmW/1yvGBRaUAv9zkMkMRixQA== +"@next/swc-linux-x64-gnu@13.4.3": + version "13.4.3" + resolved "https://registry.npmmirror.com/@next/swc-linux-x64-gnu/-/swc-linux-x64-gnu-13.4.3.tgz#3f85bc5591c6a0d4908404f7e88e3c04f4462039" + integrity sha512-AMdFX6EKJjC0G/CM6hJvkY8wUjCcbdj3Qg7uAQJ7PVejRWaVt0sDTMavbRfgMchx8h8KsAudUCtdFkG9hlEClw== -"@next/swc-linux-x64-musl@13.4.2": - version "13.4.2" - resolved "https://registry.npmmirror.com/@next/swc-linux-x64-musl/-/swc-linux-x64-musl-13.4.2.tgz#fd35919e2b64b1c739583145799fefd594ef5d63" - integrity sha512-ZWVC72x0lW4aj44e3khvBrj2oSYj1bD0jESmyah3zG/3DplEy/FOtYkMzbMjHTdDSheso7zH8GIlW6CDQnKhmQ== +"@next/swc-linux-x64-musl@13.4.3": + version "13.4.3" + resolved "https://registry.npmmirror.com/@next/swc-linux-x64-musl/-/swc-linux-x64-musl-13.4.3.tgz#f4535adc2374a86bc8e43af149b551567df065de" + integrity sha512-jySgSXE48shaLtcQbiFO9ajE9mqz7pcAVLnVLvRIlUHyQYR/WyZdK8ehLs65Mz6j9cLrJM+YdmdJPyV4WDaz2g== -"@next/swc-win32-arm64-msvc@13.4.2": - version "13.4.2" - resolved "https://registry.npmmirror.com/@next/swc-win32-arm64-msvc/-/swc-win32-arm64-msvc-13.4.2.tgz#fa95d2dbb97707c130a868a1bd7e83e64bedf4c6" - integrity sha512-pLT+OWYpzJig5K4VKhLttlIfBcVZfr2+Xbjra0Tjs83NQSkFS+y7xx+YhCwvpEmXYLIvaggj2ONPyjbiigOvHQ== +"@next/swc-win32-arm64-msvc@13.4.3": + version "13.4.3" + resolved "https://registry.npmmirror.com/@next/swc-win32-arm64-msvc/-/swc-win32-arm64-msvc-13.4.3.tgz#e76106d85391c308c5ed70cda2bca2c582d65536" + integrity sha512-5DxHo8uYcaADiE9pHrg8o28VMt/1kR8voDehmfs9AqS0qSClxAAl+CchjdboUvbCjdNWL1MISCvEfKY2InJ3JA== -"@next/swc-win32-ia32-msvc@13.4.2": - version "13.4.2" - resolved "https://registry.npmmirror.com/@next/swc-win32-ia32-msvc/-/swc-win32-ia32-msvc-13.4.2.tgz#31a98e61d3cda92ec2293c50df7cb5280fc63697" - integrity sha512-dhpiksQCyGca4WY0fJyzK3FxMDFoqMb0Cn+uDB+9GYjpU2K5//UGPQlCwiK4JHxuhg8oLMag5Nf3/IPSJNG8jw== +"@next/swc-win32-ia32-msvc@13.4.3": + version "13.4.3" + resolved "https://registry.npmmirror.com/@next/swc-win32-ia32-msvc/-/swc-win32-ia32-msvc-13.4.3.tgz#8eb5d9dd71ed7a971671291605ad64ad522fb3bc" + integrity sha512-LaqkF3d+GXRA5X6zrUjQUrXm2MN/3E2arXBtn5C7avBCNYfm9G3Xc646AmmmpN3DJZVaMYliMyCIQCMDEzk80w== -"@next/swc-win32-x64-msvc@13.4.2": - version "13.4.2" - resolved "https://registry.npmmirror.com/@next/swc-win32-x64-msvc/-/swc-win32-x64-msvc-13.4.2.tgz#8435ab6087046355f5de07122d3097949e8fab10" - integrity sha512-O7bort1Vld00cu8g0jHZq3cbSTUNMohOEvYqsqE10+yfohhdPHzvzO+ziJRz4Dyyr/fYKREwS7gR4JC0soSOMw== +"@next/swc-win32-x64-msvc@13.4.3": + version "13.4.3" + resolved "https://registry.npmmirror.com/@next/swc-win32-x64-msvc/-/swc-win32-x64-msvc-13.4.3.tgz#c7b2b1b9e158fd7749f8209e68ee8e43a997eb4c" + integrity sha512-jglUk/x7ZWeOJWlVoKyIAkHLTI+qEkOriOOV+3hr1GyiywzcqfI7TpFSiwC7kk1scOiH7NTFKp8mA3XPNO9bDw== "@nodelib/fs.scandir@2.1.5": version "2.1.5" @@ -4275,12 +4275,12 @@ natural-compare@^1.4.0: resolved "https://registry.yarnpkg.com/natural-compare/-/natural-compare-1.4.0.tgz#4abebfeed7541f2c27acfb29bdbbd15c8d5ba4f7" integrity sha512-OWND8ei3VtNC9h7V60qff3SVobHr996CTwgxubgyQYEpg290h9J0buyECNNJexkFm5sOajh5G116RYA1c8ZMSw== -next@^13.4.2: - version "13.4.2" - resolved "https://registry.npmmirror.com/next/-/next-13.4.2.tgz#972f73a794f2c61729facedc79c49b22bdc89f0c" - integrity sha512-aNFqLs3a3nTGvLWlO9SUhCuMUHVPSFQC0+tDNGAsDXqx+WJDFSbvc233gOJ5H19SBc7nw36A9LwQepOJ2u/8Kg== +next@^13.4.3: + version "13.4.3" + resolved "https://registry.npmmirror.com/next/-/next-13.4.3.tgz#7f417dec9fa2731d8c1d1819a1c7d0919ad6fc75" + integrity sha512-FV3pBrAAnAIfOclTvncw9dDohyeuEEXPe5KNcva91anT/rdycWbgtu3IjUj4n5yHnWK8YEPo0vrUecHmnmUNbA== dependencies: - "@next/env" "13.4.2" + "@next/env" "13.4.3" "@swc/helpers" "0.5.1" busboy "1.6.0" caniuse-lite "^1.0.30001406" @@ -4288,15 +4288,15 @@ next@^13.4.2: styled-jsx "5.1.1" zod "3.21.4" optionalDependencies: - "@next/swc-darwin-arm64" "13.4.2" - "@next/swc-darwin-x64" "13.4.2" - "@next/swc-linux-arm64-gnu" "13.4.2" - "@next/swc-linux-arm64-musl" "13.4.2" - "@next/swc-linux-x64-gnu" "13.4.2" - "@next/swc-linux-x64-musl" "13.4.2" - "@next/swc-win32-arm64-msvc" "13.4.2" - "@next/swc-win32-ia32-msvc" "13.4.2" - "@next/swc-win32-x64-msvc" "13.4.2" + "@next/swc-darwin-arm64" "13.4.3" + "@next/swc-darwin-x64" "13.4.3" + "@next/swc-linux-arm64-gnu" "13.4.3" + "@next/swc-linux-arm64-musl" "13.4.3" + "@next/swc-linux-x64-gnu" "13.4.3" + "@next/swc-linux-x64-musl" "13.4.3" + "@next/swc-win32-arm64-msvc" "13.4.3" + "@next/swc-win32-ia32-msvc" "13.4.3" + "@next/swc-win32-x64-msvc" "13.4.3" node-domexception@^1.0.0: version "1.0.0"