Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

7.7 KB
Raw Blame
import { Input } from '@langgenius/dify-ui/input'
import {
  Select,
  SelectContent,
  SelectItem,
  SelectItemIndicator,
  SelectItemText,
  SelectTrigger,
  SelectValue,
} from '@langgenius/dify-ui/select'
import { Textarea } from '@langgenius/dify-ui/textarea'
import * as React from 'react'
import { memo, useCallback, useId } from 'react'
import { useTranslation } from 'react-i18next'
import { FileUploaderInAttachmentWrapper } from '@/app/components/base/file-uploader'
import BoolInput from '@/app/components/workflow/nodes/_base/components/before-run-form/bool-input'
import CodeEditor from '@/app/components/workflow/nodes/_base/components/editor/code-editor'
import { CodeLanguage } from '@/app/components/workflow/nodes/code/types'
import { InputVarType } from '@/app/components/workflow/types'
import { AppSourceType } from '@/service/share'
import { useEmbeddedChatbotContext } from '../context'

type Props = Readonly<{
  showTip?: boolean
}>

const InputsFormContent = ({ showTip }: Props) => {
  const { t } = useTranslation(['share', 'workflow'])
  const baseId = useId()
  const {
    appParams,
    appSourceType,
    clearChatList,
    inputsForms,
    currentConversationId,
    currentConversationInputs,
    setCurrentConversationInputs,
    newConversationInputs,
    newConversationInputsRef,
    handleNewConversationInputsChange,
  } = useEmbeddedChatbotContext()
  const resettingFiles = clearChatList && appSourceType !== AppSourceType.tryApp
  const inputsFormValue = currentConversationId ? currentConversationInputs : newConversationInputs
  const fileInputsFormValue = currentConversationId
    ? inputsFormValue
    : {
        ...Object.fromEntries(
          inputsForms
            .filter(
              (form) =>
                form.type === InputVarType.singleFile || form.type === InputVarType.multiFiles,
            )
            .map((form) => [form.variable, form.default]),
        ),
        ...inputsFormValue,
      }

  const handleFormChange = useCallback(
    (variable: string, value: any) => {
      setCurrentConversationInputs({
        ...currentConversationInputs,
        [variable]: value,
      })
      handleNewConversationInputsChange({
        ...newConversationInputsRef.current,
        [variable]: value,
      })
    },
    [
      newConversationInputsRef,
      handleNewConversationInputsChange,
      currentConversationInputs,
      setCurrentConversationInputs,
    ],
  )

  const visibleInputsForms = inputsForms.filter((form) => form.hide !== true)

  return (
    <div className="space-y-4">
      {visibleInputsForms.map((form) => (
        <div
          key={form.variable}
          className="space-y-1"
          data-testid={`inputs-form-item-${form.variable}`}
        >
          {form.type !== InputVarType.checkbox && (
            <div className="flex h-6 items-center gap-1">
              <div
                id={`${baseId}-${form.variable}-label`}
                className="system-md-semibold text-text-secondary"
              >
                {form.label}
              </div>
              {!form.required && (
                <div className="system-xs-regular text-text-tertiary">
                  {t(($) => $['panel.optional'], { ns: 'workflow' })}
                </div>
              )}
            </div>
          )}
          {form.type === InputVarType.textInput && (
            <Input
              aria-labelledby={`${baseId}-${form.variable}-label`}
              value={inputsFormValue?.[form.variable] || ''}
              onValueChange={(value) => handleFormChange(form.variable, value)}
              placeholder={form.label}
            />
          )}
          {form.type === InputVarType.number && (
            <Input
              aria-labelledby={`${baseId}-${form.variable}-label`}
              type="number"
              value={inputsFormValue?.[form.variable] ?? ''}
              onValueChange={(value) => handleFormChange(form.variable, value)}
              placeholder={form.label}
            />
          )}
          {form.type === InputVarType.paragraph && (
            <Textarea
              aria-labelledby={`${baseId}-${form.variable}-label`}
              value={inputsFormValue?.[form.variable] || ''}
              onValueChange={(value) => handleFormChange(form.variable, value)}
              placeholder={form.label}
            />
          )}
          {form.type === InputVarType.checkbox && (
            <BoolInput
              name={form.label}
              value={inputsFormValue?.[form.variable]}
              required={form.required}
              onChange={(value) => handleFormChange(form.variable, value)}
            />
          )}
          {form.type === InputVarType.select && (
            <Select<string>
              value={(inputsFormValue?.[form.variable] ?? form.default ?? '') || null}
              onValueChange={(value) => value && handleFormChange(form.variable, value)}
            >
              <SelectTrigger
                aria-labelledby={`${baseId}-${form.variable}-label`}
                className="w-full"
              >
                <SelectValue placeholder={form.label} />
              </SelectTrigger>
              <SelectContent>
                {form.options.map((option: string) => (
                  <SelectItem key={option} value={option}>
                    <SelectItemText>{option}</SelectItemText>
                    <SelectItemIndicator />
                  </SelectItem>
                ))}
              </SelectContent>
            </Select>
          )}
          {!resettingFiles && form.type === InputVarType.singleFile && (
            <FileUploaderInAttachmentWrapper
              key={currentConversationId}
              value={
                fileInputsFormValue?.[form.variable] ? [fileInputsFormValue[form.variable]] : []
              }
              onChange={(files) =>
                handleFormChange(form.variable, files[0] ?? (form.required ? undefined : ''))
              }
              fileConfig={{
                allowed_file_types: form.allowed_file_types,
                allowed_file_extensions: form.allowed_file_extensions,
                allowed_file_upload_methods: form.allowed_file_upload_methods,
                number_limits: 1,
                fileUploadConfig: (appParams as any).system_parameters,
              }}
            />
          )}
          {!resettingFiles && form.type === InputVarType.multiFiles && (
            <FileUploaderInAttachmentWrapper
              key={currentConversationId}
              value={fileInputsFormValue?.[form.variable] || []}
              onChange={(files) => handleFormChange(form.variable, files)}
              fileConfig={{
                allowed_file_types: form.allowed_file_types,
                allowed_file_extensions: form.allowed_file_extensions,
                allowed_file_upload_methods: form.allowed_file_upload_methods,
                number_limits: form.max_length,
                fileUploadConfig: (appParams as any).system_parameters,
              }}
            />
          )}
          {form.type === InputVarType.jsonObject && (
            <CodeEditor
              language={CodeLanguage.json}
              value={inputsFormValue?.[form.variable] || ''}
              onChange={(v) => handleFormChange(form.variable, v)}
              noWrapper
              className="h-20 overflow-y-auto rounded-[10px] bg-components-input-bg-normal p-1"
              placeholder={<div className="whitespace-pre">{form.json_schema}</div>}
            />
          )}
        </div>
      ))}
      {showTip && (
        <div className="system-xs-regular text-text-tertiary">
          {t(($) => $['chat.chatFormTip'], { ns: 'share' })}
        </div>
      )}
    </div>
  )
}

export default memo(InputsFormContent)