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.9 KB
Raw Blame
import type { TFunction } from 'i18next'
import type { HumanInputV2NodeType } from './types'
import type { NodeDefault } from '@/app/components/workflow/types'
import { BlockClassification } from '@/app/components/workflow/block-selector/types'
import { BlockEnum } from '@/app/components/workflow/types'
import { genNodeMetaData } from '@/app/components/workflow/utils'
import { ENABLE_HITL_IM } from '@/config'
import {
  buildHumanInputOutputVars,
  getHumanInputSharedValidationError,
} from '../human-input/shared/default-utils'
import { getRequestURLCount } from './message-template-utils'
import { getRecipientValidationError, hasDuplicateRecipients } from './recipient-utils'
import { isHumanInputV2DebugChannel } from './types'

const metaData = genNodeMetaData({
  classification: BlockClassification.Logic,
  sort: 2,
  type: BlockEnum.HumanInputV2,
  helpLinkUri: 'human-input',
})

const nodeDefault: NodeDefault<HumanInputV2NodeType> = {
  metaData,
  defaultValue: {
    type: BlockEnum.HumanInput,
    version: '2',
    recipients_spec: [],
    message_template: { subject: '', body: '' },
    debug_mode: { enabled: false, channels: [] },
    form_content: '',
    inputs: [],
    user_actions: [],
    timeout: 36,
    timeout_unit: 'hour',
  },
  checkValid(
    payload,
    t: TFunction<['workflow']>,
    moreDataForCheckValid?: {
      resolvedContactIds?: ReadonlySet<string>
      contactLookupFailed?: boolean
    },
  ) {
    const { resolvedContactIds, contactLookupFailed } = moreDataForCheckValid ?? {}
    let errorMessage = ''
    if (!ENABLE_HITL_IM)
      errorMessage = t(($) => $['nodes.humanInputV2.disabled'], { ns: 'workflow' })
    else if (payload.version !== '2')
      errorMessage = t(($) => $['nodes.humanInputV2.error.version'], { ns: 'workflow' })
    else if (!payload.recipients_spec.length)
      errorMessage = t(($) => $['nodes.humanInputV2.error.recipientRequired'], { ns: 'workflow' })
    else if (payload.recipients_spec.some(getRecipientValidationError))
      errorMessage = t(($) => $['nodes.humanInputV2.error.recipientInvalid'], { ns: 'workflow' })
    else if (
      contactLookupFailed &&
      payload.recipients_spec.some((recipient) => recipient.type === 'contact')
    )
      errorMessage = t(($) => $['nodes.humanInputV2.error.recipientVerificationFailed'], {
        ns: 'workflow',
      })
    else if (
      resolvedContactIds &&
      payload.recipients_spec.some(
        (recipient) =>
          recipient.type === 'contact' && !resolvedContactIds.has(recipient.contact_id),
      )
    )
      errorMessage = t(($) => $['nodes.humanInputV2.error.recipientInvalid'], { ns: 'workflow' })
    else if (hasDuplicateRecipients(payload.recipients_spec))
      errorMessage = t(($) => $['nodes.humanInputV2.error.recipientDuplicate'], { ns: 'workflow' })
    else if (!payload.message_template.subject.trim())
      errorMessage = t(($) => $['nodes.humanInputV2.error.subjectRequired'], { ns: 'workflow' })
    else if (!payload.message_template.body.trim())
      errorMessage = t(($) => $['nodes.humanInputV2.error.bodyRequired'], { ns: 'workflow' })
    else if (getRequestURLCount(payload.message_template.body) === 0)
      errorMessage = t(($) => $['nodes.humanInputV2.error.requestURLRequired'], { ns: 'workflow' })
    else if (getRequestURLCount(payload.message_template.body) > 1)
      errorMessage = t(($) => $['nodes.humanInputV2.error.requestURLDuplicate'], { ns: 'workflow' })
    else if (
      payload.debug_mode.enabled &&
      !payload.debug_mode.channels.some(isHumanInputV2DebugChannel)
    )
      errorMessage = t(($) => $['nodes.humanInputV2.error.debugChannelRequired'], {
        ns: 'workflow',
      })
    else errorMessage = getHumanInputSharedValidationError(payload, t)

    return { isValid: !errorMessage, errorMessage }
  },
  getOutputVars(payload) {
    return buildHumanInputOutputVars(payload.inputs)
  },
}

export default nodeDefault