feat: webapp support change inputs after conversation started (#16901)

This commit is contained in:
KVOJJJin 2025-03-27 11:58:16 +08:00 committed by GitHub
parent 0722beeb0b
commit c23135c9e8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
15 changed files with 76 additions and 36 deletions

View File

@ -29,6 +29,7 @@ const ChatWrapper = () => {
appPrevChatTree, appPrevChatTree,
currentConversationId, currentConversationId,
currentConversationItem, currentConversationItem,
currentConversationInputs,
inputsForms, inputsForms,
newConversationInputs, newConversationInputs,
newConversationInputsRef, newConversationInputsRef,
@ -69,7 +70,7 @@ const ChatWrapper = () => {
} = useChat( } = useChat(
appConfig, appConfig,
{ {
inputs: (currentConversationId ? currentConversationItem?.inputs : newConversationInputs) as any, inputs: (currentConversationId ? currentConversationInputs : newConversationInputs) as any,
inputsForm: inputsForms, inputsForm: inputsForms,
}, },
appPrevChatTree, appPrevChatTree,
@ -77,7 +78,7 @@ const ChatWrapper = () => {
clearChatList, clearChatList,
setClearChatList, setClearChatList,
) )
const inputsFormValue = currentConversationId ? currentConversationItem?.inputs : newConversationInputsRef?.current const inputsFormValue = currentConversationId ? currentConversationInputs : newConversationInputsRef?.current
const inputDisabled = useMemo(() => { const inputDisabled = useMemo(() => {
let hasEmptyInput = '' let hasEmptyInput = ''
let fileIsUploading = false let fileIsUploading = false
@ -124,7 +125,7 @@ const ChatWrapper = () => {
const data: any = { const data: any = {
query: message, query: message,
files, files,
inputs: currentConversationId ? currentConversationItem?.inputs : newConversationInputs, inputs: currentConversationId ? currentConversationInputs : newConversationInputs,
conversation_id: currentConversationId, conversation_id: currentConversationId,
parent_message_id: (isRegenerate ? parentAnswer?.id : getLastAnswer(chatList)?.id) || null, parent_message_id: (isRegenerate ? parentAnswer?.id : getLastAnswer(chatList)?.id) || null,
} }
@ -144,6 +145,7 @@ const ChatWrapper = () => {
handleSend, handleSend,
currentConversationId, currentConversationId,
currentConversationItem, currentConversationItem,
currentConversationInputs,
newConversationInputs, newConversationInputs,
isInstalledApp, isInstalledApp,
appId, appId,
@ -245,7 +247,7 @@ const ChatWrapper = () => {
chatFooterClassName='pb-4' chatFooterClassName='pb-4'
chatFooterInnerClassName={`mx-auto w-full max-w-[768px] ${isMobile ? 'px-2' : 'px-4'}`} chatFooterInnerClassName={`mx-auto w-full max-w-[768px] ${isMobile ? 'px-2' : 'px-4'}`}
onSend={doSend} onSend={doSend}
inputs={currentConversationId ? currentConversationItem?.inputs as any : newConversationInputs} inputs={currentConversationId ? currentConversationInputs as any : newConversationInputs}
inputsForm={inputsForms} inputsForm={inputsForms}
onRegenerate={doRegenerate} onRegenerate={doRegenerate}
onStopResponding={handleStop} onStopResponding={handleStop}

View File

@ -54,6 +54,8 @@ export type ChatWithHistoryContextValue = {
setClearChatList: (state: boolean) => void setClearChatList: (state: boolean) => void
isResponding?: boolean isResponding?: boolean
setIsResponding: (state: boolean) => void, setIsResponding: (state: boolean) => void,
currentConversationInputs: Record<string, any> | null,
setCurrentConversationInputs: (v: Record<string, any>) => void,
} }
export const ChatWithHistoryContext = createContext<ChatWithHistoryContextValue>({ export const ChatWithHistoryContext = createContext<ChatWithHistoryContextValue>({
@ -85,5 +87,7 @@ export const ChatWithHistoryContext = createContext<ChatWithHistoryContextValue>
setClearChatList: () => {}, setClearChatList: () => {},
isResponding: false, isResponding: false,
setIsResponding: () => {}, setIsResponding: () => {},
currentConversationInputs: {},
setCurrentConversationInputs: () => {},
}) })
export const useChatWithHistoryContext = () => useContext(ChatWithHistoryContext) export const useChatWithHistoryContext = () => useContext(ChatWithHistoryContext)

View File

@ -120,7 +120,7 @@ const HeaderInMobile = () => {
<div className='system-xl-semibold grow text-text-secondary'>{t('share.chat.chatSettingsTitle')}</div> <div className='system-xl-semibold grow text-text-secondary'>{t('share.chat.chatSettingsTitle')}</div>
</div> </div>
<div className='p-4'> <div className='p-4'>
<InputsFormContent showTip /> <InputsFormContent />
</div> </div>
</div> </div>
</div> </div>

View File

@ -263,6 +263,17 @@ export const useChatWithHistory = (installedAppInfo?: InstalledApp) => {
return conversationItem return conversationItem
}, [conversationList, currentConversationId, pinnedConversationList]) }, [conversationList, currentConversationId, pinnedConversationList])
const currentConversationLatestInputs = useMemo(() => {
if (!currentConversationId || !appChatListData?.data.length)
return {}
return appChatListData.data.slice().pop().inputs || {}
}, [appChatListData, currentConversationId])
const [currentConversationInputs, setCurrentConversationInputs] = useState<Record<string, any>>(currentConversationLatestInputs || {})
useEffect(() => {
if (currentConversationItem)
setCurrentConversationInputs(currentConversationLatestInputs || {})
}, [currentConversationItem, currentConversationLatestInputs])
const { notify } = useToastContext() const { notify } = useToastContext()
const checkInputsRequired = useCallback((silent?: boolean) => { const checkInputsRequired = useCallback((silent?: boolean) => {
let hasEmptyInput = '' let hasEmptyInput = ''
@ -464,5 +475,7 @@ export const useChatWithHistory = (installedAppInfo?: InstalledApp) => {
setClearChatList, setClearChatList,
isResponding, isResponding,
setIsResponding, setIsResponding,
currentConversationInputs,
setCurrentConversationInputs,
} }
} }

View File

@ -157,6 +157,8 @@ const ChatWithHistoryWrap: FC<ChatWithHistoryWrapProps> = ({
setClearChatList, setClearChatList,
isResponding, isResponding,
setIsResponding, setIsResponding,
currentConversationInputs,
setCurrentConversationInputs,
} = useChatWithHistory(installedAppInfo) } = useChatWithHistory(installedAppInfo)
return ( return (
@ -198,6 +200,8 @@ const ChatWithHistoryWrap: FC<ChatWithHistoryWrapProps> = ({
setClearChatList, setClearChatList,
isResponding, isResponding,
setIsResponding, setIsResponding,
currentConversationInputs,
setCurrentConversationInputs,
}}> }}>
<ChatWithHistory className={className} /> <ChatWithHistory className={className} />
</ChatWithHistoryContext.Provider> </ChatWithHistoryContext.Provider>

View File

@ -17,20 +17,24 @@ const InputsFormContent = ({ showTip }: Props) => {
appParams, appParams,
inputsForms, inputsForms,
currentConversationId, currentConversationId,
currentConversationItem, currentConversationInputs,
setCurrentConversationInputs,
newConversationInputs, newConversationInputs,
newConversationInputsRef, newConversationInputsRef,
handleNewConversationInputsChange, handleNewConversationInputsChange,
} = useChatWithHistoryContext() } = useChatWithHistoryContext()
const inputsFormValue = currentConversationId ? currentConversationItem?.inputs : newConversationInputs const inputsFormValue = currentConversationId ? currentConversationInputs : newConversationInputs
const readonly = !!currentConversationId
const handleFormChange = useCallback((variable: string, value: any) => { const handleFormChange = useCallback((variable: string, value: any) => {
setCurrentConversationInputs({
...currentConversationInputs,
[variable]: value,
})
handleNewConversationInputsChange({ handleNewConversationInputsChange({
...newConversationInputsRef.current, ...newConversationInputsRef.current,
[variable]: value, [variable]: value,
}) })
}, [newConversationInputsRef, handleNewConversationInputsChange]) }, [newConversationInputsRef, handleNewConversationInputsChange, currentConversationInputs, setCurrentConversationInputs])
return ( return (
<div className='space-y-4'> <div className='space-y-4'>
@ -47,8 +51,6 @@ const InputsFormContent = ({ showTip }: Props) => {
value={inputsFormValue?.[form.variable] || ''} value={inputsFormValue?.[form.variable] || ''}
onChange={e => handleFormChange(form.variable, e.target.value)} onChange={e => handleFormChange(form.variable, e.target.value)}
placeholder={form.label} placeholder={form.label}
readOnly={readonly}
disabled={readonly}
/> />
)} )}
{form.type === InputVarType.number && ( {form.type === InputVarType.number && (
@ -57,8 +59,6 @@ const InputsFormContent = ({ showTip }: Props) => {
value={inputsFormValue?.[form.variable] || ''} value={inputsFormValue?.[form.variable] || ''}
onChange={e => handleFormChange(form.variable, e.target.value)} onChange={e => handleFormChange(form.variable, e.target.value)}
placeholder={form.label} placeholder={form.label}
readOnly={readonly}
disabled={readonly}
/> />
)} )}
{form.type === InputVarType.paragraph && ( {form.type === InputVarType.paragraph && (
@ -66,8 +66,6 @@ const InputsFormContent = ({ showTip }: Props) => {
value={inputsFormValue?.[form.variable] || ''} value={inputsFormValue?.[form.variable] || ''}
onChange={e => handleFormChange(form.variable, e.target.value)} onChange={e => handleFormChange(form.variable, e.target.value)}
placeholder={form.label} placeholder={form.label}
readOnly={readonly}
disabled={readonly}
/> />
)} )}
{form.type === InputVarType.select && ( {form.type === InputVarType.select && (
@ -77,7 +75,6 @@ const InputsFormContent = ({ showTip }: Props) => {
items={form.options.map((option: string) => ({ value: option, name: option }))} items={form.options.map((option: string) => ({ value: option, name: option }))}
onSelect={item => handleFormChange(form.variable, item.value as string)} onSelect={item => handleFormChange(form.variable, item.value as string)}
placeholder={form.label} placeholder={form.label}
readonly={readonly}
/> />
)} )}
{form.type === InputVarType.singleFile && ( {form.type === InputVarType.singleFile && (

View File

@ -38,7 +38,7 @@ const InputsFormNode = ({
<Message3Fill className='h-6 w-6 shrink-0' /> <Message3Fill className='h-6 w-6 shrink-0' />
<div className='system-xl-semibold grow text-text-secondary'>{t('share.chat.chatSettingsTitle')}</div> <div className='system-xl-semibold grow text-text-secondary'>{t('share.chat.chatSettingsTitle')}</div>
{collapsed && ( {collapsed && (
<Button className='uppercase text-text-tertiary' size='small' variant='ghost' onClick={() => setCollapsed(false)}>{currentConversationId ? t('common.operation.view') : t('common.operation.edit')}</Button> <Button className='uppercase text-text-tertiary' size='small' variant='ghost' onClick={() => setCollapsed(false)}>{t('common.operation.edit')}</Button>
)} )}
{!collapsed && currentConversationId && ( {!collapsed && currentConversationId && (
<Button className='uppercase text-text-tertiary' size='small' variant='ghost' onClick={() => setCollapsed(true)}>{t('common.operation.close')}</Button> <Button className='uppercase text-text-tertiary' size='small' variant='ghost' onClick={() => setCollapsed(true)}>{t('common.operation.close')}</Button>
@ -46,7 +46,7 @@ const InputsFormNode = ({
</div> </div>
{!collapsed && ( {!collapsed && (
<div className={cn('p-6', isMobile && 'p-4')}> <div className={cn('p-6', isMobile && 'p-4')}>
<InputsFormContent showTip={!!currentConversationId} /> <InputsFormContent />
</div> </div>
)} )}
{!collapsed && !currentConversationId && ( {!collapsed && !currentConversationId && (

View File

@ -36,7 +36,7 @@ const ViewFormDropdown = () => {
<div className='system-xl-semibold grow text-text-secondary'>{t('share.chat.chatSettingsTitle')}</div> <div className='system-xl-semibold grow text-text-secondary'>{t('share.chat.chatSettingsTitle')}</div>
</div> </div>
<div className='p-6'> <div className='p-6'>
<InputsFormContent showTip /> <InputsFormContent />
</div> </div>
</div> </div>
</PortalToFollowElemContent> </PortalToFollowElemContent>

View File

@ -32,6 +32,7 @@ const ChatWrapper = () => {
appPrevChatList, appPrevChatList,
currentConversationId, currentConversationId,
currentConversationItem, currentConversationItem,
currentConversationInputs,
inputsForms, inputsForms,
newConversationInputs, newConversationInputs,
newConversationInputsRef, newConversationInputsRef,
@ -70,7 +71,7 @@ const ChatWrapper = () => {
} = useChat( } = useChat(
appConfig, appConfig,
{ {
inputs: (currentConversationId ? currentConversationItem?.inputs : newConversationInputs) as any, inputs: (currentConversationId ? currentConversationInputs : newConversationInputs) as any,
inputsForm: inputsForms, inputsForm: inputsForms,
}, },
appPrevChatList, appPrevChatList,
@ -78,7 +79,7 @@ const ChatWrapper = () => {
clearChatList, clearChatList,
setClearChatList, setClearChatList,
) )
const inputsFormValue = currentConversationId ? currentConversationItem?.inputs : newConversationInputsRef?.current const inputsFormValue = currentConversationId ? currentConversationInputs : newConversationInputsRef?.current
const inputDisabled = useMemo(() => { const inputDisabled = useMemo(() => {
let hasEmptyInput = '' let hasEmptyInput = ''
let fileIsUploading = false let fileIsUploading = false
@ -123,7 +124,7 @@ const ChatWrapper = () => {
const data: any = { const data: any = {
query: message, query: message,
files, files,
inputs: currentConversationId ? currentConversationItem?.inputs : newConversationInputs, inputs: currentConversationId ? currentConversationInputs : newConversationInputs,
conversation_id: currentConversationId, conversation_id: currentConversationId,
parent_message_id: (isRegenerate ? parentAnswer?.id : getLastAnswer(chatList)?.id) || null, parent_message_id: (isRegenerate ? parentAnswer?.id : getLastAnswer(chatList)?.id) || null,
} }
@ -241,7 +242,7 @@ const ChatWrapper = () => {
chatFooterClassName={cn('pb-4', !isMobile && 'rounded-b-2xl')} chatFooterClassName={cn('pb-4', !isMobile && 'rounded-b-2xl')}
chatFooterInnerClassName={cn('mx-auto w-full max-w-full px-4', isMobile && 'px-2')} chatFooterInnerClassName={cn('mx-auto w-full max-w-full px-4', isMobile && 'px-2')}
onSend={doSend} onSend={doSend}
inputs={currentConversationId ? currentConversationItem?.inputs as any : newConversationInputs} inputs={currentConversationId ? currentConversationInputs as any : newConversationInputs}
inputsForm={inputsForms} inputsForm={inputsForms}
onRegenerate={doRegenerate} onRegenerate={doRegenerate}
onStopResponding={handleStop} onStopResponding={handleStop}

View File

@ -46,6 +46,8 @@ export type EmbeddedChatbotContextValue = {
setClearChatList: (state: boolean) => void setClearChatList: (state: boolean) => void
isResponding?: boolean isResponding?: boolean
setIsResponding: (state: boolean) => void, setIsResponding: (state: boolean) => void,
currentConversationInputs: Record<string, any> | null,
setCurrentConversationInputs: (v: Record<string, any>) => void,
} }
export const EmbeddedChatbotContext = createContext<EmbeddedChatbotContextValue>({ export const EmbeddedChatbotContext = createContext<EmbeddedChatbotContextValue>({
@ -70,5 +72,7 @@ export const EmbeddedChatbotContext = createContext<EmbeddedChatbotContextValue>
setClearChatList: () => {}, setClearChatList: () => {},
isResponding: false, isResponding: false,
setIsResponding: () => {}, setIsResponding: () => {},
currentConversationInputs: {},
setCurrentConversationInputs: () => {},
}) })
export const useEmbeddedChatbotContext = () => useContext(EmbeddedChatbotContext) export const useEmbeddedChatbotContext = () => useContext(EmbeddedChatbotContext)

View File

@ -239,6 +239,17 @@ export const useEmbeddedChatbot = () => {
return conversationItem return conversationItem
}, [conversationList, currentConversationId, pinnedConversationList]) }, [conversationList, currentConversationId, pinnedConversationList])
const currentConversationLatestInputs = useMemo(() => {
if (!currentConversationId || !appChatListData?.data.length)
return {}
return appChatListData.data.slice().pop().inputs || {}
}, [appChatListData, currentConversationId])
const [currentConversationInputs, setCurrentConversationInputs] = useState<Record<string, any>>(currentConversationLatestInputs || {})
useEffect(() => {
if (currentConversationItem)
setCurrentConversationInputs(currentConversationLatestInputs || {})
}, [currentConversationItem, currentConversationLatestInputs])
const { notify } = useToastContext() const { notify } = useToastContext()
const checkInputsRequired = useCallback((silent?: boolean) => { const checkInputsRequired = useCallback((silent?: boolean) => {
let hasEmptyInput = '' let hasEmptyInput = ''
@ -347,5 +358,7 @@ export const useEmbeddedChatbot = () => {
setClearChatList, setClearChatList,
isResponding, isResponding,
setIsResponding, setIsResponding,
currentConversationInputs,
setCurrentConversationInputs,
} }
} }

View File

@ -160,6 +160,8 @@ const EmbeddedChatbotWrapper = () => {
setClearChatList, setClearChatList,
isResponding, isResponding,
setIsResponding, setIsResponding,
currentConversationInputs,
setCurrentConversationInputs,
} = useEmbeddedChatbot() } = useEmbeddedChatbot()
return <EmbeddedChatbotContext.Provider value={{ return <EmbeddedChatbotContext.Provider value={{
@ -193,6 +195,8 @@ const EmbeddedChatbotWrapper = () => {
setClearChatList, setClearChatList,
isResponding, isResponding,
setIsResponding, setIsResponding,
currentConversationInputs,
setCurrentConversationInputs,
}}> }}>
<Chatbot /> <Chatbot />
</EmbeddedChatbotContext.Provider> </EmbeddedChatbotContext.Provider>

View File

@ -17,20 +17,25 @@ const InputsFormContent = ({ showTip }: Props) => {
appParams, appParams,
inputsForms, inputsForms,
currentConversationId, currentConversationId,
currentConversationItem, currentConversationInputs,
setCurrentConversationInputs,
newConversationInputs, newConversationInputs,
newConversationInputsRef, newConversationInputsRef,
handleNewConversationInputsChange, handleNewConversationInputsChange,
} = useEmbeddedChatbotContext() } = useEmbeddedChatbotContext()
const inputsFormValue = currentConversationId ? currentConversationItem?.inputs : newConversationInputs const inputsFormValue = currentConversationId ? currentConversationInputs : newConversationInputs
const readonly = !!currentConversationId const readonly = !!currentConversationId
const handleFormChange = useCallback((variable: string, value: any) => { const handleFormChange = useCallback((variable: string, value: any) => {
setCurrentConversationInputs({
...currentConversationInputs,
[variable]: value,
})
handleNewConversationInputsChange({ handleNewConversationInputsChange({
...newConversationInputsRef.current, ...newConversationInputsRef.current,
[variable]: value, [variable]: value,
}) })
}, [newConversationInputsRef, handleNewConversationInputsChange]) }, [newConversationInputsRef, handleNewConversationInputsChange, currentConversationInputs, setCurrentConversationInputs])
return ( return (
<div className='space-y-4'> <div className='space-y-4'>
@ -47,8 +52,6 @@ const InputsFormContent = ({ showTip }: Props) => {
value={inputsFormValue?.[form.variable] || ''} value={inputsFormValue?.[form.variable] || ''}
onChange={e => handleFormChange(form.variable, e.target.value)} onChange={e => handleFormChange(form.variable, e.target.value)}
placeholder={form.label} placeholder={form.label}
readOnly={readonly}
disabled={readonly}
/> />
)} )}
{form.type === InputVarType.number && ( {form.type === InputVarType.number && (
@ -57,8 +60,6 @@ const InputsFormContent = ({ showTip }: Props) => {
value={inputsFormValue?.[form.variable] || ''} value={inputsFormValue?.[form.variable] || ''}
onChange={e => handleFormChange(form.variable, e.target.value)} onChange={e => handleFormChange(form.variable, e.target.value)}
placeholder={form.label} placeholder={form.label}
readOnly={readonly}
disabled={readonly}
/> />
)} )}
{form.type === InputVarType.paragraph && ( {form.type === InputVarType.paragraph && (
@ -66,8 +67,6 @@ const InputsFormContent = ({ showTip }: Props) => {
value={inputsFormValue?.[form.variable] || ''} value={inputsFormValue?.[form.variable] || ''}
onChange={e => handleFormChange(form.variable, e.target.value)} onChange={e => handleFormChange(form.variable, e.target.value)}
placeholder={form.label} placeholder={form.label}
readOnly={readonly}
disabled={readonly}
/> />
)} )}
{form.type === InputVarType.select && ( {form.type === InputVarType.select && (
@ -77,7 +76,6 @@ const InputsFormContent = ({ showTip }: Props) => {
items={form.options.map((option: string) => ({ value: option, name: option }))} items={form.options.map((option: string) => ({ value: option, name: option }))}
onSelect={item => handleFormChange(form.variable, item.value as string)} onSelect={item => handleFormChange(form.variable, item.value as string)}
placeholder={form.label} placeholder={form.label}
readonly={readonly}
/> />
)} )}
{form.type === InputVarType.singleFile && ( {form.type === InputVarType.singleFile && (

View File

@ -38,7 +38,7 @@ const InputsFormNode = ({
<Message3Fill className='h-6 w-6 shrink-0' /> <Message3Fill className='h-6 w-6 shrink-0' />
<div className='system-xl-semibold grow text-text-secondary'>{t('share.chat.chatSettingsTitle')}</div> <div className='system-xl-semibold grow text-text-secondary'>{t('share.chat.chatSettingsTitle')}</div>
{collapsed && ( {collapsed && (
<Button className='uppercase text-text-tertiary' size='small' variant='ghost' onClick={() => setCollapsed(false)}>{currentConversationId ? t('common.operation.view') : t('common.operation.edit')}</Button> <Button className='uppercase text-text-tertiary' size='small' variant='ghost' onClick={() => setCollapsed(false)}>{t('common.operation.edit')}</Button>
)} )}
{!collapsed && currentConversationId && ( {!collapsed && currentConversationId && (
<Button className='uppercase text-text-tertiary' size='small' variant='ghost' onClick={() => setCollapsed(true)}>{t('common.operation.close')}</Button> <Button className='uppercase text-text-tertiary' size='small' variant='ghost' onClick={() => setCollapsed(true)}>{t('common.operation.close')}</Button>
@ -46,7 +46,7 @@ const InputsFormNode = ({
</div> </div>
{!collapsed && ( {!collapsed && (
<div className={cn('p-6', isMobile && 'p-4')}> <div className={cn('p-6', isMobile && 'p-4')}>
<InputsFormContent showTip={!!currentConversationId} /> <InputsFormContent />
</div> </div>
)} )}
{!collapsed && !currentConversationId && ( {!collapsed && !currentConversationId && (

View File

@ -40,7 +40,7 @@ const ViewFormDropdown = ({ iconColor }: Props) => {
<div className='system-xl-semibold grow text-text-secondary'>{t('share.chat.chatSettingsTitle')}</div> <div className='system-xl-semibold grow text-text-secondary'>{t('share.chat.chatSettingsTitle')}</div>
</div> </div>
<div className='p-6'> <div className='p-6'>
<InputsFormContent showTip /> <InputsFormContent />
</div> </div>
</div> </div>
</PortalToFollowElemContent> </PortalToFollowElemContent>