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

7.2 KB
Raw Blame
import { ActionErrorHandlingOptions, AiRouterAction, AiRouterMatchMode, BeginExecuteFlowOperation, BranchCondition, BranchExecutionType, CodeAction, ExecutionType, FlowAction, FlowActionType, FlowVersionState, LoopOnItemsAction, PieceAction, PropertyExecutionType, RouterExecutionType, RunEnvironment, StreamStepProgress } from '@activepieces/shared'
import { EngineConstants, ResolvedBeginExecuteFlowOperation } from '../../src/lib/handler/context/engine-constants'

export const generateMockEngineConstants = (params?: Partial<EngineConstants>): EngineConstants => {
    return new EngineConstants(
        {
            platformId: params?.platformId ?? 'platformId',
            timeoutInSeconds: params?.timeoutInSeconds ?? 10,
            flowId: params?.flowId ?? 'flowId',
            flowVersionId: params?.flowVersionId ?? 'flowVersionId',
            flowVersionState: params?.flowVersionState ?? FlowVersionState.DRAFT,
            flowRunId: params?.flowRunId ?? 'flowRunId',
            publicApiUrl: params?.publicApiUrl ?? 'http://127.0.0.1:4200/api/',
            internalApiUrl: params?.internalApiUrl ?? 'http://127.0.0.1:3000/',
            retryConstants: params?.retryConstants ?? {
                maxAttempts: 2,
                retryExponential: 1,
                retryInterval: 1,
            },
            engineToken: params?.engineToken ?? 'engineToken',
            projectId: params?.projectId ?? 'projectId',
            triggerPieceName: params?.triggerPieceName ?? 'mcp-trigger-piece-name',
            streamStepProgress: params?.streamStepProgress ?? StreamStepProgress.NONE,
            workerHandlerId: params?.workerHandlerId ?? null,
            httpRequestId: params?.httpRequestId ?? null,
            resumePayload: params?.resumePayload,
            actionRunMode: params?.actionRunMode,
            runEnvironment: params?.runEnvironment ?? RunEnvironment.TESTING,
            stepNameToTest: params?.stepNameToTest ?? undefined,
            stepNames: params?.stepNames ?? [],
            logsFileId: params?.logsFileId,
        })
}

export function buildSimpleLoopAction({
    name,
    loopItems,
    firstLoopAction,
    skip,
}: {
    name: string
    loopItems: string
    firstLoopAction?: FlowAction
    skip?: boolean
}): LoopOnItemsAction {
    return {
        name,
        displayName: 'Loop',
        type: FlowActionType.LOOP_ON_ITEMS,
        skip: skip ?? false,
        settings: {
            items: loopItems,
        },
        firstLoopAction,
        valid: true,
    }
}

export function buildRouterWithOneCondition({ children, conditions, executionType, skip }: { children: FlowAction[], conditions: (BranchCondition | null)[], executionType: RouterExecutionType, skip?: boolean }): FlowAction {
    return {
        name: 'router',
        displayName: 'Your Router Name',
        type: FlowActionType.ROUTER,
        skip: skip ?? false,
        settings: {
            branches: conditions.map((condition) => {
                if (condition === null) {
                    return {
                        branchType: BranchExecutionType.FALLBACK,
                        branchName: 'Fallback Branch',
                    }
                }
                return {
                    conditions: [[condition]],
                    branchType: BranchExecutionType.CONDITION,
                    branchName: 'Test Branch',
                }
            }),
            executionType,
        },
        children,
        valid: true,
    }
}

export function buildAiRouter({ children, routes, fallback, minConfidence, matchMode, text }: { children: (FlowAction | null)[], routes: { branchName: string, description?: string }[], fallback?: { branchName: string, description?: string }, minConfidence?: number, matchMode?: AiRouterMatchMode, text?: string }): AiRouterAction {
    const fallbackBranches = fallback === undefined ? [] : [{
        branchType: BranchExecutionType.FALLBACK as const,
        branchName: fallback.branchName,
        description: fallback.description,
    }]
    return {
        name: 'ai_router',
        displayName: 'Your AI Router Name',
        type: FlowActionType.AI_ROUTER,
        skip: false,
        settings: {
            text: text ?? 'My card was charged twice',
            question: 'Which team should handle this?',
            matchMode: matchMode ?? AiRouterMatchMode.BEST_MATCH,
            branches: [
                ...routes.map((route) => ({
                    branchType: BranchExecutionType.CONDITION as const,
                    branchName: route.branchName,
                    description: route.description,
                })),
                ...fallbackBranches,
            ],
            minConfidence,
        },
        children,
        valid: true,
    }
}

export function buildCodeAction({ name, input, skip, nextAction, errorHandlingOptions, useDeno }: { name: 'echo_step' | 'runtime' | 'echo_step_1' | 'system_error' | 'process_exit' | 'unhandled_rejection' | 'hello_world_npm' | 'stdout_on_failure' | 'setTimeout_error' | 'echo_step_legacy', input: Record<string, unknown>, skip?: boolean, errorHandlingOptions?: ActionErrorHandlingOptions, nextAction?: FlowAction, useDeno?: boolean }): CodeAction {
    return {
        name,
        displayName: 'Your Action Name',
        type: FlowActionType.CODE,
        skip: skip ?? false,
        settings: {
            input,
            sourceCode: {
                packageJson: '',
                code: '',
            },
            errorHandlingOptions,
            ...(useDeno === undefined ? {} : { useDeno }),
        },
        nextAction,
        valid: true,
    }
}

export function buildPieceAction({ name, input, skip, pieceName, actionName, nextAction, errorHandlingOptions }: { errorHandlingOptions?: ActionErrorHandlingOptions, name: string, input: Record<string, unknown>, skip?: boolean, pieceName: string, actionName: string, nextAction?: FlowAction }): PieceAction {
    return {
        name,
        displayName: 'Your Action Name',
        type: FlowActionType.PIECE,
        skip: skip ?? false,
        settings: {
            input,
            pieceName,
            pieceVersion: '1.0.0', // Not required since it's running in development mode
            actionName,
            propertySettings: Object.fromEntries(Object.entries(input).map(([key]) => [key, {
                type: PropertyExecutionType.MANUAL,
                schema: undefined,
            }])),
            errorHandlingOptions,
        },
        nextAction,
        valid: true,
    }
}

export function buildMockBeginExecuteFlowOperation(
    params: Partial<ResolvedBeginExecuteFlowOperation> & Pick<BeginExecuteFlowOperation, 'flowVersion'>,
): ResolvedBeginExecuteFlowOperation {
    return {
        projectId: 'projectId',
        engineToken: 'engineToken',
        internalApiUrl: 'http://127.0.0.1:3000/',
        publicApiUrl: 'http://127.0.0.1:4200/api/',
        timeoutInSeconds: 10,
        platformId: 'platformId',
        flowRunId: 'flowRunId',
        executionType: ExecutionType.BEGIN,
        runEnvironment: RunEnvironment.TESTING,
        workerHandlerId: null,
        httpRequestId: null,
        streamStepProgress: StreamStepProgress.NONE,
        stepNameToTest: null,
        triggerPayload: {},
        executeTrigger: false,
        ...params,
    }
}