@@ -18,7 +18,7 @@ import {
1818 extractInferenceParams ,
1919 ModelExtension ,
2020} from '@janhq/core'
21- import { useAtomValue , useSetAtom } from 'jotai'
21+ import { useAtom , useAtomValue , useSetAtom } from 'jotai'
2222import { ulid } from 'ulidx'
2323
2424import { activeModelAtom , stateModelAtom } from '@/hooks/useActiveModel'
@@ -32,6 +32,7 @@ import {
3232 updateMessageAtom ,
3333 tokenSpeedAtom ,
3434 deleteMessageAtom ,
35+ subscribedGeneratingMessageAtom ,
3536} from '@/helpers/atoms/ChatMessage.atom'
3637import { downloadedModelsAtom } from '@/helpers/atoms/Model.atom'
3738import {
@@ -40,6 +41,7 @@ import {
4041 isGeneratingResponseAtom ,
4142 updateThreadAtom ,
4243 getActiveThreadModelParamsAtom ,
44+ activeThreadAtom ,
4345} from '@/helpers/atoms/Thread.atom'
4446
4547const maxWordForThreadTitle = 10
@@ -54,6 +56,10 @@ export default function ModelHandler() {
5456 const activeModel = useAtomValue ( activeModelAtom )
5557 const setActiveModel = useSetAtom ( activeModelAtom )
5658 const setStateModel = useSetAtom ( stateModelAtom )
59+ const [ subscribedGeneratingMessage , setSubscribedGeneratingMessage ] = useAtom (
60+ subscribedGeneratingMessageAtom
61+ )
62+ const activeThread = useAtomValue ( activeThreadAtom )
5763
5864 const updateThreadWaiting = useSetAtom ( updateThreadWaitingForResponseAtom )
5965 const threads = useAtomValue ( threadsAtom )
@@ -62,11 +68,17 @@ export default function ModelHandler() {
6268 const setIsGeneratingResponse = useSetAtom ( isGeneratingResponseAtom )
6369 const updateThread = useSetAtom ( updateThreadAtom )
6470 const messagesRef = useRef ( messages )
71+ const messageGenerationSubscriber = useRef ( subscribedGeneratingMessage )
6572 const activeModelRef = useRef ( activeModel )
73+ const activeThreadRef = useRef ( activeThread )
6674 const activeModelParams = useAtomValue ( getActiveThreadModelParamsAtom )
6775 const activeModelParamsRef = useRef ( activeModelParams )
6876 const setTokenSpeed = useSetAtom ( tokenSpeedAtom )
6977
78+ useEffect ( ( ) => {
79+ activeThreadRef . current = activeThread
80+ } , [ activeThread ] )
81+
7082 useEffect ( ( ) => {
7183 threadsRef . current = threads
7284 } , [ threads ] )
@@ -87,6 +99,10 @@ export default function ModelHandler() {
8799 activeModelParamsRef . current = activeModelParams
88100 } , [ activeModelParams ] )
89101
102+ useEffect ( ( ) => {
103+ messageGenerationSubscriber . current = subscribedGeneratingMessage
104+ } , [ subscribedGeneratingMessage ] )
105+
90106 const onNewMessageResponse = useCallback (
91107 async ( message : ThreadMessage ) => {
92108 if ( message . type === MessageRequestType . Thread ) {
@@ -179,12 +195,19 @@ export default function ModelHandler() {
179195
180196 const updateThreadMessage = useCallback (
181197 ( message : ThreadMessage ) => {
182- updateMessage (
183- message . id ,
184- message . thread_id ,
185- message . content ,
186- message . status
187- )
198+ if (
199+ messageGenerationSubscriber . current &&
200+ message . thread_id === activeThreadRef . current ?. id &&
201+ ! messageGenerationSubscriber . current ! . thread_id
202+ ) {
203+ updateMessage (
204+ message . id ,
205+ message . thread_id ,
206+ message . content ,
207+ message . status
208+ )
209+ }
210+
188211 if ( message . status === MessageStatus . Pending ) {
189212 if ( message . content . length ) {
190213 setIsGeneratingResponse ( false )
@@ -244,6 +267,7 @@ export default function ModelHandler() {
244267 const metadata = {
245268 ...thread . metadata ,
246269 ...( messageContent && { lastMessage : messageContent } ) ,
270+ updated_at : Date . now ( ) ,
247271 }
248272
249273 updateThread ( {
@@ -302,15 +326,10 @@ export default function ModelHandler() {
302326
303327 const generateThreadTitle = ( message : ThreadMessage , thread : Thread ) => {
304328 // If this is the first ever prompt in the thread
305- if (
306- ( thread . title ?? thread . metadata ?. title ) ?. trim ( ) !== defaultThreadTitle
307- ) {
329+ if ( ( thread . title ?? thread . metadata ?. title ) ?. trim ( ) !== defaultThreadTitle )
308330 return
309- }
310331
311- if ( ! activeModelRef . current ) {
312- return
313- }
332+ if ( ! activeModelRef . current ) return
314333
315334 // Check model engine; we don't want to generate a title when it's not a local engine. remote model using first promp
316335 if ( ! isLocalEngine ( activeModelRef . current ?. engine as InferenceEngine ) ) {
@@ -332,6 +351,7 @@ export default function ModelHandler() {
332351 ...updatedThread ,
333352 } )
334353 } )
354+ . catch ( console . error )
335355 }
336356
337357 // This is the first time message comes in on a new thread
0 commit comments