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 { isNil } from '@activepieces/core-utils'
import { apVersionUtil } from '@activepieces/server-utils'
import { ExecutionMode, NetworkMode, WorkerGroupScope, WorkerMachineHealthcheckRequest, WorkerMachineStatus, WorkerMachineType, WorkerMachineWithStatus, WorkerSettingsResponse } from '@activepieces/shared'
import { FastifyBaseLogger } from 'fastify'
import { domainHelper } from '../../helper/domain-helper'
import { system } from '../../helper/system/system'
import { AppSystemProp } from '../../helper/system/system-props'
import { platformWorkerGroupService } from '../worker-groups/platform-worker-group.service'
import { WorkerGroupAssignment } from '../worker-groups/worker-group-queues'
import { workerMachineCache } from './machine-cache'
import { parseWorkerConcurrency, workerCapacity } from './worker-capacity'
import { workerLiveness } from './worker-liveness'
const settingsCache = new Map<string, WorkerSettingsResponse>()
async function buildSettingsResponse(_log: FastifyBaseLogger): Promise<WorkerSettingsResponse> {
const cacheKey = '__shared__'
const cached = settingsCache.get(cacheKey)
if (cached) {
return cached
}
const executionMode = system.getOrThrow<ExecutionMode>(AppSystemProp.EXECUTION_MODE)
const settings = {
TRIGGER_TIMEOUT_SECONDS: system.getNumberOrThrow(AppSystemProp.TRIGGER_TIMEOUT_SECONDS),
PAUSED_FLOW_TIMEOUT_DAYS: system.getNumberOrThrow(AppSystemProp.PAUSED_FLOW_TIMEOUT_DAYS),
EXECUTION_MODE: executionMode,
TRIGGER_HOOKS_TIMEOUT_SECONDS: system.getNumberOrThrow(AppSystemProp.TRIGGER_HOOKS_TIMEOUT_SECONDS),
FLOW_TIMEOUT_SECONDS: system.getNumberOrThrow(AppSystemProp.FLOW_TIMEOUT_SECONDS),
LOG_LEVEL: system.getOrThrow(AppSystemProp.LOG_LEVEL),
LOG_PRETTY: system.getOrThrow(AppSystemProp.LOG_PRETTY),
ENVIRONMENT: system.getOrThrow(AppSystemProp.ENVIRONMENT),
APP_WEBHOOK_SECRETS: system.getOrThrow(AppSystemProp.APP_WEBHOOK_SECRETS),
MAX_FLOW_RUN_LOG_SIZE_MB: system.getNumberOrThrow(AppSystemProp.MAX_FLOW_RUN_LOG_SIZE_MB),
FLOW_RUN_LOG_INPUT_TRUNCATE_THRESHOLD_KB: system.getNumberOrThrow(AppSystemProp.FLOW_RUN_LOG_INPUT_TRUNCATE_THRESHOLD_KB),
FLOW_RUN_LOG_SLICE_THRESHOLD_KB: system.getNumberOrThrow(AppSystemProp.FLOW_RUN_LOG_SLICE_THRESHOLD_KB),
MAX_FILE_SIZE_MB: system.getNumberOrThrow(AppSystemProp.MAX_FILE_SIZE_MB),
SANDBOX_MEMORY_LIMIT: system.getOrThrow(AppSystemProp.SANDBOX_MEMORY_LIMIT),
SANDBOX_PROPAGATED_ENV_VARS: system.get(AppSystemProp.SANDBOX_PROPAGATED_ENV_VARS)?.split(',').map(f => f.trim()) ?? [],
DEV_PIECES: system.get(AppSystemProp.DEV_PIECES)?.split(',') ?? [],
SENTRY_DSN: system.get(AppSystemProp.SENTRY_DSN),
LOKI_PASSWORD: system.get(AppSystemProp.LOKI_PASSWORD),
LOKI_URL: system.get(AppSystemProp.LOKI_URL),
LOKI_USERNAME: system.get(AppSystemProp.LOKI_USERNAME),
BETTERSTACK_HOST: system.get(AppSystemProp.BETTERSTACK_HOST),
BETTERSTACK_TOKEN: system.get(AppSystemProp.BETTERSTACK_TOKEN),
PUBLIC_URL: await domainHelper.getPublicUrl({
path: '',
}),
FILE_STORAGE_LOCATION: system.getOrThrow(AppSystemProp.FILE_STORAGE_LOCATION),
S3_USE_SIGNED_URLS: system.getOrThrow(AppSystemProp.S3_USE_SIGNED_URLS),
EVENT_DESTINATION_TIMEOUT_SECONDS: system.getNumberOrThrow(AppSystemProp.EVENT_DESTINATION_TIMEOUT_SECONDS),
EDITION: system.getOrThrow(AppSystemProp.EDITION),
SSRF_ALLOW_LIST: system.get(AppSystemProp.SSRF_ALLOW_LIST)?.split(',').map(f => f.trim()) ?? [],
ENFORCE_CONNECTION_PIECE_BINDING: system.getBoolean(AppSystemProp.ENFORCE_CONNECTION_PIECE_BINDING) ?? false,
NETWORK_MODE: system.getOrThrow<NetworkMode>(AppSystemProp.NETWORK_MODE),
PAGE_ONCALL_WEBHOOK: system.get(AppSystemProp.PAGE_ONCALL_WEBHOOK),
APP_VERSION: apVersionUtil.getCurrentRelease(),
}
settingsCache.set(cacheKey, settings)
return settings
}
export const machineService = (log: FastifyBaseLogger) => {
return {
async onDisconnect(request: OnDisconnectParams): Promise<void> {
log.info({
message: 'Worker disconnected',
worker: { id: request.workerId },
})
await workerMachineCache().delete([request.workerId])
await workerCapacity.invalidate()
},
async onConnection(request: WorkerMachineHealthcheckRequest, assignment: WorkerGroupAssignment | null = null): Promise<WorkerSettingsResponse> {
const existingWorker = await workerMachineCache().findOne(request.workerId)
const type = isNil(assignment) ? 'SHARED' : 'DEDICATED'
await workerMachineCache().upsert({
id: request.workerId,
information: request,
type,
workerGroupId: assignment?.id,
workerGroupScope: assignment?.scope,
}, existingWorker)
// Only a newly-seen worker changes capacity; heartbeats from known workers don't.
if (isNil(existingWorker)) {
await workerCapacity.invalidate()
}
return buildSettingsResponse(log)
},
async list(platformId: string): Promise<WorkerMachineWithStatus[]> {
const allWorkers = await workerMachineCache().find()
const { online: onlineWorkers, offline: offlineWorkers } = workerLiveness.partitionByLiveness(allWorkers)
await workerMachineCache().delete(offlineWorkers.map(worker => worker.id))
const platformWorkerGroupId = await platformWorkerGroupService(log).getWorkerGroupId({ platformId })
return onlineWorkers
.filter(worker => {
if (worker.workerGroupScope === WorkerGroupScope.PLATFORM) {
return !isNil(platformWorkerGroupId) && worker.workerGroupId === platformWorkerGroupId
}
if (worker.workerGroupScope === WorkerGroupScope.PROJECT || worker.workerGroupScope === WorkerGroupScope.FLOW) {
return true
}
return isNil(platformWorkerGroupId)
})
.map(worker => ({
...worker,
status: WorkerMachineStatus.ONLINE,
type: worker.type === 'DEDICATED' ? WorkerMachineType.DEDICATED : WorkerMachineType.SHARED,
workerGroupId: worker.workerGroupId,
workerGroupScope: worker.workerGroupScope,
}))
},
async listWorkerGroups(): Promise<WorkerPoolCapacity> {
const { online: onlineWorkers } = workerLiveness.partitionByLiveness(await workerMachineCache().find())
const groupsByLabel = new Map<string, WorkerGroupInfo>()
let sharedSlots = 0
for (const worker of onlineWorkers) {
const slots = parseWorkerConcurrency(worker.information.workerProps.WORKER_CONCURRENCY)
const scope = resolveDedicatedScope(worker)
if (isNil(scope)) {
sharedSlots += slots
continue
}
if (![WorkerGroupScope.PROJECT, WorkerGroupScope.FLOW].includes(scope)) {
continue
}
const groupKey = `${scope}:${worker.workerGroupId!}`
const existing = groupsByLabel.get(groupKey)
groupsByLabel.set(groupKey, {
label: worker.workerGroupId!,
slots: (existing?.slots ?? 0) + slots,
scope,
})
}
return {
groups: [...groupsByLabel.values()],
sharedSlots,
}
},
async listOnlineWorkersByGroup(): Promise<OnlineWorkerSummary[]> {
const { online: onlineWorkers } = workerLiveness.partitionByLiveness(await workerMachineCache().find())
return onlineWorkers.map((worker) => ({
id: worker.id,
slots: parseWorkerConcurrency(worker.information.workerProps.WORKER_CONCURRENCY),
workerGroupId: isNil(worker.workerGroupId) || worker.workerGroupId.length === 0 ? null : worker.workerGroupId,
scope: resolveDedicatedScope(worker),
}))
},
async getWorkerGroupScopes({ workerGroupId }: { workerGroupId: string }): Promise<WorkerGroupScope[]> {
const { online: onlineWorkers } = workerLiveness.partitionByLiveness(await workerMachineCache().find())
const scopes = onlineWorkers
.filter(worker => worker.workerGroupId === workerGroupId)
.map(resolveDedicatedScope)
.filter((scope): scope is WorkerGroupScope => !isNil(scope))
return [...new Set(scopes)]
},
}
}
function resolveDedicatedScope(worker: { workerGroupId?: string, workerGroupScope?: WorkerGroupScope }): WorkerGroupScope | null {
if (isNil(worker.workerGroupId) || worker.workerGroupId.length === 0) {
return null
}
return worker.workerGroupScope ?? WorkerGroupScope.PROJECT
}
type OnlineWorkerSummary = {
id: string
slots: number
workerGroupId: string | null
scope: WorkerGroupScope | null
}
type WorkerGroupInfo = {
label: string
slots: number
scope: WorkerGroupScope
}
type WorkerPoolCapacity = {
groups: WorkerGroupInfo[]
sharedSlots: number
}
type OnDisconnectParams = {
workerId: string
}