mirror of
http://112.124.100.131/huang.ze/ebiz-dify-ai.git
synced 2025-12-10 03:16:51 +08:00
feat: support optional query content (#1097)
Co-authored-by: Garfield Dai <dai.hai@foxmail.com>
This commit is contained in:
@@ -23,8 +23,9 @@ import { userInputsFormToPromptVariables } from '@/utils/model-config'
|
||||
import Res from '@/app/components/share/text-generation/result'
|
||||
import SavedItems from '@/app/components/app/text-generate/saved-items'
|
||||
import type { InstalledApp } from '@/models/explore'
|
||||
import { appDefaultIconBackground } from '@/config'
|
||||
import { DEFAULT_VALUE_MAX_LEN, appDefaultIconBackground } from '@/config'
|
||||
import Toast from '@/app/components/base/toast'
|
||||
|
||||
const PARALLEL_LIMIT = 5
|
||||
enum TaskStatus {
|
||||
pending = 'pending',
|
||||
@@ -34,7 +35,6 @@ enum TaskStatus {
|
||||
|
||||
type TaskParam = {
|
||||
inputs: Record<string, any>
|
||||
query: string
|
||||
}
|
||||
|
||||
type Task = {
|
||||
@@ -65,7 +65,6 @@ const TextGeneration: FC<IMainProps> = ({
|
||||
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>('')
|
||||
const [siteInfo, setSiteInfo] = useState<SiteInfo | null>(null)
|
||||
const [promptConfig, setPromptConfig] = useState<PromptConfig | null>(null)
|
||||
@@ -111,11 +110,10 @@ const TextGeneration: FC<IMainProps> = ({
|
||||
return {}
|
||||
const batchCompletionResLatest = getBatchCompletionRes()
|
||||
const res: Record<string, string> = {}
|
||||
const { inputs, query } = task.params
|
||||
const { inputs } = task.params
|
||||
promptConfig?.prompt_variables.forEach((v) => {
|
||||
res[v.name] = inputs[v.key]
|
||||
})
|
||||
res[t('share.generation.queryTitle')] = query
|
||||
res[t('share.generation.completionResult')] = batchCompletionResLatest[task.id]
|
||||
return res
|
||||
})
|
||||
@@ -135,9 +133,6 @@ const TextGeneration: FC<IMainProps> = ({
|
||||
isMapVarName = false
|
||||
})
|
||||
|
||||
if (headerData[varLen] !== t('share.generation.queryTitle'))
|
||||
isMapVarName = false
|
||||
|
||||
if (!isMapVarName) {
|
||||
notify({ type: 'error', message: t('share.generation.errorMsg.fileStructNotMatch') })
|
||||
return false
|
||||
@@ -180,6 +175,8 @@ const TextGeneration: FC<IMainProps> = ({
|
||||
}
|
||||
let errorRowIndex = 0
|
||||
let requiredVarName = ''
|
||||
let moreThanMaxLengthVarName = ''
|
||||
let maxLength = 0
|
||||
payloadData.forEach((item, index) => {
|
||||
if (errorRowIndex !== 0)
|
||||
return
|
||||
@@ -187,6 +184,15 @@ const TextGeneration: FC<IMainProps> = ({
|
||||
promptConfig?.prompt_variables.forEach((varItem, varIndex) => {
|
||||
if (errorRowIndex !== 0)
|
||||
return
|
||||
if (varItem.type === 'string') {
|
||||
const maxLen = varItem.max_length || DEFAULT_VALUE_MAX_LEN
|
||||
if (item[varIndex].length > maxLen) {
|
||||
moreThanMaxLengthVarName = varItem.name
|
||||
maxLength = maxLen
|
||||
errorRowIndex = index + 1
|
||||
return
|
||||
}
|
||||
}
|
||||
if (varItem.required === false)
|
||||
return
|
||||
|
||||
@@ -195,18 +201,15 @@ const TextGeneration: FC<IMainProps> = ({
|
||||
errorRowIndex = index + 1
|
||||
}
|
||||
})
|
||||
|
||||
if (errorRowIndex !== 0)
|
||||
return
|
||||
|
||||
if (item[varLen] === '') {
|
||||
requiredVarName = t('share.generation.queryTitle')
|
||||
errorRowIndex = index + 1
|
||||
}
|
||||
})
|
||||
|
||||
if (errorRowIndex !== 0) {
|
||||
notify({ type: 'error', message: t('share.generation.errorMsg.invalidLine', { rowIndex: errorRowIndex + 1, varName: requiredVarName }) })
|
||||
if (requiredVarName)
|
||||
notify({ type: 'error', message: t('share.generation.errorMsg.invalidLine', { rowIndex: errorRowIndex + 1, varName: requiredVarName }) })
|
||||
|
||||
if (moreThanMaxLengthVarName)
|
||||
notify({ type: 'error', message: t('share.generation.errorMsg.moreThanMaxLengthLine', { rowIndex: errorRowIndex + 1, varName: moreThanMaxLengthVarName, maxLength }) })
|
||||
|
||||
return false
|
||||
}
|
||||
return true
|
||||
@@ -234,7 +237,6 @@ const TextGeneration: FC<IMainProps> = ({
|
||||
status: i < PARALLEL_LIMIT ? TaskStatus.running : TaskStatus.pending,
|
||||
params: {
|
||||
inputs,
|
||||
query: item[varLen],
|
||||
},
|
||||
}
|
||||
})
|
||||
@@ -334,7 +336,6 @@ const TextGeneration: FC<IMainProps> = ({
|
||||
promptConfig={promptConfig}
|
||||
moreLikeThisEnabled={!!moreLikeThisConfig?.enabled}
|
||||
inputs={isCallBatchAPI ? (task as Task).params.inputs : inputs}
|
||||
query={isCallBatchAPI ? (task as Task).params.query : query}
|
||||
controlSend={controlSend}
|
||||
controlStopResponding={controlStopResponding}
|
||||
onShowRes={showResSidebar}
|
||||
@@ -379,7 +380,6 @@ const TextGeneration: FC<IMainProps> = ({
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
</div>
|
||||
|
||||
<div className='grow overflow-y-auto'>
|
||||
@@ -459,8 +459,6 @@ const TextGeneration: FC<IMainProps> = ({
|
||||
inputs={inputs}
|
||||
onInputsChange={setInputs}
|
||||
promptConfig={promptConfig}
|
||||
query={query}
|
||||
onQueryChange={setQuery}
|
||||
onSend={handleSend}
|
||||
/>
|
||||
</div>
|
||||
|
||||
@@ -21,7 +21,6 @@ export type IResultProps = {
|
||||
promptConfig: PromptConfig | null
|
||||
moreLikeThisEnabled: boolean
|
||||
inputs: Record<string, any>
|
||||
query: string
|
||||
controlSend?: number
|
||||
controlStopResponding?: number
|
||||
onShowRes: () => void
|
||||
@@ -39,7 +38,6 @@ const Result: FC<IResultProps> = ({
|
||||
promptConfig,
|
||||
moreLikeThisEnabled,
|
||||
inputs,
|
||||
query,
|
||||
controlSend,
|
||||
controlStopResponding,
|
||||
onShowRes,
|
||||
@@ -109,14 +107,8 @@ const Result: FC<IResultProps> = ({
|
||||
if (!checkCanSend())
|
||||
return
|
||||
|
||||
if (!query) {
|
||||
logError(t('appDebug.errorMessage.queryRequired'))
|
||||
return false
|
||||
}
|
||||
|
||||
const data = {
|
||||
inputs,
|
||||
query,
|
||||
}
|
||||
|
||||
setMessageId(null)
|
||||
|
||||
@@ -16,7 +16,7 @@ const CSVDownload: FC<ICSVDownloadProps> = ({
|
||||
}) => {
|
||||
const { t } = useTranslation()
|
||||
const { CSVDownloader, Type } = useCSVDownloader()
|
||||
const addQueryContentVars = [...vars, { name: t('share.generation.queryTitle') }]
|
||||
const addQueryContentVars = [...vars]
|
||||
const template = (() => {
|
||||
const res: Record<string, string> = {}
|
||||
addQueryContentVars.forEach((item) => {
|
||||
|
||||
@@ -15,20 +15,24 @@ export type IRunOnceProps = {
|
||||
promptConfig: PromptConfig
|
||||
inputs: Record<string, any>
|
||||
onInputsChange: (inputs: Record<string, any>) => void
|
||||
query: string
|
||||
onQueryChange: (query: string) => void
|
||||
onSend: () => void
|
||||
}
|
||||
const RunOnce: FC<IRunOnceProps> = ({
|
||||
promptConfig,
|
||||
inputs,
|
||||
onInputsChange,
|
||||
query,
|
||||
onQueryChange,
|
||||
onSend,
|
||||
}) => {
|
||||
const { t } = useTranslation()
|
||||
|
||||
const onClear = () => {
|
||||
const newInputs: Record<string, any> = {}
|
||||
promptConfig.prompt_variables.forEach((item) => {
|
||||
newInputs[item.key] = ''
|
||||
})
|
||||
onInputsChange(newInputs)
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="">
|
||||
<section>
|
||||
@@ -38,61 +42,58 @@ const RunOnce: FC<IRunOnceProps> = ({
|
||||
<div className='w-full mt-4' key={item.key}>
|
||||
<label className='text-gray-900 text-sm font-medium'>{item.name}</label>
|
||||
<div className='mt-2'>
|
||||
{item.type === 'select'
|
||||
? (
|
||||
<Select
|
||||
className='w-full'
|
||||
defaultValue={inputs[item.key]}
|
||||
onSelect={(i) => { onInputsChange({ ...inputs, [item.key]: i.value }) }}
|
||||
items={(item.options || []).map(i => ({ name: i, value: i }))}
|
||||
allowSearch={false}
|
||||
bgClassName='bg-gray-50'
|
||||
/>
|
||||
)
|
||||
: (
|
||||
<input
|
||||
type="text"
|
||||
className="block w-full p-2 text-gray-900 border border-gray-300 rounded-lg bg-gray-50 sm:text-xs focus:ring-blue-500 focus:border-blue-500 "
|
||||
placeholder={`${item.name}${!item.required ? `(${t('appDebug.variableTable.optional')})` : ''}`}
|
||||
value={inputs[item.key]}
|
||||
onChange={(e) => { onInputsChange({ ...inputs, [item.key]: e.target.value }) }}
|
||||
maxLength={item.max_length || DEFAULT_VALUE_MAX_LEN}
|
||||
/>
|
||||
)}
|
||||
{item.type === 'select' && (
|
||||
<Select
|
||||
className='w-full'
|
||||
defaultValue={inputs[item.key]}
|
||||
onSelect={(i) => { onInputsChange({ ...inputs, [item.key]: i.value }) }}
|
||||
items={(item.options || []).map(i => ({ name: i, value: i }))}
|
||||
allowSearch={false}
|
||||
bgClassName='bg-gray-50'
|
||||
/>
|
||||
)}
|
||||
{item.type === 'string' && (
|
||||
<input
|
||||
type="text"
|
||||
className="block w-full p-2 text-gray-900 border border-gray-300 rounded-lg bg-gray-50 sm:text-xs focus:ring-blue-500 focus:border-blue-500 "
|
||||
placeholder={`${item.name}${!item.required ? `(${t('appDebug.variableTable.optional')})` : ''}`}
|
||||
value={inputs[item.key]}
|
||||
onChange={(e) => { onInputsChange({ ...inputs, [item.key]: e.target.value }) }}
|
||||
maxLength={item.max_length || DEFAULT_VALUE_MAX_LEN}
|
||||
/>
|
||||
)}
|
||||
{item.type === 'paragraph' && (
|
||||
<textarea
|
||||
className="block w-full h-[104px] p-2 text-gray-900 border border-gray-300 rounded-lg bg-gray-50 sm:text-xs focus:ring-blue-500 focus:border-blue-500 "
|
||||
placeholder={`${item.name}${!item.required ? `(${t('appDebug.variableTable.optional')})` : ''}`}
|
||||
value={inputs[item.key]}
|
||||
onChange={(e) => { onInputsChange({ ...inputs, [item.key]: e.target.value }) }}
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
{promptConfig.prompt_variables.length > 0 && (
|
||||
<div className='mt-6 h-[1px] bg-gray-100'></div>
|
||||
<div className='mt-4 h-[1px] bg-gray-100'></div>
|
||||
)}
|
||||
<div className='w-full mt-5'>
|
||||
<label className='text-gray-900 text-sm font-medium'>{t('share.generation.queryTitle')}</label>
|
||||
<div className="mt-2 overflow-hidden rounded-lg bg-gray-50 ">
|
||||
<div className="px-4 py-2 bg-gray-50 rounded-t-lg">
|
||||
<textarea
|
||||
value={query}
|
||||
onChange={(e) => { onQueryChange(e.target.value) }}
|
||||
rows={4}
|
||||
className="w-full px-0 text-sm text-gray-900 border-0 bg-gray-50 focus:outline-none placeholder:bg-gray-50"
|
||||
placeholder={t('share.generation.queryPlaceholder') as string}
|
||||
required
|
||||
>
|
||||
</textarea>
|
||||
</div>
|
||||
<div className="flex items-center justify-between px-3 py-2">
|
||||
<div className="flex pl-0 space-x-1 sm:pl-2">
|
||||
<span className="bg-gray-100 text-gray-500 text-xs font-medium mr-2 px-2.5 py-0.5 rounded cursor-pointer">{query?.length}</span>
|
||||
</div>
|
||||
<Button
|
||||
type="primary"
|
||||
className='!h-8 !pl-3 !pr-4'
|
||||
onClick={onSend}
|
||||
disabled={!query || query === ''}
|
||||
>
|
||||
<PlayIcon className="shrink-0 w-4 h-4 mr-1" aria-hidden="true" />
|
||||
<span className='uppercase text-[13px]'>{t('share.generation.run')}</span>
|
||||
</Button>
|
||||
</div>
|
||||
<div className='w-full mt-4'>
|
||||
<div className="flex items-center justify-between">
|
||||
<Button
|
||||
className='!h-8 !p-3'
|
||||
onClick={onClear}
|
||||
disabled={false}
|
||||
>
|
||||
<span className='text-[13px]'>{t('common.operation.clear')}</span>
|
||||
</Button>
|
||||
<Button
|
||||
type="primary"
|
||||
className='!h-8 !pl-3 !pr-4'
|
||||
onClick={onSend}
|
||||
disabled={false}
|
||||
>
|
||||
<PlayIcon className="shrink-0 w-4 h-4 mr-1" aria-hidden="true" />
|
||||
<span className='text-[13px]'>{t('share.generation.run')}</span>
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
|
||||
Reference in New Issue
Block a user