fix: avoid users to create so many threads at the same time (#1930)

* fix: avoid allow users to create so many threads at the same time

* fix missing last message

* remove console

* update last message metadata thread

* update conditional statement
This commit is contained in:
Faisal Amir 2024-02-06 16:00:29 +07:00 committed by GitHub
parent 61419e5c02
commit 29a7fb8c93
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 22 additions and 2 deletions

View File

@ -33,6 +33,7 @@ import {
updateThreadWaitingForResponseAtom,
threadsAtom,
isGeneratingResponseAtom,
updateThreadAtom,
} from '@/helpers/atoms/Thread.atom'
export default function EventHandler({ children }: { children: ReactNode }) {
@ -49,6 +50,7 @@ export default function EventHandler({ children }: { children: ReactNode }) {
const modelsRef = useRef(downloadedModels)
const threadsRef = useRef(threads)
const setIsGeneratingResponse = useSetAtom(isGeneratingResponseAtom)
const updateThread = useSetAtom(updateThreadAtom)
useEffect(() => {
threadsRef.current = threads
@ -131,6 +133,12 @@ export default function EventHandler({ children }: { children: ReactNode }) {
...thread.metadata,
lastMessage: messageContent,
}
updateThread({
...thread,
metadata,
})
extensionManager
.get<ConversationalExtension>(ExtensionTypeEnum.Conversational)
?.saveThread({

View File

@ -7,7 +7,7 @@ import {
ThreadState,
Model,
} from '@janhq/core'
import { atom, useSetAtom } from 'jotai'
import { atom, useAtomValue, useSetAtom } from 'jotai'
import { selectedModelAtom } from '@/containers/DropdownListSidebar'
import { fileUploadAtom } from '@/containers/Providers/Jotai'
@ -19,6 +19,7 @@ import useRecommendedModel from './useRecommendedModel'
import useSetActiveThread from './useSetActiveThread'
import { extensionManager } from '@/extension'
import {
threadsAtom,
threadStatesAtom,
@ -53,12 +54,21 @@ export const useCreateNewThread = () => {
const { recommendedModel, downloadedModels } = useRecommendedModel()
const threads = useAtomValue(threadsAtom)
const requestCreateNewThread = async (
assistant: Assistant,
model?: Model | undefined
) => {
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
if (!lastMessage && threads.length) {
return null
}
const createdAt = Date.now()
const assistantInfo: ThreadAssistantInfo = {
assistant_id: assistant.id,

View File

@ -62,7 +62,9 @@ export default function ThreadList() {
</p>
<h2 className="line-clamp-1 font-bold">{thread.title}</h2>
<p className="mt-1 line-clamp-1 text-xs text-gray-700 group-hover/message:max-w-[160px] dark:text-gray-300">
{threadStates[thread.id]?.lastMessage ?? 'No new message'}
{threadStates[thread.id]?.lastMessage
? threadStates[thread.id]?.lastMessage
: 'No new message'}
</p>
</div>
<div