Skip to content

🀯 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.

πŸ› fix(task): hold follow-up/comment send while an inline image is still uploading (#22092)

Rdmclin2946829f68be8
diff --git a/src/features/AgentTasks/AgentTaskDetail/CommentCard.tsx b/src/features/AgentTasks/AgentTaskDetail/CommentCard.tsx
index 3d166819fee..64efa1fd0a5 100644
--- a/src/features/AgentTasks/AgentTaskDetail/CommentCard.tsx
+++ b/src/features/AgentTasks/AgentTaskDetail/CommentCard.tsx
@@ -26,6 +26,7 @@ import { EditorCanvas } from '@/features/EditorCanvas';
import { seedAttachments } from '@/features/EditorCanvas/attachmentRegistry';
import {
getAttachmentFileIdsFromEditor,
+ hasIncompleteEditorAttachments,
insertFilesIntoEditor,
} from '@/features/EditorCanvas/editorAttachments';
import { LinearFileCard } from '@/features/EditorCanvas/LinearFilePlugin';
@@ -61,6 +62,7 @@ const CommentCard = memo<CommentCardProps>(({ activity }) => {
const [isEditing, setIsEditing] = useState(false);
const [submitting, setSubmitting] = useState(false);
+ const [uploadingAttachments, setUploadingAttachments] = useState(false);
const editor = useEditor();
const mentionOption = useWorkspaceCommentMentionOption();
@@ -94,9 +96,14 @@ const CommentCard = memo<CommentCardProps>(({ activity }) => {
}, [activity.files]);
const handleEdit = useCallback(() => {
+ setUploadingAttachments(false);
setIsEditing(true);
}, []);
+ const handleEditorChange = useCallback(() => {
+ setUploadingAttachments(hasIncompleteEditorAttachments(editor));
+ }, [editor]);
+
const handleCancel = useCallback(() => {
setIsEditing(false);
}, []);
@@ -109,7 +116,8 @@ const CommentCard = memo<CommentCardProps>(({ activity }) => {
);
const handleSave = useCallback(async () => {
- if (!commentId || submitting) return;
+ // A still-uploading image would be saved as its local `blob:` preview URL.
+ if (!commentId || submitting || hasIncompleteEditorAttachments(editor)) return;
const next = String(editor?.getDocument?.('markdown') ?? '').trim();
const json = editor?.getDocument?.('json') as unknown;
const hasFiles = getAttachmentFileIdsFromEditor(editor).length > 0;
@@ -190,6 +198,7 @@ const CommentCard = memo<CommentCardProps>(({ activity }) => {
floatingToolbar={false}
mentionOption={mentionOption}
style={{ paddingBottom: 4 }}
+ onContentChange={handleEditorChange}
/>
</div>
<Flexbox horizontal align={'center'} gap={8} justify={'space-between'}>
@@ -198,7 +207,13 @@ const CommentCard = memo<CommentCardProps>(({ activity }) => {
<Button disabled={submitting} size={'small'} onClick={handleCancel}>
{t('taskDetail.comment.cancel')}
</Button>
- <Button loading={submitting} size={'small'} type={'primary'} onClick={handleSave}>
+ <Button
+ disabled={uploadingAttachments}
+ loading={submitting}
+ size={'small'}
+ type={'primary'}
+ onClick={handleSave}
+ >
{t('taskDetail.comment.save')}
</Button>
</Flexbox>
diff --git a/src/features/AgentTasks/AgentTaskDetail/CommentInput.tsx b/src/features/AgentTasks/AgentTaskDetail/CommentInput.tsx
index 797c24dcb85..e8aea8f5f20 100644
--- a/src/features/AgentTasks/AgentTaskDetail/CommentInput.tsx
+++ b/src/features/AgentTasks/AgentTaskDetail/CommentInput.tsx
@@ -9,6 +9,7 @@ import { mentionPlainClassName } from '@/features/ChatInput/InputEditor/mentionS
import { EditorCanvas } from '@/features/EditorCanvas';
import {
getAttachmentFileIdsFromEditor,
+ hasIncompleteEditorAttachments,
insertFilesIntoEditor,
} from '@/features/EditorCanvas/editorAttachments';
import { useWorkspaceCommentMentionOption } from '@/features/Portal/TopicComments/useWorkspaceCommentMentionOption';
@@ -26,12 +27,14 @@ const CommentInput = memo<{ taskId: string }>(({ taskId }) => {
const userAvatar = useUserAvatar();
const [hasContent, setHasContent] = useState(false);
const [hasAttachments, setHasAttachments] = useState(false);
+ const [uploadingAttachments, setUploadingAttachments] = useState(false);
const shouldSendOnEnter = useEnterToSend();
// Same member source as document / topic comments, so `@` produces the
// identical mention node the server resolves for notifications.
const mentionOption = useWorkspaceCommentMentionOption();
- const canSubmit = hasContent || hasAttachments;
+ // A still-uploading image would be sent as its local `blob:` preview URL.
+ const canSubmit = (hasContent || hasAttachments) && !uploadingAttachments;
const handleContentChange = useCallback(() => {
const lexicalEditor = editor?.getLexicalEditor?.();
@@ -41,6 +44,7 @@ const CommentInput = memo<{ taskId: string }>(({ taskId }) => {
setHasContent(text.length > 0);
});
setHasAttachments(getAttachmentFileIdsFromEditor(editor).length > 0);
+ setUploadingAttachments(hasIncompleteEditorAttachments(editor));
}, [editor]);
const handleAttach = useCallback(
@@ -54,8 +58,9 @@ const CommentInput = memo<{ taskId: string }>(({ taskId }) => {
(state: { hasAttachments: boolean; hasContent: boolean }) => {
setHasContent(state.hasContent);
setHasAttachments(state.hasAttachments);
+ setUploadingAttachments(hasIncompleteEditorAttachments(editor));
},
- [],
+ [editor],
);
const { submit: handleSubmit, submitting } = useCommentSubmit({
diff --git a/src/features/AgentTasks/AgentTaskDetail/TopicChatDrawer/FeedbackInput.test.tsx b/src/features/AgentTasks/AgentTaskDetail/TopicChatDrawer/FeedbackInput.test.tsx
new file mode 100644
index 00000000000..b686e419914
--- /dev/null
+++ b/src/features/AgentTasks/AgentTaskDetail/TopicChatDrawer/FeedbackInput.test.tsx
@@ -0,0 +1,152 @@
+/**
+ * @vitest-environment happy-dom
+ */
+import { act, fireEvent, render, screen } from '@testing-library/react';
+import type { ReactNode } from 'react';
+import { beforeEach, describe, expect, it, vi } from 'vitest';
+
+import FeedbackInput from './FeedbackInput';
+
+const UPLOADING_DOC = {
+ root: {
+ children: [
+ { src: 'blob:app://renderer/6c51', status: 'loading', type: 'block-image' },
+ { children: [{ text: 'why so slow', type: 'text' }], type: 'paragraph' },
+ ],
+ type: 'root',
+ },
+};
+
+const UPLOADED_DOC = {
+ root: {
+ children: [
+ { src: 'https://app.lobehub.com/f/file_1', status: 'uploaded', type: 'block-image' },
+ { children: [{ text: 'why so slow', type: 'text' }], type: 'paragraph' },
+ ],
+ type: 'root',
+ },
+};
+
+const mocks = vi.hoisted(() => ({
+ canvasProps: undefined as
+ | undefined
+ | {
+ onContentChange?: () => void;
+ onPressEnter?: (args: { event: { key: string } }) => boolean | void;
+ },
+ doc: { json: undefined as unknown, markdown: '' },
+ editor: undefined as any,
+ sendMessage: vi.fn(),
+}));
+
+mocks.editor = {
+ cleanDocument: vi.fn(),
+ getDocument: (format: string) => (format === 'markdown' ? mocks.doc.markdown : mocks.doc.json),
+ getLexicalEditor: () => ({
+ getEditorState: () => ({ read: (fn: () => void) => fn() }),
+ }),
+};
+
+vi.mock('lexical', async (importOriginal) => ({
+ ...(await importOriginal<object>()),
+ $getRoot: () => ({ getTextContent: () => mocks.doc.markdown }),
+}));
+
+vi.mock('@lobehub/editor/react', () => ({
+ ChatInput: ({ children, footer }: { children?: ReactNode; footer?: ReactNode }) => (
+ <div>
+ {children}
+ {footer}
+ </div>
+ ),
+ ChatInputActionBar: ({ right }: { right?: ReactNode }) => <div>{right}</div>,
+ SendButton: ({ disabled, onClick }: { disabled?: boolean; onClick?: () => void }) => (
+ <button data-testid="send" disabled={disabled} onClick={onClick} />
+ ),
+ useEditor: () => mocks.editor,
+}));
+
+vi.mock('@/features/EditorCanvas', () => ({
+ EditorCanvas: (props: typeof mocks.canvasProps) => {
+ mocks.canvasProps = props;
+ return null;
+ },
+}));
+
+vi.mock('@/features/AttachmentInput', () => ({ AttachmentUploadButton: () => null }));
+vi.mock('@/features/Conversation/ChatInput/OpStatusTray', () => ({ default: () => null }));
+vi.mock('@/features/Conversation/ChatInput/QueueTray', () => ({ default: () => null }));
+vi.mock('@/features/Conversation/ChatInput/useConversationChatInputUiState', () => ({
+ useConversationChatInputUiState: () => ({
+ placeholderVariant: 'default',
+ showSendWhileGenerating: false,
+ showStopButton: false,
+ }),
+}));
+vi.mock('@/features/Conversation/hooks/useConversationResourceAccess', () => ({
+ useConversationResourceAccess: () => ({ canUseResource: true }),
+}));
+vi.mock('@/features/Conversation/store', () => ({
+ useConversationStore: (selector: (s: unknown) => unknown) =>
+ selector({ sendMessage: mocks.sendMessage, stopGenerating: vi.fn() }),
+}));
+vi.mock('@/hooks/useEnterToSend', () => ({ useEnterToSend: () => () => true }));
+vi.mock('./useComposerFocus', () => ({
+ useComposerFocus: () => ({
+ collapse: vi.fn(),
+ expand: vi.fn(),
+ expanded: true,
+ handleEditorInit: vi.fn(),
+ reveal: vi.fn(),
+ }),
+}));
+vi.mock('./useFollowUpQueue', () => ({
+ useFollowUpQueue: () => ({ composerExpanded: true, hasQueuedMessages: false }),
+}));
+
+const setDoc = (json: unknown) => {
+ mocks.doc.json = json;
+ mocks.doc.markdown = 'why so slow';
+ act(() => mocks.canvasProps?.onContentChange?.());
+};
+
+describe('FeedbackInput', () => {
+ beforeEach(() => {
+ mocks.sendMessage.mockReset();
+ mocks.editor.cleanDocument.mockReset();
+ mocks.doc = { json: undefined, markdown: '' };
+ });
+
+ it('holds the follow-up while a pasted image is still uploading', async () => {
+ render(<FeedbackInput defaultExpanded />);
+ setDoc(UPLOADING_DOC);
+
+ expect(screen.getByTestId('send')).toHaveProperty('disabled', true);
+
+ await act(async () => {
+ mocks.canvasProps?.onPressEnter?.({ event: { key: 'Enter' } });
+ });
+
+ expect(mocks.sendMessage).not.toHaveBeenCalled();
+ expect(mocks.editor.cleanDocument).not.toHaveBeenCalled();
+ });
+
+ it('sends once the image has finished uploading', async () => {
+ render(<FeedbackInput defaultExpanded />);
+ setDoc(UPLOADING_DOC);
+ setDoc(UPLOADED_DOC);
+
+ const send = screen.getByTestId('send');
+ expect(send).toHaveProperty('disabled', false);
+
+ await act(async () => {
+ fireEvent.click(send);
+ });
+
+ expect(mocks.sendMessage).toHaveBeenCalledWith({
+ editorData: UPLOADED_DOC,
+ forceRuntime: 'gateway',
+ message: 'why so slow',
+ });
+ });
+});
diff --git a/src/features/AgentTasks/AgentTaskDetail/TopicChatDrawer/FeedbackInput.tsx b/src/features/AgentTasks/AgentTaskDetail/TopicChatDrawer/FeedbackInput.tsx
index 93d35059838..56b70c660ba 100644
--- a/src/features/AgentTasks/AgentTaskDetail/TopicChatDrawer/FeedbackInput.tsx
+++ b/src/features/AgentTasks/AgentTaskDetail/TopicChatDrawer/FeedbackInput.tsx
@@ -14,6 +14,7 @@ import { useConversationStore } from '@/features/Conversation/store';
import { EditorCanvas } from '@/features/EditorCanvas';
import {
getAttachmentFileIdsFromEditor,
+ hasIncompleteEditorAttachments,
insertFilesIntoEditor,
} from '@/features/EditorCanvas/editorAttachments';
import { useEnterToSend } from '@/hooks/useEnterToSend';
@@ -43,6 +44,7 @@ const FeedbackInput = memo<FeedbackInputProps>(
const [submitting, setSubmitting] = useState(false);
const [hasContent, setHasContent] = useState(false);
const [hasAttachments, setHasAttachments] = useState(false);
+ const [uploadingAttachments, setUploadingAttachments] = useState(false);
const { collapse, expand, expanded, handleEditorInit, reveal } = useComposerFocus({
defaultExpanded,
editor,
@@ -62,7 +64,10 @@ const FeedbackInput = memo<FeedbackInputProps>(
reveal,
});
- const canSubmit = hasContent || hasAttachments;
+ // Hold the send while an inline image is still uploading: its node still
+ // carries the local `blob:` preview URL, and sending would hand the agent
+ // a link it can never open.
+ const canSubmit = (hasContent || hasAttachments) && !uploadingAttachments;
// The run this drawer shows is a real op in its own conversation context, so
// the composer reads the same op-derived send area as the main composer:
// while the agent is working the control is Stop, and a typed follow-up
@@ -80,6 +85,7 @@ const FeedbackInput = memo<FeedbackInputProps>(
setHasContent(text.length > 0);
});
setHasAttachments(getAttachmentFileIdsFromEditor(editor).length > 0);
+ setUploadingAttachments(hasIncompleteEditorAttachments(editor));
}, [editor]);
const handleAttach = useCallback(
@@ -90,7 +96,7 @@ const FeedbackInput = memo<FeedbackInputProps>(
);
const handleSubmit = useCallback(async () => {
- if (submitting) return;
+ if (submitting || hasIncompleteEditorAttachments(editor)) return;
const editorData = editor?.getDocument?.('json') as Record<string, any> | undefined;
const markdown = String(editor?.getDocument?.('markdown') ?? '').trim();
const hasFiles = getAttachmentFileIdsFromEditor(editor).length > 0;
diff --git a/src/features/AgentTasks/AgentTaskDetail/useCommentSubmit.test.ts b/src/features/AgentTasks/AgentTaskDetail/useCommentSubmit.test.ts
index 0c291eb7731..d9121f4b50c 100644
--- a/src/features/AgentTasks/AgentTaskDetail/useCommentSubmit.test.ts
+++ b/src/features/AgentTasks/AgentTaskDetail/useCommentSubmit.test.ts
@@ -22,6 +22,8 @@ vi.mock('@/features/EditorCanvas/editorAttachments', () => ({
hasCompletedAttachments: false,
hasIncompleteAttachments: JSON.stringify(json ?? null).includes('"pending"'),
}),
+ hasIncompleteEditorAttachments: (editor: IEditor | undefined) =>
+ JSON.stringify(editor?.getDocument?.('json') ?? null).includes('"pending"'),
}));
const JSON_DOC = { root: { type: 'root' } };
@@ -172,6 +174,25 @@ describe('useCommentSubmit', () => {
expect(doc.json).toBe(uploading);
});
+ it('holds the send while an inline image is still uploading', async () => {
+ const { doc, editor } = createEditor();
+ doc.json = {
+ root: {
+ children: [{ src: 'blob:app://renderer/1', status: 'pending', type: 'block-image' }],
+ type: 'root',
+ },
+ };
+ const { hook } = setup(editor);
+
+ await act(async () => {
+ await hook.result.current.submit();
+ });
+
+ expect(mocks.addComment).not.toHaveBeenCalled();
+ expect(editor.cleanDocument).not.toHaveBeenCalled();
+ expect(doc.markdown).toBe('looks good');
+ });
+
it('does nothing without permission or content', async () => {
const { editor } = createEditor();
const denied = setup(editor, false).hook;
diff --git a/src/features/AgentTasks/AgentTaskDetail/useCommentSubmit.ts b/src/features/AgentTasks/AgentTaskDetail/useCommentSubmit.ts
index 80e02692a43..e67e838c9fa 100644
--- a/src/features/AgentTasks/AgentTaskDetail/useCommentSubmit.ts
+++ b/src/features/AgentTasks/AgentTaskDetail/useCommentSubmit.ts
@@ -4,6 +4,7 @@ import { useCallback, useRef, useState } from 'react';
import {
getAttachmentFileIdsFromEditor,
getEditorAttachmentStateFromJson,
+ hasIncompleteEditorAttachments,
} from '@/features/EditorCanvas/editorAttachments';
import { useTaskStore } from '@/store/task';
@@ -30,7 +31,8 @@ export const useCommentSubmit = ({
taskIdRef.current = taskId;
const submit = useCallback(async () => {
- if (!canComment || submittingRef.current) return;
+ // A still-uploading image would be sent as its local `blob:` preview URL.
+ if (!canComment || submittingRef.current || hasIncompleteEditorAttachments(editor)) return;
const json = editor?.getDocument?.('json') as unknown;
const markdown = String(editor?.getDocument?.('markdown') ?? '').trim();
const hasFiles = getAttachmentFileIdsFromEditor(editor).length > 0;
diff --git a/src/features/EditorCanvas/editorAttachments.test.ts b/src/features/EditorCanvas/editorAttachments.test.ts
index b83a6ba0f25..92e62c92d2e 100644
--- a/src/features/EditorCanvas/editorAttachments.test.ts
+++ b/src/features/EditorCanvas/editorAttachments.test.ts
@@ -5,6 +5,7 @@ import { getFileIdForUrl } from './attachmentRegistry';
import {
getEditorAttachmentStateFromJson,
getExistingEditorAttachment,
+ hasIncompleteEditorAttachments,
insertExistingAttachmentsIntoEditor,
preservePendingFileNodeSize,
} from './editorAttachments';
@@ -41,6 +42,33 @@ describe('getEditorAttachmentStateFromJson', () => {
});
});
+describe('hasIncompleteEditorAttachments', () => {
+ const editorWith = (children: unknown[]) =>
+ ({ getDocument: () => ({ root: { children, type: 'root' } }) }) as unknown as IEditor;
+
+ it('flags an image that is still uploading with its local blob URL', () => {
+ expect(
+ hasIncompleteEditorAttachments(
+ editorWith([
+ { src: 'blob:app://renderer/1', status: 'loading', type: 'block-image' },
+ { children: [{ text: 'why so slow', type: 'text' }], type: 'paragraph' },
+ ]),
+ ),
+ ).toBe(true);
+ });
+
+ it('lets text and uploaded images through', () => {
+ expect(
+ hasIncompleteEditorAttachments(
+ editorWith([
+ { src: 'https://app.lobehub.com/f/file_1', status: 'uploaded', type: 'block-image' },
+ ]),
+ ),
+ ).toBe(false);
+ expect(hasIncompleteEditorAttachments(undefined)).toBe(false);
+ });
+});
+
describe('insertExistingAttachmentsIntoEditor', () => {
it('marks library resources for reuse instead of uploading them again', () => {
const dispatchCommand = vi.fn();
diff --git a/src/features/EditorCanvas/editorAttachments.ts b/src/features/EditorCanvas/editorAttachments.ts
index d6162efdbef..5eabd2cd558 100644
--- a/src/features/EditorCanvas/editorAttachments.ts
+++ b/src/features/EditorCanvas/editorAttachments.ts
@@ -136,6 +136,14 @@ export const getAttachmentFileIdsFromEditor = (editor: IEditor | undefined): str
return getAttachmentFileIdsFromJson(editor.getDocument?.('json'));
};
+/**
+ * An image or file still uploading (or whose upload failed) serializes with
+ * its local `blob:` preview URL. Sending it would persist that URL, which no
+ * one else (the agent included) can open, so composers must hold the send.
+ */
+export const hasIncompleteEditorAttachments = (editor: IEditor | undefined): boolean =>
+ getEditorAttachmentStateFromJson(editor?.getDocument?.('json')).hasIncompleteAttachments;
+
/**
* Images β†’ `INSERT_IMAGE_COMMAND`; everything else β†’ `INSERT_FILE_COMMAND`.
*/