2023-05-15 08:51:32 +08:00
|
|
|
'use client'
|
|
|
|
import type { FC } from 'react'
|
2023-07-19 12:41:35 +08:00
|
|
|
import React, { useEffect, useState } from 'react'
|
2023-05-15 08:51:32 +08:00
|
|
|
import { useTranslation } from 'react-i18next'
|
|
|
|
import { useContext } from 'use-context-selector'
|
2023-07-19 12:41:35 +08:00
|
|
|
import TemplateVarPanel, { PanelTitle, VarOpBtnGroup } from '../value-panel'
|
2023-05-15 08:51:32 +08:00
|
|
|
import s from './style.module.css'
|
2023-07-19 12:41:35 +08:00
|
|
|
import { AppInfo, ChatBtn, EditBtn, FootLogo, PromptTemplate } from './massive-component'
|
2023-05-15 08:51:32 +08:00
|
|
|
import type { SiteInfo } from '@/models/share'
|
|
|
|
import type { PromptConfig } from '@/models/debug'
|
|
|
|
import { ToastContext } from '@/app/components/base/toast'
|
|
|
|
import Select from '@/app/components/base/select'
|
|
|
|
import { DEFAULT_VALUE_MAX_LEN } from '@/config'
|
|
|
|
|
|
|
|
// regex to match the {{}} and replace it with a span
|
|
|
|
const regex = /\{\{([^}]+)\}\}/g
|
|
|
|
|
|
|
|
export type IWelcomeProps = {
|
|
|
|
conversationName: string
|
|
|
|
hasSetInputs: boolean
|
|
|
|
isPublicVersion: boolean
|
|
|
|
siteInfo: SiteInfo
|
|
|
|
promptConfig: PromptConfig
|
|
|
|
onStartChat: (inputs: Record<string, any>) => void
|
|
|
|
canEidtInpus: boolean
|
|
|
|
savedInputs: Record<string, any>
|
|
|
|
onInputsChange: (inputs: Record<string, any>) => void
|
2023-07-27 13:27:34 +08:00
|
|
|
plan?: string
|
2023-05-15 08:51:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const Welcome: FC<IWelcomeProps> = ({
|
|
|
|
conversationName,
|
|
|
|
hasSetInputs,
|
|
|
|
isPublicVersion,
|
|
|
|
siteInfo,
|
|
|
|
plan,
|
|
|
|
promptConfig,
|
|
|
|
onStartChat,
|
|
|
|
canEidtInpus,
|
|
|
|
savedInputs,
|
2023-07-19 12:41:35 +08:00
|
|
|
onInputsChange,
|
2023-05-15 08:51:32 +08:00
|
|
|
}) => {
|
|
|
|
const { t } = useTranslation()
|
|
|
|
const hasVar = promptConfig.prompt_variables.length > 0
|
|
|
|
const [isFold, setIsFold] = useState<boolean>(true)
|
|
|
|
const [inputs, setInputs] = useState<Record<string, any>>((() => {
|
2023-07-19 12:41:35 +08:00
|
|
|
if (hasSetInputs)
|
2023-05-15 08:51:32 +08:00
|
|
|
return savedInputs
|
2023-07-19 12:41:35 +08:00
|
|
|
|
2023-05-15 08:51:32 +08:00
|
|
|
const res: Record<string, any> = {}
|
|
|
|
if (promptConfig) {
|
|
|
|
promptConfig.prompt_variables.forEach((item) => {
|
|
|
|
res[item.key] = ''
|
|
|
|
})
|
|
|
|
}
|
|
|
|
// debugger
|
|
|
|
return res
|
|
|
|
})())
|
|
|
|
useEffect(() => {
|
|
|
|
if (!savedInputs) {
|
|
|
|
const res: Record<string, any> = {}
|
|
|
|
if (promptConfig) {
|
|
|
|
promptConfig.prompt_variables.forEach((item) => {
|
|
|
|
res[item.key] = ''
|
|
|
|
})
|
|
|
|
}
|
|
|
|
setInputs(res)
|
2023-07-19 12:41:35 +08:00
|
|
|
}
|
|
|
|
else {
|
2023-05-15 08:51:32 +08:00
|
|
|
setInputs(savedInputs)
|
|
|
|
}
|
|
|
|
}, [savedInputs])
|
|
|
|
|
|
|
|
const highLightPromoptTemplate = (() => {
|
|
|
|
if (!promptConfig)
|
|
|
|
return ''
|
|
|
|
const res = promptConfig.prompt_template.replace(regex, (match, p1) => {
|
|
|
|
return `<span class='text-gray-800 font-bold'>${inputs?.[p1] ? inputs?.[p1] : match}</span>`
|
|
|
|
})
|
|
|
|
return res
|
|
|
|
})()
|
|
|
|
|
|
|
|
const { notify } = useContext(ToastContext)
|
|
|
|
const logError = (message: string) => {
|
|
|
|
notify({ type: 'error', message, duration: 3000 })
|
|
|
|
}
|
|
|
|
|
|
|
|
const renderHeader = () => {
|
|
|
|
return (
|
|
|
|
<div className='absolute top-0 left-0 right-0 flex items-center justify-between border-b border-gray-100 mobile:h-12 tablet:h-16 px-8 bg-white'>
|
|
|
|
<div className='text-gray-900'>{conversationName}</div>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
const renderInputs = () => {
|
|
|
|
return (
|
|
|
|
<div className='space-y-3'>
|
|
|
|
{promptConfig.prompt_variables.map(item => (
|
|
|
|
<div className='tablet:flex tablet:!h-9 mobile:space-y-2 tablet:space-y-0 mobile:text-xs tablet:text-sm' key={item.key}>
|
|
|
|
<label className={`flex-shrink-0 flex items-center mobile:text-gray-700 tablet:text-gray-900 mobile:font-medium pc:font-normal ${s.formLabel}`}>{item.name}</label>
|
2023-07-19 12:41:35 +08:00
|
|
|
{item.type === 'select'
|
|
|
|
? (
|
|
|
|
<Select
|
|
|
|
className='w-full'
|
|
|
|
defaultValue={inputs?.[item.key]}
|
|
|
|
onSelect={(i) => { setInputs({ ...inputs, [item.key]: i.value }) }}
|
|
|
|
items={(item.options || []).map(i => ({ name: i, value: i }))}
|
|
|
|
allowSearch={false}
|
|
|
|
bgClassName='bg-gray-50'
|
|
|
|
/>
|
|
|
|
)
|
|
|
|
: (
|
|
|
|
<input
|
|
|
|
placeholder={`${item.name}${!item.required ? `(${t('appDebug.variableTable.optional')})` : ''}`}
|
|
|
|
value={inputs?.[item.key] || ''}
|
|
|
|
onChange={(e) => { setInputs({ ...inputs, [item.key]: e.target.value }) }}
|
|
|
|
className={'w-full flex-grow py-2 pl-3 pr-3 box-border rounded-lg bg-gray-50'}
|
|
|
|
maxLength={item.max_length || DEFAULT_VALUE_MAX_LEN}
|
|
|
|
/>
|
|
|
|
)}
|
2023-05-15 08:51:32 +08:00
|
|
|
</div>
|
|
|
|
))}
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
const canChat = () => {
|
|
|
|
const prompt_variables = promptConfig?.prompt_variables
|
2023-07-19 12:41:35 +08:00
|
|
|
if (!inputs || !prompt_variables || prompt_variables?.length === 0)
|
2023-05-15 08:51:32 +08:00
|
|
|
return true
|
2023-07-19 12:41:35 +08:00
|
|
|
|
|
|
|
let hasEmptyInput = ''
|
2023-05-15 08:51:32 +08:00
|
|
|
const requiredVars = prompt_variables?.filter(({ key, name, required }) => {
|
|
|
|
const res = (!key || !key.trim()) || (!name || !name.trim()) || (required || required === undefined || required === null)
|
|
|
|
return res
|
|
|
|
}) || [] // compatible with old version
|
2023-07-19 12:41:35 +08:00
|
|
|
requiredVars.forEach(({ key, name }) => {
|
|
|
|
if (hasEmptyInput)
|
2023-05-15 08:51:32 +08:00
|
|
|
return
|
2023-07-19 12:41:35 +08:00
|
|
|
|
|
|
|
if (!inputs?.[key])
|
|
|
|
hasEmptyInput = name
|
2023-05-15 08:51:32 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
if (hasEmptyInput) {
|
2023-07-19 12:41:35 +08:00
|
|
|
logError(t('appDebug.errorMessage.valueOfVarRequired', { key: hasEmptyInput }))
|
2023-05-15 08:51:32 +08:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
return !hasEmptyInput
|
|
|
|
}
|
|
|
|
|
|
|
|
const handleChat = () => {
|
2023-07-19 12:41:35 +08:00
|
|
|
if (!canChat())
|
2023-05-15 08:51:32 +08:00
|
|
|
return
|
2023-07-19 12:41:35 +08:00
|
|
|
|
2023-05-15 08:51:32 +08:00
|
|
|
onStartChat(inputs)
|
|
|
|
}
|
|
|
|
|
|
|
|
const renderNoVarPanel = () => {
|
|
|
|
if (isPublicVersion) {
|
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
<AppInfo siteInfo={siteInfo} />
|
|
|
|
<TemplateVarPanel
|
|
|
|
isFold={false}
|
|
|
|
header={
|
|
|
|
<>
|
|
|
|
<PanelTitle
|
|
|
|
title={t('share.chat.publicPromptConfigTitle')}
|
|
|
|
className='mb-1'
|
|
|
|
/>
|
|
|
|
<PromptTemplate html={highLightPromoptTemplate} />
|
|
|
|
</>
|
|
|
|
}
|
|
|
|
>
|
|
|
|
<ChatBtn onClick={handleChat} />
|
|
|
|
</TemplateVarPanel>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
// private version
|
|
|
|
return (
|
|
|
|
<TemplateVarPanel
|
|
|
|
isFold={false}
|
|
|
|
header={
|
|
|
|
<AppInfo siteInfo={siteInfo} />
|
|
|
|
}
|
|
|
|
>
|
|
|
|
<ChatBtn onClick={handleChat} />
|
|
|
|
</TemplateVarPanel>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
const renderVarPanel = () => {
|
|
|
|
return (
|
|
|
|
<TemplateVarPanel
|
|
|
|
isFold={false}
|
|
|
|
header={
|
|
|
|
<AppInfo siteInfo={siteInfo} />
|
|
|
|
}
|
|
|
|
>
|
|
|
|
{renderInputs()}
|
|
|
|
<ChatBtn
|
|
|
|
className='mt-3 mobile:ml-0 tablet:ml-[128px]'
|
|
|
|
onClick={handleChat}
|
|
|
|
/>
|
|
|
|
</TemplateVarPanel>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
const renderVarOpBtnGroup = () => {
|
|
|
|
return (
|
|
|
|
<VarOpBtnGroup
|
|
|
|
onConfirm={() => {
|
2023-07-19 12:41:35 +08:00
|
|
|
if (!canChat())
|
2023-05-15 08:51:32 +08:00
|
|
|
return
|
2023-07-19 12:41:35 +08:00
|
|
|
|
2023-05-15 08:51:32 +08:00
|
|
|
onInputsChange(inputs)
|
|
|
|
setIsFold(true)
|
|
|
|
}}
|
|
|
|
onCancel={() => {
|
|
|
|
setInputs(savedInputs)
|
|
|
|
setIsFold(true)
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
const renderHasSetInputsPublic = () => {
|
|
|
|
if (!canEidtInpus) {
|
|
|
|
return (
|
|
|
|
<TemplateVarPanel
|
|
|
|
isFold={false}
|
|
|
|
header={
|
|
|
|
<>
|
|
|
|
<PanelTitle
|
|
|
|
title={t('share.chat.publicPromptConfigTitle')}
|
|
|
|
className='mb-1'
|
|
|
|
/>
|
|
|
|
<PromptTemplate html={highLightPromoptTemplate} />
|
|
|
|
</>
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<TemplateVarPanel
|
|
|
|
isFold={isFold}
|
|
|
|
header={
|
|
|
|
<>
|
|
|
|
<PanelTitle
|
|
|
|
title={t('share.chat.publicPromptConfigTitle')}
|
|
|
|
className='mb-1'
|
|
|
|
/>
|
|
|
|
<PromptTemplate html={highLightPromoptTemplate} />
|
|
|
|
{isFold && (
|
|
|
|
<div className='flex items-center justify-between mt-3 border-t border-indigo-100 pt-4 text-xs text-indigo-600'>
|
|
|
|
<span className='text-gray-700'>{t('share.chat.configStatusDes')}</span>
|
|
|
|
<EditBtn onClick={() => setIsFold(false)} />
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</>
|
|
|
|
}
|
|
|
|
>
|
|
|
|
{renderInputs()}
|
|
|
|
{renderVarOpBtnGroup()}
|
|
|
|
</TemplateVarPanel>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
const renderHasSetInputsPrivate = () => {
|
2023-07-19 12:41:35 +08:00
|
|
|
if (!canEidtInpus || !hasVar)
|
2023-05-15 08:51:32 +08:00
|
|
|
return null
|
2023-07-19 12:41:35 +08:00
|
|
|
|
2023-05-15 08:51:32 +08:00
|
|
|
return (
|
|
|
|
<TemplateVarPanel
|
|
|
|
isFold={isFold}
|
|
|
|
header={
|
|
|
|
<div className='flex items-center justify-between text-indigo-600'>
|
|
|
|
<PanelTitle
|
|
|
|
title={!isFold ? t('share.chat.privatePromptConfigTitle') : t('share.chat.configStatusDes')}
|
|
|
|
/>
|
|
|
|
{isFold && (
|
|
|
|
<EditBtn onClick={() => setIsFold(false)} />
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
}
|
|
|
|
>
|
|
|
|
{renderInputs()}
|
|
|
|
{renderVarOpBtnGroup()}
|
|
|
|
</TemplateVarPanel>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
const renderHasSetInputs = () => {
|
2023-07-19 12:41:35 +08:00
|
|
|
if ((!isPublicVersion && !canEidtInpus) || !hasVar)
|
2023-05-15 08:51:32 +08:00
|
|
|
return null
|
2023-07-19 12:41:35 +08:00
|
|
|
|
2023-05-15 08:51:32 +08:00
|
|
|
return (
|
|
|
|
<div
|
|
|
|
className='pt-[88px] mb-5'
|
|
|
|
>
|
|
|
|
{isPublicVersion ? renderHasSetInputsPublic() : renderHasSetInputsPrivate()}
|
|
|
|
</div>)
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className='relative mobile:min-h-[48px] tablet:min-h-[64px]'>
|
|
|
|
{hasSetInputs && renderHeader()}
|
|
|
|
<div className='mx-auto pc:w-[794px] max-w-full mobile:w-full px-3.5'>
|
|
|
|
{/* Has't set inputs */}
|
|
|
|
{
|
|
|
|
!hasSetInputs && (
|
|
|
|
<div className='mobile:pt-[72px] tablet:pt-[128px] pc:pt-[200px]'>
|
2023-07-19 12:41:35 +08:00
|
|
|
{hasVar
|
|
|
|
? (
|
|
|
|
renderVarPanel()
|
|
|
|
)
|
|
|
|
: (
|
|
|
|
renderNoVarPanel()
|
|
|
|
)}
|
2023-05-15 08:51:32 +08:00
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
{/* Has set inputs */}
|
|
|
|
{hasSetInputs && renderHasSetInputs()}
|
|
|
|
|
|
|
|
{/* foot */}
|
|
|
|
{!hasSetInputs && (
|
|
|
|
<div className='mt-4 flex justify-between items-center h-8 text-xs text-gray-400'>
|
|
|
|
|
|
|
|
{siteInfo.privacy_policy
|
|
|
|
? <div>{t('share.chat.privacyPolicyLeft')}
|
|
|
|
<a
|
|
|
|
className='text-gray-500'
|
|
|
|
href={siteInfo.privacy_policy}
|
|
|
|
target='_blank'>{t('share.chat.privacyPolicyMiddle')}</a>
|
|
|
|
{t('share.chat.privacyPolicyRight')}
|
|
|
|
</div>
|
|
|
|
: <div>
|
|
|
|
</div>}
|
|
|
|
{plan === 'basic' && <a className='flex items-center pr-3 space-x-3' href="https://dify.ai/" target="_blank">
|
|
|
|
<span className='uppercase'>{t('share.chat.powerBy')}</span>
|
|
|
|
<FootLogo />
|
|
|
|
</a>}
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
</div >
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export default React.memo(Welcome)
|