Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: unchange title and last message when clean or delete message #2937

Merged
merged 1 commit into from
May 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions web/helpers/atoms/ChatMessage.atom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ export const deleteMessageAtom = atom(null, (get, set, id: string) => {
newData[threadId] = newData[threadId].filter(
(e) => e.id !== id && e.status !== MessageStatus.Error
)

set(chatMessages, newData)
}
})
Expand Down
19 changes: 15 additions & 4 deletions web/hooks/useDeleteThread.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
ConversationalExtension,
fs,
joinPath,
Thread,
} from '@janhq/core'

import { useAtom, useAtomValue, useSetAtom } from 'jotai'
Expand All @@ -27,6 +28,7 @@ import {
setActiveThreadIdAtom,
deleteThreadStateAtom,
updateThreadStateLastMessageAtom,
updateThreadAtom,
} from '@/helpers/atoms/Thread.atom'

export default function useDeleteThread() {
Expand All @@ -41,6 +43,7 @@ export default function useDeleteThread() {

const deleteThreadState = useSetAtom(deleteThreadStateAtom)
const updateThreadLastMessage = useSetAtom(updateThreadStateLastMessageAtom)
const updateThread = useSetAtom(updateThreadAtom)

const cleanThread = useCallback(
async (threadId: string) => {
Expand Down Expand Up @@ -73,19 +76,27 @@ export default function useDeleteThread() {

thread.metadata = {
...thread.metadata,
lastMessage: undefined,
}

const updatedThread: Thread = {
...thread,
title: 'New Thread',
metadata: { ...thread.metadata, lastMessage: undefined },
}

await extensionManager
.get<ConversationalExtension>(ExtensionTypeEnum.Conversational)
?.saveThread(thread)
?.saveThread(updatedThread)
updateThreadLastMessage(threadId, undefined)
updateThread(updatedThread)
},
[
janDataFolderPath,
cleanMessages,
threads,
messages,
cleanMessages,
updateThreadLastMessage,
updateThread,
janDataFolderPath,
]
)

Expand Down
41 changes: 38 additions & 3 deletions web/screens/Chat/MessageToolbar/index.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { useCallback } from 'react'

import {
MessageStatus,
ExtensionTypeEnum,
ThreadMessage,
ChatCompletionRole,
ConversationalExtension,
ContentType,
Thread,
} from '@janhq/core'
import { useAtomValue, useSetAtom } from 'jotai'
import {
Expand All @@ -26,7 +29,11 @@ import {
editMessageAtom,
getCurrentChatMessagesAtom,
} from '@/helpers/atoms/ChatMessage.atom'
import { activeThreadAtom } from '@/helpers/atoms/Thread.atom'
import {
activeThreadAtom,
updateThreadAtom,
updateThreadStateLastMessageAtom,
} from '@/helpers/atoms/Thread.atom'

const MessageToolbar = ({ message }: { message: ThreadMessage }) => {
const deleteMessage = useSetAtom(deleteMessageAtom)
Expand All @@ -35,9 +42,19 @@ const MessageToolbar = ({ message }: { message: ThreadMessage }) => {
const messages = useAtomValue(getCurrentChatMessagesAtom)
const { resendChatMessage } = useSendChatMessage()
const clipboard = useClipboard({ timeout: 1000 })
const updateThreadLastMessage = useSetAtom(updateThreadStateLastMessageAtom)
const updateThread = useSetAtom(updateThreadAtom)

const onDeleteClick = async () => {
const onDeleteClick = useCallback(async () => {
deleteMessage(message.id ?? '')

const lastResponse = messages
.filter(
(msg) =>
msg.id !== message.id && msg.role === ChatCompletionRole.Assistant
)
.slice(-1)[0]

if (thread) {
// Should also delete error messages to clear out the error state
await extensionManager
Expand All @@ -48,8 +65,26 @@ const MessageToolbar = ({ message }: { message: ThreadMessage }) => {
(msg) => msg.id !== message.id && msg.status !== MessageStatus.Error
)
)

const updatedThread: Thread = {
...thread,
metadata: {
...thread.metadata,
lastMessage: messages.filter(
(msg) => msg.role === ChatCompletionRole.Assistant
)[
messages.filter((msg) => msg.role === ChatCompletionRole.Assistant)
.length - 1
]?.content[0].text.value,
},
}

updateThreadLastMessage(thread.id, lastResponse?.content)

updateThread(updatedThread)
}
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [messages])

const onEditClick = async () => {
setEditMessage(message.id ?? '')
Expand Down
Loading