|
|
@@ -230,24 +230,51 @@ export const useChatStore = create<ChatStoreState>((set, get) => ({
|
|
230
|
230
|
/**
|
|
231
|
231
|
* Delete a session by ID
|
|
232
|
232
|
*
|
|
|
233
|
+ * ⭐ 修改: 调用后端 API 删除会话及其关联的所有文档
|
|
|
234
|
+ *
|
|
233
|
235
|
* @param sessionId - Session ID to delete
|
|
234
|
236
|
*/
|
|
235
|
|
- deleteSession: (sessionId: string) => {
|
|
236
|
|
- set((state) => {
|
|
237
|
|
- const updatedSessions = state.sessions.filter((s) => s.id !== sessionId);
|
|
238
|
|
- saveSessionsToStorage(updatedSessions);
|
|
239
|
|
-
|
|
240
|
|
- // If deleting current session, clear current session
|
|
241
|
|
- const newCurrentSessionId = state.currentSessionId === sessionId
|
|
242
|
|
- ? null
|
|
243
|
|
- : state.currentSessionId;
|
|
|
237
|
+ deleteSession: async (sessionId: string) => {
|
|
|
238
|
+ try {
|
|
|
239
|
+ // 调用后端 API 删除会话的所有文档
|
|
|
240
|
+ const { deleteSessionHistory } = await import('../services/sessionService');
|
|
|
241
|
+ await deleteSessionHistory(sessionId);
|
|
244
|
242
|
|
|
245
|
|
- return {
|
|
246
|
|
- currentSessionId: newCurrentSessionId,
|
|
247
|
|
- sessions: updatedSessions,
|
|
248
|
|
- messages: newCurrentSessionId === null ? [] : state.messages,
|
|
249
|
|
- };
|
|
250
|
|
- });
|
|
|
243
|
+ // 删除成功后,从本地状态中移除
|
|
|
244
|
+ set((state) => {
|
|
|
245
|
+ const updatedSessions = state.sessions.filter((s) => s.id !== sessionId);
|
|
|
246
|
+ saveSessionsToStorage(updatedSessions);
|
|
|
247
|
+
|
|
|
248
|
+ // If deleting current session, clear current session
|
|
|
249
|
+ const newCurrentSessionId = state.currentSessionId === sessionId
|
|
|
250
|
+ ? null
|
|
|
251
|
+ : state.currentSessionId;
|
|
|
252
|
+
|
|
|
253
|
+ return {
|
|
|
254
|
+ currentSessionId: newCurrentSessionId,
|
|
|
255
|
+ sessions: updatedSessions,
|
|
|
256
|
+ messages: newCurrentSessionId === null ? [] : state.messages,
|
|
|
257
|
+ };
|
|
|
258
|
+ });
|
|
|
259
|
+ } catch (error) {
|
|
|
260
|
+ console.error('删除会话失败:', error);
|
|
|
261
|
+ // 即使后端删除失败,也从本地状态中移除(因为可能是网络问题)
|
|
|
262
|
+ set((state) => {
|
|
|
263
|
+ const updatedSessions = state.sessions.filter((s) => s.id !== sessionId);
|
|
|
264
|
+ saveSessionsToStorage(updatedSessions);
|
|
|
265
|
+
|
|
|
266
|
+ const newCurrentSessionId = state.currentSessionId === sessionId
|
|
|
267
|
+ ? null
|
|
|
268
|
+ : state.currentSessionId;
|
|
|
269
|
+
|
|
|
270
|
+ return {
|
|
|
271
|
+ currentSessionId: newCurrentSessionId,
|
|
|
272
|
+ sessions: updatedSessions,
|
|
|
273
|
+ messages: newCurrentSessionId === null ? [] : state.messages,
|
|
|
274
|
+ };
|
|
|
275
|
+ });
|
|
|
276
|
+ throw error; // 重新抛出错误供调用者处理
|
|
|
277
|
+ }
|
|
251
|
278
|
},
|
|
252
|
279
|
|
|
253
|
280
|
/**
|