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.

4.7 KB
Raw Blame
import type { TextGenerationTranslate } from '../types'
import type { FileEntity } from '@/app/components/base/file-uploader/types'
import type { PromptConfig } from '@/models/debug'
import type { VisionFile, VisionSettings } from '@/types/app'
import { getProcessedFiles } from '@/app/components/base/file-uploader/utils'
import { TransferMethod } from '@/types/app'
import { formatBooleanInputs } from '@/utils/model-config'

export type ResultInputValue =
  | string
  | boolean
  | number
  | string[]
  | Record<string, unknown>
  | FileEntity
  | FileEntity[]
  | undefined

type ValidationResult = {
  canSend: boolean
  notification?: {
    type: 'error' | 'info'
    message: string
  }
}

type ValidateResultRequestParams = {
  completionFiles: VisionFile[]
  inputs: Record<string, ResultInputValue>
  isCallBatchAPI: boolean
  promptConfig: PromptConfig | null
  t: TextGenerationTranslate
}

type BuildResultRequestDataParams = {
  completionFiles: VisionFile[]
  inputs: Record<string, ResultInputValue>
  promptConfig: PromptConfig | null
  visionConfig: VisionSettings
}

const isMissingRequiredInput = (
  variable: PromptConfig['prompt_variables'][number],
  value: ResultInputValue,
) => {
  if (value === undefined || value === null) return true

  if (variable.type === 'file-list') return !Array.isArray(value) || value.length === 0

  if (['string', 'paragraph', 'number', 'json_object', 'select'].includes(variable.type))
    return typeof value !== 'string' ? false : value.trim() === ''

  return false
}

const hasPendingLocalFiles = (completionFiles: VisionFile[]) => {
  return completionFiles.some(
    (item) => item.transfer_method === TransferMethod.local_file && !item.upload_file_id,
  )
}

export const validateResultRequest = ({
  completionFiles,
  inputs,
  isCallBatchAPI,
  promptConfig,
  t,
}: ValidateResultRequestParams): ValidationResult => {
  if (isCallBatchAPI) return { canSend: true }

  const promptVariables = promptConfig?.prompt_variables
  if (!promptVariables?.length) {
    if (hasPendingLocalFiles(completionFiles)) {
      return {
        canSend: false,
        notification: {
          type: 'info',
          message: t(($) => $['errorMessage.waitForFileUpload'], { ns: 'appDebug' }),
        },
      }
    }

    return { canSend: true }
  }

  const requiredVariables = promptVariables.filter(({ key, name, required, type }) => {
    if (type === 'boolean' || type === 'checkbox') return false

    return (
      !key ||
      !key.trim() ||
      !name ||
      !name.trim() ||
      required === undefined ||
      required === null ||
      required
    )
  })

  const missingRequiredVariable = requiredVariables.find((variable) =>
    isMissingRequiredInput(variable, inputs[variable.key]),
  )?.name
  if (missingRequiredVariable) {
    return {
      canSend: false,
      notification: {
        type: 'error',
        message: t(($) => $['errorMessage.valueOfVarRequired'], {
          ns: 'appDebug',
          key: missingRequiredVariable,
        }),
      },
    }
  }

  if (hasPendingLocalFiles(completionFiles)) {
    return {
      canSend: false,
      notification: {
        type: 'info',
        message: t(($) => $['errorMessage.waitForFileUpload'], { ns: 'appDebug' }),
      },
    }
  }

  return { canSend: true }
}

export const buildResultRequestData = ({
  completionFiles,
  inputs,
  promptConfig,
  visionConfig,
}: BuildResultRequestDataParams) => {
  const processedInputs = {
    ...formatBooleanInputs(
      promptConfig?.prompt_variables,
      inputs as Record<string, string | number | boolean | object>,
    ),
  }

  promptConfig?.prompt_variables.forEach((variable) => {
    const value = processedInputs[variable.key]
    if (variable.type === 'json_object' && typeof value === 'string') {
      try {
        const parsed: unknown = JSON.parse(value)
        if (parsed !== null && typeof parsed === 'object' && !Array.isArray(parsed))
          processedInputs[variable.key] = parsed
      } catch {
        // Preserve invalid input for backend validation.
      }
      return
    }

    if (variable.type === 'file' && value && typeof value === 'object' && !Array.isArray(value)) {
      processedInputs[variable.key] = getProcessedFiles([value as FileEntity])[0]!
      return
    }

    if (variable.type === 'file-list' && Array.isArray(value) && value.length > 0)
      processedInputs[variable.key] = getProcessedFiles(value)
  })

  return {
    inputs: processedInputs,
    ...(visionConfig.enabled && completionFiles.length > 0
      ? {
          files: completionFiles.map((item) => {
            if (item.transfer_method === TransferMethod.local_file) return { ...item, url: '' }

            return item
          }),
        }
      : {}),
  }
}