🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.
TypeScript agentagent-collaborationagent-harnessloop-engineeringmcppersonal-assistantproactive-agentskillssuper-agenttask-management
🐛 fix(chat): clear input immediately on send to preserve drafts during streaming (#13038)
Inneid2d9e6034e4d
diff --git a/src/features/Conversation/store.test.ts b/src/features/Conversation/store.test.tsindex 3d74f7fe18e..e0e19057b6c 100644--- a/src/features/Conversation/store.test.ts+++ b/src/features/Conversation/store.test.ts@@ -1,6 +1,8 @@import { act } from '@testing-library/react';import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';+import { useChatStore } from '@/store/chat';+import { createStore } from './store';import { type ConversationContext, type ConversationHooks } from './types';@@ -37,6 +39,7 @@ vi.mock('@/store/chat', () => ({internal_dispatchMessage: vi.fn(),internal_dispatchTopic: vi.fn(),internal_execAgentRuntime: vi.fn(),+ sendMessage: vi.fn(),switchTopic: vi.fn(),summaryTopicTitle: vi.fn(),internal_updateTopicLoading: vi.fn(),@@ -297,6 +300,64 @@ describe('ConversationStore', () => {expect(store.getState().editor).toBeNull();});});++ describe('sendMessage', () => {+ it('should preserve draft typed during pending send after streaming completes', async () => {+ const context: ConversationContext = {+ agentId: 'session-1',+ topicId: 'topic-1',+ threadId: null,+ };++ let resolveSend: (value: {+ assistantMessageId: string;+ createdThreadId?: string;+ userMessageId: string;+ }) => void;+ const pendingSend = new Promise<{+ assistantMessageId: string;+ createdThreadId?: string;+ userMessageId: string;+ }>((resolve) => {+ resolveSend = resolve;+ });++ vi.mocked(useChatStore.getState).mockReturnValue({+ ...useChatStore.getState(),+ sendMessage: vi.fn(() => pendingSend),+ } as any);++ const store = createStore({ context });++ act(() => {+ store.getState().updateInputMessage('first message');+ });++ let sendPromise: Promise<void>;+ act(() => {+ sendPromise = store.getState().sendMessage({ message: 'first message' } as any);+ });++ expect(store.getState().inputMessage).toBe('');++ act(() => {+ store.getState().updateInputMessage('draft during streaming');+ });++ expect(store.getState().inputMessage).toBe('draft during streaming');++ resolveSend!({+ assistantMessageId: 'assistant-1',+ userMessageId: 'user-1',+ });++ await act(async () => {+ await sendPromise;+ });++ expect(store.getState().inputMessage).toBe('draft during streaming');+ });+ });});describe('Store Isolation', () => {diff --git a/src/features/Conversation/store/slices/message/action/sendMessage.ts b/src/features/Conversation/store/slices/message/action/sendMessage.tsindex d338c532735..fa1fc8182f0 100644--- a/src/features/Conversation/store/slices/message/action/sendMessage.ts+++ b/src/features/Conversation/store/slices/message/action/sendMessage.ts@@ -31,6 +31,11 @@ export const sendMessage = (}}+ // Keep ConversationStore in sync with the editor, which is cleared immediately on send.+ // Do this before awaiting the full streaming lifecycle so drafts typed during generation+ // are not overwritten when the request completes.+ set({ inputMessage: '' });+// Get global chat storeconst chatStore = useChatStore.getState();@@ -56,8 +61,5 @@ export const sendMessage = (if (hooks.onAfterSendMessage) {await hooks.onAfterSendMessage();}-- // Clear input message- set({ inputMessage: '' });};};diff --git a/src/hooks/useOperationState.test.ts b/src/hooks/useOperationState.test.tsindex 3dbf94a93e9..b5a4a221fe5 100644--- a/src/hooks/useOperationState.test.ts+++ b/src/hooks/useOperationState.test.ts@@ -267,4 +267,45 @@ describe('useOperationState', () => {});});});++ describe('isInputLoading', () => {+ it('should clear loading after cancelling an operation in a null-topic context', () => {+ const context = {+ agentId: 'test-agent-id',+ topicId: null,+ threadId: null,+ } as const;++ const { result } = renderHook(() => useOperationState(context));++ expect(result.current.isInputLoading).toBe(false);++ let operationId: string;+ act(() => {+ operationId = useChatStore.getState().startOperation({+ type: 'execAgentRuntime',+ context: {+ agentId: context.agentId,+ topicId: undefined,+ threadId: undefined,+ },+ }).operationId;+ });++ expect(result.current.isInputLoading).toBe(true);++ act(() => {+ useChatStore.getState().cancelOperations({+ agentId: context.agentId,+ status: 'running',+ threadId: context.threadId ?? undefined,+ topicId: context.topicId ?? undefined,+ type: ['sendMessage', 'execAgentRuntime', 'execServerAgentRuntime'],+ });+ });++ expect(useChatStore.getState().operations[operationId!].status).toBe('cancelled');+ expect(result.current.isInputLoading).toBe(false);+ });+ });});diff --git a/src/store/chat/slices/operation/__tests__/actions.test.ts b/src/store/chat/slices/operation/__tests__/actions.test.tsindex 32a675e8950..93e55e94629 100644--- a/src/store/chat/slices/operation/__tests__/actions.test.ts+++ b/src/store/chat/slices/operation/__tests__/actions.test.ts@@ -396,6 +396,31 @@ describe('Operation Actions', () => {expect(result.current.operations[op2!].status).toBe('cancelled');expect(result.current.operations[op3!].status).toBe('running'); // Not cancelled});++ it('should treat null and undefined topicId as the same context when cancelling', () => {+ const { result } = renderHook(() => useChatStore());++ let operationId: string;++ act(() => {+ operationId = result.current.startOperation({+ type: 'execAgentRuntime',+ context: { agentId: 'session1', topicId: undefined },+ }).operationId;+ });++ act(() => {+ const cancelled = result.current.cancelOperations({+ agentId: 'session1',+ status: 'running',+ topicId: null,+ type: ['sendMessage', 'execAgentRuntime', 'execServerAgentRuntime'],+ });+ expect(cancelled).toContain(operationId!);+ });++ expect(result.current.operations[operationId!].status).toBe('cancelled');+ });});describe('cleanupCompletedOperations', () => {diff --git a/src/store/chat/slices/operation/actions.ts b/src/store/chat/slices/operation/actions.tsindex 17eae66c0eb..6e5c53d76bb 100644--- a/src/store/chat/slices/operation/actions.ts+++ b/src/store/chat/slices/operation/actions.ts@@ -22,6 +22,9 @@ import {const n = setNamespace('operation');const log = debug('lobe-store:operation');+const isSameNullableContextValue = (left?: string | null, right?: string | null): boolean =>+ (left ?? null) === (right ?? null);+/*** Operation Actions*/@@ -494,13 +497,13 @@ export class OperationActionsImpl {matches = matches && op.context.agentId === filter.agentId;}if (filter.topicId !== undefined) {- matches = matches && op.context.topicId === filter.topicId;+ matches = matches && isSameNullableContextValue(op.context.topicId, filter.topicId);}if (filter.messageId !== undefined) {matches = matches && op.context.messageId === filter.messageId;}if (filter.threadId !== undefined) {- matches = matches && op.context.threadId === filter.threadId;+ matches = matches && isSameNullableContextValue(op.context.threadId, filter.threadId);}if (filter.groupId !== undefined) {matches = matches && op.context.groupId === filter.groupId;