Skip to content

Commit

Permalink
added functionality to remove the user-assistant message pair
Browse files Browse the repository at this point in the history
  • Loading branch information
Royal-lobster committed Nov 26, 2023
1 parent aa28cc0 commit 05d3983
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 11 deletions.
15 changes: 13 additions & 2 deletions src/components/Sidebar/chat/ChatList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ import remarkBreaks from 'remark-breaks'
import rehypeRaw from 'rehype-raw'
import { ChatMessage, ChatRole } from '../../../hooks/useCurrentChat'
import FilePreviewBar from './FilePreviewBar'
import { RiCloseLine } from 'react-icons/ri'

interface ChatListProps {
messages: ChatMessage[]
removeMessagePair: (timestamp: number) => void
}

const ChatList = ({ messages }: ChatListProps) => {
const ChatList = ({ messages, removeMessagePair }: ChatListProps) => {
const containerRef = useRef<HTMLDivElement>(null)

useEffect(() => {
Expand Down Expand Up @@ -50,9 +52,18 @@ const ChatList = ({ messages }: ChatListProps) => {
.map((msg, i) => (
<div
data-user={msg.role === ChatRole.USER ? 'true' : undefined}
className="markdown cdx-px-4 cdx-py-2 data-[user]:cdx-border-l-2 cdx-border-blue-400 data-[user]:cdx-bg-black/5 data-[user]:dark:cdx-bg-neutral-900/50 cdx-max-w-[400px]"
className="markdown cdx-group cdx-relative cdx-px-4 cdx-py-2 data-[user]:cdx-border-l-2 cdx-border-blue-400 data-[user]:cdx-bg-black/5 data-[user]:dark:cdx-bg-neutral-900/50 cdx-max-w-[400px]"
key={`${msg.timestamp}-${i}`}
>
{msg.role === ChatRole.USER && (
<button
type="button"
onClick={() => removeMessagePair(msg.timestamp)}
className="cdx-absolute group-hover:cdx-visible cdx-invisible cdx-right-2 cdx-top-2 cdx-p-0.5 cdx-bg-black/20 cdx-rounded"
>
<RiCloseLine />
</button>
)}
<ReactMarkdown
remarkPlugins={[remarkGfm, remarkBreaks]}
rehypePlugins={[rehypeRaw]}
Expand Down
22 changes: 14 additions & 8 deletions src/components/Sidebar/chat/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,19 @@ interface ChatProps {
}

const Chat = ({ settings }: ChatProps) => {
const { messages, submitQuery, clearMessages, generating, cancelRequest } =
useChatCompletion({
model: settings.chat.modal,
apiKey: settings.chat.openAIKey!,
mode: settings.chat.mode,
systemPrompt: SYSTEM_PROMPT,
})
const {
messages,
submitQuery,
clearMessages,
generating,
cancelRequest,
removeMessagePair,
} = useChatCompletion({
model: settings.chat.modal,
apiKey: settings.chat.openAIKey!,
mode: settings.chat.mode,
systemPrompt: SYSTEM_PROMPT,
})

useEffect(() => {
const handleWindowMessage = (event: MessageEvent) => {
Expand All @@ -37,7 +43,7 @@ const Chat = ({ settings }: ChatProps) => {

return (
<>
<ChatList messages={messages} />
<ChatList messages={messages} removeMessagePair={removeMessagePair} />
<SidebarInput
loading={generating}
submitMessage={submitQuery}
Expand Down
10 changes: 9 additions & 1 deletion src/hooks/useChatCompletion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export const useChatCompletion = ({
addNewMessage,
commitToStoredMessages,
clearMessages,
removeMessagePair,
} = useCurrentChat()
const [generating, setGenerating] = useState(false)

Expand Down Expand Up @@ -124,5 +125,12 @@ export const useChatCompletion = ({
setGenerating(false)
}

return { messages, submitQuery, generating, cancelRequest, clearMessages }
return {
messages,
submitQuery,
generating,
cancelRequest,
clearMessages,
removeMessagePair,
}
}
11 changes: 11 additions & 0 deletions src/hooks/useCurrentChat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,16 @@ export const useCurrentChat = () => {
setMessages((m) => [...m, newMessage])
}

const removeMessagePair = (timestamp: number) => {
setMessages((p) => {
const index = p.findIndex((msg) => msg.timestamp === timestamp)
if (index === -1 || p[index].role !== ChatRole.USER) return p
p.splice(index, 2) // remove the user message and the assistant message
return [...p]
})
commitToStoredMessages()
}

const commitToStoredMessages = async () => {
if (!currentChatIdRef.current) return
setStorage(getStoredChatKey(currentChatIdRef.current), messagesRef.current)
Expand All @@ -152,5 +162,6 @@ export const useCurrentChat = () => {
commitToStoredMessages,
clearMessages,
currentChatId,
removeMessagePair,
}
}

0 comments on commit 05d3983

Please sign in to comment.