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

4.2 KB
Raw Blame
import { isNil } from '@activepieces/core-utils'
import { ContinueOnFailureBranches, FlowAction, FlowActionType, FlowState, FlowTrigger, FlowTriggerType, FlowVersion } from '@activepieces/shared'

function cleanFlowState(flowState: FlowState): FlowState {
    return {
        id: flowState.id,
        created: flowState.created,
        updated: flowState.updated,
        projectId: flowState.projectId,
        externalId: flowState.externalId,
        ownerId: flowState.ownerId,
        folderId: flowState.folderId,
        status: flowState.status,
        publishedVersionId: flowState.publishedVersionId,
        metadata: flowState.metadata,
        operationStatus: flowState.operationStatus,
        timeSavedPerRun: flowState.timeSavedPerRun,
        templateId: flowState.templateId,
        version: cleanFlowVersion(flowState.version),
        triggerSource: flowState.triggerSource,
    }
}

function cleanFlowVersion(version: FlowVersion): FlowVersion {
    return {
        id: version.id,
        created: version.created,
        updated: version.updated,
        flowId: version.flowId,
        displayName: version.displayName,
        trigger: cleanTrigger(version.trigger),
        updatedBy: version.updatedBy,
        valid: version.valid,
        schemaVersion: version.schemaVersion,
        agentIds: version.agentIds,
        state: version.state,
        connectionIds: version.connectionIds,
        backupFiles: version.backupFiles,
        notes: version.notes,
    }
}

function cleanTrigger(trigger: FlowTrigger): FlowTrigger {
    const nextAction = isNil(trigger.nextAction) ? undefined : cleanAction(trigger.nextAction)
    const commonProps = {
        name: trigger.name,
        valid: trigger.valid,
        displayName: trigger.displayName,
        lastUpdatedDate: trigger.lastUpdatedDate,
    }

    switch (trigger.type) {
        case FlowTriggerType.PIECE:
            return { ...commonProps, type: trigger.type, settings: trigger.settings, nextAction }
        case FlowTriggerType.EMPTY:
            return { ...commonProps, type: trigger.type, settings: trigger.settings, nextAction }
    }
}

function cleanAction(action: FlowAction): FlowAction {
    const nextAction = isNil(action.nextAction) ? undefined : cleanAction(action.nextAction)
    const commonProps = {
        name: action.name,
        valid: action.valid,
        displayName: action.displayName,
        skip: action.skip,
        lastUpdatedDate: action.lastUpdatedDate,
    }

    switch (action.type) {
        case FlowActionType.CODE:
            return { ...commonProps, type: action.type, settings: action.settings, nextAction, continueOnFailureBranches: cleanBranches(action.continueOnFailureBranches) }
        case FlowActionType.PIECE:
            return { ...commonProps, type: action.type, settings: action.settings, nextAction, continueOnFailureBranches: cleanBranches(action.continueOnFailureBranches) }
        case FlowActionType.LOOP_ON_ITEMS:
            return {
                ...commonProps, type: action.type, settings: action.settings, nextAction,
                firstLoopAction: isNil(action.firstLoopAction) ? undefined : cleanAction(action.firstLoopAction),
            }
        case FlowActionType.ROUTER:
            return {
                ...commonProps, type: action.type, settings: action.settings, nextAction,
                children: action.children.map((child) => isNil(child) ? null : cleanAction(child)),
            }
        case FlowActionType.AI_ROUTER:
            return {
                ...commonProps, type: action.type, settings: action.settings, nextAction,
                children: action.children.map((child) => isNil(child) ? null : cleanAction(child)),
            }
    }
}

function cleanBranches(branches: ContinueOnFailureBranches | undefined): ContinueOnFailureBranches | undefined {
    if (isNil(branches) || (isNil(branches.onSuccess) && isNil(branches.onFailure))) {
        return undefined
    }
    return {
        onSuccess: isNil(branches.onSuccess) ? undefined : cleanAction(branches.onSuccess),
        onFailure: isNil(branches.onFailure) ? undefined : cleanAction(branches.onFailure),
    }
}

export const cleanFlowStateUtil = {
    cleanFlowState,
}