feat: passing the inputs values using difyChatbotConfig (#6376)

This commit is contained in:
yoyocircle
2024-07-18 21:54:16 +08:00
committed by GitHub
parent e493ce9981
commit 284ef52bba
4 changed files with 82 additions and 42 deletions

View File

@@ -31,6 +31,7 @@ import type {
import { addFileInfos, sortAgentSorts } from '@/app/components/tools/utils'
import { useToastContext } from '@/app/components/base/toast'
import { changeLanguage } from '@/i18n/i18next-config'
import { getProcessedInputsFromUrlParams } from '@/app/components/base/chat/utils'
export const useEmbeddedChatbot = () => {
const isInstalledApp = false
@@ -109,6 +110,7 @@ export const useEmbeddedChatbot = () => {
const { t } = useTranslation()
const newConversationInputsRef = useRef<Record<string, any>>({})
const [newConversationInputs, setNewConversationInputs] = useState<Record<string, any>>({})
const [initInputs, setInitInputs] = useState<Record<string, any>>({})
const handleNewConversationInputsChange = useCallback((newInputs: Record<string, any>) => {
newConversationInputsRef.current = newInputs
setNewConversationInputs(newInputs)
@@ -116,30 +118,49 @@ export const useEmbeddedChatbot = () => {
const inputsForms = useMemo(() => {
return (appParams?.user_input_form || []).filter((item: any) => item.paragraph || item.select || item['text-input'] || item.number).map((item: any) => {
if (item.paragraph) {
let value = initInputs[item.paragraph.variable]
if (value && item.paragraph.max_length && value.length > item.paragraph.max_length)
value = value.slice(0, item.paragraph.max_length)
return {
...item.paragraph,
default: value || item.default,
type: 'paragraph',
}
}
if (item.number) {
const convertedNumber = Number(initInputs[item.number.variable]) ?? undefined
return {
...item.number,
default: convertedNumber || item.default,
type: 'number',
}
}
if (item.select) {
const isInputInOptions = item.select.options.includes(initInputs[item.select.variable])
return {
...item.select,
default: (isInputInOptions ? initInputs[item.select.variable] : undefined) || item.default,
type: 'select',
}
}
let value = initInputs[item['text-input'].variable]
if (value && item['text-input'].max_length && value.length > item['text-input'].max_length)
value = value.slice(0, item['text-input'].max_length)
return {
...item['text-input'],
default: value || item.default,
type: 'text-input',
}
})
}, [appParams])
useEffect(() => {
// init inputs from url params
setInitInputs(getProcessedInputsFromUrlParams())
}, [])
useEffect(() => {
const conversationInputs: Record<string, any> = {}

View File

@@ -0,0 +1,20 @@
async function decodeBase64AndDecompress(base64String: string) {
const binaryString = atob(base64String)
const compressedUint8Array = Uint8Array.from(binaryString, char => char.charCodeAt(0))
const decompressedStream = new Response(compressedUint8Array).body.pipeThrough(new DecompressionStream('gzip'))
const decompressedArrayBuffer = await new Response(decompressedStream).arrayBuffer()
return new TextDecoder().decode(decompressedArrayBuffer)
}
function getProcessedInputsFromUrlParams(): Record<string, any> {
const urlParams = new URLSearchParams(window.location.search)
const inputs: Record<string, any> = {}
urlParams.forEach(async (value, key) => {
inputs[key] = await decodeBase64AndDecompress(decodeURIComponent(value))
})
return inputs
}
export {
getProcessedInputsFromUrlParams,
}