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

10.1 KB
Raw Blame
import { ensureTrailingSlash, isNil, PlatformId, ProjectId } from '@activepieces/core-utils'
import { ContextVersion } from '@activepieces/pieces-framework'
import { BaseEngineOperation, BeginExecuteFlowOperation, DEFAULT_MCP_DATA, EngineGenericError, ExecutePropsOptions, ExecuteTriggerOperation, ExecutionState, ExecutionType, flowStructureUtil, FlowTrigger, FlowVersionState, Project, ResumeExecuteFlowOperation, ResumePayload, RunEnvironment, StreamStepProgress, TriggerHookType } from '@activepieces/shared'
import { retryFetch } from '../../api/retry-fetch'
import { createPropsResolver, PropsResolver } from '../../variables/props-resolver'

type RetryConstants = {
    maxAttempts: number
    retryExponential: number
    retryInterval: number
}

type EngineConstantsParams = {
    flowId: string
    flowVersionId: string
    flowVersionState: FlowVersionState
    triggerPieceName: string
    flowRunId: string
    publicApiUrl: string
    internalApiUrl: string
    retryConstants: RetryConstants
    engineToken: string
    projectId: ProjectId
    streamStepProgress: StreamStepProgress
    workerHandlerId: string | null
    httpRequestId: string | null
    resumePayload?: ResumePayload
    runEnvironment?: RunEnvironment
    stepNameToTest?: string
    entryStepName?: string
    logsFileId?: string
    timeoutInSeconds: number
    platformId: PlatformId
    stepNames: string[]
    actionRunMode?: boolean
}

const DEFAULT_RETRY_CONSTANTS: RetryConstants = {
    maxAttempts: 4,
    retryExponential: 2,
    retryInterval: 2000,
}

const DEFAULT_TRIGGER_EXECUTION = 'execute-trigger'
const DEFAULT_EXECUTE_PROPERTY = 'execute-property'

export class EngineConstants {
    public static readonly BASE_CODE_DIRECTORY = process.env.AP_BASE_CODE_DIRECTORY ?? './codes'
    public static readonly INPUT_FILE = './input.json'
    public static readonly OUTPUT_FILE = './output.json'
    public static readonly DEV_PIECES = process.env.AP_DEV_PIECES?.split(',') ?? []
    public static readonly TEST_MODE = process.env.AP_TEST_MODE === 'true'

    public readonly platformId: string
    public readonly timeoutInSeconds: number
    public readonly flowId: string
    public readonly flowVersionId: string
    public readonly flowVersionState: FlowVersionState
    public readonly triggerPieceName: string
    public readonly flowRunId: string
    public readonly publicApiUrl: string
    public readonly internalApiUrl: string
    public readonly retryConstants: RetryConstants
    public readonly engineToken: string
    public readonly projectId: ProjectId
    public readonly streamStepProgress: StreamStepProgress
    public readonly workerHandlerId: string | null
    public readonly httpRequestId: string | null
    public readonly resumePayload?: ResumePayload
    public readonly runEnvironment?: RunEnvironment
    public readonly stepNameToTest?: string
    public readonly entryStepName?: string
    public readonly logsFileId?: string
    public readonly stepNames: string[] = []
    public readonly actionRunMode: boolean
    private project: Project | null = null

    public get isRunningApTests(): boolean {
        return EngineConstants.TEST_MODE
    }

    public get isTestFlow(): boolean {
        return this.streamStepProgress === StreamStepProgress.WEBSOCKET
    }

    public get baseCodeDirectory(): string {
        return EngineConstants.BASE_CODE_DIRECTORY
    }

    public get devPieces(): string[] {
        return EngineConstants.DEV_PIECES
    }

    public constructor(params: EngineConstantsParams) {
        if (!params.publicApiUrl.endsWith('/api/')) {
            throw new EngineGenericError('PublicUrlNotEndsWithSlashError', `Public URL must end with a slash, got: ${params.publicApiUrl}`)
        }
        if (!params.internalApiUrl.endsWith('/')) {
            throw new EngineGenericError('InternalApiUrlNotEndsWithSlashError', `Internal API URL must end with a slash, got: ${params.internalApiUrl}`)
        }

        this.flowId = params.flowId
        this.flowVersionId = params.flowVersionId
        this.flowVersionState = params.flowVersionState
        this.flowRunId = params.flowRunId
        this.publicApiUrl = params.publicApiUrl
        this.internalApiUrl = params.internalApiUrl
        this.retryConstants = params.retryConstants
        this.triggerPieceName = params.triggerPieceName
        this.engineToken = params.engineToken
        this.projectId = params.projectId
        this.streamStepProgress = params.streamStepProgress
        this.workerHandlerId = params.workerHandlerId
        this.httpRequestId = params.httpRequestId
        this.resumePayload = params.resumePayload
        this.runEnvironment = params.runEnvironment
        this.stepNameToTest = params.stepNameToTest
        this.entryStepName = params.entryStepName
        this.logsFileId = params.logsFileId
        this.platformId = params.platformId
        this.timeoutInSeconds = params.timeoutInSeconds
        this.stepNames = params.stepNames
        this.actionRunMode = params.actionRunMode ?? false
    }

    public static fromExecuteFlowInput(input: ResolvedExecuteFlowOperation): EngineConstants {
        return new EngineConstants({
            ...sharedFields(input),
            ...flowFields(input.flowVersion),
            flowRunId: input.flowRunId,
            internalApiUrl: input.internalApiUrl,
            streamStepProgress: input.streamStepProgress,
            workerHandlerId: input.workerHandlerId ?? null,
            httpRequestId: input.httpRequestId ?? null,
            resumePayload: input.executionType === ExecutionType.RESUME ? input.resumePayload : undefined,
            runEnvironment: input.runEnvironment,
            stepNameToTest: input.stepNameToTest ?? undefined,
            entryStepName: input.entryStepName,
            logsFileId: input.logsFileId,
        })
    }

    public static fromExecuteActionInput(input: BaseEngineOperation & { flowVersionId?: string }): EngineConstants {
        return new EngineConstants({
            ...sharedFields(input),
            ...flowFields(undefined),
            flowVersionId: input.flowVersionId ?? DEFAULT_MCP_DATA.flowVersionId,
            flowRunId: DEFAULT_MCP_DATA.flowRunId,
            actionRunMode: true,
        })
    }

    public static fromExecutePropertyInput(input: Omit<ExecutePropsOptions, 'piece'> & { pieceName: string, pieceVersion: string }): EngineConstants {
        const flow = flowFields(input.flowVersion)
        return new EngineConstants({
            ...sharedFields(input),
            ...flow,
            triggerPieceName: flow.triggerPieceName ?? DEFAULT_MCP_DATA.triggerPieceName,
            flowRunId: DEFAULT_EXECUTE_PROPERTY,
        })
    }

    public static fromExecuteTriggerInput(input: ResolvedExecuteTriggerOperation<TriggerHookType>): EngineConstants {
        return new EngineConstants({
            ...sharedFields(input),
            ...flowFields(input.flowVersion),
            flowRunId: DEFAULT_TRIGGER_EXECUTION,
        })
    }
    public getPropsResolver({ contextVersion, pieceName }: GetPropsResolverParams): PropsResolver {
        return createPropsResolver({
            projectId: this.projectId,
            engineToken: this.engineToken,
            apiUrl: this.internalApiUrl,
            contextVersion,
            stepNames: this.stepNames,
            pieceName,
        })
    }
    private async getProject(): Promise<Project> {
        if (this.project) {
            return this.project
        }

        const getWorkerProjectEndpoint = `${this.internalApiUrl}v1/worker/project`

        const response = await retryFetch(getWorkerProjectEndpoint, {
            headers: {
                Authorization: `Bearer ${this.engineToken}`,
            },
        })

        this.project = await response.json() as Project
        return this.project
    }

    public externalProjectId = async (): Promise<string | undefined> => {
        const project = await this.getProject()
        return project.externalId ?? undefined
    }
}

function sharedFields(input: SharedFieldsSource) {
    return {
        publicApiUrl: input.publicApiUrl,
        internalApiUrl: ensureTrailingSlash(input.internalApiUrl),
        engineToken: input.engineToken,
        projectId: input.projectId,
        timeoutInSeconds: input.timeoutInSeconds,
        platformId: input.platformId,
        retryConstants: DEFAULT_RETRY_CONSTANTS,
        streamStepProgress: StreamStepProgress.NONE,
        workerHandlerId: null,
        httpRequestId: null,
    }
}

function flowFields(flowVersion: FlowFieldsSource | undefined) {
    if (isNil(flowVersion)) {
        return {
            flowId: DEFAULT_MCP_DATA.flowId,
            flowVersionId: DEFAULT_MCP_DATA.flowVersionId,
            flowVersionState: DEFAULT_MCP_DATA.flowVersionState,
            triggerPieceName: DEFAULT_MCP_DATA.triggerPieceName,
            stepNames: [],
        }
    }
    return {
        flowId: flowVersion.flowId,
        flowVersionId: flowVersion.id,
        flowVersionState: flowVersion.state,
        triggerPieceName: flowVersion.trigger?.settings.pieceName,
        stepNames: isNil(flowVersion.trigger) ? [] : flowStructureUtil.getAllSteps(flowVersion.trigger).map((step) => step.name),
    }
}

type GetPropsResolverParams = {
    contextVersion: ContextVersion | undefined
    pieceName?: string
}

type SharedFieldsSource = {
    publicApiUrl: string
    internalApiUrl: string
    engineToken: string
    projectId: ProjectId
    timeoutInSeconds: number
    platformId: PlatformId
}

type FlowFieldsSource = {
    flowId: string
    id: string
    state: FlowVersionState
    trigger?: FlowTrigger
}

export type ResolvedBeginExecuteFlowOperation = Omit<BeginExecuteFlowOperation, 'triggerPayload'> & {
    triggerPayload: unknown
}

export type ResolvedExecuteTriggerOperation<HT extends TriggerHookType> = Omit<ExecuteTriggerOperation<HT>, 'triggerPayload'> & {
    triggerPayload?: unknown
}

export type ResolvedResumeExecuteFlowOperation = Omit<ResumeExecuteFlowOperation, 'resumePayload'> & {
    resumePayload: ResumePayload
    executionState: ExecutionState
}

export type ResolvedExecuteFlowOperation = ResolvedBeginExecuteFlowOperation | ResolvedResumeExecuteFlowOperation