diff --git a/frontend/src/components/console/task/chat-inputbox.tsx b/frontend/src/components/console/task/chat-inputbox.tsx index 97ab6ad1..2ff865cf 100644 --- a/frontend/src/components/console/task/chat-inputbox.tsx +++ b/frontend/src/components/console/task/chat-inputbox.tsx @@ -145,6 +145,7 @@ export const TaskChatInputBox = React.forwardRef(null) const currentTaskIdRef = useRef(taskId) const isExecuting = (streamStatus === 'connected' || streamStatus === 'inited' || streamStatus === 'executing') @@ -201,6 +202,7 @@ export const TaskChatInputBox = React.forwardRef { + mountedRef.current = true return () => { mountedRef.current = false } @@ -231,6 +233,7 @@ export const TaskChatInputBox = React.forwardRef { - selectedQuickInputRef.current = null - setContent(text) - } - const handleContentChange = (nextContent: string) => { if ( selectedQuickInputRef.current @@ -496,6 +496,11 @@ export const TaskChatInputBox = React.forwardRef { + selectedQuickInputRef.current = null + handleContentChange(text) + } + const applyQuickInput = (text: string) => { const normalizedText = normalizeQuickInputText(text) if (!normalizedText) { @@ -503,7 +508,7 @@ export const TaskChatInputBox = React.forwardRef { textareaRef.current?.focus() const cursorPosition = normalizedText.length @@ -615,18 +620,20 @@ export const TaskChatInputBox = React.forwardRef { - setUploadedFiles((prev) => { - if (prev.length >= MAX_UPLOADED_FILES) { - return prev - } - return [...prev, file] - }) + if (uploadedFilesRef.current.length >= MAX_UPLOADED_FILES) { + return false + } + + const nextUploadedFiles = [...uploadedFilesRef.current, file] + uploadedFilesRef.current = nextUploadedFiles + setUploadedFiles(nextUploadedFiles) nextAttachmentFileIndexRef.current += 1 setSelectedUploadFile(null) setShouldAutoUpload(false) requestAnimationFrame(() => { textareaRef.current?.focus() }) + return true } const openLongContentDialog = React.useCallback(() => { @@ -658,6 +665,13 @@ export const TaskChatInputBox = React.forwardRef { if (!longContentDraft || longContentConverting) return + if (uploadedFilesRef.current.length >= MAX_UPLOADED_FILES) { + toast.error(t("taskDetail.chat.toast.maxFiles", { count: MAX_UPLOADED_FILES })) + setLongContentDraft(null) + setLongContentPromptSuppressed(true) + return + } + const conversionTaskId = taskId setLongContentConverting(true) try { @@ -668,7 +682,13 @@ export const TaskChatInputBox = React.forwardRef { - handleUploaded(file) - setWhiteboardFileIndex((prev) => prev + 1) + if (handleUploaded(file)) { + setWhiteboardFileIndex((prev) => prev + 1) + } } const hasTransferFile = (dataTransfer: DataTransfer) => { @@ -832,7 +853,7 @@ export const TaskChatInputBox = React.forwardRef { textarea.selectionStart = start + 1 textarea.selectionEnd = start + 1 @@ -1090,7 +1111,7 @@ export const TaskChatInputBox = React.forwardRef {commandItems.map((command: AvailableCommand, index: number) => ( - setContent(`/${command.name}`)}> + handleContentChange(`/${command.name}`)}>
/{command.name}
@@ -1145,12 +1166,14 @@ export const TaskChatInputBox = React.forwardRef setPreviewFile(uploadedFile)} - onRemove={() => { - if (previewFile?.accessUrl === uploadedFile.accessUrl) { - setPreviewFile(null) - } - setUploadedFiles((prev) => prev.filter((file) => file.accessUrl !== uploadedFile.accessUrl)) - }} + onRemove={() => { + if (previewFile?.accessUrl === uploadedFile.accessUrl) { + setPreviewFile(null) + } + const nextUploadedFiles = uploadedFilesRef.current.filter((file) => file.accessUrl !== uploadedFile.accessUrl) + uploadedFilesRef.current = nextUploadedFiles + setUploadedFiles(nextUploadedFiles) + }} /> ))}
diff --git a/frontend/test/task-long-content-integration.test.ts b/frontend/test/task-long-content-integration.test.ts index 13eb825c..81598849 100644 --- a/frontend/test/task-long-content-integration.test.ts +++ b/frontend/test/task-long-content-integration.test.ts @@ -19,13 +19,25 @@ test("输入框编排超长文本转换状态", () => { test("确认转换复用上传链路并保护失败原文", () => { assert.match(source, /createLongContentTextFile\(longContentDraft\.content, longContentDraft\.filename\)/) assert.match(source, /await uploadTaskFile\(file\)/) - assert.match(source, /handleUploaded\(uploadedFile\)/) + assert.match(source, /if \(!handleUploaded\(uploadedFile\)\)/) assert.match(source, /removeTaskInputDraft\(taskId\)/) assert.match(source, /setContent\(''\)/) assert.match(source, /error instanceof TaskUploadFileTooLargeError/) assert.match(source, /taskDetail\.chat\.longContent\.convertFailed/) }) +test("StrictMode 生命周期与附件竞争保持转换状态安全", () => { + assert.match(source, /React\.useEffect\(\(\) => \{\s+mountedRef\.current = true\s+return \(\) => \{\s+mountedRef\.current = false/) + assert.match(source, /const uploadedFilesRef = useRef\(uploadedFiles\)/) + assert.match(source, /if \(uploadedFilesRef\.current\.length >= MAX_UPLOADED_FILES\)/) +}) + +test("所有用户正文更新入口统一执行越界检测", () => { + assert.match(source, /const handleTextRecognized = \(text: string\) => \{\s+selectedQuickInputRef\.current = null\s+handleContentChange\(text\)/) + assert.match(source, /selectedQuickInputRef\.current = normalizedText\s+handleContentChange\(normalizedText\)/) + assert.match(source, /const nextContent = `[^`]+`\s+handleContentChange\(nextContent\)/) +}) + test("超限提示提供手动转换入口并渲染确认弹窗", () => { assert.match(source, /taskDetail\.chat\.longContent\.manualConvert/) assert.match(source, /