@@ -4,6 +4,7 @@ import { useGetFlowId } from "@/components/core/playgroundComponent/hooks/use-ge
44import { useGetMessagesQuery } from "@/controllers/API/queries/messages" ;
55import type { ChatMessageType } from "@/types/chat" ;
66import type { Message } from "@/types/messages" ;
7+ import { isMessageForSession } from "../../utils/session-filter" ;
78import sortSenderMessages from "../utils/sort-sender-messages" ;
89
910export const useChatHistory = ( visibleSession : string | null ) => {
@@ -45,17 +46,20 @@ export const useChatHistory = (visibleSession: string | null) => {
4546 if ( queryData && typeof queryData === "object" && "rows" in queryData ) {
4647 const rowsData = queryData . rows as { data ?: Message [ ] } | undefined ;
4748 if ( rowsData && typeof rowsData === "object" && "data" in rowsData ) {
48- const backendMessages = rowsData . data || [ ] ;
49+ const backendMessages = ( rowsData . data || [ ] ) . filter ( ( msg : Message ) =>
50+ isMessageForSession ( msg , currentFlowId , visibleSession ) ,
51+ ) ;
52+
4953 const existingCache =
5054 queryClient . getQueryData < Message [ ] > ( sessionCacheKey ) || [ ] ;
5155
52- // Only initialize if cache is empty and we have backend messages
56+ // Only initialize if cache is empty and we have backend messages for this session
5357 if ( existingCache . length === 0 && backendMessages . length > 0 ) {
5458 queryClient . setQueryData ( sessionCacheKey , backendMessages ) ;
5559 }
5660 }
5761 }
58- } , [ queryData , queryClient , sessionCacheKey ] ) ;
62+ } , [ queryData , queryClient , sessionCacheKey , currentFlowId , visibleSession ] ) ;
5963
6064 // Use session cache as the single source of truth
6165 // updateMessage and addUserMessage handle all updates (placeholders, streaming, etc.)
@@ -65,21 +69,10 @@ export const useChatHistory = (visibleSession: string | null) => {
6569 const chatHistory = useMemo ( ( ) => {
6670 // Filter messages for current session
6771 const filteredMessages : ChatMessageType [ ] = messages
68- . filter ( ( message : Message ) => {
69- const isCurrentFlow = message . flow_id === currentFlowId ;
70- // If visibleSession is the flow_id, it means we are in the default session
71- // In the default session, we show messages that have the same session_id as the flow_id
72- // OR messages that have NO session_id (legacy behavior)
73- if ( visibleSession === currentFlowId ) {
74- const matches =
75- isCurrentFlow &&
76- ( message . session_id === visibleSession || ! message . session_id ) ;
77- return matches ;
78- }
79- const matches = isCurrentFlow && message . session_id === visibleSession ;
80- return matches ;
81- } )
82- . map ( ( message : Message ) => {
72+ . filter ( ( message : Message ) =>
73+ isMessageForSession ( message , currentFlowId , visibleSession ) ,
74+ )
75+ . map ( ( message : Message ) : ChatMessageType => {
8376 let files = message . files ;
8477 // Handle the "[]" case, empty string, or already parsed array
8578 if ( Array . isArray ( files ) ) {
@@ -96,6 +89,28 @@ export const useChatHistory = (visibleSession: string | null) => {
9689 }
9790 const messageText = message . text || "" ;
9891
92+ // Convert Message.properties to ChatMessageType.properties (PropertiesType)
93+ // Properties are now properly typed in Message, no cast needed
94+ let properties : ChatMessageType [ "properties" ] = undefined ;
95+ if ( message . properties ?. source ?. id ) {
96+ properties = {
97+ source : {
98+ id : message . properties . source . id ,
99+ display_name : message . properties . source . display_name || "" ,
100+ source : message . properties . source . source || "" ,
101+ } ,
102+ state : message . properties . state ,
103+ icon : message . properties . icon ,
104+ background_color : message . properties . background_color ,
105+ text_color : message . properties . text_color ,
106+ targets : message . properties . targets ,
107+ edited : message . properties . edited ,
108+ allow_markdown : message . properties . allow_markdown ,
109+ positive_feedback : message . properties . positive_feedback ,
110+ build_duration : message . properties . build_duration ,
111+ } ;
112+ }
113+
99114 return {
100115 isSend : message . sender === "User" ,
101116 message : messageText ,
@@ -110,7 +125,7 @@ export const useChatHistory = (visibleSession: string | null) => {
110125 text_color : message . text_color ,
111126 content_blocks : message . content_blocks ,
112127 category : message . category ,
113- properties : message . properties ,
128+ properties : properties ,
114129 } ;
115130 } ) ;
116131
0 commit comments