@@ -15,6 +15,7 @@ import { useAuth } from "@/hooks/useAuth";
1515import { conversationService } from "@/services/conversationService" ;
1616import { storageService , convertImageUrlToApiUrl } from "@/services/storageService" ;
1717import { useConversationManagement } from "@/hooks/chat/useConversationManagement" ;
18+ import { fetchAllAgents } from "@/services/agentConfigService" ;
1819
1920import { ChatSidebar } from "../components/chatLeftSidebar" ;
2021import { FilePreview } from "@/types/chat" ;
@@ -133,6 +134,11 @@ export function ChatInterface() {
133134 // Add agent selection state
134135 const [ selectedAgentId , setSelectedAgentId ] = useState < number | null > ( null ) ;
135136
137+ // Add global cache states to avoid repeated API calls
138+ const [ cachedAgents , setCachedAgents ] = useState < any [ ] > ( [ ] ) ;
139+ const [ agentsLoaded , setAgentsLoaded ] = useState ( false ) ;
140+ const [ deploymentVersionLoaded , setDeploymentVersionLoaded ] = useState ( false ) ;
141+
136142 // Reset scroll to bottom state
137143 useEffect ( ( ) => {
138144 if ( shouldScrollToBottom ) {
@@ -173,6 +179,24 @@ export function ChatInterface() {
173179 setSidebarOpen ( ! sidebarOpen ) ;
174180 } ;
175181
182+ // Load agents only once and cache the result
183+ const loadAgentsOnce = async ( ) => {
184+ if ( agentsLoaded ) return ; // Already loaded, skip
185+
186+ try {
187+ const result = await fetchAllAgents ( ) ;
188+ if ( result . success ) {
189+ setCachedAgents ( result . data ) ;
190+ setAgentsLoaded ( true ) ;
191+ log . log ( "Agent list loaded and cached successfully" ) ;
192+ } else {
193+ log . error ( "Failed to load agent list:" , result . message ) ;
194+ }
195+ } catch ( error ) {
196+ log . error ( "Failed to load agent list:" , error ) ;
197+ }
198+ } ;
199+
176200 // Handle right panel toggle - keep it simple and clear
177201 const toggleRightPanel = ( ) => {
178202 setShowRightPanel ( ! showRightPanel ) ;
@@ -182,6 +206,11 @@ export function ChatInterface() {
182206 if ( ! conversationManagement . initialized . current ) {
183207 conversationManagement . initialized . current = true ;
184208
209+ // Load agent list only once when component initializes
210+ if ( ! agentsLoaded ) {
211+ loadAgentsOnce ( ) ;
212+ }
213+
185214 // Get conversation history list, but don't auto-select the latest conversation
186215 conversationManagement . fetchConversationList ( )
187216 . then ( ( dialogData ) => {
@@ -888,13 +917,8 @@ export function ChatInterface() {
888917 setShouldScrollToBottom ( false ) ;
889918 } , 1000 ) ;
890919
891- // Refresh history list
892- conversationManagement . fetchConversationList ( ) . catch ( ( err ) => {
893- log . error (
894- t ( "chatInterface.refreshDialogListFailedButContinue" ) ,
895- err
896- ) ;
897- } ) ;
920+ // Note: Removed unnecessary conversation list refresh when loading historical messages
921+ // Only refresh when creating, deleting, or renaming conversations
898922 } else {
899923 // No longer empty cache, only prompt no history messages
900924 conversationManagement . setConversationLoadErrorForId (
@@ -1020,13 +1044,8 @@ export function ChatInterface() {
10201044 setShouldScrollToBottom ( false ) ;
10211045 } , 1000 ) ;
10221046
1023- // Refresh history list
1024- conversationManagement . fetchConversationList ( ) . catch ( ( err ) => {
1025- log . error (
1026- t ( "chatInterface.refreshDialogListFailedButContinue" ) ,
1027- err
1028- ) ;
1029- } ) ;
1047+ // Note: Removed unnecessary conversation list refresh when loading historical messages
1048+ // Only refresh when creating, deleting, or renaming conversations
10301049 } else {
10311050 // No longer empty cache, only prompt no history messages
10321051 conversationManagement . setConversationLoadErrorForId (
@@ -1456,6 +1475,7 @@ export function ChatInterface() {
14561475 onAgentSelect = { setSelectedAgentId }
14571476 onCitationHover = { clearCompletedIndicator }
14581477 onScroll = { clearCompletedIndicator }
1478+ cachedAgents = { cachedAgents }
14591479 />
14601480 </ div >
14611481
0 commit comments