Skip to content

Commit

Permalink
fix: update conditional check last status message (janhq#1951)
Browse files Browse the repository at this point in the history
* fix: update conditional check last status message

* fix: no new message on failed message

---------

Co-authored-by: Louis <[email protected]>
  • Loading branch information
urmauur and louis-menlo authored Feb 7, 2024
1 parent 5890ade commit 9a1b1ad
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 11 deletions.
6 changes: 3 additions & 3 deletions web/containers/Providers/EventHandler.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,10 @@ export default function EventHandler({ children }: { children: ReactNode }) {

const thread = threadsRef.current?.find((e) => e.id == message.thread_id)
if (thread) {
const messageContent = message.content[0]?.text.value ?? ''
const messageContent = message.content[0]?.text?.value
const metadata = {
...thread.metadata,
lastMessage: messageContent,
...(messageContent && { lastMessage: messageContent }),
}

updateThread({
Expand All @@ -151,7 +151,7 @@ export default function EventHandler({ children }: { children: ReactNode }) {
?.addNewMessage(message)
}
},
[updateMessage, updateThreadWaiting, setIsGeneratingResponse]
[updateMessage, updateThreadWaiting, setIsGeneratingResponse, updateThread]
)

useEffect(() => {
Expand Down
14 changes: 8 additions & 6 deletions web/helpers/atoms/ChatMessage.atom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,12 @@ export const addNewMessageAtom = atom(
set(chatMessages, newData)

// Update thread last message
set(
updateThreadStateLastMessageAtom,
newMessage.thread_id,
newMessage.content
)
if (newMessage.content.length)
set(
updateThreadStateLastMessageAtom,
newMessage.thread_id,
newMessage.content
)
}
)

Expand Down Expand Up @@ -131,7 +132,8 @@ export const updateMessageAtom = atom(
newData[conversationId] = updatedMessages
set(chatMessages, newData)
// Update thread last message
set(updateThreadStateLastMessageAtom, conversationId, text)
if (text.length)
set(updateThreadStateLastMessageAtom, conversationId, text)
}
}
)
7 changes: 5 additions & 2 deletions web/hooks/useCreateNewThread.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
ThreadAssistantInfo,
ThreadState,
Model,
MessageStatus,
} from '@janhq/core'
import { atom, useAtomValue, useSetAtom } from 'jotai'

Expand All @@ -20,6 +21,7 @@ import useSetActiveThread from './useSetActiveThread'

import { extensionManager } from '@/extension'

import { getCurrentChatMessagesAtom } from '@/helpers/atoms/ChatMessage.atom'
import {
threadsAtom,
threadStatesAtom,
Expand Down Expand Up @@ -51,6 +53,7 @@ export const useCreateNewThread = () => {
const setFileUpload = useSetAtom(fileUploadAtom)
const setSelectedModel = useSetAtom(selectedModelAtom)
const setThreadModelParams = useSetAtom(setThreadModelParamsAtom)
const messages = useAtomValue(getCurrentChatMessagesAtom)

const { recommendedModel, downloadedModels } = useRecommendedModel()

Expand All @@ -63,9 +66,9 @@ export const useCreateNewThread = () => {
const defaultModel = model ?? recommendedModel ?? downloadedModels[0]

// check last thread message, if there empty last message use can not create thread
const lastMessage = threads[0]?.metadata?.lastMessage
const lastMessage = threads[threads.length - 1]?.metadata?.lastMessage

if (!lastMessage && threads.length) {
if (!lastMessage && threads.length && !messages.length) {
return null
}

Expand Down

0 comments on commit 9a1b1ad

Please sign in to comment.