Просмотр исходного кода

fix(studio): skip session fetch when local messages exist

loadSessionDetail 原本只在 set 合并阶段保护本地消息不被覆盖,
但还是会发起远端请求。改为进入 action 就检查:本地已有消息直接
return,避免在流式中或消息未持久化时把服务器返回的"无用户消息"
版本覆盖过来。

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
fanghanjun 6 месяцев назад
Родитель
Сommit
10eb42dc20
1 измененных файлов с 6 добавлено и 0 удалено
  1. 6 0
      packages/studio/src/store/chat/slices/message/action.ts

+ 6 - 0
packages/studio/src/store/chat/slices/message/action.ts

@@ -207,6 +207,10 @@ export const createMessageSlice: StateCreator<ChatStore, [], [], MessageActions>
   },
 
   loadSessionDetail: async (sessionId) => {
+    // 本地已有消息 → 不拉取远端,避免流式中或未持久化的消息被覆盖。
+    const existing = get().sessions[sessionId];
+    if (existing && existing.messages.length > 0) return;
+
     try {
       const data = await fetchJson<SessionResponse>(`/sessions/${sessionId}`);
       const detail = data.session;
@@ -216,6 +220,8 @@ export const createMessageSlice: StateCreator<ChatStore, [], [], MessageActions>
 
       set((state) => {
         const runtime = state.sessions[detailSessionId];
+        // set 执行到这里可能已有本地消息写入(比如并发 sendMessage),再查一次。
+        if (runtime && runtime.messages.length > 0) return {};
         const nextBookId = detail.bookId ?? runtime?.bookId ?? null;
         return {
           sessions: {