Bladeren bron

feat (状态仓库):对接后端会话删除接口
· 将会话删除逻辑迁移至 chatStore.deleteSession,封装为异步方法
· 会话列表(SessionList)中移除直接调用服务的写法,统一使用仓库提供的方法
· 增加异常捕获处理:接口调用失败时,提供兜底方案清理本地状态
· 实现批量删除功能,追踪每条会话的删除成功 / 失败结果
· 更新 ChatStoreState 类型声明,适配异步 deleteSession 方法
· 简化用户提示文案,统一删除流程反馈话术
· 保证逻辑顺序:优先尝试调用后端执行文档清理,成功后再移除本地状态

Zhang Yice 1 maand geleden
bovenliggende
commit
34a7290f43
3 gewijzigde bestanden met toevoegingen van 62 en 34 verwijderingen
  1. 18 17
      src/components/SessionList/SessionList.tsx
  2. 42 15
      src/stores/chatStore.ts
  3. 2 2
      src/types/store.ts

+ 18 - 17
src/components/SessionList/SessionList.tsx

@@ -180,14 +180,10 @@ const SessionList: React.FC<SessionListProps> = ({ onSessionLoad }) => {
180 180
     e.stopPropagation();
181 181
     
182 182
     try {
183
-      // 从后端删除会话文档
184
-      const { deleteSessionHistory } = await import('../../services/sessionService');
185
-      const result = await deleteSessionHistory(sessionId);
183
+      // 调用 chatStore 的 deleteSession (内部会调用后端 API)
184
+      await deleteSession(sessionId);
186 185
       
187
-      // 从本地状态删除
188
-      deleteSession(sessionId);
189
-      
190
-      antdMessage.success(`成功删除会话,共删除 ${result.data.deletedCount} 个文档`);
186
+      antdMessage.success('成功删除会话及其关联的所有文档');
191 187
     } catch (error) {
192 188
       console.error('删除会话失败:', error);
193 189
       antdMessage.error('删除会话失败,请重试');
@@ -236,26 +232,31 @@ const SessionList: React.FC<SessionListProps> = ({ onSessionLoad }) => {
236 232
     }
237 233
 
238 234
     try {
239
-      // 从后端批量删除
240
-      const { deleteSessions } = await import('../../services/sessionService');
241
-      const result = await deleteSessions(Array.from(selectedSessionIds));
235
+      let successCount = 0;
236
+      let failedCount = 0;
242 237
       
243
-      // 从本地状态删除成功的会话
244
-      selectedSessionIds.forEach(sessionId => {
245
-        deleteSession(sessionId);
246
-      });
238
+      // 逐个调用 deleteSession (内部会调用后端 API)
239
+      for (const sessionId of Array.from(selectedSessionIds)) {
240
+        try {
241
+          await deleteSession(sessionId);
242
+          successCount++;
243
+        } catch (error) {
244
+          console.error(`删除会话 ${sessionId} 失败:`, error);
245
+          failedCount++;
246
+        }
247
+      }
247 248
 
248 249
       // Reset selection state
249 250
       setSelectedSessionIds(new Set());
250 251
       setIsSelectionMode(false);
251 252
       
252
-      if (result.failedCount > 0) {
253
+      if (failedCount > 0) {
253 254
         antdMessage.warning(
254
-          `删除完成:成功 ${result.successCount} 个,失败 ${result.failedCount} 个`
255
+          `删除完成:成功 ${successCount} 个,失败 ${failedCount} 个`
255 256
         );
256 257
       } else {
257 258
         antdMessage.success(
258
-          `成功删除 ${result.successCount} 个会话,共删除 ${result.totalDeletedDocuments} 个文档`
259
+          `成功删除 ${successCount} 个会话及其关联的所有文档`
259 260
         );
260 261
       }
261 262
     } catch (error) {

+ 42 - 15
src/stores/chatStore.ts

@@ -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
   /**

+ 2 - 2
src/types/store.ts

@@ -58,8 +58,8 @@ export interface ChatStoreState {
58 58
   createSession: () => string;
59 59
   /** Load an existing session by ID */
60 60
   loadSession: (sessionId: string) => void;
61
-  /** Delete a session by ID */
62
-  deleteSession: (sessionId: string) => void;
61
+  /** Delete a session by ID (calls backend API to delete documents) */
62
+  deleteSession: (sessionId: string) => Promise<void>;
63 63
   /** Send a message to AI and get response */
64 64
   sendMessage: (content: string) => Promise<void>;
65 65
   /** Add a message to the current session */