Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

9.1 KB
Raw Blame
import { isDevEnv } from '~/common/utils/env-utils'

import { CommonConfig } from './common/config'
import { PluginServerCapabilities, PluginServerMode, stringToPluginServerMode } from './types'

// =============================================================================
// Capability Groups for Local Development
// These can be combined via hogli dev:setup to reduce event loop contention and memory overhead
// =============================================================================

/** CDP - destinations, webhooks, and realtime alerts */
export const CAPABILITIES_CDP: PluginServerCapabilities = {
    cdpProcessedEvents: true,
    cdpPersonUpdates: true,
    cdpInternalEvents: true,
    cdpCyclotronWorker: true,
    cdpApi: true,
    appManagementSingleton: true,
    cdpDataWarehouseEvents: false, // Not yet fully developed - enable when ready
    cdpLegacyOnEvent: false, // most of the times not needed
    cdpRerunWorker: true,
}

/** CDP + Workflows - full CDP with HogFlow workflow automation */
export const CAPABILITIES_CDP_WORKFLOWS: PluginServerCapabilities = {
    ...CAPABILITIES_CDP,
    cdpCyclotronWorkerBatchResolve: true,
    cdpCyclotronWorkerHogFlow: true,
    cdpCyclotronWorkerEmail: true,
    cdpCyclotronV2Janitor: isDevEnv(),
    cdpHogflowScheduler: isDevEnv(),
    cdpHogflowSubscriptionMatcher: isDevEnv(),
    cdpCohortMembershipTriggers: true,
}

/** Realtime Cohorts - cohort membership persistence */
export const CAPABILITIES_REALTIME_COHORTS: PluginServerCapabilities = {
    cdpCohortMembership: true,
}

/** Error Tracking - exception event ingestion */
export const CAPABILITIES_ERROR_TRACKING: PluginServerCapabilities = {
    errorTrackingIngestion: true,
}

/** Feature Flags - evaluation scheduler for flags and experiments */
export const CAPABILITIES_FEATURE_FLAGS: PluginServerCapabilities = {
    evaluationScheduler: true,
}

// =============================================================================
// Helper to merge capability groups
// =============================================================================

function mergeCapabilities(...groups: PluginServerCapabilities[]): PluginServerCapabilities {
    return Object.assign({}, ...groups)
}

// =============================================================================
// Main capability resolution
// =============================================================================

/** Map of capability group names to their capabilities */
const CAPABILITY_GROUP_MAP: Record<string, PluginServerCapabilities> = {
    cdp: CAPABILITIES_CDP,
    cdp_workflows: CAPABILITIES_CDP_WORKFLOWS,
    realtime_cohorts: CAPABILITIES_REALTIME_COHORTS,
    feature_flags: CAPABILITIES_FEATURE_FLAGS,
}

export function getPluginServerCapabilities(
    config: Pick<CommonConfig, 'PLUGIN_SERVER_MODE' | 'NODEJS_CAPABILITY_GROUPS'>
): PluginServerCapabilities {
    const mode: PluginServerMode | null = config.PLUGIN_SERVER_MODE
        ? stringToPluginServerMode[config.PLUGIN_SERVER_MODE]
        : null

    switch (mode) {
        // Local development modes - composable groups
        case null:
            // Check if specific capability groups are requested via env var
            if (config.NODEJS_CAPABILITY_GROUPS && config.NODEJS_CAPABILITY_GROUPS.trim()) {
                const requestedGroups = config.NODEJS_CAPABILITY_GROUPS.split(',').map((g) => g.trim())
                const capabilities: PluginServerCapabilities[] = []

                for (const group of requestedGroups) {
                    const groupCapabilities = CAPABILITY_GROUP_MAP[group]
                    if (groupCapabilities) {
                        capabilities.push(groupCapabilities)
                    } else {
                        console.warn(`Unknown Node.js capability group: ${group}`)
                    }
                }

                return mergeCapabilities(...capabilities)
            }

            // Default local dev: run everything except ingestion, recordings, logs, and traces (they run in separate processes)
            return mergeCapabilities(
                CAPABILITIES_CDP_WORKFLOWS,
                CAPABILITIES_REALTIME_COHORTS,
                CAPABILITIES_ERROR_TRACKING,
                CAPABILITIES_FEATURE_FLAGS
            )

        case PluginServerMode.local_cdp:
            // Local CDP development: CDP + workflows + realtime cohorts (ingestion runs separately)
            return mergeCapabilities(CAPABILITIES_CDP_WORKFLOWS, CAPABILITIES_REALTIME_COHORTS)

        // Production modes - granular control for dedicated pods
        case PluginServerMode.cdp_processed_events:
            return {
                cdpProcessedEvents: true,
            }
        case PluginServerMode.cdp_person_updates:
            return {
                cdpPersonUpdates: true,
            }
        case PluginServerMode.cdp_internal_events:
            return {
                cdpInternalEvents: true,
            }
        case PluginServerMode.cdp_cyclotron_worker:
            return {
                cdpCyclotronWorker: true,
            }
        case PluginServerMode.cdp_cyclotron_worker_hogflow:
            return {
                cdpCyclotronWorkerHogFlow: true,
            }
        case PluginServerMode.cdp_cyclotron_worker_email:
            return {
                cdpCyclotronWorkerEmail: true,
            }
        case PluginServerMode.cdp_precalculated_filters:
            // The consumer is gone. Boot with no capabilities so a pod that charts still
            // launches in this mode idles instead of crash-looping on an unknown mode.
            return {}
        case PluginServerMode.cdp_hogflow_subscription_matcher:
            return {
                cdpHogflowSubscriptionMatcher: true,
            }
        case PluginServerMode.cdp_cohort_membership:
            return {
                cdpCohortMembership: true,
            }
        case PluginServerMode.cdp_cohort_membership_triggers:
            return {
                cdpCohortMembershipTriggers: true,
            }
        case PluginServerMode.cdp_legacy_on_event:
            return {
                cdpLegacyOnEvent: true,
            }
        case PluginServerMode.cdp_api:
            return {
                cdpApi: true,
                // NOTE: This is temporary until we have removed plugins
                appManagementSingleton: true,
            }
        case PluginServerMode.evaluation_scheduler:
            return {
                evaluationScheduler: true,
            }
        case PluginServerMode.ingestion_logs:
        case PluginServerMode.ingestion_metrics:
        case PluginServerMode.ingestion_traces:
            throw new Error(`Mode ${mode} is handled by a dedicated server, not PluginServer`)
        case PluginServerMode.ingestion_error_tracking:
            return {
                errorTrackingIngestion: true,
            }
        case PluginServerMode.cdp_cyclotron_worker_batch_resolve:
            return {
                cdpCyclotronWorkerBatchResolve: true,
            }
        case PluginServerMode.cdp_data_warehouse_events:
            return {
                cdpDataWarehouseEvents: true,
            }
        case PluginServerMode.cdp_cyclotron_v2_janitor:
            return {
                cdpCyclotronV2Janitor: true,
            }
        case PluginServerMode.cdp_rerun_worker:
            return {
                cdpRerunWorker: true,
            }
        // Deliberately absent from CAPABILITIES_CDP: a replay is a bounded, operator-triggered run,
        // not something a local or combined stack should start on its own.
        case PluginServerMode.cdp_dlq_replay:
            return {
                cdpDlqReplay: true,
            }
        case PluginServerMode.ingestion_v2:
        case PluginServerMode.ingestion_v2_combined:
            throw new Error(`Mode ${mode} is handled by IngestionGeneralServer, not PluginServer`)
        case PluginServerMode.ingestion_api:
            throw new Error(`Mode ${mode} is handled by IngestionApiServer, not PluginServer`)
        case PluginServerMode.push_api:
            throw new Error(`Mode ${mode} is handled by PushApiServer, not PluginServer`)
        case PluginServerMode.cdp_hogflow_scheduler:
            return {
                cdpHogflowScheduler: true,
            }
        case PluginServerMode.recordings_blob_ingestion_v2:
        case PluginServerMode.recordings_blob_ingestion_v2_overflow:
        case PluginServerMode.recordings_blob_ingestion_v2_ml_mirror:
        case PluginServerMode.recordings_blob_ingestion_v2_ml_parquet_sink:
        case PluginServerMode.recordings_blob_ingestion_v2_ml_image_scrub:
        case PluginServerMode.recordings_blob_ingestion_v2_ml_image_scrub_dlq_replay:
        case PluginServerMode.recordings_blob_ingestion_v2_ml_image_fetch:
        case PluginServerMode.recordings_blob_ingestion_v2_ml_image_fetch_retry:
        case PluginServerMode.ai_research_v4_image_fetch:
        case PluginServerMode.ai_research_v4_image_fetch_retry:
        case PluginServerMode.ai_research_v4_parquet_sink:
        case PluginServerMode.recording_api:
            throw new Error(`Mode ${mode} is handled by IngestionSessionReplayServer, not PluginServer`)
    }
}