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.

3.3 KB
Raw Blame
import type { InputVarType, Node, NodeOutPutVar } from '@/app/components/workflow/types'
import type { InputVar } from '@/models/pipeline'
import { NODE_WIDTH } from '@/app/components/workflow/constants'
import { BlockEnum } from '@/app/components/workflow/types'
import { PipelineInputVarType } from '@/models/pipeline'
import { inputVarTypeToVarType } from '../../data-source/utils'

const SNIPPET_INPUT_FIELD_NODE_ID = 'start'

export const filterSnippetSystemVars = (availableVars: NodeOutPutVar[], isSnippetFlow: boolean) => {
  if (!isSnippetFlow) return availableVars

  return availableVars
    .map((nodeVar) => ({
      ...nodeVar,
      vars: nodeVar.vars.filter((variable) => !variable.variable.startsWith('sys.')),
    }))
    .filter((nodeVar) => nodeVar.vars.length > 0)
}

const toWorkflowInputType = (type: InputVar['type']) => type as InputVarType

const buildSnippetInputFieldNode = (fields: InputVar[], title: string): Node | undefined => {
  const variables = fields.filter((field) => !!field.variable)

  if (!variables.length) return undefined

  return {
    id: SNIPPET_INPUT_FIELD_NODE_ID,
    type: 'custom',
    position: { x: 0, y: 0 },
    width: NODE_WIDTH,
    height: 80,
    data: {
      title,
      desc: '',
      type: BlockEnum.Start,
      variables: variables.map((field) => ({
        type: toWorkflowInputType(field.type),
        label: field.label,
        variable: field.variable,
        max_length: field.max_length,
        default: field.default_value,
        required: field.required,
        options: field.options,
        placeholder: field.placeholder,
        unit: field.unit,
        allowed_file_upload_methods: field.allowed_file_upload_methods,
        allowed_file_types: field.allowed_file_types,
        allowed_file_extensions: field.allowed_file_extensions,
      })),
    },
  } as Node
}

const buildSnippetInputFieldVars = (
  fields: InputVar[],
  title: string,
): NodeOutPutVar | undefined => {
  const vars = fields
    .filter((field) => !!field.variable)
    .map((field) => ({
      variable: field.variable,
      type: inputVarTypeToVarType(field.type),
      isParagraph: field.type === PipelineInputVarType.paragraph,
      isSelect: field.type === PipelineInputVarType.select,
      options: field.options,
      required: field.required,
      des: field.label,
    }))

  if (!vars.length) return undefined

  return {
    nodeId: SNIPPET_INPUT_FIELD_NODE_ID,
    title,
    vars,
    isStartNode: true,
  }
}

export const appendSnippetInputFieldVars = ({
  availableNodes,
  isSnippetFlow,
  fields,
  title,
}: {
  availableNodes: Node[]
  isSnippetFlow: boolean
  fields?: InputVar[]
  title: string
}) => {
  const inputFields = fields ?? []
  const shouldAppendSnippetInputFields =
    isSnippetFlow &&
    inputFields.length > 0 &&
    !availableNodes.some((node) => node.data.type === BlockEnum.Start)
  const snippetInputFieldNode = shouldAppendSnippetInputFields
    ? buildSnippetInputFieldNode(inputFields, title)
    : undefined
  const snippetInputFieldVars = shouldAppendSnippetInputFields
    ? buildSnippetInputFieldVars(inputFields, title)
    : undefined

  return {
    availableNodes: snippetInputFieldNode
      ? [snippetInputFieldNode, ...availableNodes]
      : availableNodes,
    availableVars: snippetInputFieldVars ? [snippetInputFieldVars] : [],
  }
}