fix(chat): 阻止同会话并发 Agent Run 导致消息错乱

前端 submit 改用 chatStateRef 同步拦截连发;后端 createRun 检测同 session 活跃任务并返回 409,避免多条回复交错落库。

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
john
2026-07-07 11:48:59 +08:00
parent ac520d8ae5
commit 1e4f432754
5 changed files with 162 additions and 5 deletions
+9 -5
View File
@@ -1280,11 +1280,13 @@ export function useTKMindChat(
(value) => typeof value === 'string' && value.trim(),
);
if (!text.trim() && normalizedImageUrls.length === 0) return;
// Use the ref here (not the React state) so that rapid back-to-back calls in the
// same render cycle are blocked even before the state update has been re-rendered.
if (
chatState === 'streaming' ||
chatState === 'loading' ||
chatState === 'connecting' ||
chatState === 'waiting'
chatStateRef.current === 'streaming' ||
chatStateRef.current === 'loading' ||
chatStateRef.current === 'connecting' ||
chatStateRef.current === 'waiting'
) return;
const trimmed = text.trim();
@@ -1319,6 +1321,9 @@ export function useTKMindChat(
messageHistoryLoadedCountRef.current = messagesRef.current.length;
setMessages(messagesRef.current);
setChatState('waiting');
// Immediately reflect in the ref so any synchronous re-entry is blocked before
// the next React render cycle runs the useEffect that normally syncs this ref.
chatStateRef.current = 'waiting';
setError(null);
setPendingTool(null);
@@ -1484,7 +1489,6 @@ export function useTKMindChat(
[
notifyInsufficientBalance,
session,
chatState,
grantedSkills,
clearActiveRequestMissingTimer,
subscribeToSession,