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.

4.3 KB
Raw Blame
import { randomUUID } from 'crypto'

import { HogFlow, HogFlowAction, HogFlowEdge } from '~/cdp/schema/hogflow'
import { findActionByType } from '~/cdp/services/hogflows/hogflow-utils'
import { logger } from '~/common/utils/logger'

import { HOG_FILTERS_EXAMPLES } from '../examples'

/**
 * Helps us build like this
    actions: {
        'action-1': {
            type: 'trigger',
            config: {
                filters: {},
            },
        },
        'action-2': {
            type: 'delay',
            config: {
                delay_duration: '1h',
            },
        },
    },
    edges: {
        'edge-1': {
            from: 'action-1',
            to: 'action-2',
            type: 'continue',
        },
        'edge-2': {
            from: 'action-2',
            to: 'action-1',
            type: 'continue',
        },
    }
 */
export type SimpleHogFlowRepresentation = {
    actions: Record<string, Pick<HogFlowAction, 'type' | 'config'> & Partial<Omit<HogFlowAction, 'type' | 'config'>>>
    edges: HogFlowEdge[]
}

export class FixtureHogFlowBuilder {
    private hogFlow: HogFlow

    constructor() {
        this.hogFlow = {
            id: randomUUID(),
            version: 1,
            name: 'Hog Flow',
            team_id: 1,
            status: 'active',
            trigger: undefined as any,
            exit_condition: 'exit_on_conversion',
            edges: [],
            actions: [],
        }
    }

    build(): HogFlow {
        if (this.hogFlow.actions.length === 0) {
            this.withSimpleWorkflow()
        }
        const triggerAction = findActionByType(this.hogFlow, 'trigger')
        this.hogFlow.trigger = this.hogFlow.trigger ?? (triggerAction ? triggerAction.config : undefined)

        if (!this.hogFlow.trigger) {
            logger.error('[HogFlowBuilder] No trigger action found. Indicates a faulty built workflow')
        }

        // Compute billable_action_types based on actions
        const billableTypes = new Set(['function', 'function_email', 'function_sms', 'function_push'])
        const uniqueBillableTypes = new Set<string>()

        for (const action of this.hogFlow.actions) {
            if (billableTypes.has(action.type)) {
                uniqueBillableTypes.add(action.type)
            }
        }

        this.hogFlow.billable_action_types = Array.from(uniqueBillableTypes).sort()

        return this.hogFlow
    }

    withName(name: string): this {
        this.hogFlow.name = name
        return this
    }

    withTeamId(teamId: number): this {
        this.hogFlow.team_id = teamId
        return this
    }

    withStatus(status: HogFlow['status']): this {
        this.hogFlow.status = status
        return this
    }

    withExitCondition(exitCondition: HogFlow['exit_condition']): this {
        this.hogFlow.exit_condition = exitCondition
        return this
    }

    withWorkflow(workflow: SimpleHogFlowRepresentation): this {
        this.hogFlow.actions = Object.entries(workflow.actions).map(([id, action]) => ({
            id,
            name: action.type,
            description: action.type,
            created_at: Date.now(),
            updated_at: Date.now(),
            on_error: 'continue',
            ...(action as any), // TRICKY: Nasty cast as the union types are beyond me get right
        }))

        this.hogFlow.edges = workflow.edges

        return this
    }

    withConversion(conversion: HogFlow['conversion']): this {
        this.hogFlow.conversion = conversion
        return this
    }

    withTriggerMasking(triggerMasking: HogFlow['trigger_masking']): this {
        this.hogFlow.trigger_masking = triggerMasking
        return this
    }

    withSimpleWorkflow({ trigger }: { trigger?: HogFlow['trigger'] } = {}): this {
        return this.withWorkflow({
            actions: {
                trigger: {
                    type: 'trigger',
                    config: trigger ?? {
                        type: 'event',
                        filters: HOG_FILTERS_EXAMPLES.no_filters.filters ?? {},
                    },
                },
                exit: {
                    type: 'exit',
                    config: {},
                },
            },
            edges: [
                {
                    from: 'trigger',
                    to: 'exit',
                    type: 'continue',
                },
            ],
        })
    }
}