fix: #1612 infinite loading

This commit is contained in:
Yidadaa
2023-05-20 19:58:12 +08:00
parent bcb18ff2f4
commit af497c96ec
8 changed files with 90 additions and 84 deletions

View File

@@ -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");

View File

@@ -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];
},

View File

@@ -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,
});

View File

@@ -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",
});
}
}
});

View File

@@ -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

View File

@@ -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;
}