Skip to content

Commit 853ea38

Browse files
authored
fix: solve the problem of modifying the assistant in the chat (#476)
* refactor: refactored chat code * fix: Solve the problem of modifying the assistant in the chat * docs: update notes * docs: update notes
1 parent 4e127f8 commit 853ea38

14 files changed

Lines changed: 270 additions & 349 deletions

File tree

docs/content.en/docs/release-notes/_index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Information about release notes of Coco Server is provided here.
2222
### ✈️ Improvements
2323

2424
- chore: adjust list error message #475
25+
- fix: solve the problem of modifying the assistant in the chat #476
2526

2627
## 0.4.0 (2025-04-27)
2728

src/api/axiosRequest.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ export const handleApiError = (error: any) => {
5959
message = error.message;
6060
}
6161

62+
console.error(error);
6263
addError(message, "error");
6364
return error;
6465
};

src/components/Assistant/Chat.tsx

Lines changed: 38 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -164,10 +164,10 @@ const ChatAI = memo(
164164
isMCPActive,
165165
changeInput,
166166
websocketSessionId,
167-
showChatHistory,
167+
showChatHistory
168168
);
169169

170-
const { dealMsg, messageTimeoutRef } = useMessageHandler(
170+
const { dealMsg } = useMessageHandler(
171171
curIdRef,
172172
setCurChatEnd,
173173
setTimedoutShow,
@@ -184,7 +184,7 @@ const ChatAI = memo(
184184
}, [dealMsg, updateDealMsg]);
185185

186186
const clearChat = useCallback(() => {
187-
console.log("clearChat");
187+
//console.log("clearChat");
188188
setTimedoutShow(false);
189189
chatClose(activeChat);
190190
setActiveChat(undefined);
@@ -193,15 +193,12 @@ const ChatAI = memo(
193193
}, [
194194
activeChat,
195195
chatClose,
196-
clearChatPage,
197-
setCurChatEnd,
198-
setTimedoutShow,
199196
]);
200197

201198
const init = useCallback(
202199
async (value: string) => {
203200
try {
204-
console.log("init", isLogin, curChatEnd, activeChat?._id);
201+
//console.log("init", isLogin, curChatEnd, activeChat?._id);
205202
if (!isLogin) {
206203
addError("Please login to continue chatting");
207204
return;
@@ -222,7 +219,7 @@ const ChatAI = memo(
222219
[
223220
isLogin,
224221
curChatEnd,
225-
activeChat,
222+
activeChat?._id,
226223
createNewChat,
227224
handleSendMessage,
228225
websocketSessionId,
@@ -234,21 +231,6 @@ const ChatAI = memo(
234231
createChatWindow(createWin);
235232
}, [createChatWindow, createWin]);
236233

237-
useEffect(() => {
238-
setCurChatEnd(true);
239-
return () => {
240-
if (messageTimeoutRef.current) {
241-
clearTimeout(messageTimeoutRef.current);
242-
}
243-
Promise.resolve().then(() => {
244-
chatClose(activeChat);
245-
setActiveChat(undefined);
246-
setCurChatEnd(true);
247-
disconnectWS();
248-
});
249-
};
250-
}, [chatClose, setCurChatEnd]);
251-
252234
const onSelectChat = useCallback(
253235
async (chat: Chat) => {
254236
setTimedoutShow(false);
@@ -261,7 +243,6 @@ const ChatAI = memo(
261243
}
262244
},
263245
[
264-
clearAllChunkData,
265246
cancelChat,
266247
activeChat,
267248
chatClose,
@@ -274,19 +255,21 @@ const ChatAI = memo(
274255
(chatId: string) => {
275256
handleDelete(chatId);
276257

277-
setChats((prev) => prev.filter((chat) => chat._id !== chatId));
258+
setChats((prev) => {
259+
const updatedChats = prev.filter((chat) => chat._id !== chatId);
278260

279-
if (activeChat?._id === chatId) {
280-
const remainingChats = chats.filter((chat) => chat._id !== chatId);
281-
282-
if (remainingChats.length > 0) {
283-
setActiveChat(remainingChats[0]);
284-
} else {
285-
init("");
261+
if (activeChat?._id === chatId) {
262+
if (updatedChats.length > 0) {
263+
setActiveChat(updatedChats[0]);
264+
} else {
265+
init("");
266+
}
286267
}
287-
}
268+
269+
return updatedChats;
270+
});
288271
},
289-
[activeChat, chats, init, setActiveChat]
272+
[activeChat?._id, handleDelete, init]
290273
);
291274

292275
const handleOutsideClick = useCallback((e: MouseEvent) => {
@@ -317,38 +300,33 @@ const ChatAI = memo(
317300
!isSidebarOpenChat && getChatHistory();
318301
}, [isSidebarOpenChat, setIsSidebarOpen, getChatHistory]);
319302

320-
const renameChat = (chatId: string, title: string) => {
321-
setChats((prev) => {
322-
const updatedChats = prev.map((item) => {
323-
if (item._id !== chatId) return item;
303+
const renameChat = useCallback(
304+
(chatId: string, title: string) => {
305+
setChats((prev) => {
306+
const chatIndex = prev.findIndex((chat) => chat._id === chatId);
307+
if (chatIndex === -1) return prev;
324308

325-
return { ...item, _source: { ...item._source, title } };
326-
});
309+
const modifiedChat = {
310+
...prev[chatIndex],
311+
_source: { ...prev[chatIndex]._source, title },
312+
};
327313

328-
const modifiedChat = updatedChats.find((item) => {
329-
return item._id === chatId;
314+
const result = [...prev];
315+
result.splice(chatIndex, 1);
316+
return [modifiedChat, ...result];
330317
});
331318

332-
if (!modifiedChat) {
333-
return updatedChats;
319+
if (activeChat?._id === chatId) {
320+
setActiveChat((prev) => {
321+
if (!prev) return prev;
322+
return { ...prev, _source: { ...prev._source, title } };
323+
});
334324
}
335325

336-
return [
337-
modifiedChat,
338-
...updatedChats.filter((item) => item._id !== chatId),
339-
];
340-
});
341-
342-
if (activeChat?._id === chatId) {
343-
setActiveChat((prev) => {
344-
if (!prev) return prev;
345-
346-
return { ...prev, _source: { ...prev._source, title } };
347-
});
348-
}
349-
350-
handleRename(chatId, title);
351-
};
326+
handleRename(chatId, title);
327+
},
328+
[activeChat?._id, handleRename]
329+
);
352330

353331
return (
354332
<div

src/components/Assistant/ChatContent.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ export const ChatContent = ({
5151
});
5252

5353
useEffect(() => {
54-
console.log("activeChat", activeChat);
5554
setCurrentSessionId(activeChat?._id);
5655
}, [activeChat?._id]);
5756

@@ -82,6 +81,8 @@ export const ChatContent = ({
8281
};
8382
}, [scrollToBottom]);
8483

84+
const allMessages = activeChat?.messages || [];
85+
8586
return (
8687
<div className="relative flex flex-col h-full justify-between overflow-hidden">
8788
<div className="flex-1 w-full overflow-x-hidden overflow-y-auto border-t border-[rgba(0,0,0,0.1)] dark:border-[rgba(255,255,255,0.15)] custom-scrollbar relative">
@@ -95,6 +96,7 @@ export const ChatContent = ({
9596
onResend={handleSendMessage}
9697
/>
9798
))}
99+
98100
{(!curChatEnd ||
99101
query_intent ||
100102
tools ||
@@ -110,6 +112,7 @@ export const ChatContent = ({
110112
_id: "current",
111113
_source: {
112114
type: "assistant",
115+
assistant_id: allMessages[allMessages.length-1]?._source?.assistant_id,
113116
message: "",
114117
question: Question,
115118
},

src/components/Assistant/ChatSidebar.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ interface ChatSidebarProps {
1212
// onNewChat: () => void;
1313
onSelectChat: (chat: any) => void;
1414
onDeleteChat: (chatId: string) => void;
15-
fetchChatHistory: () => Promise<void>;
15+
fetchChatHistory: () => void;
1616
onSearch: (keyword: string) => void;
1717
onRename: (chat: any, title: string) => void;
1818
}

src/components/Assistant/ServerList.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ export function ServerList({
7878
fetchServers(true);
7979

8080
const unlisten = platformAdapter.listenEvent("login_or_logout", (event) => {
81-
console.log("Login or Logout:", currentService, event.payload);
81+
//console.log("Login or Logout:", currentService, event.payload);
8282
if (event.payload !== isLogin) {
8383
setIsLogin(!!event.payload);
8484
}

src/components/ChatMessage/markdown.scss

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -679,11 +679,14 @@
679679
white-space: pre-wrap;
680680
}
681681

682+
.markdown-body p br,
682683
.markdown-body table td br,
683684
.markdown-body table th br {
684-
display: block;
685-
content: "";
686-
margin-top: 8px;
685+
display: block !important;
686+
content: "" !important;
687+
margin-top: 8px !important;
688+
height: 1px !important;
689+
background-color: transparent !important;
687690
}
688691

689692
.markdown-body table tr {

src/components/Common/HistoryList/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ interface HistoryListProps {
2828
list: Chat[];
2929
active?: Chat;
3030
onSearch: (keyword: string) => void;
31-
onRefresh: () => Promise<void>;
31+
onRefresh: () => void;
3232
onSelect: (chat: Chat) => void;
3333
onRename: (chatId: string, title: string) => void;
3434
onRemove: (chatId: string) => void;

0 commit comments

Comments
 (0)