@@ -12,6 +12,10 @@ import log from "@/lib/logger";
1212// @ts -ignore
1313const fetch = fetchWithAuth ;
1414
15+ // Simple in-memory cache to ensure conversation list is fetched once per app session
16+ // and reused by callers. Provide an invalidate function if callers need to refresh.
17+ let conversationListCache : { ts : number ; data : ConversationListItem [ ] } | null = null ;
18+
1519// This helper function now ALWAYS connects through the current host and port.
1620// This relies on our custom `server.js` to handle the proxying in all environments.
1721const getWebSocketUrl = ( endpoint : string ) : string => {
@@ -24,15 +28,34 @@ const getWebSocketUrl = (endpoint: string): string => {
2428export const conversationService = {
2529 // Get conversation list
2630 async getList ( ) : Promise < ConversationListItem [ ] > {
27- const response = await fetch ( API_ENDPOINTS . conversation . list ) ;
31+ try {
32+ // If we already fetched the conversation list earlier in this session, return it.
33+ if ( conversationListCache ) {
34+ return conversationListCache . data ;
35+ }
2836
29- const data = await response . json ( ) as ConversationListResponse ;
30-
31- if ( data . code === 0 ) {
32- return data . data || [ ] ;
37+ const response = await fetch ( API_ENDPOINTS . conversation . list ) ;
38+ const data = ( await response . json ( ) ) as ConversationListResponse ;
39+
40+ if ( data . code === 0 ) {
41+ const list = data . data || [ ] ;
42+ conversationListCache = { ts : Date . now ( ) , data : list } ;
43+ return list ;
44+ }
45+
46+ throw new ApiError ( data . code , data . message ) ;
47+ } catch ( error ) {
48+ // On error, if we have cached data return it as a fallback
49+ if ( conversationListCache ) {
50+ return conversationListCache . data ;
51+ }
52+ throw error ;
3353 }
34-
35- throw new ApiError ( data . code , data . message ) ;
54+ } ,
55+
56+ // Invalidate the cached conversation list (call when you need to force refresh)
57+ invalidateListCache ( ) {
58+ conversationListCache = null ;
3659 } ,
3760
3861 // Create new conversation
@@ -48,6 +71,8 @@ export const conversationService = {
4871 const data = await response . json ( ) ;
4972
5073 if ( data . code === 0 ) {
74+ // Invalidate conversation list cache so callers will fetch updated list
75+ conversationListCache = null ;
5176 return data . data ;
5277 }
5378
@@ -68,6 +93,8 @@ export const conversationService = {
6893 const data = await response . json ( ) ;
6994
7095 if ( data . code === 0 ) {
96+ // Invalidate conversation list cache since titles changed
97+ conversationListCache = null ;
7198 return data . data ;
7299 }
73100
@@ -114,6 +141,8 @@ export const conversationService = {
114141 const data = await response . json ( ) ;
115142
116143 if ( data . code === 0 ) {
144+ // Invalidate conversation list cache because an item was removed
145+ conversationListCache = null ;
117146 return true ;
118147 }
119148
@@ -802,7 +831,21 @@ export const conversationService = {
802831 const data = await response . json ( ) ;
803832
804833 if ( data . code === 0 ) {
805- return data . data ;
834+ const title = data . data ;
835+ // If we have a cached conversation list, update the corresponding item's title
836+ if ( conversationListCache && Array . isArray ( conversationListCache . data ) ) {
837+ const idx = conversationListCache . data . findIndex (
838+ ( c ) => c . conversation_id === params . conversation_id
839+ ) ;
840+ if ( idx >= 0 ) {
841+ // Update in-place and refresh timestamp
842+ conversationListCache . data [ idx ] . conversation_title =
843+ title || conversationListCache . data [ idx ] . conversation_title ;
844+ conversationListCache . ts = Date . now ( ) ;
845+ }
846+ }
847+
848+ return title ;
806849 }
807850
808851 throw new ApiError ( data . code , data . message ) ;
0 commit comments