diff --git a/app/components/chat.tsx b/app/components/chat.tsx index d7540dea4..61a625b33 100644 --- a/app/components/chat.tsx +++ b/app/components/chat.tsx @@ -507,7 +507,10 @@ export function Chat(props: { bordered title={Locale.Chat.Actions.Export} onClick={() => { - exportMessages(session.messages, session.topic); + exportMessages( + session.messages.filter((msg) => !msg.isError), + session.topic, + ); }} /> diff --git a/app/requests.ts b/app/requests.ts index 281f8ff1b..ee3103b77 100644 --- a/app/requests.ts +++ b/app/requests.ts @@ -114,7 +114,7 @@ export async function requestChatStream( filterBot?: boolean; modelConfig?: ModelConfig; onMessage: (message: string, done: boolean) => void; - onError: (error: Error) => void; + onError: (error: Error, statusCode?: number) => void; onController?: (controller: AbortController) => void; }, ) { @@ -178,11 +178,10 @@ export async function requestChatStream( finish(); } else if (res.status === 401) { console.error("Anauthorized"); - responseText = Locale.Error.Unauthorized; - finish(); + options?.onError(new Error("Anauthorized"), res.status); } else { console.error("Stream Error", res.body); - options?.onError(new Error("Stream Error")); + options?.onError(new Error("Stream Error"), res.status); } } catch (err) { console.error("NetWork Error", err); diff --git a/app/store/app.ts b/app/store/app.ts index 29da33792..b943c0d97 100644 --- a/app/store/app.ts +++ b/app/store/app.ts @@ -14,6 +14,7 @@ import Locale from "../locales"; export type Message = ChatCompletionResponseMessage & { date: string; streaming?: boolean; + isError?: boolean; }; export enum SubmitKey { @@ -351,9 +352,15 @@ export const useChatStore = create()( set(() => ({})); } }, - onError(error) { - botMessage.content += "\n\n" + Locale.Store.Error; + onError(error, statusCode) { + if (statusCode === 401) { + botMessage.content = Locale.Error.Unauthorized; + } else { + botMessage.content += "\n\n" + Locale.Store.Error; + } botMessage.streaming = false; + userMessage.isError = true; + botMessage.isError = true; set(() => ({})); ControllerPool.remove(sessionIndex, messageIndex); }, @@ -383,7 +390,8 @@ export const useChatStore = create()( getMessagesWithMemory() { const session = get().currentSession(); const config = get().config; - const n = session.messages.length; + const messages = session.messages.filter((msg) => !msg.isError); + const n = messages.length; const context = session.context.slice(); @@ -393,7 +401,7 @@ export const useChatStore = create()( } const recentMessages = context.concat( - session.messages.slice(Math.max(0, n - config.historyMessageCount)), + messages.slice(Math.max(0, n - config.historyMessageCount)), ); return recentMessages;