@@ -13,7 +13,6 @@ import { trackLLMCall } from "@/lib/analytics";
1313import { FILE_CONFIRMATION , FILES_REGEX } from "@/lib/constants" ;
1414import { buildSearchPayloadFilters } from "@/lib/filter-normalization" ;
1515import { cn } from "@/lib/utils" ;
16- import { useLoadingStore } from "@/stores/loadingStore" ;
1716import { useGetConversationsQuery } from "../api/queries/useGetConversationsQuery" ;
1817import { useGetNudgesQuery } from "../api/queries/useGetNudgesQuery" ;
1918import { useGetSettingsQuery } from "../api/queries/useGetSettingsQuery" ;
@@ -29,10 +28,10 @@ import type {
2928 RequestBody ,
3029 ToolCallResult ,
3130} from "./_types/types" ;
31+ import { INITIAL_ASSISTANT_MESSAGE } from "./_types/types" ;
3232
3333function ChatPage ( ) {
3434 const isDebugMode = process . env . NEXT_PUBLIC_OPENRAG_DEBUG === "true" ;
35- const isCloudBrand = useIsCloudBrand ( ) ;
3635 const {
3736 endpoint,
3837 setEndpoint,
@@ -50,16 +49,13 @@ function ChatPage() {
5049 placeholderConversation,
5150 conversationFilter,
5251 setConversationFilter,
52+ loading,
53+ setLoading,
5354 } = useChat ( ) ;
5455 const [ messages , setMessages ] = useState < Message [ ] > ( [
55- {
56- role : "assistant" ,
57- content : "How can I assist?" ,
58- timestamp : new Date ( ) ,
59- } ,
56+ INITIAL_ASSISTANT_MESSAGE ,
6057 ] ) ;
6158 const [ input , setInput ] = useState ( "" ) ;
62- const { loading, setLoading } = useLoadingStore ( ) ;
6359 const { setChatError } = useChat ( ) ;
6460 const [ asyncMode , setAsyncMode ] = useState ( true ) ;
6561 const [ expandedFunctionCalls , setExpandedFunctionCalls ] = useState <
@@ -109,7 +105,7 @@ function ChatPage() {
109105 streamingMessage,
110106 sendMessage : sendStreamingMessage ,
111107 abortStream,
112- isLoading : isStreamLoading ,
108+ isLoading : isChatStreaming ,
113109 } = useChatStreaming ( {
114110 endpoint : apiEndpoint ,
115111 onComplete : ( message , responseId ) => {
@@ -169,7 +165,7 @@ function ChatPage() {
169165 useEffect ( ( ) => {
170166 let timeoutId : NodeJS . Timeout | null = null ;
171167
172- if ( isStreamLoading && ! streamingMessage ) {
168+ if ( isChatStreaming && ! streamingMessage ) {
173169 timeoutId = setTimeout ( ( ) => {
174170 setWaitingTooLong ( true ) ;
175171 } , 20000 ) ; // 20 seconds
@@ -180,7 +176,7 @@ function ChatPage() {
180176 return ( ) => {
181177 if ( timeoutId ) clearTimeout ( timeoutId ) ;
182178 } ;
183- } , [ isStreamLoading , streamingMessage ] ) ;
179+ } , [ isChatStreaming , streamingMessage ] ) ;
184180
185181 const handleEndpointChange = ( newEndpoint : EndpointType ) => {
186182 setEndpoint ( newEndpoint ) ;
@@ -328,13 +324,7 @@ function ChatPage() {
328324 // Abort any in-flight streaming so it doesn't bleed into new chat
329325 abortStream ( ) ;
330326 // Reset chat UI even if context state was already 'new'
331- setMessages ( [
332- {
333- role : "assistant" ,
334- content : "How can I assist?" ,
335- timestamp : new Date ( ) ,
336- } ,
337- ] ) ;
327+ setMessages ( [ INITIAL_ASSISTANT_MESSAGE ] ) ;
338328 setInput ( "" ) ;
339329 setExpandedFunctionCalls ( new Set ( ) ) ;
340330 setIsFilterHighlighted ( false ) ;
@@ -372,7 +362,7 @@ function ChatPage() {
372362
373363 if (
374364 conversationData ?. messages &&
375- ( isNewConversation || ( ! isStreamLoading && hasMessageCountChanged ) ) &&
365+ ( isNewConversation || ( ! isChatStreaming && hasMessageCountChanged ) ) &&
376366 ! isUserInteracting &&
377367 ! isForkingInProgress
378368 ) {
@@ -564,21 +554,15 @@ function ChatPage() {
564554 isUserInteracting ,
565555 isForkingInProgress ,
566556 setPreviousResponseIds ,
567- isStreamLoading ,
557+ isChatStreaming ,
568558 messages . length ,
569559 ] ) ;
570560
571561 // Handle new conversation creation - only reset messages when placeholderConversation is set
572562 useEffect ( ( ) => {
573563 if ( placeholderConversation && currentConversationId === null ) {
574564 console . log ( "Starting new conversation" ) ;
575- setMessages ( [
576- {
577- role : "assistant" ,
578- content : "How can I assist?" ,
579- timestamp : new Date ( ) ,
580- } ,
581- ] ) ;
565+ setMessages ( [ INITIAL_ASSISTANT_MESSAGE ] ) ;
582566 lastLoadedConversationRef . current = null ;
583567
584568 // Focus input when starting a new conversation
@@ -588,93 +572,6 @@ function ChatPage() {
588572 }
589573 } , [ placeholderConversation , currentConversationId ] ) ;
590574
591- // Listen for file upload events from navigation
592- useEffect ( ( ) => {
593- const handleFileUploadStart = ( event : CustomEvent ) => {
594- const { filename } = event . detail ;
595- console . log ( "Chat page received file upload start event:" , filename ) ;
596-
597- setLoading ( true ) ;
598- setIsUploading ( true ) ;
599- setUploadedFile ( null ) ; // Clear previous file
600- } ;
601-
602- const handleFileUploaded = ( event : CustomEvent ) => {
603- const { result } = event . detail ;
604- console . log ( "Chat page received file upload event:" , result ) ;
605-
606- setUploadedFile ( null ) ; // Clear file after upload
607-
608- // Update the response ID for this endpoint
609- if ( result . response_id ) {
610- setPreviousResponseIds ( ( prev ) => ( {
611- ...prev ,
612- [ endpoint ] : result . response_id ,
613- } ) ) ;
614- }
615- } ;
616-
617- const handleFileUploadComplete = ( ) => {
618- console . log ( "Chat page received file upload complete event" ) ;
619- setLoading ( false ) ;
620- setIsUploading ( false ) ;
621- } ;
622-
623- const handleFileUploadError = ( event : CustomEvent ) => {
624- const { filename, error } = event . detail ;
625- console . log (
626- "Chat page received file upload error event:" ,
627- filename ,
628- error ,
629- ) ;
630-
631- // Replace the last message with error message
632- const errorMessage : Message = {
633- role : "assistant" ,
634- content : `❌ Upload failed for **${ filename } **: ${ error } ` ,
635- timestamp : new Date ( ) ,
636- } ;
637- setMessages ( ( prev ) => [ ...prev . slice ( 0 , - 1 ) , errorMessage ] ) ;
638- setUploadedFile ( null ) ; // Clear file on error
639- } ;
640-
641- window . addEventListener (
642- "fileUploadStart" ,
643- handleFileUploadStart as EventListener ,
644- ) ;
645- window . addEventListener (
646- "fileUploaded" ,
647- handleFileUploaded as EventListener ,
648- ) ;
649- window . addEventListener (
650- "fileUploadComplete" ,
651- handleFileUploadComplete as EventListener ,
652- ) ;
653- window . addEventListener (
654- "fileUploadError" ,
655- handleFileUploadError as EventListener ,
656- ) ;
657-
658- return ( ) => {
659- window . removeEventListener (
660- "fileUploadStart" ,
661- handleFileUploadStart as EventListener ,
662- ) ;
663- window . removeEventListener (
664- "fileUploaded" ,
665- handleFileUploaded as EventListener ,
666- ) ;
667- window . removeEventListener (
668- "fileUploadComplete" ,
669- handleFileUploadComplete as EventListener ,
670- ) ;
671- window . removeEventListener (
672- "fileUploadError" ,
673- handleFileUploadError as EventListener ,
674- ) ;
675- } ;
676- } , [ endpoint , setPreviousResponseIds , setLoading ] ) ;
677-
678575 // Check onboarding completion
679576
680577 // Check if onboarding is complete (current_step >= 4 means complete)
@@ -1142,9 +1039,11 @@ function ChatPage() {
11421039 isInitialGreeting = {
11431040 index === 0 &&
11441041 messages . length === 1 &&
1145- message . content === "How can I assist?"
1042+ message . content ===
1043+ INITIAL_ASSISTANT_MESSAGE . content
11461044 }
11471045 usage = { message . usage }
1046+ timestamp = { message . timestamp }
11481047 />
11491048 ) }
11501049 </ div >
0 commit comments