mirror of
https://github.com/chaitin/MonkeyCode.git
synced 2026-11-24 05:02:20 +08:00
修复超长文本转换状态竞态
This commit is contained in:
1 parent
b9a3c27ecf
commit
471fb6ffe6
2 files changed
+59
-24
No files matched your search
@@ -145,6 +145,7 @@ export const TaskChatInputBox = React.forwardRef<TaskChatInputBoxHandle, TaskCha
|
||||
const nextAttachmentFileIndexRef = useRef(1)
|
||||
const autoSendingQueuedInputRef = useRef(false)
|
||||
const mountedRef = useRef(true)
|
||||
const uploadedFilesRef = useRef(uploadedFiles)
|
||||
const selectedQuickInputRef = useRef<string | null>(null)
|
||||
const currentTaskIdRef = useRef(taskId)
|
||||
const isExecuting = (streamStatus === 'connected' || streamStatus === 'inited' || streamStatus === 'executing')
|
||||
@@ -201,6 +202,7 @@ export const TaskChatInputBox = React.forwardRef<TaskChatInputBoxHandle, TaskCha
|
||||
}, [writeQuickInputs])
|
||||
|
||||
React.useEffect(() => {
|
||||
mountedRef.current = true
|
||||
return () => {
|
||||
mountedRef.current = false
|
||||
}
|
||||
@@ -231,6 +233,7 @@ export const TaskChatInputBox = React.forwardRef<TaskChatInputBoxHandle, TaskCha
|
||||
const lastSubmittedInput = lastSubmittedInputRef.current
|
||||
if (lastSubmittedInput) {
|
||||
setContent(lastSubmittedInput.content)
|
||||
uploadedFilesRef.current = lastSubmittedInput.uploadedFiles
|
||||
setUploadedFiles(lastSubmittedInput.uploadedFiles)
|
||||
setPreviewFile(null)
|
||||
nextAttachmentFileIndexRef.current = lastSubmittedInput.nextAttachmentFileIndex
|
||||
@@ -286,6 +289,7 @@ export const TaskChatInputBox = React.forwardRef<TaskChatInputBoxHandle, TaskCha
|
||||
removeTaskInputDraft(taskId)
|
||||
selectedQuickInputRef.current = null
|
||||
setContent('')
|
||||
uploadedFilesRef.current = []
|
||||
setUploadedFiles([])
|
||||
setPreviewFile(null)
|
||||
setWhiteboardFileIndex(1)
|
||||
@@ -411,6 +415,7 @@ export const TaskChatInputBox = React.forwardRef<TaskChatInputBoxHandle, TaskCha
|
||||
}
|
||||
|
||||
setContent(inputToSend.content)
|
||||
uploadedFilesRef.current = inputToSend.uploadedFiles
|
||||
setUploadedFiles(inputToSend.uploadedFiles)
|
||||
setPreviewFile(null)
|
||||
nextAttachmentFileIndexRef.current = inputToSend.nextAttachmentFileIndex
|
||||
@@ -473,11 +478,6 @@ export const TaskChatInputBox = React.forwardRef<TaskChatInputBoxHandle, TaskCha
|
||||
}
|
||||
}
|
||||
|
||||
const handleTextRecognized = (text: string) => {
|
||||
selectedQuickInputRef.current = null
|
||||
setContent(text)
|
||||
}
|
||||
|
||||
const handleContentChange = (nextContent: string) => {
|
||||
if (
|
||||
selectedQuickInputRef.current
|
||||
@@ -496,6 +496,11 @@ export const TaskChatInputBox = React.forwardRef<TaskChatInputBoxHandle, TaskCha
|
||||
setContent(nextContent)
|
||||
}
|
||||
|
||||
const handleTextRecognized = (text: string) => {
|
||||
selectedQuickInputRef.current = null
|
||||
handleContentChange(text)
|
||||
}
|
||||
|
||||
const applyQuickInput = (text: string) => {
|
||||
const normalizedText = normalizeQuickInputText(text)
|
||||
if (!normalizedText) {
|
||||
@@ -503,7 +508,7 @@ export const TaskChatInputBox = React.forwardRef<TaskChatInputBoxHandle, TaskCha
|
||||
}
|
||||
|
||||
selectedQuickInputRef.current = normalizedText
|
||||
setContent(normalizedText)
|
||||
handleContentChange(normalizedText)
|
||||
requestAnimationFrame(() => {
|
||||
textareaRef.current?.focus()
|
||||
const cursorPosition = normalizedText.length
|
||||
@@ -615,18 +620,20 @@ export const TaskChatInputBox = React.forwardRef<TaskChatInputBoxHandle, TaskCha
|
||||
}
|
||||
|
||||
const handleUploaded = (file: TaskUploadedFile) => {
|
||||
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<TaskChatInputBoxHandle, TaskCha
|
||||
const handleConfirmLongContentConversion = async () => {
|
||||
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<TaskChatInputBoxHandle, TaskCha
|
||||
return
|
||||
}
|
||||
|
||||
handleUploaded(uploadedFile)
|
||||
if (!handleUploaded(uploadedFile)) {
|
||||
toast.error(t("taskDetail.chat.toast.maxFiles", { count: MAX_UPLOADED_FILES }))
|
||||
setLongContentDraft(null)
|
||||
setLongContentPromptSuppressed(true)
|
||||
return
|
||||
}
|
||||
|
||||
removeTaskInputDraft(taskId)
|
||||
setContent('')
|
||||
setLongContentDraft(null)
|
||||
@@ -708,8 +728,9 @@ export const TaskChatInputBox = React.forwardRef<TaskChatInputBoxHandle, TaskCha
|
||||
}, [canUseIdleControls, contentLength, isComposing, longContentPromptPending, openLongContentDialog])
|
||||
|
||||
const handleWhiteboardUploaded = (file: TaskUploadedFile) => {
|
||||
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<TaskChatInputBoxHandle, TaskCha
|
||||
const start = textarea.selectionStart
|
||||
const end = textarea.selectionEnd
|
||||
const nextContent = `${content.slice(0, start)}\n${content.slice(end)}`
|
||||
setContent(nextContent)
|
||||
handleContentChange(nextContent)
|
||||
requestAnimationFrame(() => {
|
||||
textarea.selectionStart = start + 1
|
||||
textarea.selectionEnd = start + 1
|
||||
@@ -1090,7 +1111,7 @@ export const TaskChatInputBox = React.forwardRef<TaskChatInputBoxHandle, TaskCha
|
||||
</DropdownMenuItem>
|
||||
<DropdownMenuSeparator />
|
||||
{commandItems.map((command: AvailableCommand, index: number) => (
|
||||
<DropdownMenuItem key={index} className="flex flex-col items-start gap-1 whitespace-normal" onClick={() => setContent(`/${command.name}`)}>
|
||||
<DropdownMenuItem key={index} className="flex flex-col items-start gap-1 whitespace-normal" onClick={() => handleContentChange(`/${command.name}`)}>
|
||||
<div className="flex min-w-0 flex-row flex-wrap items-center gap-2">
|
||||
<IconCommand />
|
||||
<div className="font-bold text-xs">/{command.name}</div>
|
||||
@@ -1145,12 +1166,14 @@ export const TaskChatInputBox = React.forwardRef<TaskChatInputBoxHandle, TaskCha
|
||||
file={uploadedFile}
|
||||
disabled={!!queuedInput || inputLocked}
|
||||
onPreview={() => 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)
|
||||
}}
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
|
||||
@@ -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, /<TaskLongContentDialog/)
|
||||
|
||||
Reference in new issue
Block a user