Commit a6b599f6 authored by Joel's avatar Joel

fix: multi run quene bug

parent 96c6e7bf
......@@ -61,7 +61,9 @@ const TextGeneration: FC<IMainProps> = ({
const isMobile = media === MediaType.mobile
const [currTab, setCurrTab] = useState<string>('batch')
const [isBatch, setIsBatch] = useState(false)
// Notice this situation isCallBatchAPI but not in batch tab
const [isCallBatchAPI, setIsCallBatchAPI] = useState(false)
const isInBatchTab = currTab === 'batch'
const [inputs, setInputs] = useState<Record<string, any>>({})
const [query, setQuery] = useState('') // run once query content
const [appId, setAppId] = useState<string>('')
......@@ -92,25 +94,31 @@ const TextGeneration: FC<IMainProps> = ({
// send message task
const [controlSend, setControlSend] = useState(0)
const handleSend = () => {
setIsBatch(false)
setIsCallBatchAPI(false)
setControlSend(Date.now())
}
const [allTaskList, setAllTaskList] = useState<Task[]>([])
// const pendingTaskList = allTaskList.filter(task => task.status === TaskStatus.pending)
const runningTaskList = allTaskList.filter(task => task.status === TaskStatus.running)
const completedTaskList = allTaskList.filter(task => task.status === TaskStatus.completed)
const showTaskList = [...runningTaskList, ...completedTaskList]
const pendingTaskList = allTaskList.filter(task => task.status === TaskStatus.pending)
const noPendingTask = pendingTaskList.length === 0
const showTaskList = allTaskList.filter(task => task.status !== TaskStatus.pending)
// console.log(showTaskList.map(item => ({ id: item.id, status: item.status })))
const allTaskFinished = allTaskList.every(task => task.status === TaskStatus.completed)
const handleRunBatch = (data: string[][]) => {
setIsBatch(true)
if (!allTaskFinished) {
notify({ type: 'info', message: t('appDebug.errorMessage.waitForBatchResponse') })
return
}
setIsCallBatchAPI(true)
const allTaskList: Task[] = data.map((item, i) => {
const inputs: Record<string, string> = {}
item.slice(0, -1).forEach((input, index) => {
inputs[promptConfig?.prompt_variables[index].key as string] = input
})
return {
id: 1,
status: i + 1 < PARALLEL_LIMIT ? TaskStatus.running : TaskStatus.pending,
id: i + 1,
status: i < PARALLEL_LIMIT ? TaskStatus.running : TaskStatus.pending,
params: {
inputs,
query: item[item.length - 1],
......@@ -118,9 +126,29 @@ const TextGeneration: FC<IMainProps> = ({
}
})
setAllTaskList(allTaskList)
setControlSend(Date.now())
}
// TODO: finished add pending tasks.
const handleCompleted = (taskId?: number, isSuccess?: boolean) => {
console.log(taskId, isSuccess)
const nextPendingTaskId = pendingTaskList[0]?.id
const newAllTaskList = allTaskList.map((item) => {
if (item.id === taskId) {
return {
...item,
status: TaskStatus.completed,
}
}
if (item.id === nextPendingTaskId) {
return {
...item,
status: TaskStatus.running,
}
}
return item
})
setAllTaskList(newAllTaskList)
}
const fetchInitData = () => {
return Promise.all([isInstalledApp
......@@ -168,18 +196,20 @@ const TextGeneration: FC<IMainProps> = ({
const renderRes = (task?: Task) => (<Res
key={task?.id}
isBatch={isBatch}
isCallBatchAPI={isCallBatchAPI}
isPC={isPC}
isMobile={isMobile}
isInstalledApp={!!isInstalledApp}
installedAppInfo={installedAppInfo}
promptConfig={promptConfig}
moreLikeThisEnabled={!!moreLikeThisConfig?.enabled}
inputs={isBatch ? (task as Task).params.inputs : inputs}
query={isBatch ? (task as Task).params.query : query}
controlSend={isBatch ? 0 : controlSend}
inputs={isCallBatchAPI ? (task as Task).params.inputs : inputs}
query={isCallBatchAPI ? (task as Task).params.query : query}
controlSend={controlSend}
onShowRes={showResSidebar}
handleSaveMessage={handleSaveMessage}
taskId={task?.id}
onCompleted={handleCompleted}
/>)
const renderBatchRes = () => {
......@@ -213,7 +243,12 @@ const TextGeneration: FC<IMainProps> = ({
</div>
<div className='grow overflow-y-auto'>
{!isBatch ? renderRes() : renderBatchRes()}
{!isCallBatchAPI ? renderRes() : renderBatchRes()}
{!noPendingTask && (
<div className='mt-4'>
<Loading type='area' />
</div>
)}
</div>
</>
</div>
......@@ -278,7 +313,7 @@ const TextGeneration: FC<IMainProps> = ({
onChange={setCurrTab}
/>
<div className='grow h-20 overflow-y-auto'>
{currTab === 'create' && (
<div className={cn(currTab === 'create' ? 'block' : 'hidden')}>
<ConfigScence
siteInfo={siteInfo}
inputs={inputs}
......@@ -288,13 +323,13 @@ const TextGeneration: FC<IMainProps> = ({
onQueryChange={setQuery}
onSend={handleSend}
/>
)}
{currTab === 'batch' && (
</div>
<div className={cn(isInBatchTab ? 'block' : 'hidden')}>
<RunBatch
vars={promptConfig.prompt_variables}
onSend={handleRunBatch}
/>
)}
</div>
{currTab === 'saved' && (
<SavedItems
......
......@@ -12,9 +12,8 @@ import type { Feedbacktype } from '@/app/components/app/chat'
import Loading from '@/app/components/base/loading'
import type { PromptConfig } from '@/models/debug'
import type { InstalledApp } from '@/models/explore'
export type IResultProps = {
isBatch: boolean
isCallBatchAPI: boolean
isPC: boolean
isMobile: boolean
isInstalledApp: boolean
......@@ -26,10 +25,12 @@ export type IResultProps = {
controlSend?: number
onShowRes: () => void
handleSaveMessage: (messageId: string) => void
taskId?: number
onCompleted: (taskId?: number, success?: boolean) => void
}
const Result: FC<IResultProps> = ({
isBatch,
isCallBatchAPI,
isPC,
isMobile,
isInstalledApp,
......@@ -41,6 +42,8 @@ const Result: FC<IResultProps> = ({
controlSend,
onShowRes,
handleSaveMessage,
taskId,
onCompleted,
}) => {
const [isResponsing, { setTrue: setResponsingTrue, setFalse: setResponsingFalse }] = useBoolean(false)
const [completionRes, setCompletionRes] = useState('')
......@@ -63,7 +66,7 @@ const Result: FC<IResultProps> = ({
const checkCanSend = () => {
// batch will check outer
if (isBatch)
if (isCallBatchAPI)
return true
const prompt_variables = promptConfig?.prompt_variables
......@@ -131,24 +134,19 @@ const Result: FC<IResultProps> = ({
onCompleted: () => {
setResponsingFalse()
setMessageId(tempMessageId)
onCompleted(taskId, true)
},
onError() {
setResponsingFalse()
onCompleted(taskId, false)
},
}, isInstalledApp, installedAppInfo?.id)
}
// run once
useEffect(() => {
if (controlSend)
handleSend()
}, [controlSend])
// run batch
useEffect(() => {
if (isBatch)
handleSend()
}, [isBatch])
const renderTextGenerationRes = () => (
<TextGenerationRes
className='mt-3'
......@@ -162,13 +160,13 @@ const Result: FC<IResultProps> = ({
isMobile={isMobile}
isInstalledApp={isInstalledApp}
installedAppId={installedAppInfo?.id}
isLoading={isBatch ? (!completionRes && isResponsing) : false}
isLoading={isCallBatchAPI ? (!completionRes && isResponsing) : false}
/>
)
return (
<div className={cn((isBatch && !isNoData) ? '' : 'h-full')}>
{!isBatch && (
<div className={cn(isNoData && !isCallBatchAPI && 'h-full')}>
{!isCallBatchAPI && (
(isResponsing && !completionRes)
? (
<div className='flex h-full w-full justify-center items-center'>
......@@ -183,7 +181,7 @@ const Result: FC<IResultProps> = ({
</>
)
)}
{isBatch && (
{isCallBatchAPI && (
<div className='mt-2'>
{renderTextGenerationRes()}
</div>
......
......@@ -29,7 +29,7 @@ const CSVDownload: FC<ICSVDownloadProps> = ({
<div className='mt-6'>
<div className='text-sm text-gray-900 font-medium'>{t('share.generation.csvStructureTitle')}</div>
<div className='mt-2 max-h-[500px] overflow-auto'>
<table className='w-full border-separate border-spacing-0 border border-gray-200 rounded-lg'>
<table className='w-full border-separate border-spacing-0 border border-gray-200 rounded-lg text-xs'>
<thead className='text-gray-500'>
<tr>
{addQueryContentVars.map((item, i) => (
......
......@@ -81,6 +81,8 @@ const translation = {
queryRequired: 'Request text is required.',
waitForResponse:
'Please wait for the response to the previous message to complete.',
waitForBatchResponse:
'Please wait for the response to the batch task to complete.',
},
chatSubTitle: 'Pre Prompt',
completionSubTitle: 'Prefix Prompt',
......
......@@ -79,6 +79,7 @@ const translation = {
valueOfVarRequired: '变量值必填',
queryRequired: '主要文本必填',
waitForResponse: '请等待上条信息响应完成',
waitForBatchResponse: '请等待批量任务完成',
},
chatSubTitle: '对话前提示词',
completionSubTitle: '前缀提示词',
......
......@@ -308,13 +308,13 @@ export const ssePost = (url: string, fetchOptions: any, { isPublicAPI = false, o
}
return handleStream(res, (str: string, isFirstMessage: boolean, moreInfo: IOnDataMoreInfo) => {
if (moreInfo.errorMessage) {
onError?.(moreInfo.errorMessage)
Toast.notify({ type: 'error', message: moreInfo.errorMessage })
return
}
onData?.(str, isFirstMessage, moreInfo)
}, onCompleted)
}).catch((e) => {
// debugger
Toast.notify({ type: 'error', message: e })
onError?.(e)
})
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment