diff --git a/web/app/components/share/text-generation/index.tsx b/web/app/components/share/text-generation/index.tsx index 4eba536b743a98..e82b67d817c53a 100644 --- a/web/app/components/share/text-generation/index.tsx +++ b/web/app/components/share/text-generation/index.tsx @@ -104,13 +104,21 @@ const TextGeneration: FC = ({ const handleRetryAllFailedTask = () => { setControlRetry(Date.now()) } - const [allTaskList, setAllTaskList, getLatestTaskList] = useGetState([]) + const [allTaskList, doSetAllTaskList] = useState([]) + const allTaskListRef = useRef([]) + const getLatestTaskList = () => allTaskListRef.current + const setAllTaskList = (taskList: Task[]) => { + doSetAllTaskList(taskList) + allTaskListRef.current = taskList + } const pendingTaskList = allTaskList.filter(task => task.status === TaskStatus.pending) const noPendingTask = pendingTaskList.length === 0 const showTaskList = allTaskList.filter(task => task.status !== TaskStatus.pending) + const [currGroupNum, setCurrGroupNum, getCurrGroupNum] = useGetState(0) const allSuccessTaskList = allTaskList.filter(task => task.status === TaskStatus.completed) const allFailedTaskList = allTaskList.filter(task => task.status === TaskStatus.failed) const allTaskFinished = allTaskList.every(task => task.status === TaskStatus.completed) + const allTaskRuned = allTaskList.every(task => [TaskStatus.completed, TaskStatus.failed].includes(task.status)) const [batchCompletionRes, setBatchCompletionRes, getBatchCompletionRes] = useGetState>({}) const exportRes = allTaskList.map((task) => { const batchCompletionResLatest = getBatchCompletionRes() @@ -256,8 +264,15 @@ const TextGeneration: FC = ({ const allTasklistLatest = getLatestTaskList() const batchCompletionResLatest = getBatchCompletionRes() const pendingTaskList = allTasklistLatest.filter(task => task.status === TaskStatus.pending) - const hadRunedTaskNum = allTasklistLatest.filter(task => [TaskStatus.completed, TaskStatus.failed].includes(task.status)).length - const needToAddNextGroupTask = hadRunedTaskNum % GROUP_SIZE === 0 && pendingTaskList.length > 0 + const hadRunedTaskNum = 1 + allTasklistLatest.filter(task => [TaskStatus.completed, TaskStatus.failed].includes(task.status)).length + const needToAddNextGroupTask = (getCurrGroupNum() !== hadRunedTaskNum) && pendingTaskList.length > 0 && (hadRunedTaskNum % GROUP_SIZE === 0 || (allTasklistLatest.length - hadRunedTaskNum < GROUP_SIZE)) + // avoid add many task at the same time + if (needToAddNextGroupTask) + setCurrGroupNum(hadRunedTaskNum) + // console.group() + // console.log(`[#${taskId}]: ${isSuccess ? 'success' : 'fail'}.currGroupNum: ${getCurrGroupNum()}.hadRunedTaskNum: ${hadRunedTaskNum}, needToAddNextGroupTask: ${needToAddNextGroupTask}`) + // console.log([...allTasklistLatest.filter(task => [TaskStatus.completed, TaskStatus.failed].includes(task.status)).map(item => item.id), taskId].sort((a: any, b: any) => a - b).join(',')) + // console.groupEnd() const nextPendingTaskIds = needToAddNextGroupTask ? pendingTaskList.slice(0, GROUP_SIZE).map(item => item.id) : [] const newAllTaskList = allTasklistLatest.map((item) => { if (item.id === taskId) { @@ -488,6 +503,7 @@ const TextGeneration: FC = ({ diff --git a/web/app/components/share/text-generation/result/index.tsx b/web/app/components/share/text-generation/result/index.tsx index 4aba6a2574d982..73ec5c2c7468cd 100644 --- a/web/app/components/share/text-generation/result/index.tsx +++ b/web/app/components/share/text-generation/result/index.tsx @@ -128,6 +128,17 @@ const Result: FC = ({ onShowRes() setResponsingTrue() + const startTime = Date.now() + let isTimeout = false + const runId = setInterval(() => { + if (Date.now() - startTime > 1000 * 60) { // 1min timeout + clearInterval(runId) + setResponsingFalse() + onCompleted(getCompletionRes(), taskId, false) + isTimeout = true + console.log(`[#${taskId}]: timeout`) + } + }, 1000) sendCompletionMessage(data, { onData: (data: string, _isFirstMessage: boolean, { messageId }) => { tempMessageId = messageId @@ -135,13 +146,21 @@ const Result: FC = ({ setCompletionRes(res.join('')) }, onCompleted: () => { + if (isTimeout) + return + setResponsingFalse() setMessageId(tempMessageId) onCompleted(getCompletionRes(), taskId, true) + clearInterval(runId) }, onError() { + if (isTimeout) + return + setResponsingFalse() onCompleted(getCompletionRes(), taskId, false) + clearInterval(runId) }, }, isInstalledApp, installedAppInfo?.id) } diff --git a/web/app/components/share/text-generation/run-batch/index.tsx b/web/app/components/share/text-generation/run-batch/index.tsx index 78645bc87d10b9..849d02445c891d 100644 --- a/web/app/components/share/text-generation/run-batch/index.tsx +++ b/web/app/components/share/text-generation/run-batch/index.tsx @@ -5,18 +5,21 @@ import { PlayIcon, } from '@heroicons/react/24/solid' import { useTranslation } from 'react-i18next' +import cn from 'classnames' import CSVReader from './csv-reader' import CSVDownload from './csv-download' import Button from '@/app/components/base/button' - +import { Loading02 } from '@/app/components/base/icons/src/vender/line/general' export type IRunBatchProps = { vars: { name: string }[] onSend: (data: string[][]) => void + isAllFinished: boolean } const RunBatch: FC = ({ vars, onSend, + isAllFinished, }) => { const { t } = useTranslation() @@ -31,6 +34,7 @@ const RunBatch: FC = ({ const handleSend = () => { onSend(csvData) } + const Icon = isAllFinished ? PlayIcon : Loading02 return (
@@ -41,9 +45,9 @@ const RunBatch: FC = ({ type="primary" className='mt-4 !h-8 !pl-3 !pr-4' onClick={handleSend} - disabled={!isParsed} + disabled={!isParsed || !isAllFinished} > -