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 { ActivepiecesError, AiCallContext, AIProviderName, apId, ErrorCode, isNil, tryCatch } from '@activepieces/core-utils'
import { AiStepAction, ChooseAiRouteRequest, ChooseAiRouteResponse, ExecuteAiJobData, LATEST_JOB_DATA_SCHEMA_VERSION, StartAiRouteResponse, WorkerJobType } from '@activepieces/shared'
import { FastifyBaseLogger } from 'fastify'
import { assertCreditsAndAppSumoNotExceeded } from '../platform/billing-provider'
import { aiExecution } from './ai-execution'
import { aiProviderService } from './ai-provider-service'
import { aiCallContext } from './usage/ai-call-context'
export const aiRouterService = (log: FastifyBaseLogger) => ({
async start({ platformId, projectId, waitpointId, ...request }: StartParams): Promise<StartAiRouteResponse> {
const provider = await pickProvider({ platformId, projectId, log })
await assertHasCredits({ platformId, log })
const requestId = apId()
const callContext = await aiCallContext.forFlowRun({ flowRunId: request.flowRunId, projectId })
await aiExecution(log).enqueue(routeJob({ request, requestId, platformId, projectId, provider, waitpointId, callContext }))
log.info({ requestId, flowRun: { id: request.flowRunId }, provider }, 'Started the AI router decision for a paused step')
return { requestId }
},
async choose({ platformId, projectId, ...request }: ChooseParams): Promise<ChooseAiRouteResponse> {
const provider = await pickProvider({ platformId, projectId, log })
await assertHasCredits({ platformId, log })
const execution = aiExecution(log)
const requestId = apId()
const callContext = await aiCallContext.forFlowRun({ flowRunId: request.flowRunId, projectId })
const answer = execution.waitForAnswer({ requestId, timeoutMs: WAIT_MS })
await execution.enqueue(routeJob({ request, requestId, platformId, projectId, provider, webserverId: execution.serverId(), callContext }))
const { output, failure } = await answer
if (!isNil(failure)) {
throw new ActivepiecesError({ code: ErrorCode.ENGINE_OPERATION_FAILURE, params: { message: failure } })
}
const parsed = ChooseAiRouteResponse.safeParse(output)
if (!parsed.success) {
throw new ActivepiecesError({ code: ErrorCode.ENGINE_OPERATION_FAILURE, params: { message: 'The routing model answered in a shape the AI Router does not understand' } })
}
log.info({ matched: parsed.data.matched, matchMode: request.matchMode, provider }, 'Chose the AI router routes')
return parsed.data
},
})
function routeJob({ request, requestId, platformId, projectId, provider, waitpointId, webserverId, callContext }: RouteJobParams): ExecuteAiJobData {
return {
schemaVersion: LATEST_JOB_DATA_SCHEMA_VERSION,
jobType: WorkerJobType.EXECUTE_AI,
action: AiStepAction.ROUTE,
requestId,
waitpointId,
webserverId,
projectId,
platformId,
flowId: request.flowId,
flowRunId: request.flowRunId,
provider,
modelId: AI_ROUTER_MODEL_ID,
state: request.state,
question: request.question,
options: request.options,
matchMode: request.matchMode,
callContext,
}
}
async function pickProvider({ platformId, projectId, log }: { platformId: string, projectId: string, log: FastifyBaseLogger }): Promise<AIProviderName> {
const available = await aiProviderService(log).listForProject({ platformId, projectId })
const provider = ROUTER_PROVIDERS.find((candidate) => available.some((entry) => entry.provider === candidate))
if (isNil(provider)) {
throw new ActivepiecesError({
code: ErrorCode.FEATURE_DISABLED,
params: { message: 'The AI Router needs an OpenRouter key. On Cloud that is the Activepieces provider; self-hosted, add OpenRouter under AI providers.' },
})
}
return provider
}
async function assertHasCredits({ platformId, log }: { platformId: string, log: FastifyBaseLogger }): Promise<void> {
const { error } = await tryCatch(() => assertCreditsAndAppSumoNotExceeded({ platformId, log }))
if (isNil(error)) {
return
}
const details = error instanceof ActivepiecesError ? error.error : undefined
if (isNil(details) || details.code !== ErrorCode.QUOTA_EXCEEDED) {
log.warn({ error, platform: { id: platformId } }, '[aiRouterService] Credits check failed, allowing the call')
return
}
throw new ActivepiecesError({
code: ErrorCode.QUOTA_EXCEEDED,
params: { ...details.params, message: OUT_OF_CREDITS_MESSAGE },
})
}
const ROUTER_PROVIDERS = [AIProviderName.ACTIVEPIECES, AIProviderName.OPENROUTER] as const
const AI_ROUTER_MODEL_ID = 'typesafe/jev-1.13'
const WAIT_MS = 25_000
const OUT_OF_CREDITS_MESSAGE = 'The AI Router did not run because the platform is out of AI credits. Add credits or upgrade the plan, then retry the run.'
type ChooseParams = Omit<ChooseAiRouteRequest, 'waitpointId'> & {
platformId: string
projectId: string
}
type StartParams = ChooseParams & {
waitpointId: string
}
type RouteJobParams = {
request: Omit<ChooseAiRouteRequest, 'waitpointId'>
requestId: string
platformId: string
projectId: string
provider: AIProviderName
waitpointId?: string
webserverId?: string
callContext: AiCallContext
}