Merge pull request #401 from cyhhao/pr2

fix: context message should exclude all error tips
This commit is contained in:
Yifei Zhang 2023-04-03 14:26:32 +08:00 committed by GitHub
commit b5a69b66b2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 9 deletions

View File

@ -507,7 +507,10 @@ export function Chat(props: {
bordered bordered
title={Locale.Chat.Actions.Export} title={Locale.Chat.Actions.Export}
onClick={() => { onClick={() => {
exportMessages(session.messages, session.topic); exportMessages(
session.messages.filter((msg) => !msg.isError),
session.topic,
);
}} }}
/> />
</div> </div>

View File

@ -114,7 +114,7 @@ export async function requestChatStream(
filterBot?: boolean; filterBot?: boolean;
modelConfig?: ModelConfig; modelConfig?: ModelConfig;
onMessage: (message: string, done: boolean) => void; onMessage: (message: string, done: boolean) => void;
onError: (error: Error) => void; onError: (error: Error, statusCode?: number) => void;
onController?: (controller: AbortController) => void; onController?: (controller: AbortController) => void;
}, },
) { ) {
@ -178,11 +178,10 @@ export async function requestChatStream(
finish(); finish();
} else if (res.status === 401) { } else if (res.status === 401) {
console.error("Anauthorized"); console.error("Anauthorized");
responseText = Locale.Error.Unauthorized; options?.onError(new Error("Anauthorized"), res.status);
finish();
} else { } else {
console.error("Stream Error", res.body); console.error("Stream Error", res.body);
options?.onError(new Error("Stream Error")); options?.onError(new Error("Stream Error"), res.status);
} }
} catch (err) { } catch (err) {
console.error("NetWork Error", err); console.error("NetWork Error", err);

View File

@ -14,6 +14,7 @@ import Locale from "../locales";
export type Message = ChatCompletionResponseMessage & { export type Message = ChatCompletionResponseMessage & {
date: string; date: string;
streaming?: boolean; streaming?: boolean;
isError?: boolean;
}; };
export enum SubmitKey { export enum SubmitKey {
@ -351,9 +352,15 @@ export const useChatStore = create<ChatStore>()(
set(() => ({})); set(() => ({}));
} }
}, },
onError(error) { onError(error, statusCode) {
botMessage.content += "\n\n" + Locale.Store.Error; if (statusCode === 401) {
botMessage.content = Locale.Error.Unauthorized;
} else {
botMessage.content += "\n\n" + Locale.Store.Error;
}
botMessage.streaming = false; botMessage.streaming = false;
userMessage.isError = true;
botMessage.isError = true;
set(() => ({})); set(() => ({}));
ControllerPool.remove(sessionIndex, messageIndex); ControllerPool.remove(sessionIndex, messageIndex);
}, },
@ -383,7 +390,8 @@ export const useChatStore = create<ChatStore>()(
getMessagesWithMemory() { getMessagesWithMemory() {
const session = get().currentSession(); const session = get().currentSession();
const config = get().config; 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(); const context = session.context.slice();
@ -393,7 +401,7 @@ export const useChatStore = create<ChatStore>()(
} }
const recentMessages = context.concat( const recentMessages = context.concat(
session.messages.slice(Math.max(0, n - config.historyMessageCount)), messages.slice(Math.max(0, n - config.historyMessageCount)),
); );
return recentMessages; return recentMessages;