Skip to content

Commit

Permalink
add remove message done
Browse files Browse the repository at this point in the history
  • Loading branch information
greywen committed Jan 22, 2025
1 parent 54b2776 commit b0f2dec
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 75 deletions.
5 changes: 5 additions & 0 deletions src/FE/apis/clientApis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -382,3 +382,8 @@ export const putResponseMessageEditInPlace = (
body: params.content,
});
};

export const deleteMessage = (messageId: string) => {
const fetchServer = useFetch();
return fetchServer.delete(`/api/messages/${messageId}?recursive=true`);
};
10 changes: 4 additions & 6 deletions src/FE/components/ChatMessage/ChatMessage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ export interface Props {
isCopy?: boolean,
) => void;
onEditUserMessage?: (messageId: string, content: Content) => void;
onDeleteResponseMessage?: (messageId: string) => void;
onDeleteUserMessage?: (messageId: string) => void;
onDeleteMessage?: (messageId: string) => void;
}

export const ChatMessage: FC<Props> = memo(
Expand All @@ -43,8 +42,7 @@ export const ChatMessage: FC<Props> = memo(
onReactionMessage,
onEditResponseMessage,
onEditUserMessage,
onDeleteResponseMessage,
onDeleteUserMessage,
onDeleteMessage,
}) => {
const hasMultipleSpan = selectedMessages.find((x) => x.length > 1);
return (
Expand Down Expand Up @@ -81,7 +79,7 @@ export const ChatMessage: FC<Props> = memo(
onChangeMessage={onChangeChatLeafMessageId}
onEditAndSendMessage={onEditAndSendMessage}
onEditUserMessage={onEditUserMessage}
onDeleteUserMessage={onDeleteUserMessage}
onDeleteMessage={onDeleteMessage}
/>
</div>
)}
Expand Down Expand Up @@ -119,7 +117,7 @@ export const ChatMessage: FC<Props> = memo(
onChangeChatLeafMessageId={
onChangeChatLeafMessageId
}
onDeleteResponseMessage={onDeleteResponseMessage}
onDeleteMessage={onDeleteMessage}
/>
</div>
</div>
Expand Down
53 changes: 29 additions & 24 deletions src/FE/components/ChatMessage/DeleteAction.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,36 +8,41 @@ import { cn } from '@/lib/utils';

interface Props {
disabled?: boolean;
visible?: boolean;
isHoverVisible?: boolean;
hidden?: boolean;
onDelete: () => void;
}

const DeleteAction = (props: Props) => {
const { onDelete, disabled, visible } = props;
const { onDelete, disabled, isHoverVisible, hidden } = props;
const { t } = useTranslation();

return (
<Tips
className="h-[28px]"
trigger={
<Button
variant="ghost"
disabled={disabled}
className={cn(
visible ? 'visible' : 'invisible',
'p-1 m-0 h-auto group-hover:visible focus:visible',
)}
onClick={(e) => {
onDelete();
e.stopPropagation();
}}
>
<IconTrash />
</Button>
}
content={t('Delete message')!}
/>
);
const Render = () => {
return (
<Tips
className="h-[28px]"
trigger={
<Button
variant="ghost"
disabled={disabled}
className={cn(
isHoverVisible ? 'invisible' : 'visible',
'p-1 m-0 h-auto group-hover:visible focus:visible',
)}
onClick={(e) => {
onDelete();
e.stopPropagation();
}}
>
<IconTrash />
</Button>
}
content={t('Delete message')!}
/>
);
};

return <>{!hidden && Render()}</>;
};

export default DeleteAction;
6 changes: 3 additions & 3 deletions src/FE/components/ChatMessage/EditAction.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import { cn } from '@/lib/utils';

interface Props {
disabled?: boolean;
visible?: boolean;
isHoverVisible?: boolean;
onToggleEditing: () => void;
}

const EditAction = (props: Props) => {
const { onToggleEditing, disabled, visible } = props;
const { onToggleEditing, disabled, isHoverVisible } = props;
const { t } = useTranslation();

return (
Expand All @@ -24,7 +24,7 @@ const EditAction = (props: Props) => {
variant="ghost"
disabled={disabled}
className={cn(
visible ? 'visible' : 'invisible',
isHoverVisible ? 'invisible' : 'visible',
'p-1 m-0 h-auto group-hover:visible focus:visible',
)}
onClick={(e) => {
Expand Down
6 changes: 3 additions & 3 deletions src/FE/components/ChatMessage/ResponseMessage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ interface Props {
content: Content,
isCopy?: boolean,
) => void;
onDeleteResponseMessage?: (messageId: string) => void;
onDeleteMessage?: (messageId: string) => void;
}

const ResponseMessage = (props: Props) => {
Expand All @@ -50,7 +50,7 @@ const ResponseMessage = (props: Props) => {
onRegenerate,
onReactionMessage,
onEditResponseMessage,
onDeleteResponseMessage,
onDeleteMessage,
} = props;
const { t } = useTranslation();

Expand Down Expand Up @@ -234,7 +234,7 @@ const ResponseMessage = (props: Props) => {
onRegenerate={(messageId: string, modelId: number) => {
onRegenerate && onRegenerate(message.spanId!, messageId, modelId);
}}
onDeleteResponseMessage={onDeleteResponseMessage}
onDeleteMessage={onDeleteMessage}
/>
</>
);
Expand Down
22 changes: 11 additions & 11 deletions src/FE/components/ChatMessage/ResponseMessageActions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ interface Props {
onChangeMessage?: (messageId: string) => void;
onRegenerate?: (messageId: string, modelId: number) => void;
onReactionMessage?: (type: ReactionMessageType, messageId: string) => void;
onDeleteResponseMessage?: (messageId: string) => void;
onDeleteMessage?: (messageId: string) => void;
}

const ResponseMessageActions = (props: Props) => {
Expand All @@ -54,7 +54,7 @@ const ResponseMessageActions = (props: Props) => {
onChangeMessage,
onRegenerate,
onReactionMessage,
onDeleteResponseMessage,
onDeleteMessage,
} = props;

const { id: messageId, siblingIds, modelId, modelName, parentId } = message;
Expand All @@ -80,20 +80,20 @@ const ResponseMessageActions = (props: Props) => {
/>
<div className="visible flex gap-0 items-center">
<EditAction
visible
onToggleEditing={() => {
onToggleEditingMessage && onToggleEditingMessage(messageId);
}}
/>

<CopyAction text={message.content.text} />
{/* {siblingIds.length > 1 && (
<DeleteAction
visible
onDelete={() => {
onDeleteMessage && onDeleteMessage(messageId);
}}
/>
)} */}

<DeleteAction
hidden={siblingIds.length <= 1}
onDelete={() => {
onDeleteMessage && onDeleteMessage(messageId);
}}
/>

<GenerateInformationAction
hidden={message.edited}
message={message}
Expand Down
11 changes: 8 additions & 3 deletions src/FE/components/ChatMessage/UserMessage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ interface Props {
onChangeMessage?: (messageId: string) => void;
onEditAndSendMessage?: (editedMessage: Message, parentId?: string) => void;
onEditUserMessage?: (messageId: string, content: Content) => void;
onDeleteUserMessage?: (messageId: string) => void;
onDeleteMessage?: (messageId: string) => void;
}

const UserMessage = (props: Props) => {
Expand All @@ -44,7 +44,7 @@ const UserMessage = (props: Props) => {
onChangeMessage,
onEditAndSendMessage,
onEditUserMessage,
onDeleteUserMessage,
onDeleteMessage,
} = props;
const [isTyping, setIsTyping] = useState<boolean>(false);
const [isEditing, setIsEditing] = useState<boolean>(false);
Expand Down Expand Up @@ -194,6 +194,7 @@ const UserMessage = (props: Props) => {
{!isEditing && (
<>
<EditAction
isHoverVisible
disabled={chatStatus === ChatSpanStatus.Chatting}
onToggleEditing={handleToggleEditing}
/>
Expand All @@ -202,8 +203,12 @@ const UserMessage = (props: Props) => {
text={content.text}
/>
<DeleteAction
hidden={
!(message.parentId !== null || message?.siblingIds?.length > 1)
}
isHoverVisible
onDelete={() => {
onDeleteUserMessage && onDeleteUserMessage(messageId);
onDeleteMessage && onDeleteMessage(messageId);
}}
/>
<PaginationAction
Expand Down
47 changes: 22 additions & 25 deletions src/FE/pages/home/_components/Chat/Chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ import ChatMessageMemoized from './MemoizedChatMessage';
import NoModel from './NoModel';

import {
deleteMessage,
putChats,
putMessageReactionClear,
putMessageReactionUp,
Expand Down Expand Up @@ -565,36 +566,33 @@ const Chat = memo(() => {
messageDispatch(setSelectedMessages(selectedMsgs));
};

const handleDeleteResponseMessage = (messageId: string) => {
let nextMessageId = '';
const msg = messages.find((x) => x.id === messageId);
let msgs = messages.filter((x) => {
if (x.id === messageId) {
if (msg?.role === ChatRole.User) {
nextMessageId = x.parentId!;
} else if (msg?.role === ChatRole.Assistant) {
const siblingMsgs = messages.filter(
(x) =>
x.spanId === msg.spanId &&
x.parentId === msg.parentId &&
x.id !== msg.id,
);
nextMessageId = siblingMsgs[siblingMsgs.length - 1].id;
const handleDeleteMessage = async (messageId: string) => {
await deleteMessage(messageId);
let nextMsgId = '';
let msgs = messages.filter((x) => x.id !== messageId);
selectedMessages.forEach((msg) => {
msg.forEach((m) => {
if (m.id === messageId) {
if (m?.siblingIds?.length > 1) {
const siblingIds = m.siblingIds.filter((id) => id !== m.id);
nextMsgId = siblingIds[siblingIds.length - 1];
} else if (m.parentId !== null) {
if (m.role === ChatRole.Assistant) {
const userMsg = messages.find((x) => x.id === m.parentId);
nextMsgId = userMsg?.parentId!;
msgs = messages.filter((x) => x.id !== userMsg?.id);
} else if (m.role === ChatRole.User) nextMsgId = m.parentId;
}
}
}
return x.id !== messageId;
});
});

const leafId = findLastLeafId(msgs, nextMessageId);
const selectedMsgs = findSelectedMessageByLeafId(messages, leafId);
const leafId = findLastLeafId(msgs, nextMsgId);
const selectedMsgs = findSelectedMessageByLeafId(msgs, leafId);
messageDispatch(setSelectedMessages(selectedMsgs));
messageDispatch(setMessages(msgs));
};

const handleDeleteUserMessage = (messageId: string) => {

};

return (
<div className="relative flex-1">
<div
Expand All @@ -617,8 +615,7 @@ const Chat = memo(() => {
onReactionMessage={handleReactionMessage}
onEditResponseMessage={handleUpdateResponseMessage}
onEditUserMessage={handleUpdateUserMessage}
onDeleteResponseMessage={handleDeleteResponseMessage}
onDeleteUserMessage={handleDeleteUserMessage}
onDeleteMessage={handleDeleteMessage}
/>
</div>
{hasModel() && selectedChat && (
Expand Down

0 comments on commit b0f2dec

Please sign in to comment.