Skip to content

Commit cbc8ccb

Browse files
authored
Merge pull request #2121 from ModelEngine-Group/pyh/fix_duplicated_func_call_develop
♻️ Repeated calls in chat page (Agent list & conversation list) #2082
2 parents 79c2f9d + 38c630c commit cbc8ccb

File tree

5 files changed

+47
-17
lines changed

5 files changed

+47
-17
lines changed

frontend/app/[locale]/chat/components/chatAgentSelector.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export function ChatAgentSelector({
1515
onAgentSelect,
1616
disabled = false,
1717
isInitialMode = false,
18+
agents: propAgents,
1819
}: ChatAgentSelectorProps) {
1920
const [agents, setAgents] = useState<Agent[]>([]);
2021
const [isOpen, setIsOpen] = useState(false);
@@ -94,8 +95,9 @@ export function ChatAgentSelector({
9495
};
9596

9697
useEffect(() => {
97-
loadAgents();
98-
}, []);
98+
// Only load agents if not provided via props or if props is explicitly empty
99+
setAgents(propAgents || []);
100+
}, [propAgents]);
99101

100102
// Execute auto-selection logic when agents are loaded
101103
useEffect(() => {

frontend/app/[locale]/chat/components/chatInput.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import { useConfig } from "@/hooks/useConfig";
2323
import { extractColorsFromUri } from "@/lib/avatar";
2424
import log from "@/lib/logger";
2525
import { chatConfig } from "@/const/chatConfig";
26-
import { FilePreview } from "@/types/chat";
26+
import { FilePreview, Agent } from "@/types/chat";
2727

2828
import { ChatAgentSelector } from "./chatAgentSelector";
2929

@@ -305,6 +305,7 @@ interface ChatInputProps {
305305
onAttachmentsChange?: (attachments: FilePreview[]) => void;
306306
selectedAgentId?: number | null;
307307
onAgentSelect?: (agentId: number | null) => void;
308+
cachedAgents?: Agent[]; // Optional cached agents to avoid repeated API calls
308309
}
309310

310311
export function ChatInput({
@@ -317,6 +318,7 @@ export function ChatInput({
317318
onStop,
318319
onKeyDown,
319320
onRecordingStatusChange,
321+
cachedAgents,
320322
onFileUpload,
321323
onImageUpload,
322324
attachments = [],
@@ -1021,6 +1023,7 @@ export function ChatInput({
10211023
onAgentSelect={onAgentSelect || (() => {})}
10221024
disabled={isLoading || isStreaming}
10231025
isInitialMode={isInitialMode}
1026+
agents={cachedAgents}
10241027
/>
10251028
</div>
10261029

frontend/app/[locale]/chat/internal/chatInterface.tsx

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { useAuth } from "@/hooks/useAuth";
1515
import { conversationService } from "@/services/conversationService";
1616
import { storageService, convertImageUrlToApiUrl } from "@/services/storageService";
1717
import { useConversationManagement } from "@/hooks/chat/useConversationManagement";
18+
import { fetchAllAgents } from "@/services/agentConfigService";
1819

1920
import { ChatSidebar } from "../components/chatLeftSidebar";
2021
import { 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

frontend/app/[locale]/chat/streaming/chatStreamMain.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ export function ChatStreamMain({
4040
onAgentSelect,
4141
onCitationHover,
4242
onScroll,
43+
cachedAgents,
4344
}: ChatStreamMainProps) {
4445
const { t } = useTranslation();
4546
// Animation variants for ChatInput
@@ -375,6 +376,7 @@ export function ChatStreamMain({
375376
onImageUpload={onImageUpload}
376377
selectedAgentId={selectedAgentId}
377378
onAgentSelect={onAgentSelect}
379+
cachedAgents={cachedAgents}
378380
/>
379381
</motion.div>
380382
</AnimatePresence>
@@ -472,6 +474,7 @@ export function ChatStreamMain({
472474
onImageUpload={onImageUpload}
473475
selectedAgentId={selectedAgentId}
474476
onAgentSelect={onAgentSelect}
477+
cachedAgents={cachedAgents}
475478
/>
476479
</motion.div>
477480
</AnimatePresence>

frontend/types/chat.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ export interface ChatAgentSelectorProps {
6363
onAgentSelect: (agentId: number | null) => void;
6464
disabled?: boolean;
6565
isInitialMode?: boolean;
66+
agents?: Agent[]; // Optional pre-loaded agents to avoid repeated API calls
6667
}
6768

6869
// Search result type
@@ -173,6 +174,7 @@ export interface ChatStreamMainProps {
173174
onAgentSelect?: (agentId: number | null) => void;
174175
onCitationHover?: () => void;
175176
onScroll?: () => void;
177+
cachedAgents?: Agent[]; // Optional cached agents to avoid repeated API calls
176178
}
177179

178180
// Card item type for task window

0 commit comments

Comments
 (0)