Skip to content

Commit af497c9

Browse files
committed
fix: ChatGPTNextWeb#1612 infinite loading
1 parent bcb18ff commit af497c9

File tree

8 files changed

+98
-92
lines changed

8 files changed

+98
-92
lines changed

app/api/auth.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,6 @@ export function auth(req: NextRequest) {
5555
req.headers.set("Authorization", `Bearer ${apiKey}`);
5656
} else {
5757
console.log("[Auth] admin did not provide an api key");
58-
return {
59-
error: serverConfig.baseUrl?.includes(OPENAI_URL),
60-
msg: "admin did not provide an api key",
61-
};
6258
}
6359
} else {
6460
console.log("[Auth] use user api key");

app/client/controller.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export const ChatControllerPool = {
2828

2929
remove(sessionIndex: number, messageId: number) {
3030
const key = this.key(sessionIndex, messageId);
31+
this.controllers[key]?.abort();
3132
delete this.controllers[key];
3233
},
3334

app/client/platforms/openai.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import Locale from "../../locales";
66
import {
77
EventStreamContentType,
88
fetchEventSource,
9-
} from "@microsoft/fetch-event-source";
9+
} from "@fortaine/fetch-event-source";
1010
import { prettyObject } from "@/app/utils/format";
1111

1212
export class ChatGPTApi implements LLMApi {
@@ -145,6 +145,7 @@ export class ChatGPTApi implements LLMApi {
145145
},
146146
onerror(e) {
147147
options.onError?.(e);
148+
throw e;
148149
},
149150
openWhenHidden: true,
150151
});

app/components/chat.tsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ import { Avatar } from "./emoji";
5858
import { MaskAvatar, MaskConfig } from "./mask";
5959
import { useMaskStore } from "../store/mask";
6060
import { useCommand } from "../command";
61+
import { prettyObject } from "../utils/format";
6162

6263
const Markdown = dynamic(async () => (await import("./markdown")).Markdown, {
6364
loading: () => <LoadingIcon />,
@@ -496,13 +497,17 @@ export function Chat() {
496497
const stopTiming = Date.now() - REQUEST_TIMEOUT_MS;
497498
session.messages.forEach((m) => {
498499
// check if should stop all stale messages
499-
if (new Date(m.date).getTime() < stopTiming) {
500+
if (m.isError || new Date(m.date).getTime() < stopTiming) {
500501
if (m.streaming) {
501502
m.streaming = false;
502503
}
503504

504505
if (m.content.length === 0) {
505-
m.content = "No content in this message.";
506+
m.isError = true;
507+
m.content = prettyObject({
508+
error: true,
509+
message: "empty response",
510+
});
506511
}
507512
}
508513
});

app/store/chat.ts

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { trimTopic } from "../utils";
55

66
import Locale from "../locales";
77
import { showToast } from "../components/ui-lib";
8-
import { ModelType, useAppConfig } from "./config";
8+
import { ModelType } from "./config";
99
import { createEmptyMask, Mask } from "./mask";
1010
import { StoreKey } from "../constant";
1111
import { api, RequestMessage } from "../client/api";
@@ -277,13 +277,17 @@ export const useChatStore = create<ChatStore>()(
277277
config: { ...modelConfig, stream: true },
278278
onUpdate(message) {
279279
botMessage.streaming = true;
280-
botMessage.content = message;
280+
if (message) {
281+
botMessage.content = message;
282+
}
281283
set(() => ({}));
282284
},
283285
onFinish(message) {
284286
botMessage.streaming = false;
285-
botMessage.content = message;
286-
get().onNewMessage(botMessage);
287+
if (message) {
288+
botMessage.content = message;
289+
get().onNewMessage(botMessage);
290+
}
287291
ChatControllerPool.remove(
288292
sessionIndex,
289293
botMessage.id ?? messageIndex,
@@ -292,12 +296,12 @@ export const useChatStore = create<ChatStore>()(
292296
},
293297
onError(error) {
294298
const isAborted = error.message.includes("aborted");
295-
if (
296-
botMessage.content !== Locale.Error.Unauthorized &&
297-
!isAborted
298-
) {
299-
botMessage.content += "\n\n" + prettyObject(error);
300-
}
299+
botMessage.content =
300+
"\n\n" +
301+
prettyObject({
302+
error: true,
303+
message: error.message,
304+
});
301305
botMessage.streaming = false;
302306
userMessage.isError = !isAborted;
303307
botMessage.isError = !isAborted;
@@ -308,7 +312,7 @@ export const useChatStore = create<ChatStore>()(
308312
botMessage.id ?? messageIndex,
309313
);
310314

311-
console.error("[Chat] error ", error);
315+
console.error("[Chat] failed ", error);
312316
},
313317
onController(controller) {
314318
// collect controller for stop/retry

app/utils/format.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
export function prettyObject(msg: any) {
2-
const prettyMsg = [
3-
"```json\n",
4-
JSON.stringify(msg, null, " "),
5-
"\n```",
6-
].join("");
2+
if (typeof msg !== "string") {
3+
msg = JSON.stringify(msg, null, " ");
4+
}
5+
const prettyMsg = ["```json", msg, "```"].join("\n");
76
return prettyMsg;
87
}

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@
1414
},
1515
"dependencies": {
1616
"@hello-pangea/dnd": "^16.2.0",
17-
"@microsoft/fetch-event-source": "^2.0.1",
17+
"@fortaine/fetch-event-source": "^3.0.6",
1818
"@svgr/webpack": "^6.5.1",
1919
"@vercel/analytics": "^0.1.11",
2020
"emoji-picker-react": "^4.4.7",
2121
"fuse.js": "^6.6.2",
2222
"mermaid": "^10.1.0",
23-
"next": "^13.4.2",
23+
"next": "^13.4.3",
2424
"node-fetch": "^3.3.1",
2525
"react": "^18.2.0",
2626
"react-dom": "^18.2.0",

yarn.lock

Lines changed: 67 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1032,6 +1032,11 @@
10321032
resolved "https://registry.yarnpkg.com/@eslint/js/-/js-8.37.0.tgz#cf1b5fa24217fe007f6487a26d765274925efa7d"
10331033
integrity sha512-x5vzdtOOGgFVDCUs81QRB2+liax8rFg3+7hqM+QhBG0/G3F1ZsoYl97UrqgHgQ9KKT7G6c4V+aTUCgu/n22v1A==
10341034

1035+
"@fortaine/fetch-event-source@^3.0.6":
1036+
version "3.0.6"
1037+
resolved "https://registry.npmmirror.com/@fortaine/fetch-event-source/-/fetch-event-source-3.0.6.tgz#b8552a2ca2c5202f5699b93a92be0188d422b06e"
1038+
integrity sha512-621GAuLMvKtyZQ3IA6nlDWhV1V/7PGOTNIGLUifxt0KzM+dZIweJ6F3XvQF3QnqeNfS1N7WQ0Kil1Di/lhChEw==
1039+
10351040
"@hello-pangea/dnd@^16.2.0":
10361041
version "16.2.0"
10371042
resolved "https://registry.npmmirror.com/@hello-pangea/dnd/-/dnd-16.2.0.tgz#58cbadeb56f8c7a381da696bb7aa3bfbb87876ec"
@@ -1111,15 +1116,10 @@
11111116
dependencies:
11121117
"@types/react" ">=16.0.0"
11131118

1114-
"@microsoft/fetch-event-source@^2.0.1":
1115-
version "2.0.1"
1116-
resolved "https://registry.npmmirror.com/@microsoft/fetch-event-source/-/fetch-event-source-2.0.1.tgz#9ceecc94b49fbaa15666e38ae8587f64acce007d"
1117-
integrity sha512-W6CLUJ2eBMw3Rec70qrsEW0jOm/3twwJv21mrmj2yORiaVmVYGS4sSS5yUwvQc1ZlDLYGPnClVWmUUMagKNsfA==
1118-
1119-
"@next/env@13.4.2":
1120-
version "13.4.2"
1121-
resolved "https://registry.npmmirror.com/@next/env/-/env-13.4.2.tgz#cf3ebfd523a33d8404c1216e02ac8d856a73170e"
1122-
integrity sha512-Wqvo7lDeS0KGwtwg9TT9wKQ8raelmUxt+TQKWvG/xKfcmDXNOtCuaszcfCF8JzlBG1q0VhpI6CKaRMbVPMDWgw==
1119+
"@next/env@13.4.3":
1120+
version "13.4.3"
1121+
resolved "https://registry.npmmirror.com/@next/env/-/env-13.4.3.tgz#cb00bdd43a0619a79a52c9336df8a0aa84f8f4bf"
1122+
integrity sha512-pa1ErjyFensznttAk3EIv77vFbfSYT6cLzVRK5jx4uiRuCQo+m2wCFAREaHKIy63dlgvOyMlzh6R8Inu8H3KrQ==
11231123

11241124
"@next/eslint-plugin-next@13.2.3":
11251125
version "13.2.3"
@@ -1128,50 +1128,50 @@
11281128
dependencies:
11291129
glob "7.1.7"
11301130

1131-
"@next/swc-darwin-arm64@13.4.2":
1132-
version "13.4.2"
1133-
resolved "https://registry.npmmirror.com/@next/swc-darwin-arm64/-/swc-darwin-arm64-13.4.2.tgz#d0b497df972bd02eee3bc823d6a76c2cc8b733ef"
1134-
integrity sha512-6BBlqGu3ewgJflv9iLCwO1v1hqlecaIH2AotpKfVUEzUxuuDNJQZ2a4KLb4MBl8T9/vca1YuWhSqtbF6ZuUJJw==
1135-
1136-
"@next/swc-darwin-x64@13.4.2":
1137-
version "13.4.2"
1138-
resolved "https://registry.npmmirror.com/@next/swc-darwin-x64/-/swc-darwin-x64-13.4.2.tgz#09a800bed8dfe4beec4cbf14092f9c22db24470b"
1139-
integrity sha512-iZuYr7ZvGLPjPmfhhMl0ISm+z8EiyLBC1bLyFwGBxkWmPXqdJ60mzuTaDSr5WezDwv0fz32HB7JHmRC6JVHSZg==
1140-
1141-
"@next/swc-linux-arm64-gnu@13.4.2":
1142-
version "13.4.2"
1143-
resolved "https://registry.npmmirror.com/@next/swc-linux-arm64-gnu/-/swc-linux-arm64-gnu-13.4.2.tgz#b7ade28834564120b0b25ffa0b79d75982d290bc"
1144-
integrity sha512-2xVabFtIge6BJTcJrW8YuUnYTuQjh4jEuRuS2mscyNVOj6zUZkom3CQg+egKOoS+zh2rrro66ffSKIS+ztFJTg==
1145-
1146-
"@next/swc-linux-arm64-musl@13.4.2":
1147-
version "13.4.2"
1148-
resolved "https://registry.npmmirror.com/@next/swc-linux-arm64-musl/-/swc-linux-arm64-musl-13.4.2.tgz#f5420548234d35251630ddaa2e9a7dc32337a887"
1149-
integrity sha512-wKRCQ27xCUJx5d6IivfjYGq8oVngqIhlhSAJntgXLt7Uo9sRT/3EppMHqUZRfyuNBTbykEre1s5166z+pvRB5A==
1150-
1151-
"@next/swc-linux-x64-gnu@13.4.2":
1152-
version "13.4.2"
1153-
resolved "https://registry.npmmirror.com/@next/swc-linux-x64-gnu/-/swc-linux-x64-gnu-13.4.2.tgz#0241dc011d73f08df9d9998cffdfcf08d1971520"
1154-
integrity sha512-NpCa+UVhhuNeaFVUP1Bftm0uqtvLWq2JTm7+Ta48+2Uqj2mNXrDIvyn1DY/ZEfmW/1yvGBRaUAv9zkMkMRixQA==
1155-
1156-
"@next/swc-linux-x64-musl@13.4.2":
1157-
version "13.4.2"
1158-
resolved "https://registry.npmmirror.com/@next/swc-linux-x64-musl/-/swc-linux-x64-musl-13.4.2.tgz#fd35919e2b64b1c739583145799fefd594ef5d63"
1159-
integrity sha512-ZWVC72x0lW4aj44e3khvBrj2oSYj1bD0jESmyah3zG/3DplEy/FOtYkMzbMjHTdDSheso7zH8GIlW6CDQnKhmQ==
1160-
1161-
"@next/swc-win32-arm64-msvc@13.4.2":
1162-
version "13.4.2"
1163-
resolved "https://registry.npmmirror.com/@next/swc-win32-arm64-msvc/-/swc-win32-arm64-msvc-13.4.2.tgz#fa95d2dbb97707c130a868a1bd7e83e64bedf4c6"
1164-
integrity sha512-pLT+OWYpzJig5K4VKhLttlIfBcVZfr2+Xbjra0Tjs83NQSkFS+y7xx+YhCwvpEmXYLIvaggj2ONPyjbiigOvHQ==
1165-
1166-
"@next/swc-win32-ia32-msvc@13.4.2":
1167-
version "13.4.2"
1168-
resolved "https://registry.npmmirror.com/@next/swc-win32-ia32-msvc/-/swc-win32-ia32-msvc-13.4.2.tgz#31a98e61d3cda92ec2293c50df7cb5280fc63697"
1169-
integrity sha512-dhpiksQCyGca4WY0fJyzK3FxMDFoqMb0Cn+uDB+9GYjpU2K5//UGPQlCwiK4JHxuhg8oLMag5Nf3/IPSJNG8jw==
1170-
1171-
"@next/swc-win32-x64-msvc@13.4.2":
1172-
version "13.4.2"
1173-
resolved "https://registry.npmmirror.com/@next/swc-win32-x64-msvc/-/swc-win32-x64-msvc-13.4.2.tgz#8435ab6087046355f5de07122d3097949e8fab10"
1174-
integrity sha512-O7bort1Vld00cu8g0jHZq3cbSTUNMohOEvYqsqE10+yfohhdPHzvzO+ziJRz4Dyyr/fYKREwS7gR4JC0soSOMw==
1131+
"@next/swc-darwin-arm64@13.4.3":
1132+
version "13.4.3"
1133+
resolved "https://registry.npmmirror.com/@next/swc-darwin-arm64/-/swc-darwin-arm64-13.4.3.tgz#2d6c99dd5afbcce37e4ba0f64196317a1259034d"
1134+
integrity sha512-yx18udH/ZmR4Bw4M6lIIPE3JxsAZwo04iaucEfA2GMt1unXr2iodHUX/LAKNyi6xoLP2ghi0E+Xi1f4Qb8f1LQ==
1135+
1136+
"@next/swc-darwin-x64@13.4.3":
1137+
version "13.4.3"
1138+
resolved "https://registry.npmmirror.com/@next/swc-darwin-x64/-/swc-darwin-x64-13.4.3.tgz#162b15fb8a54d9f64e69c898ebeb55b7dac9bddd"
1139+
integrity sha512-Mi8xJWh2IOjryAM1mx18vwmal9eokJ2njY4nDh04scy37F0LEGJ/diL6JL6kTXi0UfUCGbMsOItf7vpReNiD2A==
1140+
1141+
"@next/swc-linux-arm64-gnu@13.4.3":
1142+
version "13.4.3"
1143+
resolved "https://registry.npmmirror.com/@next/swc-linux-arm64-gnu/-/swc-linux-arm64-gnu-13.4.3.tgz#aee57422f11183d6a2e4a2e8aa23b9285873e18f"
1144+
integrity sha512-aBvtry4bxJ1xwKZ/LVPeBGBwWVwxa4bTnNkRRw6YffJnn/f4Tv4EGDPaVeYHZGQVA56wsGbtA6nZMuWs/EIk4Q==
1145+
1146+
"@next/swc-linux-arm64-musl@13.4.3":
1147+
version "13.4.3"
1148+
resolved "https://registry.npmmirror.com/@next/swc-linux-arm64-musl/-/swc-linux-arm64-musl-13.4.3.tgz#c10b6aaaa47b341c6c9ea15f8b0ddb37e255d035"
1149+
integrity sha512-krT+2G3kEsEUvZoYte3/2IscscDraYPc2B+fDJFipPktJmrv088Pei/RjrhWm5TMIy5URYjZUoDZdh5k940Dyw==
1150+
1151+
"@next/swc-linux-x64-gnu@13.4.3":
1152+
version "13.4.3"
1153+
resolved "https://registry.npmmirror.com/@next/swc-linux-x64-gnu/-/swc-linux-x64-gnu-13.4.3.tgz#3f85bc5591c6a0d4908404f7e88e3c04f4462039"
1154+
integrity sha512-AMdFX6EKJjC0G/CM6hJvkY8wUjCcbdj3Qg7uAQJ7PVejRWaVt0sDTMavbRfgMchx8h8KsAudUCtdFkG9hlEClw==
1155+
1156+
"@next/swc-linux-x64-musl@13.4.3":
1157+
version "13.4.3"
1158+
resolved "https://registry.npmmirror.com/@next/swc-linux-x64-musl/-/swc-linux-x64-musl-13.4.3.tgz#f4535adc2374a86bc8e43af149b551567df065de"
1159+
integrity sha512-jySgSXE48shaLtcQbiFO9ajE9mqz7pcAVLnVLvRIlUHyQYR/WyZdK8ehLs65Mz6j9cLrJM+YdmdJPyV4WDaz2g==
1160+
1161+
"@next/swc-win32-arm64-msvc@13.4.3":
1162+
version "13.4.3"
1163+
resolved "https://registry.npmmirror.com/@next/swc-win32-arm64-msvc/-/swc-win32-arm64-msvc-13.4.3.tgz#e76106d85391c308c5ed70cda2bca2c582d65536"
1164+
integrity sha512-5DxHo8uYcaADiE9pHrg8o28VMt/1kR8voDehmfs9AqS0qSClxAAl+CchjdboUvbCjdNWL1MISCvEfKY2InJ3JA==
1165+
1166+
"@next/swc-win32-ia32-msvc@13.4.3":
1167+
version "13.4.3"
1168+
resolved "https://registry.npmmirror.com/@next/swc-win32-ia32-msvc/-/swc-win32-ia32-msvc-13.4.3.tgz#8eb5d9dd71ed7a971671291605ad64ad522fb3bc"
1169+
integrity sha512-LaqkF3d+GXRA5X6zrUjQUrXm2MN/3E2arXBtn5C7avBCNYfm9G3Xc646AmmmpN3DJZVaMYliMyCIQCMDEzk80w==
1170+
1171+
"@next/swc-win32-x64-msvc@13.4.3":
1172+
version "13.4.3"
1173+
resolved "https://registry.npmmirror.com/@next/swc-win32-x64-msvc/-/swc-win32-x64-msvc-13.4.3.tgz#c7b2b1b9e158fd7749f8209e68ee8e43a997eb4c"
1174+
integrity sha512-jglUk/x7ZWeOJWlVoKyIAkHLTI+qEkOriOOV+3hr1GyiywzcqfI7TpFSiwC7kk1scOiH7NTFKp8mA3XPNO9bDw==
11751175

11761176
"@nodelib/fs.scandir@2.1.5":
11771177
version "2.1.5"
@@ -4275,28 +4275,28 @@ natural-compare@^1.4.0:
42754275
resolved "https://registry.yarnpkg.com/natural-compare/-/natural-compare-1.4.0.tgz#4abebfeed7541f2c27acfb29bdbbd15c8d5ba4f7"
42764276
integrity sha512-OWND8ei3VtNC9h7V60qff3SVobHr996CTwgxubgyQYEpg290h9J0buyECNNJexkFm5sOajh5G116RYA1c8ZMSw==
42774277

4278-
next@^13.4.2:
4279-
version "13.4.2"
4280-
resolved "https://registry.npmmirror.com/next/-/next-13.4.2.tgz#972f73a794f2c61729facedc79c49b22bdc89f0c"
4281-
integrity sha512-aNFqLs3a3nTGvLWlO9SUhCuMUHVPSFQC0+tDNGAsDXqx+WJDFSbvc233gOJ5H19SBc7nw36A9LwQepOJ2u/8Kg==
4278+
next@^13.4.3:
4279+
version "13.4.3"
4280+
resolved "https://registry.npmmirror.com/next/-/next-13.4.3.tgz#7f417dec9fa2731d8c1d1819a1c7d0919ad6fc75"
4281+
integrity sha512-FV3pBrAAnAIfOclTvncw9dDohyeuEEXPe5KNcva91anT/rdycWbgtu3IjUj4n5yHnWK8YEPo0vrUecHmnmUNbA==
42824282
dependencies:
4283-
"@next/env" "13.4.2"
4283+
"@next/env" "13.4.3"
42844284
"@swc/helpers" "0.5.1"
42854285
busboy "1.6.0"
42864286
caniuse-lite "^1.0.30001406"
42874287
postcss "8.4.14"
42884288
styled-jsx "5.1.1"
42894289
zod "3.21.4"
42904290
optionalDependencies:
4291-
"@next/swc-darwin-arm64" "13.4.2"
4292-
"@next/swc-darwin-x64" "13.4.2"
4293-
"@next/swc-linux-arm64-gnu" "13.4.2"
4294-
"@next/swc-linux-arm64-musl" "13.4.2"
4295-
"@next/swc-linux-x64-gnu" "13.4.2"
4296-
"@next/swc-linux-x64-musl" "13.4.2"
4297-
"@next/swc-win32-arm64-msvc" "13.4.2"
4298-
"@next/swc-win32-ia32-msvc" "13.4.2"
4299-
"@next/swc-win32-x64-msvc" "13.4.2"
4291+
"@next/swc-darwin-arm64" "13.4.3"
4292+
"@next/swc-darwin-x64" "13.4.3"
4293+
"@next/swc-linux-arm64-gnu" "13.4.3"
4294+
"@next/swc-linux-arm64-musl" "13.4.3"
4295+
"@next/swc-linux-x64-gnu" "13.4.3"
4296+
"@next/swc-linux-x64-musl" "13.4.3"
4297+
"@next/swc-win32-arm64-msvc" "13.4.3"
4298+
"@next/swc-win32-ia32-msvc" "13.4.3"
4299+
"@next/swc-win32-x64-msvc" "13.4.3"
43004300

43014301
node-domexception@^1.0.0:
43024302
version "1.0.0"

0 commit comments

Comments
 (0)