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.6 KB
Raw Blame
import os from 'os'
import path from 'path'
import { ActivepiecesError, ErrorCode, isNil } from '@activepieces/core-utils'
import { ApEdition, DefaultProjectRole, ExecutionMode, FileLocation, NetworkMode, PieceSyncMode } from '@activepieces/shared'
import { FastifyBaseLogger } from 'fastify'
import { DatabaseType } from '../../database/database-type'
import { RedisType } from '../../database/redis/types'
import { pinoLogging } from '../logger'
import { AppSystemProp, ContainerType, environmentVariables, SystemProp } from './system-props'



const systemPropDefaultValues: Partial<Record<SystemProp, string>> = {
    [AppSystemProp.API_RATE_LIMIT_AUTHN_ENABLED]: 'true',
    [AppSystemProp.API_RATE_LIMIT_AUTHN_MAX]: '50',
    [AppSystemProp.API_RATE_LIMIT_AUTHN_WINDOW]: '1 minute',
    [AppSystemProp.API_RATE_LIMIT_EMAIL_CODE_MAX]: '5',
    [AppSystemProp.WORKERS]: '1',
    [AppSystemProp.CLIENT_REAL_IP_HEADER]: 'x-real-ip',
    [AppSystemProp.CLOUD_AUTH_ENABLED]: 'true',
    [AppSystemProp.CONFIG_PATH]: path.join(os.homedir(), '.activepieces'),
    [AppSystemProp.DB_TYPE]: DatabaseType.POSTGRES,
    [AppSystemProp.EDITION]: ApEdition.COMMUNITY,
    [AppSystemProp.APP_WEBHOOK_SECRETS]: '{}',
    [AppSystemProp.AUTUMN_CONSOLE_URL]: 'https://console.activepieces.com',
    [AppSystemProp.CONTAINER_TYPE]: ContainerType.WORKER_AND_APP,
    [AppSystemProp.PORT]: '3000',
    [AppSystemProp.EXECUTION_DATA_RETENTION_DAYS]: '30',
    [AppSystemProp.PAUSED_FLOW_TIMEOUT_DAYS]: '30',
    [AppSystemProp.PIECES_SYNC_MODE]: PieceSyncMode.OFFICIAL_AUTO,
    [AppSystemProp.USE_CDN_FOR_BUNDLES]: 'false',
    [AppSystemProp.ENVIRONMENT]: 'prod',
    [AppSystemProp.EXECUTION_MODE]: ExecutionMode.UNSANDBOXED,
    [AppSystemProp.WEBHOOK_TIMEOUT_SECONDS]: '30',
    [AppSystemProp.LOAD_TRANSLATIONS_FOR_DEV_PIECES]: 'false',
    [AppSystemProp.LOG_LEVEL]: 'info',
    [AppSystemProp.LOG_PRETTY]: 'false',
    [AppSystemProp.S3_USE_SIGNED_URLS]: 'false',
    [AppSystemProp.MAX_BARRIER_SIGNALS]: '10000',
    [AppSystemProp.MAX_FILE_SIZE_MB]: '25',
    [AppSystemProp.AI_CREDIT_USD_VALUE]: '0.0005',
    [AppSystemProp.MAX_FLOW_RUN_LOG_SIZE_MB]: '50',
    [AppSystemProp.FLOW_RUN_LOG_INPUT_TRUNCATE_THRESHOLD_KB]: '2',
    [AppSystemProp.FLOW_RUN_LOG_SLICE_THRESHOLD_KB]: '32',
    [AppSystemProp.MAX_WEBHOOK_PAYLOAD_SIZE_MB]: '25',
    [AppSystemProp.WEBHOOK_PAYLOAD_INLINE_THRESHOLD_KB]: '512',
    [AppSystemProp.FILE_STORAGE_LOCATION]: FileLocation.DB,
    [AppSystemProp.SANDBOX_MEMORY_LIMIT]: '1048576',
    [AppSystemProp.FLOW_TIMEOUT_SECONDS]: '600',
    [AppSystemProp.TRIGGER_TIMEOUT_SECONDS]: '60',
    [AppSystemProp.RUNS_METADATA_UPDATE_CONCURRENCY]: '2',
    [AppSystemProp.TRIGGER_HOOKS_TIMEOUT_SECONDS]: '180',
    [AppSystemProp.EVENT_DESTINATION_TIMEOUT_SECONDS]: '10',
    [AppSystemProp.REDIS_FAILED_JOB_RETENTION_DAYS]: '30',
    [AppSystemProp.REDIS_FAILED_JOB_RETENTION_MAX_COUNT]: '100000',
    [AppSystemProp.TELEMETRY_ENABLED]: 'true',
    [AppSystemProp.REDIS_TYPE]: RedisType.STANDALONE,
    [AppSystemProp.TRIGGER_DEFAULT_POLL_INTERVAL]: '5',
    [AppSystemProp.DEFAULT_CONCURRENT_JOBS_LIMIT]: '5',
    [AppSystemProp.PROJECT_RATE_LIMITER_ENABLED]: 'false',
    [AppSystemProp.MAX_RECORDS_PER_TABLE]: '10000',
    [AppSystemProp.MAX_FIELDS_PER_TABLE]: '100',
    [AppSystemProp.ENABLE_FLOW_ON_PUBLISH]: 'true',
    [AppSystemProp.ENFORCE_CONNECTION_PIECE_BINDING]: 'false',
    [AppSystemProp.ISSUE_ARCHIVE_DAYS]: '7',
    [AppSystemProp.POSTGRES_IDLE_TIMEOUT_MS]: '300000',
    [AppSystemProp.SCIM_DEFAULT_PROJECT_ROLE]: DefaultProjectRole.EDITOR,
    [AppSystemProp.NETWORK_MODE]: NetworkMode.UNRESTRICTED,
    [AppSystemProp.LOG_SAMPLE_RATE_INFO]: '100',
    [AppSystemProp.LOG_KEEP_SLOW_MS]: '2000',
}

let globalLogger: FastifyBaseLogger
export const system = {
    globalLogger(): FastifyBaseLogger {
        if (isNil(globalLogger)) {
            globalLogger = pinoLogging.initLogger()
        }
        return globalLogger
    },
    get<T extends string>(prop: SystemProp): T | undefined {
        return getEnvVarOrReturnDefaultValue(prop) as T | undefined
    },

    getNumberOrThrow(prop: SystemProp): number {
        const value = system.getNumber(prop)

        if (isNil(value)) {
            throw new ActivepiecesError(
                {
                    code: ErrorCode.SYSTEM_PROP_NOT_DEFINED,
                    params: {
                        prop,
                    },
                },
                `System property AP_${prop} is not defined, please check the documentation`,
            )
        }
        return value

    },
    getDecimalOrThrow(prop: SystemProp): number {
        const stringNumber = getEnvVarOrReturnDefaultValue(prop)
        const parsedNumber = isNil(stringNumber) || stringNumber === '' ? Number.NaN : Number(stringNumber)

        if (!Number.isFinite(parsedNumber)) {
            throw new ActivepiecesError(
                {
                    code: ErrorCode.SYSTEM_PROP_NOT_DEFINED,
                    params: {
                        prop,
                    },
                },
                `System property AP_${prop} is not a finite number, please check the documentation`,
            )
        }
        return parsedNumber
    },
    getNumber(prop: SystemProp): number | null {
        const stringNumber = getEnvVarOrReturnDefaultValue(prop)

        if (!stringNumber) {
            return null
        }

        const parsedNumber = Number.parseInt(stringNumber, 10)

        if (Number.isNaN(parsedNumber)) {
            return null
        }

        return parsedNumber
    },

    getBoolean(prop: SystemProp): boolean | undefined {
        const value = getEnvVarOrReturnDefaultValue(prop)

        if (isNil(value)) {
            return undefined
        }
        return value === 'true'
    },

    getBooleanOrThrow(prop: SystemProp): boolean {
        const value = this.getBoolean(prop)
        if (isNil(value)) {
            throw new ActivepiecesError(
                {
                    code: ErrorCode.SYSTEM_PROP_NOT_DEFINED,
                    params: {
                        prop,
                    },
                },
                `System property AP_${prop} is not defined, please check the documentation`,
            )
        }
        return value
    },
    getList(prop: SystemProp): string[] {
        const values = getEnvVarOrReturnDefaultValue(prop)

        if (isNil(values)) {
            return []
        }
        return values.split(',').map((value) => value.trim())
    },
    getOrThrow<T extends string>(prop: SystemProp): T {
        const value = getEnvVarOrReturnDefaultValue(prop) as T | undefined

        if (value === undefined) {
            throw new ActivepiecesError(
                {
                    code: ErrorCode.SYSTEM_PROP_NOT_DEFINED,
                    params: {
                        prop,
                    },
                },
                `System property AP_${prop} is not defined, please check the documentation`,
            )
        }

        return value
    },
    getEdition(): ApEdition {
        return this.getOrThrow<ApEdition>(AppSystemProp.EDITION)
    },
    isWorker(): boolean {
        return [ContainerType.WORKER, ContainerType.WORKER_AND_APP].includes(
            this.getOrThrow<ContainerType>(AppSystemProp.CONTAINER_TYPE),
        )
    },
    isApp(): boolean {
        return [ContainerType.APP, ContainerType.WORKER_AND_APP].includes(
            this.getOrThrow<ContainerType>(AppSystemProp.CONTAINER_TYPE),
        )
    },
}

const getEnvVarOrReturnDefaultValue = (prop: SystemProp): string | undefined => {
    return environmentVariables.getEnvironment(prop) ?? systemPropDefaultValues[prop]
}