Fix: When I click to interrupt the chat, the page reports an error #10553 (#10554)

### What problem does this PR solve?
Fix: When I click to interrupt the chat, the page reports an error
#10553

### Type of change

- [x] Bug Fix (non-breaking change which fixes an issue)
This commit is contained in:
balibabu
2025-10-14 19:07:18 +08:00
committed by GitHub
parent 51139de178
commit 960f47c4d4
6 changed files with 20 additions and 21 deletions

View File

@ -44,6 +44,7 @@ import { useAddChatBox } from '../use-add-box';
type MultipleChatBoxProps = { type MultipleChatBoxProps = {
controller: AbortController; controller: AbortController;
chatBoxIds: string[]; chatBoxIds: string[];
stopOutputMessage(): void;
} & Pick< } & Pick<
ReturnType<typeof useAddChatBox>, ReturnType<typeof useAddChatBox>,
'removeChatBox' | 'addChatBox' | 'chatBoxIds' 'removeChatBox' | 'addChatBox' | 'chatBoxIds'
@ -200,6 +201,7 @@ export function MultipleChatBox({
chatBoxIds, chatBoxIds,
removeChatBox, removeChatBox,
addChatBox, addChatBox,
stopOutputMessage,
}: MultipleChatBoxProps) { }: MultipleChatBoxProps) {
const { const {
value, value,
@ -207,7 +209,6 @@ export function MultipleChatBox({
messageRecord, messageRecord,
handleInputChange, handleInputChange,
handlePressEnter, handlePressEnter,
stopOutputMessage,
setFormRef, setFormRef,
handleUploadFile, handleUploadFile,
} = useSendMultipleChatMessage(controller, chatBoxIds); } = useSendMultipleChatMessage(controller, chatBoxIds);

View File

@ -20,9 +20,10 @@ import { buildMessageItemReference } from '../../utils';
interface IProps { interface IProps {
controller: AbortController; controller: AbortController;
stopOutputMessage(): void;
} }
export function SingleChatBox({ controller }: IProps) { export function SingleChatBox({ controller, stopOutputMessage }: IProps) {
const { const {
value, value,
scrollRef, scrollRef,
@ -34,7 +35,6 @@ export function SingleChatBox({ controller }: IProps) {
handlePressEnter, handlePressEnter,
regenerateMessage, regenerateMessage,
removeMessageById, removeMessageById,
stopOutputMessage,
handleUploadFile, handleUploadFile,
removeFile, removeFile,
} = useSendMessage(controller); } = useSendMessage(controller);

View File

@ -39,7 +39,7 @@ export default function Chat() {
const { t } = useTranslation(); const { t } = useTranslation();
const { data: conversation } = useFetchConversation(); const { data: conversation } = useFetchConversation();
const { handleConversationCardClick, controller } = const { handleConversationCardClick, controller, stopOutputMessage } =
useHandleClickConversationCard(); useHandleClickConversationCard();
const { visible: settingVisible, switchVisible: switchSettingVisible } = const { visible: settingVisible, switchVisible: switchSettingVisible } =
useSetModalState(true); useSetModalState(true);
@ -74,6 +74,7 @@ export default function Chat() {
controller={controller} controller={controller}
removeChatBox={removeChatBox} removeChatBox={removeChatBox}
addChatBox={addChatBox} addChatBox={addChatBox}
stopOutputMessage={stopOutputMessage}
></MultipleChatBox> ></MultipleChatBox>
</section> </section>
); );
@ -129,7 +130,10 @@ export default function Chat() {
</CardTitle> </CardTitle>
</CardHeader> </CardHeader>
<CardContent className="flex-1 p-0 min-h-0"> <CardContent className="flex-1 p-0 min-h-0">
<SingleChatBox controller={controller}></SingleChatBox> <SingleChatBox
controller={controller}
stopOutputMessage={stopOutputMessage}
></SingleChatBox>
</CardContent> </CardContent>
</Card> </Card>
{settingVisible && ( {settingVisible && (

View File

@ -5,16 +5,20 @@ export function useHandleClickConversationCard() {
const [controller, setController] = useState(new AbortController()); const [controller, setController] = useState(new AbortController());
const { handleClickConversation } = useClickConversationCard(); const { handleClickConversation } = useClickConversationCard();
const stopOutputMessage = useCallback(() => {
setController((pre) => {
pre.abort();
return new AbortController();
});
}, []);
const handleConversationCardClick = useCallback( const handleConversationCardClick = useCallback(
(conversationId: string, isNew: boolean) => { (conversationId: string, isNew: boolean) => {
handleClickConversation(conversationId, isNew ? 'true' : ''); handleClickConversation(conversationId, isNew ? 'true' : '');
setController((pre) => { stopOutputMessage();
pre.abort();
return new AbortController();
});
}, },
[handleClickConversation], [handleClickConversation, stopOutputMessage],
); );
return { controller, handleConversationCardClick }; return { controller, handleConversationCardClick, stopOutputMessage };
} }

View File

@ -123,10 +123,6 @@ export const useSendMessage = (controller: AbortController) => {
[getConversationIsNew, handleUploadFile, setConversation], [getConversationIsNew, handleUploadFile, setConversation],
); );
const stopOutputMessage = useCallback(() => {
controller.abort();
}, [controller]);
const sendMessage = useCallback( const sendMessage = useCallback(
async ({ async ({
message, message,
@ -249,7 +245,6 @@ export const useSendMessage = (controller: AbortController) => {
messageContainerRef, messageContainerRef,
derivedMessages, derivedMessages,
removeMessageById, removeMessageById,
stopOutputMessage,
handleUploadFile: onUploadFile, handleUploadFile: onUploadFile,
isUploading, isUploading,
removeFile, removeFile,

View File

@ -35,10 +35,6 @@ export function useSendMultipleChatMessage(
const { setFormRef, getLLMConfigById, isLLMConfigEmpty } = const { setFormRef, getLLMConfigById, isLLMConfigEmpty } =
useBuildFormRefs(chatBoxIds); useBuildFormRefs(chatBoxIds);
const stopOutputMessage = useCallback(() => {
controller.abort();
}, [controller]);
const addNewestQuestion = useCallback( const addNewestQuestion = useCallback(
(message: Message, answer: string = '') => { (message: Message, answer: string = '') => {
setMessageRecord((pre) => { setMessageRecord((pre) => {
@ -236,7 +232,6 @@ export function useSendMultipleChatMessage(
sendMessage, sendMessage,
handleInputChange, handleInputChange,
handlePressEnter, handlePressEnter,
stopOutputMessage,
sendLoading: !allDone, sendLoading: !allDone,
setFormRef, setFormRef,
handleUploadFile, handleUploadFile,