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.

5.6 KB
Raw Blame
import type { UploadConfig } from '@dify/contracts/api/console/files/types.gen'
import type { FileEntity } from '@/app/components/base/file-uploader/types'
import type { VarInInspect } from '@/types/workflow'
import { Textarea } from '@langgenius/dify-ui/textarea'
import { useTranslation } from 'react-i18next'
import { FileUploaderInAttachmentWrapper } from '@/app/components/base/file-uploader'
import { FILE_EXTS } from '@/app/components/base/prompt-editor/constants'
import ErrorMessage from '@/app/components/workflow/nodes/llm/components/json-schema-config-modal/error-message'
import SchemaEditor from '@/app/components/workflow/nodes/llm/components/json-schema-config-modal/schema-editor'
import { SupportUploadFileTypes } from '@/app/components/workflow/types'
import { TransferMethod } from '@/types/app'
import { PreviewMode } from '../../base/features/types'
import BoolValue from '../panel/chat-variable-panel/components/bool-value'
import { DisplayContent } from './display-content'
import LargeDataAlert from './large-data-alert'
import { PreviewType } from './types'

type TextEditorSectionProps = {
  currentVar: VarInInspect
  value: unknown
  textEditorDisabled: boolean
  isTruncated: boolean
  onTextChange: (value: string) => void
}

export const TextEditorSection = ({
  currentVar,
  value,
  textEditorDisabled,
  isTruncated,
  onTextChange,
}: TextEditorSectionProps) => {
  const { t } = useTranslation(['workflow'])

  return (
    <div className="flex h-full flex-col">
      {isTruncated && <LargeDataAlert className="mx-3 mt-1 mb-1 shrink-0" />}
      <div className="min-h-0 grow">
        {currentVar.value_type === 'string' ? (
          <DisplayContent
            previewType={PreviewType.Markdown}
            varType={currentVar.value_type}
            mdString={typeof value === 'string' ? value : String(value ?? '')}
            readonly={textEditorDisabled || isTruncated}
            handleTextChange={onTextChange}
          />
        ) : (
          <Textarea
            aria-label={t(($) => $['errorMsg.fields.variableValue'], { ns: 'workflow' })}
            readOnly={textEditorDisabled}
            disabled={textEditorDisabled || isTruncated}
            className="h-full"
            value={typeof value === 'number' ? value : String(value ?? '')}
            onValueChange={(value) => onTextChange(value)}
          />
        )}
      </div>
    </div>
  )
}

type BoolArraySectionProps = {
  values: boolean[]
  onChange: (nextValue: boolean[]) => void
}

export const BoolArraySection = ({ values, onChange }: BoolArraySectionProps) => {
  return (
    <div className="w-73.75 space-y-1">
      {values.map((value, index) => (
        <BoolValue
          key={`${index}-${String(value)}`}
          value={value}
          onChange={(newValue) => {
            const nextValue = [...values]
            nextValue[index] = newValue
            onChange(nextValue)
          }}
        />
      ))}
    </div>
  )
}

type JsonEditorSectionProps = {
  hasChunks: boolean
  schemaType?: string
  valueType: VarInInspect['value_type']
  json: string
  readonly: boolean
  isTruncated: boolean
  onChange: (value: string) => void
}

export const JsonEditorSection = ({
  hasChunks,
  schemaType,
  valueType,
  json,
  readonly,
  isTruncated,
  onChange,
}: JsonEditorSectionProps) => {
  if (hasChunks) {
    return (
      <DisplayContent
        previewType={PreviewType.Chunks}
        varType={valueType}
        schemaType={schemaType ?? ''}
        jsonString={json ?? '{}'}
        readonly={readonly}
        handleEditorChange={onChange}
      />
    )
  }

  return (
    <SchemaEditor
      readonly={readonly || isTruncated}
      className="overflow-y-auto"
      hideTopMenu
      schema={json}
      onUpdate={onChange}
      isTruncated={isTruncated}
    />
  )
}

type FileEditorSectionProps = {
  currentVar: VarInInspect
  fileValue: FileEntity[]
  fileUploadConfig?: UploadConfig
  textEditorDisabled: boolean
  onChange: (files: FileEntity[]) => void
}

export const FileEditorSection = ({
  currentVar,
  fileValue,
  fileUploadConfig,
  textEditorDisabled,
  onChange,
}: FileEditorSectionProps) => {
  return (
    <div className="max-w-115">
      <FileUploaderInAttachmentWrapper
        value={fileValue}
        onChange={onChange}
        fileConfig={{
          allowed_file_types: [
            SupportUploadFileTypes.image,
            SupportUploadFileTypes.document,
            SupportUploadFileTypes.audio,
            SupportUploadFileTypes.video,
          ],
          allowed_file_extensions: [
            ...(FILE_EXTS[SupportUploadFileTypes.image] ?? []),
            ...(FILE_EXTS[SupportUploadFileTypes.document] ?? []),
            ...(FILE_EXTS[SupportUploadFileTypes.audio] ?? []),
            ...(FILE_EXTS[SupportUploadFileTypes.video] ?? []),
          ],
          allowed_file_upload_methods: [TransferMethod.local_file, TransferMethod.remote_url],
          number_limits:
            currentVar.value_type === 'file'
              ? 1
              : fileUploadConfig?.workflow_file_upload_limit || 5,
          fileUploadConfig,
          preview_config: {
            mode: PreviewMode.NewPage,
            file_type_list: ['application/pdf'],
          },
        }}
        isDisabled={textEditorDisabled}
      />
    </div>
  )
}

export const ErrorMessages = ({
  parseError,
  validationError,
}: {
  parseError: Error | null
  validationError: string
}) => {
  return (
    <>
      {parseError && <ErrorMessage className="mt-1" message={parseError.message} />}
      {validationError && <ErrorMessage className="mt-1" message={validationError} />}
    </>
  )
}