mirror of
https://github.com/Yidadaa/ChatGPT-Next-Web.git
synced 2025-08-09 05:34:01 +08:00
fix: #1612 infinite loading
This commit is contained in:
@@ -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");
|
||||
|
@@ -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];
|
||||
},
|
||||
|
||||
|
@@ -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,
|
||||
});
|
||||
|
@@ -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: () => <LoadingIcon />,
|
||||
@@ -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",
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
|
@@ -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<ChatStore>()(
|
||||
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<ChatStore>()(
|
||||
},
|
||||
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<ChatStore>()(
|
||||
botMessage.id ?? messageIndex,
|
||||
);
|
||||
|
||||
console.error("[Chat] error ", error);
|
||||
console.error("[Chat] failed ", error);
|
||||
},
|
||||
onController(controller) {
|
||||
// collect controller for stop/retry
|
||||
|
@@ -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;
|
||||
}
|
||||
|
Reference in New Issue
Block a user