Skip to content

Commit

Permalink
fix: avoid users to create so many threads at the same time (janhq#1930)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
urmauur authored Feb 6, 2024
1 parent 61419e5 commit 29a7fb8
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
8 changes: 8 additions & 0 deletions web/containers/Providers/EventHandler.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import {
updateThreadWaitingForResponseAtom,
threadsAtom,
isGeneratingResponseAtom,
updateThreadAtom,
} from '@/helpers/atoms/Thread.atom'

export default function EventHandler({ children }: { children: ReactNode }) {
Expand All @@ -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
Expand Down Expand Up @@ -131,6 +133,12 @@ export default function EventHandler({ children }: { children: ReactNode }) {
...thread.metadata,
lastMessage: messageContent,
}

updateThread({
...thread,
metadata,
})

extensionManager
.get<ConversationalExtension>(ExtensionTypeEnum.Conversational)
?.saveThread({
Expand Down
12 changes: 11 additions & 1 deletion web/hooks/useCreateNewThread.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -19,6 +19,7 @@ import useRecommendedModel from './useRecommendedModel'
import useSetActiveThread from './useSetActiveThread'

import { extensionManager } from '@/extension'

import {
threadsAtom,
threadStatesAtom,
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 3 additions & 1 deletion web/screens/Chat/ThreadList/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 29a7fb8

Please sign in to comment.