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
TypeScript ai-agentai-agent-toolsai-agentsai-agents-frameworkmcpmcp-servermcp-toolsmcpsn8n-alternativeno-code-automationworkflowworkflow-automationworkflows
import { existsSync } from 'node:fs'
import path from 'path'
import { isNil, STEP_NAME_REGEX } from '@activepieces/core-utils'
import { LATEST_CONTEXT_VERSION } from '@activepieces/pieces-framework'
import { CodeAction, EngineGenericError, ExecutionError, ExecutionErrorType, FlowActionType, GenericStepOutput, StepOutputStatus } from '@activepieces/shared'
import { initCodeSandbox } from '../core/code/code-sandbox'
import { continueIfFailureHandler, runWithExponentialBackoff } from '../helper/error-handling'
import { flowRunProgressReporter } from '../helper/flow-run-progress-reporter'
import { utils } from '../utils'
import { ActionHandler, BaseExecutor, failStep } from './base-executor'
export const codeExecutor: BaseExecutor<CodeAction> = {
async handle({
action,
executionState,
constants,
}) {
if (executionState.isCompleted({ stepName: action.name })) {
return executionState
}
const resultExecution = await runWithExponentialBackoff(executionState, action, constants, executeAction)
return continueIfFailureHandler(resultExecution, action, constants)
},
}
const executeAction: ActionHandler<CodeAction> = async ({ action, executionState, constants }) => {
const stepStartTime = performance.now()
const stepOutput = GenericStepOutput.create({
input: {},
type: FlowActionType.CODE,
status: StepOutputStatus.RUNNING,
})
const { data: executionStateResult, error: executionStateError } = await utils.tryCatchAndThrowOnEngineError((async () => {
const { censoredInput, resolvedInput } = await constants.getPropsResolver({ contextVersion: LATEST_CONTEXT_VERSION }).resolve<Record<string, unknown>>({
unresolvedInput: action.settings.input,
executionState,
})
stepOutput.input = censoredInput
await flowRunProgressReporter.sendUpdate({
engineConstants: constants,
flowExecutorContext: await executionState.upsertStep(action.name, stepOutput),
stepNameToUpdate: action.name,
})
if (!constants.actionRunMode && isNil(constants.runEnvironment)) {
throw new EngineGenericError('RunEnvironmentNotSetError', 'Run environment is not set')
}
if (!STEP_NAME_REGEX.test(action.name)) {
// A malformed step name is bad user input, not an engine failure: fail the step
// (USER) instead of raising an ENGINE error that would page oncall. Ingress + the
// code-cache sink guard already block this upstream; this is the runtime backstop.
throw new ExecutionError('InvalidStepName', `Invalid code step name: "${action.name}"`, ExecutionErrorType.USER)
}
const useDeno = action.settings.useDeno !== false
const stepDir = path.resolve(`${constants.baseCodeDirectory}/${constants.flowVersionId}/${action.name}`)
const artifactPath = useDeno
? resolveDenoArtifactPath(stepDir)
: path.join(stepDir, 'index.js')
const codeSandbox = await initCodeSandbox({ useDeno })
const output = await codeSandbox.runCodeModule({
codeFilePath: artifactPath,
inputs: resolvedInput,
})
const succeeded = stepOutput.setOutput(output).setStatus(StepOutputStatus.SUCCEEDED).setDuration(performance.now() - stepStartTime)
return (await executionState.upsertStep(action.name, succeeded)).incrementStepsExecuted()
}))
if (executionStateError) {
return failStep({
action,
executionState,
stepOutput,
error: executionStateError,
durationMs: performance.now() - stepStartTime,
})
}
return executionStateResult
}
function resolveDenoArtifactPath(stepDir: string): string {
const transpiledPath = path.join(stepDir, 'index.cjs')
return existsSync(transpiledPath) ? transpiledPath : path.join(stepDir, 'index.ts')
}