Skip to content

AI Agents & MCPs & AI Workflow Automation • (~400 MCP servers for AI agents) • AI Automation / AI Agent with MCPs • AI Workflows & AI Agents • MCPs for AI Agents

8.7 KB
Raw Blame
import { Readable } from 'node:stream'
import { isNil, isObject } from '@activepieces/core-utils'
import { DateRangeValue, getAuthPropertyForValue, InputPropertyMap, PieceAuthProperty, PieceProperty, PiecePropertyMap, PropertyType, StaticPropsValue } from '@activepieces/pieces-framework'
import { AppConnectionValue, AUTHENTICATION_PROPERTY_NAME, PropertySettings } from '@activepieces/shared'
import { dynamicPropKeys } from '../helper/dynamic-prop-keys'
import { processors } from './processors'
import { arrayZipperProcessor } from './processors/array-zipper'

type PropsValidationError = {
    [key: string]: string[] | PropsValidationError | PropsValidationError[]
}

export const propsProcessor = {
    applyProcessorsAndValidators: async (
        resolvedInput: StaticPropsValue<PiecePropertyMap>,
        props: InputPropertyMap,
        auth: PieceAuthProperty | PieceAuthProperty[] | undefined,
        requireAuth: boolean,
        propertySettings: Record<string, PropertySettings>,
    ): Promise<{ processedInput: StaticPropsValue<PiecePropertyMap>, errors: PropsValidationError }> => {
        let dynamaicPropertiesSchema: Record<string, InputPropertyMap> | undefined = undefined
        if (Object.keys(propertySettings).length > 0) {
            dynamaicPropertiesSchema = Object.fromEntries(Object.entries(propertySettings).map(([key, propertySetting]) => [key, propertySetting.schema]))
        }
        const processedInput = { ...resolvedInput }
        const errors: PropsValidationError = {}
        const authValue: AppConnectionValue | undefined = resolvedInput[AUTHENTICATION_PROPERTY_NAME]
        if (authValue && requireAuth) {
            const authPropsToProcess = getAuthPropsToProcess(authValue, auth)
            if (authPropsToProcess) {
                const { processedInput: authProcessedInput, errors: authErrors } = await propsProcessor.applyProcessorsAndValidators(
                    resolvedInput[AUTHENTICATION_PROPERTY_NAME],
                    authPropsToProcess,
                    undefined,
                    false,
                    {},
                )
                processedInput[AUTHENTICATION_PROPERTY_NAME] = authProcessedInput
                if (Object.keys(authErrors).length > 0) {
                    errors[AUTHENTICATION_PROPERTY_NAME] = authErrors
                }
            }
        }
        for (const [key, value] of Object.entries(resolvedInput)) {
            const property = props[key]
            if (isNil(property)) {
                continue
            }
            if (property.type === PropertyType.DYNAMIC) {
                const valueWithOriginalKeys = dynamicPropKeys.unescapeInputKeys(value)
                processedInput[key] = valueWithOriginalKeys
                if (!isNil(dynamaicPropertiesSchema?.[key])) {
                    const { processedInput: itemProcessedInput, errors: itemErrors } = await propsProcessor.applyProcessorsAndValidators(
                        valueWithOriginalKeys,
                        dynamicPropKeys.unescapePropsKeys(dynamaicPropertiesSchema[key]),
                        undefined,
                        false,
                        {},
                    )
                    processedInput[key] = itemProcessedInput
                    if (Object.keys(itemErrors).length > 0) {
                        errors[key] = itemErrors
                    }
                }
            }
            if (property.type === PropertyType.ARRAY && property.properties) {
                const arrayOfObjects = arrayZipperProcessor(property, value) ?? []
                const processedArray = []
                const processedErrors = []
                for (const item of arrayOfObjects) {
                    const { processedInput: itemProcessedInput, errors: itemErrors } = await propsProcessor.applyProcessorsAndValidators(
                        item,
                        property.properties,
                        undefined,
                        false,
                        {},
                    )
                    processedArray.push(itemProcessedInput)
                    processedErrors.push(itemErrors)
                }
                processedInput[key] = processedArray
                const isThereErrors = processedErrors.some(error => Object.keys(error).length > 0)
                if (isThereErrors) {
                    errors[key] = {
                        properties: processedErrors,
                    }
                }
            }
            if (!property.required && isNil(processedInput[key])) {
                processedInput[key] = undefined
            }
            const processor = processors[property.type]
            if (processor) {
                processedInput[key] = await processor(property, processedInput[key])
            }

            const shouldValidate = key !== AUTHENTICATION_PROPERTY_NAME && property.type !== PropertyType.MARKDOWN
            if (!shouldValidate) {
                continue
            }
        }

        for (const [key, value] of Object.entries(processedInput)) {
            const property = props[key]
            if (isNil(property)) {
                continue
            }

            const validationErrors = validateProperty(property, value, resolvedInput[key])
            if (validationErrors.length > 0) {
                errors[key] = validationErrors
            }
        }

        // Streaming file inputs open a live connection when resolved. If any property
        // fails validation the action never runs, so drain those bodies here to avoid
        // leaking connections until GC.
        if (Object.keys(errors).length > 0) {
            destroyOpenStreams(processedInput)
        }

        return { processedInput, errors }
    },
}

function destroyOpenStreams(value: unknown): void {
    if (isNil(value) || typeof value !== 'object' || Buffer.isBuffer(value)) {
        return
    }
    if (value instanceof Readable) {
        value.destroy()
        return
    }
    for (const child of Object.values(value)) {
        destroyOpenStreams(child)
    }
}

const validateProperty = (property: PieceProperty, value: unknown, originalValue: unknown): string[] => {
    if (property.type === PropertyType.JSON) {
        if (!property.required && originalValue === '') {
            return []
        }
        if (!isNil(originalValue) && isNil(value)) {
            return [`Expected JSON, received: ${originalValue}`]
        }
        if (!property.required && isNil(value)) {
            return []
        }
        if (!isObject(value) && !Array.isArray(value)) {
            return [`Expected JSON, received: ${originalValue}`]
        }
        return []
    }

    if (!property.required && isNil(value)) {
        return []
    }

    switch (property.type) {
        case PropertyType.SHORT_TEXT:
        case PropertyType.LONG_TEXT:
        case PropertyType.RICH_TEXT:
            return typeof value === 'string' ? [] : [`Expected string, received: ${originalValue}`]
        case PropertyType.NUMBER:
            return typeof value === 'number' && !Number.isNaN(value) ? [] : [`Expected number, received: ${originalValue}`]
        case PropertyType.CHECKBOX:
            return typeof value === 'boolean' ? [] : [`Expected boolean, received: ${originalValue}`]
        case PropertyType.DATE_TIME:
            return typeof value === 'string' ? [] : [`Invalid datetime format. Expected ISO format (e.g. 2024-03-14T12:00:00.000Z), received: ${originalValue}`]
        case PropertyType.DATE_RANGE:
            return DateRangeValue.safeParse(value).success ? [] : [`Expected date range, received: ${originalValue}`]
        case PropertyType.ARRAY:
        case PropertyType.MULTI_SELECT_DROPDOWN:
        case PropertyType.STATIC_MULTI_SELECT_DROPDOWN:
            return Array.isArray(value) ? [] : [`Expected array, received: ${originalValue}`]
        case PropertyType.OBJECT:
            return isObject(value) ? [] : [`Expected object, received: ${originalValue}`]
        case PropertyType.FILE:
            return isObject(value) ? [] : [`Expected file url or base64 with mimeType, received: ${originalValue}`]
        default:
            return []
    }
}

function getAuthPropsToProcess(authValue: AppConnectionValue, auth: PieceAuthProperty | PieceAuthProperty[] | undefined): | null {
    if (isNil(auth)) {
        return null
    }
    const usedAuthProperty = getAuthPropertyForValue({
        authValueType: authValue.type,
        pieceAuth: auth,
    })
    const doesAuthHaveProps = usedAuthProperty?.type === PropertyType.CUSTOM_AUTH || usedAuthProperty?.type === PropertyType.OAUTH2 || usedAuthProperty?.type === PropertyType.OIDC
    if (doesAuthHaveProps && !isNil(usedAuthProperty?.props)) {
        return usedAuthProperty.props
    }
    return null
}